blob: ad6c9fce01f9f08e0bfc998e811cab63f485fbf3 [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)
112int mbedtls_ssl_check_record( mbedtls_ssl_context const *ssl,
113 unsigned char *buf,
114 size_t buflen )
115{
116 ((void) ssl);
117 ((void) buf);
118 ((void) buflen);
119 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
120}
121#endif /* MBEDTLS_SSL_RECORD_CHECKING */
122
Hanno Becker67bc7c32018-08-06 11:33:50 +0100123#define SSL_DONT_FORCE_FLUSH 0
124#define SSL_FORCE_FLUSH 1
125
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200126#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker2b1e3542018-08-06 11:19:13 +0100127
Hanno Beckera0e20d02019-05-15 14:03:01 +0100128#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100129/* Top-level Connection ID API */
130
Hanno Becker8367ccc2019-05-14 11:30:10 +0100131int mbedtls_ssl_conf_cid( mbedtls_ssl_config *conf,
132 size_t len,
133 int ignore_other_cid )
Hanno Beckerad4a1372019-05-03 13:06:44 +0100134{
135 if( len > MBEDTLS_SSL_CID_IN_LEN_MAX )
136 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
137
Hanno Becker611ac772019-05-14 11:45:26 +0100138 if( ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_FAIL &&
139 ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_IGNORE )
140 {
141 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
142 }
143
144 conf->ignore_unexpected_cid = ignore_other_cid;
Hanno Beckerad4a1372019-05-03 13:06:44 +0100145 conf->cid_len = len;
146 return( 0 );
147}
148
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100149int mbedtls_ssl_set_cid( mbedtls_ssl_context *ssl,
150 int enable,
151 unsigned char const *own_cid,
152 size_t own_cid_len )
153{
Hanno Becker76a79ab2019-05-03 14:38:32 +0100154 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
155 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
156
Hanno Beckerca092242019-04-25 16:01:49 +0100157 ssl->negotiate_cid = enable;
158 if( enable == MBEDTLS_SSL_CID_DISABLED )
159 {
160 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Disable use of CID extension." ) );
161 return( 0 );
162 }
163 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Enable use of CID extension." ) );
Hanno Beckerad4a1372019-05-03 13:06:44 +0100164 MBEDTLS_SSL_DEBUG_BUF( 3, "Own CID", own_cid, own_cid_len );
Hanno Beckerca092242019-04-25 16:01:49 +0100165
Hanno Beckerad4a1372019-05-03 13:06:44 +0100166 if( own_cid_len != ssl->conf->cid_len )
Hanno Beckerca092242019-04-25 16:01:49 +0100167 {
Hanno Beckerad4a1372019-05-03 13:06:44 +0100168 MBEDTLS_SSL_DEBUG_MSG( 3, ( "CID length %u does not match CID length %u in config",
169 (unsigned) own_cid_len,
170 (unsigned) ssl->conf->cid_len ) );
Hanno Beckerca092242019-04-25 16:01:49 +0100171 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
172 }
173
174 memcpy( ssl->own_cid, own_cid, own_cid_len );
Hanno Beckerb7ee0cf2019-04-30 14:07:31 +0100175 /* Truncation is not an issue here because
176 * MBEDTLS_SSL_CID_IN_LEN_MAX at most 255. */
177 ssl->own_cid_len = (uint8_t) own_cid_len;
Hanno Beckerca092242019-04-25 16:01:49 +0100178
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100179 return( 0 );
180}
181
182int mbedtls_ssl_get_peer_cid( mbedtls_ssl_context *ssl,
183 int *enabled,
184 unsigned char peer_cid[ MBEDTLS_SSL_CID_OUT_LEN_MAX ],
185 size_t *peer_cid_len )
186{
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100187 *enabled = MBEDTLS_SSL_CID_DISABLED;
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100188
Hanno Becker76a79ab2019-05-03 14:38:32 +0100189 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
190 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
191 {
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100192 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Becker76a79ab2019-05-03 14:38:32 +0100193 }
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100194
Hanno Beckerc5f24222019-05-03 12:54:52 +0100195 /* We report MBEDTLS_SSL_CID_DISABLED in case the CID extensions
196 * were used, but client and server requested the empty CID.
197 * This is indistinguishable from not using the CID extension
198 * in the first place. */
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100199 if( ssl->transform_in->in_cid_len == 0 &&
200 ssl->transform_in->out_cid_len == 0 )
201 {
202 return( 0 );
203 }
204
Hanno Becker615ef172019-05-22 16:50:35 +0100205 if( peer_cid_len != NULL )
206 {
207 *peer_cid_len = ssl->transform_in->out_cid_len;
208 if( peer_cid != NULL )
209 {
210 memcpy( peer_cid, ssl->transform_in->out_cid,
211 ssl->transform_in->out_cid_len );
212 }
213 }
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100214
215 *enabled = MBEDTLS_SSL_CID_ENABLED;
216
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100217 return( 0 );
218}
Hanno Beckera0e20d02019-05-15 14:03:01 +0100219#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100220
Hanno Beckerd5847772018-08-28 10:09:23 +0100221/* Forward declarations for functions related to message buffering. */
222static void ssl_buffering_free( mbedtls_ssl_context *ssl );
223static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
224 uint8_t slot );
225static void ssl_free_buffered_record( mbedtls_ssl_context *ssl );
226static int ssl_load_buffered_message( mbedtls_ssl_context *ssl );
227static int ssl_load_buffered_record( mbedtls_ssl_context *ssl );
228static int ssl_buffer_message( mbedtls_ssl_context *ssl );
229static int ssl_buffer_future_record( mbedtls_ssl_context *ssl );
Hanno Beckeref7afdf2018-08-28 17:16:31 +0100230static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl );
Hanno Beckerd5847772018-08-28 10:09:23 +0100231
Hanno Beckera67dee22018-08-22 10:05:20 +0100232static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl );
Hanno Becker11682cc2018-08-22 14:41:02 +0100233static size_t ssl_get_maximum_datagram_size( mbedtls_ssl_context const *ssl )
Hanno Becker2b1e3542018-08-06 11:19:13 +0100234{
Hanno Becker11682cc2018-08-22 14:41:02 +0100235 size_t mtu = ssl_get_current_mtu( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +0100236
237 if( mtu != 0 && mtu < MBEDTLS_SSL_OUT_BUFFER_LEN )
Hanno Becker11682cc2018-08-22 14:41:02 +0100238 return( mtu );
Hanno Becker2b1e3542018-08-06 11:19:13 +0100239
240 return( MBEDTLS_SSL_OUT_BUFFER_LEN );
241}
242
Hanno Becker67bc7c32018-08-06 11:33:50 +0100243static int ssl_get_remaining_space_in_datagram( mbedtls_ssl_context const *ssl )
244{
Hanno Becker11682cc2018-08-22 14:41:02 +0100245 size_t const bytes_written = ssl->out_left;
246 size_t const mtu = ssl_get_maximum_datagram_size( ssl );
Hanno Becker67bc7c32018-08-06 11:33:50 +0100247
248 /* Double-check that the write-index hasn't gone
249 * past what we can transmit in a single datagram. */
Hanno Becker11682cc2018-08-22 14:41:02 +0100250 if( bytes_written > mtu )
Hanno Becker67bc7c32018-08-06 11:33:50 +0100251 {
252 /* Should never happen... */
253 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
254 }
255
256 return( (int) ( mtu - bytes_written ) );
257}
258
259static int ssl_get_remaining_payload_in_datagram( mbedtls_ssl_context const *ssl )
260{
261 int ret;
262 size_t remaining, expansion;
Andrzej Kurek748face2018-10-11 07:20:19 -0400263 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
Hanno Becker67bc7c32018-08-06 11:33:50 +0100264
265#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
266 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
267
268 if( max_len > mfl )
269 max_len = mfl;
Hanno Beckerf4b010e2018-08-24 10:47:29 +0100270
271 /* By the standard (RFC 6066 Sect. 4), the MFL extension
272 * only limits the maximum record payload size, so in theory
273 * we would be allowed to pack multiple records of payload size
274 * MFL into a single datagram. However, this would mean that there's
275 * no way to explicitly communicate MTU restrictions to the peer.
276 *
277 * The following reduction of max_len makes sure that we never
278 * write datagrams larger than MFL + Record Expansion Overhead.
279 */
280 if( max_len <= ssl->out_left )
281 return( 0 );
282
283 max_len -= ssl->out_left;
Hanno Becker67bc7c32018-08-06 11:33:50 +0100284#endif
285
286 ret = ssl_get_remaining_space_in_datagram( ssl );
287 if( ret < 0 )
288 return( ret );
289 remaining = (size_t) ret;
290
291 ret = mbedtls_ssl_get_record_expansion( ssl );
292 if( ret < 0 )
293 return( ret );
294 expansion = (size_t) ret;
295
296 if( remaining <= expansion )
297 return( 0 );
298
299 remaining -= expansion;
300 if( remaining >= max_len )
301 remaining = max_len;
302
303 return( (int) remaining );
304}
305
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200306/*
307 * Double the retransmit timeout value, within the allowed range,
308 * returning -1 if the maximum value has already been reached.
309 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200310static int ssl_double_retransmit_timeout( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200311{
312 uint32_t new_timeout;
313
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200314 if( ssl->handshake->retransmit_timeout >= ssl->conf->hs_timeout_max )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200315 return( -1 );
316
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200317 /* Implement the final paragraph of RFC 6347 section 4.1.1.1
318 * in the following way: after the initial transmission and a first
319 * retransmission, back off to a temporary estimated MTU of 508 bytes.
320 * This value is guaranteed to be deliverable (if not guaranteed to be
321 * delivered) of any compliant IPv4 (and IPv6) network, and should work
322 * on most non-IP stacks too. */
323 if( ssl->handshake->retransmit_timeout != ssl->conf->hs_timeout_min )
Andrzej Kurek6290dae2018-10-05 08:06:01 -0400324 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200325 ssl->handshake->mtu = 508;
Andrzej Kurek6290dae2018-10-05 08:06:01 -0400326 MBEDTLS_SSL_DEBUG_MSG( 2, ( "mtu autoreduction to %d bytes", ssl->handshake->mtu ) );
327 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200328
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200329 new_timeout = 2 * ssl->handshake->retransmit_timeout;
330
331 /* Avoid arithmetic overflow and range overflow */
332 if( new_timeout < ssl->handshake->retransmit_timeout ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200333 new_timeout > ssl->conf->hs_timeout_max )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200334 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200335 new_timeout = ssl->conf->hs_timeout_max;
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200336 }
337
338 ssl->handshake->retransmit_timeout = new_timeout;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200339 MBEDTLS_SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs",
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200340 ssl->handshake->retransmit_timeout ) );
341
342 return( 0 );
343}
344
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200345static void ssl_reset_retransmit_timeout( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200346{
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200347 ssl->handshake->retransmit_timeout = ssl->conf->hs_timeout_min;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200348 MBEDTLS_SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs",
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200349 ssl->handshake->retransmit_timeout ) );
350}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200351#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200352
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200353#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200354/*
355 * Convert max_fragment_length codes to length.
356 * RFC 6066 says:
357 * enum{
358 * 2^9(1), 2^10(2), 2^11(3), 2^12(4), (255)
359 * } MaxFragmentLength;
360 * and we add 0 -> extension unused
361 */
Angus Grattond8213d02016-05-25 20:56:48 +1000362static unsigned int ssl_mfl_code_to_length( int mfl )
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200363{
Angus Grattond8213d02016-05-25 20:56:48 +1000364 switch( mfl )
365 {
366 case MBEDTLS_SSL_MAX_FRAG_LEN_NONE:
367 return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
368 case MBEDTLS_SSL_MAX_FRAG_LEN_512:
369 return 512;
370 case MBEDTLS_SSL_MAX_FRAG_LEN_1024:
371 return 1024;
372 case MBEDTLS_SSL_MAX_FRAG_LEN_2048:
373 return 2048;
374 case MBEDTLS_SSL_MAX_FRAG_LEN_4096:
375 return 4096;
376 default:
377 return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
378 }
379}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200380#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200381
Hanno Becker52055ae2019-02-06 14:30:46 +0000382int mbedtls_ssl_session_copy( mbedtls_ssl_session *dst,
383 const mbedtls_ssl_session *src )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200384{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200385 mbedtls_ssl_session_free( dst );
386 memcpy( dst, src, sizeof( mbedtls_ssl_session ) );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200387
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200388#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker6d1986e2019-02-07 12:27:42 +0000389
390#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200391 if( src->peer_cert != NULL )
392 {
Paul Bakker2292d1f2013-09-15 17:06:49 +0200393 int ret;
394
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200395 dst->peer_cert = mbedtls_calloc( 1, sizeof(mbedtls_x509_crt) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200396 if( dst->peer_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200397 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200398
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200399 mbedtls_x509_crt_init( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200400
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200401 if( ( ret = mbedtls_x509_crt_parse_der( dst->peer_cert, src->peer_cert->raw.p,
Manuel Pégourié-Gonnard4d2a8eb2014-06-13 20:33:27 +0200402 src->peer_cert->raw.len ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200403 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200404 mbedtls_free( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200405 dst->peer_cert = NULL;
406 return( ret );
407 }
408 }
Hanno Becker6d1986e2019-02-07 12:27:42 +0000409#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker9198ad12019-02-05 17:00:50 +0000410 if( src->peer_cert_digest != NULL )
411 {
Hanno Becker9198ad12019-02-05 17:00:50 +0000412 dst->peer_cert_digest =
Hanno Beckeraccc5992019-02-25 10:06:59 +0000413 mbedtls_calloc( 1, src->peer_cert_digest_len );
Hanno Becker9198ad12019-02-05 17:00:50 +0000414 if( dst->peer_cert_digest == NULL )
415 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
416
417 memcpy( dst->peer_cert_digest, src->peer_cert_digest,
418 src->peer_cert_digest_len );
419 dst->peer_cert_digest_type = src->peer_cert_digest_type;
Hanno Beckeraccc5992019-02-25 10:06:59 +0000420 dst->peer_cert_digest_len = src->peer_cert_digest_len;
Hanno Becker9198ad12019-02-05 17:00:50 +0000421 }
422#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
423
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200424#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200425
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200426#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200427 if( src->ticket != NULL )
428 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200429 dst->ticket = mbedtls_calloc( 1, src->ticket_len );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200430 if( dst->ticket == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200431 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200432
433 memcpy( dst->ticket, src->ticket, src->ticket_len );
434 }
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200435#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200436
437 return( 0 );
438}
439
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200440#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
441int (*mbedtls_ssl_hw_record_init)( mbedtls_ssl_context *ssl,
Paul Bakker9af723c2014-05-01 13:03:14 +0200442 const unsigned char *key_enc, const unsigned char *key_dec,
443 size_t keylen,
444 const unsigned char *iv_enc, const unsigned char *iv_dec,
445 size_t ivlen,
446 const unsigned char *mac_enc, const unsigned char *mac_dec,
Paul Bakker66d5d072014-06-17 16:39:18 +0200447 size_t maclen ) = NULL;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200448int (*mbedtls_ssl_hw_record_activate)( mbedtls_ssl_context *ssl, int direction) = NULL;
449int (*mbedtls_ssl_hw_record_reset)( mbedtls_ssl_context *ssl ) = NULL;
450int (*mbedtls_ssl_hw_record_write)( mbedtls_ssl_context *ssl ) = NULL;
451int (*mbedtls_ssl_hw_record_read)( mbedtls_ssl_context *ssl ) = NULL;
452int (*mbedtls_ssl_hw_record_finish)( mbedtls_ssl_context *ssl ) = NULL;
453#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +0000454
Paul Bakker5121ce52009-01-03 21:22:43 +0000455/*
456 * Key material generation
457 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200458#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200459static int ssl3_prf( const unsigned char *secret, size_t slen,
460 const char *label,
461 const unsigned char *random, size_t rlen,
Paul Bakker5f70b252012-09-13 14:23:06 +0000462 unsigned char *dstbuf, size_t dlen )
463{
Andres Amaya Garcia33952502017-07-20 16:29:16 +0100464 int ret = 0;
Paul Bakker5f70b252012-09-13 14:23:06 +0000465 size_t i;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200466 mbedtls_md5_context md5;
467 mbedtls_sha1_context sha1;
Paul Bakker5f70b252012-09-13 14:23:06 +0000468 unsigned char padding[16];
469 unsigned char sha1sum[20];
470 ((void)label);
471
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200472 mbedtls_md5_init( &md5 );
473 mbedtls_sha1_init( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +0200474
Paul Bakker5f70b252012-09-13 14:23:06 +0000475 /*
476 * SSLv3:
477 * block =
478 * MD5( secret + SHA1( 'A' + secret + random ) ) +
479 * MD5( secret + SHA1( 'BB' + secret + random ) ) +
480 * MD5( secret + SHA1( 'CCC' + secret + random ) ) +
481 * ...
482 */
483 for( i = 0; i < dlen / 16; i++ )
484 {
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200485 memset( padding, (unsigned char) ('A' + i), 1 + i );
Paul Bakker5f70b252012-09-13 14:23:06 +0000486
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100487 if( ( ret = mbedtls_sha1_starts_ret( &sha1 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100488 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100489 if( ( ret = mbedtls_sha1_update_ret( &sha1, padding, 1 + i ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100490 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100491 if( ( ret = mbedtls_sha1_update_ret( &sha1, secret, slen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100492 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100493 if( ( ret = mbedtls_sha1_update_ret( &sha1, random, rlen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100494 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100495 if( ( ret = mbedtls_sha1_finish_ret( &sha1, sha1sum ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100496 goto exit;
Paul Bakker5f70b252012-09-13 14:23:06 +0000497
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100498 if( ( ret = mbedtls_md5_starts_ret( &md5 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100499 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100500 if( ( ret = mbedtls_md5_update_ret( &md5, secret, slen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100501 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100502 if( ( ret = mbedtls_md5_update_ret( &md5, sha1sum, 20 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100503 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100504 if( ( ret = mbedtls_md5_finish_ret( &md5, dstbuf + i * 16 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100505 goto exit;
Paul Bakker5f70b252012-09-13 14:23:06 +0000506 }
507
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100508exit:
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200509 mbedtls_md5_free( &md5 );
510 mbedtls_sha1_free( &sha1 );
Paul Bakker5f70b252012-09-13 14:23:06 +0000511
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500512 mbedtls_platform_zeroize( padding, sizeof( padding ) );
513 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5f70b252012-09-13 14:23:06 +0000514
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100515 return( ret );
Paul Bakker5f70b252012-09-13 14:23:06 +0000516}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200517#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +0000518
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200519#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200520static int tls1_prf( const unsigned char *secret, size_t slen,
521 const char *label,
522 const unsigned char *random, size_t rlen,
Paul Bakker23986e52011-04-24 08:57:21 +0000523 unsigned char *dstbuf, size_t dlen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000524{
Paul Bakker23986e52011-04-24 08:57:21 +0000525 size_t nb, hs;
526 size_t i, j, k;
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200527 const unsigned char *S1, *S2;
Ron Eldor3b350852019-05-07 18:31:49 +0300528 unsigned char *tmp;
529 size_t tmp_len = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000530 unsigned char h_i[20];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200531 const mbedtls_md_info_t *md_info;
532 mbedtls_md_context_t md_ctx;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100533 int ret;
534
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200535 mbedtls_md_init( &md_ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000536
Ron Eldor3b350852019-05-07 18:31:49 +0300537 tmp_len = 20 + strlen( label ) + rlen;
538 tmp = mbedtls_calloc( 1, tmp_len );
539 if( tmp == NULL )
540 {
541 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
542 goto exit;
543 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000544
545 hs = ( slen + 1 ) / 2;
546 S1 = secret;
547 S2 = secret + slen - hs;
548
549 nb = strlen( label );
550 memcpy( tmp + 20, label, nb );
551 memcpy( tmp + 20 + nb, random, rlen );
552 nb += rlen;
553
554 /*
555 * First compute P_md5(secret,label+random)[0..dlen]
556 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200557 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_MD5 ) ) == NULL )
Ron Eldor3b350852019-05-07 18:31:49 +0300558 {
559 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
560 goto exit;
561 }
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100562
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200563 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Ron Eldor3b350852019-05-07 18:31:49 +0300564 {
565 goto exit;
566 }
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100567
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200568 mbedtls_md_hmac_starts( &md_ctx, S1, hs );
569 mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb );
570 mbedtls_md_hmac_finish( &md_ctx, 4 + tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000571
572 for( i = 0; i < dlen; i += 16 )
573 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200574 mbedtls_md_hmac_reset ( &md_ctx );
575 mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 + nb );
576 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100577
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200578 mbedtls_md_hmac_reset ( &md_ctx );
579 mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 );
580 mbedtls_md_hmac_finish( &md_ctx, 4 + tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000581
582 k = ( i + 16 > dlen ) ? dlen % 16 : 16;
583
584 for( j = 0; j < k; j++ )
585 dstbuf[i + j] = h_i[j];
586 }
587
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200588 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100589
Paul Bakker5121ce52009-01-03 21:22:43 +0000590 /*
591 * XOR out with P_sha1(secret,label+random)[0..dlen]
592 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200593 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_SHA1 ) ) == NULL )
Ron Eldor3b350852019-05-07 18:31:49 +0300594 {
595 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
596 goto exit;
597 }
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100598
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200599 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Ron Eldor3b350852019-05-07 18:31:49 +0300600 {
601 goto exit;
602 }
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100603
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200604 mbedtls_md_hmac_starts( &md_ctx, S2, hs );
605 mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb );
606 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000607
608 for( i = 0; i < dlen; i += 20 )
609 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200610 mbedtls_md_hmac_reset ( &md_ctx );
611 mbedtls_md_hmac_update( &md_ctx, tmp, 20 + nb );
612 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100613
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200614 mbedtls_md_hmac_reset ( &md_ctx );
615 mbedtls_md_hmac_update( &md_ctx, tmp, 20 );
616 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000617
618 k = ( i + 20 > dlen ) ? dlen % 20 : 20;
619
620 for( j = 0; j < k; j++ )
621 dstbuf[i + j] = (unsigned char)( dstbuf[i + j] ^ h_i[j] );
622 }
623
Ron Eldor3b350852019-05-07 18:31:49 +0300624exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200625 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100626
Ron Eldor3b350852019-05-07 18:31:49 +0300627 mbedtls_platform_zeroize( tmp, tmp_len );
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500628 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000629
Ron Eldor3b350852019-05-07 18:31:49 +0300630 mbedtls_free( tmp );
631 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000632}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200633#endif /* MBEDTLS_SSL_PROTO_TLS1) || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000634
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200635#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurekc929a822019-01-14 03:51:11 -0500636#if defined(MBEDTLS_USE_PSA_CRYPTO)
637static int tls_prf_generic( mbedtls_md_type_t md_type,
638 const unsigned char *secret, size_t slen,
639 const char *label,
640 const unsigned char *random, size_t rlen,
641 unsigned char *dstbuf, size_t dlen )
642{
643 psa_status_t status;
644 psa_algorithm_t alg;
Janos Follath53b8ec22019-08-08 10:28:27 +0100645 psa_key_attributes_t key_attributes;
Andrzej Kurekac5dc342019-01-23 06:57:34 -0500646 psa_key_handle_t master_slot;
Janos Follathda6ac012019-08-16 13:47:29 +0100647 psa_key_derivation_operation_t derivation =
Janos Follath8dee8772019-07-30 12:53:32 +0100648 PSA_KEY_DERIVATION_OPERATION_INIT;
Andrzej Kurekc929a822019-01-14 03:51:11 -0500649
Andrzej Kurekc929a822019-01-14 03:51:11 -0500650 if( md_type == MBEDTLS_MD_SHA384 )
651 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_384);
652 else
653 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_256);
654
Janos Follath53b8ec22019-08-08 10:28:27 +0100655 key_attributes = psa_key_attributes_init();
656 psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_DERIVE );
657 psa_set_key_algorithm( &key_attributes, alg );
658 psa_set_key_type( &key_attributes, PSA_KEY_TYPE_DERIVE );
Andrzej Kurekc929a822019-01-14 03:51:11 -0500659
Janos Follath53b8ec22019-08-08 10:28:27 +0100660 status = psa_import_key( &key_attributes, secret, slen, &master_slot );
Andrzej Kurekc929a822019-01-14 03:51:11 -0500661 if( status != PSA_SUCCESS )
662 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
663
Janos Follathda6ac012019-08-16 13:47:29 +0100664 status = psa_key_derivation( &derivation,
Andrzej Kurekc929a822019-01-14 03:51:11 -0500665 master_slot, alg,
666 random, rlen,
667 (unsigned char const *) label,
668 (size_t) strlen( label ),
669 dlen );
670 if( status != PSA_SUCCESS )
671 {
Janos Follathda6ac012019-08-16 13:47:29 +0100672 psa_key_derivation_abort( &derivation );
Andrzej Kurekc929a822019-01-14 03:51:11 -0500673 psa_destroy_key( master_slot );
674 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
675 }
676
Janos Follathda6ac012019-08-16 13:47:29 +0100677 status = psa_key_derivation_output_bytes( &derivation, dstbuf, dlen );
Andrzej Kurekc929a822019-01-14 03:51:11 -0500678 if( status != PSA_SUCCESS )
679 {
Janos Follathda6ac012019-08-16 13:47:29 +0100680 psa_key_derivation_abort( &derivation );
Andrzej Kurekc929a822019-01-14 03:51:11 -0500681 psa_destroy_key( master_slot );
682 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
683 }
684
Janos Follathda6ac012019-08-16 13:47:29 +0100685 status = psa_key_derivation_abort( &derivation );
Andrzej Kurekc929a822019-01-14 03:51:11 -0500686 if( status != PSA_SUCCESS )
Andrzej Kurek70737ca2019-01-14 05:37:13 -0500687 {
688 psa_destroy_key( master_slot );
Andrzej Kurekc929a822019-01-14 03:51:11 -0500689 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Andrzej Kurek70737ca2019-01-14 05:37:13 -0500690 }
Andrzej Kurekc929a822019-01-14 03:51:11 -0500691
692 status = psa_destroy_key( master_slot );
693 if( status != PSA_SUCCESS )
694 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
695
Andrzej Kurek33171262019-01-15 03:25:18 -0500696 return( 0 );
Andrzej Kurekc929a822019-01-14 03:51:11 -0500697}
698
699#else /* MBEDTLS_USE_PSA_CRYPTO */
700
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200701static int tls_prf_generic( mbedtls_md_type_t md_type,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100702 const unsigned char *secret, size_t slen,
703 const char *label,
704 const unsigned char *random, size_t rlen,
705 unsigned char *dstbuf, size_t dlen )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000706{
707 size_t nb;
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100708 size_t i, j, k, md_len;
Ron Eldor3b350852019-05-07 18:31:49 +0300709 unsigned char *tmp;
710 size_t tmp_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200711 unsigned char h_i[MBEDTLS_MD_MAX_SIZE];
712 const mbedtls_md_info_t *md_info;
713 mbedtls_md_context_t md_ctx;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100714 int ret;
715
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200716 mbedtls_md_init( &md_ctx );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000717
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200718 if( ( md_info = mbedtls_md_info_from_type( md_type ) ) == NULL )
719 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100720
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200721 md_len = mbedtls_md_get_size( md_info );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100722
Ron Eldor3b350852019-05-07 18:31:49 +0300723 tmp_len = md_len + strlen( label ) + rlen;
724 tmp = mbedtls_calloc( 1, tmp_len );
725 if( tmp == NULL )
726 {
727 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
728 goto exit;
729 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000730
731 nb = strlen( label );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100732 memcpy( tmp + md_len, label, nb );
733 memcpy( tmp + md_len + nb, random, rlen );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000734 nb += rlen;
735
736 /*
737 * Compute P_<hash>(secret, label + random)[0..dlen]
738 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200739 if ( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Ron Eldor3b350852019-05-07 18:31:49 +0300740 goto exit;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100741
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200742 mbedtls_md_hmac_starts( &md_ctx, secret, slen );
743 mbedtls_md_hmac_update( &md_ctx, tmp + md_len, nb );
744 mbedtls_md_hmac_finish( &md_ctx, tmp );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100745
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100746 for( i = 0; i < dlen; i += md_len )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000747 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200748 mbedtls_md_hmac_reset ( &md_ctx );
749 mbedtls_md_hmac_update( &md_ctx, tmp, md_len + nb );
750 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100751
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200752 mbedtls_md_hmac_reset ( &md_ctx );
753 mbedtls_md_hmac_update( &md_ctx, tmp, md_len );
754 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000755
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100756 k = ( i + md_len > dlen ) ? dlen % md_len : md_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +0000757
758 for( j = 0; j < k; j++ )
759 dstbuf[i + j] = h_i[j];
760 }
761
Ron Eldor3b350852019-05-07 18:31:49 +0300762exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200763 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100764
Ron Eldor3b350852019-05-07 18:31:49 +0300765 mbedtls_platform_zeroize( tmp, tmp_len );
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500766 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000767
Ron Eldor3b350852019-05-07 18:31:49 +0300768 mbedtls_free( tmp );
769
770 return( ret );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000771}
Andrzej Kurekc929a822019-01-14 03:51:11 -0500772#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200773#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100774static int tls_prf_sha256( const unsigned char *secret, size_t slen,
775 const char *label,
776 const unsigned char *random, size_t rlen,
777 unsigned char *dstbuf, size_t dlen )
778{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200779 return( tls_prf_generic( MBEDTLS_MD_SHA256, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100780 label, random, rlen, dstbuf, dlen ) );
781}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200782#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000783
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200784#if defined(MBEDTLS_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200785static int tls_prf_sha384( const unsigned char *secret, size_t slen,
786 const char *label,
787 const unsigned char *random, size_t rlen,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000788 unsigned char *dstbuf, size_t dlen )
789{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200790 return( tls_prf_generic( MBEDTLS_MD_SHA384, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100791 label, random, rlen, dstbuf, dlen ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000792}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200793#endif /* MBEDTLS_SHA512_C */
794#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +0000795
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200796static void ssl_update_checksum_start( mbedtls_ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200797
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200798#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
799 defined(MBEDTLS_SSL_PROTO_TLS1_1)
800static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200801#endif
Paul Bakker380da532012-04-18 16:10:25 +0000802
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200803#if defined(MBEDTLS_SSL_PROTO_SSL3)
804static void ssl_calc_verify_ssl( mbedtls_ssl_context *, unsigned char * );
805static void ssl_calc_finished_ssl( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200806#endif
807
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200808#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
809static void ssl_calc_verify_tls( mbedtls_ssl_context *, unsigned char * );
810static void ssl_calc_finished_tls( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200811#endif
812
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200813#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
814#if defined(MBEDTLS_SHA256_C)
815static void ssl_update_checksum_sha256( mbedtls_ssl_context *, const unsigned char *, size_t );
816static void ssl_calc_verify_tls_sha256( mbedtls_ssl_context *,unsigned char * );
817static void ssl_calc_finished_tls_sha256( mbedtls_ssl_context *,unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200818#endif
Paul Bakker769075d2012-11-24 11:26:46 +0100819
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200820#if defined(MBEDTLS_SHA512_C)
821static void ssl_update_checksum_sha384( mbedtls_ssl_context *, const unsigned char *, size_t );
822static void ssl_calc_verify_tls_sha384( mbedtls_ssl_context *, unsigned char * );
823static void ssl_calc_finished_tls_sha384( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakker769075d2012-11-24 11:26:46 +0100824#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200825#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000826
Manuel Pégourié-Gonnard45be3d82019-02-18 23:35:14 +0100827#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED) && \
Hanno Becker7d0a5692018-10-23 15:26:22 +0100828 defined(MBEDTLS_USE_PSA_CRYPTO)
829static int ssl_use_opaque_psk( mbedtls_ssl_context const *ssl )
830{
831 if( ssl->conf->f_psk != NULL )
832 {
833 /* If we've used a callback to select the PSK,
834 * the static configuration is irrelevant. */
835 if( ssl->handshake->psk_opaque != 0 )
836 return( 1 );
837
838 return( 0 );
839 }
840
841 if( ssl->conf->psk_opaque != 0 )
842 return( 1 );
843
844 return( 0 );
845}
846#endif /* MBEDTLS_USE_PSA_CRYPTO &&
Manuel Pégourié-Gonnard45be3d82019-02-18 23:35:14 +0100847 MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
Hanno Becker7d0a5692018-10-23 15:26:22 +0100848
Ron Eldorcf280092019-05-14 20:19:13 +0300849#if defined(MBEDTLS_SSL_EXPORT_KEYS)
850static mbedtls_tls_prf_types tls_prf_get_type( mbedtls_ssl_tls_prf_cb *tls_prf )
851{
852#if defined(MBEDTLS_SSL_PROTO_SSL3)
853 if( tls_prf == ssl3_prf )
854 {
Ron Eldor0810f0b2019-05-15 12:32:32 +0300855 return( MBEDTLS_SSL_TLS_PRF_SSL3 );
Ron Eldorcf280092019-05-14 20:19:13 +0300856 }
857 else
858#endif
859#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
860 if( tls_prf == tls1_prf )
861 {
862 return( MBEDTLS_SSL_TLS_PRF_TLS1 );
863 }
864 else
865#endif
866#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
867#if defined(MBEDTLS_SHA512_C)
868 if( tls_prf == tls_prf_sha384 )
869 {
870 return( MBEDTLS_SSL_TLS_PRF_SHA384 );
871 }
872 else
873#endif
874#if defined(MBEDTLS_SHA256_C)
875 if( tls_prf == tls_prf_sha256 )
876 {
877 return( MBEDTLS_SSL_TLS_PRF_SHA256 );
878 }
879 else
880#endif
881#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
882 return( MBEDTLS_SSL_TLS_PRF_NONE );
883}
884#endif /* MBEDTLS_SSL_EXPORT_KEYS */
885
Ron Eldor51d3ab52019-05-12 14:54:30 +0300886int mbedtls_ssl_tls_prf( const mbedtls_tls_prf_types prf,
887 const unsigned char *secret, size_t slen,
888 const char *label,
889 const unsigned char *random, size_t rlen,
890 unsigned char *dstbuf, size_t dlen )
891{
892 mbedtls_ssl_tls_prf_cb *tls_prf = NULL;
893
894 switch( prf )
895 {
896#if defined(MBEDTLS_SSL_PROTO_SSL3)
897 case MBEDTLS_SSL_TLS_PRF_SSL3:
898 tls_prf = ssl3_prf;
899 break;
Ron Eldord2f25f72019-05-15 14:54:22 +0300900#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Ron Eldor51d3ab52019-05-12 14:54:30 +0300901#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
902 case MBEDTLS_SSL_TLS_PRF_TLS1:
903 tls_prf = tls1_prf;
904 break;
Ron Eldord2f25f72019-05-15 14:54:22 +0300905#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
906
907#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Ron Eldor51d3ab52019-05-12 14:54:30 +0300908#if defined(MBEDTLS_SHA512_C)
909 case MBEDTLS_SSL_TLS_PRF_SHA384:
910 tls_prf = tls_prf_sha384;
911 break;
Ron Eldord2f25f72019-05-15 14:54:22 +0300912#endif /* MBEDTLS_SHA512_C */
Ron Eldor51d3ab52019-05-12 14:54:30 +0300913#if defined(MBEDTLS_SHA256_C)
914 case MBEDTLS_SSL_TLS_PRF_SHA256:
915 tls_prf = tls_prf_sha256;
916 break;
Ron Eldord2f25f72019-05-15 14:54:22 +0300917#endif /* MBEDTLS_SHA256_C */
918#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Ron Eldor51d3ab52019-05-12 14:54:30 +0300919 default:
920 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
921 }
922
923 return( tls_prf( secret, slen, label, random, rlen, dstbuf, dlen ) );
924}
925
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200926int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +0000927{
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200928 int ret = 0;
Hanno Beckercb1cc802018-11-17 22:27:38 +0000929#if defined(MBEDTLS_USE_PSA_CRYPTO)
930 int psa_fallthrough;
931#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker5121ce52009-01-03 21:22:43 +0000932 unsigned char tmp[64];
Paul Bakker5121ce52009-01-03 21:22:43 +0000933 unsigned char keyblk[256];
934 unsigned char *key1;
935 unsigned char *key2;
Paul Bakker68884e32013-01-07 18:20:04 +0100936 unsigned char *mac_enc;
937 unsigned char *mac_dec;
Hanno Becker81c7b182017-11-09 18:39:33 +0000938 size_t mac_key_len;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200939 size_t iv_copy_len;
Hanno Becker88aaf652017-12-27 08:17:40 +0000940 unsigned keylen;
Hanno Beckere694c3e2017-12-27 21:34:08 +0000941 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200942 const mbedtls_cipher_info_t *cipher_info;
943 const mbedtls_md_info_t *md_info;
Paul Bakker68884e32013-01-07 18:20:04 +0100944
Hanno Beckerf9ed7d52018-11-05 12:45:16 +0000945 /* cf. RFC 5246, Section 8.1:
946 * "The master secret is always exactly 48 bytes in length." */
947 size_t const master_secret_len = 48;
948
Hanno Becker35b23c72018-10-23 12:10:41 +0100949#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
950 unsigned char session_hash[48];
951#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
952
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200953 mbedtls_ssl_session *session = ssl->session_negotiate;
954 mbedtls_ssl_transform *transform = ssl->transform_negotiate;
955 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Paul Bakker5121ce52009-01-03 21:22:43 +0000956
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200957 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000958
Jaeden Amero2de07f12019-06-05 13:32:08 +0100959#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) && \
960 defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000961 transform->encrypt_then_mac = session->encrypt_then_mac;
962#endif
963 transform->minor_ver = ssl->minor_ver;
Hanno Beckere694c3e2017-12-27 21:34:08 +0000964
965 ciphersuite_info = handshake->ciphersuite_info;
966 cipher_info = mbedtls_cipher_info_from_type( ciphersuite_info->cipher );
Paul Bakker68884e32013-01-07 18:20:04 +0100967 if( cipher_info == NULL )
968 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200969 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %d not found",
Hanno Beckere694c3e2017-12-27 21:34:08 +0000970 ciphersuite_info->cipher ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200971 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100972 }
973
Hanno Beckere694c3e2017-12-27 21:34:08 +0000974 md_info = mbedtls_md_info_from_type( ciphersuite_info->mac );
Paul Bakker68884e32013-01-07 18:20:04 +0100975 if( md_info == NULL )
976 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200977 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %d not found",
Hanno Beckere694c3e2017-12-27 21:34:08 +0000978 ciphersuite_info->mac ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200979 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100980 }
981
Hanno Beckera0e20d02019-05-15 14:03:01 +0100982#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker4bf74652019-04-26 16:22:27 +0100983 /* Copy own and peer's CID if the use of the CID
984 * extension has been negotiated. */
985 if( ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_ENABLED )
986 {
987 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Copy CIDs into SSL transform" ) );
Hanno Becker8a7f9722019-04-30 13:52:29 +0100988
Hanno Becker05154c32019-05-03 15:23:51 +0100989 transform->in_cid_len = ssl->own_cid_len;
Hanno Becker05154c32019-05-03 15:23:51 +0100990 memcpy( transform->in_cid, ssl->own_cid, ssl->own_cid_len );
Hanno Becker1c1f0462019-05-03 12:55:51 +0100991 MBEDTLS_SSL_DEBUG_BUF( 3, "Incoming CID", transform->in_cid,
Hanno Becker4bf74652019-04-26 16:22:27 +0100992 transform->in_cid_len );
Hanno Beckerd1f20352019-05-15 10:21:55 +0100993
994 transform->out_cid_len = ssl->handshake->peer_cid_len;
995 memcpy( transform->out_cid, ssl->handshake->peer_cid,
996 ssl->handshake->peer_cid_len );
997 MBEDTLS_SSL_DEBUG_BUF( 3, "Outgoing CID", transform->out_cid,
998 transform->out_cid_len );
Hanno Becker4bf74652019-04-26 16:22:27 +0100999 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01001000#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker4bf74652019-04-26 16:22:27 +01001001
Paul Bakker5121ce52009-01-03 21:22:43 +00001002 /*
Paul Bakkerca4ab492012-04-18 14:23:57 +00001003 * Set appropriate PRF function and other SSL / TLS / TLS1.2 functions
Paul Bakker1ef83d62012-04-11 12:09:53 +00001004 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001005#if defined(MBEDTLS_SSL_PROTO_SSL3)
1006 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001007 {
Paul Bakker48916f92012-09-16 19:57:18 +00001008 handshake->tls_prf = ssl3_prf;
1009 handshake->calc_verify = ssl_calc_verify_ssl;
1010 handshake->calc_finished = ssl_calc_finished_ssl;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001011 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001012 else
1013#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001014#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
1015 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001016 {
Paul Bakker48916f92012-09-16 19:57:18 +00001017 handshake->tls_prf = tls1_prf;
1018 handshake->calc_verify = ssl_calc_verify_tls;
1019 handshake->calc_finished = ssl_calc_finished_tls;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001020 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001021 else
1022#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001023#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1024#if defined(MBEDTLS_SHA512_C)
1025 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
Hanno Beckere694c3e2017-12-27 21:34:08 +00001026 ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001027 {
Paul Bakker48916f92012-09-16 19:57:18 +00001028 handshake->tls_prf = tls_prf_sha384;
1029 handshake->calc_verify = ssl_calc_verify_tls_sha384;
1030 handshake->calc_finished = ssl_calc_finished_tls_sha384;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001031 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00001032 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001033#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001034#if defined(MBEDTLS_SHA256_C)
1035 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001036 {
Paul Bakker48916f92012-09-16 19:57:18 +00001037 handshake->tls_prf = tls_prf_sha256;
1038 handshake->calc_verify = ssl_calc_verify_tls_sha256;
1039 handshake->calc_finished = ssl_calc_finished_tls_sha256;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001040 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001041 else
1042#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001043#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02001044 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001045 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1046 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02001047 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00001048
1049 /*
Paul Bakker5121ce52009-01-03 21:22:43 +00001050 * SSLv3:
1051 * master =
1052 * MD5( premaster + SHA1( 'A' + premaster + randbytes ) ) +
1053 * MD5( premaster + SHA1( 'BB' + premaster + randbytes ) ) +
1054 * MD5( premaster + SHA1( 'CCC' + premaster + randbytes ) )
Paul Bakkerf7abd422013-04-16 13:15:56 +02001055 *
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001056 * TLSv1+:
Paul Bakker5121ce52009-01-03 21:22:43 +00001057 * master = PRF( premaster, "master secret", randbytes )[0..47]
1058 */
Hanno Becker35b23c72018-10-23 12:10:41 +01001059 if( handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001060 {
Hanno Becker35b23c72018-10-23 12:10:41 +01001061 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
1062 }
1063 else
1064 {
1065 /* The label for the KDF used for key expansion.
1066 * This is either "master secret" or "extended master secret"
1067 * depending on whether the Extended Master Secret extension
1068 * is used. */
1069 char const *lbl = "master secret";
1070
1071 /* The salt for the KDF used for key expansion.
1072 * - If the Extended Master Secret extension is not used,
1073 * this is ClientHello.Random + ServerHello.Random
1074 * (see Sect. 8.1 in RFC 5246).
1075 * - If the Extended Master Secret extension is used,
1076 * this is the transcript of the handshake so far.
1077 * (see Sect. 4 in RFC 7627). */
1078 unsigned char const *salt = handshake->randbytes;
1079 size_t salt_len = 64;
1080
1081#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001082 if( ssl->handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001083 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001084 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using extended master secret" ) );
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001085
Hanno Becker35b23c72018-10-23 12:10:41 +01001086 lbl = "extended master secret";
1087 salt = session_hash;
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001088 ssl->handshake->calc_verify( ssl, session_hash );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001089#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1090 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001091 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001092#if defined(MBEDTLS_SHA512_C)
Hanno Beckere694c3e2017-12-27 21:34:08 +00001093 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
1094 {
Hanno Becker35b23c72018-10-23 12:10:41 +01001095 salt_len = 48;
Hanno Beckere694c3e2017-12-27 21:34:08 +00001096 }
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001097 else
Hanno Becker35b23c72018-10-23 12:10:41 +01001098#endif /* MBEDTLS_SHA512_C */
1099 salt_len = 32;
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001100 }
1101 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001102#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker35b23c72018-10-23 12:10:41 +01001103 salt_len = 36;
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001104
Hanno Becker35b23c72018-10-23 12:10:41 +01001105 MBEDTLS_SSL_DEBUG_BUF( 3, "session hash", session_hash, salt_len );
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001106 }
Hanno Becker35b23c72018-10-23 12:10:41 +01001107#endif /* MBEDTLS_SSL_EXTENDED_MS_ENABLED */
1108
Hanno Becker7d0a5692018-10-23 15:26:22 +01001109#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
1110 defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
1111 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK &&
1112 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
1113 ssl_use_opaque_psk( ssl ) == 1 )
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +01001114 {
Hanno Becker7d0a5692018-10-23 15:26:22 +01001115 /* Perform PSK-to-MS expansion in a single step. */
1116 psa_status_t status;
1117 psa_algorithm_t alg;
Andrzej Kurek2349c4d2019-01-08 09:36:01 -05001118 psa_key_handle_t psk;
Janos Follathda6ac012019-08-16 13:47:29 +01001119 psa_key_derivation_operation_t derivation =
Janos Follath8dee8772019-07-30 12:53:32 +01001120 PSA_KEY_DERIVATION_OPERATION_INIT;
Hanno Becker7d0a5692018-10-23 15:26:22 +01001121
1122 MBEDTLS_SSL_DEBUG_MSG( 2, ( "perform PSA-based PSK-to-MS expansion" ) );
1123
1124 psk = ssl->conf->psk_opaque;
1125 if( ssl->handshake->psk_opaque != 0 )
1126 psk = ssl->handshake->psk_opaque;
1127
Hanno Becker22bf1452019-04-05 11:21:08 +01001128 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Hanno Becker7d0a5692018-10-23 15:26:22 +01001129 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384);
1130 else
1131 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256);
1132
Janos Follathda6ac012019-08-16 13:47:29 +01001133 status = psa_key_derivation( &derivation, psk, alg,
Hanno Becker7d0a5692018-10-23 15:26:22 +01001134 salt, salt_len,
1135 (unsigned char const *) lbl,
1136 (size_t) strlen( lbl ),
Hanno Beckerf9ed7d52018-11-05 12:45:16 +00001137 master_secret_len );
Hanno Becker7d0a5692018-10-23 15:26:22 +01001138 if( status != PSA_SUCCESS )
1139 {
Janos Follathda6ac012019-08-16 13:47:29 +01001140 psa_key_derivation_abort( &derivation );
Hanno Becker7d0a5692018-10-23 15:26:22 +01001141 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
1142 }
1143
Janos Follathda6ac012019-08-16 13:47:29 +01001144 status = psa_key_derivation_output_bytes( &derivation,
Janos Follath6de99db2019-07-30 12:55:06 +01001145 session->master,
1146 master_secret_len );
Hanno Becker7d0a5692018-10-23 15:26:22 +01001147 if( status != PSA_SUCCESS )
1148 {
Janos Follathda6ac012019-08-16 13:47:29 +01001149 psa_key_derivation_abort( &derivation );
Hanno Becker7d0a5692018-10-23 15:26:22 +01001150 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
1151 }
1152
Janos Follathda6ac012019-08-16 13:47:29 +01001153 status = psa_key_derivation_abort( &derivation );
Hanno Becker7d0a5692018-10-23 15:26:22 +01001154 if( status != PSA_SUCCESS )
1155 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +01001156 }
Hanno Becker7d0a5692018-10-23 15:26:22 +01001157 else
1158#endif
1159 {
1160 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
1161 lbl, salt, salt_len,
Hanno Beckerf9ed7d52018-11-05 12:45:16 +00001162 session->master,
1163 master_secret_len );
Hanno Becker7d0a5692018-10-23 15:26:22 +01001164 if( ret != 0 )
1165 {
1166 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
1167 return( ret );
1168 }
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001169
Hanno Becker7d0a5692018-10-23 15:26:22 +01001170 MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret",
1171 handshake->premaster,
1172 handshake->pmslen );
Hanno Becker35b23c72018-10-23 12:10:41 +01001173
Hanno Becker7d0a5692018-10-23 15:26:22 +01001174 mbedtls_platform_zeroize( handshake->premaster,
1175 sizeof(handshake->premaster) );
1176 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001177 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001178
1179 /*
1180 * Swap the client and server random values.
1181 */
Paul Bakker48916f92012-09-16 19:57:18 +00001182 memcpy( tmp, handshake->randbytes, 64 );
1183 memcpy( handshake->randbytes, tmp + 32, 32 );
1184 memcpy( handshake->randbytes + 32, tmp, 32 );
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001185 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001186
1187 /*
1188 * SSLv3:
1189 * key block =
1190 * MD5( master + SHA1( 'A' + master + randbytes ) ) +
1191 * MD5( master + SHA1( 'BB' + master + randbytes ) ) +
1192 * MD5( master + SHA1( 'CCC' + master + randbytes ) ) +
1193 * MD5( master + SHA1( 'DDDD' + master + randbytes ) ) +
1194 * ...
1195 *
1196 * TLSv1:
1197 * key block = PRF( master, "key expansion", randbytes )
1198 */
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +01001199 ret = handshake->tls_prf( session->master, 48, "key expansion",
1200 handshake->randbytes, 64, keyblk, 256 );
1201 if( ret != 0 )
1202 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001203 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +01001204 return( ret );
1205 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001206
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001207 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
1208 mbedtls_ssl_get_ciphersuite_name( session->ciphersuite ) ) );
1209 MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", session->master, 48 );
1210 MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", handshake->randbytes, 64 );
1211 MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001212
Paul Bakker5121ce52009-01-03 21:22:43 +00001213 /*
1214 * Determine the appropriate key, IV and MAC length.
1215 */
Paul Bakker68884e32013-01-07 18:20:04 +01001216
Hanno Becker88aaf652017-12-27 08:17:40 +00001217 keylen = cipher_info->key_bitlen / 8;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001218
Hanno Becker8031d062018-01-03 15:32:31 +00001219#if defined(MBEDTLS_GCM_C) || \
1220 defined(MBEDTLS_CCM_C) || \
1221 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001222 if( cipher_info->mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001223 cipher_info->mode == MBEDTLS_MODE_CCM ||
1224 cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakker5121ce52009-01-03 21:22:43 +00001225 {
Hanno Beckerf704bef2018-11-16 15:21:18 +00001226 size_t explicit_ivlen;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001227
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001228 transform->maclen = 0;
Hanno Becker81c7b182017-11-09 18:39:33 +00001229 mac_key_len = 0;
Hanno Beckere694c3e2017-12-27 21:34:08 +00001230 transform->taglen =
1231 ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001232
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001233 /* All modes haves 96-bit IVs;
1234 * GCM and CCM has 4 implicit and 8 explicit bytes
1235 * ChachaPoly has all 12 bytes implicit
1236 */
Paul Bakker68884e32013-01-07 18:20:04 +01001237 transform->ivlen = 12;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001238 if( cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
1239 transform->fixed_ivlen = 12;
1240 else
1241 transform->fixed_ivlen = 4;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001242
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001243 /* Minimum length of encrypted record */
1244 explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
Hanno Beckere694c3e2017-12-27 21:34:08 +00001245 transform->minlen = explicit_ivlen + transform->taglen;
Paul Bakker68884e32013-01-07 18:20:04 +01001246 }
1247 else
Hanno Becker8031d062018-01-03 15:32:31 +00001248#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
1249#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
1250 if( cipher_info->mode == MBEDTLS_MODE_STREAM ||
1251 cipher_info->mode == MBEDTLS_MODE_CBC )
Paul Bakker68884e32013-01-07 18:20:04 +01001252 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001253 /* Initialize HMAC contexts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001254 if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 ||
1255 ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 )
Paul Bakker68884e32013-01-07 18:20:04 +01001256 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001257 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001258 goto end;
Paul Bakker68884e32013-01-07 18:20:04 +01001259 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001260
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001261 /* Get MAC length */
Hanno Becker81c7b182017-11-09 18:39:33 +00001262 mac_key_len = mbedtls_md_get_size( md_info );
1263 transform->maclen = mac_key_len;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001264
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001265#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001266 /*
1267 * If HMAC is to be truncated, we shall keep the leftmost bytes,
1268 * (rfc 6066 page 13 or rfc 2104 section 4),
1269 * so we only need to adjust the length here.
1270 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001271 if( session->trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_ENABLED )
Hanno Beckere89353a2017-11-20 16:36:41 +00001272 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001273 transform->maclen = MBEDTLS_SSL_TRUNCATED_HMAC_LEN;
Hanno Beckere89353a2017-11-20 16:36:41 +00001274
1275#if defined(MBEDTLS_SSL_TRUNCATED_HMAC_COMPAT)
1276 /* Fall back to old, non-compliant version of the truncated
Hanno Becker563423f2017-11-21 17:20:17 +00001277 * HMAC implementation which also truncates the key
1278 * (Mbed TLS versions from 1.3 to 2.6.0) */
Hanno Beckere89353a2017-11-20 16:36:41 +00001279 mac_key_len = transform->maclen;
1280#endif
1281 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001282#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001283
1284 /* IV length */
Paul Bakker68884e32013-01-07 18:20:04 +01001285 transform->ivlen = cipher_info->iv_size;
Paul Bakker5121ce52009-01-03 21:22:43 +00001286
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001287 /* Minimum length */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001288 if( cipher_info->mode == MBEDTLS_MODE_STREAM )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001289 transform->minlen = transform->maclen;
1290 else
Paul Bakker68884e32013-01-07 18:20:04 +01001291 {
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001292 /*
1293 * GenericBlockCipher:
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +01001294 * 1. if EtM is in use: one block plus MAC
1295 * otherwise: * first multiple of blocklen greater than maclen
1296 * 2. IV except for SSL3 and TLS 1.0
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001297 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001298#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1299 if( session->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +01001300 {
1301 transform->minlen = transform->maclen
1302 + cipher_info->block_size;
1303 }
1304 else
1305#endif
1306 {
1307 transform->minlen = transform->maclen
1308 + cipher_info->block_size
1309 - transform->maclen % cipher_info->block_size;
1310 }
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001311
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001312#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
1313 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ||
1314 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001315 ; /* No need to adjust minlen */
Paul Bakker68884e32013-01-07 18:20:04 +01001316 else
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001317#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001318#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
1319 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_2 ||
1320 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001321 {
1322 transform->minlen += transform->ivlen;
1323 }
1324 else
1325#endif
1326 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001327 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001328 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1329 goto end;
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001330 }
Paul Bakker68884e32013-01-07 18:20:04 +01001331 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001332 }
Hanno Becker8031d062018-01-03 15:32:31 +00001333 else
1334#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
1335 {
1336 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1337 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1338 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001339
Hanno Becker88aaf652017-12-27 08:17:40 +00001340 MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %u, minlen: %u, ivlen: %u, maclen: %u",
1341 (unsigned) keylen,
1342 (unsigned) transform->minlen,
1343 (unsigned) transform->ivlen,
1344 (unsigned) transform->maclen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001345
1346 /*
1347 * Finally setup the cipher contexts, IVs and MAC secrets.
1348 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001349#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001350 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00001351 {
Hanno Becker81c7b182017-11-09 18:39:33 +00001352 key1 = keyblk + mac_key_len * 2;
Hanno Becker88aaf652017-12-27 08:17:40 +00001353 key2 = keyblk + mac_key_len * 2 + keylen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001354
Paul Bakker68884e32013-01-07 18:20:04 +01001355 mac_enc = keyblk;
Hanno Becker81c7b182017-11-09 18:39:33 +00001356 mac_dec = keyblk + mac_key_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001357
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001358 /*
1359 * This is not used in TLS v1.1.
1360 */
Paul Bakker48916f92012-09-16 19:57:18 +00001361 iv_copy_len = ( transform->fixed_ivlen ) ?
1362 transform->fixed_ivlen : transform->ivlen;
Hanno Becker88aaf652017-12-27 08:17:40 +00001363 memcpy( transform->iv_enc, key2 + keylen, iv_copy_len );
1364 memcpy( transform->iv_dec, key2 + keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +00001365 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001366 }
1367 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001368#endif /* MBEDTLS_SSL_CLI_C */
1369#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001370 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00001371 {
Hanno Becker88aaf652017-12-27 08:17:40 +00001372 key1 = keyblk + mac_key_len * 2 + keylen;
Hanno Becker81c7b182017-11-09 18:39:33 +00001373 key2 = keyblk + mac_key_len * 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00001374
Hanno Becker81c7b182017-11-09 18:39:33 +00001375 mac_enc = keyblk + mac_key_len;
Paul Bakker68884e32013-01-07 18:20:04 +01001376 mac_dec = keyblk;
Paul Bakker5121ce52009-01-03 21:22:43 +00001377
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001378 /*
1379 * This is not used in TLS v1.1.
1380 */
Paul Bakker48916f92012-09-16 19:57:18 +00001381 iv_copy_len = ( transform->fixed_ivlen ) ?
1382 transform->fixed_ivlen : transform->ivlen;
Hanno Becker88aaf652017-12-27 08:17:40 +00001383 memcpy( transform->iv_dec, key1 + keylen, iv_copy_len );
1384 memcpy( transform->iv_enc, key1 + keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +00001385 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001386 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001387 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001388#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001389 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001390 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001391 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1392 goto end;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001393 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001394
Hanno Beckerd56ed242018-01-03 15:32:51 +00001395#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001396#if defined(MBEDTLS_SSL_PROTO_SSL3)
1397 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker68884e32013-01-07 18:20:04 +01001398 {
Hanno Beckerd56ed242018-01-03 15:32:51 +00001399 if( mac_key_len > sizeof( transform->mac_enc ) )
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001400 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001401 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001402 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1403 goto end;
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001404 }
1405
Hanno Becker81c7b182017-11-09 18:39:33 +00001406 memcpy( transform->mac_enc, mac_enc, mac_key_len );
1407 memcpy( transform->mac_dec, mac_dec, mac_key_len );
Paul Bakker68884e32013-01-07 18:20:04 +01001408 }
1409 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001410#endif /* MBEDTLS_SSL_PROTO_SSL3 */
1411#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1412 defined(MBEDTLS_SSL_PROTO_TLS1_2)
1413 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakker68884e32013-01-07 18:20:04 +01001414 {
Gilles Peskine039fd122018-03-19 19:06:08 +01001415 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
1416 For AEAD-based ciphersuites, there is nothing to do here. */
1417 if( mac_key_len != 0 )
1418 {
1419 mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, mac_key_len );
1420 mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, mac_key_len );
1421 }
Paul Bakker68884e32013-01-07 18:20:04 +01001422 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001423 else
1424#endif
Paul Bakker577e0062013-08-28 11:57:20 +02001425 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001426 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001427 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1428 goto end;
Paul Bakker577e0062013-08-28 11:57:20 +02001429 }
Hanno Beckerd56ed242018-01-03 15:32:51 +00001430#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker68884e32013-01-07 18:20:04 +01001431
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001432#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
1433 if( mbedtls_ssl_hw_record_init != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00001434 {
1435 int ret = 0;
1436
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001437 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_init()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00001438
Hanno Becker88aaf652017-12-27 08:17:40 +00001439 if( ( ret = mbedtls_ssl_hw_record_init( ssl, key1, key2, keylen,
Paul Bakker07eb38b2012-12-19 14:42:06 +01001440 transform->iv_enc, transform->iv_dec,
1441 iv_copy_len,
Paul Bakker68884e32013-01-07 18:20:04 +01001442 mac_enc, mac_dec,
Hanno Becker81c7b182017-11-09 18:39:33 +00001443 mac_key_len ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00001444 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001445 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_init", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001446 ret = MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
1447 goto end;
Paul Bakker05ef8352012-05-08 09:17:57 +00001448 }
1449 }
Hanno Beckerd56ed242018-01-03 15:32:51 +00001450#else
1451 ((void) mac_dec);
1452 ((void) mac_enc);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001453#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00001454
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001455#if defined(MBEDTLS_SSL_EXPORT_KEYS)
1456 if( ssl->conf->f_export_keys != NULL )
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001457 {
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001458 ssl->conf->f_export_keys( ssl->conf->p_export_keys,
1459 session->master, keyblk,
Hanno Becker88aaf652017-12-27 08:17:40 +00001460 mac_key_len, keylen,
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001461 iv_copy_len );
1462 }
Ron Eldorf5cc10d2019-05-07 18:33:40 +03001463
1464 if( ssl->conf->f_export_keys_ext != NULL )
1465 {
1466 ssl->conf->f_export_keys_ext( ssl->conf->p_export_keys,
1467 session->master, keyblk,
Ron Eldorb7fd64c2019-05-12 11:03:32 +03001468 mac_key_len, keylen,
Ron Eldor51d3ab52019-05-12 14:54:30 +03001469 iv_copy_len,
Ron Eldorf5cc10d2019-05-07 18:33:40 +03001470 handshake->randbytes + 32,
Ron Eldor51d3ab52019-05-12 14:54:30 +03001471 handshake->randbytes,
Ron Eldorcf280092019-05-14 20:19:13 +03001472 tls_prf_get_type( handshake->tls_prf ) );
Ron Eldorf5cc10d2019-05-07 18:33:40 +03001473 }
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001474#endif
1475
Hanno Beckerf704bef2018-11-16 15:21:18 +00001476#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Beckercb1cc802018-11-17 22:27:38 +00001477
1478 /* Only use PSA-based ciphers for TLS-1.2.
1479 * That's relevant at least for TLS-1.0, where
1480 * we assume that mbedtls_cipher_crypt() updates
1481 * the structure field for the IV, which the PSA-based
1482 * implementation currently doesn't. */
1483#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1484 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001485 {
Hanno Beckercb1cc802018-11-17 22:27:38 +00001486 ret = mbedtls_cipher_setup_psa( &transform->cipher_ctx_enc,
Hanno Becker22bf1452019-04-05 11:21:08 +01001487 cipher_info, transform->taglen );
Hanno Beckercb1cc802018-11-17 22:27:38 +00001488 if( ret != 0 && ret != MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE )
1489 {
1490 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup_psa", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001491 goto end;
Hanno Beckercb1cc802018-11-17 22:27:38 +00001492 }
1493
1494 if( ret == 0 )
1495 {
Hanno Becker4c8c7aa2019-04-10 09:25:41 +01001496 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Successfully setup PSA-based encryption cipher context" ) );
Hanno Beckercb1cc802018-11-17 22:27:38 +00001497 psa_fallthrough = 0;
1498 }
1499 else
1500 {
1501 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to setup PSA-based cipher context for record encryption - fall through to default setup." ) );
1502 psa_fallthrough = 1;
1503 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001504 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001505 else
Hanno Beckercb1cc802018-11-17 22:27:38 +00001506 psa_fallthrough = 1;
1507#else
1508 psa_fallthrough = 1;
1509#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Beckerf704bef2018-11-16 15:21:18 +00001510
Hanno Beckercb1cc802018-11-17 22:27:38 +00001511 if( psa_fallthrough == 1 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001512#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001513 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001514 cipher_info ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001515 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001516 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001517 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001518 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001519
Hanno Beckerf704bef2018-11-16 15:21:18 +00001520#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Beckercb1cc802018-11-17 22:27:38 +00001521 /* Only use PSA-based ciphers for TLS-1.2.
1522 * That's relevant at least for TLS-1.0, where
1523 * we assume that mbedtls_cipher_crypt() updates
1524 * the structure field for the IV, which the PSA-based
1525 * implementation currently doesn't. */
1526#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1527 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001528 {
Hanno Beckercb1cc802018-11-17 22:27:38 +00001529 ret = mbedtls_cipher_setup_psa( &transform->cipher_ctx_dec,
Hanno Becker22bf1452019-04-05 11:21:08 +01001530 cipher_info, transform->taglen );
Hanno Beckercb1cc802018-11-17 22:27:38 +00001531 if( ret != 0 && ret != MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE )
1532 {
1533 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup_psa", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001534 goto end;
Hanno Beckercb1cc802018-11-17 22:27:38 +00001535 }
1536
1537 if( ret == 0 )
1538 {
Hanno Becker4c8c7aa2019-04-10 09:25:41 +01001539 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Successfully setup PSA-based decryption cipher context" ) );
Hanno Beckercb1cc802018-11-17 22:27:38 +00001540 psa_fallthrough = 0;
1541 }
1542 else
1543 {
1544 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to setup PSA-based cipher context for record decryption - fall through to default setup." ) );
1545 psa_fallthrough = 1;
1546 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001547 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001548 else
Hanno Beckercb1cc802018-11-17 22:27:38 +00001549 psa_fallthrough = 1;
1550#else
1551 psa_fallthrough = 1;
1552#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Beckerf704bef2018-11-16 15:21:18 +00001553
Hanno Beckercb1cc802018-11-17 22:27:38 +00001554 if( psa_fallthrough == 1 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001555#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001556 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001557 cipher_info ) ) != 0 )
1558 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001559 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001560 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001561 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001562
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001563 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001564 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001565 MBEDTLS_ENCRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001566 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001567 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001568 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001569 }
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001570
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001571 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001572 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001573 MBEDTLS_DECRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001574 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001575 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001576 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001577 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001578
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001579#if defined(MBEDTLS_CIPHER_MODE_CBC)
1580 if( cipher_info->mode == MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001581 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001582 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc,
1583 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001584 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001585 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001586 goto end;
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001587 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001588
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001589 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec,
1590 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001591 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001592 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001593 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001594 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001595 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001596#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001597
Paul Bakker5121ce52009-01-03 21:22:43 +00001598
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001599#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00001600 // Initialize compression
1601 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001602 if( session->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001603 {
Paul Bakker16770332013-10-11 09:59:44 +02001604 if( ssl->compress_buf == NULL )
1605 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001606 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Allocating compression buffer" ) );
Angus Grattond8213d02016-05-25 20:56:48 +10001607 ssl->compress_buf = mbedtls_calloc( 1, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Paul Bakker16770332013-10-11 09:59:44 +02001608 if( ssl->compress_buf == NULL )
1609 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02001610 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
Angus Grattond8213d02016-05-25 20:56:48 +10001611 MBEDTLS_SSL_COMPRESS_BUFFER_LEN ) );
Ron Eldore6992702019-05-07 18:27:13 +03001612 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
1613 goto end;
Paul Bakker16770332013-10-11 09:59:44 +02001614 }
1615 }
1616
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001617 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Initializing zlib states" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001618
Paul Bakker48916f92012-09-16 19:57:18 +00001619 memset( &transform->ctx_deflate, 0, sizeof( transform->ctx_deflate ) );
1620 memset( &transform->ctx_inflate, 0, sizeof( transform->ctx_inflate ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001621
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001622 if( deflateInit( &transform->ctx_deflate,
1623 Z_DEFAULT_COMPRESSION ) != Z_OK ||
Paul Bakker48916f92012-09-16 19:57:18 +00001624 inflateInit( &transform->ctx_inflate ) != Z_OK )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001625 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001626 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001627 ret = MBEDTLS_ERR_SSL_COMPRESSION_FAILED;
1628 goto end;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001629 }
1630 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001631#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00001632
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001633 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001634end:
Ron Eldora9f9a732019-05-07 18:29:02 +03001635 mbedtls_platform_zeroize( keyblk, sizeof( keyblk ) );
1636 mbedtls_platform_zeroize( handshake->randbytes,
1637 sizeof( handshake->randbytes ) );
Ron Eldore6992702019-05-07 18:27:13 +03001638 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001639}
1640
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001641#if defined(MBEDTLS_SSL_PROTO_SSL3)
1642void ssl_calc_verify_ssl( mbedtls_ssl_context *ssl, unsigned char hash[36] )
Paul Bakker5121ce52009-01-03 21:22:43 +00001643{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001644 mbedtls_md5_context md5;
1645 mbedtls_sha1_context sha1;
Paul Bakker5121ce52009-01-03 21:22:43 +00001646 unsigned char pad_1[48];
1647 unsigned char pad_2[48];
1648
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001649 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001650
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001651 mbedtls_md5_init( &md5 );
1652 mbedtls_sha1_init( &sha1 );
1653
1654 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1655 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001656
Paul Bakker380da532012-04-18 16:10:25 +00001657 memset( pad_1, 0x36, 48 );
1658 memset( pad_2, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001659
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001660 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1661 mbedtls_md5_update_ret( &md5, pad_1, 48 );
1662 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001663
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001664 mbedtls_md5_starts_ret( &md5 );
1665 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1666 mbedtls_md5_update_ret( &md5, pad_2, 48 );
1667 mbedtls_md5_update_ret( &md5, hash, 16 );
1668 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001669
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001670 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1671 mbedtls_sha1_update_ret( &sha1, pad_1, 40 );
1672 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001673
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001674 mbedtls_sha1_starts_ret( &sha1 );
1675 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1676 mbedtls_sha1_update_ret( &sha1, pad_2, 40 );
1677 mbedtls_sha1_update_ret( &sha1, hash + 16, 20 );
1678 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001679
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001680 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
1681 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001682
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001683 mbedtls_md5_free( &md5 );
1684 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001685
Paul Bakker380da532012-04-18 16:10:25 +00001686 return;
1687}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001688#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker380da532012-04-18 16:10:25 +00001689
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001690#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
1691void ssl_calc_verify_tls( mbedtls_ssl_context *ssl, unsigned char hash[36] )
Paul Bakker380da532012-04-18 16:10:25 +00001692{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001693 mbedtls_md5_context md5;
1694 mbedtls_sha1_context sha1;
Paul Bakker380da532012-04-18 16:10:25 +00001695
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001696 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001697
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001698 mbedtls_md5_init( &md5 );
1699 mbedtls_sha1_init( &sha1 );
1700
1701 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1702 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker380da532012-04-18 16:10:25 +00001703
Andrzej Kurekeb342242019-01-29 09:14:33 -05001704 mbedtls_md5_finish_ret( &md5, hash );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001705 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001706
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001707 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
1708 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001709
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001710 mbedtls_md5_free( &md5 );
1711 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001712
Paul Bakker380da532012-04-18 16:10:25 +00001713 return;
1714}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001715#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker380da532012-04-18 16:10:25 +00001716
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001717#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1718#if defined(MBEDTLS_SHA256_C)
1719void ssl_calc_verify_tls_sha256( mbedtls_ssl_context *ssl, unsigned char hash[32] )
Paul Bakker380da532012-04-18 16:10:25 +00001720{
Andrzej Kurekeb342242019-01-29 09:14:33 -05001721#if defined(MBEDTLS_USE_PSA_CRYPTO)
1722 size_t hash_size;
1723 psa_status_t status;
1724 psa_hash_operation_t sha256_psa = psa_hash_operation_init();
1725
1726 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha256" ) );
1727 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
1728 if( status != PSA_SUCCESS )
1729 {
1730 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
1731 return;
1732 }
1733
1734 status = psa_hash_finish( &sha256_psa, hash, 32, &hash_size );
1735 if( status != PSA_SUCCESS )
1736 {
1737 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
1738 return;
1739 }
1740 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, 32 );
1741 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
1742#else
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001743 mbedtls_sha256_context sha256;
Paul Bakker380da532012-04-18 16:10:25 +00001744
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001745 mbedtls_sha256_init( &sha256 );
1746
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001747 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001748
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001749 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001750 mbedtls_sha256_finish_ret( &sha256, hash );
Paul Bakker380da532012-04-18 16:10:25 +00001751
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001752 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 32 );
1753 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001754
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001755 mbedtls_sha256_free( &sha256 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001756#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker380da532012-04-18 16:10:25 +00001757 return;
1758}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001759#endif /* MBEDTLS_SHA256_C */
Paul Bakker380da532012-04-18 16:10:25 +00001760
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001761#if defined(MBEDTLS_SHA512_C)
1762void ssl_calc_verify_tls_sha384( mbedtls_ssl_context *ssl, unsigned char hash[48] )
Paul Bakker380da532012-04-18 16:10:25 +00001763{
Andrzej Kurekeb342242019-01-29 09:14:33 -05001764#if defined(MBEDTLS_USE_PSA_CRYPTO)
1765 size_t hash_size;
1766 psa_status_t status;
Andrzej Kurek972fba52019-01-30 03:29:12 -05001767 psa_hash_operation_t sha384_psa = psa_hash_operation_init();
Andrzej Kurekeb342242019-01-29 09:14:33 -05001768
1769 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha384" ) );
Andrzej Kurek972fba52019-01-30 03:29:12 -05001770 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001771 if( status != PSA_SUCCESS )
1772 {
1773 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
1774 return;
1775 }
1776
Andrzej Kurek972fba52019-01-30 03:29:12 -05001777 status = psa_hash_finish( &sha384_psa, hash, 48, &hash_size );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001778 if( status != PSA_SUCCESS )
1779 {
1780 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
1781 return;
1782 }
1783 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, 48 );
1784 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
1785#else
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001786 mbedtls_sha512_context sha512;
Paul Bakker380da532012-04-18 16:10:25 +00001787
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001788 mbedtls_sha512_init( &sha512 );
1789
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001790 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001791
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001792 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001793 mbedtls_sha512_finish_ret( &sha512, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001794
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001795 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 48 );
1796 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001797
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001798 mbedtls_sha512_free( &sha512 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001799#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker5121ce52009-01-03 21:22:43 +00001800 return;
1801}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001802#endif /* MBEDTLS_SHA512_C */
1803#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001804
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001805#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
1806int 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 +02001807{
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001808 unsigned char *p = ssl->handshake->premaster;
1809 unsigned char *end = p + sizeof( ssl->handshake->premaster );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001810 const unsigned char *psk = ssl->conf->psk;
1811 size_t psk_len = ssl->conf->psk_len;
1812
1813 /* If the psk callback was called, use its result */
1814 if( ssl->handshake->psk != NULL )
1815 {
1816 psk = ssl->handshake->psk;
1817 psk_len = ssl->handshake->psk_len;
1818 }
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001819
1820 /*
1821 * PMS = struct {
1822 * opaque other_secret<0..2^16-1>;
1823 * opaque psk<0..2^16-1>;
1824 * };
1825 * with "other_secret" depending on the particular key exchange
1826 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001827#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
1828 if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001829 {
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001830 if( end - p < 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001831 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001832
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001833 *(p++) = (unsigned char)( psk_len >> 8 );
1834 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001835
1836 if( end < p || (size_t)( end - p ) < psk_len )
1837 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1838
1839 memset( p, 0, psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001840 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001841 }
1842 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001843#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
1844#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
1845 if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001846 {
1847 /*
1848 * other_secret already set by the ClientKeyExchange message,
1849 * and is 48 bytes long
1850 */
Philippe Antoine747fd532018-05-30 09:13:21 +02001851 if( end - p < 2 )
1852 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1853
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001854 *p++ = 0;
1855 *p++ = 48;
1856 p += 48;
1857 }
1858 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001859#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
1860#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
1861 if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001862 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001863 int ret;
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01001864 size_t len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001865
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001866 /* Write length only when we know the actual value */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001867 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01001868 p + 2, end - ( p + 2 ), &len,
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001869 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001870 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001871 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001872 return( ret );
1873 }
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001874 *(p++) = (unsigned char)( len >> 8 );
1875 *(p++) = (unsigned char)( len );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001876 p += len;
1877
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001878 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001879 }
1880 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001881#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
1882#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
1883 if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001884 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001885 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001886 size_t zlen;
1887
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001888 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
Paul Bakker66d5d072014-06-17 16:39:18 +02001889 p + 2, end - ( p + 2 ),
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001890 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001891 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001892 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001893 return( ret );
1894 }
1895
1896 *(p++) = (unsigned char)( zlen >> 8 );
1897 *(p++) = (unsigned char)( zlen );
1898 p += zlen;
1899
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001900 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
1901 MBEDTLS_DEBUG_ECDH_Z );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001902 }
1903 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001904#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001905 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001906 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1907 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001908 }
1909
1910 /* opaque psk<0..2^16-1>; */
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001911 if( end - p < 2 )
1912 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01001913
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001914 *(p++) = (unsigned char)( psk_len >> 8 );
1915 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001916
1917 if( end < p || (size_t)( end - p ) < psk_len )
1918 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1919
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001920 memcpy( p, psk, psk_len );
1921 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001922
1923 ssl->handshake->pmslen = p - ssl->handshake->premaster;
1924
1925 return( 0 );
1926}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001927#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001928
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001929#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00001930/*
1931 * SSLv3.0 MAC functions
1932 */
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001933#define SSL_MAC_MAX_BYTES 20 /* MD-5 or SHA-1 */
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001934static void ssl_mac( mbedtls_md_context_t *md_ctx,
1935 const unsigned char *secret,
1936 const unsigned char *buf, size_t len,
1937 const unsigned char *ctr, int type,
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001938 unsigned char out[SSL_MAC_MAX_BYTES] )
Paul Bakker5121ce52009-01-03 21:22:43 +00001939{
1940 unsigned char header[11];
1941 unsigned char padding[48];
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001942 int padlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001943 int md_size = mbedtls_md_get_size( md_ctx->md_info );
1944 int md_type = mbedtls_md_get_type( md_ctx->md_info );
Paul Bakker68884e32013-01-07 18:20:04 +01001945
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001946 /* Only MD5 and SHA-1 supported */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001947 if( md_type == MBEDTLS_MD_MD5 )
Paul Bakker68884e32013-01-07 18:20:04 +01001948 padlen = 48;
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001949 else
Paul Bakker68884e32013-01-07 18:20:04 +01001950 padlen = 40;
Paul Bakker5121ce52009-01-03 21:22:43 +00001951
1952 memcpy( header, ctr, 8 );
1953 header[ 8] = (unsigned char) type;
1954 header[ 9] = (unsigned char)( len >> 8 );
1955 header[10] = (unsigned char)( len );
1956
Paul Bakker68884e32013-01-07 18:20:04 +01001957 memset( padding, 0x36, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001958 mbedtls_md_starts( md_ctx );
1959 mbedtls_md_update( md_ctx, secret, md_size );
1960 mbedtls_md_update( md_ctx, padding, padlen );
1961 mbedtls_md_update( md_ctx, header, 11 );
1962 mbedtls_md_update( md_ctx, buf, len );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001963 mbedtls_md_finish( md_ctx, out );
Paul Bakker5121ce52009-01-03 21:22:43 +00001964
Paul Bakker68884e32013-01-07 18:20:04 +01001965 memset( padding, 0x5C, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001966 mbedtls_md_starts( md_ctx );
1967 mbedtls_md_update( md_ctx, secret, md_size );
1968 mbedtls_md_update( md_ctx, padding, padlen );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001969 mbedtls_md_update( md_ctx, out, md_size );
1970 mbedtls_md_finish( md_ctx, out );
Paul Bakker5f70b252012-09-13 14:23:06 +00001971}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001972#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +00001973
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001974/* The function below is only used in the Lucky 13 counter-measure in
Hanno Beckerb2ca87d2018-10-18 15:43:13 +01001975 * mbedtls_ssl_decrypt_buf(). These are the defines that guard the call site. */
Hanno Becker52344c22018-01-03 15:24:20 +00001976#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) && \
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001977 ( defined(MBEDTLS_SSL_PROTO_TLS1) || \
1978 defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1979 defined(MBEDTLS_SSL_PROTO_TLS1_2) )
1980/* This function makes sure every byte in the memory region is accessed
1981 * (in ascending addresses order) */
1982static void ssl_read_memory( unsigned char *p, size_t len )
1983{
1984 unsigned char acc = 0;
1985 volatile unsigned char force;
1986
1987 for( ; len != 0; p++, len-- )
1988 acc ^= *p;
1989
1990 force = acc;
1991 (void) force;
1992}
1993#endif /* SSL_SOME_MODES_USE_MAC && ( TLS1 || TLS1_1 || TLS1_2 ) */
1994
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001995/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001996 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +02001997 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001998
Hanno Beckera0e20d02019-05-15 14:03:01 +01001999#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerd3f8c792019-05-20 15:06:12 +01002000/* This functions transforms a DTLS plaintext fragment and a record content
2001 * type into an instance of the DTLSInnerPlaintext structure:
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002002 *
2003 * struct {
2004 * opaque content[DTLSPlaintext.length];
2005 * ContentType real_type;
2006 * uint8 zeros[length_of_padding];
2007 * } DTLSInnerPlaintext;
2008 *
2009 * Input:
2010 * - `content`: The beginning of the buffer holding the
2011 * plaintext to be wrapped.
2012 * - `*content_size`: The length of the plaintext in Bytes.
2013 * - `max_len`: The number of Bytes available starting from
2014 * `content`. This must be `>= *content_size`.
2015 * - `rec_type`: The desired record content type.
2016 *
2017 * Output:
2018 * - `content`: The beginning of the resulting DTLSInnerPlaintext structure.
2019 * - `*content_size`: The length of the resulting DTLSInnerPlaintext structure.
2020 *
2021 * Returns:
2022 * - `0` on success.
2023 * - A negative error code if `max_len` didn't offer enough space
2024 * for the expansion.
2025 */
2026static int ssl_cid_build_inner_plaintext( unsigned char *content,
2027 size_t *content_size,
2028 size_t remaining,
2029 uint8_t rec_type )
2030{
2031 size_t len = *content_size;
Hanno Beckerb9ec44f2019-05-13 15:31:17 +01002032 size_t pad = ( MBEDTLS_SSL_CID_PADDING_GRANULARITY -
2033 ( len + 1 ) % MBEDTLS_SSL_CID_PADDING_GRANULARITY ) %
2034 MBEDTLS_SSL_CID_PADDING_GRANULARITY;
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002035
2036 /* Write real content type */
2037 if( remaining == 0 )
2038 return( -1 );
2039 content[ len ] = rec_type;
2040 len++;
2041 remaining--;
2042
2043 if( remaining < pad )
2044 return( -1 );
2045 memset( content + len, 0, pad );
2046 len += pad;
2047 remaining -= pad;
2048
2049 *content_size = len;
2050 return( 0 );
2051}
2052
Hanno Becker07dc97d2019-05-20 15:08:01 +01002053/* This function parses a DTLSInnerPlaintext structure.
2054 * See ssl_cid_build_inner_plaintext() for details. */
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002055static int ssl_cid_parse_inner_plaintext( unsigned char const *content,
2056 size_t *content_size,
2057 uint8_t *rec_type )
2058{
2059 size_t remaining = *content_size;
2060
2061 /* Determine length of padding by skipping zeroes from the back. */
2062 do
2063 {
2064 if( remaining == 0 )
2065 return( -1 );
2066 remaining--;
2067 } while( content[ remaining ] == 0 );
2068
2069 *content_size = remaining;
2070 *rec_type = content[ remaining ];
2071
2072 return( 0 );
2073}
Hanno Beckera0e20d02019-05-15 14:03:01 +01002074#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002075
Hanno Beckerd5aeab12019-05-20 14:50:53 +01002076/* `add_data` must have size 13 Bytes if the CID extension is disabled,
Hanno Beckerc4a190b2019-05-08 18:15:21 +01002077 * and 13 + 1 + CID-length Bytes if the CID extension is enabled. */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002078static void ssl_extract_add_data_from_record( unsigned char* add_data,
Hanno Beckercab87e62019-04-29 13:52:53 +01002079 size_t *add_data_len,
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002080 mbedtls_record *rec )
2081{
Hanno Beckerd5aeab12019-05-20 14:50:53 +01002082 /* Quoting RFC 5246 (TLS 1.2):
Hanno Beckercab87e62019-04-29 13:52:53 +01002083 *
2084 * additional_data = seq_num + TLSCompressed.type +
2085 * TLSCompressed.version + TLSCompressed.length;
2086 *
Hanno Beckerd5aeab12019-05-20 14:50:53 +01002087 * For the CID extension, this is extended as follows
2088 * (quoting draft-ietf-tls-dtls-connection-id-05,
2089 * https://tools.ietf.org/html/draft-ietf-tls-dtls-connection-id-05):
Hanno Beckercab87e62019-04-29 13:52:53 +01002090 *
2091 * additional_data = seq_num + DTLSPlaintext.type +
2092 * DTLSPlaintext.version +
Hanno Beckerd5aeab12019-05-20 14:50:53 +01002093 * cid +
2094 * cid_length +
Hanno Beckercab87e62019-04-29 13:52:53 +01002095 * length_of_DTLSInnerPlaintext;
2096 */
2097
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002098 memcpy( add_data, rec->ctr, sizeof( rec->ctr ) );
2099 add_data[8] = rec->type;
Hanno Beckeredb24f82019-05-20 15:01:46 +01002100 memcpy( add_data + 9, rec->ver, sizeof( rec->ver ) );
Hanno Beckercab87e62019-04-29 13:52:53 +01002101
Hanno Beckera0e20d02019-05-15 14:03:01 +01002102#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker95e4bbc2019-05-09 11:38:24 +01002103 if( rec->cid_len != 0 )
2104 {
2105 memcpy( add_data + 11, rec->cid, rec->cid_len );
2106 add_data[11 + rec->cid_len + 0] = rec->cid_len;
2107 add_data[11 + rec->cid_len + 1] = ( rec->data_len >> 8 ) & 0xFF;
2108 add_data[11 + rec->cid_len + 2] = ( rec->data_len >> 0 ) & 0xFF;
2109 *add_data_len = 13 + 1 + rec->cid_len;
2110 }
2111 else
Hanno Beckera0e20d02019-05-15 14:03:01 +01002112#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker95e4bbc2019-05-09 11:38:24 +01002113 {
2114 add_data[11 + 0] = ( rec->data_len >> 8 ) & 0xFF;
2115 add_data[11 + 1] = ( rec->data_len >> 0 ) & 0xFF;
2116 *add_data_len = 13;
2117 }
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002118}
2119
Hanno Beckera18d1322018-01-03 14:27:32 +00002120int mbedtls_ssl_encrypt_buf( mbedtls_ssl_context *ssl,
2121 mbedtls_ssl_transform *transform,
2122 mbedtls_record *rec,
2123 int (*f_rng)(void *, unsigned char *, size_t),
2124 void *p_rng )
Paul Bakker5121ce52009-01-03 21:22:43 +00002125{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002126 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002127 int auth_done = 0;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002128 unsigned char * data;
Hanno Becker92fb4fa2019-05-20 14:54:26 +01002129 unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_OUT_LEN_MAX ];
Hanno Beckercab87e62019-04-29 13:52:53 +01002130 size_t add_data_len;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002131 size_t post_avail;
2132
2133 /* The SSL context is only used for debugging purposes! */
Hanno Beckera18d1322018-01-03 14:27:32 +00002134#if !defined(MBEDTLS_DEBUG_C)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002135 ((void) ssl);
2136#endif
2137
2138 /* The PRNG is used for dynamic IV generation that's used
2139 * for CBC transformations in TLS 1.1 and TLS 1.2. */
2140#if !( defined(MBEDTLS_CIPHER_MODE_CBC) && \
2141 ( defined(MBEDTLS_AES_C) || \
2142 defined(MBEDTLS_ARIA_C) || \
2143 defined(MBEDTLS_CAMELLIA_C) ) && \
2144 ( defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2) ) )
2145 ((void) f_rng);
2146 ((void) p_rng);
2147#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002148
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002149 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002150
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002151 if( transform == NULL )
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002152 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002153 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no transform provided to encrypt_buf" ) );
2154 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2155 }
Hanno Becker43c24b82019-05-01 09:45:57 +01002156 if( rec == NULL
2157 || rec->buf == NULL
2158 || rec->buf_len < rec->data_offset
2159 || rec->buf_len - rec->data_offset < rec->data_len
Hanno Beckera0e20d02019-05-15 14:03:01 +01002160#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker43c24b82019-05-01 09:45:57 +01002161 || rec->cid_len != 0
2162#endif
2163 )
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002164 {
2165 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to encrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002166 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002167 }
2168
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002169 data = rec->buf + rec->data_offset;
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002170 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002171 MBEDTLS_SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002172 data, rec->data_len );
2173
2174 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc );
2175
2176 if( rec->data_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
2177 {
2178 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record content %u too large, maximum %d",
2179 (unsigned) rec->data_len,
2180 MBEDTLS_SSL_OUT_CONTENT_LEN ) );
2181 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2182 }
Manuel Pégourié-Gonnard60346be2014-11-21 11:38:37 +01002183
Hanno Beckera0e20d02019-05-15 14:03:01 +01002184#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckercab87e62019-04-29 13:52:53 +01002185 /*
2186 * Add CID information
2187 */
2188 rec->cid_len = transform->out_cid_len;
2189 memcpy( rec->cid, transform->out_cid, transform->out_cid_len );
2190 MBEDTLS_SSL_DEBUG_BUF( 3, "CID", rec->cid, rec->cid_len );
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002191
2192 if( rec->cid_len != 0 )
2193 {
2194 /*
Hanno Becker07dc97d2019-05-20 15:08:01 +01002195 * Wrap plaintext into DTLSInnerPlaintext structure.
2196 * See ssl_cid_build_inner_plaintext() for more information.
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002197 *
Hanno Becker07dc97d2019-05-20 15:08:01 +01002198 * Note that this changes `rec->data_len`, and hence
2199 * `post_avail` needs to be recalculated afterwards.
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002200 */
2201 if( ssl_cid_build_inner_plaintext( data,
2202 &rec->data_len,
2203 post_avail,
2204 rec->type ) != 0 )
2205 {
2206 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2207 }
2208
2209 rec->type = MBEDTLS_SSL_MSG_CID;
2210 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01002211#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckercab87e62019-04-29 13:52:53 +01002212
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002213 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
2214
Paul Bakker5121ce52009-01-03 21:22:43 +00002215 /*
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01002216 * Add MAC before if needed
Paul Bakker5121ce52009-01-03 21:22:43 +00002217 */
Hanno Becker52344c22018-01-03 15:24:20 +00002218#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002219 if( mode == MBEDTLS_MODE_STREAM ||
2220 ( mode == MBEDTLS_MODE_CBC
2221#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002222 && transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002223#endif
2224 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00002225 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002226 if( post_avail < transform->maclen )
2227 {
2228 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2229 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2230 }
2231
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002232#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002233 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002234 {
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01002235 unsigned char mac[SSL_MAC_MAX_BYTES];
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002236 ssl_mac( &transform->md_ctx_enc, transform->mac_enc,
2237 data, rec->data_len, rec->ctr, rec->type, mac );
2238 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002239 }
2240 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002241#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002242#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2243 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002244 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002245 {
Hanno Becker992b6872017-11-09 18:57:39 +00002246 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
2247
Hanno Beckercab87e62019-04-29 13:52:53 +01002248 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker992b6872017-11-09 18:57:39 +00002249
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002250 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
Hanno Beckercab87e62019-04-29 13:52:53 +01002251 add_data_len );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002252 mbedtls_md_hmac_update( &transform->md_ctx_enc,
2253 data, rec->data_len );
2254 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
2255 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
2256
2257 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002258 }
2259 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002260#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002261 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002262 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2263 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002264 }
2265
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002266 MBEDTLS_SSL_DEBUG_BUF( 4, "computed mac", data + rec->data_len,
2267 transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002268
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002269 rec->data_len += transform->maclen;
2270 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002271 auth_done++;
Paul Bakker577e0062013-08-28 11:57:20 +02002272 }
Hanno Becker52344c22018-01-03 15:24:20 +00002273#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00002274
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002275 /*
2276 * Encrypt
2277 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002278#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
2279 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00002280 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002281 int ret;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002282 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002283 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002284 "including %d bytes of padding",
2285 rec->data_len, 0 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002286
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002287 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
2288 transform->iv_enc, transform->ivlen,
2289 data, rec->data_len,
2290 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002291 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002292 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002293 return( ret );
2294 }
2295
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002296 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002297 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002298 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2299 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002300 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002301 }
Paul Bakker68884e32013-01-07 18:20:04 +01002302 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002303#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002304
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002305#if defined(MBEDTLS_GCM_C) || \
2306 defined(MBEDTLS_CCM_C) || \
2307 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002308 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002309 mode == MBEDTLS_MODE_CCM ||
2310 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002311 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02002312 int ret;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002313 unsigned char iv[12];
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002314 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002315
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002316 /* Check that there's space for both the authentication tag
2317 * and the explicit IV before and after the record content. */
2318 if( post_avail < transform->taglen ||
2319 rec->data_offset < explicit_iv_len )
2320 {
2321 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2322 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2323 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00002324
Paul Bakker68884e32013-01-07 18:20:04 +01002325 /*
2326 * Generate IV
2327 */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002328 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
2329 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002330 /* GCM and CCM: fixed || explicit (=seqnum) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002331 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002332 memcpy( iv + transform->fixed_ivlen, rec->ctr,
2333 explicit_iv_len );
2334 /* Prefix record content with explicit IV. */
2335 memcpy( data - explicit_iv_len, rec->ctr, explicit_iv_len );
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002336 }
2337 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
2338 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002339 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002340 unsigned char i;
2341
2342 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
2343
2344 for( i = 0; i < 8; i++ )
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002345 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002346 }
2347 else
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01002348 {
2349 /* Reminder if we ever add an AEAD mode with a different size */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002350 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2351 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01002352 }
2353
Hanno Beckercab87e62019-04-29 13:52:53 +01002354 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker1f10d762019-04-26 13:34:37 +01002355
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002356 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (internal)",
2357 iv, transform->ivlen );
2358 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (transmitted)",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002359 data - explicit_iv_len, explicit_iv_len );
2360 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Hanno Beckercab87e62019-04-29 13:52:53 +01002361 add_data, add_data_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002362 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002363 "including 0 bytes of padding",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002364 rec->data_len ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00002365
Paul Bakker68884e32013-01-07 18:20:04 +01002366 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002367 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002368 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002369
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002370 if( ( ret = mbedtls_cipher_auth_encrypt( &transform->cipher_ctx_enc,
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002371 iv, transform->ivlen,
Hanno Beckercab87e62019-04-29 13:52:53 +01002372 add_data, add_data_len, /* add data */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002373 data, rec->data_len, /* source */
2374 data, &rec->data_len, /* destination */
2375 data + rec->data_len, transform->taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002376 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002377 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_encrypt", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002378 return( ret );
2379 }
2380
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002381 MBEDTLS_SSL_DEBUG_BUF( 4, "after encrypt: tag",
2382 data + rec->data_len, transform->taglen );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002383
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002384 rec->data_len += transform->taglen + explicit_iv_len;
2385 rec->data_offset -= explicit_iv_len;
2386 post_avail -= transform->taglen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002387 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002388 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002389 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002390#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2391#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002392 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002393 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00002394 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002395 int ret;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002396 size_t padlen, i;
2397 size_t olen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002398
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002399 /* Currently we're always using minimal padding
2400 * (up to 255 bytes would be allowed). */
2401 padlen = transform->ivlen - ( rec->data_len + 1 ) % transform->ivlen;
2402 if( padlen == transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002403 padlen = 0;
2404
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002405 /* Check there's enough space in the buffer for the padding. */
2406 if( post_avail < padlen + 1 )
2407 {
2408 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2409 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2410 }
2411
Paul Bakker5121ce52009-01-03 21:22:43 +00002412 for( i = 0; i <= padlen; i++ )
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002413 data[rec->data_len + i] = (unsigned char) padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002414
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002415 rec->data_len += padlen + 1;
2416 post_avail -= padlen + 1;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002417
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002418#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002419 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00002420 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
2421 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002422 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002423 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002424 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002425 if( f_rng == NULL )
2426 {
2427 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No PRNG provided to encrypt_record routine" ) );
2428 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2429 }
2430
2431 if( rec->data_offset < transform->ivlen )
2432 {
2433 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2434 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2435 }
2436
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002437 /*
2438 * Generate IV
2439 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002440 ret = f_rng( p_rng, transform->iv_enc, transform->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00002441 if( ret != 0 )
2442 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002443
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002444 memcpy( data - transform->ivlen, transform->iv_enc,
2445 transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002446
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002447 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002448#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002449
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002450 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002451 "including %d bytes of IV and %d bytes of padding",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002452 rec->data_len, transform->ivlen,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002453 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002454
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002455 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
2456 transform->iv_enc,
2457 transform->ivlen,
2458 data, rec->data_len,
2459 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002460 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002461 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002462 return( ret );
2463 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002464
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002465 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02002466 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002467 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2468 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002469 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002470
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002471#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002472 if( transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02002473 {
2474 /*
2475 * Save IV in SSL3 and TLS1
2476 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002477 memcpy( transform->iv_enc, transform->cipher_ctx_enc.iv,
2478 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002479 }
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002480 else
Paul Bakkercca5b812013-08-31 17:40:26 +02002481#endif
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002482 {
2483 data -= transform->ivlen;
2484 rec->data_offset -= transform->ivlen;
2485 rec->data_len += transform->ivlen;
2486 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002487
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002488#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002489 if( auth_done == 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002490 {
Hanno Becker3d8c9072018-01-05 16:24:22 +00002491 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
2492
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002493 /*
2494 * MAC(MAC_write_key, seq_num +
2495 * TLSCipherText.type +
2496 * TLSCipherText.version +
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002497 * length_of( (IV +) ENC(...) ) +
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002498 * IV + // except for TLS 1.0
2499 * ENC(content + padding + padding_length));
2500 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002501
2502 if( post_avail < transform->maclen)
2503 {
2504 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2505 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2506 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002507
Hanno Beckercab87e62019-04-29 13:52:53 +01002508 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker1f10d762019-04-26 13:34:37 +01002509
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002510 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002511 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
Hanno Beckercab87e62019-04-29 13:52:53 +01002512 add_data_len );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002513
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002514 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
Hanno Beckercab87e62019-04-29 13:52:53 +01002515 add_data_len );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002516 mbedtls_md_hmac_update( &transform->md_ctx_enc,
2517 data, rec->data_len );
2518 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
2519 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002520
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002521 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002522
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002523 rec->data_len += transform->maclen;
2524 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002525 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002526 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002527#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00002528 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002529 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002530#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002531 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002532 {
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é-Gonnardf7dc3782013-09-13 14:10:44 +02002535 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002536
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002537 /* Make extra sure authentication was performed, exactly once */
2538 if( auth_done != 1 )
2539 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002540 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2541 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002542 }
2543
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002544 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002545
2546 return( 0 );
2547}
2548
Hanno Beckera18d1322018-01-03 14:27:32 +00002549int mbedtls_ssl_decrypt_buf( mbedtls_ssl_context *ssl,
2550 mbedtls_ssl_transform *transform,
2551 mbedtls_record *rec )
Paul Bakker5121ce52009-01-03 21:22:43 +00002552{
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002553 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002554 mbedtls_cipher_mode_t mode;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002555 int ret, auth_done = 0;
Hanno Becker52344c22018-01-03 15:24:20 +00002556#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Paul Bakker1e5369c2013-12-19 16:40:57 +01002557 size_t padlen = 0, correct = 1;
2558#endif
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002559 unsigned char* data;
Hanno Becker92fb4fa2019-05-20 14:54:26 +01002560 unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_IN_LEN_MAX ];
Hanno Beckercab87e62019-04-29 13:52:53 +01002561 size_t add_data_len;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002562
Hanno Beckera18d1322018-01-03 14:27:32 +00002563#if !defined(MBEDTLS_DEBUG_C)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002564 ((void) ssl);
2565#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002566
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002567 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002568 if( transform == NULL )
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002569 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002570 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no transform provided to decrypt_buf" ) );
2571 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2572 }
2573 if( rec == NULL ||
2574 rec->buf == NULL ||
2575 rec->buf_len < rec->data_offset ||
2576 rec->buf_len - rec->data_offset < rec->data_len )
2577 {
2578 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to decrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002579 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002580 }
2581
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002582 data = rec->buf + rec->data_offset;
2583 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_dec );
Paul Bakker5121ce52009-01-03 21:22:43 +00002584
Hanno Beckera0e20d02019-05-15 14:03:01 +01002585#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckercab87e62019-04-29 13:52:53 +01002586 /*
2587 * Match record's CID with incoming CID.
2588 */
Hanno Becker938489a2019-05-08 13:02:22 +01002589 if( rec->cid_len != transform->in_cid_len ||
2590 memcmp( rec->cid, transform->in_cid, rec->cid_len ) != 0 )
2591 {
Hanno Becker8367ccc2019-05-14 11:30:10 +01002592 return( MBEDTLS_ERR_SSL_UNEXPECTED_CID );
Hanno Becker938489a2019-05-08 13:02:22 +01002593 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01002594#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckercab87e62019-04-29 13:52:53 +01002595
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002596#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
2597 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01002598 {
2599 padlen = 0;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002600 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
2601 transform->iv_dec,
2602 transform->ivlen,
2603 data, rec->data_len,
2604 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002605 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002606 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002607 return( ret );
2608 }
2609
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002610 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002611 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002612 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2613 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002614 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002615 }
Paul Bakker68884e32013-01-07 18:20:04 +01002616 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002617#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002618#if defined(MBEDTLS_GCM_C) || \
2619 defined(MBEDTLS_CCM_C) || \
2620 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002621 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002622 mode == MBEDTLS_MODE_CCM ||
2623 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002624 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002625 unsigned char iv[12];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002626 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002627
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002628 /*
2629 * Compute and update sizes
2630 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002631 if( rec->data_len < explicit_iv_len + transform->taglen )
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002632 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002633 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002634 "+ taglen (%d)", rec->data_len,
2635 explicit_iv_len, transform->taglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002636 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002637 }
Paul Bakker68884e32013-01-07 18:20:04 +01002638
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002639 /*
2640 * Prepare IV
2641 */
2642 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
2643 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002644 /* GCM and CCM: fixed || explicit (transmitted) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002645 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002646 memcpy( iv + transform->fixed_ivlen, data, 8 );
Paul Bakker68884e32013-01-07 18:20:04 +01002647
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002648 }
2649 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
2650 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002651 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002652 unsigned char i;
2653
2654 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
2655
2656 for( i = 0; i < 8; i++ )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002657 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002658 }
2659 else
2660 {
2661 /* Reminder if we ever add an AEAD mode with a different size */
2662 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2663 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2664 }
2665
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002666 data += explicit_iv_len;
2667 rec->data_offset += explicit_iv_len;
2668 rec->data_len -= explicit_iv_len + transform->taglen;
2669
Hanno Beckercab87e62019-04-29 13:52:53 +01002670 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002671 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Hanno Beckercab87e62019-04-29 13:52:53 +01002672 add_data, add_data_len );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002673
2674 memcpy( transform->iv_dec + transform->fixed_ivlen,
2675 data - explicit_iv_len, explicit_iv_len );
2676
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002677 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used", iv, transform->ivlen );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002678 MBEDTLS_SSL_DEBUG_BUF( 4, "TAG used", data + rec->data_len,
Hanno Beckere694c3e2017-12-27 21:34:08 +00002679 transform->taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01002680
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002681
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002682 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002683 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002684 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002685 if( ( ret = mbedtls_cipher_auth_decrypt( &transform->cipher_ctx_dec,
2686 iv, transform->ivlen,
Hanno Beckercab87e62019-04-29 13:52:53 +01002687 add_data, add_data_len,
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002688 data, rec->data_len,
2689 data, &olen,
2690 data + rec->data_len,
2691 transform->taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002692 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002693 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_decrypt", ret );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002694
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002695 if( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED )
2696 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002697
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002698 return( ret );
2699 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002700 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002701
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002702 if( olen != rec->data_len )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002703 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002704 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2705 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002706 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00002707 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002708 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002709#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2710#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002711 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002712 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00002713 {
Paul Bakkere47b34b2013-02-27 14:48:00 +01002714 size_t minlen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002715
Paul Bakker5121ce52009-01-03 21:22:43 +00002716 /*
Paul Bakker45829992013-01-03 14:52:21 +01002717 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00002718 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002719#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002720 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
2721 {
2722 /* The ciphertext is prefixed with the CBC IV. */
2723 minlen += transform->ivlen;
2724 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002725#endif
Paul Bakker45829992013-01-03 14:52:21 +01002726
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002727 /* Size considerations:
2728 *
2729 * - The CBC cipher text must not be empty and hence
2730 * at least of size transform->ivlen.
2731 *
2732 * Together with the potential IV-prefix, this explains
2733 * the first of the two checks below.
2734 *
2735 * - The record must contain a MAC, either in plain or
2736 * encrypted, depending on whether Encrypt-then-MAC
2737 * is used or not.
2738 * - If it is, the message contains the IV-prefix,
2739 * the CBC ciphertext, and the MAC.
2740 * - If it is not, the padded plaintext, and hence
2741 * the CBC ciphertext, has at least length maclen + 1
2742 * because there is at least the padding length byte.
2743 *
2744 * As the CBC ciphertext is not empty, both cases give the
2745 * lower bound minlen + maclen + 1 on the record size, which
2746 * we test for in the second check below.
2747 */
2748 if( rec->data_len < minlen + transform->ivlen ||
2749 rec->data_len < minlen + transform->maclen + 1 )
Paul Bakker45829992013-01-03 14:52:21 +01002750 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002751 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002752 "+ 1 ) ( + expl IV )", rec->data_len,
2753 transform->ivlen,
2754 transform->maclen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002755 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker45829992013-01-03 14:52:21 +01002756 }
2757
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002758 /*
2759 * Authenticate before decrypt if enabled
2760 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002761#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002762 if( transform->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002763 {
Hanno Becker992b6872017-11-09 18:57:39 +00002764 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002765
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002766 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002767
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002768 /* Safe due to the check data_len >= minlen + maclen + 1 above. */
2769 rec->data_len -= transform->maclen;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002770
Hanno Beckercab87e62019-04-29 13:52:53 +01002771 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002772
Hanno Beckercab87e62019-04-29 13:52:53 +01002773 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
2774 add_data_len );
2775 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data,
2776 add_data_len );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002777 mbedtls_md_hmac_update( &transform->md_ctx_dec,
2778 data, rec->data_len );
2779 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
2780 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002781
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002782 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len,
2783 transform->maclen );
Hanno Becker992b6872017-11-09 18:57:39 +00002784 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect,
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002785 transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002786
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002787 if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect,
2788 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002789 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002790 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002791 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002792 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002793 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002794 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002795#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002796
2797 /*
2798 * Check length sanity
2799 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002800 if( rec->data_len % transform->ivlen != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002801 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002802 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002803 rec->data_len, transform->ivlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002804 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002805 }
2806
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002807#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002808 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00002809 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002810 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002811 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002812 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002813 /* This is safe because data_len >= minlen + maclen + 1 initially,
2814 * and at this point we have at most subtracted maclen (note that
2815 * minlen == transform->ivlen here). */
2816 memcpy( transform->iv_dec, data, transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002817
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002818 data += transform->ivlen;
2819 rec->data_offset += transform->ivlen;
2820 rec->data_len -= transform->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002821 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002822#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002823
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002824 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
2825 transform->iv_dec, transform->ivlen,
2826 data, rec->data_len, data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002827 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002828 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002829 return( ret );
2830 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002831
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002832 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02002833 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002834 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2835 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002836 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002837
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002838#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002839 if( transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02002840 {
2841 /*
2842 * Save IV in SSL3 and TLS1
2843 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002844 memcpy( transform->iv_dec, transform->cipher_ctx_dec.iv,
2845 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002846 }
Paul Bakkercca5b812013-08-31 17:40:26 +02002847#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002848
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002849 /* Safe since data_len >= minlen + maclen + 1, so after having
2850 * subtracted at most minlen and maclen up to this point,
2851 * data_len > 0. */
2852 padlen = data[rec->data_len - 1];
Paul Bakker45829992013-01-03 14:52:21 +01002853
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002854 if( auth_done == 1 )
2855 {
2856 correct *= ( rec->data_len >= padlen + 1 );
2857 padlen *= ( rec->data_len >= padlen + 1 );
2858 }
2859 else
Paul Bakker45829992013-01-03 14:52:21 +01002860 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002861#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002862 if( rec->data_len < transform->maclen + padlen + 1 )
2863 {
2864 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
2865 rec->data_len,
2866 transform->maclen,
2867 padlen + 1 ) );
2868 }
Paul Bakkerd66f0702013-01-31 16:57:45 +01002869#endif
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002870
2871 correct *= ( rec->data_len >= transform->maclen + padlen + 1 );
2872 padlen *= ( rec->data_len >= transform->maclen + padlen + 1 );
Paul Bakker45829992013-01-03 14:52:21 +01002873 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002874
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002875 padlen++;
2876
2877 /* Regardless of the validity of the padding,
2878 * we have data_len >= padlen here. */
2879
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002880#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002881 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002882 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002883 if( padlen > transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002884 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002885#if defined(MBEDTLS_SSL_DEBUG_ALL)
2886 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002887 "should be no more than %d",
2888 padlen, transform->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002889#endif
Paul Bakker45829992013-01-03 14:52:21 +01002890 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002891 }
2892 }
2893 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002894#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2895#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2896 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002897 if( transform->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002898 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002899 /* The padding check involves a series of up to 256
2900 * consecutive memory reads at the end of the record
2901 * plaintext buffer. In order to hide the length and
2902 * validity of the padding, always perform exactly
2903 * `min(256,plaintext_len)` reads (but take into account
2904 * only the last `padlen` bytes for the padding check). */
2905 size_t pad_count = 0;
2906 size_t real_count = 0;
2907 volatile unsigned char* const check = data;
Paul Bakkere47b34b2013-02-27 14:48:00 +01002908
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002909 /* Index of first padding byte; it has been ensured above
2910 * that the subtraction is safe. */
2911 size_t const padding_idx = rec->data_len - padlen;
2912 size_t const num_checks = rec->data_len <= 256 ? rec->data_len : 256;
2913 size_t const start_idx = rec->data_len - num_checks;
2914 size_t idx;
Paul Bakker956c9e02013-12-19 14:42:28 +01002915
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002916 for( idx = start_idx; idx < rec->data_len; idx++ )
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002917 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002918 real_count |= ( idx >= padding_idx );
2919 pad_count += real_count * ( check[idx] == padlen - 1 );
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002920 }
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002921 correct &= ( pad_count == padlen );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002922
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002923#if defined(MBEDTLS_SSL_DEBUG_ALL)
Paul Bakker66d5d072014-06-17 16:39:18 +02002924 if( padlen > 0 && correct == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002925 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002926#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01002927 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00002928 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002929 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002930#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
2931 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02002932 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002933 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2934 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02002935 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002936
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002937 /* If the padding was found to be invalid, padlen == 0
2938 * and the subtraction is safe. If the padding was found valid,
2939 * padlen hasn't been changed and the previous assertion
2940 * data_len >= padlen still holds. */
2941 rec->data_len -= padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002942 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002943 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002944#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002945 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002946 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002947 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2948 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002949 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002950
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02002951#if defined(MBEDTLS_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002952 MBEDTLS_SSL_DEBUG_BUF( 4, "raw buffer after decryption",
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002953 data, rec->data_len );
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02002954#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002955
2956 /*
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002957 * Authenticate if not done yet.
2958 * Compute the MAC regardless of the padding result (RFC4346, CBCTIME).
Paul Bakker5121ce52009-01-03 21:22:43 +00002959 */
Hanno Becker52344c22018-01-03 15:24:20 +00002960#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002961 if( auth_done == 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002962 {
Hanno Becker992b6872017-11-09 18:57:39 +00002963 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Paul Bakker1e5369c2013-12-19 16:40:57 +01002964
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002965 /* If the initial value of padlen was such that
2966 * data_len < maclen + padlen + 1, then padlen
2967 * got reset to 1, and the initial check
2968 * data_len >= minlen + maclen + 1
2969 * guarantees that at this point we still
2970 * have at least data_len >= maclen.
2971 *
2972 * If the initial value of padlen was such that
2973 * data_len >= maclen + padlen + 1, then we have
2974 * subtracted either padlen + 1 (if the padding was correct)
2975 * or 0 (if the padding was incorrect) since then,
2976 * hence data_len >= maclen in any case.
2977 */
2978 rec->data_len -= transform->maclen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002979
Hanno Beckercab87e62019-04-29 13:52:53 +01002980 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Paul Bakker5121ce52009-01-03 21:22:43 +00002981
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002982#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002983 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002984 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002985 ssl_mac( &transform->md_ctx_dec,
2986 transform->mac_dec,
2987 data, rec->data_len,
2988 rec->ctr, rec->type,
2989 mac_expect );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002990 }
2991 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002992#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2993#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2994 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002995 if( transform->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002996 {
2997 /*
2998 * Process MAC and always update for padlen afterwards to make
Gilles Peskine20b44082018-05-29 14:06:49 +02002999 * total time independent of padlen.
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003000 *
3001 * Known timing attacks:
3002 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
3003 *
Gilles Peskine20b44082018-05-29 14:06:49 +02003004 * To compensate for different timings for the MAC calculation
3005 * depending on how much padding was removed (which is determined
3006 * by padlen), process extra_run more blocks through the hash
3007 * function.
3008 *
3009 * The formula in the paper is
3010 * extra_run = ceil( (L1-55) / 64 ) - ceil( (L2-55) / 64 )
3011 * where L1 is the size of the header plus the decrypted message
3012 * plus CBC padding and L2 is the size of the header plus the
3013 * decrypted message. This is for an underlying hash function
3014 * with 64-byte blocks.
3015 * We use ( (Lx+8) / 64 ) to handle 'negative Lx' values
3016 * correctly. We round down instead of up, so -56 is the correct
3017 * value for our calculations instead of -55.
3018 *
Gilles Peskine1bd9d582018-06-04 11:58:44 +02003019 * Repeat the formula rather than defining a block_size variable.
3020 * This avoids requiring division by a variable at runtime
3021 * (which would be marginally less efficient and would require
3022 * linking an extra division function in some builds).
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003023 */
3024 size_t j, extra_run = 0;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003025 unsigned char tmp[MBEDTLS_MD_MAX_BLOCK_SIZE];
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003026
3027 /*
3028 * The next two sizes are the minimum and maximum values of
3029 * in_msglen over all padlen values.
3030 *
3031 * They're independent of padlen, since we previously did
3032 * in_msglen -= padlen.
3033 *
3034 * Note that max_len + maclen is never more than the buffer
3035 * length, as we previously did in_msglen -= maclen too.
3036 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003037 const size_t max_len = rec->data_len + padlen;
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003038 const size_t min_len = ( max_len > 256 ) ? max_len - 256 : 0;
3039
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003040 memset( tmp, 0, sizeof( tmp ) );
3041
3042 switch( mbedtls_md_get_type( transform->md_ctx_dec.md_info ) )
Gilles Peskine20b44082018-05-29 14:06:49 +02003043 {
Gilles Peskined0e55a42018-06-04 12:03:30 +02003044#if defined(MBEDTLS_MD5_C) || defined(MBEDTLS_SHA1_C) || \
3045 defined(MBEDTLS_SHA256_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02003046 case MBEDTLS_MD_MD5:
3047 case MBEDTLS_MD_SHA1:
Gilles Peskine20b44082018-05-29 14:06:49 +02003048 case MBEDTLS_MD_SHA256:
Gilles Peskine20b44082018-05-29 14:06:49 +02003049 /* 8 bytes of message size, 64-byte compression blocks */
Hanno Beckercab87e62019-04-29 13:52:53 +01003050 extra_run =
3051 ( add_data_len + rec->data_len + padlen + 8 ) / 64 -
3052 ( add_data_len + rec->data_len + 8 ) / 64;
Gilles Peskine20b44082018-05-29 14:06:49 +02003053 break;
3054#endif
Gilles Peskinea7fe25d2018-06-04 12:01:18 +02003055#if defined(MBEDTLS_SHA512_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02003056 case MBEDTLS_MD_SHA384:
Gilles Peskine20b44082018-05-29 14:06:49 +02003057 /* 16 bytes of message size, 128-byte compression blocks */
Hanno Beckercab87e62019-04-29 13:52:53 +01003058 extra_run =
3059 ( add_data_len + rec->data_len + padlen + 16 ) / 128 -
3060 ( add_data_len + rec->data_len + 16 ) / 128;
Gilles Peskine20b44082018-05-29 14:06:49 +02003061 break;
3062#endif
3063 default:
Gilles Peskine5c389842018-06-04 12:02:43 +02003064 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Gilles Peskine20b44082018-05-29 14:06:49 +02003065 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3066 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01003067
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003068 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01003069
Hanno Beckercab87e62019-04-29 13:52:53 +01003070 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data,
3071 add_data_len );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003072 mbedtls_md_hmac_update( &transform->md_ctx_dec, data,
3073 rec->data_len );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003074 /* Make sure we access everything even when padlen > 0. This
3075 * makes the synchronisation requirements for just-in-time
3076 * Prime+Probe attacks much tighter and hopefully impractical. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003077 ssl_read_memory( data + rec->data_len, padlen );
3078 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003079
3080 /* Call mbedtls_md_process at least once due to cache attacks
3081 * that observe whether md_process() was called of not */
Manuel Pégourié-Gonnard47fede02015-04-29 01:35:48 +02003082 for( j = 0; j < extra_run + 1; j++ )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003083 mbedtls_md_process( &transform->md_ctx_dec, tmp );
Paul Bakkere47b34b2013-02-27 14:48:00 +01003084
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003085 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003086
3087 /* Make sure we access all the memory that could contain the MAC,
3088 * before we check it in the next code block. This makes the
3089 * synchronisation requirements for just-in-time Prime+Probe
3090 * attacks much tighter and hopefully impractical. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003091 ssl_read_memory( data + min_len,
3092 max_len - min_len + transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003093 }
3094 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003095#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
3096 MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003097 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003098 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3099 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003100 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003101
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003102#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003103 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect, transform->maclen );
3104 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len, transform->maclen );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003105#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003106
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003107 if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect,
3108 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003109 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003110#if defined(MBEDTLS_SSL_DEBUG_ALL)
3111 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01003112#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003113 correct = 0;
3114 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003115 auth_done++;
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003116 }
Hanno Beckerdd3ab132018-10-17 14:43:14 +01003117
3118 /*
3119 * Finally check the correct flag
3120 */
3121 if( correct == 0 )
3122 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Hanno Becker52344c22018-01-03 15:24:20 +00003123#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003124
3125 /* Make extra sure authentication was performed, exactly once */
3126 if( auth_done != 1 )
3127 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003128 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3129 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003130 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003131
Hanno Beckera0e20d02019-05-15 14:03:01 +01003132#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01003133 if( rec->cid_len != 0 )
3134 {
3135 ret = ssl_cid_parse_inner_plaintext( data, &rec->data_len,
3136 &rec->type );
3137 if( ret != 0 )
3138 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3139 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01003140#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01003141
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003142 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003143
3144 return( 0 );
3145}
3146
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01003147#undef MAC_NONE
3148#undef MAC_PLAINTEXT
3149#undef MAC_CIPHERTEXT
3150
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003151#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00003152/*
3153 * Compression/decompression functions
3154 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003155static int ssl_compress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003156{
3157 int ret;
3158 unsigned char *msg_post = ssl->out_msg;
Andrzej Kurek5462e022018-04-20 07:58:53 -04003159 ptrdiff_t bytes_written = ssl->out_msg - ssl->out_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003160 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02003161 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003162
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003163 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003164
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02003165 if( len_pre == 0 )
3166 return( 0 );
3167
Paul Bakker2770fbd2012-07-03 13:30:23 +00003168 memcpy( msg_pre, ssl->out_msg, len_pre );
3169
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003170 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003171 ssl->out_msglen ) );
3172
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003173 MBEDTLS_SSL_DEBUG_BUF( 4, "before compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003174 ssl->out_msg, ssl->out_msglen );
3175
Paul Bakker48916f92012-09-16 19:57:18 +00003176 ssl->transform_out->ctx_deflate.next_in = msg_pre;
3177 ssl->transform_out->ctx_deflate.avail_in = len_pre;
3178 ssl->transform_out->ctx_deflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10003179 ssl->transform_out->ctx_deflate.avail_out = MBEDTLS_SSL_OUT_BUFFER_LEN - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003180
Paul Bakker48916f92012-09-16 19:57:18 +00003181 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003182 if( ret != Z_OK )
3183 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003184 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
3185 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003186 }
3187
Angus Grattond8213d02016-05-25 20:56:48 +10003188 ssl->out_msglen = MBEDTLS_SSL_OUT_BUFFER_LEN -
Andrzej Kurek5462e022018-04-20 07:58:53 -04003189 ssl->transform_out->ctx_deflate.avail_out - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003190
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003191 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003192 ssl->out_msglen ) );
3193
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003194 MBEDTLS_SSL_DEBUG_BUF( 4, "after compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003195 ssl->out_msg, ssl->out_msglen );
3196
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003197 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003198
3199 return( 0 );
3200}
3201
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003202static int ssl_decompress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003203{
3204 int ret;
3205 unsigned char *msg_post = ssl->in_msg;
Andrzej Kureka9ceef82018-04-24 06:32:44 -04003206 ptrdiff_t header_bytes = ssl->in_msg - ssl->in_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003207 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02003208 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003209
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003210 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003211
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02003212 if( len_pre == 0 )
3213 return( 0 );
3214
Paul Bakker2770fbd2012-07-03 13:30:23 +00003215 memcpy( msg_pre, ssl->in_msg, len_pre );
3216
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003217 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003218 ssl->in_msglen ) );
3219
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003220 MBEDTLS_SSL_DEBUG_BUF( 4, "before decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003221 ssl->in_msg, ssl->in_msglen );
3222
Paul Bakker48916f92012-09-16 19:57:18 +00003223 ssl->transform_in->ctx_inflate.next_in = msg_pre;
3224 ssl->transform_in->ctx_inflate.avail_in = len_pre;
3225 ssl->transform_in->ctx_inflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10003226 ssl->transform_in->ctx_inflate.avail_out = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04003227 header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003228
Paul Bakker48916f92012-09-16 19:57:18 +00003229 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003230 if( ret != Z_OK )
3231 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003232 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
3233 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003234 }
3235
Angus Grattond8213d02016-05-25 20:56:48 +10003236 ssl->in_msglen = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04003237 ssl->transform_in->ctx_inflate.avail_out - header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003238
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003239 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003240 ssl->in_msglen ) );
3241
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003242 MBEDTLS_SSL_DEBUG_BUF( 4, "after decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003243 ssl->in_msg, ssl->in_msglen );
3244
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003245 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003246
3247 return( 0 );
3248}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003249#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003250
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003251#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
3252static int ssl_write_hello_request( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003253
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003254#if defined(MBEDTLS_SSL_PROTO_DTLS)
3255static int ssl_resend_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003256{
3257 /* If renegotiation is not enforced, retransmit until we would reach max
3258 * timeout if we were using the usual handshake doubling scheme */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003259 if( ssl->conf->renego_max_records < 0 )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003260 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003261 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003262 unsigned char doublings = 1;
3263
3264 while( ratio != 0 )
3265 {
3266 ++doublings;
3267 ratio >>= 1;
3268 }
3269
3270 if( ++ssl->renego_records_seen > doublings )
3271 {
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02003272 MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003273 return( 0 );
3274 }
3275 }
3276
3277 return( ssl_write_hello_request( ssl ) );
3278}
3279#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003280#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003281
Paul Bakker5121ce52009-01-03 21:22:43 +00003282/*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003283 * Fill the input message buffer by appending data to it.
3284 * The amount of data already fetched is in ssl->in_left.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003285 *
3286 * If we return 0, is it guaranteed that (at least) nb_want bytes are
3287 * available (from this read and/or a previous one). Otherwise, an error code
3288 * is returned (possibly EOF or WANT_READ).
3289 *
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003290 * With stream transport (TLS) on success ssl->in_left == nb_want, but
3291 * with datagram transport (DTLS) on success ssl->in_left >= nb_want,
3292 * since we always read a whole datagram at once.
3293 *
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02003294 * For DTLS, it is up to the caller to set ssl->next_record_offset when
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003295 * they're done reading a record.
Paul Bakker5121ce52009-01-03 21:22:43 +00003296 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003297int mbedtls_ssl_fetch_input( mbedtls_ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00003298{
Paul Bakker23986e52011-04-24 08:57:21 +00003299 int ret;
3300 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00003301
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003302 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003303
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003304 if( ssl->f_recv == NULL && ssl->f_recv_timeout == NULL )
3305 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003306 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01003307 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003308 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003309 }
3310
Angus Grattond8213d02016-05-25 20:56:48 +10003311 if( nb_want > MBEDTLS_SSL_IN_BUFFER_LEN - (size_t)( ssl->in_hdr - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003312 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003313 MBEDTLS_SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
3314 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003315 }
3316
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003317#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003318 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00003319 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003320 uint32_t timeout;
3321
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02003322 /* Just to be sure */
3323 if( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL )
3324 {
3325 MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
3326 "mbedtls_ssl_set_timer_cb() for DTLS" ) );
3327 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3328 }
3329
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003330 /*
3331 * The point is, we need to always read a full datagram at once, so we
3332 * sometimes read more then requested, and handle the additional data.
3333 * It could be the rest of the current record (while fetching the
3334 * header) and/or some other records in the same datagram.
3335 */
3336
3337 /*
3338 * Move to the next record in the already read datagram if applicable
3339 */
3340 if( ssl->next_record_offset != 0 )
3341 {
3342 if( ssl->in_left < ssl->next_record_offset )
3343 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003344 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3345 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003346 }
3347
3348 ssl->in_left -= ssl->next_record_offset;
3349
3350 if( ssl->in_left != 0 )
3351 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003352 MBEDTLS_SSL_DEBUG_MSG( 2, ( "next record in same datagram, offset: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003353 ssl->next_record_offset ) );
3354 memmove( ssl->in_hdr,
3355 ssl->in_hdr + ssl->next_record_offset,
3356 ssl->in_left );
3357 }
3358
3359 ssl->next_record_offset = 0;
3360 }
3361
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003362 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Paul Bakker5121ce52009-01-03 21:22:43 +00003363 ssl->in_left, nb_want ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003364
3365 /*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003366 * Done if we already have enough data.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003367 */
3368 if( nb_want <= ssl->in_left)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003369 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003370 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003371 return( 0 );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003372 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003373
3374 /*
Antonin Décimo36e89b52019-01-23 15:24:37 +01003375 * A record can't be split across datagrams. If we need to read but
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003376 * are not at the beginning of a new record, the caller did something
3377 * wrong.
3378 */
3379 if( ssl->in_left != 0 )
3380 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003381 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3382 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003383 }
3384
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003385 /*
3386 * Don't even try to read if time's out already.
3387 * This avoids by-passing the timer when repeatedly receiving messages
3388 * that will end up being dropped.
3389 */
3390 if( ssl_check_timer( ssl ) != 0 )
Hanno Beckere65ce782017-05-22 14:47:48 +01003391 {
3392 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timer has expired" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003393 ret = MBEDTLS_ERR_SSL_TIMEOUT;
Hanno Beckere65ce782017-05-22 14:47:48 +01003394 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003395 else
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003396 {
Angus Grattond8213d02016-05-25 20:56:48 +10003397 len = MBEDTLS_SSL_IN_BUFFER_LEN - ( ssl->in_hdr - ssl->in_buf );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003398
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003399 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003400 timeout = ssl->handshake->retransmit_timeout;
3401 else
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003402 timeout = ssl->conf->read_timeout;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003403
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003404 MBEDTLS_SSL_DEBUG_MSG( 3, ( "f_recv_timeout: %u ms", timeout ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003405
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003406 if( ssl->f_recv_timeout != NULL )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003407 ret = ssl->f_recv_timeout( ssl->p_bio, ssl->in_hdr, len,
3408 timeout );
3409 else
3410 ret = ssl->f_recv( ssl->p_bio, ssl->in_hdr, len );
3411
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003412 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003413
3414 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003415 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003416 }
3417
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003418 if( ret == MBEDTLS_ERR_SSL_TIMEOUT )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003419 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003420 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timeout" ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003421 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003422
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003423 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003424 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003425 if( ssl_double_retransmit_timeout( ssl ) != 0 )
3426 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003427 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake timeout" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003428 return( MBEDTLS_ERR_SSL_TIMEOUT );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003429 }
3430
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003431 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003432 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003433 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003434 return( ret );
3435 }
3436
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003437 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003438 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003439#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003440 else if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003441 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003442 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003443 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003444 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003445 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003446 return( ret );
3447 }
3448
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003449 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003450 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003451#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003452 }
3453
Paul Bakker5121ce52009-01-03 21:22:43 +00003454 if( ret < 0 )
3455 return( ret );
3456
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003457 ssl->in_left = ret;
3458 }
3459 else
3460#endif
3461 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003462 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003463 ssl->in_left, nb_want ) );
3464
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003465 while( ssl->in_left < nb_want )
3466 {
3467 len = nb_want - ssl->in_left;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02003468
3469 if( ssl_check_timer( ssl ) != 0 )
3470 ret = MBEDTLS_ERR_SSL_TIMEOUT;
3471 else
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003472 {
3473 if( ssl->f_recv_timeout != NULL )
3474 {
3475 ret = ssl->f_recv_timeout( ssl->p_bio,
3476 ssl->in_hdr + ssl->in_left, len,
3477 ssl->conf->read_timeout );
3478 }
3479 else
3480 {
3481 ret = ssl->f_recv( ssl->p_bio,
3482 ssl->in_hdr + ssl->in_left, len );
3483 }
3484 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003485
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003486 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003487 ssl->in_left, nb_want ) );
3488 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003489
3490 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003491 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003492
3493 if( ret < 0 )
3494 return( ret );
3495
mohammad160352aecb92018-03-28 23:41:40 -07003496 if ( (size_t)ret > len || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16035bd15cb2018-02-28 04:30:59 -08003497 {
Darryl Green11999bb2018-03-13 15:22:58 +00003498 MBEDTLS_SSL_DEBUG_MSG( 1,
3499 ( "f_recv returned %d bytes but only %lu were requested",
mohammad160319d392b2018-04-02 07:25:26 -07003500 ret, (unsigned long)len ) );
mohammad16035bd15cb2018-02-28 04:30:59 -08003501 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3502 }
3503
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003504 ssl->in_left += ret;
3505 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003506 }
3507
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003508 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003509
3510 return( 0 );
3511}
3512
3513/*
3514 * Flush any data not yet written
3515 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003516int mbedtls_ssl_flush_output( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003517{
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01003518 int ret;
Hanno Becker04484622018-08-06 09:49:38 +01003519 unsigned char *buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00003520
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003521 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003522
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003523 if( ssl->f_send == NULL )
3524 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003525 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01003526 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003527 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003528 }
3529
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003530 /* Avoid incrementing counter if data is flushed */
3531 if( ssl->out_left == 0 )
3532 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003533 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003534 return( 0 );
3535 }
3536
Paul Bakker5121ce52009-01-03 21:22:43 +00003537 while( ssl->out_left > 0 )
3538 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003539 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
Hanno Becker5903de42019-05-03 14:46:38 +01003540 mbedtls_ssl_out_hdr_len( ssl ) + ssl->out_msglen, ssl->out_left ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003541
Hanno Becker2b1e3542018-08-06 11:19:13 +01003542 buf = ssl->out_hdr - ssl->out_left;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003543 ret = ssl->f_send( ssl->p_bio, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00003544
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003545 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003546
3547 if( ret <= 0 )
3548 return( ret );
3549
mohammad160352aecb92018-03-28 23:41:40 -07003550 if( (size_t)ret > ssl->out_left || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16034bbaeb42018-02-22 04:29:04 -08003551 {
Darryl Green11999bb2018-03-13 15:22:58 +00003552 MBEDTLS_SSL_DEBUG_MSG( 1,
3553 ( "f_send returned %d bytes but only %lu bytes were sent",
mohammad160319d392b2018-04-02 07:25:26 -07003554 ret, (unsigned long)ssl->out_left ) );
mohammad16034bbaeb42018-02-22 04:29:04 -08003555 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3556 }
3557
Paul Bakker5121ce52009-01-03 21:22:43 +00003558 ssl->out_left -= ret;
3559 }
3560
Hanno Becker2b1e3542018-08-06 11:19:13 +01003561#if defined(MBEDTLS_SSL_PROTO_DTLS)
3562 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003563 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01003564 ssl->out_hdr = ssl->out_buf;
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003565 }
Hanno Becker2b1e3542018-08-06 11:19:13 +01003566 else
3567#endif
3568 {
3569 ssl->out_hdr = ssl->out_buf + 8;
3570 }
3571 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003572
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003573 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003574
3575 return( 0 );
3576}
3577
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003578/*
3579 * Functions to handle the DTLS retransmission state machine
3580 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003581#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003582/*
3583 * Append current handshake message to current outgoing flight
3584 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003585static int ssl_flight_append( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003586{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003587 mbedtls_ssl_flight_item *msg;
Hanno Becker3b235902018-08-06 09:54:53 +01003588 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_flight_append" ) );
3589 MBEDTLS_SSL_DEBUG_BUF( 4, "message appended to flight",
3590 ssl->out_msg, ssl->out_msglen );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003591
3592 /* Allocate space for current message */
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003593 if( ( msg = mbedtls_calloc( 1, sizeof( mbedtls_ssl_flight_item ) ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003594 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003595 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003596 sizeof( mbedtls_ssl_flight_item ) ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003597 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003598 }
3599
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003600 if( ( msg->p = mbedtls_calloc( 1, ssl->out_msglen ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003601 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003602 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed", ssl->out_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003603 mbedtls_free( msg );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003604 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003605 }
3606
3607 /* Copy current handshake message with headers */
3608 memcpy( msg->p, ssl->out_msg, ssl->out_msglen );
3609 msg->len = ssl->out_msglen;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003610 msg->type = ssl->out_msgtype;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003611 msg->next = NULL;
3612
3613 /* Append to the current flight */
3614 if( ssl->handshake->flight == NULL )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003615 ssl->handshake->flight = msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003616 else
3617 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003618 mbedtls_ssl_flight_item *cur = ssl->handshake->flight;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003619 while( cur->next != NULL )
3620 cur = cur->next;
3621 cur->next = msg;
3622 }
3623
Hanno Becker3b235902018-08-06 09:54:53 +01003624 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_flight_append" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003625 return( 0 );
3626}
3627
3628/*
3629 * Free the current flight of handshake messages
3630 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003631static void ssl_flight_free( mbedtls_ssl_flight_item *flight )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003632{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003633 mbedtls_ssl_flight_item *cur = flight;
3634 mbedtls_ssl_flight_item *next;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003635
3636 while( cur != NULL )
3637 {
3638 next = cur->next;
3639
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003640 mbedtls_free( cur->p );
3641 mbedtls_free( cur );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003642
3643 cur = next;
3644 }
3645}
3646
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003647#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3648static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003649#endif
3650
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003651/*
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003652 * Swap transform_out and out_ctr with the alternative ones
3653 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003654static void ssl_swap_epochs( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003655{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003656 mbedtls_ssl_transform *tmp_transform;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003657 unsigned char tmp_out_ctr[8];
3658
3659 if( ssl->transform_out == ssl->handshake->alt_transform_out )
3660 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003661 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003662 return;
3663 }
3664
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003665 MBEDTLS_SSL_DEBUG_MSG( 3, ( "swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003666
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003667 /* Swap transforms */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003668 tmp_transform = ssl->transform_out;
3669 ssl->transform_out = ssl->handshake->alt_transform_out;
3670 ssl->handshake->alt_transform_out = tmp_transform;
3671
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003672 /* Swap epoch + sequence_number */
Hanno Becker19859472018-08-06 09:40:20 +01003673 memcpy( tmp_out_ctr, ssl->cur_out_ctr, 8 );
3674 memcpy( ssl->cur_out_ctr, ssl->handshake->alt_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003675 memcpy( ssl->handshake->alt_out_ctr, tmp_out_ctr, 8 );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003676
3677 /* Adjust to the newly activated transform */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01003678 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003679
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003680#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3681 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003682 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003683 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003684 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003685 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
3686 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003687 }
3688 }
3689#endif
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003690}
3691
3692/*
3693 * Retransmit the current flight of messages.
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003694 */
3695int mbedtls_ssl_resend( mbedtls_ssl_context *ssl )
3696{
3697 int ret = 0;
3698
3699 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_resend" ) );
3700
3701 ret = mbedtls_ssl_flight_transmit( ssl );
3702
3703 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_resend" ) );
3704
3705 return( ret );
3706}
3707
3708/*
3709 * Transmit or retransmit the current flight of messages.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003710 *
3711 * Need to remember the current message in case flush_output returns
3712 * WANT_WRITE, causing us to exit this function and come back later.
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003713 * This function must be called until state is no longer SENDING.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003714 */
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003715int mbedtls_ssl_flight_transmit( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003716{
Hanno Becker67bc7c32018-08-06 11:33:50 +01003717 int ret;
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003718 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003719
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003720 if( ssl->handshake->retransmit_state != MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003721 {
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003722 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialise flight transmission" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003723
3724 ssl->handshake->cur_msg = ssl->handshake->flight;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003725 ssl->handshake->cur_msg_p = ssl->handshake->flight->p + 12;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003726 ssl_swap_epochs( ssl );
3727
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003728 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_SENDING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003729 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003730
3731 while( ssl->handshake->cur_msg != NULL )
3732 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003733 size_t max_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003734 const mbedtls_ssl_flight_item * const cur = ssl->handshake->cur_msg;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003735
Hanno Beckere1dcb032018-08-17 16:47:58 +01003736 int const is_finished =
3737 ( cur->type == MBEDTLS_SSL_MSG_HANDSHAKE &&
3738 cur->p[0] == MBEDTLS_SSL_HS_FINISHED );
3739
Hanno Becker04da1892018-08-14 13:22:10 +01003740 uint8_t const force_flush = ssl->disable_datagram_packing == 1 ?
3741 SSL_FORCE_FLUSH : SSL_DONT_FORCE_FLUSH;
3742
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003743 /* Swap epochs before sending Finished: we can't do it after
3744 * sending ChangeCipherSpec, in case write returns WANT_READ.
3745 * Must be done before copying, may change out_msg pointer */
Hanno Beckere1dcb032018-08-17 16:47:58 +01003746 if( is_finished && ssl->handshake->cur_msg_p == ( cur->p + 12 ) )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003747 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003748 MBEDTLS_SSL_DEBUG_MSG( 2, ( "swap epochs to send finished message" ) );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003749 ssl_swap_epochs( ssl );
3750 }
3751
Hanno Becker67bc7c32018-08-06 11:33:50 +01003752 ret = ssl_get_remaining_payload_in_datagram( ssl );
3753 if( ret < 0 )
3754 return( ret );
3755 max_frag_len = (size_t) ret;
3756
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003757 /* CCS is copied as is, while HS messages may need fragmentation */
3758 if( cur->type == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
3759 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003760 if( max_frag_len == 0 )
3761 {
3762 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3763 return( ret );
3764
3765 continue;
3766 }
3767
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003768 memcpy( ssl->out_msg, cur->p, cur->len );
Hanno Becker67bc7c32018-08-06 11:33:50 +01003769 ssl->out_msglen = cur->len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003770 ssl->out_msgtype = cur->type;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003771
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003772 /* Update position inside current message */
3773 ssl->handshake->cur_msg_p += cur->len;
3774 }
3775 else
3776 {
3777 const unsigned char * const p = ssl->handshake->cur_msg_p;
3778 const size_t hs_len = cur->len - 12;
3779 const size_t frag_off = p - ( cur->p + 12 );
3780 const size_t rem_len = hs_len - frag_off;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003781 size_t cur_hs_frag_len, max_hs_frag_len;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003782
Hanno Beckere1dcb032018-08-17 16:47:58 +01003783 if( ( max_frag_len < 12 ) || ( max_frag_len == 12 && hs_len != 0 ) )
Manuel Pégourié-Gonnarda1071a52018-08-20 11:56:14 +02003784 {
Hanno Beckere1dcb032018-08-17 16:47:58 +01003785 if( is_finished )
Hanno Becker67bc7c32018-08-06 11:33:50 +01003786 ssl_swap_epochs( ssl );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003787
Hanno Becker67bc7c32018-08-06 11:33:50 +01003788 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3789 return( ret );
3790
3791 continue;
3792 }
3793 max_hs_frag_len = max_frag_len - 12;
3794
3795 cur_hs_frag_len = rem_len > max_hs_frag_len ?
3796 max_hs_frag_len : rem_len;
3797
3798 if( frag_off == 0 && cur_hs_frag_len != hs_len )
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003799 {
3800 MBEDTLS_SSL_DEBUG_MSG( 2, ( "fragmenting handshake message (%u > %u)",
Hanno Becker67bc7c32018-08-06 11:33:50 +01003801 (unsigned) cur_hs_frag_len,
3802 (unsigned) max_hs_frag_len ) );
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003803 }
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02003804
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003805 /* Messages are stored with handshake headers as if not fragmented,
3806 * copy beginning of headers then fill fragmentation fields.
3807 * Handshake headers: type(1) len(3) seq(2) f_off(3) f_len(3) */
3808 memcpy( ssl->out_msg, cur->p, 6 );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003809
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003810 ssl->out_msg[6] = ( ( frag_off >> 16 ) & 0xff );
3811 ssl->out_msg[7] = ( ( frag_off >> 8 ) & 0xff );
3812 ssl->out_msg[8] = ( ( frag_off ) & 0xff );
3813
Hanno Becker67bc7c32018-08-06 11:33:50 +01003814 ssl->out_msg[ 9] = ( ( cur_hs_frag_len >> 16 ) & 0xff );
3815 ssl->out_msg[10] = ( ( cur_hs_frag_len >> 8 ) & 0xff );
3816 ssl->out_msg[11] = ( ( cur_hs_frag_len ) & 0xff );
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003817
3818 MBEDTLS_SSL_DEBUG_BUF( 3, "handshake header", ssl->out_msg, 12 );
3819
Hanno Becker3f7b9732018-08-28 09:53:25 +01003820 /* Copy the handshake message content and set records fields */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003821 memcpy( ssl->out_msg + 12, p, cur_hs_frag_len );
3822 ssl->out_msglen = cur_hs_frag_len + 12;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003823 ssl->out_msgtype = cur->type;
3824
3825 /* Update position inside current message */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003826 ssl->handshake->cur_msg_p += cur_hs_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003827 }
3828
3829 /* If done with the current message move to the next one if any */
3830 if( ssl->handshake->cur_msg_p >= cur->p + cur->len )
3831 {
3832 if( cur->next != NULL )
3833 {
3834 ssl->handshake->cur_msg = cur->next;
3835 ssl->handshake->cur_msg_p = cur->next->p + 12;
3836 }
3837 else
3838 {
3839 ssl->handshake->cur_msg = NULL;
3840 ssl->handshake->cur_msg_p = NULL;
3841 }
3842 }
3843
3844 /* Actually send the message out */
Hanno Becker04da1892018-08-14 13:22:10 +01003845 if( ( ret = mbedtls_ssl_write_record( ssl, force_flush ) ) != 0 )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003846 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003847 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003848 return( ret );
3849 }
3850 }
3851
Hanno Becker67bc7c32018-08-06 11:33:50 +01003852 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3853 return( ret );
3854
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003855 /* Update state and set timer */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003856 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
3857 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard23b7b702014-09-25 13:50:12 +02003858 else
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003859 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003860 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003861 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
3862 }
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003863
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003864 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003865
3866 return( 0 );
3867}
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003868
3869/*
3870 * To be called when the last message of an incoming flight is received.
3871 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003872void mbedtls_ssl_recv_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003873{
3874 /* We won't need to resend that one any more */
3875 ssl_flight_free( ssl->handshake->flight );
3876 ssl->handshake->flight = NULL;
3877 ssl->handshake->cur_msg = NULL;
3878
3879 /* The next incoming flight will start with this msg_seq */
3880 ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq;
3881
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003882 /* We don't want to remember CCS's across flight boundaries. */
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01003883 ssl->handshake->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003884
Hanno Becker0271f962018-08-16 13:23:47 +01003885 /* Clear future message buffering structure. */
3886 ssl_buffering_free( ssl );
3887
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02003888 /* Cancel timer */
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003889 ssl_set_timer( ssl, 0 );
3890
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003891 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3892 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003893 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003894 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003895 }
3896 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003897 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003898}
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003899
3900/*
3901 * To be called when the last message of an outgoing flight is send.
3902 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003903void mbedtls_ssl_send_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003904{
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02003905 ssl_reset_retransmit_timeout( ssl );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003906 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003907
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003908 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3909 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003910 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003911 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003912 }
3913 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003914 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003915}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003916#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003917
Paul Bakker5121ce52009-01-03 21:22:43 +00003918/*
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003919 * Handshake layer functions
Paul Bakker5121ce52009-01-03 21:22:43 +00003920 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003921
3922/*
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003923 * Write (DTLS: or queue) current handshake (including CCS) message.
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003924 *
3925 * - fill in handshake headers
3926 * - update handshake checksum
3927 * - DTLS: save message for resending
3928 * - then pass to the record layer
3929 *
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003930 * DTLS: except for HelloRequest, messages are only queued, and will only be
3931 * actually sent when calling flight_transmit() or resend().
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003932 *
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003933 * Inputs:
3934 * - ssl->out_msglen: 4 + actual handshake message len
3935 * (4 is the size of handshake headers for TLS)
3936 * - ssl->out_msg[0]: the handshake type (ClientHello, ServerHello, etc)
3937 * - ssl->out_msg + 4: the handshake message body
3938 *
Manuel Pégourié-Gonnard065a2a32018-08-20 11:09:26 +02003939 * Outputs, ie state before passing to flight_append() or write_record():
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003940 * - ssl->out_msglen: the length of the record contents
3941 * (including handshake headers but excluding record headers)
3942 * - ssl->out_msg: the record contents (handshake headers + content)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003943 */
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003944int mbedtls_ssl_write_handshake_msg( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003945{
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003946 int ret;
3947 const size_t hs_len = ssl->out_msglen - 4;
3948 const unsigned char hs_type = ssl->out_msg[0];
Paul Bakker5121ce52009-01-03 21:22:43 +00003949
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003950 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write handshake message" ) );
3951
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003952 /*
3953 * Sanity checks
3954 */
Hanno Beckerc83d2b32018-08-22 16:05:47 +01003955 if( ssl->out_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003956 ssl->out_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
3957 {
Hanno Beckerc83d2b32018-08-22 16:05:47 +01003958 /* In SSLv3, the client might send a NoCertificate alert. */
3959#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
3960 if( ! ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
3961 ssl->out_msgtype == MBEDTLS_SSL_MSG_ALERT &&
3962 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) )
3963#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
3964 {
3965 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3966 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3967 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003968 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003969
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05003970 /* Whenever we send anything different from a
3971 * HelloRequest we should be in a handshake - double check. */
3972 if( ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3973 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) &&
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003974 ssl->handshake == NULL )
3975 {
3976 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3977 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3978 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003979
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003980#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003981 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003982 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003983 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003984 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003985 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3986 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003987 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003988#endif
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003989
Hanno Beckerb50a2532018-08-06 11:52:54 +01003990 /* Double-check that we did not exceed the bounds
3991 * of the outgoing record buffer.
3992 * This should never fail as the various message
3993 * writing functions must obey the bounds of the
3994 * outgoing record buffer, but better be safe.
3995 *
3996 * Note: We deliberately do not check for the MTU or MFL here.
3997 */
3998 if( ssl->out_msglen > MBEDTLS_SSL_OUT_CONTENT_LEN )
3999 {
4000 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record too large: "
4001 "size %u, maximum %u",
4002 (unsigned) ssl->out_msglen,
4003 (unsigned) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
4004 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4005 }
4006
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004007 /*
4008 * Fill handshake headers
4009 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004010 if( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00004011 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004012 ssl->out_msg[1] = (unsigned char)( hs_len >> 16 );
4013 ssl->out_msg[2] = (unsigned char)( hs_len >> 8 );
4014 ssl->out_msg[3] = (unsigned char)( hs_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00004015
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004016 /*
4017 * DTLS has additional fields in the Handshake layer,
4018 * between the length field and the actual payload:
4019 * uint16 message_seq;
4020 * uint24 fragment_offset;
4021 * uint24 fragment_length;
4022 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004023#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004024 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004025 {
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01004026 /* Make room for the additional DTLS fields */
Angus Grattond8213d02016-05-25 20:56:48 +10004027 if( MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen < 8 )
Hanno Becker9648f8b2017-09-18 10:55:54 +01004028 {
4029 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS handshake message too large: "
4030 "size %u, maximum %u",
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004031 (unsigned) ( hs_len ),
Angus Grattond8213d02016-05-25 20:56:48 +10004032 (unsigned) ( MBEDTLS_SSL_OUT_CONTENT_LEN - 12 ) ) );
Hanno Becker9648f8b2017-09-18 10:55:54 +01004033 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4034 }
4035
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004036 memmove( ssl->out_msg + 12, ssl->out_msg + 4, hs_len );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004037 ssl->out_msglen += 8;
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004038
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02004039 /* Write message_seq and update it, except for HelloRequest */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004040 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02004041 {
Manuel Pégourié-Gonnardd9ba0d92014-09-02 18:30:26 +02004042 ssl->out_msg[4] = ( ssl->handshake->out_msg_seq >> 8 ) & 0xFF;
4043 ssl->out_msg[5] = ( ssl->handshake->out_msg_seq ) & 0xFF;
4044 ++( ssl->handshake->out_msg_seq );
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02004045 }
4046 else
4047 {
4048 ssl->out_msg[4] = 0;
4049 ssl->out_msg[5] = 0;
4050 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01004051
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004052 /* Handshake hashes are computed without fragmentation,
4053 * so set frag_offset = 0 and frag_len = hs_len for now */
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01004054 memset( ssl->out_msg + 6, 0x00, 3 );
4055 memcpy( ssl->out_msg + 9, ssl->out_msg + 1, 3 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004056 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004057#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004058
Hanno Becker0207e532018-08-28 10:28:28 +01004059 /* Update running hashes of handshake messages seen */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004060 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
4061 ssl->handshake->update_checksum( ssl, ssl->out_msg, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00004062 }
4063
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004064 /* Either send now, or just save to be sent (and resent) later */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004065#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004066 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05004067 ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4068 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004069 {
4070 if( ( ret = ssl_flight_append( ssl ) ) != 0 )
4071 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004072 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_flight_append", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004073 return( ret );
4074 }
4075 }
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004076 else
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004077#endif
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004078 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004079 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004080 {
4081 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4082 return( ret );
4083 }
4084 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004085
4086 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write handshake message" ) );
4087
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004088 return( 0 );
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004089}
4090
4091/*
4092 * Record layer functions
4093 */
4094
4095/*
4096 * Write current record.
4097 *
4098 * Uses:
4099 * - ssl->out_msgtype: type of the message (AppData, Handshake, Alert, CCS)
4100 * - ssl->out_msglen: length of the record content (excl headers)
4101 * - ssl->out_msg: record content
4102 */
Hanno Becker67bc7c32018-08-06 11:33:50 +01004103int mbedtls_ssl_write_record( mbedtls_ssl_context *ssl, uint8_t force_flush )
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004104{
4105 int ret, done = 0;
4106 size_t len = ssl->out_msglen;
Hanno Becker67bc7c32018-08-06 11:33:50 +01004107 uint8_t flush = force_flush;
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004108
4109 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write record" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004110
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004111#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00004112 if( ssl->transform_out != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004113 ssl->session_out->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00004114 {
4115 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
4116 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004117 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00004118 return( ret );
4119 }
4120
4121 len = ssl->out_msglen;
4122 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004123#endif /*MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00004124
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004125#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
4126 if( mbedtls_ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004127 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004128 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004129
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004130 ret = mbedtls_ssl_hw_record_write( ssl );
4131 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00004132 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004133 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_write", ret );
4134 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00004135 }
Paul Bakkerc7878112012-12-19 14:41:14 +01004136
4137 if( ret == 0 )
4138 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00004139 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004140#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00004141 if( !done )
4142 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01004143 unsigned i;
4144 size_t protected_record_size;
4145
Hanno Becker6430faf2019-05-08 11:57:13 +01004146 /* Skip writing the record content type to after the encryption,
4147 * as it may change when using the CID extension. */
4148
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004149 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004150 ssl->conf->transport, ssl->out_hdr + 1 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01004151
Hanno Becker19859472018-08-06 09:40:20 +01004152 memcpy( ssl->out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01004153 ssl->out_len[0] = (unsigned char)( len >> 8 );
4154 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00004155
Paul Bakker48916f92012-09-16 19:57:18 +00004156 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00004157 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00004158 mbedtls_record rec;
4159
4160 rec.buf = ssl->out_iv;
4161 rec.buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN -
4162 ( ssl->out_iv - ssl->out_buf );
4163 rec.data_len = ssl->out_msglen;
4164 rec.data_offset = ssl->out_msg - rec.buf;
4165
4166 memcpy( &rec.ctr[0], ssl->out_ctr, 8 );
4167 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
4168 ssl->conf->transport, rec.ver );
4169 rec.type = ssl->out_msgtype;
4170
Hanno Beckera0e20d02019-05-15 14:03:01 +01004171#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker43c24b82019-05-01 09:45:57 +01004172 /* The CID is set by mbedtls_ssl_encrypt_buf(). */
Hanno Beckercab87e62019-04-29 13:52:53 +01004173 rec.cid_len = 0;
Hanno Beckera0e20d02019-05-15 14:03:01 +01004174#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckercab87e62019-04-29 13:52:53 +01004175
Hanno Beckera18d1322018-01-03 14:27:32 +00004176 if( ( ret = mbedtls_ssl_encrypt_buf( ssl, ssl->transform_out, &rec,
Hanno Becker9eddaeb2017-12-27 21:37:21 +00004177 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00004178 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004179 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
Paul Bakker05ef8352012-05-08 09:17:57 +00004180 return( ret );
4181 }
4182
Hanno Becker9eddaeb2017-12-27 21:37:21 +00004183 if( rec.data_offset != 0 )
4184 {
4185 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4186 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4187 }
4188
Hanno Becker6430faf2019-05-08 11:57:13 +01004189 /* Update the record content type and CID. */
4190 ssl->out_msgtype = rec.type;
Hanno Beckera0e20d02019-05-15 14:03:01 +01004191#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID )
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01004192 memcpy( ssl->out_cid, rec.cid, rec.cid_len );
Hanno Beckera0e20d02019-05-15 14:03:01 +01004193#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker78f839d2019-03-14 12:56:23 +00004194 ssl->out_msglen = len = rec.data_len;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00004195 ssl->out_len[0] = (unsigned char)( rec.data_len >> 8 );
4196 ssl->out_len[1] = (unsigned char)( rec.data_len );
Paul Bakker05ef8352012-05-08 09:17:57 +00004197 }
4198
Hanno Becker5903de42019-05-03 14:46:38 +01004199 protected_record_size = len + mbedtls_ssl_out_hdr_len( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01004200
4201#if defined(MBEDTLS_SSL_PROTO_DTLS)
4202 /* In case of DTLS, double-check that we don't exceed
4203 * the remaining space in the datagram. */
4204 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4205 {
Hanno Becker554b0af2018-08-22 20:33:41 +01004206 ret = ssl_get_remaining_space_in_datagram( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01004207 if( ret < 0 )
4208 return( ret );
4209
4210 if( protected_record_size > (size_t) ret )
4211 {
4212 /* Should never happen */
4213 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4214 }
4215 }
4216#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker05ef8352012-05-08 09:17:57 +00004217
Hanno Becker6430faf2019-05-08 11:57:13 +01004218 /* Now write the potentially updated record content type. */
4219 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
4220
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004221 MBEDTLS_SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01004222 "version = [%d:%d], msglen = %d",
4223 ssl->out_hdr[0], ssl->out_hdr[1],
4224 ssl->out_hdr[2], len ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00004225
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004226 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01004227 ssl->out_hdr, protected_record_size );
Hanno Becker2b1e3542018-08-06 11:19:13 +01004228
4229 ssl->out_left += protected_record_size;
4230 ssl->out_hdr += protected_record_size;
4231 ssl_update_out_pointers( ssl, ssl->transform_out );
4232
Hanno Becker04484622018-08-06 09:49:38 +01004233 for( i = 8; i > ssl_ep_len( ssl ); i-- )
4234 if( ++ssl->cur_out_ctr[i - 1] != 0 )
4235 break;
4236
4237 /* The loop goes to its end iff the counter is wrapping */
4238 if( i == ssl_ep_len( ssl ) )
4239 {
4240 MBEDTLS_SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
4241 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
4242 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004243 }
4244
Hanno Becker67bc7c32018-08-06 11:33:50 +01004245#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker47db8772018-08-21 13:32:13 +01004246 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
4247 flush == SSL_DONT_FORCE_FLUSH )
Hanno Becker67bc7c32018-08-06 11:33:50 +01004248 {
Hanno Becker1f5a15d2018-08-21 13:31:31 +01004249 size_t remaining;
4250 ret = ssl_get_remaining_payload_in_datagram( ssl );
4251 if( ret < 0 )
4252 {
4253 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_get_remaining_payload_in_datagram",
4254 ret );
4255 return( ret );
4256 }
4257
4258 remaining = (size_t) ret;
Hanno Becker67bc7c32018-08-06 11:33:50 +01004259 if( remaining == 0 )
Hanno Beckerf0da6672018-08-28 09:55:10 +01004260 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004261 flush = SSL_FORCE_FLUSH;
Hanno Beckerf0da6672018-08-28 09:55:10 +01004262 }
Hanno Becker67bc7c32018-08-06 11:33:50 +01004263 else
4264 {
Hanno Becker513815a2018-08-20 11:56:09 +01004265 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Still %u bytes available in current datagram", (unsigned) remaining ) );
Hanno Becker67bc7c32018-08-06 11:33:50 +01004266 }
4267 }
4268#endif /* MBEDTLS_SSL_PROTO_DTLS */
4269
4270 if( ( flush == SSL_FORCE_FLUSH ) &&
4271 ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004272 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004273 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004274 return( ret );
4275 }
4276
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004277 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write record" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004278
4279 return( 0 );
4280}
4281
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004282#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckere25e3b72018-08-16 09:30:53 +01004283
4284static int ssl_hs_is_proper_fragment( mbedtls_ssl_context *ssl )
4285{
4286 if( ssl->in_msglen < ssl->in_hslen ||
4287 memcmp( ssl->in_msg + 6, "\0\0\0", 3 ) != 0 ||
4288 memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 )
4289 {
4290 return( 1 );
4291 }
4292 return( 0 );
4293}
Hanno Becker44650b72018-08-16 12:51:11 +01004294
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004295static uint32_t ssl_get_hs_frag_len( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004296{
4297 return( ( ssl->in_msg[9] << 16 ) |
4298 ( ssl->in_msg[10] << 8 ) |
4299 ssl->in_msg[11] );
4300}
4301
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004302static uint32_t ssl_get_hs_frag_off( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004303{
4304 return( ( ssl->in_msg[6] << 16 ) |
4305 ( ssl->in_msg[7] << 8 ) |
4306 ssl->in_msg[8] );
4307}
4308
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004309static int ssl_check_hs_header( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004310{
4311 uint32_t msg_len, frag_off, frag_len;
4312
4313 msg_len = ssl_get_hs_total_len( ssl );
4314 frag_off = ssl_get_hs_frag_off( ssl );
4315 frag_len = ssl_get_hs_frag_len( ssl );
4316
4317 if( frag_off > msg_len )
4318 return( -1 );
4319
4320 if( frag_len > msg_len - frag_off )
4321 return( -1 );
4322
4323 if( frag_len + 12 > ssl->in_msglen )
4324 return( -1 );
4325
4326 return( 0 );
4327}
4328
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004329/*
4330 * Mark bits in bitmask (used for DTLS HS reassembly)
4331 */
4332static void ssl_bitmask_set( unsigned char *mask, size_t offset, size_t len )
4333{
4334 unsigned int start_bits, end_bits;
4335
4336 start_bits = 8 - ( offset % 8 );
4337 if( start_bits != 8 )
4338 {
4339 size_t first_byte_idx = offset / 8;
4340
Manuel Pégourié-Gonnardac030522014-09-02 14:23:40 +02004341 /* Special case */
4342 if( len <= start_bits )
4343 {
4344 for( ; len != 0; len-- )
4345 mask[first_byte_idx] |= 1 << ( start_bits - len );
4346
4347 /* Avoid potential issues with offset or len becoming invalid */
4348 return;
4349 }
4350
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004351 offset += start_bits; /* Now offset % 8 == 0 */
4352 len -= start_bits;
4353
4354 for( ; start_bits != 0; start_bits-- )
4355 mask[first_byte_idx] |= 1 << ( start_bits - 1 );
4356 }
4357
4358 end_bits = len % 8;
4359 if( end_bits != 0 )
4360 {
4361 size_t last_byte_idx = ( offset + len ) / 8;
4362
4363 len -= end_bits; /* Now len % 8 == 0 */
4364
4365 for( ; end_bits != 0; end_bits-- )
4366 mask[last_byte_idx] |= 1 << ( 8 - end_bits );
4367 }
4368
4369 memset( mask + offset / 8, 0xFF, len / 8 );
4370}
4371
4372/*
4373 * Check that bitmask is full
4374 */
4375static int ssl_bitmask_check( unsigned char *mask, size_t len )
4376{
4377 size_t i;
4378
4379 for( i = 0; i < len / 8; i++ )
4380 if( mask[i] != 0xFF )
4381 return( -1 );
4382
4383 for( i = 0; i < len % 8; i++ )
4384 if( ( mask[len / 8] & ( 1 << ( 7 - i ) ) ) == 0 )
4385 return( -1 );
4386
4387 return( 0 );
4388}
4389
Hanno Becker56e205e2018-08-16 09:06:12 +01004390/* msg_len does not include the handshake header */
Hanno Becker65dc8852018-08-23 09:40:49 +01004391static size_t ssl_get_reassembly_buffer_size( size_t msg_len,
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004392 unsigned add_bitmap )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004393{
Hanno Becker56e205e2018-08-16 09:06:12 +01004394 size_t alloc_len;
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004395
Hanno Becker56e205e2018-08-16 09:06:12 +01004396 alloc_len = 12; /* Handshake header */
4397 alloc_len += msg_len; /* Content buffer */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004398
Hanno Beckerd07df862018-08-16 09:14:58 +01004399 if( add_bitmap )
4400 alloc_len += msg_len / 8 + ( msg_len % 8 != 0 ); /* Bitmap */
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004401
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004402 return( alloc_len );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004403}
Hanno Becker56e205e2018-08-16 09:06:12 +01004404
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004405#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004406
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004407static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl )
Hanno Becker12555c62018-08-16 12:47:53 +01004408{
4409 return( ( ssl->in_msg[1] << 16 ) |
4410 ( ssl->in_msg[2] << 8 ) |
4411 ssl->in_msg[3] );
4412}
Hanno Beckere25e3b72018-08-16 09:30:53 +01004413
Simon Butcher99000142016-10-13 17:21:01 +01004414int mbedtls_ssl_prepare_handshake_record( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004415{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004416 if( ssl->in_msglen < mbedtls_ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004417 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004418 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too short: %d",
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004419 ssl->in_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004420 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004421 }
4422
Hanno Becker12555c62018-08-16 12:47:53 +01004423 ssl->in_hslen = mbedtls_ssl_hs_hdr_len( ssl ) + ssl_get_hs_total_len( ssl );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004424
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004425 MBEDTLS_SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004426 " %d, type = %d, hslen = %d",
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004427 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004428
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004429#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004430 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004431 {
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004432 int ret;
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004433 unsigned int recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004434
Hanno Becker44650b72018-08-16 12:51:11 +01004435 if( ssl_check_hs_header( ssl ) != 0 )
4436 {
4437 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid handshake header" ) );
4438 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4439 }
4440
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004441 if( ssl->handshake != NULL &&
Hanno Beckerc76c6192017-06-06 10:03:17 +01004442 ( ( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER &&
4443 recv_msg_seq != ssl->handshake->in_msg_seq ) ||
4444 ( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
4445 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO ) ) )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004446 {
Hanno Becker9e1ec222018-08-15 15:54:43 +01004447 if( recv_msg_seq > ssl->handshake->in_msg_seq )
4448 {
4449 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received future handshake message of sequence number %u (next %u)",
4450 recv_msg_seq,
4451 ssl->handshake->in_msg_seq ) );
4452 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4453 }
4454
Manuel Pégourié-Gonnardfc572dd2014-10-09 17:56:57 +02004455 /* Retransmit only on last message from previous flight, to avoid
4456 * too many retransmissions.
4457 * Besides, No sane server ever retransmits HelloVerifyRequest */
4458 if( recv_msg_seq == ssl->handshake->in_flight_start_seq - 1 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004459 ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004460 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004461 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received message from last flight, "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004462 "message_seq = %d, start_of_flight = %d",
4463 recv_msg_seq,
4464 ssl->handshake->in_flight_start_seq ) );
4465
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004466 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004467 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004468 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004469 return( ret );
4470 }
4471 }
4472 else
4473 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004474 MBEDTLS_SSL_DEBUG_MSG( 2, ( "dropping out-of-sequence message: "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004475 "message_seq = %d, expected = %d",
4476 recv_msg_seq,
4477 ssl->handshake->in_msg_seq ) );
4478 }
4479
Hanno Becker90333da2017-10-10 11:27:13 +01004480 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004481 }
4482 /* Wait until message completion to increment in_msg_seq */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004483
Hanno Becker6d97ef52018-08-16 13:09:04 +01004484 /* Message reassembly is handled alongside buffering of future
4485 * messages; the commonality is that both handshake fragments and
Hanno Becker83ab41c2018-08-28 17:19:38 +01004486 * future messages cannot be forwarded immediately to the
Hanno Becker6d97ef52018-08-16 13:09:04 +01004487 * handshake logic layer. */
Hanno Beckere25e3b72018-08-16 09:30:53 +01004488 if( ssl_hs_is_proper_fragment( ssl ) == 1 )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004489 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004490 MBEDTLS_SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) );
Hanno Becker6d97ef52018-08-16 13:09:04 +01004491 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004492 }
4493 }
4494 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004495#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004496 /* With TLS we don't handle fragmentation (for now) */
4497 if( ssl->in_msglen < ssl->in_hslen )
4498 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004499 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) );
4500 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004501 }
4502
Simon Butcher99000142016-10-13 17:21:01 +01004503 return( 0 );
4504}
4505
4506void mbedtls_ssl_update_handshake_status( mbedtls_ssl_context *ssl )
4507{
Hanno Becker0271f962018-08-16 13:23:47 +01004508 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Simon Butcher99000142016-10-13 17:21:01 +01004509
Hanno Becker0271f962018-08-16 13:23:47 +01004510 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER && hs != NULL )
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02004511 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004512 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02004513 }
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004514
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004515 /* Handshake message is complete, increment counter */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004516#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004517 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004518 ssl->handshake != NULL )
4519 {
Hanno Becker0271f962018-08-16 13:23:47 +01004520 unsigned offset;
4521 mbedtls_ssl_hs_buffer *hs_buf;
Hanno Beckere25e3b72018-08-16 09:30:53 +01004522
Hanno Becker0271f962018-08-16 13:23:47 +01004523 /* Increment handshake sequence number */
4524 hs->in_msg_seq++;
4525
4526 /*
4527 * Clear up handshake buffering and reassembly structure.
4528 */
4529
4530 /* Free first entry */
Hanno Beckere605b192018-08-21 15:59:07 +01004531 ssl_buffering_free_slot( ssl, 0 );
Hanno Becker0271f962018-08-16 13:23:47 +01004532
4533 /* Shift all other entries */
Hanno Beckere605b192018-08-21 15:59:07 +01004534 for( offset = 0, hs_buf = &hs->buffering.hs[0];
4535 offset + 1 < MBEDTLS_SSL_MAX_BUFFERED_HS;
Hanno Becker0271f962018-08-16 13:23:47 +01004536 offset++, hs_buf++ )
4537 {
4538 *hs_buf = *(hs_buf + 1);
4539 }
4540
4541 /* Create a fresh last entry */
4542 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004543 }
4544#endif
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004545}
4546
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004547/*
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004548 * DTLS anti-replay: RFC 6347 4.1.2.6
4549 *
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004550 * in_window is a field of bits numbered from 0 (lsb) to 63 (msb).
4551 * Bit n is set iff record number in_window_top - n has been seen.
4552 *
4553 * Usually, in_window_top is the last record number seen and the lsb of
4554 * in_window is set. The only exception is the initial state (record number 0
4555 * not seen yet).
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004556 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004557#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4558static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004559{
4560 ssl->in_window_top = 0;
4561 ssl->in_window = 0;
4562}
4563
4564static inline uint64_t ssl_load_six_bytes( unsigned char *buf )
4565{
4566 return( ( (uint64_t) buf[0] << 40 ) |
4567 ( (uint64_t) buf[1] << 32 ) |
4568 ( (uint64_t) buf[2] << 24 ) |
4569 ( (uint64_t) buf[3] << 16 ) |
4570 ( (uint64_t) buf[4] << 8 ) |
4571 ( (uint64_t) buf[5] ) );
4572}
4573
4574/*
4575 * Return 0 if sequence number is acceptable, -1 otherwise
4576 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004577int mbedtls_ssl_dtls_replay_check( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004578{
4579 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
4580 uint64_t bit;
4581
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004582 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004583 return( 0 );
4584
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004585 if( rec_seqnum > ssl->in_window_top )
4586 return( 0 );
4587
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004588 bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004589
4590 if( bit >= 64 )
4591 return( -1 );
4592
4593 if( ( ssl->in_window & ( (uint64_t) 1 << bit ) ) != 0 )
4594 return( -1 );
4595
4596 return( 0 );
4597}
4598
4599/*
4600 * Update replay window on new validated record
4601 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004602void mbedtls_ssl_dtls_replay_update( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004603{
4604 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
4605
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004606 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004607 return;
4608
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004609 if( rec_seqnum > ssl->in_window_top )
4610 {
4611 /* Update window_top and the contents of the window */
4612 uint64_t shift = rec_seqnum - ssl->in_window_top;
4613
4614 if( shift >= 64 )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004615 ssl->in_window = 1;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004616 else
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004617 {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004618 ssl->in_window <<= shift;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004619 ssl->in_window |= 1;
4620 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004621
4622 ssl->in_window_top = rec_seqnum;
4623 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004624 else
4625 {
4626 /* Mark that number as seen in the current window */
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004627 uint64_t bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004628
4629 if( bit < 64 ) /* Always true, but be extra sure */
4630 ssl->in_window |= (uint64_t) 1 << bit;
4631 }
4632}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004633#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004634
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004635#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004636/* Forward declaration */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02004637static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial );
4638
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004639/*
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004640 * Without any SSL context, check if a datagram looks like a ClientHello with
4641 * a valid cookie, and if it doesn't, generate a HelloVerifyRequest message.
Simon Butcher0789aed2015-09-11 17:15:17 +01004642 * Both input and output include full DTLS headers.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004643 *
4644 * - if cookie is valid, return 0
4645 * - if ClientHello looks superficially valid but cookie is not,
4646 * fill obuf and set olen, then
4647 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
4648 * - otherwise return a specific error code
4649 */
4650static int ssl_check_dtls_clihlo_cookie(
4651 mbedtls_ssl_cookie_write_t *f_cookie_write,
4652 mbedtls_ssl_cookie_check_t *f_cookie_check,
4653 void *p_cookie,
4654 const unsigned char *cli_id, size_t cli_id_len,
4655 const unsigned char *in, size_t in_len,
4656 unsigned char *obuf, size_t buf_len, size_t *olen )
4657{
4658 size_t sid_len, cookie_len;
4659 unsigned char *p;
4660
4661 if( f_cookie_write == NULL || f_cookie_check == NULL )
4662 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4663
4664 /*
4665 * Structure of ClientHello with record and handshake headers,
4666 * and expected values. We don't need to check a lot, more checks will be
4667 * done when actually parsing the ClientHello - skipping those checks
4668 * avoids code duplication and does not make cookie forging any easier.
4669 *
4670 * 0-0 ContentType type; copied, must be handshake
4671 * 1-2 ProtocolVersion version; copied
4672 * 3-4 uint16 epoch; copied, must be 0
4673 * 5-10 uint48 sequence_number; copied
4674 * 11-12 uint16 length; (ignored)
4675 *
4676 * 13-13 HandshakeType msg_type; (ignored)
4677 * 14-16 uint24 length; (ignored)
4678 * 17-18 uint16 message_seq; copied
4679 * 19-21 uint24 fragment_offset; copied, must be 0
4680 * 22-24 uint24 fragment_length; (ignored)
4681 *
4682 * 25-26 ProtocolVersion client_version; (ignored)
4683 * 27-58 Random random; (ignored)
4684 * 59-xx SessionID session_id; 1 byte len + sid_len content
4685 * 60+ opaque cookie<0..2^8-1>; 1 byte len + content
4686 * ...
4687 *
4688 * Minimum length is 61 bytes.
4689 */
4690 if( in_len < 61 ||
4691 in[0] != MBEDTLS_SSL_MSG_HANDSHAKE ||
4692 in[3] != 0 || in[4] != 0 ||
4693 in[19] != 0 || in[20] != 0 || in[21] != 0 )
4694 {
4695 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4696 }
4697
4698 sid_len = in[59];
4699 if( sid_len > in_len - 61 )
4700 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4701
4702 cookie_len = in[60 + sid_len];
4703 if( cookie_len > in_len - 60 )
4704 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4705
4706 if( f_cookie_check( p_cookie, in + sid_len + 61, cookie_len,
4707 cli_id, cli_id_len ) == 0 )
4708 {
4709 /* Valid cookie */
4710 return( 0 );
4711 }
4712
4713 /*
4714 * If we get here, we've got an invalid cookie, let's prepare HVR.
4715 *
4716 * 0-0 ContentType type; copied
4717 * 1-2 ProtocolVersion version; copied
4718 * 3-4 uint16 epoch; copied
4719 * 5-10 uint48 sequence_number; copied
4720 * 11-12 uint16 length; olen - 13
4721 *
4722 * 13-13 HandshakeType msg_type; hello_verify_request
4723 * 14-16 uint24 length; olen - 25
4724 * 17-18 uint16 message_seq; copied
4725 * 19-21 uint24 fragment_offset; copied
4726 * 22-24 uint24 fragment_length; olen - 25
4727 *
4728 * 25-26 ProtocolVersion server_version; 0xfe 0xff
4729 * 27-27 opaque cookie<0..2^8-1>; cookie_len = olen - 27, cookie
4730 *
4731 * Minimum length is 28.
4732 */
4733 if( buf_len < 28 )
4734 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
4735
4736 /* Copy most fields and adapt others */
4737 memcpy( obuf, in, 25 );
4738 obuf[13] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST;
4739 obuf[25] = 0xfe;
4740 obuf[26] = 0xff;
4741
4742 /* Generate and write actual cookie */
4743 p = obuf + 28;
4744 if( f_cookie_write( p_cookie,
4745 &p, obuf + buf_len, cli_id, cli_id_len ) != 0 )
4746 {
4747 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4748 }
4749
4750 *olen = p - obuf;
4751
4752 /* Go back and fill length fields */
4753 obuf[27] = (unsigned char)( *olen - 28 );
4754
4755 obuf[14] = obuf[22] = (unsigned char)( ( *olen - 25 ) >> 16 );
4756 obuf[15] = obuf[23] = (unsigned char)( ( *olen - 25 ) >> 8 );
4757 obuf[16] = obuf[24] = (unsigned char)( ( *olen - 25 ) );
4758
4759 obuf[11] = (unsigned char)( ( *olen - 13 ) >> 8 );
4760 obuf[12] = (unsigned char)( ( *olen - 13 ) );
4761
4762 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
4763}
4764
4765/*
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004766 * Handle possible client reconnect with the same UDP quadruplet
4767 * (RFC 6347 Section 4.2.8).
4768 *
4769 * Called by ssl_parse_record_header() in case we receive an epoch 0 record
4770 * that looks like a ClientHello.
4771 *
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004772 * - if the input looks like a ClientHello without cookies,
4773 * send back HelloVerifyRequest, then
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004774 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004775 * - if the input looks like a ClientHello with a valid cookie,
4776 * reset the session of the current context, and
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02004777 * return MBEDTLS_ERR_SSL_CLIENT_RECONNECT
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004778 * - if anything goes wrong, return a specific error code
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004779 *
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004780 * mbedtls_ssl_read_record() will ignore the record if anything else than
Simon Butcherd0bf6a32015-09-11 17:34:49 +01004781 * MBEDTLS_ERR_SSL_CLIENT_RECONNECT or 0 is returned, although this function
4782 * cannot not return 0.
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004783 */
4784static int ssl_handle_possible_reconnect( mbedtls_ssl_context *ssl )
4785{
4786 int ret;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004787 size_t len;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004788
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004789 ret = ssl_check_dtls_clihlo_cookie(
4790 ssl->conf->f_cookie_write,
4791 ssl->conf->f_cookie_check,
4792 ssl->conf->p_cookie,
4793 ssl->cli_id, ssl->cli_id_len,
4794 ssl->in_buf, ssl->in_left,
Angus Grattond8213d02016-05-25 20:56:48 +10004795 ssl->out_buf, MBEDTLS_SSL_OUT_CONTENT_LEN, &len );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004796
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004797 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_check_dtls_clihlo_cookie", ret );
4798
4799 if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004800 {
Brian J Murray1903fb32016-11-06 04:45:15 -08004801 /* Don't check write errors as we can't do anything here.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004802 * If the error is permanent we'll catch it later,
4803 * if it's not, then hopefully it'll work next time. */
4804 (void) ssl->f_send( ssl->p_bio, ssl->out_buf, len );
4805
4806 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004807 }
4808
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004809 if( ret == 0 )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004810 {
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004811 /* Got a valid cookie, partially reset context */
4812 if( ( ret = ssl_session_reset_int( ssl, 1 ) ) != 0 )
4813 {
4814 MBEDTLS_SSL_DEBUG_RET( 1, "reset", ret );
4815 return( ret );
4816 }
4817
4818 return( MBEDTLS_ERR_SSL_CLIENT_RECONNECT );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004819 }
4820
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004821 return( ret );
4822}
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004823#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004824
Hanno Beckerf661c9c2019-05-03 13:25:54 +01004825static int ssl_check_record_type( uint8_t record_type )
4826{
4827 if( record_type != MBEDTLS_SSL_MSG_HANDSHAKE &&
4828 record_type != MBEDTLS_SSL_MSG_ALERT &&
4829 record_type != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC &&
4830 record_type != MBEDTLS_SSL_MSG_APPLICATION_DATA )
4831 {
4832 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4833 }
4834
4835 return( 0 );
4836}
4837
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004838/*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004839 * ContentType type;
4840 * ProtocolVersion version;
4841 * uint16 epoch; // DTLS only
4842 * uint48 sequence_number; // DTLS only
4843 * uint16 length;
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004844 *
4845 * Return 0 if header looks sane (and, for DTLS, the record is expected)
Simon Butcher207990d2015-12-16 01:51:30 +00004846 * MBEDTLS_ERR_SSL_INVALID_RECORD if the header looks bad,
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004847 * MBEDTLS_ERR_SSL_UNEXPECTED_RECORD (DTLS only) if sane but unexpected.
4848 *
4849 * With DTLS, mbedtls_ssl_read_record() will:
Simon Butcher207990d2015-12-16 01:51:30 +00004850 * 1. proceed with the record if this function returns 0
4851 * 2. drop only the current record if this function returns UNEXPECTED_RECORD
4852 * 3. return CLIENT_RECONNECT if this function return that value
4853 * 4. drop the whole datagram if this function returns anything else.
4854 * Point 2 is needed when the peer is resending, and we have already received
4855 * the first record from a datagram but are still waiting for the others.
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004856 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004857static int ssl_parse_record_header( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004858{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01004859 int major_ver, minor_ver;
Hanno Beckerca59c2b2019-05-08 12:03:28 +01004860 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00004861
Hanno Beckerca59c2b2019-05-08 12:03:28 +01004862 /* Parse and validate record content type and version */
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02004863
Paul Bakker5121ce52009-01-03 21:22:43 +00004864 ssl->in_msgtype = ssl->in_hdr[0];
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004865 mbedtls_ssl_read_version( &major_ver, &minor_ver, ssl->conf->transport, ssl->in_hdr + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004866
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004867 /* Check record type */
Hanno Beckera0e20d02019-05-15 14:03:01 +01004868#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerca59c2b2019-05-08 12:03:28 +01004869 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
4870 ssl->in_msgtype == MBEDTLS_SSL_MSG_CID &&
4871 ssl->conf->cid_len != 0 )
4872 {
4873 /* Shift pointers to account for record header including CID
4874 * struct {
4875 * ContentType special_type = tls12_cid;
4876 * ProtocolVersion version;
4877 * uint16 epoch;
4878 * uint48 sequence_number;
Hanno Becker8e55b0f2019-05-23 17:03:19 +01004879 * opaque cid[cid_length]; // Additional field compared to
4880 * // default DTLS record format
Hanno Beckerca59c2b2019-05-08 12:03:28 +01004881 * uint16 length;
4882 * opaque enc_content[DTLSCiphertext.length];
4883 * } DTLSCiphertext;
4884 */
4885
4886 /* So far, we only support static CID lengths
4887 * fixed in the configuration. */
4888 ssl->in_len = ssl->in_cid + ssl->conf->cid_len;
4889 ssl->in_iv = ssl->in_msg = ssl->in_len + 2;
4890 }
4891 else
Hanno Beckera0e20d02019-05-15 14:03:01 +01004892#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf661c9c2019-05-03 13:25:54 +01004893 if( ssl_check_record_type( ssl->in_msgtype ) )
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004894 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004895 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004896
4897#if defined(MBEDTLS_SSL_PROTO_DTLS)
Andres Amaya Garcia01692532017-06-28 09:26:46 +01004898 /* Silently ignore invalid DTLS records as recommended by RFC 6347
4899 * Section 4.1.2.7 */
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004900 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4901#endif /* MBEDTLS_SSL_PROTO_DTLS */
4902 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4903 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
4904
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004905 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004906 }
4907
4908 /* Check version */
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01004909 if( major_ver != ssl->major_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00004910 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004911 MBEDTLS_SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
4912 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004913 }
4914
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004915 if( minor_ver > ssl->conf->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00004916 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004917 MBEDTLS_SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
4918 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004919 }
4920
Hanno Beckerca59c2b2019-05-08 12:03:28 +01004921 /* Now that the total length of the record header is known, ensure
4922 * that the current datagram is large enough to hold it.
4923 * This would fail, for example, if we received a datagram of
4924 * size 13 + n Bytes where n is less than the size of incoming CIDs. */
4925 ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_in_hdr_len( ssl ) );
4926 if( ret != 0 )
4927 {
4928 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
4929 return( ret );
4930 }
4931 MBEDTLS_SSL_DEBUG_BUF( 4, "input record header", ssl->in_hdr, mbedtls_ssl_in_hdr_len( ssl ) );
4932
4933 /* Parse and validate record length
4934 * This must happen after the CID parsing because
4935 * its position in the record header depends on
4936 * the presence of a CID. */
4937
4938 ssl->in_msglen = ( ssl->in_len[0] << 8 ) | ssl->in_len[1];
Angus Grattond8213d02016-05-25 20:56:48 +10004939 if( ssl->in_msglen > MBEDTLS_SSL_IN_BUFFER_LEN
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004940 - (size_t)( ssl->in_msg - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02004941 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004942 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4943 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02004944 }
4945
Hanno Beckerca59c2b2019-05-08 12:03:28 +01004946 MBEDTLS_SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
Hanno Becker92d30f52019-05-23 17:03:44 +01004947 "version = [%d:%d], msglen = %d",
4948 ssl->in_msgtype,
4949 major_ver, minor_ver, ssl->in_msglen ) );
Hanno Beckerca59c2b2019-05-08 12:03:28 +01004950
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004951 /*
Hanno Becker52c6dc62017-05-26 16:07:36 +01004952 * DTLS-related tests.
4953 * Check epoch before checking length constraint because
4954 * the latter varies with the epoch. E.g., if a ChangeCipherSpec
4955 * message gets duplicated before the corresponding Finished message,
4956 * the second ChangeCipherSpec should be discarded because it belongs
4957 * to an old epoch, but not because its length is shorter than
4958 * the minimum record length for packets using the new record transform.
4959 * Note that these two kinds of failures are handled differently,
4960 * as an unexpected record is silently skipped but an invalid
4961 * record leads to the entire datagram being dropped.
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004962 */
4963#if defined(MBEDTLS_SSL_PROTO_DTLS)
4964 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4965 {
4966 unsigned int rec_epoch = ( ssl->in_ctr[0] << 8 ) | ssl->in_ctr[1];
4967
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004968 /* Check epoch (and sequence number) with DTLS */
4969 if( rec_epoch != ssl->in_epoch )
4970 {
4971 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record from another epoch: "
4972 "expected %d, received %d",
4973 ssl->in_epoch, rec_epoch ) );
4974
4975#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
4976 /*
4977 * Check for an epoch 0 ClientHello. We can't use in_msg here to
4978 * access the first byte of record content (handshake type), as we
4979 * have an active transform (possibly iv_len != 0), so use the
4980 * fact that the record header len is 13 instead.
4981 */
4982 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
4983 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
4984 rec_epoch == 0 &&
4985 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4986 ssl->in_left > 13 &&
4987 ssl->in_buf[13] == MBEDTLS_SSL_HS_CLIENT_HELLO )
4988 {
4989 MBEDTLS_SSL_DEBUG_MSG( 1, ( "possible client reconnect "
4990 "from the same port" ) );
4991 return( ssl_handle_possible_reconnect( ssl ) );
4992 }
4993 else
4994#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Hanno Becker5f066e72018-08-16 14:56:31 +01004995 {
4996 /* Consider buffering the record. */
4997 if( rec_epoch == (unsigned int) ssl->in_epoch + 1 )
4998 {
4999 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Consider record for buffering" ) );
5000 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
5001 }
5002
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005003 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
Hanno Becker5f066e72018-08-16 14:56:31 +01005004 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005005 }
5006
5007#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
5008 /* Replay detection only works for the current epoch */
5009 if( rec_epoch == ssl->in_epoch &&
5010 mbedtls_ssl_dtls_replay_check( ssl ) != 0 )
5011 {
5012 MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record" ) );
5013 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
5014 }
5015#endif
5016 }
5017#endif /* MBEDTLS_SSL_PROTO_DTLS */
5018
Hanno Becker52c6dc62017-05-26 16:07:36 +01005019
5020 /* Check length against bounds of the current transform and version */
5021 if( ssl->transform_in == NULL )
5022 {
5023 if( ssl->in_msglen < 1 ||
Angus Grattond8213d02016-05-25 20:56:48 +10005024 ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker52c6dc62017-05-26 16:07:36 +01005025 {
5026 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
5027 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5028 }
5029 }
5030 else
5031 {
5032 if( ssl->in_msglen < ssl->transform_in->minlen )
5033 {
5034 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
5035 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5036 }
5037
5038#if defined(MBEDTLS_SSL_PROTO_SSL3)
5039 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
Angus Grattond8213d02016-05-25 20:56:48 +10005040 ssl->in_msglen > ssl->transform_in->minlen + MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker52c6dc62017-05-26 16:07:36 +01005041 {
5042 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
5043 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5044 }
5045#endif
5046#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
5047 defined(MBEDTLS_SSL_PROTO_TLS1_2)
5048 /*
5049 * TLS encrypted messages can have up to 256 bytes of padding
5050 */
5051 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 &&
5052 ssl->in_msglen > ssl->transform_in->minlen +
Angus Grattond8213d02016-05-25 20:56:48 +10005053 MBEDTLS_SSL_IN_CONTENT_LEN + 256 )
Hanno Becker52c6dc62017-05-26 16:07:36 +01005054 {
5055 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
5056 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5057 }
5058#endif
5059 }
5060
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005061 return( 0 );
5062}
Paul Bakker5121ce52009-01-03 21:22:43 +00005063
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005064/*
5065 * If applicable, decrypt (and decompress) record content
5066 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005067static int ssl_prepare_record_content( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005068{
5069 int ret, done = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02005070
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005071 MBEDTLS_SSL_DEBUG_BUF( 4, "input record from network",
Hanno Becker5903de42019-05-03 14:46:38 +01005072 ssl->in_hdr, mbedtls_ssl_in_hdr_len( ssl ) + ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00005073
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005074#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
5075 if( mbedtls_ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00005076 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005077 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_read()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00005078
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005079 ret = mbedtls_ssl_hw_record_read( ssl );
5080 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00005081 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005082 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_read", ret );
5083 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00005084 }
Paul Bakkerc7878112012-12-19 14:41:14 +01005085
5086 if( ret == 0 )
5087 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00005088 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005089#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00005090 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005091 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00005092 mbedtls_record rec;
5093
5094 rec.buf = ssl->in_iv;
5095 rec.buf_len = MBEDTLS_SSL_IN_BUFFER_LEN
5096 - ( ssl->in_iv - ssl->in_buf );
5097 rec.data_len = ssl->in_msglen;
5098 rec.data_offset = 0;
Hanno Beckera0e20d02019-05-15 14:03:01 +01005099#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID )
Hanno Becker2cdc5c32019-05-09 15:54:28 +01005100 rec.cid_len = (uint8_t)( ssl->in_len - ssl->in_cid );
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01005101 memcpy( rec.cid, ssl->in_cid, rec.cid_len );
Hanno Beckera0e20d02019-05-15 14:03:01 +01005102#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00005103
5104 memcpy( &rec.ctr[0], ssl->in_ctr, 8 );
5105 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
5106 ssl->conf->transport, rec.ver );
5107 rec.type = ssl->in_msgtype;
Hanno Beckera18d1322018-01-03 14:27:32 +00005108 if( ( ret = mbedtls_ssl_decrypt_buf( ssl, ssl->transform_in,
5109 &rec ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005110 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005111 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
Hanno Becker8367ccc2019-05-14 11:30:10 +01005112
Hanno Beckera0e20d02019-05-15 14:03:01 +01005113#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker8367ccc2019-05-14 11:30:10 +01005114 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_CID &&
5115 ssl->conf->ignore_unexpected_cid
5116 == MBEDTLS_SSL_UNEXPECTED_CID_IGNORE )
5117 {
Hanno Beckere8d6afd2019-05-24 10:11:06 +01005118 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ignoring unexpected CID" ) );
Hanno Becker16ded982019-05-08 13:02:55 +01005119 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
Hanno Becker8367ccc2019-05-14 11:30:10 +01005120 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01005121#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker16ded982019-05-08 13:02:55 +01005122
Paul Bakker5121ce52009-01-03 21:22:43 +00005123 return( ret );
5124 }
5125
Hanno Becker6430faf2019-05-08 11:57:13 +01005126 if( ssl->in_msgtype != rec.type )
5127 {
5128 MBEDTLS_SSL_DEBUG_MSG( 4, ( "record type after decrypt (before %d): %d",
5129 ssl->in_msgtype, rec.type ) );
5130 }
5131
5132 /* The record content type may change during decryption,
5133 * so re-read it. */
5134 ssl->in_msgtype = rec.type;
5135 /* Also update the input buffer, because unfortunately
5136 * the server-side ssl_parse_client_hello() reparses the
5137 * record header when receiving a ClientHello initiating
5138 * a renegotiation. */
5139 ssl->in_hdr[0] = rec.type;
Hanno Becker79594fd2019-05-08 09:38:41 +01005140 ssl->in_msg = rec.buf + rec.data_offset;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00005141 ssl->in_msglen = rec.data_len;
5142 ssl->in_len[0] = (unsigned char)( rec.data_len >> 8 );
5143 ssl->in_len[1] = (unsigned char)( rec.data_len );
5144
Hanno Becker1c0c37f2018-08-07 14:29:29 +01005145 MBEDTLS_SSL_DEBUG_BUF( 4, "input payload after decrypt",
5146 ssl->in_msg, ssl->in_msglen );
5147
Hanno Beckera0e20d02019-05-15 14:03:01 +01005148#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker6430faf2019-05-08 11:57:13 +01005149 /* We have already checked the record content type
5150 * in ssl_parse_record_header(), failing or silently
5151 * dropping the record in the case of an unknown type.
5152 *
5153 * Since with the use of CIDs, the record content type
5154 * might change during decryption, re-check the record
5155 * content type, but treat a failure as fatal this time. */
5156 if( ssl_check_record_type( ssl->in_msgtype ) )
5157 {
5158 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
5159 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5160 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01005161#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker6430faf2019-05-08 11:57:13 +01005162
Angus Grattond8213d02016-05-25 20:56:48 +10005163 if( ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00005164 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005165 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
5166 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00005167 }
Hanno Becker2e24c3b2017-12-27 21:28:58 +00005168 else if( ssl->in_msglen == 0 )
5169 {
5170#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5171 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3
5172 && ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
5173 {
5174 /* TLS v1.2 explicitly disallows zero-length messages which are not application data */
5175 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid zero-length message type: %d", ssl->in_msgtype ) );
5176 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5177 }
5178#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
5179
5180 ssl->nb_zero++;
5181
5182 /*
5183 * Three or more empty messages may be a DoS attack
5184 * (excessive CPU consumption).
5185 */
5186 if( ssl->nb_zero > 3 )
5187 {
5188 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
Hanno Becker6e7700d2019-05-08 10:38:32 +01005189 "messages, possible DoS attack" ) );
5190 /* Treat the records as if they were not properly authenticated,
5191 * thereby failing the connection if we see more than allowed
5192 * by the configured bad MAC threshold. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00005193 return( MBEDTLS_ERR_SSL_INVALID_MAC );
5194 }
5195 }
5196 else
5197 ssl->nb_zero = 0;
5198
5199#if defined(MBEDTLS_SSL_PROTO_DTLS)
5200 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5201 {
5202 ; /* in_ctr read from peer, not maintained internally */
5203 }
5204 else
5205#endif
5206 {
5207 unsigned i;
5208 for( i = 8; i > ssl_ep_len( ssl ); i-- )
5209 if( ++ssl->in_ctr[i - 1] != 0 )
5210 break;
5211
5212 /* The loop goes to its end iff the counter is wrapping */
5213 if( i == ssl_ep_len( ssl ) )
5214 {
5215 MBEDTLS_SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
5216 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
5217 }
5218 }
5219
Paul Bakker5121ce52009-01-03 21:22:43 +00005220 }
5221
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005222#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00005223 if( ssl->transform_in != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005224 ssl->session_in->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00005225 {
5226 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
5227 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005228 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00005229 return( ret );
5230 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00005231 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005232#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00005233
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005234#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005235 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02005236 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005237 mbedtls_ssl_dtls_replay_update( ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02005238 }
5239#endif
5240
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005241 return( 0 );
5242}
5243
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005244static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005245
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005246/*
5247 * Read a record.
5248 *
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005249 * Silently ignore non-fatal alert (and for DTLS, invalid records as well,
5250 * RFC 6347 4.1.2.7) and continue reading until a valid record is found.
5251 *
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005252 */
Hanno Becker1097b342018-08-15 14:09:41 +01005253
5254/* Helper functions for mbedtls_ssl_read_record(). */
5255static int ssl_consume_current_message( mbedtls_ssl_context *ssl );
Hanno Beckere74d5562018-08-15 14:26:08 +01005256static int ssl_get_next_record( mbedtls_ssl_context *ssl );
5257static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl );
Hanno Becker4162b112018-08-15 14:05:04 +01005258
Hanno Becker327c93b2018-08-15 13:56:18 +01005259int mbedtls_ssl_read_record( mbedtls_ssl_context *ssl,
Hanno Becker3a0aad12018-08-20 09:44:02 +01005260 unsigned update_hs_digest )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005261{
5262 int ret;
5263
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005264 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read record" ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005265
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005266 if( ssl->keep_current_message == 0 )
5267 {
5268 do {
Simon Butcher99000142016-10-13 17:21:01 +01005269
Hanno Becker26994592018-08-15 14:14:59 +01005270 ret = ssl_consume_current_message( ssl );
Hanno Becker90333da2017-10-10 11:27:13 +01005271 if( ret != 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005272 return( ret );
Hanno Becker26994592018-08-15 14:14:59 +01005273
Hanno Beckere74d5562018-08-15 14:26:08 +01005274 if( ssl_record_is_in_progress( ssl ) == 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005275 {
Hanno Becker40f50842018-08-15 14:48:01 +01005276#if defined(MBEDTLS_SSL_PROTO_DTLS)
5277 int have_buffered = 0;
Hanno Beckere74d5562018-08-15 14:26:08 +01005278
Hanno Becker40f50842018-08-15 14:48:01 +01005279 /* We only check for buffered messages if the
5280 * current datagram is fully consumed. */
5281 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005282 ssl_next_record_is_in_datagram( ssl ) == 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01005283 {
Hanno Becker40f50842018-08-15 14:48:01 +01005284 if( ssl_load_buffered_message( ssl ) == 0 )
5285 have_buffered = 1;
5286 }
5287
5288 if( have_buffered == 0 )
5289#endif /* MBEDTLS_SSL_PROTO_DTLS */
5290 {
5291 ret = ssl_get_next_record( ssl );
5292 if( ret == MBEDTLS_ERR_SSL_CONTINUE_PROCESSING )
5293 continue;
5294
5295 if( ret != 0 )
5296 {
Hanno Beckerc573ac32018-08-28 17:15:25 +01005297 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_get_next_record" ), ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005298 return( ret );
5299 }
Hanno Beckere74d5562018-08-15 14:26:08 +01005300 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005301 }
5302
5303 ret = mbedtls_ssl_handle_message_type( ssl );
5304
Hanno Becker40f50842018-08-15 14:48:01 +01005305#if defined(MBEDTLS_SSL_PROTO_DTLS)
5306 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
5307 {
5308 /* Buffer future message */
5309 ret = ssl_buffer_message( ssl );
5310 if( ret != 0 )
5311 return( ret );
5312
5313 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
5314 }
5315#endif /* MBEDTLS_SSL_PROTO_DTLS */
5316
Hanno Becker90333da2017-10-10 11:27:13 +01005317 } while( MBEDTLS_ERR_SSL_NON_FATAL == ret ||
5318 MBEDTLS_ERR_SSL_CONTINUE_PROCESSING == ret );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005319
5320 if( 0 != ret )
Simon Butcher99000142016-10-13 17:21:01 +01005321 {
Hanno Becker05c4fc82017-11-09 14:34:06 +00005322 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_handle_message_type" ), ret );
Simon Butcher99000142016-10-13 17:21:01 +01005323 return( ret );
5324 }
5325
Hanno Becker327c93b2018-08-15 13:56:18 +01005326 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
Hanno Becker3a0aad12018-08-20 09:44:02 +01005327 update_hs_digest == 1 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005328 {
5329 mbedtls_ssl_update_handshake_status( ssl );
5330 }
Simon Butcher99000142016-10-13 17:21:01 +01005331 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005332 else
Simon Butcher99000142016-10-13 17:21:01 +01005333 {
Hanno Becker02f59072018-08-15 14:00:24 +01005334 MBEDTLS_SSL_DEBUG_MSG( 2, ( "reuse previously read message" ) );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005335 ssl->keep_current_message = 0;
Simon Butcher99000142016-10-13 17:21:01 +01005336 }
5337
5338 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read record" ) );
5339
5340 return( 0 );
5341}
5342
Hanno Becker40f50842018-08-15 14:48:01 +01005343#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005344static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl )
Simon Butcher99000142016-10-13 17:21:01 +01005345{
Hanno Becker40f50842018-08-15 14:48:01 +01005346 if( ssl->in_left > ssl->next_record_offset )
5347 return( 1 );
Simon Butcher99000142016-10-13 17:21:01 +01005348
Hanno Becker40f50842018-08-15 14:48:01 +01005349 return( 0 );
5350}
5351
5352static int ssl_load_buffered_message( mbedtls_ssl_context *ssl )
5353{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005354 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker37f95322018-08-16 13:55:32 +01005355 mbedtls_ssl_hs_buffer * hs_buf;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005356 int ret = 0;
5357
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005358 if( hs == NULL )
5359 return( -1 );
5360
Hanno Beckere00ae372018-08-20 09:39:42 +01005361 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_messsage" ) );
5362
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005363 if( ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC ||
5364 ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
5365 {
5366 /* Check if we have seen a ChangeCipherSpec before.
5367 * If yes, synthesize a CCS record. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01005368 if( !hs->buffering.seen_ccs )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005369 {
5370 MBEDTLS_SSL_DEBUG_MSG( 2, ( "CCS not seen in the current flight" ) );
5371 ret = -1;
Hanno Becker0d4b3762018-08-20 09:36:59 +01005372 goto exit;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005373 }
5374
Hanno Becker39b8bc92018-08-28 17:17:13 +01005375 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Injecting buffered CCS message" ) );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005376 ssl->in_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
5377 ssl->in_msglen = 1;
5378 ssl->in_msg[0] = 1;
5379
5380 /* As long as they are equal, the exact value doesn't matter. */
5381 ssl->in_left = 0;
5382 ssl->next_record_offset = 0;
5383
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01005384 hs->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005385 goto exit;
5386 }
Hanno Becker37f95322018-08-16 13:55:32 +01005387
Hanno Beckerb8f50142018-08-28 10:01:34 +01005388#if defined(MBEDTLS_DEBUG_C)
Hanno Becker37f95322018-08-16 13:55:32 +01005389 /* Debug only */
5390 {
5391 unsigned offset;
5392 for( offset = 1; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
5393 {
5394 hs_buf = &hs->buffering.hs[offset];
5395 if( hs_buf->is_valid == 1 )
5396 {
5397 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Future message with sequence number %u %s buffered.",
5398 hs->in_msg_seq + offset,
Hanno Beckera591c482018-08-28 17:20:00 +01005399 hs_buf->is_complete ? "fully" : "partially" ) );
Hanno Becker37f95322018-08-16 13:55:32 +01005400 }
5401 }
5402 }
Hanno Beckerb8f50142018-08-28 10:01:34 +01005403#endif /* MBEDTLS_DEBUG_C */
Hanno Becker37f95322018-08-16 13:55:32 +01005404
5405 /* Check if we have buffered and/or fully reassembled the
5406 * next handshake message. */
5407 hs_buf = &hs->buffering.hs[0];
5408 if( ( hs_buf->is_valid == 1 ) && ( hs_buf->is_complete == 1 ) )
5409 {
5410 /* Synthesize a record containing the buffered HS message. */
5411 size_t msg_len = ( hs_buf->data[1] << 16 ) |
5412 ( hs_buf->data[2] << 8 ) |
5413 hs_buf->data[3];
5414
5415 /* Double-check that we haven't accidentally buffered
5416 * a message that doesn't fit into the input buffer. */
5417 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
5418 {
5419 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5420 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5421 }
5422
5423 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message has been buffered - load" ) );
5424 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered handshake message (incl. header)",
5425 hs_buf->data, msg_len + 12 );
5426
5427 ssl->in_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
5428 ssl->in_hslen = msg_len + 12;
5429 ssl->in_msglen = msg_len + 12;
5430 memcpy( ssl->in_msg, hs_buf->data, ssl->in_hslen );
5431
5432 ret = 0;
5433 goto exit;
5434 }
5435 else
5436 {
5437 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message %u not or only partially bufffered",
5438 hs->in_msg_seq ) );
5439 }
5440
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005441 ret = -1;
5442
5443exit:
5444
5445 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_message" ) );
5446 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005447}
5448
Hanno Beckera02b0b42018-08-21 17:20:27 +01005449static int ssl_buffer_make_space( mbedtls_ssl_context *ssl,
5450 size_t desired )
5451{
5452 int offset;
5453 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005454 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Attempt to free buffered messages to have %u bytes available",
5455 (unsigned) desired ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005456
Hanno Becker01315ea2018-08-21 17:22:17 +01005457 /* Get rid of future records epoch first, if such exist. */
5458 ssl_free_buffered_record( ssl );
5459
5460 /* Check if we have enough space available now. */
5461 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5462 hs->buffering.total_bytes_buffered ) )
5463 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005464 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing future epoch record" ) );
Hanno Becker01315ea2018-08-21 17:22:17 +01005465 return( 0 );
5466 }
Hanno Beckera02b0b42018-08-21 17:20:27 +01005467
Hanno Becker4f432ad2018-08-28 10:02:32 +01005468 /* We don't have enough space to buffer the next expected handshake
5469 * message. Remove buffers used for future messages to gain space,
5470 * starting with the most distant one. */
Hanno Beckera02b0b42018-08-21 17:20:27 +01005471 for( offset = MBEDTLS_SSL_MAX_BUFFERED_HS - 1;
5472 offset >= 0; offset-- )
5473 {
5474 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Free buffering slot %d to make space for reassembly of next handshake message",
5475 offset ) );
5476
Hanno Beckerb309b922018-08-23 13:18:05 +01005477 ssl_buffering_free_slot( ssl, (uint8_t) offset );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005478
5479 /* Check if we have enough space available now. */
5480 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5481 hs->buffering.total_bytes_buffered ) )
5482 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005483 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing buffered HS messages" ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005484 return( 0 );
5485 }
5486 }
5487
5488 return( -1 );
5489}
5490
Hanno Becker40f50842018-08-15 14:48:01 +01005491static int ssl_buffer_message( mbedtls_ssl_context *ssl )
5492{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005493 int ret = 0;
5494 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5495
5496 if( hs == NULL )
5497 return( 0 );
5498
5499 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_buffer_message" ) );
5500
5501 switch( ssl->in_msgtype )
5502 {
5503 case MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC:
5504 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Remember CCS message" ) );
Hanno Beckere678eaa2018-08-21 14:57:46 +01005505
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01005506 hs->buffering.seen_ccs = 1;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005507 break;
5508
5509 case MBEDTLS_SSL_MSG_HANDSHAKE:
Hanno Becker37f95322018-08-16 13:55:32 +01005510 {
5511 unsigned recv_msg_seq_offset;
5512 unsigned recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
5513 mbedtls_ssl_hs_buffer *hs_buf;
5514 size_t msg_len = ssl->in_hslen - 12;
5515
5516 /* We should never receive an old handshake
5517 * message - double-check nonetheless. */
5518 if( recv_msg_seq < ssl->handshake->in_msg_seq )
5519 {
5520 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5521 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5522 }
5523
5524 recv_msg_seq_offset = recv_msg_seq - ssl->handshake->in_msg_seq;
5525 if( recv_msg_seq_offset >= MBEDTLS_SSL_MAX_BUFFERED_HS )
5526 {
5527 /* Silently ignore -- message too far in the future */
5528 MBEDTLS_SSL_DEBUG_MSG( 2,
5529 ( "Ignore future HS message with sequence number %u, "
5530 "buffering window %u - %u",
5531 recv_msg_seq, ssl->handshake->in_msg_seq,
5532 ssl->handshake->in_msg_seq + MBEDTLS_SSL_MAX_BUFFERED_HS - 1 ) );
5533
5534 goto exit;
5535 }
5536
5537 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering HS message with sequence number %u, offset %u ",
5538 recv_msg_seq, recv_msg_seq_offset ) );
5539
5540 hs_buf = &hs->buffering.hs[ recv_msg_seq_offset ];
5541
5542 /* Check if the buffering for this seq nr has already commenced. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01005543 if( !hs_buf->is_valid )
Hanno Becker37f95322018-08-16 13:55:32 +01005544 {
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005545 size_t reassembly_buf_sz;
5546
Hanno Becker37f95322018-08-16 13:55:32 +01005547 hs_buf->is_fragmented =
5548 ( ssl_hs_is_proper_fragment( ssl ) == 1 );
5549
5550 /* We copy the message back into the input buffer
5551 * after reassembly, so check that it's not too large.
5552 * This is an implementation-specific limitation
5553 * and not one from the standard, hence it is not
5554 * checked in ssl_check_hs_header(). */
Hanno Becker96a6c692018-08-21 15:56:03 +01005555 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker37f95322018-08-16 13:55:32 +01005556 {
5557 /* Ignore message */
5558 goto exit;
5559 }
5560
Hanno Beckere0b150f2018-08-21 15:51:03 +01005561 /* Check if we have enough space to buffer the message. */
5562 if( hs->buffering.total_bytes_buffered >
5563 MBEDTLS_SSL_DTLS_MAX_BUFFERING )
5564 {
5565 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5566 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5567 }
5568
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005569 reassembly_buf_sz = ssl_get_reassembly_buffer_size( msg_len,
5570 hs_buf->is_fragmented );
Hanno Beckere0b150f2018-08-21 15:51:03 +01005571
5572 if( reassembly_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5573 hs->buffering.total_bytes_buffered ) )
5574 {
5575 if( recv_msg_seq_offset > 0 )
5576 {
5577 /* If we can't buffer a future message because
5578 * of space limitations -- ignore. */
5579 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",
5580 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5581 (unsigned) hs->buffering.total_bytes_buffered ) );
5582 goto exit;
5583 }
Hanno Beckere1801392018-08-21 16:51:05 +01005584 else
5585 {
5586 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",
5587 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5588 (unsigned) hs->buffering.total_bytes_buffered ) );
5589 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005590
Hanno Beckera02b0b42018-08-21 17:20:27 +01005591 if( ssl_buffer_make_space( ssl, reassembly_buf_sz ) != 0 )
Hanno Becker55e9e2a2018-08-21 16:07:55 +01005592 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005593 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",
5594 (unsigned) msg_len,
5595 (unsigned) reassembly_buf_sz,
5596 MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Hanno Beckere0b150f2018-08-21 15:51:03 +01005597 (unsigned) hs->buffering.total_bytes_buffered ) );
Hanno Becker55e9e2a2018-08-21 16:07:55 +01005598 ret = MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
5599 goto exit;
5600 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005601 }
5602
5603 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %d",
5604 msg_len ) );
5605
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005606 hs_buf->data = mbedtls_calloc( 1, reassembly_buf_sz );
5607 if( hs_buf->data == NULL )
Hanno Becker37f95322018-08-16 13:55:32 +01005608 {
Hanno Beckere0b150f2018-08-21 15:51:03 +01005609 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
Hanno Becker37f95322018-08-16 13:55:32 +01005610 goto exit;
5611 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005612 hs_buf->data_len = reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01005613
5614 /* Prepare final header: copy msg_type, length and message_seq,
5615 * then add standardised fragment_offset and fragment_length */
5616 memcpy( hs_buf->data, ssl->in_msg, 6 );
5617 memset( hs_buf->data + 6, 0, 3 );
5618 memcpy( hs_buf->data + 9, hs_buf->data + 1, 3 );
5619
5620 hs_buf->is_valid = 1;
Hanno Beckere0b150f2018-08-21 15:51:03 +01005621
5622 hs->buffering.total_bytes_buffered += reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01005623 }
5624 else
5625 {
5626 /* Make sure msg_type and length are consistent */
5627 if( memcmp( hs_buf->data, ssl->in_msg, 4 ) != 0 )
5628 {
5629 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Fragment header mismatch - ignore" ) );
5630 /* Ignore */
5631 goto exit;
5632 }
5633 }
5634
Hanno Becker4422bbb2018-08-20 09:40:19 +01005635 if( !hs_buf->is_complete )
Hanno Becker37f95322018-08-16 13:55:32 +01005636 {
5637 size_t frag_len, frag_off;
5638 unsigned char * const msg = hs_buf->data + 12;
5639
5640 /*
5641 * Check and copy current fragment
5642 */
5643
5644 /* Validation of header fields already done in
5645 * mbedtls_ssl_prepare_handshake_record(). */
5646 frag_off = ssl_get_hs_frag_off( ssl );
5647 frag_len = ssl_get_hs_frag_len( ssl );
5648
5649 MBEDTLS_SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %d, length = %d",
5650 frag_off, frag_len ) );
5651 memcpy( msg + frag_off, ssl->in_msg + 12, frag_len );
5652
5653 if( hs_buf->is_fragmented )
5654 {
5655 unsigned char * const bitmask = msg + msg_len;
5656 ssl_bitmask_set( bitmask, frag_off, frag_len );
5657 hs_buf->is_complete = ( ssl_bitmask_check( bitmask,
5658 msg_len ) == 0 );
5659 }
5660 else
5661 {
5662 hs_buf->is_complete = 1;
5663 }
5664
5665 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message %scomplete",
5666 hs_buf->is_complete ? "" : "not yet " ) );
5667 }
5668
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005669 break;
Hanno Becker37f95322018-08-16 13:55:32 +01005670 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005671
5672 default:
Hanno Becker360bef32018-08-28 10:04:33 +01005673 /* We don't buffer other types of messages. */
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005674 break;
5675 }
5676
5677exit:
5678
5679 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_buffer_message" ) );
5680 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005681}
5682#endif /* MBEDTLS_SSL_PROTO_DTLS */
5683
Hanno Becker1097b342018-08-15 14:09:41 +01005684static int ssl_consume_current_message( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005685{
Hanno Becker4a810fb2017-05-24 16:27:30 +01005686 /*
Hanno Becker4a810fb2017-05-24 16:27:30 +01005687 * Consume last content-layer message and potentially
5688 * update in_msglen which keeps track of the contents'
5689 * consumption state.
5690 *
5691 * (1) Handshake messages:
5692 * Remove last handshake message, move content
5693 * and adapt in_msglen.
5694 *
5695 * (2) Alert messages:
5696 * Consume whole record content, in_msglen = 0.
5697 *
Hanno Becker4a810fb2017-05-24 16:27:30 +01005698 * (3) Change cipher spec:
5699 * Consume whole record content, in_msglen = 0.
5700 *
5701 * (4) Application data:
5702 * Don't do anything - the record layer provides
5703 * the application data as a stream transport
5704 * and consumes through mbedtls_ssl_read only.
5705 *
5706 */
5707
5708 /* Case (1): Handshake messages */
5709 if( ssl->in_hslen != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005710 {
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01005711 /* Hard assertion to be sure that no application data
5712 * is in flight, as corrupting ssl->in_msglen during
5713 * ssl->in_offt != NULL is fatal. */
5714 if( ssl->in_offt != NULL )
5715 {
5716 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5717 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5718 }
5719
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005720 /*
5721 * Get next Handshake message in the current record
5722 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005723
Hanno Becker4a810fb2017-05-24 16:27:30 +01005724 /* Notes:
Hanno Beckere72489d2017-10-23 13:23:50 +01005725 * (1) in_hslen is not necessarily the size of the
Hanno Becker4a810fb2017-05-24 16:27:30 +01005726 * current handshake content: If DTLS handshake
5727 * fragmentation is used, that's the fragment
5728 * size instead. Using the total handshake message
Hanno Beckere72489d2017-10-23 13:23:50 +01005729 * size here is faulty and should be changed at
5730 * some point.
Hanno Becker4a810fb2017-05-24 16:27:30 +01005731 * (2) While it doesn't seem to cause problems, one
5732 * has to be very careful not to assume that in_hslen
5733 * is always <= in_msglen in a sensible communication.
5734 * Again, it's wrong for DTLS handshake fragmentation.
5735 * The following check is therefore mandatory, and
5736 * should not be treated as a silently corrected assertion.
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01005737 * Additionally, ssl->in_hslen might be arbitrarily out of
5738 * bounds after handling a DTLS message with an unexpected
5739 * sequence number, see mbedtls_ssl_prepare_handshake_record.
Hanno Becker4a810fb2017-05-24 16:27:30 +01005740 */
5741 if( ssl->in_hslen < ssl->in_msglen )
5742 {
5743 ssl->in_msglen -= ssl->in_hslen;
5744 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
5745 ssl->in_msglen );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005746
Hanno Becker4a810fb2017-05-24 16:27:30 +01005747 MBEDTLS_SSL_DEBUG_BUF( 4, "remaining content in record",
5748 ssl->in_msg, ssl->in_msglen );
5749 }
5750 else
5751 {
5752 ssl->in_msglen = 0;
5753 }
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02005754
Hanno Becker4a810fb2017-05-24 16:27:30 +01005755 ssl->in_hslen = 0;
5756 }
5757 /* Case (4): Application data */
5758 else if( ssl->in_offt != NULL )
5759 {
5760 return( 0 );
5761 }
5762 /* Everything else (CCS & Alerts) */
5763 else
5764 {
5765 ssl->in_msglen = 0;
5766 }
5767
Hanno Becker1097b342018-08-15 14:09:41 +01005768 return( 0 );
5769}
Hanno Becker4a810fb2017-05-24 16:27:30 +01005770
Hanno Beckere74d5562018-08-15 14:26:08 +01005771static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl )
5772{
Hanno Becker4a810fb2017-05-24 16:27:30 +01005773 if( ssl->in_msglen > 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01005774 return( 1 );
5775
5776 return( 0 );
5777}
5778
Hanno Becker5f066e72018-08-16 14:56:31 +01005779#if defined(MBEDTLS_SSL_PROTO_DTLS)
5780
5781static void ssl_free_buffered_record( mbedtls_ssl_context *ssl )
5782{
5783 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5784 if( hs == NULL )
5785 return;
5786
Hanno Becker01315ea2018-08-21 17:22:17 +01005787 if( hs->buffering.future_record.data != NULL )
Hanno Becker4a810fb2017-05-24 16:27:30 +01005788 {
Hanno Becker01315ea2018-08-21 17:22:17 +01005789 hs->buffering.total_bytes_buffered -=
5790 hs->buffering.future_record.len;
5791
5792 mbedtls_free( hs->buffering.future_record.data );
5793 hs->buffering.future_record.data = NULL;
5794 }
Hanno Becker5f066e72018-08-16 14:56:31 +01005795}
5796
5797static int ssl_load_buffered_record( mbedtls_ssl_context *ssl )
5798{
5799 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5800 unsigned char * rec;
5801 size_t rec_len;
5802 unsigned rec_epoch;
5803
5804 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5805 return( 0 );
5806
5807 if( hs == NULL )
5808 return( 0 );
5809
Hanno Becker5f066e72018-08-16 14:56:31 +01005810 rec = hs->buffering.future_record.data;
5811 rec_len = hs->buffering.future_record.len;
5812 rec_epoch = hs->buffering.future_record.epoch;
5813
5814 if( rec == NULL )
5815 return( 0 );
5816
Hanno Becker4cb782d2018-08-20 11:19:05 +01005817 /* Only consider loading future records if the
5818 * input buffer is empty. */
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005819 if( ssl_next_record_is_in_datagram( ssl ) == 1 )
Hanno Becker4cb782d2018-08-20 11:19:05 +01005820 return( 0 );
5821
Hanno Becker5f066e72018-08-16 14:56:31 +01005822 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_record" ) );
5823
5824 if( rec_epoch != ssl->in_epoch )
5825 {
5826 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffered record not from current epoch." ) );
5827 goto exit;
5828 }
5829
5830 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Found buffered record from current epoch - load" ) );
5831
5832 /* Double-check that the record is not too large */
5833 if( rec_len > MBEDTLS_SSL_IN_BUFFER_LEN -
5834 (size_t)( ssl->in_hdr - ssl->in_buf ) )
5835 {
5836 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5837 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5838 }
5839
5840 memcpy( ssl->in_hdr, rec, rec_len );
5841 ssl->in_left = rec_len;
5842 ssl->next_record_offset = 0;
5843
5844 ssl_free_buffered_record( ssl );
5845
5846exit:
5847 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_record" ) );
5848 return( 0 );
5849}
5850
5851static int ssl_buffer_future_record( mbedtls_ssl_context *ssl )
5852{
5853 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5854 size_t const rec_hdr_len = 13;
Hanno Becker01315ea2018-08-21 17:22:17 +01005855 size_t const total_buf_sz = rec_hdr_len + ssl->in_msglen;
Hanno Becker5f066e72018-08-16 14:56:31 +01005856
5857 /* Don't buffer future records outside handshakes. */
5858 if( hs == NULL )
5859 return( 0 );
5860
5861 /* Only buffer handshake records (we are only interested
5862 * in Finished messages). */
5863 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
5864 return( 0 );
5865
5866 /* Don't buffer more than one future epoch record. */
5867 if( hs->buffering.future_record.data != NULL )
5868 return( 0 );
5869
Hanno Becker01315ea2018-08-21 17:22:17 +01005870 /* Don't buffer record if there's not enough buffering space remaining. */
5871 if( total_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5872 hs->buffering.total_bytes_buffered ) )
5873 {
5874 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",
5875 (unsigned) total_buf_sz, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5876 (unsigned) hs->buffering.total_bytes_buffered ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005877 return( 0 );
5878 }
5879
Hanno Becker5f066e72018-08-16 14:56:31 +01005880 /* Buffer record */
5881 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffer record from epoch %u",
5882 ssl->in_epoch + 1 ) );
5883 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered record", ssl->in_hdr,
5884 rec_hdr_len + ssl->in_msglen );
5885
5886 /* ssl_parse_record_header() only considers records
5887 * of the next epoch as candidates for buffering. */
5888 hs->buffering.future_record.epoch = ssl->in_epoch + 1;
Hanno Becker01315ea2018-08-21 17:22:17 +01005889 hs->buffering.future_record.len = total_buf_sz;
Hanno Becker5f066e72018-08-16 14:56:31 +01005890
5891 hs->buffering.future_record.data =
5892 mbedtls_calloc( 1, hs->buffering.future_record.len );
5893 if( hs->buffering.future_record.data == NULL )
5894 {
5895 /* If we run out of RAM trying to buffer a
5896 * record from the next epoch, just ignore. */
5897 return( 0 );
5898 }
5899
Hanno Becker01315ea2018-08-21 17:22:17 +01005900 memcpy( hs->buffering.future_record.data, ssl->in_hdr, total_buf_sz );
Hanno Becker5f066e72018-08-16 14:56:31 +01005901
Hanno Becker01315ea2018-08-21 17:22:17 +01005902 hs->buffering.total_bytes_buffered += total_buf_sz;
Hanno Becker5f066e72018-08-16 14:56:31 +01005903 return( 0 );
5904}
5905
5906#endif /* MBEDTLS_SSL_PROTO_DTLS */
5907
Hanno Beckere74d5562018-08-15 14:26:08 +01005908static int ssl_get_next_record( mbedtls_ssl_context *ssl )
Hanno Becker1097b342018-08-15 14:09:41 +01005909{
5910 int ret;
5911
Hanno Becker5f066e72018-08-16 14:56:31 +01005912#if defined(MBEDTLS_SSL_PROTO_DTLS)
5913 /* We might have buffered a future record; if so,
5914 * and if the epoch matches now, load it.
5915 * On success, this call will set ssl->in_left to
5916 * the length of the buffered record, so that
5917 * the calls to ssl_fetch_input() below will
5918 * essentially be no-ops. */
5919 ret = ssl_load_buffered_record( ssl );
5920 if( ret != 0 )
5921 return( ret );
5922#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker4a810fb2017-05-24 16:27:30 +01005923
Hanno Beckerca59c2b2019-05-08 12:03:28 +01005924 /* Reset in pointers to default state for TLS/DTLS records,
5925 * assuming no CID and no offset between record content and
5926 * record plaintext. */
5927 ssl_update_in_pointers( ssl );
5928
5929 /* Ensure that we have enough space available for the default form
5930 * of TLS / DTLS record headers (5 Bytes for TLS, 13 Bytes for DTLS,
5931 * with no space for CIDs counted in). */
5932 ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_in_hdr_len( ssl ) );
5933 if( ret != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005934 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005935 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005936 return( ret );
5937 }
5938
5939 if( ( ret = ssl_parse_record_header( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005940 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005941#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02005942 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5943 ret != MBEDTLS_ERR_SSL_CLIENT_RECONNECT )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005944 {
Hanno Becker5f066e72018-08-16 14:56:31 +01005945 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
5946 {
5947 ret = ssl_buffer_future_record( ssl );
5948 if( ret != 0 )
5949 return( ret );
5950
5951 /* Fall through to handling of unexpected records */
5952 ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
5953 }
5954
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005955 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD )
5956 {
5957 /* Skip unexpected record (but not whole datagram) */
5958 ssl->next_record_offset = ssl->in_msglen
Hanno Becker5903de42019-05-03 14:46:38 +01005959 + mbedtls_ssl_in_hdr_len( ssl );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005960
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005961 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding unexpected record "
5962 "(header)" ) );
5963 }
5964 else
5965 {
5966 /* Skip invalid record and the rest of the datagram */
5967 ssl->next_record_offset = 0;
5968 ssl->in_left = 0;
5969
5970 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record "
5971 "(header)" ) );
5972 }
5973
5974 /* Get next record */
Hanno Becker90333da2017-10-10 11:27:13 +01005975 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005976 }
5977#endif
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005978 return( ret );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005979 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005980
5981 /*
5982 * Read and optionally decrypt the message contents
5983 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005984 if( ( ret = mbedtls_ssl_fetch_input( ssl,
Hanno Becker5903de42019-05-03 14:46:38 +01005985 mbedtls_ssl_in_hdr_len( ssl ) + ssl->in_msglen ) ) != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005986 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005987 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005988 return( ret );
5989 }
5990
5991 /* Done reading this record, get ready for the next one */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005992#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005993 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Beckere65ce782017-05-22 14:47:48 +01005994 {
Hanno Becker5903de42019-05-03 14:46:38 +01005995 ssl->next_record_offset = ssl->in_msglen + mbedtls_ssl_in_hdr_len( ssl );
Hanno Beckere65ce782017-05-22 14:47:48 +01005996 if( ssl->next_record_offset < ssl->in_left )
5997 {
5998 MBEDTLS_SSL_DEBUG_MSG( 3, ( "more than one record within datagram" ) );
5999 }
6000 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006001 else
6002#endif
6003 ssl->in_left = 0;
6004
6005 if( ( ret = ssl_prepare_record_content( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006006 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006007#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006008 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006009 {
6010 /* Silently discard invalid records */
Hanno Becker82e2a392019-05-03 16:36:59 +01006011 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006012 {
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02006013 /* Except when waiting for Finished as a bad mac here
6014 * probably means something went wrong in the handshake
6015 * (eg wrong psk used, mitm downgrade attempt, etc.) */
6016 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED ||
6017 ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
6018 {
6019#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
6020 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
6021 {
6022 mbedtls_ssl_send_alert_message( ssl,
6023 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6024 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
6025 }
6026#endif
6027 return( ret );
6028 }
6029
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006030#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006031 if( ssl->conf->badmac_limit != 0 &&
6032 ++ssl->badmac_seen >= ssl->conf->badmac_limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02006033 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006034 MBEDTLS_SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) );
6035 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02006036 }
6037#endif
6038
Hanno Becker4a810fb2017-05-24 16:27:30 +01006039 /* As above, invalid records cause
6040 * dismissal of the whole datagram. */
6041
6042 ssl->next_record_offset = 0;
6043 ssl->in_left = 0;
6044
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006045 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record (mac)" ) );
Hanno Becker90333da2017-10-10 11:27:13 +01006046 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006047 }
6048
6049 return( ret );
6050 }
6051 else
6052#endif
6053 {
6054 /* Error out (and send alert) on invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006055#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
6056 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006057 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006058 mbedtls_ssl_send_alert_message( ssl,
6059 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6060 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006061 }
6062#endif
6063 return( ret );
6064 }
6065 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006066
Simon Butcher99000142016-10-13 17:21:01 +01006067 return( 0 );
6068}
6069
6070int mbedtls_ssl_handle_message_type( mbedtls_ssl_context *ssl )
6071{
6072 int ret;
6073
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006074 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006075 * Handle particular types of records
6076 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006077 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00006078 {
Simon Butcher99000142016-10-13 17:21:01 +01006079 if( ( ret = mbedtls_ssl_prepare_handshake_record( ssl ) ) != 0 )
6080 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01006081 return( ret );
Simon Butcher99000142016-10-13 17:21:01 +01006082 }
Paul Bakker5121ce52009-01-03 21:22:43 +00006083 }
6084
Hanno Beckere678eaa2018-08-21 14:57:46 +01006085 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006086 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01006087 if( ssl->in_msglen != 1 )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006088 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01006089 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, len: %d",
6090 ssl->in_msglen ) );
6091 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006092 }
6093
Hanno Beckere678eaa2018-08-21 14:57:46 +01006094 if( ssl->in_msg[0] != 1 )
6095 {
6096 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, content: %02x",
6097 ssl->in_msg[0] ) );
6098 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
6099 }
6100
6101#if defined(MBEDTLS_SSL_PROTO_DTLS)
6102 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
6103 ssl->state != MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC &&
6104 ssl->state != MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
6105 {
6106 if( ssl->handshake == NULL )
6107 {
6108 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping ChangeCipherSpec outside handshake" ) );
6109 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
6110 }
6111
6112 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received out-of-order ChangeCipherSpec - remember" ) );
6113 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
6114 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006115#endif
Hanno Beckere678eaa2018-08-21 14:57:46 +01006116 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006117
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006118 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00006119 {
Angus Gratton1a7a17e2018-06-20 15:43:50 +10006120 if( ssl->in_msglen != 2 )
6121 {
6122 /* Note: Standard allows for more than one 2 byte alert
6123 to be packed in a single message, but Mbed TLS doesn't
6124 currently support this. */
6125 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid alert message, len: %d",
6126 ssl->in_msglen ) );
6127 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
6128 }
6129
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006130 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
Paul Bakker5121ce52009-01-03 21:22:43 +00006131 ssl->in_msg[0], ssl->in_msg[1] ) );
6132
6133 /*
Simon Butcher459a9502015-10-27 16:09:03 +00006134 * Ignore non-fatal alerts, except close_notify and no_renegotiation
Paul Bakker5121ce52009-01-03 21:22:43 +00006135 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006136 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006137 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006138 MBEDTLS_SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
Paul Bakker2770fbd2012-07-03 13:30:23 +00006139 ssl->in_msg[1] ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006140 return( MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006141 }
6142
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006143 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6144 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00006145 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006146 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
6147 return( MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00006148 }
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02006149
6150#if defined(MBEDTLS_SSL_RENEGOTIATION_ENABLED)
6151 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6152 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION )
6153 {
Hanno Becker90333da2017-10-10 11:27:13 +01006154 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no renegotiation alert" ) );
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02006155 /* Will be handled when trying to parse ServerHello */
6156 return( 0 );
6157 }
6158#endif
6159
6160#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_SRV_C)
6161 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
6162 ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
6163 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6164 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
6165 {
6166 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no_cert" ) );
6167 /* Will be handled in mbedtls_ssl_parse_certificate() */
6168 return( 0 );
6169 }
6170#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
6171
6172 /* Silently ignore: fetch new message */
Simon Butcher99000142016-10-13 17:21:01 +01006173 return MBEDTLS_ERR_SSL_NON_FATAL;
Paul Bakker5121ce52009-01-03 21:22:43 +00006174 }
6175
Hanno Beckerc76c6192017-06-06 10:03:17 +01006176#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker37ae9522019-05-03 16:54:26 +01006177 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Beckerc76c6192017-06-06 10:03:17 +01006178 {
Hanno Becker37ae9522019-05-03 16:54:26 +01006179 /* Drop unexpected ApplicationData records,
6180 * except at the beginning of renegotiations */
6181 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA &&
6182 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER
6183#if defined(MBEDTLS_SSL_RENEGOTIATION)
6184 && ! ( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
6185 ssl->state == MBEDTLS_SSL_SERVER_HELLO )
Hanno Beckerc76c6192017-06-06 10:03:17 +01006186#endif
Hanno Becker37ae9522019-05-03 16:54:26 +01006187 )
6188 {
6189 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) );
6190 return( MBEDTLS_ERR_SSL_NON_FATAL );
6191 }
6192
6193 if( ssl->handshake != NULL &&
6194 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
6195 {
6196 ssl_handshake_wrapup_free_hs_transform( ssl );
6197 }
6198 }
Hanno Becker4a4af9f2019-05-08 16:26:21 +01006199#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Beckerc76c6192017-06-06 10:03:17 +01006200
Paul Bakker5121ce52009-01-03 21:22:43 +00006201 return( 0 );
6202}
6203
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006204int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00006205{
6206 int ret;
6207
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006208 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
6209 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6210 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00006211 {
6212 return( ret );
6213 }
6214
6215 return( 0 );
6216}
6217
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006218int mbedtls_ssl_send_alert_message( mbedtls_ssl_context *ssl,
Paul Bakker0a925182012-04-16 06:46:41 +00006219 unsigned char level,
6220 unsigned char message )
6221{
6222 int ret;
6223
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02006224 if( ssl == NULL || ssl->conf == NULL )
6225 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6226
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006227 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006228 MBEDTLS_SSL_DEBUG_MSG( 3, ( "send alert level=%u message=%u", level, message ));
Paul Bakker0a925182012-04-16 06:46:41 +00006229
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006230 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
Paul Bakker0a925182012-04-16 06:46:41 +00006231 ssl->out_msglen = 2;
6232 ssl->out_msg[0] = level;
6233 ssl->out_msg[1] = message;
6234
Hanno Becker67bc7c32018-08-06 11:33:50 +01006235 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00006236 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006237 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker0a925182012-04-16 06:46:41 +00006238 return( ret );
6239 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006240 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
Paul Bakker0a925182012-04-16 06:46:41 +00006241
6242 return( 0 );
6243}
6244
Hanno Beckerb9d44792019-02-08 07:19:04 +00006245#if defined(MBEDTLS_X509_CRT_PARSE_C)
6246static void ssl_clear_peer_cert( mbedtls_ssl_session *session )
6247{
6248#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6249 if( session->peer_cert != NULL )
6250 {
6251 mbedtls_x509_crt_free( session->peer_cert );
6252 mbedtls_free( session->peer_cert );
6253 session->peer_cert = NULL;
6254 }
6255#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6256 if( session->peer_cert_digest != NULL )
6257 {
6258 /* Zeroization is not necessary. */
6259 mbedtls_free( session->peer_cert_digest );
6260 session->peer_cert_digest = NULL;
6261 session->peer_cert_digest_type = MBEDTLS_MD_NONE;
6262 session->peer_cert_digest_len = 0;
6263 }
6264#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6265}
6266#endif /* MBEDTLS_X509_CRT_PARSE_C */
6267
Paul Bakker5121ce52009-01-03 21:22:43 +00006268/*
6269 * Handshake functions
6270 */
Hanno Becker21489932019-02-05 13:20:55 +00006271#if !defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Gilles Peskinef9828522017-05-03 12:28:43 +02006272/* No certificate support -> dummy functions */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006273int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006274{
Hanno Beckere694c3e2017-12-27 21:34:08 +00006275 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6276 ssl->handshake->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00006277
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006278 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006279
Hanno Becker7177a882019-02-05 13:36:46 +00006280 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02006281 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006282 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02006283 ssl->state++;
6284 return( 0 );
6285 }
6286
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006287 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6288 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006289}
6290
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006291int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006292{
Hanno Beckere694c3e2017-12-27 21:34:08 +00006293 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6294 ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006295
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006296 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006297
Hanno Becker7177a882019-02-05 13:36:46 +00006298 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006299 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006300 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006301 ssl->state++;
6302 return( 0 );
6303 }
6304
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006305 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6306 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006307}
Gilles Peskinef9828522017-05-03 12:28:43 +02006308
Hanno Becker21489932019-02-05 13:20:55 +00006309#else /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Gilles Peskinef9828522017-05-03 12:28:43 +02006310/* Some certificate support -> implement write and parse */
6311
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006312int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006313{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006314 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006315 size_t i, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006316 const mbedtls_x509_crt *crt;
Hanno Beckere694c3e2017-12-27 21:34:08 +00006317 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6318 ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006319
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006320 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006321
Hanno Becker7177a882019-02-05 13:36:46 +00006322 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006323 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006324 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006325 ssl->state++;
6326 return( 0 );
6327 }
6328
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006329#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006330 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00006331 {
6332 if( ssl->client_auth == 0 )
6333 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006334 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006335 ssl->state++;
6336 return( 0 );
6337 }
6338
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006339#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00006340 /*
6341 * If using SSLv3 and got no cert, send an Alert message
6342 * (otherwise an empty Certificate message will be sent).
6343 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006344 if( mbedtls_ssl_own_cert( ssl ) == NULL &&
6345 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006346 {
6347 ssl->out_msglen = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006348 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
6349 ssl->out_msg[0] = MBEDTLS_SSL_ALERT_LEVEL_WARNING;
6350 ssl->out_msg[1] = MBEDTLS_SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00006351
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006352 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006353 goto write_msg;
6354 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006355#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006356 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006357#endif /* MBEDTLS_SSL_CLI_C */
6358#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006359 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00006360 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006361 if( mbedtls_ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006362 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006363 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
6364 return( MBEDTLS_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00006365 }
6366 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006367#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006368
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006369 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006370
6371 /*
6372 * 0 . 0 handshake type
6373 * 1 . 3 handshake length
6374 * 4 . 6 length of all certs
6375 * 7 . 9 length of cert. 1
6376 * 10 . n-1 peer certificate
6377 * n . n+2 length of cert. 2
6378 * n+3 . ... upper level cert, etc.
6379 */
6380 i = 7;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006381 crt = mbedtls_ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00006382
Paul Bakker29087132010-03-21 21:03:34 +00006383 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006384 {
6385 n = crt->raw.len;
Angus Grattond8213d02016-05-25 20:56:48 +10006386 if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00006387 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006388 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
Angus Grattond8213d02016-05-25 20:56:48 +10006389 i + 3 + n, MBEDTLS_SSL_OUT_CONTENT_LEN ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006390 return( MBEDTLS_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006391 }
6392
6393 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
6394 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
6395 ssl->out_msg[i + 2] = (unsigned char)( n );
6396
6397 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
6398 i += n; crt = crt->next;
6399 }
6400
6401 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
6402 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
6403 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
6404
6405 ssl->out_msglen = i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006406 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
6407 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
Paul Bakker5121ce52009-01-03 21:22:43 +00006408
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02006409#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00006410write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006411#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006412
6413 ssl->state++;
6414
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006415 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006416 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006417 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006418 return( ret );
6419 }
6420
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006421 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006422
Paul Bakkered27a042013-04-18 22:46:23 +02006423 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006424}
6425
Hanno Becker84879e32019-01-31 07:44:03 +00006426#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Hanno Becker177475a2019-02-05 17:02:46 +00006427
6428#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006429static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6430 unsigned char *crt_buf,
6431 size_t crt_buf_len )
6432{
6433 mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert;
6434
6435 if( peer_crt == NULL )
6436 return( -1 );
6437
6438 if( peer_crt->raw.len != crt_buf_len )
6439 return( -1 );
6440
Hanno Becker46f34d02019-02-08 14:00:04 +00006441 return( memcmp( peer_crt->raw.p, crt_buf, crt_buf_len ) );
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006442}
Hanno Becker177475a2019-02-05 17:02:46 +00006443#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6444static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6445 unsigned char *crt_buf,
6446 size_t crt_buf_len )
6447{
6448 int ret;
6449 unsigned char const * const peer_cert_digest =
6450 ssl->session->peer_cert_digest;
6451 mbedtls_md_type_t const peer_cert_digest_type =
6452 ssl->session->peer_cert_digest_type;
6453 mbedtls_md_info_t const * const digest_info =
6454 mbedtls_md_info_from_type( peer_cert_digest_type );
6455 unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN];
6456 size_t digest_len;
6457
6458 if( peer_cert_digest == NULL || digest_info == NULL )
6459 return( -1 );
6460
6461 digest_len = mbedtls_md_get_size( digest_info );
6462 if( digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN )
6463 return( -1 );
6464
6465 ret = mbedtls_md( digest_info, crt_buf, crt_buf_len, tmp_digest );
6466 if( ret != 0 )
6467 return( -1 );
6468
6469 return( memcmp( tmp_digest, peer_cert_digest, digest_len ) );
6470}
6471#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker84879e32019-01-31 07:44:03 +00006472#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006473
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006474/*
6475 * Once the certificate message is read, parse it into a cert chain and
6476 * perform basic checks, but leave actual verification to the caller
6477 */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006478static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl,
6479 mbedtls_x509_crt *chain )
Paul Bakker5121ce52009-01-03 21:22:43 +00006480{
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006481 int ret;
6482#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6483 int crt_cnt=0;
6484#endif
Paul Bakker23986e52011-04-24 08:57:21 +00006485 size_t i, n;
Gilles Peskine064a85c2017-05-10 10:46:40 +02006486 uint8_t alert;
Paul Bakker5121ce52009-01-03 21:22:43 +00006487
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006488 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00006489 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006490 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006491 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6492 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006493 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006494 }
6495
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006496 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE ||
6497 ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006498 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006499 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006500 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6501 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006502 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006503 }
6504
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006505 i = mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006506
Paul Bakker5121ce52009-01-03 21:22:43 +00006507 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006508 * Same message structure as in mbedtls_ssl_write_certificate()
Paul Bakker5121ce52009-01-03 21:22:43 +00006509 */
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006510 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
Paul Bakker5121ce52009-01-03 21:22:43 +00006511
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006512 if( ssl->in_msg[i] != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006513 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00006514 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006515 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006516 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6517 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006518 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006519 }
6520
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006521 /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */
6522 i += 3;
6523
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006524 /* Iterate through and parse the CRTs in the provided chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006525 while( i < ssl->in_hslen )
6526 {
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006527 /* Check that there's room for the next CRT's length fields. */
Philippe Antoine747fd532018-05-30 09:13:21 +02006528 if ( i + 3 > ssl->in_hslen ) {
6529 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckere2734e22019-01-31 07:44:17 +00006530 mbedtls_ssl_send_alert_message( ssl,
6531 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6532 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Philippe Antoine747fd532018-05-30 09:13:21 +02006533 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
6534 }
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006535 /* In theory, the CRT can be up to 2**24 Bytes, but we don't support
6536 * anything beyond 2**16 ~ 64K. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006537 if( ssl->in_msg[i] != 0 )
6538 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006539 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckere2734e22019-01-31 07:44:17 +00006540 mbedtls_ssl_send_alert_message( ssl,
6541 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6542 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006543 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006544 }
6545
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006546 /* Read length of the next CRT in the chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006547 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
6548 | (unsigned int) ssl->in_msg[i + 2];
6549 i += 3;
6550
6551 if( n < 128 || i + n > ssl->in_hslen )
6552 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006553 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckere2734e22019-01-31 07:44:17 +00006554 mbedtls_ssl_send_alert_message( ssl,
6555 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6556 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006557 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006558 }
6559
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006560 /* Check if we're handling the first CRT in the chain. */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006561#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6562 if( crt_cnt++ == 0 &&
6563 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
6564 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006565 {
Hanno Becker46f34d02019-02-08 14:00:04 +00006566 /* During client-side renegotiation, check that the server's
6567 * end-CRTs hasn't changed compared to the initial handshake,
6568 * mitigating the triple handshake attack. On success, reuse
6569 * the original end-CRT instead of parsing it again. */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006570 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Check that peer CRT hasn't changed during renegotiation" ) );
6571 if( ssl_check_peer_crt_unchanged( ssl,
6572 &ssl->in_msg[i],
6573 n ) != 0 )
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006574 {
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006575 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
6576 mbedtls_ssl_send_alert_message( ssl,
6577 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6578 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
6579 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006580 }
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006581
6582 /* Now we can safely free the original chain. */
6583 ssl_clear_peer_cert( ssl->session );
6584 }
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006585#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
6586
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006587 /* Parse the next certificate in the chain. */
Hanno Becker0056eab2019-02-08 14:39:16 +00006588#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006589 ret = mbedtls_x509_crt_parse_der( chain, ssl->in_msg + i, n );
Hanno Becker0056eab2019-02-08 14:39:16 +00006590#else
Hanno Becker353a6f02019-02-26 11:51:34 +00006591 /* If we don't need to store the CRT chain permanently, parse
Hanno Becker0056eab2019-02-08 14:39:16 +00006592 * it in-place from the input buffer instead of making a copy. */
6593 ret = mbedtls_x509_crt_parse_der_nocopy( chain, ssl->in_msg + i, n );
6594#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006595 switch( ret )
Paul Bakker5121ce52009-01-03 21:22:43 +00006596 {
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006597 case 0: /*ok*/
6598 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
6599 /* Ignore certificate with an unknown algorithm: maybe a
6600 prior certificate was already trusted. */
6601 break;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006602
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006603 case MBEDTLS_ERR_X509_ALLOC_FAILED:
6604 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
6605 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006606
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006607 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
6608 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6609 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006610
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006611 default:
6612 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6613 crt_parse_der_failed:
6614 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert );
6615 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
6616 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006617 }
6618
6619 i += n;
6620 }
6621
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006622 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", chain );
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006623 return( 0 );
6624}
6625
Hanno Becker4a55f632019-02-05 12:49:06 +00006626#if defined(MBEDTLS_SSL_SRV_C)
6627static int ssl_srv_check_client_no_crt_notification( mbedtls_ssl_context *ssl )
6628{
6629 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
6630 return( -1 );
6631
6632#if defined(MBEDTLS_SSL_PROTO_SSL3)
6633 /*
6634 * Check if the client sent an empty certificate
6635 */
6636 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
6637 {
6638 if( ssl->in_msglen == 2 &&
6639 ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT &&
6640 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6641 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
6642 {
6643 MBEDTLS_SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
6644 return( 0 );
6645 }
6646
6647 return( -1 );
6648 }
6649#endif /* MBEDTLS_SSL_PROTO_SSL3 */
6650
6651#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
6652 defined(MBEDTLS_SSL_PROTO_TLS1_2)
6653 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
6654 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
6655 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
6656 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
6657 {
6658 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
6659 return( 0 );
6660 }
6661
6662 return( -1 );
6663#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
6664 MBEDTLS_SSL_PROTO_TLS1_2 */
6665}
6666#endif /* MBEDTLS_SSL_SRV_C */
6667
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006668/* Check if a certificate message is expected.
6669 * Return either
6670 * - SSL_CERTIFICATE_EXPECTED, or
6671 * - SSL_CERTIFICATE_SKIP
6672 * indicating whether a Certificate message is expected or not.
6673 */
6674#define SSL_CERTIFICATE_EXPECTED 0
6675#define SSL_CERTIFICATE_SKIP 1
6676static int ssl_parse_certificate_coordinate( mbedtls_ssl_context *ssl,
6677 int authmode )
6678{
6679 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00006680 ssl->handshake->ciphersuite_info;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006681
6682 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6683 return( SSL_CERTIFICATE_SKIP );
6684
6685#if defined(MBEDTLS_SSL_SRV_C)
6686 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
6687 {
6688 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
6689 return( SSL_CERTIFICATE_SKIP );
6690
6691 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6692 {
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006693 ssl->session_negotiate->verify_result =
6694 MBEDTLS_X509_BADCERT_SKIP_VERIFY;
6695 return( SSL_CERTIFICATE_SKIP );
6696 }
6697 }
Hanno Becker84d9d272019-03-01 08:10:46 +00006698#else
6699 ((void) authmode);
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006700#endif /* MBEDTLS_SSL_SRV_C */
6701
6702 return( SSL_CERTIFICATE_EXPECTED );
6703}
6704
Hanno Becker68636192019-02-05 14:36:34 +00006705static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl,
6706 int authmode,
6707 mbedtls_x509_crt *chain,
6708 void *rs_ctx )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006709{
Hanno Becker6bdfab22019-02-05 13:11:17 +00006710 int ret = 0;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006711 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00006712 ssl->handshake->ciphersuite_info;
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006713 int have_ca_chain = 0;
Hanno Becker68636192019-02-05 14:36:34 +00006714
Hanno Becker8927c832019-04-03 12:52:50 +01006715 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *);
6716 void *p_vrfy;
6717
Hanno Becker68636192019-02-05 14:36:34 +00006718 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6719 return( 0 );
6720
Hanno Becker8927c832019-04-03 12:52:50 +01006721 if( ssl->f_vrfy != NULL )
6722 {
Hanno Beckerefb440a2019-04-03 13:04:33 +01006723 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use context-specific verification callback" ) );
Hanno Becker8927c832019-04-03 12:52:50 +01006724 f_vrfy = ssl->f_vrfy;
6725 p_vrfy = ssl->p_vrfy;
6726 }
6727 else
6728 {
Hanno Beckerefb440a2019-04-03 13:04:33 +01006729 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use configuration-specific verification callback" ) );
Hanno Becker8927c832019-04-03 12:52:50 +01006730 f_vrfy = ssl->conf->f_vrfy;
6731 p_vrfy = ssl->conf->p_vrfy;
6732 }
6733
Hanno Becker68636192019-02-05 14:36:34 +00006734 /*
6735 * Main check: verify certificate
6736 */
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006737#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
6738 if( ssl->conf->f_ca_cb != NULL )
6739 {
6740 ((void) rs_ctx);
6741 have_ca_chain = 1;
6742
6743 MBEDTLS_SSL_DEBUG_MSG( 3, ( "use CA callback for X.509 CRT verification" ) );
Jarno Lamsa9822c0d2019-04-01 16:59:48 +03006744 ret = mbedtls_x509_crt_verify_with_ca_cb(
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006745 chain,
6746 ssl->conf->f_ca_cb,
6747 ssl->conf->p_ca_cb,
6748 ssl->conf->cert_profile,
6749 ssl->hostname,
6750 &ssl->session_negotiate->verify_result,
Jaeden Amerofe710672019-04-16 15:03:12 +01006751 f_vrfy, p_vrfy );
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006752 }
6753 else
6754#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
6755 {
6756 mbedtls_x509_crt *ca_chain;
6757 mbedtls_x509_crl *ca_crl;
6758
6759#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6760 if( ssl->handshake->sni_ca_chain != NULL )
6761 {
6762 ca_chain = ssl->handshake->sni_ca_chain;
6763 ca_crl = ssl->handshake->sni_ca_crl;
6764 }
6765 else
6766#endif
6767 {
6768 ca_chain = ssl->conf->ca_chain;
6769 ca_crl = ssl->conf->ca_crl;
6770 }
6771
6772 if( ca_chain != NULL )
6773 have_ca_chain = 1;
6774
6775 ret = mbedtls_x509_crt_verify_restartable(
6776 chain,
6777 ca_chain, ca_crl,
6778 ssl->conf->cert_profile,
6779 ssl->hostname,
6780 &ssl->session_negotiate->verify_result,
Jaeden Amerofe710672019-04-16 15:03:12 +01006781 f_vrfy, p_vrfy, rs_ctx );
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006782 }
Hanno Becker68636192019-02-05 14:36:34 +00006783
6784 if( ret != 0 )
6785 {
6786 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
6787 }
6788
6789#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6790 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
6791 return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS );
6792#endif
6793
6794 /*
6795 * Secondary checks: always done, but change 'ret' only if it was 0
6796 */
6797
6798#if defined(MBEDTLS_ECP_C)
6799 {
6800 const mbedtls_pk_context *pk = &chain->pk;
6801
6802 /* If certificate uses an EC key, make sure the curve is OK */
6803 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) &&
6804 mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id ) != 0 )
6805 {
6806 ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
6807
6808 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
6809 if( ret == 0 )
6810 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
6811 }
6812 }
6813#endif /* MBEDTLS_ECP_C */
6814
6815 if( mbedtls_ssl_check_cert_usage( chain,
6816 ciphersuite_info,
6817 ! ssl->conf->endpoint,
6818 &ssl->session_negotiate->verify_result ) != 0 )
6819 {
6820 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
6821 if( ret == 0 )
6822 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
6823 }
6824
6825 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
6826 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
6827 * with details encoded in the verification flags. All other kinds
6828 * of error codes, including those from the user provided f_vrfy
6829 * functions, are treated as fatal and lead to a failure of
6830 * ssl_parse_certificate even if verification was optional. */
6831 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
6832 ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
6833 ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ) )
6834 {
6835 ret = 0;
6836 }
6837
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006838 if( have_ca_chain == 0 && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
Hanno Becker68636192019-02-05 14:36:34 +00006839 {
6840 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
6841 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
6842 }
6843
6844 if( ret != 0 )
6845 {
6846 uint8_t alert;
6847
6848 /* The certificate may have been rejected for several reasons.
6849 Pick one and send the corresponding alert. Which alert to send
6850 may be a subject of debate in some cases. */
6851 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
6852 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
6853 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
6854 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6855 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
6856 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6857 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
6858 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6859 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
6860 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6861 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
6862 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6863 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
6864 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6865 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
6866 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
6867 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
6868 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
6869 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
6870 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
6871 else
6872 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
6873 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6874 alert );
6875 }
6876
6877#if defined(MBEDTLS_DEBUG_C)
6878 if( ssl->session_negotiate->verify_result != 0 )
6879 {
6880 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %x",
6881 ssl->session_negotiate->verify_result ) );
6882 }
6883 else
6884 {
6885 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
6886 }
6887#endif /* MBEDTLS_DEBUG_C */
6888
6889 return( ret );
6890}
6891
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006892#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6893static int ssl_remember_peer_crt_digest( mbedtls_ssl_context *ssl,
6894 unsigned char *start, size_t len )
6895{
6896 int ret;
6897 /* Remember digest of the peer's end-CRT. */
6898 ssl->session_negotiate->peer_cert_digest =
6899 mbedtls_calloc( 1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN );
6900 if( ssl->session_negotiate->peer_cert_digest == NULL )
6901 {
6902 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
6903 sizeof( MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ) ) );
6904 mbedtls_ssl_send_alert_message( ssl,
6905 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6906 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
6907
6908 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
6909 }
6910
6911 ret = mbedtls_md( mbedtls_md_info_from_type(
6912 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE ),
6913 start, len,
6914 ssl->session_negotiate->peer_cert_digest );
6915
6916 ssl->session_negotiate->peer_cert_digest_type =
6917 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
6918 ssl->session_negotiate->peer_cert_digest_len =
6919 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
6920
6921 return( ret );
6922}
6923
6924static int ssl_remember_peer_pubkey( mbedtls_ssl_context *ssl,
6925 unsigned char *start, size_t len )
6926{
6927 unsigned char *end = start + len;
6928 int ret;
6929
6930 /* Make a copy of the peer's raw public key. */
6931 mbedtls_pk_init( &ssl->handshake->peer_pubkey );
6932 ret = mbedtls_pk_parse_subpubkey( &start, end,
6933 &ssl->handshake->peer_pubkey );
6934 if( ret != 0 )
6935 {
6936 /* We should have parsed the public key before. */
6937 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6938 }
6939
6940 return( 0 );
6941}
6942#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6943
Hanno Becker68636192019-02-05 14:36:34 +00006944int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
6945{
6946 int ret = 0;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006947 int crt_expected;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006948#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6949 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
6950 ? ssl->handshake->sni_authmode
6951 : ssl->conf->authmode;
6952#else
6953 const int authmode = ssl->conf->authmode;
6954#endif
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006955 void *rs_ctx = NULL;
Hanno Becker3dad3112019-02-05 17:19:52 +00006956 mbedtls_x509_crt *chain = NULL;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006957
6958 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
6959
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006960 crt_expected = ssl_parse_certificate_coordinate( ssl, authmode );
6961 if( crt_expected == SSL_CERTIFICATE_SKIP )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006962 {
6963 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Hanno Becker6bdfab22019-02-05 13:11:17 +00006964 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006965 }
6966
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006967#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6968 if( ssl->handshake->ecrs_enabled &&
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02006969 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify )
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006970 {
Hanno Becker3dad3112019-02-05 17:19:52 +00006971 chain = ssl->handshake->ecrs_peer_cert;
6972 ssl->handshake->ecrs_peer_cert = NULL;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006973 goto crt_verify;
6974 }
6975#endif
6976
Manuel Pégourié-Gonnard125af942018-09-11 11:08:12 +02006977 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006978 {
6979 /* mbedtls_ssl_read_record may have sent an alert already. We
6980 let it decide whether to alert. */
6981 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Hanno Becker3dad3112019-02-05 17:19:52 +00006982 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006983 }
6984
Hanno Becker4a55f632019-02-05 12:49:06 +00006985#if defined(MBEDTLS_SSL_SRV_C)
6986 if( ssl_srv_check_client_no_crt_notification( ssl ) == 0 )
6987 {
6988 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Hanno Becker4a55f632019-02-05 12:49:06 +00006989
6990 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
Hanno Becker6bdfab22019-02-05 13:11:17 +00006991 ret = 0;
6992 else
6993 ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
Hanno Becker4a55f632019-02-05 12:49:06 +00006994
Hanno Becker6bdfab22019-02-05 13:11:17 +00006995 goto exit;
Hanno Becker4a55f632019-02-05 12:49:06 +00006996 }
6997#endif /* MBEDTLS_SSL_SRV_C */
6998
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006999 /* Clear existing peer CRT structure in case we tried to
7000 * reuse a session but it failed, and allocate a new one. */
Hanno Becker7a955a02019-02-05 13:08:01 +00007001 ssl_clear_peer_cert( ssl->session_negotiate );
Hanno Becker3dad3112019-02-05 17:19:52 +00007002
7003 chain = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
7004 if( chain == NULL )
Hanno Beckerc7bd7802019-02-05 15:37:23 +00007005 {
7006 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
7007 sizeof( mbedtls_x509_crt ) ) );
7008 mbedtls_ssl_send_alert_message( ssl,
7009 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7010 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Hanno Becker7a955a02019-02-05 13:08:01 +00007011
Hanno Becker3dad3112019-02-05 17:19:52 +00007012 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
7013 goto exit;
7014 }
7015 mbedtls_x509_crt_init( chain );
7016
7017 ret = ssl_parse_certificate_chain( ssl, chain );
Hanno Beckerc7bd7802019-02-05 15:37:23 +00007018 if( ret != 0 )
Hanno Becker3dad3112019-02-05 17:19:52 +00007019 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007020
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007021#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
7022 if( ssl->handshake->ecrs_enabled)
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02007023 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007024
7025crt_verify:
7026 if( ssl->handshake->ecrs_enabled)
7027 rs_ctx = &ssl->handshake->ecrs_ctx;
7028#endif
7029
Hanno Becker68636192019-02-05 14:36:34 +00007030 ret = ssl_parse_certificate_verify( ssl, authmode,
Hanno Becker3dad3112019-02-05 17:19:52 +00007031 chain, rs_ctx );
Hanno Becker68636192019-02-05 14:36:34 +00007032 if( ret != 0 )
Hanno Becker3dad3112019-02-05 17:19:52 +00007033 goto exit;
Paul Bakker5121ce52009-01-03 21:22:43 +00007034
Hanno Becker6bbd94c2019-02-05 17:02:28 +00007035#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Becker6bbd94c2019-02-05 17:02:28 +00007036 {
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007037 unsigned char *crt_start, *pk_start;
7038 size_t crt_len, pk_len;
Hanno Becker3dad3112019-02-05 17:19:52 +00007039
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007040 /* We parse the CRT chain without copying, so
7041 * these pointers point into the input buffer,
7042 * and are hence still valid after freeing the
7043 * CRT chain. */
Hanno Becker6bbd94c2019-02-05 17:02:28 +00007044
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007045 crt_start = chain->raw.p;
7046 crt_len = chain->raw.len;
Hanno Becker6bbd94c2019-02-05 17:02:28 +00007047
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007048 pk_start = chain->pk_raw.p;
7049 pk_len = chain->pk_raw.len;
7050
7051 /* Free the CRT structures before computing
7052 * digest and copying the peer's public key. */
7053 mbedtls_x509_crt_free( chain );
7054 mbedtls_free( chain );
7055 chain = NULL;
7056
7057 ret = ssl_remember_peer_crt_digest( ssl, crt_start, crt_len );
Hanno Beckera2747532019-02-06 16:19:04 +00007058 if( ret != 0 )
Hanno Beckera2747532019-02-06 16:19:04 +00007059 goto exit;
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007060
7061 ret = ssl_remember_peer_pubkey( ssl, pk_start, pk_len );
7062 if( ret != 0 )
7063 goto exit;
Hanno Beckera2747532019-02-06 16:19:04 +00007064 }
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007065#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7066 /* Pass ownership to session structure. */
Hanno Becker3dad3112019-02-05 17:19:52 +00007067 ssl->session_negotiate->peer_cert = chain;
7068 chain = NULL;
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007069#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker3dad3112019-02-05 17:19:52 +00007070
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007071 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007072
Hanno Becker6bdfab22019-02-05 13:11:17 +00007073exit:
7074
Hanno Becker3dad3112019-02-05 17:19:52 +00007075 if( ret == 0 )
7076 ssl->state++;
7077
7078#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
7079 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
7080 {
7081 ssl->handshake->ecrs_peer_cert = chain;
7082 chain = NULL;
7083 }
7084#endif
7085
7086 if( chain != NULL )
7087 {
7088 mbedtls_x509_crt_free( chain );
7089 mbedtls_free( chain );
7090 }
7091
Paul Bakker5121ce52009-01-03 21:22:43 +00007092 return( ret );
7093}
Hanno Becker21489932019-02-05 13:20:55 +00007094#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00007095
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007096int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007097{
7098 int ret;
7099
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007100 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007101
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007102 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00007103 ssl->out_msglen = 1;
7104 ssl->out_msg[0] = 1;
7105
Paul Bakker5121ce52009-01-03 21:22:43 +00007106 ssl->state++;
7107
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007108 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007109 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007110 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007111 return( ret );
7112 }
7113
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007114 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007115
7116 return( 0 );
7117}
7118
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007119int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007120{
7121 int ret;
7122
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007123 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007124
Hanno Becker327c93b2018-08-15 13:56:18 +01007125 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007126 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007127 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007128 return( ret );
7129 }
7130
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007131 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Paul Bakker5121ce52009-01-03 21:22:43 +00007132 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007133 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007134 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7135 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007136 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007137 }
7138
Hanno Beckere678eaa2018-08-21 14:57:46 +01007139 /* CCS records are only accepted if they have length 1 and content '1',
7140 * so we don't need to check this here. */
Paul Bakker5121ce52009-01-03 21:22:43 +00007141
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007142 /*
7143 * Switch to our negotiated transform and session parameters for inbound
7144 * data.
7145 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007146 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007147 ssl->transform_in = ssl->transform_negotiate;
7148 ssl->session_in = ssl->session_negotiate;
7149
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007150#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007151 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007152 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007153#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007154 ssl_dtls_replay_reset( ssl );
7155#endif
7156
7157 /* Increment epoch */
7158 if( ++ssl->in_epoch == 0 )
7159 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007160 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007161 /* This is highly unlikely to happen for legitimate reasons, so
7162 treat it as an attack and don't send an alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007163 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007164 }
7165 }
7166 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007167#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007168 memset( ssl->in_ctr, 0, 8 );
7169
Hanno Becker79594fd2019-05-08 09:38:41 +01007170 ssl_update_in_pointers( ssl );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007171
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007172#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7173 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007174 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007175 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_INBOUND ) ) != 0 )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007176 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007177 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007178 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7179 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007180 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007181 }
7182 }
7183#endif
7184
Paul Bakker5121ce52009-01-03 21:22:43 +00007185 ssl->state++;
7186
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007187 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007188
7189 return( 0 );
7190}
7191
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007192void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl,
7193 const mbedtls_ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00007194{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02007195 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01007196
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007197#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7198 defined(MBEDTLS_SSL_PROTO_TLS1_1)
7199 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00007200 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00007201 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007202#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007203#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7204#if defined(MBEDTLS_SHA512_C)
7205 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007206 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
7207 else
7208#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007209#if defined(MBEDTLS_SHA256_C)
7210 if( ciphersuite_info->mac != MBEDTLS_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00007211 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007212 else
7213#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007214#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02007215 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007216 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007217 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02007218 }
Paul Bakker380da532012-04-18 16:10:25 +00007219}
Paul Bakkerf7abd422013-04-16 13:15:56 +02007220
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007221void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007222{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007223#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7224 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007225 mbedtls_md5_starts_ret( &ssl->handshake->fin_md5 );
7226 mbedtls_sha1_starts_ret( &ssl->handshake->fin_sha1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007227#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007228#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7229#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007230#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek2ad22972019-01-30 03:32:12 -05007231 psa_hash_abort( &ssl->handshake->fin_sha256_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007232 psa_hash_setup( &ssl->handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
7233#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007234 mbedtls_sha256_starts_ret( &ssl->handshake->fin_sha256, 0 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007235#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007236#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007237#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007238#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek2ad22972019-01-30 03:32:12 -05007239 psa_hash_abort( &ssl->handshake->fin_sha384_psa );
Andrzej Kurek972fba52019-01-30 03:29:12 -05007240 psa_hash_setup( &ssl->handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007241#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007242 mbedtls_sha512_starts_ret( &ssl->handshake->fin_sha512, 1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007243#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007244#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007245#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007246}
7247
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007248static void ssl_update_checksum_start( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007249 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00007250{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007251#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7252 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007253 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
7254 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007255#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007256#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7257#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007258#if defined(MBEDTLS_USE_PSA_CRYPTO)
7259 psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len );
7260#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007261 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007262#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007263#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007264#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007265#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05007266 psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007267#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007268 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01007269#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007270#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007271#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00007272}
7273
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007274#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7275 defined(MBEDTLS_SSL_PROTO_TLS1_1)
7276static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007277 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00007278{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007279 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
7280 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00007281}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007282#endif
Paul Bakker380da532012-04-18 16:10:25 +00007283
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007284#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7285#if defined(MBEDTLS_SHA256_C)
7286static void ssl_update_checksum_sha256( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007287 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00007288{
Andrzej Kurekeb342242019-01-29 09:14:33 -05007289#if defined(MBEDTLS_USE_PSA_CRYPTO)
7290 psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len );
7291#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007292 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007293#endif
Paul Bakker380da532012-04-18 16:10:25 +00007294}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007295#endif
Paul Bakker380da532012-04-18 16:10:25 +00007296
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007297#if defined(MBEDTLS_SHA512_C)
7298static void ssl_update_checksum_sha384( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007299 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00007300{
Andrzej Kurekeb342242019-01-29 09:14:33 -05007301#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05007302 psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007303#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007304 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007305#endif
Paul Bakker380da532012-04-18 16:10:25 +00007306}
Paul Bakker769075d2012-11-24 11:26:46 +01007307#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007308#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00007309
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007310#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00007311static void ssl_calc_finished_ssl(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007312 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00007313{
Paul Bakker3c2122f2013-06-24 19:03:14 +02007314 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007315 mbedtls_md5_context md5;
7316 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007317
Paul Bakker5121ce52009-01-03 21:22:43 +00007318 unsigned char padbuf[48];
7319 unsigned char md5sum[16];
7320 unsigned char sha1sum[20];
7321
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007322 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007323 if( !session )
7324 session = ssl->session;
7325
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007326 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007327
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007328 mbedtls_md5_init( &md5 );
7329 mbedtls_sha1_init( &sha1 );
7330
7331 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
7332 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007333
7334 /*
7335 * SSLv3:
7336 * hash =
7337 * MD5( master + pad2 +
7338 * MD5( handshake + sender + master + pad1 ) )
7339 * + SHA1( master + pad2 +
7340 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00007341 */
7342
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007343#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007344 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
7345 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007346#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007347
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007348#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007349 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
7350 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007351#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007352
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007353 sender = ( from == MBEDTLS_SSL_IS_CLIENT ) ? "CLNT"
Paul Bakker3c2122f2013-06-24 19:03:14 +02007354 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00007355
Paul Bakker1ef83d62012-04-11 12:09:53 +00007356 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007357
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007358 mbedtls_md5_update_ret( &md5, (const unsigned char *) sender, 4 );
7359 mbedtls_md5_update_ret( &md5, session->master, 48 );
7360 mbedtls_md5_update_ret( &md5, padbuf, 48 );
7361 mbedtls_md5_finish_ret( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00007362
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007363 mbedtls_sha1_update_ret( &sha1, (const unsigned char *) sender, 4 );
7364 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
7365 mbedtls_sha1_update_ret( &sha1, padbuf, 40 );
7366 mbedtls_sha1_finish_ret( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00007367
Paul Bakker1ef83d62012-04-11 12:09:53 +00007368 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007369
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007370 mbedtls_md5_starts_ret( &md5 );
7371 mbedtls_md5_update_ret( &md5, session->master, 48 );
7372 mbedtls_md5_update_ret( &md5, padbuf, 48 );
7373 mbedtls_md5_update_ret( &md5, md5sum, 16 );
7374 mbedtls_md5_finish_ret( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00007375
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007376 mbedtls_sha1_starts_ret( &sha1 );
7377 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
7378 mbedtls_sha1_update_ret( &sha1, padbuf , 40 );
7379 mbedtls_sha1_update_ret( &sha1, sha1sum, 20 );
7380 mbedtls_sha1_finish_ret( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007381
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007382 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007383
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007384 mbedtls_md5_free( &md5 );
7385 mbedtls_sha1_free( &sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007386
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007387 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
7388 mbedtls_platform_zeroize( md5sum, sizeof( md5sum ) );
7389 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007390
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007391 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007392}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007393#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00007394
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007395#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00007396static void ssl_calc_finished_tls(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007397 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00007398{
Paul Bakker1ef83d62012-04-11 12:09:53 +00007399 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02007400 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007401 mbedtls_md5_context md5;
7402 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007403 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00007404
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007405 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007406 if( !session )
7407 session = ssl->session;
7408
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007409 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007410
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007411 mbedtls_md5_init( &md5 );
7412 mbedtls_sha1_init( &sha1 );
7413
7414 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
7415 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007416
Paul Bakker1ef83d62012-04-11 12:09:53 +00007417 /*
7418 * TLSv1:
7419 * hash = PRF( master, finished_label,
7420 * MD5( handshake ) + SHA1( handshake ) )[0..11]
7421 */
Paul Bakker5121ce52009-01-03 21:22:43 +00007422
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007423#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007424 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
7425 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007426#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007427
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007428#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007429 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
7430 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007431#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007432
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007433 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02007434 ? "client finished"
7435 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00007436
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007437 mbedtls_md5_finish_ret( &md5, padbuf );
7438 mbedtls_sha1_finish_ret( &sha1, padbuf + 16 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007439
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007440 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00007441 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007442
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007443 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007444
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007445 mbedtls_md5_free( &md5 );
7446 mbedtls_sha1_free( &sha1 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007447
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007448 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007449
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007450 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007451}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007452#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00007453
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007454#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7455#if defined(MBEDTLS_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00007456static void ssl_calc_finished_tls_sha256(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007457 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker1ef83d62012-04-11 12:09:53 +00007458{
7459 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02007460 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007461 unsigned char padbuf[32];
Andrzej Kurekeb342242019-01-29 09:14:33 -05007462#if defined(MBEDTLS_USE_PSA_CRYPTO)
7463 size_t hash_size;
Jaeden Amero34973232019-02-20 10:32:28 +00007464 psa_hash_operation_t sha256_psa = PSA_HASH_OPERATION_INIT;
Andrzej Kurekeb342242019-01-29 09:14:33 -05007465 psa_status_t status;
7466#else
7467 mbedtls_sha256_context sha256;
7468#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007469
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007470 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007471 if( !session )
7472 session = ssl->session;
7473
Andrzej Kurekeb342242019-01-29 09:14:33 -05007474 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
7475 ? "client finished"
7476 : "server finished";
7477
7478#if defined(MBEDTLS_USE_PSA_CRYPTO)
7479 sha256_psa = psa_hash_operation_init();
7480
7481 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha256" ) );
7482
7483 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
7484 if( status != PSA_SUCCESS )
7485 {
7486 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
7487 return;
7488 }
7489
7490 status = psa_hash_finish( &sha256_psa, padbuf, sizeof( padbuf ), &hash_size );
7491 if( status != PSA_SUCCESS )
7492 {
7493 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
7494 return;
7495 }
7496 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 32 );
7497#else
7498
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007499 mbedtls_sha256_init( &sha256 );
7500
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007501 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007502
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007503 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007504
7505 /*
7506 * TLSv1.2:
7507 * hash = PRF( master, finished_label,
7508 * Hash( handshake ) )[0.11]
7509 */
7510
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007511#if !defined(MBEDTLS_SHA256_ALT)
7512 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007513 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007514#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007515
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007516 mbedtls_sha256_finish_ret( &sha256, padbuf );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007517 mbedtls_sha256_free( &sha256 );
7518#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker1ef83d62012-04-11 12:09:53 +00007519
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007520 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00007521 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007522
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007523 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007524
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007525 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007526
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007527 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007528}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007529#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00007530
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007531#if defined(MBEDTLS_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00007532static void ssl_calc_finished_tls_sha384(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007533 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakkerca4ab492012-04-18 14:23:57 +00007534{
7535 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02007536 const char *sender;
Paul Bakkerca4ab492012-04-18 14:23:57 +00007537 unsigned char padbuf[48];
Andrzej Kurekeb342242019-01-29 09:14:33 -05007538#if defined(MBEDTLS_USE_PSA_CRYPTO)
7539 size_t hash_size;
Jaeden Amero34973232019-02-20 10:32:28 +00007540 psa_hash_operation_t sha384_psa = PSA_HASH_OPERATION_INIT;
Andrzej Kurekeb342242019-01-29 09:14:33 -05007541 psa_status_t status;
7542#else
7543 mbedtls_sha512_context sha512;
7544#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00007545
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007546 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007547 if( !session )
7548 session = ssl->session;
7549
Andrzej Kurekeb342242019-01-29 09:14:33 -05007550 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
7551 ? "client finished"
7552 : "server finished";
7553
7554#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05007555 sha384_psa = psa_hash_operation_init();
Andrzej Kurekeb342242019-01-29 09:14:33 -05007556
7557 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha384" ) );
7558
Andrzej Kurek972fba52019-01-30 03:29:12 -05007559 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007560 if( status != PSA_SUCCESS )
7561 {
7562 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
7563 return;
7564 }
7565
Andrzej Kurek972fba52019-01-30 03:29:12 -05007566 status = psa_hash_finish( &sha384_psa, padbuf, sizeof( padbuf ), &hash_size );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007567 if( status != PSA_SUCCESS )
7568 {
7569 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
7570 return;
7571 }
7572 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 48 );
7573#else
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007574 mbedtls_sha512_init( &sha512 );
7575
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007576 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007577
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007578 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007579
7580 /*
7581 * TLSv1.2:
7582 * hash = PRF( master, finished_label,
7583 * Hash( handshake ) )[0.11]
7584 */
7585
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007586#if !defined(MBEDTLS_SHA512_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007587 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
7588 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007589#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00007590
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007591 mbedtls_sha512_finish_ret( &sha512, padbuf );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007592 mbedtls_sha512_free( &sha512 );
7593#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00007594
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007595 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00007596 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007597
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007598 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007599
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007600 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007601
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007602 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007603}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007604#endif /* MBEDTLS_SHA512_C */
7605#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00007606
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007607static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00007608{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007609 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007610
7611 /*
7612 * Free our handshake params
7613 */
Gilles Peskine9b562d52018-04-25 20:32:43 +02007614 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007615 mbedtls_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00007616 ssl->handshake = NULL;
7617
7618 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007619 * Free the previous transform and swith in the current one
Paul Bakker48916f92012-09-16 19:57:18 +00007620 */
7621 if( ssl->transform )
7622 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007623 mbedtls_ssl_transform_free( ssl->transform );
7624 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00007625 }
7626 ssl->transform = ssl->transform_negotiate;
7627 ssl->transform_negotiate = NULL;
7628
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007629 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007630}
7631
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007632void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007633{
7634 int resume = ssl->handshake->resume;
7635
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007636 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007637
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007638#if defined(MBEDTLS_SSL_RENEGOTIATION)
7639 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007640 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007641 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007642 ssl->renego_records_seen = 0;
7643 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007644#endif
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007645
7646 /*
7647 * Free the previous session and switch in the current one
7648 */
Paul Bakker0a597072012-09-25 21:55:46 +00007649 if( ssl->session )
7650 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007651#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01007652 /* RFC 7366 3.1: keep the EtM state */
7653 ssl->session_negotiate->encrypt_then_mac =
7654 ssl->session->encrypt_then_mac;
7655#endif
7656
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007657 mbedtls_ssl_session_free( ssl->session );
7658 mbedtls_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00007659 }
7660 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007661 ssl->session_negotiate = NULL;
7662
Paul Bakker0a597072012-09-25 21:55:46 +00007663 /*
7664 * Add cache entry
7665 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007666 if( ssl->conf->f_set_cache != NULL &&
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02007667 ssl->session->id_len != 0 &&
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02007668 resume == 0 )
7669 {
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007670 if( ssl->conf->f_set_cache( ssl->conf->p_cache, ssl->session ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007671 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02007672 }
Paul Bakker0a597072012-09-25 21:55:46 +00007673
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007674#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007675 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007676 ssl->handshake->flight != NULL )
7677 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02007678 /* Cancel handshake timer */
7679 ssl_set_timer( ssl, 0 );
7680
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007681 /* Keep last flight around in case we need to resend it:
7682 * we need the handshake and transform structures for that */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007683 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007684 }
7685 else
7686#endif
7687 ssl_handshake_wrapup_free_hs_transform( ssl );
7688
Paul Bakker48916f92012-09-16 19:57:18 +00007689 ssl->state++;
7690
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007691 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007692}
7693
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007694int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
Paul Bakker1ef83d62012-04-11 12:09:53 +00007695{
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007696 int ret, hash_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007697
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007698 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007699
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007700 ssl_update_out_pointers( ssl, ssl->transform_negotiate );
Paul Bakker92be97b2013-01-02 17:30:03 +01007701
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007702 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007703
Manuel Pégourié-Gonnard214a8482016-02-22 11:27:26 +01007704 /*
7705 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
7706 * may define some other value. Currently (early 2016), no defined
7707 * ciphersuite does this (and this is unlikely to change as activity has
7708 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
7709 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007710 hash_len = ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) ? 36 : 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00007711
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007712#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00007713 ssl->verify_data_len = hash_len;
7714 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007715#endif
Paul Bakker48916f92012-09-16 19:57:18 +00007716
Paul Bakker5121ce52009-01-03 21:22:43 +00007717 ssl->out_msglen = 4 + hash_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007718 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
7719 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
Paul Bakker5121ce52009-01-03 21:22:43 +00007720
7721 /*
7722 * In case of session resuming, invert the client and server
7723 * ChangeCipherSpec messages order.
7724 */
Paul Bakker0a597072012-09-25 21:55:46 +00007725 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007726 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007727#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007728 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007729 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007730#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007731#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007732 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007733 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007734#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007735 }
7736 else
7737 ssl->state++;
7738
Paul Bakker48916f92012-09-16 19:57:18 +00007739 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02007740 * Switch to our negotiated transform and session parameters for outbound
7741 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00007742 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007743 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01007744
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007745#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007746 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007747 {
7748 unsigned char i;
7749
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007750 /* Remember current epoch settings for resending */
7751 ssl->handshake->alt_transform_out = ssl->transform_out;
Hanno Becker19859472018-08-06 09:40:20 +01007752 memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007753
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007754 /* Set sequence_number to zero */
Hanno Becker19859472018-08-06 09:40:20 +01007755 memset( ssl->cur_out_ctr + 2, 0, 6 );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007756
7757 /* Increment epoch */
7758 for( i = 2; i > 0; i-- )
Hanno Becker19859472018-08-06 09:40:20 +01007759 if( ++ssl->cur_out_ctr[i - 1] != 0 )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007760 break;
7761
7762 /* The loop goes to its end iff the counter is wrapping */
7763 if( i == 0 )
7764 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007765 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
7766 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007767 }
7768 }
7769 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007770#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker19859472018-08-06 09:40:20 +01007771 memset( ssl->cur_out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007772
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007773 ssl->transform_out = ssl->transform_negotiate;
7774 ssl->session_out = ssl->session_negotiate;
7775
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007776#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7777 if( mbedtls_ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01007778 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007779 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Paul Bakker07eb38b2012-12-19 14:42:06 +01007780 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007781 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
7782 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker07eb38b2012-12-19 14:42:06 +01007783 }
7784 }
7785#endif
7786
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007787#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007788 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007789 mbedtls_ssl_send_flight_completed( ssl );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02007790#endif
7791
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007792 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007793 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007794 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007795 return( ret );
7796 }
7797
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02007798#if defined(MBEDTLS_SSL_PROTO_DTLS)
7799 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7800 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
7801 {
7802 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
7803 return( ret );
7804 }
7805#endif
7806
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007807 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007808
7809 return( 0 );
7810}
7811
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007812#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007813#define SSL_MAX_HASH_LEN 36
7814#else
7815#define SSL_MAX_HASH_LEN 12
7816#endif
7817
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007818int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007819{
Paul Bakker23986e52011-04-24 08:57:21 +00007820 int ret;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007821 unsigned int hash_len;
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007822 unsigned char buf[SSL_MAX_HASH_LEN];
Paul Bakker5121ce52009-01-03 21:22:43 +00007823
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007824 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007825
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007826 ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007827
Hanno Becker327c93b2018-08-15 13:56:18 +01007828 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007829 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007830 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007831 return( ret );
7832 }
7833
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007834 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00007835 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007836 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007837 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7838 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007839 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007840 }
7841
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007842 /* There is currently no ciphersuite using another length with TLS 1.2 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007843#if defined(MBEDTLS_SSL_PROTO_SSL3)
7844 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007845 hash_len = 36;
7846 else
7847#endif
7848 hash_len = 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00007849
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007850 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED ||
7851 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00007852 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007853 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007854 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7855 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007856 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00007857 }
7858
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007859 if( mbedtls_ssl_safer_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00007860 buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007861 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007862 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007863 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7864 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007865 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00007866 }
7867
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007868#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00007869 ssl->verify_data_len = hash_len;
7870 memcpy( ssl->peer_verify_data, buf, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007871#endif
Paul Bakker48916f92012-09-16 19:57:18 +00007872
Paul Bakker0a597072012-09-25 21:55:46 +00007873 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007874 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007875#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007876 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007877 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007878#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007879#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007880 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007881 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007882#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007883 }
7884 else
7885 ssl->state++;
7886
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007887#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007888 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007889 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007890#endif
7891
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007892 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007893
7894 return( 0 );
7895}
7896
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007897static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007898{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007899 memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007900
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007901#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7902 defined(MBEDTLS_SSL_PROTO_TLS1_1)
7903 mbedtls_md5_init( &handshake->fin_md5 );
7904 mbedtls_sha1_init( &handshake->fin_sha1 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007905 mbedtls_md5_starts_ret( &handshake->fin_md5 );
7906 mbedtls_sha1_starts_ret( &handshake->fin_sha1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007907#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007908#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7909#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007910#if defined(MBEDTLS_USE_PSA_CRYPTO)
7911 handshake->fin_sha256_psa = psa_hash_operation_init();
7912 psa_hash_setup( &handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
7913#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007914 mbedtls_sha256_init( &handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007915 mbedtls_sha256_starts_ret( &handshake->fin_sha256, 0 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007916#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007917#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007918#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007919#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05007920 handshake->fin_sha384_psa = psa_hash_operation_init();
7921 psa_hash_setup( &handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007922#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007923 mbedtls_sha512_init( &handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007924 mbedtls_sha512_starts_ret( &handshake->fin_sha512, 1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007925#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007926#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007927#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007928
7929 handshake->update_checksum = ssl_update_checksum_start;
Hanno Becker7e5437a2017-04-28 17:15:26 +01007930
7931#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
7932 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
7933 mbedtls_ssl_sig_hash_set_init( &handshake->hash_algs );
7934#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007935
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007936#if defined(MBEDTLS_DHM_C)
7937 mbedtls_dhm_init( &handshake->dhm_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007938#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007939#if defined(MBEDTLS_ECDH_C)
7940 mbedtls_ecdh_init( &handshake->ecdh_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007941#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02007942#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02007943 mbedtls_ecjpake_init( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02007944#if defined(MBEDTLS_SSL_CLI_C)
7945 handshake->ecjpake_cache = NULL;
7946 handshake->ecjpake_cache_len = 0;
7947#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02007948#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007949
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02007950#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02007951 mbedtls_x509_crt_restart_init( &handshake->ecrs_ctx );
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02007952#endif
7953
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007954#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7955 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
7956#endif
Hanno Becker75173122019-02-06 16:18:31 +00007957
7958#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
7959 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
7960 mbedtls_pk_init( &handshake->peer_pubkey );
7961#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007962}
7963
Hanno Beckera18d1322018-01-03 14:27:32 +00007964void mbedtls_ssl_transform_init( mbedtls_ssl_transform *transform )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007965{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007966 memset( transform, 0, sizeof(mbedtls_ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02007967
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007968 mbedtls_cipher_init( &transform->cipher_ctx_enc );
7969 mbedtls_cipher_init( &transform->cipher_ctx_dec );
Paul Bakker84bbeb52014-07-01 14:53:22 +02007970
Hanno Beckerd56ed242018-01-03 15:32:51 +00007971#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007972 mbedtls_md_init( &transform->md_ctx_enc );
7973 mbedtls_md_init( &transform->md_ctx_dec );
Hanno Beckerd56ed242018-01-03 15:32:51 +00007974#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007975}
7976
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007977void mbedtls_ssl_session_init( mbedtls_ssl_session *session )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007978{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007979 memset( session, 0, sizeof(mbedtls_ssl_session) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007980}
7981
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007982static int ssl_handshake_init( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00007983{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007984 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00007985 if( ssl->transform_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007986 mbedtls_ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007987 if( ssl->session_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007988 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007989 if( ssl->handshake )
Gilles Peskine9b562d52018-04-25 20:32:43 +02007990 mbedtls_ssl_handshake_free( ssl );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007991
7992 /*
7993 * Either the pointers are now NULL or cleared properly and can be freed.
7994 * Now allocate missing structures.
7995 */
7996 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007997 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007998 ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007999 }
Paul Bakker48916f92012-09-16 19:57:18 +00008000
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008001 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008002 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008003 ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008004 }
Paul Bakker48916f92012-09-16 19:57:18 +00008005
Paul Bakker82788fb2014-10-20 13:59:19 +02008006 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008007 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008008 ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008009 }
Paul Bakker48916f92012-09-16 19:57:18 +00008010
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008011 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00008012 if( ssl->handshake == NULL ||
8013 ssl->transform_negotiate == NULL ||
8014 ssl->session_negotiate == NULL )
8015 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02008016 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008017
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008018 mbedtls_free( ssl->handshake );
8019 mbedtls_free( ssl->transform_negotiate );
8020 mbedtls_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008021
8022 ssl->handshake = NULL;
8023 ssl->transform_negotiate = NULL;
8024 ssl->session_negotiate = NULL;
8025
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008026 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker48916f92012-09-16 19:57:18 +00008027 }
8028
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008029 /* Initialize structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008030 mbedtls_ssl_session_init( ssl->session_negotiate );
Hanno Beckera18d1322018-01-03 14:27:32 +00008031 mbedtls_ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02008032 ssl_handshake_params_init( ssl->handshake );
8033
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008034#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02008035 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
8036 {
8037 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008038
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02008039 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
8040 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
8041 else
8042 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008043
8044 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02008045 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008046#endif
8047
Paul Bakker48916f92012-09-16 19:57:18 +00008048 return( 0 );
8049}
8050
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008051#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008052/* Dummy cookie callbacks for defaults */
8053static int ssl_cookie_write_dummy( void *ctx,
8054 unsigned char **p, unsigned char *end,
8055 const unsigned char *cli_id, size_t cli_id_len )
8056{
8057 ((void) ctx);
8058 ((void) p);
8059 ((void) end);
8060 ((void) cli_id);
8061 ((void) cli_id_len);
8062
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008063 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008064}
8065
8066static int ssl_cookie_check_dummy( void *ctx,
8067 const unsigned char *cookie, size_t cookie_len,
8068 const unsigned char *cli_id, size_t cli_id_len )
8069{
8070 ((void) ctx);
8071 ((void) cookie);
8072 ((void) cookie_len);
8073 ((void) cli_id);
8074 ((void) cli_id_len);
8075
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008076 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008077}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008078#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008079
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008080/* Once ssl->out_hdr as the address of the beginning of the
8081 * next outgoing record is set, deduce the other pointers.
8082 *
8083 * Note: For TLS, we save the implicit record sequence number
8084 * (entering MAC computation) in the 8 bytes before ssl->out_hdr,
8085 * and the caller has to make sure there's space for this.
8086 */
8087
8088static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
8089 mbedtls_ssl_transform *transform )
8090{
8091#if defined(MBEDTLS_SSL_PROTO_DTLS)
8092 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
8093 {
8094 ssl->out_ctr = ssl->out_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008095#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008096 ssl->out_cid = ssl->out_ctr + 8;
8097 ssl->out_len = ssl->out_cid;
8098 if( transform != NULL )
8099 ssl->out_len += transform->out_cid_len;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008100#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008101 ssl->out_len = ssl->out_ctr + 8;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008102#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008103 ssl->out_iv = ssl->out_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008104 }
8105 else
8106#endif
8107 {
8108 ssl->out_ctr = ssl->out_hdr - 8;
8109 ssl->out_len = ssl->out_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008110#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker4c3eb7c2019-05-08 16:43:21 +01008111 ssl->out_cid = ssl->out_len;
8112#endif
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008113 ssl->out_iv = ssl->out_hdr + 5;
8114 }
8115
8116 /* Adjust out_msg to make space for explicit IV, if used. */
8117 if( transform != NULL &&
8118 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
8119 {
8120 ssl->out_msg = ssl->out_iv + transform->ivlen - transform->fixed_ivlen;
8121 }
8122 else
8123 ssl->out_msg = ssl->out_iv;
8124}
8125
8126/* Once ssl->in_hdr as the address of the beginning of the
8127 * next incoming record is set, deduce the other pointers.
8128 *
8129 * Note: For TLS, we save the implicit record sequence number
8130 * (entering MAC computation) in the 8 bytes before ssl->in_hdr,
8131 * and the caller has to make sure there's space for this.
8132 */
8133
Hanno Becker79594fd2019-05-08 09:38:41 +01008134static void ssl_update_in_pointers( mbedtls_ssl_context *ssl )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008135{
Hanno Becker79594fd2019-05-08 09:38:41 +01008136 /* This function sets the pointers to match the case
8137 * of unprotected TLS/DTLS records, with both ssl->in_iv
8138 * and ssl->in_msg pointing to the beginning of the record
8139 * content.
8140 *
8141 * When decrypting a protected record, ssl->in_msg
8142 * will be shifted to point to the beginning of the
8143 * record plaintext.
8144 */
8145
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008146#if defined(MBEDTLS_SSL_PROTO_DTLS)
8147 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
8148 {
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008149 /* This sets the header pointers to match records
8150 * without CID. When we receive a record containing
8151 * a CID, the fields are shifted accordingly in
8152 * ssl_parse_record_header(). */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008153 ssl->in_ctr = ssl->in_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008154#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008155 ssl->in_cid = ssl->in_ctr + 8;
8156 ssl->in_len = ssl->in_cid; /* Default: no CID */
Hanno Beckera0e20d02019-05-15 14:03:01 +01008157#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008158 ssl->in_len = ssl->in_ctr + 8;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008159#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008160 ssl->in_iv = ssl->in_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008161 }
8162 else
8163#endif
8164 {
8165 ssl->in_ctr = ssl->in_hdr - 8;
8166 ssl->in_len = ssl->in_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008167#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker4c3eb7c2019-05-08 16:43:21 +01008168 ssl->in_cid = ssl->in_len;
8169#endif
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008170 ssl->in_iv = ssl->in_hdr + 5;
8171 }
8172
Hanno Becker79594fd2019-05-08 09:38:41 +01008173 /* This will be adjusted at record decryption time. */
8174 ssl->in_msg = ssl->in_iv;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008175}
8176
Paul Bakker5121ce52009-01-03 21:22:43 +00008177/*
8178 * Initialize an SSL context
8179 */
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02008180void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
8181{
8182 memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
8183}
8184
8185/*
8186 * Setup an SSL context
8187 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008188
8189static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl )
8190{
8191 /* Set the incoming and outgoing record pointers. */
8192#if defined(MBEDTLS_SSL_PROTO_DTLS)
8193 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
8194 {
8195 ssl->out_hdr = ssl->out_buf;
8196 ssl->in_hdr = ssl->in_buf;
8197 }
8198 else
8199#endif /* MBEDTLS_SSL_PROTO_DTLS */
8200 {
8201 ssl->out_hdr = ssl->out_buf + 8;
8202 ssl->in_hdr = ssl->in_buf + 8;
8203 }
8204
8205 /* Derive other internal pointers. */
8206 ssl_update_out_pointers( ssl, NULL /* no transform enabled */ );
Hanno Becker79594fd2019-05-08 09:38:41 +01008207 ssl_update_in_pointers ( ssl );
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008208}
8209
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02008210int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +02008211 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +00008212{
Paul Bakker48916f92012-09-16 19:57:18 +00008213 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00008214
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02008215 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00008216
8217 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01008218 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00008219 */
k-stachowiakc9a5f022018-07-24 13:53:31 +02008220
8221 /* Set to NULL in case of an error condition */
8222 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02008223
Angus Grattond8213d02016-05-25 20:56:48 +10008224 ssl->in_buf = mbedtls_calloc( 1, MBEDTLS_SSL_IN_BUFFER_LEN );
8225 if( ssl->in_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00008226 {
Angus Grattond8213d02016-05-25 20:56:48 +10008227 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_IN_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02008228 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02008229 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +10008230 }
8231
8232 ssl->out_buf = mbedtls_calloc( 1, MBEDTLS_SSL_OUT_BUFFER_LEN );
8233 if( ssl->out_buf == NULL )
8234 {
8235 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_OUT_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02008236 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02008237 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00008238 }
8239
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008240 ssl_reset_in_out_pointers( ssl );
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02008241
Paul Bakker48916f92012-09-16 19:57:18 +00008242 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
k-stachowiaka47911c2018-07-04 17:41:58 +02008243 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00008244
8245 return( 0 );
k-stachowiaka47911c2018-07-04 17:41:58 +02008246
8247error:
8248 mbedtls_free( ssl->in_buf );
8249 mbedtls_free( ssl->out_buf );
8250
8251 ssl->conf = NULL;
8252
8253 ssl->in_buf = NULL;
8254 ssl->out_buf = NULL;
8255
8256 ssl->in_hdr = NULL;
8257 ssl->in_ctr = NULL;
8258 ssl->in_len = NULL;
8259 ssl->in_iv = NULL;
8260 ssl->in_msg = NULL;
8261
8262 ssl->out_hdr = NULL;
8263 ssl->out_ctr = NULL;
8264 ssl->out_len = NULL;
8265 ssl->out_iv = NULL;
8266 ssl->out_msg = NULL;
8267
k-stachowiak9f7798e2018-07-31 16:52:32 +02008268 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008269}
8270
8271/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00008272 * Reset an initialized and used SSL context for re-use while retaining
8273 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008274 *
8275 * If partial is non-zero, keep data in the input buffer and client ID.
8276 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00008277 */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008278static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +00008279{
Paul Bakker48916f92012-09-16 19:57:18 +00008280 int ret;
8281
Hanno Becker7e772132018-08-10 12:38:21 +01008282#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || \
8283 !defined(MBEDTLS_SSL_SRV_C)
8284 ((void) partial);
8285#endif
8286
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008287 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008288
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008289 /* Cancel any possibly running timer */
8290 ssl_set_timer( ssl, 0 );
8291
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008292#if defined(MBEDTLS_SSL_RENEGOTIATION)
8293 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008294 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00008295
8296 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008297 memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
8298 memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008299#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008300 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00008301
Paul Bakker7eb013f2011-10-06 12:37:39 +00008302 ssl->in_offt = NULL;
Hanno Beckerf29d4702018-08-10 11:31:15 +01008303 ssl_reset_in_out_pointers( ssl );
Paul Bakker7eb013f2011-10-06 12:37:39 +00008304
8305 ssl->in_msgtype = 0;
8306 ssl->in_msglen = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008307#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02008308 ssl->next_record_offset = 0;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02008309 ssl->in_epoch = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02008310#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008311#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02008312 ssl_dtls_replay_reset( ssl );
8313#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00008314
8315 ssl->in_hslen = 0;
8316 ssl->nb_zero = 0;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01008317
8318 ssl->keep_current_message = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00008319
8320 ssl->out_msgtype = 0;
8321 ssl->out_msglen = 0;
8322 ssl->out_left = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008323#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
8324 if( ssl->split_done != MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008325 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008326#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00008327
Hanno Becker19859472018-08-06 09:40:20 +01008328 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
8329
Paul Bakker48916f92012-09-16 19:57:18 +00008330 ssl->transform_in = NULL;
8331 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00008332
Hanno Becker78640902018-08-13 16:35:15 +01008333 ssl->session_in = NULL;
8334 ssl->session_out = NULL;
8335
Angus Grattond8213d02016-05-25 20:56:48 +10008336 memset( ssl->out_buf, 0, MBEDTLS_SSL_OUT_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01008337
8338#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008339 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01008340#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
8341 {
8342 ssl->in_left = 0;
Angus Grattond8213d02016-05-25 20:56:48 +10008343 memset( ssl->in_buf, 0, MBEDTLS_SSL_IN_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01008344 }
Paul Bakker05ef8352012-05-08 09:17:57 +00008345
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008346#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
8347 if( mbedtls_ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00008348 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008349 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_reset()" ) );
8350 if( ( ret = mbedtls_ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00008351 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008352 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_reset", ret );
8353 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00008354 }
Paul Bakker05ef8352012-05-08 09:17:57 +00008355 }
8356#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00008357
Paul Bakker48916f92012-09-16 19:57:18 +00008358 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00008359 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008360 mbedtls_ssl_transform_free( ssl->transform );
8361 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00008362 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00008363 }
Paul Bakker48916f92012-09-16 19:57:18 +00008364
Paul Bakkerc0463502013-02-14 11:19:38 +01008365 if( ssl->session )
8366 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008367 mbedtls_ssl_session_free( ssl->session );
8368 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01008369 ssl->session = NULL;
8370 }
8371
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008372#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008373 ssl->alpn_chosen = NULL;
8374#endif
8375
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008376#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker4ccbf062018-08-10 11:20:38 +01008377#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008378 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01008379#endif
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008380 {
8381 mbedtls_free( ssl->cli_id );
8382 ssl->cli_id = NULL;
8383 ssl->cli_id_len = 0;
8384 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02008385#endif
8386
Paul Bakker48916f92012-09-16 19:57:18 +00008387 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
8388 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00008389
8390 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00008391}
8392
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02008393/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008394 * Reset an initialized and used SSL context for re-use while retaining
8395 * all application-set variables, function pointers and data.
8396 */
8397int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
8398{
8399 return( ssl_session_reset_int( ssl, 0 ) );
8400}
8401
8402/*
Paul Bakker5121ce52009-01-03 21:22:43 +00008403 * SSL set accessors
8404 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008405void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00008406{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008407 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00008408}
8409
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008410void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01008411{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008412 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01008413}
8414
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008415#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008416void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02008417{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008418 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02008419}
8420#endif
8421
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008422#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008423void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf, unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02008424{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008425 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02008426}
8427#endif
8428
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008429#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01008430
Hanno Becker1841b0a2018-08-24 11:13:57 +01008431void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl,
8432 unsigned allow_packing )
Hanno Becker04da1892018-08-14 13:22:10 +01008433{
8434 ssl->disable_datagram_packing = !allow_packing;
8435}
8436
8437void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
8438 uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02008439{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008440 conf->hs_timeout_min = min;
8441 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02008442}
8443#endif
8444
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008445void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00008446{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008447 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00008448}
8449
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008450#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008451void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02008452 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008453 void *p_vrfy )
8454{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008455 conf->f_vrfy = f_vrfy;
8456 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008457}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008458#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008459
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008460void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00008461 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00008462 void *p_rng )
8463{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01008464 conf->f_rng = f_rng;
8465 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00008466}
8467
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008468void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02008469 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00008470 void *p_dbg )
8471{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008472 conf->f_dbg = f_dbg;
8473 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00008474}
8475
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008476void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008477 void *p_bio,
Simon Butchere846b512016-03-01 17:31:49 +00008478 mbedtls_ssl_send_t *f_send,
8479 mbedtls_ssl_recv_t *f_recv,
8480 mbedtls_ssl_recv_timeout_t *f_recv_timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008481{
8482 ssl->p_bio = p_bio;
8483 ssl->f_send = f_send;
8484 ssl->f_recv = f_recv;
8485 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01008486}
8487
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02008488#if defined(MBEDTLS_SSL_PROTO_DTLS)
8489void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu )
8490{
8491 ssl->mtu = mtu;
8492}
8493#endif
8494
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008495void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01008496{
8497 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008498}
8499
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008500void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
8501 void *p_timer,
Simon Butchere846b512016-03-01 17:31:49 +00008502 mbedtls_ssl_set_timer_t *f_set_timer,
8503 mbedtls_ssl_get_timer_t *f_get_timer )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008504{
8505 ssl->p_timer = p_timer;
8506 ssl->f_set_timer = f_set_timer;
8507 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008508
8509 /* Make sure we start with no timer running */
8510 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008511}
8512
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008513#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008514void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01008515 void *p_cache,
8516 int (*f_get_cache)(void *, mbedtls_ssl_session *),
8517 int (*f_set_cache)(void *, const mbedtls_ssl_session *) )
Paul Bakker5121ce52009-01-03 21:22:43 +00008518{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01008519 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008520 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008521 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00008522}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008523#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008524
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008525#if defined(MBEDTLS_SSL_CLI_C)
8526int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00008527{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008528 int ret;
8529
8530 if( ssl == NULL ||
8531 session == NULL ||
8532 ssl->session_negotiate == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008533 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008534 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008535 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008536 }
8537
Hanno Becker52055ae2019-02-06 14:30:46 +00008538 if( ( ret = mbedtls_ssl_session_copy( ssl->session_negotiate,
8539 session ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008540 return( ret );
8541
Paul Bakker0a597072012-09-25 21:55:46 +00008542 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008543
8544 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00008545}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008546#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008547
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008548void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008549 const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00008550{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008551 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = ciphersuites;
8552 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = ciphersuites;
8553 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = ciphersuites;
8554 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = ciphersuites;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008555}
8556
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008557void mbedtls_ssl_conf_ciphersuites_for_version( mbedtls_ssl_config *conf,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02008558 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008559 int major, int minor )
8560{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008561 if( major != MBEDTLS_SSL_MAJOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008562 return;
8563
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008564 if( minor < MBEDTLS_SSL_MINOR_VERSION_0 || minor > MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008565 return;
8566
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008567 conf->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00008568}
8569
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008570#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02008571void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01008572 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02008573{
8574 conf->cert_profile = profile;
8575}
8576
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008577/* Append a new keycert entry to a (possibly empty) list */
8578static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
8579 mbedtls_x509_crt *cert,
8580 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008581{
niisato8ee24222018-06-25 19:05:48 +09008582 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008583
niisato8ee24222018-06-25 19:05:48 +09008584 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
8585 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008586 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008587
niisato8ee24222018-06-25 19:05:48 +09008588 new_cert->cert = cert;
8589 new_cert->key = key;
8590 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008591
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008592 /* Update head is the list was null, else add to the end */
8593 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01008594 {
niisato8ee24222018-06-25 19:05:48 +09008595 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01008596 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008597 else
8598 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008599 mbedtls_ssl_key_cert *cur = *head;
8600 while( cur->next != NULL )
8601 cur = cur->next;
niisato8ee24222018-06-25 19:05:48 +09008602 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008603 }
8604
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008605 return( 0 );
8606}
8607
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008608int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008609 mbedtls_x509_crt *own_cert,
8610 mbedtls_pk_context *pk_key )
8611{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02008612 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008613}
8614
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008615void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008616 mbedtls_x509_crt *ca_chain,
8617 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008618{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008619 conf->ca_chain = ca_chain;
8620 conf->ca_crl = ca_crl;
Hanno Becker5adaad92019-03-27 16:54:37 +00008621
8622#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
8623 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
8624 * cannot be used together. */
8625 conf->f_ca_cb = NULL;
8626 conf->p_ca_cb = NULL;
8627#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Paul Bakker5121ce52009-01-03 21:22:43 +00008628}
Hanno Becker5adaad92019-03-27 16:54:37 +00008629
8630#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
8631void mbedtls_ssl_conf_ca_cb( mbedtls_ssl_config *conf,
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00008632 mbedtls_x509_crt_ca_cb_t f_ca_cb,
Hanno Becker5adaad92019-03-27 16:54:37 +00008633 void *p_ca_cb )
8634{
8635 conf->f_ca_cb = f_ca_cb;
8636 conf->p_ca_cb = p_ca_cb;
8637
8638 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
8639 * cannot be used together. */
8640 conf->ca_chain = NULL;
8641 conf->ca_crl = NULL;
8642}
8643#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008644#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00008645
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02008646#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
8647int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
8648 mbedtls_x509_crt *own_cert,
8649 mbedtls_pk_context *pk_key )
8650{
8651 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
8652 own_cert, pk_key ) );
8653}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02008654
8655void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
8656 mbedtls_x509_crt *ca_chain,
8657 mbedtls_x509_crl *ca_crl )
8658{
8659 ssl->handshake->sni_ca_chain = ca_chain;
8660 ssl->handshake->sni_ca_crl = ca_crl;
8661}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02008662
8663void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
8664 int authmode )
8665{
8666 ssl->handshake->sni_authmode = authmode;
8667}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02008668#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
8669
Hanno Becker8927c832019-04-03 12:52:50 +01008670#if defined(MBEDTLS_X509_CRT_PARSE_C)
8671void mbedtls_ssl_set_verify( mbedtls_ssl_context *ssl,
8672 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
8673 void *p_vrfy )
8674{
8675 ssl->f_vrfy = f_vrfy;
8676 ssl->p_vrfy = p_vrfy;
8677}
8678#endif
8679
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008680#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008681/*
8682 * Set EC J-PAKE password for current handshake
8683 */
8684int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
8685 const unsigned char *pw,
8686 size_t pw_len )
8687{
8688 mbedtls_ecjpake_role role;
8689
Janos Follath8eb64132016-06-03 15:40:57 +01008690 if( ssl->handshake == NULL || ssl->conf == NULL )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008691 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8692
8693 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
8694 role = MBEDTLS_ECJPAKE_SERVER;
8695 else
8696 role = MBEDTLS_ECJPAKE_CLIENT;
8697
8698 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
8699 role,
8700 MBEDTLS_MD_SHA256,
8701 MBEDTLS_ECP_DP_SECP256R1,
8702 pw, pw_len ) );
8703}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008704#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008705
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008706#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008707
8708static void ssl_conf_remove_psk( mbedtls_ssl_config *conf )
8709{
8710 /* Remove reference to existing PSK, if any. */
8711#if defined(MBEDTLS_USE_PSA_CRYPTO)
8712 if( conf->psk_opaque != 0 )
8713 {
8714 /* The maintenance of the PSK key slot is the
8715 * user's responsibility. */
8716 conf->psk_opaque = 0;
8717 }
Hanno Beckera63ac3f2018-11-05 12:47:16 +00008718 /* This and the following branch should never
8719 * be taken simultaenously as we maintain the
8720 * invariant that raw and opaque PSKs are never
8721 * configured simultaneously. As a safeguard,
8722 * though, `else` is omitted here. */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008723#endif /* MBEDTLS_USE_PSA_CRYPTO */
8724 if( conf->psk != NULL )
8725 {
8726 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
8727
8728 mbedtls_free( conf->psk );
8729 conf->psk = NULL;
8730 conf->psk_len = 0;
8731 }
8732
8733 /* Remove reference to PSK identity, if any. */
8734 if( conf->psk_identity != NULL )
8735 {
8736 mbedtls_free( conf->psk_identity );
8737 conf->psk_identity = NULL;
8738 conf->psk_identity_len = 0;
8739 }
8740}
8741
Hanno Becker7390c712018-11-15 13:33:04 +00008742/* This function assumes that PSK identity in the SSL config is unset.
8743 * It checks that the provided identity is well-formed and attempts
8744 * to make a copy of it in the SSL config.
8745 * On failure, the PSK identity in the config remains unset. */
8746static int ssl_conf_set_psk_identity( mbedtls_ssl_config *conf,
8747 unsigned char const *psk_identity,
8748 size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02008749{
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02008750 /* Identity len will be encoded on two bytes */
Hanno Becker7390c712018-11-15 13:33:04 +00008751 if( psk_identity == NULL ||
8752 ( psk_identity_len >> 16 ) != 0 ||
Angus Grattond8213d02016-05-25 20:56:48 +10008753 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02008754 {
8755 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8756 }
8757
Hanno Becker7390c712018-11-15 13:33:04 +00008758 conf->psk_identity = mbedtls_calloc( 1, psk_identity_len );
8759 if( conf->psk_identity == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008760 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker6db455e2013-09-18 17:29:31 +02008761
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008762 conf->psk_identity_len = psk_identity_len;
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008763 memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker5ad403f2013-09-18 21:21:30 +02008764
8765 return( 0 );
Paul Bakker6db455e2013-09-18 17:29:31 +02008766}
8767
Hanno Becker7390c712018-11-15 13:33:04 +00008768int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
8769 const unsigned char *psk, size_t psk_len,
8770 const unsigned char *psk_identity, size_t psk_identity_len )
8771{
8772 int ret;
8773 /* Remove opaque/raw PSK + PSK Identity */
8774 ssl_conf_remove_psk( conf );
8775
8776 /* Check and set raw PSK */
8777 if( psk == NULL || psk_len > MBEDTLS_PSK_MAX_LEN )
8778 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8779 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
8780 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
8781 conf->psk_len = psk_len;
8782 memcpy( conf->psk, psk, conf->psk_len );
8783
8784 /* Check and set PSK Identity */
8785 ret = ssl_conf_set_psk_identity( conf, psk_identity, psk_identity_len );
8786 if( ret != 0 )
8787 ssl_conf_remove_psk( conf );
8788
8789 return( ret );
8790}
8791
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008792static void ssl_remove_psk( mbedtls_ssl_context *ssl )
8793{
8794#if defined(MBEDTLS_USE_PSA_CRYPTO)
8795 if( ssl->handshake->psk_opaque != 0 )
8796 {
8797 ssl->handshake->psk_opaque = 0;
8798 }
8799 else
8800#endif /* MBEDTLS_USE_PSA_CRYPTO */
8801 if( ssl->handshake->psk != NULL )
8802 {
8803 mbedtls_platform_zeroize( ssl->handshake->psk,
8804 ssl->handshake->psk_len );
8805 mbedtls_free( ssl->handshake->psk );
8806 ssl->handshake->psk_len = 0;
8807 }
8808}
8809
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008810int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
8811 const unsigned char *psk, size_t psk_len )
8812{
8813 if( psk == NULL || ssl->handshake == NULL )
8814 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8815
8816 if( psk_len > MBEDTLS_PSK_MAX_LEN )
8817 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8818
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008819 ssl_remove_psk( ssl );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008820
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008821 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008822 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008823
8824 ssl->handshake->psk_len = psk_len;
8825 memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
8826
8827 return( 0 );
8828}
8829
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008830#if defined(MBEDTLS_USE_PSA_CRYPTO)
8831int mbedtls_ssl_conf_psk_opaque( mbedtls_ssl_config *conf,
Andrzej Kurek2349c4d2019-01-08 09:36:01 -05008832 psa_key_handle_t psk_slot,
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008833 const unsigned char *psk_identity,
8834 size_t psk_identity_len )
8835{
Hanno Becker7390c712018-11-15 13:33:04 +00008836 int ret;
8837 /* Clear opaque/raw PSK + PSK Identity, if present. */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008838 ssl_conf_remove_psk( conf );
8839
Hanno Becker7390c712018-11-15 13:33:04 +00008840 /* Check and set opaque PSK */
8841 if( psk_slot == 0 )
8842 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008843 conf->psk_opaque = psk_slot;
Hanno Becker7390c712018-11-15 13:33:04 +00008844
8845 /* Check and set PSK Identity */
8846 ret = ssl_conf_set_psk_identity( conf, psk_identity,
8847 psk_identity_len );
8848 if( ret != 0 )
8849 ssl_conf_remove_psk( conf );
8850
8851 return( ret );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008852}
8853
8854int mbedtls_ssl_set_hs_psk_opaque( mbedtls_ssl_context *ssl,
Andrzej Kurek2349c4d2019-01-08 09:36:01 -05008855 psa_key_handle_t psk_slot )
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008856{
8857 if( psk_slot == 0 || ssl->handshake == NULL )
8858 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8859
8860 ssl_remove_psk( ssl );
8861 ssl->handshake->psk_opaque = psk_slot;
8862 return( 0 );
8863}
8864#endif /* MBEDTLS_USE_PSA_CRYPTO */
8865
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008866void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008867 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
Paul Bakker6db455e2013-09-18 17:29:31 +02008868 size_t),
8869 void *p_psk )
8870{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008871 conf->f_psk = f_psk;
8872 conf->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02008873}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008874#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00008875
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02008876#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker470a8c42017-10-04 15:28:46 +01008877
8878#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008879int mbedtls_ssl_conf_dh_param( mbedtls_ssl_config *conf, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00008880{
8881 int ret;
8882
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008883 if( ( ret = mbedtls_mpi_read_string( &conf->dhm_P, 16, dhm_P ) ) != 0 ||
8884 ( ret = mbedtls_mpi_read_string( &conf->dhm_G, 16, dhm_G ) ) != 0 )
8885 {
8886 mbedtls_mpi_free( &conf->dhm_P );
8887 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00008888 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008889 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008890
8891 return( 0 );
8892}
Hanno Becker470a8c42017-10-04 15:28:46 +01008893#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +00008894
Hanno Beckera90658f2017-10-04 15:29:08 +01008895int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
8896 const unsigned char *dhm_P, size_t P_len,
8897 const unsigned char *dhm_G, size_t G_len )
8898{
8899 int ret;
8900
8901 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
8902 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
8903 {
8904 mbedtls_mpi_free( &conf->dhm_P );
8905 mbedtls_mpi_free( &conf->dhm_G );
8906 return( ret );
8907 }
8908
8909 return( 0 );
8910}
Paul Bakker5121ce52009-01-03 21:22:43 +00008911
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008912int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00008913{
8914 int ret;
8915
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008916 if( ( ret = mbedtls_mpi_copy( &conf->dhm_P, &dhm_ctx->P ) ) != 0 ||
8917 ( ret = mbedtls_mpi_copy( &conf->dhm_G, &dhm_ctx->G ) ) != 0 )
8918 {
8919 mbedtls_mpi_free( &conf->dhm_P );
8920 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00008921 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008922 }
Paul Bakker1b57b062011-01-06 15:48:19 +00008923
8924 return( 0 );
8925}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02008926#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00008927
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02008928#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
8929/*
8930 * Set the minimum length for Diffie-Hellman parameters
8931 */
8932void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
8933 unsigned int bitlen )
8934{
8935 conf->dhm_min_bitlen = bitlen;
8936}
8937#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
8938
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02008939#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02008940/*
8941 * Set allowed/preferred hashes for handshake signatures
8942 */
8943void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
8944 const int *hashes )
8945{
8946 conf->sig_hashes = hashes;
8947}
Hanno Becker947194e2017-04-07 13:25:49 +01008948#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02008949
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02008950#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008951/*
8952 * Set the allowed elliptic curves
8953 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008954void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008955 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008956{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008957 conf->curve_list = curve_list;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008958}
Hanno Becker947194e2017-04-07 13:25:49 +01008959#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008960
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008961#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008962int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00008963{
Hanno Becker947194e2017-04-07 13:25:49 +01008964 /* Initialize to suppress unnecessary compiler warning */
8965 size_t hostname_len = 0;
8966
8967 /* Check if new hostname is valid before
8968 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01008969 if( hostname != NULL )
8970 {
8971 hostname_len = strlen( hostname );
8972
8973 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
8974 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8975 }
8976
8977 /* Now it's clear that we will overwrite the old hostname,
8978 * so we can free it safely */
8979
8980 if( ssl->hostname != NULL )
8981 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008982 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Hanno Becker947194e2017-04-07 13:25:49 +01008983 mbedtls_free( ssl->hostname );
8984 }
8985
8986 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01008987
Paul Bakker5121ce52009-01-03 21:22:43 +00008988 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01008989 {
8990 ssl->hostname = NULL;
8991 }
8992 else
8993 {
8994 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01008995 if( ssl->hostname == NULL )
8996 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02008997
Hanno Becker947194e2017-04-07 13:25:49 +01008998 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02008999
Hanno Becker947194e2017-04-07 13:25:49 +01009000 ssl->hostname[hostname_len] = '\0';
9001 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009002
9003 return( 0 );
9004}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01009005#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00009006
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01009007#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009008void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009009 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00009010 const unsigned char *, size_t),
9011 void *p_sni )
9012{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009013 conf->f_sni = f_sni;
9014 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00009015}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009016#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00009017
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009018#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009019int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009020{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009021 size_t cur_len, tot_len;
9022 const char **p;
9023
9024 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08009025 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
9026 * MUST NOT be truncated."
9027 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009028 */
9029 tot_len = 0;
9030 for( p = protos; *p != NULL; p++ )
9031 {
9032 cur_len = strlen( *p );
9033 tot_len += cur_len;
9034
9035 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009036 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009037 }
9038
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009039 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009040
9041 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009042}
9043
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009044const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009045{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009046 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009047}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009048#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009049
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02009050void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00009051{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009052 conf->max_major_ver = major;
9053 conf->max_minor_ver = minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00009054}
9055
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02009056void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00009057{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009058 conf->min_major_ver = major;
9059 conf->min_minor_ver = minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00009060}
9061
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009062#if defined(MBEDTLS_SSL_FALLBACK_SCSV) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009063void mbedtls_ssl_conf_fallback( mbedtls_ssl_config *conf, char fallback )
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02009064{
Manuel Pégourié-Gonnard684b0592015-05-06 09:27:31 +01009065 conf->fallback = fallback;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02009066}
9067#endif
9068
Janos Follath088ce432017-04-10 12:42:31 +01009069#if defined(MBEDTLS_SSL_SRV_C)
9070void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
9071 char cert_req_ca_list )
9072{
9073 conf->cert_req_ca_list = cert_req_ca_list;
9074}
9075#endif
9076
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009077#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009078void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01009079{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009080 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01009081}
9082#endif
9083
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009084#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009085void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02009086{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009087 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02009088}
9089#endif
9090
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02009091#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009092void mbedtls_ssl_conf_arc4_support( mbedtls_ssl_config *conf, char arc4 )
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01009093{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009094 conf->arc4_disabled = arc4;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01009095}
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02009096#endif
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01009097
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009098#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009099int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009100{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009101 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
Angus Grattond8213d02016-05-25 20:56:48 +10009102 ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009103 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009104 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009105 }
9106
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01009107 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009108
9109 return( 0 );
9110}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009111#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009112
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009113#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02009114void mbedtls_ssl_conf_truncated_hmac( mbedtls_ssl_config *conf, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02009115{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009116 conf->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02009117}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009118#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02009119
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009120#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009121void mbedtls_ssl_conf_cbc_record_splitting( mbedtls_ssl_config *conf, char split )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009122{
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01009123 conf->cbc_record_splitting = split;
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009124}
9125#endif
9126
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009127void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00009128{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009129 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00009130}
9131
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009132#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009133void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01009134{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009135 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01009136}
9137
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009138void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009139{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009140 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009141}
9142
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009143void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01009144 const unsigned char period[8] )
9145{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009146 memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01009147}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009148#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00009149
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009150#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009151#if defined(MBEDTLS_SSL_CLI_C)
9152void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02009153{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01009154 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02009155}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009156#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02009157
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009158#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02009159void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
9160 mbedtls_ssl_ticket_write_t *f_ticket_write,
9161 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
9162 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02009163{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02009164 conf->f_ticket_write = f_ticket_write;
9165 conf->f_ticket_parse = f_ticket_parse;
9166 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02009167}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009168#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009169#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02009170
Robert Cragie4feb7ae2015-10-02 13:33:37 +01009171#if defined(MBEDTLS_SSL_EXPORT_KEYS)
9172void mbedtls_ssl_conf_export_keys_cb( mbedtls_ssl_config *conf,
9173 mbedtls_ssl_export_keys_t *f_export_keys,
9174 void *p_export_keys )
9175{
9176 conf->f_export_keys = f_export_keys;
9177 conf->p_export_keys = p_export_keys;
9178}
Ron Eldorf5cc10d2019-05-07 18:33:40 +03009179
9180void mbedtls_ssl_conf_export_keys_ext_cb( mbedtls_ssl_config *conf,
9181 mbedtls_ssl_export_keys_ext_t *f_export_keys_ext,
9182 void *p_export_keys )
9183{
9184 conf->f_export_keys_ext = f_export_keys_ext;
9185 conf->p_export_keys = p_export_keys;
9186}
Robert Cragie4feb7ae2015-10-02 13:33:37 +01009187#endif
9188
Gilles Peskineb74a1c72018-04-24 13:09:22 +02009189#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009190void mbedtls_ssl_conf_async_private_cb(
9191 mbedtls_ssl_config *conf,
9192 mbedtls_ssl_async_sign_t *f_async_sign,
9193 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
9194 mbedtls_ssl_async_resume_t *f_async_resume,
9195 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009196 void *async_config_data )
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009197{
9198 conf->f_async_sign_start = f_async_sign;
9199 conf->f_async_decrypt_start = f_async_decrypt;
9200 conf->f_async_resume = f_async_resume;
9201 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009202 conf->p_async_config_data = async_config_data;
9203}
9204
Gilles Peskine8f97af72018-04-26 11:46:10 +02009205void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
9206{
9207 return( conf->p_async_config_data );
9208}
9209
Gilles Peskine1febfef2018-04-30 11:54:39 +02009210void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009211{
9212 if( ssl->handshake == NULL )
9213 return( NULL );
9214 else
9215 return( ssl->handshake->user_async_ctx );
9216}
9217
Gilles Peskine1febfef2018-04-30 11:54:39 +02009218void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009219 void *ctx )
9220{
9221 if( ssl->handshake != NULL )
9222 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009223}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02009224#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009225
Paul Bakker5121ce52009-01-03 21:22:43 +00009226/*
9227 * SSL get accessors
9228 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009229size_t mbedtls_ssl_get_bytes_avail( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009230{
9231 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
9232}
9233
Hanno Becker8b170a02017-10-10 11:51:19 +01009234int mbedtls_ssl_check_pending( const mbedtls_ssl_context *ssl )
9235{
9236 /*
9237 * Case A: We're currently holding back
9238 * a message for further processing.
9239 */
9240
9241 if( ssl->keep_current_message == 1 )
9242 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009243 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: record held back for processing" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009244 return( 1 );
9245 }
9246
9247 /*
9248 * Case B: Further records are pending in the current datagram.
9249 */
9250
9251#if defined(MBEDTLS_SSL_PROTO_DTLS)
9252 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
9253 ssl->in_left > ssl->next_record_offset )
9254 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009255 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more records within current datagram" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009256 return( 1 );
9257 }
9258#endif /* MBEDTLS_SSL_PROTO_DTLS */
9259
9260 /*
9261 * Case C: A handshake message is being processed.
9262 */
9263
Hanno Becker8b170a02017-10-10 11:51:19 +01009264 if( ssl->in_hslen > 0 && ssl->in_hslen < ssl->in_msglen )
9265 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009266 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more handshake messages within current record" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009267 return( 1 );
9268 }
9269
9270 /*
9271 * Case D: An application data message is being processed
9272 */
9273 if( ssl->in_offt != NULL )
9274 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009275 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: application data record is being processed" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009276 return( 1 );
9277 }
9278
9279 /*
9280 * In all other cases, the rest of the message can be dropped.
Hanno Beckerc573ac32018-08-28 17:15:25 +01009281 * As in ssl_get_next_record, this needs to be adapted if
Hanno Becker8b170a02017-10-10 11:51:19 +01009282 * we implement support for multiple alerts in single records.
9283 */
9284
9285 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: nothing pending" ) );
9286 return( 0 );
9287}
9288
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02009289uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009290{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00009291 if( ssl->session != NULL )
9292 return( ssl->session->verify_result );
9293
9294 if( ssl->session_negotiate != NULL )
9295 return( ssl->session_negotiate->verify_result );
9296
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02009297 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00009298}
9299
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009300const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00009301{
Paul Bakker926c8e42013-03-06 10:23:34 +01009302 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009303 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01009304
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009305 return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00009306}
9307
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009308const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00009309{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009310#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009311 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009312 {
9313 switch( ssl->minor_ver )
9314 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009315 case MBEDTLS_SSL_MINOR_VERSION_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009316 return( "DTLSv1.0" );
9317
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009318 case MBEDTLS_SSL_MINOR_VERSION_3:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009319 return( "DTLSv1.2" );
9320
9321 default:
9322 return( "unknown (DTLS)" );
9323 }
9324 }
9325#endif
9326
Paul Bakker43ca69c2011-01-15 17:35:19 +00009327 switch( ssl->minor_ver )
9328 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009329 case MBEDTLS_SSL_MINOR_VERSION_0:
Paul Bakker43ca69c2011-01-15 17:35:19 +00009330 return( "SSLv3.0" );
9331
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009332 case MBEDTLS_SSL_MINOR_VERSION_1:
Paul Bakker43ca69c2011-01-15 17:35:19 +00009333 return( "TLSv1.0" );
9334
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009335 case MBEDTLS_SSL_MINOR_VERSION_2:
Paul Bakker43ca69c2011-01-15 17:35:19 +00009336 return( "TLSv1.1" );
9337
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009338 case MBEDTLS_SSL_MINOR_VERSION_3:
Paul Bakker1ef83d62012-04-11 12:09:53 +00009339 return( "TLSv1.2" );
9340
Paul Bakker43ca69c2011-01-15 17:35:19 +00009341 default:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009342 return( "unknown" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00009343 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00009344}
9345
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009346int mbedtls_ssl_get_record_expansion( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009347{
Hanno Becker3136ede2018-08-17 15:28:19 +01009348 size_t transform_expansion = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009349 const mbedtls_ssl_transform *transform = ssl->transform_out;
Hanno Becker5b559ac2018-08-03 09:40:07 +01009350 unsigned block_size;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009351
Hanno Becker5903de42019-05-03 14:46:38 +01009352 size_t out_hdr_len = mbedtls_ssl_out_hdr_len( ssl );
9353
Hanno Becker78640902018-08-13 16:35:15 +01009354 if( transform == NULL )
Hanno Becker5903de42019-05-03 14:46:38 +01009355 return( (int) out_hdr_len );
Hanno Becker78640902018-08-13 16:35:15 +01009356
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009357#if defined(MBEDTLS_ZLIB_SUPPORT)
9358 if( ssl->session_out->compression != MBEDTLS_SSL_COMPRESS_NULL )
9359 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009360#endif
9361
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009362 switch( mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009363 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009364 case MBEDTLS_MODE_GCM:
9365 case MBEDTLS_MODE_CCM:
Hanno Becker5b559ac2018-08-03 09:40:07 +01009366 case MBEDTLS_MODE_CHACHAPOLY:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009367 case MBEDTLS_MODE_STREAM:
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009368 transform_expansion = transform->minlen;
9369 break;
9370
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009371 case MBEDTLS_MODE_CBC:
Hanno Becker5b559ac2018-08-03 09:40:07 +01009372
9373 block_size = mbedtls_cipher_get_block_size(
9374 &transform->cipher_ctx_enc );
9375
Hanno Becker3136ede2018-08-17 15:28:19 +01009376 /* Expansion due to the addition of the MAC. */
9377 transform_expansion += transform->maclen;
9378
9379 /* Expansion due to the addition of CBC padding;
9380 * Theoretically up to 256 bytes, but we never use
9381 * more than the block size of the underlying cipher. */
9382 transform_expansion += block_size;
9383
9384 /* For TLS 1.1 or higher, an explicit IV is added
9385 * after the record header. */
Hanno Becker5b559ac2018-08-03 09:40:07 +01009386#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
9387 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Hanno Becker3136ede2018-08-17 15:28:19 +01009388 transform_expansion += block_size;
Hanno Becker5b559ac2018-08-03 09:40:07 +01009389#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker3136ede2018-08-17 15:28:19 +01009390
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009391 break;
9392
9393 default:
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02009394 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009395 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009396 }
9397
Hanno Beckera0e20d02019-05-15 14:03:01 +01009398#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker6cbad552019-05-08 15:40:11 +01009399 if( transform->out_cid_len != 0 )
9400 transform_expansion += MBEDTLS_SSL_MAX_CID_EXPANSION;
Hanno Beckera0e20d02019-05-15 14:03:01 +01009401#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker6cbad552019-05-08 15:40:11 +01009402
Hanno Becker5903de42019-05-03 14:46:38 +01009403 return( (int)( out_hdr_len + transform_expansion ) );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009404}
9405
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009406#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9407size_t mbedtls_ssl_get_max_frag_len( const mbedtls_ssl_context *ssl )
9408{
9409 size_t max_len;
9410
9411 /*
9412 * Assume mfl_code is correct since it was checked when set
9413 */
Angus Grattond8213d02016-05-25 20:56:48 +10009414 max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009415
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02009416 /* Check if a smaller max length was negotiated */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009417 if( ssl->session_out != NULL &&
Angus Grattond8213d02016-05-25 20:56:48 +10009418 ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009419 {
Angus Grattond8213d02016-05-25 20:56:48 +10009420 max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009421 }
9422
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02009423 /* During a handshake, use the value being negotiated */
9424 if( ssl->session_negotiate != NULL &&
9425 ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
9426 {
9427 max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
9428 }
9429
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009430 return( max_len );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009431}
9432#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
9433
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009434#if defined(MBEDTLS_SSL_PROTO_DTLS)
9435static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl )
9436{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04009437 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
9438 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
9439 ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
9440 ssl->state == MBEDTLS_SSL_SERVER_HELLO ) )
9441 return ( 0 );
9442
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009443 if( ssl->handshake == NULL || ssl->handshake->mtu == 0 )
9444 return( ssl->mtu );
9445
9446 if( ssl->mtu == 0 )
9447 return( ssl->handshake->mtu );
9448
9449 return( ssl->mtu < ssl->handshake->mtu ?
9450 ssl->mtu : ssl->handshake->mtu );
9451}
9452#endif /* MBEDTLS_SSL_PROTO_DTLS */
9453
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009454int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
9455{
9456 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
9457
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02009458#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
9459 !defined(MBEDTLS_SSL_PROTO_DTLS)
9460 (void) ssl;
9461#endif
9462
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009463#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9464 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
9465
9466 if( max_len > mfl )
9467 max_len = mfl;
9468#endif
9469
9470#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009471 if( ssl_get_current_mtu( ssl ) != 0 )
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009472 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009473 const size_t mtu = ssl_get_current_mtu( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009474 const int ret = mbedtls_ssl_get_record_expansion( ssl );
9475 const size_t overhead = (size_t) ret;
9476
9477 if( ret < 0 )
9478 return( ret );
9479
9480 if( mtu <= overhead )
9481 {
9482 MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
9483 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
9484 }
9485
9486 if( max_len > mtu - overhead )
9487 max_len = mtu - overhead;
9488 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009489#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009490
Hanno Becker0defedb2018-08-10 12:35:02 +01009491#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
9492 !defined(MBEDTLS_SSL_PROTO_DTLS)
9493 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009494#endif
9495
9496 return( (int) max_len );
9497}
9498
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009499#if defined(MBEDTLS_X509_CRT_PARSE_C)
9500const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00009501{
9502 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009503 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00009504
Hanno Beckere6824572019-02-07 13:18:46 +00009505#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009506 return( ssl->session->peer_cert );
Hanno Beckere6824572019-02-07 13:18:46 +00009507#else
9508 return( NULL );
9509#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakkerb0550d92012-10-30 07:51:03 +00009510}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009511#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00009512
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009513#if defined(MBEDTLS_SSL_CLI_C)
Hanno Beckerf852b1c2019-02-05 11:42:30 +00009514int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl,
9515 mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009516{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009517 if( ssl == NULL ||
9518 dst == NULL ||
9519 ssl->session == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009520 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009521 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009522 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009523 }
9524
Hanno Becker52055ae2019-02-06 14:30:46 +00009525 return( mbedtls_ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009526}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009527#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009528
Paul Bakker5121ce52009-01-03 21:22:43 +00009529/*
Paul Bakker1961b702013-01-25 14:49:24 +01009530 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00009531 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009532int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009533{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009534 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00009535
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009536 if( ssl == NULL || ssl->conf == NULL )
9537 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9538
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009539#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009540 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009541 ret = mbedtls_ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00009542#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009543#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009544 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009545 ret = mbedtls_ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00009546#endif
9547
Paul Bakker1961b702013-01-25 14:49:24 +01009548 return( ret );
9549}
9550
9551/*
9552 * Perform the SSL handshake
9553 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009554int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +01009555{
9556 int ret = 0;
9557
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009558 if( ssl == NULL || ssl->conf == NULL )
9559 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9560
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009561 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +01009562
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009563 while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker1961b702013-01-25 14:49:24 +01009564 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009565 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01009566
9567 if( ret != 0 )
9568 break;
9569 }
9570
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009571 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009572
9573 return( ret );
9574}
9575
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009576#if defined(MBEDTLS_SSL_RENEGOTIATION)
9577#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00009578/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009579 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00009580 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009581static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009582{
9583 int ret;
9584
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009585 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009586
9587 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009588 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
9589 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009590
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02009591 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009592 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02009593 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009594 return( ret );
9595 }
9596
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009597 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009598
9599 return( 0 );
9600}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009601#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009602
9603/*
9604 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009605 * - any side: calling mbedtls_ssl_renegotiate(),
9606 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
9607 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02009608 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009609 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009610 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009611 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009612static int ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00009613{
9614 int ret;
9615
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009616 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009617
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009618 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
9619 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00009620
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009621 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
9622 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009623#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009624 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009625 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009626 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009627 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02009628 ssl->handshake->out_msg_seq = 1;
9629 else
9630 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009631 }
9632#endif
9633
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009634 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
9635 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00009636
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009637 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00009638 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009639 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00009640 return( ret );
9641 }
9642
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009643 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009644
9645 return( 0 );
9646}
9647
9648/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009649 * Renegotiate current connection on client,
9650 * or request renegotiation on server
9651 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009652int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009653{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009654 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009655
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009656 if( ssl == NULL || ssl->conf == NULL )
9657 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9658
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009659#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009660 /* On server, just send the request */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009661 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009662 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009663 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
9664 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009665
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009666 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02009667
9668 /* Did we already try/start sending HelloRequest? */
9669 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009670 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02009671
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009672 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009673 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009674#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009675
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009676#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009677 /*
9678 * On client, either start the renegotiation process or,
9679 * if already in progress, continue the handshake
9680 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009681 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009682 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009683 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
9684 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009685
9686 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
9687 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009688 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009689 return( ret );
9690 }
9691 }
9692 else
9693 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009694 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009695 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009696 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009697 return( ret );
9698 }
9699 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009700#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009701
Paul Bakker37ce0ff2013-10-31 14:32:04 +01009702 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009703}
9704
9705/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009706 * Check record counters and renegotiate if they're above the limit.
9707 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009708static int ssl_check_ctr_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009709{
Andres AG2196c7f2016-12-15 17:01:16 +00009710 size_t ep_len = ssl_ep_len( ssl );
9711 int in_ctr_cmp;
9712 int out_ctr_cmp;
9713
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009714 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ||
9715 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009716 ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009717 {
9718 return( 0 );
9719 }
9720
Andres AG2196c7f2016-12-15 17:01:16 +00009721 in_ctr_cmp = memcmp( ssl->in_ctr + ep_len,
9722 ssl->conf->renego_period + ep_len, 8 - ep_len );
Hanno Becker19859472018-08-06 09:40:20 +01009723 out_ctr_cmp = memcmp( ssl->cur_out_ctr + ep_len,
Andres AG2196c7f2016-12-15 17:01:16 +00009724 ssl->conf->renego_period + ep_len, 8 - ep_len );
9725
9726 if( in_ctr_cmp <= 0 && out_ctr_cmp <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009727 {
9728 return( 0 );
9729 }
9730
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02009731 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009732 return( mbedtls_ssl_renegotiate( ssl ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009733}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009734#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00009735
9736/*
9737 * Receive application data decrypted from the SSL layer
9738 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009739int mbedtls_ssl_read( mbedtls_ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00009740{
Hanno Becker4a810fb2017-05-24 16:27:30 +01009741 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +00009742 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00009743
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009744 if( ssl == NULL || ssl->conf == NULL )
9745 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9746
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009747 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009748
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009749#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009750 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009751 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009752 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009753 return( ret );
9754
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009755 if( ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009756 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009757 {
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02009758 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009759 return( ret );
9760 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009761 }
9762#endif
9763
Hanno Becker4a810fb2017-05-24 16:27:30 +01009764 /*
9765 * Check if renegotiation is necessary and/or handshake is
9766 * in process. If yes, perform/continue, and fall through
9767 * if an unexpected packet is received while the client
9768 * is waiting for the ServerHello.
9769 *
9770 * (There is no equivalent to the last condition on
9771 * the server-side as it is not treated as within
9772 * a handshake while waiting for the ClientHello
9773 * after a renegotiation request.)
9774 */
9775
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009776#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01009777 ret = ssl_check_ctr_renegotiate( ssl );
9778 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
9779 ret != 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009780 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009781 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009782 return( ret );
9783 }
9784#endif
9785
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009786 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00009787 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009788 ret = mbedtls_ssl_handshake( ssl );
Hanno Becker4a810fb2017-05-24 16:27:30 +01009789 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
9790 ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009791 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009792 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009793 return( ret );
9794 }
9795 }
9796
Hanno Beckere41158b2017-10-23 13:30:32 +01009797 /* Loop as long as no application data record is available */
Hanno Becker90333da2017-10-10 11:27:13 +01009798 while( ssl->in_offt == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00009799 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02009800 /* Start timer if not already running */
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02009801 if( ssl->f_get_timer != NULL &&
9802 ssl->f_get_timer( ssl->p_timer ) == -1 )
9803 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009804 ssl_set_timer( ssl, ssl->conf->read_timeout );
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02009805 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02009806
Hanno Becker327c93b2018-08-15 13:56:18 +01009807 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009808 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01009809 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
9810 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +00009811
Hanno Becker4a810fb2017-05-24 16:27:30 +01009812 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
9813 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009814 }
9815
9816 if( ssl->in_msglen == 0 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009817 ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00009818 {
9819 /*
9820 * OpenSSL sends empty messages to randomize the IV
9821 */
Hanno Becker327c93b2018-08-15 13:56:18 +01009822 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009823 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009824 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
Paul Bakker831a7552011-05-18 13:32:51 +00009825 return( 0 );
9826
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009827 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009828 return( ret );
9829 }
9830 }
9831
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009832 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +00009833 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009834 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009835
Hanno Becker4a810fb2017-05-24 16:27:30 +01009836 /*
9837 * - For client-side, expect SERVER_HELLO_REQUEST.
9838 * - For server-side, expect CLIENT_HELLO.
9839 * - Fail (TLS) or silently drop record (DTLS) in other cases.
9840 */
9841
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009842#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009843 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009844 ( ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST ||
Hanno Becker4a810fb2017-05-24 16:27:30 +01009845 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ) )
Paul Bakker48916f92012-09-16 19:57:18 +00009846 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009847 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009848
9849 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009850#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009851 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01009852 {
9853 continue;
9854 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009855#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009856 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009857 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01009858#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009859
Hanno Becker4a810fb2017-05-24 16:27:30 +01009860#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009861 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009862 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO )
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009863 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009864 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009865
9866 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009867#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009868 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01009869 {
9870 continue;
9871 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009872#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009873 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker48916f92012-09-16 19:57:18 +00009874 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01009875#endif /* MBEDTLS_SSL_SRV_C */
9876
Hanno Becker21df7f92017-10-17 11:03:26 +01009877#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01009878 /* Determine whether renegotiation attempt should be accepted */
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01009879 if( ! ( ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
9880 ( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
9881 ssl->conf->allow_legacy_renegotiation ==
9882 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) ) )
9883 {
9884 /*
9885 * Accept renegotiation request
9886 */
Paul Bakker48916f92012-09-16 19:57:18 +00009887
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01009888 /* DTLS clients need to know renego is server-initiated */
9889#if defined(MBEDTLS_SSL_PROTO_DTLS)
9890 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
9891 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
9892 {
9893 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
9894 }
9895#endif
9896 ret = ssl_start_renegotiation( ssl );
9897 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
9898 ret != 0 )
9899 {
9900 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
9901 return( ret );
9902 }
9903 }
9904 else
Hanno Becker21df7f92017-10-17 11:03:26 +01009905#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +00009906 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01009907 /*
9908 * Refuse renegotiation
9909 */
9910
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009911 MBEDTLS_SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009912
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009913#if defined(MBEDTLS_SSL_PROTO_SSL3)
9914 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00009915 {
Gilles Peskine92e44262017-05-10 17:27:49 +02009916 /* SSLv3 does not have a "no_renegotiation" warning, so
9917 we send a fatal alert and abort the connection. */
9918 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
9919 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
9920 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00009921 }
9922 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009923#endif /* MBEDTLS_SSL_PROTO_SSL3 */
9924#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
9925 defined(MBEDTLS_SSL_PROTO_TLS1_2)
9926 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00009927 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009928 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
9929 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
9930 MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00009931 {
9932 return( ret );
9933 }
Paul Bakker48916f92012-09-16 19:57:18 +00009934 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02009935 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009936#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 ||
9937 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02009938 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009939 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
9940 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02009941 }
Paul Bakker48916f92012-09-16 19:57:18 +00009942 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009943
Hanno Becker90333da2017-10-10 11:27:13 +01009944 /* At this point, we don't know whether the renegotiation has been
9945 * completed or not. The cases to consider are the following:
9946 * 1) The renegotiation is complete. In this case, no new record
9947 * has been read yet.
9948 * 2) The renegotiation is incomplete because the client received
9949 * an application data record while awaiting the ServerHello.
9950 * 3) The renegotiation is incomplete because the client received
9951 * a non-handshake, non-application data message while awaiting
9952 * the ServerHello.
9953 * In each of these case, looping will be the proper action:
9954 * - For 1), the next iteration will read a new record and check
9955 * if it's application data.
9956 * - For 2), the loop condition isn't satisfied as application data
9957 * is present, hence continue is the same as break
9958 * - For 3), the loop condition is satisfied and read_record
9959 * will re-deliver the message that was held back by the client
9960 * when expecting the ServerHello.
9961 */
9962 continue;
Paul Bakker48916f92012-09-16 19:57:18 +00009963 }
Hanno Becker21df7f92017-10-17 11:03:26 +01009964#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009965 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01009966 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009967 if( ssl->conf->renego_max_records >= 0 )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009968 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009969 if( ++ssl->renego_records_seen > ssl->conf->renego_max_records )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009970 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009971 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009972 "but not honored by client" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009973 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009974 }
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009975 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01009976 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009977#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009978
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009979 /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */
9980 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009981 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009982 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01009983 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009984 }
9985
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009986 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00009987 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009988 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
9989 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00009990 }
9991
9992 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02009993
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02009994 /* We're going to return something now, cancel timer,
9995 * except if handshake (renegotiation) is in progress */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009996 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02009997 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009998
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02009999#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010000 /* If we requested renego but received AppData, resend HelloRequest.
10001 * Do it now, after setting in_offt, to avoid taking this branch
10002 * again if ssl_write_hello_request() returns WANT_WRITE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010003#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010004 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010005 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010006 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020010007 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010008 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010009 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010010 return( ret );
10011 }
10012 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010013#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker4a810fb2017-05-24 16:27:30 +010010014#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +000010015 }
10016
10017 n = ( len < ssl->in_msglen )
10018 ? len : ssl->in_msglen;
10019
10020 memcpy( buf, ssl->in_offt, n );
10021 ssl->in_msglen -= n;
10022
10023 if( ssl->in_msglen == 0 )
Hanno Becker4a810fb2017-05-24 16:27:30 +010010024 {
10025 /* all bytes consumed */
Paul Bakker5121ce52009-01-03 21:22:43 +000010026 ssl->in_offt = NULL;
Hanno Beckerbdf39052017-06-09 10:42:03 +010010027 ssl->keep_current_message = 0;
Hanno Becker4a810fb2017-05-24 16:27:30 +010010028 }
Paul Bakker5121ce52009-01-03 21:22:43 +000010029 else
Hanno Becker4a810fb2017-05-24 16:27:30 +010010030 {
Paul Bakker5121ce52009-01-03 21:22:43 +000010031 /* more data available */
10032 ssl->in_offt += n;
Hanno Becker4a810fb2017-05-24 16:27:30 +010010033 }
Paul Bakker5121ce52009-01-03 21:22:43 +000010034
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010035 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010036
Paul Bakker23986e52011-04-24 08:57:21 +000010037 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +000010038}
10039
10040/*
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010010041 * Send application data to be encrypted by the SSL layer, taking care of max
10042 * fragment length and buffer size.
10043 *
10044 * According to RFC 5246 Section 6.2.1:
10045 *
10046 * Zero-length fragments of Application data MAY be sent as they are
10047 * potentially useful as a traffic analysis countermeasure.
10048 *
10049 * Therefore, it is possible that the input message length is 0 and the
10050 * corresponding return code is 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +000010051 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010052static int ssl_write_real( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010053 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +000010054{
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +020010055 int ret = mbedtls_ssl_get_max_out_record_payload( ssl );
10056 const size_t max_len = (size_t) ret;
10057
10058 if( ret < 0 )
10059 {
10060 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_get_max_out_record_payload", ret );
10061 return( ret );
10062 }
10063
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010064 if( len > max_len )
10065 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010066#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010067 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010068 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010069 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) "
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010070 "maximum fragment length: %d > %d",
10071 len, max_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010072 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010073 }
10074 else
10075#endif
10076 len = max_len;
10077 }
Paul Bakker887bd502011-06-08 13:10:54 +000010078
Paul Bakker5121ce52009-01-03 21:22:43 +000010079 if( ssl->out_left != 0 )
10080 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010010081 /*
10082 * The user has previously tried to send the data and
10083 * MBEDTLS_ERR_SSL_WANT_WRITE or the message was only partially
10084 * written. In this case, we expect the high-level write function
10085 * (e.g. mbedtls_ssl_write()) to be called with the same parameters
10086 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010087 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010088 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010089 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010090 return( ret );
10091 }
10092 }
Paul Bakker887bd502011-06-08 13:10:54 +000010093 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +000010094 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010010095 /*
10096 * The user is trying to send a message the first time, so we need to
10097 * copy the data into the internal buffers and setup the data structure
10098 * to keep track of partial writes
10099 */
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010100 ssl->out_msglen = len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010101 ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA;
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010102 memcpy( ssl->out_msg, buf, len );
Paul Bakker887bd502011-06-08 13:10:54 +000010103
Hanno Becker67bc7c32018-08-06 11:33:50 +010010104 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker887bd502011-06-08 13:10:54 +000010105 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010106 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker887bd502011-06-08 13:10:54 +000010107 return( ret );
10108 }
Paul Bakker5121ce52009-01-03 21:22:43 +000010109 }
10110
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010111 return( (int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +000010112}
10113
10114/*
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010115 * Write application data, doing 1/n-1 splitting if necessary.
10116 *
10117 * With non-blocking I/O, ssl_write_real() may return WANT_WRITE,
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +010010118 * then the caller will call us again with the same arguments, so
Hanno Becker2b187c42017-09-18 14:58:11 +010010119 * remember whether we already did the split or not.
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010120 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010121#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010122static int ssl_write_split( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010123 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010124{
10125 int ret;
10126
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +010010127 if( ssl->conf->cbc_record_splitting ==
10128 MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED ||
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +010010129 len <= 1 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010130 ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_1 ||
10131 mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc )
10132 != MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010133 {
10134 return( ssl_write_real( ssl, buf, len ) );
10135 }
10136
10137 if( ssl->split_done == 0 )
10138 {
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010010139 if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010140 return( ret );
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010010141 ssl->split_done = 1;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010142 }
10143
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010010144 if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 )
10145 return( ret );
10146 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010147
10148 return( ret + 1 );
10149}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010150#endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010151
10152/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010153 * Write application data (public-facing wrapper)
10154 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010155int mbedtls_ssl_write( mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010156{
10157 int ret;
10158
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010159 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010160
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010161 if( ssl == NULL || ssl->conf == NULL )
10162 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10163
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010164#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010165 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
10166 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010167 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010168 return( ret );
10169 }
10170#endif
10171
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010172 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010173 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010174 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010175 {
Manuel Pégourié-Gonnard151dc772015-05-14 13:55:51 +020010176 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010177 return( ret );
10178 }
10179 }
10180
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010181#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010182 ret = ssl_write_split( ssl, buf, len );
10183#else
10184 ret = ssl_write_real( ssl, buf, len );
10185#endif
10186
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010187 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010188
10189 return( ret );
10190}
10191
10192/*
Paul Bakker5121ce52009-01-03 21:22:43 +000010193 * Notify the peer that the connection is being closed
10194 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010195int mbedtls_ssl_close_notify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000010196{
10197 int ret;
10198
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010199 if( ssl == NULL || ssl->conf == NULL )
10200 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10201
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010202 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010203
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +020010204 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010205 return( mbedtls_ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010206
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010207 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +000010208 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010209 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
10210 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
10211 MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010212 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010213 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010214 return( ret );
10215 }
10216 }
10217
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010218 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010219
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +020010220 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +000010221}
10222
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010223void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform )
Paul Bakker48916f92012-09-16 19:57:18 +000010224{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010225 if( transform == NULL )
10226 return;
10227
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010228#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +000010229 deflateEnd( &transform->ctx_deflate );
10230 inflateEnd( &transform->ctx_inflate );
10231#endif
10232
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010233 mbedtls_cipher_free( &transform->cipher_ctx_enc );
10234 mbedtls_cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +020010235
Hanno Beckerd56ed242018-01-03 15:32:51 +000010236#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010237 mbedtls_md_free( &transform->md_ctx_enc );
10238 mbedtls_md_free( &transform->md_ctx_dec );
Hanno Beckerd56ed242018-01-03 15:32:51 +000010239#endif
Paul Bakker61d113b2013-07-04 11:51:43 +020010240
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010241 mbedtls_platform_zeroize( transform, sizeof( mbedtls_ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010242}
10243
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010244#if defined(MBEDTLS_X509_CRT_PARSE_C)
10245static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010246{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010247 mbedtls_ssl_key_cert *cur = key_cert, *next;
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010248
10249 while( cur != NULL )
10250 {
10251 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010252 mbedtls_free( cur );
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010253 cur = next;
10254 }
10255}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010256#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010257
Hanno Becker0271f962018-08-16 13:23:47 +010010258#if defined(MBEDTLS_SSL_PROTO_DTLS)
10259
10260static void ssl_buffering_free( mbedtls_ssl_context *ssl )
10261{
10262 unsigned offset;
10263 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
10264
10265 if( hs == NULL )
10266 return;
10267
Hanno Becker283f5ef2018-08-24 09:34:47 +010010268 ssl_free_buffered_record( ssl );
10269
Hanno Becker0271f962018-08-16 13:23:47 +010010270 for( offset = 0; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
Hanno Beckere605b192018-08-21 15:59:07 +010010271 ssl_buffering_free_slot( ssl, offset );
10272}
10273
10274static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
10275 uint8_t slot )
10276{
10277 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
10278 mbedtls_ssl_hs_buffer * const hs_buf = &hs->buffering.hs[slot];
Hanno Beckerb309b922018-08-23 13:18:05 +010010279
10280 if( slot >= MBEDTLS_SSL_MAX_BUFFERED_HS )
10281 return;
10282
Hanno Beckere605b192018-08-21 15:59:07 +010010283 if( hs_buf->is_valid == 1 )
Hanno Becker0271f962018-08-16 13:23:47 +010010284 {
Hanno Beckere605b192018-08-21 15:59:07 +010010285 hs->buffering.total_bytes_buffered -= hs_buf->data_len;
Hanno Becker805f2e12018-10-12 16:31:41 +010010286 mbedtls_platform_zeroize( hs_buf->data, hs_buf->data_len );
Hanno Beckere605b192018-08-21 15:59:07 +010010287 mbedtls_free( hs_buf->data );
10288 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Hanno Becker0271f962018-08-16 13:23:47 +010010289 }
10290}
10291
10292#endif /* MBEDTLS_SSL_PROTO_DTLS */
10293
Gilles Peskine9b562d52018-04-25 20:32:43 +020010294void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +000010295{
Gilles Peskine9b562d52018-04-25 20:32:43 +020010296 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
10297
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010298 if( handshake == NULL )
10299 return;
10300
Gilles Peskinedf13d5c2018-04-25 20:39:48 +020010301#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
10302 if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
10303 {
Gilles Peskine8f97af72018-04-26 11:46:10 +020010304 ssl->conf->f_async_cancel( ssl );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +020010305 handshake->async_in_progress = 0;
10306 }
10307#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
10308
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010309#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
10310 defined(MBEDTLS_SSL_PROTO_TLS1_1)
10311 mbedtls_md5_free( &handshake->fin_md5 );
10312 mbedtls_sha1_free( &handshake->fin_sha1 );
10313#endif
10314#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
10315#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -050010316#if defined(MBEDTLS_USE_PSA_CRYPTO)
10317 psa_hash_abort( &handshake->fin_sha256_psa );
10318#else
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010319 mbedtls_sha256_free( &handshake->fin_sha256 );
10320#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -050010321#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010322#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -050010323#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -050010324 psa_hash_abort( &handshake->fin_sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -050010325#else
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010326 mbedtls_sha512_free( &handshake->fin_sha512 );
10327#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -050010328#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010329#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
10330
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010331#if defined(MBEDTLS_DHM_C)
10332 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +000010333#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010334#if defined(MBEDTLS_ECDH_C)
10335 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +020010336#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +020010337#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +020010338 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +020010339#if defined(MBEDTLS_SSL_CLI_C)
10340 mbedtls_free( handshake->ecjpake_cache );
10341 handshake->ecjpake_cache = NULL;
10342 handshake->ecjpake_cache_len = 0;
10343#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +020010344#endif
Paul Bakker61d113b2013-07-04 11:51:43 +020010345
Janos Follath4ae5c292016-02-10 11:27:43 +000010346#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
10347 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +020010348 /* explicit void pointer cast for buggy MS compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010349 mbedtls_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +020010350#endif
10351
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +010010352#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
10353 if( handshake->psk != NULL )
10354 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010355 mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +010010356 mbedtls_free( handshake->psk );
10357 }
10358#endif
10359
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010360#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
10361 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020010362 /*
10363 * Free only the linked list wrapper, not the keys themselves
10364 * since the belong to the SNI callback
10365 */
10366 if( handshake->sni_key_cert != NULL )
10367 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010368 mbedtls_ssl_key_cert *cur = handshake->sni_key_cert, *next;
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020010369
10370 while( cur != NULL )
10371 {
10372 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010373 mbedtls_free( cur );
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020010374 cur = next;
10375 }
10376 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010377#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010378
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +020010379#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +020010380 mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx );
Hanno Becker3dad3112019-02-05 17:19:52 +000010381 if( handshake->ecrs_peer_cert != NULL )
10382 {
10383 mbedtls_x509_crt_free( handshake->ecrs_peer_cert );
10384 mbedtls_free( handshake->ecrs_peer_cert );
10385 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +020010386#endif
10387
Hanno Becker75173122019-02-06 16:18:31 +000010388#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
10389 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
10390 mbedtls_pk_free( &handshake->peer_pubkey );
10391#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
10392
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010393#if defined(MBEDTLS_SSL_PROTO_DTLS)
10394 mbedtls_free( handshake->verify_cookie );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +020010395 ssl_flight_free( handshake->flight );
Hanno Becker0271f962018-08-16 13:23:47 +010010396 ssl_buffering_free( ssl );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +020010397#endif
10398
Hanno Becker4a63ed42019-01-08 11:39:35 +000010399#if defined(MBEDTLS_ECDH_C) && \
10400 defined(MBEDTLS_USE_PSA_CRYPTO)
10401 psa_destroy_key( handshake->ecdh_psa_privkey );
10402#endif /* MBEDTLS_ECDH_C && MBEDTLS_USE_PSA_CRYPTO */
10403
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010404 mbedtls_platform_zeroize( handshake,
10405 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010406}
10407
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010408void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +000010409{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010410 if( session == NULL )
10411 return;
10412
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010413#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker1294a0b2019-02-05 12:38:15 +000010414 ssl_clear_peer_cert( session );
Paul Bakkered27a042013-04-18 22:46:23 +020010415#endif
Paul Bakker0a597072012-09-25 21:55:46 +000010416
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +020010417#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010418 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +020010419#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +020010420
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010421 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010422}
10423
Paul Bakker5121ce52009-01-03 21:22:43 +000010424/*
10425 * Free an SSL context
10426 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010427void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000010428{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010429 if( ssl == NULL )
10430 return;
10431
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010432 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010433
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +010010434 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010435 {
Angus Grattond8213d02016-05-25 20:56:48 +100010436 mbedtls_platform_zeroize( ssl->out_buf, MBEDTLS_SSL_OUT_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010437 mbedtls_free( ssl->out_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +000010438 }
10439
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +010010440 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010441 {
Angus Grattond8213d02016-05-25 20:56:48 +100010442 mbedtls_platform_zeroize( ssl->in_buf, MBEDTLS_SSL_IN_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010443 mbedtls_free( ssl->in_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +000010444 }
10445
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010446#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker16770332013-10-11 09:59:44 +020010447 if( ssl->compress_buf != NULL )
10448 {
Angus Grattond8213d02016-05-25 20:56:48 +100010449 mbedtls_platform_zeroize( ssl->compress_buf, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010450 mbedtls_free( ssl->compress_buf );
Paul Bakker16770332013-10-11 09:59:44 +020010451 }
10452#endif
10453
Paul Bakker48916f92012-09-16 19:57:18 +000010454 if( ssl->transform )
10455 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010456 mbedtls_ssl_transform_free( ssl->transform );
10457 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +000010458 }
10459
10460 if( ssl->handshake )
10461 {
Gilles Peskine9b562d52018-04-25 20:32:43 +020010462 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010463 mbedtls_ssl_transform_free( ssl->transform_negotiate );
10464 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +000010465
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010466 mbedtls_free( ssl->handshake );
10467 mbedtls_free( ssl->transform_negotiate );
10468 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +000010469 }
10470
Paul Bakkerc0463502013-02-14 11:19:38 +010010471 if( ssl->session )
10472 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010473 mbedtls_ssl_session_free( ssl->session );
10474 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +010010475 }
10476
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +020010477#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker66d5d072014-06-17 16:39:18 +020010478 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010479 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010480 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010481 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +000010482 }
Paul Bakker0be444a2013-08-27 21:55:01 +020010483#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000010484
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010485#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
10486 if( mbedtls_ssl_hw_record_finish != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +000010487 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010488 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_finish()" ) );
10489 mbedtls_ssl_hw_record_finish( ssl );
Paul Bakker05ef8352012-05-08 09:17:57 +000010490 }
10491#endif
10492
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +020010493#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010494 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +020010495#endif
10496
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010497 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +000010498
Paul Bakker86f04f42013-02-14 11:20:09 +010010499 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010500 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010501}
10502
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010503/*
10504 * Initialze mbedtls_ssl_config
10505 */
10506void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
10507{
10508 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
10509}
10510
Simon Butcherc97b6972015-12-27 23:48:17 +000010511#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010512static int ssl_preset_default_hashes[] = {
10513#if defined(MBEDTLS_SHA512_C)
10514 MBEDTLS_MD_SHA512,
10515 MBEDTLS_MD_SHA384,
10516#endif
10517#if defined(MBEDTLS_SHA256_C)
10518 MBEDTLS_MD_SHA256,
10519 MBEDTLS_MD_SHA224,
10520#endif
Gilles Peskine5d2511c2017-05-12 13:16:40 +020010521#if defined(MBEDTLS_SHA1_C) && defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_KEY_EXCHANGE)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010522 MBEDTLS_MD_SHA1,
10523#endif
10524 MBEDTLS_MD_NONE
10525};
Simon Butcherc97b6972015-12-27 23:48:17 +000010526#endif
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010527
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010528static int ssl_preset_suiteb_ciphersuites[] = {
10529 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
10530 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
10531 0
10532};
10533
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010534#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010535static int ssl_preset_suiteb_hashes[] = {
10536 MBEDTLS_MD_SHA256,
10537 MBEDTLS_MD_SHA384,
10538 MBEDTLS_MD_NONE
10539};
10540#endif
10541
10542#if defined(MBEDTLS_ECP_C)
10543static mbedtls_ecp_group_id ssl_preset_suiteb_curves[] = {
Jaeden Amerod4311042019-06-03 08:27:16 +010010544#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010545 MBEDTLS_ECP_DP_SECP256R1,
Jaeden Amerod4311042019-06-03 08:27:16 +010010546#endif
10547#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010548 MBEDTLS_ECP_DP_SECP384R1,
Jaeden Amerod4311042019-06-03 08:27:16 +010010549#endif
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010550 MBEDTLS_ECP_DP_NONE
10551};
10552#endif
10553
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010554/*
Tillmann Karras588ad502015-09-25 04:27:22 +020010555 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010556 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020010557int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010558 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010559{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +020010560#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010561 int ret;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +020010562#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010563
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +020010564 /* Use the functions here so that they are covered in tests,
10565 * but otherwise access member directly for efficiency */
10566 mbedtls_ssl_conf_endpoint( conf, endpoint );
10567 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010568
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010569 /*
10570 * Things that are common to all presets
10571 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020010572#if defined(MBEDTLS_SSL_CLI_C)
10573 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
10574 {
10575 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
10576#if defined(MBEDTLS_SSL_SESSION_TICKETS)
10577 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
10578#endif
10579 }
10580#endif
10581
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +020010582#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010583 conf->arc4_disabled = MBEDTLS_SSL_ARC4_DISABLED;
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +020010584#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010585
10586#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
10587 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
10588#endif
10589
10590#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
10591 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
10592#endif
10593
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +010010594#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
10595 conf->cbc_record_splitting = MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED;
10596#endif
10597
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +020010598#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010599 conf->f_cookie_write = ssl_cookie_write_dummy;
10600 conf->f_cookie_check = ssl_cookie_check_dummy;
10601#endif
10602
10603#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
10604 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
10605#endif
10606
Janos Follath088ce432017-04-10 12:42:31 +010010607#if defined(MBEDTLS_SSL_SRV_C)
10608 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
10609#endif
10610
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010611#if defined(MBEDTLS_SSL_PROTO_DTLS)
10612 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
10613 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
10614#endif
10615
10616#if defined(MBEDTLS_SSL_RENEGOTIATION)
10617 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Andres AG2196c7f2016-12-15 17:01:16 +000010618 memset( conf->renego_period, 0x00, 2 );
10619 memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010620#endif
10621
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010622#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
10623 if( endpoint == MBEDTLS_SSL_IS_SERVER )
10624 {
Hanno Becker00d0a682017-10-04 13:14:29 +010010625 const unsigned char dhm_p[] =
10626 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
10627 const unsigned char dhm_g[] =
10628 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
10629
Hanno Beckera90658f2017-10-04 15:29:08 +010010630 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
10631 dhm_p, sizeof( dhm_p ),
10632 dhm_g, sizeof( dhm_g ) ) ) != 0 )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010633 {
10634 return( ret );
10635 }
10636 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +020010637#endif
10638
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010639 /*
10640 * Preset-specific defaults
10641 */
10642 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010643 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010644 /*
10645 * NSA Suite B
10646 */
10647 case MBEDTLS_SSL_PRESET_SUITEB:
10648 conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
10649 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */
10650 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
10651 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
10652
10653 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
10654 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
10655 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
10656 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
10657 ssl_preset_suiteb_ciphersuites;
10658
10659#if defined(MBEDTLS_X509_CRT_PARSE_C)
10660 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010661#endif
10662
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010663#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010664 conf->sig_hashes = ssl_preset_suiteb_hashes;
10665#endif
10666
10667#if defined(MBEDTLS_ECP_C)
10668 conf->curve_list = ssl_preset_suiteb_curves;
10669#endif
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +020010670 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010671
10672 /*
10673 * Default
10674 */
10675 default:
Ron Eldor5e9f14d2017-05-28 10:46:38 +030010676 conf->min_major_ver = ( MBEDTLS_SSL_MIN_MAJOR_VERSION >
10677 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION ) ?
10678 MBEDTLS_SSL_MIN_MAJOR_VERSION :
10679 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION;
10680 conf->min_minor_ver = ( MBEDTLS_SSL_MIN_MINOR_VERSION >
10681 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION ) ?
10682 MBEDTLS_SSL_MIN_MINOR_VERSION :
10683 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010684 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
10685 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
10686
10687#if defined(MBEDTLS_SSL_PROTO_DTLS)
10688 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
10689 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_2;
10690#endif
10691
10692 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
10693 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
10694 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
10695 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
10696 mbedtls_ssl_list_ciphersuites();
10697
10698#if defined(MBEDTLS_X509_CRT_PARSE_C)
10699 conf->cert_profile = &mbedtls_x509_crt_profile_default;
10700#endif
10701
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010702#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010703 conf->sig_hashes = ssl_preset_default_hashes;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010704#endif
10705
10706#if defined(MBEDTLS_ECP_C)
10707 conf->curve_list = mbedtls_ecp_grp_id_list();
10708#endif
10709
10710#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
10711 conf->dhm_min_bitlen = 1024;
10712#endif
10713 }
10714
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010715 return( 0 );
10716}
10717
10718/*
10719 * Free mbedtls_ssl_config
10720 */
10721void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
10722{
10723#if defined(MBEDTLS_DHM_C)
10724 mbedtls_mpi_free( &conf->dhm_P );
10725 mbedtls_mpi_free( &conf->dhm_G );
10726#endif
10727
10728#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
10729 if( conf->psk != NULL )
10730 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010731 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010732 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +000010733 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010734 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +090010735 }
10736
10737 if( conf->psk_identity != NULL )
10738 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010739 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +090010740 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +000010741 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010742 conf->psk_identity_len = 0;
10743 }
10744#endif
10745
10746#if defined(MBEDTLS_X509_CRT_PARSE_C)
10747 ssl_key_cert_free( conf->key_cert );
10748#endif
10749
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010750 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010751}
10752
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020010753#if defined(MBEDTLS_PK_C) && \
10754 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010755/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010756 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010757 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010758unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010759{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010760#if defined(MBEDTLS_RSA_C)
10761 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
10762 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010763#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010764#if defined(MBEDTLS_ECDSA_C)
10765 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
10766 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010767#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010768 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010769}
10770
Hanno Becker7e5437a2017-04-28 17:15:26 +010010771unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
10772{
10773 switch( type ) {
10774 case MBEDTLS_PK_RSA:
10775 return( MBEDTLS_SSL_SIG_RSA );
10776 case MBEDTLS_PK_ECDSA:
10777 case MBEDTLS_PK_ECKEY:
10778 return( MBEDTLS_SSL_SIG_ECDSA );
10779 default:
10780 return( MBEDTLS_SSL_SIG_ANON );
10781 }
10782}
10783
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010784mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010785{
10786 switch( sig )
10787 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010788#if defined(MBEDTLS_RSA_C)
10789 case MBEDTLS_SSL_SIG_RSA:
10790 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010791#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010792#if defined(MBEDTLS_ECDSA_C)
10793 case MBEDTLS_SSL_SIG_ECDSA:
10794 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010795#endif
10796 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010797 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010798 }
10799}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020010800#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010801
Hanno Becker7e5437a2017-04-28 17:15:26 +010010802#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
10803 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
10804
10805/* Find an entry in a signature-hash set matching a given hash algorithm. */
10806mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set,
10807 mbedtls_pk_type_t sig_alg )
10808{
10809 switch( sig_alg )
10810 {
10811 case MBEDTLS_PK_RSA:
10812 return( set->rsa );
10813 case MBEDTLS_PK_ECDSA:
10814 return( set->ecdsa );
10815 default:
10816 return( MBEDTLS_MD_NONE );
10817 }
10818}
10819
10820/* Add a signature-hash-pair to a signature-hash set */
10821void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set,
10822 mbedtls_pk_type_t sig_alg,
10823 mbedtls_md_type_t md_alg )
10824{
10825 switch( sig_alg )
10826 {
10827 case MBEDTLS_PK_RSA:
10828 if( set->rsa == MBEDTLS_MD_NONE )
10829 set->rsa = md_alg;
10830 break;
10831
10832 case MBEDTLS_PK_ECDSA:
10833 if( set->ecdsa == MBEDTLS_MD_NONE )
10834 set->ecdsa = md_alg;
10835 break;
10836
10837 default:
10838 break;
10839 }
10840}
10841
10842/* Allow exactly one hash algorithm for each signature. */
10843void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set,
10844 mbedtls_md_type_t md_alg )
10845{
10846 set->rsa = md_alg;
10847 set->ecdsa = md_alg;
10848}
10849
10850#endif /* MBEDTLS_SSL_PROTO_TLS1_2) &&
10851 MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
10852
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020010853/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010854 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020010855 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010856mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010857{
10858 switch( hash )
10859 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010860#if defined(MBEDTLS_MD5_C)
10861 case MBEDTLS_SSL_HASH_MD5:
10862 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010863#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010864#if defined(MBEDTLS_SHA1_C)
10865 case MBEDTLS_SSL_HASH_SHA1:
10866 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010867#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010868#if defined(MBEDTLS_SHA256_C)
10869 case MBEDTLS_SSL_HASH_SHA224:
10870 return( MBEDTLS_MD_SHA224 );
10871 case MBEDTLS_SSL_HASH_SHA256:
10872 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010873#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010874#if defined(MBEDTLS_SHA512_C)
10875 case MBEDTLS_SSL_HASH_SHA384:
10876 return( MBEDTLS_MD_SHA384 );
10877 case MBEDTLS_SSL_HASH_SHA512:
10878 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010879#endif
10880 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010881 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010882 }
10883}
10884
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010885/*
10886 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
10887 */
10888unsigned char mbedtls_ssl_hash_from_md_alg( int md )
10889{
10890 switch( md )
10891 {
10892#if defined(MBEDTLS_MD5_C)
10893 case MBEDTLS_MD_MD5:
10894 return( MBEDTLS_SSL_HASH_MD5 );
10895#endif
10896#if defined(MBEDTLS_SHA1_C)
10897 case MBEDTLS_MD_SHA1:
10898 return( MBEDTLS_SSL_HASH_SHA1 );
10899#endif
10900#if defined(MBEDTLS_SHA256_C)
10901 case MBEDTLS_MD_SHA224:
10902 return( MBEDTLS_SSL_HASH_SHA224 );
10903 case MBEDTLS_MD_SHA256:
10904 return( MBEDTLS_SSL_HASH_SHA256 );
10905#endif
10906#if defined(MBEDTLS_SHA512_C)
10907 case MBEDTLS_MD_SHA384:
10908 return( MBEDTLS_SSL_HASH_SHA384 );
10909 case MBEDTLS_MD_SHA512:
10910 return( MBEDTLS_SSL_HASH_SHA512 );
10911#endif
10912 default:
10913 return( MBEDTLS_SSL_HASH_NONE );
10914 }
10915}
10916
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020010917#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010918/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010919 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010920 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010921 */
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010922int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010923{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010924 const mbedtls_ecp_group_id *gid;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010925
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010926 if( ssl->conf->curve_list == NULL )
10927 return( -1 );
10928
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010929 for( gid = ssl->conf->curve_list; *gid != MBEDTLS_ECP_DP_NONE; gid++ )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010930 if( *gid == grp_id )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010931 return( 0 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010932
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010933 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010934}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020010935#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010936
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010937#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010938/*
10939 * Check if a hash proposed by the peer is in our list.
10940 * Return 0 if we're willing to use it, -1 otherwise.
10941 */
10942int mbedtls_ssl_check_sig_hash( const mbedtls_ssl_context *ssl,
10943 mbedtls_md_type_t md )
10944{
10945 const int *cur;
10946
10947 if( ssl->conf->sig_hashes == NULL )
10948 return( -1 );
10949
10950 for( cur = ssl->conf->sig_hashes; *cur != MBEDTLS_MD_NONE; cur++ )
10951 if( *cur == (int) md )
10952 return( 0 );
10953
10954 return( -1 );
10955}
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010956#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010957
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010958#if defined(MBEDTLS_X509_CRT_PARSE_C)
10959int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
10960 const mbedtls_ssl_ciphersuite_t *ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010961 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +020010962 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010963{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010964 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010965#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010966 int usage = 0;
10967#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010968#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010969 const char *ext_oid;
10970 size_t ext_len;
10971#endif
10972
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010973#if !defined(MBEDTLS_X509_CHECK_KEY_USAGE) && \
10974 !defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010975 ((void) cert);
10976 ((void) cert_endpoint);
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010977 ((void) flags);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010978#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010979
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010980#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
10981 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010982 {
10983 /* Server part of the key exchange */
10984 switch( ciphersuite->key_exchange )
10985 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010986 case MBEDTLS_KEY_EXCHANGE_RSA:
10987 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010988 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010989 break;
10990
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010991 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
10992 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
10993 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
10994 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010995 break;
10996
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010997 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
10998 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010999 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011000 break;
11001
11002 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011003 case MBEDTLS_KEY_EXCHANGE_NONE:
11004 case MBEDTLS_KEY_EXCHANGE_PSK:
11005 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
11006 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +020011007 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011008 usage = 0;
11009 }
11010 }
11011 else
11012 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011013 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
11014 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011015 }
11016
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011017 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011018 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010011019 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011020 ret = -1;
11021 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011022#else
11023 ((void) ciphersuite);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011024#endif /* MBEDTLS_X509_CHECK_KEY_USAGE */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011025
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011026#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
11027 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011028 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011029 ext_oid = MBEDTLS_OID_SERVER_AUTH;
11030 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011031 }
11032 else
11033 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011034 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
11035 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011036 }
11037
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011038 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011039 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010011040 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011041 ret = -1;
11042 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011043#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011044
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011045 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011046}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011047#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +020011048
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011049/*
11050 * Convert version numbers to/from wire format
11051 * and, for DTLS, to/from TLS equivalent.
11052 *
11053 * For TLS this is the identity.
Brian J Murray1903fb32016-11-06 04:45:15 -080011054 * For DTLS, use 1's complement (v -> 255 - v, and then map as follows:
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011055 * 1.0 <-> 3.2 (DTLS 1.0 is based on TLS 1.1)
11056 * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2)
11057 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011058void mbedtls_ssl_write_version( int major, int minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011059 unsigned char ver[2] )
11060{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011061#if defined(MBEDTLS_SSL_PROTO_DTLS)
11062 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011063 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011064 if( minor == MBEDTLS_SSL_MINOR_VERSION_2 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011065 --minor; /* DTLS 1.0 stored as TLS 1.1 internally */
11066
11067 ver[0] = (unsigned char)( 255 - ( major - 2 ) );
11068 ver[1] = (unsigned char)( 255 - ( minor - 1 ) );
11069 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010011070 else
11071#else
11072 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011073#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010011074 {
11075 ver[0] = (unsigned char) major;
11076 ver[1] = (unsigned char) minor;
11077 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011078}
11079
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011080void mbedtls_ssl_read_version( int *major, int *minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011081 const unsigned char ver[2] )
11082{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011083#if defined(MBEDTLS_SSL_PROTO_DTLS)
11084 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011085 {
11086 *major = 255 - ver[0] + 2;
11087 *minor = 255 - ver[1] + 1;
11088
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011089 if( *minor == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011090 ++*minor; /* DTLS 1.0 stored as TLS 1.1 internally */
11091 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010011092 else
11093#else
11094 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011095#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010011096 {
11097 *major = ver[0];
11098 *minor = ver[1];
11099 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011100}
11101
Simon Butcher99000142016-10-13 17:21:01 +010011102int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md )
11103{
11104#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
11105 if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 )
11106 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
11107
11108 switch( md )
11109 {
11110#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
11111#if defined(MBEDTLS_MD5_C)
11112 case MBEDTLS_SSL_HASH_MD5:
Janos Follath182013f2016-10-25 10:50:22 +010011113 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
Simon Butcher99000142016-10-13 17:21:01 +010011114#endif
11115#if defined(MBEDTLS_SHA1_C)
11116 case MBEDTLS_SSL_HASH_SHA1:
11117 ssl->handshake->calc_verify = ssl_calc_verify_tls;
11118 break;
11119#endif
11120#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
11121#if defined(MBEDTLS_SHA512_C)
11122 case MBEDTLS_SSL_HASH_SHA384:
11123 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
11124 break;
11125#endif
11126#if defined(MBEDTLS_SHA256_C)
11127 case MBEDTLS_SSL_HASH_SHA256:
11128 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
11129 break;
11130#endif
11131 default:
11132 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
11133 }
11134
11135 return 0;
11136#else /* !MBEDTLS_SSL_PROTO_TLS1_2 */
11137 (void) ssl;
11138 (void) md;
11139
11140 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
11141#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
11142}
11143
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011144#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
11145 defined(MBEDTLS_SSL_PROTO_TLS1_1)
11146int mbedtls_ssl_get_key_exchange_md_ssl_tls( mbedtls_ssl_context *ssl,
11147 unsigned char *output,
11148 unsigned char *data, size_t data_len )
11149{
11150 int ret = 0;
11151 mbedtls_md5_context mbedtls_md5;
11152 mbedtls_sha1_context mbedtls_sha1;
11153
11154 mbedtls_md5_init( &mbedtls_md5 );
11155 mbedtls_sha1_init( &mbedtls_sha1 );
11156
11157 /*
11158 * digitally-signed struct {
11159 * opaque md5_hash[16];
11160 * opaque sha_hash[20];
11161 * };
11162 *
11163 * md5_hash
11164 * MD5(ClientHello.random + ServerHello.random
11165 * + ServerParams);
11166 * sha_hash
11167 * SHA(ClientHello.random + ServerHello.random
11168 * + ServerParams);
11169 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011170 if( ( ret = mbedtls_md5_starts_ret( &mbedtls_md5 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011171 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011172 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011173 goto exit;
11174 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011175 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011176 ssl->handshake->randbytes, 64 ) ) != 0 )
11177 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011178 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011179 goto exit;
11180 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011181 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5, data, data_len ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011182 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011183 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011184 goto exit;
11185 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011186 if( ( ret = mbedtls_md5_finish_ret( &mbedtls_md5, output ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011187 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011188 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011189 goto exit;
11190 }
11191
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011192 if( ( ret = mbedtls_sha1_starts_ret( &mbedtls_sha1 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011193 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011194 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011195 goto exit;
11196 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011197 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011198 ssl->handshake->randbytes, 64 ) ) != 0 )
11199 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011200 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011201 goto exit;
11202 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011203 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1, data,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011204 data_len ) ) != 0 )
11205 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011206 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011207 goto exit;
11208 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011209 if( ( ret = mbedtls_sha1_finish_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011210 output + 16 ) ) != 0 )
11211 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011212 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011213 goto exit;
11214 }
11215
11216exit:
11217 mbedtls_md5_free( &mbedtls_md5 );
11218 mbedtls_sha1_free( &mbedtls_sha1 );
11219
11220 if( ret != 0 )
11221 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
11222 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
11223
11224 return( ret );
11225
11226}
11227#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
11228 MBEDTLS_SSL_PROTO_TLS1_1 */
11229
11230#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
11231 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011232
11233#if defined(MBEDTLS_USE_PSA_CRYPTO)
11234int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
11235 unsigned char *hash, size_t *hashlen,
11236 unsigned char *data, size_t data_len,
11237 mbedtls_md_type_t md_alg )
11238{
Andrzej Kurek814feff2019-01-14 04:35:19 -050011239 psa_status_t status;
Jaeden Amero34973232019-02-20 10:32:28 +000011240 psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT;
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011241 psa_algorithm_t hash_alg = mbedtls_psa_translate_md( md_alg );
11242
Hanno Becker4c8c7aa2019-04-10 09:25:41 +010011243 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform PSA-based computation of digest of ServerKeyExchange" ) );
Andrzej Kurek814feff2019-01-14 04:35:19 -050011244
11245 if( ( status = psa_hash_setup( &hash_operation,
11246 hash_alg ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011247 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050011248 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_setup", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011249 goto exit;
11250 }
11251
Andrzej Kurek814feff2019-01-14 04:35:19 -050011252 if( ( status = psa_hash_update( &hash_operation, ssl->handshake->randbytes,
11253 64 ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011254 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050011255 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011256 goto exit;
11257 }
11258
Andrzej Kurek814feff2019-01-14 04:35:19 -050011259 if( ( status = psa_hash_update( &hash_operation,
11260 data, data_len ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011261 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050011262 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011263 goto exit;
11264 }
11265
Andrzej Kurek814feff2019-01-14 04:35:19 -050011266 if( ( status = psa_hash_finish( &hash_operation, hash, MBEDTLS_MD_MAX_SIZE,
11267 hashlen ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011268 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050011269 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_finish", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011270 goto exit;
11271 }
11272
11273exit:
Andrzej Kurek814feff2019-01-14 04:35:19 -050011274 if( status != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011275 {
11276 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
11277 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Andrzej Kurek814feff2019-01-14 04:35:19 -050011278 switch( status )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011279 {
11280 case PSA_ERROR_NOT_SUPPORTED:
11281 return( MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE );
Andrzej Kurek814feff2019-01-14 04:35:19 -050011282 case PSA_ERROR_BAD_STATE: /* Intentional fallthrough */
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011283 case PSA_ERROR_BUFFER_TOO_SMALL:
11284 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
11285 case PSA_ERROR_INSUFFICIENT_MEMORY:
11286 return( MBEDTLS_ERR_MD_ALLOC_FAILED );
11287 default:
11288 return( MBEDTLS_ERR_MD_HW_ACCEL_FAILED );
11289 }
11290 }
11291 return( 0 );
11292}
11293
11294#else
11295
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011296int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
Gilles Peskineca1d7422018-04-24 11:53:22 +020011297 unsigned char *hash, size_t *hashlen,
11298 unsigned char *data, size_t data_len,
11299 mbedtls_md_type_t md_alg )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011300{
11301 int ret = 0;
11302 mbedtls_md_context_t ctx;
11303 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
Gilles Peskineca1d7422018-04-24 11:53:22 +020011304 *hashlen = mbedtls_md_get_size( md_info );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011305
Hanno Becker4c8c7aa2019-04-10 09:25:41 +010011306 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform mbedtls-based computation of digest of ServerKeyExchange" ) );
Andrzej Kurek814feff2019-01-14 04:35:19 -050011307
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011308 mbedtls_md_init( &ctx );
11309
11310 /*
11311 * digitally-signed struct {
11312 * opaque client_random[32];
11313 * opaque server_random[32];
11314 * ServerDHParams params;
11315 * };
11316 */
11317 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
11318 {
11319 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
11320 goto exit;
11321 }
11322 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
11323 {
11324 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
11325 goto exit;
11326 }
11327 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
11328 {
11329 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
11330 goto exit;
11331 }
11332 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
11333 {
11334 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
11335 goto exit;
11336 }
Gilles Peskineca1d7422018-04-24 11:53:22 +020011337 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011338 {
11339 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
11340 goto exit;
11341 }
11342
11343exit:
11344 mbedtls_md_free( &ctx );
11345
11346 if( ret != 0 )
11347 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
11348 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
11349
11350 return( ret );
11351}
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011352#endif /* MBEDTLS_USE_PSA_CRYPTO */
11353
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011354#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
11355 MBEDTLS_SSL_PROTO_TLS1_2 */
11356
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011357#endif /* MBEDTLS_SSL_TLS_C */