blob: cc70510cb8e85ac2f9eb90ae578c3cda979cac9f [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)
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +0200858static void ssl_calc_verify_ssl( const mbedtls_ssl_context *, unsigned char *, size_t * );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200859static 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)
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +0200863static void ssl_calc_verify_tls( const mbedtls_ssl_context *, unsigned char *, size_t * );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200864static 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 );
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +0200870static void ssl_calc_verify_tls_sha256( const mbedtls_ssl_context *,unsigned char *, size_t * );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200871static 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 );
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +0200876static void ssl_calc_verify_tls_sha384( const mbedtls_ssl_context *, unsigned char *, size_t * );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200877static 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é-Gonnard9b108c22019-05-06 13:32:17 +0200980/* Type for the TLS PRF */
981typedef int ssl_tls_prf_t(const unsigned char *, size_t, const char *,
982 const unsigned char *, size_t,
983 unsigned char *, size_t);
984
Manuel Pégourié-Gonnarde59ae232019-04-30 11:41:40 +0200985/*
Manuel Pégourié-Gonnardcba40d92019-05-06 12:55:40 +0200986 * Populate a transform structure with session keys and all the other
987 * necessary information.
Manuel Pégourié-Gonnarde59ae232019-04-30 11:41:40 +0200988 *
Manuel Pégourié-Gonnardcba40d92019-05-06 12:55:40 +0200989 * Parameters:
990 * - [in/out]: transform: structure to populate
991 * [in] must be just initialised with mbedtls_ssl_transform_init()
Manuel Pégourié-Gonnardc864f6a2019-05-06 13:48:22 +0200992 * [out] fully populated, ready for use by mbedtls_ssl_{en,de}crypt_buf()
Manuel Pégourié-Gonnard6fa57bf2019-05-10 10:50:04 +0200993 * - [in] ciphersuite
994 * - [in] master
995 * - [in] encrypt_then_mac
996 * - [in] trunc_hmac
997 * - [in] compression
Manuel Pégourié-Gonnard9b108c22019-05-06 13:32:17 +0200998 * - [in] tls_prf: pointer to PRF to use for key derivation
999 * - [in] randbytes: buffer holding ServerHello.random + ClientHello.random
Manuel Pégourié-Gonnardc864f6a2019-05-06 13:48:22 +02001000 * - [in] minor_ver: SSL/TLS minor version
1001 * - [in] endpoint: client or server
1002 * - [in] ssl: optionally used for:
1003 * - MBEDTLS_SSL_HW_RECORD_ACCEL: whole context
1004 * - MBEDTLS_SSL_EXPORT_KEYS: ssl->conf->{f,p}_export_keys
1005 * - MBEDTLS_DEBUG_C: ssl->conf->{f,p}_dbg
Manuel Pégourié-Gonnarde59ae232019-04-30 11:41:40 +02001006 */
Manuel Pégourié-Gonnardcba40d92019-05-06 12:55:40 +02001007static int ssl_populate_transform( mbedtls_ssl_transform *transform,
Manuel Pégourié-Gonnard6fa57bf2019-05-10 10:50:04 +02001008 int ciphersuite,
1009 const unsigned char master[48],
1010#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1011 int encrypt_then_mac,
1012#endif
1013#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
1014 int trunc_hmac,
1015#endif
1016#if defined(MBEDTLS_ZLIB_SUPPORT)
1017 int compression,
1018#endif
Manuel Pégourié-Gonnard9b108c22019-05-06 13:32:17 +02001019 ssl_tls_prf_t tls_prf,
1020 const unsigned char randbytes[64],
Manuel Pégourié-Gonnardc864f6a2019-05-06 13:48:22 +02001021 int minor_ver,
1022 unsigned endpoint,
Manuel Pégourié-Gonnardcba40d92019-05-06 12:55:40 +02001023 const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00001024{
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001025 int ret = 0;
Hanno Beckercb1cc802018-11-17 22:27:38 +00001026#if defined(MBEDTLS_USE_PSA_CRYPTO)
1027 int psa_fallthrough;
1028#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker5121ce52009-01-03 21:22:43 +00001029 unsigned char keyblk[256];
1030 unsigned char *key1;
1031 unsigned char *key2;
Paul Bakker68884e32013-01-07 18:20:04 +01001032 unsigned char *mac_enc;
1033 unsigned char *mac_dec;
Hanno Becker81c7b182017-11-09 18:39:33 +00001034 size_t mac_key_len;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001035 size_t iv_copy_len;
Hanno Becker88aaf652017-12-27 08:17:40 +00001036 unsigned keylen;
Hanno Beckere694c3e2017-12-27 21:34:08 +00001037 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001038 const mbedtls_cipher_info_t *cipher_info;
1039 const mbedtls_md_info_t *md_info;
Paul Bakker68884e32013-01-07 18:20:04 +01001040
Manuel Pégourié-Gonnardc864f6a2019-05-06 13:48:22 +02001041#if !defined(MBEDTLS_SSL_HW_RECORD_ACCEL) && \
1042 !defined(MBEDTLS_SSL_EXPORT_KEYS) && \
1043 !defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnarda7505d12019-05-07 10:17:56 +02001044 ssl = NULL; /* make sure we don't use it except for those cases */
Manuel Pégourié-Gonnardc864f6a2019-05-06 13:48:22 +02001045 (void) ssl;
1046#endif
1047
Manuel Pégourié-Gonnard9b108c22019-05-06 13:32:17 +02001048 /* Copy info about negotiated version and extensions */
Jaeden Amero2de07f12019-06-05 13:32:08 +01001049#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) && \
1050 defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard6fa57bf2019-05-10 10:50:04 +02001051 transform->encrypt_then_mac = encrypt_then_mac;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001052#endif
Manuel Pégourié-Gonnardc864f6a2019-05-06 13:48:22 +02001053 transform->minor_ver = minor_ver;
Hanno Beckere694c3e2017-12-27 21:34:08 +00001054
Manuel Pégourié-Gonnard9b108c22019-05-06 13:32:17 +02001055 /*
1056 * Get various info structures
1057 */
Manuel Pégourié-Gonnard6fa57bf2019-05-10 10:50:04 +02001058 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( ciphersuite );
Manuel Pégourié-Gonnard9b108c22019-05-06 13:32:17 +02001059 if( ciphersuite_info == NULL )
1060 {
1061 MBEDTLS_SSL_DEBUG_MSG( 1, ( "ciphersuite info for %d not found",
Manuel Pégourié-Gonnard6fa57bf2019-05-10 10:50:04 +02001062 ciphersuite ) );
Manuel Pégourié-Gonnard9b108c22019-05-06 13:32:17 +02001063 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1064 }
1065
Hanno Beckere694c3e2017-12-27 21:34:08 +00001066 cipher_info = mbedtls_cipher_info_from_type( ciphersuite_info->cipher );
Paul Bakker68884e32013-01-07 18:20:04 +01001067 if( cipher_info == NULL )
1068 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001069 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %d not found",
Hanno Beckere694c3e2017-12-27 21:34:08 +00001070 ciphersuite_info->cipher ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001071 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +01001072 }
1073
Hanno Beckere694c3e2017-12-27 21:34:08 +00001074 md_info = mbedtls_md_info_from_type( ciphersuite_info->mac );
Paul Bakker68884e32013-01-07 18:20:04 +01001075 if( md_info == NULL )
1076 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001077 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %d not found",
Hanno Beckere694c3e2017-12-27 21:34:08 +00001078 ciphersuite_info->mac ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001079 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +01001080 }
1081
Hanno Beckera0e20d02019-05-15 14:03:01 +01001082#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker4bf74652019-04-26 16:22:27 +01001083 /* Copy own and peer's CID if the use of the CID
1084 * extension has been negotiated. */
1085 if( ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_ENABLED )
1086 {
1087 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Copy CIDs into SSL transform" ) );
Hanno Becker8a7f9722019-04-30 13:52:29 +01001088
Hanno Becker05154c32019-05-03 15:23:51 +01001089 transform->in_cid_len = ssl->own_cid_len;
Hanno Becker05154c32019-05-03 15:23:51 +01001090 memcpy( transform->in_cid, ssl->own_cid, ssl->own_cid_len );
Hanno Becker1c1f0462019-05-03 12:55:51 +01001091 MBEDTLS_SSL_DEBUG_BUF( 3, "Incoming CID", transform->in_cid,
Hanno Becker4bf74652019-04-26 16:22:27 +01001092 transform->in_cid_len );
Hanno Beckerd1f20352019-05-15 10:21:55 +01001093
1094 transform->out_cid_len = ssl->handshake->peer_cid_len;
1095 memcpy( transform->out_cid, ssl->handshake->peer_cid,
1096 ssl->handshake->peer_cid_len );
1097 MBEDTLS_SSL_DEBUG_BUF( 3, "Outgoing CID", transform->out_cid,
1098 transform->out_cid_len );
Hanno Becker4bf74652019-04-26 16:22:27 +01001099 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01001100#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker4bf74652019-04-26 16:22:27 +01001101
Paul Bakker5121ce52009-01-03 21:22:43 +00001102 /*
Manuel Pégourié-Gonnard9b108c22019-05-06 13:32:17 +02001103 * Compute key block using the PRF
Paul Bakker5121ce52009-01-03 21:22:43 +00001104 */
Manuel Pégourié-Gonnard6fa57bf2019-05-10 10:50:04 +02001105 ret = tls_prf( master, 48, "key expansion", randbytes, 64, keyblk, 256 );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +01001106 if( ret != 0 )
1107 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001108 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +01001109 return( ret );
1110 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001111
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001112 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
Manuel Pégourié-Gonnardd91efa42019-05-20 10:27:20 +02001113 mbedtls_ssl_get_ciphersuite_name( ciphersuite ) ) );
Manuel Pégourié-Gonnard6fa57bf2019-05-10 10:50:04 +02001114 MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", master, 48 );
Manuel Pégourié-Gonnard9b108c22019-05-06 13:32:17 +02001115 MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", randbytes, 64 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001116 MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001117
Paul Bakker5121ce52009-01-03 21:22:43 +00001118 /*
1119 * Determine the appropriate key, IV and MAC length.
1120 */
Paul Bakker68884e32013-01-07 18:20:04 +01001121
Hanno Becker88aaf652017-12-27 08:17:40 +00001122 keylen = cipher_info->key_bitlen / 8;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001123
Hanno Becker8031d062018-01-03 15:32:31 +00001124#if defined(MBEDTLS_GCM_C) || \
1125 defined(MBEDTLS_CCM_C) || \
1126 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001127 if( cipher_info->mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001128 cipher_info->mode == MBEDTLS_MODE_CCM ||
1129 cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakker5121ce52009-01-03 21:22:43 +00001130 {
Hanno Beckerf704bef2018-11-16 15:21:18 +00001131 size_t explicit_ivlen;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001132
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001133 transform->maclen = 0;
Hanno Becker81c7b182017-11-09 18:39:33 +00001134 mac_key_len = 0;
Hanno Beckere694c3e2017-12-27 21:34:08 +00001135 transform->taglen =
1136 ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001137
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001138 /* All modes haves 96-bit IVs;
1139 * GCM and CCM has 4 implicit and 8 explicit bytes
1140 * ChachaPoly has all 12 bytes implicit
1141 */
Paul Bakker68884e32013-01-07 18:20:04 +01001142 transform->ivlen = 12;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001143 if( cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
1144 transform->fixed_ivlen = 12;
1145 else
1146 transform->fixed_ivlen = 4;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001147
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001148 /* Minimum length of encrypted record */
1149 explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
Hanno Beckere694c3e2017-12-27 21:34:08 +00001150 transform->minlen = explicit_ivlen + transform->taglen;
Paul Bakker68884e32013-01-07 18:20:04 +01001151 }
1152 else
Hanno Becker8031d062018-01-03 15:32:31 +00001153#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
1154#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
1155 if( cipher_info->mode == MBEDTLS_MODE_STREAM ||
1156 cipher_info->mode == MBEDTLS_MODE_CBC )
Paul Bakker68884e32013-01-07 18:20:04 +01001157 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001158 /* Initialize HMAC contexts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001159 if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 ||
1160 ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 )
Paul Bakker68884e32013-01-07 18:20:04 +01001161 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001162 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001163 goto end;
Paul Bakker68884e32013-01-07 18:20:04 +01001164 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001165
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001166 /* Get MAC length */
Hanno Becker81c7b182017-11-09 18:39:33 +00001167 mac_key_len = mbedtls_md_get_size( md_info );
1168 transform->maclen = mac_key_len;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001169
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001170#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001171 /*
1172 * If HMAC is to be truncated, we shall keep the leftmost bytes,
1173 * (rfc 6066 page 13 or rfc 2104 section 4),
1174 * so we only need to adjust the length here.
1175 */
Manuel Pégourié-Gonnard6fa57bf2019-05-10 10:50:04 +02001176 if( trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_ENABLED )
Hanno Beckere89353a2017-11-20 16:36:41 +00001177 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001178 transform->maclen = MBEDTLS_SSL_TRUNCATED_HMAC_LEN;
Hanno Beckere89353a2017-11-20 16:36:41 +00001179
1180#if defined(MBEDTLS_SSL_TRUNCATED_HMAC_COMPAT)
1181 /* Fall back to old, non-compliant version of the truncated
Hanno Becker563423f2017-11-21 17:20:17 +00001182 * HMAC implementation which also truncates the key
1183 * (Mbed TLS versions from 1.3 to 2.6.0) */
Hanno Beckere89353a2017-11-20 16:36:41 +00001184 mac_key_len = transform->maclen;
1185#endif
1186 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001187#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001188
1189 /* IV length */
Paul Bakker68884e32013-01-07 18:20:04 +01001190 transform->ivlen = cipher_info->iv_size;
Paul Bakker5121ce52009-01-03 21:22:43 +00001191
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001192 /* Minimum length */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001193 if( cipher_info->mode == MBEDTLS_MODE_STREAM )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001194 transform->minlen = transform->maclen;
1195 else
Paul Bakker68884e32013-01-07 18:20:04 +01001196 {
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001197 /*
1198 * GenericBlockCipher:
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +01001199 * 1. if EtM is in use: one block plus MAC
1200 * otherwise: * first multiple of blocklen greater than maclen
1201 * 2. IV except for SSL3 and TLS 1.0
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001202 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001203#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6fa57bf2019-05-10 10:50:04 +02001204 if( encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +01001205 {
1206 transform->minlen = transform->maclen
1207 + cipher_info->block_size;
1208 }
1209 else
1210#endif
1211 {
1212 transform->minlen = transform->maclen
1213 + cipher_info->block_size
1214 - transform->maclen % cipher_info->block_size;
1215 }
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001216
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001217#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Manuel Pégourié-Gonnardc864f6a2019-05-06 13:48:22 +02001218 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ||
1219 minor_ver == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001220 ; /* No need to adjust minlen */
Paul Bakker68884e32013-01-07 18:20:04 +01001221 else
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001222#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001223#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnardc864f6a2019-05-06 13:48:22 +02001224 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_2 ||
1225 minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001226 {
1227 transform->minlen += transform->ivlen;
1228 }
1229 else
1230#endif
1231 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001232 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001233 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1234 goto end;
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001235 }
Paul Bakker68884e32013-01-07 18:20:04 +01001236 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001237 }
Hanno Becker8031d062018-01-03 15:32:31 +00001238 else
1239#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
1240 {
1241 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1242 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1243 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001244
Hanno Becker88aaf652017-12-27 08:17:40 +00001245 MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %u, minlen: %u, ivlen: %u, maclen: %u",
1246 (unsigned) keylen,
1247 (unsigned) transform->minlen,
1248 (unsigned) transform->ivlen,
1249 (unsigned) transform->maclen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001250
1251 /*
1252 * Finally setup the cipher contexts, IVs and MAC secrets.
1253 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001254#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnardc864f6a2019-05-06 13:48:22 +02001255 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00001256 {
Hanno Becker81c7b182017-11-09 18:39:33 +00001257 key1 = keyblk + mac_key_len * 2;
Hanno Becker88aaf652017-12-27 08:17:40 +00001258 key2 = keyblk + mac_key_len * 2 + keylen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001259
Paul Bakker68884e32013-01-07 18:20:04 +01001260 mac_enc = keyblk;
Hanno Becker81c7b182017-11-09 18:39:33 +00001261 mac_dec = keyblk + mac_key_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001262
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001263 /*
1264 * This is not used in TLS v1.1.
1265 */
Paul Bakker48916f92012-09-16 19:57:18 +00001266 iv_copy_len = ( transform->fixed_ivlen ) ?
1267 transform->fixed_ivlen : transform->ivlen;
Hanno Becker88aaf652017-12-27 08:17:40 +00001268 memcpy( transform->iv_enc, key2 + keylen, iv_copy_len );
1269 memcpy( transform->iv_dec, key2 + keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +00001270 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001271 }
1272 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001273#endif /* MBEDTLS_SSL_CLI_C */
1274#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardc864f6a2019-05-06 13:48:22 +02001275 if( endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00001276 {
Hanno Becker88aaf652017-12-27 08:17:40 +00001277 key1 = keyblk + mac_key_len * 2 + keylen;
Hanno Becker81c7b182017-11-09 18:39:33 +00001278 key2 = keyblk + mac_key_len * 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00001279
Hanno Becker81c7b182017-11-09 18:39:33 +00001280 mac_enc = keyblk + mac_key_len;
Paul Bakker68884e32013-01-07 18:20:04 +01001281 mac_dec = keyblk;
Paul Bakker5121ce52009-01-03 21:22:43 +00001282
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001283 /*
1284 * This is not used in TLS v1.1.
1285 */
Paul Bakker48916f92012-09-16 19:57:18 +00001286 iv_copy_len = ( transform->fixed_ivlen ) ?
1287 transform->fixed_ivlen : transform->ivlen;
Hanno Becker88aaf652017-12-27 08:17:40 +00001288 memcpy( transform->iv_dec, key1 + keylen, iv_copy_len );
1289 memcpy( transform->iv_enc, key1 + keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +00001290 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001291 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001292 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001293#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001294 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001295 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001296 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1297 goto end;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001298 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001299
Hanno Beckerd56ed242018-01-03 15:32:51 +00001300#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001301#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardc864f6a2019-05-06 13:48:22 +02001302 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker68884e32013-01-07 18:20:04 +01001303 {
Hanno Beckerd56ed242018-01-03 15:32:51 +00001304 if( mac_key_len > sizeof( transform->mac_enc ) )
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001305 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001306 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001307 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1308 goto end;
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001309 }
1310
Hanno Becker81c7b182017-11-09 18:39:33 +00001311 memcpy( transform->mac_enc, mac_enc, mac_key_len );
1312 memcpy( transform->mac_dec, mac_dec, mac_key_len );
Paul Bakker68884e32013-01-07 18:20:04 +01001313 }
1314 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001315#endif /* MBEDTLS_SSL_PROTO_SSL3 */
1316#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1317 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnardc864f6a2019-05-06 13:48:22 +02001318 if( minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakker68884e32013-01-07 18:20:04 +01001319 {
Gilles Peskine039fd122018-03-19 19:06:08 +01001320 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
1321 For AEAD-based ciphersuites, there is nothing to do here. */
1322 if( mac_key_len != 0 )
1323 {
1324 mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, mac_key_len );
1325 mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, mac_key_len );
1326 }
Paul Bakker68884e32013-01-07 18:20:04 +01001327 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001328 else
1329#endif
Paul Bakker577e0062013-08-28 11:57:20 +02001330 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001331 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001332 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1333 goto end;
Paul Bakker577e0062013-08-28 11:57:20 +02001334 }
Hanno Beckerd56ed242018-01-03 15:32:51 +00001335#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker68884e32013-01-07 18:20:04 +01001336
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001337#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
1338 if( mbedtls_ssl_hw_record_init != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00001339 {
1340 int ret = 0;
1341
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001342 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_init()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00001343
Hanno Becker88aaf652017-12-27 08:17:40 +00001344 if( ( ret = mbedtls_ssl_hw_record_init( ssl, key1, key2, keylen,
Paul Bakker07eb38b2012-12-19 14:42:06 +01001345 transform->iv_enc, transform->iv_dec,
1346 iv_copy_len,
Paul Bakker68884e32013-01-07 18:20:04 +01001347 mac_enc, mac_dec,
Hanno Becker81c7b182017-11-09 18:39:33 +00001348 mac_key_len ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00001349 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001350 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_init", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001351 ret = MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
1352 goto end;
Paul Bakker05ef8352012-05-08 09:17:57 +00001353 }
1354 }
Hanno Beckerd56ed242018-01-03 15:32:51 +00001355#else
1356 ((void) mac_dec);
1357 ((void) mac_enc);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001358#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00001359
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001360#if defined(MBEDTLS_SSL_EXPORT_KEYS)
1361 if( ssl->conf->f_export_keys != NULL )
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001362 {
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001363 ssl->conf->f_export_keys( ssl->conf->p_export_keys,
Manuel Pégourié-Gonnard6fa57bf2019-05-10 10:50:04 +02001364 master, keyblk,
Hanno Becker88aaf652017-12-27 08:17:40 +00001365 mac_key_len, keylen,
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001366 iv_copy_len );
1367 }
Ron Eldorf5cc10d2019-05-07 18:33:40 +03001368
1369 if( ssl->conf->f_export_keys_ext != NULL )
1370 {
1371 ssl->conf->f_export_keys_ext( ssl->conf->p_export_keys,
Manuel Pégourié-Gonnard6fa57bf2019-05-10 10:50:04 +02001372 master, keyblk,
Ron Eldorb7fd64c2019-05-12 11:03:32 +03001373 mac_key_len, keylen,
Ron Eldor51d3ab52019-05-12 14:54:30 +03001374 iv_copy_len,
Manuel Pégourié-Gonnard344460c2019-07-25 13:17:38 +02001375 /* work around bug in exporter type */
Manuel Pégourié-Gonnard9b108c22019-05-06 13:32:17 +02001376 (unsigned char *) randbytes + 32,
1377 (unsigned char *) randbytes,
1378 tls_prf_get_type( tls_prf ) );
Ron Eldorf5cc10d2019-05-07 18:33:40 +03001379 }
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001380#endif
1381
Hanno Beckerf704bef2018-11-16 15:21:18 +00001382#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Beckercb1cc802018-11-17 22:27:38 +00001383
1384 /* Only use PSA-based ciphers for TLS-1.2.
1385 * That's relevant at least for TLS-1.0, where
1386 * we assume that mbedtls_cipher_crypt() updates
1387 * the structure field for the IV, which the PSA-based
1388 * implementation currently doesn't. */
1389#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1390 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001391 {
Hanno Beckercb1cc802018-11-17 22:27:38 +00001392 ret = mbedtls_cipher_setup_psa( &transform->cipher_ctx_enc,
Hanno Becker22bf1452019-04-05 11:21:08 +01001393 cipher_info, transform->taglen );
Hanno Beckercb1cc802018-11-17 22:27:38 +00001394 if( ret != 0 && ret != MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE )
1395 {
1396 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup_psa", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001397 goto end;
Hanno Beckercb1cc802018-11-17 22:27:38 +00001398 }
1399
1400 if( ret == 0 )
1401 {
Hanno Becker4c8c7aa2019-04-10 09:25:41 +01001402 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Successfully setup PSA-based encryption cipher context" ) );
Hanno Beckercb1cc802018-11-17 22:27:38 +00001403 psa_fallthrough = 0;
1404 }
1405 else
1406 {
1407 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to setup PSA-based cipher context for record encryption - fall through to default setup." ) );
1408 psa_fallthrough = 1;
1409 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001410 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001411 else
Hanno Beckercb1cc802018-11-17 22:27:38 +00001412 psa_fallthrough = 1;
1413#else
1414 psa_fallthrough = 1;
1415#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Beckerf704bef2018-11-16 15:21:18 +00001416
Hanno Beckercb1cc802018-11-17 22:27:38 +00001417 if( psa_fallthrough == 1 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001418#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001419 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001420 cipher_info ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001421 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001422 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001423 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001424 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001425
Hanno Beckerf704bef2018-11-16 15:21:18 +00001426#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Beckercb1cc802018-11-17 22:27:38 +00001427 /* Only use PSA-based ciphers for TLS-1.2.
1428 * That's relevant at least for TLS-1.0, where
1429 * we assume that mbedtls_cipher_crypt() updates
1430 * the structure field for the IV, which the PSA-based
1431 * implementation currently doesn't. */
1432#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1433 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001434 {
Hanno Beckercb1cc802018-11-17 22:27:38 +00001435 ret = mbedtls_cipher_setup_psa( &transform->cipher_ctx_dec,
Hanno Becker22bf1452019-04-05 11:21:08 +01001436 cipher_info, transform->taglen );
Hanno Beckercb1cc802018-11-17 22:27:38 +00001437 if( ret != 0 && ret != MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE )
1438 {
1439 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup_psa", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001440 goto end;
Hanno Beckercb1cc802018-11-17 22:27:38 +00001441 }
1442
1443 if( ret == 0 )
1444 {
Hanno Becker4c8c7aa2019-04-10 09:25:41 +01001445 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Successfully setup PSA-based decryption cipher context" ) );
Hanno Beckercb1cc802018-11-17 22:27:38 +00001446 psa_fallthrough = 0;
1447 }
1448 else
1449 {
1450 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to setup PSA-based cipher context for record decryption - fall through to default setup." ) );
1451 psa_fallthrough = 1;
1452 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001453 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001454 else
Hanno Beckercb1cc802018-11-17 22:27:38 +00001455 psa_fallthrough = 1;
1456#else
1457 psa_fallthrough = 1;
1458#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Beckerf704bef2018-11-16 15:21:18 +00001459
Hanno Beckercb1cc802018-11-17 22:27:38 +00001460 if( psa_fallthrough == 1 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001461#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001462 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001463 cipher_info ) ) != 0 )
1464 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001465 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001466 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001467 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001468
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001469 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001470 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001471 MBEDTLS_ENCRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001472 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001473 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001474 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001475 }
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001476
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001477 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001478 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001479 MBEDTLS_DECRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001480 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001481 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001482 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001483 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001484
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001485#if defined(MBEDTLS_CIPHER_MODE_CBC)
1486 if( cipher_info->mode == MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001487 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001488 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc,
1489 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001490 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001491 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001492 goto end;
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001493 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001494
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001495 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec,
1496 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001497 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001498 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001499 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001500 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001501 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001502#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001503
Paul Bakker5121ce52009-01-03 21:22:43 +00001504
Manuel Pégourié-Gonnardd73b47f2019-05-06 12:44:24 +02001505 /* Initialize Zlib contexts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001506#if defined(MBEDTLS_ZLIB_SUPPORT)
Manuel Pégourié-Gonnard6fa57bf2019-05-10 10:50:04 +02001507 if( compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001508 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001509 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Initializing zlib states" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001510
Paul Bakker48916f92012-09-16 19:57:18 +00001511 memset( &transform->ctx_deflate, 0, sizeof( transform->ctx_deflate ) );
1512 memset( &transform->ctx_inflate, 0, sizeof( transform->ctx_inflate ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001513
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001514 if( deflateInit( &transform->ctx_deflate,
1515 Z_DEFAULT_COMPRESSION ) != Z_OK ||
Paul Bakker48916f92012-09-16 19:57:18 +00001516 inflateInit( &transform->ctx_inflate ) != Z_OK )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001517 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001518 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001519 ret = MBEDTLS_ERR_SSL_COMPRESSION_FAILED;
1520 goto end;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001521 }
1522 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001523#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00001524
Ron Eldore6992702019-05-07 18:27:13 +03001525end:
Ron Eldora9f9a732019-05-07 18:29:02 +03001526 mbedtls_platform_zeroize( keyblk, sizeof( keyblk ) );
Ron Eldore6992702019-05-07 18:27:13 +03001527 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001528}
1529
Manuel Pégourié-Gonnard8d2805c2019-04-30 12:08:59 +02001530/*
Manuel Pégourié-Gonnard47e33e12019-05-20 10:10:17 +02001531 * Set appropriate PRF function and other SSL / TLS 1.0/1.1 / TLS1.2 functions
Manuel Pégourié-Gonnard8d2805c2019-04-30 12:08:59 +02001532 *
1533 * Inputs:
1534 * - SSL/TLS minor version
1535 * - hash associated with the ciphersuite (only used by TLS 1.2)
1536 *
Manuel Pégourié-Gonnard31d3ef12019-05-10 10:25:00 +02001537 * Outputs:
Manuel Pégourié-Gonnard8d2805c2019-04-30 12:08:59 +02001538 * - the tls_prf, calc_verify and calc_finished members of handshake structure
1539 */
1540static int ssl_set_handshake_prfs( mbedtls_ssl_handshake_params *handshake,
1541 int minor_ver,
1542 mbedtls_md_type_t hash )
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001543{
Manuel Pégourié-Gonnard8d2805c2019-04-30 12:08:59 +02001544#if !defined(MBEDTLS_SSL_PROTO_TLS1_2) || !defined(MBEDTLS_SHA512_C)
1545 (void) hash;
1546#endif
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001547
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001548#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnard8d2805c2019-04-30 12:08:59 +02001549 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001550 {
1551 handshake->tls_prf = ssl3_prf;
1552 handshake->calc_verify = ssl_calc_verify_ssl;
1553 handshake->calc_finished = ssl_calc_finished_ssl;
1554 }
1555 else
1556#endif
1557#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Manuel Pégourié-Gonnard8d2805c2019-04-30 12:08:59 +02001558 if( minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001559 {
1560 handshake->tls_prf = tls1_prf;
1561 handshake->calc_verify = ssl_calc_verify_tls;
1562 handshake->calc_finished = ssl_calc_finished_tls;
1563 }
1564 else
1565#endif
1566#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1567#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard8d2805c2019-04-30 12:08:59 +02001568 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
1569 hash == MBEDTLS_MD_SHA384 )
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001570 {
1571 handshake->tls_prf = tls_prf_sha384;
1572 handshake->calc_verify = ssl_calc_verify_tls_sha384;
1573 handshake->calc_finished = ssl_calc_finished_tls_sha384;
1574 }
1575 else
1576#endif
1577#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnard8d2805c2019-04-30 12:08:59 +02001578 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001579 {
1580 handshake->tls_prf = tls_prf_sha256;
1581 handshake->calc_verify = ssl_calc_verify_tls_sha256;
1582 handshake->calc_finished = ssl_calc_finished_tls_sha256;
1583 }
1584 else
1585#endif
1586#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
1587 {
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001588 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1589 }
1590
1591 return( 0 );
1592}
1593
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001594/*
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001595 * Compute master secret if needed
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001596 *
1597 * Parameters:
1598 * [in/out] handshake
1599 * [in] resume, premaster, extended_ms, calc_verify, tls_prf
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001600 * (PSA-PSK) ciphersuite_info, psk_opaque
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001601 * [out] premaster (cleared)
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001602 * [out] master
1603 * [in] ssl: optionally used for debugging, EMS and PSA-PSK
1604 * debug: conf->f_dbg, conf->p_dbg
1605 * EMS: passed to calc_verify (debug + (SSL3) session_negotiate)
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001606 * PSA-PSA: minor_ver, conf
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001607 */
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001608static int ssl_compute_master( mbedtls_ssl_handshake_params *handshake,
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001609 unsigned char *master,
Manuel Pégourié-Gonnard0d56aaa2019-05-03 09:58:33 +02001610 const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001611{
1612 int ret;
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001613
1614 /* cf. RFC 5246, Section 8.1:
1615 * "The master secret is always exactly 48 bytes in length." */
1616 size_t const master_secret_len = 48;
1617
1618#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
1619 unsigned char session_hash[48];
1620#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
1621
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001622 /* The label for the KDF used for key expansion.
1623 * This is either "master secret" or "extended master secret"
1624 * depending on whether the Extended Master Secret extension
1625 * is used. */
1626 char const *lbl = "master secret";
1627
1628 /* The salt for the KDF used for key expansion.
1629 * - If the Extended Master Secret extension is not used,
1630 * this is ClientHello.Random + ServerHello.Random
1631 * (see Sect. 8.1 in RFC 5246).
1632 * - If the Extended Master Secret extension is used,
1633 * this is the transcript of the handshake so far.
1634 * (see Sect. 4 in RFC 7627). */
1635 unsigned char const *salt = handshake->randbytes;
1636 size_t salt_len = 64;
1637
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001638#if !defined(MBEDTLS_DEBUG_C) && \
1639 !defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) && \
1640 !(defined(MBEDTLS_USE_PSA_CRYPTO) && \
1641 defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED))
Manuel Pégourié-Gonnarda7505d12019-05-07 10:17:56 +02001642 ssl = NULL; /* make sure we don't use it except for those cases */
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001643 (void) ssl;
1644#endif
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001645
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001646 if( handshake->resume != 0 )
1647 {
1648 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001649 return( 0 );
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001650 }
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001651
1652#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001653 if( handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED )
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001654 {
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001655 lbl = "extended master secret";
1656 salt = session_hash;
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001657 handshake->calc_verify( ssl, session_hash, &salt_len );
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001658
Manuel Pégourié-Gonnard8faa70e2019-05-20 12:09:50 +02001659 MBEDTLS_SSL_DEBUG_BUF( 3, "session hash for extended master secret",
1660 session_hash, salt_len );
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001661 }
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001662#endif /* MBEDTLS_SSL_EXTENDED_MS_ENABLED */
1663
1664#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001665 defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
1666 if( handshake->ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK &&
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001667 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001668 ssl_use_opaque_psk( ssl ) == 1 )
1669 {
1670 /* Perform PSK-to-MS expansion in a single step. */
1671 psa_status_t status;
1672 psa_algorithm_t alg;
1673 psa_key_handle_t psk;
1674 psa_key_derivation_operation_t derivation =
1675 PSA_KEY_DERIVATION_OPERATION_INIT;
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001676 mbedtls_md_type_t hash_alg = handshake->ciphersuite_info->mac;
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001677
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001678 MBEDTLS_SSL_DEBUG_MSG( 2, ( "perform PSA-based PSK-to-MS expansion" ) );
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001679
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001680 psk = ssl->conf->psk_opaque;
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001681 if( handshake->psk_opaque != 0 )
1682 psk = handshake->psk_opaque;
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001683
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001684 if( hash_alg == MBEDTLS_MD_SHA384 )
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001685 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384);
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001686 else
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001687 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256);
1688
1689 status = psa_key_derivation( &derivation, psk, alg,
1690 salt, salt_len,
1691 (unsigned char const *) lbl,
1692 (size_t) strlen( lbl ),
1693 master_secret_len );
1694 if( status != PSA_SUCCESS )
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001695 {
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001696 psa_key_derivation_abort( &derivation );
1697 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001698 }
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001699
1700 status = psa_key_derivation_output_bytes( &derivation,
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001701 master,
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001702 master_secret_len );
1703 if( status != PSA_SUCCESS )
1704 {
1705 psa_key_derivation_abort( &derivation );
1706 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
1707 }
1708
1709 status = psa_key_derivation_abort( &derivation );
1710 if( status != PSA_SUCCESS )
1711 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
1712 }
1713 else
1714#endif
1715 {
1716 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
1717 lbl, salt, salt_len,
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001718 master,
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001719 master_secret_len );
1720 if( ret != 0 )
1721 {
1722 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
1723 return( ret );
1724 }
1725
1726 MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret",
1727 handshake->premaster,
1728 handshake->pmslen );
1729
1730 mbedtls_platform_zeroize( handshake->premaster,
1731 sizeof(handshake->premaster) );
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001732 }
1733
1734 return( 0 );
1735}
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001736
Manuel Pégourié-Gonnarde59ae232019-04-30 11:41:40 +02001737int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl )
1738{
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001739 int ret;
Manuel Pégourié-Gonnard8d2805c2019-04-30 12:08:59 +02001740 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
1741 ssl->handshake->ciphersuite_info;
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001742
Manuel Pégourié-Gonnard040a9512019-05-06 12:05:58 +02001743 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
1744
1745 /* Set PRF, calc_verify and calc_finished function pointers */
Manuel Pégourié-Gonnard8d2805c2019-04-30 12:08:59 +02001746 ret = ssl_set_handshake_prfs( ssl->handshake,
1747 ssl->minor_ver,
1748 ciphersuite_info->mac );
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001749 if( ret != 0 )
Manuel Pégourié-Gonnard8d2805c2019-04-30 12:08:59 +02001750 {
1751 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_set_handshake_prfs", ret );
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001752 return( ret );
Manuel Pégourié-Gonnard8d2805c2019-04-30 12:08:59 +02001753 }
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001754
Manuel Pégourié-Gonnard040a9512019-05-06 12:05:58 +02001755 /* Compute master secret if needed */
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001756 ret = ssl_compute_master( ssl->handshake,
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001757 ssl->session_negotiate->master,
1758 ssl );
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001759 if( ret != 0 )
1760 {
1761 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compute_master", ret );
1762 return( ret );
1763 }
1764
Manuel Pégourié-Gonnard040a9512019-05-06 12:05:58 +02001765 /* Swap the client and server random values:
1766 * - MS derivation wanted client+server (RFC 5246 8.1)
1767 * - key derivation wants server+client (RFC 5246 6.3) */
1768 {
1769 unsigned char tmp[64];
1770 memcpy( tmp, ssl->handshake->randbytes, 64 );
1771 memcpy( ssl->handshake->randbytes, tmp + 32, 32 );
1772 memcpy( ssl->handshake->randbytes + 32, tmp, 32 );
1773 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
1774 }
1775
1776 /* Populate transform structure */
Manuel Pégourié-Gonnardcba40d92019-05-06 12:55:40 +02001777 ret = ssl_populate_transform( ssl->transform_negotiate,
Manuel Pégourié-Gonnard6fa57bf2019-05-10 10:50:04 +02001778 ssl->session_negotiate->ciphersuite,
1779 ssl->session_negotiate->master,
1780#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1781 ssl->session_negotiate->encrypt_then_mac,
1782#endif
1783#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
1784 ssl->session_negotiate->trunc_hmac,
1785#endif
1786#if defined(MBEDTLS_ZLIB_SUPPORT)
1787 ssl->session_negotiate->compression,
1788#endif
Manuel Pégourié-Gonnard9b108c22019-05-06 13:32:17 +02001789 ssl->handshake->tls_prf,
1790 ssl->handshake->randbytes,
Manuel Pégourié-Gonnardc864f6a2019-05-06 13:48:22 +02001791 ssl->minor_ver,
1792 ssl->conf->endpoint,
Manuel Pégourié-Gonnardcba40d92019-05-06 12:55:40 +02001793 ssl );
Manuel Pégourié-Gonnard040a9512019-05-06 12:05:58 +02001794 if( ret != 0 )
1795 {
1796 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_populate_transform", ret );
1797 return( ret );
1798 }
1799
1800 /* We no longer need Server/ClientHello.random values */
1801 mbedtls_platform_zeroize( ssl->handshake->randbytes,
1802 sizeof( ssl->handshake->randbytes ) );
1803
Manuel Pégourié-Gonnardd73b47f2019-05-06 12:44:24 +02001804 /* Allocate compression buffer */
1805#if defined(MBEDTLS_ZLIB_SUPPORT)
1806 if( session->compression == MBEDTLS_SSL_COMPRESS_DEFLATE &&
1807 ssl->compress_buf == NULL )
1808 {
1809 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Allocating compression buffer" ) );
1810 ssl->compress_buf = mbedtls_calloc( 1, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
1811 if( ssl->compress_buf == NULL )
1812 {
1813 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
Manuel Pégourié-Gonnardd91efa42019-05-20 10:27:20 +02001814 MBEDTLS_SSL_COMPRESS_BUFFER_LEN ) );
Manuel Pégourié-Gonnardd73b47f2019-05-06 12:44:24 +02001815 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
1816 }
1817 }
1818#endif
1819
Manuel Pégourié-Gonnard040a9512019-05-06 12:05:58 +02001820 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
1821
1822 return( 0 );
Manuel Pégourié-Gonnarde59ae232019-04-30 11:41:40 +02001823}
1824
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001825#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001826void ssl_calc_verify_ssl( const mbedtls_ssl_context *ssl,
1827 unsigned char hash[36],
1828 size_t *hlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001829{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001830 mbedtls_md5_context md5;
1831 mbedtls_sha1_context sha1;
Paul Bakker5121ce52009-01-03 21:22:43 +00001832 unsigned char pad_1[48];
1833 unsigned char pad_2[48];
1834
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001835 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001836
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001837 mbedtls_md5_init( &md5 );
1838 mbedtls_sha1_init( &sha1 );
1839
1840 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1841 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001842
Paul Bakker380da532012-04-18 16:10:25 +00001843 memset( pad_1, 0x36, 48 );
1844 memset( pad_2, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001845
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001846 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1847 mbedtls_md5_update_ret( &md5, pad_1, 48 );
1848 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001849
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001850 mbedtls_md5_starts_ret( &md5 );
1851 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1852 mbedtls_md5_update_ret( &md5, pad_2, 48 );
1853 mbedtls_md5_update_ret( &md5, hash, 16 );
1854 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001855
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001856 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1857 mbedtls_sha1_update_ret( &sha1, pad_1, 40 );
1858 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001859
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001860 mbedtls_sha1_starts_ret( &sha1 );
1861 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1862 mbedtls_sha1_update_ret( &sha1, pad_2, 40 );
1863 mbedtls_sha1_update_ret( &sha1, hash + 16, 20 );
1864 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001865
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001866 *hlen = 36;
1867
1868 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001869 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001870
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001871 mbedtls_md5_free( &md5 );
1872 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001873
Paul Bakker380da532012-04-18 16:10:25 +00001874 return;
1875}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001876#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker380da532012-04-18 16:10:25 +00001877
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001878#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001879void ssl_calc_verify_tls( const mbedtls_ssl_context *ssl,
1880 unsigned char hash[36],
1881 size_t *hlen )
Paul Bakker380da532012-04-18 16:10:25 +00001882{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001883 mbedtls_md5_context md5;
1884 mbedtls_sha1_context sha1;
Paul Bakker380da532012-04-18 16:10:25 +00001885
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001886 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001887
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001888 mbedtls_md5_init( &md5 );
1889 mbedtls_sha1_init( &sha1 );
1890
1891 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1892 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker380da532012-04-18 16:10:25 +00001893
Andrzej Kurekeb342242019-01-29 09:14:33 -05001894 mbedtls_md5_finish_ret( &md5, hash );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001895 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001896
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001897 *hlen = 36;
1898
1899 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001900 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001901
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001902 mbedtls_md5_free( &md5 );
1903 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001904
Paul Bakker380da532012-04-18 16:10:25 +00001905 return;
1906}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001907#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker380da532012-04-18 16:10:25 +00001908
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001909#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1910#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001911void ssl_calc_verify_tls_sha256( const mbedtls_ssl_context *ssl,
1912 unsigned char hash[32],
1913 size_t *hlen )
Paul Bakker380da532012-04-18 16:10:25 +00001914{
Andrzej Kurekeb342242019-01-29 09:14:33 -05001915#if defined(MBEDTLS_USE_PSA_CRYPTO)
1916 size_t hash_size;
1917 psa_status_t status;
1918 psa_hash_operation_t sha256_psa = psa_hash_operation_init();
1919
1920 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha256" ) );
1921 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
1922 if( status != PSA_SUCCESS )
1923 {
1924 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
1925 return;
1926 }
1927
1928 status = psa_hash_finish( &sha256_psa, hash, 32, &hash_size );
1929 if( status != PSA_SUCCESS )
1930 {
1931 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
1932 return;
1933 }
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001934
1935 *hlen = 32;
1936 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, *hlen );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001937 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
1938#else
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001939 mbedtls_sha256_context sha256;
Paul Bakker380da532012-04-18 16:10:25 +00001940
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001941 mbedtls_sha256_init( &sha256 );
1942
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001943 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001944
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001945 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001946 mbedtls_sha256_finish_ret( &sha256, hash );
Paul Bakker380da532012-04-18 16:10:25 +00001947
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001948 *hlen = 32;
1949
1950 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001951 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001952
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001953 mbedtls_sha256_free( &sha256 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001954#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker380da532012-04-18 16:10:25 +00001955 return;
1956}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001957#endif /* MBEDTLS_SHA256_C */
Paul Bakker380da532012-04-18 16:10:25 +00001958
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001959#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001960void ssl_calc_verify_tls_sha384( const mbedtls_ssl_context *ssl,
1961 unsigned char hash[48],
1962 size_t *hlen )
Paul Bakker380da532012-04-18 16:10:25 +00001963{
Andrzej Kurekeb342242019-01-29 09:14:33 -05001964#if defined(MBEDTLS_USE_PSA_CRYPTO)
1965 size_t hash_size;
1966 psa_status_t status;
Andrzej Kurek972fba52019-01-30 03:29:12 -05001967 psa_hash_operation_t sha384_psa = psa_hash_operation_init();
Andrzej Kurekeb342242019-01-29 09:14:33 -05001968
1969 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha384" ) );
Andrzej Kurek972fba52019-01-30 03:29:12 -05001970 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001971 if( status != PSA_SUCCESS )
1972 {
1973 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
1974 return;
1975 }
1976
Andrzej Kurek972fba52019-01-30 03:29:12 -05001977 status = psa_hash_finish( &sha384_psa, hash, 48, &hash_size );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001978 if( status != PSA_SUCCESS )
1979 {
1980 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
1981 return;
1982 }
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001983
1984 *hlen = 48;
1985 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, *hlen );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001986 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
1987#else
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001988 mbedtls_sha512_context sha512;
Paul Bakker380da532012-04-18 16:10:25 +00001989
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001990 mbedtls_sha512_init( &sha512 );
1991
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001992 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001993
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001994 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001995 mbedtls_sha512_finish_ret( &sha512, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001996
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001997 *hlen = 48;
1998
1999 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002000 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002001
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02002002 mbedtls_sha512_free( &sha512 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05002003#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker5121ce52009-01-03 21:22:43 +00002004 return;
2005}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002006#endif /* MBEDTLS_SHA512_C */
2007#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002008
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002009#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
2010int 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 +02002011{
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002012 unsigned char *p = ssl->handshake->premaster;
2013 unsigned char *end = p + sizeof( ssl->handshake->premaster );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002014 const unsigned char *psk = ssl->conf->psk;
2015 size_t psk_len = ssl->conf->psk_len;
2016
2017 /* If the psk callback was called, use its result */
2018 if( ssl->handshake->psk != NULL )
2019 {
2020 psk = ssl->handshake->psk;
2021 psk_len = ssl->handshake->psk_len;
2022 }
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002023
2024 /*
2025 * PMS = struct {
2026 * opaque other_secret<0..2^16-1>;
2027 * opaque psk<0..2^16-1>;
2028 * };
2029 * with "other_secret" depending on the particular key exchange
2030 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002031#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
2032 if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002033 {
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02002034 if( end - p < 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002035 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002036
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002037 *(p++) = (unsigned char)( psk_len >> 8 );
2038 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02002039
2040 if( end < p || (size_t)( end - p ) < psk_len )
2041 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2042
2043 memset( p, 0, psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002044 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002045 }
2046 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002047#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
2048#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
2049 if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002050 {
2051 /*
2052 * other_secret already set by the ClientKeyExchange message,
2053 * and is 48 bytes long
2054 */
Philippe Antoine747fd532018-05-30 09:13:21 +02002055 if( end - p < 2 )
2056 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2057
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002058 *p++ = 0;
2059 *p++ = 48;
2060 p += 48;
2061 }
2062 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002063#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
2064#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
2065 if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002066 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02002067 int ret;
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01002068 size_t len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002069
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02002070 /* Write length only when we know the actual value */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002071 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01002072 p + 2, end - ( p + 2 ), &len,
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01002073 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002074 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002075 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002076 return( ret );
2077 }
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02002078 *(p++) = (unsigned char)( len >> 8 );
2079 *(p++) = (unsigned char)( len );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002080 p += len;
2081
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002082 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002083 }
2084 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002085#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
2086#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
2087 if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002088 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02002089 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002090 size_t zlen;
2091
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002092 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
Paul Bakker66d5d072014-06-17 16:39:18 +02002093 p + 2, end - ( p + 2 ),
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01002094 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002095 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002096 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002097 return( ret );
2098 }
2099
2100 *(p++) = (unsigned char)( zlen >> 8 );
2101 *(p++) = (unsigned char)( zlen );
2102 p += zlen;
2103
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002104 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
2105 MBEDTLS_DEBUG_ECDH_Z );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002106 }
2107 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002108#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002109 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002110 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2111 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002112 }
2113
2114 /* opaque psk<0..2^16-1>; */
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02002115 if( end - p < 2 )
2116 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01002117
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002118 *(p++) = (unsigned char)( psk_len >> 8 );
2119 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02002120
2121 if( end < p || (size_t)( end - p ) < psk_len )
2122 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2123
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002124 memcpy( p, psk, psk_len );
2125 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002126
2127 ssl->handshake->pmslen = p - ssl->handshake->premaster;
2128
2129 return( 0 );
2130}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002131#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002132
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002133#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002134/*
2135 * SSLv3.0 MAC functions
2136 */
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01002137#define SSL_MAC_MAX_BYTES 20 /* MD-5 or SHA-1 */
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01002138static void ssl_mac( mbedtls_md_context_t *md_ctx,
2139 const unsigned char *secret,
2140 const unsigned char *buf, size_t len,
2141 const unsigned char *ctr, int type,
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01002142 unsigned char out[SSL_MAC_MAX_BYTES] )
Paul Bakker5121ce52009-01-03 21:22:43 +00002143{
2144 unsigned char header[11];
2145 unsigned char padding[48];
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02002146 int padlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002147 int md_size = mbedtls_md_get_size( md_ctx->md_info );
2148 int md_type = mbedtls_md_get_type( md_ctx->md_info );
Paul Bakker68884e32013-01-07 18:20:04 +01002149
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02002150 /* Only MD5 and SHA-1 supported */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002151 if( md_type == MBEDTLS_MD_MD5 )
Paul Bakker68884e32013-01-07 18:20:04 +01002152 padlen = 48;
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02002153 else
Paul Bakker68884e32013-01-07 18:20:04 +01002154 padlen = 40;
Paul Bakker5121ce52009-01-03 21:22:43 +00002155
2156 memcpy( header, ctr, 8 );
2157 header[ 8] = (unsigned char) type;
2158 header[ 9] = (unsigned char)( len >> 8 );
2159 header[10] = (unsigned char)( len );
2160
Paul Bakker68884e32013-01-07 18:20:04 +01002161 memset( padding, 0x36, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002162 mbedtls_md_starts( md_ctx );
2163 mbedtls_md_update( md_ctx, secret, md_size );
2164 mbedtls_md_update( md_ctx, padding, padlen );
2165 mbedtls_md_update( md_ctx, header, 11 );
2166 mbedtls_md_update( md_ctx, buf, len );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01002167 mbedtls_md_finish( md_ctx, out );
Paul Bakker5121ce52009-01-03 21:22:43 +00002168
Paul Bakker68884e32013-01-07 18:20:04 +01002169 memset( padding, 0x5C, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002170 mbedtls_md_starts( md_ctx );
2171 mbedtls_md_update( md_ctx, secret, md_size );
2172 mbedtls_md_update( md_ctx, padding, padlen );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01002173 mbedtls_md_update( md_ctx, out, md_size );
2174 mbedtls_md_finish( md_ctx, out );
Paul Bakker5f70b252012-09-13 14:23:06 +00002175}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002176#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +00002177
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002178/* The function below is only used in the Lucky 13 counter-measure in
Hanno Beckerb2ca87d2018-10-18 15:43:13 +01002179 * mbedtls_ssl_decrypt_buf(). These are the defines that guard the call site. */
Hanno Becker52344c22018-01-03 15:24:20 +00002180#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) && \
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002181 ( defined(MBEDTLS_SSL_PROTO_TLS1) || \
2182 defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2183 defined(MBEDTLS_SSL_PROTO_TLS1_2) )
2184/* This function makes sure every byte in the memory region is accessed
2185 * (in ascending addresses order) */
2186static void ssl_read_memory( unsigned char *p, size_t len )
2187{
2188 unsigned char acc = 0;
2189 volatile unsigned char force;
2190
2191 for( ; len != 0; p++, len-- )
2192 acc ^= *p;
2193
2194 force = acc;
2195 (void) force;
2196}
2197#endif /* SSL_SOME_MODES_USE_MAC && ( TLS1 || TLS1_1 || TLS1_2 ) */
2198
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01002199/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002200 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +02002201 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002202
Hanno Beckera0e20d02019-05-15 14:03:01 +01002203#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerd3f8c792019-05-20 15:06:12 +01002204/* This functions transforms a DTLS plaintext fragment and a record content
2205 * type into an instance of the DTLSInnerPlaintext structure:
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002206 *
2207 * struct {
2208 * opaque content[DTLSPlaintext.length];
2209 * ContentType real_type;
2210 * uint8 zeros[length_of_padding];
2211 * } DTLSInnerPlaintext;
2212 *
2213 * Input:
2214 * - `content`: The beginning of the buffer holding the
2215 * plaintext to be wrapped.
2216 * - `*content_size`: The length of the plaintext in Bytes.
2217 * - `max_len`: The number of Bytes available starting from
2218 * `content`. This must be `>= *content_size`.
2219 * - `rec_type`: The desired record content type.
2220 *
2221 * Output:
2222 * - `content`: The beginning of the resulting DTLSInnerPlaintext structure.
2223 * - `*content_size`: The length of the resulting DTLSInnerPlaintext structure.
2224 *
2225 * Returns:
2226 * - `0` on success.
2227 * - A negative error code if `max_len` didn't offer enough space
2228 * for the expansion.
2229 */
2230static int ssl_cid_build_inner_plaintext( unsigned char *content,
2231 size_t *content_size,
2232 size_t remaining,
2233 uint8_t rec_type )
2234{
2235 size_t len = *content_size;
Hanno Beckerb9ec44f2019-05-13 15:31:17 +01002236 size_t pad = ( MBEDTLS_SSL_CID_PADDING_GRANULARITY -
2237 ( len + 1 ) % MBEDTLS_SSL_CID_PADDING_GRANULARITY ) %
2238 MBEDTLS_SSL_CID_PADDING_GRANULARITY;
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002239
2240 /* Write real content type */
2241 if( remaining == 0 )
2242 return( -1 );
2243 content[ len ] = rec_type;
2244 len++;
2245 remaining--;
2246
2247 if( remaining < pad )
2248 return( -1 );
2249 memset( content + len, 0, pad );
2250 len += pad;
2251 remaining -= pad;
2252
2253 *content_size = len;
2254 return( 0 );
2255}
2256
Hanno Becker07dc97d2019-05-20 15:08:01 +01002257/* This function parses a DTLSInnerPlaintext structure.
2258 * See ssl_cid_build_inner_plaintext() for details. */
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002259static int ssl_cid_parse_inner_plaintext( unsigned char const *content,
2260 size_t *content_size,
2261 uint8_t *rec_type )
2262{
2263 size_t remaining = *content_size;
2264
2265 /* Determine length of padding by skipping zeroes from the back. */
2266 do
2267 {
2268 if( remaining == 0 )
2269 return( -1 );
2270 remaining--;
2271 } while( content[ remaining ] == 0 );
2272
2273 *content_size = remaining;
2274 *rec_type = content[ remaining ];
2275
2276 return( 0 );
2277}
Hanno Beckera0e20d02019-05-15 14:03:01 +01002278#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002279
Hanno Beckerd5aeab12019-05-20 14:50:53 +01002280/* `add_data` must have size 13 Bytes if the CID extension is disabled,
Hanno Beckerc4a190b2019-05-08 18:15:21 +01002281 * and 13 + 1 + CID-length Bytes if the CID extension is enabled. */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002282static void ssl_extract_add_data_from_record( unsigned char* add_data,
Hanno Beckercab87e62019-04-29 13:52:53 +01002283 size_t *add_data_len,
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002284 mbedtls_record *rec )
2285{
Hanno Beckerd5aeab12019-05-20 14:50:53 +01002286 /* Quoting RFC 5246 (TLS 1.2):
Hanno Beckercab87e62019-04-29 13:52:53 +01002287 *
2288 * additional_data = seq_num + TLSCompressed.type +
2289 * TLSCompressed.version + TLSCompressed.length;
2290 *
Hanno Beckerd5aeab12019-05-20 14:50:53 +01002291 * For the CID extension, this is extended as follows
2292 * (quoting draft-ietf-tls-dtls-connection-id-05,
2293 * https://tools.ietf.org/html/draft-ietf-tls-dtls-connection-id-05):
Hanno Beckercab87e62019-04-29 13:52:53 +01002294 *
2295 * additional_data = seq_num + DTLSPlaintext.type +
2296 * DTLSPlaintext.version +
Hanno Beckerd5aeab12019-05-20 14:50:53 +01002297 * cid +
2298 * cid_length +
Hanno Beckercab87e62019-04-29 13:52:53 +01002299 * length_of_DTLSInnerPlaintext;
2300 */
2301
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002302 memcpy( add_data, rec->ctr, sizeof( rec->ctr ) );
2303 add_data[8] = rec->type;
Hanno Beckeredb24f82019-05-20 15:01:46 +01002304 memcpy( add_data + 9, rec->ver, sizeof( rec->ver ) );
Hanno Beckercab87e62019-04-29 13:52:53 +01002305
Hanno Beckera0e20d02019-05-15 14:03:01 +01002306#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker95e4bbc2019-05-09 11:38:24 +01002307 if( rec->cid_len != 0 )
2308 {
2309 memcpy( add_data + 11, rec->cid, rec->cid_len );
2310 add_data[11 + rec->cid_len + 0] = rec->cid_len;
2311 add_data[11 + rec->cid_len + 1] = ( rec->data_len >> 8 ) & 0xFF;
2312 add_data[11 + rec->cid_len + 2] = ( rec->data_len >> 0 ) & 0xFF;
2313 *add_data_len = 13 + 1 + rec->cid_len;
2314 }
2315 else
Hanno Beckera0e20d02019-05-15 14:03:01 +01002316#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker95e4bbc2019-05-09 11:38:24 +01002317 {
2318 add_data[11 + 0] = ( rec->data_len >> 8 ) & 0xFF;
2319 add_data[11 + 1] = ( rec->data_len >> 0 ) & 0xFF;
2320 *add_data_len = 13;
2321 }
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002322}
2323
Hanno Beckera18d1322018-01-03 14:27:32 +00002324int mbedtls_ssl_encrypt_buf( mbedtls_ssl_context *ssl,
2325 mbedtls_ssl_transform *transform,
2326 mbedtls_record *rec,
2327 int (*f_rng)(void *, unsigned char *, size_t),
2328 void *p_rng )
Paul Bakker5121ce52009-01-03 21:22:43 +00002329{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002330 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002331 int auth_done = 0;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002332 unsigned char * data;
Hanno Becker92fb4fa2019-05-20 14:54:26 +01002333 unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_OUT_LEN_MAX ];
Hanno Beckercab87e62019-04-29 13:52:53 +01002334 size_t add_data_len;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002335 size_t post_avail;
2336
2337 /* The SSL context is only used for debugging purposes! */
Hanno Beckera18d1322018-01-03 14:27:32 +00002338#if !defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnarda7505d12019-05-07 10:17:56 +02002339 ssl = NULL; /* make sure we don't use it except for debug */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002340 ((void) ssl);
2341#endif
2342
2343 /* The PRNG is used for dynamic IV generation that's used
2344 * for CBC transformations in TLS 1.1 and TLS 1.2. */
2345#if !( defined(MBEDTLS_CIPHER_MODE_CBC) && \
2346 ( defined(MBEDTLS_AES_C) || \
2347 defined(MBEDTLS_ARIA_C) || \
2348 defined(MBEDTLS_CAMELLIA_C) ) && \
2349 ( defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2) ) )
2350 ((void) f_rng);
2351 ((void) p_rng);
2352#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002353
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002354 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002355
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002356 if( transform == NULL )
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002357 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002358 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no transform provided to encrypt_buf" ) );
2359 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2360 }
Hanno Becker43c24b82019-05-01 09:45:57 +01002361 if( rec == NULL
2362 || rec->buf == NULL
2363 || rec->buf_len < rec->data_offset
2364 || rec->buf_len - rec->data_offset < rec->data_len
Hanno Beckera0e20d02019-05-15 14:03:01 +01002365#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker43c24b82019-05-01 09:45:57 +01002366 || rec->cid_len != 0
2367#endif
2368 )
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002369 {
2370 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to encrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002371 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002372 }
2373
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002374 data = rec->buf + rec->data_offset;
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002375 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002376 MBEDTLS_SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002377 data, rec->data_len );
2378
2379 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc );
2380
2381 if( rec->data_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
2382 {
2383 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record content %u too large, maximum %d",
2384 (unsigned) rec->data_len,
2385 MBEDTLS_SSL_OUT_CONTENT_LEN ) );
2386 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2387 }
Manuel Pégourié-Gonnard60346be2014-11-21 11:38:37 +01002388
Hanno Beckera0e20d02019-05-15 14:03:01 +01002389#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckercab87e62019-04-29 13:52:53 +01002390 /*
2391 * Add CID information
2392 */
2393 rec->cid_len = transform->out_cid_len;
2394 memcpy( rec->cid, transform->out_cid, transform->out_cid_len );
2395 MBEDTLS_SSL_DEBUG_BUF( 3, "CID", rec->cid, rec->cid_len );
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002396
2397 if( rec->cid_len != 0 )
2398 {
2399 /*
Hanno Becker07dc97d2019-05-20 15:08:01 +01002400 * Wrap plaintext into DTLSInnerPlaintext structure.
2401 * See ssl_cid_build_inner_plaintext() for more information.
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002402 *
Hanno Becker07dc97d2019-05-20 15:08:01 +01002403 * Note that this changes `rec->data_len`, and hence
2404 * `post_avail` needs to be recalculated afterwards.
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002405 */
2406 if( ssl_cid_build_inner_plaintext( data,
2407 &rec->data_len,
2408 post_avail,
2409 rec->type ) != 0 )
2410 {
2411 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2412 }
2413
2414 rec->type = MBEDTLS_SSL_MSG_CID;
2415 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01002416#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckercab87e62019-04-29 13:52:53 +01002417
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002418 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
2419
Paul Bakker5121ce52009-01-03 21:22:43 +00002420 /*
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01002421 * Add MAC before if needed
Paul Bakker5121ce52009-01-03 21:22:43 +00002422 */
Hanno Becker52344c22018-01-03 15:24:20 +00002423#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002424 if( mode == MBEDTLS_MODE_STREAM ||
2425 ( mode == MBEDTLS_MODE_CBC
2426#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002427 && transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002428#endif
2429 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00002430 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002431 if( post_avail < transform->maclen )
2432 {
2433 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2434 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2435 }
2436
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002437#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002438 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002439 {
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01002440 unsigned char mac[SSL_MAC_MAX_BYTES];
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002441 ssl_mac( &transform->md_ctx_enc, transform->mac_enc,
2442 data, rec->data_len, rec->ctr, rec->type, mac );
2443 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002444 }
2445 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002446#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002447#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2448 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002449 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002450 {
Hanno Becker992b6872017-11-09 18:57:39 +00002451 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
2452
Hanno Beckercab87e62019-04-29 13:52:53 +01002453 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker992b6872017-11-09 18:57:39 +00002454
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002455 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
Hanno Beckercab87e62019-04-29 13:52:53 +01002456 add_data_len );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002457 mbedtls_md_hmac_update( &transform->md_ctx_enc,
2458 data, rec->data_len );
2459 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
2460 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
2461
2462 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002463 }
2464 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002465#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002466 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002467 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2468 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002469 }
2470
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002471 MBEDTLS_SSL_DEBUG_BUF( 4, "computed mac", data + rec->data_len,
2472 transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002473
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002474 rec->data_len += transform->maclen;
2475 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002476 auth_done++;
Paul Bakker577e0062013-08-28 11:57:20 +02002477 }
Hanno Becker52344c22018-01-03 15:24:20 +00002478#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00002479
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002480 /*
2481 * Encrypt
2482 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002483#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
2484 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00002485 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002486 int ret;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002487 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002488 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002489 "including %d bytes of padding",
2490 rec->data_len, 0 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002491
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002492 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
2493 transform->iv_enc, transform->ivlen,
2494 data, rec->data_len,
2495 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002496 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002497 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002498 return( ret );
2499 }
2500
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002501 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002502 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002503 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2504 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002505 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002506 }
Paul Bakker68884e32013-01-07 18:20:04 +01002507 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002508#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002509
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002510#if defined(MBEDTLS_GCM_C) || \
2511 defined(MBEDTLS_CCM_C) || \
2512 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002513 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002514 mode == MBEDTLS_MODE_CCM ||
2515 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002516 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02002517 int ret;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002518 unsigned char iv[12];
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002519 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002520
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002521 /* Check that there's space for both the authentication tag
2522 * and the explicit IV before and after the record content. */
2523 if( post_avail < transform->taglen ||
2524 rec->data_offset < explicit_iv_len )
2525 {
2526 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2527 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2528 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00002529
Paul Bakker68884e32013-01-07 18:20:04 +01002530 /*
2531 * Generate IV
2532 */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002533 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
2534 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002535 /* GCM and CCM: fixed || explicit (=seqnum) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002536 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002537 memcpy( iv + transform->fixed_ivlen, rec->ctr,
2538 explicit_iv_len );
2539 /* Prefix record content with explicit IV. */
2540 memcpy( data - explicit_iv_len, rec->ctr, explicit_iv_len );
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002541 }
2542 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
2543 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002544 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002545 unsigned char i;
2546
2547 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
2548
2549 for( i = 0; i < 8; i++ )
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002550 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002551 }
2552 else
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01002553 {
2554 /* Reminder if we ever add an AEAD mode with a different size */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002555 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2556 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01002557 }
2558
Hanno Beckercab87e62019-04-29 13:52:53 +01002559 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker1f10d762019-04-26 13:34:37 +01002560
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002561 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (internal)",
2562 iv, transform->ivlen );
2563 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (transmitted)",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002564 data - explicit_iv_len, explicit_iv_len );
2565 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Hanno Beckercab87e62019-04-29 13:52:53 +01002566 add_data, add_data_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002567 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002568 "including 0 bytes of padding",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002569 rec->data_len ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00002570
Paul Bakker68884e32013-01-07 18:20:04 +01002571 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002572 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002573 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002574
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002575 if( ( ret = mbedtls_cipher_auth_encrypt( &transform->cipher_ctx_enc,
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002576 iv, transform->ivlen,
Hanno Beckercab87e62019-04-29 13:52:53 +01002577 add_data, add_data_len, /* add data */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002578 data, rec->data_len, /* source */
2579 data, &rec->data_len, /* destination */
2580 data + rec->data_len, transform->taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002581 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002582 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_encrypt", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002583 return( ret );
2584 }
2585
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002586 MBEDTLS_SSL_DEBUG_BUF( 4, "after encrypt: tag",
2587 data + rec->data_len, transform->taglen );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002588
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002589 rec->data_len += transform->taglen + explicit_iv_len;
2590 rec->data_offset -= explicit_iv_len;
2591 post_avail -= transform->taglen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002592 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002593 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002594 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002595#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2596#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002597 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002598 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00002599 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002600 int ret;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002601 size_t padlen, i;
2602 size_t olen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002603
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002604 /* Currently we're always using minimal padding
2605 * (up to 255 bytes would be allowed). */
2606 padlen = transform->ivlen - ( rec->data_len + 1 ) % transform->ivlen;
2607 if( padlen == transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002608 padlen = 0;
2609
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002610 /* Check there's enough space in the buffer for the padding. */
2611 if( post_avail < padlen + 1 )
2612 {
2613 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2614 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2615 }
2616
Paul Bakker5121ce52009-01-03 21:22:43 +00002617 for( i = 0; i <= padlen; i++ )
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002618 data[rec->data_len + i] = (unsigned char) padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002619
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002620 rec->data_len += padlen + 1;
2621 post_avail -= padlen + 1;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002622
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002623#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002624 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00002625 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
2626 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002627 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002628 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002629 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002630 if( f_rng == NULL )
2631 {
2632 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No PRNG provided to encrypt_record routine" ) );
2633 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2634 }
2635
2636 if( rec->data_offset < transform->ivlen )
2637 {
2638 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2639 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2640 }
2641
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002642 /*
2643 * Generate IV
2644 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002645 ret = f_rng( p_rng, transform->iv_enc, transform->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00002646 if( ret != 0 )
2647 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002648
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002649 memcpy( data - transform->ivlen, transform->iv_enc,
2650 transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002651
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002652 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002653#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002654
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002655 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002656 "including %d bytes of IV and %d bytes of padding",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002657 rec->data_len, transform->ivlen,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002658 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002659
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002660 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
2661 transform->iv_enc,
2662 transform->ivlen,
2663 data, rec->data_len,
2664 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002665 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002666 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002667 return( ret );
2668 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002669
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002670 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02002671 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002672 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2673 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002674 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002675
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002676#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002677 if( transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02002678 {
2679 /*
2680 * Save IV in SSL3 and TLS1
2681 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002682 memcpy( transform->iv_enc, transform->cipher_ctx_enc.iv,
2683 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002684 }
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002685 else
Paul Bakkercca5b812013-08-31 17:40:26 +02002686#endif
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002687 {
2688 data -= transform->ivlen;
2689 rec->data_offset -= transform->ivlen;
2690 rec->data_len += transform->ivlen;
2691 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002692
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002693#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002694 if( auth_done == 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002695 {
Hanno Becker3d8c9072018-01-05 16:24:22 +00002696 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
2697
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002698 /*
2699 * MAC(MAC_write_key, seq_num +
2700 * TLSCipherText.type +
2701 * TLSCipherText.version +
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002702 * length_of( (IV +) ENC(...) ) +
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002703 * IV + // except for TLS 1.0
2704 * ENC(content + padding + padding_length));
2705 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002706
2707 if( post_avail < transform->maclen)
2708 {
2709 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2710 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2711 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002712
Hanno Beckercab87e62019-04-29 13:52:53 +01002713 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker1f10d762019-04-26 13:34:37 +01002714
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002715 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002716 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
Hanno Beckercab87e62019-04-29 13:52:53 +01002717 add_data_len );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002718
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002719 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
Hanno Beckercab87e62019-04-29 13:52:53 +01002720 add_data_len );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002721 mbedtls_md_hmac_update( &transform->md_ctx_enc,
2722 data, rec->data_len );
2723 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
2724 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002725
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002726 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002727
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002728 rec->data_len += transform->maclen;
2729 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002730 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002731 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002732#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00002733 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002734 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002735#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002736 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002737 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002738 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2739 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002740 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002741
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002742 /* Make extra sure authentication was performed, exactly once */
2743 if( auth_done != 1 )
2744 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002745 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2746 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002747 }
2748
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002749 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002750
2751 return( 0 );
2752}
2753
Hanno Becker605949f2019-07-12 08:23:59 +01002754int mbedtls_ssl_decrypt_buf( mbedtls_ssl_context const *ssl,
Hanno Beckera18d1322018-01-03 14:27:32 +00002755 mbedtls_ssl_transform *transform,
2756 mbedtls_record *rec )
Paul Bakker5121ce52009-01-03 21:22:43 +00002757{
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002758 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002759 mbedtls_cipher_mode_t mode;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002760 int ret, auth_done = 0;
Hanno Becker52344c22018-01-03 15:24:20 +00002761#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Paul Bakker1e5369c2013-12-19 16:40:57 +01002762 size_t padlen = 0, correct = 1;
2763#endif
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002764 unsigned char* data;
Hanno Becker92fb4fa2019-05-20 14:54:26 +01002765 unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_IN_LEN_MAX ];
Hanno Beckercab87e62019-04-29 13:52:53 +01002766 size_t add_data_len;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002767
Hanno Beckera18d1322018-01-03 14:27:32 +00002768#if !defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnarda7505d12019-05-07 10:17:56 +02002769 ssl = NULL; /* make sure we don't use it except for debug */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002770 ((void) ssl);
2771#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002772
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002773 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002774 if( rec == NULL ||
2775 rec->buf == NULL ||
2776 rec->buf_len < rec->data_offset ||
2777 rec->buf_len - rec->data_offset < rec->data_len )
2778 {
2779 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to decrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002780 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002781 }
2782
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002783 data = rec->buf + rec->data_offset;
2784 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_dec );
Paul Bakker5121ce52009-01-03 21:22:43 +00002785
Hanno Beckera0e20d02019-05-15 14:03:01 +01002786#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckercab87e62019-04-29 13:52:53 +01002787 /*
2788 * Match record's CID with incoming CID.
2789 */
Hanno Becker938489a2019-05-08 13:02:22 +01002790 if( rec->cid_len != transform->in_cid_len ||
2791 memcmp( rec->cid, transform->in_cid, rec->cid_len ) != 0 )
2792 {
Hanno Becker8367ccc2019-05-14 11:30:10 +01002793 return( MBEDTLS_ERR_SSL_UNEXPECTED_CID );
Hanno Becker938489a2019-05-08 13:02:22 +01002794 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01002795#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckercab87e62019-04-29 13:52:53 +01002796
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002797#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
2798 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01002799 {
2800 padlen = 0;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002801 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
2802 transform->iv_dec,
2803 transform->ivlen,
2804 data, rec->data_len,
2805 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002806 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002807 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002808 return( ret );
2809 }
2810
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002811 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002812 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002813 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2814 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002815 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002816 }
Paul Bakker68884e32013-01-07 18:20:04 +01002817 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002818#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002819#if defined(MBEDTLS_GCM_C) || \
2820 defined(MBEDTLS_CCM_C) || \
2821 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002822 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002823 mode == MBEDTLS_MODE_CCM ||
2824 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002825 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002826 unsigned char iv[12];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002827 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002828
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002829 /*
Hanno Beckerd96a6522019-07-10 13:55:25 +01002830 * Prepare IV from explicit and implicit data.
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002831 */
Hanno Beckerd96a6522019-07-10 13:55:25 +01002832
2833 /* Check that there's enough space for the explicit IV
2834 * (at the beginning of the record) and the MAC (at the
2835 * end of the record). */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002836 if( rec->data_len < explicit_iv_len + transform->taglen )
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002837 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002838 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002839 "+ taglen (%d)", rec->data_len,
2840 explicit_iv_len, transform->taglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002841 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002842 }
Paul Bakker68884e32013-01-07 18:20:04 +01002843
Hanno Beckerd96a6522019-07-10 13:55:25 +01002844#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CCM_C)
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002845 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
2846 {
Hanno Beckerd96a6522019-07-10 13:55:25 +01002847 /* GCM and CCM: fixed || explicit */
Paul Bakker68884e32013-01-07 18:20:04 +01002848
Hanno Beckerd96a6522019-07-10 13:55:25 +01002849 /* Fixed */
2850 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
2851 /* Explicit */
2852 memcpy( iv + transform->fixed_ivlen, data, 8 );
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002853 }
Hanno Beckerd96a6522019-07-10 13:55:25 +01002854 else
2855#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2856#if defined(MBEDTLS_CHACHAPOLY_C)
2857 if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002858 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002859 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002860 unsigned char i;
2861
2862 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
2863
2864 for( i = 0; i < 8; i++ )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002865 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002866 }
2867 else
Hanno Beckerd96a6522019-07-10 13:55:25 +01002868#endif /* MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002869 {
2870 /* Reminder if we ever add an AEAD mode with a different size */
2871 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2872 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2873 }
2874
Hanno Beckerd96a6522019-07-10 13:55:25 +01002875 /* Group changes to data, data_len, and add_data, because
2876 * add_data depends on data_len. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002877 data += explicit_iv_len;
2878 rec->data_offset += explicit_iv_len;
2879 rec->data_len -= explicit_iv_len + transform->taglen;
2880
Hanno Beckercab87e62019-04-29 13:52:53 +01002881 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002882 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Hanno Beckercab87e62019-04-29 13:52:53 +01002883 add_data, add_data_len );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002884
Hanno Beckerd96a6522019-07-10 13:55:25 +01002885 /* Because of the check above, we know that there are
2886 * explicit_iv_len Bytes preceeding data, and taglen
2887 * bytes following data + data_len. This justifies
Hanno Becker20016652019-07-10 11:44:13 +01002888 * the debug message and the invocation of
Hanno Beckerd96a6522019-07-10 13:55:25 +01002889 * mbedtls_cipher_auth_decrypt() below. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002890
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002891 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used", iv, transform->ivlen );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002892 MBEDTLS_SSL_DEBUG_BUF( 4, "TAG used", data + rec->data_len,
Hanno Beckere694c3e2017-12-27 21:34:08 +00002893 transform->taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01002894
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002895 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002896 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002897 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002898 if( ( ret = mbedtls_cipher_auth_decrypt( &transform->cipher_ctx_dec,
2899 iv, transform->ivlen,
Hanno Beckercab87e62019-04-29 13:52:53 +01002900 add_data, add_data_len,
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002901 data, rec->data_len,
2902 data, &olen,
2903 data + rec->data_len,
2904 transform->taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002905 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002906 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_decrypt", ret );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002907
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002908 if( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED )
2909 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002910
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002911 return( ret );
2912 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002913 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002914
Hanno Beckerd96a6522019-07-10 13:55:25 +01002915 /* Double-check that AEAD decryption doesn't change content length. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002916 if( olen != rec->data_len )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002917 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002918 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2919 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002920 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00002921 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002922 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002923#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2924#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002925 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002926 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00002927 {
Paul Bakkere47b34b2013-02-27 14:48:00 +01002928 size_t minlen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002929
Paul Bakker5121ce52009-01-03 21:22:43 +00002930 /*
Paul Bakker45829992013-01-03 14:52:21 +01002931 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00002932 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002933#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002934 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
2935 {
2936 /* The ciphertext is prefixed with the CBC IV. */
2937 minlen += transform->ivlen;
2938 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002939#endif
Paul Bakker45829992013-01-03 14:52:21 +01002940
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002941 /* Size considerations:
2942 *
2943 * - The CBC cipher text must not be empty and hence
2944 * at least of size transform->ivlen.
2945 *
2946 * Together with the potential IV-prefix, this explains
2947 * the first of the two checks below.
2948 *
2949 * - The record must contain a MAC, either in plain or
2950 * encrypted, depending on whether Encrypt-then-MAC
2951 * is used or not.
2952 * - If it is, the message contains the IV-prefix,
2953 * the CBC ciphertext, and the MAC.
2954 * - If it is not, the padded plaintext, and hence
2955 * the CBC ciphertext, has at least length maclen + 1
2956 * because there is at least the padding length byte.
2957 *
2958 * As the CBC ciphertext is not empty, both cases give the
2959 * lower bound minlen + maclen + 1 on the record size, which
2960 * we test for in the second check below.
2961 */
2962 if( rec->data_len < minlen + transform->ivlen ||
2963 rec->data_len < minlen + transform->maclen + 1 )
Paul Bakker45829992013-01-03 14:52:21 +01002964 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002965 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002966 "+ 1 ) ( + expl IV )", rec->data_len,
2967 transform->ivlen,
2968 transform->maclen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002969 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker45829992013-01-03 14:52:21 +01002970 }
2971
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002972 /*
2973 * Authenticate before decrypt if enabled
2974 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002975#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002976 if( transform->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002977 {
Hanno Becker992b6872017-11-09 18:57:39 +00002978 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002979
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002980 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002981
Hanno Beckerd96a6522019-07-10 13:55:25 +01002982 /* Update data_len in tandem with add_data.
2983 *
2984 * The subtraction is safe because of the previous check
2985 * data_len >= minlen + maclen + 1.
2986 *
2987 * Afterwards, we know that data + data_len is followed by at
2988 * least maclen Bytes, which justifies the call to
2989 * mbedtls_ssl_safer_memcmp() below.
2990 *
2991 * Further, we still know that data_len > minlen */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002992 rec->data_len -= transform->maclen;
Hanno Beckercab87e62019-04-29 13:52:53 +01002993 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002994
Hanno Beckerd96a6522019-07-10 13:55:25 +01002995 /* Calculate expected MAC. */
Hanno Beckercab87e62019-04-29 13:52:53 +01002996 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
2997 add_data_len );
2998 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data,
2999 add_data_len );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003000 mbedtls_md_hmac_update( &transform->md_ctx_dec,
3001 data, rec->data_len );
3002 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
3003 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01003004
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003005 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len,
3006 transform->maclen );
Hanno Becker992b6872017-11-09 18:57:39 +00003007 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect,
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003008 transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003009
Hanno Beckerd96a6522019-07-10 13:55:25 +01003010 /* Compare expected MAC with MAC at the end of the record. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003011 if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect,
3012 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003013 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003014 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003015 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003016 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003017 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003018 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003019#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003020
3021 /*
3022 * Check length sanity
3023 */
Hanno Beckerd96a6522019-07-10 13:55:25 +01003024
3025 /* We know from above that data_len > minlen >= 0,
3026 * so the following check in particular implies that
3027 * data_len >= minlen + ivlen ( = minlen or 2 * minlen ). */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003028 if( rec->data_len % transform->ivlen != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003029 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003030 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003031 rec->data_len, transform->ivlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003032 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003033 }
3034
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003035#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003036 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00003037 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003038 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003039 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003040 {
Hanno Beckerd96a6522019-07-10 13:55:25 +01003041 /* Safe because data_len >= minlen + ivlen = 2 * ivlen. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003042 memcpy( transform->iv_dec, data, transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003043
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003044 data += transform->ivlen;
3045 rec->data_offset += transform->ivlen;
3046 rec->data_len -= transform->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003047 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003048#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003049
Hanno Beckerd96a6522019-07-10 13:55:25 +01003050 /* We still have data_len % ivlen == 0 and data_len >= ivlen here. */
3051
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003052 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
3053 transform->iv_dec, transform->ivlen,
3054 data, rec->data_len, data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02003055 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003056 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02003057 return( ret );
3058 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02003059
Hanno Beckerd96a6522019-07-10 13:55:25 +01003060 /* Double-check that length hasn't changed during decryption. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003061 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02003062 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003063 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3064 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02003065 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02003066
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003067#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003068 if( transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02003069 {
3070 /*
Hanno Beckerd96a6522019-07-10 13:55:25 +01003071 * Save IV in SSL3 and TLS1, where CBC decryption of consecutive
3072 * records is equivalent to CBC decryption of the concatenation
3073 * of the records; in other words, IVs are maintained across
3074 * record decryptions.
Paul Bakkercca5b812013-08-31 17:40:26 +02003075 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003076 memcpy( transform->iv_dec, transform->cipher_ctx_dec.iv,
3077 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00003078 }
Paul Bakkercca5b812013-08-31 17:40:26 +02003079#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003080
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003081 /* Safe since data_len >= minlen + maclen + 1, so after having
3082 * subtracted at most minlen and maclen up to this point,
Hanno Beckerd96a6522019-07-10 13:55:25 +01003083 * data_len > 0 (because of data_len % ivlen == 0, it's actually
3084 * >= ivlen ). */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003085 padlen = data[rec->data_len - 1];
Paul Bakker45829992013-01-03 14:52:21 +01003086
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003087 if( auth_done == 1 )
3088 {
3089 correct *= ( rec->data_len >= padlen + 1 );
3090 padlen *= ( rec->data_len >= padlen + 1 );
3091 }
3092 else
Paul Bakker45829992013-01-03 14:52:21 +01003093 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003094#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003095 if( rec->data_len < transform->maclen + padlen + 1 )
3096 {
3097 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
3098 rec->data_len,
3099 transform->maclen,
3100 padlen + 1 ) );
3101 }
Paul Bakkerd66f0702013-01-31 16:57:45 +01003102#endif
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003103
3104 correct *= ( rec->data_len >= transform->maclen + padlen + 1 );
3105 padlen *= ( rec->data_len >= transform->maclen + padlen + 1 );
Paul Bakker45829992013-01-03 14:52:21 +01003106 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003107
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003108 padlen++;
3109
3110 /* Regardless of the validity of the padding,
3111 * we have data_len >= padlen here. */
3112
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003113#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003114 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003115 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003116 if( padlen > transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00003117 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003118#if defined(MBEDTLS_SSL_DEBUG_ALL)
3119 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003120 "should be no more than %d",
3121 padlen, transform->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01003122#endif
Paul Bakker45829992013-01-03 14:52:21 +01003123 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00003124 }
3125 }
3126 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003127#endif /* MBEDTLS_SSL_PROTO_SSL3 */
3128#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
3129 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003130 if( transform->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003131 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003132 /* The padding check involves a series of up to 256
3133 * consecutive memory reads at the end of the record
3134 * plaintext buffer. In order to hide the length and
3135 * validity of the padding, always perform exactly
3136 * `min(256,plaintext_len)` reads (but take into account
3137 * only the last `padlen` bytes for the padding check). */
3138 size_t pad_count = 0;
3139 size_t real_count = 0;
3140 volatile unsigned char* const check = data;
Paul Bakkere47b34b2013-02-27 14:48:00 +01003141
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003142 /* Index of first padding byte; it has been ensured above
3143 * that the subtraction is safe. */
3144 size_t const padding_idx = rec->data_len - padlen;
3145 size_t const num_checks = rec->data_len <= 256 ? rec->data_len : 256;
3146 size_t const start_idx = rec->data_len - num_checks;
3147 size_t idx;
Paul Bakker956c9e02013-12-19 14:42:28 +01003148
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003149 for( idx = start_idx; idx < rec->data_len; idx++ )
Paul Bakkerca9c87e2013-09-25 18:52:37 +02003150 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003151 real_count |= ( idx >= padding_idx );
3152 pad_count += real_count * ( check[idx] == padlen - 1 );
Paul Bakkerca9c87e2013-09-25 18:52:37 +02003153 }
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003154 correct &= ( pad_count == padlen );
Paul Bakkere47b34b2013-02-27 14:48:00 +01003155
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003156#if defined(MBEDTLS_SSL_DEBUG_ALL)
Paul Bakker66d5d072014-06-17 16:39:18 +02003157 if( padlen > 0 && correct == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003158 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01003159#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01003160 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00003161 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003162 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003163#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
3164 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02003165 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003166 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3167 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02003168 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003169
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003170 /* If the padding was found to be invalid, padlen == 0
3171 * and the subtraction is safe. If the padding was found valid,
3172 * padlen hasn't been changed and the previous assertion
3173 * data_len >= padlen still holds. */
3174 rec->data_len -= padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00003175 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02003176 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003177#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00003178 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02003179 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003180 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3181 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02003182 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003183
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02003184#if defined(MBEDTLS_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003185 MBEDTLS_SSL_DEBUG_BUF( 4, "raw buffer after decryption",
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003186 data, rec->data_len );
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02003187#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003188
3189 /*
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003190 * Authenticate if not done yet.
3191 * Compute the MAC regardless of the padding result (RFC4346, CBCTIME).
Paul Bakker5121ce52009-01-03 21:22:43 +00003192 */
Hanno Becker52344c22018-01-03 15:24:20 +00003193#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003194 if( auth_done == 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003195 {
Hanno Becker992b6872017-11-09 18:57:39 +00003196 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Paul Bakker1e5369c2013-12-19 16:40:57 +01003197
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003198 /* If the initial value of padlen was such that
3199 * data_len < maclen + padlen + 1, then padlen
3200 * got reset to 1, and the initial check
3201 * data_len >= minlen + maclen + 1
3202 * guarantees that at this point we still
3203 * have at least data_len >= maclen.
3204 *
3205 * If the initial value of padlen was such that
3206 * data_len >= maclen + padlen + 1, then we have
3207 * subtracted either padlen + 1 (if the padding was correct)
3208 * or 0 (if the padding was incorrect) since then,
3209 * hence data_len >= maclen in any case.
3210 */
3211 rec->data_len -= transform->maclen;
Hanno Beckercab87e62019-04-29 13:52:53 +01003212 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Paul Bakker5121ce52009-01-03 21:22:43 +00003213
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003214#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003215 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003216 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003217 ssl_mac( &transform->md_ctx_dec,
3218 transform->mac_dec,
3219 data, rec->data_len,
3220 rec->ctr, rec->type,
3221 mac_expect );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003222 }
3223 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003224#endif /* MBEDTLS_SSL_PROTO_SSL3 */
3225#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
3226 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003227 if( transform->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003228 {
3229 /*
3230 * Process MAC and always update for padlen afterwards to make
Gilles Peskine20b44082018-05-29 14:06:49 +02003231 * total time independent of padlen.
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003232 *
3233 * Known timing attacks:
3234 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
3235 *
Gilles Peskine20b44082018-05-29 14:06:49 +02003236 * To compensate for different timings for the MAC calculation
3237 * depending on how much padding was removed (which is determined
3238 * by padlen), process extra_run more blocks through the hash
3239 * function.
3240 *
3241 * The formula in the paper is
3242 * extra_run = ceil( (L1-55) / 64 ) - ceil( (L2-55) / 64 )
3243 * where L1 is the size of the header plus the decrypted message
3244 * plus CBC padding and L2 is the size of the header plus the
3245 * decrypted message. This is for an underlying hash function
3246 * with 64-byte blocks.
3247 * We use ( (Lx+8) / 64 ) to handle 'negative Lx' values
3248 * correctly. We round down instead of up, so -56 is the correct
3249 * value for our calculations instead of -55.
3250 *
Gilles Peskine1bd9d582018-06-04 11:58:44 +02003251 * Repeat the formula rather than defining a block_size variable.
3252 * This avoids requiring division by a variable at runtime
3253 * (which would be marginally less efficient and would require
3254 * linking an extra division function in some builds).
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003255 */
3256 size_t j, extra_run = 0;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003257 unsigned char tmp[MBEDTLS_MD_MAX_BLOCK_SIZE];
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003258
3259 /*
3260 * The next two sizes are the minimum and maximum values of
3261 * in_msglen over all padlen values.
3262 *
3263 * They're independent of padlen, since we previously did
Hanno Beckerd96a6522019-07-10 13:55:25 +01003264 * data_len -= padlen.
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003265 *
3266 * Note that max_len + maclen is never more than the buffer
3267 * length, as we previously did in_msglen -= maclen too.
3268 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003269 const size_t max_len = rec->data_len + padlen;
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003270 const size_t min_len = ( max_len > 256 ) ? max_len - 256 : 0;
3271
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003272 memset( tmp, 0, sizeof( tmp ) );
3273
3274 switch( mbedtls_md_get_type( transform->md_ctx_dec.md_info ) )
Gilles Peskine20b44082018-05-29 14:06:49 +02003275 {
Gilles Peskined0e55a42018-06-04 12:03:30 +02003276#if defined(MBEDTLS_MD5_C) || defined(MBEDTLS_SHA1_C) || \
3277 defined(MBEDTLS_SHA256_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02003278 case MBEDTLS_MD_MD5:
3279 case MBEDTLS_MD_SHA1:
Gilles Peskine20b44082018-05-29 14:06:49 +02003280 case MBEDTLS_MD_SHA256:
Gilles Peskine20b44082018-05-29 14:06:49 +02003281 /* 8 bytes of message size, 64-byte compression blocks */
Hanno Beckercab87e62019-04-29 13:52:53 +01003282 extra_run =
3283 ( add_data_len + rec->data_len + padlen + 8 ) / 64 -
3284 ( add_data_len + rec->data_len + 8 ) / 64;
Gilles Peskine20b44082018-05-29 14:06:49 +02003285 break;
3286#endif
Gilles Peskinea7fe25d2018-06-04 12:01:18 +02003287#if defined(MBEDTLS_SHA512_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02003288 case MBEDTLS_MD_SHA384:
Gilles Peskine20b44082018-05-29 14:06:49 +02003289 /* 16 bytes of message size, 128-byte compression blocks */
Hanno Beckercab87e62019-04-29 13:52:53 +01003290 extra_run =
3291 ( add_data_len + rec->data_len + padlen + 16 ) / 128 -
3292 ( add_data_len + rec->data_len + 16 ) / 128;
Gilles Peskine20b44082018-05-29 14:06:49 +02003293 break;
3294#endif
3295 default:
Gilles Peskine5c389842018-06-04 12:02:43 +02003296 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Gilles Peskine20b44082018-05-29 14:06:49 +02003297 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3298 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01003299
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003300 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01003301
Hanno Beckercab87e62019-04-29 13:52:53 +01003302 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data,
3303 add_data_len );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003304 mbedtls_md_hmac_update( &transform->md_ctx_dec, data,
3305 rec->data_len );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003306 /* Make sure we access everything even when padlen > 0. This
3307 * makes the synchronisation requirements for just-in-time
3308 * Prime+Probe attacks much tighter and hopefully impractical. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003309 ssl_read_memory( data + rec->data_len, padlen );
3310 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003311
3312 /* Call mbedtls_md_process at least once due to cache attacks
3313 * that observe whether md_process() was called of not */
Manuel Pégourié-Gonnard47fede02015-04-29 01:35:48 +02003314 for( j = 0; j < extra_run + 1; j++ )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003315 mbedtls_md_process( &transform->md_ctx_dec, tmp );
Paul Bakkere47b34b2013-02-27 14:48:00 +01003316
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003317 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003318
3319 /* Make sure we access all the memory that could contain the MAC,
3320 * before we check it in the next code block. This makes the
3321 * synchronisation requirements for just-in-time Prime+Probe
3322 * attacks much tighter and hopefully impractical. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003323 ssl_read_memory( data + min_len,
3324 max_len - min_len + transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003325 }
3326 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003327#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
3328 MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003329 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003330 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3331 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003332 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003333
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003334#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003335 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect, transform->maclen );
3336 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len, transform->maclen );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003337#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003338
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003339 if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect,
3340 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003341 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003342#if defined(MBEDTLS_SSL_DEBUG_ALL)
3343 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01003344#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003345 correct = 0;
3346 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003347 auth_done++;
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003348 }
Hanno Beckerdd3ab132018-10-17 14:43:14 +01003349
3350 /*
3351 * Finally check the correct flag
3352 */
3353 if( correct == 0 )
3354 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Hanno Becker52344c22018-01-03 15:24:20 +00003355#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003356
3357 /* Make extra sure authentication was performed, exactly once */
3358 if( auth_done != 1 )
3359 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003360 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3361 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003362 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003363
Hanno Beckera0e20d02019-05-15 14:03:01 +01003364#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01003365 if( rec->cid_len != 0 )
3366 {
3367 ret = ssl_cid_parse_inner_plaintext( data, &rec->data_len,
3368 &rec->type );
3369 if( ret != 0 )
3370 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3371 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01003372#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01003373
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003374 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003375
3376 return( 0 );
3377}
3378
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01003379#undef MAC_NONE
3380#undef MAC_PLAINTEXT
3381#undef MAC_CIPHERTEXT
3382
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003383#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00003384/*
3385 * Compression/decompression functions
3386 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003387static int ssl_compress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003388{
3389 int ret;
3390 unsigned char *msg_post = ssl->out_msg;
Andrzej Kurek5462e022018-04-20 07:58:53 -04003391 ptrdiff_t bytes_written = ssl->out_msg - ssl->out_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003392 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02003393 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003394
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003395 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003396
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02003397 if( len_pre == 0 )
3398 return( 0 );
3399
Paul Bakker2770fbd2012-07-03 13:30:23 +00003400 memcpy( msg_pre, ssl->out_msg, len_pre );
3401
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003402 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003403 ssl->out_msglen ) );
3404
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003405 MBEDTLS_SSL_DEBUG_BUF( 4, "before compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003406 ssl->out_msg, ssl->out_msglen );
3407
Paul Bakker48916f92012-09-16 19:57:18 +00003408 ssl->transform_out->ctx_deflate.next_in = msg_pre;
3409 ssl->transform_out->ctx_deflate.avail_in = len_pre;
3410 ssl->transform_out->ctx_deflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10003411 ssl->transform_out->ctx_deflate.avail_out = MBEDTLS_SSL_OUT_BUFFER_LEN - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003412
Paul Bakker48916f92012-09-16 19:57:18 +00003413 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003414 if( ret != Z_OK )
3415 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003416 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
3417 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003418 }
3419
Angus Grattond8213d02016-05-25 20:56:48 +10003420 ssl->out_msglen = MBEDTLS_SSL_OUT_BUFFER_LEN -
Andrzej Kurek5462e022018-04-20 07:58:53 -04003421 ssl->transform_out->ctx_deflate.avail_out - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003422
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003423 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003424 ssl->out_msglen ) );
3425
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003426 MBEDTLS_SSL_DEBUG_BUF( 4, "after compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003427 ssl->out_msg, ssl->out_msglen );
3428
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003429 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003430
3431 return( 0 );
3432}
3433
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003434static int ssl_decompress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003435{
3436 int ret;
3437 unsigned char *msg_post = ssl->in_msg;
Andrzej Kureka9ceef82018-04-24 06:32:44 -04003438 ptrdiff_t header_bytes = ssl->in_msg - ssl->in_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003439 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02003440 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003441
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003442 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003443
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02003444 if( len_pre == 0 )
3445 return( 0 );
3446
Paul Bakker2770fbd2012-07-03 13:30:23 +00003447 memcpy( msg_pre, ssl->in_msg, len_pre );
3448
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003449 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003450 ssl->in_msglen ) );
3451
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003452 MBEDTLS_SSL_DEBUG_BUF( 4, "before decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003453 ssl->in_msg, ssl->in_msglen );
3454
Paul Bakker48916f92012-09-16 19:57:18 +00003455 ssl->transform_in->ctx_inflate.next_in = msg_pre;
3456 ssl->transform_in->ctx_inflate.avail_in = len_pre;
3457 ssl->transform_in->ctx_inflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10003458 ssl->transform_in->ctx_inflate.avail_out = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04003459 header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003460
Paul Bakker48916f92012-09-16 19:57:18 +00003461 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003462 if( ret != Z_OK )
3463 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003464 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
3465 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003466 }
3467
Angus Grattond8213d02016-05-25 20:56:48 +10003468 ssl->in_msglen = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04003469 ssl->transform_in->ctx_inflate.avail_out - header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003470
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003471 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003472 ssl->in_msglen ) );
3473
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003474 MBEDTLS_SSL_DEBUG_BUF( 4, "after decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003475 ssl->in_msg, ssl->in_msglen );
3476
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003477 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003478
3479 return( 0 );
3480}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003481#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003482
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003483#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
3484static int ssl_write_hello_request( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003485
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003486#if defined(MBEDTLS_SSL_PROTO_DTLS)
3487static int ssl_resend_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003488{
3489 /* If renegotiation is not enforced, retransmit until we would reach max
3490 * timeout if we were using the usual handshake doubling scheme */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003491 if( ssl->conf->renego_max_records < 0 )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003492 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003493 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003494 unsigned char doublings = 1;
3495
3496 while( ratio != 0 )
3497 {
3498 ++doublings;
3499 ratio >>= 1;
3500 }
3501
3502 if( ++ssl->renego_records_seen > doublings )
3503 {
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02003504 MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003505 return( 0 );
3506 }
3507 }
3508
3509 return( ssl_write_hello_request( ssl ) );
3510}
3511#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003512#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003513
Paul Bakker5121ce52009-01-03 21:22:43 +00003514/*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003515 * Fill the input message buffer by appending data to it.
3516 * The amount of data already fetched is in ssl->in_left.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003517 *
3518 * If we return 0, is it guaranteed that (at least) nb_want bytes are
3519 * available (from this read and/or a previous one). Otherwise, an error code
3520 * is returned (possibly EOF or WANT_READ).
3521 *
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003522 * With stream transport (TLS) on success ssl->in_left == nb_want, but
3523 * with datagram transport (DTLS) on success ssl->in_left >= nb_want,
3524 * since we always read a whole datagram at once.
3525 *
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02003526 * For DTLS, it is up to the caller to set ssl->next_record_offset when
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003527 * they're done reading a record.
Paul Bakker5121ce52009-01-03 21:22:43 +00003528 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003529int mbedtls_ssl_fetch_input( mbedtls_ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00003530{
Paul Bakker23986e52011-04-24 08:57:21 +00003531 int ret;
3532 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00003533
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003534 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003535
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003536 if( ssl->f_recv == NULL && ssl->f_recv_timeout == NULL )
3537 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003538 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01003539 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003540 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003541 }
3542
Angus Grattond8213d02016-05-25 20:56:48 +10003543 if( nb_want > MBEDTLS_SSL_IN_BUFFER_LEN - (size_t)( ssl->in_hdr - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003544 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003545 MBEDTLS_SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
3546 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003547 }
3548
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003549#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003550 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00003551 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003552 uint32_t timeout;
3553
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02003554 /* Just to be sure */
3555 if( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL )
3556 {
3557 MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
3558 "mbedtls_ssl_set_timer_cb() for DTLS" ) );
3559 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3560 }
3561
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003562 /*
3563 * The point is, we need to always read a full datagram at once, so we
3564 * sometimes read more then requested, and handle the additional data.
3565 * It could be the rest of the current record (while fetching the
3566 * header) and/or some other records in the same datagram.
3567 */
3568
3569 /*
3570 * Move to the next record in the already read datagram if applicable
3571 */
3572 if( ssl->next_record_offset != 0 )
3573 {
3574 if( ssl->in_left < ssl->next_record_offset )
3575 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003576 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3577 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003578 }
3579
3580 ssl->in_left -= ssl->next_record_offset;
3581
3582 if( ssl->in_left != 0 )
3583 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003584 MBEDTLS_SSL_DEBUG_MSG( 2, ( "next record in same datagram, offset: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003585 ssl->next_record_offset ) );
3586 memmove( ssl->in_hdr,
3587 ssl->in_hdr + ssl->next_record_offset,
3588 ssl->in_left );
3589 }
3590
3591 ssl->next_record_offset = 0;
3592 }
3593
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003594 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Paul Bakker5121ce52009-01-03 21:22:43 +00003595 ssl->in_left, nb_want ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003596
3597 /*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003598 * Done if we already have enough data.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003599 */
3600 if( nb_want <= ssl->in_left)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003601 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003602 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003603 return( 0 );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003604 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003605
3606 /*
Antonin Décimo36e89b52019-01-23 15:24:37 +01003607 * A record can't be split across datagrams. If we need to read but
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003608 * are not at the beginning of a new record, the caller did something
3609 * wrong.
3610 */
3611 if( ssl->in_left != 0 )
3612 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003613 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3614 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003615 }
3616
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003617 /*
3618 * Don't even try to read if time's out already.
3619 * This avoids by-passing the timer when repeatedly receiving messages
3620 * that will end up being dropped.
3621 */
3622 if( ssl_check_timer( ssl ) != 0 )
Hanno Beckere65ce782017-05-22 14:47:48 +01003623 {
3624 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timer has expired" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003625 ret = MBEDTLS_ERR_SSL_TIMEOUT;
Hanno Beckere65ce782017-05-22 14:47:48 +01003626 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003627 else
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003628 {
Angus Grattond8213d02016-05-25 20:56:48 +10003629 len = MBEDTLS_SSL_IN_BUFFER_LEN - ( ssl->in_hdr - ssl->in_buf );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003630
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003631 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003632 timeout = ssl->handshake->retransmit_timeout;
3633 else
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003634 timeout = ssl->conf->read_timeout;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003635
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003636 MBEDTLS_SSL_DEBUG_MSG( 3, ( "f_recv_timeout: %u ms", timeout ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003637
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003638 if( ssl->f_recv_timeout != NULL )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003639 ret = ssl->f_recv_timeout( ssl->p_bio, ssl->in_hdr, len,
3640 timeout );
3641 else
3642 ret = ssl->f_recv( ssl->p_bio, ssl->in_hdr, len );
3643
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003644 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003645
3646 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003647 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003648 }
3649
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003650 if( ret == MBEDTLS_ERR_SSL_TIMEOUT )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003651 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003652 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timeout" ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003653 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003654
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003655 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003656 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003657 if( ssl_double_retransmit_timeout( ssl ) != 0 )
3658 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003659 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake timeout" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003660 return( MBEDTLS_ERR_SSL_TIMEOUT );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003661 }
3662
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003663 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003664 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003665 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003666 return( ret );
3667 }
3668
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003669 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003670 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003671#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003672 else if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003673 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003674 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003675 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003676 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003677 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003678 return( ret );
3679 }
3680
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003681 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003682 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003683#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003684 }
3685
Paul Bakker5121ce52009-01-03 21:22:43 +00003686 if( ret < 0 )
3687 return( ret );
3688
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003689 ssl->in_left = ret;
3690 }
3691 else
3692#endif
3693 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003694 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003695 ssl->in_left, nb_want ) );
3696
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003697 while( ssl->in_left < nb_want )
3698 {
3699 len = nb_want - ssl->in_left;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02003700
3701 if( ssl_check_timer( ssl ) != 0 )
3702 ret = MBEDTLS_ERR_SSL_TIMEOUT;
3703 else
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003704 {
3705 if( ssl->f_recv_timeout != NULL )
3706 {
3707 ret = ssl->f_recv_timeout( ssl->p_bio,
3708 ssl->in_hdr + ssl->in_left, len,
3709 ssl->conf->read_timeout );
3710 }
3711 else
3712 {
3713 ret = ssl->f_recv( ssl->p_bio,
3714 ssl->in_hdr + ssl->in_left, len );
3715 }
3716 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003717
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003718 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003719 ssl->in_left, nb_want ) );
3720 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003721
3722 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003723 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003724
3725 if( ret < 0 )
3726 return( ret );
3727
mohammad160352aecb92018-03-28 23:41:40 -07003728 if ( (size_t)ret > len || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16035bd15cb2018-02-28 04:30:59 -08003729 {
Darryl Green11999bb2018-03-13 15:22:58 +00003730 MBEDTLS_SSL_DEBUG_MSG( 1,
3731 ( "f_recv returned %d bytes but only %lu were requested",
mohammad160319d392b2018-04-02 07:25:26 -07003732 ret, (unsigned long)len ) );
mohammad16035bd15cb2018-02-28 04:30:59 -08003733 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3734 }
3735
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003736 ssl->in_left += ret;
3737 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003738 }
3739
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003740 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003741
3742 return( 0 );
3743}
3744
3745/*
3746 * Flush any data not yet written
3747 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003748int mbedtls_ssl_flush_output( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003749{
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01003750 int ret;
Hanno Becker04484622018-08-06 09:49:38 +01003751 unsigned char *buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00003752
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003753 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003754
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003755 if( ssl->f_send == NULL )
3756 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003757 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01003758 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003759 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003760 }
3761
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003762 /* Avoid incrementing counter if data is flushed */
3763 if( ssl->out_left == 0 )
3764 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003765 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003766 return( 0 );
3767 }
3768
Paul Bakker5121ce52009-01-03 21:22:43 +00003769 while( ssl->out_left > 0 )
3770 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003771 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
Hanno Becker5903de42019-05-03 14:46:38 +01003772 mbedtls_ssl_out_hdr_len( ssl ) + ssl->out_msglen, ssl->out_left ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003773
Hanno Becker2b1e3542018-08-06 11:19:13 +01003774 buf = ssl->out_hdr - ssl->out_left;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003775 ret = ssl->f_send( ssl->p_bio, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00003776
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003777 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003778
3779 if( ret <= 0 )
3780 return( ret );
3781
mohammad160352aecb92018-03-28 23:41:40 -07003782 if( (size_t)ret > ssl->out_left || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16034bbaeb42018-02-22 04:29:04 -08003783 {
Darryl Green11999bb2018-03-13 15:22:58 +00003784 MBEDTLS_SSL_DEBUG_MSG( 1,
3785 ( "f_send returned %d bytes but only %lu bytes were sent",
mohammad160319d392b2018-04-02 07:25:26 -07003786 ret, (unsigned long)ssl->out_left ) );
mohammad16034bbaeb42018-02-22 04:29:04 -08003787 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3788 }
3789
Paul Bakker5121ce52009-01-03 21:22:43 +00003790 ssl->out_left -= ret;
3791 }
3792
Hanno Becker2b1e3542018-08-06 11:19:13 +01003793#if defined(MBEDTLS_SSL_PROTO_DTLS)
3794 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003795 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01003796 ssl->out_hdr = ssl->out_buf;
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003797 }
Hanno Becker2b1e3542018-08-06 11:19:13 +01003798 else
3799#endif
3800 {
3801 ssl->out_hdr = ssl->out_buf + 8;
3802 }
3803 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003804
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003805 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003806
3807 return( 0 );
3808}
3809
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003810/*
3811 * Functions to handle the DTLS retransmission state machine
3812 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003813#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003814/*
3815 * Append current handshake message to current outgoing flight
3816 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003817static int ssl_flight_append( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003818{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003819 mbedtls_ssl_flight_item *msg;
Hanno Becker3b235902018-08-06 09:54:53 +01003820 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_flight_append" ) );
3821 MBEDTLS_SSL_DEBUG_BUF( 4, "message appended to flight",
3822 ssl->out_msg, ssl->out_msglen );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003823
3824 /* Allocate space for current message */
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003825 if( ( msg = mbedtls_calloc( 1, sizeof( mbedtls_ssl_flight_item ) ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003826 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003827 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003828 sizeof( mbedtls_ssl_flight_item ) ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003829 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003830 }
3831
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003832 if( ( msg->p = mbedtls_calloc( 1, ssl->out_msglen ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003833 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003834 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed", ssl->out_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003835 mbedtls_free( msg );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003836 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003837 }
3838
3839 /* Copy current handshake message with headers */
3840 memcpy( msg->p, ssl->out_msg, ssl->out_msglen );
3841 msg->len = ssl->out_msglen;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003842 msg->type = ssl->out_msgtype;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003843 msg->next = NULL;
3844
3845 /* Append to the current flight */
3846 if( ssl->handshake->flight == NULL )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003847 ssl->handshake->flight = msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003848 else
3849 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003850 mbedtls_ssl_flight_item *cur = ssl->handshake->flight;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003851 while( cur->next != NULL )
3852 cur = cur->next;
3853 cur->next = msg;
3854 }
3855
Hanno Becker3b235902018-08-06 09:54:53 +01003856 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_flight_append" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003857 return( 0 );
3858}
3859
3860/*
3861 * Free the current flight of handshake messages
3862 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003863static void ssl_flight_free( mbedtls_ssl_flight_item *flight )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003864{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003865 mbedtls_ssl_flight_item *cur = flight;
3866 mbedtls_ssl_flight_item *next;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003867
3868 while( cur != NULL )
3869 {
3870 next = cur->next;
3871
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003872 mbedtls_free( cur->p );
3873 mbedtls_free( cur );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003874
3875 cur = next;
3876 }
3877}
3878
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003879#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3880static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003881#endif
3882
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003883/*
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003884 * Swap transform_out and out_ctr with the alternative ones
3885 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003886static void ssl_swap_epochs( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003887{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003888 mbedtls_ssl_transform *tmp_transform;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003889 unsigned char tmp_out_ctr[8];
3890
3891 if( ssl->transform_out == ssl->handshake->alt_transform_out )
3892 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003893 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003894 return;
3895 }
3896
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003897 MBEDTLS_SSL_DEBUG_MSG( 3, ( "swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003898
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003899 /* Swap transforms */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003900 tmp_transform = ssl->transform_out;
3901 ssl->transform_out = ssl->handshake->alt_transform_out;
3902 ssl->handshake->alt_transform_out = tmp_transform;
3903
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003904 /* Swap epoch + sequence_number */
Hanno Becker19859472018-08-06 09:40:20 +01003905 memcpy( tmp_out_ctr, ssl->cur_out_ctr, 8 );
3906 memcpy( ssl->cur_out_ctr, ssl->handshake->alt_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003907 memcpy( ssl->handshake->alt_out_ctr, tmp_out_ctr, 8 );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003908
3909 /* Adjust to the newly activated transform */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01003910 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003911
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003912#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3913 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003914 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003915 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003916 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003917 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
3918 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003919 }
3920 }
3921#endif
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003922}
3923
3924/*
3925 * Retransmit the current flight of messages.
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003926 */
3927int mbedtls_ssl_resend( mbedtls_ssl_context *ssl )
3928{
3929 int ret = 0;
3930
3931 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_resend" ) );
3932
3933 ret = mbedtls_ssl_flight_transmit( ssl );
3934
3935 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_resend" ) );
3936
3937 return( ret );
3938}
3939
3940/*
3941 * Transmit or retransmit the current flight of messages.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003942 *
3943 * Need to remember the current message in case flush_output returns
3944 * WANT_WRITE, causing us to exit this function and come back later.
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003945 * This function must be called until state is no longer SENDING.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003946 */
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003947int mbedtls_ssl_flight_transmit( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003948{
Hanno Becker67bc7c32018-08-06 11:33:50 +01003949 int ret;
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003950 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003951
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003952 if( ssl->handshake->retransmit_state != MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003953 {
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003954 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialise flight transmission" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003955
3956 ssl->handshake->cur_msg = ssl->handshake->flight;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003957 ssl->handshake->cur_msg_p = ssl->handshake->flight->p + 12;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003958 ssl_swap_epochs( ssl );
3959
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003960 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_SENDING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003961 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003962
3963 while( ssl->handshake->cur_msg != NULL )
3964 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003965 size_t max_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003966 const mbedtls_ssl_flight_item * const cur = ssl->handshake->cur_msg;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003967
Hanno Beckere1dcb032018-08-17 16:47:58 +01003968 int const is_finished =
3969 ( cur->type == MBEDTLS_SSL_MSG_HANDSHAKE &&
3970 cur->p[0] == MBEDTLS_SSL_HS_FINISHED );
3971
Hanno Becker04da1892018-08-14 13:22:10 +01003972 uint8_t const force_flush = ssl->disable_datagram_packing == 1 ?
3973 SSL_FORCE_FLUSH : SSL_DONT_FORCE_FLUSH;
3974
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003975 /* Swap epochs before sending Finished: we can't do it after
3976 * sending ChangeCipherSpec, in case write returns WANT_READ.
3977 * Must be done before copying, may change out_msg pointer */
Hanno Beckere1dcb032018-08-17 16:47:58 +01003978 if( is_finished && ssl->handshake->cur_msg_p == ( cur->p + 12 ) )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003979 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003980 MBEDTLS_SSL_DEBUG_MSG( 2, ( "swap epochs to send finished message" ) );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003981 ssl_swap_epochs( ssl );
3982 }
3983
Hanno Becker67bc7c32018-08-06 11:33:50 +01003984 ret = ssl_get_remaining_payload_in_datagram( ssl );
3985 if( ret < 0 )
3986 return( ret );
3987 max_frag_len = (size_t) ret;
3988
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003989 /* CCS is copied as is, while HS messages may need fragmentation */
3990 if( cur->type == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
3991 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003992 if( max_frag_len == 0 )
3993 {
3994 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3995 return( ret );
3996
3997 continue;
3998 }
3999
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004000 memcpy( ssl->out_msg, cur->p, cur->len );
Hanno Becker67bc7c32018-08-06 11:33:50 +01004001 ssl->out_msglen = cur->len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004002 ssl->out_msgtype = cur->type;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004003
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004004 /* Update position inside current message */
4005 ssl->handshake->cur_msg_p += cur->len;
4006 }
4007 else
4008 {
4009 const unsigned char * const p = ssl->handshake->cur_msg_p;
4010 const size_t hs_len = cur->len - 12;
4011 const size_t frag_off = p - ( cur->p + 12 );
4012 const size_t rem_len = hs_len - frag_off;
Hanno Becker67bc7c32018-08-06 11:33:50 +01004013 size_t cur_hs_frag_len, max_hs_frag_len;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004014
Hanno Beckere1dcb032018-08-17 16:47:58 +01004015 if( ( max_frag_len < 12 ) || ( max_frag_len == 12 && hs_len != 0 ) )
Manuel Pégourié-Gonnarda1071a52018-08-20 11:56:14 +02004016 {
Hanno Beckere1dcb032018-08-17 16:47:58 +01004017 if( is_finished )
Hanno Becker67bc7c32018-08-06 11:33:50 +01004018 ssl_swap_epochs( ssl );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004019
Hanno Becker67bc7c32018-08-06 11:33:50 +01004020 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
4021 return( ret );
4022
4023 continue;
4024 }
4025 max_hs_frag_len = max_frag_len - 12;
4026
4027 cur_hs_frag_len = rem_len > max_hs_frag_len ?
4028 max_hs_frag_len : rem_len;
4029
4030 if( frag_off == 0 && cur_hs_frag_len != hs_len )
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02004031 {
4032 MBEDTLS_SSL_DEBUG_MSG( 2, ( "fragmenting handshake message (%u > %u)",
Hanno Becker67bc7c32018-08-06 11:33:50 +01004033 (unsigned) cur_hs_frag_len,
4034 (unsigned) max_hs_frag_len ) );
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02004035 }
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02004036
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004037 /* Messages are stored with handshake headers as if not fragmented,
4038 * copy beginning of headers then fill fragmentation fields.
4039 * Handshake headers: type(1) len(3) seq(2) f_off(3) f_len(3) */
4040 memcpy( ssl->out_msg, cur->p, 6 );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004041
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004042 ssl->out_msg[6] = ( ( frag_off >> 16 ) & 0xff );
4043 ssl->out_msg[7] = ( ( frag_off >> 8 ) & 0xff );
4044 ssl->out_msg[8] = ( ( frag_off ) & 0xff );
4045
Hanno Becker67bc7c32018-08-06 11:33:50 +01004046 ssl->out_msg[ 9] = ( ( cur_hs_frag_len >> 16 ) & 0xff );
4047 ssl->out_msg[10] = ( ( cur_hs_frag_len >> 8 ) & 0xff );
4048 ssl->out_msg[11] = ( ( cur_hs_frag_len ) & 0xff );
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004049
4050 MBEDTLS_SSL_DEBUG_BUF( 3, "handshake header", ssl->out_msg, 12 );
4051
Hanno Becker3f7b9732018-08-28 09:53:25 +01004052 /* Copy the handshake message content and set records fields */
Hanno Becker67bc7c32018-08-06 11:33:50 +01004053 memcpy( ssl->out_msg + 12, p, cur_hs_frag_len );
4054 ssl->out_msglen = cur_hs_frag_len + 12;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004055 ssl->out_msgtype = cur->type;
4056
4057 /* Update position inside current message */
Hanno Becker67bc7c32018-08-06 11:33:50 +01004058 ssl->handshake->cur_msg_p += cur_hs_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004059 }
4060
4061 /* If done with the current message move to the next one if any */
4062 if( ssl->handshake->cur_msg_p >= cur->p + cur->len )
4063 {
4064 if( cur->next != NULL )
4065 {
4066 ssl->handshake->cur_msg = cur->next;
4067 ssl->handshake->cur_msg_p = cur->next->p + 12;
4068 }
4069 else
4070 {
4071 ssl->handshake->cur_msg = NULL;
4072 ssl->handshake->cur_msg_p = NULL;
4073 }
4074 }
4075
4076 /* Actually send the message out */
Hanno Becker04da1892018-08-14 13:22:10 +01004077 if( ( ret = mbedtls_ssl_write_record( ssl, force_flush ) ) != 0 )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004078 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004079 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004080 return( ret );
4081 }
4082 }
4083
Hanno Becker67bc7c32018-08-06 11:33:50 +01004084 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
4085 return( ret );
4086
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004087 /* Update state and set timer */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004088 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
4089 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard23b7b702014-09-25 13:50:12 +02004090 else
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02004091 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004092 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02004093 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
4094 }
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004095
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004096 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004097
4098 return( 0 );
4099}
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004100
4101/*
4102 * To be called when the last message of an incoming flight is received.
4103 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004104void mbedtls_ssl_recv_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004105{
4106 /* We won't need to resend that one any more */
4107 ssl_flight_free( ssl->handshake->flight );
4108 ssl->handshake->flight = NULL;
4109 ssl->handshake->cur_msg = NULL;
4110
4111 /* The next incoming flight will start with this msg_seq */
4112 ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq;
4113
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004114 /* We don't want to remember CCS's across flight boundaries. */
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01004115 ssl->handshake->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004116
Hanno Becker0271f962018-08-16 13:23:47 +01004117 /* Clear future message buffering structure. */
4118 ssl_buffering_free( ssl );
4119
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02004120 /* Cancel timer */
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004121 ssl_set_timer( ssl, 0 );
4122
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004123 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4124 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004125 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004126 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004127 }
4128 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004129 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004130}
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004131
4132/*
4133 * To be called when the last message of an outgoing flight is send.
4134 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004135void mbedtls_ssl_send_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004136{
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02004137 ssl_reset_retransmit_timeout( ssl );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02004138 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004139
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004140 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4141 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004142 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004143 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004144 }
4145 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004146 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004147}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004148#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004149
Paul Bakker5121ce52009-01-03 21:22:43 +00004150/*
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004151 * Handshake layer functions
Paul Bakker5121ce52009-01-03 21:22:43 +00004152 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004153
4154/*
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004155 * Write (DTLS: or queue) current handshake (including CCS) message.
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004156 *
4157 * - fill in handshake headers
4158 * - update handshake checksum
4159 * - DTLS: save message for resending
4160 * - then pass to the record layer
4161 *
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004162 * DTLS: except for HelloRequest, messages are only queued, and will only be
4163 * actually sent when calling flight_transmit() or resend().
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004164 *
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004165 * Inputs:
4166 * - ssl->out_msglen: 4 + actual handshake message len
4167 * (4 is the size of handshake headers for TLS)
4168 * - ssl->out_msg[0]: the handshake type (ClientHello, ServerHello, etc)
4169 * - ssl->out_msg + 4: the handshake message body
4170 *
Manuel Pégourié-Gonnard065a2a32018-08-20 11:09:26 +02004171 * Outputs, ie state before passing to flight_append() or write_record():
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004172 * - ssl->out_msglen: the length of the record contents
4173 * (including handshake headers but excluding record headers)
4174 * - ssl->out_msg: the record contents (handshake headers + content)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004175 */
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004176int mbedtls_ssl_write_handshake_msg( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004177{
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004178 int ret;
4179 const size_t hs_len = ssl->out_msglen - 4;
4180 const unsigned char hs_type = ssl->out_msg[0];
Paul Bakker5121ce52009-01-03 21:22:43 +00004181
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004182 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write handshake message" ) );
4183
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004184 /*
4185 * Sanity checks
4186 */
Hanno Beckerc83d2b32018-08-22 16:05:47 +01004187 if( ssl->out_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004188 ssl->out_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
4189 {
Hanno Beckerc83d2b32018-08-22 16:05:47 +01004190 /* In SSLv3, the client might send a NoCertificate alert. */
4191#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
4192 if( ! ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
4193 ssl->out_msgtype == MBEDTLS_SSL_MSG_ALERT &&
4194 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) )
4195#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
4196 {
4197 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4198 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4199 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004200 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004201
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05004202 /* Whenever we send anything different from a
4203 * HelloRequest we should be in a handshake - double check. */
4204 if( ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4205 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) &&
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004206 ssl->handshake == NULL )
4207 {
4208 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4209 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4210 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004211
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004212#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004213 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004214 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004215 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004216 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004217 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4218 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004219 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004220#endif
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004221
Hanno Beckerb50a2532018-08-06 11:52:54 +01004222 /* Double-check that we did not exceed the bounds
4223 * of the outgoing record buffer.
4224 * This should never fail as the various message
4225 * writing functions must obey the bounds of the
4226 * outgoing record buffer, but better be safe.
4227 *
4228 * Note: We deliberately do not check for the MTU or MFL here.
4229 */
4230 if( ssl->out_msglen > MBEDTLS_SSL_OUT_CONTENT_LEN )
4231 {
4232 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record too large: "
4233 "size %u, maximum %u",
4234 (unsigned) ssl->out_msglen,
4235 (unsigned) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
4236 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4237 }
4238
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004239 /*
4240 * Fill handshake headers
4241 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004242 if( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00004243 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004244 ssl->out_msg[1] = (unsigned char)( hs_len >> 16 );
4245 ssl->out_msg[2] = (unsigned char)( hs_len >> 8 );
4246 ssl->out_msg[3] = (unsigned char)( hs_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00004247
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004248 /*
4249 * DTLS has additional fields in the Handshake layer,
4250 * between the length field and the actual payload:
4251 * uint16 message_seq;
4252 * uint24 fragment_offset;
4253 * uint24 fragment_length;
4254 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004255#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004256 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004257 {
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01004258 /* Make room for the additional DTLS fields */
Angus Grattond8213d02016-05-25 20:56:48 +10004259 if( MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen < 8 )
Hanno Becker9648f8b2017-09-18 10:55:54 +01004260 {
4261 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS handshake message too large: "
4262 "size %u, maximum %u",
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004263 (unsigned) ( hs_len ),
Angus Grattond8213d02016-05-25 20:56:48 +10004264 (unsigned) ( MBEDTLS_SSL_OUT_CONTENT_LEN - 12 ) ) );
Hanno Becker9648f8b2017-09-18 10:55:54 +01004265 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4266 }
4267
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004268 memmove( ssl->out_msg + 12, ssl->out_msg + 4, hs_len );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004269 ssl->out_msglen += 8;
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004270
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02004271 /* Write message_seq and update it, except for HelloRequest */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004272 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02004273 {
Manuel Pégourié-Gonnardd9ba0d92014-09-02 18:30:26 +02004274 ssl->out_msg[4] = ( ssl->handshake->out_msg_seq >> 8 ) & 0xFF;
4275 ssl->out_msg[5] = ( ssl->handshake->out_msg_seq ) & 0xFF;
4276 ++( ssl->handshake->out_msg_seq );
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02004277 }
4278 else
4279 {
4280 ssl->out_msg[4] = 0;
4281 ssl->out_msg[5] = 0;
4282 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01004283
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004284 /* Handshake hashes are computed without fragmentation,
4285 * so set frag_offset = 0 and frag_len = hs_len for now */
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01004286 memset( ssl->out_msg + 6, 0x00, 3 );
4287 memcpy( ssl->out_msg + 9, ssl->out_msg + 1, 3 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004288 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004289#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004290
Hanno Becker0207e532018-08-28 10:28:28 +01004291 /* Update running hashes of handshake messages seen */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004292 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
4293 ssl->handshake->update_checksum( ssl, ssl->out_msg, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00004294 }
4295
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004296 /* Either send now, or just save to be sent (and resent) later */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004297#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004298 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05004299 ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4300 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004301 {
4302 if( ( ret = ssl_flight_append( ssl ) ) != 0 )
4303 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004304 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_flight_append", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004305 return( ret );
4306 }
4307 }
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004308 else
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004309#endif
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004310 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004311 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004312 {
4313 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4314 return( ret );
4315 }
4316 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004317
4318 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write handshake message" ) );
4319
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004320 return( 0 );
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004321}
4322
4323/*
4324 * Record layer functions
4325 */
4326
4327/*
4328 * Write current record.
4329 *
4330 * Uses:
4331 * - ssl->out_msgtype: type of the message (AppData, Handshake, Alert, CCS)
4332 * - ssl->out_msglen: length of the record content (excl headers)
4333 * - ssl->out_msg: record content
4334 */
Hanno Becker67bc7c32018-08-06 11:33:50 +01004335int mbedtls_ssl_write_record( mbedtls_ssl_context *ssl, uint8_t force_flush )
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004336{
4337 int ret, done = 0;
4338 size_t len = ssl->out_msglen;
Hanno Becker67bc7c32018-08-06 11:33:50 +01004339 uint8_t flush = force_flush;
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004340
4341 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write record" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004342
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004343#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00004344 if( ssl->transform_out != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004345 ssl->session_out->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00004346 {
4347 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
4348 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004349 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00004350 return( ret );
4351 }
4352
4353 len = ssl->out_msglen;
4354 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004355#endif /*MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00004356
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004357#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
4358 if( mbedtls_ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004359 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004360 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004361
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004362 ret = mbedtls_ssl_hw_record_write( ssl );
4363 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00004364 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004365 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_write", ret );
4366 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00004367 }
Paul Bakkerc7878112012-12-19 14:41:14 +01004368
4369 if( ret == 0 )
4370 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00004371 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004372#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00004373 if( !done )
4374 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01004375 unsigned i;
4376 size_t protected_record_size;
4377
Hanno Becker6430faf2019-05-08 11:57:13 +01004378 /* Skip writing the record content type to after the encryption,
4379 * as it may change when using the CID extension. */
4380
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004381 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004382 ssl->conf->transport, ssl->out_hdr + 1 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01004383
Hanno Becker19859472018-08-06 09:40:20 +01004384 memcpy( ssl->out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01004385 ssl->out_len[0] = (unsigned char)( len >> 8 );
4386 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00004387
Paul Bakker48916f92012-09-16 19:57:18 +00004388 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00004389 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00004390 mbedtls_record rec;
4391
4392 rec.buf = ssl->out_iv;
4393 rec.buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN -
4394 ( ssl->out_iv - ssl->out_buf );
4395 rec.data_len = ssl->out_msglen;
4396 rec.data_offset = ssl->out_msg - rec.buf;
4397
4398 memcpy( &rec.ctr[0], ssl->out_ctr, 8 );
4399 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
4400 ssl->conf->transport, rec.ver );
4401 rec.type = ssl->out_msgtype;
4402
Hanno Beckera0e20d02019-05-15 14:03:01 +01004403#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker43c24b82019-05-01 09:45:57 +01004404 /* The CID is set by mbedtls_ssl_encrypt_buf(). */
Hanno Beckercab87e62019-04-29 13:52:53 +01004405 rec.cid_len = 0;
Hanno Beckera0e20d02019-05-15 14:03:01 +01004406#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckercab87e62019-04-29 13:52:53 +01004407
Hanno Beckera18d1322018-01-03 14:27:32 +00004408 if( ( ret = mbedtls_ssl_encrypt_buf( ssl, ssl->transform_out, &rec,
Hanno Becker9eddaeb2017-12-27 21:37:21 +00004409 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00004410 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004411 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
Paul Bakker05ef8352012-05-08 09:17:57 +00004412 return( ret );
4413 }
4414
Hanno Becker9eddaeb2017-12-27 21:37:21 +00004415 if( rec.data_offset != 0 )
4416 {
4417 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4418 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4419 }
4420
Hanno Becker6430faf2019-05-08 11:57:13 +01004421 /* Update the record content type and CID. */
4422 ssl->out_msgtype = rec.type;
Hanno Beckera0e20d02019-05-15 14:03:01 +01004423#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID )
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01004424 memcpy( ssl->out_cid, rec.cid, rec.cid_len );
Hanno Beckera0e20d02019-05-15 14:03:01 +01004425#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker78f839d2019-03-14 12:56:23 +00004426 ssl->out_msglen = len = rec.data_len;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00004427 ssl->out_len[0] = (unsigned char)( rec.data_len >> 8 );
4428 ssl->out_len[1] = (unsigned char)( rec.data_len );
Paul Bakker05ef8352012-05-08 09:17:57 +00004429 }
4430
Hanno Becker5903de42019-05-03 14:46:38 +01004431 protected_record_size = len + mbedtls_ssl_out_hdr_len( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01004432
4433#if defined(MBEDTLS_SSL_PROTO_DTLS)
4434 /* In case of DTLS, double-check that we don't exceed
4435 * the remaining space in the datagram. */
4436 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4437 {
Hanno Becker554b0af2018-08-22 20:33:41 +01004438 ret = ssl_get_remaining_space_in_datagram( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01004439 if( ret < 0 )
4440 return( ret );
4441
4442 if( protected_record_size > (size_t) ret )
4443 {
4444 /* Should never happen */
4445 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4446 }
4447 }
4448#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker05ef8352012-05-08 09:17:57 +00004449
Hanno Becker6430faf2019-05-08 11:57:13 +01004450 /* Now write the potentially updated record content type. */
4451 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
4452
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004453 MBEDTLS_SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01004454 "version = [%d:%d], msglen = %d",
4455 ssl->out_hdr[0], ssl->out_hdr[1],
4456 ssl->out_hdr[2], len ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00004457
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004458 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01004459 ssl->out_hdr, protected_record_size );
Hanno Becker2b1e3542018-08-06 11:19:13 +01004460
4461 ssl->out_left += protected_record_size;
4462 ssl->out_hdr += protected_record_size;
4463 ssl_update_out_pointers( ssl, ssl->transform_out );
4464
Hanno Becker04484622018-08-06 09:49:38 +01004465 for( i = 8; i > ssl_ep_len( ssl ); i-- )
4466 if( ++ssl->cur_out_ctr[i - 1] != 0 )
4467 break;
4468
4469 /* The loop goes to its end iff the counter is wrapping */
4470 if( i == ssl_ep_len( ssl ) )
4471 {
4472 MBEDTLS_SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
4473 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
4474 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004475 }
4476
Hanno Becker67bc7c32018-08-06 11:33:50 +01004477#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker47db8772018-08-21 13:32:13 +01004478 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
4479 flush == SSL_DONT_FORCE_FLUSH )
Hanno Becker67bc7c32018-08-06 11:33:50 +01004480 {
Hanno Becker1f5a15d2018-08-21 13:31:31 +01004481 size_t remaining;
4482 ret = ssl_get_remaining_payload_in_datagram( ssl );
4483 if( ret < 0 )
4484 {
4485 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_get_remaining_payload_in_datagram",
4486 ret );
4487 return( ret );
4488 }
4489
4490 remaining = (size_t) ret;
Hanno Becker67bc7c32018-08-06 11:33:50 +01004491 if( remaining == 0 )
Hanno Beckerf0da6672018-08-28 09:55:10 +01004492 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004493 flush = SSL_FORCE_FLUSH;
Hanno Beckerf0da6672018-08-28 09:55:10 +01004494 }
Hanno Becker67bc7c32018-08-06 11:33:50 +01004495 else
4496 {
Hanno Becker513815a2018-08-20 11:56:09 +01004497 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Still %u bytes available in current datagram", (unsigned) remaining ) );
Hanno Becker67bc7c32018-08-06 11:33:50 +01004498 }
4499 }
4500#endif /* MBEDTLS_SSL_PROTO_DTLS */
4501
4502 if( ( flush == SSL_FORCE_FLUSH ) &&
4503 ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004504 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004505 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004506 return( ret );
4507 }
4508
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004509 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write record" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004510
4511 return( 0 );
4512}
4513
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004514#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckere25e3b72018-08-16 09:30:53 +01004515
4516static int ssl_hs_is_proper_fragment( mbedtls_ssl_context *ssl )
4517{
4518 if( ssl->in_msglen < ssl->in_hslen ||
4519 memcmp( ssl->in_msg + 6, "\0\0\0", 3 ) != 0 ||
4520 memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 )
4521 {
4522 return( 1 );
4523 }
4524 return( 0 );
4525}
Hanno Becker44650b72018-08-16 12:51:11 +01004526
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004527static uint32_t ssl_get_hs_frag_len( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004528{
4529 return( ( ssl->in_msg[9] << 16 ) |
4530 ( ssl->in_msg[10] << 8 ) |
4531 ssl->in_msg[11] );
4532}
4533
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004534static uint32_t ssl_get_hs_frag_off( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004535{
4536 return( ( ssl->in_msg[6] << 16 ) |
4537 ( ssl->in_msg[7] << 8 ) |
4538 ssl->in_msg[8] );
4539}
4540
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004541static int ssl_check_hs_header( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004542{
4543 uint32_t msg_len, frag_off, frag_len;
4544
4545 msg_len = ssl_get_hs_total_len( ssl );
4546 frag_off = ssl_get_hs_frag_off( ssl );
4547 frag_len = ssl_get_hs_frag_len( ssl );
4548
4549 if( frag_off > msg_len )
4550 return( -1 );
4551
4552 if( frag_len > msg_len - frag_off )
4553 return( -1 );
4554
4555 if( frag_len + 12 > ssl->in_msglen )
4556 return( -1 );
4557
4558 return( 0 );
4559}
4560
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004561/*
4562 * Mark bits in bitmask (used for DTLS HS reassembly)
4563 */
4564static void ssl_bitmask_set( unsigned char *mask, size_t offset, size_t len )
4565{
4566 unsigned int start_bits, end_bits;
4567
4568 start_bits = 8 - ( offset % 8 );
4569 if( start_bits != 8 )
4570 {
4571 size_t first_byte_idx = offset / 8;
4572
Manuel Pégourié-Gonnardac030522014-09-02 14:23:40 +02004573 /* Special case */
4574 if( len <= start_bits )
4575 {
4576 for( ; len != 0; len-- )
4577 mask[first_byte_idx] |= 1 << ( start_bits - len );
4578
4579 /* Avoid potential issues with offset or len becoming invalid */
4580 return;
4581 }
4582
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004583 offset += start_bits; /* Now offset % 8 == 0 */
4584 len -= start_bits;
4585
4586 for( ; start_bits != 0; start_bits-- )
4587 mask[first_byte_idx] |= 1 << ( start_bits - 1 );
4588 }
4589
4590 end_bits = len % 8;
4591 if( end_bits != 0 )
4592 {
4593 size_t last_byte_idx = ( offset + len ) / 8;
4594
4595 len -= end_bits; /* Now len % 8 == 0 */
4596
4597 for( ; end_bits != 0; end_bits-- )
4598 mask[last_byte_idx] |= 1 << ( 8 - end_bits );
4599 }
4600
4601 memset( mask + offset / 8, 0xFF, len / 8 );
4602}
4603
4604/*
4605 * Check that bitmask is full
4606 */
4607static int ssl_bitmask_check( unsigned char *mask, size_t len )
4608{
4609 size_t i;
4610
4611 for( i = 0; i < len / 8; i++ )
4612 if( mask[i] != 0xFF )
4613 return( -1 );
4614
4615 for( i = 0; i < len % 8; i++ )
4616 if( ( mask[len / 8] & ( 1 << ( 7 - i ) ) ) == 0 )
4617 return( -1 );
4618
4619 return( 0 );
4620}
4621
Hanno Becker56e205e2018-08-16 09:06:12 +01004622/* msg_len does not include the handshake header */
Hanno Becker65dc8852018-08-23 09:40:49 +01004623static size_t ssl_get_reassembly_buffer_size( size_t msg_len,
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004624 unsigned add_bitmap )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004625{
Hanno Becker56e205e2018-08-16 09:06:12 +01004626 size_t alloc_len;
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004627
Hanno Becker56e205e2018-08-16 09:06:12 +01004628 alloc_len = 12; /* Handshake header */
4629 alloc_len += msg_len; /* Content buffer */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004630
Hanno Beckerd07df862018-08-16 09:14:58 +01004631 if( add_bitmap )
4632 alloc_len += msg_len / 8 + ( msg_len % 8 != 0 ); /* Bitmap */
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004633
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004634 return( alloc_len );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004635}
Hanno Becker56e205e2018-08-16 09:06:12 +01004636
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004637#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004638
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004639static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl )
Hanno Becker12555c62018-08-16 12:47:53 +01004640{
4641 return( ( ssl->in_msg[1] << 16 ) |
4642 ( ssl->in_msg[2] << 8 ) |
4643 ssl->in_msg[3] );
4644}
Hanno Beckere25e3b72018-08-16 09:30:53 +01004645
Simon Butcher99000142016-10-13 17:21:01 +01004646int mbedtls_ssl_prepare_handshake_record( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004647{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004648 if( ssl->in_msglen < mbedtls_ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004649 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004650 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too short: %d",
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004651 ssl->in_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004652 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004653 }
4654
Hanno Becker12555c62018-08-16 12:47:53 +01004655 ssl->in_hslen = mbedtls_ssl_hs_hdr_len( ssl ) + ssl_get_hs_total_len( ssl );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004656
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004657 MBEDTLS_SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004658 " %d, type = %d, hslen = %d",
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004659 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004660
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004661#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004662 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004663 {
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004664 int ret;
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004665 unsigned int recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004666
Hanno Becker44650b72018-08-16 12:51:11 +01004667 if( ssl_check_hs_header( ssl ) != 0 )
4668 {
4669 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid handshake header" ) );
4670 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4671 }
4672
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004673 if( ssl->handshake != NULL &&
Hanno Beckerc76c6192017-06-06 10:03:17 +01004674 ( ( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER &&
4675 recv_msg_seq != ssl->handshake->in_msg_seq ) ||
4676 ( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
4677 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO ) ) )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004678 {
Hanno Becker9e1ec222018-08-15 15:54:43 +01004679 if( recv_msg_seq > ssl->handshake->in_msg_seq )
4680 {
4681 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received future handshake message of sequence number %u (next %u)",
4682 recv_msg_seq,
4683 ssl->handshake->in_msg_seq ) );
4684 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4685 }
4686
Manuel Pégourié-Gonnardfc572dd2014-10-09 17:56:57 +02004687 /* Retransmit only on last message from previous flight, to avoid
4688 * too many retransmissions.
4689 * Besides, No sane server ever retransmits HelloVerifyRequest */
4690 if( recv_msg_seq == ssl->handshake->in_flight_start_seq - 1 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004691 ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004692 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004693 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received message from last flight, "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004694 "message_seq = %d, start_of_flight = %d",
4695 recv_msg_seq,
4696 ssl->handshake->in_flight_start_seq ) );
4697
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004698 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004699 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004700 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004701 return( ret );
4702 }
4703 }
4704 else
4705 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004706 MBEDTLS_SSL_DEBUG_MSG( 2, ( "dropping out-of-sequence message: "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004707 "message_seq = %d, expected = %d",
4708 recv_msg_seq,
4709 ssl->handshake->in_msg_seq ) );
4710 }
4711
Hanno Becker90333da2017-10-10 11:27:13 +01004712 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004713 }
4714 /* Wait until message completion to increment in_msg_seq */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004715
Hanno Becker6d97ef52018-08-16 13:09:04 +01004716 /* Message reassembly is handled alongside buffering of future
4717 * messages; the commonality is that both handshake fragments and
Hanno Becker83ab41c2018-08-28 17:19:38 +01004718 * future messages cannot be forwarded immediately to the
Hanno Becker6d97ef52018-08-16 13:09:04 +01004719 * handshake logic layer. */
Hanno Beckere25e3b72018-08-16 09:30:53 +01004720 if( ssl_hs_is_proper_fragment( ssl ) == 1 )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004721 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004722 MBEDTLS_SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) );
Hanno Becker6d97ef52018-08-16 13:09:04 +01004723 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004724 }
4725 }
4726 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004727#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004728 /* With TLS we don't handle fragmentation (for now) */
4729 if( ssl->in_msglen < ssl->in_hslen )
4730 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004731 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) );
4732 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004733 }
4734
Simon Butcher99000142016-10-13 17:21:01 +01004735 return( 0 );
4736}
4737
4738void mbedtls_ssl_update_handshake_status( mbedtls_ssl_context *ssl )
4739{
Hanno Becker0271f962018-08-16 13:23:47 +01004740 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Simon Butcher99000142016-10-13 17:21:01 +01004741
Hanno Becker0271f962018-08-16 13:23:47 +01004742 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER && hs != NULL )
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02004743 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004744 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02004745 }
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004746
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004747 /* Handshake message is complete, increment counter */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004748#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004749 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004750 ssl->handshake != NULL )
4751 {
Hanno Becker0271f962018-08-16 13:23:47 +01004752 unsigned offset;
4753 mbedtls_ssl_hs_buffer *hs_buf;
Hanno Beckere25e3b72018-08-16 09:30:53 +01004754
Hanno Becker0271f962018-08-16 13:23:47 +01004755 /* Increment handshake sequence number */
4756 hs->in_msg_seq++;
4757
4758 /*
4759 * Clear up handshake buffering and reassembly structure.
4760 */
4761
4762 /* Free first entry */
Hanno Beckere605b192018-08-21 15:59:07 +01004763 ssl_buffering_free_slot( ssl, 0 );
Hanno Becker0271f962018-08-16 13:23:47 +01004764
4765 /* Shift all other entries */
Hanno Beckere605b192018-08-21 15:59:07 +01004766 for( offset = 0, hs_buf = &hs->buffering.hs[0];
4767 offset + 1 < MBEDTLS_SSL_MAX_BUFFERED_HS;
Hanno Becker0271f962018-08-16 13:23:47 +01004768 offset++, hs_buf++ )
4769 {
4770 *hs_buf = *(hs_buf + 1);
4771 }
4772
4773 /* Create a fresh last entry */
4774 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004775 }
4776#endif
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004777}
4778
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004779/*
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004780 * DTLS anti-replay: RFC 6347 4.1.2.6
4781 *
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004782 * in_window is a field of bits numbered from 0 (lsb) to 63 (msb).
4783 * Bit n is set iff record number in_window_top - n has been seen.
4784 *
4785 * Usually, in_window_top is the last record number seen and the lsb of
4786 * in_window is set. The only exception is the initial state (record number 0
4787 * not seen yet).
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004788 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004789#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4790static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004791{
4792 ssl->in_window_top = 0;
4793 ssl->in_window = 0;
4794}
4795
4796static inline uint64_t ssl_load_six_bytes( unsigned char *buf )
4797{
4798 return( ( (uint64_t) buf[0] << 40 ) |
4799 ( (uint64_t) buf[1] << 32 ) |
4800 ( (uint64_t) buf[2] << 24 ) |
4801 ( (uint64_t) buf[3] << 16 ) |
4802 ( (uint64_t) buf[4] << 8 ) |
4803 ( (uint64_t) buf[5] ) );
4804}
4805
4806/*
4807 * Return 0 if sequence number is acceptable, -1 otherwise
4808 */
Hanno Becker0183d692019-07-12 08:50:37 +01004809int mbedtls_ssl_dtls_replay_check( mbedtls_ssl_context const *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004810{
4811 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
4812 uint64_t bit;
4813
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004814 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004815 return( 0 );
4816
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004817 if( rec_seqnum > ssl->in_window_top )
4818 return( 0 );
4819
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004820 bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004821
4822 if( bit >= 64 )
4823 return( -1 );
4824
4825 if( ( ssl->in_window & ( (uint64_t) 1 << bit ) ) != 0 )
4826 return( -1 );
4827
4828 return( 0 );
4829}
4830
4831/*
4832 * Update replay window on new validated record
4833 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004834void mbedtls_ssl_dtls_replay_update( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004835{
4836 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
4837
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004838 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004839 return;
4840
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004841 if( rec_seqnum > ssl->in_window_top )
4842 {
4843 /* Update window_top and the contents of the window */
4844 uint64_t shift = rec_seqnum - ssl->in_window_top;
4845
4846 if( shift >= 64 )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004847 ssl->in_window = 1;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004848 else
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004849 {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004850 ssl->in_window <<= shift;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004851 ssl->in_window |= 1;
4852 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004853
4854 ssl->in_window_top = rec_seqnum;
4855 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004856 else
4857 {
4858 /* Mark that number as seen in the current window */
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004859 uint64_t bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004860
4861 if( bit < 64 ) /* Always true, but be extra sure */
4862 ssl->in_window |= (uint64_t) 1 << bit;
4863 }
4864}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004865#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004866
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004867#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004868/* Forward declaration */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02004869static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial );
4870
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004871/*
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004872 * Without any SSL context, check if a datagram looks like a ClientHello with
4873 * a valid cookie, and if it doesn't, generate a HelloVerifyRequest message.
Simon Butcher0789aed2015-09-11 17:15:17 +01004874 * Both input and output include full DTLS headers.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004875 *
4876 * - if cookie is valid, return 0
4877 * - if ClientHello looks superficially valid but cookie is not,
4878 * fill obuf and set olen, then
4879 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
4880 * - otherwise return a specific error code
4881 */
4882static int ssl_check_dtls_clihlo_cookie(
4883 mbedtls_ssl_cookie_write_t *f_cookie_write,
4884 mbedtls_ssl_cookie_check_t *f_cookie_check,
4885 void *p_cookie,
4886 const unsigned char *cli_id, size_t cli_id_len,
4887 const unsigned char *in, size_t in_len,
4888 unsigned char *obuf, size_t buf_len, size_t *olen )
4889{
4890 size_t sid_len, cookie_len;
4891 unsigned char *p;
4892
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004893 /*
4894 * Structure of ClientHello with record and handshake headers,
4895 * and expected values. We don't need to check a lot, more checks will be
4896 * done when actually parsing the ClientHello - skipping those checks
4897 * avoids code duplication and does not make cookie forging any easier.
4898 *
4899 * 0-0 ContentType type; copied, must be handshake
4900 * 1-2 ProtocolVersion version; copied
4901 * 3-4 uint16 epoch; copied, must be 0
4902 * 5-10 uint48 sequence_number; copied
4903 * 11-12 uint16 length; (ignored)
4904 *
4905 * 13-13 HandshakeType msg_type; (ignored)
4906 * 14-16 uint24 length; (ignored)
4907 * 17-18 uint16 message_seq; copied
4908 * 19-21 uint24 fragment_offset; copied, must be 0
4909 * 22-24 uint24 fragment_length; (ignored)
4910 *
4911 * 25-26 ProtocolVersion client_version; (ignored)
4912 * 27-58 Random random; (ignored)
4913 * 59-xx SessionID session_id; 1 byte len + sid_len content
4914 * 60+ opaque cookie<0..2^8-1>; 1 byte len + content
4915 * ...
4916 *
4917 * Minimum length is 61 bytes.
4918 */
4919 if( in_len < 61 ||
4920 in[0] != MBEDTLS_SSL_MSG_HANDSHAKE ||
4921 in[3] != 0 || in[4] != 0 ||
4922 in[19] != 0 || in[20] != 0 || in[21] != 0 )
4923 {
4924 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4925 }
4926
4927 sid_len = in[59];
4928 if( sid_len > in_len - 61 )
4929 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4930
4931 cookie_len = in[60 + sid_len];
4932 if( cookie_len > in_len - 60 )
4933 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4934
4935 if( f_cookie_check( p_cookie, in + sid_len + 61, cookie_len,
4936 cli_id, cli_id_len ) == 0 )
4937 {
4938 /* Valid cookie */
4939 return( 0 );
4940 }
4941
4942 /*
4943 * If we get here, we've got an invalid cookie, let's prepare HVR.
4944 *
4945 * 0-0 ContentType type; copied
4946 * 1-2 ProtocolVersion version; copied
4947 * 3-4 uint16 epoch; copied
4948 * 5-10 uint48 sequence_number; copied
4949 * 11-12 uint16 length; olen - 13
4950 *
4951 * 13-13 HandshakeType msg_type; hello_verify_request
4952 * 14-16 uint24 length; olen - 25
4953 * 17-18 uint16 message_seq; copied
4954 * 19-21 uint24 fragment_offset; copied
4955 * 22-24 uint24 fragment_length; olen - 25
4956 *
4957 * 25-26 ProtocolVersion server_version; 0xfe 0xff
4958 * 27-27 opaque cookie<0..2^8-1>; cookie_len = olen - 27, cookie
4959 *
4960 * Minimum length is 28.
4961 */
4962 if( buf_len < 28 )
4963 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
4964
4965 /* Copy most fields and adapt others */
4966 memcpy( obuf, in, 25 );
4967 obuf[13] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST;
4968 obuf[25] = 0xfe;
4969 obuf[26] = 0xff;
4970
4971 /* Generate and write actual cookie */
4972 p = obuf + 28;
4973 if( f_cookie_write( p_cookie,
4974 &p, obuf + buf_len, cli_id, cli_id_len ) != 0 )
4975 {
4976 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4977 }
4978
4979 *olen = p - obuf;
4980
4981 /* Go back and fill length fields */
4982 obuf[27] = (unsigned char)( *olen - 28 );
4983
4984 obuf[14] = obuf[22] = (unsigned char)( ( *olen - 25 ) >> 16 );
4985 obuf[15] = obuf[23] = (unsigned char)( ( *olen - 25 ) >> 8 );
4986 obuf[16] = obuf[24] = (unsigned char)( ( *olen - 25 ) );
4987
4988 obuf[11] = (unsigned char)( ( *olen - 13 ) >> 8 );
4989 obuf[12] = (unsigned char)( ( *olen - 13 ) );
4990
4991 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
4992}
4993
4994/*
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004995 * Handle possible client reconnect with the same UDP quadruplet
4996 * (RFC 6347 Section 4.2.8).
4997 *
4998 * Called by ssl_parse_record_header() in case we receive an epoch 0 record
4999 * that looks like a ClientHello.
5000 *
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005001 * - if the input looks like a ClientHello without cookies,
5002 * send back HelloVerifyRequest, then
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005003 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005004 * - if the input looks like a ClientHello with a valid cookie,
5005 * reset the session of the current context, and
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02005006 * return MBEDTLS_ERR_SSL_CLIENT_RECONNECT
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005007 * - if anything goes wrong, return a specific error code
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005008 *
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005009 * mbedtls_ssl_read_record() will ignore the record if anything else than
Simon Butcherd0bf6a32015-09-11 17:34:49 +01005010 * MBEDTLS_ERR_SSL_CLIENT_RECONNECT or 0 is returned, although this function
5011 * cannot not return 0.
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005012 */
5013static int ssl_handle_possible_reconnect( mbedtls_ssl_context *ssl )
5014{
5015 int ret;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005016 size_t len;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005017
Hanno Becker2fddd372019-07-10 14:37:41 +01005018 if( ssl->conf->f_cookie_write == NULL ||
5019 ssl->conf->f_cookie_check == NULL )
5020 {
5021 /* If we can't use cookies to verify reachability of the peer,
5022 * drop the record. */
5023 return( 0 );
5024 }
5025
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005026 ret = ssl_check_dtls_clihlo_cookie(
5027 ssl->conf->f_cookie_write,
5028 ssl->conf->f_cookie_check,
5029 ssl->conf->p_cookie,
5030 ssl->cli_id, ssl->cli_id_len,
5031 ssl->in_buf, ssl->in_left,
Angus Grattond8213d02016-05-25 20:56:48 +10005032 ssl->out_buf, MBEDTLS_SSL_OUT_CONTENT_LEN, &len );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005033
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005034 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_check_dtls_clihlo_cookie", ret );
5035
5036 if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005037 {
Brian J Murray1903fb32016-11-06 04:45:15 -08005038 /* Don't check write errors as we can't do anything here.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005039 * If the error is permanent we'll catch it later,
5040 * if it's not, then hopefully it'll work next time. */
5041 (void) ssl->f_send( ssl->p_bio, ssl->out_buf, len );
Hanno Becker2fddd372019-07-10 14:37:41 +01005042 ret = 0;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005043 }
5044
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005045 if( ret == 0 )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005046 {
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005047 /* Got a valid cookie, partially reset context */
5048 if( ( ret = ssl_session_reset_int( ssl, 1 ) ) != 0 )
5049 {
5050 MBEDTLS_SSL_DEBUG_RET( 1, "reset", ret );
5051 return( ret );
5052 }
5053
5054 return( MBEDTLS_ERR_SSL_CLIENT_RECONNECT );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005055 }
5056
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005057 return( ret );
5058}
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02005059#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005060
Hanno Beckerf661c9c2019-05-03 13:25:54 +01005061static int ssl_check_record_type( uint8_t record_type )
5062{
5063 if( record_type != MBEDTLS_SSL_MSG_HANDSHAKE &&
5064 record_type != MBEDTLS_SSL_MSG_ALERT &&
5065 record_type != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC &&
5066 record_type != MBEDTLS_SSL_MSG_APPLICATION_DATA )
5067 {
5068 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5069 }
5070
5071 return( 0 );
5072}
5073
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02005074/*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005075 * ContentType type;
5076 * ProtocolVersion version;
5077 * uint16 epoch; // DTLS only
5078 * uint48 sequence_number; // DTLS only
5079 * uint16 length;
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005080 *
5081 * Return 0 if header looks sane (and, for DTLS, the record is expected)
Simon Butcher207990d2015-12-16 01:51:30 +00005082 * MBEDTLS_ERR_SSL_INVALID_RECORD if the header looks bad,
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005083 * MBEDTLS_ERR_SSL_UNEXPECTED_RECORD (DTLS only) if sane but unexpected.
5084 *
5085 * With DTLS, mbedtls_ssl_read_record() will:
Simon Butcher207990d2015-12-16 01:51:30 +00005086 * 1. proceed with the record if this function returns 0
5087 * 2. drop only the current record if this function returns UNEXPECTED_RECORD
5088 * 3. return CLIENT_RECONNECT if this function return that value
5089 * 4. drop the whole datagram if this function returns anything else.
5090 * Point 2 is needed when the peer is resending, and we have already received
5091 * the first record from a datagram but are still waiting for the others.
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005092 */
Hanno Becker331de3d2019-07-12 11:10:16 +01005093static int ssl_parse_record_header( mbedtls_ssl_context const *ssl,
Hanno Beckere5e7e782019-07-11 12:29:35 +01005094 unsigned char *buf,
5095 size_t len,
5096 mbedtls_record *rec )
Paul Bakker5121ce52009-01-03 21:22:43 +00005097{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005098 int major_ver, minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00005099
Hanno Beckere5e7e782019-07-11 12:29:35 +01005100 size_t const rec_hdr_type_offset = 0;
5101 size_t const rec_hdr_type_len = 1;
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02005102
Hanno Beckere5e7e782019-07-11 12:29:35 +01005103 size_t const rec_hdr_version_offset = rec_hdr_type_offset +
5104 rec_hdr_type_len;
5105 size_t const rec_hdr_version_len = 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00005106
Hanno Beckere5e7e782019-07-11 12:29:35 +01005107 size_t const rec_hdr_ctr_len = 8;
5108#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckerf5466252019-07-25 10:13:02 +01005109 uint32_t rec_epoch;
Hanno Beckere5e7e782019-07-11 12:29:35 +01005110 size_t const rec_hdr_ctr_offset = rec_hdr_version_offset +
5111 rec_hdr_version_len;
5112
Hanno Beckera0e20d02019-05-15 14:03:01 +01005113#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckere5e7e782019-07-11 12:29:35 +01005114 size_t const rec_hdr_cid_offset = rec_hdr_ctr_offset +
5115 rec_hdr_ctr_len;
Hanno Beckerf5466252019-07-25 10:13:02 +01005116 size_t rec_hdr_cid_len = 0;
Hanno Beckere5e7e782019-07-11 12:29:35 +01005117#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
5118#endif /* MBEDTLS_SSL_PROTO_DTLS */
5119
5120 size_t rec_hdr_len_offset; /* To be determined */
5121 size_t const rec_hdr_len_len = 2;
5122
5123 /*
5124 * Check minimum lengths for record header.
5125 */
5126
5127#if defined(MBEDTLS_SSL_PROTO_DTLS)
5128 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5129 {
5130 rec_hdr_len_offset = rec_hdr_ctr_offset + rec_hdr_ctr_len;
5131 }
5132 else
5133#endif /* MBEDTLS_SSL_PROTO_DTLS */
5134 {
5135 rec_hdr_len_offset = rec_hdr_version_offset + rec_hdr_version_len;
5136 }
5137
5138 if( len < rec_hdr_len_offset + rec_hdr_len_len )
5139 {
5140 MBEDTLS_SSL_DEBUG_MSG( 1, ( "datagram of length %u too small to hold DTLS record header of length %u",
5141 (unsigned) len,
5142 (unsigned)( rec_hdr_len_len + rec_hdr_len_len ) ) );
5143 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5144 }
5145
5146 /*
5147 * Parse and validate record content type
5148 */
5149
5150 rec->type = buf[ rec_hdr_type_offset ];
Hanno Beckere5e7e782019-07-11 12:29:35 +01005151
5152 /* Check record content type */
5153#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
5154 rec->cid_len = 0;
5155
Hanno Beckerca59c2b2019-05-08 12:03:28 +01005156 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Hanno Beckere5e7e782019-07-11 12:29:35 +01005157 ssl->conf->cid_len != 0 &&
5158 rec->type == MBEDTLS_SSL_MSG_CID )
Hanno Beckerca59c2b2019-05-08 12:03:28 +01005159 {
5160 /* Shift pointers to account for record header including CID
5161 * struct {
5162 * ContentType special_type = tls12_cid;
5163 * ProtocolVersion version;
5164 * uint16 epoch;
5165 * uint48 sequence_number;
Hanno Becker8e55b0f2019-05-23 17:03:19 +01005166 * opaque cid[cid_length]; // Additional field compared to
5167 * // default DTLS record format
Hanno Beckerca59c2b2019-05-08 12:03:28 +01005168 * uint16 length;
5169 * opaque enc_content[DTLSCiphertext.length];
5170 * } DTLSCiphertext;
5171 */
5172
5173 /* So far, we only support static CID lengths
5174 * fixed in the configuration. */
Hanno Beckere5e7e782019-07-11 12:29:35 +01005175 rec_hdr_cid_len = ssl->conf->cid_len;
5176 rec_hdr_len_offset += rec_hdr_cid_len;
Hanno Beckere538d822019-07-10 14:50:10 +01005177
Hanno Beckere5e7e782019-07-11 12:29:35 +01005178 if( len < rec_hdr_len_offset + rec_hdr_len_len )
Hanno Beckere538d822019-07-10 14:50:10 +01005179 {
Hanno Beckere5e7e782019-07-11 12:29:35 +01005180 MBEDTLS_SSL_DEBUG_MSG( 1, ( "datagram of length %u too small to hold DTLS record header including CID, length %u",
5181 (unsigned) len,
5182 (unsigned)( rec_hdr_len_offset + rec_hdr_len_len ) ) );
Hanno Becker59be60e2019-07-10 14:53:43 +01005183 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Beckere538d822019-07-10 14:50:10 +01005184 }
Hanno Beckere5e7e782019-07-11 12:29:35 +01005185
Manuel Pégourié-Gonnard7e821b52019-08-02 10:17:15 +02005186 /* configured CID len is guaranteed at most 255, see
5187 * MBEDTLS_SSL_CID_OUT_LEN_MAX in check_config.h */
5188 rec->cid_len = (uint8_t) rec_hdr_cid_len;
Hanno Beckere5e7e782019-07-11 12:29:35 +01005189 memcpy( rec->cid, buf + rec_hdr_cid_offset, rec_hdr_cid_len );
Hanno Beckerca59c2b2019-05-08 12:03:28 +01005190 }
5191 else
Hanno Beckera0e20d02019-05-15 14:03:01 +01005192#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02005193 {
Hanno Beckere5e7e782019-07-11 12:29:35 +01005194 if( ssl_check_record_type( rec->type ) )
5195 {
Hanno Becker54229812019-07-12 14:40:00 +01005196 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type %u",
5197 (unsigned) rec->type ) );
Hanno Beckere5e7e782019-07-11 12:29:35 +01005198 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5199 }
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02005200 }
5201
Hanno Beckere5e7e782019-07-11 12:29:35 +01005202 /*
5203 * Parse and validate record version
5204 */
5205
Hanno Beckerd0b66d02019-07-26 08:07:03 +01005206 rec->ver[0] = buf[ rec_hdr_version_offset + 0 ];
5207 rec->ver[1] = buf[ rec_hdr_version_offset + 1 ];
Hanno Beckere5e7e782019-07-11 12:29:35 +01005208 mbedtls_ssl_read_version( &major_ver, &minor_ver,
5209 ssl->conf->transport,
Hanno Beckerd0b66d02019-07-26 08:07:03 +01005210 &rec->ver[0] );
Hanno Beckere5e7e782019-07-11 12:29:35 +01005211
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005212 if( major_ver != ssl->major_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00005213 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005214 MBEDTLS_SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
5215 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00005216 }
5217
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005218 if( minor_ver > ssl->conf->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00005219 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005220 MBEDTLS_SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
5221 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00005222 }
5223
Hanno Beckere5e7e782019-07-11 12:29:35 +01005224 /*
5225 * Parse/Copy record sequence number.
5226 */
Hanno Beckerca59c2b2019-05-08 12:03:28 +01005227
Hanno Beckere5e7e782019-07-11 12:29:35 +01005228#if defined(MBEDTLS_SSL_PROTO_DTLS)
5229 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02005230 {
Hanno Beckere5e7e782019-07-11 12:29:35 +01005231 /* Copy explicit record sequence number from input buffer. */
5232 memcpy( &rec->ctr[0], buf + rec_hdr_ctr_offset,
5233 rec_hdr_ctr_len );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02005234 }
Hanno Beckere5e7e782019-07-11 12:29:35 +01005235 else
5236#endif /* MBEDTLS_SSL_PROTO_DTLS */
5237 {
5238 /* Copy implicit record sequence number from SSL context structure. */
5239 memcpy( &rec->ctr[0], ssl->in_ctr, rec_hdr_ctr_len );
5240 }
Paul Bakker40e46942009-01-03 21:51:57 +00005241
Hanno Beckere5e7e782019-07-11 12:29:35 +01005242 /*
5243 * Parse record length.
5244 */
5245
Hanno Beckere5e7e782019-07-11 12:29:35 +01005246 rec->data_offset = rec_hdr_len_offset + rec_hdr_len_len;
Hanno Becker9eca2762019-07-25 10:16:37 +01005247 rec->data_len = ( (size_t) buf[ rec_hdr_len_offset + 0 ] << 8 ) |
5248 ( (size_t) buf[ rec_hdr_len_offset + 1 ] << 0 );
Hanno Beckere5e7e782019-07-11 12:29:35 +01005249 MBEDTLS_SSL_DEBUG_BUF( 4, "input record header", buf, rec->data_offset );
Paul Bakker5121ce52009-01-03 21:22:43 +00005250
Hanno Beckerca59c2b2019-05-08 12:03:28 +01005251 MBEDTLS_SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
Hanno Becker92d30f52019-05-23 17:03:44 +01005252 "version = [%d:%d], msglen = %d",
Hanno Beckere5e7e782019-07-11 12:29:35 +01005253 rec->type,
5254 major_ver, minor_ver, rec->data_len ) );
5255
5256 rec->buf = buf;
5257 rec->buf_len = rec->data_offset + rec->data_len;
Hanno Beckerca59c2b2019-05-08 12:03:28 +01005258
Hanno Beckerd417cc92019-07-26 08:20:27 +01005259 if( rec->data_len == 0 )
5260 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00005261
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005262 /*
Hanno Becker52c6dc62017-05-26 16:07:36 +01005263 * DTLS-related tests.
5264 * Check epoch before checking length constraint because
5265 * the latter varies with the epoch. E.g., if a ChangeCipherSpec
5266 * message gets duplicated before the corresponding Finished message,
5267 * the second ChangeCipherSpec should be discarded because it belongs
5268 * to an old epoch, but not because its length is shorter than
5269 * the minimum record length for packets using the new record transform.
5270 * Note that these two kinds of failures are handled differently,
5271 * as an unexpected record is silently skipped but an invalid
5272 * record leads to the entire datagram being dropped.
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005273 */
5274#if defined(MBEDTLS_SSL_PROTO_DTLS)
5275 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5276 {
Hanno Beckere5e7e782019-07-11 12:29:35 +01005277 rec_epoch = ( rec->ctr[0] << 8 ) | rec->ctr[1];
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005278
Hanno Becker955a5c92019-07-10 17:12:07 +01005279 /* Check that the datagram is large enough to contain a record
5280 * of the advertised length. */
Hanno Beckere5e7e782019-07-11 12:29:35 +01005281 if( len < rec->data_offset + rec->data_len )
Hanno Becker955a5c92019-07-10 17:12:07 +01005282 {
Hanno Beckere5e7e782019-07-11 12:29:35 +01005283 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Datagram of length %u too small to contain record of advertised length %u.",
5284 (unsigned) len,
5285 (unsigned)( rec->data_offset + rec->data_len ) ) );
Hanno Becker955a5c92019-07-10 17:12:07 +01005286 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5287 }
Hanno Becker37cfe732019-07-10 17:20:01 +01005288
Hanno Becker37cfe732019-07-10 17:20:01 +01005289 /* Records from other, non-matching epochs are silently discarded.
5290 * (The case of same-port Client reconnects must be considered in
5291 * the caller). */
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005292 if( rec_epoch != ssl->in_epoch )
5293 {
5294 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record from another epoch: "
5295 "expected %d, received %d",
5296 ssl->in_epoch, rec_epoch ) );
5297
Hanno Becker552f7472019-07-19 10:59:12 +01005298 /* Records from the next epoch are considered for buffering
5299 * (concretely: early Finished messages). */
5300 if( rec_epoch == (unsigned) ssl->in_epoch + 1 )
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005301 {
Hanno Becker552f7472019-07-19 10:59:12 +01005302 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Consider record for buffering" ) );
5303 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005304 }
Hanno Becker5f066e72018-08-16 14:56:31 +01005305
Hanno Becker2fddd372019-07-10 14:37:41 +01005306 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005307 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005308#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Hanno Becker37cfe732019-07-10 17:20:01 +01005309 /* For records from the correct epoch, check whether their
5310 * sequence number has been seen before. */
Hanno Becker2fddd372019-07-10 14:37:41 +01005311 else if( mbedtls_ssl_dtls_replay_check( ssl ) != 0 )
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005312 {
5313 MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record" ) );
5314 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
5315 }
5316#endif
5317 }
5318#endif /* MBEDTLS_SSL_PROTO_DTLS */
5319
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005320 return( 0 );
5321}
Paul Bakker5121ce52009-01-03 21:22:43 +00005322
Paul Bakker5121ce52009-01-03 21:22:43 +00005323
Hanno Becker2fddd372019-07-10 14:37:41 +01005324#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
5325static int ssl_check_client_reconnect( mbedtls_ssl_context *ssl )
5326{
5327 unsigned int rec_epoch = ( ssl->in_ctr[0] << 8 ) | ssl->in_ctr[1];
5328
5329 /*
5330 * Check for an epoch 0 ClientHello. We can't use in_msg here to
5331 * access the first byte of record content (handshake type), as we
5332 * have an active transform (possibly iv_len != 0), so use the
5333 * fact that the record header len is 13 instead.
5334 */
5335 if( rec_epoch == 0 &&
5336 ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
5337 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
5338 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
5339 ssl->in_left > 13 &&
5340 ssl->in_buf[13] == MBEDTLS_SSL_HS_CLIENT_HELLO )
5341 {
5342 MBEDTLS_SSL_DEBUG_MSG( 1, ( "possible client reconnect "
5343 "from the same port" ) );
5344 return( ssl_handle_possible_reconnect( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005345 }
5346
5347 return( 0 );
5348}
Hanno Becker2fddd372019-07-10 14:37:41 +01005349#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00005350
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005351/*
5352 * If applicable, decrypt (and decompress) record content
5353 */
Hanno Beckerfdf66042019-07-11 13:07:45 +01005354static int ssl_prepare_record_content( mbedtls_ssl_context *ssl,
5355 mbedtls_record *rec )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005356{
5357 int ret, done = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02005358
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005359 MBEDTLS_SSL_DEBUG_BUF( 4, "input record from network",
Hanno Beckerfdf66042019-07-11 13:07:45 +01005360 rec->buf, rec->buf_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00005361
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005362#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
5363 if( mbedtls_ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00005364 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005365 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_read()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00005366
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005367 ret = mbedtls_ssl_hw_record_read( ssl );
5368 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00005369 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005370 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_read", ret );
5371 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00005372 }
Paul Bakkerc7878112012-12-19 14:41:14 +01005373
5374 if( ret == 0 )
5375 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00005376 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005377#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00005378 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005379 {
Hanno Becker58ef0bf2019-07-12 09:35:58 +01005380 unsigned char const old_msg_type = rec->type;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00005381
Hanno Beckera18d1322018-01-03 14:27:32 +00005382 if( ( ret = mbedtls_ssl_decrypt_buf( ssl, ssl->transform_in,
Hanno Beckerfdf66042019-07-11 13:07:45 +01005383 rec ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005384 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005385 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
Hanno Becker8367ccc2019-05-14 11:30:10 +01005386
Hanno Beckera0e20d02019-05-15 14:03:01 +01005387#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker8367ccc2019-05-14 11:30:10 +01005388 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_CID &&
5389 ssl->conf->ignore_unexpected_cid
5390 == MBEDTLS_SSL_UNEXPECTED_CID_IGNORE )
5391 {
Hanno Beckere8d6afd2019-05-24 10:11:06 +01005392 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ignoring unexpected CID" ) );
Hanno Becker16ded982019-05-08 13:02:55 +01005393 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
Hanno Becker8367ccc2019-05-14 11:30:10 +01005394 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01005395#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker16ded982019-05-08 13:02:55 +01005396
Paul Bakker5121ce52009-01-03 21:22:43 +00005397 return( ret );
5398 }
5399
Hanno Becker58ef0bf2019-07-12 09:35:58 +01005400 if( old_msg_type != rec->type )
Hanno Becker6430faf2019-05-08 11:57:13 +01005401 {
5402 MBEDTLS_SSL_DEBUG_MSG( 4, ( "record type after decrypt (before %d): %d",
Hanno Becker58ef0bf2019-07-12 09:35:58 +01005403 old_msg_type, rec->type ) );
Hanno Becker6430faf2019-05-08 11:57:13 +01005404 }
5405
Hanno Becker1c0c37f2018-08-07 14:29:29 +01005406 MBEDTLS_SSL_DEBUG_BUF( 4, "input payload after decrypt",
Hanno Becker58ef0bf2019-07-12 09:35:58 +01005407 rec->buf + rec->data_offset, rec->data_len );
Hanno Becker1c0c37f2018-08-07 14:29:29 +01005408
Hanno Beckera0e20d02019-05-15 14:03:01 +01005409#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker6430faf2019-05-08 11:57:13 +01005410 /* We have already checked the record content type
5411 * in ssl_parse_record_header(), failing or silently
5412 * dropping the record in the case of an unknown type.
5413 *
5414 * Since with the use of CIDs, the record content type
5415 * might change during decryption, re-check the record
5416 * content type, but treat a failure as fatal this time. */
Hanno Becker58ef0bf2019-07-12 09:35:58 +01005417 if( ssl_check_record_type( rec->type ) )
Hanno Becker6430faf2019-05-08 11:57:13 +01005418 {
5419 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
5420 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5421 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01005422#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker6430faf2019-05-08 11:57:13 +01005423
Hanno Becker58ef0bf2019-07-12 09:35:58 +01005424 if( rec->data_len == 0 )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00005425 {
5426#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5427 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3
Hanno Becker58ef0bf2019-07-12 09:35:58 +01005428 && rec->type != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00005429 {
5430 /* TLS v1.2 explicitly disallows zero-length messages which are not application data */
5431 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid zero-length message type: %d", ssl->in_msgtype ) );
5432 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5433 }
5434#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
5435
5436 ssl->nb_zero++;
5437
5438 /*
5439 * Three or more empty messages may be a DoS attack
5440 * (excessive CPU consumption).
5441 */
5442 if( ssl->nb_zero > 3 )
5443 {
5444 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
Hanno Becker6e7700d2019-05-08 10:38:32 +01005445 "messages, possible DoS attack" ) );
5446 /* Treat the records as if they were not properly authenticated,
5447 * thereby failing the connection if we see more than allowed
5448 * by the configured bad MAC threshold. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00005449 return( MBEDTLS_ERR_SSL_INVALID_MAC );
5450 }
5451 }
5452 else
5453 ssl->nb_zero = 0;
5454
5455#if defined(MBEDTLS_SSL_PROTO_DTLS)
5456 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5457 {
5458 ; /* in_ctr read from peer, not maintained internally */
5459 }
5460 else
5461#endif
5462 {
5463 unsigned i;
5464 for( i = 8; i > ssl_ep_len( ssl ); i-- )
5465 if( ++ssl->in_ctr[i - 1] != 0 )
5466 break;
5467
5468 /* The loop goes to its end iff the counter is wrapping */
5469 if( i == ssl_ep_len( ssl ) )
5470 {
5471 MBEDTLS_SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
5472 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
5473 }
5474 }
5475
Paul Bakker5121ce52009-01-03 21:22:43 +00005476 }
5477
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005478#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00005479 if( ssl->transform_in != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005480 ssl->session_in->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00005481 {
5482 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
5483 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005484 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00005485 return( ret );
5486 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00005487 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005488#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00005489
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005490#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005491 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02005492 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005493 mbedtls_ssl_dtls_replay_update( ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02005494 }
5495#endif
5496
Hanno Beckerd96e10b2019-07-09 17:30:02 +01005497 /* Check actual (decrypted) record content length against
5498 * configured maximum. */
5499 if( ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
5500 {
5501 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
5502 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5503 }
5504
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005505 return( 0 );
5506}
5507
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005508static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005509
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005510/*
5511 * Read a record.
5512 *
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005513 * Silently ignore non-fatal alert (and for DTLS, invalid records as well,
5514 * RFC 6347 4.1.2.7) and continue reading until a valid record is found.
5515 *
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005516 */
Hanno Becker1097b342018-08-15 14:09:41 +01005517
5518/* Helper functions for mbedtls_ssl_read_record(). */
5519static int ssl_consume_current_message( mbedtls_ssl_context *ssl );
Hanno Beckere74d5562018-08-15 14:26:08 +01005520static int ssl_get_next_record( mbedtls_ssl_context *ssl );
5521static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl );
Hanno Becker4162b112018-08-15 14:05:04 +01005522
Hanno Becker327c93b2018-08-15 13:56:18 +01005523int mbedtls_ssl_read_record( mbedtls_ssl_context *ssl,
Hanno Becker3a0aad12018-08-20 09:44:02 +01005524 unsigned update_hs_digest )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005525{
5526 int ret;
5527
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005528 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read record" ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005529
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005530 if( ssl->keep_current_message == 0 )
5531 {
5532 do {
Simon Butcher99000142016-10-13 17:21:01 +01005533
Hanno Becker26994592018-08-15 14:14:59 +01005534 ret = ssl_consume_current_message( ssl );
Hanno Becker90333da2017-10-10 11:27:13 +01005535 if( ret != 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005536 return( ret );
Hanno Becker26994592018-08-15 14:14:59 +01005537
Hanno Beckere74d5562018-08-15 14:26:08 +01005538 if( ssl_record_is_in_progress( ssl ) == 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005539 {
Hanno Becker40f50842018-08-15 14:48:01 +01005540#if defined(MBEDTLS_SSL_PROTO_DTLS)
5541 int have_buffered = 0;
Hanno Beckere74d5562018-08-15 14:26:08 +01005542
Hanno Becker40f50842018-08-15 14:48:01 +01005543 /* We only check for buffered messages if the
5544 * current datagram is fully consumed. */
5545 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005546 ssl_next_record_is_in_datagram( ssl ) == 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01005547 {
Hanno Becker40f50842018-08-15 14:48:01 +01005548 if( ssl_load_buffered_message( ssl ) == 0 )
5549 have_buffered = 1;
5550 }
5551
5552 if( have_buffered == 0 )
5553#endif /* MBEDTLS_SSL_PROTO_DTLS */
5554 {
5555 ret = ssl_get_next_record( ssl );
5556 if( ret == MBEDTLS_ERR_SSL_CONTINUE_PROCESSING )
5557 continue;
5558
5559 if( ret != 0 )
5560 {
Hanno Beckerc573ac32018-08-28 17:15:25 +01005561 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_get_next_record" ), ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005562 return( ret );
5563 }
Hanno Beckere74d5562018-08-15 14:26:08 +01005564 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005565 }
5566
5567 ret = mbedtls_ssl_handle_message_type( ssl );
5568
Hanno Becker40f50842018-08-15 14:48:01 +01005569#if defined(MBEDTLS_SSL_PROTO_DTLS)
5570 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
5571 {
5572 /* Buffer future message */
5573 ret = ssl_buffer_message( ssl );
5574 if( ret != 0 )
5575 return( ret );
5576
5577 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
5578 }
5579#endif /* MBEDTLS_SSL_PROTO_DTLS */
5580
Hanno Becker90333da2017-10-10 11:27:13 +01005581 } while( MBEDTLS_ERR_SSL_NON_FATAL == ret ||
5582 MBEDTLS_ERR_SSL_CONTINUE_PROCESSING == ret );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005583
5584 if( 0 != ret )
Simon Butcher99000142016-10-13 17:21:01 +01005585 {
Hanno Becker05c4fc82017-11-09 14:34:06 +00005586 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_handle_message_type" ), ret );
Simon Butcher99000142016-10-13 17:21:01 +01005587 return( ret );
5588 }
5589
Hanno Becker327c93b2018-08-15 13:56:18 +01005590 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
Hanno Becker3a0aad12018-08-20 09:44:02 +01005591 update_hs_digest == 1 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005592 {
5593 mbedtls_ssl_update_handshake_status( ssl );
5594 }
Simon Butcher99000142016-10-13 17:21:01 +01005595 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005596 else
Simon Butcher99000142016-10-13 17:21:01 +01005597 {
Hanno Becker02f59072018-08-15 14:00:24 +01005598 MBEDTLS_SSL_DEBUG_MSG( 2, ( "reuse previously read message" ) );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005599 ssl->keep_current_message = 0;
Simon Butcher99000142016-10-13 17:21:01 +01005600 }
5601
5602 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read record" ) );
5603
5604 return( 0 );
5605}
5606
Hanno Becker40f50842018-08-15 14:48:01 +01005607#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005608static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl )
Simon Butcher99000142016-10-13 17:21:01 +01005609{
Hanno Becker40f50842018-08-15 14:48:01 +01005610 if( ssl->in_left > ssl->next_record_offset )
5611 return( 1 );
Simon Butcher99000142016-10-13 17:21:01 +01005612
Hanno Becker40f50842018-08-15 14:48:01 +01005613 return( 0 );
5614}
5615
5616static int ssl_load_buffered_message( mbedtls_ssl_context *ssl )
5617{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005618 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker37f95322018-08-16 13:55:32 +01005619 mbedtls_ssl_hs_buffer * hs_buf;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005620 int ret = 0;
5621
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005622 if( hs == NULL )
5623 return( -1 );
5624
Hanno Beckere00ae372018-08-20 09:39:42 +01005625 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_messsage" ) );
5626
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005627 if( ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC ||
5628 ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
5629 {
5630 /* Check if we have seen a ChangeCipherSpec before.
5631 * If yes, synthesize a CCS record. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01005632 if( !hs->buffering.seen_ccs )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005633 {
5634 MBEDTLS_SSL_DEBUG_MSG( 2, ( "CCS not seen in the current flight" ) );
5635 ret = -1;
Hanno Becker0d4b3762018-08-20 09:36:59 +01005636 goto exit;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005637 }
5638
Hanno Becker39b8bc92018-08-28 17:17:13 +01005639 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Injecting buffered CCS message" ) );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005640 ssl->in_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
5641 ssl->in_msglen = 1;
5642 ssl->in_msg[0] = 1;
5643
5644 /* As long as they are equal, the exact value doesn't matter. */
5645 ssl->in_left = 0;
5646 ssl->next_record_offset = 0;
5647
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01005648 hs->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005649 goto exit;
5650 }
Hanno Becker37f95322018-08-16 13:55:32 +01005651
Hanno Beckerb8f50142018-08-28 10:01:34 +01005652#if defined(MBEDTLS_DEBUG_C)
Hanno Becker37f95322018-08-16 13:55:32 +01005653 /* Debug only */
5654 {
5655 unsigned offset;
5656 for( offset = 1; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
5657 {
5658 hs_buf = &hs->buffering.hs[offset];
5659 if( hs_buf->is_valid == 1 )
5660 {
5661 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Future message with sequence number %u %s buffered.",
5662 hs->in_msg_seq + offset,
Hanno Beckera591c482018-08-28 17:20:00 +01005663 hs_buf->is_complete ? "fully" : "partially" ) );
Hanno Becker37f95322018-08-16 13:55:32 +01005664 }
5665 }
5666 }
Hanno Beckerb8f50142018-08-28 10:01:34 +01005667#endif /* MBEDTLS_DEBUG_C */
Hanno Becker37f95322018-08-16 13:55:32 +01005668
5669 /* Check if we have buffered and/or fully reassembled the
5670 * next handshake message. */
5671 hs_buf = &hs->buffering.hs[0];
5672 if( ( hs_buf->is_valid == 1 ) && ( hs_buf->is_complete == 1 ) )
5673 {
5674 /* Synthesize a record containing the buffered HS message. */
5675 size_t msg_len = ( hs_buf->data[1] << 16 ) |
5676 ( hs_buf->data[2] << 8 ) |
5677 hs_buf->data[3];
5678
5679 /* Double-check that we haven't accidentally buffered
5680 * a message that doesn't fit into the input buffer. */
5681 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
5682 {
5683 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5684 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5685 }
5686
5687 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message has been buffered - load" ) );
5688 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered handshake message (incl. header)",
5689 hs_buf->data, msg_len + 12 );
5690
5691 ssl->in_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
5692 ssl->in_hslen = msg_len + 12;
5693 ssl->in_msglen = msg_len + 12;
5694 memcpy( ssl->in_msg, hs_buf->data, ssl->in_hslen );
5695
5696 ret = 0;
5697 goto exit;
5698 }
5699 else
5700 {
5701 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message %u not or only partially bufffered",
5702 hs->in_msg_seq ) );
5703 }
5704
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005705 ret = -1;
5706
5707exit:
5708
5709 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_message" ) );
5710 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005711}
5712
Hanno Beckera02b0b42018-08-21 17:20:27 +01005713static int ssl_buffer_make_space( mbedtls_ssl_context *ssl,
5714 size_t desired )
5715{
5716 int offset;
5717 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005718 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Attempt to free buffered messages to have %u bytes available",
5719 (unsigned) desired ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005720
Hanno Becker01315ea2018-08-21 17:22:17 +01005721 /* Get rid of future records epoch first, if such exist. */
5722 ssl_free_buffered_record( ssl );
5723
5724 /* Check if we have enough space available now. */
5725 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5726 hs->buffering.total_bytes_buffered ) )
5727 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005728 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing future epoch record" ) );
Hanno Becker01315ea2018-08-21 17:22:17 +01005729 return( 0 );
5730 }
Hanno Beckera02b0b42018-08-21 17:20:27 +01005731
Hanno Becker4f432ad2018-08-28 10:02:32 +01005732 /* We don't have enough space to buffer the next expected handshake
5733 * message. Remove buffers used for future messages to gain space,
5734 * starting with the most distant one. */
Hanno Beckera02b0b42018-08-21 17:20:27 +01005735 for( offset = MBEDTLS_SSL_MAX_BUFFERED_HS - 1;
5736 offset >= 0; offset-- )
5737 {
5738 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Free buffering slot %d to make space for reassembly of next handshake message",
5739 offset ) );
5740
Hanno Beckerb309b922018-08-23 13:18:05 +01005741 ssl_buffering_free_slot( ssl, (uint8_t) offset );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005742
5743 /* Check if we have enough space available now. */
5744 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5745 hs->buffering.total_bytes_buffered ) )
5746 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005747 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing buffered HS messages" ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005748 return( 0 );
5749 }
5750 }
5751
5752 return( -1 );
5753}
5754
Hanno Becker40f50842018-08-15 14:48:01 +01005755static int ssl_buffer_message( mbedtls_ssl_context *ssl )
5756{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005757 int ret = 0;
5758 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5759
5760 if( hs == NULL )
5761 return( 0 );
5762
5763 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_buffer_message" ) );
5764
5765 switch( ssl->in_msgtype )
5766 {
5767 case MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC:
5768 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Remember CCS message" ) );
Hanno Beckere678eaa2018-08-21 14:57:46 +01005769
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01005770 hs->buffering.seen_ccs = 1;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005771 break;
5772
5773 case MBEDTLS_SSL_MSG_HANDSHAKE:
Hanno Becker37f95322018-08-16 13:55:32 +01005774 {
5775 unsigned recv_msg_seq_offset;
5776 unsigned recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
5777 mbedtls_ssl_hs_buffer *hs_buf;
5778 size_t msg_len = ssl->in_hslen - 12;
5779
5780 /* We should never receive an old handshake
5781 * message - double-check nonetheless. */
5782 if( recv_msg_seq < ssl->handshake->in_msg_seq )
5783 {
5784 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5785 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5786 }
5787
5788 recv_msg_seq_offset = recv_msg_seq - ssl->handshake->in_msg_seq;
5789 if( recv_msg_seq_offset >= MBEDTLS_SSL_MAX_BUFFERED_HS )
5790 {
5791 /* Silently ignore -- message too far in the future */
5792 MBEDTLS_SSL_DEBUG_MSG( 2,
5793 ( "Ignore future HS message with sequence number %u, "
5794 "buffering window %u - %u",
5795 recv_msg_seq, ssl->handshake->in_msg_seq,
5796 ssl->handshake->in_msg_seq + MBEDTLS_SSL_MAX_BUFFERED_HS - 1 ) );
5797
5798 goto exit;
5799 }
5800
5801 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering HS message with sequence number %u, offset %u ",
5802 recv_msg_seq, recv_msg_seq_offset ) );
5803
5804 hs_buf = &hs->buffering.hs[ recv_msg_seq_offset ];
5805
5806 /* Check if the buffering for this seq nr has already commenced. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01005807 if( !hs_buf->is_valid )
Hanno Becker37f95322018-08-16 13:55:32 +01005808 {
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005809 size_t reassembly_buf_sz;
5810
Hanno Becker37f95322018-08-16 13:55:32 +01005811 hs_buf->is_fragmented =
5812 ( ssl_hs_is_proper_fragment( ssl ) == 1 );
5813
5814 /* We copy the message back into the input buffer
5815 * after reassembly, so check that it's not too large.
5816 * This is an implementation-specific limitation
5817 * and not one from the standard, hence it is not
5818 * checked in ssl_check_hs_header(). */
Hanno Becker96a6c692018-08-21 15:56:03 +01005819 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker37f95322018-08-16 13:55:32 +01005820 {
5821 /* Ignore message */
5822 goto exit;
5823 }
5824
Hanno Beckere0b150f2018-08-21 15:51:03 +01005825 /* Check if we have enough space to buffer the message. */
5826 if( hs->buffering.total_bytes_buffered >
5827 MBEDTLS_SSL_DTLS_MAX_BUFFERING )
5828 {
5829 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5830 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5831 }
5832
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005833 reassembly_buf_sz = ssl_get_reassembly_buffer_size( msg_len,
5834 hs_buf->is_fragmented );
Hanno Beckere0b150f2018-08-21 15:51:03 +01005835
5836 if( reassembly_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5837 hs->buffering.total_bytes_buffered ) )
5838 {
5839 if( recv_msg_seq_offset > 0 )
5840 {
5841 /* If we can't buffer a future message because
5842 * of space limitations -- ignore. */
5843 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",
5844 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5845 (unsigned) hs->buffering.total_bytes_buffered ) );
5846 goto exit;
5847 }
Hanno Beckere1801392018-08-21 16:51:05 +01005848 else
5849 {
5850 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",
5851 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5852 (unsigned) hs->buffering.total_bytes_buffered ) );
5853 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005854
Hanno Beckera02b0b42018-08-21 17:20:27 +01005855 if( ssl_buffer_make_space( ssl, reassembly_buf_sz ) != 0 )
Hanno Becker55e9e2a2018-08-21 16:07:55 +01005856 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005857 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",
5858 (unsigned) msg_len,
5859 (unsigned) reassembly_buf_sz,
5860 MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Hanno Beckere0b150f2018-08-21 15:51:03 +01005861 (unsigned) hs->buffering.total_bytes_buffered ) );
Hanno Becker55e9e2a2018-08-21 16:07:55 +01005862 ret = MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
5863 goto exit;
5864 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005865 }
5866
5867 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %d",
5868 msg_len ) );
5869
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005870 hs_buf->data = mbedtls_calloc( 1, reassembly_buf_sz );
5871 if( hs_buf->data == NULL )
Hanno Becker37f95322018-08-16 13:55:32 +01005872 {
Hanno Beckere0b150f2018-08-21 15:51:03 +01005873 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
Hanno Becker37f95322018-08-16 13:55:32 +01005874 goto exit;
5875 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005876 hs_buf->data_len = reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01005877
5878 /* Prepare final header: copy msg_type, length and message_seq,
5879 * then add standardised fragment_offset and fragment_length */
5880 memcpy( hs_buf->data, ssl->in_msg, 6 );
5881 memset( hs_buf->data + 6, 0, 3 );
5882 memcpy( hs_buf->data + 9, hs_buf->data + 1, 3 );
5883
5884 hs_buf->is_valid = 1;
Hanno Beckere0b150f2018-08-21 15:51:03 +01005885
5886 hs->buffering.total_bytes_buffered += reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01005887 }
5888 else
5889 {
5890 /* Make sure msg_type and length are consistent */
5891 if( memcmp( hs_buf->data, ssl->in_msg, 4 ) != 0 )
5892 {
5893 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Fragment header mismatch - ignore" ) );
5894 /* Ignore */
5895 goto exit;
5896 }
5897 }
5898
Hanno Becker4422bbb2018-08-20 09:40:19 +01005899 if( !hs_buf->is_complete )
Hanno Becker37f95322018-08-16 13:55:32 +01005900 {
5901 size_t frag_len, frag_off;
5902 unsigned char * const msg = hs_buf->data + 12;
5903
5904 /*
5905 * Check and copy current fragment
5906 */
5907
5908 /* Validation of header fields already done in
5909 * mbedtls_ssl_prepare_handshake_record(). */
5910 frag_off = ssl_get_hs_frag_off( ssl );
5911 frag_len = ssl_get_hs_frag_len( ssl );
5912
5913 MBEDTLS_SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %d, length = %d",
5914 frag_off, frag_len ) );
5915 memcpy( msg + frag_off, ssl->in_msg + 12, frag_len );
5916
5917 if( hs_buf->is_fragmented )
5918 {
5919 unsigned char * const bitmask = msg + msg_len;
5920 ssl_bitmask_set( bitmask, frag_off, frag_len );
5921 hs_buf->is_complete = ( ssl_bitmask_check( bitmask,
5922 msg_len ) == 0 );
5923 }
5924 else
5925 {
5926 hs_buf->is_complete = 1;
5927 }
5928
5929 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message %scomplete",
5930 hs_buf->is_complete ? "" : "not yet " ) );
5931 }
5932
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005933 break;
Hanno Becker37f95322018-08-16 13:55:32 +01005934 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005935
5936 default:
Hanno Becker360bef32018-08-28 10:04:33 +01005937 /* We don't buffer other types of messages. */
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005938 break;
5939 }
5940
5941exit:
5942
5943 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_buffer_message" ) );
5944 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005945}
5946#endif /* MBEDTLS_SSL_PROTO_DTLS */
5947
Hanno Becker1097b342018-08-15 14:09:41 +01005948static int ssl_consume_current_message( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005949{
Hanno Becker4a810fb2017-05-24 16:27:30 +01005950 /*
Hanno Becker4a810fb2017-05-24 16:27:30 +01005951 * Consume last content-layer message and potentially
5952 * update in_msglen which keeps track of the contents'
5953 * consumption state.
5954 *
5955 * (1) Handshake messages:
5956 * Remove last handshake message, move content
5957 * and adapt in_msglen.
5958 *
5959 * (2) Alert messages:
5960 * Consume whole record content, in_msglen = 0.
5961 *
Hanno Becker4a810fb2017-05-24 16:27:30 +01005962 * (3) Change cipher spec:
5963 * Consume whole record content, in_msglen = 0.
5964 *
5965 * (4) Application data:
5966 * Don't do anything - the record layer provides
5967 * the application data as a stream transport
5968 * and consumes through mbedtls_ssl_read only.
5969 *
5970 */
5971
5972 /* Case (1): Handshake messages */
5973 if( ssl->in_hslen != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005974 {
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01005975 /* Hard assertion to be sure that no application data
5976 * is in flight, as corrupting ssl->in_msglen during
5977 * ssl->in_offt != NULL is fatal. */
5978 if( ssl->in_offt != NULL )
5979 {
5980 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5981 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5982 }
5983
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005984 /*
5985 * Get next Handshake message in the current record
5986 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005987
Hanno Becker4a810fb2017-05-24 16:27:30 +01005988 /* Notes:
Hanno Beckere72489d2017-10-23 13:23:50 +01005989 * (1) in_hslen is not necessarily the size of the
Hanno Becker4a810fb2017-05-24 16:27:30 +01005990 * current handshake content: If DTLS handshake
5991 * fragmentation is used, that's the fragment
5992 * size instead. Using the total handshake message
Hanno Beckere72489d2017-10-23 13:23:50 +01005993 * size here is faulty and should be changed at
5994 * some point.
Hanno Becker4a810fb2017-05-24 16:27:30 +01005995 * (2) While it doesn't seem to cause problems, one
5996 * has to be very careful not to assume that in_hslen
5997 * is always <= in_msglen in a sensible communication.
5998 * Again, it's wrong for DTLS handshake fragmentation.
5999 * The following check is therefore mandatory, and
6000 * should not be treated as a silently corrected assertion.
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01006001 * Additionally, ssl->in_hslen might be arbitrarily out of
6002 * bounds after handling a DTLS message with an unexpected
6003 * sequence number, see mbedtls_ssl_prepare_handshake_record.
Hanno Becker4a810fb2017-05-24 16:27:30 +01006004 */
6005 if( ssl->in_hslen < ssl->in_msglen )
6006 {
6007 ssl->in_msglen -= ssl->in_hslen;
6008 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
6009 ssl->in_msglen );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006010
Hanno Becker4a810fb2017-05-24 16:27:30 +01006011 MBEDTLS_SSL_DEBUG_BUF( 4, "remaining content in record",
6012 ssl->in_msg, ssl->in_msglen );
6013 }
6014 else
6015 {
6016 ssl->in_msglen = 0;
6017 }
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02006018
Hanno Becker4a810fb2017-05-24 16:27:30 +01006019 ssl->in_hslen = 0;
6020 }
6021 /* Case (4): Application data */
6022 else if( ssl->in_offt != NULL )
6023 {
6024 return( 0 );
6025 }
6026 /* Everything else (CCS & Alerts) */
6027 else
6028 {
6029 ssl->in_msglen = 0;
6030 }
6031
Hanno Becker1097b342018-08-15 14:09:41 +01006032 return( 0 );
6033}
Hanno Becker4a810fb2017-05-24 16:27:30 +01006034
Hanno Beckere74d5562018-08-15 14:26:08 +01006035static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl )
6036{
Hanno Becker4a810fb2017-05-24 16:27:30 +01006037 if( ssl->in_msglen > 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01006038 return( 1 );
6039
6040 return( 0 );
6041}
6042
Hanno Becker5f066e72018-08-16 14:56:31 +01006043#if defined(MBEDTLS_SSL_PROTO_DTLS)
6044
6045static void ssl_free_buffered_record( mbedtls_ssl_context *ssl )
6046{
6047 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
6048 if( hs == NULL )
6049 return;
6050
Hanno Becker01315ea2018-08-21 17:22:17 +01006051 if( hs->buffering.future_record.data != NULL )
Hanno Becker4a810fb2017-05-24 16:27:30 +01006052 {
Hanno Becker01315ea2018-08-21 17:22:17 +01006053 hs->buffering.total_bytes_buffered -=
6054 hs->buffering.future_record.len;
6055
6056 mbedtls_free( hs->buffering.future_record.data );
6057 hs->buffering.future_record.data = NULL;
6058 }
Hanno Becker5f066e72018-08-16 14:56:31 +01006059}
6060
6061static int ssl_load_buffered_record( mbedtls_ssl_context *ssl )
6062{
6063 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
6064 unsigned char * rec;
6065 size_t rec_len;
6066 unsigned rec_epoch;
6067
6068 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
6069 return( 0 );
6070
6071 if( hs == NULL )
6072 return( 0 );
6073
Hanno Becker5f066e72018-08-16 14:56:31 +01006074 rec = hs->buffering.future_record.data;
6075 rec_len = hs->buffering.future_record.len;
6076 rec_epoch = hs->buffering.future_record.epoch;
6077
6078 if( rec == NULL )
6079 return( 0 );
6080
Hanno Becker4cb782d2018-08-20 11:19:05 +01006081 /* Only consider loading future records if the
6082 * input buffer is empty. */
Hanno Beckeref7afdf2018-08-28 17:16:31 +01006083 if( ssl_next_record_is_in_datagram( ssl ) == 1 )
Hanno Becker4cb782d2018-08-20 11:19:05 +01006084 return( 0 );
6085
Hanno Becker5f066e72018-08-16 14:56:31 +01006086 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_record" ) );
6087
6088 if( rec_epoch != ssl->in_epoch )
6089 {
6090 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffered record not from current epoch." ) );
6091 goto exit;
6092 }
6093
6094 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Found buffered record from current epoch - load" ) );
6095
6096 /* Double-check that the record is not too large */
6097 if( rec_len > MBEDTLS_SSL_IN_BUFFER_LEN -
6098 (size_t)( ssl->in_hdr - ssl->in_buf ) )
6099 {
6100 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6101 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6102 }
6103
6104 memcpy( ssl->in_hdr, rec, rec_len );
6105 ssl->in_left = rec_len;
6106 ssl->next_record_offset = 0;
6107
6108 ssl_free_buffered_record( ssl );
6109
6110exit:
6111 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_record" ) );
6112 return( 0 );
6113}
6114
Hanno Becker519f15d2019-07-11 12:43:20 +01006115static int ssl_buffer_future_record( mbedtls_ssl_context *ssl,
6116 mbedtls_record const *rec )
Hanno Becker5f066e72018-08-16 14:56:31 +01006117{
6118 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker5f066e72018-08-16 14:56:31 +01006119
6120 /* Don't buffer future records outside handshakes. */
6121 if( hs == NULL )
6122 return( 0 );
6123
6124 /* Only buffer handshake records (we are only interested
6125 * in Finished messages). */
Hanno Becker519f15d2019-07-11 12:43:20 +01006126 if( rec->type != MBEDTLS_SSL_MSG_HANDSHAKE )
Hanno Becker5f066e72018-08-16 14:56:31 +01006127 return( 0 );
6128
6129 /* Don't buffer more than one future epoch record. */
6130 if( hs->buffering.future_record.data != NULL )
6131 return( 0 );
6132
Hanno Becker01315ea2018-08-21 17:22:17 +01006133 /* Don't buffer record if there's not enough buffering space remaining. */
Hanno Becker519f15d2019-07-11 12:43:20 +01006134 if( rec->buf_len > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
Hanno Becker01315ea2018-08-21 17:22:17 +01006135 hs->buffering.total_bytes_buffered ) )
6136 {
6137 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 +01006138 (unsigned) rec->buf_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Hanno Becker01315ea2018-08-21 17:22:17 +01006139 (unsigned) hs->buffering.total_bytes_buffered ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006140 return( 0 );
6141 }
6142
Hanno Becker5f066e72018-08-16 14:56:31 +01006143 /* Buffer record */
6144 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffer record from epoch %u",
6145 ssl->in_epoch + 1 ) );
Hanno Becker519f15d2019-07-11 12:43:20 +01006146 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered record", rec->buf, rec->buf_len );
Hanno Becker5f066e72018-08-16 14:56:31 +01006147
6148 /* ssl_parse_record_header() only considers records
6149 * of the next epoch as candidates for buffering. */
6150 hs->buffering.future_record.epoch = ssl->in_epoch + 1;
Hanno Becker519f15d2019-07-11 12:43:20 +01006151 hs->buffering.future_record.len = rec->buf_len;
Hanno Becker5f066e72018-08-16 14:56:31 +01006152
6153 hs->buffering.future_record.data =
6154 mbedtls_calloc( 1, hs->buffering.future_record.len );
6155 if( hs->buffering.future_record.data == NULL )
6156 {
6157 /* If we run out of RAM trying to buffer a
6158 * record from the next epoch, just ignore. */
6159 return( 0 );
6160 }
6161
Hanno Becker519f15d2019-07-11 12:43:20 +01006162 memcpy( hs->buffering.future_record.data, rec->buf, rec->buf_len );
Hanno Becker5f066e72018-08-16 14:56:31 +01006163
Hanno Becker519f15d2019-07-11 12:43:20 +01006164 hs->buffering.total_bytes_buffered += rec->buf_len;
Hanno Becker5f066e72018-08-16 14:56:31 +01006165 return( 0 );
6166}
6167
6168#endif /* MBEDTLS_SSL_PROTO_DTLS */
6169
Hanno Beckere74d5562018-08-15 14:26:08 +01006170static int ssl_get_next_record( mbedtls_ssl_context *ssl )
Hanno Becker1097b342018-08-15 14:09:41 +01006171{
6172 int ret;
Hanno Beckere5e7e782019-07-11 12:29:35 +01006173 mbedtls_record rec;
Hanno Becker1097b342018-08-15 14:09:41 +01006174
Hanno Becker5f066e72018-08-16 14:56:31 +01006175#if defined(MBEDTLS_SSL_PROTO_DTLS)
6176 /* We might have buffered a future record; if so,
6177 * and if the epoch matches now, load it.
6178 * On success, this call will set ssl->in_left to
6179 * the length of the buffered record, so that
6180 * the calls to ssl_fetch_input() below will
6181 * essentially be no-ops. */
6182 ret = ssl_load_buffered_record( ssl );
6183 if( ret != 0 )
6184 return( ret );
6185#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker4a810fb2017-05-24 16:27:30 +01006186
Hanno Beckerca59c2b2019-05-08 12:03:28 +01006187 /* Ensure that we have enough space available for the default form
6188 * of TLS / DTLS record headers (5 Bytes for TLS, 13 Bytes for DTLS,
6189 * with no space for CIDs counted in). */
6190 ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_in_hdr_len( ssl ) );
6191 if( ret != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006192 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006193 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006194 return( ret );
6195 }
6196
Hanno Beckere5e7e782019-07-11 12:29:35 +01006197 ret = ssl_parse_record_header( ssl, ssl->in_hdr, ssl->in_left, &rec );
6198 if( ret != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006199 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006200#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker2fddd372019-07-10 14:37:41 +01006201 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006202 {
Hanno Becker5f066e72018-08-16 14:56:31 +01006203 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
6204 {
Hanno Becker519f15d2019-07-11 12:43:20 +01006205 ret = ssl_buffer_future_record( ssl, &rec );
Hanno Becker5f066e72018-08-16 14:56:31 +01006206 if( ret != 0 )
6207 return( ret );
6208
6209 /* Fall through to handling of unexpected records */
6210 ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
6211 }
6212
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01006213 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD )
6214 {
Hanno Becker2fddd372019-07-10 14:37:41 +01006215#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Hanno Beckerd8bf8ce2019-07-12 09:23:47 +01006216 /* Reset in pointers to default state for TLS/DTLS records,
6217 * assuming no CID and no offset between record content and
6218 * record plaintext. */
6219 ssl_update_in_pointers( ssl );
6220
Hanno Becker7ae20e02019-07-12 08:33:49 +01006221 /* Setup internal message pointers from record structure. */
6222 ssl->in_msgtype = rec.type;
6223#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
6224 ssl->in_len = ssl->in_cid + rec.cid_len;
6225#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
6226 ssl->in_iv = ssl->in_msg = ssl->in_len + 2;
6227 ssl->in_msglen = rec.data_len;
6228
Hanno Becker2fddd372019-07-10 14:37:41 +01006229 ret = ssl_check_client_reconnect( ssl );
6230 if( ret != 0 )
6231 return( ret );
6232#endif
6233
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01006234 /* Skip unexpected record (but not whole datagram) */
Hanno Becker4acada32019-07-11 12:48:53 +01006235 ssl->next_record_offset = rec.buf_len;
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006236
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01006237 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding unexpected record "
6238 "(header)" ) );
6239 }
6240 else
6241 {
6242 /* Skip invalid record and the rest of the datagram */
6243 ssl->next_record_offset = 0;
6244 ssl->in_left = 0;
6245
6246 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record "
6247 "(header)" ) );
6248 }
6249
6250 /* Get next record */
Hanno Becker90333da2017-10-10 11:27:13 +01006251 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006252 }
Hanno Becker2fddd372019-07-10 14:37:41 +01006253 else
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006254#endif
Hanno Becker2fddd372019-07-10 14:37:41 +01006255 {
6256 return( ret );
6257 }
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006258 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006259
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006260#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006261 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Beckere65ce782017-05-22 14:47:48 +01006262 {
Hanno Beckera8814792019-07-10 15:01:45 +01006263 /* Remember offset of next record within datagram. */
Hanno Beckerf50da502019-07-11 12:50:10 +01006264 ssl->next_record_offset = rec.buf_len;
Hanno Beckere65ce782017-05-22 14:47:48 +01006265 if( ssl->next_record_offset < ssl->in_left )
6266 {
6267 MBEDTLS_SSL_DEBUG_MSG( 3, ( "more than one record within datagram" ) );
6268 }
6269 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006270 else
6271#endif
Hanno Beckera8814792019-07-10 15:01:45 +01006272 {
Hanno Becker955a5c92019-07-10 17:12:07 +01006273 /*
6274 * Fetch record contents from underlying transport.
6275 */
Hanno Beckera3175662019-07-11 12:50:29 +01006276 ret = mbedtls_ssl_fetch_input( ssl, rec.buf_len );
Hanno Beckera8814792019-07-10 15:01:45 +01006277 if( ret != 0 )
6278 {
6279 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
6280 return( ret );
6281 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006282
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006283 ssl->in_left = 0;
Hanno Beckera8814792019-07-10 15:01:45 +01006284 }
6285
6286 /*
6287 * Decrypt record contents.
6288 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006289
Hanno Beckerfdf66042019-07-11 13:07:45 +01006290 if( ( ret = ssl_prepare_record_content( ssl, &rec ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006291 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006292#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006293 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006294 {
6295 /* Silently discard invalid records */
Hanno Becker82e2a392019-05-03 16:36:59 +01006296 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006297 {
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02006298 /* Except when waiting for Finished as a bad mac here
6299 * probably means something went wrong in the handshake
6300 * (eg wrong psk used, mitm downgrade attempt, etc.) */
6301 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED ||
6302 ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
6303 {
6304#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
6305 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
6306 {
6307 mbedtls_ssl_send_alert_message( ssl,
6308 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6309 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
6310 }
6311#endif
6312 return( ret );
6313 }
6314
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006315#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006316 if( ssl->conf->badmac_limit != 0 &&
6317 ++ssl->badmac_seen >= ssl->conf->badmac_limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02006318 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006319 MBEDTLS_SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) );
6320 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02006321 }
6322#endif
6323
Hanno Becker4a810fb2017-05-24 16:27:30 +01006324 /* As above, invalid records cause
6325 * dismissal of the whole datagram. */
6326
6327 ssl->next_record_offset = 0;
6328 ssl->in_left = 0;
6329
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006330 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record (mac)" ) );
Hanno Becker90333da2017-10-10 11:27:13 +01006331 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006332 }
6333
6334 return( ret );
6335 }
6336 else
6337#endif
6338 {
6339 /* Error out (and send alert) on invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006340#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
6341 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006342 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006343 mbedtls_ssl_send_alert_message( ssl,
6344 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6345 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006346 }
6347#endif
6348 return( ret );
6349 }
6350 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006351
Hanno Becker44d89b22019-07-12 09:40:44 +01006352
6353 /* Reset in pointers to default state for TLS/DTLS records,
6354 * assuming no CID and no offset between record content and
6355 * record plaintext. */
6356 ssl_update_in_pointers( ssl );
Hanno Becker44d89b22019-07-12 09:40:44 +01006357#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
6358 ssl->in_len = ssl->in_cid + rec.cid_len;
6359#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
6360 ssl->in_iv = ssl->in_msg = ssl->in_len + 2;
Hanno Becker44d89b22019-07-12 09:40:44 +01006361
Hanno Becker8685c822019-07-12 09:37:30 +01006362 /* The record content type may change during decryption,
6363 * so re-read it. */
6364 ssl->in_msgtype = rec.type;
6365 /* Also update the input buffer, because unfortunately
6366 * the server-side ssl_parse_client_hello() reparses the
6367 * record header when receiving a ClientHello initiating
6368 * a renegotiation. */
6369 ssl->in_hdr[0] = rec.type;
6370 ssl->in_msg = rec.buf + rec.data_offset;
6371 ssl->in_msglen = rec.data_len;
6372 ssl->in_len[0] = (unsigned char)( rec.data_len >> 8 );
6373 ssl->in_len[1] = (unsigned char)( rec.data_len );
6374
Simon Butcher99000142016-10-13 17:21:01 +01006375 return( 0 );
6376}
6377
6378int mbedtls_ssl_handle_message_type( mbedtls_ssl_context *ssl )
6379{
6380 int ret;
6381
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006382 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006383 * Handle particular types of records
6384 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006385 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00006386 {
Simon Butcher99000142016-10-13 17:21:01 +01006387 if( ( ret = mbedtls_ssl_prepare_handshake_record( ssl ) ) != 0 )
6388 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01006389 return( ret );
Simon Butcher99000142016-10-13 17:21:01 +01006390 }
Paul Bakker5121ce52009-01-03 21:22:43 +00006391 }
6392
Hanno Beckere678eaa2018-08-21 14:57:46 +01006393 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006394 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01006395 if( ssl->in_msglen != 1 )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006396 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01006397 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, len: %d",
6398 ssl->in_msglen ) );
6399 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006400 }
6401
Hanno Beckere678eaa2018-08-21 14:57:46 +01006402 if( ssl->in_msg[0] != 1 )
6403 {
6404 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, content: %02x",
6405 ssl->in_msg[0] ) );
6406 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
6407 }
6408
6409#if defined(MBEDTLS_SSL_PROTO_DTLS)
6410 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
6411 ssl->state != MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC &&
6412 ssl->state != MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
6413 {
6414 if( ssl->handshake == NULL )
6415 {
6416 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping ChangeCipherSpec outside handshake" ) );
6417 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
6418 }
6419
6420 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received out-of-order ChangeCipherSpec - remember" ) );
6421 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
6422 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006423#endif
Hanno Beckere678eaa2018-08-21 14:57:46 +01006424 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006425
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006426 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00006427 {
Angus Gratton1a7a17e2018-06-20 15:43:50 +10006428 if( ssl->in_msglen != 2 )
6429 {
6430 /* Note: Standard allows for more than one 2 byte alert
6431 to be packed in a single message, but Mbed TLS doesn't
6432 currently support this. */
6433 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid alert message, len: %d",
6434 ssl->in_msglen ) );
6435 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
6436 }
6437
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006438 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
Paul Bakker5121ce52009-01-03 21:22:43 +00006439 ssl->in_msg[0], ssl->in_msg[1] ) );
6440
6441 /*
Simon Butcher459a9502015-10-27 16:09:03 +00006442 * Ignore non-fatal alerts, except close_notify and no_renegotiation
Paul Bakker5121ce52009-01-03 21:22:43 +00006443 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006444 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006445 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006446 MBEDTLS_SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
Paul Bakker2770fbd2012-07-03 13:30:23 +00006447 ssl->in_msg[1] ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006448 return( MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006449 }
6450
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006451 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6452 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00006453 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006454 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
6455 return( MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00006456 }
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02006457
6458#if defined(MBEDTLS_SSL_RENEGOTIATION_ENABLED)
6459 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6460 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION )
6461 {
Hanno Becker90333da2017-10-10 11:27:13 +01006462 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no renegotiation alert" ) );
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02006463 /* Will be handled when trying to parse ServerHello */
6464 return( 0 );
6465 }
6466#endif
6467
6468#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_SRV_C)
6469 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
6470 ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
6471 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6472 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
6473 {
6474 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no_cert" ) );
6475 /* Will be handled in mbedtls_ssl_parse_certificate() */
6476 return( 0 );
6477 }
6478#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
6479
6480 /* Silently ignore: fetch new message */
Simon Butcher99000142016-10-13 17:21:01 +01006481 return MBEDTLS_ERR_SSL_NON_FATAL;
Paul Bakker5121ce52009-01-03 21:22:43 +00006482 }
6483
Hanno Beckerc76c6192017-06-06 10:03:17 +01006484#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker37ae9522019-05-03 16:54:26 +01006485 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Beckerc76c6192017-06-06 10:03:17 +01006486 {
Hanno Becker37ae9522019-05-03 16:54:26 +01006487 /* Drop unexpected ApplicationData records,
6488 * except at the beginning of renegotiations */
6489 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA &&
6490 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER
6491#if defined(MBEDTLS_SSL_RENEGOTIATION)
6492 && ! ( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
6493 ssl->state == MBEDTLS_SSL_SERVER_HELLO )
Hanno Beckerc76c6192017-06-06 10:03:17 +01006494#endif
Hanno Becker37ae9522019-05-03 16:54:26 +01006495 )
6496 {
6497 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) );
6498 return( MBEDTLS_ERR_SSL_NON_FATAL );
6499 }
6500
6501 if( ssl->handshake != NULL &&
6502 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
6503 {
6504 ssl_handshake_wrapup_free_hs_transform( ssl );
6505 }
6506 }
Hanno Becker4a4af9f2019-05-08 16:26:21 +01006507#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Beckerc76c6192017-06-06 10:03:17 +01006508
Paul Bakker5121ce52009-01-03 21:22:43 +00006509 return( 0 );
6510}
6511
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006512int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00006513{
6514 int ret;
6515
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006516 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
6517 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6518 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00006519 {
6520 return( ret );
6521 }
6522
6523 return( 0 );
6524}
6525
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006526int mbedtls_ssl_send_alert_message( mbedtls_ssl_context *ssl,
Paul Bakker0a925182012-04-16 06:46:41 +00006527 unsigned char level,
6528 unsigned char message )
6529{
6530 int ret;
6531
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02006532 if( ssl == NULL || ssl->conf == NULL )
6533 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6534
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006535 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006536 MBEDTLS_SSL_DEBUG_MSG( 3, ( "send alert level=%u message=%u", level, message ));
Paul Bakker0a925182012-04-16 06:46:41 +00006537
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006538 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
Paul Bakker0a925182012-04-16 06:46:41 +00006539 ssl->out_msglen = 2;
6540 ssl->out_msg[0] = level;
6541 ssl->out_msg[1] = message;
6542
Hanno Becker67bc7c32018-08-06 11:33:50 +01006543 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00006544 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006545 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker0a925182012-04-16 06:46:41 +00006546 return( ret );
6547 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006548 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
Paul Bakker0a925182012-04-16 06:46:41 +00006549
6550 return( 0 );
6551}
6552
Hanno Beckerb9d44792019-02-08 07:19:04 +00006553#if defined(MBEDTLS_X509_CRT_PARSE_C)
6554static void ssl_clear_peer_cert( mbedtls_ssl_session *session )
6555{
6556#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6557 if( session->peer_cert != NULL )
6558 {
6559 mbedtls_x509_crt_free( session->peer_cert );
6560 mbedtls_free( session->peer_cert );
6561 session->peer_cert = NULL;
6562 }
6563#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6564 if( session->peer_cert_digest != NULL )
6565 {
6566 /* Zeroization is not necessary. */
6567 mbedtls_free( session->peer_cert_digest );
6568 session->peer_cert_digest = NULL;
6569 session->peer_cert_digest_type = MBEDTLS_MD_NONE;
6570 session->peer_cert_digest_len = 0;
6571 }
6572#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6573}
6574#endif /* MBEDTLS_X509_CRT_PARSE_C */
6575
Paul Bakker5121ce52009-01-03 21:22:43 +00006576/*
6577 * Handshake functions
6578 */
Hanno Becker21489932019-02-05 13:20:55 +00006579#if !defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Gilles Peskinef9828522017-05-03 12:28:43 +02006580/* No certificate support -> dummy functions */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006581int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006582{
Hanno Beckere694c3e2017-12-27 21:34:08 +00006583 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6584 ssl->handshake->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00006585
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006586 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006587
Hanno Becker7177a882019-02-05 13:36:46 +00006588 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02006589 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006590 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02006591 ssl->state++;
6592 return( 0 );
6593 }
6594
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006595 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6596 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006597}
6598
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006599int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006600{
Hanno Beckere694c3e2017-12-27 21:34:08 +00006601 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6602 ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006603
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006604 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006605
Hanno Becker7177a882019-02-05 13:36:46 +00006606 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006607 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006608 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006609 ssl->state++;
6610 return( 0 );
6611 }
6612
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006613 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6614 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006615}
Gilles Peskinef9828522017-05-03 12:28:43 +02006616
Hanno Becker21489932019-02-05 13:20:55 +00006617#else /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Gilles Peskinef9828522017-05-03 12:28:43 +02006618/* Some certificate support -> implement write and parse */
6619
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006620int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006621{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006622 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006623 size_t i, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006624 const mbedtls_x509_crt *crt;
Hanno Beckere694c3e2017-12-27 21:34:08 +00006625 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6626 ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006627
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006628 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006629
Hanno Becker7177a882019-02-05 13:36:46 +00006630 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006631 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006632 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006633 ssl->state++;
6634 return( 0 );
6635 }
6636
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006637#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006638 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00006639 {
6640 if( ssl->client_auth == 0 )
6641 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006642 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006643 ssl->state++;
6644 return( 0 );
6645 }
6646
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006647#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00006648 /*
6649 * If using SSLv3 and got no cert, send an Alert message
6650 * (otherwise an empty Certificate message will be sent).
6651 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006652 if( mbedtls_ssl_own_cert( ssl ) == NULL &&
6653 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006654 {
6655 ssl->out_msglen = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006656 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
6657 ssl->out_msg[0] = MBEDTLS_SSL_ALERT_LEVEL_WARNING;
6658 ssl->out_msg[1] = MBEDTLS_SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00006659
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006660 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006661 goto write_msg;
6662 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006663#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006664 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006665#endif /* MBEDTLS_SSL_CLI_C */
6666#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006667 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00006668 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006669 if( mbedtls_ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006670 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006671 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
6672 return( MBEDTLS_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00006673 }
6674 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006675#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006676
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006677 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006678
6679 /*
6680 * 0 . 0 handshake type
6681 * 1 . 3 handshake length
6682 * 4 . 6 length of all certs
6683 * 7 . 9 length of cert. 1
6684 * 10 . n-1 peer certificate
6685 * n . n+2 length of cert. 2
6686 * n+3 . ... upper level cert, etc.
6687 */
6688 i = 7;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006689 crt = mbedtls_ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00006690
Paul Bakker29087132010-03-21 21:03:34 +00006691 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006692 {
6693 n = crt->raw.len;
Angus Grattond8213d02016-05-25 20:56:48 +10006694 if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00006695 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006696 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
Angus Grattond8213d02016-05-25 20:56:48 +10006697 i + 3 + n, MBEDTLS_SSL_OUT_CONTENT_LEN ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006698 return( MBEDTLS_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006699 }
6700
6701 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
6702 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
6703 ssl->out_msg[i + 2] = (unsigned char)( n );
6704
6705 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
6706 i += n; crt = crt->next;
6707 }
6708
6709 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
6710 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
6711 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
6712
6713 ssl->out_msglen = i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006714 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
6715 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
Paul Bakker5121ce52009-01-03 21:22:43 +00006716
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02006717#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00006718write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006719#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006720
6721 ssl->state++;
6722
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006723 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006724 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006725 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006726 return( ret );
6727 }
6728
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006729 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006730
Paul Bakkered27a042013-04-18 22:46:23 +02006731 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006732}
6733
Hanno Becker84879e32019-01-31 07:44:03 +00006734#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Hanno Becker177475a2019-02-05 17:02:46 +00006735
6736#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006737static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6738 unsigned char *crt_buf,
6739 size_t crt_buf_len )
6740{
6741 mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert;
6742
6743 if( peer_crt == NULL )
6744 return( -1 );
6745
6746 if( peer_crt->raw.len != crt_buf_len )
6747 return( -1 );
6748
Hanno Becker46f34d02019-02-08 14:00:04 +00006749 return( memcmp( peer_crt->raw.p, crt_buf, crt_buf_len ) );
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006750}
Hanno Becker177475a2019-02-05 17:02:46 +00006751#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6752static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6753 unsigned char *crt_buf,
6754 size_t crt_buf_len )
6755{
6756 int ret;
6757 unsigned char const * const peer_cert_digest =
6758 ssl->session->peer_cert_digest;
6759 mbedtls_md_type_t const peer_cert_digest_type =
6760 ssl->session->peer_cert_digest_type;
6761 mbedtls_md_info_t const * const digest_info =
6762 mbedtls_md_info_from_type( peer_cert_digest_type );
6763 unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN];
6764 size_t digest_len;
6765
6766 if( peer_cert_digest == NULL || digest_info == NULL )
6767 return( -1 );
6768
6769 digest_len = mbedtls_md_get_size( digest_info );
6770 if( digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN )
6771 return( -1 );
6772
6773 ret = mbedtls_md( digest_info, crt_buf, crt_buf_len, tmp_digest );
6774 if( ret != 0 )
6775 return( -1 );
6776
6777 return( memcmp( tmp_digest, peer_cert_digest, digest_len ) );
6778}
6779#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker84879e32019-01-31 07:44:03 +00006780#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006781
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006782/*
6783 * Once the certificate message is read, parse it into a cert chain and
6784 * perform basic checks, but leave actual verification to the caller
6785 */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006786static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl,
6787 mbedtls_x509_crt *chain )
Paul Bakker5121ce52009-01-03 21:22:43 +00006788{
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006789 int ret;
6790#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6791 int crt_cnt=0;
6792#endif
Paul Bakker23986e52011-04-24 08:57:21 +00006793 size_t i, n;
Gilles Peskine064a85c2017-05-10 10:46:40 +02006794 uint8_t alert;
Paul Bakker5121ce52009-01-03 21:22:43 +00006795
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006796 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00006797 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006798 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006799 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6800 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006801 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006802 }
6803
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006804 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE ||
6805 ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006806 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006807 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006808 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6809 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006810 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006811 }
6812
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006813 i = mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006814
Paul Bakker5121ce52009-01-03 21:22:43 +00006815 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006816 * Same message structure as in mbedtls_ssl_write_certificate()
Paul Bakker5121ce52009-01-03 21:22:43 +00006817 */
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006818 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
Paul Bakker5121ce52009-01-03 21:22:43 +00006819
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006820 if( ssl->in_msg[i] != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006821 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00006822 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006823 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006824 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6825 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006826 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006827 }
6828
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006829 /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */
6830 i += 3;
6831
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006832 /* Iterate through and parse the CRTs in the provided chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006833 while( i < ssl->in_hslen )
6834 {
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006835 /* Check that there's room for the next CRT's length fields. */
Philippe Antoine747fd532018-05-30 09:13:21 +02006836 if ( i + 3 > ssl->in_hslen ) {
6837 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckere2734e22019-01-31 07:44:17 +00006838 mbedtls_ssl_send_alert_message( ssl,
6839 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6840 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Philippe Antoine747fd532018-05-30 09:13:21 +02006841 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
6842 }
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006843 /* In theory, the CRT can be up to 2**24 Bytes, but we don't support
6844 * anything beyond 2**16 ~ 64K. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006845 if( ssl->in_msg[i] != 0 )
6846 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006847 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckere2734e22019-01-31 07:44:17 +00006848 mbedtls_ssl_send_alert_message( ssl,
6849 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6850 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006851 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006852 }
6853
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006854 /* Read length of the next CRT in the chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006855 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
6856 | (unsigned int) ssl->in_msg[i + 2];
6857 i += 3;
6858
6859 if( n < 128 || i + n > ssl->in_hslen )
6860 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006861 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckere2734e22019-01-31 07:44:17 +00006862 mbedtls_ssl_send_alert_message( ssl,
6863 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6864 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006865 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006866 }
6867
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006868 /* Check if we're handling the first CRT in the chain. */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006869#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6870 if( crt_cnt++ == 0 &&
6871 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
6872 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006873 {
Hanno Becker46f34d02019-02-08 14:00:04 +00006874 /* During client-side renegotiation, check that the server's
6875 * end-CRTs hasn't changed compared to the initial handshake,
6876 * mitigating the triple handshake attack. On success, reuse
6877 * the original end-CRT instead of parsing it again. */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006878 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Check that peer CRT hasn't changed during renegotiation" ) );
6879 if( ssl_check_peer_crt_unchanged( ssl,
6880 &ssl->in_msg[i],
6881 n ) != 0 )
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006882 {
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006883 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
6884 mbedtls_ssl_send_alert_message( ssl,
6885 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6886 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
6887 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006888 }
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006889
6890 /* Now we can safely free the original chain. */
6891 ssl_clear_peer_cert( ssl->session );
6892 }
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006893#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
6894
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006895 /* Parse the next certificate in the chain. */
Hanno Becker0056eab2019-02-08 14:39:16 +00006896#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006897 ret = mbedtls_x509_crt_parse_der( chain, ssl->in_msg + i, n );
Hanno Becker0056eab2019-02-08 14:39:16 +00006898#else
Hanno Becker353a6f02019-02-26 11:51:34 +00006899 /* If we don't need to store the CRT chain permanently, parse
Hanno Becker0056eab2019-02-08 14:39:16 +00006900 * it in-place from the input buffer instead of making a copy. */
6901 ret = mbedtls_x509_crt_parse_der_nocopy( chain, ssl->in_msg + i, n );
6902#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006903 switch( ret )
Paul Bakker5121ce52009-01-03 21:22:43 +00006904 {
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006905 case 0: /*ok*/
6906 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
6907 /* Ignore certificate with an unknown algorithm: maybe a
6908 prior certificate was already trusted. */
6909 break;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006910
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006911 case MBEDTLS_ERR_X509_ALLOC_FAILED:
6912 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
6913 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006914
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006915 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
6916 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6917 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006918
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006919 default:
6920 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6921 crt_parse_der_failed:
6922 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert );
6923 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
6924 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006925 }
6926
6927 i += n;
6928 }
6929
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006930 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", chain );
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006931 return( 0 );
6932}
6933
Hanno Becker4a55f632019-02-05 12:49:06 +00006934#if defined(MBEDTLS_SSL_SRV_C)
6935static int ssl_srv_check_client_no_crt_notification( mbedtls_ssl_context *ssl )
6936{
6937 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
6938 return( -1 );
6939
6940#if defined(MBEDTLS_SSL_PROTO_SSL3)
6941 /*
6942 * Check if the client sent an empty certificate
6943 */
6944 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
6945 {
6946 if( ssl->in_msglen == 2 &&
6947 ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT &&
6948 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6949 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
6950 {
6951 MBEDTLS_SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
6952 return( 0 );
6953 }
6954
6955 return( -1 );
6956 }
6957#endif /* MBEDTLS_SSL_PROTO_SSL3 */
6958
6959#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
6960 defined(MBEDTLS_SSL_PROTO_TLS1_2)
6961 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
6962 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
6963 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
6964 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
6965 {
6966 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
6967 return( 0 );
6968 }
6969
6970 return( -1 );
6971#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
6972 MBEDTLS_SSL_PROTO_TLS1_2 */
6973}
6974#endif /* MBEDTLS_SSL_SRV_C */
6975
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006976/* Check if a certificate message is expected.
6977 * Return either
6978 * - SSL_CERTIFICATE_EXPECTED, or
6979 * - SSL_CERTIFICATE_SKIP
6980 * indicating whether a Certificate message is expected or not.
6981 */
6982#define SSL_CERTIFICATE_EXPECTED 0
6983#define SSL_CERTIFICATE_SKIP 1
6984static int ssl_parse_certificate_coordinate( mbedtls_ssl_context *ssl,
6985 int authmode )
6986{
6987 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00006988 ssl->handshake->ciphersuite_info;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006989
6990 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6991 return( SSL_CERTIFICATE_SKIP );
6992
6993#if defined(MBEDTLS_SSL_SRV_C)
6994 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
6995 {
6996 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
6997 return( SSL_CERTIFICATE_SKIP );
6998
6999 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
7000 {
Hanno Becker28f2fcd2019-02-07 10:11:07 +00007001 ssl->session_negotiate->verify_result =
7002 MBEDTLS_X509_BADCERT_SKIP_VERIFY;
7003 return( SSL_CERTIFICATE_SKIP );
7004 }
7005 }
Hanno Becker84d9d272019-03-01 08:10:46 +00007006#else
7007 ((void) authmode);
Hanno Becker28f2fcd2019-02-07 10:11:07 +00007008#endif /* MBEDTLS_SSL_SRV_C */
7009
7010 return( SSL_CERTIFICATE_EXPECTED );
7011}
7012
Hanno Becker68636192019-02-05 14:36:34 +00007013static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl,
7014 int authmode,
7015 mbedtls_x509_crt *chain,
7016 void *rs_ctx )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007017{
Hanno Becker6bdfab22019-02-05 13:11:17 +00007018 int ret = 0;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00007019 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00007020 ssl->handshake->ciphersuite_info;
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00007021 int have_ca_chain = 0;
Hanno Becker68636192019-02-05 14:36:34 +00007022
Hanno Becker8927c832019-04-03 12:52:50 +01007023 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *);
7024 void *p_vrfy;
7025
Hanno Becker68636192019-02-05 14:36:34 +00007026 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
7027 return( 0 );
7028
Hanno Becker8927c832019-04-03 12:52:50 +01007029 if( ssl->f_vrfy != NULL )
7030 {
Hanno Beckerefb440a2019-04-03 13:04:33 +01007031 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use context-specific verification callback" ) );
Hanno Becker8927c832019-04-03 12:52:50 +01007032 f_vrfy = ssl->f_vrfy;
7033 p_vrfy = ssl->p_vrfy;
7034 }
7035 else
7036 {
Hanno Beckerefb440a2019-04-03 13:04:33 +01007037 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use configuration-specific verification callback" ) );
Hanno Becker8927c832019-04-03 12:52:50 +01007038 f_vrfy = ssl->conf->f_vrfy;
7039 p_vrfy = ssl->conf->p_vrfy;
7040 }
7041
Hanno Becker68636192019-02-05 14:36:34 +00007042 /*
7043 * Main check: verify certificate
7044 */
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00007045#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
7046 if( ssl->conf->f_ca_cb != NULL )
7047 {
7048 ((void) rs_ctx);
7049 have_ca_chain = 1;
7050
7051 MBEDTLS_SSL_DEBUG_MSG( 3, ( "use CA callback for X.509 CRT verification" ) );
Jarno Lamsa9822c0d2019-04-01 16:59:48 +03007052 ret = mbedtls_x509_crt_verify_with_ca_cb(
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00007053 chain,
7054 ssl->conf->f_ca_cb,
7055 ssl->conf->p_ca_cb,
7056 ssl->conf->cert_profile,
7057 ssl->hostname,
7058 &ssl->session_negotiate->verify_result,
Jaeden Amerofe710672019-04-16 15:03:12 +01007059 f_vrfy, p_vrfy );
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00007060 }
7061 else
7062#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
7063 {
7064 mbedtls_x509_crt *ca_chain;
7065 mbedtls_x509_crl *ca_crl;
7066
7067#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7068 if( ssl->handshake->sni_ca_chain != NULL )
7069 {
7070 ca_chain = ssl->handshake->sni_ca_chain;
7071 ca_crl = ssl->handshake->sni_ca_crl;
7072 }
7073 else
7074#endif
7075 {
7076 ca_chain = ssl->conf->ca_chain;
7077 ca_crl = ssl->conf->ca_crl;
7078 }
7079
7080 if( ca_chain != NULL )
7081 have_ca_chain = 1;
7082
7083 ret = mbedtls_x509_crt_verify_restartable(
7084 chain,
7085 ca_chain, ca_crl,
7086 ssl->conf->cert_profile,
7087 ssl->hostname,
7088 &ssl->session_negotiate->verify_result,
Jaeden Amerofe710672019-04-16 15:03:12 +01007089 f_vrfy, p_vrfy, rs_ctx );
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00007090 }
Hanno Becker68636192019-02-05 14:36:34 +00007091
7092 if( ret != 0 )
7093 {
7094 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
7095 }
7096
7097#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
7098 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
7099 return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS );
7100#endif
7101
7102 /*
7103 * Secondary checks: always done, but change 'ret' only if it was 0
7104 */
7105
7106#if defined(MBEDTLS_ECP_C)
7107 {
7108 const mbedtls_pk_context *pk = &chain->pk;
7109
7110 /* If certificate uses an EC key, make sure the curve is OK */
7111 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) &&
7112 mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id ) != 0 )
7113 {
7114 ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
7115
7116 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
7117 if( ret == 0 )
7118 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
7119 }
7120 }
7121#endif /* MBEDTLS_ECP_C */
7122
7123 if( mbedtls_ssl_check_cert_usage( chain,
7124 ciphersuite_info,
7125 ! ssl->conf->endpoint,
7126 &ssl->session_negotiate->verify_result ) != 0 )
7127 {
7128 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
7129 if( ret == 0 )
7130 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
7131 }
7132
7133 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
7134 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
7135 * with details encoded in the verification flags. All other kinds
7136 * of error codes, including those from the user provided f_vrfy
7137 * functions, are treated as fatal and lead to a failure of
7138 * ssl_parse_certificate even if verification was optional. */
7139 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
7140 ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
7141 ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ) )
7142 {
7143 ret = 0;
7144 }
7145
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00007146 if( have_ca_chain == 0 && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
Hanno Becker68636192019-02-05 14:36:34 +00007147 {
7148 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
7149 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
7150 }
7151
7152 if( ret != 0 )
7153 {
7154 uint8_t alert;
7155
7156 /* The certificate may have been rejected for several reasons.
7157 Pick one and send the corresponding alert. Which alert to send
7158 may be a subject of debate in some cases. */
7159 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
7160 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
7161 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
7162 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
7163 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
7164 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7165 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
7166 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7167 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
7168 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7169 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
7170 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7171 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
7172 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7173 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
7174 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
7175 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
7176 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
7177 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
7178 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
7179 else
7180 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
7181 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7182 alert );
7183 }
7184
7185#if defined(MBEDTLS_DEBUG_C)
7186 if( ssl->session_negotiate->verify_result != 0 )
7187 {
7188 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %x",
7189 ssl->session_negotiate->verify_result ) );
7190 }
7191 else
7192 {
7193 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
7194 }
7195#endif /* MBEDTLS_DEBUG_C */
7196
7197 return( ret );
7198}
7199
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007200#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
7201static int ssl_remember_peer_crt_digest( mbedtls_ssl_context *ssl,
7202 unsigned char *start, size_t len )
7203{
7204 int ret;
7205 /* Remember digest of the peer's end-CRT. */
7206 ssl->session_negotiate->peer_cert_digest =
7207 mbedtls_calloc( 1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN );
7208 if( ssl->session_negotiate->peer_cert_digest == NULL )
7209 {
7210 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
7211 sizeof( MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ) ) );
7212 mbedtls_ssl_send_alert_message( ssl,
7213 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7214 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
7215
7216 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
7217 }
7218
7219 ret = mbedtls_md( mbedtls_md_info_from_type(
7220 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE ),
7221 start, len,
7222 ssl->session_negotiate->peer_cert_digest );
7223
7224 ssl->session_negotiate->peer_cert_digest_type =
7225 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
7226 ssl->session_negotiate->peer_cert_digest_len =
7227 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
7228
7229 return( ret );
7230}
7231
7232static int ssl_remember_peer_pubkey( mbedtls_ssl_context *ssl,
7233 unsigned char *start, size_t len )
7234{
7235 unsigned char *end = start + len;
7236 int ret;
7237
7238 /* Make a copy of the peer's raw public key. */
7239 mbedtls_pk_init( &ssl->handshake->peer_pubkey );
7240 ret = mbedtls_pk_parse_subpubkey( &start, end,
7241 &ssl->handshake->peer_pubkey );
7242 if( ret != 0 )
7243 {
7244 /* We should have parsed the public key before. */
7245 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
7246 }
7247
7248 return( 0 );
7249}
7250#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7251
Hanno Becker68636192019-02-05 14:36:34 +00007252int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
7253{
7254 int ret = 0;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00007255 int crt_expected;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007256#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7257 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
7258 ? ssl->handshake->sni_authmode
7259 : ssl->conf->authmode;
7260#else
7261 const int authmode = ssl->conf->authmode;
7262#endif
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007263 void *rs_ctx = NULL;
Hanno Becker3dad3112019-02-05 17:19:52 +00007264 mbedtls_x509_crt *chain = NULL;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007265
7266 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
7267
Hanno Becker28f2fcd2019-02-07 10:11:07 +00007268 crt_expected = ssl_parse_certificate_coordinate( ssl, authmode );
7269 if( crt_expected == SSL_CERTIFICATE_SKIP )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007270 {
7271 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Hanno Becker6bdfab22019-02-05 13:11:17 +00007272 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007273 }
7274
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007275#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
7276 if( ssl->handshake->ecrs_enabled &&
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02007277 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify )
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007278 {
Hanno Becker3dad3112019-02-05 17:19:52 +00007279 chain = ssl->handshake->ecrs_peer_cert;
7280 ssl->handshake->ecrs_peer_cert = NULL;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007281 goto crt_verify;
7282 }
7283#endif
7284
Manuel Pégourié-Gonnard125af942018-09-11 11:08:12 +02007285 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007286 {
7287 /* mbedtls_ssl_read_record may have sent an alert already. We
7288 let it decide whether to alert. */
7289 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Hanno Becker3dad3112019-02-05 17:19:52 +00007290 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007291 }
7292
Hanno Becker4a55f632019-02-05 12:49:06 +00007293#if defined(MBEDTLS_SSL_SRV_C)
7294 if( ssl_srv_check_client_no_crt_notification( ssl ) == 0 )
7295 {
7296 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Hanno Becker4a55f632019-02-05 12:49:06 +00007297
7298 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
Hanno Becker6bdfab22019-02-05 13:11:17 +00007299 ret = 0;
7300 else
7301 ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
Hanno Becker4a55f632019-02-05 12:49:06 +00007302
Hanno Becker6bdfab22019-02-05 13:11:17 +00007303 goto exit;
Hanno Becker4a55f632019-02-05 12:49:06 +00007304 }
7305#endif /* MBEDTLS_SSL_SRV_C */
7306
Hanno Beckerc7bd7802019-02-05 15:37:23 +00007307 /* Clear existing peer CRT structure in case we tried to
7308 * reuse a session but it failed, and allocate a new one. */
Hanno Becker7a955a02019-02-05 13:08:01 +00007309 ssl_clear_peer_cert( ssl->session_negotiate );
Hanno Becker3dad3112019-02-05 17:19:52 +00007310
7311 chain = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
7312 if( chain == NULL )
Hanno Beckerc7bd7802019-02-05 15:37:23 +00007313 {
7314 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
7315 sizeof( mbedtls_x509_crt ) ) );
7316 mbedtls_ssl_send_alert_message( ssl,
7317 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7318 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Hanno Becker7a955a02019-02-05 13:08:01 +00007319
Hanno Becker3dad3112019-02-05 17:19:52 +00007320 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
7321 goto exit;
7322 }
7323 mbedtls_x509_crt_init( chain );
7324
7325 ret = ssl_parse_certificate_chain( ssl, chain );
Hanno Beckerc7bd7802019-02-05 15:37:23 +00007326 if( ret != 0 )
Hanno Becker3dad3112019-02-05 17:19:52 +00007327 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007328
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007329#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
7330 if( ssl->handshake->ecrs_enabled)
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02007331 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007332
7333crt_verify:
7334 if( ssl->handshake->ecrs_enabled)
7335 rs_ctx = &ssl->handshake->ecrs_ctx;
7336#endif
7337
Hanno Becker68636192019-02-05 14:36:34 +00007338 ret = ssl_parse_certificate_verify( ssl, authmode,
Hanno Becker3dad3112019-02-05 17:19:52 +00007339 chain, rs_ctx );
Hanno Becker68636192019-02-05 14:36:34 +00007340 if( ret != 0 )
Hanno Becker3dad3112019-02-05 17:19:52 +00007341 goto exit;
Paul Bakker5121ce52009-01-03 21:22:43 +00007342
Hanno Becker6bbd94c2019-02-05 17:02:28 +00007343#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Becker6bbd94c2019-02-05 17:02:28 +00007344 {
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007345 unsigned char *crt_start, *pk_start;
7346 size_t crt_len, pk_len;
Hanno Becker3dad3112019-02-05 17:19:52 +00007347
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007348 /* We parse the CRT chain without copying, so
7349 * these pointers point into the input buffer,
7350 * and are hence still valid after freeing the
7351 * CRT chain. */
Hanno Becker6bbd94c2019-02-05 17:02:28 +00007352
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007353 crt_start = chain->raw.p;
7354 crt_len = chain->raw.len;
Hanno Becker6bbd94c2019-02-05 17:02:28 +00007355
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007356 pk_start = chain->pk_raw.p;
7357 pk_len = chain->pk_raw.len;
7358
7359 /* Free the CRT structures before computing
7360 * digest and copying the peer's public key. */
7361 mbedtls_x509_crt_free( chain );
7362 mbedtls_free( chain );
7363 chain = NULL;
7364
7365 ret = ssl_remember_peer_crt_digest( ssl, crt_start, crt_len );
Hanno Beckera2747532019-02-06 16:19:04 +00007366 if( ret != 0 )
Hanno Beckera2747532019-02-06 16:19:04 +00007367 goto exit;
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007368
7369 ret = ssl_remember_peer_pubkey( ssl, pk_start, pk_len );
7370 if( ret != 0 )
7371 goto exit;
Hanno Beckera2747532019-02-06 16:19:04 +00007372 }
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007373#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7374 /* Pass ownership to session structure. */
Hanno Becker3dad3112019-02-05 17:19:52 +00007375 ssl->session_negotiate->peer_cert = chain;
7376 chain = NULL;
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007377#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker3dad3112019-02-05 17:19:52 +00007378
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007379 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007380
Hanno Becker6bdfab22019-02-05 13:11:17 +00007381exit:
7382
Hanno Becker3dad3112019-02-05 17:19:52 +00007383 if( ret == 0 )
7384 ssl->state++;
7385
7386#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
7387 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
7388 {
7389 ssl->handshake->ecrs_peer_cert = chain;
7390 chain = NULL;
7391 }
7392#endif
7393
7394 if( chain != NULL )
7395 {
7396 mbedtls_x509_crt_free( chain );
7397 mbedtls_free( chain );
7398 }
7399
Paul Bakker5121ce52009-01-03 21:22:43 +00007400 return( ret );
7401}
Hanno Becker21489932019-02-05 13:20:55 +00007402#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00007403
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007404int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007405{
7406 int ret;
7407
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007408 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007409
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007410 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00007411 ssl->out_msglen = 1;
7412 ssl->out_msg[0] = 1;
7413
Paul Bakker5121ce52009-01-03 21:22:43 +00007414 ssl->state++;
7415
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007416 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007417 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007418 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007419 return( ret );
7420 }
7421
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007422 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007423
7424 return( 0 );
7425}
7426
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007427int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007428{
7429 int ret;
7430
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007431 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007432
Hanno Becker327c93b2018-08-15 13:56:18 +01007433 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007434 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007435 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007436 return( ret );
7437 }
7438
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007439 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Paul Bakker5121ce52009-01-03 21:22:43 +00007440 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007441 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007442 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7443 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007444 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007445 }
7446
Hanno Beckere678eaa2018-08-21 14:57:46 +01007447 /* CCS records are only accepted if they have length 1 and content '1',
7448 * so we don't need to check this here. */
Paul Bakker5121ce52009-01-03 21:22:43 +00007449
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007450 /*
7451 * Switch to our negotiated transform and session parameters for inbound
7452 * data.
7453 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007454 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007455 ssl->transform_in = ssl->transform_negotiate;
7456 ssl->session_in = ssl->session_negotiate;
7457
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007458#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007459 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007460 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007461#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007462 ssl_dtls_replay_reset( ssl );
7463#endif
7464
7465 /* Increment epoch */
7466 if( ++ssl->in_epoch == 0 )
7467 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007468 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007469 /* This is highly unlikely to happen for legitimate reasons, so
7470 treat it as an attack and don't send an alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007471 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007472 }
7473 }
7474 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007475#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007476 memset( ssl->in_ctr, 0, 8 );
7477
Hanno Becker79594fd2019-05-08 09:38:41 +01007478 ssl_update_in_pointers( ssl );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007479
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007480#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7481 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007482 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007483 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_INBOUND ) ) != 0 )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007484 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007485 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007486 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7487 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007488 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007489 }
7490 }
7491#endif
7492
Paul Bakker5121ce52009-01-03 21:22:43 +00007493 ssl->state++;
7494
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007495 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007496
7497 return( 0 );
7498}
7499
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007500void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl,
7501 const mbedtls_ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00007502{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02007503 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01007504
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007505#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7506 defined(MBEDTLS_SSL_PROTO_TLS1_1)
7507 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00007508 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00007509 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007510#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007511#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7512#if defined(MBEDTLS_SHA512_C)
7513 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007514 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
7515 else
7516#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007517#if defined(MBEDTLS_SHA256_C)
7518 if( ciphersuite_info->mac != MBEDTLS_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00007519 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007520 else
7521#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007522#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02007523 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007524 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007525 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02007526 }
Paul Bakker380da532012-04-18 16:10:25 +00007527}
Paul Bakkerf7abd422013-04-16 13:15:56 +02007528
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007529void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007530{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007531#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7532 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007533 mbedtls_md5_starts_ret( &ssl->handshake->fin_md5 );
7534 mbedtls_sha1_starts_ret( &ssl->handshake->fin_sha1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007535#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007536#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7537#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007538#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek2ad22972019-01-30 03:32:12 -05007539 psa_hash_abort( &ssl->handshake->fin_sha256_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007540 psa_hash_setup( &ssl->handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
7541#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007542 mbedtls_sha256_starts_ret( &ssl->handshake->fin_sha256, 0 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007543#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007544#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007545#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007546#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek2ad22972019-01-30 03:32:12 -05007547 psa_hash_abort( &ssl->handshake->fin_sha384_psa );
Andrzej Kurek972fba52019-01-30 03:29:12 -05007548 psa_hash_setup( &ssl->handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007549#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007550 mbedtls_sha512_starts_ret( &ssl->handshake->fin_sha512, 1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007551#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007552#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007553#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007554}
7555
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007556static void ssl_update_checksum_start( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007557 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00007558{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007559#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7560 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007561 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
7562 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007563#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007564#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7565#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007566#if defined(MBEDTLS_USE_PSA_CRYPTO)
7567 psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len );
7568#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007569 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007570#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007571#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007572#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007573#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05007574 psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007575#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007576 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01007577#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007578#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007579#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00007580}
7581
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007582#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7583 defined(MBEDTLS_SSL_PROTO_TLS1_1)
7584static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007585 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00007586{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007587 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
7588 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00007589}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007590#endif
Paul Bakker380da532012-04-18 16:10:25 +00007591
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007592#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7593#if defined(MBEDTLS_SHA256_C)
7594static void ssl_update_checksum_sha256( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007595 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00007596{
Andrzej Kurekeb342242019-01-29 09:14:33 -05007597#if defined(MBEDTLS_USE_PSA_CRYPTO)
7598 psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len );
7599#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007600 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007601#endif
Paul Bakker380da532012-04-18 16:10:25 +00007602}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007603#endif
Paul Bakker380da532012-04-18 16:10:25 +00007604
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007605#if defined(MBEDTLS_SHA512_C)
7606static void ssl_update_checksum_sha384( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007607 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00007608{
Andrzej Kurekeb342242019-01-29 09:14:33 -05007609#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05007610 psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007611#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007612 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007613#endif
Paul Bakker380da532012-04-18 16:10:25 +00007614}
Paul Bakker769075d2012-11-24 11:26:46 +01007615#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007616#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00007617
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007618#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00007619static void ssl_calc_finished_ssl(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007620 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00007621{
Paul Bakker3c2122f2013-06-24 19:03:14 +02007622 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007623 mbedtls_md5_context md5;
7624 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007625
Paul Bakker5121ce52009-01-03 21:22:43 +00007626 unsigned char padbuf[48];
7627 unsigned char md5sum[16];
7628 unsigned char sha1sum[20];
7629
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007630 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007631 if( !session )
7632 session = ssl->session;
7633
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007634 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007635
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007636 mbedtls_md5_init( &md5 );
7637 mbedtls_sha1_init( &sha1 );
7638
7639 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
7640 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007641
7642 /*
7643 * SSLv3:
7644 * hash =
7645 * MD5( master + pad2 +
7646 * MD5( handshake + sender + master + pad1 ) )
7647 * + SHA1( master + pad2 +
7648 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00007649 */
7650
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007651#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007652 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
7653 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007654#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007655
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007656#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007657 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
7658 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007659#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007660
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007661 sender = ( from == MBEDTLS_SSL_IS_CLIENT ) ? "CLNT"
Paul Bakker3c2122f2013-06-24 19:03:14 +02007662 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00007663
Paul Bakker1ef83d62012-04-11 12:09:53 +00007664 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007665
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007666 mbedtls_md5_update_ret( &md5, (const unsigned char *) sender, 4 );
7667 mbedtls_md5_update_ret( &md5, session->master, 48 );
7668 mbedtls_md5_update_ret( &md5, padbuf, 48 );
7669 mbedtls_md5_finish_ret( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00007670
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007671 mbedtls_sha1_update_ret( &sha1, (const unsigned char *) sender, 4 );
7672 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
7673 mbedtls_sha1_update_ret( &sha1, padbuf, 40 );
7674 mbedtls_sha1_finish_ret( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00007675
Paul Bakker1ef83d62012-04-11 12:09:53 +00007676 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007677
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007678 mbedtls_md5_starts_ret( &md5 );
7679 mbedtls_md5_update_ret( &md5, session->master, 48 );
7680 mbedtls_md5_update_ret( &md5, padbuf, 48 );
7681 mbedtls_md5_update_ret( &md5, md5sum, 16 );
7682 mbedtls_md5_finish_ret( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00007683
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007684 mbedtls_sha1_starts_ret( &sha1 );
7685 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
7686 mbedtls_sha1_update_ret( &sha1, padbuf , 40 );
7687 mbedtls_sha1_update_ret( &sha1, sha1sum, 20 );
7688 mbedtls_sha1_finish_ret( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007689
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007690 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007691
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007692 mbedtls_md5_free( &md5 );
7693 mbedtls_sha1_free( &sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007694
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007695 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
7696 mbedtls_platform_zeroize( md5sum, sizeof( md5sum ) );
7697 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007698
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007699 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007700}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007701#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00007702
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007703#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00007704static void ssl_calc_finished_tls(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007705 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00007706{
Paul Bakker1ef83d62012-04-11 12:09:53 +00007707 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02007708 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007709 mbedtls_md5_context md5;
7710 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007711 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00007712
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007713 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007714 if( !session )
7715 session = ssl->session;
7716
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007717 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007718
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007719 mbedtls_md5_init( &md5 );
7720 mbedtls_sha1_init( &sha1 );
7721
7722 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
7723 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007724
Paul Bakker1ef83d62012-04-11 12:09:53 +00007725 /*
7726 * TLSv1:
7727 * hash = PRF( master, finished_label,
7728 * MD5( handshake ) + SHA1( handshake ) )[0..11]
7729 */
Paul Bakker5121ce52009-01-03 21:22:43 +00007730
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007731#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007732 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
7733 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007734#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007735
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007736#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007737 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
7738 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007739#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007740
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007741 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02007742 ? "client finished"
7743 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00007744
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007745 mbedtls_md5_finish_ret( &md5, padbuf );
7746 mbedtls_sha1_finish_ret( &sha1, padbuf + 16 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007747
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007748 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00007749 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007750
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007751 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007752
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007753 mbedtls_md5_free( &md5 );
7754 mbedtls_sha1_free( &sha1 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007755
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007756 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007757
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007758 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007759}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007760#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00007761
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007762#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7763#if defined(MBEDTLS_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00007764static void ssl_calc_finished_tls_sha256(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007765 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker1ef83d62012-04-11 12:09:53 +00007766{
7767 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02007768 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007769 unsigned char padbuf[32];
Andrzej Kurekeb342242019-01-29 09:14:33 -05007770#if defined(MBEDTLS_USE_PSA_CRYPTO)
7771 size_t hash_size;
Jaeden Amero34973232019-02-20 10:32:28 +00007772 psa_hash_operation_t sha256_psa = PSA_HASH_OPERATION_INIT;
Andrzej Kurekeb342242019-01-29 09:14:33 -05007773 psa_status_t status;
7774#else
7775 mbedtls_sha256_context sha256;
7776#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007777
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007778 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007779 if( !session )
7780 session = ssl->session;
7781
Andrzej Kurekeb342242019-01-29 09:14:33 -05007782 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
7783 ? "client finished"
7784 : "server finished";
7785
7786#if defined(MBEDTLS_USE_PSA_CRYPTO)
7787 sha256_psa = psa_hash_operation_init();
7788
7789 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha256" ) );
7790
7791 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
7792 if( status != PSA_SUCCESS )
7793 {
7794 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
7795 return;
7796 }
7797
7798 status = psa_hash_finish( &sha256_psa, padbuf, sizeof( padbuf ), &hash_size );
7799 if( status != PSA_SUCCESS )
7800 {
7801 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
7802 return;
7803 }
7804 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 32 );
7805#else
7806
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007807 mbedtls_sha256_init( &sha256 );
7808
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007809 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007810
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007811 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007812
7813 /*
7814 * TLSv1.2:
7815 * hash = PRF( master, finished_label,
7816 * Hash( handshake ) )[0.11]
7817 */
7818
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007819#if !defined(MBEDTLS_SHA256_ALT)
7820 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007821 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007822#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007823
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007824 mbedtls_sha256_finish_ret( &sha256, padbuf );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007825 mbedtls_sha256_free( &sha256 );
7826#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker1ef83d62012-04-11 12:09:53 +00007827
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007828 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00007829 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007830
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007831 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007832
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007833 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007834
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007835 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007836}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007837#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00007838
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007839#if defined(MBEDTLS_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00007840static void ssl_calc_finished_tls_sha384(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007841 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakkerca4ab492012-04-18 14:23:57 +00007842{
7843 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02007844 const char *sender;
Paul Bakkerca4ab492012-04-18 14:23:57 +00007845 unsigned char padbuf[48];
Andrzej Kurekeb342242019-01-29 09:14:33 -05007846#if defined(MBEDTLS_USE_PSA_CRYPTO)
7847 size_t hash_size;
Jaeden Amero34973232019-02-20 10:32:28 +00007848 psa_hash_operation_t sha384_psa = PSA_HASH_OPERATION_INIT;
Andrzej Kurekeb342242019-01-29 09:14:33 -05007849 psa_status_t status;
7850#else
7851 mbedtls_sha512_context sha512;
7852#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00007853
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007854 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007855 if( !session )
7856 session = ssl->session;
7857
Andrzej Kurekeb342242019-01-29 09:14:33 -05007858 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
7859 ? "client finished"
7860 : "server finished";
7861
7862#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05007863 sha384_psa = psa_hash_operation_init();
Andrzej Kurekeb342242019-01-29 09:14:33 -05007864
7865 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha384" ) );
7866
Andrzej Kurek972fba52019-01-30 03:29:12 -05007867 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007868 if( status != PSA_SUCCESS )
7869 {
7870 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
7871 return;
7872 }
7873
Andrzej Kurek972fba52019-01-30 03:29:12 -05007874 status = psa_hash_finish( &sha384_psa, padbuf, sizeof( padbuf ), &hash_size );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007875 if( status != PSA_SUCCESS )
7876 {
7877 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
7878 return;
7879 }
7880 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 48 );
7881#else
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007882 mbedtls_sha512_init( &sha512 );
7883
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007884 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007885
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007886 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007887
7888 /*
7889 * TLSv1.2:
7890 * hash = PRF( master, finished_label,
7891 * Hash( handshake ) )[0.11]
7892 */
7893
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007894#if !defined(MBEDTLS_SHA512_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007895 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
7896 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007897#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00007898
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007899 mbedtls_sha512_finish_ret( &sha512, padbuf );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007900 mbedtls_sha512_free( &sha512 );
7901#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00007902
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007903 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00007904 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007905
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007906 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007907
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007908 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007909
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007910 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007911}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007912#endif /* MBEDTLS_SHA512_C */
7913#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00007914
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007915static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00007916{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007917 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007918
7919 /*
7920 * Free our handshake params
7921 */
Gilles Peskine9b562d52018-04-25 20:32:43 +02007922 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007923 mbedtls_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00007924 ssl->handshake = NULL;
7925
7926 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007927 * Free the previous transform and swith in the current one
Paul Bakker48916f92012-09-16 19:57:18 +00007928 */
7929 if( ssl->transform )
7930 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007931 mbedtls_ssl_transform_free( ssl->transform );
7932 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00007933 }
7934 ssl->transform = ssl->transform_negotiate;
7935 ssl->transform_negotiate = NULL;
7936
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007937 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007938}
7939
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007940void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007941{
7942 int resume = ssl->handshake->resume;
7943
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007944 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007945
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007946#if defined(MBEDTLS_SSL_RENEGOTIATION)
7947 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007948 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007949 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007950 ssl->renego_records_seen = 0;
7951 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007952#endif
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007953
7954 /*
7955 * Free the previous session and switch in the current one
7956 */
Paul Bakker0a597072012-09-25 21:55:46 +00007957 if( ssl->session )
7958 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007959#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01007960 /* RFC 7366 3.1: keep the EtM state */
7961 ssl->session_negotiate->encrypt_then_mac =
7962 ssl->session->encrypt_then_mac;
7963#endif
7964
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007965 mbedtls_ssl_session_free( ssl->session );
7966 mbedtls_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00007967 }
7968 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007969 ssl->session_negotiate = NULL;
7970
Paul Bakker0a597072012-09-25 21:55:46 +00007971 /*
7972 * Add cache entry
7973 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007974 if( ssl->conf->f_set_cache != NULL &&
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02007975 ssl->session->id_len != 0 &&
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02007976 resume == 0 )
7977 {
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007978 if( ssl->conf->f_set_cache( ssl->conf->p_cache, ssl->session ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007979 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02007980 }
Paul Bakker0a597072012-09-25 21:55:46 +00007981
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007982#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007983 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007984 ssl->handshake->flight != NULL )
7985 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02007986 /* Cancel handshake timer */
7987 ssl_set_timer( ssl, 0 );
7988
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007989 /* Keep last flight around in case we need to resend it:
7990 * we need the handshake and transform structures for that */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007991 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007992 }
7993 else
7994#endif
7995 ssl_handshake_wrapup_free_hs_transform( ssl );
7996
Paul Bakker48916f92012-09-16 19:57:18 +00007997 ssl->state++;
7998
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007999 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008000}
8001
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008002int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
Paul Bakker1ef83d62012-04-11 12:09:53 +00008003{
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02008004 int ret, hash_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00008005
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008006 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00008007
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008008 ssl_update_out_pointers( ssl, ssl->transform_negotiate );
Paul Bakker92be97b2013-01-02 17:30:03 +01008009
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008010 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00008011
Manuel Pégourié-Gonnard214a8482016-02-22 11:27:26 +01008012 /*
8013 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
8014 * may define some other value. Currently (early 2016), no defined
8015 * ciphersuite does this (and this is unlikely to change as activity has
8016 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
8017 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008018 hash_len = ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) ? 36 : 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00008019
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008020#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00008021 ssl->verify_data_len = hash_len;
8022 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008023#endif
Paul Bakker48916f92012-09-16 19:57:18 +00008024
Paul Bakker5121ce52009-01-03 21:22:43 +00008025 ssl->out_msglen = 4 + hash_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008026 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
8027 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
Paul Bakker5121ce52009-01-03 21:22:43 +00008028
8029 /*
8030 * In case of session resuming, invert the client and server
8031 * ChangeCipherSpec messages order.
8032 */
Paul Bakker0a597072012-09-25 21:55:46 +00008033 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008034 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008035#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008036 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008037 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01008038#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008039#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008040 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008041 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01008042#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00008043 }
8044 else
8045 ssl->state++;
8046
Paul Bakker48916f92012-09-16 19:57:18 +00008047 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02008048 * Switch to our negotiated transform and session parameters for outbound
8049 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00008050 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008051 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01008052
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008053#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008054 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02008055 {
8056 unsigned char i;
8057
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008058 /* Remember current epoch settings for resending */
8059 ssl->handshake->alt_transform_out = ssl->transform_out;
Hanno Becker19859472018-08-06 09:40:20 +01008060 memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008061
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02008062 /* Set sequence_number to zero */
Hanno Becker19859472018-08-06 09:40:20 +01008063 memset( ssl->cur_out_ctr + 2, 0, 6 );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02008064
8065 /* Increment epoch */
8066 for( i = 2; i > 0; i-- )
Hanno Becker19859472018-08-06 09:40:20 +01008067 if( ++ssl->cur_out_ctr[i - 1] != 0 )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02008068 break;
8069
8070 /* The loop goes to its end iff the counter is wrapping */
8071 if( i == 0 )
8072 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008073 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
8074 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02008075 }
8076 }
8077 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008078#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker19859472018-08-06 09:40:20 +01008079 memset( ssl->cur_out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00008080
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008081 ssl->transform_out = ssl->transform_negotiate;
8082 ssl->session_out = ssl->session_negotiate;
8083
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008084#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
8085 if( mbedtls_ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01008086 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008087 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Paul Bakker07eb38b2012-12-19 14:42:06 +01008088 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008089 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
8090 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker07eb38b2012-12-19 14:42:06 +01008091 }
8092 }
8093#endif
8094
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008095#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008096 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008097 mbedtls_ssl_send_flight_completed( ssl );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02008098#endif
8099
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02008100 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008101 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02008102 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008103 return( ret );
8104 }
8105
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02008106#if defined(MBEDTLS_SSL_PROTO_DTLS)
8107 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
8108 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
8109 {
8110 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
8111 return( ret );
8112 }
8113#endif
8114
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008115 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008116
8117 return( 0 );
8118}
8119
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008120#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00008121#define SSL_MAX_HASH_LEN 36
8122#else
8123#define SSL_MAX_HASH_LEN 12
8124#endif
8125
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008126int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008127{
Paul Bakker23986e52011-04-24 08:57:21 +00008128 int ret;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02008129 unsigned int hash_len;
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00008130 unsigned char buf[SSL_MAX_HASH_LEN];
Paul Bakker5121ce52009-01-03 21:22:43 +00008131
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008132 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008133
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008134 ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00008135
Hanno Becker327c93b2018-08-15 13:56:18 +01008136 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008137 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008138 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008139 return( ret );
8140 }
8141
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008142 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00008143 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008144 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02008145 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8146 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008147 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00008148 }
8149
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00008150 /* There is currently no ciphersuite using another length with TLS 1.2 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008151#if defined(MBEDTLS_SSL_PROTO_SSL3)
8152 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00008153 hash_len = 36;
8154 else
8155#endif
8156 hash_len = 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00008157
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008158 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED ||
8159 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00008160 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008161 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02008162 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8163 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008164 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00008165 }
8166
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008167 if( mbedtls_ssl_safer_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00008168 buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008169 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008170 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02008171 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8172 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008173 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00008174 }
8175
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008176#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00008177 ssl->verify_data_len = hash_len;
8178 memcpy( ssl->peer_verify_data, buf, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008179#endif
Paul Bakker48916f92012-09-16 19:57:18 +00008180
Paul Bakker0a597072012-09-25 21:55:46 +00008181 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008182 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008183#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008184 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008185 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01008186#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008187#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008188 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008189 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01008190#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00008191 }
8192 else
8193 ssl->state++;
8194
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008195#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008196 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008197 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008198#endif
8199
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008200 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008201
8202 return( 0 );
8203}
8204
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008205static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008206{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008207 memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008208
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008209#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
8210 defined(MBEDTLS_SSL_PROTO_TLS1_1)
8211 mbedtls_md5_init( &handshake->fin_md5 );
8212 mbedtls_sha1_init( &handshake->fin_sha1 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008213 mbedtls_md5_starts_ret( &handshake->fin_md5 );
8214 mbedtls_sha1_starts_ret( &handshake->fin_sha1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008215#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008216#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
8217#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05008218#if defined(MBEDTLS_USE_PSA_CRYPTO)
8219 handshake->fin_sha256_psa = psa_hash_operation_init();
8220 psa_hash_setup( &handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
8221#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008222 mbedtls_sha256_init( &handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008223 mbedtls_sha256_starts_ret( &handshake->fin_sha256, 0 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008224#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05008225#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008226#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05008227#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05008228 handshake->fin_sha384_psa = psa_hash_operation_init();
8229 psa_hash_setup( &handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05008230#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008231 mbedtls_sha512_init( &handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008232 mbedtls_sha512_starts_ret( &handshake->fin_sha512, 1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008233#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05008234#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008235#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008236
8237 handshake->update_checksum = ssl_update_checksum_start;
Hanno Becker7e5437a2017-04-28 17:15:26 +01008238
8239#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
8240 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
8241 mbedtls_ssl_sig_hash_set_init( &handshake->hash_algs );
8242#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008243
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008244#if defined(MBEDTLS_DHM_C)
8245 mbedtls_dhm_init( &handshake->dhm_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008246#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008247#if defined(MBEDTLS_ECDH_C)
8248 mbedtls_ecdh_init( &handshake->ecdh_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008249#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008250#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02008251 mbedtls_ecjpake_init( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02008252#if defined(MBEDTLS_SSL_CLI_C)
8253 handshake->ecjpake_cache = NULL;
8254 handshake->ecjpake_cache_len = 0;
8255#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02008256#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02008257
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02008258#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02008259 mbedtls_x509_crt_restart_init( &handshake->ecrs_ctx );
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02008260#endif
8261
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02008262#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
8263 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
8264#endif
Hanno Becker75173122019-02-06 16:18:31 +00008265
8266#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
8267 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8268 mbedtls_pk_init( &handshake->peer_pubkey );
8269#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008270}
8271
Hanno Beckera18d1322018-01-03 14:27:32 +00008272void mbedtls_ssl_transform_init( mbedtls_ssl_transform *transform )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008273{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008274 memset( transform, 0, sizeof(mbedtls_ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02008275
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008276 mbedtls_cipher_init( &transform->cipher_ctx_enc );
8277 mbedtls_cipher_init( &transform->cipher_ctx_dec );
Paul Bakker84bbeb52014-07-01 14:53:22 +02008278
Hanno Beckerd56ed242018-01-03 15:32:51 +00008279#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008280 mbedtls_md_init( &transform->md_ctx_enc );
8281 mbedtls_md_init( &transform->md_ctx_dec );
Hanno Beckerd56ed242018-01-03 15:32:51 +00008282#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008283}
8284
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008285void mbedtls_ssl_session_init( mbedtls_ssl_session *session )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008286{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008287 memset( session, 0, sizeof(mbedtls_ssl_session) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008288}
8289
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008290static int ssl_handshake_init( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00008291{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008292 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00008293 if( ssl->transform_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008294 mbedtls_ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008295 if( ssl->session_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008296 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008297 if( ssl->handshake )
Gilles Peskine9b562d52018-04-25 20:32:43 +02008298 mbedtls_ssl_handshake_free( ssl );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008299
8300 /*
8301 * Either the pointers are now NULL or cleared properly and can be freed.
8302 * Now allocate missing structures.
8303 */
8304 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008305 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008306 ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008307 }
Paul Bakker48916f92012-09-16 19:57:18 +00008308
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008309 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008310 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008311 ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008312 }
Paul Bakker48916f92012-09-16 19:57:18 +00008313
Paul Bakker82788fb2014-10-20 13:59:19 +02008314 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008315 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008316 ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008317 }
Paul Bakker48916f92012-09-16 19:57:18 +00008318
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008319 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00008320 if( ssl->handshake == NULL ||
8321 ssl->transform_negotiate == NULL ||
8322 ssl->session_negotiate == NULL )
8323 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02008324 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008325
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008326 mbedtls_free( ssl->handshake );
8327 mbedtls_free( ssl->transform_negotiate );
8328 mbedtls_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008329
8330 ssl->handshake = NULL;
8331 ssl->transform_negotiate = NULL;
8332 ssl->session_negotiate = NULL;
8333
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008334 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker48916f92012-09-16 19:57:18 +00008335 }
8336
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008337 /* Initialize structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008338 mbedtls_ssl_session_init( ssl->session_negotiate );
Hanno Beckera18d1322018-01-03 14:27:32 +00008339 mbedtls_ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02008340 ssl_handshake_params_init( ssl->handshake );
8341
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008342#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02008343 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
8344 {
8345 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008346
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02008347 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
8348 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
8349 else
8350 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008351
8352 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02008353 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008354#endif
8355
Paul Bakker48916f92012-09-16 19:57:18 +00008356 return( 0 );
8357}
8358
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008359#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008360/* Dummy cookie callbacks for defaults */
8361static int ssl_cookie_write_dummy( void *ctx,
8362 unsigned char **p, unsigned char *end,
8363 const unsigned char *cli_id, size_t cli_id_len )
8364{
8365 ((void) ctx);
8366 ((void) p);
8367 ((void) end);
8368 ((void) cli_id);
8369 ((void) cli_id_len);
8370
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008371 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008372}
8373
8374static int ssl_cookie_check_dummy( void *ctx,
8375 const unsigned char *cookie, size_t cookie_len,
8376 const unsigned char *cli_id, size_t cli_id_len )
8377{
8378 ((void) ctx);
8379 ((void) cookie);
8380 ((void) cookie_len);
8381 ((void) cli_id);
8382 ((void) cli_id_len);
8383
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008384 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008385}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008386#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008387
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008388/* Once ssl->out_hdr as the address of the beginning of the
8389 * next outgoing record is set, deduce the other pointers.
8390 *
8391 * Note: For TLS, we save the implicit record sequence number
8392 * (entering MAC computation) in the 8 bytes before ssl->out_hdr,
8393 * and the caller has to make sure there's space for this.
8394 */
8395
8396static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
8397 mbedtls_ssl_transform *transform )
8398{
8399#if defined(MBEDTLS_SSL_PROTO_DTLS)
8400 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
8401 {
8402 ssl->out_ctr = ssl->out_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008403#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008404 ssl->out_cid = ssl->out_ctr + 8;
8405 ssl->out_len = ssl->out_cid;
8406 if( transform != NULL )
8407 ssl->out_len += transform->out_cid_len;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008408#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008409 ssl->out_len = ssl->out_ctr + 8;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008410#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008411 ssl->out_iv = ssl->out_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008412 }
8413 else
8414#endif
8415 {
8416 ssl->out_ctr = ssl->out_hdr - 8;
8417 ssl->out_len = ssl->out_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008418#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker4c3eb7c2019-05-08 16:43:21 +01008419 ssl->out_cid = ssl->out_len;
8420#endif
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008421 ssl->out_iv = ssl->out_hdr + 5;
8422 }
8423
8424 /* Adjust out_msg to make space for explicit IV, if used. */
8425 if( transform != NULL &&
8426 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
8427 {
8428 ssl->out_msg = ssl->out_iv + transform->ivlen - transform->fixed_ivlen;
8429 }
8430 else
8431 ssl->out_msg = ssl->out_iv;
8432}
8433
8434/* Once ssl->in_hdr as the address of the beginning of the
8435 * next incoming record is set, deduce the other pointers.
8436 *
8437 * Note: For TLS, we save the implicit record sequence number
8438 * (entering MAC computation) in the 8 bytes before ssl->in_hdr,
8439 * and the caller has to make sure there's space for this.
8440 */
8441
Hanno Becker79594fd2019-05-08 09:38:41 +01008442static void ssl_update_in_pointers( mbedtls_ssl_context *ssl )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008443{
Hanno Becker79594fd2019-05-08 09:38:41 +01008444 /* This function sets the pointers to match the case
8445 * of unprotected TLS/DTLS records, with both ssl->in_iv
8446 * and ssl->in_msg pointing to the beginning of the record
8447 * content.
8448 *
8449 * When decrypting a protected record, ssl->in_msg
8450 * will be shifted to point to the beginning of the
8451 * record plaintext.
8452 */
8453
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008454#if defined(MBEDTLS_SSL_PROTO_DTLS)
8455 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
8456 {
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008457 /* This sets the header pointers to match records
8458 * without CID. When we receive a record containing
8459 * a CID, the fields are shifted accordingly in
8460 * ssl_parse_record_header(). */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008461 ssl->in_ctr = ssl->in_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008462#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008463 ssl->in_cid = ssl->in_ctr + 8;
8464 ssl->in_len = ssl->in_cid; /* Default: no CID */
Hanno Beckera0e20d02019-05-15 14:03:01 +01008465#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008466 ssl->in_len = ssl->in_ctr + 8;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008467#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008468 ssl->in_iv = ssl->in_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008469 }
8470 else
8471#endif
8472 {
8473 ssl->in_ctr = ssl->in_hdr - 8;
8474 ssl->in_len = ssl->in_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008475#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker4c3eb7c2019-05-08 16:43:21 +01008476 ssl->in_cid = ssl->in_len;
8477#endif
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008478 ssl->in_iv = ssl->in_hdr + 5;
8479 }
8480
Hanno Becker79594fd2019-05-08 09:38:41 +01008481 /* This will be adjusted at record decryption time. */
8482 ssl->in_msg = ssl->in_iv;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008483}
8484
Paul Bakker5121ce52009-01-03 21:22:43 +00008485/*
8486 * Initialize an SSL context
8487 */
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02008488void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
8489{
8490 memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
8491}
8492
8493/*
8494 * Setup an SSL context
8495 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008496
8497static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl )
8498{
8499 /* Set the incoming and outgoing record pointers. */
8500#if defined(MBEDTLS_SSL_PROTO_DTLS)
8501 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
8502 {
8503 ssl->out_hdr = ssl->out_buf;
8504 ssl->in_hdr = ssl->in_buf;
8505 }
8506 else
8507#endif /* MBEDTLS_SSL_PROTO_DTLS */
8508 {
8509 ssl->out_hdr = ssl->out_buf + 8;
8510 ssl->in_hdr = ssl->in_buf + 8;
8511 }
8512
8513 /* Derive other internal pointers. */
8514 ssl_update_out_pointers( ssl, NULL /* no transform enabled */ );
Hanno Becker79594fd2019-05-08 09:38:41 +01008515 ssl_update_in_pointers ( ssl );
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008516}
8517
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02008518int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +02008519 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +00008520{
Paul Bakker48916f92012-09-16 19:57:18 +00008521 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00008522
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02008523 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00008524
8525 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01008526 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00008527 */
k-stachowiakc9a5f022018-07-24 13:53:31 +02008528
8529 /* Set to NULL in case of an error condition */
8530 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02008531
Angus Grattond8213d02016-05-25 20:56:48 +10008532 ssl->in_buf = mbedtls_calloc( 1, MBEDTLS_SSL_IN_BUFFER_LEN );
8533 if( ssl->in_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00008534 {
Angus Grattond8213d02016-05-25 20:56:48 +10008535 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_IN_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02008536 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02008537 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +10008538 }
8539
8540 ssl->out_buf = mbedtls_calloc( 1, MBEDTLS_SSL_OUT_BUFFER_LEN );
8541 if( ssl->out_buf == NULL )
8542 {
8543 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_OUT_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02008544 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02008545 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00008546 }
8547
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008548 ssl_reset_in_out_pointers( ssl );
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02008549
Paul Bakker48916f92012-09-16 19:57:18 +00008550 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
k-stachowiaka47911c2018-07-04 17:41:58 +02008551 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00008552
8553 return( 0 );
k-stachowiaka47911c2018-07-04 17:41:58 +02008554
8555error:
8556 mbedtls_free( ssl->in_buf );
8557 mbedtls_free( ssl->out_buf );
8558
8559 ssl->conf = NULL;
8560
8561 ssl->in_buf = NULL;
8562 ssl->out_buf = NULL;
8563
8564 ssl->in_hdr = NULL;
8565 ssl->in_ctr = NULL;
8566 ssl->in_len = NULL;
8567 ssl->in_iv = NULL;
8568 ssl->in_msg = NULL;
8569
8570 ssl->out_hdr = NULL;
8571 ssl->out_ctr = NULL;
8572 ssl->out_len = NULL;
8573 ssl->out_iv = NULL;
8574 ssl->out_msg = NULL;
8575
k-stachowiak9f7798e2018-07-31 16:52:32 +02008576 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008577}
8578
8579/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00008580 * Reset an initialized and used SSL context for re-use while retaining
8581 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008582 *
8583 * If partial is non-zero, keep data in the input buffer and client ID.
8584 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00008585 */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008586static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +00008587{
Paul Bakker48916f92012-09-16 19:57:18 +00008588 int ret;
8589
Hanno Becker7e772132018-08-10 12:38:21 +01008590#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || \
8591 !defined(MBEDTLS_SSL_SRV_C)
8592 ((void) partial);
8593#endif
8594
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008595 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008596
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008597 /* Cancel any possibly running timer */
8598 ssl_set_timer( ssl, 0 );
8599
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008600#if defined(MBEDTLS_SSL_RENEGOTIATION)
8601 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008602 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00008603
8604 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008605 memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
8606 memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008607#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008608 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00008609
Paul Bakker7eb013f2011-10-06 12:37:39 +00008610 ssl->in_offt = NULL;
Hanno Beckerf29d4702018-08-10 11:31:15 +01008611 ssl_reset_in_out_pointers( ssl );
Paul Bakker7eb013f2011-10-06 12:37:39 +00008612
8613 ssl->in_msgtype = 0;
8614 ssl->in_msglen = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008615#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02008616 ssl->next_record_offset = 0;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02008617 ssl->in_epoch = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02008618#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008619#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02008620 ssl_dtls_replay_reset( ssl );
8621#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00008622
8623 ssl->in_hslen = 0;
8624 ssl->nb_zero = 0;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01008625
8626 ssl->keep_current_message = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00008627
8628 ssl->out_msgtype = 0;
8629 ssl->out_msglen = 0;
8630 ssl->out_left = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008631#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
8632 if( ssl->split_done != MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008633 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008634#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00008635
Hanno Becker19859472018-08-06 09:40:20 +01008636 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
8637
Paul Bakker48916f92012-09-16 19:57:18 +00008638 ssl->transform_in = NULL;
8639 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00008640
Hanno Becker78640902018-08-13 16:35:15 +01008641 ssl->session_in = NULL;
8642 ssl->session_out = NULL;
8643
Angus Grattond8213d02016-05-25 20:56:48 +10008644 memset( ssl->out_buf, 0, MBEDTLS_SSL_OUT_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01008645
8646#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008647 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01008648#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
8649 {
8650 ssl->in_left = 0;
Angus Grattond8213d02016-05-25 20:56:48 +10008651 memset( ssl->in_buf, 0, MBEDTLS_SSL_IN_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01008652 }
Paul Bakker05ef8352012-05-08 09:17:57 +00008653
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008654#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
8655 if( mbedtls_ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00008656 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008657 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_reset()" ) );
8658 if( ( ret = mbedtls_ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00008659 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008660 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_reset", ret );
8661 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00008662 }
Paul Bakker05ef8352012-05-08 09:17:57 +00008663 }
8664#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00008665
Paul Bakker48916f92012-09-16 19:57:18 +00008666 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00008667 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008668 mbedtls_ssl_transform_free( ssl->transform );
8669 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00008670 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00008671 }
Paul Bakker48916f92012-09-16 19:57:18 +00008672
Paul Bakkerc0463502013-02-14 11:19:38 +01008673 if( ssl->session )
8674 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008675 mbedtls_ssl_session_free( ssl->session );
8676 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01008677 ssl->session = NULL;
8678 }
8679
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008680#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008681 ssl->alpn_chosen = NULL;
8682#endif
8683
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008684#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker4ccbf062018-08-10 11:20:38 +01008685#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008686 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01008687#endif
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008688 {
8689 mbedtls_free( ssl->cli_id );
8690 ssl->cli_id = NULL;
8691 ssl->cli_id_len = 0;
8692 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02008693#endif
8694
Paul Bakker48916f92012-09-16 19:57:18 +00008695 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
8696 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00008697
8698 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00008699}
8700
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02008701/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008702 * Reset an initialized and used SSL context for re-use while retaining
8703 * all application-set variables, function pointers and data.
8704 */
8705int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
8706{
8707 return( ssl_session_reset_int( ssl, 0 ) );
8708}
8709
8710/*
Paul Bakker5121ce52009-01-03 21:22:43 +00008711 * SSL set accessors
8712 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008713void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00008714{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008715 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00008716}
8717
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008718void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01008719{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008720 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01008721}
8722
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008723#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008724void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02008725{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008726 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02008727}
8728#endif
8729
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008730#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008731void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf, unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02008732{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008733 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02008734}
8735#endif
8736
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008737#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01008738
Hanno Becker1841b0a2018-08-24 11:13:57 +01008739void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl,
8740 unsigned allow_packing )
Hanno Becker04da1892018-08-14 13:22:10 +01008741{
8742 ssl->disable_datagram_packing = !allow_packing;
8743}
8744
8745void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
8746 uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02008747{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008748 conf->hs_timeout_min = min;
8749 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02008750}
8751#endif
8752
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008753void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00008754{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008755 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00008756}
8757
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008758#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008759void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02008760 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008761 void *p_vrfy )
8762{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008763 conf->f_vrfy = f_vrfy;
8764 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008765}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008766#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008767
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008768void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00008769 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00008770 void *p_rng )
8771{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01008772 conf->f_rng = f_rng;
8773 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00008774}
8775
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008776void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02008777 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00008778 void *p_dbg )
8779{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008780 conf->f_dbg = f_dbg;
8781 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00008782}
8783
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008784void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008785 void *p_bio,
Simon Butchere846b512016-03-01 17:31:49 +00008786 mbedtls_ssl_send_t *f_send,
8787 mbedtls_ssl_recv_t *f_recv,
8788 mbedtls_ssl_recv_timeout_t *f_recv_timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008789{
8790 ssl->p_bio = p_bio;
8791 ssl->f_send = f_send;
8792 ssl->f_recv = f_recv;
8793 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01008794}
8795
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02008796#if defined(MBEDTLS_SSL_PROTO_DTLS)
8797void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu )
8798{
8799 ssl->mtu = mtu;
8800}
8801#endif
8802
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008803void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01008804{
8805 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008806}
8807
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008808void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
8809 void *p_timer,
Simon Butchere846b512016-03-01 17:31:49 +00008810 mbedtls_ssl_set_timer_t *f_set_timer,
8811 mbedtls_ssl_get_timer_t *f_get_timer )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008812{
8813 ssl->p_timer = p_timer;
8814 ssl->f_set_timer = f_set_timer;
8815 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008816
8817 /* Make sure we start with no timer running */
8818 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008819}
8820
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008821#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008822void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01008823 void *p_cache,
8824 int (*f_get_cache)(void *, mbedtls_ssl_session *),
8825 int (*f_set_cache)(void *, const mbedtls_ssl_session *) )
Paul Bakker5121ce52009-01-03 21:22:43 +00008826{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01008827 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008828 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008829 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00008830}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008831#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008832
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008833#if defined(MBEDTLS_SSL_CLI_C)
8834int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00008835{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008836 int ret;
8837
8838 if( ssl == NULL ||
8839 session == NULL ||
8840 ssl->session_negotiate == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008841 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008842 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008843 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008844 }
8845
Hanno Becker52055ae2019-02-06 14:30:46 +00008846 if( ( ret = mbedtls_ssl_session_copy( ssl->session_negotiate,
8847 session ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008848 return( ret );
8849
Paul Bakker0a597072012-09-25 21:55:46 +00008850 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008851
8852 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00008853}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008854#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008855
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008856void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008857 const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00008858{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008859 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = ciphersuites;
8860 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = ciphersuites;
8861 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = ciphersuites;
8862 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = ciphersuites;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008863}
8864
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008865void mbedtls_ssl_conf_ciphersuites_for_version( mbedtls_ssl_config *conf,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02008866 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008867 int major, int minor )
8868{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008869 if( major != MBEDTLS_SSL_MAJOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008870 return;
8871
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008872 if( minor < MBEDTLS_SSL_MINOR_VERSION_0 || minor > MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008873 return;
8874
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008875 conf->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00008876}
8877
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008878#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02008879void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01008880 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02008881{
8882 conf->cert_profile = profile;
8883}
8884
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008885/* Append a new keycert entry to a (possibly empty) list */
8886static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
8887 mbedtls_x509_crt *cert,
8888 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008889{
niisato8ee24222018-06-25 19:05:48 +09008890 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008891
niisato8ee24222018-06-25 19:05:48 +09008892 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
8893 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008894 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008895
niisato8ee24222018-06-25 19:05:48 +09008896 new_cert->cert = cert;
8897 new_cert->key = key;
8898 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008899
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008900 /* Update head is the list was null, else add to the end */
8901 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01008902 {
niisato8ee24222018-06-25 19:05:48 +09008903 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01008904 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008905 else
8906 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008907 mbedtls_ssl_key_cert *cur = *head;
8908 while( cur->next != NULL )
8909 cur = cur->next;
niisato8ee24222018-06-25 19:05:48 +09008910 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008911 }
8912
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008913 return( 0 );
8914}
8915
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008916int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008917 mbedtls_x509_crt *own_cert,
8918 mbedtls_pk_context *pk_key )
8919{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02008920 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008921}
8922
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008923void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008924 mbedtls_x509_crt *ca_chain,
8925 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008926{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008927 conf->ca_chain = ca_chain;
8928 conf->ca_crl = ca_crl;
Hanno Becker5adaad92019-03-27 16:54:37 +00008929
8930#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
8931 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
8932 * cannot be used together. */
8933 conf->f_ca_cb = NULL;
8934 conf->p_ca_cb = NULL;
8935#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Paul Bakker5121ce52009-01-03 21:22:43 +00008936}
Hanno Becker5adaad92019-03-27 16:54:37 +00008937
8938#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
8939void mbedtls_ssl_conf_ca_cb( mbedtls_ssl_config *conf,
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00008940 mbedtls_x509_crt_ca_cb_t f_ca_cb,
Hanno Becker5adaad92019-03-27 16:54:37 +00008941 void *p_ca_cb )
8942{
8943 conf->f_ca_cb = f_ca_cb;
8944 conf->p_ca_cb = p_ca_cb;
8945
8946 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
8947 * cannot be used together. */
8948 conf->ca_chain = NULL;
8949 conf->ca_crl = NULL;
8950}
8951#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008952#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00008953
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02008954#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
8955int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
8956 mbedtls_x509_crt *own_cert,
8957 mbedtls_pk_context *pk_key )
8958{
8959 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
8960 own_cert, pk_key ) );
8961}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02008962
8963void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
8964 mbedtls_x509_crt *ca_chain,
8965 mbedtls_x509_crl *ca_crl )
8966{
8967 ssl->handshake->sni_ca_chain = ca_chain;
8968 ssl->handshake->sni_ca_crl = ca_crl;
8969}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02008970
8971void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
8972 int authmode )
8973{
8974 ssl->handshake->sni_authmode = authmode;
8975}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02008976#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
8977
Hanno Becker8927c832019-04-03 12:52:50 +01008978#if defined(MBEDTLS_X509_CRT_PARSE_C)
8979void mbedtls_ssl_set_verify( mbedtls_ssl_context *ssl,
8980 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
8981 void *p_vrfy )
8982{
8983 ssl->f_vrfy = f_vrfy;
8984 ssl->p_vrfy = p_vrfy;
8985}
8986#endif
8987
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008988#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008989/*
8990 * Set EC J-PAKE password for current handshake
8991 */
8992int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
8993 const unsigned char *pw,
8994 size_t pw_len )
8995{
8996 mbedtls_ecjpake_role role;
8997
Janos Follath8eb64132016-06-03 15:40:57 +01008998 if( ssl->handshake == NULL || ssl->conf == NULL )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008999 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9000
9001 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
9002 role = MBEDTLS_ECJPAKE_SERVER;
9003 else
9004 role = MBEDTLS_ECJPAKE_CLIENT;
9005
9006 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
9007 role,
9008 MBEDTLS_MD_SHA256,
9009 MBEDTLS_ECP_DP_SECP256R1,
9010 pw, pw_len ) );
9011}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02009012#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02009013
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009014#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01009015
9016static void ssl_conf_remove_psk( mbedtls_ssl_config *conf )
9017{
9018 /* Remove reference to existing PSK, if any. */
9019#if defined(MBEDTLS_USE_PSA_CRYPTO)
9020 if( conf->psk_opaque != 0 )
9021 {
9022 /* The maintenance of the PSK key slot is the
9023 * user's responsibility. */
9024 conf->psk_opaque = 0;
9025 }
Hanno Beckera63ac3f2018-11-05 12:47:16 +00009026 /* This and the following branch should never
9027 * be taken simultaenously as we maintain the
9028 * invariant that raw and opaque PSKs are never
9029 * configured simultaneously. As a safeguard,
9030 * though, `else` is omitted here. */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01009031#endif /* MBEDTLS_USE_PSA_CRYPTO */
9032 if( conf->psk != NULL )
9033 {
9034 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
9035
9036 mbedtls_free( conf->psk );
9037 conf->psk = NULL;
9038 conf->psk_len = 0;
9039 }
9040
9041 /* Remove reference to PSK identity, if any. */
9042 if( conf->psk_identity != NULL )
9043 {
9044 mbedtls_free( conf->psk_identity );
9045 conf->psk_identity = NULL;
9046 conf->psk_identity_len = 0;
9047 }
9048}
9049
Hanno Becker7390c712018-11-15 13:33:04 +00009050/* This function assumes that PSK identity in the SSL config is unset.
9051 * It checks that the provided identity is well-formed and attempts
9052 * to make a copy of it in the SSL config.
9053 * On failure, the PSK identity in the config remains unset. */
9054static int ssl_conf_set_psk_identity( mbedtls_ssl_config *conf,
9055 unsigned char const *psk_identity,
9056 size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02009057{
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02009058 /* Identity len will be encoded on two bytes */
Hanno Becker7390c712018-11-15 13:33:04 +00009059 if( psk_identity == NULL ||
9060 ( psk_identity_len >> 16 ) != 0 ||
Angus Grattond8213d02016-05-25 20:56:48 +10009061 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02009062 {
9063 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9064 }
9065
Hanno Becker7390c712018-11-15 13:33:04 +00009066 conf->psk_identity = mbedtls_calloc( 1, psk_identity_len );
9067 if( conf->psk_identity == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02009068 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker6db455e2013-09-18 17:29:31 +02009069
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01009070 conf->psk_identity_len = psk_identity_len;
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01009071 memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker5ad403f2013-09-18 21:21:30 +02009072
9073 return( 0 );
Paul Bakker6db455e2013-09-18 17:29:31 +02009074}
9075
Hanno Becker7390c712018-11-15 13:33:04 +00009076int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
9077 const unsigned char *psk, size_t psk_len,
9078 const unsigned char *psk_identity, size_t psk_identity_len )
9079{
9080 int ret;
9081 /* Remove opaque/raw PSK + PSK Identity */
9082 ssl_conf_remove_psk( conf );
9083
9084 /* Check and set raw PSK */
9085 if( psk == NULL || psk_len > MBEDTLS_PSK_MAX_LEN )
9086 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9087 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
9088 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
9089 conf->psk_len = psk_len;
9090 memcpy( conf->psk, psk, conf->psk_len );
9091
9092 /* Check and set PSK Identity */
9093 ret = ssl_conf_set_psk_identity( conf, psk_identity, psk_identity_len );
9094 if( ret != 0 )
9095 ssl_conf_remove_psk( conf );
9096
9097 return( ret );
9098}
9099
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01009100static void ssl_remove_psk( mbedtls_ssl_context *ssl )
9101{
9102#if defined(MBEDTLS_USE_PSA_CRYPTO)
9103 if( ssl->handshake->psk_opaque != 0 )
9104 {
9105 ssl->handshake->psk_opaque = 0;
9106 }
9107 else
9108#endif /* MBEDTLS_USE_PSA_CRYPTO */
9109 if( ssl->handshake->psk != NULL )
9110 {
9111 mbedtls_platform_zeroize( ssl->handshake->psk,
9112 ssl->handshake->psk_len );
9113 mbedtls_free( ssl->handshake->psk );
9114 ssl->handshake->psk_len = 0;
9115 }
9116}
9117
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01009118int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
9119 const unsigned char *psk, size_t psk_len )
9120{
9121 if( psk == NULL || ssl->handshake == NULL )
9122 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9123
9124 if( psk_len > MBEDTLS_PSK_MAX_LEN )
9125 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9126
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01009127 ssl_remove_psk( ssl );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01009128
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02009129 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02009130 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01009131
9132 ssl->handshake->psk_len = psk_len;
9133 memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
9134
9135 return( 0 );
9136}
9137
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01009138#if defined(MBEDTLS_USE_PSA_CRYPTO)
9139int mbedtls_ssl_conf_psk_opaque( mbedtls_ssl_config *conf,
Andrzej Kurek2349c4d2019-01-08 09:36:01 -05009140 psa_key_handle_t psk_slot,
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01009141 const unsigned char *psk_identity,
9142 size_t psk_identity_len )
9143{
Hanno Becker7390c712018-11-15 13:33:04 +00009144 int ret;
9145 /* Clear opaque/raw PSK + PSK Identity, if present. */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01009146 ssl_conf_remove_psk( conf );
9147
Hanno Becker7390c712018-11-15 13:33:04 +00009148 /* Check and set opaque PSK */
9149 if( psk_slot == 0 )
9150 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01009151 conf->psk_opaque = psk_slot;
Hanno Becker7390c712018-11-15 13:33:04 +00009152
9153 /* Check and set PSK Identity */
9154 ret = ssl_conf_set_psk_identity( conf, psk_identity,
9155 psk_identity_len );
9156 if( ret != 0 )
9157 ssl_conf_remove_psk( conf );
9158
9159 return( ret );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01009160}
9161
9162int mbedtls_ssl_set_hs_psk_opaque( mbedtls_ssl_context *ssl,
Andrzej Kurek2349c4d2019-01-08 09:36:01 -05009163 psa_key_handle_t psk_slot )
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01009164{
9165 if( psk_slot == 0 || ssl->handshake == NULL )
9166 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9167
9168 ssl_remove_psk( ssl );
9169 ssl->handshake->psk_opaque = psk_slot;
9170 return( 0 );
9171}
9172#endif /* MBEDTLS_USE_PSA_CRYPTO */
9173
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009174void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009175 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
Paul Bakker6db455e2013-09-18 17:29:31 +02009176 size_t),
9177 void *p_psk )
9178{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009179 conf->f_psk = f_psk;
9180 conf->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02009181}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009182#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00009183
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02009184#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker470a8c42017-10-04 15:28:46 +01009185
9186#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009187int mbedtls_ssl_conf_dh_param( mbedtls_ssl_config *conf, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00009188{
9189 int ret;
9190
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01009191 if( ( ret = mbedtls_mpi_read_string( &conf->dhm_P, 16, dhm_P ) ) != 0 ||
9192 ( ret = mbedtls_mpi_read_string( &conf->dhm_G, 16, dhm_G ) ) != 0 )
9193 {
9194 mbedtls_mpi_free( &conf->dhm_P );
9195 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00009196 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01009197 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009198
9199 return( 0 );
9200}
Hanno Becker470a8c42017-10-04 15:28:46 +01009201#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +00009202
Hanno Beckera90658f2017-10-04 15:29:08 +01009203int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
9204 const unsigned char *dhm_P, size_t P_len,
9205 const unsigned char *dhm_G, size_t G_len )
9206{
9207 int ret;
9208
9209 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
9210 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
9211 {
9212 mbedtls_mpi_free( &conf->dhm_P );
9213 mbedtls_mpi_free( &conf->dhm_G );
9214 return( ret );
9215 }
9216
9217 return( 0 );
9218}
Paul Bakker5121ce52009-01-03 21:22:43 +00009219
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009220int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00009221{
9222 int ret;
9223
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01009224 if( ( ret = mbedtls_mpi_copy( &conf->dhm_P, &dhm_ctx->P ) ) != 0 ||
9225 ( ret = mbedtls_mpi_copy( &conf->dhm_G, &dhm_ctx->G ) ) != 0 )
9226 {
9227 mbedtls_mpi_free( &conf->dhm_P );
9228 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00009229 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01009230 }
Paul Bakker1b57b062011-01-06 15:48:19 +00009231
9232 return( 0 );
9233}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02009234#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00009235
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02009236#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
9237/*
9238 * Set the minimum length for Diffie-Hellman parameters
9239 */
9240void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
9241 unsigned int bitlen )
9242{
9243 conf->dhm_min_bitlen = bitlen;
9244}
9245#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
9246
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009247#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02009248/*
9249 * Set allowed/preferred hashes for handshake signatures
9250 */
9251void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
9252 const int *hashes )
9253{
9254 conf->sig_hashes = hashes;
9255}
Hanno Becker947194e2017-04-07 13:25:49 +01009256#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02009257
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02009258#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01009259/*
9260 * Set the allowed elliptic curves
9261 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009262void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009263 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01009264{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009265 conf->curve_list = curve_list;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01009266}
Hanno Becker947194e2017-04-07 13:25:49 +01009267#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01009268
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01009269#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009270int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00009271{
Hanno Becker947194e2017-04-07 13:25:49 +01009272 /* Initialize to suppress unnecessary compiler warning */
9273 size_t hostname_len = 0;
9274
9275 /* Check if new hostname is valid before
9276 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01009277 if( hostname != NULL )
9278 {
9279 hostname_len = strlen( hostname );
9280
9281 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
9282 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9283 }
9284
9285 /* Now it's clear that we will overwrite the old hostname,
9286 * so we can free it safely */
9287
9288 if( ssl->hostname != NULL )
9289 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009290 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Hanno Becker947194e2017-04-07 13:25:49 +01009291 mbedtls_free( ssl->hostname );
9292 }
9293
9294 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01009295
Paul Bakker5121ce52009-01-03 21:22:43 +00009296 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01009297 {
9298 ssl->hostname = NULL;
9299 }
9300 else
9301 {
9302 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01009303 if( ssl->hostname == NULL )
9304 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02009305
Hanno Becker947194e2017-04-07 13:25:49 +01009306 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02009307
Hanno Becker947194e2017-04-07 13:25:49 +01009308 ssl->hostname[hostname_len] = '\0';
9309 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009310
9311 return( 0 );
9312}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01009313#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00009314
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01009315#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009316void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009317 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00009318 const unsigned char *, size_t),
9319 void *p_sni )
9320{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009321 conf->f_sni = f_sni;
9322 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00009323}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009324#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00009325
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009326#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009327int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009328{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009329 size_t cur_len, tot_len;
9330 const char **p;
9331
9332 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08009333 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
9334 * MUST NOT be truncated."
9335 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009336 */
9337 tot_len = 0;
9338 for( p = protos; *p != NULL; p++ )
9339 {
9340 cur_len = strlen( *p );
9341 tot_len += cur_len;
9342
9343 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009344 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009345 }
9346
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009347 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009348
9349 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009350}
9351
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009352const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009353{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009354 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009355}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009356#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009357
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02009358void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00009359{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009360 conf->max_major_ver = major;
9361 conf->max_minor_ver = minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00009362}
9363
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02009364void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00009365{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009366 conf->min_major_ver = major;
9367 conf->min_minor_ver = minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00009368}
9369
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009370#if defined(MBEDTLS_SSL_FALLBACK_SCSV) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009371void mbedtls_ssl_conf_fallback( mbedtls_ssl_config *conf, char fallback )
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02009372{
Manuel Pégourié-Gonnard684b0592015-05-06 09:27:31 +01009373 conf->fallback = fallback;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02009374}
9375#endif
9376
Janos Follath088ce432017-04-10 12:42:31 +01009377#if defined(MBEDTLS_SSL_SRV_C)
9378void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
9379 char cert_req_ca_list )
9380{
9381 conf->cert_req_ca_list = cert_req_ca_list;
9382}
9383#endif
9384
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009385#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009386void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01009387{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009388 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01009389}
9390#endif
9391
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009392#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009393void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02009394{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009395 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02009396}
9397#endif
9398
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02009399#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009400void mbedtls_ssl_conf_arc4_support( mbedtls_ssl_config *conf, char arc4 )
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01009401{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009402 conf->arc4_disabled = arc4;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01009403}
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02009404#endif
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01009405
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009406#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009407int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009408{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009409 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
Angus Grattond8213d02016-05-25 20:56:48 +10009410 ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009411 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009412 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009413 }
9414
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01009415 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009416
9417 return( 0 );
9418}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009419#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009420
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009421#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02009422void mbedtls_ssl_conf_truncated_hmac( mbedtls_ssl_config *conf, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02009423{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009424 conf->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02009425}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009426#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02009427
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009428#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009429void mbedtls_ssl_conf_cbc_record_splitting( mbedtls_ssl_config *conf, char split )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009430{
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01009431 conf->cbc_record_splitting = split;
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009432}
9433#endif
9434
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009435void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00009436{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009437 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00009438}
9439
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009440#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009441void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01009442{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009443 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01009444}
9445
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009446void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009447{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009448 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009449}
9450
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009451void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01009452 const unsigned char period[8] )
9453{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009454 memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01009455}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009456#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00009457
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009458#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009459#if defined(MBEDTLS_SSL_CLI_C)
9460void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02009461{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01009462 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02009463}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009464#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02009465
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009466#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02009467void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
9468 mbedtls_ssl_ticket_write_t *f_ticket_write,
9469 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
9470 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02009471{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02009472 conf->f_ticket_write = f_ticket_write;
9473 conf->f_ticket_parse = f_ticket_parse;
9474 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02009475}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009476#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009477#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02009478
Robert Cragie4feb7ae2015-10-02 13:33:37 +01009479#if defined(MBEDTLS_SSL_EXPORT_KEYS)
9480void mbedtls_ssl_conf_export_keys_cb( mbedtls_ssl_config *conf,
9481 mbedtls_ssl_export_keys_t *f_export_keys,
9482 void *p_export_keys )
9483{
9484 conf->f_export_keys = f_export_keys;
9485 conf->p_export_keys = p_export_keys;
9486}
Ron Eldorf5cc10d2019-05-07 18:33:40 +03009487
9488void mbedtls_ssl_conf_export_keys_ext_cb( mbedtls_ssl_config *conf,
9489 mbedtls_ssl_export_keys_ext_t *f_export_keys_ext,
9490 void *p_export_keys )
9491{
9492 conf->f_export_keys_ext = f_export_keys_ext;
9493 conf->p_export_keys = p_export_keys;
9494}
Robert Cragie4feb7ae2015-10-02 13:33:37 +01009495#endif
9496
Gilles Peskineb74a1c72018-04-24 13:09:22 +02009497#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009498void mbedtls_ssl_conf_async_private_cb(
9499 mbedtls_ssl_config *conf,
9500 mbedtls_ssl_async_sign_t *f_async_sign,
9501 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
9502 mbedtls_ssl_async_resume_t *f_async_resume,
9503 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009504 void *async_config_data )
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009505{
9506 conf->f_async_sign_start = f_async_sign;
9507 conf->f_async_decrypt_start = f_async_decrypt;
9508 conf->f_async_resume = f_async_resume;
9509 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009510 conf->p_async_config_data = async_config_data;
9511}
9512
Gilles Peskine8f97af72018-04-26 11:46:10 +02009513void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
9514{
9515 return( conf->p_async_config_data );
9516}
9517
Gilles Peskine1febfef2018-04-30 11:54:39 +02009518void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009519{
9520 if( ssl->handshake == NULL )
9521 return( NULL );
9522 else
9523 return( ssl->handshake->user_async_ctx );
9524}
9525
Gilles Peskine1febfef2018-04-30 11:54:39 +02009526void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009527 void *ctx )
9528{
9529 if( ssl->handshake != NULL )
9530 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009531}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02009532#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009533
Paul Bakker5121ce52009-01-03 21:22:43 +00009534/*
9535 * SSL get accessors
9536 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009537size_t mbedtls_ssl_get_bytes_avail( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009538{
9539 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
9540}
9541
Hanno Becker8b170a02017-10-10 11:51:19 +01009542int mbedtls_ssl_check_pending( const mbedtls_ssl_context *ssl )
9543{
9544 /*
9545 * Case A: We're currently holding back
9546 * a message for further processing.
9547 */
9548
9549 if( ssl->keep_current_message == 1 )
9550 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009551 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: record held back for processing" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009552 return( 1 );
9553 }
9554
9555 /*
9556 * Case B: Further records are pending in the current datagram.
9557 */
9558
9559#if defined(MBEDTLS_SSL_PROTO_DTLS)
9560 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
9561 ssl->in_left > ssl->next_record_offset )
9562 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009563 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more records within current datagram" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009564 return( 1 );
9565 }
9566#endif /* MBEDTLS_SSL_PROTO_DTLS */
9567
9568 /*
9569 * Case C: A handshake message is being processed.
9570 */
9571
Hanno Becker8b170a02017-10-10 11:51:19 +01009572 if( ssl->in_hslen > 0 && ssl->in_hslen < ssl->in_msglen )
9573 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009574 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more handshake messages within current record" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009575 return( 1 );
9576 }
9577
9578 /*
9579 * Case D: An application data message is being processed
9580 */
9581 if( ssl->in_offt != NULL )
9582 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009583 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: application data record is being processed" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009584 return( 1 );
9585 }
9586
9587 /*
9588 * In all other cases, the rest of the message can be dropped.
Hanno Beckerc573ac32018-08-28 17:15:25 +01009589 * As in ssl_get_next_record, this needs to be adapted if
Hanno Becker8b170a02017-10-10 11:51:19 +01009590 * we implement support for multiple alerts in single records.
9591 */
9592
9593 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: nothing pending" ) );
9594 return( 0 );
9595}
9596
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02009597uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009598{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00009599 if( ssl->session != NULL )
9600 return( ssl->session->verify_result );
9601
9602 if( ssl->session_negotiate != NULL )
9603 return( ssl->session_negotiate->verify_result );
9604
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02009605 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00009606}
9607
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009608const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00009609{
Paul Bakker926c8e42013-03-06 10:23:34 +01009610 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009611 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01009612
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009613 return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00009614}
9615
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009616const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00009617{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009618#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009619 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009620 {
9621 switch( ssl->minor_ver )
9622 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009623 case MBEDTLS_SSL_MINOR_VERSION_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009624 return( "DTLSv1.0" );
9625
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009626 case MBEDTLS_SSL_MINOR_VERSION_3:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009627 return( "DTLSv1.2" );
9628
9629 default:
9630 return( "unknown (DTLS)" );
9631 }
9632 }
9633#endif
9634
Paul Bakker43ca69c2011-01-15 17:35:19 +00009635 switch( ssl->minor_ver )
9636 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009637 case MBEDTLS_SSL_MINOR_VERSION_0:
Paul Bakker43ca69c2011-01-15 17:35:19 +00009638 return( "SSLv3.0" );
9639
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009640 case MBEDTLS_SSL_MINOR_VERSION_1:
Paul Bakker43ca69c2011-01-15 17:35:19 +00009641 return( "TLSv1.0" );
9642
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009643 case MBEDTLS_SSL_MINOR_VERSION_2:
Paul Bakker43ca69c2011-01-15 17:35:19 +00009644 return( "TLSv1.1" );
9645
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009646 case MBEDTLS_SSL_MINOR_VERSION_3:
Paul Bakker1ef83d62012-04-11 12:09:53 +00009647 return( "TLSv1.2" );
9648
Paul Bakker43ca69c2011-01-15 17:35:19 +00009649 default:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009650 return( "unknown" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00009651 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00009652}
9653
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009654int mbedtls_ssl_get_record_expansion( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009655{
Hanno Becker3136ede2018-08-17 15:28:19 +01009656 size_t transform_expansion = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009657 const mbedtls_ssl_transform *transform = ssl->transform_out;
Hanno Becker5b559ac2018-08-03 09:40:07 +01009658 unsigned block_size;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009659
Hanno Becker5903de42019-05-03 14:46:38 +01009660 size_t out_hdr_len = mbedtls_ssl_out_hdr_len( ssl );
9661
Hanno Becker78640902018-08-13 16:35:15 +01009662 if( transform == NULL )
Hanno Becker5903de42019-05-03 14:46:38 +01009663 return( (int) out_hdr_len );
Hanno Becker78640902018-08-13 16:35:15 +01009664
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009665#if defined(MBEDTLS_ZLIB_SUPPORT)
9666 if( ssl->session_out->compression != MBEDTLS_SSL_COMPRESS_NULL )
9667 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009668#endif
9669
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009670 switch( mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009671 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009672 case MBEDTLS_MODE_GCM:
9673 case MBEDTLS_MODE_CCM:
Hanno Becker5b559ac2018-08-03 09:40:07 +01009674 case MBEDTLS_MODE_CHACHAPOLY:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009675 case MBEDTLS_MODE_STREAM:
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009676 transform_expansion = transform->minlen;
9677 break;
9678
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009679 case MBEDTLS_MODE_CBC:
Hanno Becker5b559ac2018-08-03 09:40:07 +01009680
9681 block_size = mbedtls_cipher_get_block_size(
9682 &transform->cipher_ctx_enc );
9683
Hanno Becker3136ede2018-08-17 15:28:19 +01009684 /* Expansion due to the addition of the MAC. */
9685 transform_expansion += transform->maclen;
9686
9687 /* Expansion due to the addition of CBC padding;
9688 * Theoretically up to 256 bytes, but we never use
9689 * more than the block size of the underlying cipher. */
9690 transform_expansion += block_size;
9691
9692 /* For TLS 1.1 or higher, an explicit IV is added
9693 * after the record header. */
Hanno Becker5b559ac2018-08-03 09:40:07 +01009694#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
9695 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Hanno Becker3136ede2018-08-17 15:28:19 +01009696 transform_expansion += block_size;
Hanno Becker5b559ac2018-08-03 09:40:07 +01009697#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker3136ede2018-08-17 15:28:19 +01009698
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009699 break;
9700
9701 default:
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02009702 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009703 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009704 }
9705
Hanno Beckera0e20d02019-05-15 14:03:01 +01009706#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker6cbad552019-05-08 15:40:11 +01009707 if( transform->out_cid_len != 0 )
9708 transform_expansion += MBEDTLS_SSL_MAX_CID_EXPANSION;
Hanno Beckera0e20d02019-05-15 14:03:01 +01009709#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker6cbad552019-05-08 15:40:11 +01009710
Hanno Becker5903de42019-05-03 14:46:38 +01009711 return( (int)( out_hdr_len + transform_expansion ) );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009712}
9713
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009714#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9715size_t mbedtls_ssl_get_max_frag_len( const mbedtls_ssl_context *ssl )
9716{
9717 size_t max_len;
9718
9719 /*
9720 * Assume mfl_code is correct since it was checked when set
9721 */
Angus Grattond8213d02016-05-25 20:56:48 +10009722 max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009723
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02009724 /* Check if a smaller max length was negotiated */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009725 if( ssl->session_out != NULL &&
Angus Grattond8213d02016-05-25 20:56:48 +10009726 ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009727 {
Angus Grattond8213d02016-05-25 20:56:48 +10009728 max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009729 }
9730
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02009731 /* During a handshake, use the value being negotiated */
9732 if( ssl->session_negotiate != NULL &&
9733 ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
9734 {
9735 max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
9736 }
9737
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009738 return( max_len );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009739}
9740#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
9741
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009742#if defined(MBEDTLS_SSL_PROTO_DTLS)
9743static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl )
9744{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04009745 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
9746 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
9747 ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
9748 ssl->state == MBEDTLS_SSL_SERVER_HELLO ) )
9749 return ( 0 );
9750
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009751 if( ssl->handshake == NULL || ssl->handshake->mtu == 0 )
9752 return( ssl->mtu );
9753
9754 if( ssl->mtu == 0 )
9755 return( ssl->handshake->mtu );
9756
9757 return( ssl->mtu < ssl->handshake->mtu ?
9758 ssl->mtu : ssl->handshake->mtu );
9759}
9760#endif /* MBEDTLS_SSL_PROTO_DTLS */
9761
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009762int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
9763{
9764 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
9765
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02009766#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
9767 !defined(MBEDTLS_SSL_PROTO_DTLS)
9768 (void) ssl;
9769#endif
9770
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009771#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9772 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
9773
9774 if( max_len > mfl )
9775 max_len = mfl;
9776#endif
9777
9778#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009779 if( ssl_get_current_mtu( ssl ) != 0 )
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009780 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009781 const size_t mtu = ssl_get_current_mtu( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009782 const int ret = mbedtls_ssl_get_record_expansion( ssl );
9783 const size_t overhead = (size_t) ret;
9784
9785 if( ret < 0 )
9786 return( ret );
9787
9788 if( mtu <= overhead )
9789 {
9790 MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
9791 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
9792 }
9793
9794 if( max_len > mtu - overhead )
9795 max_len = mtu - overhead;
9796 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009797#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009798
Hanno Becker0defedb2018-08-10 12:35:02 +01009799#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
9800 !defined(MBEDTLS_SSL_PROTO_DTLS)
9801 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009802#endif
9803
9804 return( (int) max_len );
9805}
9806
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009807#if defined(MBEDTLS_X509_CRT_PARSE_C)
9808const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00009809{
9810 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009811 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00009812
Hanno Beckere6824572019-02-07 13:18:46 +00009813#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009814 return( ssl->session->peer_cert );
Hanno Beckere6824572019-02-07 13:18:46 +00009815#else
9816 return( NULL );
9817#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakkerb0550d92012-10-30 07:51:03 +00009818}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009819#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00009820
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009821#if defined(MBEDTLS_SSL_CLI_C)
Hanno Beckerf852b1c2019-02-05 11:42:30 +00009822int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl,
9823 mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009824{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009825 if( ssl == NULL ||
9826 dst == NULL ||
9827 ssl->session == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009828 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009829 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009830 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009831 }
9832
Hanno Becker52055ae2019-02-06 14:30:46 +00009833 return( mbedtls_ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009834}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009835#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009836
Manuel Pégourié-Gonnardb5e4e0a2019-05-20 11:12:28 +02009837const mbedtls_ssl_session *mbedtls_ssl_get_session_pointer( const mbedtls_ssl_context *ssl )
9838{
9839 if( ssl == NULL )
9840 return( NULL );
9841
9842 return( ssl->session );
9843}
9844
Paul Bakker5121ce52009-01-03 21:22:43 +00009845/*
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02009846 * Serialize a session in the following format:
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02009847 * (in the presentation language of TLS, RFC 8446 section 3)
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02009848 *
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02009849 * opaque session_struct[n]; // n = sizeof(mbedtls_ssl_session)
9850 * select (MBEDTLS_SSL_KEEP_PEER_CERTIFICATE) {
9851 * case enabled: opaque peer_cert<0..2^24-1>; // length 0 means no cert
9852 * case disabled: uint8_t peer_cert_digest_type;
9853 * opaque peer_cert_digest<0..2^8-1>;
9854 * }
9855 * opaque ticket<0..2^24-1>; // 0 means no ticket
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02009856 *
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02009857 * Only the peer's certificate is saved, not the whole chain.
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02009858 */
9859int mbedtls_ssl_session_save( const mbedtls_ssl_session *session,
9860 unsigned char *buf,
9861 size_t buf_len,
9862 size_t *olen )
9863{
9864 unsigned char *p = buf;
Manuel Pégourié-Gonnard26f982f2019-05-21 11:01:32 +02009865 size_t used = 0;
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02009866#if defined(MBEDTLS_X509_CRT_PARSE_C)
9867#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
9868 size_t cert_len;
9869#else
9870 size_t cert_digest_len;
9871#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
9872#endif /* MBEDTLS_X509_CRT_PARSE_C */
9873
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02009874 /*
9875 * Shallow copy of the session structure
9876 */
Manuel Pégourié-Gonnard26f982f2019-05-21 11:01:32 +02009877 used += sizeof( mbedtls_ssl_session );
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02009878
9879 /* This also copies the values of pointer fields in the
9880 * session to be serialized, but they'll be ignored when
9881 * loading the session through ssl_load_session(). */
Manuel Pégourié-Gonnard26f982f2019-05-21 11:01:32 +02009882 if( used <= buf_len )
9883 {
9884 memcpy( p, session, sizeof( mbedtls_ssl_session ) );
9885 p += sizeof( mbedtls_ssl_session );
9886 }
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02009887
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02009888 /*
9889 * Copy of the peer's end-entity certificate
9890 */
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02009891#if defined(MBEDTLS_X509_CRT_PARSE_C)
9892#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
9893 if( session->peer_cert == NULL )
9894 cert_len = 0;
9895 else
9896 cert_len = session->peer_cert->raw.len;
9897
Manuel Pégourié-Gonnard26f982f2019-05-21 11:01:32 +02009898 used += 3 + cert_len;
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02009899
Manuel Pégourié-Gonnard26f982f2019-05-21 11:01:32 +02009900 if( used <= buf_len )
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02009901 {
Manuel Pégourié-Gonnard26f982f2019-05-21 11:01:32 +02009902 *p++ = (unsigned char)( ( cert_len >> 16 ) & 0xFF );
9903 *p++ = (unsigned char)( ( cert_len >> 8 ) & 0xFF );
9904 *p++ = (unsigned char)( ( cert_len ) & 0xFF );
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02009905
Manuel Pégourié-Gonnard26f982f2019-05-21 11:01:32 +02009906 if( session->peer_cert != NULL )
9907 {
9908 memcpy( p, session->peer_cert->raw.p, cert_len );
9909 p += cert_len;
9910 }
9911 }
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02009912#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
9913 if( session->peer_cert_digest != NULL )
9914 cert_digest_len = 0;
9915 else
9916 cert_digest_len = session->peer_cert_digest_len;
9917
Manuel Pégourié-Gonnard26f982f2019-05-21 11:01:32 +02009918 used += 1 + cert_digest_len;
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02009919
Manuel Pégourié-Gonnard26f982f2019-05-21 11:01:32 +02009920 if( used <= buf_len )
9921 {
9922 *p++ = (unsigned char) cert_digest_len;
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02009923
Manuel Pégourié-Gonnard26f982f2019-05-21 11:01:32 +02009924 if( session->peer_cert_digest != NULL )
9925 memcpy( p, session->peer_cert_digest, cert_digest_len );
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02009926
Manuel Pégourié-Gonnard26f982f2019-05-21 11:01:32 +02009927 p += cert_digest_len;
9928 }
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02009929#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
9930#endif /* MBEDTLS_X509_CRT_PARSE_C */
9931
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02009932 /*
9933 * Copy of the session ticket if any
9934 */
9935#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard26f982f2019-05-21 11:01:32 +02009936 used += 3 + session->ticket_len;
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02009937
Manuel Pégourié-Gonnard26f982f2019-05-21 11:01:32 +02009938 if( used <= buf_len )
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02009939 {
Manuel Pégourié-Gonnard26f982f2019-05-21 11:01:32 +02009940 *p++ = (unsigned char)( ( session->ticket_len >> 16 ) & 0xFF );
9941 *p++ = (unsigned char)( ( session->ticket_len >> 8 ) & 0xFF );
9942 *p++ = (unsigned char)( ( session->ticket_len ) & 0xFF );
9943
9944 if( session->ticket != NULL )
9945 {
9946 memcpy( p, session->ticket, session->ticket_len );
9947 p += session->ticket_len;
9948 }
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02009949 }
9950#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
9951
9952 /* Done */
Manuel Pégourié-Gonnard26f982f2019-05-21 11:01:32 +02009953 *olen = used;
9954
9955 if( used > buf_len )
9956 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02009957
9958 return( 0 );
9959}
9960
9961/*
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02009962 * Unserialise session, see mbedtls_ssl_session_save().
9963 *
9964 * This internal version is wrapped by a public function that cleans up in
9965 * case of error.
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02009966 */
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02009967static int ssl_session_load( mbedtls_ssl_session *session,
9968 const unsigned char *buf,
9969 size_t len )
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02009970{
9971 const unsigned char *p = buf;
9972 const unsigned char * const end = buf + len;
9973#if defined(MBEDTLS_X509_CRT_PARSE_C)
9974#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
9975 size_t cert_len;
9976#else
9977 size_t cert_digest_len;
9978#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
9979#endif /* MBEDTLS_X509_CRT_PARSE_C */
9980
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02009981 /*
9982 * Shallow session structure
9983 */
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02009984 if( sizeof( mbedtls_ssl_session ) > (size_t)( end - p ) )
9985 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9986
9987 memcpy( session, p, sizeof( mbedtls_ssl_session ) );
9988 p += sizeof( mbedtls_ssl_session );
9989
9990 /* Non-NULL pointer fields of `session` are meaningless
9991 * and potentially harmful. Zeroize them for safety. */
9992#if defined(MBEDTLS_X509_CRT_PARSE_C)
9993#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
9994 session->peer_cert = NULL;
9995#else
9996 session->peer_cert_digest = NULL;
9997#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
9998#endif /* MBEDTLS_X509_CRT_PARSE_C */
9999#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
10000 session->ticket = NULL;
10001#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
10002
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +020010003 /*
10004 * Peer certificate
10005 */
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +020010006#if defined(MBEDTLS_X509_CRT_PARSE_C)
10007#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
10008 /* Deserialize CRT from the end of the ticket. */
10009 if( 3 > (size_t)( end - p ) )
10010 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10011
10012 cert_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2];
10013 p += 3;
10014
10015 if( cert_len != 0 )
10016 {
10017 int ret;
10018
10019 if( cert_len > (size_t)( end - p ) )
10020 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10021
10022 session->peer_cert = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
10023
10024 if( session->peer_cert == NULL )
10025 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
10026
10027 mbedtls_x509_crt_init( session->peer_cert );
10028
10029 if( ( ret = mbedtls_x509_crt_parse_der( session->peer_cert,
10030 p, cert_len ) ) != 0 )
10031 {
10032 mbedtls_x509_crt_free( session->peer_cert );
10033 mbedtls_free( session->peer_cert );
10034 session->peer_cert = NULL;
10035 return( ret );
10036 }
10037
10038 p += cert_len;
10039 }
10040#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
10041 /* Deserialize CRT digest from the end of the ticket. */
10042 if( 1 > (size_t)( end - p ) )
10043 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10044
10045 cert_digest_len = (size_t) p[0];
10046 p++;
10047
10048 if( cert_digest_len != 0 )
10049 {
10050 if( cert_digest_len > (size_t)( end - p ) ||
10051 cert_digest_len != session->peer_cert_digest_len )
10052 {
10053 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10054 }
10055
10056 session->peer_cert_digest = mbedtls_calloc( 1, cert_digest_len );
10057 if( session->peer_cert_digest == NULL )
10058 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
10059
10060 memcpy( session->peer_cert_digest, p, cert_digest_len );
10061 p += cert_digest_len;
10062 }
10063#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
10064#endif /* MBEDTLS_X509_CRT_PARSE_C */
10065
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +020010066 /*
10067 * Session ticket
10068 */
10069#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
10070 if( 3 > (size_t)( end - p ) )
10071 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10072
10073 session->ticket_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2];
10074 p += 3;
10075
10076 if( session->ticket_len != 0 )
10077 {
10078 if( session->ticket_len > (size_t)( end - p ) )
10079 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10080
10081 session->ticket = mbedtls_calloc( 1, session->ticket_len );
10082 if( session->ticket == NULL )
10083 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
10084
10085 memcpy( session->ticket, p, session->ticket_len );
10086 p += session->ticket_len;
10087 }
10088#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
10089
10090 /* Done, should have consumed entire buffer */
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +020010091 if( p != end )
10092 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10093
10094 return( 0 );
10095}
10096
10097/*
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +020010098 * Unserialise session: public wrapper for error cleaning
10099 */
10100int mbedtls_ssl_session_load( mbedtls_ssl_session *session,
10101 const unsigned char *buf,
10102 size_t len )
10103{
10104 int ret = ssl_session_load( session, buf, len );
10105
10106 if( ret != 0 )
10107 mbedtls_ssl_session_free( session );
10108
10109 return( ret );
10110}
10111
10112/*
Paul Bakker1961b702013-01-25 14:49:24 +010010113 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +000010114 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010115int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000010116{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010117 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +000010118
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010119 if( ssl == NULL || ssl->conf == NULL )
10120 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10121
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010122#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010123 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010124 ret = mbedtls_ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +000010125#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010126#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010127 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010128 ret = mbedtls_ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +000010129#endif
10130
Paul Bakker1961b702013-01-25 14:49:24 +010010131 return( ret );
10132}
10133
10134/*
10135 * Perform the SSL handshake
10136 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010137int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +010010138{
10139 int ret = 0;
10140
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010141 if( ssl == NULL || ssl->conf == NULL )
10142 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10143
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010144 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +010010145
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010146 while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker1961b702013-01-25 14:49:24 +010010147 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010148 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +010010149
10150 if( ret != 0 )
10151 break;
10152 }
10153
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010154 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010155
10156 return( ret );
10157}
10158
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010159#if defined(MBEDTLS_SSL_RENEGOTIATION)
10160#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000010161/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010162 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +000010163 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010164static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010165{
10166 int ret;
10167
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010168 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010169
10170 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010171 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
10172 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010173
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +020010174 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010175 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +020010176 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010177 return( ret );
10178 }
10179
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010180 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010181
10182 return( 0 );
10183}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010184#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010185
10186/*
10187 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010188 * - any side: calling mbedtls_ssl_renegotiate(),
10189 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
10190 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +020010191 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010192 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010193 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010194 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010195static int ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +000010196{
10197 int ret;
10198
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010199 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010200
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010201 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
10202 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +000010203
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +020010204 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
10205 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010206#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010207 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010208 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +020010209 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010210 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +020010211 ssl->handshake->out_msg_seq = 1;
10212 else
10213 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +020010214 }
10215#endif
10216
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010217 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
10218 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +000010219
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010220 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +000010221 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010222 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +000010223 return( ret );
10224 }
10225
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010226 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010227
10228 return( 0 );
10229}
10230
10231/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010232 * Renegotiate current connection on client,
10233 * or request renegotiation on server
10234 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010235int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010236{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010237 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010238
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010239 if( ssl == NULL || ssl->conf == NULL )
10240 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10241
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010242#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010243 /* On server, just send the request */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010244 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010245 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010246 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
10247 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010248
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010249 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +020010250
10251 /* Did we already try/start sending HelloRequest? */
10252 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010253 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +020010254
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010255 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010256 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010257#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010258
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010259#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010260 /*
10261 * On client, either start the renegotiation process or,
10262 * if already in progress, continue the handshake
10263 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010264 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010265 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010266 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
10267 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010268
10269 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
10270 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010271 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010272 return( ret );
10273 }
10274 }
10275 else
10276 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010277 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010278 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010279 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010280 return( ret );
10281 }
10282 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010283#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010284
Paul Bakker37ce0ff2013-10-31 14:32:04 +010010285 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010286}
10287
10288/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010289 * Check record counters and renegotiate if they're above the limit.
10290 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010291static int ssl_check_ctr_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010292{
Andres AG2196c7f2016-12-15 17:01:16 +000010293 size_t ep_len = ssl_ep_len( ssl );
10294 int in_ctr_cmp;
10295 int out_ctr_cmp;
10296
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010297 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ||
10298 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010299 ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010300 {
10301 return( 0 );
10302 }
10303
Andres AG2196c7f2016-12-15 17:01:16 +000010304 in_ctr_cmp = memcmp( ssl->in_ctr + ep_len,
10305 ssl->conf->renego_period + ep_len, 8 - ep_len );
Hanno Becker19859472018-08-06 09:40:20 +010010306 out_ctr_cmp = memcmp( ssl->cur_out_ctr + ep_len,
Andres AG2196c7f2016-12-15 17:01:16 +000010307 ssl->conf->renego_period + ep_len, 8 - ep_len );
10308
10309 if( in_ctr_cmp <= 0 && out_ctr_cmp <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010310 {
10311 return( 0 );
10312 }
10313
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +020010314 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010315 return( mbedtls_ssl_renegotiate( ssl ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010316}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010317#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +000010318
10319/*
10320 * Receive application data decrypted from the SSL layer
10321 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010322int mbedtls_ssl_read( mbedtls_ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +000010323{
Hanno Becker4a810fb2017-05-24 16:27:30 +010010324 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +000010325 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +000010326
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010327 if( ssl == NULL || ssl->conf == NULL )
10328 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10329
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010330 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010331
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010332#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010333 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +020010334 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010335 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +020010336 return( ret );
10337
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010338 if( ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010339 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010340 {
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +020010341 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010342 return( ret );
10343 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +020010344 }
10345#endif
10346
Hanno Becker4a810fb2017-05-24 16:27:30 +010010347 /*
10348 * Check if renegotiation is necessary and/or handshake is
10349 * in process. If yes, perform/continue, and fall through
10350 * if an unexpected packet is received while the client
10351 * is waiting for the ServerHello.
10352 *
10353 * (There is no equivalent to the last condition on
10354 * the server-side as it is not treated as within
10355 * a handshake while waiting for the ClientHello
10356 * after a renegotiation request.)
10357 */
10358
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010359#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +010010360 ret = ssl_check_ctr_renegotiate( ssl );
10361 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
10362 ret != 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010363 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010364 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010365 return( ret );
10366 }
10367#endif
10368
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010369 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +000010370 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010371 ret = mbedtls_ssl_handshake( ssl );
Hanno Becker4a810fb2017-05-24 16:27:30 +010010372 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
10373 ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010374 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010375 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010376 return( ret );
10377 }
10378 }
10379
Hanno Beckere41158b2017-10-23 13:30:32 +010010380 /* Loop as long as no application data record is available */
Hanno Becker90333da2017-10-10 11:27:13 +010010381 while( ssl->in_offt == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010382 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +020010383 /* Start timer if not already running */
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +020010384 if( ssl->f_get_timer != NULL &&
10385 ssl->f_get_timer( ssl->p_timer ) == -1 )
10386 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010387 ssl_set_timer( ssl, ssl->conf->read_timeout );
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +020010388 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +020010389
Hanno Becker327c93b2018-08-15 13:56:18 +010010390 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010391 {
Hanno Becker4a810fb2017-05-24 16:27:30 +010010392 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
10393 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +000010394
Hanno Becker4a810fb2017-05-24 16:27:30 +010010395 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
10396 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010397 }
10398
10399 if( ssl->in_msglen == 0 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010400 ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +000010401 {
10402 /*
10403 * OpenSSL sends empty messages to randomize the IV
10404 */
Hanno Becker327c93b2018-08-15 13:56:18 +010010405 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010406 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010407 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
Paul Bakker831a7552011-05-18 13:32:51 +000010408 return( 0 );
10409
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010410 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010411 return( ret );
10412 }
10413 }
10414
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010415 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +000010416 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010417 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010418
Hanno Becker4a810fb2017-05-24 16:27:30 +010010419 /*
10420 * - For client-side, expect SERVER_HELLO_REQUEST.
10421 * - For server-side, expect CLIENT_HELLO.
10422 * - Fail (TLS) or silently drop record (DTLS) in other cases.
10423 */
10424
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010425#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010426 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010427 ( ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST ||
Hanno Becker4a810fb2017-05-24 16:27:30 +010010428 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ) )
Paul Bakker48916f92012-09-16 19:57:18 +000010429 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010430 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010431
10432 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010433#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010434 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +010010435 {
10436 continue;
10437 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010438#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010439 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010440 }
Hanno Becker4a810fb2017-05-24 16:27:30 +010010441#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010442
Hanno Becker4a810fb2017-05-24 16:27:30 +010010443#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010444 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010445 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO )
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010446 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010447 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010448
10449 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010450#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010451 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +010010452 {
10453 continue;
10454 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010455#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010456 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker48916f92012-09-16 19:57:18 +000010457 }
Hanno Becker4a810fb2017-05-24 16:27:30 +010010458#endif /* MBEDTLS_SSL_SRV_C */
10459
Hanno Becker21df7f92017-10-17 11:03:26 +010010460#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +010010461 /* Determine whether renegotiation attempt should be accepted */
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +010010462 if( ! ( ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
10463 ( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
10464 ssl->conf->allow_legacy_renegotiation ==
10465 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) ) )
10466 {
10467 /*
10468 * Accept renegotiation request
10469 */
Paul Bakker48916f92012-09-16 19:57:18 +000010470
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +010010471 /* DTLS clients need to know renego is server-initiated */
10472#if defined(MBEDTLS_SSL_PROTO_DTLS)
10473 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
10474 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
10475 {
10476 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
10477 }
10478#endif
10479 ret = ssl_start_renegotiation( ssl );
10480 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
10481 ret != 0 )
10482 {
10483 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
10484 return( ret );
10485 }
10486 }
10487 else
Hanno Becker21df7f92017-10-17 11:03:26 +010010488#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +000010489 {
Hanno Becker4a810fb2017-05-24 16:27:30 +010010490 /*
10491 * Refuse renegotiation
10492 */
10493
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010494 MBEDTLS_SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010495
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010496#if defined(MBEDTLS_SSL_PROTO_SSL3)
10497 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +000010498 {
Gilles Peskine92e44262017-05-10 17:27:49 +020010499 /* SSLv3 does not have a "no_renegotiation" warning, so
10500 we send a fatal alert and abort the connection. */
10501 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
10502 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
10503 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +000010504 }
10505 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010506#endif /* MBEDTLS_SSL_PROTO_SSL3 */
10507#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
10508 defined(MBEDTLS_SSL_PROTO_TLS1_2)
10509 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +000010510 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010511 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
10512 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
10513 MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +000010514 {
10515 return( ret );
10516 }
Paul Bakker48916f92012-09-16 19:57:18 +000010517 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +020010518 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010519#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 ||
10520 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +020010521 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010522 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
10523 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +020010524 }
Paul Bakker48916f92012-09-16 19:57:18 +000010525 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020010526
Hanno Becker90333da2017-10-10 11:27:13 +010010527 /* At this point, we don't know whether the renegotiation has been
10528 * completed or not. The cases to consider are the following:
10529 * 1) The renegotiation is complete. In this case, no new record
10530 * has been read yet.
10531 * 2) The renegotiation is incomplete because the client received
10532 * an application data record while awaiting the ServerHello.
10533 * 3) The renegotiation is incomplete because the client received
10534 * a non-handshake, non-application data message while awaiting
10535 * the ServerHello.
10536 * In each of these case, looping will be the proper action:
10537 * - For 1), the next iteration will read a new record and check
10538 * if it's application data.
10539 * - For 2), the loop condition isn't satisfied as application data
10540 * is present, hence continue is the same as break
10541 * - For 3), the loop condition is satisfied and read_record
10542 * will re-deliver the message that was held back by the client
10543 * when expecting the ServerHello.
10544 */
10545 continue;
Paul Bakker48916f92012-09-16 19:57:18 +000010546 }
Hanno Becker21df7f92017-10-17 11:03:26 +010010547#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010548 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +010010549 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010550 if( ssl->conf->renego_max_records >= 0 )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +020010551 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010552 if( ++ssl->renego_records_seen > ssl->conf->renego_max_records )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020010553 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010554 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020010555 "but not honored by client" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010556 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020010557 }
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +020010558 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +010010559 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010560#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020010561
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010562 /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */
10563 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020010564 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010565 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +010010566 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020010567 }
10568
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010569 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +000010570 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010571 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
10572 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +000010573 }
10574
10575 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +020010576
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +020010577 /* We're going to return something now, cancel timer,
10578 * except if handshake (renegotiation) is in progress */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010579 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +020010580 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010581
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +020010582#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010583 /* If we requested renego but received AppData, resend HelloRequest.
10584 * Do it now, after setting in_offt, to avoid taking this branch
10585 * again if ssl_write_hello_request() returns WANT_WRITE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010586#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010587 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010588 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010589 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020010590 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010591 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010592 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010593 return( ret );
10594 }
10595 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010596#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker4a810fb2017-05-24 16:27:30 +010010597#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +000010598 }
10599
10600 n = ( len < ssl->in_msglen )
10601 ? len : ssl->in_msglen;
10602
10603 memcpy( buf, ssl->in_offt, n );
10604 ssl->in_msglen -= n;
10605
10606 if( ssl->in_msglen == 0 )
Hanno Becker4a810fb2017-05-24 16:27:30 +010010607 {
10608 /* all bytes consumed */
Paul Bakker5121ce52009-01-03 21:22:43 +000010609 ssl->in_offt = NULL;
Hanno Beckerbdf39052017-06-09 10:42:03 +010010610 ssl->keep_current_message = 0;
Hanno Becker4a810fb2017-05-24 16:27:30 +010010611 }
Paul Bakker5121ce52009-01-03 21:22:43 +000010612 else
Hanno Becker4a810fb2017-05-24 16:27:30 +010010613 {
Paul Bakker5121ce52009-01-03 21:22:43 +000010614 /* more data available */
10615 ssl->in_offt += n;
Hanno Becker4a810fb2017-05-24 16:27:30 +010010616 }
Paul Bakker5121ce52009-01-03 21:22:43 +000010617
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010618 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010619
Paul Bakker23986e52011-04-24 08:57:21 +000010620 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +000010621}
10622
10623/*
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010010624 * Send application data to be encrypted by the SSL layer, taking care of max
10625 * fragment length and buffer size.
10626 *
10627 * According to RFC 5246 Section 6.2.1:
10628 *
10629 * Zero-length fragments of Application data MAY be sent as they are
10630 * potentially useful as a traffic analysis countermeasure.
10631 *
10632 * Therefore, it is possible that the input message length is 0 and the
10633 * corresponding return code is 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +000010634 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010635static int ssl_write_real( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010636 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +000010637{
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +020010638 int ret = mbedtls_ssl_get_max_out_record_payload( ssl );
10639 const size_t max_len = (size_t) ret;
10640
10641 if( ret < 0 )
10642 {
10643 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_get_max_out_record_payload", ret );
10644 return( ret );
10645 }
10646
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010647 if( len > max_len )
10648 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010649#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010650 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010651 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010652 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) "
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010653 "maximum fragment length: %d > %d",
10654 len, max_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010655 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010656 }
10657 else
10658#endif
10659 len = max_len;
10660 }
Paul Bakker887bd502011-06-08 13:10:54 +000010661
Paul Bakker5121ce52009-01-03 21:22:43 +000010662 if( ssl->out_left != 0 )
10663 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010010664 /*
10665 * The user has previously tried to send the data and
10666 * MBEDTLS_ERR_SSL_WANT_WRITE or the message was only partially
10667 * written. In this case, we expect the high-level write function
10668 * (e.g. mbedtls_ssl_write()) to be called with the same parameters
10669 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010670 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010671 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010672 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010673 return( ret );
10674 }
10675 }
Paul Bakker887bd502011-06-08 13:10:54 +000010676 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +000010677 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010010678 /*
10679 * The user is trying to send a message the first time, so we need to
10680 * copy the data into the internal buffers and setup the data structure
10681 * to keep track of partial writes
10682 */
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010683 ssl->out_msglen = len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010684 ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA;
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010685 memcpy( ssl->out_msg, buf, len );
Paul Bakker887bd502011-06-08 13:10:54 +000010686
Hanno Becker67bc7c32018-08-06 11:33:50 +010010687 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker887bd502011-06-08 13:10:54 +000010688 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010689 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker887bd502011-06-08 13:10:54 +000010690 return( ret );
10691 }
Paul Bakker5121ce52009-01-03 21:22:43 +000010692 }
10693
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010694 return( (int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +000010695}
10696
10697/*
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010698 * Write application data, doing 1/n-1 splitting if necessary.
10699 *
10700 * With non-blocking I/O, ssl_write_real() may return WANT_WRITE,
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +010010701 * then the caller will call us again with the same arguments, so
Hanno Becker2b187c42017-09-18 14:58:11 +010010702 * remember whether we already did the split or not.
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010703 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010704#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010705static int ssl_write_split( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010706 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010707{
10708 int ret;
10709
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +010010710 if( ssl->conf->cbc_record_splitting ==
10711 MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED ||
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +010010712 len <= 1 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010713 ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_1 ||
10714 mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc )
10715 != MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010716 {
10717 return( ssl_write_real( ssl, buf, len ) );
10718 }
10719
10720 if( ssl->split_done == 0 )
10721 {
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010010722 if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010723 return( ret );
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010010724 ssl->split_done = 1;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010725 }
10726
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010010727 if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 )
10728 return( ret );
10729 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010730
10731 return( ret + 1 );
10732}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010733#endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010734
10735/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010736 * Write application data (public-facing wrapper)
10737 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010738int mbedtls_ssl_write( mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010739{
10740 int ret;
10741
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010742 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010743
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010744 if( ssl == NULL || ssl->conf == NULL )
10745 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10746
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010747#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010748 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
10749 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010750 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010751 return( ret );
10752 }
10753#endif
10754
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010755 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010756 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010757 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010758 {
Manuel Pégourié-Gonnard151dc772015-05-14 13:55:51 +020010759 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010760 return( ret );
10761 }
10762 }
10763
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010764#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010765 ret = ssl_write_split( ssl, buf, len );
10766#else
10767 ret = ssl_write_real( ssl, buf, len );
10768#endif
10769
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010770 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010771
10772 return( ret );
10773}
10774
10775/*
Paul Bakker5121ce52009-01-03 21:22:43 +000010776 * Notify the peer that the connection is being closed
10777 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010778int mbedtls_ssl_close_notify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000010779{
10780 int ret;
10781
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010782 if( ssl == NULL || ssl->conf == NULL )
10783 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10784
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010785 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010786
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +020010787 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010788 return( mbedtls_ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010789
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010790 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +000010791 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010792 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
10793 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
10794 MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010795 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010796 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010797 return( ret );
10798 }
10799 }
10800
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010801 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010802
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +020010803 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +000010804}
10805
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010806void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform )
Paul Bakker48916f92012-09-16 19:57:18 +000010807{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010808 if( transform == NULL )
10809 return;
10810
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010811#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +000010812 deflateEnd( &transform->ctx_deflate );
10813 inflateEnd( &transform->ctx_inflate );
10814#endif
10815
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010816 mbedtls_cipher_free( &transform->cipher_ctx_enc );
10817 mbedtls_cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +020010818
Hanno Beckerd56ed242018-01-03 15:32:51 +000010819#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010820 mbedtls_md_free( &transform->md_ctx_enc );
10821 mbedtls_md_free( &transform->md_ctx_dec );
Hanno Beckerd56ed242018-01-03 15:32:51 +000010822#endif
Paul Bakker61d113b2013-07-04 11:51:43 +020010823
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010824 mbedtls_platform_zeroize( transform, sizeof( mbedtls_ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010825}
10826
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010827#if defined(MBEDTLS_X509_CRT_PARSE_C)
10828static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010829{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010830 mbedtls_ssl_key_cert *cur = key_cert, *next;
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010831
10832 while( cur != NULL )
10833 {
10834 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010835 mbedtls_free( cur );
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010836 cur = next;
10837 }
10838}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010839#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010840
Hanno Becker0271f962018-08-16 13:23:47 +010010841#if defined(MBEDTLS_SSL_PROTO_DTLS)
10842
10843static void ssl_buffering_free( mbedtls_ssl_context *ssl )
10844{
10845 unsigned offset;
10846 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
10847
10848 if( hs == NULL )
10849 return;
10850
Hanno Becker283f5ef2018-08-24 09:34:47 +010010851 ssl_free_buffered_record( ssl );
10852
Hanno Becker0271f962018-08-16 13:23:47 +010010853 for( offset = 0; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
Hanno Beckere605b192018-08-21 15:59:07 +010010854 ssl_buffering_free_slot( ssl, offset );
10855}
10856
10857static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
10858 uint8_t slot )
10859{
10860 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
10861 mbedtls_ssl_hs_buffer * const hs_buf = &hs->buffering.hs[slot];
Hanno Beckerb309b922018-08-23 13:18:05 +010010862
10863 if( slot >= MBEDTLS_SSL_MAX_BUFFERED_HS )
10864 return;
10865
Hanno Beckere605b192018-08-21 15:59:07 +010010866 if( hs_buf->is_valid == 1 )
Hanno Becker0271f962018-08-16 13:23:47 +010010867 {
Hanno Beckere605b192018-08-21 15:59:07 +010010868 hs->buffering.total_bytes_buffered -= hs_buf->data_len;
Hanno Becker805f2e12018-10-12 16:31:41 +010010869 mbedtls_platform_zeroize( hs_buf->data, hs_buf->data_len );
Hanno Beckere605b192018-08-21 15:59:07 +010010870 mbedtls_free( hs_buf->data );
10871 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Hanno Becker0271f962018-08-16 13:23:47 +010010872 }
10873}
10874
10875#endif /* MBEDTLS_SSL_PROTO_DTLS */
10876
Gilles Peskine9b562d52018-04-25 20:32:43 +020010877void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +000010878{
Gilles Peskine9b562d52018-04-25 20:32:43 +020010879 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
10880
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010881 if( handshake == NULL )
10882 return;
10883
Gilles Peskinedf13d5c2018-04-25 20:39:48 +020010884#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
10885 if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
10886 {
Gilles Peskine8f97af72018-04-26 11:46:10 +020010887 ssl->conf->f_async_cancel( ssl );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +020010888 handshake->async_in_progress = 0;
10889 }
10890#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
10891
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010892#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
10893 defined(MBEDTLS_SSL_PROTO_TLS1_1)
10894 mbedtls_md5_free( &handshake->fin_md5 );
10895 mbedtls_sha1_free( &handshake->fin_sha1 );
10896#endif
10897#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
10898#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -050010899#if defined(MBEDTLS_USE_PSA_CRYPTO)
10900 psa_hash_abort( &handshake->fin_sha256_psa );
10901#else
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010902 mbedtls_sha256_free( &handshake->fin_sha256 );
10903#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -050010904#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010905#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -050010906#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -050010907 psa_hash_abort( &handshake->fin_sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -050010908#else
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010909 mbedtls_sha512_free( &handshake->fin_sha512 );
10910#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -050010911#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010912#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
10913
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010914#if defined(MBEDTLS_DHM_C)
10915 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +000010916#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010917#if defined(MBEDTLS_ECDH_C)
10918 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +020010919#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +020010920#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +020010921 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +020010922#if defined(MBEDTLS_SSL_CLI_C)
10923 mbedtls_free( handshake->ecjpake_cache );
10924 handshake->ecjpake_cache = NULL;
10925 handshake->ecjpake_cache_len = 0;
10926#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +020010927#endif
Paul Bakker61d113b2013-07-04 11:51:43 +020010928
Janos Follath4ae5c292016-02-10 11:27:43 +000010929#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
10930 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +020010931 /* explicit void pointer cast for buggy MS compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010932 mbedtls_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +020010933#endif
10934
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +010010935#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
10936 if( handshake->psk != NULL )
10937 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010938 mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +010010939 mbedtls_free( handshake->psk );
10940 }
10941#endif
10942
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010943#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
10944 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020010945 /*
10946 * Free only the linked list wrapper, not the keys themselves
10947 * since the belong to the SNI callback
10948 */
10949 if( handshake->sni_key_cert != NULL )
10950 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010951 mbedtls_ssl_key_cert *cur = handshake->sni_key_cert, *next;
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020010952
10953 while( cur != NULL )
10954 {
10955 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010956 mbedtls_free( cur );
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020010957 cur = next;
10958 }
10959 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010960#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010961
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +020010962#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +020010963 mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx );
Hanno Becker3dad3112019-02-05 17:19:52 +000010964 if( handshake->ecrs_peer_cert != NULL )
10965 {
10966 mbedtls_x509_crt_free( handshake->ecrs_peer_cert );
10967 mbedtls_free( handshake->ecrs_peer_cert );
10968 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +020010969#endif
10970
Hanno Becker75173122019-02-06 16:18:31 +000010971#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
10972 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
10973 mbedtls_pk_free( &handshake->peer_pubkey );
10974#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
10975
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010976#if defined(MBEDTLS_SSL_PROTO_DTLS)
10977 mbedtls_free( handshake->verify_cookie );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +020010978 ssl_flight_free( handshake->flight );
Hanno Becker0271f962018-08-16 13:23:47 +010010979 ssl_buffering_free( ssl );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +020010980#endif
10981
Hanno Becker4a63ed42019-01-08 11:39:35 +000010982#if defined(MBEDTLS_ECDH_C) && \
10983 defined(MBEDTLS_USE_PSA_CRYPTO)
10984 psa_destroy_key( handshake->ecdh_psa_privkey );
10985#endif /* MBEDTLS_ECDH_C && MBEDTLS_USE_PSA_CRYPTO */
10986
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010987 mbedtls_platform_zeroize( handshake,
10988 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010989}
10990
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010991void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +000010992{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010993 if( session == NULL )
10994 return;
10995
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010996#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker1294a0b2019-02-05 12:38:15 +000010997 ssl_clear_peer_cert( session );
Paul Bakkered27a042013-04-18 22:46:23 +020010998#endif
Paul Bakker0a597072012-09-25 21:55:46 +000010999
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +020011000#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011001 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +020011002#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +020011003
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050011004 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +000011005}
11006
Paul Bakker5121ce52009-01-03 21:22:43 +000011007/*
11008 * Free an SSL context
11009 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011010void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000011011{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020011012 if( ssl == NULL )
11013 return;
11014
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011015 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000011016
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +010011017 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000011018 {
Angus Grattond8213d02016-05-25 20:56:48 +100011019 mbedtls_platform_zeroize( ssl->out_buf, MBEDTLS_SSL_OUT_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011020 mbedtls_free( ssl->out_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +000011021 }
11022
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +010011023 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000011024 {
Angus Grattond8213d02016-05-25 20:56:48 +100011025 mbedtls_platform_zeroize( ssl->in_buf, MBEDTLS_SSL_IN_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011026 mbedtls_free( ssl->in_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +000011027 }
11028
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011029#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker16770332013-10-11 09:59:44 +020011030 if( ssl->compress_buf != NULL )
11031 {
Angus Grattond8213d02016-05-25 20:56:48 +100011032 mbedtls_platform_zeroize( ssl->compress_buf, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011033 mbedtls_free( ssl->compress_buf );
Paul Bakker16770332013-10-11 09:59:44 +020011034 }
11035#endif
11036
Paul Bakker48916f92012-09-16 19:57:18 +000011037 if( ssl->transform )
11038 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011039 mbedtls_ssl_transform_free( ssl->transform );
11040 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +000011041 }
11042
11043 if( ssl->handshake )
11044 {
Gilles Peskine9b562d52018-04-25 20:32:43 +020011045 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011046 mbedtls_ssl_transform_free( ssl->transform_negotiate );
11047 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +000011048
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011049 mbedtls_free( ssl->handshake );
11050 mbedtls_free( ssl->transform_negotiate );
11051 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +000011052 }
11053
Paul Bakkerc0463502013-02-14 11:19:38 +010011054 if( ssl->session )
11055 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011056 mbedtls_ssl_session_free( ssl->session );
11057 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +010011058 }
11059
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +020011060#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker66d5d072014-06-17 16:39:18 +020011061 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000011062 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050011063 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011064 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +000011065 }
Paul Bakker0be444a2013-08-27 21:55:01 +020011066#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000011067
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011068#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
11069 if( mbedtls_ssl_hw_record_finish != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +000011070 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011071 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_finish()" ) );
11072 mbedtls_ssl_hw_record_finish( ssl );
Paul Bakker05ef8352012-05-08 09:17:57 +000011073 }
11074#endif
11075
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +020011076#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011077 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +020011078#endif
11079
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011080 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +000011081
Paul Bakker86f04f42013-02-14 11:20:09 +010011082 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050011083 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000011084}
11085
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011086/*
11087 * Initialze mbedtls_ssl_config
11088 */
11089void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
11090{
11091 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
11092}
11093
Simon Butcherc97b6972015-12-27 23:48:17 +000011094#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010011095static int ssl_preset_default_hashes[] = {
11096#if defined(MBEDTLS_SHA512_C)
11097 MBEDTLS_MD_SHA512,
11098 MBEDTLS_MD_SHA384,
11099#endif
11100#if defined(MBEDTLS_SHA256_C)
11101 MBEDTLS_MD_SHA256,
11102 MBEDTLS_MD_SHA224,
11103#endif
Gilles Peskine5d2511c2017-05-12 13:16:40 +020011104#if defined(MBEDTLS_SHA1_C) && defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_KEY_EXCHANGE)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010011105 MBEDTLS_MD_SHA1,
11106#endif
11107 MBEDTLS_MD_NONE
11108};
Simon Butcherc97b6972015-12-27 23:48:17 +000011109#endif
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010011110
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011111static int ssl_preset_suiteb_ciphersuites[] = {
11112 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
11113 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
11114 0
11115};
11116
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020011117#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011118static int ssl_preset_suiteb_hashes[] = {
11119 MBEDTLS_MD_SHA256,
11120 MBEDTLS_MD_SHA384,
11121 MBEDTLS_MD_NONE
11122};
11123#endif
11124
11125#if defined(MBEDTLS_ECP_C)
11126static mbedtls_ecp_group_id ssl_preset_suiteb_curves[] = {
Jaeden Amerod4311042019-06-03 08:27:16 +010011127#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011128 MBEDTLS_ECP_DP_SECP256R1,
Jaeden Amerod4311042019-06-03 08:27:16 +010011129#endif
11130#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011131 MBEDTLS_ECP_DP_SECP384R1,
Jaeden Amerod4311042019-06-03 08:27:16 +010011132#endif
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011133 MBEDTLS_ECP_DP_NONE
11134};
11135#endif
11136
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011137/*
Tillmann Karras588ad502015-09-25 04:27:22 +020011138 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011139 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020011140int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011141 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011142{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +020011143#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011144 int ret;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +020011145#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011146
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +020011147 /* Use the functions here so that they are covered in tests,
11148 * but otherwise access member directly for efficiency */
11149 mbedtls_ssl_conf_endpoint( conf, endpoint );
11150 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011151
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011152 /*
11153 * Things that are common to all presets
11154 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020011155#if defined(MBEDTLS_SSL_CLI_C)
11156 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
11157 {
11158 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
11159#if defined(MBEDTLS_SSL_SESSION_TICKETS)
11160 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
11161#endif
11162 }
11163#endif
11164
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +020011165#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011166 conf->arc4_disabled = MBEDTLS_SSL_ARC4_DISABLED;
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +020011167#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011168
11169#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
11170 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
11171#endif
11172
11173#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
11174 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
11175#endif
11176
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +010011177#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
11178 conf->cbc_record_splitting = MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED;
11179#endif
11180
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +020011181#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011182 conf->f_cookie_write = ssl_cookie_write_dummy;
11183 conf->f_cookie_check = ssl_cookie_check_dummy;
11184#endif
11185
11186#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
11187 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
11188#endif
11189
Janos Follath088ce432017-04-10 12:42:31 +010011190#if defined(MBEDTLS_SSL_SRV_C)
11191 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
11192#endif
11193
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011194#if defined(MBEDTLS_SSL_PROTO_DTLS)
11195 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
11196 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
11197#endif
11198
11199#if defined(MBEDTLS_SSL_RENEGOTIATION)
11200 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Andres AG2196c7f2016-12-15 17:01:16 +000011201 memset( conf->renego_period, 0x00, 2 );
11202 memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011203#endif
11204
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011205#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
11206 if( endpoint == MBEDTLS_SSL_IS_SERVER )
11207 {
Hanno Becker00d0a682017-10-04 13:14:29 +010011208 const unsigned char dhm_p[] =
11209 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
11210 const unsigned char dhm_g[] =
11211 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
11212
Hanno Beckera90658f2017-10-04 15:29:08 +010011213 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
11214 dhm_p, sizeof( dhm_p ),
11215 dhm_g, sizeof( dhm_g ) ) ) != 0 )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011216 {
11217 return( ret );
11218 }
11219 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +020011220#endif
11221
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011222 /*
11223 * Preset-specific defaults
11224 */
11225 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011226 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011227 /*
11228 * NSA Suite B
11229 */
11230 case MBEDTLS_SSL_PRESET_SUITEB:
11231 conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
11232 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */
11233 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
11234 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
11235
11236 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
11237 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
11238 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
11239 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
11240 ssl_preset_suiteb_ciphersuites;
11241
11242#if defined(MBEDTLS_X509_CRT_PARSE_C)
11243 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011244#endif
11245
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020011246#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011247 conf->sig_hashes = ssl_preset_suiteb_hashes;
11248#endif
11249
11250#if defined(MBEDTLS_ECP_C)
11251 conf->curve_list = ssl_preset_suiteb_curves;
11252#endif
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +020011253 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011254
11255 /*
11256 * Default
11257 */
11258 default:
Ron Eldor5e9f14d2017-05-28 10:46:38 +030011259 conf->min_major_ver = ( MBEDTLS_SSL_MIN_MAJOR_VERSION >
11260 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION ) ?
11261 MBEDTLS_SSL_MIN_MAJOR_VERSION :
11262 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION;
11263 conf->min_minor_ver = ( MBEDTLS_SSL_MIN_MINOR_VERSION >
11264 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION ) ?
11265 MBEDTLS_SSL_MIN_MINOR_VERSION :
11266 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011267 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
11268 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
11269
11270#if defined(MBEDTLS_SSL_PROTO_DTLS)
11271 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
11272 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_2;
11273#endif
11274
11275 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
11276 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
11277 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
11278 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
11279 mbedtls_ssl_list_ciphersuites();
11280
11281#if defined(MBEDTLS_X509_CRT_PARSE_C)
11282 conf->cert_profile = &mbedtls_x509_crt_profile_default;
11283#endif
11284
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020011285#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010011286 conf->sig_hashes = ssl_preset_default_hashes;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011287#endif
11288
11289#if defined(MBEDTLS_ECP_C)
11290 conf->curve_list = mbedtls_ecp_grp_id_list();
11291#endif
11292
11293#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
11294 conf->dhm_min_bitlen = 1024;
11295#endif
11296 }
11297
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011298 return( 0 );
11299}
11300
11301/*
11302 * Free mbedtls_ssl_config
11303 */
11304void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
11305{
11306#if defined(MBEDTLS_DHM_C)
11307 mbedtls_mpi_free( &conf->dhm_P );
11308 mbedtls_mpi_free( &conf->dhm_G );
11309#endif
11310
11311#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
11312 if( conf->psk != NULL )
11313 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050011314 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011315 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +000011316 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011317 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +090011318 }
11319
11320 if( conf->psk_identity != NULL )
11321 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050011322 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +090011323 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +000011324 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011325 conf->psk_identity_len = 0;
11326 }
11327#endif
11328
11329#if defined(MBEDTLS_X509_CRT_PARSE_C)
11330 ssl_key_cert_free( conf->key_cert );
11331#endif
11332
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050011333 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011334}
11335
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020011336#if defined(MBEDTLS_PK_C) && \
11337 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020011338/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011339 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020011340 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011341unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020011342{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011343#if defined(MBEDTLS_RSA_C)
11344 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
11345 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020011346#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011347#if defined(MBEDTLS_ECDSA_C)
11348 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
11349 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020011350#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011351 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020011352}
11353
Hanno Becker7e5437a2017-04-28 17:15:26 +010011354unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
11355{
11356 switch( type ) {
11357 case MBEDTLS_PK_RSA:
11358 return( MBEDTLS_SSL_SIG_RSA );
11359 case MBEDTLS_PK_ECDSA:
11360 case MBEDTLS_PK_ECKEY:
11361 return( MBEDTLS_SSL_SIG_ECDSA );
11362 default:
11363 return( MBEDTLS_SSL_SIG_ANON );
11364 }
11365}
11366
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011367mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011368{
11369 switch( sig )
11370 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011371#if defined(MBEDTLS_RSA_C)
11372 case MBEDTLS_SSL_SIG_RSA:
11373 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011374#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011375#if defined(MBEDTLS_ECDSA_C)
11376 case MBEDTLS_SSL_SIG_ECDSA:
11377 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011378#endif
11379 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011380 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011381 }
11382}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020011383#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011384
Hanno Becker7e5437a2017-04-28 17:15:26 +010011385#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
11386 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
11387
11388/* Find an entry in a signature-hash set matching a given hash algorithm. */
11389mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set,
11390 mbedtls_pk_type_t sig_alg )
11391{
11392 switch( sig_alg )
11393 {
11394 case MBEDTLS_PK_RSA:
11395 return( set->rsa );
11396 case MBEDTLS_PK_ECDSA:
11397 return( set->ecdsa );
11398 default:
11399 return( MBEDTLS_MD_NONE );
11400 }
11401}
11402
11403/* Add a signature-hash-pair to a signature-hash set */
11404void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set,
11405 mbedtls_pk_type_t sig_alg,
11406 mbedtls_md_type_t md_alg )
11407{
11408 switch( sig_alg )
11409 {
11410 case MBEDTLS_PK_RSA:
11411 if( set->rsa == MBEDTLS_MD_NONE )
11412 set->rsa = md_alg;
11413 break;
11414
11415 case MBEDTLS_PK_ECDSA:
11416 if( set->ecdsa == MBEDTLS_MD_NONE )
11417 set->ecdsa = md_alg;
11418 break;
11419
11420 default:
11421 break;
11422 }
11423}
11424
11425/* Allow exactly one hash algorithm for each signature. */
11426void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set,
11427 mbedtls_md_type_t md_alg )
11428{
11429 set->rsa = md_alg;
11430 set->ecdsa = md_alg;
11431}
11432
11433#endif /* MBEDTLS_SSL_PROTO_TLS1_2) &&
11434 MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
11435
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020011436/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020011437 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020011438 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011439mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011440{
11441 switch( hash )
11442 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011443#if defined(MBEDTLS_MD5_C)
11444 case MBEDTLS_SSL_HASH_MD5:
11445 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011446#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011447#if defined(MBEDTLS_SHA1_C)
11448 case MBEDTLS_SSL_HASH_SHA1:
11449 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011450#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011451#if defined(MBEDTLS_SHA256_C)
11452 case MBEDTLS_SSL_HASH_SHA224:
11453 return( MBEDTLS_MD_SHA224 );
11454 case MBEDTLS_SSL_HASH_SHA256:
11455 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011456#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011457#if defined(MBEDTLS_SHA512_C)
11458 case MBEDTLS_SSL_HASH_SHA384:
11459 return( MBEDTLS_MD_SHA384 );
11460 case MBEDTLS_SSL_HASH_SHA512:
11461 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011462#endif
11463 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011464 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011465 }
11466}
11467
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020011468/*
11469 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
11470 */
11471unsigned char mbedtls_ssl_hash_from_md_alg( int md )
11472{
11473 switch( md )
11474 {
11475#if defined(MBEDTLS_MD5_C)
11476 case MBEDTLS_MD_MD5:
11477 return( MBEDTLS_SSL_HASH_MD5 );
11478#endif
11479#if defined(MBEDTLS_SHA1_C)
11480 case MBEDTLS_MD_SHA1:
11481 return( MBEDTLS_SSL_HASH_SHA1 );
11482#endif
11483#if defined(MBEDTLS_SHA256_C)
11484 case MBEDTLS_MD_SHA224:
11485 return( MBEDTLS_SSL_HASH_SHA224 );
11486 case MBEDTLS_MD_SHA256:
11487 return( MBEDTLS_SSL_HASH_SHA256 );
11488#endif
11489#if defined(MBEDTLS_SHA512_C)
11490 case MBEDTLS_MD_SHA384:
11491 return( MBEDTLS_SSL_HASH_SHA384 );
11492 case MBEDTLS_MD_SHA512:
11493 return( MBEDTLS_SSL_HASH_SHA512 );
11494#endif
11495 default:
11496 return( MBEDTLS_SSL_HASH_NONE );
11497 }
11498}
11499
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020011500#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011501/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020011502 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020011503 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011504 */
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020011505int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011506{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011507 const mbedtls_ecp_group_id *gid;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011508
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020011509 if( ssl->conf->curve_list == NULL )
11510 return( -1 );
11511
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020011512 for( gid = ssl->conf->curve_list; *gid != MBEDTLS_ECP_DP_NONE; gid++ )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011513 if( *gid == grp_id )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020011514 return( 0 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011515
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020011516 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011517}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020011518#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011519
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020011520#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020011521/*
11522 * Check if a hash proposed by the peer is in our list.
11523 * Return 0 if we're willing to use it, -1 otherwise.
11524 */
11525int mbedtls_ssl_check_sig_hash( const mbedtls_ssl_context *ssl,
11526 mbedtls_md_type_t md )
11527{
11528 const int *cur;
11529
11530 if( ssl->conf->sig_hashes == NULL )
11531 return( -1 );
11532
11533 for( cur = ssl->conf->sig_hashes; *cur != MBEDTLS_MD_NONE; cur++ )
11534 if( *cur == (int) md )
11535 return( 0 );
11536
11537 return( -1 );
11538}
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020011539#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020011540
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011541#if defined(MBEDTLS_X509_CRT_PARSE_C)
11542int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
11543 const mbedtls_ssl_ciphersuite_t *ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011544 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +020011545 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011546{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011547 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011548#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011549 int usage = 0;
11550#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011551#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011552 const char *ext_oid;
11553 size_t ext_len;
11554#endif
11555
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011556#if !defined(MBEDTLS_X509_CHECK_KEY_USAGE) && \
11557 !defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011558 ((void) cert);
11559 ((void) cert_endpoint);
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011560 ((void) flags);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011561#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011562
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011563#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
11564 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011565 {
11566 /* Server part of the key exchange */
11567 switch( ciphersuite->key_exchange )
11568 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011569 case MBEDTLS_KEY_EXCHANGE_RSA:
11570 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010011571 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011572 break;
11573
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011574 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
11575 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
11576 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
11577 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011578 break;
11579
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011580 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
11581 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010011582 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011583 break;
11584
11585 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011586 case MBEDTLS_KEY_EXCHANGE_NONE:
11587 case MBEDTLS_KEY_EXCHANGE_PSK:
11588 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
11589 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +020011590 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011591 usage = 0;
11592 }
11593 }
11594 else
11595 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011596 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
11597 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011598 }
11599
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011600 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011601 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010011602 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011603 ret = -1;
11604 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011605#else
11606 ((void) ciphersuite);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011607#endif /* MBEDTLS_X509_CHECK_KEY_USAGE */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011608
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011609#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
11610 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011611 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011612 ext_oid = MBEDTLS_OID_SERVER_AUTH;
11613 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011614 }
11615 else
11616 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011617 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
11618 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011619 }
11620
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011621 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011622 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010011623 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011624 ret = -1;
11625 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011626#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011627
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011628 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011629}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011630#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +020011631
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011632/*
11633 * Convert version numbers to/from wire format
11634 * and, for DTLS, to/from TLS equivalent.
11635 *
11636 * For TLS this is the identity.
Brian J Murray1903fb32016-11-06 04:45:15 -080011637 * For DTLS, use 1's complement (v -> 255 - v, and then map as follows:
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011638 * 1.0 <-> 3.2 (DTLS 1.0 is based on TLS 1.1)
11639 * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2)
11640 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011641void mbedtls_ssl_write_version( int major, int minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011642 unsigned char ver[2] )
11643{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011644#if defined(MBEDTLS_SSL_PROTO_DTLS)
11645 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011646 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011647 if( minor == MBEDTLS_SSL_MINOR_VERSION_2 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011648 --minor; /* DTLS 1.0 stored as TLS 1.1 internally */
11649
11650 ver[0] = (unsigned char)( 255 - ( major - 2 ) );
11651 ver[1] = (unsigned char)( 255 - ( minor - 1 ) );
11652 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010011653 else
11654#else
11655 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011656#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010011657 {
11658 ver[0] = (unsigned char) major;
11659 ver[1] = (unsigned char) minor;
11660 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011661}
11662
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011663void mbedtls_ssl_read_version( int *major, int *minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011664 const unsigned char ver[2] )
11665{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011666#if defined(MBEDTLS_SSL_PROTO_DTLS)
11667 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011668 {
11669 *major = 255 - ver[0] + 2;
11670 *minor = 255 - ver[1] + 1;
11671
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011672 if( *minor == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011673 ++*minor; /* DTLS 1.0 stored as TLS 1.1 internally */
11674 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010011675 else
11676#else
11677 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011678#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010011679 {
11680 *major = ver[0];
11681 *minor = ver[1];
11682 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011683}
11684
Simon Butcher99000142016-10-13 17:21:01 +010011685int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md )
11686{
11687#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
11688 if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 )
11689 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
11690
11691 switch( md )
11692 {
11693#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
11694#if defined(MBEDTLS_MD5_C)
11695 case MBEDTLS_SSL_HASH_MD5:
Janos Follath182013f2016-10-25 10:50:22 +010011696 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
Simon Butcher99000142016-10-13 17:21:01 +010011697#endif
11698#if defined(MBEDTLS_SHA1_C)
11699 case MBEDTLS_SSL_HASH_SHA1:
11700 ssl->handshake->calc_verify = ssl_calc_verify_tls;
11701 break;
11702#endif
11703#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
11704#if defined(MBEDTLS_SHA512_C)
11705 case MBEDTLS_SSL_HASH_SHA384:
11706 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
11707 break;
11708#endif
11709#if defined(MBEDTLS_SHA256_C)
11710 case MBEDTLS_SSL_HASH_SHA256:
11711 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
11712 break;
11713#endif
11714 default:
11715 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
11716 }
11717
11718 return 0;
11719#else /* !MBEDTLS_SSL_PROTO_TLS1_2 */
11720 (void) ssl;
11721 (void) md;
11722
11723 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
11724#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
11725}
11726
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011727#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
11728 defined(MBEDTLS_SSL_PROTO_TLS1_1)
11729int mbedtls_ssl_get_key_exchange_md_ssl_tls( mbedtls_ssl_context *ssl,
11730 unsigned char *output,
11731 unsigned char *data, size_t data_len )
11732{
11733 int ret = 0;
11734 mbedtls_md5_context mbedtls_md5;
11735 mbedtls_sha1_context mbedtls_sha1;
11736
11737 mbedtls_md5_init( &mbedtls_md5 );
11738 mbedtls_sha1_init( &mbedtls_sha1 );
11739
11740 /*
11741 * digitally-signed struct {
11742 * opaque md5_hash[16];
11743 * opaque sha_hash[20];
11744 * };
11745 *
11746 * md5_hash
11747 * MD5(ClientHello.random + ServerHello.random
11748 * + ServerParams);
11749 * sha_hash
11750 * SHA(ClientHello.random + ServerHello.random
11751 * + ServerParams);
11752 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011753 if( ( ret = mbedtls_md5_starts_ret( &mbedtls_md5 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011754 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011755 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011756 goto exit;
11757 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011758 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011759 ssl->handshake->randbytes, 64 ) ) != 0 )
11760 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011761 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011762 goto exit;
11763 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011764 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5, data, data_len ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011765 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011766 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011767 goto exit;
11768 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011769 if( ( ret = mbedtls_md5_finish_ret( &mbedtls_md5, output ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011770 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011771 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011772 goto exit;
11773 }
11774
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011775 if( ( ret = mbedtls_sha1_starts_ret( &mbedtls_sha1 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011776 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011777 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011778 goto exit;
11779 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011780 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011781 ssl->handshake->randbytes, 64 ) ) != 0 )
11782 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011783 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011784 goto exit;
11785 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011786 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1, data,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011787 data_len ) ) != 0 )
11788 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011789 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011790 goto exit;
11791 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011792 if( ( ret = mbedtls_sha1_finish_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011793 output + 16 ) ) != 0 )
11794 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011795 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011796 goto exit;
11797 }
11798
11799exit:
11800 mbedtls_md5_free( &mbedtls_md5 );
11801 mbedtls_sha1_free( &mbedtls_sha1 );
11802
11803 if( ret != 0 )
11804 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
11805 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
11806
11807 return( ret );
11808
11809}
11810#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
11811 MBEDTLS_SSL_PROTO_TLS1_1 */
11812
11813#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
11814 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011815
11816#if defined(MBEDTLS_USE_PSA_CRYPTO)
11817int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
11818 unsigned char *hash, size_t *hashlen,
11819 unsigned char *data, size_t data_len,
11820 mbedtls_md_type_t md_alg )
11821{
Andrzej Kurek814feff2019-01-14 04:35:19 -050011822 psa_status_t status;
Jaeden Amero34973232019-02-20 10:32:28 +000011823 psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT;
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011824 psa_algorithm_t hash_alg = mbedtls_psa_translate_md( md_alg );
11825
Hanno Becker4c8c7aa2019-04-10 09:25:41 +010011826 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform PSA-based computation of digest of ServerKeyExchange" ) );
Andrzej Kurek814feff2019-01-14 04:35:19 -050011827
11828 if( ( status = psa_hash_setup( &hash_operation,
11829 hash_alg ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011830 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050011831 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_setup", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011832 goto exit;
11833 }
11834
Andrzej Kurek814feff2019-01-14 04:35:19 -050011835 if( ( status = psa_hash_update( &hash_operation, ssl->handshake->randbytes,
11836 64 ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011837 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050011838 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011839 goto exit;
11840 }
11841
Andrzej Kurek814feff2019-01-14 04:35:19 -050011842 if( ( status = psa_hash_update( &hash_operation,
11843 data, data_len ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011844 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050011845 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011846 goto exit;
11847 }
11848
Andrzej Kurek814feff2019-01-14 04:35:19 -050011849 if( ( status = psa_hash_finish( &hash_operation, hash, MBEDTLS_MD_MAX_SIZE,
11850 hashlen ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011851 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050011852 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_finish", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011853 goto exit;
11854 }
11855
11856exit:
Andrzej Kurek814feff2019-01-14 04:35:19 -050011857 if( status != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011858 {
11859 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
11860 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Andrzej Kurek814feff2019-01-14 04:35:19 -050011861 switch( status )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011862 {
11863 case PSA_ERROR_NOT_SUPPORTED:
11864 return( MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE );
Andrzej Kurek814feff2019-01-14 04:35:19 -050011865 case PSA_ERROR_BAD_STATE: /* Intentional fallthrough */
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011866 case PSA_ERROR_BUFFER_TOO_SMALL:
11867 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
11868 case PSA_ERROR_INSUFFICIENT_MEMORY:
11869 return( MBEDTLS_ERR_MD_ALLOC_FAILED );
11870 default:
11871 return( MBEDTLS_ERR_MD_HW_ACCEL_FAILED );
11872 }
11873 }
11874 return( 0 );
11875}
11876
11877#else
11878
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011879int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
Gilles Peskineca1d7422018-04-24 11:53:22 +020011880 unsigned char *hash, size_t *hashlen,
11881 unsigned char *data, size_t data_len,
11882 mbedtls_md_type_t md_alg )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011883{
11884 int ret = 0;
11885 mbedtls_md_context_t ctx;
11886 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
Gilles Peskineca1d7422018-04-24 11:53:22 +020011887 *hashlen = mbedtls_md_get_size( md_info );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011888
Hanno Becker4c8c7aa2019-04-10 09:25:41 +010011889 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform mbedtls-based computation of digest of ServerKeyExchange" ) );
Andrzej Kurek814feff2019-01-14 04:35:19 -050011890
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011891 mbedtls_md_init( &ctx );
11892
11893 /*
11894 * digitally-signed struct {
11895 * opaque client_random[32];
11896 * opaque server_random[32];
11897 * ServerDHParams params;
11898 * };
11899 */
11900 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
11901 {
11902 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
11903 goto exit;
11904 }
11905 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
11906 {
11907 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
11908 goto exit;
11909 }
11910 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
11911 {
11912 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
11913 goto exit;
11914 }
11915 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
11916 {
11917 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
11918 goto exit;
11919 }
Gilles Peskineca1d7422018-04-24 11:53:22 +020011920 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011921 {
11922 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
11923 goto exit;
11924 }
11925
11926exit:
11927 mbedtls_md_free( &ctx );
11928
11929 if( ret != 0 )
11930 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
11931 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
11932
11933 return( ret );
11934}
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011935#endif /* MBEDTLS_USE_PSA_CRYPTO */
11936
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011937#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
11938 MBEDTLS_SSL_PROTO_TLS1_2 */
11939
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011940#endif /* MBEDTLS_SSL_TLS_C */