blob: 74cb756e70297d854e8a6308a2038a881e111122 [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é-Gonnard9b108c22019-05-06 13:32:17 +0200993 * - [in] session: used: ciphersuite, encrypt_then_mac, master, compression
994 * - [in] tls_prf: pointer to PRF to use for key derivation
995 * - [in] randbytes: buffer holding ServerHello.random + ClientHello.random
Manuel Pégourié-Gonnardc864f6a2019-05-06 13:48:22 +0200996 * - [in] minor_ver: SSL/TLS minor version
997 * - [in] endpoint: client or server
998 * - [in] ssl: optionally used for:
999 * - MBEDTLS_SSL_HW_RECORD_ACCEL: whole context
1000 * - MBEDTLS_SSL_EXPORT_KEYS: ssl->conf->{f,p}_export_keys
1001 * - MBEDTLS_DEBUG_C: ssl->conf->{f,p}_dbg
Manuel Pégourié-Gonnarde59ae232019-04-30 11:41:40 +02001002 */
Manuel Pégourié-Gonnardcba40d92019-05-06 12:55:40 +02001003static int ssl_populate_transform( mbedtls_ssl_transform *transform,
1004 const mbedtls_ssl_session *session,
Manuel Pégourié-Gonnard9b108c22019-05-06 13:32:17 +02001005 ssl_tls_prf_t tls_prf,
1006 const unsigned char randbytes[64],
Manuel Pégourié-Gonnardc864f6a2019-05-06 13:48:22 +02001007 int minor_ver,
1008 unsigned endpoint,
Manuel Pégourié-Gonnardcba40d92019-05-06 12:55:40 +02001009 const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00001010{
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001011 int ret = 0;
Hanno Beckercb1cc802018-11-17 22:27:38 +00001012#if defined(MBEDTLS_USE_PSA_CRYPTO)
1013 int psa_fallthrough;
1014#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker5121ce52009-01-03 21:22:43 +00001015 unsigned char keyblk[256];
1016 unsigned char *key1;
1017 unsigned char *key2;
Paul Bakker68884e32013-01-07 18:20:04 +01001018 unsigned char *mac_enc;
1019 unsigned char *mac_dec;
Hanno Becker81c7b182017-11-09 18:39:33 +00001020 size_t mac_key_len;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001021 size_t iv_copy_len;
Hanno Becker88aaf652017-12-27 08:17:40 +00001022 unsigned keylen;
Hanno Beckere694c3e2017-12-27 21:34:08 +00001023 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001024 const mbedtls_cipher_info_t *cipher_info;
1025 const mbedtls_md_info_t *md_info;
Paul Bakker68884e32013-01-07 18:20:04 +01001026
Manuel Pégourié-Gonnardc864f6a2019-05-06 13:48:22 +02001027#if !defined(MBEDTLS_SSL_HW_RECORD_ACCEL) && \
1028 !defined(MBEDTLS_SSL_EXPORT_KEYS) && \
1029 !defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnarda7505d12019-05-07 10:17:56 +02001030 ssl = NULL; /* make sure we don't use it except for those cases */
Manuel Pégourié-Gonnardc864f6a2019-05-06 13:48:22 +02001031 (void) ssl;
1032#endif
1033
Manuel Pégourié-Gonnard9b108c22019-05-06 13:32:17 +02001034 /* Copy info about negotiated version and extensions */
Jaeden Amero2de07f12019-06-05 13:32:08 +01001035#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) && \
1036 defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001037 transform->encrypt_then_mac = session->encrypt_then_mac;
1038#endif
Manuel Pégourié-Gonnardc864f6a2019-05-06 13:48:22 +02001039 transform->minor_ver = minor_ver;
Hanno Beckere694c3e2017-12-27 21:34:08 +00001040
Manuel Pégourié-Gonnard9b108c22019-05-06 13:32:17 +02001041 /*
1042 * Get various info structures
1043 */
1044 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( session->ciphersuite );
1045 if( ciphersuite_info == NULL )
1046 {
1047 MBEDTLS_SSL_DEBUG_MSG( 1, ( "ciphersuite info for %d not found",
1048 session->ciphersuite ) );
1049 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1050 }
1051
Hanno Beckere694c3e2017-12-27 21:34:08 +00001052 cipher_info = mbedtls_cipher_info_from_type( ciphersuite_info->cipher );
Paul Bakker68884e32013-01-07 18:20:04 +01001053 if( cipher_info == NULL )
1054 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001055 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %d not found",
Hanno Beckere694c3e2017-12-27 21:34:08 +00001056 ciphersuite_info->cipher ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001057 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +01001058 }
1059
Hanno Beckere694c3e2017-12-27 21:34:08 +00001060 md_info = mbedtls_md_info_from_type( ciphersuite_info->mac );
Paul Bakker68884e32013-01-07 18:20:04 +01001061 if( md_info == NULL )
1062 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001063 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %d not found",
Hanno Beckere694c3e2017-12-27 21:34:08 +00001064 ciphersuite_info->mac ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001065 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +01001066 }
1067
Hanno Beckera0e20d02019-05-15 14:03:01 +01001068#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker4bf74652019-04-26 16:22:27 +01001069 /* Copy own and peer's CID if the use of the CID
1070 * extension has been negotiated. */
1071 if( ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_ENABLED )
1072 {
1073 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Copy CIDs into SSL transform" ) );
Hanno Becker8a7f9722019-04-30 13:52:29 +01001074
Hanno Becker05154c32019-05-03 15:23:51 +01001075 transform->in_cid_len = ssl->own_cid_len;
Hanno Becker05154c32019-05-03 15:23:51 +01001076 memcpy( transform->in_cid, ssl->own_cid, ssl->own_cid_len );
Hanno Becker1c1f0462019-05-03 12:55:51 +01001077 MBEDTLS_SSL_DEBUG_BUF( 3, "Incoming CID", transform->in_cid,
Hanno Becker4bf74652019-04-26 16:22:27 +01001078 transform->in_cid_len );
Hanno Beckerd1f20352019-05-15 10:21:55 +01001079
1080 transform->out_cid_len = ssl->handshake->peer_cid_len;
1081 memcpy( transform->out_cid, ssl->handshake->peer_cid,
1082 ssl->handshake->peer_cid_len );
1083 MBEDTLS_SSL_DEBUG_BUF( 3, "Outgoing CID", transform->out_cid,
1084 transform->out_cid_len );
Hanno Becker4bf74652019-04-26 16:22:27 +01001085 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01001086#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker4bf74652019-04-26 16:22:27 +01001087
Paul Bakker5121ce52009-01-03 21:22:43 +00001088 /*
Manuel Pégourié-Gonnard9b108c22019-05-06 13:32:17 +02001089 * Compute key block using the PRF
Paul Bakker5121ce52009-01-03 21:22:43 +00001090 */
Manuel Pégourié-Gonnard9b108c22019-05-06 13:32:17 +02001091 ret = tls_prf( session->master, 48, "key expansion",
1092 randbytes, 64, keyblk, 256 );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +01001093 if( ret != 0 )
1094 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001095 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +01001096 return( ret );
1097 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001098
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001099 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
1100 mbedtls_ssl_get_ciphersuite_name( session->ciphersuite ) ) );
1101 MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", session->master, 48 );
Manuel Pégourié-Gonnard9b108c22019-05-06 13:32:17 +02001102 MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", randbytes, 64 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001103 MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001104
Paul Bakker5121ce52009-01-03 21:22:43 +00001105 /*
1106 * Determine the appropriate key, IV and MAC length.
1107 */
Paul Bakker68884e32013-01-07 18:20:04 +01001108
Hanno Becker88aaf652017-12-27 08:17:40 +00001109 keylen = cipher_info->key_bitlen / 8;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001110
Hanno Becker8031d062018-01-03 15:32:31 +00001111#if defined(MBEDTLS_GCM_C) || \
1112 defined(MBEDTLS_CCM_C) || \
1113 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001114 if( cipher_info->mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001115 cipher_info->mode == MBEDTLS_MODE_CCM ||
1116 cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakker5121ce52009-01-03 21:22:43 +00001117 {
Hanno Beckerf704bef2018-11-16 15:21:18 +00001118 size_t explicit_ivlen;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001119
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001120 transform->maclen = 0;
Hanno Becker81c7b182017-11-09 18:39:33 +00001121 mac_key_len = 0;
Hanno Beckere694c3e2017-12-27 21:34:08 +00001122 transform->taglen =
1123 ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001124
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001125 /* All modes haves 96-bit IVs;
1126 * GCM and CCM has 4 implicit and 8 explicit bytes
1127 * ChachaPoly has all 12 bytes implicit
1128 */
Paul Bakker68884e32013-01-07 18:20:04 +01001129 transform->ivlen = 12;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001130 if( cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
1131 transform->fixed_ivlen = 12;
1132 else
1133 transform->fixed_ivlen = 4;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001134
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001135 /* Minimum length of encrypted record */
1136 explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
Hanno Beckere694c3e2017-12-27 21:34:08 +00001137 transform->minlen = explicit_ivlen + transform->taglen;
Paul Bakker68884e32013-01-07 18:20:04 +01001138 }
1139 else
Hanno Becker8031d062018-01-03 15:32:31 +00001140#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
1141#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
1142 if( cipher_info->mode == MBEDTLS_MODE_STREAM ||
1143 cipher_info->mode == MBEDTLS_MODE_CBC )
Paul Bakker68884e32013-01-07 18:20:04 +01001144 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001145 /* Initialize HMAC contexts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001146 if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 ||
1147 ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 )
Paul Bakker68884e32013-01-07 18:20:04 +01001148 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001149 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001150 goto end;
Paul Bakker68884e32013-01-07 18:20:04 +01001151 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001152
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001153 /* Get MAC length */
Hanno Becker81c7b182017-11-09 18:39:33 +00001154 mac_key_len = mbedtls_md_get_size( md_info );
1155 transform->maclen = mac_key_len;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001156
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001157#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001158 /*
1159 * If HMAC is to be truncated, we shall keep the leftmost bytes,
1160 * (rfc 6066 page 13 or rfc 2104 section 4),
1161 * so we only need to adjust the length here.
1162 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001163 if( session->trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_ENABLED )
Hanno Beckere89353a2017-11-20 16:36:41 +00001164 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001165 transform->maclen = MBEDTLS_SSL_TRUNCATED_HMAC_LEN;
Hanno Beckere89353a2017-11-20 16:36:41 +00001166
1167#if defined(MBEDTLS_SSL_TRUNCATED_HMAC_COMPAT)
1168 /* Fall back to old, non-compliant version of the truncated
Hanno Becker563423f2017-11-21 17:20:17 +00001169 * HMAC implementation which also truncates the key
1170 * (Mbed TLS versions from 1.3 to 2.6.0) */
Hanno Beckere89353a2017-11-20 16:36:41 +00001171 mac_key_len = transform->maclen;
1172#endif
1173 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001174#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001175
1176 /* IV length */
Paul Bakker68884e32013-01-07 18:20:04 +01001177 transform->ivlen = cipher_info->iv_size;
Paul Bakker5121ce52009-01-03 21:22:43 +00001178
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001179 /* Minimum length */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001180 if( cipher_info->mode == MBEDTLS_MODE_STREAM )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001181 transform->minlen = transform->maclen;
1182 else
Paul Bakker68884e32013-01-07 18:20:04 +01001183 {
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001184 /*
1185 * GenericBlockCipher:
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +01001186 * 1. if EtM is in use: one block plus MAC
1187 * otherwise: * first multiple of blocklen greater than maclen
1188 * 2. IV except for SSL3 and TLS 1.0
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001189 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001190#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1191 if( session->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +01001192 {
1193 transform->minlen = transform->maclen
1194 + cipher_info->block_size;
1195 }
1196 else
1197#endif
1198 {
1199 transform->minlen = transform->maclen
1200 + cipher_info->block_size
1201 - transform->maclen % cipher_info->block_size;
1202 }
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001203
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001204#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Manuel Pégourié-Gonnardc864f6a2019-05-06 13:48:22 +02001205 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ||
1206 minor_ver == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001207 ; /* No need to adjust minlen */
Paul Bakker68884e32013-01-07 18:20:04 +01001208 else
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001209#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001210#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnardc864f6a2019-05-06 13:48:22 +02001211 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_2 ||
1212 minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001213 {
1214 transform->minlen += transform->ivlen;
1215 }
1216 else
1217#endif
1218 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001219 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001220 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1221 goto end;
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001222 }
Paul Bakker68884e32013-01-07 18:20:04 +01001223 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001224 }
Hanno Becker8031d062018-01-03 15:32:31 +00001225 else
1226#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
1227 {
1228 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1229 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1230 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001231
Hanno Becker88aaf652017-12-27 08:17:40 +00001232 MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %u, minlen: %u, ivlen: %u, maclen: %u",
1233 (unsigned) keylen,
1234 (unsigned) transform->minlen,
1235 (unsigned) transform->ivlen,
1236 (unsigned) transform->maclen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001237
1238 /*
1239 * Finally setup the cipher contexts, IVs and MAC secrets.
1240 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001241#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnardc864f6a2019-05-06 13:48:22 +02001242 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00001243 {
Hanno Becker81c7b182017-11-09 18:39:33 +00001244 key1 = keyblk + mac_key_len * 2;
Hanno Becker88aaf652017-12-27 08:17:40 +00001245 key2 = keyblk + mac_key_len * 2 + keylen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001246
Paul Bakker68884e32013-01-07 18:20:04 +01001247 mac_enc = keyblk;
Hanno Becker81c7b182017-11-09 18:39:33 +00001248 mac_dec = keyblk + mac_key_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001249
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001250 /*
1251 * This is not used in TLS v1.1.
1252 */
Paul Bakker48916f92012-09-16 19:57:18 +00001253 iv_copy_len = ( transform->fixed_ivlen ) ?
1254 transform->fixed_ivlen : transform->ivlen;
Hanno Becker88aaf652017-12-27 08:17:40 +00001255 memcpy( transform->iv_enc, key2 + keylen, iv_copy_len );
1256 memcpy( transform->iv_dec, key2 + keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +00001257 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001258 }
1259 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001260#endif /* MBEDTLS_SSL_CLI_C */
1261#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardc864f6a2019-05-06 13:48:22 +02001262 if( endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00001263 {
Hanno Becker88aaf652017-12-27 08:17:40 +00001264 key1 = keyblk + mac_key_len * 2 + keylen;
Hanno Becker81c7b182017-11-09 18:39:33 +00001265 key2 = keyblk + mac_key_len * 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00001266
Hanno Becker81c7b182017-11-09 18:39:33 +00001267 mac_enc = keyblk + mac_key_len;
Paul Bakker68884e32013-01-07 18:20:04 +01001268 mac_dec = keyblk;
Paul Bakker5121ce52009-01-03 21:22:43 +00001269
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001270 /*
1271 * This is not used in TLS v1.1.
1272 */
Paul Bakker48916f92012-09-16 19:57:18 +00001273 iv_copy_len = ( transform->fixed_ivlen ) ?
1274 transform->fixed_ivlen : transform->ivlen;
Hanno Becker88aaf652017-12-27 08:17:40 +00001275 memcpy( transform->iv_dec, key1 + keylen, iv_copy_len );
1276 memcpy( transform->iv_enc, key1 + keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +00001277 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001278 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001279 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001280#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001281 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001282 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001283 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1284 goto end;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001285 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001286
Hanno Beckerd56ed242018-01-03 15:32:51 +00001287#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001288#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardc864f6a2019-05-06 13:48:22 +02001289 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker68884e32013-01-07 18:20:04 +01001290 {
Hanno Beckerd56ed242018-01-03 15:32:51 +00001291 if( mac_key_len > sizeof( transform->mac_enc ) )
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001292 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001293 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001294 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1295 goto end;
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001296 }
1297
Hanno Becker81c7b182017-11-09 18:39:33 +00001298 memcpy( transform->mac_enc, mac_enc, mac_key_len );
1299 memcpy( transform->mac_dec, mac_dec, mac_key_len );
Paul Bakker68884e32013-01-07 18:20:04 +01001300 }
1301 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001302#endif /* MBEDTLS_SSL_PROTO_SSL3 */
1303#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1304 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnardc864f6a2019-05-06 13:48:22 +02001305 if( minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakker68884e32013-01-07 18:20:04 +01001306 {
Gilles Peskine039fd122018-03-19 19:06:08 +01001307 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
1308 For AEAD-based ciphersuites, there is nothing to do here. */
1309 if( mac_key_len != 0 )
1310 {
1311 mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, mac_key_len );
1312 mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, mac_key_len );
1313 }
Paul Bakker68884e32013-01-07 18:20:04 +01001314 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001315 else
1316#endif
Paul Bakker577e0062013-08-28 11:57:20 +02001317 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001318 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001319 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1320 goto end;
Paul Bakker577e0062013-08-28 11:57:20 +02001321 }
Hanno Beckerd56ed242018-01-03 15:32:51 +00001322#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker68884e32013-01-07 18:20:04 +01001323
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001324#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
1325 if( mbedtls_ssl_hw_record_init != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00001326 {
1327 int ret = 0;
1328
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001329 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_init()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00001330
Hanno Becker88aaf652017-12-27 08:17:40 +00001331 if( ( ret = mbedtls_ssl_hw_record_init( ssl, key1, key2, keylen,
Paul Bakker07eb38b2012-12-19 14:42:06 +01001332 transform->iv_enc, transform->iv_dec,
1333 iv_copy_len,
Paul Bakker68884e32013-01-07 18:20:04 +01001334 mac_enc, mac_dec,
Hanno Becker81c7b182017-11-09 18:39:33 +00001335 mac_key_len ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00001336 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001337 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_init", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001338 ret = MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
1339 goto end;
Paul Bakker05ef8352012-05-08 09:17:57 +00001340 }
1341 }
Hanno Beckerd56ed242018-01-03 15:32:51 +00001342#else
1343 ((void) mac_dec);
1344 ((void) mac_enc);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001345#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00001346
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001347#if defined(MBEDTLS_SSL_EXPORT_KEYS)
1348 if( ssl->conf->f_export_keys != NULL )
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001349 {
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001350 ssl->conf->f_export_keys( ssl->conf->p_export_keys,
1351 session->master, keyblk,
Hanno Becker88aaf652017-12-27 08:17:40 +00001352 mac_key_len, keylen,
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001353 iv_copy_len );
1354 }
Ron Eldorf5cc10d2019-05-07 18:33:40 +03001355
1356 if( ssl->conf->f_export_keys_ext != NULL )
1357 {
1358 ssl->conf->f_export_keys_ext( ssl->conf->p_export_keys,
1359 session->master, keyblk,
Ron Eldorb7fd64c2019-05-12 11:03:32 +03001360 mac_key_len, keylen,
Ron Eldor51d3ab52019-05-12 14:54:30 +03001361 iv_copy_len,
Manuel Pégourié-Gonnard344460c2019-07-25 13:17:38 +02001362 /* work around bug in exporter type */
Manuel Pégourié-Gonnard9b108c22019-05-06 13:32:17 +02001363 (unsigned char *) randbytes + 32,
1364 (unsigned char *) randbytes,
1365 tls_prf_get_type( tls_prf ) );
Ron Eldorf5cc10d2019-05-07 18:33:40 +03001366 }
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001367#endif
1368
Hanno Beckerf704bef2018-11-16 15:21:18 +00001369#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Beckercb1cc802018-11-17 22:27:38 +00001370
1371 /* Only use PSA-based ciphers for TLS-1.2.
1372 * That's relevant at least for TLS-1.0, where
1373 * we assume that mbedtls_cipher_crypt() updates
1374 * the structure field for the IV, which the PSA-based
1375 * implementation currently doesn't. */
1376#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1377 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001378 {
Hanno Beckercb1cc802018-11-17 22:27:38 +00001379 ret = mbedtls_cipher_setup_psa( &transform->cipher_ctx_enc,
Hanno Becker22bf1452019-04-05 11:21:08 +01001380 cipher_info, transform->taglen );
Hanno Beckercb1cc802018-11-17 22:27:38 +00001381 if( ret != 0 && ret != MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE )
1382 {
1383 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup_psa", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001384 goto end;
Hanno Beckercb1cc802018-11-17 22:27:38 +00001385 }
1386
1387 if( ret == 0 )
1388 {
Hanno Becker4c8c7aa2019-04-10 09:25:41 +01001389 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Successfully setup PSA-based encryption cipher context" ) );
Hanno Beckercb1cc802018-11-17 22:27:38 +00001390 psa_fallthrough = 0;
1391 }
1392 else
1393 {
1394 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to setup PSA-based cipher context for record encryption - fall through to default setup." ) );
1395 psa_fallthrough = 1;
1396 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001397 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001398 else
Hanno Beckercb1cc802018-11-17 22:27:38 +00001399 psa_fallthrough = 1;
1400#else
1401 psa_fallthrough = 1;
1402#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Beckerf704bef2018-11-16 15:21:18 +00001403
Hanno Beckercb1cc802018-11-17 22:27:38 +00001404 if( psa_fallthrough == 1 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001405#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001406 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001407 cipher_info ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001408 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001409 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001410 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001411 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001412
Hanno Beckerf704bef2018-11-16 15:21:18 +00001413#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Beckercb1cc802018-11-17 22:27:38 +00001414 /* Only use PSA-based ciphers for TLS-1.2.
1415 * That's relevant at least for TLS-1.0, where
1416 * we assume that mbedtls_cipher_crypt() updates
1417 * the structure field for the IV, which the PSA-based
1418 * implementation currently doesn't. */
1419#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1420 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001421 {
Hanno Beckercb1cc802018-11-17 22:27:38 +00001422 ret = mbedtls_cipher_setup_psa( &transform->cipher_ctx_dec,
Hanno Becker22bf1452019-04-05 11:21:08 +01001423 cipher_info, transform->taglen );
Hanno Beckercb1cc802018-11-17 22:27:38 +00001424 if( ret != 0 && ret != MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE )
1425 {
1426 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup_psa", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001427 goto end;
Hanno Beckercb1cc802018-11-17 22:27:38 +00001428 }
1429
1430 if( ret == 0 )
1431 {
Hanno Becker4c8c7aa2019-04-10 09:25:41 +01001432 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Successfully setup PSA-based decryption cipher context" ) );
Hanno Beckercb1cc802018-11-17 22:27:38 +00001433 psa_fallthrough = 0;
1434 }
1435 else
1436 {
1437 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to setup PSA-based cipher context for record decryption - fall through to default setup." ) );
1438 psa_fallthrough = 1;
1439 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001440 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001441 else
Hanno Beckercb1cc802018-11-17 22:27:38 +00001442 psa_fallthrough = 1;
1443#else
1444 psa_fallthrough = 1;
1445#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Beckerf704bef2018-11-16 15:21:18 +00001446
Hanno Beckercb1cc802018-11-17 22:27:38 +00001447 if( psa_fallthrough == 1 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001448#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001449 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001450 cipher_info ) ) != 0 )
1451 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001452 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001453 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001454 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001455
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001456 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001457 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001458 MBEDTLS_ENCRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001459 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001460 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001461 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001462 }
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001463
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001464 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001465 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001466 MBEDTLS_DECRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001467 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001468 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001469 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001470 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001471
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001472#if defined(MBEDTLS_CIPHER_MODE_CBC)
1473 if( cipher_info->mode == MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001474 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001475 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc,
1476 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001477 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001478 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001479 goto end;
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001480 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001481
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001482 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec,
1483 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001484 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001485 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001486 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001487 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001488 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001489#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001490
Paul Bakker5121ce52009-01-03 21:22:43 +00001491
Manuel Pégourié-Gonnardd73b47f2019-05-06 12:44:24 +02001492 /* Initialize Zlib contexts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001493#if defined(MBEDTLS_ZLIB_SUPPORT)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001494 if( session->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001495 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001496 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Initializing zlib states" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001497
Paul Bakker48916f92012-09-16 19:57:18 +00001498 memset( &transform->ctx_deflate, 0, sizeof( transform->ctx_deflate ) );
1499 memset( &transform->ctx_inflate, 0, sizeof( transform->ctx_inflate ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001500
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001501 if( deflateInit( &transform->ctx_deflate,
1502 Z_DEFAULT_COMPRESSION ) != Z_OK ||
Paul Bakker48916f92012-09-16 19:57:18 +00001503 inflateInit( &transform->ctx_inflate ) != Z_OK )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001504 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001505 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001506 ret = MBEDTLS_ERR_SSL_COMPRESSION_FAILED;
1507 goto end;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001508 }
1509 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001510#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00001511
Ron Eldore6992702019-05-07 18:27:13 +03001512end:
Ron Eldora9f9a732019-05-07 18:29:02 +03001513 mbedtls_platform_zeroize( keyblk, sizeof( keyblk ) );
Ron Eldore6992702019-05-07 18:27:13 +03001514 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001515}
1516
Manuel Pégourié-Gonnard8d2805c2019-04-30 12:08:59 +02001517/*
1518 * Set appropriate PRF function and other SSL / TLS / TLS1.2 functions
1519 *
1520 * Inputs:
1521 * - SSL/TLS minor version
1522 * - hash associated with the ciphersuite (only used by TLS 1.2)
1523 *
1524 * Ouputs:
1525 * - the tls_prf, calc_verify and calc_finished members of handshake structure
1526 */
1527static int ssl_set_handshake_prfs( mbedtls_ssl_handshake_params *handshake,
1528 int minor_ver,
1529 mbedtls_md_type_t hash )
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001530{
Manuel Pégourié-Gonnard8d2805c2019-04-30 12:08:59 +02001531#if !defined(MBEDTLS_SSL_PROTO_TLS1_2) || !defined(MBEDTLS_SHA512_C)
1532 (void) hash;
1533#endif
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001534
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001535#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnard8d2805c2019-04-30 12:08:59 +02001536 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001537 {
1538 handshake->tls_prf = ssl3_prf;
1539 handshake->calc_verify = ssl_calc_verify_ssl;
1540 handshake->calc_finished = ssl_calc_finished_ssl;
1541 }
1542 else
1543#endif
1544#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Manuel Pégourié-Gonnard8d2805c2019-04-30 12:08:59 +02001545 if( minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001546 {
1547 handshake->tls_prf = tls1_prf;
1548 handshake->calc_verify = ssl_calc_verify_tls;
1549 handshake->calc_finished = ssl_calc_finished_tls;
1550 }
1551 else
1552#endif
1553#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1554#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard8d2805c2019-04-30 12:08:59 +02001555 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
1556 hash == MBEDTLS_MD_SHA384 )
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001557 {
1558 handshake->tls_prf = tls_prf_sha384;
1559 handshake->calc_verify = ssl_calc_verify_tls_sha384;
1560 handshake->calc_finished = ssl_calc_finished_tls_sha384;
1561 }
1562 else
1563#endif
1564#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnard8d2805c2019-04-30 12:08:59 +02001565 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001566 {
1567 handshake->tls_prf = tls_prf_sha256;
1568 handshake->calc_verify = ssl_calc_verify_tls_sha256;
1569 handshake->calc_finished = ssl_calc_finished_tls_sha256;
1570 }
1571 else
1572#endif
1573#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
1574 {
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001575 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1576 }
1577
1578 return( 0 );
1579}
1580
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001581/*
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001582 * Compute master secret if needed
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001583 *
1584 * Parameters:
1585 * [in/out] handshake
1586 * [in] resume, premaster, extended_ms, calc_verify, tls_prf
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001587 * (PSA-PSK) ciphersuite_info, psk_opaque
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001588 * [out] premaster (cleared)
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001589 * [out] master
1590 * [in] ssl: optionally used for debugging, EMS and PSA-PSK
1591 * debug: conf->f_dbg, conf->p_dbg
1592 * EMS: passed to calc_verify (debug + (SSL3) session_negotiate)
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001593 * PSA-PSA: minor_ver, conf
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001594 */
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001595static int ssl_compute_master( mbedtls_ssl_handshake_params *handshake,
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001596 unsigned char *master,
Manuel Pégourié-Gonnard0d56aaa2019-05-03 09:58:33 +02001597 const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001598{
1599 int ret;
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001600
1601 /* cf. RFC 5246, Section 8.1:
1602 * "The master secret is always exactly 48 bytes in length." */
1603 size_t const master_secret_len = 48;
1604
1605#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
1606 unsigned char session_hash[48];
1607#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
1608
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001609 /* The label for the KDF used for key expansion.
1610 * This is either "master secret" or "extended master secret"
1611 * depending on whether the Extended Master Secret extension
1612 * is used. */
1613 char const *lbl = "master secret";
1614
1615 /* The salt for the KDF used for key expansion.
1616 * - If the Extended Master Secret extension is not used,
1617 * this is ClientHello.Random + ServerHello.Random
1618 * (see Sect. 8.1 in RFC 5246).
1619 * - If the Extended Master Secret extension is used,
1620 * this is the transcript of the handshake so far.
1621 * (see Sect. 4 in RFC 7627). */
1622 unsigned char const *salt = handshake->randbytes;
1623 size_t salt_len = 64;
1624
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001625#if !defined(MBEDTLS_DEBUG_C) && \
1626 !defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) && \
1627 !(defined(MBEDTLS_USE_PSA_CRYPTO) && \
1628 defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED))
Manuel Pégourié-Gonnarda7505d12019-05-07 10:17:56 +02001629 ssl = NULL; /* make sure we don't use it except for those cases */
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001630 (void) ssl;
1631#endif
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001632
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001633 if( handshake->resume != 0 )
1634 {
1635 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001636 return( 0 );
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001637 }
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001638
1639#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001640 if( handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED )
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001641 {
1642 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using extended master secret" ) );
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001643
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001644 lbl = "extended master secret";
1645 salt = session_hash;
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001646 handshake->calc_verify( ssl, session_hash, &salt_len );
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001647
1648 MBEDTLS_SSL_DEBUG_BUF( 3, "session hash", session_hash, salt_len );
1649 }
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001650#endif /* MBEDTLS_SSL_EXTENDED_MS_ENABLED */
1651
1652#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001653 defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
1654 if( handshake->ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK &&
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001655 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001656 ssl_use_opaque_psk( ssl ) == 1 )
1657 {
1658 /* Perform PSK-to-MS expansion in a single step. */
1659 psa_status_t status;
1660 psa_algorithm_t alg;
1661 psa_key_handle_t psk;
1662 psa_key_derivation_operation_t derivation =
1663 PSA_KEY_DERIVATION_OPERATION_INIT;
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001664 mbedtls_md_type_t hash_alg = handshake->ciphersuite_info->mac;
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001665
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001666 MBEDTLS_SSL_DEBUG_MSG( 2, ( "perform PSA-based PSK-to-MS expansion" ) );
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001667
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001668 psk = ssl->conf->psk_opaque;
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001669 if( handshake->psk_opaque != 0 )
1670 psk = handshake->psk_opaque;
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001671
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001672 if( hash_alg == MBEDTLS_MD_SHA384 )
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001673 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384);
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001674 else
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001675 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256);
1676
1677 status = psa_key_derivation( &derivation, psk, alg,
1678 salt, salt_len,
1679 (unsigned char const *) lbl,
1680 (size_t) strlen( lbl ),
1681 master_secret_len );
1682 if( status != PSA_SUCCESS )
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001683 {
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001684 psa_key_derivation_abort( &derivation );
1685 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001686 }
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001687
1688 status = psa_key_derivation_output_bytes( &derivation,
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001689 master,
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001690 master_secret_len );
1691 if( status != PSA_SUCCESS )
1692 {
1693 psa_key_derivation_abort( &derivation );
1694 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
1695 }
1696
1697 status = psa_key_derivation_abort( &derivation );
1698 if( status != PSA_SUCCESS )
1699 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
1700 }
1701 else
1702#endif
1703 {
1704 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
1705 lbl, salt, salt_len,
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001706 master,
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001707 master_secret_len );
1708 if( ret != 0 )
1709 {
1710 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
1711 return( ret );
1712 }
1713
1714 MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret",
1715 handshake->premaster,
1716 handshake->pmslen );
1717
1718 mbedtls_platform_zeroize( handshake->premaster,
1719 sizeof(handshake->premaster) );
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001720 }
1721
1722 return( 0 );
1723}
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001724
Manuel Pégourié-Gonnarde59ae232019-04-30 11:41:40 +02001725int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl )
1726{
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001727 int ret;
Manuel Pégourié-Gonnard8d2805c2019-04-30 12:08:59 +02001728 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
1729 ssl->handshake->ciphersuite_info;
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001730
Manuel Pégourié-Gonnard040a9512019-05-06 12:05:58 +02001731 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
1732
1733 /* Set PRF, calc_verify and calc_finished function pointers */
Manuel Pégourié-Gonnard8d2805c2019-04-30 12:08:59 +02001734 ret = ssl_set_handshake_prfs( ssl->handshake,
1735 ssl->minor_ver,
1736 ciphersuite_info->mac );
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001737 if( ret != 0 )
Manuel Pégourié-Gonnard8d2805c2019-04-30 12:08:59 +02001738 {
1739 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_set_handshake_prfs", ret );
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001740 return( ret );
Manuel Pégourié-Gonnard8d2805c2019-04-30 12:08:59 +02001741 }
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001742
Manuel Pégourié-Gonnard040a9512019-05-06 12:05:58 +02001743 /* Compute master secret if needed */
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001744 ret = ssl_compute_master( ssl->handshake,
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001745 ssl->session_negotiate->master,
1746 ssl );
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001747 if( ret != 0 )
1748 {
1749 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compute_master", ret );
1750 return( ret );
1751 }
1752
Manuel Pégourié-Gonnard040a9512019-05-06 12:05:58 +02001753 /* Swap the client and server random values:
1754 * - MS derivation wanted client+server (RFC 5246 8.1)
1755 * - key derivation wants server+client (RFC 5246 6.3) */
1756 {
1757 unsigned char tmp[64];
1758 memcpy( tmp, ssl->handshake->randbytes, 64 );
1759 memcpy( ssl->handshake->randbytes, tmp + 32, 32 );
1760 memcpy( ssl->handshake->randbytes + 32, tmp, 32 );
1761 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
1762 }
1763
1764 /* Populate transform structure */
Manuel Pégourié-Gonnardcba40d92019-05-06 12:55:40 +02001765 ret = ssl_populate_transform( ssl->transform_negotiate,
1766 ssl->session_negotiate,
Manuel Pégourié-Gonnard9b108c22019-05-06 13:32:17 +02001767 ssl->handshake->tls_prf,
1768 ssl->handshake->randbytes,
Manuel Pégourié-Gonnardc864f6a2019-05-06 13:48:22 +02001769 ssl->minor_ver,
1770 ssl->conf->endpoint,
Manuel Pégourié-Gonnardcba40d92019-05-06 12:55:40 +02001771 ssl );
Manuel Pégourié-Gonnard040a9512019-05-06 12:05:58 +02001772 if( ret != 0 )
1773 {
1774 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_populate_transform", ret );
1775 return( ret );
1776 }
1777
1778 /* We no longer need Server/ClientHello.random values */
1779 mbedtls_platform_zeroize( ssl->handshake->randbytes,
1780 sizeof( ssl->handshake->randbytes ) );
1781
Manuel Pégourié-Gonnardd73b47f2019-05-06 12:44:24 +02001782 /* Allocate compression buffer */
1783#if defined(MBEDTLS_ZLIB_SUPPORT)
1784 if( session->compression == MBEDTLS_SSL_COMPRESS_DEFLATE &&
1785 ssl->compress_buf == NULL )
1786 {
1787 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Allocating compression buffer" ) );
1788 ssl->compress_buf = mbedtls_calloc( 1, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
1789 if( ssl->compress_buf == NULL )
1790 {
1791 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
1792 MBEDTLS_SSL_COMPRESS_BUFFER_LEN ) );
1793 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
1794 }
1795 }
1796#endif
1797
Manuel Pégourié-Gonnard040a9512019-05-06 12:05:58 +02001798 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
1799
1800 return( 0 );
Manuel Pégourié-Gonnarde59ae232019-04-30 11:41:40 +02001801}
1802
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001803#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001804void ssl_calc_verify_ssl( const mbedtls_ssl_context *ssl,
1805 unsigned char hash[36],
1806 size_t *hlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001807{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001808 mbedtls_md5_context md5;
1809 mbedtls_sha1_context sha1;
Paul Bakker5121ce52009-01-03 21:22:43 +00001810 unsigned char pad_1[48];
1811 unsigned char pad_2[48];
1812
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001813 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001814
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001815 mbedtls_md5_init( &md5 );
1816 mbedtls_sha1_init( &sha1 );
1817
1818 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1819 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001820
Paul Bakker380da532012-04-18 16:10:25 +00001821 memset( pad_1, 0x36, 48 );
1822 memset( pad_2, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001823
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001824 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1825 mbedtls_md5_update_ret( &md5, pad_1, 48 );
1826 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001827
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001828 mbedtls_md5_starts_ret( &md5 );
1829 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1830 mbedtls_md5_update_ret( &md5, pad_2, 48 );
1831 mbedtls_md5_update_ret( &md5, hash, 16 );
1832 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001833
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001834 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1835 mbedtls_sha1_update_ret( &sha1, pad_1, 40 );
1836 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001837
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001838 mbedtls_sha1_starts_ret( &sha1 );
1839 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1840 mbedtls_sha1_update_ret( &sha1, pad_2, 40 );
1841 mbedtls_sha1_update_ret( &sha1, hash + 16, 20 );
1842 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001843
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001844 *hlen = 36;
1845
1846 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001847 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001848
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001849 mbedtls_md5_free( &md5 );
1850 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001851
Paul Bakker380da532012-04-18 16:10:25 +00001852 return;
1853}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001854#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker380da532012-04-18 16:10:25 +00001855
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001856#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001857void ssl_calc_verify_tls( const mbedtls_ssl_context *ssl,
1858 unsigned char hash[36],
1859 size_t *hlen )
Paul Bakker380da532012-04-18 16:10:25 +00001860{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001861 mbedtls_md5_context md5;
1862 mbedtls_sha1_context sha1;
Paul Bakker380da532012-04-18 16:10:25 +00001863
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001864 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001865
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001866 mbedtls_md5_init( &md5 );
1867 mbedtls_sha1_init( &sha1 );
1868
1869 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1870 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker380da532012-04-18 16:10:25 +00001871
Andrzej Kurekeb342242019-01-29 09:14:33 -05001872 mbedtls_md5_finish_ret( &md5, hash );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001873 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001874
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001875 *hlen = 36;
1876
1877 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001878 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001879
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001880 mbedtls_md5_free( &md5 );
1881 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001882
Paul Bakker380da532012-04-18 16:10:25 +00001883 return;
1884}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001885#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker380da532012-04-18 16:10:25 +00001886
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001887#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1888#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001889void ssl_calc_verify_tls_sha256( const mbedtls_ssl_context *ssl,
1890 unsigned char hash[32],
1891 size_t *hlen )
Paul Bakker380da532012-04-18 16:10:25 +00001892{
Andrzej Kurekeb342242019-01-29 09:14:33 -05001893#if defined(MBEDTLS_USE_PSA_CRYPTO)
1894 size_t hash_size;
1895 psa_status_t status;
1896 psa_hash_operation_t sha256_psa = psa_hash_operation_init();
1897
1898 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha256" ) );
1899 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
1900 if( status != PSA_SUCCESS )
1901 {
1902 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
1903 return;
1904 }
1905
1906 status = psa_hash_finish( &sha256_psa, hash, 32, &hash_size );
1907 if( status != PSA_SUCCESS )
1908 {
1909 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
1910 return;
1911 }
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001912
1913 *hlen = 32;
1914 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, *hlen );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001915 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
1916#else
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001917 mbedtls_sha256_context sha256;
Paul Bakker380da532012-04-18 16:10:25 +00001918
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001919 mbedtls_sha256_init( &sha256 );
1920
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001921 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001922
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001923 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001924 mbedtls_sha256_finish_ret( &sha256, hash );
Paul Bakker380da532012-04-18 16:10:25 +00001925
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001926 *hlen = 32;
1927
1928 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001929 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001930
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001931 mbedtls_sha256_free( &sha256 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001932#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker380da532012-04-18 16:10:25 +00001933 return;
1934}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001935#endif /* MBEDTLS_SHA256_C */
Paul Bakker380da532012-04-18 16:10:25 +00001936
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001937#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001938void ssl_calc_verify_tls_sha384( const mbedtls_ssl_context *ssl,
1939 unsigned char hash[48],
1940 size_t *hlen )
Paul Bakker380da532012-04-18 16:10:25 +00001941{
Andrzej Kurekeb342242019-01-29 09:14:33 -05001942#if defined(MBEDTLS_USE_PSA_CRYPTO)
1943 size_t hash_size;
1944 psa_status_t status;
Andrzej Kurek972fba52019-01-30 03:29:12 -05001945 psa_hash_operation_t sha384_psa = psa_hash_operation_init();
Andrzej Kurekeb342242019-01-29 09:14:33 -05001946
1947 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha384" ) );
Andrzej Kurek972fba52019-01-30 03:29:12 -05001948 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001949 if( status != PSA_SUCCESS )
1950 {
1951 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
1952 return;
1953 }
1954
Andrzej Kurek972fba52019-01-30 03:29:12 -05001955 status = psa_hash_finish( &sha384_psa, hash, 48, &hash_size );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001956 if( status != PSA_SUCCESS )
1957 {
1958 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
1959 return;
1960 }
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001961
1962 *hlen = 48;
1963 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, *hlen );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001964 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
1965#else
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001966 mbedtls_sha512_context sha512;
Paul Bakker380da532012-04-18 16:10:25 +00001967
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001968 mbedtls_sha512_init( &sha512 );
1969
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001970 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001971
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001972 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001973 mbedtls_sha512_finish_ret( &sha512, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001974
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001975 *hlen = 48;
1976
1977 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001978 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001979
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001980 mbedtls_sha512_free( &sha512 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001981#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker5121ce52009-01-03 21:22:43 +00001982 return;
1983}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001984#endif /* MBEDTLS_SHA512_C */
1985#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001986
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001987#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
1988int 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 +02001989{
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001990 unsigned char *p = ssl->handshake->premaster;
1991 unsigned char *end = p + sizeof( ssl->handshake->premaster );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001992 const unsigned char *psk = ssl->conf->psk;
1993 size_t psk_len = ssl->conf->psk_len;
1994
1995 /* If the psk callback was called, use its result */
1996 if( ssl->handshake->psk != NULL )
1997 {
1998 psk = ssl->handshake->psk;
1999 psk_len = ssl->handshake->psk_len;
2000 }
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002001
2002 /*
2003 * PMS = struct {
2004 * opaque other_secret<0..2^16-1>;
2005 * opaque psk<0..2^16-1>;
2006 * };
2007 * with "other_secret" depending on the particular key exchange
2008 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002009#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
2010 if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002011 {
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02002012 if( end - p < 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002013 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002014
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002015 *(p++) = (unsigned char)( psk_len >> 8 );
2016 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02002017
2018 if( end < p || (size_t)( end - p ) < psk_len )
2019 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2020
2021 memset( p, 0, psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002022 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002023 }
2024 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002025#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
2026#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
2027 if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002028 {
2029 /*
2030 * other_secret already set by the ClientKeyExchange message,
2031 * and is 48 bytes long
2032 */
Philippe Antoine747fd532018-05-30 09:13:21 +02002033 if( end - p < 2 )
2034 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2035
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002036 *p++ = 0;
2037 *p++ = 48;
2038 p += 48;
2039 }
2040 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002041#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
2042#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
2043 if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002044 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02002045 int ret;
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01002046 size_t len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002047
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02002048 /* Write length only when we know the actual value */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002049 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01002050 p + 2, end - ( p + 2 ), &len,
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01002051 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002052 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002053 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002054 return( ret );
2055 }
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02002056 *(p++) = (unsigned char)( len >> 8 );
2057 *(p++) = (unsigned char)( len );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002058 p += len;
2059
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002060 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002061 }
2062 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002063#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
2064#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
2065 if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_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é-Gonnardbd1ae242013-10-14 13:09:25 +02002068 size_t zlen;
2069
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002070 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
Paul Bakker66d5d072014-06-17 16:39:18 +02002071 p + 2, end - ( p + 2 ),
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01002072 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002073 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002074 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002075 return( ret );
2076 }
2077
2078 *(p++) = (unsigned char)( zlen >> 8 );
2079 *(p++) = (unsigned char)( zlen );
2080 p += zlen;
2081
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002082 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
2083 MBEDTLS_DEBUG_ECDH_Z );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002084 }
2085 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002086#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002087 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002088 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2089 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002090 }
2091
2092 /* opaque psk<0..2^16-1>; */
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02002093 if( end - p < 2 )
2094 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01002095
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002096 *(p++) = (unsigned char)( psk_len >> 8 );
2097 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02002098
2099 if( end < p || (size_t)( end - p ) < psk_len )
2100 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2101
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002102 memcpy( p, psk, psk_len );
2103 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002104
2105 ssl->handshake->pmslen = p - ssl->handshake->premaster;
2106
2107 return( 0 );
2108}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002109#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002110
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002111#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002112/*
2113 * SSLv3.0 MAC functions
2114 */
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01002115#define SSL_MAC_MAX_BYTES 20 /* MD-5 or SHA-1 */
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01002116static void ssl_mac( mbedtls_md_context_t *md_ctx,
2117 const unsigned char *secret,
2118 const unsigned char *buf, size_t len,
2119 const unsigned char *ctr, int type,
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01002120 unsigned char out[SSL_MAC_MAX_BYTES] )
Paul Bakker5121ce52009-01-03 21:22:43 +00002121{
2122 unsigned char header[11];
2123 unsigned char padding[48];
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02002124 int padlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002125 int md_size = mbedtls_md_get_size( md_ctx->md_info );
2126 int md_type = mbedtls_md_get_type( md_ctx->md_info );
Paul Bakker68884e32013-01-07 18:20:04 +01002127
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02002128 /* Only MD5 and SHA-1 supported */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002129 if( md_type == MBEDTLS_MD_MD5 )
Paul Bakker68884e32013-01-07 18:20:04 +01002130 padlen = 48;
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02002131 else
Paul Bakker68884e32013-01-07 18:20:04 +01002132 padlen = 40;
Paul Bakker5121ce52009-01-03 21:22:43 +00002133
2134 memcpy( header, ctr, 8 );
2135 header[ 8] = (unsigned char) type;
2136 header[ 9] = (unsigned char)( len >> 8 );
2137 header[10] = (unsigned char)( len );
2138
Paul Bakker68884e32013-01-07 18:20:04 +01002139 memset( padding, 0x36, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002140 mbedtls_md_starts( md_ctx );
2141 mbedtls_md_update( md_ctx, secret, md_size );
2142 mbedtls_md_update( md_ctx, padding, padlen );
2143 mbedtls_md_update( md_ctx, header, 11 );
2144 mbedtls_md_update( md_ctx, buf, len );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01002145 mbedtls_md_finish( md_ctx, out );
Paul Bakker5121ce52009-01-03 21:22:43 +00002146
Paul Bakker68884e32013-01-07 18:20:04 +01002147 memset( padding, 0x5C, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002148 mbedtls_md_starts( md_ctx );
2149 mbedtls_md_update( md_ctx, secret, md_size );
2150 mbedtls_md_update( md_ctx, padding, padlen );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01002151 mbedtls_md_update( md_ctx, out, md_size );
2152 mbedtls_md_finish( md_ctx, out );
Paul Bakker5f70b252012-09-13 14:23:06 +00002153}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002154#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +00002155
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002156/* The function below is only used in the Lucky 13 counter-measure in
Hanno Beckerb2ca87d2018-10-18 15:43:13 +01002157 * mbedtls_ssl_decrypt_buf(). These are the defines that guard the call site. */
Hanno Becker52344c22018-01-03 15:24:20 +00002158#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) && \
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002159 ( defined(MBEDTLS_SSL_PROTO_TLS1) || \
2160 defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2161 defined(MBEDTLS_SSL_PROTO_TLS1_2) )
2162/* This function makes sure every byte in the memory region is accessed
2163 * (in ascending addresses order) */
2164static void ssl_read_memory( unsigned char *p, size_t len )
2165{
2166 unsigned char acc = 0;
2167 volatile unsigned char force;
2168
2169 for( ; len != 0; p++, len-- )
2170 acc ^= *p;
2171
2172 force = acc;
2173 (void) force;
2174}
2175#endif /* SSL_SOME_MODES_USE_MAC && ( TLS1 || TLS1_1 || TLS1_2 ) */
2176
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01002177/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002178 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +02002179 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002180
Hanno Beckera0e20d02019-05-15 14:03:01 +01002181#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerd3f8c792019-05-20 15:06:12 +01002182/* This functions transforms a DTLS plaintext fragment and a record content
2183 * type into an instance of the DTLSInnerPlaintext structure:
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002184 *
2185 * struct {
2186 * opaque content[DTLSPlaintext.length];
2187 * ContentType real_type;
2188 * uint8 zeros[length_of_padding];
2189 * } DTLSInnerPlaintext;
2190 *
2191 * Input:
2192 * - `content`: The beginning of the buffer holding the
2193 * plaintext to be wrapped.
2194 * - `*content_size`: The length of the plaintext in Bytes.
2195 * - `max_len`: The number of Bytes available starting from
2196 * `content`. This must be `>= *content_size`.
2197 * - `rec_type`: The desired record content type.
2198 *
2199 * Output:
2200 * - `content`: The beginning of the resulting DTLSInnerPlaintext structure.
2201 * - `*content_size`: The length of the resulting DTLSInnerPlaintext structure.
2202 *
2203 * Returns:
2204 * - `0` on success.
2205 * - A negative error code if `max_len` didn't offer enough space
2206 * for the expansion.
2207 */
2208static int ssl_cid_build_inner_plaintext( unsigned char *content,
2209 size_t *content_size,
2210 size_t remaining,
2211 uint8_t rec_type )
2212{
2213 size_t len = *content_size;
Hanno Beckerb9ec44f2019-05-13 15:31:17 +01002214 size_t pad = ( MBEDTLS_SSL_CID_PADDING_GRANULARITY -
2215 ( len + 1 ) % MBEDTLS_SSL_CID_PADDING_GRANULARITY ) %
2216 MBEDTLS_SSL_CID_PADDING_GRANULARITY;
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002217
2218 /* Write real content type */
2219 if( remaining == 0 )
2220 return( -1 );
2221 content[ len ] = rec_type;
2222 len++;
2223 remaining--;
2224
2225 if( remaining < pad )
2226 return( -1 );
2227 memset( content + len, 0, pad );
2228 len += pad;
2229 remaining -= pad;
2230
2231 *content_size = len;
2232 return( 0 );
2233}
2234
Hanno Becker07dc97d2019-05-20 15:08:01 +01002235/* This function parses a DTLSInnerPlaintext structure.
2236 * See ssl_cid_build_inner_plaintext() for details. */
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002237static int ssl_cid_parse_inner_plaintext( unsigned char const *content,
2238 size_t *content_size,
2239 uint8_t *rec_type )
2240{
2241 size_t remaining = *content_size;
2242
2243 /* Determine length of padding by skipping zeroes from the back. */
2244 do
2245 {
2246 if( remaining == 0 )
2247 return( -1 );
2248 remaining--;
2249 } while( content[ remaining ] == 0 );
2250
2251 *content_size = remaining;
2252 *rec_type = content[ remaining ];
2253
2254 return( 0 );
2255}
Hanno Beckera0e20d02019-05-15 14:03:01 +01002256#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002257
Hanno Beckerd5aeab12019-05-20 14:50:53 +01002258/* `add_data` must have size 13 Bytes if the CID extension is disabled,
Hanno Beckerc4a190b2019-05-08 18:15:21 +01002259 * and 13 + 1 + CID-length Bytes if the CID extension is enabled. */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002260static void ssl_extract_add_data_from_record( unsigned char* add_data,
Hanno Beckercab87e62019-04-29 13:52:53 +01002261 size_t *add_data_len,
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002262 mbedtls_record *rec )
2263{
Hanno Beckerd5aeab12019-05-20 14:50:53 +01002264 /* Quoting RFC 5246 (TLS 1.2):
Hanno Beckercab87e62019-04-29 13:52:53 +01002265 *
2266 * additional_data = seq_num + TLSCompressed.type +
2267 * TLSCompressed.version + TLSCompressed.length;
2268 *
Hanno Beckerd5aeab12019-05-20 14:50:53 +01002269 * For the CID extension, this is extended as follows
2270 * (quoting draft-ietf-tls-dtls-connection-id-05,
2271 * https://tools.ietf.org/html/draft-ietf-tls-dtls-connection-id-05):
Hanno Beckercab87e62019-04-29 13:52:53 +01002272 *
2273 * additional_data = seq_num + DTLSPlaintext.type +
2274 * DTLSPlaintext.version +
Hanno Beckerd5aeab12019-05-20 14:50:53 +01002275 * cid +
2276 * cid_length +
Hanno Beckercab87e62019-04-29 13:52:53 +01002277 * length_of_DTLSInnerPlaintext;
2278 */
2279
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002280 memcpy( add_data, rec->ctr, sizeof( rec->ctr ) );
2281 add_data[8] = rec->type;
Hanno Beckeredb24f82019-05-20 15:01:46 +01002282 memcpy( add_data + 9, rec->ver, sizeof( rec->ver ) );
Hanno Beckercab87e62019-04-29 13:52:53 +01002283
Hanno Beckera0e20d02019-05-15 14:03:01 +01002284#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker95e4bbc2019-05-09 11:38:24 +01002285 if( rec->cid_len != 0 )
2286 {
2287 memcpy( add_data + 11, rec->cid, rec->cid_len );
2288 add_data[11 + rec->cid_len + 0] = rec->cid_len;
2289 add_data[11 + rec->cid_len + 1] = ( rec->data_len >> 8 ) & 0xFF;
2290 add_data[11 + rec->cid_len + 2] = ( rec->data_len >> 0 ) & 0xFF;
2291 *add_data_len = 13 + 1 + rec->cid_len;
2292 }
2293 else
Hanno Beckera0e20d02019-05-15 14:03:01 +01002294#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker95e4bbc2019-05-09 11:38:24 +01002295 {
2296 add_data[11 + 0] = ( rec->data_len >> 8 ) & 0xFF;
2297 add_data[11 + 1] = ( rec->data_len >> 0 ) & 0xFF;
2298 *add_data_len = 13;
2299 }
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002300}
2301
Hanno Beckera18d1322018-01-03 14:27:32 +00002302int mbedtls_ssl_encrypt_buf( mbedtls_ssl_context *ssl,
2303 mbedtls_ssl_transform *transform,
2304 mbedtls_record *rec,
2305 int (*f_rng)(void *, unsigned char *, size_t),
2306 void *p_rng )
Paul Bakker5121ce52009-01-03 21:22:43 +00002307{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002308 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002309 int auth_done = 0;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002310 unsigned char * data;
Hanno Becker92fb4fa2019-05-20 14:54:26 +01002311 unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_OUT_LEN_MAX ];
Hanno Beckercab87e62019-04-29 13:52:53 +01002312 size_t add_data_len;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002313 size_t post_avail;
2314
2315 /* The SSL context is only used for debugging purposes! */
Hanno Beckera18d1322018-01-03 14:27:32 +00002316#if !defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnarda7505d12019-05-07 10:17:56 +02002317 ssl = NULL; /* make sure we don't use it except for debug */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002318 ((void) ssl);
2319#endif
2320
2321 /* The PRNG is used for dynamic IV generation that's used
2322 * for CBC transformations in TLS 1.1 and TLS 1.2. */
2323#if !( defined(MBEDTLS_CIPHER_MODE_CBC) && \
2324 ( defined(MBEDTLS_AES_C) || \
2325 defined(MBEDTLS_ARIA_C) || \
2326 defined(MBEDTLS_CAMELLIA_C) ) && \
2327 ( defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2) ) )
2328 ((void) f_rng);
2329 ((void) p_rng);
2330#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002331
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002332 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002333
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002334 if( transform == NULL )
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002335 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002336 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no transform provided to encrypt_buf" ) );
2337 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2338 }
Hanno Becker43c24b82019-05-01 09:45:57 +01002339 if( rec == NULL
2340 || rec->buf == NULL
2341 || rec->buf_len < rec->data_offset
2342 || rec->buf_len - rec->data_offset < rec->data_len
Hanno Beckera0e20d02019-05-15 14:03:01 +01002343#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker43c24b82019-05-01 09:45:57 +01002344 || rec->cid_len != 0
2345#endif
2346 )
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002347 {
2348 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to encrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002349 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002350 }
2351
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002352 data = rec->buf + rec->data_offset;
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002353 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002354 MBEDTLS_SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002355 data, rec->data_len );
2356
2357 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc );
2358
2359 if( rec->data_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
2360 {
2361 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record content %u too large, maximum %d",
2362 (unsigned) rec->data_len,
2363 MBEDTLS_SSL_OUT_CONTENT_LEN ) );
2364 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2365 }
Manuel Pégourié-Gonnard60346be2014-11-21 11:38:37 +01002366
Hanno Beckera0e20d02019-05-15 14:03:01 +01002367#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckercab87e62019-04-29 13:52:53 +01002368 /*
2369 * Add CID information
2370 */
2371 rec->cid_len = transform->out_cid_len;
2372 memcpy( rec->cid, transform->out_cid, transform->out_cid_len );
2373 MBEDTLS_SSL_DEBUG_BUF( 3, "CID", rec->cid, rec->cid_len );
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002374
2375 if( rec->cid_len != 0 )
2376 {
2377 /*
Hanno Becker07dc97d2019-05-20 15:08:01 +01002378 * Wrap plaintext into DTLSInnerPlaintext structure.
2379 * See ssl_cid_build_inner_plaintext() for more information.
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002380 *
Hanno Becker07dc97d2019-05-20 15:08:01 +01002381 * Note that this changes `rec->data_len`, and hence
2382 * `post_avail` needs to be recalculated afterwards.
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002383 */
2384 if( ssl_cid_build_inner_plaintext( data,
2385 &rec->data_len,
2386 post_avail,
2387 rec->type ) != 0 )
2388 {
2389 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2390 }
2391
2392 rec->type = MBEDTLS_SSL_MSG_CID;
2393 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01002394#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckercab87e62019-04-29 13:52:53 +01002395
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002396 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
2397
Paul Bakker5121ce52009-01-03 21:22:43 +00002398 /*
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01002399 * Add MAC before if needed
Paul Bakker5121ce52009-01-03 21:22:43 +00002400 */
Hanno Becker52344c22018-01-03 15:24:20 +00002401#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002402 if( mode == MBEDTLS_MODE_STREAM ||
2403 ( mode == MBEDTLS_MODE_CBC
2404#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002405 && transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002406#endif
2407 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00002408 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002409 if( post_avail < transform->maclen )
2410 {
2411 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2412 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2413 }
2414
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002415#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002416 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002417 {
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01002418 unsigned char mac[SSL_MAC_MAX_BYTES];
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002419 ssl_mac( &transform->md_ctx_enc, transform->mac_enc,
2420 data, rec->data_len, rec->ctr, rec->type, mac );
2421 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002422 }
2423 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002424#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002425#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2426 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002427 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002428 {
Hanno Becker992b6872017-11-09 18:57:39 +00002429 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
2430
Hanno Beckercab87e62019-04-29 13:52:53 +01002431 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker992b6872017-11-09 18:57:39 +00002432
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002433 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
Hanno Beckercab87e62019-04-29 13:52:53 +01002434 add_data_len );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002435 mbedtls_md_hmac_update( &transform->md_ctx_enc,
2436 data, rec->data_len );
2437 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
2438 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
2439
2440 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002441 }
2442 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002443#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002444 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002445 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2446 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002447 }
2448
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002449 MBEDTLS_SSL_DEBUG_BUF( 4, "computed mac", data + rec->data_len,
2450 transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002451
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002452 rec->data_len += transform->maclen;
2453 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002454 auth_done++;
Paul Bakker577e0062013-08-28 11:57:20 +02002455 }
Hanno Becker52344c22018-01-03 15:24:20 +00002456#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00002457
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002458 /*
2459 * Encrypt
2460 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002461#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
2462 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00002463 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002464 int ret;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002465 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002466 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002467 "including %d bytes of padding",
2468 rec->data_len, 0 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002469
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002470 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
2471 transform->iv_enc, transform->ivlen,
2472 data, rec->data_len,
2473 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002474 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002475 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002476 return( ret );
2477 }
2478
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002479 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002480 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002481 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2482 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002483 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002484 }
Paul Bakker68884e32013-01-07 18:20:04 +01002485 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002486#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002487
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002488#if defined(MBEDTLS_GCM_C) || \
2489 defined(MBEDTLS_CCM_C) || \
2490 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002491 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002492 mode == MBEDTLS_MODE_CCM ||
2493 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002494 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02002495 int ret;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002496 unsigned char iv[12];
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002497 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002498
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002499 /* Check that there's space for both the authentication tag
2500 * and the explicit IV before and after the record content. */
2501 if( post_avail < transform->taglen ||
2502 rec->data_offset < explicit_iv_len )
2503 {
2504 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2505 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2506 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00002507
Paul Bakker68884e32013-01-07 18:20:04 +01002508 /*
2509 * Generate IV
2510 */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002511 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
2512 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002513 /* GCM and CCM: fixed || explicit (=seqnum) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002514 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002515 memcpy( iv + transform->fixed_ivlen, rec->ctr,
2516 explicit_iv_len );
2517 /* Prefix record content with explicit IV. */
2518 memcpy( data - explicit_iv_len, rec->ctr, explicit_iv_len );
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002519 }
2520 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
2521 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002522 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002523 unsigned char i;
2524
2525 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
2526
2527 for( i = 0; i < 8; i++ )
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002528 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002529 }
2530 else
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01002531 {
2532 /* Reminder if we ever add an AEAD mode with a different size */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002533 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2534 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01002535 }
2536
Hanno Beckercab87e62019-04-29 13:52:53 +01002537 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker1f10d762019-04-26 13:34:37 +01002538
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002539 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (internal)",
2540 iv, transform->ivlen );
2541 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (transmitted)",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002542 data - explicit_iv_len, explicit_iv_len );
2543 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Hanno Beckercab87e62019-04-29 13:52:53 +01002544 add_data, add_data_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002545 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002546 "including 0 bytes of padding",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002547 rec->data_len ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00002548
Paul Bakker68884e32013-01-07 18:20:04 +01002549 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002550 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002551 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002552
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002553 if( ( ret = mbedtls_cipher_auth_encrypt( &transform->cipher_ctx_enc,
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002554 iv, transform->ivlen,
Hanno Beckercab87e62019-04-29 13:52:53 +01002555 add_data, add_data_len, /* add data */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002556 data, rec->data_len, /* source */
2557 data, &rec->data_len, /* destination */
2558 data + rec->data_len, transform->taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002559 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002560 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_encrypt", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002561 return( ret );
2562 }
2563
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002564 MBEDTLS_SSL_DEBUG_BUF( 4, "after encrypt: tag",
2565 data + rec->data_len, transform->taglen );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002566
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002567 rec->data_len += transform->taglen + explicit_iv_len;
2568 rec->data_offset -= explicit_iv_len;
2569 post_avail -= transform->taglen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002570 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002571 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002572 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002573#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2574#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002575 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002576 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00002577 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002578 int ret;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002579 size_t padlen, i;
2580 size_t olen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002581
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002582 /* Currently we're always using minimal padding
2583 * (up to 255 bytes would be allowed). */
2584 padlen = transform->ivlen - ( rec->data_len + 1 ) % transform->ivlen;
2585 if( padlen == transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002586 padlen = 0;
2587
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002588 /* Check there's enough space in the buffer for the padding. */
2589 if( post_avail < padlen + 1 )
2590 {
2591 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2592 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2593 }
2594
Paul Bakker5121ce52009-01-03 21:22:43 +00002595 for( i = 0; i <= padlen; i++ )
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002596 data[rec->data_len + i] = (unsigned char) padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002597
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002598 rec->data_len += padlen + 1;
2599 post_avail -= padlen + 1;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002600
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002601#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002602 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00002603 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
2604 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002605 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002606 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002607 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002608 if( f_rng == NULL )
2609 {
2610 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No PRNG provided to encrypt_record routine" ) );
2611 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2612 }
2613
2614 if( rec->data_offset < transform->ivlen )
2615 {
2616 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2617 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2618 }
2619
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002620 /*
2621 * Generate IV
2622 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002623 ret = f_rng( p_rng, transform->iv_enc, transform->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00002624 if( ret != 0 )
2625 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002626
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002627 memcpy( data - transform->ivlen, transform->iv_enc,
2628 transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002629
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002630 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002631#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002632
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002633 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002634 "including %d bytes of IV and %d bytes of padding",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002635 rec->data_len, transform->ivlen,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002636 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002637
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002638 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
2639 transform->iv_enc,
2640 transform->ivlen,
2641 data, rec->data_len,
2642 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002643 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002644 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002645 return( ret );
2646 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002647
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002648 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02002649 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002650 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2651 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002652 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002653
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002654#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002655 if( transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02002656 {
2657 /*
2658 * Save IV in SSL3 and TLS1
2659 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002660 memcpy( transform->iv_enc, transform->cipher_ctx_enc.iv,
2661 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002662 }
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002663 else
Paul Bakkercca5b812013-08-31 17:40:26 +02002664#endif
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002665 {
2666 data -= transform->ivlen;
2667 rec->data_offset -= transform->ivlen;
2668 rec->data_len += transform->ivlen;
2669 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002670
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002671#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002672 if( auth_done == 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002673 {
Hanno Becker3d8c9072018-01-05 16:24:22 +00002674 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
2675
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002676 /*
2677 * MAC(MAC_write_key, seq_num +
2678 * TLSCipherText.type +
2679 * TLSCipherText.version +
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002680 * length_of( (IV +) ENC(...) ) +
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002681 * IV + // except for TLS 1.0
2682 * ENC(content + padding + padding_length));
2683 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002684
2685 if( post_avail < transform->maclen)
2686 {
2687 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2688 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2689 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002690
Hanno Beckercab87e62019-04-29 13:52:53 +01002691 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker1f10d762019-04-26 13:34:37 +01002692
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002693 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002694 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
Hanno Beckercab87e62019-04-29 13:52:53 +01002695 add_data_len );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002696
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002697 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
Hanno Beckercab87e62019-04-29 13:52:53 +01002698 add_data_len );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002699 mbedtls_md_hmac_update( &transform->md_ctx_enc,
2700 data, rec->data_len );
2701 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
2702 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002703
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002704 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002705
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002706 rec->data_len += transform->maclen;
2707 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002708 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002709 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002710#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00002711 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002712 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002713#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002714 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002715 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002716 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2717 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002718 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002719
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002720 /* Make extra sure authentication was performed, exactly once */
2721 if( auth_done != 1 )
2722 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002723 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2724 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002725 }
2726
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002727 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002728
2729 return( 0 );
2730}
2731
Hanno Becker605949f2019-07-12 08:23:59 +01002732int mbedtls_ssl_decrypt_buf( mbedtls_ssl_context const *ssl,
Hanno Beckera18d1322018-01-03 14:27:32 +00002733 mbedtls_ssl_transform *transform,
2734 mbedtls_record *rec )
Paul Bakker5121ce52009-01-03 21:22:43 +00002735{
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002736 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002737 mbedtls_cipher_mode_t mode;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002738 int ret, auth_done = 0;
Hanno Becker52344c22018-01-03 15:24:20 +00002739#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Paul Bakker1e5369c2013-12-19 16:40:57 +01002740 size_t padlen = 0, correct = 1;
2741#endif
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002742 unsigned char* data;
Hanno Becker92fb4fa2019-05-20 14:54:26 +01002743 unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_IN_LEN_MAX ];
Hanno Beckercab87e62019-04-29 13:52:53 +01002744 size_t add_data_len;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002745
Hanno Beckera18d1322018-01-03 14:27:32 +00002746#if !defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnarda7505d12019-05-07 10:17:56 +02002747 ssl = NULL; /* make sure we don't use it except for debug */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002748 ((void) ssl);
2749#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002750
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002751 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002752 if( rec == NULL ||
2753 rec->buf == NULL ||
2754 rec->buf_len < rec->data_offset ||
2755 rec->buf_len - rec->data_offset < rec->data_len )
2756 {
2757 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to decrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002758 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002759 }
2760
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002761 data = rec->buf + rec->data_offset;
2762 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_dec );
Paul Bakker5121ce52009-01-03 21:22:43 +00002763
Hanno Beckera0e20d02019-05-15 14:03:01 +01002764#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckercab87e62019-04-29 13:52:53 +01002765 /*
2766 * Match record's CID with incoming CID.
2767 */
Hanno Becker938489a2019-05-08 13:02:22 +01002768 if( rec->cid_len != transform->in_cid_len ||
2769 memcmp( rec->cid, transform->in_cid, rec->cid_len ) != 0 )
2770 {
Hanno Becker8367ccc2019-05-14 11:30:10 +01002771 return( MBEDTLS_ERR_SSL_UNEXPECTED_CID );
Hanno Becker938489a2019-05-08 13:02:22 +01002772 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01002773#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckercab87e62019-04-29 13:52:53 +01002774
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002775#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
2776 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01002777 {
2778 padlen = 0;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002779 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
2780 transform->iv_dec,
2781 transform->ivlen,
2782 data, rec->data_len,
2783 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002784 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002785 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002786 return( ret );
2787 }
2788
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002789 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002790 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002791 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2792 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002793 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002794 }
Paul Bakker68884e32013-01-07 18:20:04 +01002795 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002796#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002797#if defined(MBEDTLS_GCM_C) || \
2798 defined(MBEDTLS_CCM_C) || \
2799 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002800 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002801 mode == MBEDTLS_MODE_CCM ||
2802 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002803 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002804 unsigned char iv[12];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002805 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002806
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002807 /*
Hanno Beckerd96a6522019-07-10 13:55:25 +01002808 * Prepare IV from explicit and implicit data.
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002809 */
Hanno Beckerd96a6522019-07-10 13:55:25 +01002810
2811 /* Check that there's enough space for the explicit IV
2812 * (at the beginning of the record) and the MAC (at the
2813 * end of the record). */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002814 if( rec->data_len < explicit_iv_len + transform->taglen )
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002815 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002816 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002817 "+ taglen (%d)", rec->data_len,
2818 explicit_iv_len, transform->taglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002819 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002820 }
Paul Bakker68884e32013-01-07 18:20:04 +01002821
Hanno Beckerd96a6522019-07-10 13:55:25 +01002822#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CCM_C)
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002823 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
2824 {
Hanno Beckerd96a6522019-07-10 13:55:25 +01002825 /* GCM and CCM: fixed || explicit */
Paul Bakker68884e32013-01-07 18:20:04 +01002826
Hanno Beckerd96a6522019-07-10 13:55:25 +01002827 /* Fixed */
2828 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
2829 /* Explicit */
2830 memcpy( iv + transform->fixed_ivlen, data, 8 );
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002831 }
Hanno Beckerd96a6522019-07-10 13:55:25 +01002832 else
2833#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2834#if defined(MBEDTLS_CHACHAPOLY_C)
2835 if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002836 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002837 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002838 unsigned char i;
2839
2840 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
2841
2842 for( i = 0; i < 8; i++ )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002843 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002844 }
2845 else
Hanno Beckerd96a6522019-07-10 13:55:25 +01002846#endif /* MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002847 {
2848 /* Reminder if we ever add an AEAD mode with a different size */
2849 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2850 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2851 }
2852
Hanno Beckerd96a6522019-07-10 13:55:25 +01002853 /* Group changes to data, data_len, and add_data, because
2854 * add_data depends on data_len. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002855 data += explicit_iv_len;
2856 rec->data_offset += explicit_iv_len;
2857 rec->data_len -= explicit_iv_len + transform->taglen;
2858
Hanno Beckercab87e62019-04-29 13:52:53 +01002859 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002860 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Hanno Beckercab87e62019-04-29 13:52:53 +01002861 add_data, add_data_len );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002862
Hanno Beckerd96a6522019-07-10 13:55:25 +01002863 /* Because of the check above, we know that there are
2864 * explicit_iv_len Bytes preceeding data, and taglen
2865 * bytes following data + data_len. This justifies
Hanno Becker20016652019-07-10 11:44:13 +01002866 * the debug message and the invocation of
Hanno Beckerd96a6522019-07-10 13:55:25 +01002867 * mbedtls_cipher_auth_decrypt() below. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002868
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002869 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used", iv, transform->ivlen );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002870 MBEDTLS_SSL_DEBUG_BUF( 4, "TAG used", data + rec->data_len,
Hanno Beckere694c3e2017-12-27 21:34:08 +00002871 transform->taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01002872
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002873 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002874 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002875 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002876 if( ( ret = mbedtls_cipher_auth_decrypt( &transform->cipher_ctx_dec,
2877 iv, transform->ivlen,
Hanno Beckercab87e62019-04-29 13:52:53 +01002878 add_data, add_data_len,
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002879 data, rec->data_len,
2880 data, &olen,
2881 data + rec->data_len,
2882 transform->taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002883 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002884 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_decrypt", ret );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002885
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002886 if( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED )
2887 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002888
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002889 return( ret );
2890 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002891 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002892
Hanno Beckerd96a6522019-07-10 13:55:25 +01002893 /* Double-check that AEAD decryption doesn't change content length. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002894 if( olen != rec->data_len )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002895 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002896 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2897 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002898 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00002899 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002900 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002901#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2902#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002903 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002904 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00002905 {
Paul Bakkere47b34b2013-02-27 14:48:00 +01002906 size_t minlen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002907
Paul Bakker5121ce52009-01-03 21:22:43 +00002908 /*
Paul Bakker45829992013-01-03 14:52:21 +01002909 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00002910 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002911#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002912 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
2913 {
2914 /* The ciphertext is prefixed with the CBC IV. */
2915 minlen += transform->ivlen;
2916 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002917#endif
Paul Bakker45829992013-01-03 14:52:21 +01002918
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002919 /* Size considerations:
2920 *
2921 * - The CBC cipher text must not be empty and hence
2922 * at least of size transform->ivlen.
2923 *
2924 * Together with the potential IV-prefix, this explains
2925 * the first of the two checks below.
2926 *
2927 * - The record must contain a MAC, either in plain or
2928 * encrypted, depending on whether Encrypt-then-MAC
2929 * is used or not.
2930 * - If it is, the message contains the IV-prefix,
2931 * the CBC ciphertext, and the MAC.
2932 * - If it is not, the padded plaintext, and hence
2933 * the CBC ciphertext, has at least length maclen + 1
2934 * because there is at least the padding length byte.
2935 *
2936 * As the CBC ciphertext is not empty, both cases give the
2937 * lower bound minlen + maclen + 1 on the record size, which
2938 * we test for in the second check below.
2939 */
2940 if( rec->data_len < minlen + transform->ivlen ||
2941 rec->data_len < minlen + transform->maclen + 1 )
Paul Bakker45829992013-01-03 14:52:21 +01002942 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002943 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002944 "+ 1 ) ( + expl IV )", rec->data_len,
2945 transform->ivlen,
2946 transform->maclen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002947 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker45829992013-01-03 14:52:21 +01002948 }
2949
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002950 /*
2951 * Authenticate before decrypt if enabled
2952 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002953#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002954 if( transform->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002955 {
Hanno Becker992b6872017-11-09 18:57:39 +00002956 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002957
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002958 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002959
Hanno Beckerd96a6522019-07-10 13:55:25 +01002960 /* Update data_len in tandem with add_data.
2961 *
2962 * The subtraction is safe because of the previous check
2963 * data_len >= minlen + maclen + 1.
2964 *
2965 * Afterwards, we know that data + data_len is followed by at
2966 * least maclen Bytes, which justifies the call to
2967 * mbedtls_ssl_safer_memcmp() below.
2968 *
2969 * Further, we still know that data_len > minlen */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002970 rec->data_len -= transform->maclen;
Hanno Beckercab87e62019-04-29 13:52:53 +01002971 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002972
Hanno Beckerd96a6522019-07-10 13:55:25 +01002973 /* Calculate expected MAC. */
Hanno Beckercab87e62019-04-29 13:52:53 +01002974 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
2975 add_data_len );
2976 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data,
2977 add_data_len );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002978 mbedtls_md_hmac_update( &transform->md_ctx_dec,
2979 data, rec->data_len );
2980 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
2981 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002982
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002983 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len,
2984 transform->maclen );
Hanno Becker992b6872017-11-09 18:57:39 +00002985 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect,
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002986 transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002987
Hanno Beckerd96a6522019-07-10 13:55:25 +01002988 /* Compare expected MAC with MAC at the end of the record. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002989 if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect,
2990 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002991 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002992 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002993 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002994 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002995 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002996 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002997#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002998
2999 /*
3000 * Check length sanity
3001 */
Hanno Beckerd96a6522019-07-10 13:55:25 +01003002
3003 /* We know from above that data_len > minlen >= 0,
3004 * so the following check in particular implies that
3005 * data_len >= minlen + ivlen ( = minlen or 2 * minlen ). */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003006 if( rec->data_len % transform->ivlen != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003007 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003008 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003009 rec->data_len, transform->ivlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003010 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003011 }
3012
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003013#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003014 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00003015 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003016 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003017 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003018 {
Hanno Beckerd96a6522019-07-10 13:55:25 +01003019 /* Safe because data_len >= minlen + ivlen = 2 * ivlen. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003020 memcpy( transform->iv_dec, data, transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003021
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003022 data += transform->ivlen;
3023 rec->data_offset += transform->ivlen;
3024 rec->data_len -= transform->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003025 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003026#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003027
Hanno Beckerd96a6522019-07-10 13:55:25 +01003028 /* We still have data_len % ivlen == 0 and data_len >= ivlen here. */
3029
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003030 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
3031 transform->iv_dec, transform->ivlen,
3032 data, rec->data_len, data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02003033 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003034 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02003035 return( ret );
3036 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02003037
Hanno Beckerd96a6522019-07-10 13:55:25 +01003038 /* Double-check that length hasn't changed during decryption. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003039 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02003040 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003041 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3042 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02003043 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02003044
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003045#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003046 if( transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02003047 {
3048 /*
Hanno Beckerd96a6522019-07-10 13:55:25 +01003049 * Save IV in SSL3 and TLS1, where CBC decryption of consecutive
3050 * records is equivalent to CBC decryption of the concatenation
3051 * of the records; in other words, IVs are maintained across
3052 * record decryptions.
Paul Bakkercca5b812013-08-31 17:40:26 +02003053 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003054 memcpy( transform->iv_dec, transform->cipher_ctx_dec.iv,
3055 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00003056 }
Paul Bakkercca5b812013-08-31 17:40:26 +02003057#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003058
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003059 /* Safe since data_len >= minlen + maclen + 1, so after having
3060 * subtracted at most minlen and maclen up to this point,
Hanno Beckerd96a6522019-07-10 13:55:25 +01003061 * data_len > 0 (because of data_len % ivlen == 0, it's actually
3062 * >= ivlen ). */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003063 padlen = data[rec->data_len - 1];
Paul Bakker45829992013-01-03 14:52:21 +01003064
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003065 if( auth_done == 1 )
3066 {
3067 correct *= ( rec->data_len >= padlen + 1 );
3068 padlen *= ( rec->data_len >= padlen + 1 );
3069 }
3070 else
Paul Bakker45829992013-01-03 14:52:21 +01003071 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003072#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003073 if( rec->data_len < transform->maclen + padlen + 1 )
3074 {
3075 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
3076 rec->data_len,
3077 transform->maclen,
3078 padlen + 1 ) );
3079 }
Paul Bakkerd66f0702013-01-31 16:57:45 +01003080#endif
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003081
3082 correct *= ( rec->data_len >= transform->maclen + padlen + 1 );
3083 padlen *= ( rec->data_len >= transform->maclen + padlen + 1 );
Paul Bakker45829992013-01-03 14:52:21 +01003084 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003085
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003086 padlen++;
3087
3088 /* Regardless of the validity of the padding,
3089 * we have data_len >= padlen here. */
3090
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003091#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003092 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003093 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003094 if( padlen > transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00003095 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003096#if defined(MBEDTLS_SSL_DEBUG_ALL)
3097 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003098 "should be no more than %d",
3099 padlen, transform->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01003100#endif
Paul Bakker45829992013-01-03 14:52:21 +01003101 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00003102 }
3103 }
3104 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003105#endif /* MBEDTLS_SSL_PROTO_SSL3 */
3106#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
3107 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003108 if( transform->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003109 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003110 /* The padding check involves a series of up to 256
3111 * consecutive memory reads at the end of the record
3112 * plaintext buffer. In order to hide the length and
3113 * validity of the padding, always perform exactly
3114 * `min(256,plaintext_len)` reads (but take into account
3115 * only the last `padlen` bytes for the padding check). */
3116 size_t pad_count = 0;
3117 size_t real_count = 0;
3118 volatile unsigned char* const check = data;
Paul Bakkere47b34b2013-02-27 14:48:00 +01003119
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003120 /* Index of first padding byte; it has been ensured above
3121 * that the subtraction is safe. */
3122 size_t const padding_idx = rec->data_len - padlen;
3123 size_t const num_checks = rec->data_len <= 256 ? rec->data_len : 256;
3124 size_t const start_idx = rec->data_len - num_checks;
3125 size_t idx;
Paul Bakker956c9e02013-12-19 14:42:28 +01003126
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003127 for( idx = start_idx; idx < rec->data_len; idx++ )
Paul Bakkerca9c87e2013-09-25 18:52:37 +02003128 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003129 real_count |= ( idx >= padding_idx );
3130 pad_count += real_count * ( check[idx] == padlen - 1 );
Paul Bakkerca9c87e2013-09-25 18:52:37 +02003131 }
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003132 correct &= ( pad_count == padlen );
Paul Bakkere47b34b2013-02-27 14:48:00 +01003133
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003134#if defined(MBEDTLS_SSL_DEBUG_ALL)
Paul Bakker66d5d072014-06-17 16:39:18 +02003135 if( padlen > 0 && correct == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003136 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01003137#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01003138 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00003139 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003140 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003141#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
3142 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02003143 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003144 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3145 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02003146 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003147
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003148 /* If the padding was found to be invalid, padlen == 0
3149 * and the subtraction is safe. If the padding was found valid,
3150 * padlen hasn't been changed and the previous assertion
3151 * data_len >= padlen still holds. */
3152 rec->data_len -= padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00003153 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02003154 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003155#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00003156 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02003157 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003158 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3159 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02003160 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003161
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02003162#if defined(MBEDTLS_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003163 MBEDTLS_SSL_DEBUG_BUF( 4, "raw buffer after decryption",
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003164 data, rec->data_len );
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02003165#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003166
3167 /*
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003168 * Authenticate if not done yet.
3169 * Compute the MAC regardless of the padding result (RFC4346, CBCTIME).
Paul Bakker5121ce52009-01-03 21:22:43 +00003170 */
Hanno Becker52344c22018-01-03 15:24:20 +00003171#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003172 if( auth_done == 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003173 {
Hanno Becker992b6872017-11-09 18:57:39 +00003174 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Paul Bakker1e5369c2013-12-19 16:40:57 +01003175
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003176 /* If the initial value of padlen was such that
3177 * data_len < maclen + padlen + 1, then padlen
3178 * got reset to 1, and the initial check
3179 * data_len >= minlen + maclen + 1
3180 * guarantees that at this point we still
3181 * have at least data_len >= maclen.
3182 *
3183 * If the initial value of padlen was such that
3184 * data_len >= maclen + padlen + 1, then we have
3185 * subtracted either padlen + 1 (if the padding was correct)
3186 * or 0 (if the padding was incorrect) since then,
3187 * hence data_len >= maclen in any case.
3188 */
3189 rec->data_len -= transform->maclen;
Hanno Beckercab87e62019-04-29 13:52:53 +01003190 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Paul Bakker5121ce52009-01-03 21:22:43 +00003191
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003192#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003193 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003194 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003195 ssl_mac( &transform->md_ctx_dec,
3196 transform->mac_dec,
3197 data, rec->data_len,
3198 rec->ctr, rec->type,
3199 mac_expect );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003200 }
3201 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003202#endif /* MBEDTLS_SSL_PROTO_SSL3 */
3203#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
3204 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003205 if( transform->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003206 {
3207 /*
3208 * Process MAC and always update for padlen afterwards to make
Gilles Peskine20b44082018-05-29 14:06:49 +02003209 * total time independent of padlen.
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003210 *
3211 * Known timing attacks:
3212 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
3213 *
Gilles Peskine20b44082018-05-29 14:06:49 +02003214 * To compensate for different timings for the MAC calculation
3215 * depending on how much padding was removed (which is determined
3216 * by padlen), process extra_run more blocks through the hash
3217 * function.
3218 *
3219 * The formula in the paper is
3220 * extra_run = ceil( (L1-55) / 64 ) - ceil( (L2-55) / 64 )
3221 * where L1 is the size of the header plus the decrypted message
3222 * plus CBC padding and L2 is the size of the header plus the
3223 * decrypted message. This is for an underlying hash function
3224 * with 64-byte blocks.
3225 * We use ( (Lx+8) / 64 ) to handle 'negative Lx' values
3226 * correctly. We round down instead of up, so -56 is the correct
3227 * value for our calculations instead of -55.
3228 *
Gilles Peskine1bd9d582018-06-04 11:58:44 +02003229 * Repeat the formula rather than defining a block_size variable.
3230 * This avoids requiring division by a variable at runtime
3231 * (which would be marginally less efficient and would require
3232 * linking an extra division function in some builds).
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003233 */
3234 size_t j, extra_run = 0;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003235 unsigned char tmp[MBEDTLS_MD_MAX_BLOCK_SIZE];
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003236
3237 /*
3238 * The next two sizes are the minimum and maximum values of
3239 * in_msglen over all padlen values.
3240 *
3241 * They're independent of padlen, since we previously did
Hanno Beckerd96a6522019-07-10 13:55:25 +01003242 * data_len -= padlen.
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003243 *
3244 * Note that max_len + maclen is never more than the buffer
3245 * length, as we previously did in_msglen -= maclen too.
3246 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003247 const size_t max_len = rec->data_len + padlen;
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003248 const size_t min_len = ( max_len > 256 ) ? max_len - 256 : 0;
3249
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003250 memset( tmp, 0, sizeof( tmp ) );
3251
3252 switch( mbedtls_md_get_type( transform->md_ctx_dec.md_info ) )
Gilles Peskine20b44082018-05-29 14:06:49 +02003253 {
Gilles Peskined0e55a42018-06-04 12:03:30 +02003254#if defined(MBEDTLS_MD5_C) || defined(MBEDTLS_SHA1_C) || \
3255 defined(MBEDTLS_SHA256_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02003256 case MBEDTLS_MD_MD5:
3257 case MBEDTLS_MD_SHA1:
Gilles Peskine20b44082018-05-29 14:06:49 +02003258 case MBEDTLS_MD_SHA256:
Gilles Peskine20b44082018-05-29 14:06:49 +02003259 /* 8 bytes of message size, 64-byte compression blocks */
Hanno Beckercab87e62019-04-29 13:52:53 +01003260 extra_run =
3261 ( add_data_len + rec->data_len + padlen + 8 ) / 64 -
3262 ( add_data_len + rec->data_len + 8 ) / 64;
Gilles Peskine20b44082018-05-29 14:06:49 +02003263 break;
3264#endif
Gilles Peskinea7fe25d2018-06-04 12:01:18 +02003265#if defined(MBEDTLS_SHA512_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02003266 case MBEDTLS_MD_SHA384:
Gilles Peskine20b44082018-05-29 14:06:49 +02003267 /* 16 bytes of message size, 128-byte compression blocks */
Hanno Beckercab87e62019-04-29 13:52:53 +01003268 extra_run =
3269 ( add_data_len + rec->data_len + padlen + 16 ) / 128 -
3270 ( add_data_len + rec->data_len + 16 ) / 128;
Gilles Peskine20b44082018-05-29 14:06:49 +02003271 break;
3272#endif
3273 default:
Gilles Peskine5c389842018-06-04 12:02:43 +02003274 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Gilles Peskine20b44082018-05-29 14:06:49 +02003275 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3276 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01003277
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003278 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01003279
Hanno Beckercab87e62019-04-29 13:52:53 +01003280 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data,
3281 add_data_len );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003282 mbedtls_md_hmac_update( &transform->md_ctx_dec, data,
3283 rec->data_len );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003284 /* Make sure we access everything even when padlen > 0. This
3285 * makes the synchronisation requirements for just-in-time
3286 * Prime+Probe attacks much tighter and hopefully impractical. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003287 ssl_read_memory( data + rec->data_len, padlen );
3288 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003289
3290 /* Call mbedtls_md_process at least once due to cache attacks
3291 * that observe whether md_process() was called of not */
Manuel Pégourié-Gonnard47fede02015-04-29 01:35:48 +02003292 for( j = 0; j < extra_run + 1; j++ )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003293 mbedtls_md_process( &transform->md_ctx_dec, tmp );
Paul Bakkere47b34b2013-02-27 14:48:00 +01003294
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003295 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003296
3297 /* Make sure we access all the memory that could contain the MAC,
3298 * before we check it in the next code block. This makes the
3299 * synchronisation requirements for just-in-time Prime+Probe
3300 * attacks much tighter and hopefully impractical. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003301 ssl_read_memory( data + min_len,
3302 max_len - min_len + transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003303 }
3304 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003305#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
3306 MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003307 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003308 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3309 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003310 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003311
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003312#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003313 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect, transform->maclen );
3314 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len, transform->maclen );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003315#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003316
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003317 if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect,
3318 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003319 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003320#if defined(MBEDTLS_SSL_DEBUG_ALL)
3321 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01003322#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003323 correct = 0;
3324 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003325 auth_done++;
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003326 }
Hanno Beckerdd3ab132018-10-17 14:43:14 +01003327
3328 /*
3329 * Finally check the correct flag
3330 */
3331 if( correct == 0 )
3332 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Hanno Becker52344c22018-01-03 15:24:20 +00003333#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003334
3335 /* Make extra sure authentication was performed, exactly once */
3336 if( auth_done != 1 )
3337 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003338 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3339 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003340 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003341
Hanno Beckera0e20d02019-05-15 14:03:01 +01003342#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01003343 if( rec->cid_len != 0 )
3344 {
3345 ret = ssl_cid_parse_inner_plaintext( data, &rec->data_len,
3346 &rec->type );
3347 if( ret != 0 )
3348 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3349 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01003350#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01003351
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003352 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003353
3354 return( 0 );
3355}
3356
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01003357#undef MAC_NONE
3358#undef MAC_PLAINTEXT
3359#undef MAC_CIPHERTEXT
3360
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003361#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00003362/*
3363 * Compression/decompression functions
3364 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003365static int ssl_compress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003366{
3367 int ret;
3368 unsigned char *msg_post = ssl->out_msg;
Andrzej Kurek5462e022018-04-20 07:58:53 -04003369 ptrdiff_t bytes_written = ssl->out_msg - ssl->out_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003370 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02003371 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003372
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003373 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003374
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02003375 if( len_pre == 0 )
3376 return( 0 );
3377
Paul Bakker2770fbd2012-07-03 13:30:23 +00003378 memcpy( msg_pre, ssl->out_msg, len_pre );
3379
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003380 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003381 ssl->out_msglen ) );
3382
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003383 MBEDTLS_SSL_DEBUG_BUF( 4, "before compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003384 ssl->out_msg, ssl->out_msglen );
3385
Paul Bakker48916f92012-09-16 19:57:18 +00003386 ssl->transform_out->ctx_deflate.next_in = msg_pre;
3387 ssl->transform_out->ctx_deflate.avail_in = len_pre;
3388 ssl->transform_out->ctx_deflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10003389 ssl->transform_out->ctx_deflate.avail_out = MBEDTLS_SSL_OUT_BUFFER_LEN - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003390
Paul Bakker48916f92012-09-16 19:57:18 +00003391 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003392 if( ret != Z_OK )
3393 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003394 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
3395 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003396 }
3397
Angus Grattond8213d02016-05-25 20:56:48 +10003398 ssl->out_msglen = MBEDTLS_SSL_OUT_BUFFER_LEN -
Andrzej Kurek5462e022018-04-20 07:58:53 -04003399 ssl->transform_out->ctx_deflate.avail_out - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003400
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003401 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003402 ssl->out_msglen ) );
3403
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003404 MBEDTLS_SSL_DEBUG_BUF( 4, "after compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003405 ssl->out_msg, ssl->out_msglen );
3406
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003407 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003408
3409 return( 0 );
3410}
3411
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003412static int ssl_decompress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003413{
3414 int ret;
3415 unsigned char *msg_post = ssl->in_msg;
Andrzej Kureka9ceef82018-04-24 06:32:44 -04003416 ptrdiff_t header_bytes = ssl->in_msg - ssl->in_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003417 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02003418 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003419
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003420 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003421
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02003422 if( len_pre == 0 )
3423 return( 0 );
3424
Paul Bakker2770fbd2012-07-03 13:30:23 +00003425 memcpy( msg_pre, ssl->in_msg, len_pre );
3426
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003427 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003428 ssl->in_msglen ) );
3429
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003430 MBEDTLS_SSL_DEBUG_BUF( 4, "before decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003431 ssl->in_msg, ssl->in_msglen );
3432
Paul Bakker48916f92012-09-16 19:57:18 +00003433 ssl->transform_in->ctx_inflate.next_in = msg_pre;
3434 ssl->transform_in->ctx_inflate.avail_in = len_pre;
3435 ssl->transform_in->ctx_inflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10003436 ssl->transform_in->ctx_inflate.avail_out = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04003437 header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003438
Paul Bakker48916f92012-09-16 19:57:18 +00003439 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003440 if( ret != Z_OK )
3441 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003442 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
3443 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003444 }
3445
Angus Grattond8213d02016-05-25 20:56:48 +10003446 ssl->in_msglen = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04003447 ssl->transform_in->ctx_inflate.avail_out - header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003448
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003449 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after 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, "after decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003453 ssl->in_msg, ssl->in_msglen );
3454
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003455 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003456
3457 return( 0 );
3458}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003459#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003460
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003461#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
3462static int ssl_write_hello_request( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003463
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003464#if defined(MBEDTLS_SSL_PROTO_DTLS)
3465static int ssl_resend_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003466{
3467 /* If renegotiation is not enforced, retransmit until we would reach max
3468 * timeout if we were using the usual handshake doubling scheme */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003469 if( ssl->conf->renego_max_records < 0 )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003470 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003471 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003472 unsigned char doublings = 1;
3473
3474 while( ratio != 0 )
3475 {
3476 ++doublings;
3477 ratio >>= 1;
3478 }
3479
3480 if( ++ssl->renego_records_seen > doublings )
3481 {
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02003482 MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003483 return( 0 );
3484 }
3485 }
3486
3487 return( ssl_write_hello_request( ssl ) );
3488}
3489#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003490#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003491
Paul Bakker5121ce52009-01-03 21:22:43 +00003492/*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003493 * Fill the input message buffer by appending data to it.
3494 * The amount of data already fetched is in ssl->in_left.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003495 *
3496 * If we return 0, is it guaranteed that (at least) nb_want bytes are
3497 * available (from this read and/or a previous one). Otherwise, an error code
3498 * is returned (possibly EOF or WANT_READ).
3499 *
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003500 * With stream transport (TLS) on success ssl->in_left == nb_want, but
3501 * with datagram transport (DTLS) on success ssl->in_left >= nb_want,
3502 * since we always read a whole datagram at once.
3503 *
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02003504 * For DTLS, it is up to the caller to set ssl->next_record_offset when
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003505 * they're done reading a record.
Paul Bakker5121ce52009-01-03 21:22:43 +00003506 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003507int mbedtls_ssl_fetch_input( mbedtls_ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00003508{
Paul Bakker23986e52011-04-24 08:57:21 +00003509 int ret;
3510 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00003511
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003512 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003513
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003514 if( ssl->f_recv == NULL && ssl->f_recv_timeout == NULL )
3515 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003516 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01003517 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003518 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003519 }
3520
Angus Grattond8213d02016-05-25 20:56:48 +10003521 if( nb_want > MBEDTLS_SSL_IN_BUFFER_LEN - (size_t)( ssl->in_hdr - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003522 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003523 MBEDTLS_SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
3524 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003525 }
3526
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003527#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003528 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00003529 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003530 uint32_t timeout;
3531
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02003532 /* Just to be sure */
3533 if( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL )
3534 {
3535 MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
3536 "mbedtls_ssl_set_timer_cb() for DTLS" ) );
3537 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3538 }
3539
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003540 /*
3541 * The point is, we need to always read a full datagram at once, so we
3542 * sometimes read more then requested, and handle the additional data.
3543 * It could be the rest of the current record (while fetching the
3544 * header) and/or some other records in the same datagram.
3545 */
3546
3547 /*
3548 * Move to the next record in the already read datagram if applicable
3549 */
3550 if( ssl->next_record_offset != 0 )
3551 {
3552 if( ssl->in_left < ssl->next_record_offset )
3553 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003554 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3555 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003556 }
3557
3558 ssl->in_left -= ssl->next_record_offset;
3559
3560 if( ssl->in_left != 0 )
3561 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003562 MBEDTLS_SSL_DEBUG_MSG( 2, ( "next record in same datagram, offset: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003563 ssl->next_record_offset ) );
3564 memmove( ssl->in_hdr,
3565 ssl->in_hdr + ssl->next_record_offset,
3566 ssl->in_left );
3567 }
3568
3569 ssl->next_record_offset = 0;
3570 }
3571
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003572 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Paul Bakker5121ce52009-01-03 21:22:43 +00003573 ssl->in_left, nb_want ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003574
3575 /*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003576 * Done if we already have enough data.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003577 */
3578 if( nb_want <= ssl->in_left)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003579 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003580 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003581 return( 0 );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003582 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003583
3584 /*
Antonin Décimo36e89b52019-01-23 15:24:37 +01003585 * A record can't be split across datagrams. If we need to read but
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003586 * are not at the beginning of a new record, the caller did something
3587 * wrong.
3588 */
3589 if( ssl->in_left != 0 )
3590 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003591 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3592 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003593 }
3594
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003595 /*
3596 * Don't even try to read if time's out already.
3597 * This avoids by-passing the timer when repeatedly receiving messages
3598 * that will end up being dropped.
3599 */
3600 if( ssl_check_timer( ssl ) != 0 )
Hanno Beckere65ce782017-05-22 14:47:48 +01003601 {
3602 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timer has expired" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003603 ret = MBEDTLS_ERR_SSL_TIMEOUT;
Hanno Beckere65ce782017-05-22 14:47:48 +01003604 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003605 else
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003606 {
Angus Grattond8213d02016-05-25 20:56:48 +10003607 len = MBEDTLS_SSL_IN_BUFFER_LEN - ( ssl->in_hdr - ssl->in_buf );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003608
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003609 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003610 timeout = ssl->handshake->retransmit_timeout;
3611 else
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003612 timeout = ssl->conf->read_timeout;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003613
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003614 MBEDTLS_SSL_DEBUG_MSG( 3, ( "f_recv_timeout: %u ms", timeout ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003615
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003616 if( ssl->f_recv_timeout != NULL )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003617 ret = ssl->f_recv_timeout( ssl->p_bio, ssl->in_hdr, len,
3618 timeout );
3619 else
3620 ret = ssl->f_recv( ssl->p_bio, ssl->in_hdr, len );
3621
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003622 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003623
3624 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003625 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003626 }
3627
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003628 if( ret == MBEDTLS_ERR_SSL_TIMEOUT )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003629 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003630 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timeout" ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003631 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003632
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003633 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003634 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003635 if( ssl_double_retransmit_timeout( ssl ) != 0 )
3636 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003637 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake timeout" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003638 return( MBEDTLS_ERR_SSL_TIMEOUT );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003639 }
3640
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003641 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003642 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003643 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003644 return( ret );
3645 }
3646
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003647 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003648 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003649#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003650 else if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003651 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003652 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003653 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003654 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003655 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003656 return( ret );
3657 }
3658
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003659 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003660 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003661#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003662 }
3663
Paul Bakker5121ce52009-01-03 21:22:43 +00003664 if( ret < 0 )
3665 return( ret );
3666
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003667 ssl->in_left = ret;
3668 }
3669 else
3670#endif
3671 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003672 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003673 ssl->in_left, nb_want ) );
3674
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003675 while( ssl->in_left < nb_want )
3676 {
3677 len = nb_want - ssl->in_left;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02003678
3679 if( ssl_check_timer( ssl ) != 0 )
3680 ret = MBEDTLS_ERR_SSL_TIMEOUT;
3681 else
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003682 {
3683 if( ssl->f_recv_timeout != NULL )
3684 {
3685 ret = ssl->f_recv_timeout( ssl->p_bio,
3686 ssl->in_hdr + ssl->in_left, len,
3687 ssl->conf->read_timeout );
3688 }
3689 else
3690 {
3691 ret = ssl->f_recv( ssl->p_bio,
3692 ssl->in_hdr + ssl->in_left, len );
3693 }
3694 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003695
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003696 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003697 ssl->in_left, nb_want ) );
3698 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003699
3700 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003701 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003702
3703 if( ret < 0 )
3704 return( ret );
3705
mohammad160352aecb92018-03-28 23:41:40 -07003706 if ( (size_t)ret > len || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16035bd15cb2018-02-28 04:30:59 -08003707 {
Darryl Green11999bb2018-03-13 15:22:58 +00003708 MBEDTLS_SSL_DEBUG_MSG( 1,
3709 ( "f_recv returned %d bytes but only %lu were requested",
mohammad160319d392b2018-04-02 07:25:26 -07003710 ret, (unsigned long)len ) );
mohammad16035bd15cb2018-02-28 04:30:59 -08003711 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3712 }
3713
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003714 ssl->in_left += ret;
3715 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003716 }
3717
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003718 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003719
3720 return( 0 );
3721}
3722
3723/*
3724 * Flush any data not yet written
3725 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003726int mbedtls_ssl_flush_output( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003727{
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01003728 int ret;
Hanno Becker04484622018-08-06 09:49:38 +01003729 unsigned char *buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00003730
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003731 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003732
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003733 if( ssl->f_send == NULL )
3734 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003735 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01003736 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003737 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003738 }
3739
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003740 /* Avoid incrementing counter if data is flushed */
3741 if( ssl->out_left == 0 )
3742 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003743 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003744 return( 0 );
3745 }
3746
Paul Bakker5121ce52009-01-03 21:22:43 +00003747 while( ssl->out_left > 0 )
3748 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003749 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
Hanno Becker5903de42019-05-03 14:46:38 +01003750 mbedtls_ssl_out_hdr_len( ssl ) + ssl->out_msglen, ssl->out_left ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003751
Hanno Becker2b1e3542018-08-06 11:19:13 +01003752 buf = ssl->out_hdr - ssl->out_left;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003753 ret = ssl->f_send( ssl->p_bio, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00003754
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003755 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003756
3757 if( ret <= 0 )
3758 return( ret );
3759
mohammad160352aecb92018-03-28 23:41:40 -07003760 if( (size_t)ret > ssl->out_left || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16034bbaeb42018-02-22 04:29:04 -08003761 {
Darryl Green11999bb2018-03-13 15:22:58 +00003762 MBEDTLS_SSL_DEBUG_MSG( 1,
3763 ( "f_send returned %d bytes but only %lu bytes were sent",
mohammad160319d392b2018-04-02 07:25:26 -07003764 ret, (unsigned long)ssl->out_left ) );
mohammad16034bbaeb42018-02-22 04:29:04 -08003765 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3766 }
3767
Paul Bakker5121ce52009-01-03 21:22:43 +00003768 ssl->out_left -= ret;
3769 }
3770
Hanno Becker2b1e3542018-08-06 11:19:13 +01003771#if defined(MBEDTLS_SSL_PROTO_DTLS)
3772 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003773 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01003774 ssl->out_hdr = ssl->out_buf;
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003775 }
Hanno Becker2b1e3542018-08-06 11:19:13 +01003776 else
3777#endif
3778 {
3779 ssl->out_hdr = ssl->out_buf + 8;
3780 }
3781 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003782
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003783 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003784
3785 return( 0 );
3786}
3787
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003788/*
3789 * Functions to handle the DTLS retransmission state machine
3790 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003791#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003792/*
3793 * Append current handshake message to current outgoing flight
3794 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003795static int ssl_flight_append( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003796{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003797 mbedtls_ssl_flight_item *msg;
Hanno Becker3b235902018-08-06 09:54:53 +01003798 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_flight_append" ) );
3799 MBEDTLS_SSL_DEBUG_BUF( 4, "message appended to flight",
3800 ssl->out_msg, ssl->out_msglen );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003801
3802 /* Allocate space for current message */
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003803 if( ( msg = mbedtls_calloc( 1, sizeof( mbedtls_ssl_flight_item ) ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003804 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003805 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003806 sizeof( mbedtls_ssl_flight_item ) ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003807 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003808 }
3809
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003810 if( ( msg->p = mbedtls_calloc( 1, ssl->out_msglen ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003811 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003812 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed", ssl->out_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003813 mbedtls_free( msg );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003814 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003815 }
3816
3817 /* Copy current handshake message with headers */
3818 memcpy( msg->p, ssl->out_msg, ssl->out_msglen );
3819 msg->len = ssl->out_msglen;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003820 msg->type = ssl->out_msgtype;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003821 msg->next = NULL;
3822
3823 /* Append to the current flight */
3824 if( ssl->handshake->flight == NULL )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003825 ssl->handshake->flight = msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003826 else
3827 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003828 mbedtls_ssl_flight_item *cur = ssl->handshake->flight;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003829 while( cur->next != NULL )
3830 cur = cur->next;
3831 cur->next = msg;
3832 }
3833
Hanno Becker3b235902018-08-06 09:54:53 +01003834 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_flight_append" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003835 return( 0 );
3836}
3837
3838/*
3839 * Free the current flight of handshake messages
3840 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003841static void ssl_flight_free( mbedtls_ssl_flight_item *flight )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003842{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003843 mbedtls_ssl_flight_item *cur = flight;
3844 mbedtls_ssl_flight_item *next;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003845
3846 while( cur != NULL )
3847 {
3848 next = cur->next;
3849
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003850 mbedtls_free( cur->p );
3851 mbedtls_free( cur );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003852
3853 cur = next;
3854 }
3855}
3856
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003857#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3858static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003859#endif
3860
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003861/*
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003862 * Swap transform_out and out_ctr with the alternative ones
3863 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003864static void ssl_swap_epochs( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003865{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003866 mbedtls_ssl_transform *tmp_transform;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003867 unsigned char tmp_out_ctr[8];
3868
3869 if( ssl->transform_out == ssl->handshake->alt_transform_out )
3870 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003871 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003872 return;
3873 }
3874
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003875 MBEDTLS_SSL_DEBUG_MSG( 3, ( "swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003876
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003877 /* Swap transforms */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003878 tmp_transform = ssl->transform_out;
3879 ssl->transform_out = ssl->handshake->alt_transform_out;
3880 ssl->handshake->alt_transform_out = tmp_transform;
3881
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003882 /* Swap epoch + sequence_number */
Hanno Becker19859472018-08-06 09:40:20 +01003883 memcpy( tmp_out_ctr, ssl->cur_out_ctr, 8 );
3884 memcpy( ssl->cur_out_ctr, ssl->handshake->alt_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003885 memcpy( ssl->handshake->alt_out_ctr, tmp_out_ctr, 8 );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003886
3887 /* Adjust to the newly activated transform */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01003888 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003889
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003890#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3891 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003892 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003893 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003894 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003895 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
3896 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003897 }
3898 }
3899#endif
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003900}
3901
3902/*
3903 * Retransmit the current flight of messages.
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003904 */
3905int mbedtls_ssl_resend( mbedtls_ssl_context *ssl )
3906{
3907 int ret = 0;
3908
3909 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_resend" ) );
3910
3911 ret = mbedtls_ssl_flight_transmit( ssl );
3912
3913 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_resend" ) );
3914
3915 return( ret );
3916}
3917
3918/*
3919 * Transmit or retransmit the current flight of messages.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003920 *
3921 * Need to remember the current message in case flush_output returns
3922 * WANT_WRITE, causing us to exit this function and come back later.
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003923 * This function must be called until state is no longer SENDING.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003924 */
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003925int mbedtls_ssl_flight_transmit( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003926{
Hanno Becker67bc7c32018-08-06 11:33:50 +01003927 int ret;
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003928 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003929
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003930 if( ssl->handshake->retransmit_state != MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003931 {
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003932 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialise flight transmission" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003933
3934 ssl->handshake->cur_msg = ssl->handshake->flight;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003935 ssl->handshake->cur_msg_p = ssl->handshake->flight->p + 12;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003936 ssl_swap_epochs( ssl );
3937
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003938 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_SENDING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003939 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003940
3941 while( ssl->handshake->cur_msg != NULL )
3942 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003943 size_t max_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003944 const mbedtls_ssl_flight_item * const cur = ssl->handshake->cur_msg;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003945
Hanno Beckere1dcb032018-08-17 16:47:58 +01003946 int const is_finished =
3947 ( cur->type == MBEDTLS_SSL_MSG_HANDSHAKE &&
3948 cur->p[0] == MBEDTLS_SSL_HS_FINISHED );
3949
Hanno Becker04da1892018-08-14 13:22:10 +01003950 uint8_t const force_flush = ssl->disable_datagram_packing == 1 ?
3951 SSL_FORCE_FLUSH : SSL_DONT_FORCE_FLUSH;
3952
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003953 /* Swap epochs before sending Finished: we can't do it after
3954 * sending ChangeCipherSpec, in case write returns WANT_READ.
3955 * Must be done before copying, may change out_msg pointer */
Hanno Beckere1dcb032018-08-17 16:47:58 +01003956 if( is_finished && ssl->handshake->cur_msg_p == ( cur->p + 12 ) )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003957 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003958 MBEDTLS_SSL_DEBUG_MSG( 2, ( "swap epochs to send finished message" ) );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003959 ssl_swap_epochs( ssl );
3960 }
3961
Hanno Becker67bc7c32018-08-06 11:33:50 +01003962 ret = ssl_get_remaining_payload_in_datagram( ssl );
3963 if( ret < 0 )
3964 return( ret );
3965 max_frag_len = (size_t) ret;
3966
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003967 /* CCS is copied as is, while HS messages may need fragmentation */
3968 if( cur->type == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
3969 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003970 if( max_frag_len == 0 )
3971 {
3972 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3973 return( ret );
3974
3975 continue;
3976 }
3977
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003978 memcpy( ssl->out_msg, cur->p, cur->len );
Hanno Becker67bc7c32018-08-06 11:33:50 +01003979 ssl->out_msglen = cur->len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003980 ssl->out_msgtype = cur->type;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003981
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003982 /* Update position inside current message */
3983 ssl->handshake->cur_msg_p += cur->len;
3984 }
3985 else
3986 {
3987 const unsigned char * const p = ssl->handshake->cur_msg_p;
3988 const size_t hs_len = cur->len - 12;
3989 const size_t frag_off = p - ( cur->p + 12 );
3990 const size_t rem_len = hs_len - frag_off;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003991 size_t cur_hs_frag_len, max_hs_frag_len;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003992
Hanno Beckere1dcb032018-08-17 16:47:58 +01003993 if( ( max_frag_len < 12 ) || ( max_frag_len == 12 && hs_len != 0 ) )
Manuel Pégourié-Gonnarda1071a52018-08-20 11:56:14 +02003994 {
Hanno Beckere1dcb032018-08-17 16:47:58 +01003995 if( is_finished )
Hanno Becker67bc7c32018-08-06 11:33:50 +01003996 ssl_swap_epochs( ssl );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003997
Hanno Becker67bc7c32018-08-06 11:33:50 +01003998 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3999 return( ret );
4000
4001 continue;
4002 }
4003 max_hs_frag_len = max_frag_len - 12;
4004
4005 cur_hs_frag_len = rem_len > max_hs_frag_len ?
4006 max_hs_frag_len : rem_len;
4007
4008 if( frag_off == 0 && cur_hs_frag_len != hs_len )
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02004009 {
4010 MBEDTLS_SSL_DEBUG_MSG( 2, ( "fragmenting handshake message (%u > %u)",
Hanno Becker67bc7c32018-08-06 11:33:50 +01004011 (unsigned) cur_hs_frag_len,
4012 (unsigned) max_hs_frag_len ) );
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02004013 }
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02004014
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004015 /* Messages are stored with handshake headers as if not fragmented,
4016 * copy beginning of headers then fill fragmentation fields.
4017 * Handshake headers: type(1) len(3) seq(2) f_off(3) f_len(3) */
4018 memcpy( ssl->out_msg, cur->p, 6 );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004019
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004020 ssl->out_msg[6] = ( ( frag_off >> 16 ) & 0xff );
4021 ssl->out_msg[7] = ( ( frag_off >> 8 ) & 0xff );
4022 ssl->out_msg[8] = ( ( frag_off ) & 0xff );
4023
Hanno Becker67bc7c32018-08-06 11:33:50 +01004024 ssl->out_msg[ 9] = ( ( cur_hs_frag_len >> 16 ) & 0xff );
4025 ssl->out_msg[10] = ( ( cur_hs_frag_len >> 8 ) & 0xff );
4026 ssl->out_msg[11] = ( ( cur_hs_frag_len ) & 0xff );
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004027
4028 MBEDTLS_SSL_DEBUG_BUF( 3, "handshake header", ssl->out_msg, 12 );
4029
Hanno Becker3f7b9732018-08-28 09:53:25 +01004030 /* Copy the handshake message content and set records fields */
Hanno Becker67bc7c32018-08-06 11:33:50 +01004031 memcpy( ssl->out_msg + 12, p, cur_hs_frag_len );
4032 ssl->out_msglen = cur_hs_frag_len + 12;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004033 ssl->out_msgtype = cur->type;
4034
4035 /* Update position inside current message */
Hanno Becker67bc7c32018-08-06 11:33:50 +01004036 ssl->handshake->cur_msg_p += cur_hs_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004037 }
4038
4039 /* If done with the current message move to the next one if any */
4040 if( ssl->handshake->cur_msg_p >= cur->p + cur->len )
4041 {
4042 if( cur->next != NULL )
4043 {
4044 ssl->handshake->cur_msg = cur->next;
4045 ssl->handshake->cur_msg_p = cur->next->p + 12;
4046 }
4047 else
4048 {
4049 ssl->handshake->cur_msg = NULL;
4050 ssl->handshake->cur_msg_p = NULL;
4051 }
4052 }
4053
4054 /* Actually send the message out */
Hanno Becker04da1892018-08-14 13:22:10 +01004055 if( ( ret = mbedtls_ssl_write_record( ssl, force_flush ) ) != 0 )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004056 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004057 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004058 return( ret );
4059 }
4060 }
4061
Hanno Becker67bc7c32018-08-06 11:33:50 +01004062 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
4063 return( ret );
4064
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004065 /* Update state and set timer */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004066 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
4067 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard23b7b702014-09-25 13:50:12 +02004068 else
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02004069 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004070 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02004071 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
4072 }
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004073
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004074 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004075
4076 return( 0 );
4077}
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004078
4079/*
4080 * To be called when the last message of an incoming flight is received.
4081 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004082void mbedtls_ssl_recv_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004083{
4084 /* We won't need to resend that one any more */
4085 ssl_flight_free( ssl->handshake->flight );
4086 ssl->handshake->flight = NULL;
4087 ssl->handshake->cur_msg = NULL;
4088
4089 /* The next incoming flight will start with this msg_seq */
4090 ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq;
4091
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004092 /* We don't want to remember CCS's across flight boundaries. */
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01004093 ssl->handshake->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004094
Hanno Becker0271f962018-08-16 13:23:47 +01004095 /* Clear future message buffering structure. */
4096 ssl_buffering_free( ssl );
4097
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02004098 /* Cancel timer */
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004099 ssl_set_timer( ssl, 0 );
4100
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004101 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4102 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004103 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004104 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004105 }
4106 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004107 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004108}
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004109
4110/*
4111 * To be called when the last message of an outgoing flight is send.
4112 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004113void mbedtls_ssl_send_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004114{
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02004115 ssl_reset_retransmit_timeout( ssl );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02004116 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004117
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004118 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4119 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004120 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004121 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004122 }
4123 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004124 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004125}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004126#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004127
Paul Bakker5121ce52009-01-03 21:22:43 +00004128/*
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004129 * Handshake layer functions
Paul Bakker5121ce52009-01-03 21:22:43 +00004130 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004131
4132/*
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004133 * Write (DTLS: or queue) current handshake (including CCS) message.
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004134 *
4135 * - fill in handshake headers
4136 * - update handshake checksum
4137 * - DTLS: save message for resending
4138 * - then pass to the record layer
4139 *
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004140 * DTLS: except for HelloRequest, messages are only queued, and will only be
4141 * actually sent when calling flight_transmit() or resend().
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004142 *
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004143 * Inputs:
4144 * - ssl->out_msglen: 4 + actual handshake message len
4145 * (4 is the size of handshake headers for TLS)
4146 * - ssl->out_msg[0]: the handshake type (ClientHello, ServerHello, etc)
4147 * - ssl->out_msg + 4: the handshake message body
4148 *
Manuel Pégourié-Gonnard065a2a32018-08-20 11:09:26 +02004149 * Outputs, ie state before passing to flight_append() or write_record():
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004150 * - ssl->out_msglen: the length of the record contents
4151 * (including handshake headers but excluding record headers)
4152 * - ssl->out_msg: the record contents (handshake headers + content)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004153 */
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004154int mbedtls_ssl_write_handshake_msg( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004155{
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004156 int ret;
4157 const size_t hs_len = ssl->out_msglen - 4;
4158 const unsigned char hs_type = ssl->out_msg[0];
Paul Bakker5121ce52009-01-03 21:22:43 +00004159
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004160 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write handshake message" ) );
4161
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004162 /*
4163 * Sanity checks
4164 */
Hanno Beckerc83d2b32018-08-22 16:05:47 +01004165 if( ssl->out_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004166 ssl->out_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
4167 {
Hanno Beckerc83d2b32018-08-22 16:05:47 +01004168 /* In SSLv3, the client might send a NoCertificate alert. */
4169#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
4170 if( ! ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
4171 ssl->out_msgtype == MBEDTLS_SSL_MSG_ALERT &&
4172 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) )
4173#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
4174 {
4175 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4176 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4177 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004178 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004179
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05004180 /* Whenever we send anything different from a
4181 * HelloRequest we should be in a handshake - double check. */
4182 if( ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4183 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) &&
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004184 ssl->handshake == NULL )
4185 {
4186 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4187 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4188 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004189
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004190#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004191 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004192 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004193 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004194 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004195 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4196 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004197 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004198#endif
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004199
Hanno Beckerb50a2532018-08-06 11:52:54 +01004200 /* Double-check that we did not exceed the bounds
4201 * of the outgoing record buffer.
4202 * This should never fail as the various message
4203 * writing functions must obey the bounds of the
4204 * outgoing record buffer, but better be safe.
4205 *
4206 * Note: We deliberately do not check for the MTU or MFL here.
4207 */
4208 if( ssl->out_msglen > MBEDTLS_SSL_OUT_CONTENT_LEN )
4209 {
4210 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record too large: "
4211 "size %u, maximum %u",
4212 (unsigned) ssl->out_msglen,
4213 (unsigned) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
4214 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4215 }
4216
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004217 /*
4218 * Fill handshake headers
4219 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004220 if( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00004221 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004222 ssl->out_msg[1] = (unsigned char)( hs_len >> 16 );
4223 ssl->out_msg[2] = (unsigned char)( hs_len >> 8 );
4224 ssl->out_msg[3] = (unsigned char)( hs_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00004225
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004226 /*
4227 * DTLS has additional fields in the Handshake layer,
4228 * between the length field and the actual payload:
4229 * uint16 message_seq;
4230 * uint24 fragment_offset;
4231 * uint24 fragment_length;
4232 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004233#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004234 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004235 {
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01004236 /* Make room for the additional DTLS fields */
Angus Grattond8213d02016-05-25 20:56:48 +10004237 if( MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen < 8 )
Hanno Becker9648f8b2017-09-18 10:55:54 +01004238 {
4239 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS handshake message too large: "
4240 "size %u, maximum %u",
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004241 (unsigned) ( hs_len ),
Angus Grattond8213d02016-05-25 20:56:48 +10004242 (unsigned) ( MBEDTLS_SSL_OUT_CONTENT_LEN - 12 ) ) );
Hanno Becker9648f8b2017-09-18 10:55:54 +01004243 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4244 }
4245
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004246 memmove( ssl->out_msg + 12, ssl->out_msg + 4, hs_len );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004247 ssl->out_msglen += 8;
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004248
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02004249 /* Write message_seq and update it, except for HelloRequest */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004250 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02004251 {
Manuel Pégourié-Gonnardd9ba0d92014-09-02 18:30:26 +02004252 ssl->out_msg[4] = ( ssl->handshake->out_msg_seq >> 8 ) & 0xFF;
4253 ssl->out_msg[5] = ( ssl->handshake->out_msg_seq ) & 0xFF;
4254 ++( ssl->handshake->out_msg_seq );
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02004255 }
4256 else
4257 {
4258 ssl->out_msg[4] = 0;
4259 ssl->out_msg[5] = 0;
4260 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01004261
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004262 /* Handshake hashes are computed without fragmentation,
4263 * so set frag_offset = 0 and frag_len = hs_len for now */
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01004264 memset( ssl->out_msg + 6, 0x00, 3 );
4265 memcpy( ssl->out_msg + 9, ssl->out_msg + 1, 3 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004266 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004267#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004268
Hanno Becker0207e532018-08-28 10:28:28 +01004269 /* Update running hashes of handshake messages seen */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004270 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
4271 ssl->handshake->update_checksum( ssl, ssl->out_msg, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00004272 }
4273
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004274 /* Either send now, or just save to be sent (and resent) later */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004275#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004276 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05004277 ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4278 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004279 {
4280 if( ( ret = ssl_flight_append( ssl ) ) != 0 )
4281 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004282 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_flight_append", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004283 return( ret );
4284 }
4285 }
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004286 else
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004287#endif
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004288 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004289 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004290 {
4291 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4292 return( ret );
4293 }
4294 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004295
4296 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write handshake message" ) );
4297
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004298 return( 0 );
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004299}
4300
4301/*
4302 * Record layer functions
4303 */
4304
4305/*
4306 * Write current record.
4307 *
4308 * Uses:
4309 * - ssl->out_msgtype: type of the message (AppData, Handshake, Alert, CCS)
4310 * - ssl->out_msglen: length of the record content (excl headers)
4311 * - ssl->out_msg: record content
4312 */
Hanno Becker67bc7c32018-08-06 11:33:50 +01004313int mbedtls_ssl_write_record( mbedtls_ssl_context *ssl, uint8_t force_flush )
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004314{
4315 int ret, done = 0;
4316 size_t len = ssl->out_msglen;
Hanno Becker67bc7c32018-08-06 11:33:50 +01004317 uint8_t flush = force_flush;
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004318
4319 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write record" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004320
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004321#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00004322 if( ssl->transform_out != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004323 ssl->session_out->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00004324 {
4325 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
4326 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004327 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00004328 return( ret );
4329 }
4330
4331 len = ssl->out_msglen;
4332 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004333#endif /*MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00004334
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004335#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
4336 if( mbedtls_ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004337 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004338 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004339
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004340 ret = mbedtls_ssl_hw_record_write( ssl );
4341 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00004342 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004343 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_write", ret );
4344 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00004345 }
Paul Bakkerc7878112012-12-19 14:41:14 +01004346
4347 if( ret == 0 )
4348 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00004349 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004350#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00004351 if( !done )
4352 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01004353 unsigned i;
4354 size_t protected_record_size;
4355
Hanno Becker6430faf2019-05-08 11:57:13 +01004356 /* Skip writing the record content type to after the encryption,
4357 * as it may change when using the CID extension. */
4358
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004359 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004360 ssl->conf->transport, ssl->out_hdr + 1 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01004361
Hanno Becker19859472018-08-06 09:40:20 +01004362 memcpy( ssl->out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01004363 ssl->out_len[0] = (unsigned char)( len >> 8 );
4364 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00004365
Paul Bakker48916f92012-09-16 19:57:18 +00004366 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00004367 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00004368 mbedtls_record rec;
4369
4370 rec.buf = ssl->out_iv;
4371 rec.buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN -
4372 ( ssl->out_iv - ssl->out_buf );
4373 rec.data_len = ssl->out_msglen;
4374 rec.data_offset = ssl->out_msg - rec.buf;
4375
4376 memcpy( &rec.ctr[0], ssl->out_ctr, 8 );
4377 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
4378 ssl->conf->transport, rec.ver );
4379 rec.type = ssl->out_msgtype;
4380
Hanno Beckera0e20d02019-05-15 14:03:01 +01004381#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker43c24b82019-05-01 09:45:57 +01004382 /* The CID is set by mbedtls_ssl_encrypt_buf(). */
Hanno Beckercab87e62019-04-29 13:52:53 +01004383 rec.cid_len = 0;
Hanno Beckera0e20d02019-05-15 14:03:01 +01004384#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckercab87e62019-04-29 13:52:53 +01004385
Hanno Beckera18d1322018-01-03 14:27:32 +00004386 if( ( ret = mbedtls_ssl_encrypt_buf( ssl, ssl->transform_out, &rec,
Hanno Becker9eddaeb2017-12-27 21:37:21 +00004387 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00004388 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004389 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
Paul Bakker05ef8352012-05-08 09:17:57 +00004390 return( ret );
4391 }
4392
Hanno Becker9eddaeb2017-12-27 21:37:21 +00004393 if( rec.data_offset != 0 )
4394 {
4395 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4396 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4397 }
4398
Hanno Becker6430faf2019-05-08 11:57:13 +01004399 /* Update the record content type and CID. */
4400 ssl->out_msgtype = rec.type;
Hanno Beckera0e20d02019-05-15 14:03:01 +01004401#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID )
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01004402 memcpy( ssl->out_cid, rec.cid, rec.cid_len );
Hanno Beckera0e20d02019-05-15 14:03:01 +01004403#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker78f839d2019-03-14 12:56:23 +00004404 ssl->out_msglen = len = rec.data_len;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00004405 ssl->out_len[0] = (unsigned char)( rec.data_len >> 8 );
4406 ssl->out_len[1] = (unsigned char)( rec.data_len );
Paul Bakker05ef8352012-05-08 09:17:57 +00004407 }
4408
Hanno Becker5903de42019-05-03 14:46:38 +01004409 protected_record_size = len + mbedtls_ssl_out_hdr_len( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01004410
4411#if defined(MBEDTLS_SSL_PROTO_DTLS)
4412 /* In case of DTLS, double-check that we don't exceed
4413 * the remaining space in the datagram. */
4414 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4415 {
Hanno Becker554b0af2018-08-22 20:33:41 +01004416 ret = ssl_get_remaining_space_in_datagram( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01004417 if( ret < 0 )
4418 return( ret );
4419
4420 if( protected_record_size > (size_t) ret )
4421 {
4422 /* Should never happen */
4423 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4424 }
4425 }
4426#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker05ef8352012-05-08 09:17:57 +00004427
Hanno Becker6430faf2019-05-08 11:57:13 +01004428 /* Now write the potentially updated record content type. */
4429 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
4430
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004431 MBEDTLS_SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01004432 "version = [%d:%d], msglen = %d",
4433 ssl->out_hdr[0], ssl->out_hdr[1],
4434 ssl->out_hdr[2], len ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00004435
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004436 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01004437 ssl->out_hdr, protected_record_size );
Hanno Becker2b1e3542018-08-06 11:19:13 +01004438
4439 ssl->out_left += protected_record_size;
4440 ssl->out_hdr += protected_record_size;
4441 ssl_update_out_pointers( ssl, ssl->transform_out );
4442
Hanno Becker04484622018-08-06 09:49:38 +01004443 for( i = 8; i > ssl_ep_len( ssl ); i-- )
4444 if( ++ssl->cur_out_ctr[i - 1] != 0 )
4445 break;
4446
4447 /* The loop goes to its end iff the counter is wrapping */
4448 if( i == ssl_ep_len( ssl ) )
4449 {
4450 MBEDTLS_SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
4451 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
4452 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004453 }
4454
Hanno Becker67bc7c32018-08-06 11:33:50 +01004455#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker47db8772018-08-21 13:32:13 +01004456 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
4457 flush == SSL_DONT_FORCE_FLUSH )
Hanno Becker67bc7c32018-08-06 11:33:50 +01004458 {
Hanno Becker1f5a15d2018-08-21 13:31:31 +01004459 size_t remaining;
4460 ret = ssl_get_remaining_payload_in_datagram( ssl );
4461 if( ret < 0 )
4462 {
4463 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_get_remaining_payload_in_datagram",
4464 ret );
4465 return( ret );
4466 }
4467
4468 remaining = (size_t) ret;
Hanno Becker67bc7c32018-08-06 11:33:50 +01004469 if( remaining == 0 )
Hanno Beckerf0da6672018-08-28 09:55:10 +01004470 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004471 flush = SSL_FORCE_FLUSH;
Hanno Beckerf0da6672018-08-28 09:55:10 +01004472 }
Hanno Becker67bc7c32018-08-06 11:33:50 +01004473 else
4474 {
Hanno Becker513815a2018-08-20 11:56:09 +01004475 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Still %u bytes available in current datagram", (unsigned) remaining ) );
Hanno Becker67bc7c32018-08-06 11:33:50 +01004476 }
4477 }
4478#endif /* MBEDTLS_SSL_PROTO_DTLS */
4479
4480 if( ( flush == SSL_FORCE_FLUSH ) &&
4481 ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004482 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004483 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004484 return( ret );
4485 }
4486
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004487 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write record" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004488
4489 return( 0 );
4490}
4491
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004492#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckere25e3b72018-08-16 09:30:53 +01004493
4494static int ssl_hs_is_proper_fragment( mbedtls_ssl_context *ssl )
4495{
4496 if( ssl->in_msglen < ssl->in_hslen ||
4497 memcmp( ssl->in_msg + 6, "\0\0\0", 3 ) != 0 ||
4498 memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 )
4499 {
4500 return( 1 );
4501 }
4502 return( 0 );
4503}
Hanno Becker44650b72018-08-16 12:51:11 +01004504
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004505static uint32_t ssl_get_hs_frag_len( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004506{
4507 return( ( ssl->in_msg[9] << 16 ) |
4508 ( ssl->in_msg[10] << 8 ) |
4509 ssl->in_msg[11] );
4510}
4511
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004512static uint32_t ssl_get_hs_frag_off( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004513{
4514 return( ( ssl->in_msg[6] << 16 ) |
4515 ( ssl->in_msg[7] << 8 ) |
4516 ssl->in_msg[8] );
4517}
4518
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004519static int ssl_check_hs_header( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004520{
4521 uint32_t msg_len, frag_off, frag_len;
4522
4523 msg_len = ssl_get_hs_total_len( ssl );
4524 frag_off = ssl_get_hs_frag_off( ssl );
4525 frag_len = ssl_get_hs_frag_len( ssl );
4526
4527 if( frag_off > msg_len )
4528 return( -1 );
4529
4530 if( frag_len > msg_len - frag_off )
4531 return( -1 );
4532
4533 if( frag_len + 12 > ssl->in_msglen )
4534 return( -1 );
4535
4536 return( 0 );
4537}
4538
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004539/*
4540 * Mark bits in bitmask (used for DTLS HS reassembly)
4541 */
4542static void ssl_bitmask_set( unsigned char *mask, size_t offset, size_t len )
4543{
4544 unsigned int start_bits, end_bits;
4545
4546 start_bits = 8 - ( offset % 8 );
4547 if( start_bits != 8 )
4548 {
4549 size_t first_byte_idx = offset / 8;
4550
Manuel Pégourié-Gonnardac030522014-09-02 14:23:40 +02004551 /* Special case */
4552 if( len <= start_bits )
4553 {
4554 for( ; len != 0; len-- )
4555 mask[first_byte_idx] |= 1 << ( start_bits - len );
4556
4557 /* Avoid potential issues with offset or len becoming invalid */
4558 return;
4559 }
4560
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004561 offset += start_bits; /* Now offset % 8 == 0 */
4562 len -= start_bits;
4563
4564 for( ; start_bits != 0; start_bits-- )
4565 mask[first_byte_idx] |= 1 << ( start_bits - 1 );
4566 }
4567
4568 end_bits = len % 8;
4569 if( end_bits != 0 )
4570 {
4571 size_t last_byte_idx = ( offset + len ) / 8;
4572
4573 len -= end_bits; /* Now len % 8 == 0 */
4574
4575 for( ; end_bits != 0; end_bits-- )
4576 mask[last_byte_idx] |= 1 << ( 8 - end_bits );
4577 }
4578
4579 memset( mask + offset / 8, 0xFF, len / 8 );
4580}
4581
4582/*
4583 * Check that bitmask is full
4584 */
4585static int ssl_bitmask_check( unsigned char *mask, size_t len )
4586{
4587 size_t i;
4588
4589 for( i = 0; i < len / 8; i++ )
4590 if( mask[i] != 0xFF )
4591 return( -1 );
4592
4593 for( i = 0; i < len % 8; i++ )
4594 if( ( mask[len / 8] & ( 1 << ( 7 - i ) ) ) == 0 )
4595 return( -1 );
4596
4597 return( 0 );
4598}
4599
Hanno Becker56e205e2018-08-16 09:06:12 +01004600/* msg_len does not include the handshake header */
Hanno Becker65dc8852018-08-23 09:40:49 +01004601static size_t ssl_get_reassembly_buffer_size( size_t msg_len,
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004602 unsigned add_bitmap )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004603{
Hanno Becker56e205e2018-08-16 09:06:12 +01004604 size_t alloc_len;
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004605
Hanno Becker56e205e2018-08-16 09:06:12 +01004606 alloc_len = 12; /* Handshake header */
4607 alloc_len += msg_len; /* Content buffer */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004608
Hanno Beckerd07df862018-08-16 09:14:58 +01004609 if( add_bitmap )
4610 alloc_len += msg_len / 8 + ( msg_len % 8 != 0 ); /* Bitmap */
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004611
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004612 return( alloc_len );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004613}
Hanno Becker56e205e2018-08-16 09:06:12 +01004614
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004615#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004616
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004617static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl )
Hanno Becker12555c62018-08-16 12:47:53 +01004618{
4619 return( ( ssl->in_msg[1] << 16 ) |
4620 ( ssl->in_msg[2] << 8 ) |
4621 ssl->in_msg[3] );
4622}
Hanno Beckere25e3b72018-08-16 09:30:53 +01004623
Simon Butcher99000142016-10-13 17:21:01 +01004624int mbedtls_ssl_prepare_handshake_record( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004625{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004626 if( ssl->in_msglen < mbedtls_ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004627 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004628 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too short: %d",
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004629 ssl->in_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004630 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004631 }
4632
Hanno Becker12555c62018-08-16 12:47:53 +01004633 ssl->in_hslen = mbedtls_ssl_hs_hdr_len( ssl ) + ssl_get_hs_total_len( ssl );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004634
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004635 MBEDTLS_SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004636 " %d, type = %d, hslen = %d",
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004637 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004638
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004639#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004640 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004641 {
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004642 int ret;
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004643 unsigned int recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004644
Hanno Becker44650b72018-08-16 12:51:11 +01004645 if( ssl_check_hs_header( ssl ) != 0 )
4646 {
4647 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid handshake header" ) );
4648 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4649 }
4650
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004651 if( ssl->handshake != NULL &&
Hanno Beckerc76c6192017-06-06 10:03:17 +01004652 ( ( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER &&
4653 recv_msg_seq != ssl->handshake->in_msg_seq ) ||
4654 ( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
4655 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO ) ) )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004656 {
Hanno Becker9e1ec222018-08-15 15:54:43 +01004657 if( recv_msg_seq > ssl->handshake->in_msg_seq )
4658 {
4659 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received future handshake message of sequence number %u (next %u)",
4660 recv_msg_seq,
4661 ssl->handshake->in_msg_seq ) );
4662 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4663 }
4664
Manuel Pégourié-Gonnardfc572dd2014-10-09 17:56:57 +02004665 /* Retransmit only on last message from previous flight, to avoid
4666 * too many retransmissions.
4667 * Besides, No sane server ever retransmits HelloVerifyRequest */
4668 if( recv_msg_seq == ssl->handshake->in_flight_start_seq - 1 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004669 ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004670 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004671 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received message from last flight, "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004672 "message_seq = %d, start_of_flight = %d",
4673 recv_msg_seq,
4674 ssl->handshake->in_flight_start_seq ) );
4675
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004676 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004677 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004678 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004679 return( ret );
4680 }
4681 }
4682 else
4683 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004684 MBEDTLS_SSL_DEBUG_MSG( 2, ( "dropping out-of-sequence message: "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004685 "message_seq = %d, expected = %d",
4686 recv_msg_seq,
4687 ssl->handshake->in_msg_seq ) );
4688 }
4689
Hanno Becker90333da2017-10-10 11:27:13 +01004690 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004691 }
4692 /* Wait until message completion to increment in_msg_seq */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004693
Hanno Becker6d97ef52018-08-16 13:09:04 +01004694 /* Message reassembly is handled alongside buffering of future
4695 * messages; the commonality is that both handshake fragments and
Hanno Becker83ab41c2018-08-28 17:19:38 +01004696 * future messages cannot be forwarded immediately to the
Hanno Becker6d97ef52018-08-16 13:09:04 +01004697 * handshake logic layer. */
Hanno Beckere25e3b72018-08-16 09:30:53 +01004698 if( ssl_hs_is_proper_fragment( ssl ) == 1 )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004699 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004700 MBEDTLS_SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) );
Hanno Becker6d97ef52018-08-16 13:09:04 +01004701 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004702 }
4703 }
4704 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004705#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004706 /* With TLS we don't handle fragmentation (for now) */
4707 if( ssl->in_msglen < ssl->in_hslen )
4708 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004709 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) );
4710 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004711 }
4712
Simon Butcher99000142016-10-13 17:21:01 +01004713 return( 0 );
4714}
4715
4716void mbedtls_ssl_update_handshake_status( mbedtls_ssl_context *ssl )
4717{
Hanno Becker0271f962018-08-16 13:23:47 +01004718 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Simon Butcher99000142016-10-13 17:21:01 +01004719
Hanno Becker0271f962018-08-16 13:23:47 +01004720 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER && hs != NULL )
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02004721 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004722 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02004723 }
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004724
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004725 /* Handshake message is complete, increment counter */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004726#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004727 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004728 ssl->handshake != NULL )
4729 {
Hanno Becker0271f962018-08-16 13:23:47 +01004730 unsigned offset;
4731 mbedtls_ssl_hs_buffer *hs_buf;
Hanno Beckere25e3b72018-08-16 09:30:53 +01004732
Hanno Becker0271f962018-08-16 13:23:47 +01004733 /* Increment handshake sequence number */
4734 hs->in_msg_seq++;
4735
4736 /*
4737 * Clear up handshake buffering and reassembly structure.
4738 */
4739
4740 /* Free first entry */
Hanno Beckere605b192018-08-21 15:59:07 +01004741 ssl_buffering_free_slot( ssl, 0 );
Hanno Becker0271f962018-08-16 13:23:47 +01004742
4743 /* Shift all other entries */
Hanno Beckere605b192018-08-21 15:59:07 +01004744 for( offset = 0, hs_buf = &hs->buffering.hs[0];
4745 offset + 1 < MBEDTLS_SSL_MAX_BUFFERED_HS;
Hanno Becker0271f962018-08-16 13:23:47 +01004746 offset++, hs_buf++ )
4747 {
4748 *hs_buf = *(hs_buf + 1);
4749 }
4750
4751 /* Create a fresh last entry */
4752 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004753 }
4754#endif
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004755}
4756
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004757/*
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004758 * DTLS anti-replay: RFC 6347 4.1.2.6
4759 *
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004760 * in_window is a field of bits numbered from 0 (lsb) to 63 (msb).
4761 * Bit n is set iff record number in_window_top - n has been seen.
4762 *
4763 * Usually, in_window_top is the last record number seen and the lsb of
4764 * in_window is set. The only exception is the initial state (record number 0
4765 * not seen yet).
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004766 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004767#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4768static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004769{
4770 ssl->in_window_top = 0;
4771 ssl->in_window = 0;
4772}
4773
4774static inline uint64_t ssl_load_six_bytes( unsigned char *buf )
4775{
4776 return( ( (uint64_t) buf[0] << 40 ) |
4777 ( (uint64_t) buf[1] << 32 ) |
4778 ( (uint64_t) buf[2] << 24 ) |
4779 ( (uint64_t) buf[3] << 16 ) |
4780 ( (uint64_t) buf[4] << 8 ) |
4781 ( (uint64_t) buf[5] ) );
4782}
4783
4784/*
4785 * Return 0 if sequence number is acceptable, -1 otherwise
4786 */
Hanno Becker0183d692019-07-12 08:50:37 +01004787int mbedtls_ssl_dtls_replay_check( mbedtls_ssl_context const *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004788{
4789 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
4790 uint64_t bit;
4791
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004792 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004793 return( 0 );
4794
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004795 if( rec_seqnum > ssl->in_window_top )
4796 return( 0 );
4797
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004798 bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004799
4800 if( bit >= 64 )
4801 return( -1 );
4802
4803 if( ( ssl->in_window & ( (uint64_t) 1 << bit ) ) != 0 )
4804 return( -1 );
4805
4806 return( 0 );
4807}
4808
4809/*
4810 * Update replay window on new validated record
4811 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004812void mbedtls_ssl_dtls_replay_update( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004813{
4814 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
4815
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004816 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004817 return;
4818
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004819 if( rec_seqnum > ssl->in_window_top )
4820 {
4821 /* Update window_top and the contents of the window */
4822 uint64_t shift = rec_seqnum - ssl->in_window_top;
4823
4824 if( shift >= 64 )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004825 ssl->in_window = 1;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004826 else
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004827 {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004828 ssl->in_window <<= shift;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004829 ssl->in_window |= 1;
4830 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004831
4832 ssl->in_window_top = rec_seqnum;
4833 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004834 else
4835 {
4836 /* Mark that number as seen in the current window */
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004837 uint64_t bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004838
4839 if( bit < 64 ) /* Always true, but be extra sure */
4840 ssl->in_window |= (uint64_t) 1 << bit;
4841 }
4842}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004843#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004844
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004845#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004846/* Forward declaration */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02004847static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial );
4848
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004849/*
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004850 * Without any SSL context, check if a datagram looks like a ClientHello with
4851 * a valid cookie, and if it doesn't, generate a HelloVerifyRequest message.
Simon Butcher0789aed2015-09-11 17:15:17 +01004852 * Both input and output include full DTLS headers.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004853 *
4854 * - if cookie is valid, return 0
4855 * - if ClientHello looks superficially valid but cookie is not,
4856 * fill obuf and set olen, then
4857 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
4858 * - otherwise return a specific error code
4859 */
4860static int ssl_check_dtls_clihlo_cookie(
4861 mbedtls_ssl_cookie_write_t *f_cookie_write,
4862 mbedtls_ssl_cookie_check_t *f_cookie_check,
4863 void *p_cookie,
4864 const unsigned char *cli_id, size_t cli_id_len,
4865 const unsigned char *in, size_t in_len,
4866 unsigned char *obuf, size_t buf_len, size_t *olen )
4867{
4868 size_t sid_len, cookie_len;
4869 unsigned char *p;
4870
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004871 /*
4872 * Structure of ClientHello with record and handshake headers,
4873 * and expected values. We don't need to check a lot, more checks will be
4874 * done when actually parsing the ClientHello - skipping those checks
4875 * avoids code duplication and does not make cookie forging any easier.
4876 *
4877 * 0-0 ContentType type; copied, must be handshake
4878 * 1-2 ProtocolVersion version; copied
4879 * 3-4 uint16 epoch; copied, must be 0
4880 * 5-10 uint48 sequence_number; copied
4881 * 11-12 uint16 length; (ignored)
4882 *
4883 * 13-13 HandshakeType msg_type; (ignored)
4884 * 14-16 uint24 length; (ignored)
4885 * 17-18 uint16 message_seq; copied
4886 * 19-21 uint24 fragment_offset; copied, must be 0
4887 * 22-24 uint24 fragment_length; (ignored)
4888 *
4889 * 25-26 ProtocolVersion client_version; (ignored)
4890 * 27-58 Random random; (ignored)
4891 * 59-xx SessionID session_id; 1 byte len + sid_len content
4892 * 60+ opaque cookie<0..2^8-1>; 1 byte len + content
4893 * ...
4894 *
4895 * Minimum length is 61 bytes.
4896 */
4897 if( in_len < 61 ||
4898 in[0] != MBEDTLS_SSL_MSG_HANDSHAKE ||
4899 in[3] != 0 || in[4] != 0 ||
4900 in[19] != 0 || in[20] != 0 || in[21] != 0 )
4901 {
4902 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4903 }
4904
4905 sid_len = in[59];
4906 if( sid_len > in_len - 61 )
4907 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4908
4909 cookie_len = in[60 + sid_len];
4910 if( cookie_len > in_len - 60 )
4911 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4912
4913 if( f_cookie_check( p_cookie, in + sid_len + 61, cookie_len,
4914 cli_id, cli_id_len ) == 0 )
4915 {
4916 /* Valid cookie */
4917 return( 0 );
4918 }
4919
4920 /*
4921 * If we get here, we've got an invalid cookie, let's prepare HVR.
4922 *
4923 * 0-0 ContentType type; copied
4924 * 1-2 ProtocolVersion version; copied
4925 * 3-4 uint16 epoch; copied
4926 * 5-10 uint48 sequence_number; copied
4927 * 11-12 uint16 length; olen - 13
4928 *
4929 * 13-13 HandshakeType msg_type; hello_verify_request
4930 * 14-16 uint24 length; olen - 25
4931 * 17-18 uint16 message_seq; copied
4932 * 19-21 uint24 fragment_offset; copied
4933 * 22-24 uint24 fragment_length; olen - 25
4934 *
4935 * 25-26 ProtocolVersion server_version; 0xfe 0xff
4936 * 27-27 opaque cookie<0..2^8-1>; cookie_len = olen - 27, cookie
4937 *
4938 * Minimum length is 28.
4939 */
4940 if( buf_len < 28 )
4941 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
4942
4943 /* Copy most fields and adapt others */
4944 memcpy( obuf, in, 25 );
4945 obuf[13] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST;
4946 obuf[25] = 0xfe;
4947 obuf[26] = 0xff;
4948
4949 /* Generate and write actual cookie */
4950 p = obuf + 28;
4951 if( f_cookie_write( p_cookie,
4952 &p, obuf + buf_len, cli_id, cli_id_len ) != 0 )
4953 {
4954 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4955 }
4956
4957 *olen = p - obuf;
4958
4959 /* Go back and fill length fields */
4960 obuf[27] = (unsigned char)( *olen - 28 );
4961
4962 obuf[14] = obuf[22] = (unsigned char)( ( *olen - 25 ) >> 16 );
4963 obuf[15] = obuf[23] = (unsigned char)( ( *olen - 25 ) >> 8 );
4964 obuf[16] = obuf[24] = (unsigned char)( ( *olen - 25 ) );
4965
4966 obuf[11] = (unsigned char)( ( *olen - 13 ) >> 8 );
4967 obuf[12] = (unsigned char)( ( *olen - 13 ) );
4968
4969 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
4970}
4971
4972/*
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004973 * Handle possible client reconnect with the same UDP quadruplet
4974 * (RFC 6347 Section 4.2.8).
4975 *
4976 * Called by ssl_parse_record_header() in case we receive an epoch 0 record
4977 * that looks like a ClientHello.
4978 *
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004979 * - if the input looks like a ClientHello without cookies,
4980 * send back HelloVerifyRequest, then
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004981 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004982 * - if the input looks like a ClientHello with a valid cookie,
4983 * reset the session of the current context, and
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02004984 * return MBEDTLS_ERR_SSL_CLIENT_RECONNECT
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004985 * - if anything goes wrong, return a specific error code
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004986 *
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004987 * mbedtls_ssl_read_record() will ignore the record if anything else than
Simon Butcherd0bf6a32015-09-11 17:34:49 +01004988 * MBEDTLS_ERR_SSL_CLIENT_RECONNECT or 0 is returned, although this function
4989 * cannot not return 0.
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004990 */
4991static int ssl_handle_possible_reconnect( mbedtls_ssl_context *ssl )
4992{
4993 int ret;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004994 size_t len;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004995
Hanno Becker2fddd372019-07-10 14:37:41 +01004996 if( ssl->conf->f_cookie_write == NULL ||
4997 ssl->conf->f_cookie_check == NULL )
4998 {
4999 /* If we can't use cookies to verify reachability of the peer,
5000 * drop the record. */
5001 return( 0 );
5002 }
5003
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005004 ret = ssl_check_dtls_clihlo_cookie(
5005 ssl->conf->f_cookie_write,
5006 ssl->conf->f_cookie_check,
5007 ssl->conf->p_cookie,
5008 ssl->cli_id, ssl->cli_id_len,
5009 ssl->in_buf, ssl->in_left,
Angus Grattond8213d02016-05-25 20:56:48 +10005010 ssl->out_buf, MBEDTLS_SSL_OUT_CONTENT_LEN, &len );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005011
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005012 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_check_dtls_clihlo_cookie", ret );
5013
5014 if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005015 {
Brian J Murray1903fb32016-11-06 04:45:15 -08005016 /* Don't check write errors as we can't do anything here.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005017 * If the error is permanent we'll catch it later,
5018 * if it's not, then hopefully it'll work next time. */
5019 (void) ssl->f_send( ssl->p_bio, ssl->out_buf, len );
Hanno Becker2fddd372019-07-10 14:37:41 +01005020 ret = 0;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005021 }
5022
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005023 if( ret == 0 )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005024 {
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005025 /* Got a valid cookie, partially reset context */
5026 if( ( ret = ssl_session_reset_int( ssl, 1 ) ) != 0 )
5027 {
5028 MBEDTLS_SSL_DEBUG_RET( 1, "reset", ret );
5029 return( ret );
5030 }
5031
5032 return( MBEDTLS_ERR_SSL_CLIENT_RECONNECT );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005033 }
5034
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005035 return( ret );
5036}
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02005037#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005038
Hanno Beckerf661c9c2019-05-03 13:25:54 +01005039static int ssl_check_record_type( uint8_t record_type )
5040{
5041 if( record_type != MBEDTLS_SSL_MSG_HANDSHAKE &&
5042 record_type != MBEDTLS_SSL_MSG_ALERT &&
5043 record_type != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC &&
5044 record_type != MBEDTLS_SSL_MSG_APPLICATION_DATA )
5045 {
5046 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5047 }
5048
5049 return( 0 );
5050}
5051
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02005052/*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005053 * ContentType type;
5054 * ProtocolVersion version;
5055 * uint16 epoch; // DTLS only
5056 * uint48 sequence_number; // DTLS only
5057 * uint16 length;
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005058 *
5059 * Return 0 if header looks sane (and, for DTLS, the record is expected)
Simon Butcher207990d2015-12-16 01:51:30 +00005060 * MBEDTLS_ERR_SSL_INVALID_RECORD if the header looks bad,
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005061 * MBEDTLS_ERR_SSL_UNEXPECTED_RECORD (DTLS only) if sane but unexpected.
5062 *
5063 * With DTLS, mbedtls_ssl_read_record() will:
Simon Butcher207990d2015-12-16 01:51:30 +00005064 * 1. proceed with the record if this function returns 0
5065 * 2. drop only the current record if this function returns UNEXPECTED_RECORD
5066 * 3. return CLIENT_RECONNECT if this function return that value
5067 * 4. drop the whole datagram if this function returns anything else.
5068 * Point 2 is needed when the peer is resending, and we have already received
5069 * the first record from a datagram but are still waiting for the others.
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005070 */
Hanno Becker331de3d2019-07-12 11:10:16 +01005071static int ssl_parse_record_header( mbedtls_ssl_context const *ssl,
Hanno Beckere5e7e782019-07-11 12:29:35 +01005072 unsigned char *buf,
5073 size_t len,
5074 mbedtls_record *rec )
Paul Bakker5121ce52009-01-03 21:22:43 +00005075{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005076 int major_ver, minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00005077
Hanno Beckere5e7e782019-07-11 12:29:35 +01005078 size_t const rec_hdr_type_offset = 0;
5079 size_t const rec_hdr_type_len = 1;
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02005080
Hanno Beckere5e7e782019-07-11 12:29:35 +01005081 size_t const rec_hdr_version_offset = rec_hdr_type_offset +
5082 rec_hdr_type_len;
5083 size_t const rec_hdr_version_len = 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00005084
Hanno Beckere5e7e782019-07-11 12:29:35 +01005085 size_t const rec_hdr_ctr_len = 8;
5086#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckerf5466252019-07-25 10:13:02 +01005087 uint32_t rec_epoch;
Hanno Beckere5e7e782019-07-11 12:29:35 +01005088 size_t const rec_hdr_ctr_offset = rec_hdr_version_offset +
5089 rec_hdr_version_len;
5090
Hanno Beckera0e20d02019-05-15 14:03:01 +01005091#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckere5e7e782019-07-11 12:29:35 +01005092 size_t const rec_hdr_cid_offset = rec_hdr_ctr_offset +
5093 rec_hdr_ctr_len;
Hanno Beckerf5466252019-07-25 10:13:02 +01005094 size_t rec_hdr_cid_len = 0;
Hanno Beckere5e7e782019-07-11 12:29:35 +01005095#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
5096#endif /* MBEDTLS_SSL_PROTO_DTLS */
5097
5098 size_t rec_hdr_len_offset; /* To be determined */
5099 size_t const rec_hdr_len_len = 2;
5100
5101 /*
5102 * Check minimum lengths for record header.
5103 */
5104
5105#if defined(MBEDTLS_SSL_PROTO_DTLS)
5106 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5107 {
5108 rec_hdr_len_offset = rec_hdr_ctr_offset + rec_hdr_ctr_len;
5109 }
5110 else
5111#endif /* MBEDTLS_SSL_PROTO_DTLS */
5112 {
5113 rec_hdr_len_offset = rec_hdr_version_offset + rec_hdr_version_len;
5114 }
5115
5116 if( len < rec_hdr_len_offset + rec_hdr_len_len )
5117 {
5118 MBEDTLS_SSL_DEBUG_MSG( 1, ( "datagram of length %u too small to hold DTLS record header of length %u",
5119 (unsigned) len,
5120 (unsigned)( rec_hdr_len_len + rec_hdr_len_len ) ) );
5121 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5122 }
5123
5124 /*
5125 * Parse and validate record content type
5126 */
5127
5128 rec->type = buf[ rec_hdr_type_offset ];
Hanno Beckere5e7e782019-07-11 12:29:35 +01005129
5130 /* Check record content type */
5131#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
5132 rec->cid_len = 0;
5133
Hanno Beckerca59c2b2019-05-08 12:03:28 +01005134 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Hanno Beckere5e7e782019-07-11 12:29:35 +01005135 ssl->conf->cid_len != 0 &&
5136 rec->type == MBEDTLS_SSL_MSG_CID )
Hanno Beckerca59c2b2019-05-08 12:03:28 +01005137 {
5138 /* Shift pointers to account for record header including CID
5139 * struct {
5140 * ContentType special_type = tls12_cid;
5141 * ProtocolVersion version;
5142 * uint16 epoch;
5143 * uint48 sequence_number;
Hanno Becker8e55b0f2019-05-23 17:03:19 +01005144 * opaque cid[cid_length]; // Additional field compared to
5145 * // default DTLS record format
Hanno Beckerca59c2b2019-05-08 12:03:28 +01005146 * uint16 length;
5147 * opaque enc_content[DTLSCiphertext.length];
5148 * } DTLSCiphertext;
5149 */
5150
5151 /* So far, we only support static CID lengths
5152 * fixed in the configuration. */
Hanno Beckere5e7e782019-07-11 12:29:35 +01005153 rec_hdr_cid_len = ssl->conf->cid_len;
5154 rec_hdr_len_offset += rec_hdr_cid_len;
Hanno Beckere538d822019-07-10 14:50:10 +01005155
Hanno Beckere5e7e782019-07-11 12:29:35 +01005156 if( len < rec_hdr_len_offset + rec_hdr_len_len )
Hanno Beckere538d822019-07-10 14:50:10 +01005157 {
Hanno Beckere5e7e782019-07-11 12:29:35 +01005158 MBEDTLS_SSL_DEBUG_MSG( 1, ( "datagram of length %u too small to hold DTLS record header including CID, length %u",
5159 (unsigned) len,
5160 (unsigned)( rec_hdr_len_offset + rec_hdr_len_len ) ) );
Hanno Becker59be60e2019-07-10 14:53:43 +01005161 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Beckere538d822019-07-10 14:50:10 +01005162 }
Hanno Beckere5e7e782019-07-11 12:29:35 +01005163
Manuel Pégourié-Gonnard7e821b52019-08-02 10:17:15 +02005164 /* configured CID len is guaranteed at most 255, see
5165 * MBEDTLS_SSL_CID_OUT_LEN_MAX in check_config.h */
5166 rec->cid_len = (uint8_t) rec_hdr_cid_len;
Hanno Beckere5e7e782019-07-11 12:29:35 +01005167 memcpy( rec->cid, buf + rec_hdr_cid_offset, rec_hdr_cid_len );
Hanno Beckerca59c2b2019-05-08 12:03:28 +01005168 }
5169 else
Hanno Beckera0e20d02019-05-15 14:03:01 +01005170#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02005171 {
Hanno Beckere5e7e782019-07-11 12:29:35 +01005172 if( ssl_check_record_type( rec->type ) )
5173 {
Hanno Becker54229812019-07-12 14:40:00 +01005174 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type %u",
5175 (unsigned) rec->type ) );
Hanno Beckere5e7e782019-07-11 12:29:35 +01005176 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5177 }
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02005178 }
5179
Hanno Beckere5e7e782019-07-11 12:29:35 +01005180 /*
5181 * Parse and validate record version
5182 */
5183
Hanno Beckerd0b66d02019-07-26 08:07:03 +01005184 rec->ver[0] = buf[ rec_hdr_version_offset + 0 ];
5185 rec->ver[1] = buf[ rec_hdr_version_offset + 1 ];
Hanno Beckere5e7e782019-07-11 12:29:35 +01005186 mbedtls_ssl_read_version( &major_ver, &minor_ver,
5187 ssl->conf->transport,
Hanno Beckerd0b66d02019-07-26 08:07:03 +01005188 &rec->ver[0] );
Hanno Beckere5e7e782019-07-11 12:29:35 +01005189
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005190 if( major_ver != ssl->major_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00005191 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005192 MBEDTLS_SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
5193 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00005194 }
5195
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005196 if( minor_ver > ssl->conf->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00005197 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005198 MBEDTLS_SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
5199 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00005200 }
5201
Hanno Beckere5e7e782019-07-11 12:29:35 +01005202 /*
5203 * Parse/Copy record sequence number.
5204 */
Hanno Beckerca59c2b2019-05-08 12:03:28 +01005205
Hanno Beckere5e7e782019-07-11 12:29:35 +01005206#if defined(MBEDTLS_SSL_PROTO_DTLS)
5207 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02005208 {
Hanno Beckere5e7e782019-07-11 12:29:35 +01005209 /* Copy explicit record sequence number from input buffer. */
5210 memcpy( &rec->ctr[0], buf + rec_hdr_ctr_offset,
5211 rec_hdr_ctr_len );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02005212 }
Hanno Beckere5e7e782019-07-11 12:29:35 +01005213 else
5214#endif /* MBEDTLS_SSL_PROTO_DTLS */
5215 {
5216 /* Copy implicit record sequence number from SSL context structure. */
5217 memcpy( &rec->ctr[0], ssl->in_ctr, rec_hdr_ctr_len );
5218 }
Paul Bakker40e46942009-01-03 21:51:57 +00005219
Hanno Beckere5e7e782019-07-11 12:29:35 +01005220 /*
5221 * Parse record length.
5222 */
5223
Hanno Beckere5e7e782019-07-11 12:29:35 +01005224 rec->data_offset = rec_hdr_len_offset + rec_hdr_len_len;
Hanno Becker9eca2762019-07-25 10:16:37 +01005225 rec->data_len = ( (size_t) buf[ rec_hdr_len_offset + 0 ] << 8 ) |
5226 ( (size_t) buf[ rec_hdr_len_offset + 1 ] << 0 );
Hanno Beckere5e7e782019-07-11 12:29:35 +01005227 MBEDTLS_SSL_DEBUG_BUF( 4, "input record header", buf, rec->data_offset );
Paul Bakker5121ce52009-01-03 21:22:43 +00005228
Hanno Beckerca59c2b2019-05-08 12:03:28 +01005229 MBEDTLS_SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
Hanno Becker92d30f52019-05-23 17:03:44 +01005230 "version = [%d:%d], msglen = %d",
Hanno Beckere5e7e782019-07-11 12:29:35 +01005231 rec->type,
5232 major_ver, minor_ver, rec->data_len ) );
5233
5234 rec->buf = buf;
5235 rec->buf_len = rec->data_offset + rec->data_len;
Hanno Beckerca59c2b2019-05-08 12:03:28 +01005236
Hanno Beckerd417cc92019-07-26 08:20:27 +01005237 if( rec->data_len == 0 )
5238 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00005239
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005240 /*
Hanno Becker52c6dc62017-05-26 16:07:36 +01005241 * DTLS-related tests.
5242 * Check epoch before checking length constraint because
5243 * the latter varies with the epoch. E.g., if a ChangeCipherSpec
5244 * message gets duplicated before the corresponding Finished message,
5245 * the second ChangeCipherSpec should be discarded because it belongs
5246 * to an old epoch, but not because its length is shorter than
5247 * the minimum record length for packets using the new record transform.
5248 * Note that these two kinds of failures are handled differently,
5249 * as an unexpected record is silently skipped but an invalid
5250 * record leads to the entire datagram being dropped.
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005251 */
5252#if defined(MBEDTLS_SSL_PROTO_DTLS)
5253 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5254 {
Hanno Beckere5e7e782019-07-11 12:29:35 +01005255 rec_epoch = ( rec->ctr[0] << 8 ) | rec->ctr[1];
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005256
Hanno Becker955a5c92019-07-10 17:12:07 +01005257 /* Check that the datagram is large enough to contain a record
5258 * of the advertised length. */
Hanno Beckere5e7e782019-07-11 12:29:35 +01005259 if( len < rec->data_offset + rec->data_len )
Hanno Becker955a5c92019-07-10 17:12:07 +01005260 {
Hanno Beckere5e7e782019-07-11 12:29:35 +01005261 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Datagram of length %u too small to contain record of advertised length %u.",
5262 (unsigned) len,
5263 (unsigned)( rec->data_offset + rec->data_len ) ) );
Hanno Becker955a5c92019-07-10 17:12:07 +01005264 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5265 }
Hanno Becker37cfe732019-07-10 17:20:01 +01005266
Hanno Becker37cfe732019-07-10 17:20:01 +01005267 /* Records from other, non-matching epochs are silently discarded.
5268 * (The case of same-port Client reconnects must be considered in
5269 * the caller). */
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005270 if( rec_epoch != ssl->in_epoch )
5271 {
5272 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record from another epoch: "
5273 "expected %d, received %d",
5274 ssl->in_epoch, rec_epoch ) );
5275
Hanno Becker552f7472019-07-19 10:59:12 +01005276 /* Records from the next epoch are considered for buffering
5277 * (concretely: early Finished messages). */
5278 if( rec_epoch == (unsigned) ssl->in_epoch + 1 )
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005279 {
Hanno Becker552f7472019-07-19 10:59:12 +01005280 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Consider record for buffering" ) );
5281 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005282 }
Hanno Becker5f066e72018-08-16 14:56:31 +01005283
Hanno Becker2fddd372019-07-10 14:37:41 +01005284 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005285 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005286#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Hanno Becker37cfe732019-07-10 17:20:01 +01005287 /* For records from the correct epoch, check whether their
5288 * sequence number has been seen before. */
Hanno Becker2fddd372019-07-10 14:37:41 +01005289 else if( mbedtls_ssl_dtls_replay_check( ssl ) != 0 )
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005290 {
5291 MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record" ) );
5292 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
5293 }
5294#endif
5295 }
5296#endif /* MBEDTLS_SSL_PROTO_DTLS */
5297
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005298 return( 0 );
5299}
Paul Bakker5121ce52009-01-03 21:22:43 +00005300
Paul Bakker5121ce52009-01-03 21:22:43 +00005301
Hanno Becker2fddd372019-07-10 14:37:41 +01005302#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
5303static int ssl_check_client_reconnect( mbedtls_ssl_context *ssl )
5304{
5305 unsigned int rec_epoch = ( ssl->in_ctr[0] << 8 ) | ssl->in_ctr[1];
5306
5307 /*
5308 * Check for an epoch 0 ClientHello. We can't use in_msg here to
5309 * access the first byte of record content (handshake type), as we
5310 * have an active transform (possibly iv_len != 0), so use the
5311 * fact that the record header len is 13 instead.
5312 */
5313 if( rec_epoch == 0 &&
5314 ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
5315 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
5316 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
5317 ssl->in_left > 13 &&
5318 ssl->in_buf[13] == MBEDTLS_SSL_HS_CLIENT_HELLO )
5319 {
5320 MBEDTLS_SSL_DEBUG_MSG( 1, ( "possible client reconnect "
5321 "from the same port" ) );
5322 return( ssl_handle_possible_reconnect( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005323 }
5324
5325 return( 0 );
5326}
Hanno Becker2fddd372019-07-10 14:37:41 +01005327#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00005328
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005329/*
5330 * If applicable, decrypt (and decompress) record content
5331 */
Hanno Beckerfdf66042019-07-11 13:07:45 +01005332static int ssl_prepare_record_content( mbedtls_ssl_context *ssl,
5333 mbedtls_record *rec )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005334{
5335 int ret, done = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02005336
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005337 MBEDTLS_SSL_DEBUG_BUF( 4, "input record from network",
Hanno Beckerfdf66042019-07-11 13:07:45 +01005338 rec->buf, rec->buf_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00005339
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005340#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
5341 if( mbedtls_ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00005342 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005343 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_read()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00005344
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005345 ret = mbedtls_ssl_hw_record_read( ssl );
5346 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00005347 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005348 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_read", ret );
5349 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00005350 }
Paul Bakkerc7878112012-12-19 14:41:14 +01005351
5352 if( ret == 0 )
5353 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00005354 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005355#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00005356 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005357 {
Hanno Becker58ef0bf2019-07-12 09:35:58 +01005358 unsigned char const old_msg_type = rec->type;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00005359
Hanno Beckera18d1322018-01-03 14:27:32 +00005360 if( ( ret = mbedtls_ssl_decrypt_buf( ssl, ssl->transform_in,
Hanno Beckerfdf66042019-07-11 13:07:45 +01005361 rec ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005362 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005363 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
Hanno Becker8367ccc2019-05-14 11:30:10 +01005364
Hanno Beckera0e20d02019-05-15 14:03:01 +01005365#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker8367ccc2019-05-14 11:30:10 +01005366 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_CID &&
5367 ssl->conf->ignore_unexpected_cid
5368 == MBEDTLS_SSL_UNEXPECTED_CID_IGNORE )
5369 {
Hanno Beckere8d6afd2019-05-24 10:11:06 +01005370 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ignoring unexpected CID" ) );
Hanno Becker16ded982019-05-08 13:02:55 +01005371 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
Hanno Becker8367ccc2019-05-14 11:30:10 +01005372 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01005373#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker16ded982019-05-08 13:02:55 +01005374
Paul Bakker5121ce52009-01-03 21:22:43 +00005375 return( ret );
5376 }
5377
Hanno Becker58ef0bf2019-07-12 09:35:58 +01005378 if( old_msg_type != rec->type )
Hanno Becker6430faf2019-05-08 11:57:13 +01005379 {
5380 MBEDTLS_SSL_DEBUG_MSG( 4, ( "record type after decrypt (before %d): %d",
Hanno Becker58ef0bf2019-07-12 09:35:58 +01005381 old_msg_type, rec->type ) );
Hanno Becker6430faf2019-05-08 11:57:13 +01005382 }
5383
Hanno Becker1c0c37f2018-08-07 14:29:29 +01005384 MBEDTLS_SSL_DEBUG_BUF( 4, "input payload after decrypt",
Hanno Becker58ef0bf2019-07-12 09:35:58 +01005385 rec->buf + rec->data_offset, rec->data_len );
Hanno Becker1c0c37f2018-08-07 14:29:29 +01005386
Hanno Beckera0e20d02019-05-15 14:03:01 +01005387#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker6430faf2019-05-08 11:57:13 +01005388 /* We have already checked the record content type
5389 * in ssl_parse_record_header(), failing or silently
5390 * dropping the record in the case of an unknown type.
5391 *
5392 * Since with the use of CIDs, the record content type
5393 * might change during decryption, re-check the record
5394 * content type, but treat a failure as fatal this time. */
Hanno Becker58ef0bf2019-07-12 09:35:58 +01005395 if( ssl_check_record_type( rec->type ) )
Hanno Becker6430faf2019-05-08 11:57:13 +01005396 {
5397 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
5398 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5399 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01005400#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker6430faf2019-05-08 11:57:13 +01005401
Hanno Becker58ef0bf2019-07-12 09:35:58 +01005402 if( rec->data_len == 0 )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00005403 {
5404#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5405 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3
Hanno Becker58ef0bf2019-07-12 09:35:58 +01005406 && rec->type != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00005407 {
5408 /* TLS v1.2 explicitly disallows zero-length messages which are not application data */
5409 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid zero-length message type: %d", ssl->in_msgtype ) );
5410 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5411 }
5412#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
5413
5414 ssl->nb_zero++;
5415
5416 /*
5417 * Three or more empty messages may be a DoS attack
5418 * (excessive CPU consumption).
5419 */
5420 if( ssl->nb_zero > 3 )
5421 {
5422 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
Hanno Becker6e7700d2019-05-08 10:38:32 +01005423 "messages, possible DoS attack" ) );
5424 /* Treat the records as if they were not properly authenticated,
5425 * thereby failing the connection if we see more than allowed
5426 * by the configured bad MAC threshold. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00005427 return( MBEDTLS_ERR_SSL_INVALID_MAC );
5428 }
5429 }
5430 else
5431 ssl->nb_zero = 0;
5432
5433#if defined(MBEDTLS_SSL_PROTO_DTLS)
5434 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5435 {
5436 ; /* in_ctr read from peer, not maintained internally */
5437 }
5438 else
5439#endif
5440 {
5441 unsigned i;
5442 for( i = 8; i > ssl_ep_len( ssl ); i-- )
5443 if( ++ssl->in_ctr[i - 1] != 0 )
5444 break;
5445
5446 /* The loop goes to its end iff the counter is wrapping */
5447 if( i == ssl_ep_len( ssl ) )
5448 {
5449 MBEDTLS_SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
5450 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
5451 }
5452 }
5453
Paul Bakker5121ce52009-01-03 21:22:43 +00005454 }
5455
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005456#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00005457 if( ssl->transform_in != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005458 ssl->session_in->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00005459 {
5460 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
5461 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005462 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00005463 return( ret );
5464 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00005465 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005466#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00005467
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005468#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005469 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02005470 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005471 mbedtls_ssl_dtls_replay_update( ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02005472 }
5473#endif
5474
Hanno Beckerd96e10b2019-07-09 17:30:02 +01005475 /* Check actual (decrypted) record content length against
5476 * configured maximum. */
5477 if( ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
5478 {
5479 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
5480 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5481 }
5482
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005483 return( 0 );
5484}
5485
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005486static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005487
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005488/*
5489 * Read a record.
5490 *
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005491 * Silently ignore non-fatal alert (and for DTLS, invalid records as well,
5492 * RFC 6347 4.1.2.7) and continue reading until a valid record is found.
5493 *
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005494 */
Hanno Becker1097b342018-08-15 14:09:41 +01005495
5496/* Helper functions for mbedtls_ssl_read_record(). */
5497static int ssl_consume_current_message( mbedtls_ssl_context *ssl );
Hanno Beckere74d5562018-08-15 14:26:08 +01005498static int ssl_get_next_record( mbedtls_ssl_context *ssl );
5499static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl );
Hanno Becker4162b112018-08-15 14:05:04 +01005500
Hanno Becker327c93b2018-08-15 13:56:18 +01005501int mbedtls_ssl_read_record( mbedtls_ssl_context *ssl,
Hanno Becker3a0aad12018-08-20 09:44:02 +01005502 unsigned update_hs_digest )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005503{
5504 int ret;
5505
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005506 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read record" ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005507
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005508 if( ssl->keep_current_message == 0 )
5509 {
5510 do {
Simon Butcher99000142016-10-13 17:21:01 +01005511
Hanno Becker26994592018-08-15 14:14:59 +01005512 ret = ssl_consume_current_message( ssl );
Hanno Becker90333da2017-10-10 11:27:13 +01005513 if( ret != 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005514 return( ret );
Hanno Becker26994592018-08-15 14:14:59 +01005515
Hanno Beckere74d5562018-08-15 14:26:08 +01005516 if( ssl_record_is_in_progress( ssl ) == 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005517 {
Hanno Becker40f50842018-08-15 14:48:01 +01005518#if defined(MBEDTLS_SSL_PROTO_DTLS)
5519 int have_buffered = 0;
Hanno Beckere74d5562018-08-15 14:26:08 +01005520
Hanno Becker40f50842018-08-15 14:48:01 +01005521 /* We only check for buffered messages if the
5522 * current datagram is fully consumed. */
5523 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005524 ssl_next_record_is_in_datagram( ssl ) == 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01005525 {
Hanno Becker40f50842018-08-15 14:48:01 +01005526 if( ssl_load_buffered_message( ssl ) == 0 )
5527 have_buffered = 1;
5528 }
5529
5530 if( have_buffered == 0 )
5531#endif /* MBEDTLS_SSL_PROTO_DTLS */
5532 {
5533 ret = ssl_get_next_record( ssl );
5534 if( ret == MBEDTLS_ERR_SSL_CONTINUE_PROCESSING )
5535 continue;
5536
5537 if( ret != 0 )
5538 {
Hanno Beckerc573ac32018-08-28 17:15:25 +01005539 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_get_next_record" ), ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005540 return( ret );
5541 }
Hanno Beckere74d5562018-08-15 14:26:08 +01005542 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005543 }
5544
5545 ret = mbedtls_ssl_handle_message_type( ssl );
5546
Hanno Becker40f50842018-08-15 14:48:01 +01005547#if defined(MBEDTLS_SSL_PROTO_DTLS)
5548 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
5549 {
5550 /* Buffer future message */
5551 ret = ssl_buffer_message( ssl );
5552 if( ret != 0 )
5553 return( ret );
5554
5555 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
5556 }
5557#endif /* MBEDTLS_SSL_PROTO_DTLS */
5558
Hanno Becker90333da2017-10-10 11:27:13 +01005559 } while( MBEDTLS_ERR_SSL_NON_FATAL == ret ||
5560 MBEDTLS_ERR_SSL_CONTINUE_PROCESSING == ret );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005561
5562 if( 0 != ret )
Simon Butcher99000142016-10-13 17:21:01 +01005563 {
Hanno Becker05c4fc82017-11-09 14:34:06 +00005564 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_handle_message_type" ), ret );
Simon Butcher99000142016-10-13 17:21:01 +01005565 return( ret );
5566 }
5567
Hanno Becker327c93b2018-08-15 13:56:18 +01005568 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
Hanno Becker3a0aad12018-08-20 09:44:02 +01005569 update_hs_digest == 1 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005570 {
5571 mbedtls_ssl_update_handshake_status( ssl );
5572 }
Simon Butcher99000142016-10-13 17:21:01 +01005573 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005574 else
Simon Butcher99000142016-10-13 17:21:01 +01005575 {
Hanno Becker02f59072018-08-15 14:00:24 +01005576 MBEDTLS_SSL_DEBUG_MSG( 2, ( "reuse previously read message" ) );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005577 ssl->keep_current_message = 0;
Simon Butcher99000142016-10-13 17:21:01 +01005578 }
5579
5580 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read record" ) );
5581
5582 return( 0 );
5583}
5584
Hanno Becker40f50842018-08-15 14:48:01 +01005585#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005586static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl )
Simon Butcher99000142016-10-13 17:21:01 +01005587{
Hanno Becker40f50842018-08-15 14:48:01 +01005588 if( ssl->in_left > ssl->next_record_offset )
5589 return( 1 );
Simon Butcher99000142016-10-13 17:21:01 +01005590
Hanno Becker40f50842018-08-15 14:48:01 +01005591 return( 0 );
5592}
5593
5594static int ssl_load_buffered_message( mbedtls_ssl_context *ssl )
5595{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005596 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker37f95322018-08-16 13:55:32 +01005597 mbedtls_ssl_hs_buffer * hs_buf;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005598 int ret = 0;
5599
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005600 if( hs == NULL )
5601 return( -1 );
5602
Hanno Beckere00ae372018-08-20 09:39:42 +01005603 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_messsage" ) );
5604
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005605 if( ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC ||
5606 ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
5607 {
5608 /* Check if we have seen a ChangeCipherSpec before.
5609 * If yes, synthesize a CCS record. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01005610 if( !hs->buffering.seen_ccs )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005611 {
5612 MBEDTLS_SSL_DEBUG_MSG( 2, ( "CCS not seen in the current flight" ) );
5613 ret = -1;
Hanno Becker0d4b3762018-08-20 09:36:59 +01005614 goto exit;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005615 }
5616
Hanno Becker39b8bc92018-08-28 17:17:13 +01005617 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Injecting buffered CCS message" ) );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005618 ssl->in_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
5619 ssl->in_msglen = 1;
5620 ssl->in_msg[0] = 1;
5621
5622 /* As long as they are equal, the exact value doesn't matter. */
5623 ssl->in_left = 0;
5624 ssl->next_record_offset = 0;
5625
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01005626 hs->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005627 goto exit;
5628 }
Hanno Becker37f95322018-08-16 13:55:32 +01005629
Hanno Beckerb8f50142018-08-28 10:01:34 +01005630#if defined(MBEDTLS_DEBUG_C)
Hanno Becker37f95322018-08-16 13:55:32 +01005631 /* Debug only */
5632 {
5633 unsigned offset;
5634 for( offset = 1; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
5635 {
5636 hs_buf = &hs->buffering.hs[offset];
5637 if( hs_buf->is_valid == 1 )
5638 {
5639 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Future message with sequence number %u %s buffered.",
5640 hs->in_msg_seq + offset,
Hanno Beckera591c482018-08-28 17:20:00 +01005641 hs_buf->is_complete ? "fully" : "partially" ) );
Hanno Becker37f95322018-08-16 13:55:32 +01005642 }
5643 }
5644 }
Hanno Beckerb8f50142018-08-28 10:01:34 +01005645#endif /* MBEDTLS_DEBUG_C */
Hanno Becker37f95322018-08-16 13:55:32 +01005646
5647 /* Check if we have buffered and/or fully reassembled the
5648 * next handshake message. */
5649 hs_buf = &hs->buffering.hs[0];
5650 if( ( hs_buf->is_valid == 1 ) && ( hs_buf->is_complete == 1 ) )
5651 {
5652 /* Synthesize a record containing the buffered HS message. */
5653 size_t msg_len = ( hs_buf->data[1] << 16 ) |
5654 ( hs_buf->data[2] << 8 ) |
5655 hs_buf->data[3];
5656
5657 /* Double-check that we haven't accidentally buffered
5658 * a message that doesn't fit into the input buffer. */
5659 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
5660 {
5661 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5662 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5663 }
5664
5665 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message has been buffered - load" ) );
5666 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered handshake message (incl. header)",
5667 hs_buf->data, msg_len + 12 );
5668
5669 ssl->in_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
5670 ssl->in_hslen = msg_len + 12;
5671 ssl->in_msglen = msg_len + 12;
5672 memcpy( ssl->in_msg, hs_buf->data, ssl->in_hslen );
5673
5674 ret = 0;
5675 goto exit;
5676 }
5677 else
5678 {
5679 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message %u not or only partially bufffered",
5680 hs->in_msg_seq ) );
5681 }
5682
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005683 ret = -1;
5684
5685exit:
5686
5687 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_message" ) );
5688 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005689}
5690
Hanno Beckera02b0b42018-08-21 17:20:27 +01005691static int ssl_buffer_make_space( mbedtls_ssl_context *ssl,
5692 size_t desired )
5693{
5694 int offset;
5695 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005696 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Attempt to free buffered messages to have %u bytes available",
5697 (unsigned) desired ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005698
Hanno Becker01315ea2018-08-21 17:22:17 +01005699 /* Get rid of future records epoch first, if such exist. */
5700 ssl_free_buffered_record( ssl );
5701
5702 /* Check if we have enough space available now. */
5703 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5704 hs->buffering.total_bytes_buffered ) )
5705 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005706 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing future epoch record" ) );
Hanno Becker01315ea2018-08-21 17:22:17 +01005707 return( 0 );
5708 }
Hanno Beckera02b0b42018-08-21 17:20:27 +01005709
Hanno Becker4f432ad2018-08-28 10:02:32 +01005710 /* We don't have enough space to buffer the next expected handshake
5711 * message. Remove buffers used for future messages to gain space,
5712 * starting with the most distant one. */
Hanno Beckera02b0b42018-08-21 17:20:27 +01005713 for( offset = MBEDTLS_SSL_MAX_BUFFERED_HS - 1;
5714 offset >= 0; offset-- )
5715 {
5716 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Free buffering slot %d to make space for reassembly of next handshake message",
5717 offset ) );
5718
Hanno Beckerb309b922018-08-23 13:18:05 +01005719 ssl_buffering_free_slot( ssl, (uint8_t) offset );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005720
5721 /* Check if we have enough space available now. */
5722 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5723 hs->buffering.total_bytes_buffered ) )
5724 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005725 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing buffered HS messages" ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005726 return( 0 );
5727 }
5728 }
5729
5730 return( -1 );
5731}
5732
Hanno Becker40f50842018-08-15 14:48:01 +01005733static int ssl_buffer_message( mbedtls_ssl_context *ssl )
5734{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005735 int ret = 0;
5736 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5737
5738 if( hs == NULL )
5739 return( 0 );
5740
5741 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_buffer_message" ) );
5742
5743 switch( ssl->in_msgtype )
5744 {
5745 case MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC:
5746 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Remember CCS message" ) );
Hanno Beckere678eaa2018-08-21 14:57:46 +01005747
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01005748 hs->buffering.seen_ccs = 1;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005749 break;
5750
5751 case MBEDTLS_SSL_MSG_HANDSHAKE:
Hanno Becker37f95322018-08-16 13:55:32 +01005752 {
5753 unsigned recv_msg_seq_offset;
5754 unsigned recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
5755 mbedtls_ssl_hs_buffer *hs_buf;
5756 size_t msg_len = ssl->in_hslen - 12;
5757
5758 /* We should never receive an old handshake
5759 * message - double-check nonetheless. */
5760 if( recv_msg_seq < ssl->handshake->in_msg_seq )
5761 {
5762 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5763 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5764 }
5765
5766 recv_msg_seq_offset = recv_msg_seq - ssl->handshake->in_msg_seq;
5767 if( recv_msg_seq_offset >= MBEDTLS_SSL_MAX_BUFFERED_HS )
5768 {
5769 /* Silently ignore -- message too far in the future */
5770 MBEDTLS_SSL_DEBUG_MSG( 2,
5771 ( "Ignore future HS message with sequence number %u, "
5772 "buffering window %u - %u",
5773 recv_msg_seq, ssl->handshake->in_msg_seq,
5774 ssl->handshake->in_msg_seq + MBEDTLS_SSL_MAX_BUFFERED_HS - 1 ) );
5775
5776 goto exit;
5777 }
5778
5779 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering HS message with sequence number %u, offset %u ",
5780 recv_msg_seq, recv_msg_seq_offset ) );
5781
5782 hs_buf = &hs->buffering.hs[ recv_msg_seq_offset ];
5783
5784 /* Check if the buffering for this seq nr has already commenced. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01005785 if( !hs_buf->is_valid )
Hanno Becker37f95322018-08-16 13:55:32 +01005786 {
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005787 size_t reassembly_buf_sz;
5788
Hanno Becker37f95322018-08-16 13:55:32 +01005789 hs_buf->is_fragmented =
5790 ( ssl_hs_is_proper_fragment( ssl ) == 1 );
5791
5792 /* We copy the message back into the input buffer
5793 * after reassembly, so check that it's not too large.
5794 * This is an implementation-specific limitation
5795 * and not one from the standard, hence it is not
5796 * checked in ssl_check_hs_header(). */
Hanno Becker96a6c692018-08-21 15:56:03 +01005797 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker37f95322018-08-16 13:55:32 +01005798 {
5799 /* Ignore message */
5800 goto exit;
5801 }
5802
Hanno Beckere0b150f2018-08-21 15:51:03 +01005803 /* Check if we have enough space to buffer the message. */
5804 if( hs->buffering.total_bytes_buffered >
5805 MBEDTLS_SSL_DTLS_MAX_BUFFERING )
5806 {
5807 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5808 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5809 }
5810
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005811 reassembly_buf_sz = ssl_get_reassembly_buffer_size( msg_len,
5812 hs_buf->is_fragmented );
Hanno Beckere0b150f2018-08-21 15:51:03 +01005813
5814 if( reassembly_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5815 hs->buffering.total_bytes_buffered ) )
5816 {
5817 if( recv_msg_seq_offset > 0 )
5818 {
5819 /* If we can't buffer a future message because
5820 * of space limitations -- ignore. */
5821 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",
5822 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5823 (unsigned) hs->buffering.total_bytes_buffered ) );
5824 goto exit;
5825 }
Hanno Beckere1801392018-08-21 16:51:05 +01005826 else
5827 {
5828 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",
5829 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5830 (unsigned) hs->buffering.total_bytes_buffered ) );
5831 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005832
Hanno Beckera02b0b42018-08-21 17:20:27 +01005833 if( ssl_buffer_make_space( ssl, reassembly_buf_sz ) != 0 )
Hanno Becker55e9e2a2018-08-21 16:07:55 +01005834 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005835 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",
5836 (unsigned) msg_len,
5837 (unsigned) reassembly_buf_sz,
5838 MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Hanno Beckere0b150f2018-08-21 15:51:03 +01005839 (unsigned) hs->buffering.total_bytes_buffered ) );
Hanno Becker55e9e2a2018-08-21 16:07:55 +01005840 ret = MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
5841 goto exit;
5842 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005843 }
5844
5845 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %d",
5846 msg_len ) );
5847
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005848 hs_buf->data = mbedtls_calloc( 1, reassembly_buf_sz );
5849 if( hs_buf->data == NULL )
Hanno Becker37f95322018-08-16 13:55:32 +01005850 {
Hanno Beckere0b150f2018-08-21 15:51:03 +01005851 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
Hanno Becker37f95322018-08-16 13:55:32 +01005852 goto exit;
5853 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005854 hs_buf->data_len = reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01005855
5856 /* Prepare final header: copy msg_type, length and message_seq,
5857 * then add standardised fragment_offset and fragment_length */
5858 memcpy( hs_buf->data, ssl->in_msg, 6 );
5859 memset( hs_buf->data + 6, 0, 3 );
5860 memcpy( hs_buf->data + 9, hs_buf->data + 1, 3 );
5861
5862 hs_buf->is_valid = 1;
Hanno Beckere0b150f2018-08-21 15:51:03 +01005863
5864 hs->buffering.total_bytes_buffered += reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01005865 }
5866 else
5867 {
5868 /* Make sure msg_type and length are consistent */
5869 if( memcmp( hs_buf->data, ssl->in_msg, 4 ) != 0 )
5870 {
5871 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Fragment header mismatch - ignore" ) );
5872 /* Ignore */
5873 goto exit;
5874 }
5875 }
5876
Hanno Becker4422bbb2018-08-20 09:40:19 +01005877 if( !hs_buf->is_complete )
Hanno Becker37f95322018-08-16 13:55:32 +01005878 {
5879 size_t frag_len, frag_off;
5880 unsigned char * const msg = hs_buf->data + 12;
5881
5882 /*
5883 * Check and copy current fragment
5884 */
5885
5886 /* Validation of header fields already done in
5887 * mbedtls_ssl_prepare_handshake_record(). */
5888 frag_off = ssl_get_hs_frag_off( ssl );
5889 frag_len = ssl_get_hs_frag_len( ssl );
5890
5891 MBEDTLS_SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %d, length = %d",
5892 frag_off, frag_len ) );
5893 memcpy( msg + frag_off, ssl->in_msg + 12, frag_len );
5894
5895 if( hs_buf->is_fragmented )
5896 {
5897 unsigned char * const bitmask = msg + msg_len;
5898 ssl_bitmask_set( bitmask, frag_off, frag_len );
5899 hs_buf->is_complete = ( ssl_bitmask_check( bitmask,
5900 msg_len ) == 0 );
5901 }
5902 else
5903 {
5904 hs_buf->is_complete = 1;
5905 }
5906
5907 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message %scomplete",
5908 hs_buf->is_complete ? "" : "not yet " ) );
5909 }
5910
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005911 break;
Hanno Becker37f95322018-08-16 13:55:32 +01005912 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005913
5914 default:
Hanno Becker360bef32018-08-28 10:04:33 +01005915 /* We don't buffer other types of messages. */
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005916 break;
5917 }
5918
5919exit:
5920
5921 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_buffer_message" ) );
5922 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005923}
5924#endif /* MBEDTLS_SSL_PROTO_DTLS */
5925
Hanno Becker1097b342018-08-15 14:09:41 +01005926static int ssl_consume_current_message( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005927{
Hanno Becker4a810fb2017-05-24 16:27:30 +01005928 /*
Hanno Becker4a810fb2017-05-24 16:27:30 +01005929 * Consume last content-layer message and potentially
5930 * update in_msglen which keeps track of the contents'
5931 * consumption state.
5932 *
5933 * (1) Handshake messages:
5934 * Remove last handshake message, move content
5935 * and adapt in_msglen.
5936 *
5937 * (2) Alert messages:
5938 * Consume whole record content, in_msglen = 0.
5939 *
Hanno Becker4a810fb2017-05-24 16:27:30 +01005940 * (3) Change cipher spec:
5941 * Consume whole record content, in_msglen = 0.
5942 *
5943 * (4) Application data:
5944 * Don't do anything - the record layer provides
5945 * the application data as a stream transport
5946 * and consumes through mbedtls_ssl_read only.
5947 *
5948 */
5949
5950 /* Case (1): Handshake messages */
5951 if( ssl->in_hslen != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005952 {
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01005953 /* Hard assertion to be sure that no application data
5954 * is in flight, as corrupting ssl->in_msglen during
5955 * ssl->in_offt != NULL is fatal. */
5956 if( ssl->in_offt != NULL )
5957 {
5958 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5959 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5960 }
5961
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005962 /*
5963 * Get next Handshake message in the current record
5964 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005965
Hanno Becker4a810fb2017-05-24 16:27:30 +01005966 /* Notes:
Hanno Beckere72489d2017-10-23 13:23:50 +01005967 * (1) in_hslen is not necessarily the size of the
Hanno Becker4a810fb2017-05-24 16:27:30 +01005968 * current handshake content: If DTLS handshake
5969 * fragmentation is used, that's the fragment
5970 * size instead. Using the total handshake message
Hanno Beckere72489d2017-10-23 13:23:50 +01005971 * size here is faulty and should be changed at
5972 * some point.
Hanno Becker4a810fb2017-05-24 16:27:30 +01005973 * (2) While it doesn't seem to cause problems, one
5974 * has to be very careful not to assume that in_hslen
5975 * is always <= in_msglen in a sensible communication.
5976 * Again, it's wrong for DTLS handshake fragmentation.
5977 * The following check is therefore mandatory, and
5978 * should not be treated as a silently corrected assertion.
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01005979 * Additionally, ssl->in_hslen might be arbitrarily out of
5980 * bounds after handling a DTLS message with an unexpected
5981 * sequence number, see mbedtls_ssl_prepare_handshake_record.
Hanno Becker4a810fb2017-05-24 16:27:30 +01005982 */
5983 if( ssl->in_hslen < ssl->in_msglen )
5984 {
5985 ssl->in_msglen -= ssl->in_hslen;
5986 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
5987 ssl->in_msglen );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005988
Hanno Becker4a810fb2017-05-24 16:27:30 +01005989 MBEDTLS_SSL_DEBUG_BUF( 4, "remaining content in record",
5990 ssl->in_msg, ssl->in_msglen );
5991 }
5992 else
5993 {
5994 ssl->in_msglen = 0;
5995 }
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02005996
Hanno Becker4a810fb2017-05-24 16:27:30 +01005997 ssl->in_hslen = 0;
5998 }
5999 /* Case (4): Application data */
6000 else if( ssl->in_offt != NULL )
6001 {
6002 return( 0 );
6003 }
6004 /* Everything else (CCS & Alerts) */
6005 else
6006 {
6007 ssl->in_msglen = 0;
6008 }
6009
Hanno Becker1097b342018-08-15 14:09:41 +01006010 return( 0 );
6011}
Hanno Becker4a810fb2017-05-24 16:27:30 +01006012
Hanno Beckere74d5562018-08-15 14:26:08 +01006013static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl )
6014{
Hanno Becker4a810fb2017-05-24 16:27:30 +01006015 if( ssl->in_msglen > 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01006016 return( 1 );
6017
6018 return( 0 );
6019}
6020
Hanno Becker5f066e72018-08-16 14:56:31 +01006021#if defined(MBEDTLS_SSL_PROTO_DTLS)
6022
6023static void ssl_free_buffered_record( mbedtls_ssl_context *ssl )
6024{
6025 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
6026 if( hs == NULL )
6027 return;
6028
Hanno Becker01315ea2018-08-21 17:22:17 +01006029 if( hs->buffering.future_record.data != NULL )
Hanno Becker4a810fb2017-05-24 16:27:30 +01006030 {
Hanno Becker01315ea2018-08-21 17:22:17 +01006031 hs->buffering.total_bytes_buffered -=
6032 hs->buffering.future_record.len;
6033
6034 mbedtls_free( hs->buffering.future_record.data );
6035 hs->buffering.future_record.data = NULL;
6036 }
Hanno Becker5f066e72018-08-16 14:56:31 +01006037}
6038
6039static int ssl_load_buffered_record( mbedtls_ssl_context *ssl )
6040{
6041 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
6042 unsigned char * rec;
6043 size_t rec_len;
6044 unsigned rec_epoch;
6045
6046 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
6047 return( 0 );
6048
6049 if( hs == NULL )
6050 return( 0 );
6051
Hanno Becker5f066e72018-08-16 14:56:31 +01006052 rec = hs->buffering.future_record.data;
6053 rec_len = hs->buffering.future_record.len;
6054 rec_epoch = hs->buffering.future_record.epoch;
6055
6056 if( rec == NULL )
6057 return( 0 );
6058
Hanno Becker4cb782d2018-08-20 11:19:05 +01006059 /* Only consider loading future records if the
6060 * input buffer is empty. */
Hanno Beckeref7afdf2018-08-28 17:16:31 +01006061 if( ssl_next_record_is_in_datagram( ssl ) == 1 )
Hanno Becker4cb782d2018-08-20 11:19:05 +01006062 return( 0 );
6063
Hanno Becker5f066e72018-08-16 14:56:31 +01006064 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_record" ) );
6065
6066 if( rec_epoch != ssl->in_epoch )
6067 {
6068 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffered record not from current epoch." ) );
6069 goto exit;
6070 }
6071
6072 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Found buffered record from current epoch - load" ) );
6073
6074 /* Double-check that the record is not too large */
6075 if( rec_len > MBEDTLS_SSL_IN_BUFFER_LEN -
6076 (size_t)( ssl->in_hdr - ssl->in_buf ) )
6077 {
6078 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6079 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6080 }
6081
6082 memcpy( ssl->in_hdr, rec, rec_len );
6083 ssl->in_left = rec_len;
6084 ssl->next_record_offset = 0;
6085
6086 ssl_free_buffered_record( ssl );
6087
6088exit:
6089 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_record" ) );
6090 return( 0 );
6091}
6092
Hanno Becker519f15d2019-07-11 12:43:20 +01006093static int ssl_buffer_future_record( mbedtls_ssl_context *ssl,
6094 mbedtls_record const *rec )
Hanno Becker5f066e72018-08-16 14:56:31 +01006095{
6096 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker5f066e72018-08-16 14:56:31 +01006097
6098 /* Don't buffer future records outside handshakes. */
6099 if( hs == NULL )
6100 return( 0 );
6101
6102 /* Only buffer handshake records (we are only interested
6103 * in Finished messages). */
Hanno Becker519f15d2019-07-11 12:43:20 +01006104 if( rec->type != MBEDTLS_SSL_MSG_HANDSHAKE )
Hanno Becker5f066e72018-08-16 14:56:31 +01006105 return( 0 );
6106
6107 /* Don't buffer more than one future epoch record. */
6108 if( hs->buffering.future_record.data != NULL )
6109 return( 0 );
6110
Hanno Becker01315ea2018-08-21 17:22:17 +01006111 /* Don't buffer record if there's not enough buffering space remaining. */
Hanno Becker519f15d2019-07-11 12:43:20 +01006112 if( rec->buf_len > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
Hanno Becker01315ea2018-08-21 17:22:17 +01006113 hs->buffering.total_bytes_buffered ) )
6114 {
6115 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 +01006116 (unsigned) rec->buf_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Hanno Becker01315ea2018-08-21 17:22:17 +01006117 (unsigned) hs->buffering.total_bytes_buffered ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006118 return( 0 );
6119 }
6120
Hanno Becker5f066e72018-08-16 14:56:31 +01006121 /* Buffer record */
6122 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffer record from epoch %u",
6123 ssl->in_epoch + 1 ) );
Hanno Becker519f15d2019-07-11 12:43:20 +01006124 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered record", rec->buf, rec->buf_len );
Hanno Becker5f066e72018-08-16 14:56:31 +01006125
6126 /* ssl_parse_record_header() only considers records
6127 * of the next epoch as candidates for buffering. */
6128 hs->buffering.future_record.epoch = ssl->in_epoch + 1;
Hanno Becker519f15d2019-07-11 12:43:20 +01006129 hs->buffering.future_record.len = rec->buf_len;
Hanno Becker5f066e72018-08-16 14:56:31 +01006130
6131 hs->buffering.future_record.data =
6132 mbedtls_calloc( 1, hs->buffering.future_record.len );
6133 if( hs->buffering.future_record.data == NULL )
6134 {
6135 /* If we run out of RAM trying to buffer a
6136 * record from the next epoch, just ignore. */
6137 return( 0 );
6138 }
6139
Hanno Becker519f15d2019-07-11 12:43:20 +01006140 memcpy( hs->buffering.future_record.data, rec->buf, rec->buf_len );
Hanno Becker5f066e72018-08-16 14:56:31 +01006141
Hanno Becker519f15d2019-07-11 12:43:20 +01006142 hs->buffering.total_bytes_buffered += rec->buf_len;
Hanno Becker5f066e72018-08-16 14:56:31 +01006143 return( 0 );
6144}
6145
6146#endif /* MBEDTLS_SSL_PROTO_DTLS */
6147
Hanno Beckere74d5562018-08-15 14:26:08 +01006148static int ssl_get_next_record( mbedtls_ssl_context *ssl )
Hanno Becker1097b342018-08-15 14:09:41 +01006149{
6150 int ret;
Hanno Beckere5e7e782019-07-11 12:29:35 +01006151 mbedtls_record rec;
Hanno Becker1097b342018-08-15 14:09:41 +01006152
Hanno Becker5f066e72018-08-16 14:56:31 +01006153#if defined(MBEDTLS_SSL_PROTO_DTLS)
6154 /* We might have buffered a future record; if so,
6155 * and if the epoch matches now, load it.
6156 * On success, this call will set ssl->in_left to
6157 * the length of the buffered record, so that
6158 * the calls to ssl_fetch_input() below will
6159 * essentially be no-ops. */
6160 ret = ssl_load_buffered_record( ssl );
6161 if( ret != 0 )
6162 return( ret );
6163#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker4a810fb2017-05-24 16:27:30 +01006164
Hanno Beckerca59c2b2019-05-08 12:03:28 +01006165 /* Ensure that we have enough space available for the default form
6166 * of TLS / DTLS record headers (5 Bytes for TLS, 13 Bytes for DTLS,
6167 * with no space for CIDs counted in). */
6168 ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_in_hdr_len( ssl ) );
6169 if( ret != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006170 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006171 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006172 return( ret );
6173 }
6174
Hanno Beckere5e7e782019-07-11 12:29:35 +01006175 ret = ssl_parse_record_header( ssl, ssl->in_hdr, ssl->in_left, &rec );
6176 if( ret != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006177 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006178#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker2fddd372019-07-10 14:37:41 +01006179 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006180 {
Hanno Becker5f066e72018-08-16 14:56:31 +01006181 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
6182 {
Hanno Becker519f15d2019-07-11 12:43:20 +01006183 ret = ssl_buffer_future_record( ssl, &rec );
Hanno Becker5f066e72018-08-16 14:56:31 +01006184 if( ret != 0 )
6185 return( ret );
6186
6187 /* Fall through to handling of unexpected records */
6188 ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
6189 }
6190
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01006191 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD )
6192 {
Hanno Becker2fddd372019-07-10 14:37:41 +01006193#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Hanno Beckerd8bf8ce2019-07-12 09:23:47 +01006194 /* Reset in pointers to default state for TLS/DTLS records,
6195 * assuming no CID and no offset between record content and
6196 * record plaintext. */
6197 ssl_update_in_pointers( ssl );
6198
Hanno Becker7ae20e02019-07-12 08:33:49 +01006199 /* Setup internal message pointers from record structure. */
6200 ssl->in_msgtype = rec.type;
6201#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
6202 ssl->in_len = ssl->in_cid + rec.cid_len;
6203#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
6204 ssl->in_iv = ssl->in_msg = ssl->in_len + 2;
6205 ssl->in_msglen = rec.data_len;
6206
Hanno Becker2fddd372019-07-10 14:37:41 +01006207 ret = ssl_check_client_reconnect( ssl );
6208 if( ret != 0 )
6209 return( ret );
6210#endif
6211
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01006212 /* Skip unexpected record (but not whole datagram) */
Hanno Becker4acada32019-07-11 12:48:53 +01006213 ssl->next_record_offset = rec.buf_len;
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006214
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01006215 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding unexpected record "
6216 "(header)" ) );
6217 }
6218 else
6219 {
6220 /* Skip invalid record and the rest of the datagram */
6221 ssl->next_record_offset = 0;
6222 ssl->in_left = 0;
6223
6224 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record "
6225 "(header)" ) );
6226 }
6227
6228 /* Get next record */
Hanno Becker90333da2017-10-10 11:27:13 +01006229 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006230 }
Hanno Becker2fddd372019-07-10 14:37:41 +01006231 else
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006232#endif
Hanno Becker2fddd372019-07-10 14:37:41 +01006233 {
6234 return( ret );
6235 }
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006236 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006237
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006238#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006239 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Beckere65ce782017-05-22 14:47:48 +01006240 {
Hanno Beckera8814792019-07-10 15:01:45 +01006241 /* Remember offset of next record within datagram. */
Hanno Beckerf50da502019-07-11 12:50:10 +01006242 ssl->next_record_offset = rec.buf_len;
Hanno Beckere65ce782017-05-22 14:47:48 +01006243 if( ssl->next_record_offset < ssl->in_left )
6244 {
6245 MBEDTLS_SSL_DEBUG_MSG( 3, ( "more than one record within datagram" ) );
6246 }
6247 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006248 else
6249#endif
Hanno Beckera8814792019-07-10 15:01:45 +01006250 {
Hanno Becker955a5c92019-07-10 17:12:07 +01006251 /*
6252 * Fetch record contents from underlying transport.
6253 */
Hanno Beckera3175662019-07-11 12:50:29 +01006254 ret = mbedtls_ssl_fetch_input( ssl, rec.buf_len );
Hanno Beckera8814792019-07-10 15:01:45 +01006255 if( ret != 0 )
6256 {
6257 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
6258 return( ret );
6259 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006260
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006261 ssl->in_left = 0;
Hanno Beckera8814792019-07-10 15:01:45 +01006262 }
6263
6264 /*
6265 * Decrypt record contents.
6266 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006267
Hanno Beckerfdf66042019-07-11 13:07:45 +01006268 if( ( ret = ssl_prepare_record_content( ssl, &rec ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006269 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006270#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006271 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006272 {
6273 /* Silently discard invalid records */
Hanno Becker82e2a392019-05-03 16:36:59 +01006274 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006275 {
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02006276 /* Except when waiting for Finished as a bad mac here
6277 * probably means something went wrong in the handshake
6278 * (eg wrong psk used, mitm downgrade attempt, etc.) */
6279 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED ||
6280 ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
6281 {
6282#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
6283 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
6284 {
6285 mbedtls_ssl_send_alert_message( ssl,
6286 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6287 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
6288 }
6289#endif
6290 return( ret );
6291 }
6292
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006293#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006294 if( ssl->conf->badmac_limit != 0 &&
6295 ++ssl->badmac_seen >= ssl->conf->badmac_limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02006296 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006297 MBEDTLS_SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) );
6298 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02006299 }
6300#endif
6301
Hanno Becker4a810fb2017-05-24 16:27:30 +01006302 /* As above, invalid records cause
6303 * dismissal of the whole datagram. */
6304
6305 ssl->next_record_offset = 0;
6306 ssl->in_left = 0;
6307
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006308 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record (mac)" ) );
Hanno Becker90333da2017-10-10 11:27:13 +01006309 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006310 }
6311
6312 return( ret );
6313 }
6314 else
6315#endif
6316 {
6317 /* Error out (and send alert) on invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006318#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
6319 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006320 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006321 mbedtls_ssl_send_alert_message( ssl,
6322 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6323 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006324 }
6325#endif
6326 return( ret );
6327 }
6328 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006329
Hanno Becker44d89b22019-07-12 09:40:44 +01006330
6331 /* Reset in pointers to default state for TLS/DTLS records,
6332 * assuming no CID and no offset between record content and
6333 * record plaintext. */
6334 ssl_update_in_pointers( ssl );
Hanno Becker44d89b22019-07-12 09:40:44 +01006335#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
6336 ssl->in_len = ssl->in_cid + rec.cid_len;
6337#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
6338 ssl->in_iv = ssl->in_msg = ssl->in_len + 2;
Hanno Becker44d89b22019-07-12 09:40:44 +01006339
Hanno Becker8685c822019-07-12 09:37:30 +01006340 /* The record content type may change during decryption,
6341 * so re-read it. */
6342 ssl->in_msgtype = rec.type;
6343 /* Also update the input buffer, because unfortunately
6344 * the server-side ssl_parse_client_hello() reparses the
6345 * record header when receiving a ClientHello initiating
6346 * a renegotiation. */
6347 ssl->in_hdr[0] = rec.type;
6348 ssl->in_msg = rec.buf + rec.data_offset;
6349 ssl->in_msglen = rec.data_len;
6350 ssl->in_len[0] = (unsigned char)( rec.data_len >> 8 );
6351 ssl->in_len[1] = (unsigned char)( rec.data_len );
6352
Simon Butcher99000142016-10-13 17:21:01 +01006353 return( 0 );
6354}
6355
6356int mbedtls_ssl_handle_message_type( mbedtls_ssl_context *ssl )
6357{
6358 int ret;
6359
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006360 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006361 * Handle particular types of records
6362 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006363 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00006364 {
Simon Butcher99000142016-10-13 17:21:01 +01006365 if( ( ret = mbedtls_ssl_prepare_handshake_record( ssl ) ) != 0 )
6366 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01006367 return( ret );
Simon Butcher99000142016-10-13 17:21:01 +01006368 }
Paul Bakker5121ce52009-01-03 21:22:43 +00006369 }
6370
Hanno Beckere678eaa2018-08-21 14:57:46 +01006371 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006372 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01006373 if( ssl->in_msglen != 1 )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006374 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01006375 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, len: %d",
6376 ssl->in_msglen ) );
6377 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006378 }
6379
Hanno Beckere678eaa2018-08-21 14:57:46 +01006380 if( ssl->in_msg[0] != 1 )
6381 {
6382 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, content: %02x",
6383 ssl->in_msg[0] ) );
6384 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
6385 }
6386
6387#if defined(MBEDTLS_SSL_PROTO_DTLS)
6388 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
6389 ssl->state != MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC &&
6390 ssl->state != MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
6391 {
6392 if( ssl->handshake == NULL )
6393 {
6394 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping ChangeCipherSpec outside handshake" ) );
6395 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
6396 }
6397
6398 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received out-of-order ChangeCipherSpec - remember" ) );
6399 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
6400 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006401#endif
Hanno Beckere678eaa2018-08-21 14:57:46 +01006402 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006403
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006404 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00006405 {
Angus Gratton1a7a17e2018-06-20 15:43:50 +10006406 if( ssl->in_msglen != 2 )
6407 {
6408 /* Note: Standard allows for more than one 2 byte alert
6409 to be packed in a single message, but Mbed TLS doesn't
6410 currently support this. */
6411 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid alert message, len: %d",
6412 ssl->in_msglen ) );
6413 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
6414 }
6415
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006416 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
Paul Bakker5121ce52009-01-03 21:22:43 +00006417 ssl->in_msg[0], ssl->in_msg[1] ) );
6418
6419 /*
Simon Butcher459a9502015-10-27 16:09:03 +00006420 * Ignore non-fatal alerts, except close_notify and no_renegotiation
Paul Bakker5121ce52009-01-03 21:22:43 +00006421 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006422 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006423 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006424 MBEDTLS_SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
Paul Bakker2770fbd2012-07-03 13:30:23 +00006425 ssl->in_msg[1] ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006426 return( MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006427 }
6428
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006429 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6430 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00006431 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006432 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
6433 return( MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00006434 }
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02006435
6436#if defined(MBEDTLS_SSL_RENEGOTIATION_ENABLED)
6437 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6438 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION )
6439 {
Hanno Becker90333da2017-10-10 11:27:13 +01006440 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no renegotiation alert" ) );
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02006441 /* Will be handled when trying to parse ServerHello */
6442 return( 0 );
6443 }
6444#endif
6445
6446#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_SRV_C)
6447 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
6448 ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
6449 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6450 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
6451 {
6452 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no_cert" ) );
6453 /* Will be handled in mbedtls_ssl_parse_certificate() */
6454 return( 0 );
6455 }
6456#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
6457
6458 /* Silently ignore: fetch new message */
Simon Butcher99000142016-10-13 17:21:01 +01006459 return MBEDTLS_ERR_SSL_NON_FATAL;
Paul Bakker5121ce52009-01-03 21:22:43 +00006460 }
6461
Hanno Beckerc76c6192017-06-06 10:03:17 +01006462#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker37ae9522019-05-03 16:54:26 +01006463 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Beckerc76c6192017-06-06 10:03:17 +01006464 {
Hanno Becker37ae9522019-05-03 16:54:26 +01006465 /* Drop unexpected ApplicationData records,
6466 * except at the beginning of renegotiations */
6467 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA &&
6468 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER
6469#if defined(MBEDTLS_SSL_RENEGOTIATION)
6470 && ! ( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
6471 ssl->state == MBEDTLS_SSL_SERVER_HELLO )
Hanno Beckerc76c6192017-06-06 10:03:17 +01006472#endif
Hanno Becker37ae9522019-05-03 16:54:26 +01006473 )
6474 {
6475 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) );
6476 return( MBEDTLS_ERR_SSL_NON_FATAL );
6477 }
6478
6479 if( ssl->handshake != NULL &&
6480 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
6481 {
6482 ssl_handshake_wrapup_free_hs_transform( ssl );
6483 }
6484 }
Hanno Becker4a4af9f2019-05-08 16:26:21 +01006485#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Beckerc76c6192017-06-06 10:03:17 +01006486
Paul Bakker5121ce52009-01-03 21:22:43 +00006487 return( 0 );
6488}
6489
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006490int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00006491{
6492 int ret;
6493
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006494 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
6495 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6496 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00006497 {
6498 return( ret );
6499 }
6500
6501 return( 0 );
6502}
6503
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006504int mbedtls_ssl_send_alert_message( mbedtls_ssl_context *ssl,
Paul Bakker0a925182012-04-16 06:46:41 +00006505 unsigned char level,
6506 unsigned char message )
6507{
6508 int ret;
6509
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02006510 if( ssl == NULL || ssl->conf == NULL )
6511 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6512
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006513 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006514 MBEDTLS_SSL_DEBUG_MSG( 3, ( "send alert level=%u message=%u", level, message ));
Paul Bakker0a925182012-04-16 06:46:41 +00006515
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006516 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
Paul Bakker0a925182012-04-16 06:46:41 +00006517 ssl->out_msglen = 2;
6518 ssl->out_msg[0] = level;
6519 ssl->out_msg[1] = message;
6520
Hanno Becker67bc7c32018-08-06 11:33:50 +01006521 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00006522 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006523 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker0a925182012-04-16 06:46:41 +00006524 return( ret );
6525 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006526 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
Paul Bakker0a925182012-04-16 06:46:41 +00006527
6528 return( 0 );
6529}
6530
Hanno Beckerb9d44792019-02-08 07:19:04 +00006531#if defined(MBEDTLS_X509_CRT_PARSE_C)
6532static void ssl_clear_peer_cert( mbedtls_ssl_session *session )
6533{
6534#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6535 if( session->peer_cert != NULL )
6536 {
6537 mbedtls_x509_crt_free( session->peer_cert );
6538 mbedtls_free( session->peer_cert );
6539 session->peer_cert = NULL;
6540 }
6541#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6542 if( session->peer_cert_digest != NULL )
6543 {
6544 /* Zeroization is not necessary. */
6545 mbedtls_free( session->peer_cert_digest );
6546 session->peer_cert_digest = NULL;
6547 session->peer_cert_digest_type = MBEDTLS_MD_NONE;
6548 session->peer_cert_digest_len = 0;
6549 }
6550#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6551}
6552#endif /* MBEDTLS_X509_CRT_PARSE_C */
6553
Paul Bakker5121ce52009-01-03 21:22:43 +00006554/*
6555 * Handshake functions
6556 */
Hanno Becker21489932019-02-05 13:20:55 +00006557#if !defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Gilles Peskinef9828522017-05-03 12:28:43 +02006558/* No certificate support -> dummy functions */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006559int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006560{
Hanno Beckere694c3e2017-12-27 21:34:08 +00006561 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6562 ssl->handshake->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00006563
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006564 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006565
Hanno Becker7177a882019-02-05 13:36:46 +00006566 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02006567 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006568 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02006569 ssl->state++;
6570 return( 0 );
6571 }
6572
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006573 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6574 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006575}
6576
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006577int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006578{
Hanno Beckere694c3e2017-12-27 21:34:08 +00006579 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6580 ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006581
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006582 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006583
Hanno Becker7177a882019-02-05 13:36:46 +00006584 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006585 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006586 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006587 ssl->state++;
6588 return( 0 );
6589 }
6590
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006591 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6592 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006593}
Gilles Peskinef9828522017-05-03 12:28:43 +02006594
Hanno Becker21489932019-02-05 13:20:55 +00006595#else /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Gilles Peskinef9828522017-05-03 12:28:43 +02006596/* Some certificate support -> implement write and parse */
6597
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006598int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006599{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006600 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006601 size_t i, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006602 const mbedtls_x509_crt *crt;
Hanno Beckere694c3e2017-12-27 21:34:08 +00006603 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6604 ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006605
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006606 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006607
Hanno Becker7177a882019-02-05 13:36:46 +00006608 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006609 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006610 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006611 ssl->state++;
6612 return( 0 );
6613 }
6614
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006615#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006616 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00006617 {
6618 if( ssl->client_auth == 0 )
6619 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006620 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006621 ssl->state++;
6622 return( 0 );
6623 }
6624
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006625#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00006626 /*
6627 * If using SSLv3 and got no cert, send an Alert message
6628 * (otherwise an empty Certificate message will be sent).
6629 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006630 if( mbedtls_ssl_own_cert( ssl ) == NULL &&
6631 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006632 {
6633 ssl->out_msglen = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006634 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
6635 ssl->out_msg[0] = MBEDTLS_SSL_ALERT_LEVEL_WARNING;
6636 ssl->out_msg[1] = MBEDTLS_SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00006637
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006638 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006639 goto write_msg;
6640 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006641#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006642 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006643#endif /* MBEDTLS_SSL_CLI_C */
6644#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006645 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00006646 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006647 if( mbedtls_ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006648 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006649 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
6650 return( MBEDTLS_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00006651 }
6652 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006653#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006654
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006655 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006656
6657 /*
6658 * 0 . 0 handshake type
6659 * 1 . 3 handshake length
6660 * 4 . 6 length of all certs
6661 * 7 . 9 length of cert. 1
6662 * 10 . n-1 peer certificate
6663 * n . n+2 length of cert. 2
6664 * n+3 . ... upper level cert, etc.
6665 */
6666 i = 7;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006667 crt = mbedtls_ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00006668
Paul Bakker29087132010-03-21 21:03:34 +00006669 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006670 {
6671 n = crt->raw.len;
Angus Grattond8213d02016-05-25 20:56:48 +10006672 if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00006673 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006674 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
Angus Grattond8213d02016-05-25 20:56:48 +10006675 i + 3 + n, MBEDTLS_SSL_OUT_CONTENT_LEN ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006676 return( MBEDTLS_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006677 }
6678
6679 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
6680 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
6681 ssl->out_msg[i + 2] = (unsigned char)( n );
6682
6683 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
6684 i += n; crt = crt->next;
6685 }
6686
6687 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
6688 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
6689 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
6690
6691 ssl->out_msglen = i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006692 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
6693 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
Paul Bakker5121ce52009-01-03 21:22:43 +00006694
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02006695#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00006696write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006697#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006698
6699 ssl->state++;
6700
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006701 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006702 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006703 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006704 return( ret );
6705 }
6706
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006707 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006708
Paul Bakkered27a042013-04-18 22:46:23 +02006709 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006710}
6711
Hanno Becker84879e32019-01-31 07:44:03 +00006712#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Hanno Becker177475a2019-02-05 17:02:46 +00006713
6714#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006715static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6716 unsigned char *crt_buf,
6717 size_t crt_buf_len )
6718{
6719 mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert;
6720
6721 if( peer_crt == NULL )
6722 return( -1 );
6723
6724 if( peer_crt->raw.len != crt_buf_len )
6725 return( -1 );
6726
Hanno Becker46f34d02019-02-08 14:00:04 +00006727 return( memcmp( peer_crt->raw.p, crt_buf, crt_buf_len ) );
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006728}
Hanno Becker177475a2019-02-05 17:02:46 +00006729#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6730static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6731 unsigned char *crt_buf,
6732 size_t crt_buf_len )
6733{
6734 int ret;
6735 unsigned char const * const peer_cert_digest =
6736 ssl->session->peer_cert_digest;
6737 mbedtls_md_type_t const peer_cert_digest_type =
6738 ssl->session->peer_cert_digest_type;
6739 mbedtls_md_info_t const * const digest_info =
6740 mbedtls_md_info_from_type( peer_cert_digest_type );
6741 unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN];
6742 size_t digest_len;
6743
6744 if( peer_cert_digest == NULL || digest_info == NULL )
6745 return( -1 );
6746
6747 digest_len = mbedtls_md_get_size( digest_info );
6748 if( digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN )
6749 return( -1 );
6750
6751 ret = mbedtls_md( digest_info, crt_buf, crt_buf_len, tmp_digest );
6752 if( ret != 0 )
6753 return( -1 );
6754
6755 return( memcmp( tmp_digest, peer_cert_digest, digest_len ) );
6756}
6757#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker84879e32019-01-31 07:44:03 +00006758#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006759
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006760/*
6761 * Once the certificate message is read, parse it into a cert chain and
6762 * perform basic checks, but leave actual verification to the caller
6763 */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006764static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl,
6765 mbedtls_x509_crt *chain )
Paul Bakker5121ce52009-01-03 21:22:43 +00006766{
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006767 int ret;
6768#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6769 int crt_cnt=0;
6770#endif
Paul Bakker23986e52011-04-24 08:57:21 +00006771 size_t i, n;
Gilles Peskine064a85c2017-05-10 10:46:40 +02006772 uint8_t alert;
Paul Bakker5121ce52009-01-03 21:22:43 +00006773
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006774 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00006775 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006776 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006777 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6778 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006779 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006780 }
6781
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006782 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE ||
6783 ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006784 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006785 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006786 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6787 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006788 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006789 }
6790
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006791 i = mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006792
Paul Bakker5121ce52009-01-03 21:22:43 +00006793 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006794 * Same message structure as in mbedtls_ssl_write_certificate()
Paul Bakker5121ce52009-01-03 21:22:43 +00006795 */
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006796 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
Paul Bakker5121ce52009-01-03 21:22:43 +00006797
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006798 if( ssl->in_msg[i] != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006799 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00006800 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006801 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006802 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6803 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006804 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006805 }
6806
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006807 /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */
6808 i += 3;
6809
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006810 /* Iterate through and parse the CRTs in the provided chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006811 while( i < ssl->in_hslen )
6812 {
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006813 /* Check that there's room for the next CRT's length fields. */
Philippe Antoine747fd532018-05-30 09:13:21 +02006814 if ( i + 3 > ssl->in_hslen ) {
6815 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckere2734e22019-01-31 07:44:17 +00006816 mbedtls_ssl_send_alert_message( ssl,
6817 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6818 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Philippe Antoine747fd532018-05-30 09:13:21 +02006819 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
6820 }
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006821 /* In theory, the CRT can be up to 2**24 Bytes, but we don't support
6822 * anything beyond 2**16 ~ 64K. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006823 if( ssl->in_msg[i] != 0 )
6824 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006825 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckere2734e22019-01-31 07:44:17 +00006826 mbedtls_ssl_send_alert_message( ssl,
6827 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6828 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006829 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006830 }
6831
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006832 /* Read length of the next CRT in the chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006833 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
6834 | (unsigned int) ssl->in_msg[i + 2];
6835 i += 3;
6836
6837 if( n < 128 || i + n > ssl->in_hslen )
6838 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006839 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckere2734e22019-01-31 07:44:17 +00006840 mbedtls_ssl_send_alert_message( ssl,
6841 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6842 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006843 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006844 }
6845
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006846 /* Check if we're handling the first CRT in the chain. */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006847#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6848 if( crt_cnt++ == 0 &&
6849 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
6850 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006851 {
Hanno Becker46f34d02019-02-08 14:00:04 +00006852 /* During client-side renegotiation, check that the server's
6853 * end-CRTs hasn't changed compared to the initial handshake,
6854 * mitigating the triple handshake attack. On success, reuse
6855 * the original end-CRT instead of parsing it again. */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006856 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Check that peer CRT hasn't changed during renegotiation" ) );
6857 if( ssl_check_peer_crt_unchanged( ssl,
6858 &ssl->in_msg[i],
6859 n ) != 0 )
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006860 {
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006861 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
6862 mbedtls_ssl_send_alert_message( ssl,
6863 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6864 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
6865 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006866 }
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006867
6868 /* Now we can safely free the original chain. */
6869 ssl_clear_peer_cert( ssl->session );
6870 }
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006871#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
6872
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006873 /* Parse the next certificate in the chain. */
Hanno Becker0056eab2019-02-08 14:39:16 +00006874#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006875 ret = mbedtls_x509_crt_parse_der( chain, ssl->in_msg + i, n );
Hanno Becker0056eab2019-02-08 14:39:16 +00006876#else
Hanno Becker353a6f02019-02-26 11:51:34 +00006877 /* If we don't need to store the CRT chain permanently, parse
Hanno Becker0056eab2019-02-08 14:39:16 +00006878 * it in-place from the input buffer instead of making a copy. */
6879 ret = mbedtls_x509_crt_parse_der_nocopy( chain, ssl->in_msg + i, n );
6880#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006881 switch( ret )
Paul Bakker5121ce52009-01-03 21:22:43 +00006882 {
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006883 case 0: /*ok*/
6884 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
6885 /* Ignore certificate with an unknown algorithm: maybe a
6886 prior certificate was already trusted. */
6887 break;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006888
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006889 case MBEDTLS_ERR_X509_ALLOC_FAILED:
6890 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
6891 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006892
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006893 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
6894 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6895 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006896
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006897 default:
6898 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6899 crt_parse_der_failed:
6900 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert );
6901 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
6902 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006903 }
6904
6905 i += n;
6906 }
6907
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006908 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", chain );
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006909 return( 0 );
6910}
6911
Hanno Becker4a55f632019-02-05 12:49:06 +00006912#if defined(MBEDTLS_SSL_SRV_C)
6913static int ssl_srv_check_client_no_crt_notification( mbedtls_ssl_context *ssl )
6914{
6915 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
6916 return( -1 );
6917
6918#if defined(MBEDTLS_SSL_PROTO_SSL3)
6919 /*
6920 * Check if the client sent an empty certificate
6921 */
6922 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
6923 {
6924 if( ssl->in_msglen == 2 &&
6925 ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT &&
6926 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6927 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
6928 {
6929 MBEDTLS_SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
6930 return( 0 );
6931 }
6932
6933 return( -1 );
6934 }
6935#endif /* MBEDTLS_SSL_PROTO_SSL3 */
6936
6937#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
6938 defined(MBEDTLS_SSL_PROTO_TLS1_2)
6939 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
6940 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
6941 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
6942 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
6943 {
6944 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
6945 return( 0 );
6946 }
6947
6948 return( -1 );
6949#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
6950 MBEDTLS_SSL_PROTO_TLS1_2 */
6951}
6952#endif /* MBEDTLS_SSL_SRV_C */
6953
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006954/* Check if a certificate message is expected.
6955 * Return either
6956 * - SSL_CERTIFICATE_EXPECTED, or
6957 * - SSL_CERTIFICATE_SKIP
6958 * indicating whether a Certificate message is expected or not.
6959 */
6960#define SSL_CERTIFICATE_EXPECTED 0
6961#define SSL_CERTIFICATE_SKIP 1
6962static int ssl_parse_certificate_coordinate( mbedtls_ssl_context *ssl,
6963 int authmode )
6964{
6965 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00006966 ssl->handshake->ciphersuite_info;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006967
6968 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6969 return( SSL_CERTIFICATE_SKIP );
6970
6971#if defined(MBEDTLS_SSL_SRV_C)
6972 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
6973 {
6974 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
6975 return( SSL_CERTIFICATE_SKIP );
6976
6977 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6978 {
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006979 ssl->session_negotiate->verify_result =
6980 MBEDTLS_X509_BADCERT_SKIP_VERIFY;
6981 return( SSL_CERTIFICATE_SKIP );
6982 }
6983 }
Hanno Becker84d9d272019-03-01 08:10:46 +00006984#else
6985 ((void) authmode);
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006986#endif /* MBEDTLS_SSL_SRV_C */
6987
6988 return( SSL_CERTIFICATE_EXPECTED );
6989}
6990
Hanno Becker68636192019-02-05 14:36:34 +00006991static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl,
6992 int authmode,
6993 mbedtls_x509_crt *chain,
6994 void *rs_ctx )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006995{
Hanno Becker6bdfab22019-02-05 13:11:17 +00006996 int ret = 0;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006997 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00006998 ssl->handshake->ciphersuite_info;
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006999 int have_ca_chain = 0;
Hanno Becker68636192019-02-05 14:36:34 +00007000
Hanno Becker8927c832019-04-03 12:52:50 +01007001 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *);
7002 void *p_vrfy;
7003
Hanno Becker68636192019-02-05 14:36:34 +00007004 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
7005 return( 0 );
7006
Hanno Becker8927c832019-04-03 12:52:50 +01007007 if( ssl->f_vrfy != NULL )
7008 {
Hanno Beckerefb440a2019-04-03 13:04:33 +01007009 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use context-specific verification callback" ) );
Hanno Becker8927c832019-04-03 12:52:50 +01007010 f_vrfy = ssl->f_vrfy;
7011 p_vrfy = ssl->p_vrfy;
7012 }
7013 else
7014 {
Hanno Beckerefb440a2019-04-03 13:04:33 +01007015 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use configuration-specific verification callback" ) );
Hanno Becker8927c832019-04-03 12:52:50 +01007016 f_vrfy = ssl->conf->f_vrfy;
7017 p_vrfy = ssl->conf->p_vrfy;
7018 }
7019
Hanno Becker68636192019-02-05 14:36:34 +00007020 /*
7021 * Main check: verify certificate
7022 */
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00007023#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
7024 if( ssl->conf->f_ca_cb != NULL )
7025 {
7026 ((void) rs_ctx);
7027 have_ca_chain = 1;
7028
7029 MBEDTLS_SSL_DEBUG_MSG( 3, ( "use CA callback for X.509 CRT verification" ) );
Jarno Lamsa9822c0d2019-04-01 16:59:48 +03007030 ret = mbedtls_x509_crt_verify_with_ca_cb(
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00007031 chain,
7032 ssl->conf->f_ca_cb,
7033 ssl->conf->p_ca_cb,
7034 ssl->conf->cert_profile,
7035 ssl->hostname,
7036 &ssl->session_negotiate->verify_result,
Jaeden Amerofe710672019-04-16 15:03:12 +01007037 f_vrfy, p_vrfy );
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00007038 }
7039 else
7040#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
7041 {
7042 mbedtls_x509_crt *ca_chain;
7043 mbedtls_x509_crl *ca_crl;
7044
7045#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7046 if( ssl->handshake->sni_ca_chain != NULL )
7047 {
7048 ca_chain = ssl->handshake->sni_ca_chain;
7049 ca_crl = ssl->handshake->sni_ca_crl;
7050 }
7051 else
7052#endif
7053 {
7054 ca_chain = ssl->conf->ca_chain;
7055 ca_crl = ssl->conf->ca_crl;
7056 }
7057
7058 if( ca_chain != NULL )
7059 have_ca_chain = 1;
7060
7061 ret = mbedtls_x509_crt_verify_restartable(
7062 chain,
7063 ca_chain, ca_crl,
7064 ssl->conf->cert_profile,
7065 ssl->hostname,
7066 &ssl->session_negotiate->verify_result,
Jaeden Amerofe710672019-04-16 15:03:12 +01007067 f_vrfy, p_vrfy, rs_ctx );
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00007068 }
Hanno Becker68636192019-02-05 14:36:34 +00007069
7070 if( ret != 0 )
7071 {
7072 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
7073 }
7074
7075#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
7076 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
7077 return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS );
7078#endif
7079
7080 /*
7081 * Secondary checks: always done, but change 'ret' only if it was 0
7082 */
7083
7084#if defined(MBEDTLS_ECP_C)
7085 {
7086 const mbedtls_pk_context *pk = &chain->pk;
7087
7088 /* If certificate uses an EC key, make sure the curve is OK */
7089 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) &&
7090 mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id ) != 0 )
7091 {
7092 ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
7093
7094 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
7095 if( ret == 0 )
7096 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
7097 }
7098 }
7099#endif /* MBEDTLS_ECP_C */
7100
7101 if( mbedtls_ssl_check_cert_usage( chain,
7102 ciphersuite_info,
7103 ! ssl->conf->endpoint,
7104 &ssl->session_negotiate->verify_result ) != 0 )
7105 {
7106 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
7107 if( ret == 0 )
7108 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
7109 }
7110
7111 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
7112 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
7113 * with details encoded in the verification flags. All other kinds
7114 * of error codes, including those from the user provided f_vrfy
7115 * functions, are treated as fatal and lead to a failure of
7116 * ssl_parse_certificate even if verification was optional. */
7117 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
7118 ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
7119 ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ) )
7120 {
7121 ret = 0;
7122 }
7123
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00007124 if( have_ca_chain == 0 && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
Hanno Becker68636192019-02-05 14:36:34 +00007125 {
7126 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
7127 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
7128 }
7129
7130 if( ret != 0 )
7131 {
7132 uint8_t alert;
7133
7134 /* The certificate may have been rejected for several reasons.
7135 Pick one and send the corresponding alert. Which alert to send
7136 may be a subject of debate in some cases. */
7137 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
7138 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
7139 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
7140 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
7141 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
7142 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7143 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
7144 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7145 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
7146 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7147 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
7148 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7149 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
7150 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7151 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
7152 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
7153 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
7154 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
7155 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
7156 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
7157 else
7158 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
7159 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7160 alert );
7161 }
7162
7163#if defined(MBEDTLS_DEBUG_C)
7164 if( ssl->session_negotiate->verify_result != 0 )
7165 {
7166 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %x",
7167 ssl->session_negotiate->verify_result ) );
7168 }
7169 else
7170 {
7171 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
7172 }
7173#endif /* MBEDTLS_DEBUG_C */
7174
7175 return( ret );
7176}
7177
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007178#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
7179static int ssl_remember_peer_crt_digest( mbedtls_ssl_context *ssl,
7180 unsigned char *start, size_t len )
7181{
7182 int ret;
7183 /* Remember digest of the peer's end-CRT. */
7184 ssl->session_negotiate->peer_cert_digest =
7185 mbedtls_calloc( 1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN );
7186 if( ssl->session_negotiate->peer_cert_digest == NULL )
7187 {
7188 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
7189 sizeof( MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ) ) );
7190 mbedtls_ssl_send_alert_message( ssl,
7191 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7192 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
7193
7194 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
7195 }
7196
7197 ret = mbedtls_md( mbedtls_md_info_from_type(
7198 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE ),
7199 start, len,
7200 ssl->session_negotiate->peer_cert_digest );
7201
7202 ssl->session_negotiate->peer_cert_digest_type =
7203 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
7204 ssl->session_negotiate->peer_cert_digest_len =
7205 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
7206
7207 return( ret );
7208}
7209
7210static int ssl_remember_peer_pubkey( mbedtls_ssl_context *ssl,
7211 unsigned char *start, size_t len )
7212{
7213 unsigned char *end = start + len;
7214 int ret;
7215
7216 /* Make a copy of the peer's raw public key. */
7217 mbedtls_pk_init( &ssl->handshake->peer_pubkey );
7218 ret = mbedtls_pk_parse_subpubkey( &start, end,
7219 &ssl->handshake->peer_pubkey );
7220 if( ret != 0 )
7221 {
7222 /* We should have parsed the public key before. */
7223 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
7224 }
7225
7226 return( 0 );
7227}
7228#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7229
Hanno Becker68636192019-02-05 14:36:34 +00007230int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
7231{
7232 int ret = 0;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00007233 int crt_expected;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007234#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7235 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
7236 ? ssl->handshake->sni_authmode
7237 : ssl->conf->authmode;
7238#else
7239 const int authmode = ssl->conf->authmode;
7240#endif
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007241 void *rs_ctx = NULL;
Hanno Becker3dad3112019-02-05 17:19:52 +00007242 mbedtls_x509_crt *chain = NULL;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007243
7244 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
7245
Hanno Becker28f2fcd2019-02-07 10:11:07 +00007246 crt_expected = ssl_parse_certificate_coordinate( ssl, authmode );
7247 if( crt_expected == SSL_CERTIFICATE_SKIP )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007248 {
7249 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Hanno Becker6bdfab22019-02-05 13:11:17 +00007250 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007251 }
7252
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007253#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
7254 if( ssl->handshake->ecrs_enabled &&
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02007255 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify )
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007256 {
Hanno Becker3dad3112019-02-05 17:19:52 +00007257 chain = ssl->handshake->ecrs_peer_cert;
7258 ssl->handshake->ecrs_peer_cert = NULL;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007259 goto crt_verify;
7260 }
7261#endif
7262
Manuel Pégourié-Gonnard125af942018-09-11 11:08:12 +02007263 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007264 {
7265 /* mbedtls_ssl_read_record may have sent an alert already. We
7266 let it decide whether to alert. */
7267 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Hanno Becker3dad3112019-02-05 17:19:52 +00007268 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007269 }
7270
Hanno Becker4a55f632019-02-05 12:49:06 +00007271#if defined(MBEDTLS_SSL_SRV_C)
7272 if( ssl_srv_check_client_no_crt_notification( ssl ) == 0 )
7273 {
7274 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Hanno Becker4a55f632019-02-05 12:49:06 +00007275
7276 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
Hanno Becker6bdfab22019-02-05 13:11:17 +00007277 ret = 0;
7278 else
7279 ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
Hanno Becker4a55f632019-02-05 12:49:06 +00007280
Hanno Becker6bdfab22019-02-05 13:11:17 +00007281 goto exit;
Hanno Becker4a55f632019-02-05 12:49:06 +00007282 }
7283#endif /* MBEDTLS_SSL_SRV_C */
7284
Hanno Beckerc7bd7802019-02-05 15:37:23 +00007285 /* Clear existing peer CRT structure in case we tried to
7286 * reuse a session but it failed, and allocate a new one. */
Hanno Becker7a955a02019-02-05 13:08:01 +00007287 ssl_clear_peer_cert( ssl->session_negotiate );
Hanno Becker3dad3112019-02-05 17:19:52 +00007288
7289 chain = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
7290 if( chain == NULL )
Hanno Beckerc7bd7802019-02-05 15:37:23 +00007291 {
7292 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
7293 sizeof( mbedtls_x509_crt ) ) );
7294 mbedtls_ssl_send_alert_message( ssl,
7295 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7296 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Hanno Becker7a955a02019-02-05 13:08:01 +00007297
Hanno Becker3dad3112019-02-05 17:19:52 +00007298 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
7299 goto exit;
7300 }
7301 mbedtls_x509_crt_init( chain );
7302
7303 ret = ssl_parse_certificate_chain( ssl, chain );
Hanno Beckerc7bd7802019-02-05 15:37:23 +00007304 if( ret != 0 )
Hanno Becker3dad3112019-02-05 17:19:52 +00007305 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007306
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007307#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
7308 if( ssl->handshake->ecrs_enabled)
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02007309 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007310
7311crt_verify:
7312 if( ssl->handshake->ecrs_enabled)
7313 rs_ctx = &ssl->handshake->ecrs_ctx;
7314#endif
7315
Hanno Becker68636192019-02-05 14:36:34 +00007316 ret = ssl_parse_certificate_verify( ssl, authmode,
Hanno Becker3dad3112019-02-05 17:19:52 +00007317 chain, rs_ctx );
Hanno Becker68636192019-02-05 14:36:34 +00007318 if( ret != 0 )
Hanno Becker3dad3112019-02-05 17:19:52 +00007319 goto exit;
Paul Bakker5121ce52009-01-03 21:22:43 +00007320
Hanno Becker6bbd94c2019-02-05 17:02:28 +00007321#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Becker6bbd94c2019-02-05 17:02:28 +00007322 {
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007323 unsigned char *crt_start, *pk_start;
7324 size_t crt_len, pk_len;
Hanno Becker3dad3112019-02-05 17:19:52 +00007325
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007326 /* We parse the CRT chain without copying, so
7327 * these pointers point into the input buffer,
7328 * and are hence still valid after freeing the
7329 * CRT chain. */
Hanno Becker6bbd94c2019-02-05 17:02:28 +00007330
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007331 crt_start = chain->raw.p;
7332 crt_len = chain->raw.len;
Hanno Becker6bbd94c2019-02-05 17:02:28 +00007333
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007334 pk_start = chain->pk_raw.p;
7335 pk_len = chain->pk_raw.len;
7336
7337 /* Free the CRT structures before computing
7338 * digest and copying the peer's public key. */
7339 mbedtls_x509_crt_free( chain );
7340 mbedtls_free( chain );
7341 chain = NULL;
7342
7343 ret = ssl_remember_peer_crt_digest( ssl, crt_start, crt_len );
Hanno Beckera2747532019-02-06 16:19:04 +00007344 if( ret != 0 )
Hanno Beckera2747532019-02-06 16:19:04 +00007345 goto exit;
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007346
7347 ret = ssl_remember_peer_pubkey( ssl, pk_start, pk_len );
7348 if( ret != 0 )
7349 goto exit;
Hanno Beckera2747532019-02-06 16:19:04 +00007350 }
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007351#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7352 /* Pass ownership to session structure. */
Hanno Becker3dad3112019-02-05 17:19:52 +00007353 ssl->session_negotiate->peer_cert = chain;
7354 chain = NULL;
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007355#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker3dad3112019-02-05 17:19:52 +00007356
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007357 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007358
Hanno Becker6bdfab22019-02-05 13:11:17 +00007359exit:
7360
Hanno Becker3dad3112019-02-05 17:19:52 +00007361 if( ret == 0 )
7362 ssl->state++;
7363
7364#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
7365 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
7366 {
7367 ssl->handshake->ecrs_peer_cert = chain;
7368 chain = NULL;
7369 }
7370#endif
7371
7372 if( chain != NULL )
7373 {
7374 mbedtls_x509_crt_free( chain );
7375 mbedtls_free( chain );
7376 }
7377
Paul Bakker5121ce52009-01-03 21:22:43 +00007378 return( ret );
7379}
Hanno Becker21489932019-02-05 13:20:55 +00007380#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00007381
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007382int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007383{
7384 int ret;
7385
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007386 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007387
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007388 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00007389 ssl->out_msglen = 1;
7390 ssl->out_msg[0] = 1;
7391
Paul Bakker5121ce52009-01-03 21:22:43 +00007392 ssl->state++;
7393
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007394 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007395 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007396 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007397 return( ret );
7398 }
7399
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007400 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007401
7402 return( 0 );
7403}
7404
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007405int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007406{
7407 int ret;
7408
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007409 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007410
Hanno Becker327c93b2018-08-15 13:56:18 +01007411 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007412 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007413 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007414 return( ret );
7415 }
7416
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007417 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Paul Bakker5121ce52009-01-03 21:22:43 +00007418 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007419 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007420 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7421 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007422 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007423 }
7424
Hanno Beckere678eaa2018-08-21 14:57:46 +01007425 /* CCS records are only accepted if they have length 1 and content '1',
7426 * so we don't need to check this here. */
Paul Bakker5121ce52009-01-03 21:22:43 +00007427
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007428 /*
7429 * Switch to our negotiated transform and session parameters for inbound
7430 * data.
7431 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007432 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007433 ssl->transform_in = ssl->transform_negotiate;
7434 ssl->session_in = ssl->session_negotiate;
7435
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007436#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007437 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007438 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007439#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007440 ssl_dtls_replay_reset( ssl );
7441#endif
7442
7443 /* Increment epoch */
7444 if( ++ssl->in_epoch == 0 )
7445 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007446 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007447 /* This is highly unlikely to happen for legitimate reasons, so
7448 treat it as an attack and don't send an alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007449 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007450 }
7451 }
7452 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007453#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007454 memset( ssl->in_ctr, 0, 8 );
7455
Hanno Becker79594fd2019-05-08 09:38:41 +01007456 ssl_update_in_pointers( ssl );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007457
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007458#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7459 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007460 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007461 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_INBOUND ) ) != 0 )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007462 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007463 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007464 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7465 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007466 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007467 }
7468 }
7469#endif
7470
Paul Bakker5121ce52009-01-03 21:22:43 +00007471 ssl->state++;
7472
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007473 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007474
7475 return( 0 );
7476}
7477
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007478void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl,
7479 const mbedtls_ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00007480{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02007481 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01007482
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007483#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7484 defined(MBEDTLS_SSL_PROTO_TLS1_1)
7485 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00007486 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00007487 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007488#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007489#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7490#if defined(MBEDTLS_SHA512_C)
7491 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007492 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
7493 else
7494#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007495#if defined(MBEDTLS_SHA256_C)
7496 if( ciphersuite_info->mac != MBEDTLS_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00007497 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007498 else
7499#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007500#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02007501 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007502 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007503 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02007504 }
Paul Bakker380da532012-04-18 16:10:25 +00007505}
Paul Bakkerf7abd422013-04-16 13:15:56 +02007506
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007507void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007508{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007509#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7510 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007511 mbedtls_md5_starts_ret( &ssl->handshake->fin_md5 );
7512 mbedtls_sha1_starts_ret( &ssl->handshake->fin_sha1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007513#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007514#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7515#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007516#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek2ad22972019-01-30 03:32:12 -05007517 psa_hash_abort( &ssl->handshake->fin_sha256_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007518 psa_hash_setup( &ssl->handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
7519#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007520 mbedtls_sha256_starts_ret( &ssl->handshake->fin_sha256, 0 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007521#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007522#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007523#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007524#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek2ad22972019-01-30 03:32:12 -05007525 psa_hash_abort( &ssl->handshake->fin_sha384_psa );
Andrzej Kurek972fba52019-01-30 03:29:12 -05007526 psa_hash_setup( &ssl->handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007527#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007528 mbedtls_sha512_starts_ret( &ssl->handshake->fin_sha512, 1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007529#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007530#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007531#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007532}
7533
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007534static void ssl_update_checksum_start( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007535 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00007536{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007537#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7538 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007539 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
7540 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007541#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007542#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7543#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007544#if defined(MBEDTLS_USE_PSA_CRYPTO)
7545 psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len );
7546#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007547 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007548#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007549#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007550#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007551#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05007552 psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007553#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007554 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01007555#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007556#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007557#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00007558}
7559
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007560#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7561 defined(MBEDTLS_SSL_PROTO_TLS1_1)
7562static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007563 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00007564{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007565 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
7566 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00007567}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007568#endif
Paul Bakker380da532012-04-18 16:10:25 +00007569
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007570#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7571#if defined(MBEDTLS_SHA256_C)
7572static void ssl_update_checksum_sha256( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007573 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00007574{
Andrzej Kurekeb342242019-01-29 09:14:33 -05007575#if defined(MBEDTLS_USE_PSA_CRYPTO)
7576 psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len );
7577#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007578 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007579#endif
Paul Bakker380da532012-04-18 16:10:25 +00007580}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007581#endif
Paul Bakker380da532012-04-18 16:10:25 +00007582
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007583#if defined(MBEDTLS_SHA512_C)
7584static void ssl_update_checksum_sha384( 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{
Andrzej Kurekeb342242019-01-29 09:14:33 -05007587#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05007588 psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007589#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007590 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007591#endif
Paul Bakker380da532012-04-18 16:10:25 +00007592}
Paul Bakker769075d2012-11-24 11:26:46 +01007593#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007594#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00007595
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007596#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00007597static void ssl_calc_finished_ssl(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007598 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00007599{
Paul Bakker3c2122f2013-06-24 19:03:14 +02007600 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007601 mbedtls_md5_context md5;
7602 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007603
Paul Bakker5121ce52009-01-03 21:22:43 +00007604 unsigned char padbuf[48];
7605 unsigned char md5sum[16];
7606 unsigned char sha1sum[20];
7607
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007608 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007609 if( !session )
7610 session = ssl->session;
7611
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007612 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007613
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007614 mbedtls_md5_init( &md5 );
7615 mbedtls_sha1_init( &sha1 );
7616
7617 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
7618 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007619
7620 /*
7621 * SSLv3:
7622 * hash =
7623 * MD5( master + pad2 +
7624 * MD5( handshake + sender + master + pad1 ) )
7625 * + SHA1( master + pad2 +
7626 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00007627 */
7628
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007629#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007630 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
7631 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007632#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007633
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007634#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007635 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
7636 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007637#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007638
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007639 sender = ( from == MBEDTLS_SSL_IS_CLIENT ) ? "CLNT"
Paul Bakker3c2122f2013-06-24 19:03:14 +02007640 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00007641
Paul Bakker1ef83d62012-04-11 12:09:53 +00007642 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007643
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007644 mbedtls_md5_update_ret( &md5, (const unsigned char *) sender, 4 );
7645 mbedtls_md5_update_ret( &md5, session->master, 48 );
7646 mbedtls_md5_update_ret( &md5, padbuf, 48 );
7647 mbedtls_md5_finish_ret( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00007648
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007649 mbedtls_sha1_update_ret( &sha1, (const unsigned char *) sender, 4 );
7650 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
7651 mbedtls_sha1_update_ret( &sha1, padbuf, 40 );
7652 mbedtls_sha1_finish_ret( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00007653
Paul Bakker1ef83d62012-04-11 12:09:53 +00007654 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007655
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007656 mbedtls_md5_starts_ret( &md5 );
7657 mbedtls_md5_update_ret( &md5, session->master, 48 );
7658 mbedtls_md5_update_ret( &md5, padbuf, 48 );
7659 mbedtls_md5_update_ret( &md5, md5sum, 16 );
7660 mbedtls_md5_finish_ret( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00007661
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007662 mbedtls_sha1_starts_ret( &sha1 );
7663 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
7664 mbedtls_sha1_update_ret( &sha1, padbuf , 40 );
7665 mbedtls_sha1_update_ret( &sha1, sha1sum, 20 );
7666 mbedtls_sha1_finish_ret( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007667
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007668 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007669
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007670 mbedtls_md5_free( &md5 );
7671 mbedtls_sha1_free( &sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007672
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007673 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
7674 mbedtls_platform_zeroize( md5sum, sizeof( md5sum ) );
7675 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007676
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007677 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007678}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007679#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00007680
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007681#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00007682static void ssl_calc_finished_tls(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007683 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00007684{
Paul Bakker1ef83d62012-04-11 12:09:53 +00007685 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02007686 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007687 mbedtls_md5_context md5;
7688 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007689 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00007690
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007691 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007692 if( !session )
7693 session = ssl->session;
7694
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007695 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007696
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007697 mbedtls_md5_init( &md5 );
7698 mbedtls_sha1_init( &sha1 );
7699
7700 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
7701 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007702
Paul Bakker1ef83d62012-04-11 12:09:53 +00007703 /*
7704 * TLSv1:
7705 * hash = PRF( master, finished_label,
7706 * MD5( handshake ) + SHA1( handshake ) )[0..11]
7707 */
Paul Bakker5121ce52009-01-03 21:22:43 +00007708
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007709#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007710 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
7711 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007712#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007713
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007714#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007715 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
7716 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007717#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007718
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007719 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02007720 ? "client finished"
7721 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00007722
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007723 mbedtls_md5_finish_ret( &md5, padbuf );
7724 mbedtls_sha1_finish_ret( &sha1, padbuf + 16 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007725
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007726 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00007727 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007728
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007729 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007730
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007731 mbedtls_md5_free( &md5 );
7732 mbedtls_sha1_free( &sha1 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007733
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007734 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007735
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007736 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007737}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007738#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00007739
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007740#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7741#if defined(MBEDTLS_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00007742static void ssl_calc_finished_tls_sha256(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007743 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker1ef83d62012-04-11 12:09:53 +00007744{
7745 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02007746 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007747 unsigned char padbuf[32];
Andrzej Kurekeb342242019-01-29 09:14:33 -05007748#if defined(MBEDTLS_USE_PSA_CRYPTO)
7749 size_t hash_size;
Jaeden Amero34973232019-02-20 10:32:28 +00007750 psa_hash_operation_t sha256_psa = PSA_HASH_OPERATION_INIT;
Andrzej Kurekeb342242019-01-29 09:14:33 -05007751 psa_status_t status;
7752#else
7753 mbedtls_sha256_context sha256;
7754#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007755
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007756 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007757 if( !session )
7758 session = ssl->session;
7759
Andrzej Kurekeb342242019-01-29 09:14:33 -05007760 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
7761 ? "client finished"
7762 : "server finished";
7763
7764#if defined(MBEDTLS_USE_PSA_CRYPTO)
7765 sha256_psa = psa_hash_operation_init();
7766
7767 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha256" ) );
7768
7769 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
7770 if( status != PSA_SUCCESS )
7771 {
7772 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
7773 return;
7774 }
7775
7776 status = psa_hash_finish( &sha256_psa, padbuf, sizeof( padbuf ), &hash_size );
7777 if( status != PSA_SUCCESS )
7778 {
7779 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
7780 return;
7781 }
7782 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 32 );
7783#else
7784
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007785 mbedtls_sha256_init( &sha256 );
7786
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007787 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007788
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007789 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007790
7791 /*
7792 * TLSv1.2:
7793 * hash = PRF( master, finished_label,
7794 * Hash( handshake ) )[0.11]
7795 */
7796
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007797#if !defined(MBEDTLS_SHA256_ALT)
7798 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007799 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007800#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007801
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007802 mbedtls_sha256_finish_ret( &sha256, padbuf );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007803 mbedtls_sha256_free( &sha256 );
7804#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker1ef83d62012-04-11 12:09:53 +00007805
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007806 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00007807 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007808
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007809 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007810
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007811 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007812
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007813 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007814}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007815#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00007816
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007817#if defined(MBEDTLS_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00007818static void ssl_calc_finished_tls_sha384(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007819 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakkerca4ab492012-04-18 14:23:57 +00007820{
7821 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02007822 const char *sender;
Paul Bakkerca4ab492012-04-18 14:23:57 +00007823 unsigned char padbuf[48];
Andrzej Kurekeb342242019-01-29 09:14:33 -05007824#if defined(MBEDTLS_USE_PSA_CRYPTO)
7825 size_t hash_size;
Jaeden Amero34973232019-02-20 10:32:28 +00007826 psa_hash_operation_t sha384_psa = PSA_HASH_OPERATION_INIT;
Andrzej Kurekeb342242019-01-29 09:14:33 -05007827 psa_status_t status;
7828#else
7829 mbedtls_sha512_context sha512;
7830#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00007831
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007832 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007833 if( !session )
7834 session = ssl->session;
7835
Andrzej Kurekeb342242019-01-29 09:14:33 -05007836 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
7837 ? "client finished"
7838 : "server finished";
7839
7840#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05007841 sha384_psa = psa_hash_operation_init();
Andrzej Kurekeb342242019-01-29 09:14:33 -05007842
7843 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha384" ) );
7844
Andrzej Kurek972fba52019-01-30 03:29:12 -05007845 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007846 if( status != PSA_SUCCESS )
7847 {
7848 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
7849 return;
7850 }
7851
Andrzej Kurek972fba52019-01-30 03:29:12 -05007852 status = psa_hash_finish( &sha384_psa, padbuf, sizeof( padbuf ), &hash_size );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007853 if( status != PSA_SUCCESS )
7854 {
7855 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
7856 return;
7857 }
7858 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 48 );
7859#else
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007860 mbedtls_sha512_init( &sha512 );
7861
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007862 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007863
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007864 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007865
7866 /*
7867 * TLSv1.2:
7868 * hash = PRF( master, finished_label,
7869 * Hash( handshake ) )[0.11]
7870 */
7871
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007872#if !defined(MBEDTLS_SHA512_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007873 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
7874 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007875#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00007876
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007877 mbedtls_sha512_finish_ret( &sha512, padbuf );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007878 mbedtls_sha512_free( &sha512 );
7879#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00007880
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007881 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00007882 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007883
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007884 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007885
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007886 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007887
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007888 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007889}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007890#endif /* MBEDTLS_SHA512_C */
7891#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00007892
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007893static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00007894{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007895 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007896
7897 /*
7898 * Free our handshake params
7899 */
Gilles Peskine9b562d52018-04-25 20:32:43 +02007900 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007901 mbedtls_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00007902 ssl->handshake = NULL;
7903
7904 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007905 * Free the previous transform and swith in the current one
Paul Bakker48916f92012-09-16 19:57:18 +00007906 */
7907 if( ssl->transform )
7908 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007909 mbedtls_ssl_transform_free( ssl->transform );
7910 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00007911 }
7912 ssl->transform = ssl->transform_negotiate;
7913 ssl->transform_negotiate = NULL;
7914
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007915 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007916}
7917
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007918void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007919{
7920 int resume = ssl->handshake->resume;
7921
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007922 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007923
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007924#if defined(MBEDTLS_SSL_RENEGOTIATION)
7925 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007926 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007927 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007928 ssl->renego_records_seen = 0;
7929 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007930#endif
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007931
7932 /*
7933 * Free the previous session and switch in the current one
7934 */
Paul Bakker0a597072012-09-25 21:55:46 +00007935 if( ssl->session )
7936 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007937#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01007938 /* RFC 7366 3.1: keep the EtM state */
7939 ssl->session_negotiate->encrypt_then_mac =
7940 ssl->session->encrypt_then_mac;
7941#endif
7942
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007943 mbedtls_ssl_session_free( ssl->session );
7944 mbedtls_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00007945 }
7946 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007947 ssl->session_negotiate = NULL;
7948
Paul Bakker0a597072012-09-25 21:55:46 +00007949 /*
7950 * Add cache entry
7951 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007952 if( ssl->conf->f_set_cache != NULL &&
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02007953 ssl->session->id_len != 0 &&
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02007954 resume == 0 )
7955 {
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007956 if( ssl->conf->f_set_cache( ssl->conf->p_cache, ssl->session ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007957 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02007958 }
Paul Bakker0a597072012-09-25 21:55:46 +00007959
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007960#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007961 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007962 ssl->handshake->flight != NULL )
7963 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02007964 /* Cancel handshake timer */
7965 ssl_set_timer( ssl, 0 );
7966
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007967 /* Keep last flight around in case we need to resend it:
7968 * we need the handshake and transform structures for that */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007969 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007970 }
7971 else
7972#endif
7973 ssl_handshake_wrapup_free_hs_transform( ssl );
7974
Paul Bakker48916f92012-09-16 19:57:18 +00007975 ssl->state++;
7976
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007977 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007978}
7979
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007980int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
Paul Bakker1ef83d62012-04-11 12:09:53 +00007981{
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007982 int ret, hash_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007983
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007984 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007985
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007986 ssl_update_out_pointers( ssl, ssl->transform_negotiate );
Paul Bakker92be97b2013-01-02 17:30:03 +01007987
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007988 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007989
Manuel Pégourié-Gonnard214a8482016-02-22 11:27:26 +01007990 /*
7991 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
7992 * may define some other value. Currently (early 2016), no defined
7993 * ciphersuite does this (and this is unlikely to change as activity has
7994 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
7995 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007996 hash_len = ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) ? 36 : 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00007997
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007998#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00007999 ssl->verify_data_len = hash_len;
8000 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008001#endif
Paul Bakker48916f92012-09-16 19:57:18 +00008002
Paul Bakker5121ce52009-01-03 21:22:43 +00008003 ssl->out_msglen = 4 + hash_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008004 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
8005 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
Paul Bakker5121ce52009-01-03 21:22:43 +00008006
8007 /*
8008 * In case of session resuming, invert the client and server
8009 * ChangeCipherSpec messages order.
8010 */
Paul Bakker0a597072012-09-25 21:55:46 +00008011 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008012 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008013#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008014 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008015 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01008016#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008017#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008018 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008019 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01008020#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00008021 }
8022 else
8023 ssl->state++;
8024
Paul Bakker48916f92012-09-16 19:57:18 +00008025 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02008026 * Switch to our negotiated transform and session parameters for outbound
8027 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00008028 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008029 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01008030
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008031#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008032 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02008033 {
8034 unsigned char i;
8035
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008036 /* Remember current epoch settings for resending */
8037 ssl->handshake->alt_transform_out = ssl->transform_out;
Hanno Becker19859472018-08-06 09:40:20 +01008038 memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008039
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02008040 /* Set sequence_number to zero */
Hanno Becker19859472018-08-06 09:40:20 +01008041 memset( ssl->cur_out_ctr + 2, 0, 6 );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02008042
8043 /* Increment epoch */
8044 for( i = 2; i > 0; i-- )
Hanno Becker19859472018-08-06 09:40:20 +01008045 if( ++ssl->cur_out_ctr[i - 1] != 0 )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02008046 break;
8047
8048 /* The loop goes to its end iff the counter is wrapping */
8049 if( i == 0 )
8050 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008051 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
8052 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02008053 }
8054 }
8055 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008056#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker19859472018-08-06 09:40:20 +01008057 memset( ssl->cur_out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00008058
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008059 ssl->transform_out = ssl->transform_negotiate;
8060 ssl->session_out = ssl->session_negotiate;
8061
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008062#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
8063 if( mbedtls_ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01008064 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008065 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Paul Bakker07eb38b2012-12-19 14:42:06 +01008066 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008067 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
8068 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker07eb38b2012-12-19 14:42:06 +01008069 }
8070 }
8071#endif
8072
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008073#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008074 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008075 mbedtls_ssl_send_flight_completed( ssl );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02008076#endif
8077
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02008078 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008079 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02008080 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008081 return( ret );
8082 }
8083
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02008084#if defined(MBEDTLS_SSL_PROTO_DTLS)
8085 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
8086 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
8087 {
8088 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
8089 return( ret );
8090 }
8091#endif
8092
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008093 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008094
8095 return( 0 );
8096}
8097
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008098#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00008099#define SSL_MAX_HASH_LEN 36
8100#else
8101#define SSL_MAX_HASH_LEN 12
8102#endif
8103
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008104int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008105{
Paul Bakker23986e52011-04-24 08:57:21 +00008106 int ret;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02008107 unsigned int hash_len;
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00008108 unsigned char buf[SSL_MAX_HASH_LEN];
Paul Bakker5121ce52009-01-03 21:22:43 +00008109
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008110 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008111
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008112 ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00008113
Hanno Becker327c93b2018-08-15 13:56:18 +01008114 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008115 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008116 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008117 return( ret );
8118 }
8119
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008120 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00008121 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008122 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02008123 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8124 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008125 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00008126 }
8127
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00008128 /* There is currently no ciphersuite using another length with TLS 1.2 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008129#if defined(MBEDTLS_SSL_PROTO_SSL3)
8130 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00008131 hash_len = 36;
8132 else
8133#endif
8134 hash_len = 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00008135
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008136 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED ||
8137 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00008138 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008139 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02008140 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8141 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008142 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00008143 }
8144
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008145 if( mbedtls_ssl_safer_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00008146 buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008147 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008148 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02008149 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8150 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008151 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00008152 }
8153
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008154#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00008155 ssl->verify_data_len = hash_len;
8156 memcpy( ssl->peer_verify_data, buf, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008157#endif
Paul Bakker48916f92012-09-16 19:57:18 +00008158
Paul Bakker0a597072012-09-25 21:55:46 +00008159 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008160 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008161#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008162 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008163 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01008164#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008165#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008166 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008167 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01008168#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00008169 }
8170 else
8171 ssl->state++;
8172
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008173#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008174 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008175 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008176#endif
8177
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008178 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008179
8180 return( 0 );
8181}
8182
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008183static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008184{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008185 memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008186
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008187#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
8188 defined(MBEDTLS_SSL_PROTO_TLS1_1)
8189 mbedtls_md5_init( &handshake->fin_md5 );
8190 mbedtls_sha1_init( &handshake->fin_sha1 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008191 mbedtls_md5_starts_ret( &handshake->fin_md5 );
8192 mbedtls_sha1_starts_ret( &handshake->fin_sha1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008193#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008194#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
8195#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05008196#if defined(MBEDTLS_USE_PSA_CRYPTO)
8197 handshake->fin_sha256_psa = psa_hash_operation_init();
8198 psa_hash_setup( &handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
8199#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008200 mbedtls_sha256_init( &handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008201 mbedtls_sha256_starts_ret( &handshake->fin_sha256, 0 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008202#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05008203#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008204#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05008205#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05008206 handshake->fin_sha384_psa = psa_hash_operation_init();
8207 psa_hash_setup( &handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05008208#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008209 mbedtls_sha512_init( &handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008210 mbedtls_sha512_starts_ret( &handshake->fin_sha512, 1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008211#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05008212#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008213#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008214
8215 handshake->update_checksum = ssl_update_checksum_start;
Hanno Becker7e5437a2017-04-28 17:15:26 +01008216
8217#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
8218 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
8219 mbedtls_ssl_sig_hash_set_init( &handshake->hash_algs );
8220#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008221
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008222#if defined(MBEDTLS_DHM_C)
8223 mbedtls_dhm_init( &handshake->dhm_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008224#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008225#if defined(MBEDTLS_ECDH_C)
8226 mbedtls_ecdh_init( &handshake->ecdh_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008227#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008228#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02008229 mbedtls_ecjpake_init( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02008230#if defined(MBEDTLS_SSL_CLI_C)
8231 handshake->ecjpake_cache = NULL;
8232 handshake->ecjpake_cache_len = 0;
8233#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02008234#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02008235
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02008236#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02008237 mbedtls_x509_crt_restart_init( &handshake->ecrs_ctx );
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02008238#endif
8239
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02008240#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
8241 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
8242#endif
Hanno Becker75173122019-02-06 16:18:31 +00008243
8244#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
8245 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8246 mbedtls_pk_init( &handshake->peer_pubkey );
8247#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008248}
8249
Hanno Beckera18d1322018-01-03 14:27:32 +00008250void mbedtls_ssl_transform_init( mbedtls_ssl_transform *transform )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008251{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008252 memset( transform, 0, sizeof(mbedtls_ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02008253
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008254 mbedtls_cipher_init( &transform->cipher_ctx_enc );
8255 mbedtls_cipher_init( &transform->cipher_ctx_dec );
Paul Bakker84bbeb52014-07-01 14:53:22 +02008256
Hanno Beckerd56ed242018-01-03 15:32:51 +00008257#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008258 mbedtls_md_init( &transform->md_ctx_enc );
8259 mbedtls_md_init( &transform->md_ctx_dec );
Hanno Beckerd56ed242018-01-03 15:32:51 +00008260#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008261}
8262
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008263void mbedtls_ssl_session_init( mbedtls_ssl_session *session )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008264{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008265 memset( session, 0, sizeof(mbedtls_ssl_session) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008266}
8267
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008268static int ssl_handshake_init( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00008269{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008270 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00008271 if( ssl->transform_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008272 mbedtls_ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008273 if( ssl->session_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008274 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008275 if( ssl->handshake )
Gilles Peskine9b562d52018-04-25 20:32:43 +02008276 mbedtls_ssl_handshake_free( ssl );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008277
8278 /*
8279 * Either the pointers are now NULL or cleared properly and can be freed.
8280 * Now allocate missing structures.
8281 */
8282 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008283 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008284 ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008285 }
Paul Bakker48916f92012-09-16 19:57:18 +00008286
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008287 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008288 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008289 ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008290 }
Paul Bakker48916f92012-09-16 19:57:18 +00008291
Paul Bakker82788fb2014-10-20 13:59:19 +02008292 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008293 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008294 ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008295 }
Paul Bakker48916f92012-09-16 19:57:18 +00008296
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008297 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00008298 if( ssl->handshake == NULL ||
8299 ssl->transform_negotiate == NULL ||
8300 ssl->session_negotiate == NULL )
8301 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02008302 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008303
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008304 mbedtls_free( ssl->handshake );
8305 mbedtls_free( ssl->transform_negotiate );
8306 mbedtls_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008307
8308 ssl->handshake = NULL;
8309 ssl->transform_negotiate = NULL;
8310 ssl->session_negotiate = NULL;
8311
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008312 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker48916f92012-09-16 19:57:18 +00008313 }
8314
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008315 /* Initialize structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008316 mbedtls_ssl_session_init( ssl->session_negotiate );
Hanno Beckera18d1322018-01-03 14:27:32 +00008317 mbedtls_ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02008318 ssl_handshake_params_init( ssl->handshake );
8319
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008320#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02008321 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
8322 {
8323 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008324
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02008325 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
8326 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
8327 else
8328 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008329
8330 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02008331 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008332#endif
8333
Paul Bakker48916f92012-09-16 19:57:18 +00008334 return( 0 );
8335}
8336
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008337#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008338/* Dummy cookie callbacks for defaults */
8339static int ssl_cookie_write_dummy( void *ctx,
8340 unsigned char **p, unsigned char *end,
8341 const unsigned char *cli_id, size_t cli_id_len )
8342{
8343 ((void) ctx);
8344 ((void) p);
8345 ((void) end);
8346 ((void) cli_id);
8347 ((void) cli_id_len);
8348
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008349 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008350}
8351
8352static int ssl_cookie_check_dummy( void *ctx,
8353 const unsigned char *cookie, size_t cookie_len,
8354 const unsigned char *cli_id, size_t cli_id_len )
8355{
8356 ((void) ctx);
8357 ((void) cookie);
8358 ((void) cookie_len);
8359 ((void) cli_id);
8360 ((void) cli_id_len);
8361
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008362 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008363}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008364#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008365
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008366/* Once ssl->out_hdr as the address of the beginning of the
8367 * next outgoing record is set, deduce the other pointers.
8368 *
8369 * Note: For TLS, we save the implicit record sequence number
8370 * (entering MAC computation) in the 8 bytes before ssl->out_hdr,
8371 * and the caller has to make sure there's space for this.
8372 */
8373
8374static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
8375 mbedtls_ssl_transform *transform )
8376{
8377#if defined(MBEDTLS_SSL_PROTO_DTLS)
8378 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
8379 {
8380 ssl->out_ctr = ssl->out_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008381#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008382 ssl->out_cid = ssl->out_ctr + 8;
8383 ssl->out_len = ssl->out_cid;
8384 if( transform != NULL )
8385 ssl->out_len += transform->out_cid_len;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008386#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008387 ssl->out_len = ssl->out_ctr + 8;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008388#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008389 ssl->out_iv = ssl->out_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008390 }
8391 else
8392#endif
8393 {
8394 ssl->out_ctr = ssl->out_hdr - 8;
8395 ssl->out_len = ssl->out_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008396#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker4c3eb7c2019-05-08 16:43:21 +01008397 ssl->out_cid = ssl->out_len;
8398#endif
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008399 ssl->out_iv = ssl->out_hdr + 5;
8400 }
8401
8402 /* Adjust out_msg to make space for explicit IV, if used. */
8403 if( transform != NULL &&
8404 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
8405 {
8406 ssl->out_msg = ssl->out_iv + transform->ivlen - transform->fixed_ivlen;
8407 }
8408 else
8409 ssl->out_msg = ssl->out_iv;
8410}
8411
8412/* Once ssl->in_hdr as the address of the beginning of the
8413 * next incoming record is set, deduce the other pointers.
8414 *
8415 * Note: For TLS, we save the implicit record sequence number
8416 * (entering MAC computation) in the 8 bytes before ssl->in_hdr,
8417 * and the caller has to make sure there's space for this.
8418 */
8419
Hanno Becker79594fd2019-05-08 09:38:41 +01008420static void ssl_update_in_pointers( mbedtls_ssl_context *ssl )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008421{
Hanno Becker79594fd2019-05-08 09:38:41 +01008422 /* This function sets the pointers to match the case
8423 * of unprotected TLS/DTLS records, with both ssl->in_iv
8424 * and ssl->in_msg pointing to the beginning of the record
8425 * content.
8426 *
8427 * When decrypting a protected record, ssl->in_msg
8428 * will be shifted to point to the beginning of the
8429 * record plaintext.
8430 */
8431
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008432#if defined(MBEDTLS_SSL_PROTO_DTLS)
8433 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
8434 {
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008435 /* This sets the header pointers to match records
8436 * without CID. When we receive a record containing
8437 * a CID, the fields are shifted accordingly in
8438 * ssl_parse_record_header(). */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008439 ssl->in_ctr = ssl->in_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008440#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008441 ssl->in_cid = ssl->in_ctr + 8;
8442 ssl->in_len = ssl->in_cid; /* Default: no CID */
Hanno Beckera0e20d02019-05-15 14:03:01 +01008443#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008444 ssl->in_len = ssl->in_ctr + 8;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008445#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008446 ssl->in_iv = ssl->in_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008447 }
8448 else
8449#endif
8450 {
8451 ssl->in_ctr = ssl->in_hdr - 8;
8452 ssl->in_len = ssl->in_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008453#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker4c3eb7c2019-05-08 16:43:21 +01008454 ssl->in_cid = ssl->in_len;
8455#endif
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008456 ssl->in_iv = ssl->in_hdr + 5;
8457 }
8458
Hanno Becker79594fd2019-05-08 09:38:41 +01008459 /* This will be adjusted at record decryption time. */
8460 ssl->in_msg = ssl->in_iv;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008461}
8462
Paul Bakker5121ce52009-01-03 21:22:43 +00008463/*
8464 * Initialize an SSL context
8465 */
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02008466void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
8467{
8468 memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
8469}
8470
8471/*
8472 * Setup an SSL context
8473 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008474
8475static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl )
8476{
8477 /* Set the incoming and outgoing record pointers. */
8478#if defined(MBEDTLS_SSL_PROTO_DTLS)
8479 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
8480 {
8481 ssl->out_hdr = ssl->out_buf;
8482 ssl->in_hdr = ssl->in_buf;
8483 }
8484 else
8485#endif /* MBEDTLS_SSL_PROTO_DTLS */
8486 {
8487 ssl->out_hdr = ssl->out_buf + 8;
8488 ssl->in_hdr = ssl->in_buf + 8;
8489 }
8490
8491 /* Derive other internal pointers. */
8492 ssl_update_out_pointers( ssl, NULL /* no transform enabled */ );
Hanno Becker79594fd2019-05-08 09:38:41 +01008493 ssl_update_in_pointers ( ssl );
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008494}
8495
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02008496int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +02008497 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +00008498{
Paul Bakker48916f92012-09-16 19:57:18 +00008499 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00008500
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02008501 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00008502
8503 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01008504 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00008505 */
k-stachowiakc9a5f022018-07-24 13:53:31 +02008506
8507 /* Set to NULL in case of an error condition */
8508 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02008509
Angus Grattond8213d02016-05-25 20:56:48 +10008510 ssl->in_buf = mbedtls_calloc( 1, MBEDTLS_SSL_IN_BUFFER_LEN );
8511 if( ssl->in_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00008512 {
Angus Grattond8213d02016-05-25 20:56:48 +10008513 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_IN_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02008514 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02008515 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +10008516 }
8517
8518 ssl->out_buf = mbedtls_calloc( 1, MBEDTLS_SSL_OUT_BUFFER_LEN );
8519 if( ssl->out_buf == NULL )
8520 {
8521 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_OUT_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02008522 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02008523 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00008524 }
8525
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008526 ssl_reset_in_out_pointers( ssl );
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02008527
Paul Bakker48916f92012-09-16 19:57:18 +00008528 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
k-stachowiaka47911c2018-07-04 17:41:58 +02008529 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00008530
8531 return( 0 );
k-stachowiaka47911c2018-07-04 17:41:58 +02008532
8533error:
8534 mbedtls_free( ssl->in_buf );
8535 mbedtls_free( ssl->out_buf );
8536
8537 ssl->conf = NULL;
8538
8539 ssl->in_buf = NULL;
8540 ssl->out_buf = NULL;
8541
8542 ssl->in_hdr = NULL;
8543 ssl->in_ctr = NULL;
8544 ssl->in_len = NULL;
8545 ssl->in_iv = NULL;
8546 ssl->in_msg = NULL;
8547
8548 ssl->out_hdr = NULL;
8549 ssl->out_ctr = NULL;
8550 ssl->out_len = NULL;
8551 ssl->out_iv = NULL;
8552 ssl->out_msg = NULL;
8553
k-stachowiak9f7798e2018-07-31 16:52:32 +02008554 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008555}
8556
8557/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00008558 * Reset an initialized and used SSL context for re-use while retaining
8559 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008560 *
8561 * If partial is non-zero, keep data in the input buffer and client ID.
8562 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00008563 */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008564static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +00008565{
Paul Bakker48916f92012-09-16 19:57:18 +00008566 int ret;
8567
Hanno Becker7e772132018-08-10 12:38:21 +01008568#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || \
8569 !defined(MBEDTLS_SSL_SRV_C)
8570 ((void) partial);
8571#endif
8572
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008573 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008574
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008575 /* Cancel any possibly running timer */
8576 ssl_set_timer( ssl, 0 );
8577
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008578#if defined(MBEDTLS_SSL_RENEGOTIATION)
8579 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008580 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00008581
8582 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008583 memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
8584 memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008585#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008586 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00008587
Paul Bakker7eb013f2011-10-06 12:37:39 +00008588 ssl->in_offt = NULL;
Hanno Beckerf29d4702018-08-10 11:31:15 +01008589 ssl_reset_in_out_pointers( ssl );
Paul Bakker7eb013f2011-10-06 12:37:39 +00008590
8591 ssl->in_msgtype = 0;
8592 ssl->in_msglen = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008593#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02008594 ssl->next_record_offset = 0;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02008595 ssl->in_epoch = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02008596#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008597#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02008598 ssl_dtls_replay_reset( ssl );
8599#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00008600
8601 ssl->in_hslen = 0;
8602 ssl->nb_zero = 0;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01008603
8604 ssl->keep_current_message = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00008605
8606 ssl->out_msgtype = 0;
8607 ssl->out_msglen = 0;
8608 ssl->out_left = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008609#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
8610 if( ssl->split_done != MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008611 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008612#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00008613
Hanno Becker19859472018-08-06 09:40:20 +01008614 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
8615
Paul Bakker48916f92012-09-16 19:57:18 +00008616 ssl->transform_in = NULL;
8617 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00008618
Hanno Becker78640902018-08-13 16:35:15 +01008619 ssl->session_in = NULL;
8620 ssl->session_out = NULL;
8621
Angus Grattond8213d02016-05-25 20:56:48 +10008622 memset( ssl->out_buf, 0, MBEDTLS_SSL_OUT_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01008623
8624#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008625 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01008626#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
8627 {
8628 ssl->in_left = 0;
Angus Grattond8213d02016-05-25 20:56:48 +10008629 memset( ssl->in_buf, 0, MBEDTLS_SSL_IN_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01008630 }
Paul Bakker05ef8352012-05-08 09:17:57 +00008631
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008632#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
8633 if( mbedtls_ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00008634 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008635 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_reset()" ) );
8636 if( ( ret = mbedtls_ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00008637 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008638 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_reset", ret );
8639 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00008640 }
Paul Bakker05ef8352012-05-08 09:17:57 +00008641 }
8642#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00008643
Paul Bakker48916f92012-09-16 19:57:18 +00008644 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00008645 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008646 mbedtls_ssl_transform_free( ssl->transform );
8647 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00008648 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00008649 }
Paul Bakker48916f92012-09-16 19:57:18 +00008650
Paul Bakkerc0463502013-02-14 11:19:38 +01008651 if( ssl->session )
8652 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008653 mbedtls_ssl_session_free( ssl->session );
8654 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01008655 ssl->session = NULL;
8656 }
8657
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008658#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008659 ssl->alpn_chosen = NULL;
8660#endif
8661
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008662#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker4ccbf062018-08-10 11:20:38 +01008663#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008664 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01008665#endif
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008666 {
8667 mbedtls_free( ssl->cli_id );
8668 ssl->cli_id = NULL;
8669 ssl->cli_id_len = 0;
8670 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02008671#endif
8672
Paul Bakker48916f92012-09-16 19:57:18 +00008673 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
8674 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00008675
8676 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00008677}
8678
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02008679/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008680 * Reset an initialized and used SSL context for re-use while retaining
8681 * all application-set variables, function pointers and data.
8682 */
8683int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
8684{
8685 return( ssl_session_reset_int( ssl, 0 ) );
8686}
8687
8688/*
Paul Bakker5121ce52009-01-03 21:22:43 +00008689 * SSL set accessors
8690 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008691void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00008692{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008693 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00008694}
8695
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008696void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01008697{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008698 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01008699}
8700
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008701#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008702void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02008703{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008704 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02008705}
8706#endif
8707
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008708#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008709void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf, unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02008710{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008711 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02008712}
8713#endif
8714
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008715#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01008716
Hanno Becker1841b0a2018-08-24 11:13:57 +01008717void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl,
8718 unsigned allow_packing )
Hanno Becker04da1892018-08-14 13:22:10 +01008719{
8720 ssl->disable_datagram_packing = !allow_packing;
8721}
8722
8723void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
8724 uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02008725{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008726 conf->hs_timeout_min = min;
8727 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02008728}
8729#endif
8730
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008731void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00008732{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008733 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00008734}
8735
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008736#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008737void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02008738 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008739 void *p_vrfy )
8740{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008741 conf->f_vrfy = f_vrfy;
8742 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008743}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008744#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008745
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008746void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00008747 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00008748 void *p_rng )
8749{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01008750 conf->f_rng = f_rng;
8751 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00008752}
8753
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008754void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02008755 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00008756 void *p_dbg )
8757{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008758 conf->f_dbg = f_dbg;
8759 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00008760}
8761
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008762void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008763 void *p_bio,
Simon Butchere846b512016-03-01 17:31:49 +00008764 mbedtls_ssl_send_t *f_send,
8765 mbedtls_ssl_recv_t *f_recv,
8766 mbedtls_ssl_recv_timeout_t *f_recv_timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008767{
8768 ssl->p_bio = p_bio;
8769 ssl->f_send = f_send;
8770 ssl->f_recv = f_recv;
8771 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01008772}
8773
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02008774#if defined(MBEDTLS_SSL_PROTO_DTLS)
8775void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu )
8776{
8777 ssl->mtu = mtu;
8778}
8779#endif
8780
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008781void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01008782{
8783 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008784}
8785
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008786void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
8787 void *p_timer,
Simon Butchere846b512016-03-01 17:31:49 +00008788 mbedtls_ssl_set_timer_t *f_set_timer,
8789 mbedtls_ssl_get_timer_t *f_get_timer )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008790{
8791 ssl->p_timer = p_timer;
8792 ssl->f_set_timer = f_set_timer;
8793 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008794
8795 /* Make sure we start with no timer running */
8796 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008797}
8798
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008799#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008800void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01008801 void *p_cache,
8802 int (*f_get_cache)(void *, mbedtls_ssl_session *),
8803 int (*f_set_cache)(void *, const mbedtls_ssl_session *) )
Paul Bakker5121ce52009-01-03 21:22:43 +00008804{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01008805 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008806 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008807 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00008808}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008809#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008810
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008811#if defined(MBEDTLS_SSL_CLI_C)
8812int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00008813{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008814 int ret;
8815
8816 if( ssl == NULL ||
8817 session == NULL ||
8818 ssl->session_negotiate == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008819 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008820 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008821 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008822 }
8823
Hanno Becker52055ae2019-02-06 14:30:46 +00008824 if( ( ret = mbedtls_ssl_session_copy( ssl->session_negotiate,
8825 session ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008826 return( ret );
8827
Paul Bakker0a597072012-09-25 21:55:46 +00008828 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008829
8830 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00008831}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008832#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008833
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008834void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008835 const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00008836{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008837 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = ciphersuites;
8838 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = ciphersuites;
8839 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = ciphersuites;
8840 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = ciphersuites;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008841}
8842
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008843void mbedtls_ssl_conf_ciphersuites_for_version( mbedtls_ssl_config *conf,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02008844 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008845 int major, int minor )
8846{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008847 if( major != MBEDTLS_SSL_MAJOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008848 return;
8849
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008850 if( minor < MBEDTLS_SSL_MINOR_VERSION_0 || minor > MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008851 return;
8852
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008853 conf->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00008854}
8855
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008856#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02008857void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01008858 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02008859{
8860 conf->cert_profile = profile;
8861}
8862
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008863/* Append a new keycert entry to a (possibly empty) list */
8864static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
8865 mbedtls_x509_crt *cert,
8866 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008867{
niisato8ee24222018-06-25 19:05:48 +09008868 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008869
niisato8ee24222018-06-25 19:05:48 +09008870 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
8871 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008872 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008873
niisato8ee24222018-06-25 19:05:48 +09008874 new_cert->cert = cert;
8875 new_cert->key = key;
8876 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008877
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008878 /* Update head is the list was null, else add to the end */
8879 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01008880 {
niisato8ee24222018-06-25 19:05:48 +09008881 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01008882 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008883 else
8884 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008885 mbedtls_ssl_key_cert *cur = *head;
8886 while( cur->next != NULL )
8887 cur = cur->next;
niisato8ee24222018-06-25 19:05:48 +09008888 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008889 }
8890
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008891 return( 0 );
8892}
8893
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008894int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008895 mbedtls_x509_crt *own_cert,
8896 mbedtls_pk_context *pk_key )
8897{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02008898 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008899}
8900
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008901void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008902 mbedtls_x509_crt *ca_chain,
8903 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008904{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008905 conf->ca_chain = ca_chain;
8906 conf->ca_crl = ca_crl;
Hanno Becker5adaad92019-03-27 16:54:37 +00008907
8908#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
8909 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
8910 * cannot be used together. */
8911 conf->f_ca_cb = NULL;
8912 conf->p_ca_cb = NULL;
8913#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Paul Bakker5121ce52009-01-03 21:22:43 +00008914}
Hanno Becker5adaad92019-03-27 16:54:37 +00008915
8916#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
8917void mbedtls_ssl_conf_ca_cb( mbedtls_ssl_config *conf,
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00008918 mbedtls_x509_crt_ca_cb_t f_ca_cb,
Hanno Becker5adaad92019-03-27 16:54:37 +00008919 void *p_ca_cb )
8920{
8921 conf->f_ca_cb = f_ca_cb;
8922 conf->p_ca_cb = p_ca_cb;
8923
8924 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
8925 * cannot be used together. */
8926 conf->ca_chain = NULL;
8927 conf->ca_crl = NULL;
8928}
8929#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008930#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00008931
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02008932#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
8933int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
8934 mbedtls_x509_crt *own_cert,
8935 mbedtls_pk_context *pk_key )
8936{
8937 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
8938 own_cert, pk_key ) );
8939}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02008940
8941void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
8942 mbedtls_x509_crt *ca_chain,
8943 mbedtls_x509_crl *ca_crl )
8944{
8945 ssl->handshake->sni_ca_chain = ca_chain;
8946 ssl->handshake->sni_ca_crl = ca_crl;
8947}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02008948
8949void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
8950 int authmode )
8951{
8952 ssl->handshake->sni_authmode = authmode;
8953}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02008954#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
8955
Hanno Becker8927c832019-04-03 12:52:50 +01008956#if defined(MBEDTLS_X509_CRT_PARSE_C)
8957void mbedtls_ssl_set_verify( mbedtls_ssl_context *ssl,
8958 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
8959 void *p_vrfy )
8960{
8961 ssl->f_vrfy = f_vrfy;
8962 ssl->p_vrfy = p_vrfy;
8963}
8964#endif
8965
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008966#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008967/*
8968 * Set EC J-PAKE password for current handshake
8969 */
8970int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
8971 const unsigned char *pw,
8972 size_t pw_len )
8973{
8974 mbedtls_ecjpake_role role;
8975
Janos Follath8eb64132016-06-03 15:40:57 +01008976 if( ssl->handshake == NULL || ssl->conf == NULL )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008977 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8978
8979 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
8980 role = MBEDTLS_ECJPAKE_SERVER;
8981 else
8982 role = MBEDTLS_ECJPAKE_CLIENT;
8983
8984 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
8985 role,
8986 MBEDTLS_MD_SHA256,
8987 MBEDTLS_ECP_DP_SECP256R1,
8988 pw, pw_len ) );
8989}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008990#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008991
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008992#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008993
8994static void ssl_conf_remove_psk( mbedtls_ssl_config *conf )
8995{
8996 /* Remove reference to existing PSK, if any. */
8997#if defined(MBEDTLS_USE_PSA_CRYPTO)
8998 if( conf->psk_opaque != 0 )
8999 {
9000 /* The maintenance of the PSK key slot is the
9001 * user's responsibility. */
9002 conf->psk_opaque = 0;
9003 }
Hanno Beckera63ac3f2018-11-05 12:47:16 +00009004 /* This and the following branch should never
9005 * be taken simultaenously as we maintain the
9006 * invariant that raw and opaque PSKs are never
9007 * configured simultaneously. As a safeguard,
9008 * though, `else` is omitted here. */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01009009#endif /* MBEDTLS_USE_PSA_CRYPTO */
9010 if( conf->psk != NULL )
9011 {
9012 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
9013
9014 mbedtls_free( conf->psk );
9015 conf->psk = NULL;
9016 conf->psk_len = 0;
9017 }
9018
9019 /* Remove reference to PSK identity, if any. */
9020 if( conf->psk_identity != NULL )
9021 {
9022 mbedtls_free( conf->psk_identity );
9023 conf->psk_identity = NULL;
9024 conf->psk_identity_len = 0;
9025 }
9026}
9027
Hanno Becker7390c712018-11-15 13:33:04 +00009028/* This function assumes that PSK identity in the SSL config is unset.
9029 * It checks that the provided identity is well-formed and attempts
9030 * to make a copy of it in the SSL config.
9031 * On failure, the PSK identity in the config remains unset. */
9032static int ssl_conf_set_psk_identity( mbedtls_ssl_config *conf,
9033 unsigned char const *psk_identity,
9034 size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02009035{
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02009036 /* Identity len will be encoded on two bytes */
Hanno Becker7390c712018-11-15 13:33:04 +00009037 if( psk_identity == NULL ||
9038 ( psk_identity_len >> 16 ) != 0 ||
Angus Grattond8213d02016-05-25 20:56:48 +10009039 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02009040 {
9041 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9042 }
9043
Hanno Becker7390c712018-11-15 13:33:04 +00009044 conf->psk_identity = mbedtls_calloc( 1, psk_identity_len );
9045 if( conf->psk_identity == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02009046 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker6db455e2013-09-18 17:29:31 +02009047
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01009048 conf->psk_identity_len = psk_identity_len;
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01009049 memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker5ad403f2013-09-18 21:21:30 +02009050
9051 return( 0 );
Paul Bakker6db455e2013-09-18 17:29:31 +02009052}
9053
Hanno Becker7390c712018-11-15 13:33:04 +00009054int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
9055 const unsigned char *psk, size_t psk_len,
9056 const unsigned char *psk_identity, size_t psk_identity_len )
9057{
9058 int ret;
9059 /* Remove opaque/raw PSK + PSK Identity */
9060 ssl_conf_remove_psk( conf );
9061
9062 /* Check and set raw PSK */
9063 if( psk == NULL || psk_len > MBEDTLS_PSK_MAX_LEN )
9064 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9065 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
9066 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
9067 conf->psk_len = psk_len;
9068 memcpy( conf->psk, psk, conf->psk_len );
9069
9070 /* Check and set PSK Identity */
9071 ret = ssl_conf_set_psk_identity( conf, psk_identity, psk_identity_len );
9072 if( ret != 0 )
9073 ssl_conf_remove_psk( conf );
9074
9075 return( ret );
9076}
9077
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01009078static void ssl_remove_psk( mbedtls_ssl_context *ssl )
9079{
9080#if defined(MBEDTLS_USE_PSA_CRYPTO)
9081 if( ssl->handshake->psk_opaque != 0 )
9082 {
9083 ssl->handshake->psk_opaque = 0;
9084 }
9085 else
9086#endif /* MBEDTLS_USE_PSA_CRYPTO */
9087 if( ssl->handshake->psk != NULL )
9088 {
9089 mbedtls_platform_zeroize( ssl->handshake->psk,
9090 ssl->handshake->psk_len );
9091 mbedtls_free( ssl->handshake->psk );
9092 ssl->handshake->psk_len = 0;
9093 }
9094}
9095
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01009096int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
9097 const unsigned char *psk, size_t psk_len )
9098{
9099 if( psk == NULL || ssl->handshake == NULL )
9100 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9101
9102 if( psk_len > MBEDTLS_PSK_MAX_LEN )
9103 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9104
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01009105 ssl_remove_psk( ssl );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01009106
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02009107 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02009108 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01009109
9110 ssl->handshake->psk_len = psk_len;
9111 memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
9112
9113 return( 0 );
9114}
9115
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01009116#if defined(MBEDTLS_USE_PSA_CRYPTO)
9117int mbedtls_ssl_conf_psk_opaque( mbedtls_ssl_config *conf,
Andrzej Kurek2349c4d2019-01-08 09:36:01 -05009118 psa_key_handle_t psk_slot,
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01009119 const unsigned char *psk_identity,
9120 size_t psk_identity_len )
9121{
Hanno Becker7390c712018-11-15 13:33:04 +00009122 int ret;
9123 /* Clear opaque/raw PSK + PSK Identity, if present. */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01009124 ssl_conf_remove_psk( conf );
9125
Hanno Becker7390c712018-11-15 13:33:04 +00009126 /* Check and set opaque PSK */
9127 if( psk_slot == 0 )
9128 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01009129 conf->psk_opaque = psk_slot;
Hanno Becker7390c712018-11-15 13:33:04 +00009130
9131 /* Check and set PSK Identity */
9132 ret = ssl_conf_set_psk_identity( conf, psk_identity,
9133 psk_identity_len );
9134 if( ret != 0 )
9135 ssl_conf_remove_psk( conf );
9136
9137 return( ret );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01009138}
9139
9140int mbedtls_ssl_set_hs_psk_opaque( mbedtls_ssl_context *ssl,
Andrzej Kurek2349c4d2019-01-08 09:36:01 -05009141 psa_key_handle_t psk_slot )
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01009142{
9143 if( psk_slot == 0 || ssl->handshake == NULL )
9144 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9145
9146 ssl_remove_psk( ssl );
9147 ssl->handshake->psk_opaque = psk_slot;
9148 return( 0 );
9149}
9150#endif /* MBEDTLS_USE_PSA_CRYPTO */
9151
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009152void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009153 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
Paul Bakker6db455e2013-09-18 17:29:31 +02009154 size_t),
9155 void *p_psk )
9156{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009157 conf->f_psk = f_psk;
9158 conf->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02009159}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009160#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00009161
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02009162#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker470a8c42017-10-04 15:28:46 +01009163
9164#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009165int mbedtls_ssl_conf_dh_param( mbedtls_ssl_config *conf, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00009166{
9167 int ret;
9168
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01009169 if( ( ret = mbedtls_mpi_read_string( &conf->dhm_P, 16, dhm_P ) ) != 0 ||
9170 ( ret = mbedtls_mpi_read_string( &conf->dhm_G, 16, dhm_G ) ) != 0 )
9171 {
9172 mbedtls_mpi_free( &conf->dhm_P );
9173 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00009174 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01009175 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009176
9177 return( 0 );
9178}
Hanno Becker470a8c42017-10-04 15:28:46 +01009179#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +00009180
Hanno Beckera90658f2017-10-04 15:29:08 +01009181int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
9182 const unsigned char *dhm_P, size_t P_len,
9183 const unsigned char *dhm_G, size_t G_len )
9184{
9185 int ret;
9186
9187 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
9188 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
9189 {
9190 mbedtls_mpi_free( &conf->dhm_P );
9191 mbedtls_mpi_free( &conf->dhm_G );
9192 return( ret );
9193 }
9194
9195 return( 0 );
9196}
Paul Bakker5121ce52009-01-03 21:22:43 +00009197
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009198int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00009199{
9200 int ret;
9201
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01009202 if( ( ret = mbedtls_mpi_copy( &conf->dhm_P, &dhm_ctx->P ) ) != 0 ||
9203 ( ret = mbedtls_mpi_copy( &conf->dhm_G, &dhm_ctx->G ) ) != 0 )
9204 {
9205 mbedtls_mpi_free( &conf->dhm_P );
9206 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00009207 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01009208 }
Paul Bakker1b57b062011-01-06 15:48:19 +00009209
9210 return( 0 );
9211}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02009212#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00009213
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02009214#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
9215/*
9216 * Set the minimum length for Diffie-Hellman parameters
9217 */
9218void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
9219 unsigned int bitlen )
9220{
9221 conf->dhm_min_bitlen = bitlen;
9222}
9223#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
9224
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009225#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02009226/*
9227 * Set allowed/preferred hashes for handshake signatures
9228 */
9229void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
9230 const int *hashes )
9231{
9232 conf->sig_hashes = hashes;
9233}
Hanno Becker947194e2017-04-07 13:25:49 +01009234#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02009235
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02009236#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01009237/*
9238 * Set the allowed elliptic curves
9239 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009240void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009241 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01009242{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009243 conf->curve_list = curve_list;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01009244}
Hanno Becker947194e2017-04-07 13:25:49 +01009245#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01009246
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01009247#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009248int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00009249{
Hanno Becker947194e2017-04-07 13:25:49 +01009250 /* Initialize to suppress unnecessary compiler warning */
9251 size_t hostname_len = 0;
9252
9253 /* Check if new hostname is valid before
9254 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01009255 if( hostname != NULL )
9256 {
9257 hostname_len = strlen( hostname );
9258
9259 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
9260 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9261 }
9262
9263 /* Now it's clear that we will overwrite the old hostname,
9264 * so we can free it safely */
9265
9266 if( ssl->hostname != NULL )
9267 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009268 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Hanno Becker947194e2017-04-07 13:25:49 +01009269 mbedtls_free( ssl->hostname );
9270 }
9271
9272 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01009273
Paul Bakker5121ce52009-01-03 21:22:43 +00009274 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01009275 {
9276 ssl->hostname = NULL;
9277 }
9278 else
9279 {
9280 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01009281 if( ssl->hostname == NULL )
9282 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02009283
Hanno Becker947194e2017-04-07 13:25:49 +01009284 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02009285
Hanno Becker947194e2017-04-07 13:25:49 +01009286 ssl->hostname[hostname_len] = '\0';
9287 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009288
9289 return( 0 );
9290}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01009291#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00009292
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01009293#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009294void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009295 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00009296 const unsigned char *, size_t),
9297 void *p_sni )
9298{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009299 conf->f_sni = f_sni;
9300 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00009301}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009302#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00009303
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009304#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009305int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009306{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009307 size_t cur_len, tot_len;
9308 const char **p;
9309
9310 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08009311 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
9312 * MUST NOT be truncated."
9313 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009314 */
9315 tot_len = 0;
9316 for( p = protos; *p != NULL; p++ )
9317 {
9318 cur_len = strlen( *p );
9319 tot_len += cur_len;
9320
9321 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009322 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009323 }
9324
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009325 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009326
9327 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009328}
9329
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009330const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009331{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009332 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009333}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009334#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009335
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02009336void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00009337{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009338 conf->max_major_ver = major;
9339 conf->max_minor_ver = minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00009340}
9341
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02009342void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00009343{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009344 conf->min_major_ver = major;
9345 conf->min_minor_ver = minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00009346}
9347
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009348#if defined(MBEDTLS_SSL_FALLBACK_SCSV) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009349void mbedtls_ssl_conf_fallback( mbedtls_ssl_config *conf, char fallback )
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02009350{
Manuel Pégourié-Gonnard684b0592015-05-06 09:27:31 +01009351 conf->fallback = fallback;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02009352}
9353#endif
9354
Janos Follath088ce432017-04-10 12:42:31 +01009355#if defined(MBEDTLS_SSL_SRV_C)
9356void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
9357 char cert_req_ca_list )
9358{
9359 conf->cert_req_ca_list = cert_req_ca_list;
9360}
9361#endif
9362
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009363#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009364void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01009365{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009366 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01009367}
9368#endif
9369
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009370#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009371void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02009372{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009373 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02009374}
9375#endif
9376
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02009377#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009378void mbedtls_ssl_conf_arc4_support( mbedtls_ssl_config *conf, char arc4 )
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01009379{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009380 conf->arc4_disabled = arc4;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01009381}
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02009382#endif
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01009383
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009384#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009385int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009386{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009387 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
Angus Grattond8213d02016-05-25 20:56:48 +10009388 ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009389 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009390 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009391 }
9392
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01009393 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009394
9395 return( 0 );
9396}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009397#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009398
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009399#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02009400void mbedtls_ssl_conf_truncated_hmac( mbedtls_ssl_config *conf, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02009401{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009402 conf->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02009403}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009404#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02009405
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009406#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009407void mbedtls_ssl_conf_cbc_record_splitting( mbedtls_ssl_config *conf, char split )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009408{
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01009409 conf->cbc_record_splitting = split;
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009410}
9411#endif
9412
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009413void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00009414{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009415 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00009416}
9417
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009418#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009419void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01009420{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009421 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01009422}
9423
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009424void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009425{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009426 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009427}
9428
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009429void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01009430 const unsigned char period[8] )
9431{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009432 memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01009433}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009434#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00009435
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009436#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009437#if defined(MBEDTLS_SSL_CLI_C)
9438void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02009439{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01009440 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02009441}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009442#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02009443
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009444#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02009445void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
9446 mbedtls_ssl_ticket_write_t *f_ticket_write,
9447 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
9448 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02009449{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02009450 conf->f_ticket_write = f_ticket_write;
9451 conf->f_ticket_parse = f_ticket_parse;
9452 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02009453}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009454#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009455#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02009456
Robert Cragie4feb7ae2015-10-02 13:33:37 +01009457#if defined(MBEDTLS_SSL_EXPORT_KEYS)
9458void mbedtls_ssl_conf_export_keys_cb( mbedtls_ssl_config *conf,
9459 mbedtls_ssl_export_keys_t *f_export_keys,
9460 void *p_export_keys )
9461{
9462 conf->f_export_keys = f_export_keys;
9463 conf->p_export_keys = p_export_keys;
9464}
Ron Eldorf5cc10d2019-05-07 18:33:40 +03009465
9466void mbedtls_ssl_conf_export_keys_ext_cb( mbedtls_ssl_config *conf,
9467 mbedtls_ssl_export_keys_ext_t *f_export_keys_ext,
9468 void *p_export_keys )
9469{
9470 conf->f_export_keys_ext = f_export_keys_ext;
9471 conf->p_export_keys = p_export_keys;
9472}
Robert Cragie4feb7ae2015-10-02 13:33:37 +01009473#endif
9474
Gilles Peskineb74a1c72018-04-24 13:09:22 +02009475#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009476void mbedtls_ssl_conf_async_private_cb(
9477 mbedtls_ssl_config *conf,
9478 mbedtls_ssl_async_sign_t *f_async_sign,
9479 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
9480 mbedtls_ssl_async_resume_t *f_async_resume,
9481 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009482 void *async_config_data )
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009483{
9484 conf->f_async_sign_start = f_async_sign;
9485 conf->f_async_decrypt_start = f_async_decrypt;
9486 conf->f_async_resume = f_async_resume;
9487 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009488 conf->p_async_config_data = async_config_data;
9489}
9490
Gilles Peskine8f97af72018-04-26 11:46:10 +02009491void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
9492{
9493 return( conf->p_async_config_data );
9494}
9495
Gilles Peskine1febfef2018-04-30 11:54:39 +02009496void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009497{
9498 if( ssl->handshake == NULL )
9499 return( NULL );
9500 else
9501 return( ssl->handshake->user_async_ctx );
9502}
9503
Gilles Peskine1febfef2018-04-30 11:54:39 +02009504void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009505 void *ctx )
9506{
9507 if( ssl->handshake != NULL )
9508 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009509}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02009510#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009511
Paul Bakker5121ce52009-01-03 21:22:43 +00009512/*
9513 * SSL get accessors
9514 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009515size_t mbedtls_ssl_get_bytes_avail( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009516{
9517 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
9518}
9519
Hanno Becker8b170a02017-10-10 11:51:19 +01009520int mbedtls_ssl_check_pending( const mbedtls_ssl_context *ssl )
9521{
9522 /*
9523 * Case A: We're currently holding back
9524 * a message for further processing.
9525 */
9526
9527 if( ssl->keep_current_message == 1 )
9528 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009529 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: record held back for processing" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009530 return( 1 );
9531 }
9532
9533 /*
9534 * Case B: Further records are pending in the current datagram.
9535 */
9536
9537#if defined(MBEDTLS_SSL_PROTO_DTLS)
9538 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
9539 ssl->in_left > ssl->next_record_offset )
9540 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009541 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more records within current datagram" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009542 return( 1 );
9543 }
9544#endif /* MBEDTLS_SSL_PROTO_DTLS */
9545
9546 /*
9547 * Case C: A handshake message is being processed.
9548 */
9549
Hanno Becker8b170a02017-10-10 11:51:19 +01009550 if( ssl->in_hslen > 0 && ssl->in_hslen < ssl->in_msglen )
9551 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009552 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more handshake messages within current record" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009553 return( 1 );
9554 }
9555
9556 /*
9557 * Case D: An application data message is being processed
9558 */
9559 if( ssl->in_offt != NULL )
9560 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009561 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: application data record is being processed" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009562 return( 1 );
9563 }
9564
9565 /*
9566 * In all other cases, the rest of the message can be dropped.
Hanno Beckerc573ac32018-08-28 17:15:25 +01009567 * As in ssl_get_next_record, this needs to be adapted if
Hanno Becker8b170a02017-10-10 11:51:19 +01009568 * we implement support for multiple alerts in single records.
9569 */
9570
9571 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: nothing pending" ) );
9572 return( 0 );
9573}
9574
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02009575uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009576{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00009577 if( ssl->session != NULL )
9578 return( ssl->session->verify_result );
9579
9580 if( ssl->session_negotiate != NULL )
9581 return( ssl->session_negotiate->verify_result );
9582
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02009583 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00009584}
9585
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009586const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00009587{
Paul Bakker926c8e42013-03-06 10:23:34 +01009588 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009589 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01009590
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009591 return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00009592}
9593
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009594const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00009595{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009596#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009597 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009598 {
9599 switch( ssl->minor_ver )
9600 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009601 case MBEDTLS_SSL_MINOR_VERSION_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009602 return( "DTLSv1.0" );
9603
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009604 case MBEDTLS_SSL_MINOR_VERSION_3:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009605 return( "DTLSv1.2" );
9606
9607 default:
9608 return( "unknown (DTLS)" );
9609 }
9610 }
9611#endif
9612
Paul Bakker43ca69c2011-01-15 17:35:19 +00009613 switch( ssl->minor_ver )
9614 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009615 case MBEDTLS_SSL_MINOR_VERSION_0:
Paul Bakker43ca69c2011-01-15 17:35:19 +00009616 return( "SSLv3.0" );
9617
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009618 case MBEDTLS_SSL_MINOR_VERSION_1:
Paul Bakker43ca69c2011-01-15 17:35:19 +00009619 return( "TLSv1.0" );
9620
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009621 case MBEDTLS_SSL_MINOR_VERSION_2:
Paul Bakker43ca69c2011-01-15 17:35:19 +00009622 return( "TLSv1.1" );
9623
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009624 case MBEDTLS_SSL_MINOR_VERSION_3:
Paul Bakker1ef83d62012-04-11 12:09:53 +00009625 return( "TLSv1.2" );
9626
Paul Bakker43ca69c2011-01-15 17:35:19 +00009627 default:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009628 return( "unknown" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00009629 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00009630}
9631
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009632int mbedtls_ssl_get_record_expansion( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009633{
Hanno Becker3136ede2018-08-17 15:28:19 +01009634 size_t transform_expansion = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009635 const mbedtls_ssl_transform *transform = ssl->transform_out;
Hanno Becker5b559ac2018-08-03 09:40:07 +01009636 unsigned block_size;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009637
Hanno Becker5903de42019-05-03 14:46:38 +01009638 size_t out_hdr_len = mbedtls_ssl_out_hdr_len( ssl );
9639
Hanno Becker78640902018-08-13 16:35:15 +01009640 if( transform == NULL )
Hanno Becker5903de42019-05-03 14:46:38 +01009641 return( (int) out_hdr_len );
Hanno Becker78640902018-08-13 16:35:15 +01009642
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009643#if defined(MBEDTLS_ZLIB_SUPPORT)
9644 if( ssl->session_out->compression != MBEDTLS_SSL_COMPRESS_NULL )
9645 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009646#endif
9647
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009648 switch( mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009649 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009650 case MBEDTLS_MODE_GCM:
9651 case MBEDTLS_MODE_CCM:
Hanno Becker5b559ac2018-08-03 09:40:07 +01009652 case MBEDTLS_MODE_CHACHAPOLY:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009653 case MBEDTLS_MODE_STREAM:
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009654 transform_expansion = transform->minlen;
9655 break;
9656
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009657 case MBEDTLS_MODE_CBC:
Hanno Becker5b559ac2018-08-03 09:40:07 +01009658
9659 block_size = mbedtls_cipher_get_block_size(
9660 &transform->cipher_ctx_enc );
9661
Hanno Becker3136ede2018-08-17 15:28:19 +01009662 /* Expansion due to the addition of the MAC. */
9663 transform_expansion += transform->maclen;
9664
9665 /* Expansion due to the addition of CBC padding;
9666 * Theoretically up to 256 bytes, but we never use
9667 * more than the block size of the underlying cipher. */
9668 transform_expansion += block_size;
9669
9670 /* For TLS 1.1 or higher, an explicit IV is added
9671 * after the record header. */
Hanno Becker5b559ac2018-08-03 09:40:07 +01009672#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
9673 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Hanno Becker3136ede2018-08-17 15:28:19 +01009674 transform_expansion += block_size;
Hanno Becker5b559ac2018-08-03 09:40:07 +01009675#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker3136ede2018-08-17 15:28:19 +01009676
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009677 break;
9678
9679 default:
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02009680 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009681 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009682 }
9683
Hanno Beckera0e20d02019-05-15 14:03:01 +01009684#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker6cbad552019-05-08 15:40:11 +01009685 if( transform->out_cid_len != 0 )
9686 transform_expansion += MBEDTLS_SSL_MAX_CID_EXPANSION;
Hanno Beckera0e20d02019-05-15 14:03:01 +01009687#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker6cbad552019-05-08 15:40:11 +01009688
Hanno Becker5903de42019-05-03 14:46:38 +01009689 return( (int)( out_hdr_len + transform_expansion ) );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009690}
9691
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009692#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9693size_t mbedtls_ssl_get_max_frag_len( const mbedtls_ssl_context *ssl )
9694{
9695 size_t max_len;
9696
9697 /*
9698 * Assume mfl_code is correct since it was checked when set
9699 */
Angus Grattond8213d02016-05-25 20:56:48 +10009700 max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009701
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02009702 /* Check if a smaller max length was negotiated */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009703 if( ssl->session_out != NULL &&
Angus Grattond8213d02016-05-25 20:56:48 +10009704 ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009705 {
Angus Grattond8213d02016-05-25 20:56:48 +10009706 max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009707 }
9708
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02009709 /* During a handshake, use the value being negotiated */
9710 if( ssl->session_negotiate != NULL &&
9711 ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
9712 {
9713 max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
9714 }
9715
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009716 return( max_len );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009717}
9718#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
9719
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009720#if defined(MBEDTLS_SSL_PROTO_DTLS)
9721static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl )
9722{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04009723 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
9724 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
9725 ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
9726 ssl->state == MBEDTLS_SSL_SERVER_HELLO ) )
9727 return ( 0 );
9728
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009729 if( ssl->handshake == NULL || ssl->handshake->mtu == 0 )
9730 return( ssl->mtu );
9731
9732 if( ssl->mtu == 0 )
9733 return( ssl->handshake->mtu );
9734
9735 return( ssl->mtu < ssl->handshake->mtu ?
9736 ssl->mtu : ssl->handshake->mtu );
9737}
9738#endif /* MBEDTLS_SSL_PROTO_DTLS */
9739
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009740int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
9741{
9742 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
9743
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02009744#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
9745 !defined(MBEDTLS_SSL_PROTO_DTLS)
9746 (void) ssl;
9747#endif
9748
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009749#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9750 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
9751
9752 if( max_len > mfl )
9753 max_len = mfl;
9754#endif
9755
9756#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009757 if( ssl_get_current_mtu( ssl ) != 0 )
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009758 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009759 const size_t mtu = ssl_get_current_mtu( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009760 const int ret = mbedtls_ssl_get_record_expansion( ssl );
9761 const size_t overhead = (size_t) ret;
9762
9763 if( ret < 0 )
9764 return( ret );
9765
9766 if( mtu <= overhead )
9767 {
9768 MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
9769 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
9770 }
9771
9772 if( max_len > mtu - overhead )
9773 max_len = mtu - overhead;
9774 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009775#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009776
Hanno Becker0defedb2018-08-10 12:35:02 +01009777#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
9778 !defined(MBEDTLS_SSL_PROTO_DTLS)
9779 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009780#endif
9781
9782 return( (int) max_len );
9783}
9784
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009785#if defined(MBEDTLS_X509_CRT_PARSE_C)
9786const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00009787{
9788 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009789 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00009790
Hanno Beckere6824572019-02-07 13:18:46 +00009791#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009792 return( ssl->session->peer_cert );
Hanno Beckere6824572019-02-07 13:18:46 +00009793#else
9794 return( NULL );
9795#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakkerb0550d92012-10-30 07:51:03 +00009796}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009797#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00009798
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009799#if defined(MBEDTLS_SSL_CLI_C)
Hanno Beckerf852b1c2019-02-05 11:42:30 +00009800int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl,
9801 mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009802{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009803 if( ssl == NULL ||
9804 dst == NULL ||
9805 ssl->session == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009806 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009807 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009808 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009809 }
9810
Hanno Becker52055ae2019-02-06 14:30:46 +00009811 return( mbedtls_ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009812}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009813#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009814
Paul Bakker5121ce52009-01-03 21:22:43 +00009815/*
Paul Bakker1961b702013-01-25 14:49:24 +01009816 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00009817 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009818int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009819{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009820 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00009821
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009822 if( ssl == NULL || ssl->conf == NULL )
9823 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9824
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009825#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009826 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009827 ret = mbedtls_ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00009828#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009829#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009830 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009831 ret = mbedtls_ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00009832#endif
9833
Paul Bakker1961b702013-01-25 14:49:24 +01009834 return( ret );
9835}
9836
9837/*
9838 * Perform the SSL handshake
9839 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009840int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +01009841{
9842 int ret = 0;
9843
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009844 if( ssl == NULL || ssl->conf == NULL )
9845 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9846
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009847 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +01009848
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009849 while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker1961b702013-01-25 14:49:24 +01009850 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009851 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01009852
9853 if( ret != 0 )
9854 break;
9855 }
9856
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009857 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009858
9859 return( ret );
9860}
9861
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009862#if defined(MBEDTLS_SSL_RENEGOTIATION)
9863#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00009864/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009865 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00009866 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009867static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009868{
9869 int ret;
9870
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009871 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009872
9873 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009874 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
9875 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009876
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02009877 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009878 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02009879 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009880 return( ret );
9881 }
9882
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009883 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009884
9885 return( 0 );
9886}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009887#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009888
9889/*
9890 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009891 * - any side: calling mbedtls_ssl_renegotiate(),
9892 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
9893 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02009894 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009895 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009896 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009897 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009898static int ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00009899{
9900 int ret;
9901
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009902 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009903
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009904 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
9905 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00009906
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009907 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
9908 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009909#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009910 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009911 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009912 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009913 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02009914 ssl->handshake->out_msg_seq = 1;
9915 else
9916 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009917 }
9918#endif
9919
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009920 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
9921 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00009922
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009923 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00009924 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009925 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00009926 return( ret );
9927 }
9928
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009929 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009930
9931 return( 0 );
9932}
9933
9934/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009935 * Renegotiate current connection on client,
9936 * or request renegotiation on server
9937 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009938int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009939{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009940 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009941
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009942 if( ssl == NULL || ssl->conf == NULL )
9943 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9944
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009945#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009946 /* On server, just send the request */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009947 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009948 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009949 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
9950 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009951
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009952 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02009953
9954 /* Did we already try/start sending HelloRequest? */
9955 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009956 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02009957
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009958 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009959 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009960#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009961
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009962#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009963 /*
9964 * On client, either start the renegotiation process or,
9965 * if already in progress, continue the handshake
9966 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009967 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009968 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009969 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
9970 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009971
9972 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
9973 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009974 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009975 return( ret );
9976 }
9977 }
9978 else
9979 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009980 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009981 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009982 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009983 return( ret );
9984 }
9985 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009986#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009987
Paul Bakker37ce0ff2013-10-31 14:32:04 +01009988 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009989}
9990
9991/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009992 * Check record counters and renegotiate if they're above the limit.
9993 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009994static int ssl_check_ctr_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009995{
Andres AG2196c7f2016-12-15 17:01:16 +00009996 size_t ep_len = ssl_ep_len( ssl );
9997 int in_ctr_cmp;
9998 int out_ctr_cmp;
9999
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010000 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ||
10001 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010002 ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010003 {
10004 return( 0 );
10005 }
10006
Andres AG2196c7f2016-12-15 17:01:16 +000010007 in_ctr_cmp = memcmp( ssl->in_ctr + ep_len,
10008 ssl->conf->renego_period + ep_len, 8 - ep_len );
Hanno Becker19859472018-08-06 09:40:20 +010010009 out_ctr_cmp = memcmp( ssl->cur_out_ctr + ep_len,
Andres AG2196c7f2016-12-15 17:01:16 +000010010 ssl->conf->renego_period + ep_len, 8 - ep_len );
10011
10012 if( in_ctr_cmp <= 0 && out_ctr_cmp <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010013 {
10014 return( 0 );
10015 }
10016
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +020010017 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010018 return( mbedtls_ssl_renegotiate( ssl ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010019}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010020#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +000010021
10022/*
10023 * Receive application data decrypted from the SSL layer
10024 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010025int mbedtls_ssl_read( mbedtls_ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +000010026{
Hanno Becker4a810fb2017-05-24 16:27:30 +010010027 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +000010028 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +000010029
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010030 if( ssl == NULL || ssl->conf == NULL )
10031 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10032
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010033 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010034
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010035#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010036 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +020010037 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010038 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +020010039 return( ret );
10040
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010041 if( ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010042 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010043 {
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +020010044 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010045 return( ret );
10046 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +020010047 }
10048#endif
10049
Hanno Becker4a810fb2017-05-24 16:27:30 +010010050 /*
10051 * Check if renegotiation is necessary and/or handshake is
10052 * in process. If yes, perform/continue, and fall through
10053 * if an unexpected packet is received while the client
10054 * is waiting for the ServerHello.
10055 *
10056 * (There is no equivalent to the last condition on
10057 * the server-side as it is not treated as within
10058 * a handshake while waiting for the ClientHello
10059 * after a renegotiation request.)
10060 */
10061
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010062#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +010010063 ret = ssl_check_ctr_renegotiate( ssl );
10064 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
10065 ret != 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010066 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010067 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010068 return( ret );
10069 }
10070#endif
10071
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010072 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +000010073 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010074 ret = mbedtls_ssl_handshake( ssl );
Hanno Becker4a810fb2017-05-24 16:27:30 +010010075 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
10076 ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010077 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010078 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010079 return( ret );
10080 }
10081 }
10082
Hanno Beckere41158b2017-10-23 13:30:32 +010010083 /* Loop as long as no application data record is available */
Hanno Becker90333da2017-10-10 11:27:13 +010010084 while( ssl->in_offt == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010085 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +020010086 /* Start timer if not already running */
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +020010087 if( ssl->f_get_timer != NULL &&
10088 ssl->f_get_timer( ssl->p_timer ) == -1 )
10089 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010090 ssl_set_timer( ssl, ssl->conf->read_timeout );
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +020010091 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +020010092
Hanno Becker327c93b2018-08-15 13:56:18 +010010093 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010094 {
Hanno Becker4a810fb2017-05-24 16:27:30 +010010095 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
10096 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +000010097
Hanno Becker4a810fb2017-05-24 16:27:30 +010010098 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
10099 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010100 }
10101
10102 if( ssl->in_msglen == 0 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010103 ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +000010104 {
10105 /*
10106 * OpenSSL sends empty messages to randomize the IV
10107 */
Hanno Becker327c93b2018-08-15 13:56:18 +010010108 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010109 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010110 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
Paul Bakker831a7552011-05-18 13:32:51 +000010111 return( 0 );
10112
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010113 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010114 return( ret );
10115 }
10116 }
10117
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010118 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +000010119 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010120 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010121
Hanno Becker4a810fb2017-05-24 16:27:30 +010010122 /*
10123 * - For client-side, expect SERVER_HELLO_REQUEST.
10124 * - For server-side, expect CLIENT_HELLO.
10125 * - Fail (TLS) or silently drop record (DTLS) in other cases.
10126 */
10127
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010128#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010129 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010130 ( ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST ||
Hanno Becker4a810fb2017-05-24 16:27:30 +010010131 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ) )
Paul Bakker48916f92012-09-16 19:57:18 +000010132 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010133 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010134
10135 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010136#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010137 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +010010138 {
10139 continue;
10140 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010141#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010142 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010143 }
Hanno Becker4a810fb2017-05-24 16:27:30 +010010144#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010145
Hanno Becker4a810fb2017-05-24 16:27:30 +010010146#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010147 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010148 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO )
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010149 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010150 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010151
10152 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010153#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010154 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +010010155 {
10156 continue;
10157 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010158#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010159 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker48916f92012-09-16 19:57:18 +000010160 }
Hanno Becker4a810fb2017-05-24 16:27:30 +010010161#endif /* MBEDTLS_SSL_SRV_C */
10162
Hanno Becker21df7f92017-10-17 11:03:26 +010010163#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +010010164 /* Determine whether renegotiation attempt should be accepted */
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +010010165 if( ! ( ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
10166 ( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
10167 ssl->conf->allow_legacy_renegotiation ==
10168 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) ) )
10169 {
10170 /*
10171 * Accept renegotiation request
10172 */
Paul Bakker48916f92012-09-16 19:57:18 +000010173
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +010010174 /* DTLS clients need to know renego is server-initiated */
10175#if defined(MBEDTLS_SSL_PROTO_DTLS)
10176 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
10177 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
10178 {
10179 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
10180 }
10181#endif
10182 ret = ssl_start_renegotiation( ssl );
10183 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
10184 ret != 0 )
10185 {
10186 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
10187 return( ret );
10188 }
10189 }
10190 else
Hanno Becker21df7f92017-10-17 11:03:26 +010010191#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +000010192 {
Hanno Becker4a810fb2017-05-24 16:27:30 +010010193 /*
10194 * Refuse renegotiation
10195 */
10196
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010197 MBEDTLS_SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010198
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010199#if defined(MBEDTLS_SSL_PROTO_SSL3)
10200 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +000010201 {
Gilles Peskine92e44262017-05-10 17:27:49 +020010202 /* SSLv3 does not have a "no_renegotiation" warning, so
10203 we send a fatal alert and abort the connection. */
10204 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
10205 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
10206 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +000010207 }
10208 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010209#endif /* MBEDTLS_SSL_PROTO_SSL3 */
10210#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
10211 defined(MBEDTLS_SSL_PROTO_TLS1_2)
10212 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +000010213 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010214 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
10215 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
10216 MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +000010217 {
10218 return( ret );
10219 }
Paul Bakker48916f92012-09-16 19:57:18 +000010220 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +020010221 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010222#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 ||
10223 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +020010224 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010225 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
10226 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +020010227 }
Paul Bakker48916f92012-09-16 19:57:18 +000010228 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020010229
Hanno Becker90333da2017-10-10 11:27:13 +010010230 /* At this point, we don't know whether the renegotiation has been
10231 * completed or not. The cases to consider are the following:
10232 * 1) The renegotiation is complete. In this case, no new record
10233 * has been read yet.
10234 * 2) The renegotiation is incomplete because the client received
10235 * an application data record while awaiting the ServerHello.
10236 * 3) The renegotiation is incomplete because the client received
10237 * a non-handshake, non-application data message while awaiting
10238 * the ServerHello.
10239 * In each of these case, looping will be the proper action:
10240 * - For 1), the next iteration will read a new record and check
10241 * if it's application data.
10242 * - For 2), the loop condition isn't satisfied as application data
10243 * is present, hence continue is the same as break
10244 * - For 3), the loop condition is satisfied and read_record
10245 * will re-deliver the message that was held back by the client
10246 * when expecting the ServerHello.
10247 */
10248 continue;
Paul Bakker48916f92012-09-16 19:57:18 +000010249 }
Hanno Becker21df7f92017-10-17 11:03:26 +010010250#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010251 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +010010252 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010253 if( ssl->conf->renego_max_records >= 0 )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +020010254 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010255 if( ++ssl->renego_records_seen > ssl->conf->renego_max_records )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020010256 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010257 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020010258 "but not honored by client" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010259 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020010260 }
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +020010261 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +010010262 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010263#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020010264
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010265 /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */
10266 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020010267 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010268 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +010010269 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020010270 }
10271
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010272 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +000010273 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010274 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
10275 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +000010276 }
10277
10278 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +020010279
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +020010280 /* We're going to return something now, cancel timer,
10281 * except if handshake (renegotiation) is in progress */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010282 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +020010283 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010284
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +020010285#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010286 /* If we requested renego but received AppData, resend HelloRequest.
10287 * Do it now, after setting in_offt, to avoid taking this branch
10288 * again if ssl_write_hello_request() returns WANT_WRITE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010289#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010290 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010291 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010292 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020010293 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010294 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010295 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010296 return( ret );
10297 }
10298 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010299#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker4a810fb2017-05-24 16:27:30 +010010300#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +000010301 }
10302
10303 n = ( len < ssl->in_msglen )
10304 ? len : ssl->in_msglen;
10305
10306 memcpy( buf, ssl->in_offt, n );
10307 ssl->in_msglen -= n;
10308
10309 if( ssl->in_msglen == 0 )
Hanno Becker4a810fb2017-05-24 16:27:30 +010010310 {
10311 /* all bytes consumed */
Paul Bakker5121ce52009-01-03 21:22:43 +000010312 ssl->in_offt = NULL;
Hanno Beckerbdf39052017-06-09 10:42:03 +010010313 ssl->keep_current_message = 0;
Hanno Becker4a810fb2017-05-24 16:27:30 +010010314 }
Paul Bakker5121ce52009-01-03 21:22:43 +000010315 else
Hanno Becker4a810fb2017-05-24 16:27:30 +010010316 {
Paul Bakker5121ce52009-01-03 21:22:43 +000010317 /* more data available */
10318 ssl->in_offt += n;
Hanno Becker4a810fb2017-05-24 16:27:30 +010010319 }
Paul Bakker5121ce52009-01-03 21:22:43 +000010320
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010321 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010322
Paul Bakker23986e52011-04-24 08:57:21 +000010323 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +000010324}
10325
10326/*
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010010327 * Send application data to be encrypted by the SSL layer, taking care of max
10328 * fragment length and buffer size.
10329 *
10330 * According to RFC 5246 Section 6.2.1:
10331 *
10332 * Zero-length fragments of Application data MAY be sent as they are
10333 * potentially useful as a traffic analysis countermeasure.
10334 *
10335 * Therefore, it is possible that the input message length is 0 and the
10336 * corresponding return code is 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +000010337 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010338static int ssl_write_real( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010339 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +000010340{
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +020010341 int ret = mbedtls_ssl_get_max_out_record_payload( ssl );
10342 const size_t max_len = (size_t) ret;
10343
10344 if( ret < 0 )
10345 {
10346 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_get_max_out_record_payload", ret );
10347 return( ret );
10348 }
10349
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010350 if( len > max_len )
10351 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010352#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010353 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010354 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010355 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) "
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010356 "maximum fragment length: %d > %d",
10357 len, max_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010358 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010359 }
10360 else
10361#endif
10362 len = max_len;
10363 }
Paul Bakker887bd502011-06-08 13:10:54 +000010364
Paul Bakker5121ce52009-01-03 21:22:43 +000010365 if( ssl->out_left != 0 )
10366 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010010367 /*
10368 * The user has previously tried to send the data and
10369 * MBEDTLS_ERR_SSL_WANT_WRITE or the message was only partially
10370 * written. In this case, we expect the high-level write function
10371 * (e.g. mbedtls_ssl_write()) to be called with the same parameters
10372 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010373 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 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_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010376 return( ret );
10377 }
10378 }
Paul Bakker887bd502011-06-08 13:10:54 +000010379 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +000010380 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010010381 /*
10382 * The user is trying to send a message the first time, so we need to
10383 * copy the data into the internal buffers and setup the data structure
10384 * to keep track of partial writes
10385 */
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010386 ssl->out_msglen = len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010387 ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA;
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010388 memcpy( ssl->out_msg, buf, len );
Paul Bakker887bd502011-06-08 13:10:54 +000010389
Hanno Becker67bc7c32018-08-06 11:33:50 +010010390 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker887bd502011-06-08 13:10:54 +000010391 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010392 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker887bd502011-06-08 13:10:54 +000010393 return( ret );
10394 }
Paul Bakker5121ce52009-01-03 21:22:43 +000010395 }
10396
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010397 return( (int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +000010398}
10399
10400/*
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010401 * Write application data, doing 1/n-1 splitting if necessary.
10402 *
10403 * With non-blocking I/O, ssl_write_real() may return WANT_WRITE,
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +010010404 * then the caller will call us again with the same arguments, so
Hanno Becker2b187c42017-09-18 14:58:11 +010010405 * remember whether we already did the split or not.
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010406 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010407#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010408static int ssl_write_split( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010409 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010410{
10411 int ret;
10412
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +010010413 if( ssl->conf->cbc_record_splitting ==
10414 MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED ||
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +010010415 len <= 1 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010416 ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_1 ||
10417 mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc )
10418 != MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010419 {
10420 return( ssl_write_real( ssl, buf, len ) );
10421 }
10422
10423 if( ssl->split_done == 0 )
10424 {
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010010425 if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010426 return( ret );
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010010427 ssl->split_done = 1;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010428 }
10429
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010010430 if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 )
10431 return( ret );
10432 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010433
10434 return( ret + 1 );
10435}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010436#endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010437
10438/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010439 * Write application data (public-facing wrapper)
10440 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010441int mbedtls_ssl_write( mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010442{
10443 int ret;
10444
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010445 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010446
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010447 if( ssl == NULL || ssl->conf == NULL )
10448 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10449
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010450#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010451 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
10452 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010453 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010454 return( ret );
10455 }
10456#endif
10457
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010458 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010459 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010460 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010461 {
Manuel Pégourié-Gonnard151dc772015-05-14 13:55:51 +020010462 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010463 return( ret );
10464 }
10465 }
10466
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010467#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010468 ret = ssl_write_split( ssl, buf, len );
10469#else
10470 ret = ssl_write_real( ssl, buf, len );
10471#endif
10472
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010473 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010474
10475 return( ret );
10476}
10477
10478/*
Paul Bakker5121ce52009-01-03 21:22:43 +000010479 * Notify the peer that the connection is being closed
10480 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010481int mbedtls_ssl_close_notify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000010482{
10483 int ret;
10484
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010485 if( ssl == NULL || ssl->conf == NULL )
10486 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10487
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010488 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010489
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +020010490 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010491 return( mbedtls_ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010492
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010493 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +000010494 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010495 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
10496 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
10497 MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010498 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010499 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010500 return( ret );
10501 }
10502 }
10503
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010504 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010505
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +020010506 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +000010507}
10508
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010509void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform )
Paul Bakker48916f92012-09-16 19:57:18 +000010510{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010511 if( transform == NULL )
10512 return;
10513
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010514#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +000010515 deflateEnd( &transform->ctx_deflate );
10516 inflateEnd( &transform->ctx_inflate );
10517#endif
10518
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010519 mbedtls_cipher_free( &transform->cipher_ctx_enc );
10520 mbedtls_cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +020010521
Hanno Beckerd56ed242018-01-03 15:32:51 +000010522#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010523 mbedtls_md_free( &transform->md_ctx_enc );
10524 mbedtls_md_free( &transform->md_ctx_dec );
Hanno Beckerd56ed242018-01-03 15:32:51 +000010525#endif
Paul Bakker61d113b2013-07-04 11:51:43 +020010526
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010527 mbedtls_platform_zeroize( transform, sizeof( mbedtls_ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010528}
10529
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010530#if defined(MBEDTLS_X509_CRT_PARSE_C)
10531static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010532{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010533 mbedtls_ssl_key_cert *cur = key_cert, *next;
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010534
10535 while( cur != NULL )
10536 {
10537 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010538 mbedtls_free( cur );
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010539 cur = next;
10540 }
10541}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010542#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010543
Hanno Becker0271f962018-08-16 13:23:47 +010010544#if defined(MBEDTLS_SSL_PROTO_DTLS)
10545
10546static void ssl_buffering_free( mbedtls_ssl_context *ssl )
10547{
10548 unsigned offset;
10549 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
10550
10551 if( hs == NULL )
10552 return;
10553
Hanno Becker283f5ef2018-08-24 09:34:47 +010010554 ssl_free_buffered_record( ssl );
10555
Hanno Becker0271f962018-08-16 13:23:47 +010010556 for( offset = 0; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
Hanno Beckere605b192018-08-21 15:59:07 +010010557 ssl_buffering_free_slot( ssl, offset );
10558}
10559
10560static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
10561 uint8_t slot )
10562{
10563 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
10564 mbedtls_ssl_hs_buffer * const hs_buf = &hs->buffering.hs[slot];
Hanno Beckerb309b922018-08-23 13:18:05 +010010565
10566 if( slot >= MBEDTLS_SSL_MAX_BUFFERED_HS )
10567 return;
10568
Hanno Beckere605b192018-08-21 15:59:07 +010010569 if( hs_buf->is_valid == 1 )
Hanno Becker0271f962018-08-16 13:23:47 +010010570 {
Hanno Beckere605b192018-08-21 15:59:07 +010010571 hs->buffering.total_bytes_buffered -= hs_buf->data_len;
Hanno Becker805f2e12018-10-12 16:31:41 +010010572 mbedtls_platform_zeroize( hs_buf->data, hs_buf->data_len );
Hanno Beckere605b192018-08-21 15:59:07 +010010573 mbedtls_free( hs_buf->data );
10574 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Hanno Becker0271f962018-08-16 13:23:47 +010010575 }
10576}
10577
10578#endif /* MBEDTLS_SSL_PROTO_DTLS */
10579
Gilles Peskine9b562d52018-04-25 20:32:43 +020010580void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +000010581{
Gilles Peskine9b562d52018-04-25 20:32:43 +020010582 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
10583
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010584 if( handshake == NULL )
10585 return;
10586
Gilles Peskinedf13d5c2018-04-25 20:39:48 +020010587#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
10588 if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
10589 {
Gilles Peskine8f97af72018-04-26 11:46:10 +020010590 ssl->conf->f_async_cancel( ssl );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +020010591 handshake->async_in_progress = 0;
10592 }
10593#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
10594
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010595#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
10596 defined(MBEDTLS_SSL_PROTO_TLS1_1)
10597 mbedtls_md5_free( &handshake->fin_md5 );
10598 mbedtls_sha1_free( &handshake->fin_sha1 );
10599#endif
10600#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
10601#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -050010602#if defined(MBEDTLS_USE_PSA_CRYPTO)
10603 psa_hash_abort( &handshake->fin_sha256_psa );
10604#else
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010605 mbedtls_sha256_free( &handshake->fin_sha256 );
10606#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -050010607#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010608#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -050010609#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -050010610 psa_hash_abort( &handshake->fin_sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -050010611#else
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010612 mbedtls_sha512_free( &handshake->fin_sha512 );
10613#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -050010614#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010615#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
10616
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010617#if defined(MBEDTLS_DHM_C)
10618 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +000010619#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010620#if defined(MBEDTLS_ECDH_C)
10621 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +020010622#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +020010623#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +020010624 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +020010625#if defined(MBEDTLS_SSL_CLI_C)
10626 mbedtls_free( handshake->ecjpake_cache );
10627 handshake->ecjpake_cache = NULL;
10628 handshake->ecjpake_cache_len = 0;
10629#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +020010630#endif
Paul Bakker61d113b2013-07-04 11:51:43 +020010631
Janos Follath4ae5c292016-02-10 11:27:43 +000010632#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
10633 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +020010634 /* explicit void pointer cast for buggy MS compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010635 mbedtls_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +020010636#endif
10637
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +010010638#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
10639 if( handshake->psk != NULL )
10640 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010641 mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +010010642 mbedtls_free( handshake->psk );
10643 }
10644#endif
10645
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010646#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
10647 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020010648 /*
10649 * Free only the linked list wrapper, not the keys themselves
10650 * since the belong to the SNI callback
10651 */
10652 if( handshake->sni_key_cert != NULL )
10653 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010654 mbedtls_ssl_key_cert *cur = handshake->sni_key_cert, *next;
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020010655
10656 while( cur != NULL )
10657 {
10658 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010659 mbedtls_free( cur );
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020010660 cur = next;
10661 }
10662 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010663#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010664
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +020010665#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +020010666 mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx );
Hanno Becker3dad3112019-02-05 17:19:52 +000010667 if( handshake->ecrs_peer_cert != NULL )
10668 {
10669 mbedtls_x509_crt_free( handshake->ecrs_peer_cert );
10670 mbedtls_free( handshake->ecrs_peer_cert );
10671 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +020010672#endif
10673
Hanno Becker75173122019-02-06 16:18:31 +000010674#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
10675 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
10676 mbedtls_pk_free( &handshake->peer_pubkey );
10677#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
10678
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010679#if defined(MBEDTLS_SSL_PROTO_DTLS)
10680 mbedtls_free( handshake->verify_cookie );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +020010681 ssl_flight_free( handshake->flight );
Hanno Becker0271f962018-08-16 13:23:47 +010010682 ssl_buffering_free( ssl );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +020010683#endif
10684
Hanno Becker4a63ed42019-01-08 11:39:35 +000010685#if defined(MBEDTLS_ECDH_C) && \
10686 defined(MBEDTLS_USE_PSA_CRYPTO)
10687 psa_destroy_key( handshake->ecdh_psa_privkey );
10688#endif /* MBEDTLS_ECDH_C && MBEDTLS_USE_PSA_CRYPTO */
10689
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010690 mbedtls_platform_zeroize( handshake,
10691 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010692}
10693
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010694void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +000010695{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010696 if( session == NULL )
10697 return;
10698
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010699#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker1294a0b2019-02-05 12:38:15 +000010700 ssl_clear_peer_cert( session );
Paul Bakkered27a042013-04-18 22:46:23 +020010701#endif
Paul Bakker0a597072012-09-25 21:55:46 +000010702
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +020010703#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010704 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +020010705#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +020010706
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010707 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010708}
10709
Paul Bakker5121ce52009-01-03 21:22:43 +000010710/*
10711 * Free an SSL context
10712 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010713void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000010714{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010715 if( ssl == NULL )
10716 return;
10717
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010718 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010719
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +010010720 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010721 {
Angus Grattond8213d02016-05-25 20:56:48 +100010722 mbedtls_platform_zeroize( ssl->out_buf, MBEDTLS_SSL_OUT_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010723 mbedtls_free( ssl->out_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +000010724 }
10725
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +010010726 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010727 {
Angus Grattond8213d02016-05-25 20:56:48 +100010728 mbedtls_platform_zeroize( ssl->in_buf, MBEDTLS_SSL_IN_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010729 mbedtls_free( ssl->in_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +000010730 }
10731
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010732#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker16770332013-10-11 09:59:44 +020010733 if( ssl->compress_buf != NULL )
10734 {
Angus Grattond8213d02016-05-25 20:56:48 +100010735 mbedtls_platform_zeroize( ssl->compress_buf, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010736 mbedtls_free( ssl->compress_buf );
Paul Bakker16770332013-10-11 09:59:44 +020010737 }
10738#endif
10739
Paul Bakker48916f92012-09-16 19:57:18 +000010740 if( ssl->transform )
10741 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010742 mbedtls_ssl_transform_free( ssl->transform );
10743 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +000010744 }
10745
10746 if( ssl->handshake )
10747 {
Gilles Peskine9b562d52018-04-25 20:32:43 +020010748 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010749 mbedtls_ssl_transform_free( ssl->transform_negotiate );
10750 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +000010751
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010752 mbedtls_free( ssl->handshake );
10753 mbedtls_free( ssl->transform_negotiate );
10754 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +000010755 }
10756
Paul Bakkerc0463502013-02-14 11:19:38 +010010757 if( ssl->session )
10758 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010759 mbedtls_ssl_session_free( ssl->session );
10760 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +010010761 }
10762
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +020010763#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker66d5d072014-06-17 16:39:18 +020010764 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010765 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010766 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010767 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +000010768 }
Paul Bakker0be444a2013-08-27 21:55:01 +020010769#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000010770
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010771#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
10772 if( mbedtls_ssl_hw_record_finish != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +000010773 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010774 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_finish()" ) );
10775 mbedtls_ssl_hw_record_finish( ssl );
Paul Bakker05ef8352012-05-08 09:17:57 +000010776 }
10777#endif
10778
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +020010779#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010780 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +020010781#endif
10782
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010783 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +000010784
Paul Bakker86f04f42013-02-14 11:20:09 +010010785 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010786 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010787}
10788
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010789/*
10790 * Initialze mbedtls_ssl_config
10791 */
10792void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
10793{
10794 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
10795}
10796
Simon Butcherc97b6972015-12-27 23:48:17 +000010797#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010798static int ssl_preset_default_hashes[] = {
10799#if defined(MBEDTLS_SHA512_C)
10800 MBEDTLS_MD_SHA512,
10801 MBEDTLS_MD_SHA384,
10802#endif
10803#if defined(MBEDTLS_SHA256_C)
10804 MBEDTLS_MD_SHA256,
10805 MBEDTLS_MD_SHA224,
10806#endif
Gilles Peskine5d2511c2017-05-12 13:16:40 +020010807#if defined(MBEDTLS_SHA1_C) && defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_KEY_EXCHANGE)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010808 MBEDTLS_MD_SHA1,
10809#endif
10810 MBEDTLS_MD_NONE
10811};
Simon Butcherc97b6972015-12-27 23:48:17 +000010812#endif
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010813
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010814static int ssl_preset_suiteb_ciphersuites[] = {
10815 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
10816 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
10817 0
10818};
10819
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010820#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010821static int ssl_preset_suiteb_hashes[] = {
10822 MBEDTLS_MD_SHA256,
10823 MBEDTLS_MD_SHA384,
10824 MBEDTLS_MD_NONE
10825};
10826#endif
10827
10828#if defined(MBEDTLS_ECP_C)
10829static mbedtls_ecp_group_id ssl_preset_suiteb_curves[] = {
Jaeden Amerod4311042019-06-03 08:27:16 +010010830#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010831 MBEDTLS_ECP_DP_SECP256R1,
Jaeden Amerod4311042019-06-03 08:27:16 +010010832#endif
10833#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010834 MBEDTLS_ECP_DP_SECP384R1,
Jaeden Amerod4311042019-06-03 08:27:16 +010010835#endif
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010836 MBEDTLS_ECP_DP_NONE
10837};
10838#endif
10839
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010840/*
Tillmann Karras588ad502015-09-25 04:27:22 +020010841 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010842 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020010843int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010844 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010845{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +020010846#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010847 int ret;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +020010848#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010849
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +020010850 /* Use the functions here so that they are covered in tests,
10851 * but otherwise access member directly for efficiency */
10852 mbedtls_ssl_conf_endpoint( conf, endpoint );
10853 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010854
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010855 /*
10856 * Things that are common to all presets
10857 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020010858#if defined(MBEDTLS_SSL_CLI_C)
10859 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
10860 {
10861 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
10862#if defined(MBEDTLS_SSL_SESSION_TICKETS)
10863 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
10864#endif
10865 }
10866#endif
10867
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +020010868#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010869 conf->arc4_disabled = MBEDTLS_SSL_ARC4_DISABLED;
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +020010870#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010871
10872#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
10873 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
10874#endif
10875
10876#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
10877 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
10878#endif
10879
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +010010880#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
10881 conf->cbc_record_splitting = MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED;
10882#endif
10883
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +020010884#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010885 conf->f_cookie_write = ssl_cookie_write_dummy;
10886 conf->f_cookie_check = ssl_cookie_check_dummy;
10887#endif
10888
10889#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
10890 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
10891#endif
10892
Janos Follath088ce432017-04-10 12:42:31 +010010893#if defined(MBEDTLS_SSL_SRV_C)
10894 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
10895#endif
10896
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010897#if defined(MBEDTLS_SSL_PROTO_DTLS)
10898 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
10899 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
10900#endif
10901
10902#if defined(MBEDTLS_SSL_RENEGOTIATION)
10903 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Andres AG2196c7f2016-12-15 17:01:16 +000010904 memset( conf->renego_period, 0x00, 2 );
10905 memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010906#endif
10907
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010908#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
10909 if( endpoint == MBEDTLS_SSL_IS_SERVER )
10910 {
Hanno Becker00d0a682017-10-04 13:14:29 +010010911 const unsigned char dhm_p[] =
10912 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
10913 const unsigned char dhm_g[] =
10914 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
10915
Hanno Beckera90658f2017-10-04 15:29:08 +010010916 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
10917 dhm_p, sizeof( dhm_p ),
10918 dhm_g, sizeof( dhm_g ) ) ) != 0 )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010919 {
10920 return( ret );
10921 }
10922 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +020010923#endif
10924
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010925 /*
10926 * Preset-specific defaults
10927 */
10928 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010929 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010930 /*
10931 * NSA Suite B
10932 */
10933 case MBEDTLS_SSL_PRESET_SUITEB:
10934 conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
10935 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */
10936 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
10937 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
10938
10939 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
10940 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
10941 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
10942 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
10943 ssl_preset_suiteb_ciphersuites;
10944
10945#if defined(MBEDTLS_X509_CRT_PARSE_C)
10946 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010947#endif
10948
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010949#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010950 conf->sig_hashes = ssl_preset_suiteb_hashes;
10951#endif
10952
10953#if defined(MBEDTLS_ECP_C)
10954 conf->curve_list = ssl_preset_suiteb_curves;
10955#endif
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +020010956 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010957
10958 /*
10959 * Default
10960 */
10961 default:
Ron Eldor5e9f14d2017-05-28 10:46:38 +030010962 conf->min_major_ver = ( MBEDTLS_SSL_MIN_MAJOR_VERSION >
10963 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION ) ?
10964 MBEDTLS_SSL_MIN_MAJOR_VERSION :
10965 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION;
10966 conf->min_minor_ver = ( MBEDTLS_SSL_MIN_MINOR_VERSION >
10967 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION ) ?
10968 MBEDTLS_SSL_MIN_MINOR_VERSION :
10969 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010970 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
10971 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
10972
10973#if defined(MBEDTLS_SSL_PROTO_DTLS)
10974 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
10975 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_2;
10976#endif
10977
10978 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
10979 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
10980 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
10981 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
10982 mbedtls_ssl_list_ciphersuites();
10983
10984#if defined(MBEDTLS_X509_CRT_PARSE_C)
10985 conf->cert_profile = &mbedtls_x509_crt_profile_default;
10986#endif
10987
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010988#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010989 conf->sig_hashes = ssl_preset_default_hashes;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010990#endif
10991
10992#if defined(MBEDTLS_ECP_C)
10993 conf->curve_list = mbedtls_ecp_grp_id_list();
10994#endif
10995
10996#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
10997 conf->dhm_min_bitlen = 1024;
10998#endif
10999 }
11000
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011001 return( 0 );
11002}
11003
11004/*
11005 * Free mbedtls_ssl_config
11006 */
11007void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
11008{
11009#if defined(MBEDTLS_DHM_C)
11010 mbedtls_mpi_free( &conf->dhm_P );
11011 mbedtls_mpi_free( &conf->dhm_G );
11012#endif
11013
11014#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
11015 if( conf->psk != NULL )
11016 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050011017 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011018 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +000011019 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011020 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +090011021 }
11022
11023 if( conf->psk_identity != NULL )
11024 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050011025 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +090011026 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +000011027 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011028 conf->psk_identity_len = 0;
11029 }
11030#endif
11031
11032#if defined(MBEDTLS_X509_CRT_PARSE_C)
11033 ssl_key_cert_free( conf->key_cert );
11034#endif
11035
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050011036 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011037}
11038
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020011039#if defined(MBEDTLS_PK_C) && \
11040 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020011041/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011042 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020011043 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011044unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020011045{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011046#if defined(MBEDTLS_RSA_C)
11047 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
11048 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020011049#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011050#if defined(MBEDTLS_ECDSA_C)
11051 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
11052 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020011053#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011054 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020011055}
11056
Hanno Becker7e5437a2017-04-28 17:15:26 +010011057unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
11058{
11059 switch( type ) {
11060 case MBEDTLS_PK_RSA:
11061 return( MBEDTLS_SSL_SIG_RSA );
11062 case MBEDTLS_PK_ECDSA:
11063 case MBEDTLS_PK_ECKEY:
11064 return( MBEDTLS_SSL_SIG_ECDSA );
11065 default:
11066 return( MBEDTLS_SSL_SIG_ANON );
11067 }
11068}
11069
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011070mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011071{
11072 switch( sig )
11073 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011074#if defined(MBEDTLS_RSA_C)
11075 case MBEDTLS_SSL_SIG_RSA:
11076 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011077#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011078#if defined(MBEDTLS_ECDSA_C)
11079 case MBEDTLS_SSL_SIG_ECDSA:
11080 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011081#endif
11082 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011083 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011084 }
11085}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020011086#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011087
Hanno Becker7e5437a2017-04-28 17:15:26 +010011088#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
11089 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
11090
11091/* Find an entry in a signature-hash set matching a given hash algorithm. */
11092mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set,
11093 mbedtls_pk_type_t sig_alg )
11094{
11095 switch( sig_alg )
11096 {
11097 case MBEDTLS_PK_RSA:
11098 return( set->rsa );
11099 case MBEDTLS_PK_ECDSA:
11100 return( set->ecdsa );
11101 default:
11102 return( MBEDTLS_MD_NONE );
11103 }
11104}
11105
11106/* Add a signature-hash-pair to a signature-hash set */
11107void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set,
11108 mbedtls_pk_type_t sig_alg,
11109 mbedtls_md_type_t md_alg )
11110{
11111 switch( sig_alg )
11112 {
11113 case MBEDTLS_PK_RSA:
11114 if( set->rsa == MBEDTLS_MD_NONE )
11115 set->rsa = md_alg;
11116 break;
11117
11118 case MBEDTLS_PK_ECDSA:
11119 if( set->ecdsa == MBEDTLS_MD_NONE )
11120 set->ecdsa = md_alg;
11121 break;
11122
11123 default:
11124 break;
11125 }
11126}
11127
11128/* Allow exactly one hash algorithm for each signature. */
11129void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set,
11130 mbedtls_md_type_t md_alg )
11131{
11132 set->rsa = md_alg;
11133 set->ecdsa = md_alg;
11134}
11135
11136#endif /* MBEDTLS_SSL_PROTO_TLS1_2) &&
11137 MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
11138
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020011139/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020011140 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020011141 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011142mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011143{
11144 switch( hash )
11145 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011146#if defined(MBEDTLS_MD5_C)
11147 case MBEDTLS_SSL_HASH_MD5:
11148 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011149#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011150#if defined(MBEDTLS_SHA1_C)
11151 case MBEDTLS_SSL_HASH_SHA1:
11152 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011153#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011154#if defined(MBEDTLS_SHA256_C)
11155 case MBEDTLS_SSL_HASH_SHA224:
11156 return( MBEDTLS_MD_SHA224 );
11157 case MBEDTLS_SSL_HASH_SHA256:
11158 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011159#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011160#if defined(MBEDTLS_SHA512_C)
11161 case MBEDTLS_SSL_HASH_SHA384:
11162 return( MBEDTLS_MD_SHA384 );
11163 case MBEDTLS_SSL_HASH_SHA512:
11164 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011165#endif
11166 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011167 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011168 }
11169}
11170
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020011171/*
11172 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
11173 */
11174unsigned char mbedtls_ssl_hash_from_md_alg( int md )
11175{
11176 switch( md )
11177 {
11178#if defined(MBEDTLS_MD5_C)
11179 case MBEDTLS_MD_MD5:
11180 return( MBEDTLS_SSL_HASH_MD5 );
11181#endif
11182#if defined(MBEDTLS_SHA1_C)
11183 case MBEDTLS_MD_SHA1:
11184 return( MBEDTLS_SSL_HASH_SHA1 );
11185#endif
11186#if defined(MBEDTLS_SHA256_C)
11187 case MBEDTLS_MD_SHA224:
11188 return( MBEDTLS_SSL_HASH_SHA224 );
11189 case MBEDTLS_MD_SHA256:
11190 return( MBEDTLS_SSL_HASH_SHA256 );
11191#endif
11192#if defined(MBEDTLS_SHA512_C)
11193 case MBEDTLS_MD_SHA384:
11194 return( MBEDTLS_SSL_HASH_SHA384 );
11195 case MBEDTLS_MD_SHA512:
11196 return( MBEDTLS_SSL_HASH_SHA512 );
11197#endif
11198 default:
11199 return( MBEDTLS_SSL_HASH_NONE );
11200 }
11201}
11202
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020011203#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011204/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020011205 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020011206 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011207 */
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020011208int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011209{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011210 const mbedtls_ecp_group_id *gid;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011211
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020011212 if( ssl->conf->curve_list == NULL )
11213 return( -1 );
11214
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020011215 for( gid = ssl->conf->curve_list; *gid != MBEDTLS_ECP_DP_NONE; gid++ )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011216 if( *gid == grp_id )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020011217 return( 0 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011218
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020011219 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011220}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020011221#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011222
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020011223#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020011224/*
11225 * Check if a hash proposed by the peer is in our list.
11226 * Return 0 if we're willing to use it, -1 otherwise.
11227 */
11228int mbedtls_ssl_check_sig_hash( const mbedtls_ssl_context *ssl,
11229 mbedtls_md_type_t md )
11230{
11231 const int *cur;
11232
11233 if( ssl->conf->sig_hashes == NULL )
11234 return( -1 );
11235
11236 for( cur = ssl->conf->sig_hashes; *cur != MBEDTLS_MD_NONE; cur++ )
11237 if( *cur == (int) md )
11238 return( 0 );
11239
11240 return( -1 );
11241}
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020011242#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020011243
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011244#if defined(MBEDTLS_X509_CRT_PARSE_C)
11245int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
11246 const mbedtls_ssl_ciphersuite_t *ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011247 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +020011248 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011249{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011250 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011251#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011252 int usage = 0;
11253#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011254#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011255 const char *ext_oid;
11256 size_t ext_len;
11257#endif
11258
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011259#if !defined(MBEDTLS_X509_CHECK_KEY_USAGE) && \
11260 !defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011261 ((void) cert);
11262 ((void) cert_endpoint);
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011263 ((void) flags);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011264#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011265
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011266#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
11267 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011268 {
11269 /* Server part of the key exchange */
11270 switch( ciphersuite->key_exchange )
11271 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011272 case MBEDTLS_KEY_EXCHANGE_RSA:
11273 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010011274 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011275 break;
11276
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011277 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
11278 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
11279 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
11280 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011281 break;
11282
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011283 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
11284 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010011285 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011286 break;
11287
11288 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011289 case MBEDTLS_KEY_EXCHANGE_NONE:
11290 case MBEDTLS_KEY_EXCHANGE_PSK:
11291 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
11292 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +020011293 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011294 usage = 0;
11295 }
11296 }
11297 else
11298 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011299 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
11300 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011301 }
11302
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011303 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011304 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010011305 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011306 ret = -1;
11307 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011308#else
11309 ((void) ciphersuite);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011310#endif /* MBEDTLS_X509_CHECK_KEY_USAGE */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011311
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011312#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
11313 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011314 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011315 ext_oid = MBEDTLS_OID_SERVER_AUTH;
11316 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011317 }
11318 else
11319 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011320 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
11321 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011322 }
11323
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011324 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011325 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010011326 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011327 ret = -1;
11328 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011329#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011330
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011331 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011332}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011333#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +020011334
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011335/*
11336 * Convert version numbers to/from wire format
11337 * and, for DTLS, to/from TLS equivalent.
11338 *
11339 * For TLS this is the identity.
Brian J Murray1903fb32016-11-06 04:45:15 -080011340 * For DTLS, use 1's complement (v -> 255 - v, and then map as follows:
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011341 * 1.0 <-> 3.2 (DTLS 1.0 is based on TLS 1.1)
11342 * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2)
11343 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011344void mbedtls_ssl_write_version( int major, int minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011345 unsigned char ver[2] )
11346{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011347#if defined(MBEDTLS_SSL_PROTO_DTLS)
11348 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011349 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011350 if( minor == MBEDTLS_SSL_MINOR_VERSION_2 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011351 --minor; /* DTLS 1.0 stored as TLS 1.1 internally */
11352
11353 ver[0] = (unsigned char)( 255 - ( major - 2 ) );
11354 ver[1] = (unsigned char)( 255 - ( minor - 1 ) );
11355 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010011356 else
11357#else
11358 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011359#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010011360 {
11361 ver[0] = (unsigned char) major;
11362 ver[1] = (unsigned char) minor;
11363 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011364}
11365
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011366void mbedtls_ssl_read_version( int *major, int *minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011367 const unsigned char ver[2] )
11368{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011369#if defined(MBEDTLS_SSL_PROTO_DTLS)
11370 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011371 {
11372 *major = 255 - ver[0] + 2;
11373 *minor = 255 - ver[1] + 1;
11374
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011375 if( *minor == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011376 ++*minor; /* DTLS 1.0 stored as TLS 1.1 internally */
11377 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010011378 else
11379#else
11380 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011381#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010011382 {
11383 *major = ver[0];
11384 *minor = ver[1];
11385 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011386}
11387
Simon Butcher99000142016-10-13 17:21:01 +010011388int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md )
11389{
11390#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
11391 if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 )
11392 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
11393
11394 switch( md )
11395 {
11396#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
11397#if defined(MBEDTLS_MD5_C)
11398 case MBEDTLS_SSL_HASH_MD5:
Janos Follath182013f2016-10-25 10:50:22 +010011399 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
Simon Butcher99000142016-10-13 17:21:01 +010011400#endif
11401#if defined(MBEDTLS_SHA1_C)
11402 case MBEDTLS_SSL_HASH_SHA1:
11403 ssl->handshake->calc_verify = ssl_calc_verify_tls;
11404 break;
11405#endif
11406#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
11407#if defined(MBEDTLS_SHA512_C)
11408 case MBEDTLS_SSL_HASH_SHA384:
11409 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
11410 break;
11411#endif
11412#if defined(MBEDTLS_SHA256_C)
11413 case MBEDTLS_SSL_HASH_SHA256:
11414 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
11415 break;
11416#endif
11417 default:
11418 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
11419 }
11420
11421 return 0;
11422#else /* !MBEDTLS_SSL_PROTO_TLS1_2 */
11423 (void) ssl;
11424 (void) md;
11425
11426 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
11427#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
11428}
11429
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011430#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
11431 defined(MBEDTLS_SSL_PROTO_TLS1_1)
11432int mbedtls_ssl_get_key_exchange_md_ssl_tls( mbedtls_ssl_context *ssl,
11433 unsigned char *output,
11434 unsigned char *data, size_t data_len )
11435{
11436 int ret = 0;
11437 mbedtls_md5_context mbedtls_md5;
11438 mbedtls_sha1_context mbedtls_sha1;
11439
11440 mbedtls_md5_init( &mbedtls_md5 );
11441 mbedtls_sha1_init( &mbedtls_sha1 );
11442
11443 /*
11444 * digitally-signed struct {
11445 * opaque md5_hash[16];
11446 * opaque sha_hash[20];
11447 * };
11448 *
11449 * md5_hash
11450 * MD5(ClientHello.random + ServerHello.random
11451 * + ServerParams);
11452 * sha_hash
11453 * SHA(ClientHello.random + ServerHello.random
11454 * + ServerParams);
11455 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011456 if( ( ret = mbedtls_md5_starts_ret( &mbedtls_md5 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011457 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011458 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011459 goto exit;
11460 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011461 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011462 ssl->handshake->randbytes, 64 ) ) != 0 )
11463 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011464 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011465 goto exit;
11466 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011467 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5, data, data_len ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011468 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011469 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011470 goto exit;
11471 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011472 if( ( ret = mbedtls_md5_finish_ret( &mbedtls_md5, output ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011473 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011474 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011475 goto exit;
11476 }
11477
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011478 if( ( ret = mbedtls_sha1_starts_ret( &mbedtls_sha1 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011479 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011480 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011481 goto exit;
11482 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011483 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011484 ssl->handshake->randbytes, 64 ) ) != 0 )
11485 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011486 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011487 goto exit;
11488 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011489 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1, data,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011490 data_len ) ) != 0 )
11491 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011492 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011493 goto exit;
11494 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011495 if( ( ret = mbedtls_sha1_finish_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011496 output + 16 ) ) != 0 )
11497 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011498 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011499 goto exit;
11500 }
11501
11502exit:
11503 mbedtls_md5_free( &mbedtls_md5 );
11504 mbedtls_sha1_free( &mbedtls_sha1 );
11505
11506 if( ret != 0 )
11507 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
11508 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
11509
11510 return( ret );
11511
11512}
11513#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
11514 MBEDTLS_SSL_PROTO_TLS1_1 */
11515
11516#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
11517 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011518
11519#if defined(MBEDTLS_USE_PSA_CRYPTO)
11520int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
11521 unsigned char *hash, size_t *hashlen,
11522 unsigned char *data, size_t data_len,
11523 mbedtls_md_type_t md_alg )
11524{
Andrzej Kurek814feff2019-01-14 04:35:19 -050011525 psa_status_t status;
Jaeden Amero34973232019-02-20 10:32:28 +000011526 psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT;
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011527 psa_algorithm_t hash_alg = mbedtls_psa_translate_md( md_alg );
11528
Hanno Becker4c8c7aa2019-04-10 09:25:41 +010011529 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform PSA-based computation of digest of ServerKeyExchange" ) );
Andrzej Kurek814feff2019-01-14 04:35:19 -050011530
11531 if( ( status = psa_hash_setup( &hash_operation,
11532 hash_alg ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011533 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050011534 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_setup", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011535 goto exit;
11536 }
11537
Andrzej Kurek814feff2019-01-14 04:35:19 -050011538 if( ( status = psa_hash_update( &hash_operation, ssl->handshake->randbytes,
11539 64 ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011540 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050011541 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011542 goto exit;
11543 }
11544
Andrzej Kurek814feff2019-01-14 04:35:19 -050011545 if( ( status = psa_hash_update( &hash_operation,
11546 data, data_len ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011547 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050011548 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011549 goto exit;
11550 }
11551
Andrzej Kurek814feff2019-01-14 04:35:19 -050011552 if( ( status = psa_hash_finish( &hash_operation, hash, MBEDTLS_MD_MAX_SIZE,
11553 hashlen ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011554 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050011555 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_finish", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011556 goto exit;
11557 }
11558
11559exit:
Andrzej Kurek814feff2019-01-14 04:35:19 -050011560 if( status != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011561 {
11562 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
11563 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Andrzej Kurek814feff2019-01-14 04:35:19 -050011564 switch( status )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011565 {
11566 case PSA_ERROR_NOT_SUPPORTED:
11567 return( MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE );
Andrzej Kurek814feff2019-01-14 04:35:19 -050011568 case PSA_ERROR_BAD_STATE: /* Intentional fallthrough */
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011569 case PSA_ERROR_BUFFER_TOO_SMALL:
11570 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
11571 case PSA_ERROR_INSUFFICIENT_MEMORY:
11572 return( MBEDTLS_ERR_MD_ALLOC_FAILED );
11573 default:
11574 return( MBEDTLS_ERR_MD_HW_ACCEL_FAILED );
11575 }
11576 }
11577 return( 0 );
11578}
11579
11580#else
11581
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011582int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
Gilles Peskineca1d7422018-04-24 11:53:22 +020011583 unsigned char *hash, size_t *hashlen,
11584 unsigned char *data, size_t data_len,
11585 mbedtls_md_type_t md_alg )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011586{
11587 int ret = 0;
11588 mbedtls_md_context_t ctx;
11589 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
Gilles Peskineca1d7422018-04-24 11:53:22 +020011590 *hashlen = mbedtls_md_get_size( md_info );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011591
Hanno Becker4c8c7aa2019-04-10 09:25:41 +010011592 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform mbedtls-based computation of digest of ServerKeyExchange" ) );
Andrzej Kurek814feff2019-01-14 04:35:19 -050011593
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011594 mbedtls_md_init( &ctx );
11595
11596 /*
11597 * digitally-signed struct {
11598 * opaque client_random[32];
11599 * opaque server_random[32];
11600 * ServerDHParams params;
11601 * };
11602 */
11603 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
11604 {
11605 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
11606 goto exit;
11607 }
11608 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
11609 {
11610 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
11611 goto exit;
11612 }
11613 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
11614 {
11615 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
11616 goto exit;
11617 }
11618 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
11619 {
11620 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
11621 goto exit;
11622 }
Gilles Peskineca1d7422018-04-24 11:53:22 +020011623 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011624 {
11625 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
11626 goto exit;
11627 }
11628
11629exit:
11630 mbedtls_md_free( &ctx );
11631
11632 if( ret != 0 )
11633 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
11634 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
11635
11636 return( ret );
11637}
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011638#endif /* MBEDTLS_USE_PSA_CRYPTO */
11639
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011640#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
11641 MBEDTLS_SSL_PROTO_TLS1_2 */
11642
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011643#endif /* MBEDTLS_SSL_TLS_C */