blob: fb3a4a0902cbbd15674c8de0e304a31c432fb3ea [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * SSLv3/TLSv1 shared functions
3 *
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02004 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Paul Bakkerb96f1542010-07-18 20:36:00 +000018 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000019 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker5121ce52009-01-03 21:22:43 +000020 */
21/*
22 * The SSL 3.0 specification was drafted by Netscape in 1996,
23 * and became an IETF standard in 1999.
24 *
25 * http://wp.netscape.com/eng/ssl3/
26 * http://www.ietf.org/rfc/rfc2246.txt
27 * http://www.ietf.org/rfc/rfc4346.txt
28 */
29
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020030#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000031#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020032#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020033#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020034#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000035
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020036#if defined(MBEDTLS_SSL_TLS_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000037
SimonBd5800b72016-04-26 07:43:27 +010038#if defined(MBEDTLS_PLATFORM_C)
39#include "mbedtls/platform.h"
40#else
41#include <stdlib.h>
42#define mbedtls_calloc calloc
43#define mbedtls_free free
SimonBd5800b72016-04-26 07:43:27 +010044#endif
45
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000046#include "mbedtls/debug.h"
47#include "mbedtls/ssl.h"
Manuel Pégourié-Gonnard5e94dde2015-05-26 11:57:05 +020048#include "mbedtls/ssl_internal.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050049#include "mbedtls/platform_util.h"
Paul Bakker0be444a2013-08-27 21:55:01 +020050
Rich Evans00ab4702015-02-06 13:43:58 +000051#include <string.h>
52
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050053#if defined(MBEDTLS_USE_PSA_CRYPTO)
54#include "mbedtls/psa_util.h"
55#include "psa/crypto.h"
56#endif
57
Janos Follath23bdca02016-10-07 14:47:14 +010058#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000059#include "mbedtls/oid.h"
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020060#endif
61
Andrzej Kurekc929a822019-01-14 03:51:11 -050062#if defined(MBEDTLS_USE_PSA_CRYPTO)
63#include "mbedtls/psa_util.h"
64#endif
65
Hanno Becker2a43f6f2018-08-10 11:12:52 +010066static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl );
Hanno Beckercd9dcda2018-08-28 17:18:56 +010067static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl );
Hanno Becker2a43f6f2018-08-10 11:12:52 +010068
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010069/* Length of the "epoch" field in the record header */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020070static inline size_t ssl_ep_len( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010071{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020072#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020073 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010074 return( 2 );
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010075#else
76 ((void) ssl);
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010077#endif
78 return( 0 );
79}
80
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020081/*
82 * Start a timer.
83 * Passing millisecs = 0 cancels a running timer.
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020084 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020085static void ssl_set_timer( mbedtls_ssl_context *ssl, uint32_t millisecs )
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020086{
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +020087 if( ssl->f_set_timer == NULL )
88 return;
89
90 MBEDTLS_SSL_DEBUG_MSG( 3, ( "set_timer to %d ms", (int) millisecs ) );
91 ssl->f_set_timer( ssl->p_timer, millisecs / 4, millisecs );
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020092}
93
94/*
95 * Return -1 is timer is expired, 0 if it isn't.
96 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020097static int ssl_check_timer( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020098{
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +020099 if( ssl->f_get_timer == NULL )
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +0200100 return( 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +0200101
102 if( ssl->f_get_timer( ssl->p_timer ) == 2 )
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200103 {
104 MBEDTLS_SSL_DEBUG_MSG( 3, ( "timer expired" ) );
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200105 return( -1 );
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200106 }
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200107
108 return( 0 );
109}
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200110
Hanno Becker5aa4e2c2018-08-06 09:26:08 +0100111static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
112 mbedtls_ssl_transform *transform );
Hanno Becker79594fd2019-05-08 09:38:41 +0100113static void ssl_update_in_pointers( mbedtls_ssl_context *ssl );
Hanno Becker67bc7c32018-08-06 11:33:50 +0100114
Hanno Beckercfe45792019-07-03 16:13:00 +0100115#if defined(MBEDTLS_SSL_RECORD_CHECKING)
116int mbedtls_ssl_check_record( mbedtls_ssl_context const *ssl,
117 unsigned char *buf,
118 size_t buflen )
119{
120 ((void) ssl);
121 ((void) buf);
122 ((void) buflen);
123 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
124}
125#endif /* MBEDTLS_SSL_RECORD_CHECKING */
126
Hanno Becker67bc7c32018-08-06 11:33:50 +0100127#define SSL_DONT_FORCE_FLUSH 0
128#define SSL_FORCE_FLUSH 1
129
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200130#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker2b1e3542018-08-06 11:19:13 +0100131
Hanno Beckera0e20d02019-05-15 14:03:01 +0100132#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100133/* Top-level Connection ID API */
134
Hanno Becker8367ccc2019-05-14 11:30:10 +0100135int mbedtls_ssl_conf_cid( mbedtls_ssl_config *conf,
136 size_t len,
137 int ignore_other_cid )
Hanno Beckerad4a1372019-05-03 13:06:44 +0100138{
139 if( len > MBEDTLS_SSL_CID_IN_LEN_MAX )
140 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
141
Hanno Becker611ac772019-05-14 11:45:26 +0100142 if( ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_FAIL &&
143 ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_IGNORE )
144 {
145 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
146 }
147
148 conf->ignore_unexpected_cid = ignore_other_cid;
Hanno Beckerad4a1372019-05-03 13:06:44 +0100149 conf->cid_len = len;
150 return( 0 );
151}
152
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100153int mbedtls_ssl_set_cid( mbedtls_ssl_context *ssl,
154 int enable,
155 unsigned char const *own_cid,
156 size_t own_cid_len )
157{
Hanno Becker76a79ab2019-05-03 14:38:32 +0100158 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
159 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
160
Hanno Beckerca092242019-04-25 16:01:49 +0100161 ssl->negotiate_cid = enable;
162 if( enable == MBEDTLS_SSL_CID_DISABLED )
163 {
164 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Disable use of CID extension." ) );
165 return( 0 );
166 }
167 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Enable use of CID extension." ) );
Hanno Beckerad4a1372019-05-03 13:06:44 +0100168 MBEDTLS_SSL_DEBUG_BUF( 3, "Own CID", own_cid, own_cid_len );
Hanno Beckerca092242019-04-25 16:01:49 +0100169
Hanno Beckerad4a1372019-05-03 13:06:44 +0100170 if( own_cid_len != ssl->conf->cid_len )
Hanno Beckerca092242019-04-25 16:01:49 +0100171 {
Hanno Beckerad4a1372019-05-03 13:06:44 +0100172 MBEDTLS_SSL_DEBUG_MSG( 3, ( "CID length %u does not match CID length %u in config",
173 (unsigned) own_cid_len,
174 (unsigned) ssl->conf->cid_len ) );
Hanno Beckerca092242019-04-25 16:01:49 +0100175 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
176 }
177
178 memcpy( ssl->own_cid, own_cid, own_cid_len );
Hanno Beckerb7ee0cf2019-04-30 14:07:31 +0100179 /* Truncation is not an issue here because
180 * MBEDTLS_SSL_CID_IN_LEN_MAX at most 255. */
181 ssl->own_cid_len = (uint8_t) own_cid_len;
Hanno Beckerca092242019-04-25 16:01:49 +0100182
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100183 return( 0 );
184}
185
186int mbedtls_ssl_get_peer_cid( mbedtls_ssl_context *ssl,
187 int *enabled,
188 unsigned char peer_cid[ MBEDTLS_SSL_CID_OUT_LEN_MAX ],
189 size_t *peer_cid_len )
190{
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100191 *enabled = MBEDTLS_SSL_CID_DISABLED;
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100192
Hanno Becker76a79ab2019-05-03 14:38:32 +0100193 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
194 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
195 {
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100196 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Becker76a79ab2019-05-03 14:38:32 +0100197 }
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100198
Hanno Beckerc5f24222019-05-03 12:54:52 +0100199 /* We report MBEDTLS_SSL_CID_DISABLED in case the CID extensions
200 * were used, but client and server requested the empty CID.
201 * This is indistinguishable from not using the CID extension
202 * in the first place. */
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100203 if( ssl->transform_in->in_cid_len == 0 &&
204 ssl->transform_in->out_cid_len == 0 )
205 {
206 return( 0 );
207 }
208
Hanno Becker615ef172019-05-22 16:50:35 +0100209 if( peer_cid_len != NULL )
210 {
211 *peer_cid_len = ssl->transform_in->out_cid_len;
212 if( peer_cid != NULL )
213 {
214 memcpy( peer_cid, ssl->transform_in->out_cid,
215 ssl->transform_in->out_cid_len );
216 }
217 }
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100218
219 *enabled = MBEDTLS_SSL_CID_ENABLED;
220
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100221 return( 0 );
222}
Hanno Beckera0e20d02019-05-15 14:03:01 +0100223#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100224
Hanno Beckerd5847772018-08-28 10:09:23 +0100225/* Forward declarations for functions related to message buffering. */
226static void ssl_buffering_free( mbedtls_ssl_context *ssl );
227static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
228 uint8_t slot );
229static void ssl_free_buffered_record( mbedtls_ssl_context *ssl );
230static int ssl_load_buffered_message( mbedtls_ssl_context *ssl );
231static int ssl_load_buffered_record( mbedtls_ssl_context *ssl );
232static int ssl_buffer_message( mbedtls_ssl_context *ssl );
Hanno Becker519f15d2019-07-11 12:43:20 +0100233static int ssl_buffer_future_record( mbedtls_ssl_context *ssl,
234 mbedtls_record const *rec );
Hanno Beckeref7afdf2018-08-28 17:16:31 +0100235static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl );
Hanno Beckerd5847772018-08-28 10:09:23 +0100236
Hanno Beckera67dee22018-08-22 10:05:20 +0100237static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl );
Hanno Becker11682cc2018-08-22 14:41:02 +0100238static size_t ssl_get_maximum_datagram_size( mbedtls_ssl_context const *ssl )
Hanno Becker2b1e3542018-08-06 11:19:13 +0100239{
Hanno Becker11682cc2018-08-22 14:41:02 +0100240 size_t mtu = ssl_get_current_mtu( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +0100241
242 if( mtu != 0 && mtu < MBEDTLS_SSL_OUT_BUFFER_LEN )
Hanno Becker11682cc2018-08-22 14:41:02 +0100243 return( mtu );
Hanno Becker2b1e3542018-08-06 11:19:13 +0100244
245 return( MBEDTLS_SSL_OUT_BUFFER_LEN );
246}
247
Hanno Becker67bc7c32018-08-06 11:33:50 +0100248static int ssl_get_remaining_space_in_datagram( mbedtls_ssl_context const *ssl )
249{
Hanno Becker11682cc2018-08-22 14:41:02 +0100250 size_t const bytes_written = ssl->out_left;
251 size_t const mtu = ssl_get_maximum_datagram_size( ssl );
Hanno Becker67bc7c32018-08-06 11:33:50 +0100252
253 /* Double-check that the write-index hasn't gone
254 * past what we can transmit in a single datagram. */
Hanno Becker11682cc2018-08-22 14:41:02 +0100255 if( bytes_written > mtu )
Hanno Becker67bc7c32018-08-06 11:33:50 +0100256 {
257 /* Should never happen... */
258 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
259 }
260
261 return( (int) ( mtu - bytes_written ) );
262}
263
264static int ssl_get_remaining_payload_in_datagram( mbedtls_ssl_context const *ssl )
265{
266 int ret;
267 size_t remaining, expansion;
Andrzej Kurek748face2018-10-11 07:20:19 -0400268 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
Hanno Becker67bc7c32018-08-06 11:33:50 +0100269
270#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
271 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
272
273 if( max_len > mfl )
274 max_len = mfl;
Hanno Beckerf4b010e2018-08-24 10:47:29 +0100275
276 /* By the standard (RFC 6066 Sect. 4), the MFL extension
277 * only limits the maximum record payload size, so in theory
278 * we would be allowed to pack multiple records of payload size
279 * MFL into a single datagram. However, this would mean that there's
280 * no way to explicitly communicate MTU restrictions to the peer.
281 *
282 * The following reduction of max_len makes sure that we never
283 * write datagrams larger than MFL + Record Expansion Overhead.
284 */
285 if( max_len <= ssl->out_left )
286 return( 0 );
287
288 max_len -= ssl->out_left;
Hanno Becker67bc7c32018-08-06 11:33:50 +0100289#endif
290
291 ret = ssl_get_remaining_space_in_datagram( ssl );
292 if( ret < 0 )
293 return( ret );
294 remaining = (size_t) ret;
295
296 ret = mbedtls_ssl_get_record_expansion( ssl );
297 if( ret < 0 )
298 return( ret );
299 expansion = (size_t) ret;
300
301 if( remaining <= expansion )
302 return( 0 );
303
304 remaining -= expansion;
305 if( remaining >= max_len )
306 remaining = max_len;
307
308 return( (int) remaining );
309}
310
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200311/*
312 * Double the retransmit timeout value, within the allowed range,
313 * returning -1 if the maximum value has already been reached.
314 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200315static int ssl_double_retransmit_timeout( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200316{
317 uint32_t new_timeout;
318
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200319 if( ssl->handshake->retransmit_timeout >= ssl->conf->hs_timeout_max )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200320 return( -1 );
321
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200322 /* Implement the final paragraph of RFC 6347 section 4.1.1.1
323 * in the following way: after the initial transmission and a first
324 * retransmission, back off to a temporary estimated MTU of 508 bytes.
325 * This value is guaranteed to be deliverable (if not guaranteed to be
326 * delivered) of any compliant IPv4 (and IPv6) network, and should work
327 * on most non-IP stacks too. */
328 if( ssl->handshake->retransmit_timeout != ssl->conf->hs_timeout_min )
Andrzej Kurek6290dae2018-10-05 08:06:01 -0400329 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200330 ssl->handshake->mtu = 508;
Andrzej Kurek6290dae2018-10-05 08:06:01 -0400331 MBEDTLS_SSL_DEBUG_MSG( 2, ( "mtu autoreduction to %d bytes", ssl->handshake->mtu ) );
332 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200333
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200334 new_timeout = 2 * ssl->handshake->retransmit_timeout;
335
336 /* Avoid arithmetic overflow and range overflow */
337 if( new_timeout < ssl->handshake->retransmit_timeout ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200338 new_timeout > ssl->conf->hs_timeout_max )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200339 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200340 new_timeout = ssl->conf->hs_timeout_max;
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200341 }
342
343 ssl->handshake->retransmit_timeout = new_timeout;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200344 MBEDTLS_SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs",
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200345 ssl->handshake->retransmit_timeout ) );
346
347 return( 0 );
348}
349
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200350static void ssl_reset_retransmit_timeout( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200351{
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200352 ssl->handshake->retransmit_timeout = ssl->conf->hs_timeout_min;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200353 MBEDTLS_SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs",
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200354 ssl->handshake->retransmit_timeout ) );
355}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200356#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200357
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200358#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200359/*
360 * Convert max_fragment_length codes to length.
361 * RFC 6066 says:
362 * enum{
363 * 2^9(1), 2^10(2), 2^11(3), 2^12(4), (255)
364 * } MaxFragmentLength;
365 * and we add 0 -> extension unused
366 */
Angus Grattond8213d02016-05-25 20:56:48 +1000367static unsigned int ssl_mfl_code_to_length( int mfl )
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200368{
Angus Grattond8213d02016-05-25 20:56:48 +1000369 switch( mfl )
370 {
371 case MBEDTLS_SSL_MAX_FRAG_LEN_NONE:
372 return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
373 case MBEDTLS_SSL_MAX_FRAG_LEN_512:
374 return 512;
375 case MBEDTLS_SSL_MAX_FRAG_LEN_1024:
376 return 1024;
377 case MBEDTLS_SSL_MAX_FRAG_LEN_2048:
378 return 2048;
379 case MBEDTLS_SSL_MAX_FRAG_LEN_4096:
380 return 4096;
381 default:
382 return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
383 }
384}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200385#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200386
Hanno Becker52055ae2019-02-06 14:30:46 +0000387int mbedtls_ssl_session_copy( mbedtls_ssl_session *dst,
388 const mbedtls_ssl_session *src )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200389{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200390 mbedtls_ssl_session_free( dst );
391 memcpy( dst, src, sizeof( mbedtls_ssl_session ) );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200392
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200393#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker6d1986e2019-02-07 12:27:42 +0000394
395#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200396 if( src->peer_cert != NULL )
397 {
Paul Bakker2292d1f2013-09-15 17:06:49 +0200398 int ret;
399
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200400 dst->peer_cert = mbedtls_calloc( 1, sizeof(mbedtls_x509_crt) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200401 if( dst->peer_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200402 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200403
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200404 mbedtls_x509_crt_init( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200405
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200406 if( ( ret = mbedtls_x509_crt_parse_der( dst->peer_cert, src->peer_cert->raw.p,
Manuel Pégourié-Gonnard4d2a8eb2014-06-13 20:33:27 +0200407 src->peer_cert->raw.len ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200408 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200409 mbedtls_free( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200410 dst->peer_cert = NULL;
411 return( ret );
412 }
413 }
Hanno Becker6d1986e2019-02-07 12:27:42 +0000414#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker9198ad12019-02-05 17:00:50 +0000415 if( src->peer_cert_digest != NULL )
416 {
Hanno Becker9198ad12019-02-05 17:00:50 +0000417 dst->peer_cert_digest =
Hanno Beckeraccc5992019-02-25 10:06:59 +0000418 mbedtls_calloc( 1, src->peer_cert_digest_len );
Hanno Becker9198ad12019-02-05 17:00:50 +0000419 if( dst->peer_cert_digest == NULL )
420 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
421
422 memcpy( dst->peer_cert_digest, src->peer_cert_digest,
423 src->peer_cert_digest_len );
424 dst->peer_cert_digest_type = src->peer_cert_digest_type;
Hanno Beckeraccc5992019-02-25 10:06:59 +0000425 dst->peer_cert_digest_len = src->peer_cert_digest_len;
Hanno Becker9198ad12019-02-05 17:00:50 +0000426 }
427#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
428
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200429#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200430
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200431#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200432 if( src->ticket != NULL )
433 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200434 dst->ticket = mbedtls_calloc( 1, src->ticket_len );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200435 if( dst->ticket == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200436 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200437
438 memcpy( dst->ticket, src->ticket, src->ticket_len );
439 }
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200440#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200441
442 return( 0 );
443}
444
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200445#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
446int (*mbedtls_ssl_hw_record_init)( mbedtls_ssl_context *ssl,
Paul Bakker9af723c2014-05-01 13:03:14 +0200447 const unsigned char *key_enc, const unsigned char *key_dec,
448 size_t keylen,
449 const unsigned char *iv_enc, const unsigned char *iv_dec,
450 size_t ivlen,
451 const unsigned char *mac_enc, const unsigned char *mac_dec,
Paul Bakker66d5d072014-06-17 16:39:18 +0200452 size_t maclen ) = NULL;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200453int (*mbedtls_ssl_hw_record_activate)( mbedtls_ssl_context *ssl, int direction) = NULL;
454int (*mbedtls_ssl_hw_record_reset)( mbedtls_ssl_context *ssl ) = NULL;
455int (*mbedtls_ssl_hw_record_write)( mbedtls_ssl_context *ssl ) = NULL;
456int (*mbedtls_ssl_hw_record_read)( mbedtls_ssl_context *ssl ) = NULL;
457int (*mbedtls_ssl_hw_record_finish)( mbedtls_ssl_context *ssl ) = NULL;
458#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +0000459
Paul Bakker5121ce52009-01-03 21:22:43 +0000460/*
461 * Key material generation
462 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200463#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200464static int ssl3_prf( const unsigned char *secret, size_t slen,
465 const char *label,
466 const unsigned char *random, size_t rlen,
Paul Bakker5f70b252012-09-13 14:23:06 +0000467 unsigned char *dstbuf, size_t dlen )
468{
Andres Amaya Garcia33952502017-07-20 16:29:16 +0100469 int ret = 0;
Paul Bakker5f70b252012-09-13 14:23:06 +0000470 size_t i;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200471 mbedtls_md5_context md5;
472 mbedtls_sha1_context sha1;
Paul Bakker5f70b252012-09-13 14:23:06 +0000473 unsigned char padding[16];
474 unsigned char sha1sum[20];
475 ((void)label);
476
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200477 mbedtls_md5_init( &md5 );
478 mbedtls_sha1_init( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +0200479
Paul Bakker5f70b252012-09-13 14:23:06 +0000480 /*
481 * SSLv3:
482 * block =
483 * MD5( secret + SHA1( 'A' + secret + random ) ) +
484 * MD5( secret + SHA1( 'BB' + secret + random ) ) +
485 * MD5( secret + SHA1( 'CCC' + secret + random ) ) +
486 * ...
487 */
488 for( i = 0; i < dlen / 16; i++ )
489 {
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200490 memset( padding, (unsigned char) ('A' + i), 1 + i );
Paul Bakker5f70b252012-09-13 14:23:06 +0000491
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100492 if( ( ret = mbedtls_sha1_starts_ret( &sha1 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100493 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100494 if( ( ret = mbedtls_sha1_update_ret( &sha1, padding, 1 + i ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100495 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100496 if( ( ret = mbedtls_sha1_update_ret( &sha1, secret, slen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100497 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100498 if( ( ret = mbedtls_sha1_update_ret( &sha1, random, rlen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100499 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100500 if( ( ret = mbedtls_sha1_finish_ret( &sha1, sha1sum ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100501 goto exit;
Paul Bakker5f70b252012-09-13 14:23:06 +0000502
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100503 if( ( ret = mbedtls_md5_starts_ret( &md5 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100504 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100505 if( ( ret = mbedtls_md5_update_ret( &md5, secret, slen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100506 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100507 if( ( ret = mbedtls_md5_update_ret( &md5, sha1sum, 20 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100508 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100509 if( ( ret = mbedtls_md5_finish_ret( &md5, dstbuf + i * 16 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100510 goto exit;
Paul Bakker5f70b252012-09-13 14:23:06 +0000511 }
512
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100513exit:
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200514 mbedtls_md5_free( &md5 );
515 mbedtls_sha1_free( &sha1 );
Paul Bakker5f70b252012-09-13 14:23:06 +0000516
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500517 mbedtls_platform_zeroize( padding, sizeof( padding ) );
518 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5f70b252012-09-13 14:23:06 +0000519
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100520 return( ret );
Paul Bakker5f70b252012-09-13 14:23:06 +0000521}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200522#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +0000523
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200524#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200525static int tls1_prf( const unsigned char *secret, size_t slen,
526 const char *label,
527 const unsigned char *random, size_t rlen,
Paul Bakker23986e52011-04-24 08:57:21 +0000528 unsigned char *dstbuf, size_t dlen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000529{
Paul Bakker23986e52011-04-24 08:57:21 +0000530 size_t nb, hs;
531 size_t i, j, k;
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200532 const unsigned char *S1, *S2;
Ron Eldor3b350852019-05-07 18:31:49 +0300533 unsigned char *tmp;
534 size_t tmp_len = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000535 unsigned char h_i[20];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200536 const mbedtls_md_info_t *md_info;
537 mbedtls_md_context_t md_ctx;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100538 int ret;
539
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200540 mbedtls_md_init( &md_ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000541
Ron Eldor3b350852019-05-07 18:31:49 +0300542 tmp_len = 20 + strlen( label ) + rlen;
543 tmp = mbedtls_calloc( 1, tmp_len );
544 if( tmp == NULL )
545 {
546 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
547 goto exit;
548 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000549
550 hs = ( slen + 1 ) / 2;
551 S1 = secret;
552 S2 = secret + slen - hs;
553
554 nb = strlen( label );
555 memcpy( tmp + 20, label, nb );
556 memcpy( tmp + 20 + nb, random, rlen );
557 nb += rlen;
558
559 /*
560 * First compute P_md5(secret,label+random)[0..dlen]
561 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200562 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_MD5 ) ) == NULL )
Ron Eldor3b350852019-05-07 18:31:49 +0300563 {
564 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
565 goto exit;
566 }
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100567
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200568 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Ron Eldor3b350852019-05-07 18:31:49 +0300569 {
570 goto exit;
571 }
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100572
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200573 mbedtls_md_hmac_starts( &md_ctx, S1, hs );
574 mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb );
575 mbedtls_md_hmac_finish( &md_ctx, 4 + tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000576
577 for( i = 0; i < dlen; i += 16 )
578 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200579 mbedtls_md_hmac_reset ( &md_ctx );
580 mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 + nb );
581 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100582
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200583 mbedtls_md_hmac_reset ( &md_ctx );
584 mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 );
585 mbedtls_md_hmac_finish( &md_ctx, 4 + tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000586
587 k = ( i + 16 > dlen ) ? dlen % 16 : 16;
588
589 for( j = 0; j < k; j++ )
590 dstbuf[i + j] = h_i[j];
591 }
592
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200593 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100594
Paul Bakker5121ce52009-01-03 21:22:43 +0000595 /*
596 * XOR out with P_sha1(secret,label+random)[0..dlen]
597 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200598 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_SHA1 ) ) == NULL )
Ron Eldor3b350852019-05-07 18:31:49 +0300599 {
600 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
601 goto exit;
602 }
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100603
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200604 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Ron Eldor3b350852019-05-07 18:31:49 +0300605 {
606 goto exit;
607 }
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100608
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200609 mbedtls_md_hmac_starts( &md_ctx, S2, hs );
610 mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb );
611 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000612
613 for( i = 0; i < dlen; i += 20 )
614 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200615 mbedtls_md_hmac_reset ( &md_ctx );
616 mbedtls_md_hmac_update( &md_ctx, tmp, 20 + nb );
617 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100618
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200619 mbedtls_md_hmac_reset ( &md_ctx );
620 mbedtls_md_hmac_update( &md_ctx, tmp, 20 );
621 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000622
623 k = ( i + 20 > dlen ) ? dlen % 20 : 20;
624
625 for( j = 0; j < k; j++ )
626 dstbuf[i + j] = (unsigned char)( dstbuf[i + j] ^ h_i[j] );
627 }
628
Ron Eldor3b350852019-05-07 18:31:49 +0300629exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200630 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100631
Ron Eldor3b350852019-05-07 18:31:49 +0300632 mbedtls_platform_zeroize( tmp, tmp_len );
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500633 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000634
Ron Eldor3b350852019-05-07 18:31:49 +0300635 mbedtls_free( tmp );
636 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000637}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200638#endif /* MBEDTLS_SSL_PROTO_TLS1) || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000639
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200640#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurekc929a822019-01-14 03:51:11 -0500641#if defined(MBEDTLS_USE_PSA_CRYPTO)
642static int tls_prf_generic( mbedtls_md_type_t md_type,
643 const unsigned char *secret, size_t slen,
644 const char *label,
645 const unsigned char *random, size_t rlen,
646 unsigned char *dstbuf, size_t dlen )
647{
648 psa_status_t status;
649 psa_algorithm_t alg;
650 psa_key_policy_t policy;
Andrzej Kurekac5dc342019-01-23 06:57:34 -0500651 psa_key_handle_t master_slot;
Andrzej Kurekc929a822019-01-14 03:51:11 -0500652 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
653
Andrzej Kurek2f760752019-01-28 08:08:15 -0500654 if( ( status = psa_allocate_key( &master_slot ) ) != PSA_SUCCESS )
Andrzej Kurekac5dc342019-01-23 06:57:34 -0500655 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Andrzej Kurek2d4faa62019-01-29 03:14:15 -0500656
Andrzej Kurekc929a822019-01-14 03:51:11 -0500657 if( md_type == MBEDTLS_MD_SHA384 )
658 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_384);
659 else
660 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_256);
661
Andrzej Kurek2f760752019-01-28 08:08:15 -0500662 policy = psa_key_policy_init();
Andrzej Kurekc929a822019-01-14 03:51:11 -0500663 psa_key_policy_set_usage( &policy,
664 PSA_KEY_USAGE_DERIVE,
665 alg );
666 status = psa_set_key_policy( master_slot, &policy );
667 if( status != PSA_SUCCESS )
668 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
669
670 status = psa_import_key( master_slot, PSA_KEY_TYPE_DERIVE, secret, slen );
671 if( status != PSA_SUCCESS )
672 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
673
674 status = psa_key_derivation( &generator,
675 master_slot, alg,
676 random, rlen,
677 (unsigned char const *) label,
678 (size_t) strlen( label ),
679 dlen );
680 if( status != PSA_SUCCESS )
681 {
682 psa_generator_abort( &generator );
683 psa_destroy_key( master_slot );
684 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
685 }
686
687 status = psa_generator_read( &generator, dstbuf, dlen );
688 if( status != PSA_SUCCESS )
689 {
690 psa_generator_abort( &generator );
691 psa_destroy_key( master_slot );
692 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
693 }
694
695 status = psa_generator_abort( &generator );
696 if( status != PSA_SUCCESS )
Andrzej Kurek70737ca2019-01-14 05:37:13 -0500697 {
698 psa_destroy_key( master_slot );
Andrzej Kurekc929a822019-01-14 03:51:11 -0500699 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Andrzej Kurek70737ca2019-01-14 05:37:13 -0500700 }
Andrzej Kurekc929a822019-01-14 03:51:11 -0500701
702 status = psa_destroy_key( master_slot );
703 if( status != PSA_SUCCESS )
704 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
705
Andrzej Kurek33171262019-01-15 03:25:18 -0500706 return( 0 );
Andrzej Kurekc929a822019-01-14 03:51:11 -0500707}
708
709#else /* MBEDTLS_USE_PSA_CRYPTO */
710
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200711static int tls_prf_generic( mbedtls_md_type_t md_type,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100712 const unsigned char *secret, size_t slen,
713 const char *label,
714 const unsigned char *random, size_t rlen,
715 unsigned char *dstbuf, size_t dlen )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000716{
717 size_t nb;
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100718 size_t i, j, k, md_len;
Ron Eldor3b350852019-05-07 18:31:49 +0300719 unsigned char *tmp;
720 size_t tmp_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200721 unsigned char h_i[MBEDTLS_MD_MAX_SIZE];
722 const mbedtls_md_info_t *md_info;
723 mbedtls_md_context_t md_ctx;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100724 int ret;
725
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200726 mbedtls_md_init( &md_ctx );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000727
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200728 if( ( md_info = mbedtls_md_info_from_type( md_type ) ) == NULL )
729 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100730
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200731 md_len = mbedtls_md_get_size( md_info );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100732
Ron Eldor3b350852019-05-07 18:31:49 +0300733 tmp_len = md_len + strlen( label ) + rlen;
734 tmp = mbedtls_calloc( 1, tmp_len );
735 if( tmp == NULL )
736 {
737 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
738 goto exit;
739 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000740
741 nb = strlen( label );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100742 memcpy( tmp + md_len, label, nb );
743 memcpy( tmp + md_len + nb, random, rlen );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000744 nb += rlen;
745
746 /*
747 * Compute P_<hash>(secret, label + random)[0..dlen]
748 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200749 if ( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Ron Eldor3b350852019-05-07 18:31:49 +0300750 goto exit;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100751
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200752 mbedtls_md_hmac_starts( &md_ctx, secret, slen );
753 mbedtls_md_hmac_update( &md_ctx, tmp + md_len, nb );
754 mbedtls_md_hmac_finish( &md_ctx, tmp );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100755
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100756 for( i = 0; i < dlen; i += md_len )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000757 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200758 mbedtls_md_hmac_reset ( &md_ctx );
759 mbedtls_md_hmac_update( &md_ctx, tmp, md_len + nb );
760 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100761
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200762 mbedtls_md_hmac_reset ( &md_ctx );
763 mbedtls_md_hmac_update( &md_ctx, tmp, md_len );
764 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000765
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100766 k = ( i + md_len > dlen ) ? dlen % md_len : md_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +0000767
768 for( j = 0; j < k; j++ )
769 dstbuf[i + j] = h_i[j];
770 }
771
Ron Eldor3b350852019-05-07 18:31:49 +0300772exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200773 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100774
Ron Eldor3b350852019-05-07 18:31:49 +0300775 mbedtls_platform_zeroize( tmp, tmp_len );
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500776 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000777
Ron Eldor3b350852019-05-07 18:31:49 +0300778 mbedtls_free( tmp );
779
780 return( ret );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000781}
Andrzej Kurekc929a822019-01-14 03:51:11 -0500782#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200783#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100784static int tls_prf_sha256( const unsigned char *secret, size_t slen,
785 const char *label,
786 const unsigned char *random, size_t rlen,
787 unsigned char *dstbuf, size_t dlen )
788{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200789 return( tls_prf_generic( MBEDTLS_MD_SHA256, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100790 label, random, rlen, dstbuf, dlen ) );
791}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200792#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000793
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200794#if defined(MBEDTLS_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200795static int tls_prf_sha384( const unsigned char *secret, size_t slen,
796 const char *label,
797 const unsigned char *random, size_t rlen,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000798 unsigned char *dstbuf, size_t dlen )
799{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200800 return( tls_prf_generic( MBEDTLS_MD_SHA384, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100801 label, random, rlen, dstbuf, dlen ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000802}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200803#endif /* MBEDTLS_SHA512_C */
804#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +0000805
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200806static void ssl_update_checksum_start( mbedtls_ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200807
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200808#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
809 defined(MBEDTLS_SSL_PROTO_TLS1_1)
810static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200811#endif
Paul Bakker380da532012-04-18 16:10:25 +0000812
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200813#if defined(MBEDTLS_SSL_PROTO_SSL3)
814static void ssl_calc_verify_ssl( mbedtls_ssl_context *, unsigned char * );
815static void ssl_calc_finished_ssl( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200816#endif
817
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200818#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
819static void ssl_calc_verify_tls( mbedtls_ssl_context *, unsigned char * );
820static void ssl_calc_finished_tls( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200821#endif
822
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200823#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
824#if defined(MBEDTLS_SHA256_C)
825static void ssl_update_checksum_sha256( mbedtls_ssl_context *, const unsigned char *, size_t );
826static void ssl_calc_verify_tls_sha256( mbedtls_ssl_context *,unsigned char * );
827static void ssl_calc_finished_tls_sha256( mbedtls_ssl_context *,unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200828#endif
Paul Bakker769075d2012-11-24 11:26:46 +0100829
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200830#if defined(MBEDTLS_SHA512_C)
831static void ssl_update_checksum_sha384( mbedtls_ssl_context *, const unsigned char *, size_t );
832static void ssl_calc_verify_tls_sha384( mbedtls_ssl_context *, unsigned char * );
833static void ssl_calc_finished_tls_sha384( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakker769075d2012-11-24 11:26:46 +0100834#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200835#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000836
Manuel Pégourié-Gonnard45be3d82019-02-18 23:35:14 +0100837#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED) && \
Hanno Becker7d0a5692018-10-23 15:26:22 +0100838 defined(MBEDTLS_USE_PSA_CRYPTO)
839static int ssl_use_opaque_psk( mbedtls_ssl_context const *ssl )
840{
841 if( ssl->conf->f_psk != NULL )
842 {
843 /* If we've used a callback to select the PSK,
844 * the static configuration is irrelevant. */
845 if( ssl->handshake->psk_opaque != 0 )
846 return( 1 );
847
848 return( 0 );
849 }
850
851 if( ssl->conf->psk_opaque != 0 )
852 return( 1 );
853
854 return( 0 );
855}
856#endif /* MBEDTLS_USE_PSA_CRYPTO &&
Manuel Pégourié-Gonnard45be3d82019-02-18 23:35:14 +0100857 MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
Hanno Becker7d0a5692018-10-23 15:26:22 +0100858
Ron Eldorcf280092019-05-14 20:19:13 +0300859#if defined(MBEDTLS_SSL_EXPORT_KEYS)
860static mbedtls_tls_prf_types tls_prf_get_type( mbedtls_ssl_tls_prf_cb *tls_prf )
861{
862#if defined(MBEDTLS_SSL_PROTO_SSL3)
863 if( tls_prf == ssl3_prf )
864 {
Ron Eldor0810f0b2019-05-15 12:32:32 +0300865 return( MBEDTLS_SSL_TLS_PRF_SSL3 );
Ron Eldorcf280092019-05-14 20:19:13 +0300866 }
867 else
868#endif
869#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
870 if( tls_prf == tls1_prf )
871 {
872 return( MBEDTLS_SSL_TLS_PRF_TLS1 );
873 }
874 else
875#endif
876#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
877#if defined(MBEDTLS_SHA512_C)
878 if( tls_prf == tls_prf_sha384 )
879 {
880 return( MBEDTLS_SSL_TLS_PRF_SHA384 );
881 }
882 else
883#endif
884#if defined(MBEDTLS_SHA256_C)
885 if( tls_prf == tls_prf_sha256 )
886 {
887 return( MBEDTLS_SSL_TLS_PRF_SHA256 );
888 }
889 else
890#endif
891#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
892 return( MBEDTLS_SSL_TLS_PRF_NONE );
893}
894#endif /* MBEDTLS_SSL_EXPORT_KEYS */
895
Ron Eldor51d3ab52019-05-12 14:54:30 +0300896int mbedtls_ssl_tls_prf( const mbedtls_tls_prf_types prf,
897 const unsigned char *secret, size_t slen,
898 const char *label,
899 const unsigned char *random, size_t rlen,
900 unsigned char *dstbuf, size_t dlen )
901{
902 mbedtls_ssl_tls_prf_cb *tls_prf = NULL;
903
904 switch( prf )
905 {
906#if defined(MBEDTLS_SSL_PROTO_SSL3)
907 case MBEDTLS_SSL_TLS_PRF_SSL3:
908 tls_prf = ssl3_prf;
909 break;
Ron Eldord2f25f72019-05-15 14:54:22 +0300910#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Ron Eldor51d3ab52019-05-12 14:54:30 +0300911#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
912 case MBEDTLS_SSL_TLS_PRF_TLS1:
913 tls_prf = tls1_prf;
914 break;
Ron Eldord2f25f72019-05-15 14:54:22 +0300915#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
916
917#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Ron Eldor51d3ab52019-05-12 14:54:30 +0300918#if defined(MBEDTLS_SHA512_C)
919 case MBEDTLS_SSL_TLS_PRF_SHA384:
920 tls_prf = tls_prf_sha384;
921 break;
Ron Eldord2f25f72019-05-15 14:54:22 +0300922#endif /* MBEDTLS_SHA512_C */
Ron Eldor51d3ab52019-05-12 14:54:30 +0300923#if defined(MBEDTLS_SHA256_C)
924 case MBEDTLS_SSL_TLS_PRF_SHA256:
925 tls_prf = tls_prf_sha256;
926 break;
Ron Eldord2f25f72019-05-15 14:54:22 +0300927#endif /* MBEDTLS_SHA256_C */
928#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Ron Eldor51d3ab52019-05-12 14:54:30 +0300929 default:
930 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
931 }
932
933 return( tls_prf( secret, slen, label, random, rlen, dstbuf, dlen ) );
934}
935
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200936int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +0000937{
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200938 int ret = 0;
Hanno Beckercb1cc802018-11-17 22:27:38 +0000939#if defined(MBEDTLS_USE_PSA_CRYPTO)
940 int psa_fallthrough;
941#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker5121ce52009-01-03 21:22:43 +0000942 unsigned char tmp[64];
Paul Bakker5121ce52009-01-03 21:22:43 +0000943 unsigned char keyblk[256];
944 unsigned char *key1;
945 unsigned char *key2;
Paul Bakker68884e32013-01-07 18:20:04 +0100946 unsigned char *mac_enc;
947 unsigned char *mac_dec;
Hanno Becker81c7b182017-11-09 18:39:33 +0000948 size_t mac_key_len;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200949 size_t iv_copy_len;
Hanno Becker88aaf652017-12-27 08:17:40 +0000950 unsigned keylen;
Hanno Beckere694c3e2017-12-27 21:34:08 +0000951 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200952 const mbedtls_cipher_info_t *cipher_info;
953 const mbedtls_md_info_t *md_info;
Paul Bakker68884e32013-01-07 18:20:04 +0100954
Hanno Beckerf9ed7d52018-11-05 12:45:16 +0000955 /* cf. RFC 5246, Section 8.1:
956 * "The master secret is always exactly 48 bytes in length." */
957 size_t const master_secret_len = 48;
958
Hanno Becker35b23c72018-10-23 12:10:41 +0100959#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
960 unsigned char session_hash[48];
961#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
962
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200963 mbedtls_ssl_session *session = ssl->session_negotiate;
964 mbedtls_ssl_transform *transform = ssl->transform_negotiate;
965 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Paul Bakker5121ce52009-01-03 21:22:43 +0000966
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200967 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000968
Jaeden Amero2de07f12019-06-05 13:32:08 +0100969#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) && \
970 defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000971 transform->encrypt_then_mac = session->encrypt_then_mac;
972#endif
973 transform->minor_ver = ssl->minor_ver;
Hanno Beckere694c3e2017-12-27 21:34:08 +0000974
975 ciphersuite_info = handshake->ciphersuite_info;
976 cipher_info = mbedtls_cipher_info_from_type( ciphersuite_info->cipher );
Paul Bakker68884e32013-01-07 18:20:04 +0100977 if( cipher_info == NULL )
978 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200979 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %d not found",
Hanno Beckere694c3e2017-12-27 21:34:08 +0000980 ciphersuite_info->cipher ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200981 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100982 }
983
Hanno Beckere694c3e2017-12-27 21:34:08 +0000984 md_info = mbedtls_md_info_from_type( ciphersuite_info->mac );
Paul Bakker68884e32013-01-07 18:20:04 +0100985 if( md_info == NULL )
986 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200987 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %d not found",
Hanno Beckere694c3e2017-12-27 21:34:08 +0000988 ciphersuite_info->mac ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200989 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100990 }
991
Hanno Beckera0e20d02019-05-15 14:03:01 +0100992#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker4bf74652019-04-26 16:22:27 +0100993 /* Copy own and peer's CID if the use of the CID
994 * extension has been negotiated. */
995 if( ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_ENABLED )
996 {
997 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Copy CIDs into SSL transform" ) );
Hanno Becker8a7f9722019-04-30 13:52:29 +0100998
Hanno Becker05154c32019-05-03 15:23:51 +0100999 transform->in_cid_len = ssl->own_cid_len;
Hanno Becker05154c32019-05-03 15:23:51 +01001000 memcpy( transform->in_cid, ssl->own_cid, ssl->own_cid_len );
Hanno Becker1c1f0462019-05-03 12:55:51 +01001001 MBEDTLS_SSL_DEBUG_BUF( 3, "Incoming CID", transform->in_cid,
Hanno Becker4bf74652019-04-26 16:22:27 +01001002 transform->in_cid_len );
Hanno Beckerd1f20352019-05-15 10:21:55 +01001003
1004 transform->out_cid_len = ssl->handshake->peer_cid_len;
1005 memcpy( transform->out_cid, ssl->handshake->peer_cid,
1006 ssl->handshake->peer_cid_len );
1007 MBEDTLS_SSL_DEBUG_BUF( 3, "Outgoing CID", transform->out_cid,
1008 transform->out_cid_len );
Hanno Becker4bf74652019-04-26 16:22:27 +01001009 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01001010#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker4bf74652019-04-26 16:22:27 +01001011
Paul Bakker5121ce52009-01-03 21:22:43 +00001012 /*
Paul Bakkerca4ab492012-04-18 14:23:57 +00001013 * Set appropriate PRF function and other SSL / TLS / TLS1.2 functions
Paul Bakker1ef83d62012-04-11 12:09:53 +00001014 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001015#if defined(MBEDTLS_SSL_PROTO_SSL3)
1016 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001017 {
Paul Bakker48916f92012-09-16 19:57:18 +00001018 handshake->tls_prf = ssl3_prf;
1019 handshake->calc_verify = ssl_calc_verify_ssl;
1020 handshake->calc_finished = ssl_calc_finished_ssl;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001021 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001022 else
1023#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001024#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
1025 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001026 {
Paul Bakker48916f92012-09-16 19:57:18 +00001027 handshake->tls_prf = tls1_prf;
1028 handshake->calc_verify = ssl_calc_verify_tls;
1029 handshake->calc_finished = ssl_calc_finished_tls;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001030 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001031 else
1032#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001033#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1034#if defined(MBEDTLS_SHA512_C)
1035 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
Hanno Beckere694c3e2017-12-27 21:34:08 +00001036 ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001037 {
Paul Bakker48916f92012-09-16 19:57:18 +00001038 handshake->tls_prf = tls_prf_sha384;
1039 handshake->calc_verify = ssl_calc_verify_tls_sha384;
1040 handshake->calc_finished = ssl_calc_finished_tls_sha384;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001041 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00001042 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001043#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001044#if defined(MBEDTLS_SHA256_C)
1045 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001046 {
Paul Bakker48916f92012-09-16 19:57:18 +00001047 handshake->tls_prf = tls_prf_sha256;
1048 handshake->calc_verify = ssl_calc_verify_tls_sha256;
1049 handshake->calc_finished = ssl_calc_finished_tls_sha256;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001050 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001051 else
1052#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001053#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02001054 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001055 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1056 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02001057 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00001058
1059 /*
Paul Bakker5121ce52009-01-03 21:22:43 +00001060 * SSLv3:
1061 * master =
1062 * MD5( premaster + SHA1( 'A' + premaster + randbytes ) ) +
1063 * MD5( premaster + SHA1( 'BB' + premaster + randbytes ) ) +
1064 * MD5( premaster + SHA1( 'CCC' + premaster + randbytes ) )
Paul Bakkerf7abd422013-04-16 13:15:56 +02001065 *
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001066 * TLSv1+:
Paul Bakker5121ce52009-01-03 21:22:43 +00001067 * master = PRF( premaster, "master secret", randbytes )[0..47]
1068 */
Hanno Becker35b23c72018-10-23 12:10:41 +01001069 if( handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001070 {
Hanno Becker35b23c72018-10-23 12:10:41 +01001071 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
1072 }
1073 else
1074 {
1075 /* The label for the KDF used for key expansion.
1076 * This is either "master secret" or "extended master secret"
1077 * depending on whether the Extended Master Secret extension
1078 * is used. */
1079 char const *lbl = "master secret";
1080
1081 /* The salt for the KDF used for key expansion.
1082 * - If the Extended Master Secret extension is not used,
1083 * this is ClientHello.Random + ServerHello.Random
1084 * (see Sect. 8.1 in RFC 5246).
1085 * - If the Extended Master Secret extension is used,
1086 * this is the transcript of the handshake so far.
1087 * (see Sect. 4 in RFC 7627). */
1088 unsigned char const *salt = handshake->randbytes;
1089 size_t salt_len = 64;
1090
1091#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001092 if( ssl->handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001093 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001094 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using extended master secret" ) );
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001095
Hanno Becker35b23c72018-10-23 12:10:41 +01001096 lbl = "extended master secret";
1097 salt = session_hash;
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001098 ssl->handshake->calc_verify( ssl, session_hash );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001099#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1100 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001101 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001102#if defined(MBEDTLS_SHA512_C)
Hanno Beckere694c3e2017-12-27 21:34:08 +00001103 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
1104 {
Hanno Becker35b23c72018-10-23 12:10:41 +01001105 salt_len = 48;
Hanno Beckere694c3e2017-12-27 21:34:08 +00001106 }
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001107 else
Hanno Becker35b23c72018-10-23 12:10:41 +01001108#endif /* MBEDTLS_SHA512_C */
1109 salt_len = 32;
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001110 }
1111 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001112#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker35b23c72018-10-23 12:10:41 +01001113 salt_len = 36;
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001114
Hanno Becker35b23c72018-10-23 12:10:41 +01001115 MBEDTLS_SSL_DEBUG_BUF( 3, "session hash", session_hash, salt_len );
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001116 }
Hanno Becker35b23c72018-10-23 12:10:41 +01001117#endif /* MBEDTLS_SSL_EXTENDED_MS_ENABLED */
1118
Hanno Becker7d0a5692018-10-23 15:26:22 +01001119#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
1120 defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
1121 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK &&
1122 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
1123 ssl_use_opaque_psk( ssl ) == 1 )
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +01001124 {
Hanno Becker7d0a5692018-10-23 15:26:22 +01001125 /* Perform PSK-to-MS expansion in a single step. */
1126 psa_status_t status;
1127 psa_algorithm_t alg;
1128 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Andrzej Kurek2349c4d2019-01-08 09:36:01 -05001129 psa_key_handle_t psk;
Hanno Becker7d0a5692018-10-23 15:26:22 +01001130
1131 MBEDTLS_SSL_DEBUG_MSG( 2, ( "perform PSA-based PSK-to-MS expansion" ) );
1132
1133 psk = ssl->conf->psk_opaque;
1134 if( ssl->handshake->psk_opaque != 0 )
1135 psk = ssl->handshake->psk_opaque;
1136
Hanno Becker22bf1452019-04-05 11:21:08 +01001137 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Hanno Becker7d0a5692018-10-23 15:26:22 +01001138 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384);
1139 else
1140 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256);
1141
1142 status = psa_key_derivation( &generator, psk, alg,
1143 salt, salt_len,
1144 (unsigned char const *) lbl,
1145 (size_t) strlen( lbl ),
Hanno Beckerf9ed7d52018-11-05 12:45:16 +00001146 master_secret_len );
Hanno Becker7d0a5692018-10-23 15:26:22 +01001147 if( status != PSA_SUCCESS )
1148 {
1149 psa_generator_abort( &generator );
1150 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
1151 }
1152
Hanno Beckerf9ed7d52018-11-05 12:45:16 +00001153 status = psa_generator_read( &generator, session->master,
1154 master_secret_len );
Hanno Becker7d0a5692018-10-23 15:26:22 +01001155 if( status != PSA_SUCCESS )
1156 {
1157 psa_generator_abort( &generator );
1158 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
1159 }
1160
1161 status = psa_generator_abort( &generator );
1162 if( status != PSA_SUCCESS )
1163 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +01001164 }
Hanno Becker7d0a5692018-10-23 15:26:22 +01001165 else
1166#endif
1167 {
1168 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
1169 lbl, salt, salt_len,
Hanno Beckerf9ed7d52018-11-05 12:45:16 +00001170 session->master,
1171 master_secret_len );
Hanno Becker7d0a5692018-10-23 15:26:22 +01001172 if( ret != 0 )
1173 {
1174 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
1175 return( ret );
1176 }
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001177
Hanno Becker7d0a5692018-10-23 15:26:22 +01001178 MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret",
1179 handshake->premaster,
1180 handshake->pmslen );
Hanno Becker35b23c72018-10-23 12:10:41 +01001181
Hanno Becker7d0a5692018-10-23 15:26:22 +01001182 mbedtls_platform_zeroize( handshake->premaster,
1183 sizeof(handshake->premaster) );
1184 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001185 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001186
1187 /*
1188 * Swap the client and server random values.
1189 */
Paul Bakker48916f92012-09-16 19:57:18 +00001190 memcpy( tmp, handshake->randbytes, 64 );
1191 memcpy( handshake->randbytes, tmp + 32, 32 );
1192 memcpy( handshake->randbytes + 32, tmp, 32 );
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001193 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001194
1195 /*
1196 * SSLv3:
1197 * key block =
1198 * MD5( master + SHA1( 'A' + master + randbytes ) ) +
1199 * MD5( master + SHA1( 'BB' + master + randbytes ) ) +
1200 * MD5( master + SHA1( 'CCC' + master + randbytes ) ) +
1201 * MD5( master + SHA1( 'DDDD' + master + randbytes ) ) +
1202 * ...
1203 *
1204 * TLSv1:
1205 * key block = PRF( master, "key expansion", randbytes )
1206 */
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +01001207 ret = handshake->tls_prf( session->master, 48, "key expansion",
1208 handshake->randbytes, 64, keyblk, 256 );
1209 if( ret != 0 )
1210 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001211 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +01001212 return( ret );
1213 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001214
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001215 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
1216 mbedtls_ssl_get_ciphersuite_name( session->ciphersuite ) ) );
1217 MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", session->master, 48 );
1218 MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", handshake->randbytes, 64 );
1219 MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001220
Paul Bakker5121ce52009-01-03 21:22:43 +00001221 /*
1222 * Determine the appropriate key, IV and MAC length.
1223 */
Paul Bakker68884e32013-01-07 18:20:04 +01001224
Hanno Becker88aaf652017-12-27 08:17:40 +00001225 keylen = cipher_info->key_bitlen / 8;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001226
Hanno Becker8031d062018-01-03 15:32:31 +00001227#if defined(MBEDTLS_GCM_C) || \
1228 defined(MBEDTLS_CCM_C) || \
1229 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001230 if( cipher_info->mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001231 cipher_info->mode == MBEDTLS_MODE_CCM ||
1232 cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakker5121ce52009-01-03 21:22:43 +00001233 {
Hanno Beckerf704bef2018-11-16 15:21:18 +00001234 size_t explicit_ivlen;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001235
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001236 transform->maclen = 0;
Hanno Becker81c7b182017-11-09 18:39:33 +00001237 mac_key_len = 0;
Hanno Beckere694c3e2017-12-27 21:34:08 +00001238 transform->taglen =
1239 ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001240
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001241 /* All modes haves 96-bit IVs;
1242 * GCM and CCM has 4 implicit and 8 explicit bytes
1243 * ChachaPoly has all 12 bytes implicit
1244 */
Paul Bakker68884e32013-01-07 18:20:04 +01001245 transform->ivlen = 12;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001246 if( cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
1247 transform->fixed_ivlen = 12;
1248 else
1249 transform->fixed_ivlen = 4;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001250
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001251 /* Minimum length of encrypted record */
1252 explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
Hanno Beckere694c3e2017-12-27 21:34:08 +00001253 transform->minlen = explicit_ivlen + transform->taglen;
Paul Bakker68884e32013-01-07 18:20:04 +01001254 }
1255 else
Hanno Becker8031d062018-01-03 15:32:31 +00001256#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
1257#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
1258 if( cipher_info->mode == MBEDTLS_MODE_STREAM ||
1259 cipher_info->mode == MBEDTLS_MODE_CBC )
Paul Bakker68884e32013-01-07 18:20:04 +01001260 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001261 /* Initialize HMAC contexts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001262 if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 ||
1263 ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 )
Paul Bakker68884e32013-01-07 18:20:04 +01001264 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001265 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001266 goto end;
Paul Bakker68884e32013-01-07 18:20:04 +01001267 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001268
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001269 /* Get MAC length */
Hanno Becker81c7b182017-11-09 18:39:33 +00001270 mac_key_len = mbedtls_md_get_size( md_info );
1271 transform->maclen = mac_key_len;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001272
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001273#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001274 /*
1275 * If HMAC is to be truncated, we shall keep the leftmost bytes,
1276 * (rfc 6066 page 13 or rfc 2104 section 4),
1277 * so we only need to adjust the length here.
1278 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001279 if( session->trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_ENABLED )
Hanno Beckere89353a2017-11-20 16:36:41 +00001280 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001281 transform->maclen = MBEDTLS_SSL_TRUNCATED_HMAC_LEN;
Hanno Beckere89353a2017-11-20 16:36:41 +00001282
1283#if defined(MBEDTLS_SSL_TRUNCATED_HMAC_COMPAT)
1284 /* Fall back to old, non-compliant version of the truncated
Hanno Becker563423f2017-11-21 17:20:17 +00001285 * HMAC implementation which also truncates the key
1286 * (Mbed TLS versions from 1.3 to 2.6.0) */
Hanno Beckere89353a2017-11-20 16:36:41 +00001287 mac_key_len = transform->maclen;
1288#endif
1289 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001290#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001291
1292 /* IV length */
Paul Bakker68884e32013-01-07 18:20:04 +01001293 transform->ivlen = cipher_info->iv_size;
Paul Bakker5121ce52009-01-03 21:22:43 +00001294
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001295 /* Minimum length */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001296 if( cipher_info->mode == MBEDTLS_MODE_STREAM )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001297 transform->minlen = transform->maclen;
1298 else
Paul Bakker68884e32013-01-07 18:20:04 +01001299 {
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001300 /*
1301 * GenericBlockCipher:
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +01001302 * 1. if EtM is in use: one block plus MAC
1303 * otherwise: * first multiple of blocklen greater than maclen
1304 * 2. IV except for SSL3 and TLS 1.0
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001305 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001306#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1307 if( session->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +01001308 {
1309 transform->minlen = transform->maclen
1310 + cipher_info->block_size;
1311 }
1312 else
1313#endif
1314 {
1315 transform->minlen = transform->maclen
1316 + cipher_info->block_size
1317 - transform->maclen % cipher_info->block_size;
1318 }
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001319
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001320#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
1321 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ||
1322 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001323 ; /* No need to adjust minlen */
Paul Bakker68884e32013-01-07 18:20:04 +01001324 else
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001325#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001326#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
1327 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_2 ||
1328 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001329 {
1330 transform->minlen += transform->ivlen;
1331 }
1332 else
1333#endif
1334 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001335 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001336 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1337 goto end;
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001338 }
Paul Bakker68884e32013-01-07 18:20:04 +01001339 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001340 }
Hanno Becker8031d062018-01-03 15:32:31 +00001341 else
1342#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
1343 {
1344 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1345 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1346 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001347
Hanno Becker88aaf652017-12-27 08:17:40 +00001348 MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %u, minlen: %u, ivlen: %u, maclen: %u",
1349 (unsigned) keylen,
1350 (unsigned) transform->minlen,
1351 (unsigned) transform->ivlen,
1352 (unsigned) transform->maclen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001353
1354 /*
1355 * Finally setup the cipher contexts, IVs and MAC secrets.
1356 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001357#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001358 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00001359 {
Hanno Becker81c7b182017-11-09 18:39:33 +00001360 key1 = keyblk + mac_key_len * 2;
Hanno Becker88aaf652017-12-27 08:17:40 +00001361 key2 = keyblk + mac_key_len * 2 + keylen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001362
Paul Bakker68884e32013-01-07 18:20:04 +01001363 mac_enc = keyblk;
Hanno Becker81c7b182017-11-09 18:39:33 +00001364 mac_dec = keyblk + mac_key_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001365
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001366 /*
1367 * This is not used in TLS v1.1.
1368 */
Paul Bakker48916f92012-09-16 19:57:18 +00001369 iv_copy_len = ( transform->fixed_ivlen ) ?
1370 transform->fixed_ivlen : transform->ivlen;
Hanno Becker88aaf652017-12-27 08:17:40 +00001371 memcpy( transform->iv_enc, key2 + keylen, iv_copy_len );
1372 memcpy( transform->iv_dec, key2 + keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +00001373 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001374 }
1375 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001376#endif /* MBEDTLS_SSL_CLI_C */
1377#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001378 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00001379 {
Hanno Becker88aaf652017-12-27 08:17:40 +00001380 key1 = keyblk + mac_key_len * 2 + keylen;
Hanno Becker81c7b182017-11-09 18:39:33 +00001381 key2 = keyblk + mac_key_len * 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00001382
Hanno Becker81c7b182017-11-09 18:39:33 +00001383 mac_enc = keyblk + mac_key_len;
Paul Bakker68884e32013-01-07 18:20:04 +01001384 mac_dec = keyblk;
Paul Bakker5121ce52009-01-03 21:22:43 +00001385
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001386 /*
1387 * This is not used in TLS v1.1.
1388 */
Paul Bakker48916f92012-09-16 19:57:18 +00001389 iv_copy_len = ( transform->fixed_ivlen ) ?
1390 transform->fixed_ivlen : transform->ivlen;
Hanno Becker88aaf652017-12-27 08:17:40 +00001391 memcpy( transform->iv_dec, key1 + keylen, iv_copy_len );
1392 memcpy( transform->iv_enc, key1 + keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +00001393 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001394 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001395 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001396#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001397 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001398 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001399 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1400 goto end;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001401 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001402
Hanno Beckerd56ed242018-01-03 15:32:51 +00001403#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001404#if defined(MBEDTLS_SSL_PROTO_SSL3)
1405 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker68884e32013-01-07 18:20:04 +01001406 {
Hanno Beckerd56ed242018-01-03 15:32:51 +00001407 if( mac_key_len > sizeof( transform->mac_enc ) )
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001408 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001409 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001410 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1411 goto end;
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001412 }
1413
Hanno Becker81c7b182017-11-09 18:39:33 +00001414 memcpy( transform->mac_enc, mac_enc, mac_key_len );
1415 memcpy( transform->mac_dec, mac_dec, mac_key_len );
Paul Bakker68884e32013-01-07 18:20:04 +01001416 }
1417 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001418#endif /* MBEDTLS_SSL_PROTO_SSL3 */
1419#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1420 defined(MBEDTLS_SSL_PROTO_TLS1_2)
1421 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakker68884e32013-01-07 18:20:04 +01001422 {
Gilles Peskine039fd122018-03-19 19:06:08 +01001423 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
1424 For AEAD-based ciphersuites, there is nothing to do here. */
1425 if( mac_key_len != 0 )
1426 {
1427 mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, mac_key_len );
1428 mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, mac_key_len );
1429 }
Paul Bakker68884e32013-01-07 18:20:04 +01001430 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001431 else
1432#endif
Paul Bakker577e0062013-08-28 11:57:20 +02001433 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001434 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001435 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1436 goto end;
Paul Bakker577e0062013-08-28 11:57:20 +02001437 }
Hanno Beckerd56ed242018-01-03 15:32:51 +00001438#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker68884e32013-01-07 18:20:04 +01001439
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001440#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
1441 if( mbedtls_ssl_hw_record_init != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00001442 {
1443 int ret = 0;
1444
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001445 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_init()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00001446
Hanno Becker88aaf652017-12-27 08:17:40 +00001447 if( ( ret = mbedtls_ssl_hw_record_init( ssl, key1, key2, keylen,
Paul Bakker07eb38b2012-12-19 14:42:06 +01001448 transform->iv_enc, transform->iv_dec,
1449 iv_copy_len,
Paul Bakker68884e32013-01-07 18:20:04 +01001450 mac_enc, mac_dec,
Hanno Becker81c7b182017-11-09 18:39:33 +00001451 mac_key_len ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00001452 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001453 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_init", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001454 ret = MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
1455 goto end;
Paul Bakker05ef8352012-05-08 09:17:57 +00001456 }
1457 }
Hanno Beckerd56ed242018-01-03 15:32:51 +00001458#else
1459 ((void) mac_dec);
1460 ((void) mac_enc);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001461#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00001462
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001463#if defined(MBEDTLS_SSL_EXPORT_KEYS)
1464 if( ssl->conf->f_export_keys != NULL )
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001465 {
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001466 ssl->conf->f_export_keys( ssl->conf->p_export_keys,
1467 session->master, keyblk,
Hanno Becker88aaf652017-12-27 08:17:40 +00001468 mac_key_len, keylen,
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001469 iv_copy_len );
1470 }
Ron Eldorf5cc10d2019-05-07 18:33:40 +03001471
1472 if( ssl->conf->f_export_keys_ext != NULL )
1473 {
1474 ssl->conf->f_export_keys_ext( ssl->conf->p_export_keys,
1475 session->master, keyblk,
Ron Eldorb7fd64c2019-05-12 11:03:32 +03001476 mac_key_len, keylen,
Ron Eldor51d3ab52019-05-12 14:54:30 +03001477 iv_copy_len,
Ron Eldorf5cc10d2019-05-07 18:33:40 +03001478 handshake->randbytes + 32,
Ron Eldor51d3ab52019-05-12 14:54:30 +03001479 handshake->randbytes,
Ron Eldorcf280092019-05-14 20:19:13 +03001480 tls_prf_get_type( handshake->tls_prf ) );
Ron Eldorf5cc10d2019-05-07 18:33:40 +03001481 }
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001482#endif
1483
Hanno Beckerf704bef2018-11-16 15:21:18 +00001484#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Beckercb1cc802018-11-17 22:27:38 +00001485
1486 /* Only use PSA-based ciphers for TLS-1.2.
1487 * That's relevant at least for TLS-1.0, where
1488 * we assume that mbedtls_cipher_crypt() updates
1489 * the structure field for the IV, which the PSA-based
1490 * implementation currently doesn't. */
1491#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1492 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001493 {
Hanno Beckercb1cc802018-11-17 22:27:38 +00001494 ret = mbedtls_cipher_setup_psa( &transform->cipher_ctx_enc,
Hanno Becker22bf1452019-04-05 11:21:08 +01001495 cipher_info, transform->taglen );
Hanno Beckercb1cc802018-11-17 22:27:38 +00001496 if( ret != 0 && ret != MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE )
1497 {
1498 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup_psa", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001499 goto end;
Hanno Beckercb1cc802018-11-17 22:27:38 +00001500 }
1501
1502 if( ret == 0 )
1503 {
Hanno Becker4c8c7aa2019-04-10 09:25:41 +01001504 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Successfully setup PSA-based encryption cipher context" ) );
Hanno Beckercb1cc802018-11-17 22:27:38 +00001505 psa_fallthrough = 0;
1506 }
1507 else
1508 {
1509 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to setup PSA-based cipher context for record encryption - fall through to default setup." ) );
1510 psa_fallthrough = 1;
1511 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001512 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001513 else
Hanno Beckercb1cc802018-11-17 22:27:38 +00001514 psa_fallthrough = 1;
1515#else
1516 psa_fallthrough = 1;
1517#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Beckerf704bef2018-11-16 15:21:18 +00001518
Hanno Beckercb1cc802018-11-17 22:27:38 +00001519 if( psa_fallthrough == 1 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001520#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001521 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001522 cipher_info ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001523 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001524 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001525 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001526 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001527
Hanno Beckerf704bef2018-11-16 15:21:18 +00001528#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Beckercb1cc802018-11-17 22:27:38 +00001529 /* Only use PSA-based ciphers for TLS-1.2.
1530 * That's relevant at least for TLS-1.0, where
1531 * we assume that mbedtls_cipher_crypt() updates
1532 * the structure field for the IV, which the PSA-based
1533 * implementation currently doesn't. */
1534#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1535 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001536 {
Hanno Beckercb1cc802018-11-17 22:27:38 +00001537 ret = mbedtls_cipher_setup_psa( &transform->cipher_ctx_dec,
Hanno Becker22bf1452019-04-05 11:21:08 +01001538 cipher_info, transform->taglen );
Hanno Beckercb1cc802018-11-17 22:27:38 +00001539 if( ret != 0 && ret != MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE )
1540 {
1541 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup_psa", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001542 goto end;
Hanno Beckercb1cc802018-11-17 22:27:38 +00001543 }
1544
1545 if( ret == 0 )
1546 {
Hanno Becker4c8c7aa2019-04-10 09:25:41 +01001547 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Successfully setup PSA-based decryption cipher context" ) );
Hanno Beckercb1cc802018-11-17 22:27:38 +00001548 psa_fallthrough = 0;
1549 }
1550 else
1551 {
1552 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to setup PSA-based cipher context for record decryption - fall through to default setup." ) );
1553 psa_fallthrough = 1;
1554 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001555 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001556 else
Hanno Beckercb1cc802018-11-17 22:27:38 +00001557 psa_fallthrough = 1;
1558#else
1559 psa_fallthrough = 1;
1560#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Beckerf704bef2018-11-16 15:21:18 +00001561
Hanno Beckercb1cc802018-11-17 22:27:38 +00001562 if( psa_fallthrough == 1 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001563#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001564 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001565 cipher_info ) ) != 0 )
1566 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001567 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001568 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001569 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001570
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001571 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1,
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_ENCRYPT ) ) != 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 Bakkerea6ad3f2013-09-02 14:57:01 +02001578
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001579 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001580 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001581 MBEDTLS_DECRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001582 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001583 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001584 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001585 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001586
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001587#if defined(MBEDTLS_CIPHER_MODE_CBC)
1588 if( cipher_info->mode == MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001589 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001590 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc,
1591 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001592 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001593 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001594 goto end;
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001595 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001596
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001597 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec,
1598 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001599 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001600 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001601 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001602 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001603 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001604#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001605
Paul Bakker5121ce52009-01-03 21:22:43 +00001606
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001607#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00001608 // Initialize compression
1609 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001610 if( session->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001611 {
Paul Bakker16770332013-10-11 09:59:44 +02001612 if( ssl->compress_buf == NULL )
1613 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001614 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Allocating compression buffer" ) );
Angus Grattond8213d02016-05-25 20:56:48 +10001615 ssl->compress_buf = mbedtls_calloc( 1, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Paul Bakker16770332013-10-11 09:59:44 +02001616 if( ssl->compress_buf == NULL )
1617 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02001618 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
Angus Grattond8213d02016-05-25 20:56:48 +10001619 MBEDTLS_SSL_COMPRESS_BUFFER_LEN ) );
Ron Eldore6992702019-05-07 18:27:13 +03001620 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
1621 goto end;
Paul Bakker16770332013-10-11 09:59:44 +02001622 }
1623 }
1624
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001625 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Initializing zlib states" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001626
Paul Bakker48916f92012-09-16 19:57:18 +00001627 memset( &transform->ctx_deflate, 0, sizeof( transform->ctx_deflate ) );
1628 memset( &transform->ctx_inflate, 0, sizeof( transform->ctx_inflate ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001629
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001630 if( deflateInit( &transform->ctx_deflate,
1631 Z_DEFAULT_COMPRESSION ) != Z_OK ||
Paul Bakker48916f92012-09-16 19:57:18 +00001632 inflateInit( &transform->ctx_inflate ) != Z_OK )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001633 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001634 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001635 ret = MBEDTLS_ERR_SSL_COMPRESSION_FAILED;
1636 goto end;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001637 }
1638 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001639#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00001640
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001641 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001642end:
Ron Eldora9f9a732019-05-07 18:29:02 +03001643 mbedtls_platform_zeroize( keyblk, sizeof( keyblk ) );
1644 mbedtls_platform_zeroize( handshake->randbytes,
1645 sizeof( handshake->randbytes ) );
Ron Eldore6992702019-05-07 18:27:13 +03001646 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001647}
1648
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001649#if defined(MBEDTLS_SSL_PROTO_SSL3)
1650void ssl_calc_verify_ssl( mbedtls_ssl_context *ssl, unsigned char hash[36] )
Paul Bakker5121ce52009-01-03 21:22:43 +00001651{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001652 mbedtls_md5_context md5;
1653 mbedtls_sha1_context sha1;
Paul Bakker5121ce52009-01-03 21:22:43 +00001654 unsigned char pad_1[48];
1655 unsigned char pad_2[48];
1656
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001657 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001658
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001659 mbedtls_md5_init( &md5 );
1660 mbedtls_sha1_init( &sha1 );
1661
1662 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1663 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001664
Paul Bakker380da532012-04-18 16:10:25 +00001665 memset( pad_1, 0x36, 48 );
1666 memset( pad_2, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001667
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001668 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1669 mbedtls_md5_update_ret( &md5, pad_1, 48 );
1670 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001671
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001672 mbedtls_md5_starts_ret( &md5 );
1673 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1674 mbedtls_md5_update_ret( &md5, pad_2, 48 );
1675 mbedtls_md5_update_ret( &md5, hash, 16 );
1676 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001677
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001678 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1679 mbedtls_sha1_update_ret( &sha1, pad_1, 40 );
1680 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001681
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001682 mbedtls_sha1_starts_ret( &sha1 );
1683 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1684 mbedtls_sha1_update_ret( &sha1, pad_2, 40 );
1685 mbedtls_sha1_update_ret( &sha1, hash + 16, 20 );
1686 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001687
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001688 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
1689 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001690
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001691 mbedtls_md5_free( &md5 );
1692 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001693
Paul Bakker380da532012-04-18 16:10:25 +00001694 return;
1695}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001696#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker380da532012-04-18 16:10:25 +00001697
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001698#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
1699void ssl_calc_verify_tls( mbedtls_ssl_context *ssl, unsigned char hash[36] )
Paul Bakker380da532012-04-18 16:10:25 +00001700{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001701 mbedtls_md5_context md5;
1702 mbedtls_sha1_context sha1;
Paul Bakker380da532012-04-18 16:10:25 +00001703
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001704 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001705
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001706 mbedtls_md5_init( &md5 );
1707 mbedtls_sha1_init( &sha1 );
1708
1709 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1710 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker380da532012-04-18 16:10:25 +00001711
Andrzej Kurekeb342242019-01-29 09:14:33 -05001712 mbedtls_md5_finish_ret( &md5, hash );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001713 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001714
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001715 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
1716 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001717
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001718 mbedtls_md5_free( &md5 );
1719 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001720
Paul Bakker380da532012-04-18 16:10:25 +00001721 return;
1722}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001723#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker380da532012-04-18 16:10:25 +00001724
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001725#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1726#if defined(MBEDTLS_SHA256_C)
1727void ssl_calc_verify_tls_sha256( mbedtls_ssl_context *ssl, unsigned char hash[32] )
Paul Bakker380da532012-04-18 16:10:25 +00001728{
Andrzej Kurekeb342242019-01-29 09:14:33 -05001729#if defined(MBEDTLS_USE_PSA_CRYPTO)
1730 size_t hash_size;
1731 psa_status_t status;
1732 psa_hash_operation_t sha256_psa = psa_hash_operation_init();
1733
1734 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha256" ) );
1735 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
1736 if( status != PSA_SUCCESS )
1737 {
1738 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
1739 return;
1740 }
1741
1742 status = psa_hash_finish( &sha256_psa, hash, 32, &hash_size );
1743 if( status != PSA_SUCCESS )
1744 {
1745 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
1746 return;
1747 }
1748 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, 32 );
1749 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
1750#else
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001751 mbedtls_sha256_context sha256;
Paul Bakker380da532012-04-18 16:10:25 +00001752
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001753 mbedtls_sha256_init( &sha256 );
1754
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001755 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001756
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001757 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001758 mbedtls_sha256_finish_ret( &sha256, hash );
Paul Bakker380da532012-04-18 16:10:25 +00001759
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001760 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 32 );
1761 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001762
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001763 mbedtls_sha256_free( &sha256 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001764#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker380da532012-04-18 16:10:25 +00001765 return;
1766}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001767#endif /* MBEDTLS_SHA256_C */
Paul Bakker380da532012-04-18 16:10:25 +00001768
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001769#if defined(MBEDTLS_SHA512_C)
1770void ssl_calc_verify_tls_sha384( mbedtls_ssl_context *ssl, unsigned char hash[48] )
Paul Bakker380da532012-04-18 16:10:25 +00001771{
Andrzej Kurekeb342242019-01-29 09:14:33 -05001772#if defined(MBEDTLS_USE_PSA_CRYPTO)
1773 size_t hash_size;
1774 psa_status_t status;
Andrzej Kurek972fba52019-01-30 03:29:12 -05001775 psa_hash_operation_t sha384_psa = psa_hash_operation_init();
Andrzej Kurekeb342242019-01-29 09:14:33 -05001776
1777 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha384" ) );
Andrzej Kurek972fba52019-01-30 03:29:12 -05001778 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001779 if( status != PSA_SUCCESS )
1780 {
1781 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
1782 return;
1783 }
1784
Andrzej Kurek972fba52019-01-30 03:29:12 -05001785 status = psa_hash_finish( &sha384_psa, hash, 48, &hash_size );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001786 if( status != PSA_SUCCESS )
1787 {
1788 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
1789 return;
1790 }
1791 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, 48 );
1792 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
1793#else
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001794 mbedtls_sha512_context sha512;
Paul Bakker380da532012-04-18 16:10:25 +00001795
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001796 mbedtls_sha512_init( &sha512 );
1797
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001798 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001799
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001800 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001801 mbedtls_sha512_finish_ret( &sha512, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001802
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001803 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 48 );
1804 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001805
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001806 mbedtls_sha512_free( &sha512 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001807#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker5121ce52009-01-03 21:22:43 +00001808 return;
1809}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001810#endif /* MBEDTLS_SHA512_C */
1811#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001812
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001813#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
1814int 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 +02001815{
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001816 unsigned char *p = ssl->handshake->premaster;
1817 unsigned char *end = p + sizeof( ssl->handshake->premaster );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001818 const unsigned char *psk = ssl->conf->psk;
1819 size_t psk_len = ssl->conf->psk_len;
1820
1821 /* If the psk callback was called, use its result */
1822 if( ssl->handshake->psk != NULL )
1823 {
1824 psk = ssl->handshake->psk;
1825 psk_len = ssl->handshake->psk_len;
1826 }
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001827
1828 /*
1829 * PMS = struct {
1830 * opaque other_secret<0..2^16-1>;
1831 * opaque psk<0..2^16-1>;
1832 * };
1833 * with "other_secret" depending on the particular key exchange
1834 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001835#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
1836 if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001837 {
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001838 if( end - p < 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001839 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001840
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001841 *(p++) = (unsigned char)( psk_len >> 8 );
1842 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001843
1844 if( end < p || (size_t)( end - p ) < psk_len )
1845 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1846
1847 memset( p, 0, psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001848 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001849 }
1850 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001851#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
1852#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
1853 if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001854 {
1855 /*
1856 * other_secret already set by the ClientKeyExchange message,
1857 * and is 48 bytes long
1858 */
Philippe Antoine747fd532018-05-30 09:13:21 +02001859 if( end - p < 2 )
1860 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1861
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001862 *p++ = 0;
1863 *p++ = 48;
1864 p += 48;
1865 }
1866 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001867#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
1868#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
1869 if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001870 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001871 int ret;
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01001872 size_t len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001873
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001874 /* Write length only when we know the actual value */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001875 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01001876 p + 2, end - ( p + 2 ), &len,
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001877 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001878 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001879 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001880 return( ret );
1881 }
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001882 *(p++) = (unsigned char)( len >> 8 );
1883 *(p++) = (unsigned char)( len );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001884 p += len;
1885
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001886 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001887 }
1888 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001889#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
1890#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
1891 if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001892 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001893 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001894 size_t zlen;
1895
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001896 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
Paul Bakker66d5d072014-06-17 16:39:18 +02001897 p + 2, end - ( p + 2 ),
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001898 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001899 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001900 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001901 return( ret );
1902 }
1903
1904 *(p++) = (unsigned char)( zlen >> 8 );
1905 *(p++) = (unsigned char)( zlen );
1906 p += zlen;
1907
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001908 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
1909 MBEDTLS_DEBUG_ECDH_Z );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001910 }
1911 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001912#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001913 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001914 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1915 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001916 }
1917
1918 /* opaque psk<0..2^16-1>; */
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001919 if( end - p < 2 )
1920 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01001921
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001922 *(p++) = (unsigned char)( psk_len >> 8 );
1923 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001924
1925 if( end < p || (size_t)( end - p ) < psk_len )
1926 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1927
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001928 memcpy( p, psk, psk_len );
1929 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001930
1931 ssl->handshake->pmslen = p - ssl->handshake->premaster;
1932
1933 return( 0 );
1934}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001935#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001936
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001937#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00001938/*
1939 * SSLv3.0 MAC functions
1940 */
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001941#define SSL_MAC_MAX_BYTES 20 /* MD-5 or SHA-1 */
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001942static void ssl_mac( mbedtls_md_context_t *md_ctx,
1943 const unsigned char *secret,
1944 const unsigned char *buf, size_t len,
1945 const unsigned char *ctr, int type,
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001946 unsigned char out[SSL_MAC_MAX_BYTES] )
Paul Bakker5121ce52009-01-03 21:22:43 +00001947{
1948 unsigned char header[11];
1949 unsigned char padding[48];
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001950 int padlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001951 int md_size = mbedtls_md_get_size( md_ctx->md_info );
1952 int md_type = mbedtls_md_get_type( md_ctx->md_info );
Paul Bakker68884e32013-01-07 18:20:04 +01001953
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001954 /* Only MD5 and SHA-1 supported */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001955 if( md_type == MBEDTLS_MD_MD5 )
Paul Bakker68884e32013-01-07 18:20:04 +01001956 padlen = 48;
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001957 else
Paul Bakker68884e32013-01-07 18:20:04 +01001958 padlen = 40;
Paul Bakker5121ce52009-01-03 21:22:43 +00001959
1960 memcpy( header, ctr, 8 );
1961 header[ 8] = (unsigned char) type;
1962 header[ 9] = (unsigned char)( len >> 8 );
1963 header[10] = (unsigned char)( len );
1964
Paul Bakker68884e32013-01-07 18:20:04 +01001965 memset( padding, 0x36, 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 );
1969 mbedtls_md_update( md_ctx, header, 11 );
1970 mbedtls_md_update( md_ctx, buf, len );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001971 mbedtls_md_finish( md_ctx, out );
Paul Bakker5121ce52009-01-03 21:22:43 +00001972
Paul Bakker68884e32013-01-07 18:20:04 +01001973 memset( padding, 0x5C, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001974 mbedtls_md_starts( md_ctx );
1975 mbedtls_md_update( md_ctx, secret, md_size );
1976 mbedtls_md_update( md_ctx, padding, padlen );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001977 mbedtls_md_update( md_ctx, out, md_size );
1978 mbedtls_md_finish( md_ctx, out );
Paul Bakker5f70b252012-09-13 14:23:06 +00001979}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001980#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +00001981
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001982/* The function below is only used in the Lucky 13 counter-measure in
Hanno Beckerb2ca87d2018-10-18 15:43:13 +01001983 * mbedtls_ssl_decrypt_buf(). These are the defines that guard the call site. */
Hanno Becker52344c22018-01-03 15:24:20 +00001984#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) && \
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001985 ( defined(MBEDTLS_SSL_PROTO_TLS1) || \
1986 defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1987 defined(MBEDTLS_SSL_PROTO_TLS1_2) )
1988/* This function makes sure every byte in the memory region is accessed
1989 * (in ascending addresses order) */
1990static void ssl_read_memory( unsigned char *p, size_t len )
1991{
1992 unsigned char acc = 0;
1993 volatile unsigned char force;
1994
1995 for( ; len != 0; p++, len-- )
1996 acc ^= *p;
1997
1998 force = acc;
1999 (void) force;
2000}
2001#endif /* SSL_SOME_MODES_USE_MAC && ( TLS1 || TLS1_1 || TLS1_2 ) */
2002
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01002003/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002004 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +02002005 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002006
Hanno Beckera0e20d02019-05-15 14:03:01 +01002007#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerd3f8c792019-05-20 15:06:12 +01002008/* This functions transforms a DTLS plaintext fragment and a record content
2009 * type into an instance of the DTLSInnerPlaintext structure:
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002010 *
2011 * struct {
2012 * opaque content[DTLSPlaintext.length];
2013 * ContentType real_type;
2014 * uint8 zeros[length_of_padding];
2015 * } DTLSInnerPlaintext;
2016 *
2017 * Input:
2018 * - `content`: The beginning of the buffer holding the
2019 * plaintext to be wrapped.
2020 * - `*content_size`: The length of the plaintext in Bytes.
2021 * - `max_len`: The number of Bytes available starting from
2022 * `content`. This must be `>= *content_size`.
2023 * - `rec_type`: The desired record content type.
2024 *
2025 * Output:
2026 * - `content`: The beginning of the resulting DTLSInnerPlaintext structure.
2027 * - `*content_size`: The length of the resulting DTLSInnerPlaintext structure.
2028 *
2029 * Returns:
2030 * - `0` on success.
2031 * - A negative error code if `max_len` didn't offer enough space
2032 * for the expansion.
2033 */
2034static int ssl_cid_build_inner_plaintext( unsigned char *content,
2035 size_t *content_size,
2036 size_t remaining,
2037 uint8_t rec_type )
2038{
2039 size_t len = *content_size;
Hanno Beckerb9ec44f2019-05-13 15:31:17 +01002040 size_t pad = ( MBEDTLS_SSL_CID_PADDING_GRANULARITY -
2041 ( len + 1 ) % MBEDTLS_SSL_CID_PADDING_GRANULARITY ) %
2042 MBEDTLS_SSL_CID_PADDING_GRANULARITY;
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002043
2044 /* Write real content type */
2045 if( remaining == 0 )
2046 return( -1 );
2047 content[ len ] = rec_type;
2048 len++;
2049 remaining--;
2050
2051 if( remaining < pad )
2052 return( -1 );
2053 memset( content + len, 0, pad );
2054 len += pad;
2055 remaining -= pad;
2056
2057 *content_size = len;
2058 return( 0 );
2059}
2060
Hanno Becker07dc97d2019-05-20 15:08:01 +01002061/* This function parses a DTLSInnerPlaintext structure.
2062 * See ssl_cid_build_inner_plaintext() for details. */
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002063static int ssl_cid_parse_inner_plaintext( unsigned char const *content,
2064 size_t *content_size,
2065 uint8_t *rec_type )
2066{
2067 size_t remaining = *content_size;
2068
2069 /* Determine length of padding by skipping zeroes from the back. */
2070 do
2071 {
2072 if( remaining == 0 )
2073 return( -1 );
2074 remaining--;
2075 } while( content[ remaining ] == 0 );
2076
2077 *content_size = remaining;
2078 *rec_type = content[ remaining ];
2079
2080 return( 0 );
2081}
Hanno Beckera0e20d02019-05-15 14:03:01 +01002082#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002083
Hanno Beckerd5aeab12019-05-20 14:50:53 +01002084/* `add_data` must have size 13 Bytes if the CID extension is disabled,
Hanno Beckerc4a190b2019-05-08 18:15:21 +01002085 * and 13 + 1 + CID-length Bytes if the CID extension is enabled. */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002086static void ssl_extract_add_data_from_record( unsigned char* add_data,
Hanno Beckercab87e62019-04-29 13:52:53 +01002087 size_t *add_data_len,
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002088 mbedtls_record *rec )
2089{
Hanno Beckerd5aeab12019-05-20 14:50:53 +01002090 /* Quoting RFC 5246 (TLS 1.2):
Hanno Beckercab87e62019-04-29 13:52:53 +01002091 *
2092 * additional_data = seq_num + TLSCompressed.type +
2093 * TLSCompressed.version + TLSCompressed.length;
2094 *
Hanno Beckerd5aeab12019-05-20 14:50:53 +01002095 * For the CID extension, this is extended as follows
2096 * (quoting draft-ietf-tls-dtls-connection-id-05,
2097 * https://tools.ietf.org/html/draft-ietf-tls-dtls-connection-id-05):
Hanno Beckercab87e62019-04-29 13:52:53 +01002098 *
2099 * additional_data = seq_num + DTLSPlaintext.type +
2100 * DTLSPlaintext.version +
Hanno Beckerd5aeab12019-05-20 14:50:53 +01002101 * cid +
2102 * cid_length +
Hanno Beckercab87e62019-04-29 13:52:53 +01002103 * length_of_DTLSInnerPlaintext;
2104 */
2105
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002106 memcpy( add_data, rec->ctr, sizeof( rec->ctr ) );
2107 add_data[8] = rec->type;
Hanno Beckeredb24f82019-05-20 15:01:46 +01002108 memcpy( add_data + 9, rec->ver, sizeof( rec->ver ) );
Hanno Beckercab87e62019-04-29 13:52:53 +01002109
Hanno Beckera0e20d02019-05-15 14:03:01 +01002110#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker95e4bbc2019-05-09 11:38:24 +01002111 if( rec->cid_len != 0 )
2112 {
2113 memcpy( add_data + 11, rec->cid, rec->cid_len );
2114 add_data[11 + rec->cid_len + 0] = rec->cid_len;
2115 add_data[11 + rec->cid_len + 1] = ( rec->data_len >> 8 ) & 0xFF;
2116 add_data[11 + rec->cid_len + 2] = ( rec->data_len >> 0 ) & 0xFF;
2117 *add_data_len = 13 + 1 + rec->cid_len;
2118 }
2119 else
Hanno Beckera0e20d02019-05-15 14:03:01 +01002120#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker95e4bbc2019-05-09 11:38:24 +01002121 {
2122 add_data[11 + 0] = ( rec->data_len >> 8 ) & 0xFF;
2123 add_data[11 + 1] = ( rec->data_len >> 0 ) & 0xFF;
2124 *add_data_len = 13;
2125 }
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002126}
2127
Hanno Beckera18d1322018-01-03 14:27:32 +00002128int mbedtls_ssl_encrypt_buf( mbedtls_ssl_context *ssl,
2129 mbedtls_ssl_transform *transform,
2130 mbedtls_record *rec,
2131 int (*f_rng)(void *, unsigned char *, size_t),
2132 void *p_rng )
Paul Bakker5121ce52009-01-03 21:22:43 +00002133{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002134 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002135 int auth_done = 0;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002136 unsigned char * data;
Hanno Becker92fb4fa2019-05-20 14:54:26 +01002137 unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_OUT_LEN_MAX ];
Hanno Beckercab87e62019-04-29 13:52:53 +01002138 size_t add_data_len;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002139 size_t post_avail;
2140
2141 /* The SSL context is only used for debugging purposes! */
Hanno Beckera18d1322018-01-03 14:27:32 +00002142#if !defined(MBEDTLS_DEBUG_C)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002143 ((void) ssl);
2144#endif
2145
2146 /* The PRNG is used for dynamic IV generation that's used
2147 * for CBC transformations in TLS 1.1 and TLS 1.2. */
2148#if !( defined(MBEDTLS_CIPHER_MODE_CBC) && \
2149 ( defined(MBEDTLS_AES_C) || \
2150 defined(MBEDTLS_ARIA_C) || \
2151 defined(MBEDTLS_CAMELLIA_C) ) && \
2152 ( defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2) ) )
2153 ((void) f_rng);
2154 ((void) p_rng);
2155#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002156
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002157 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002158
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002159 if( transform == NULL )
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002160 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002161 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no transform provided to encrypt_buf" ) );
2162 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2163 }
Hanno Becker43c24b82019-05-01 09:45:57 +01002164 if( rec == NULL
2165 || rec->buf == NULL
2166 || rec->buf_len < rec->data_offset
2167 || rec->buf_len - rec->data_offset < rec->data_len
Hanno Beckera0e20d02019-05-15 14:03:01 +01002168#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker43c24b82019-05-01 09:45:57 +01002169 || rec->cid_len != 0
2170#endif
2171 )
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002172 {
2173 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to encrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002174 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002175 }
2176
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002177 data = rec->buf + rec->data_offset;
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002178 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002179 MBEDTLS_SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002180 data, rec->data_len );
2181
2182 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc );
2183
2184 if( rec->data_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
2185 {
2186 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record content %u too large, maximum %d",
2187 (unsigned) rec->data_len,
2188 MBEDTLS_SSL_OUT_CONTENT_LEN ) );
2189 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2190 }
Manuel Pégourié-Gonnard60346be2014-11-21 11:38:37 +01002191
Hanno Beckera0e20d02019-05-15 14:03:01 +01002192#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckercab87e62019-04-29 13:52:53 +01002193 /*
2194 * Add CID information
2195 */
2196 rec->cid_len = transform->out_cid_len;
2197 memcpy( rec->cid, transform->out_cid, transform->out_cid_len );
2198 MBEDTLS_SSL_DEBUG_BUF( 3, "CID", rec->cid, rec->cid_len );
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002199
2200 if( rec->cid_len != 0 )
2201 {
2202 /*
Hanno Becker07dc97d2019-05-20 15:08:01 +01002203 * Wrap plaintext into DTLSInnerPlaintext structure.
2204 * See ssl_cid_build_inner_plaintext() for more information.
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002205 *
Hanno Becker07dc97d2019-05-20 15:08:01 +01002206 * Note that this changes `rec->data_len`, and hence
2207 * `post_avail` needs to be recalculated afterwards.
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002208 */
2209 if( ssl_cid_build_inner_plaintext( data,
2210 &rec->data_len,
2211 post_avail,
2212 rec->type ) != 0 )
2213 {
2214 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2215 }
2216
2217 rec->type = MBEDTLS_SSL_MSG_CID;
2218 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01002219#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckercab87e62019-04-29 13:52:53 +01002220
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002221 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
2222
Paul Bakker5121ce52009-01-03 21:22:43 +00002223 /*
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01002224 * Add MAC before if needed
Paul Bakker5121ce52009-01-03 21:22:43 +00002225 */
Hanno Becker52344c22018-01-03 15:24:20 +00002226#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002227 if( mode == MBEDTLS_MODE_STREAM ||
2228 ( mode == MBEDTLS_MODE_CBC
2229#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002230 && transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002231#endif
2232 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00002233 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002234 if( post_avail < transform->maclen )
2235 {
2236 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2237 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2238 }
2239
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002240#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002241 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002242 {
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01002243 unsigned char mac[SSL_MAC_MAX_BYTES];
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002244 ssl_mac( &transform->md_ctx_enc, transform->mac_enc,
2245 data, rec->data_len, rec->ctr, rec->type, mac );
2246 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002247 }
2248 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002249#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002250#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2251 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002252 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002253 {
Hanno Becker992b6872017-11-09 18:57:39 +00002254 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
2255
Hanno Beckercab87e62019-04-29 13:52:53 +01002256 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker992b6872017-11-09 18:57:39 +00002257
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002258 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
Hanno Beckercab87e62019-04-29 13:52:53 +01002259 add_data_len );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002260 mbedtls_md_hmac_update( &transform->md_ctx_enc,
2261 data, rec->data_len );
2262 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
2263 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
2264
2265 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002266 }
2267 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002268#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002269 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002270 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2271 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002272 }
2273
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002274 MBEDTLS_SSL_DEBUG_BUF( 4, "computed mac", data + rec->data_len,
2275 transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002276
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002277 rec->data_len += transform->maclen;
2278 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002279 auth_done++;
Paul Bakker577e0062013-08-28 11:57:20 +02002280 }
Hanno Becker52344c22018-01-03 15:24:20 +00002281#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00002282
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002283 /*
2284 * Encrypt
2285 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002286#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
2287 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00002288 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002289 int ret;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002290 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002291 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002292 "including %d bytes of padding",
2293 rec->data_len, 0 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002294
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002295 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
2296 transform->iv_enc, transform->ivlen,
2297 data, rec->data_len,
2298 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002299 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002300 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002301 return( ret );
2302 }
2303
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002304 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002305 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002306 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2307 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002308 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002309 }
Paul Bakker68884e32013-01-07 18:20:04 +01002310 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002311#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002312
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002313#if defined(MBEDTLS_GCM_C) || \
2314 defined(MBEDTLS_CCM_C) || \
2315 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002316 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002317 mode == MBEDTLS_MODE_CCM ||
2318 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002319 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02002320 int ret;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002321 unsigned char iv[12];
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002322 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002323
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002324 /* Check that there's space for both the authentication tag
2325 * and the explicit IV before and after the record content. */
2326 if( post_avail < transform->taglen ||
2327 rec->data_offset < explicit_iv_len )
2328 {
2329 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2330 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2331 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00002332
Paul Bakker68884e32013-01-07 18:20:04 +01002333 /*
2334 * Generate IV
2335 */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002336 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
2337 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002338 /* GCM and CCM: fixed || explicit (=seqnum) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002339 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002340 memcpy( iv + transform->fixed_ivlen, rec->ctr,
2341 explicit_iv_len );
2342 /* Prefix record content with explicit IV. */
2343 memcpy( data - explicit_iv_len, rec->ctr, explicit_iv_len );
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002344 }
2345 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
2346 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002347 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002348 unsigned char i;
2349
2350 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
2351
2352 for( i = 0; i < 8; i++ )
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002353 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002354 }
2355 else
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01002356 {
2357 /* Reminder if we ever add an AEAD mode with a different size */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002358 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2359 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01002360 }
2361
Hanno Beckercab87e62019-04-29 13:52:53 +01002362 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker1f10d762019-04-26 13:34:37 +01002363
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002364 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (internal)",
2365 iv, transform->ivlen );
2366 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (transmitted)",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002367 data - explicit_iv_len, explicit_iv_len );
2368 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Hanno Beckercab87e62019-04-29 13:52:53 +01002369 add_data, add_data_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002370 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002371 "including 0 bytes of padding",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002372 rec->data_len ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00002373
Paul Bakker68884e32013-01-07 18:20:04 +01002374 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002375 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002376 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002377
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002378 if( ( ret = mbedtls_cipher_auth_encrypt( &transform->cipher_ctx_enc,
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002379 iv, transform->ivlen,
Hanno Beckercab87e62019-04-29 13:52:53 +01002380 add_data, add_data_len, /* add data */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002381 data, rec->data_len, /* source */
2382 data, &rec->data_len, /* destination */
2383 data + rec->data_len, transform->taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002384 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002385 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_encrypt", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002386 return( ret );
2387 }
2388
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002389 MBEDTLS_SSL_DEBUG_BUF( 4, "after encrypt: tag",
2390 data + rec->data_len, transform->taglen );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002391
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002392 rec->data_len += transform->taglen + explicit_iv_len;
2393 rec->data_offset -= explicit_iv_len;
2394 post_avail -= transform->taglen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002395 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002396 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002397 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002398#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2399#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002400 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002401 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00002402 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002403 int ret;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002404 size_t padlen, i;
2405 size_t olen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002406
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002407 /* Currently we're always using minimal padding
2408 * (up to 255 bytes would be allowed). */
2409 padlen = transform->ivlen - ( rec->data_len + 1 ) % transform->ivlen;
2410 if( padlen == transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002411 padlen = 0;
2412
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002413 /* Check there's enough space in the buffer for the padding. */
2414 if( post_avail < padlen + 1 )
2415 {
2416 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2417 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2418 }
2419
Paul Bakker5121ce52009-01-03 21:22:43 +00002420 for( i = 0; i <= padlen; i++ )
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002421 data[rec->data_len + i] = (unsigned char) padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002422
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002423 rec->data_len += padlen + 1;
2424 post_avail -= padlen + 1;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002425
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002426#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002427 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00002428 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
2429 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002430 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002431 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002432 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002433 if( f_rng == NULL )
2434 {
2435 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No PRNG provided to encrypt_record routine" ) );
2436 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2437 }
2438
2439 if( rec->data_offset < transform->ivlen )
2440 {
2441 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2442 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2443 }
2444
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002445 /*
2446 * Generate IV
2447 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002448 ret = f_rng( p_rng, transform->iv_enc, transform->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00002449 if( ret != 0 )
2450 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002451
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002452 memcpy( data - transform->ivlen, transform->iv_enc,
2453 transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002454
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002455 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002456#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002457
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002458 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002459 "including %d bytes of IV and %d bytes of padding",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002460 rec->data_len, transform->ivlen,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002461 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002462
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002463 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
2464 transform->iv_enc,
2465 transform->ivlen,
2466 data, rec->data_len,
2467 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002468 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002469 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002470 return( ret );
2471 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002472
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002473 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02002474 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002475 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2476 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002477 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002478
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002479#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002480 if( transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02002481 {
2482 /*
2483 * Save IV in SSL3 and TLS1
2484 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002485 memcpy( transform->iv_enc, transform->cipher_ctx_enc.iv,
2486 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002487 }
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002488 else
Paul Bakkercca5b812013-08-31 17:40:26 +02002489#endif
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002490 {
2491 data -= transform->ivlen;
2492 rec->data_offset -= transform->ivlen;
2493 rec->data_len += transform->ivlen;
2494 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002495
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002496#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002497 if( auth_done == 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002498 {
Hanno Becker3d8c9072018-01-05 16:24:22 +00002499 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
2500
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002501 /*
2502 * MAC(MAC_write_key, seq_num +
2503 * TLSCipherText.type +
2504 * TLSCipherText.version +
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002505 * length_of( (IV +) ENC(...) ) +
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002506 * IV + // except for TLS 1.0
2507 * ENC(content + padding + padding_length));
2508 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002509
2510 if( post_avail < transform->maclen)
2511 {
2512 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2513 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2514 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002515
Hanno Beckercab87e62019-04-29 13:52:53 +01002516 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker1f10d762019-04-26 13:34:37 +01002517
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002518 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002519 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
Hanno Beckercab87e62019-04-29 13:52:53 +01002520 add_data_len );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002521
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002522 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
Hanno Beckercab87e62019-04-29 13:52:53 +01002523 add_data_len );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002524 mbedtls_md_hmac_update( &transform->md_ctx_enc,
2525 data, rec->data_len );
2526 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
2527 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002528
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002529 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002530
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002531 rec->data_len += transform->maclen;
2532 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002533 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002534 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002535#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00002536 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002537 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002538#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002539 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002540 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002541 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2542 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002543 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002544
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002545 /* Make extra sure authentication was performed, exactly once */
2546 if( auth_done != 1 )
2547 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002548 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2549 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002550 }
2551
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002552 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002553
2554 return( 0 );
2555}
2556
Hanno Becker605949f2019-07-12 08:23:59 +01002557int mbedtls_ssl_decrypt_buf( mbedtls_ssl_context const *ssl,
Hanno Beckera18d1322018-01-03 14:27:32 +00002558 mbedtls_ssl_transform *transform,
2559 mbedtls_record *rec )
Paul Bakker5121ce52009-01-03 21:22:43 +00002560{
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002561 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002562 mbedtls_cipher_mode_t mode;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002563 int ret, auth_done = 0;
Hanno Becker52344c22018-01-03 15:24:20 +00002564#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Paul Bakker1e5369c2013-12-19 16:40:57 +01002565 size_t padlen = 0, correct = 1;
2566#endif
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002567 unsigned char* data;
Hanno Becker92fb4fa2019-05-20 14:54:26 +01002568 unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_IN_LEN_MAX ];
Hanno Beckercab87e62019-04-29 13:52:53 +01002569 size_t add_data_len;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002570
Hanno Beckera18d1322018-01-03 14:27:32 +00002571#if !defined(MBEDTLS_DEBUG_C)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002572 ((void) ssl);
2573#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002574
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002575 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002576 if( rec == NULL ||
2577 rec->buf == NULL ||
2578 rec->buf_len < rec->data_offset ||
2579 rec->buf_len - rec->data_offset < rec->data_len )
2580 {
2581 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to decrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002582 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002583 }
2584
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002585 data = rec->buf + rec->data_offset;
2586 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_dec );
Paul Bakker5121ce52009-01-03 21:22:43 +00002587
Hanno Beckera0e20d02019-05-15 14:03:01 +01002588#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckercab87e62019-04-29 13:52:53 +01002589 /*
2590 * Match record's CID with incoming CID.
2591 */
Hanno Becker938489a2019-05-08 13:02:22 +01002592 if( rec->cid_len != transform->in_cid_len ||
2593 memcmp( rec->cid, transform->in_cid, rec->cid_len ) != 0 )
2594 {
Hanno Becker8367ccc2019-05-14 11:30:10 +01002595 return( MBEDTLS_ERR_SSL_UNEXPECTED_CID );
Hanno Becker938489a2019-05-08 13:02:22 +01002596 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01002597#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckercab87e62019-04-29 13:52:53 +01002598
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002599#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
2600 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01002601 {
2602 padlen = 0;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002603 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
2604 transform->iv_dec,
2605 transform->ivlen,
2606 data, rec->data_len,
2607 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002608 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002609 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002610 return( ret );
2611 }
2612
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002613 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002614 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002615 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2616 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002617 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002618 }
Paul Bakker68884e32013-01-07 18:20:04 +01002619 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002620#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002621#if defined(MBEDTLS_GCM_C) || \
2622 defined(MBEDTLS_CCM_C) || \
2623 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002624 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002625 mode == MBEDTLS_MODE_CCM ||
2626 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002627 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002628 unsigned char iv[12];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002629 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002630
Hanno Beckerd96a6522019-07-10 13:55:25 +01002631 /*
2632 * Prepare IV from explicit and implicit data.
2633 */
2634
2635 /* Check that there's enough space for the explicit IV
2636 * (at the beginning of the record) and the MAC (at the
2637 * end of the record). */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002638 if( rec->data_len < explicit_iv_len + transform->taglen )
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002639 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002640 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002641 "+ taglen (%d)", rec->data_len,
2642 explicit_iv_len, transform->taglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002643 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002644 }
Paul Bakker68884e32013-01-07 18:20:04 +01002645
Hanno Beckerd96a6522019-07-10 13:55:25 +01002646#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CCM_C)
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002647 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
2648 {
Hanno Beckerd96a6522019-07-10 13:55:25 +01002649 /* GCM and CCM: fixed || explicit */
Paul Bakker68884e32013-01-07 18:20:04 +01002650
Hanno Beckerd96a6522019-07-10 13:55:25 +01002651 /* Fixed */
2652 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
2653 /* Explicit */
2654 memcpy( iv + transform->fixed_ivlen, data, 8 );
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002655 }
Hanno Beckerd96a6522019-07-10 13:55:25 +01002656 else
2657#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2658#if defined(MBEDTLS_CHACHAPOLY_C)
2659 if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002660 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002661 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002662 unsigned char i;
2663
2664 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
2665
2666 for( i = 0; i < 8; i++ )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002667 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002668 }
2669 else
Hanno Beckerd96a6522019-07-10 13:55:25 +01002670#endif /* MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002671 {
2672 /* Reminder if we ever add an AEAD mode with a different size */
2673 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2674 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2675 }
2676
Hanno Beckerd96a6522019-07-10 13:55:25 +01002677 /* Group changes to data, data_len, and add_data, because
2678 * add_data depends on data_len. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002679 data += explicit_iv_len;
2680 rec->data_offset += explicit_iv_len;
2681 rec->data_len -= explicit_iv_len + transform->taglen;
2682
Hanno Beckercab87e62019-04-29 13:52:53 +01002683 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002684 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Hanno Beckercab87e62019-04-29 13:52:53 +01002685 add_data, add_data_len );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002686
Hanno Beckerd96a6522019-07-10 13:55:25 +01002687 /* Because of the check above, we know that there are
2688 * explicit_iv_len Bytes preceeding data, and taglen
2689 * bytes following data + data_len. This justifies
Hanno Becker20016652019-07-10 11:44:13 +01002690 * the debug message and the invocation of
Hanno Beckerd96a6522019-07-10 13:55:25 +01002691 * mbedtls_cipher_auth_decrypt() below. */
2692
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002693 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used", iv, transform->ivlen );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002694 MBEDTLS_SSL_DEBUG_BUF( 4, "TAG used", data + rec->data_len,
Hanno Beckere694c3e2017-12-27 21:34:08 +00002695 transform->taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01002696
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002697 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002698 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002699 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002700 if( ( ret = mbedtls_cipher_auth_decrypt( &transform->cipher_ctx_dec,
2701 iv, transform->ivlen,
Hanno Beckercab87e62019-04-29 13:52:53 +01002702 add_data, add_data_len,
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002703 data, rec->data_len,
2704 data, &olen,
2705 data + rec->data_len,
2706 transform->taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002707 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002708 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_decrypt", ret );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002709
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002710 if( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED )
2711 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002712
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002713 return( ret );
2714 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002715 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002716
Hanno Beckerd96a6522019-07-10 13:55:25 +01002717 /* Double-check that AEAD decryption doesn't change content length. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002718 if( olen != rec->data_len )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002719 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002720 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2721 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002722 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00002723 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002724 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002725#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2726#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002727 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002728 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00002729 {
Paul Bakkere47b34b2013-02-27 14:48:00 +01002730 size_t minlen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002731
Paul Bakker5121ce52009-01-03 21:22:43 +00002732 /*
Paul Bakker45829992013-01-03 14:52:21 +01002733 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00002734 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002735#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002736 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
2737 {
2738 /* The ciphertext is prefixed with the CBC IV. */
2739 minlen += transform->ivlen;
2740 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002741#endif
Paul Bakker45829992013-01-03 14:52:21 +01002742
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002743 /* Size considerations:
2744 *
2745 * - The CBC cipher text must not be empty and hence
2746 * at least of size transform->ivlen.
2747 *
2748 * Together with the potential IV-prefix, this explains
2749 * the first of the two checks below.
2750 *
2751 * - The record must contain a MAC, either in plain or
2752 * encrypted, depending on whether Encrypt-then-MAC
2753 * is used or not.
2754 * - If it is, the message contains the IV-prefix,
2755 * the CBC ciphertext, and the MAC.
2756 * - If it is not, the padded plaintext, and hence
2757 * the CBC ciphertext, has at least length maclen + 1
2758 * because there is at least the padding length byte.
2759 *
2760 * As the CBC ciphertext is not empty, both cases give the
2761 * lower bound minlen + maclen + 1 on the record size, which
2762 * we test for in the second check below.
2763 */
2764 if( rec->data_len < minlen + transform->ivlen ||
2765 rec->data_len < minlen + transform->maclen + 1 )
Paul Bakker45829992013-01-03 14:52:21 +01002766 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002767 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002768 "+ 1 ) ( + expl IV )", rec->data_len,
2769 transform->ivlen,
2770 transform->maclen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002771 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker45829992013-01-03 14:52:21 +01002772 }
2773
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002774 /*
2775 * Authenticate before decrypt if enabled
2776 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002777#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002778 if( transform->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002779 {
Hanno Becker992b6872017-11-09 18:57:39 +00002780 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002781
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002782 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002783
Hanno Beckerd96a6522019-07-10 13:55:25 +01002784 /* Update data_len in tandem with add_data.
2785 *
2786 * The subtraction is safe because of the previous check
2787 * data_len >= minlen + maclen + 1.
2788 *
2789 * Afterwards, we know that data + data_len is followed by at
2790 * least maclen Bytes, which justifies the call to
2791 * mbedtls_ssl_safer_memcmp() below.
2792 *
2793 * Further, we still know that data_len > minlen */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002794 rec->data_len -= transform->maclen;
Hanno Beckercab87e62019-04-29 13:52:53 +01002795 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002796
Hanno Beckerd96a6522019-07-10 13:55:25 +01002797 /* Calculate expected MAC. */
Hanno Beckercab87e62019-04-29 13:52:53 +01002798 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
2799 add_data_len );
2800 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data,
2801 add_data_len );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002802 mbedtls_md_hmac_update( &transform->md_ctx_dec,
2803 data, rec->data_len );
2804 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
2805 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002806
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002807 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len,
2808 transform->maclen );
Hanno Becker992b6872017-11-09 18:57:39 +00002809 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect,
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002810 transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002811
Hanno Beckerd96a6522019-07-10 13:55:25 +01002812 /* Compare expected MAC with MAC at the end of the record. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002813 if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect,
2814 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002815 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002816 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002817 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002818 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002819 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002820 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002821#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002822
2823 /*
2824 * Check length sanity
2825 */
Hanno Beckerd96a6522019-07-10 13:55:25 +01002826
2827 /* We know from above that data_len > minlen >= 0,
2828 * so the following check in particular implies that
2829 * data_len >= minlen + ivlen ( = minlen or 2 * minlen ). */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002830 if( rec->data_len % transform->ivlen != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002831 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002832 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002833 rec->data_len, transform->ivlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002834 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002835 }
2836
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002837#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002838 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00002839 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002840 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002841 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002842 {
Hanno Beckerd96a6522019-07-10 13:55:25 +01002843 /* Safe because data_len >= minlen + ivlen = 2 * ivlen. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002844 memcpy( transform->iv_dec, data, transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002845
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002846 data += transform->ivlen;
2847 rec->data_offset += transform->ivlen;
2848 rec->data_len -= transform->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002849 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002850#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002851
Hanno Beckerd96a6522019-07-10 13:55:25 +01002852 /* We still have data_len % ivlen == 0 and data_len >= ivlen here. */
2853
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002854 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
2855 transform->iv_dec, transform->ivlen,
2856 data, rec->data_len, data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002857 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002858 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002859 return( ret );
2860 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002861
Hanno Beckerd96a6522019-07-10 13:55:25 +01002862 /* Double-check that length hasn't changed during decryption. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002863 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02002864 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002865 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2866 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002867 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002868
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002869#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002870 if( transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02002871 {
2872 /*
Hanno Beckerd96a6522019-07-10 13:55:25 +01002873 * Save IV in SSL3 and TLS1, where CBC decryption of consecutive
2874 * records is equivalent to CBC decryption of the concatenation
2875 * of the records; in other words, IVs are maintained across
2876 * record decryptions.
Paul Bakkercca5b812013-08-31 17:40:26 +02002877 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002878 memcpy( transform->iv_dec, transform->cipher_ctx_dec.iv,
2879 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002880 }
Paul Bakkercca5b812013-08-31 17:40:26 +02002881#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002882
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002883 /* Safe since data_len >= minlen + maclen + 1, so after having
2884 * subtracted at most minlen and maclen up to this point,
Hanno Beckerd96a6522019-07-10 13:55:25 +01002885 * data_len > 0 (because of data_len % ivlen == 0, it's actually
2886 * >= ivlen ). */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002887 padlen = data[rec->data_len - 1];
Paul Bakker45829992013-01-03 14:52:21 +01002888
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002889 if( auth_done == 1 )
2890 {
2891 correct *= ( rec->data_len >= padlen + 1 );
2892 padlen *= ( rec->data_len >= padlen + 1 );
2893 }
2894 else
Paul Bakker45829992013-01-03 14:52:21 +01002895 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002896#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002897 if( rec->data_len < transform->maclen + padlen + 1 )
2898 {
2899 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
2900 rec->data_len,
2901 transform->maclen,
2902 padlen + 1 ) );
2903 }
Paul Bakkerd66f0702013-01-31 16:57:45 +01002904#endif
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002905
2906 correct *= ( rec->data_len >= transform->maclen + padlen + 1 );
2907 padlen *= ( rec->data_len >= transform->maclen + padlen + 1 );
Paul Bakker45829992013-01-03 14:52:21 +01002908 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002909
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002910 padlen++;
2911
2912 /* Regardless of the validity of the padding,
2913 * we have data_len >= padlen here. */
2914
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002915#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002916 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002917 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002918 if( padlen > transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002919 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002920#if defined(MBEDTLS_SSL_DEBUG_ALL)
2921 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002922 "should be no more than %d",
2923 padlen, transform->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002924#endif
Paul Bakker45829992013-01-03 14:52:21 +01002925 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002926 }
2927 }
2928 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002929#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2930#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2931 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002932 if( transform->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002933 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002934 /* The padding check involves a series of up to 256
2935 * consecutive memory reads at the end of the record
2936 * plaintext buffer. In order to hide the length and
2937 * validity of the padding, always perform exactly
2938 * `min(256,plaintext_len)` reads (but take into account
2939 * only the last `padlen` bytes for the padding check). */
2940 size_t pad_count = 0;
2941 size_t real_count = 0;
2942 volatile unsigned char* const check = data;
Paul Bakkere47b34b2013-02-27 14:48:00 +01002943
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002944 /* Index of first padding byte; it has been ensured above
2945 * that the subtraction is safe. */
2946 size_t const padding_idx = rec->data_len - padlen;
2947 size_t const num_checks = rec->data_len <= 256 ? rec->data_len : 256;
2948 size_t const start_idx = rec->data_len - num_checks;
2949 size_t idx;
Paul Bakker956c9e02013-12-19 14:42:28 +01002950
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002951 for( idx = start_idx; idx < rec->data_len; idx++ )
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002952 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002953 real_count |= ( idx >= padding_idx );
2954 pad_count += real_count * ( check[idx] == padlen - 1 );
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002955 }
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002956 correct &= ( pad_count == padlen );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002957
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002958#if defined(MBEDTLS_SSL_DEBUG_ALL)
Paul Bakker66d5d072014-06-17 16:39:18 +02002959 if( padlen > 0 && correct == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002960 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002961#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01002962 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00002963 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002964 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002965#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
2966 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02002967 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002968 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2969 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02002970 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002971
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002972 /* If the padding was found to be invalid, padlen == 0
2973 * and the subtraction is safe. If the padding was found valid,
2974 * padlen hasn't been changed and the previous assertion
2975 * data_len >= padlen still holds. */
2976 rec->data_len -= padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002977 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002978 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002979#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002980 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002981 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002982 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2983 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002984 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002985
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02002986#if defined(MBEDTLS_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002987 MBEDTLS_SSL_DEBUG_BUF( 4, "raw buffer after decryption",
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002988 data, rec->data_len );
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02002989#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002990
2991 /*
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002992 * Authenticate if not done yet.
2993 * Compute the MAC regardless of the padding result (RFC4346, CBCTIME).
Paul Bakker5121ce52009-01-03 21:22:43 +00002994 */
Hanno Becker52344c22018-01-03 15:24:20 +00002995#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002996 if( auth_done == 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002997 {
Hanno Becker992b6872017-11-09 18:57:39 +00002998 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Paul Bakker1e5369c2013-12-19 16:40:57 +01002999
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003000 /* If the initial value of padlen was such that
3001 * data_len < maclen + padlen + 1, then padlen
3002 * got reset to 1, and the initial check
3003 * data_len >= minlen + maclen + 1
3004 * guarantees that at this point we still
3005 * have at least data_len >= maclen.
3006 *
3007 * If the initial value of padlen was such that
3008 * data_len >= maclen + padlen + 1, then we have
3009 * subtracted either padlen + 1 (if the padding was correct)
3010 * or 0 (if the padding was incorrect) since then,
3011 * hence data_len >= maclen in any case.
3012 */
3013 rec->data_len -= transform->maclen;
Hanno Beckercab87e62019-04-29 13:52:53 +01003014 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Paul Bakker5121ce52009-01-03 21:22:43 +00003015
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003016#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003017 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003018 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003019 ssl_mac( &transform->md_ctx_dec,
3020 transform->mac_dec,
3021 data, rec->data_len,
3022 rec->ctr, rec->type,
3023 mac_expect );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003024 }
3025 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003026#endif /* MBEDTLS_SSL_PROTO_SSL3 */
3027#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
3028 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003029 if( transform->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003030 {
3031 /*
3032 * Process MAC and always update for padlen afterwards to make
Gilles Peskine20b44082018-05-29 14:06:49 +02003033 * total time independent of padlen.
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003034 *
3035 * Known timing attacks:
3036 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
3037 *
Gilles Peskine20b44082018-05-29 14:06:49 +02003038 * To compensate for different timings for the MAC calculation
3039 * depending on how much padding was removed (which is determined
3040 * by padlen), process extra_run more blocks through the hash
3041 * function.
3042 *
3043 * The formula in the paper is
3044 * extra_run = ceil( (L1-55) / 64 ) - ceil( (L2-55) / 64 )
3045 * where L1 is the size of the header plus the decrypted message
3046 * plus CBC padding and L2 is the size of the header plus the
3047 * decrypted message. This is for an underlying hash function
3048 * with 64-byte blocks.
3049 * We use ( (Lx+8) / 64 ) to handle 'negative Lx' values
3050 * correctly. We round down instead of up, so -56 is the correct
3051 * value for our calculations instead of -55.
3052 *
Gilles Peskine1bd9d582018-06-04 11:58:44 +02003053 * Repeat the formula rather than defining a block_size variable.
3054 * This avoids requiring division by a variable at runtime
3055 * (which would be marginally less efficient and would require
3056 * linking an extra division function in some builds).
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003057 */
3058 size_t j, extra_run = 0;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003059 unsigned char tmp[MBEDTLS_MD_MAX_BLOCK_SIZE];
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003060
3061 /*
3062 * The next two sizes are the minimum and maximum values of
3063 * in_msglen over all padlen values.
3064 *
3065 * They're independent of padlen, since we previously did
Hanno Beckerd96a6522019-07-10 13:55:25 +01003066 * data_len -= padlen.
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003067 *
3068 * Note that max_len + maclen is never more than the buffer
3069 * length, as we previously did in_msglen -= maclen too.
3070 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003071 const size_t max_len = rec->data_len + padlen;
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003072 const size_t min_len = ( max_len > 256 ) ? max_len - 256 : 0;
3073
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003074 memset( tmp, 0, sizeof( tmp ) );
3075
3076 switch( mbedtls_md_get_type( transform->md_ctx_dec.md_info ) )
Gilles Peskine20b44082018-05-29 14:06:49 +02003077 {
Gilles Peskined0e55a42018-06-04 12:03:30 +02003078#if defined(MBEDTLS_MD5_C) || defined(MBEDTLS_SHA1_C) || \
3079 defined(MBEDTLS_SHA256_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02003080 case MBEDTLS_MD_MD5:
3081 case MBEDTLS_MD_SHA1:
Gilles Peskine20b44082018-05-29 14:06:49 +02003082 case MBEDTLS_MD_SHA256:
Gilles Peskine20b44082018-05-29 14:06:49 +02003083 /* 8 bytes of message size, 64-byte compression blocks */
Hanno Beckercab87e62019-04-29 13:52:53 +01003084 extra_run =
3085 ( add_data_len + rec->data_len + padlen + 8 ) / 64 -
3086 ( add_data_len + rec->data_len + 8 ) / 64;
Gilles Peskine20b44082018-05-29 14:06:49 +02003087 break;
3088#endif
Gilles Peskinea7fe25d2018-06-04 12:01:18 +02003089#if defined(MBEDTLS_SHA512_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02003090 case MBEDTLS_MD_SHA384:
Gilles Peskine20b44082018-05-29 14:06:49 +02003091 /* 16 bytes of message size, 128-byte compression blocks */
Hanno Beckercab87e62019-04-29 13:52:53 +01003092 extra_run =
3093 ( add_data_len + rec->data_len + padlen + 16 ) / 128 -
3094 ( add_data_len + rec->data_len + 16 ) / 128;
Gilles Peskine20b44082018-05-29 14:06:49 +02003095 break;
3096#endif
3097 default:
Gilles Peskine5c389842018-06-04 12:02:43 +02003098 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Gilles Peskine20b44082018-05-29 14:06:49 +02003099 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3100 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01003101
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003102 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01003103
Hanno Beckercab87e62019-04-29 13:52:53 +01003104 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data,
3105 add_data_len );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003106 mbedtls_md_hmac_update( &transform->md_ctx_dec, data,
3107 rec->data_len );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003108 /* Make sure we access everything even when padlen > 0. This
3109 * makes the synchronisation requirements for just-in-time
3110 * Prime+Probe attacks much tighter and hopefully impractical. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003111 ssl_read_memory( data + rec->data_len, padlen );
3112 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003113
3114 /* Call mbedtls_md_process at least once due to cache attacks
3115 * that observe whether md_process() was called of not */
Manuel Pégourié-Gonnard47fede02015-04-29 01:35:48 +02003116 for( j = 0; j < extra_run + 1; j++ )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003117 mbedtls_md_process( &transform->md_ctx_dec, tmp );
Paul Bakkere47b34b2013-02-27 14:48:00 +01003118
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003119 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003120
3121 /* Make sure we access all the memory that could contain the MAC,
3122 * before we check it in the next code block. This makes the
3123 * synchronisation requirements for just-in-time Prime+Probe
3124 * attacks much tighter and hopefully impractical. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003125 ssl_read_memory( data + min_len,
3126 max_len - min_len + transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003127 }
3128 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003129#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
3130 MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003131 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003132 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3133 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003134 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003135
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003136#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003137 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect, transform->maclen );
3138 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len, transform->maclen );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003139#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003140
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003141 if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect,
3142 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003143 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003144#if defined(MBEDTLS_SSL_DEBUG_ALL)
3145 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01003146#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003147 correct = 0;
3148 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003149 auth_done++;
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003150 }
Hanno Beckerdd3ab132018-10-17 14:43:14 +01003151
3152 /*
3153 * Finally check the correct flag
3154 */
3155 if( correct == 0 )
3156 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Hanno Becker52344c22018-01-03 15:24:20 +00003157#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003158
3159 /* Make extra sure authentication was performed, exactly once */
3160 if( auth_done != 1 )
3161 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003162 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3163 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003164 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003165
Hanno Beckera0e20d02019-05-15 14:03:01 +01003166#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01003167 if( rec->cid_len != 0 )
3168 {
3169 ret = ssl_cid_parse_inner_plaintext( data, &rec->data_len,
3170 &rec->type );
3171 if( ret != 0 )
3172 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3173 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01003174#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01003175
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003176 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003177
3178 return( 0 );
3179}
3180
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01003181#undef MAC_NONE
3182#undef MAC_PLAINTEXT
3183#undef MAC_CIPHERTEXT
3184
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003185#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00003186/*
3187 * Compression/decompression functions
3188 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003189static int ssl_compress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003190{
3191 int ret;
3192 unsigned char *msg_post = ssl->out_msg;
Andrzej Kurek5462e022018-04-20 07:58:53 -04003193 ptrdiff_t bytes_written = ssl->out_msg - ssl->out_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003194 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02003195 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003196
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
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02003199 if( len_pre == 0 )
3200 return( 0 );
3201
Paul Bakker2770fbd2012-07-03 13:30:23 +00003202 memcpy( msg_pre, ssl->out_msg, len_pre );
3203
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003204 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003205 ssl->out_msglen ) );
3206
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003207 MBEDTLS_SSL_DEBUG_BUF( 4, "before compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003208 ssl->out_msg, ssl->out_msglen );
3209
Paul Bakker48916f92012-09-16 19:57:18 +00003210 ssl->transform_out->ctx_deflate.next_in = msg_pre;
3211 ssl->transform_out->ctx_deflate.avail_in = len_pre;
3212 ssl->transform_out->ctx_deflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10003213 ssl->transform_out->ctx_deflate.avail_out = MBEDTLS_SSL_OUT_BUFFER_LEN - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003214
Paul Bakker48916f92012-09-16 19:57:18 +00003215 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003216 if( ret != Z_OK )
3217 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003218 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
3219 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003220 }
3221
Angus Grattond8213d02016-05-25 20:56:48 +10003222 ssl->out_msglen = MBEDTLS_SSL_OUT_BUFFER_LEN -
Andrzej Kurek5462e022018-04-20 07:58:53 -04003223 ssl->transform_out->ctx_deflate.avail_out - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003224
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003225 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003226 ssl->out_msglen ) );
3227
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003228 MBEDTLS_SSL_DEBUG_BUF( 4, "after compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003229 ssl->out_msg, ssl->out_msglen );
3230
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003231 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003232
3233 return( 0 );
3234}
3235
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003236static int ssl_decompress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003237{
3238 int ret;
3239 unsigned char *msg_post = ssl->in_msg;
Andrzej Kureka9ceef82018-04-24 06:32:44 -04003240 ptrdiff_t header_bytes = ssl->in_msg - ssl->in_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003241 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02003242 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003243
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003244 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003245
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02003246 if( len_pre == 0 )
3247 return( 0 );
3248
Paul Bakker2770fbd2012-07-03 13:30:23 +00003249 memcpy( msg_pre, ssl->in_msg, len_pre );
3250
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003251 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003252 ssl->in_msglen ) );
3253
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003254 MBEDTLS_SSL_DEBUG_BUF( 4, "before decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003255 ssl->in_msg, ssl->in_msglen );
3256
Paul Bakker48916f92012-09-16 19:57:18 +00003257 ssl->transform_in->ctx_inflate.next_in = msg_pre;
3258 ssl->transform_in->ctx_inflate.avail_in = len_pre;
3259 ssl->transform_in->ctx_inflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10003260 ssl->transform_in->ctx_inflate.avail_out = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04003261 header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003262
Paul Bakker48916f92012-09-16 19:57:18 +00003263 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003264 if( ret != Z_OK )
3265 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003266 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
3267 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003268 }
3269
Angus Grattond8213d02016-05-25 20:56:48 +10003270 ssl->in_msglen = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04003271 ssl->transform_in->ctx_inflate.avail_out - header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003272
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003273 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003274 ssl->in_msglen ) );
3275
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003276 MBEDTLS_SSL_DEBUG_BUF( 4, "after decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003277 ssl->in_msg, ssl->in_msglen );
3278
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003279 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003280
3281 return( 0 );
3282}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003283#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003284
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003285#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
3286static int ssl_write_hello_request( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003287
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003288#if defined(MBEDTLS_SSL_PROTO_DTLS)
3289static int ssl_resend_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003290{
3291 /* If renegotiation is not enforced, retransmit until we would reach max
3292 * timeout if we were using the usual handshake doubling scheme */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003293 if( ssl->conf->renego_max_records < 0 )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003294 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003295 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003296 unsigned char doublings = 1;
3297
3298 while( ratio != 0 )
3299 {
3300 ++doublings;
3301 ratio >>= 1;
3302 }
3303
3304 if( ++ssl->renego_records_seen > doublings )
3305 {
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02003306 MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003307 return( 0 );
3308 }
3309 }
3310
3311 return( ssl_write_hello_request( ssl ) );
3312}
3313#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003314#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003315
Paul Bakker5121ce52009-01-03 21:22:43 +00003316/*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003317 * Fill the input message buffer by appending data to it.
3318 * The amount of data already fetched is in ssl->in_left.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003319 *
3320 * If we return 0, is it guaranteed that (at least) nb_want bytes are
3321 * available (from this read and/or a previous one). Otherwise, an error code
3322 * is returned (possibly EOF or WANT_READ).
3323 *
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003324 * With stream transport (TLS) on success ssl->in_left == nb_want, but
3325 * with datagram transport (DTLS) on success ssl->in_left >= nb_want,
3326 * since we always read a whole datagram at once.
3327 *
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02003328 * For DTLS, it is up to the caller to set ssl->next_record_offset when
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003329 * they're done reading a record.
Paul Bakker5121ce52009-01-03 21:22:43 +00003330 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003331int mbedtls_ssl_fetch_input( mbedtls_ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00003332{
Paul Bakker23986e52011-04-24 08:57:21 +00003333 int ret;
3334 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00003335
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003336 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003337
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003338 if( ssl->f_recv == NULL && ssl->f_recv_timeout == NULL )
3339 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003340 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01003341 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003342 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003343 }
3344
Angus Grattond8213d02016-05-25 20:56:48 +10003345 if( nb_want > MBEDTLS_SSL_IN_BUFFER_LEN - (size_t)( ssl->in_hdr - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003346 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003347 MBEDTLS_SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
3348 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003349 }
3350
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003351#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003352 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00003353 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003354 uint32_t timeout;
3355
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02003356 /* Just to be sure */
3357 if( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL )
3358 {
3359 MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
3360 "mbedtls_ssl_set_timer_cb() for DTLS" ) );
3361 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3362 }
3363
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003364 /*
3365 * The point is, we need to always read a full datagram at once, so we
3366 * sometimes read more then requested, and handle the additional data.
3367 * It could be the rest of the current record (while fetching the
3368 * header) and/or some other records in the same datagram.
3369 */
3370
3371 /*
3372 * Move to the next record in the already read datagram if applicable
3373 */
3374 if( ssl->next_record_offset != 0 )
3375 {
3376 if( ssl->in_left < ssl->next_record_offset )
3377 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003378 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3379 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003380 }
3381
3382 ssl->in_left -= ssl->next_record_offset;
3383
3384 if( ssl->in_left != 0 )
3385 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003386 MBEDTLS_SSL_DEBUG_MSG( 2, ( "next record in same datagram, offset: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003387 ssl->next_record_offset ) );
3388 memmove( ssl->in_hdr,
3389 ssl->in_hdr + ssl->next_record_offset,
3390 ssl->in_left );
3391 }
3392
3393 ssl->next_record_offset = 0;
3394 }
3395
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003396 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Paul Bakker5121ce52009-01-03 21:22:43 +00003397 ssl->in_left, nb_want ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003398
3399 /*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003400 * Done if we already have enough data.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003401 */
3402 if( nb_want <= ssl->in_left)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003403 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003404 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003405 return( 0 );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003406 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003407
3408 /*
Antonin Décimo36e89b52019-01-23 15:24:37 +01003409 * A record can't be split across datagrams. If we need to read but
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003410 * are not at the beginning of a new record, the caller did something
3411 * wrong.
3412 */
3413 if( ssl->in_left != 0 )
3414 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003415 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3416 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003417 }
3418
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003419 /*
3420 * Don't even try to read if time's out already.
3421 * This avoids by-passing the timer when repeatedly receiving messages
3422 * that will end up being dropped.
3423 */
3424 if( ssl_check_timer( ssl ) != 0 )
Hanno Beckere65ce782017-05-22 14:47:48 +01003425 {
3426 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timer has expired" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003427 ret = MBEDTLS_ERR_SSL_TIMEOUT;
Hanno Beckere65ce782017-05-22 14:47:48 +01003428 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003429 else
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003430 {
Angus Grattond8213d02016-05-25 20:56:48 +10003431 len = MBEDTLS_SSL_IN_BUFFER_LEN - ( ssl->in_hdr - ssl->in_buf );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003432
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003433 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003434 timeout = ssl->handshake->retransmit_timeout;
3435 else
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003436 timeout = ssl->conf->read_timeout;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003437
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003438 MBEDTLS_SSL_DEBUG_MSG( 3, ( "f_recv_timeout: %u ms", timeout ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003439
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003440 if( ssl->f_recv_timeout != NULL )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003441 ret = ssl->f_recv_timeout( ssl->p_bio, ssl->in_hdr, len,
3442 timeout );
3443 else
3444 ret = ssl->f_recv( ssl->p_bio, ssl->in_hdr, len );
3445
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003446 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003447
3448 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003449 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003450 }
3451
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003452 if( ret == MBEDTLS_ERR_SSL_TIMEOUT )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003453 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003454 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timeout" ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003455 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003456
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003457 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003458 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003459 if( ssl_double_retransmit_timeout( ssl ) != 0 )
3460 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003461 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake timeout" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003462 return( MBEDTLS_ERR_SSL_TIMEOUT );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003463 }
3464
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003465 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003466 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003467 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003468 return( ret );
3469 }
3470
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003471 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003472 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003473#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003474 else if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003475 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003476 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003477 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003478 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003479 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003480 return( ret );
3481 }
3482
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003483 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003484 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003485#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003486 }
3487
Paul Bakker5121ce52009-01-03 21:22:43 +00003488 if( ret < 0 )
3489 return( ret );
3490
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003491 ssl->in_left = ret;
3492 }
3493 else
3494#endif
3495 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003496 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003497 ssl->in_left, nb_want ) );
3498
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003499 while( ssl->in_left < nb_want )
3500 {
3501 len = nb_want - ssl->in_left;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02003502
3503 if( ssl_check_timer( ssl ) != 0 )
3504 ret = MBEDTLS_ERR_SSL_TIMEOUT;
3505 else
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003506 {
3507 if( ssl->f_recv_timeout != NULL )
3508 {
3509 ret = ssl->f_recv_timeout( ssl->p_bio,
3510 ssl->in_hdr + ssl->in_left, len,
3511 ssl->conf->read_timeout );
3512 }
3513 else
3514 {
3515 ret = ssl->f_recv( ssl->p_bio,
3516 ssl->in_hdr + ssl->in_left, len );
3517 }
3518 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003519
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003520 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003521 ssl->in_left, nb_want ) );
3522 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003523
3524 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003525 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003526
3527 if( ret < 0 )
3528 return( ret );
3529
mohammad160352aecb92018-03-28 23:41:40 -07003530 if ( (size_t)ret > len || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16035bd15cb2018-02-28 04:30:59 -08003531 {
Darryl Green11999bb2018-03-13 15:22:58 +00003532 MBEDTLS_SSL_DEBUG_MSG( 1,
3533 ( "f_recv returned %d bytes but only %lu were requested",
mohammad160319d392b2018-04-02 07:25:26 -07003534 ret, (unsigned long)len ) );
mohammad16035bd15cb2018-02-28 04:30:59 -08003535 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3536 }
3537
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003538 ssl->in_left += ret;
3539 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003540 }
3541
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003542 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003543
3544 return( 0 );
3545}
3546
3547/*
3548 * Flush any data not yet written
3549 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003550int mbedtls_ssl_flush_output( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003551{
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01003552 int ret;
Hanno Becker04484622018-08-06 09:49:38 +01003553 unsigned char *buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00003554
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003555 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003556
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003557 if( ssl->f_send == NULL )
3558 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003559 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01003560 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003561 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003562 }
3563
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003564 /* Avoid incrementing counter if data is flushed */
3565 if( ssl->out_left == 0 )
3566 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003567 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003568 return( 0 );
3569 }
3570
Paul Bakker5121ce52009-01-03 21:22:43 +00003571 while( ssl->out_left > 0 )
3572 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003573 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
Hanno Becker5903de42019-05-03 14:46:38 +01003574 mbedtls_ssl_out_hdr_len( ssl ) + ssl->out_msglen, ssl->out_left ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003575
Hanno Becker2b1e3542018-08-06 11:19:13 +01003576 buf = ssl->out_hdr - ssl->out_left;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003577 ret = ssl->f_send( ssl->p_bio, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00003578
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003579 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003580
3581 if( ret <= 0 )
3582 return( ret );
3583
mohammad160352aecb92018-03-28 23:41:40 -07003584 if( (size_t)ret > ssl->out_left || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16034bbaeb42018-02-22 04:29:04 -08003585 {
Darryl Green11999bb2018-03-13 15:22:58 +00003586 MBEDTLS_SSL_DEBUG_MSG( 1,
3587 ( "f_send returned %d bytes but only %lu bytes were sent",
mohammad160319d392b2018-04-02 07:25:26 -07003588 ret, (unsigned long)ssl->out_left ) );
mohammad16034bbaeb42018-02-22 04:29:04 -08003589 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3590 }
3591
Paul Bakker5121ce52009-01-03 21:22:43 +00003592 ssl->out_left -= ret;
3593 }
3594
Hanno Becker2b1e3542018-08-06 11:19:13 +01003595#if defined(MBEDTLS_SSL_PROTO_DTLS)
3596 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003597 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01003598 ssl->out_hdr = ssl->out_buf;
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003599 }
Hanno Becker2b1e3542018-08-06 11:19:13 +01003600 else
3601#endif
3602 {
3603 ssl->out_hdr = ssl->out_buf + 8;
3604 }
3605 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003606
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003607 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003608
3609 return( 0 );
3610}
3611
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003612/*
3613 * Functions to handle the DTLS retransmission state machine
3614 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003615#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003616/*
3617 * Append current handshake message to current outgoing flight
3618 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003619static int ssl_flight_append( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003620{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003621 mbedtls_ssl_flight_item *msg;
Hanno Becker3b235902018-08-06 09:54:53 +01003622 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_flight_append" ) );
3623 MBEDTLS_SSL_DEBUG_BUF( 4, "message appended to flight",
3624 ssl->out_msg, ssl->out_msglen );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003625
3626 /* Allocate space for current message */
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003627 if( ( msg = mbedtls_calloc( 1, sizeof( mbedtls_ssl_flight_item ) ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003628 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003629 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003630 sizeof( mbedtls_ssl_flight_item ) ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003631 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003632 }
3633
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003634 if( ( msg->p = mbedtls_calloc( 1, ssl->out_msglen ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003635 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003636 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed", ssl->out_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003637 mbedtls_free( msg );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003638 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003639 }
3640
3641 /* Copy current handshake message with headers */
3642 memcpy( msg->p, ssl->out_msg, ssl->out_msglen );
3643 msg->len = ssl->out_msglen;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003644 msg->type = ssl->out_msgtype;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003645 msg->next = NULL;
3646
3647 /* Append to the current flight */
3648 if( ssl->handshake->flight == NULL )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003649 ssl->handshake->flight = msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003650 else
3651 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003652 mbedtls_ssl_flight_item *cur = ssl->handshake->flight;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003653 while( cur->next != NULL )
3654 cur = cur->next;
3655 cur->next = msg;
3656 }
3657
Hanno Becker3b235902018-08-06 09:54:53 +01003658 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_flight_append" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003659 return( 0 );
3660}
3661
3662/*
3663 * Free the current flight of handshake messages
3664 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003665static void ssl_flight_free( mbedtls_ssl_flight_item *flight )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003666{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003667 mbedtls_ssl_flight_item *cur = flight;
3668 mbedtls_ssl_flight_item *next;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003669
3670 while( cur != NULL )
3671 {
3672 next = cur->next;
3673
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003674 mbedtls_free( cur->p );
3675 mbedtls_free( cur );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003676
3677 cur = next;
3678 }
3679}
3680
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003681#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3682static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003683#endif
3684
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003685/*
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003686 * Swap transform_out and out_ctr with the alternative ones
3687 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003688static void ssl_swap_epochs( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003689{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003690 mbedtls_ssl_transform *tmp_transform;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003691 unsigned char tmp_out_ctr[8];
3692
3693 if( ssl->transform_out == ssl->handshake->alt_transform_out )
3694 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003695 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003696 return;
3697 }
3698
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003699 MBEDTLS_SSL_DEBUG_MSG( 3, ( "swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003700
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003701 /* Swap transforms */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003702 tmp_transform = ssl->transform_out;
3703 ssl->transform_out = ssl->handshake->alt_transform_out;
3704 ssl->handshake->alt_transform_out = tmp_transform;
3705
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003706 /* Swap epoch + sequence_number */
Hanno Becker19859472018-08-06 09:40:20 +01003707 memcpy( tmp_out_ctr, ssl->cur_out_ctr, 8 );
3708 memcpy( ssl->cur_out_ctr, ssl->handshake->alt_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003709 memcpy( ssl->handshake->alt_out_ctr, tmp_out_ctr, 8 );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003710
3711 /* Adjust to the newly activated transform */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01003712 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003713
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003714#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3715 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003716 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003717 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003718 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003719 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
3720 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003721 }
3722 }
3723#endif
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003724}
3725
3726/*
3727 * Retransmit the current flight of messages.
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003728 */
3729int mbedtls_ssl_resend( mbedtls_ssl_context *ssl )
3730{
3731 int ret = 0;
3732
3733 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_resend" ) );
3734
3735 ret = mbedtls_ssl_flight_transmit( ssl );
3736
3737 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_resend" ) );
3738
3739 return( ret );
3740}
3741
3742/*
3743 * Transmit or retransmit the current flight of messages.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003744 *
3745 * Need to remember the current message in case flush_output returns
3746 * WANT_WRITE, causing us to exit this function and come back later.
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003747 * This function must be called until state is no longer SENDING.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003748 */
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003749int mbedtls_ssl_flight_transmit( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003750{
Hanno Becker67bc7c32018-08-06 11:33:50 +01003751 int ret;
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003752 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003753
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003754 if( ssl->handshake->retransmit_state != MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003755 {
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003756 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialise flight transmission" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003757
3758 ssl->handshake->cur_msg = ssl->handshake->flight;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003759 ssl->handshake->cur_msg_p = ssl->handshake->flight->p + 12;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003760 ssl_swap_epochs( ssl );
3761
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003762 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_SENDING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003763 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003764
3765 while( ssl->handshake->cur_msg != NULL )
3766 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003767 size_t max_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003768 const mbedtls_ssl_flight_item * const cur = ssl->handshake->cur_msg;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003769
Hanno Beckere1dcb032018-08-17 16:47:58 +01003770 int const is_finished =
3771 ( cur->type == MBEDTLS_SSL_MSG_HANDSHAKE &&
3772 cur->p[0] == MBEDTLS_SSL_HS_FINISHED );
3773
Hanno Becker04da1892018-08-14 13:22:10 +01003774 uint8_t const force_flush = ssl->disable_datagram_packing == 1 ?
3775 SSL_FORCE_FLUSH : SSL_DONT_FORCE_FLUSH;
3776
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003777 /* Swap epochs before sending Finished: we can't do it after
3778 * sending ChangeCipherSpec, in case write returns WANT_READ.
3779 * Must be done before copying, may change out_msg pointer */
Hanno Beckere1dcb032018-08-17 16:47:58 +01003780 if( is_finished && ssl->handshake->cur_msg_p == ( cur->p + 12 ) )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003781 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003782 MBEDTLS_SSL_DEBUG_MSG( 2, ( "swap epochs to send finished message" ) );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003783 ssl_swap_epochs( ssl );
3784 }
3785
Hanno Becker67bc7c32018-08-06 11:33:50 +01003786 ret = ssl_get_remaining_payload_in_datagram( ssl );
3787 if( ret < 0 )
3788 return( ret );
3789 max_frag_len = (size_t) ret;
3790
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003791 /* CCS is copied as is, while HS messages may need fragmentation */
3792 if( cur->type == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
3793 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003794 if( max_frag_len == 0 )
3795 {
3796 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3797 return( ret );
3798
3799 continue;
3800 }
3801
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003802 memcpy( ssl->out_msg, cur->p, cur->len );
Hanno Becker67bc7c32018-08-06 11:33:50 +01003803 ssl->out_msglen = cur->len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003804 ssl->out_msgtype = cur->type;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003805
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003806 /* Update position inside current message */
3807 ssl->handshake->cur_msg_p += cur->len;
3808 }
3809 else
3810 {
3811 const unsigned char * const p = ssl->handshake->cur_msg_p;
3812 const size_t hs_len = cur->len - 12;
3813 const size_t frag_off = p - ( cur->p + 12 );
3814 const size_t rem_len = hs_len - frag_off;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003815 size_t cur_hs_frag_len, max_hs_frag_len;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003816
Hanno Beckere1dcb032018-08-17 16:47:58 +01003817 if( ( max_frag_len < 12 ) || ( max_frag_len == 12 && hs_len != 0 ) )
Manuel Pégourié-Gonnarda1071a52018-08-20 11:56:14 +02003818 {
Hanno Beckere1dcb032018-08-17 16:47:58 +01003819 if( is_finished )
Hanno Becker67bc7c32018-08-06 11:33:50 +01003820 ssl_swap_epochs( ssl );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003821
Hanno Becker67bc7c32018-08-06 11:33:50 +01003822 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3823 return( ret );
3824
3825 continue;
3826 }
3827 max_hs_frag_len = max_frag_len - 12;
3828
3829 cur_hs_frag_len = rem_len > max_hs_frag_len ?
3830 max_hs_frag_len : rem_len;
3831
3832 if( frag_off == 0 && cur_hs_frag_len != hs_len )
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003833 {
3834 MBEDTLS_SSL_DEBUG_MSG( 2, ( "fragmenting handshake message (%u > %u)",
Hanno Becker67bc7c32018-08-06 11:33:50 +01003835 (unsigned) cur_hs_frag_len,
3836 (unsigned) max_hs_frag_len ) );
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003837 }
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02003838
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003839 /* Messages are stored with handshake headers as if not fragmented,
3840 * copy beginning of headers then fill fragmentation fields.
3841 * Handshake headers: type(1) len(3) seq(2) f_off(3) f_len(3) */
3842 memcpy( ssl->out_msg, cur->p, 6 );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003843
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003844 ssl->out_msg[6] = ( ( frag_off >> 16 ) & 0xff );
3845 ssl->out_msg[7] = ( ( frag_off >> 8 ) & 0xff );
3846 ssl->out_msg[8] = ( ( frag_off ) & 0xff );
3847
Hanno Becker67bc7c32018-08-06 11:33:50 +01003848 ssl->out_msg[ 9] = ( ( cur_hs_frag_len >> 16 ) & 0xff );
3849 ssl->out_msg[10] = ( ( cur_hs_frag_len >> 8 ) & 0xff );
3850 ssl->out_msg[11] = ( ( cur_hs_frag_len ) & 0xff );
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003851
3852 MBEDTLS_SSL_DEBUG_BUF( 3, "handshake header", ssl->out_msg, 12 );
3853
Hanno Becker3f7b9732018-08-28 09:53:25 +01003854 /* Copy the handshake message content and set records fields */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003855 memcpy( ssl->out_msg + 12, p, cur_hs_frag_len );
3856 ssl->out_msglen = cur_hs_frag_len + 12;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003857 ssl->out_msgtype = cur->type;
3858
3859 /* Update position inside current message */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003860 ssl->handshake->cur_msg_p += cur_hs_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003861 }
3862
3863 /* If done with the current message move to the next one if any */
3864 if( ssl->handshake->cur_msg_p >= cur->p + cur->len )
3865 {
3866 if( cur->next != NULL )
3867 {
3868 ssl->handshake->cur_msg = cur->next;
3869 ssl->handshake->cur_msg_p = cur->next->p + 12;
3870 }
3871 else
3872 {
3873 ssl->handshake->cur_msg = NULL;
3874 ssl->handshake->cur_msg_p = NULL;
3875 }
3876 }
3877
3878 /* Actually send the message out */
Hanno Becker04da1892018-08-14 13:22:10 +01003879 if( ( ret = mbedtls_ssl_write_record( ssl, force_flush ) ) != 0 )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003880 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003881 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003882 return( ret );
3883 }
3884 }
3885
Hanno Becker67bc7c32018-08-06 11:33:50 +01003886 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3887 return( ret );
3888
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003889 /* Update state and set timer */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003890 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
3891 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard23b7b702014-09-25 13:50:12 +02003892 else
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003893 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003894 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003895 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
3896 }
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003897
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003898 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003899
3900 return( 0 );
3901}
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003902
3903/*
3904 * To be called when the last message of an incoming flight is received.
3905 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003906void mbedtls_ssl_recv_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003907{
3908 /* We won't need to resend that one any more */
3909 ssl_flight_free( ssl->handshake->flight );
3910 ssl->handshake->flight = NULL;
3911 ssl->handshake->cur_msg = NULL;
3912
3913 /* The next incoming flight will start with this msg_seq */
3914 ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq;
3915
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003916 /* We don't want to remember CCS's across flight boundaries. */
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01003917 ssl->handshake->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003918
Hanno Becker0271f962018-08-16 13:23:47 +01003919 /* Clear future message buffering structure. */
3920 ssl_buffering_free( ssl );
3921
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02003922 /* Cancel timer */
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003923 ssl_set_timer( ssl, 0 );
3924
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003925 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3926 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003927 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003928 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003929 }
3930 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003931 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003932}
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003933
3934/*
3935 * To be called when the last message of an outgoing flight is send.
3936 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003937void mbedtls_ssl_send_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003938{
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02003939 ssl_reset_retransmit_timeout( ssl );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003940 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003941
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003942 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3943 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003944 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003945 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003946 }
3947 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003948 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003949}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003950#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003951
Paul Bakker5121ce52009-01-03 21:22:43 +00003952/*
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003953 * Handshake layer functions
Paul Bakker5121ce52009-01-03 21:22:43 +00003954 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003955
3956/*
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003957 * Write (DTLS: or queue) current handshake (including CCS) message.
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003958 *
3959 * - fill in handshake headers
3960 * - update handshake checksum
3961 * - DTLS: save message for resending
3962 * - then pass to the record layer
3963 *
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003964 * DTLS: except for HelloRequest, messages are only queued, and will only be
3965 * actually sent when calling flight_transmit() or resend().
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003966 *
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003967 * Inputs:
3968 * - ssl->out_msglen: 4 + actual handshake message len
3969 * (4 is the size of handshake headers for TLS)
3970 * - ssl->out_msg[0]: the handshake type (ClientHello, ServerHello, etc)
3971 * - ssl->out_msg + 4: the handshake message body
3972 *
Manuel Pégourié-Gonnard065a2a32018-08-20 11:09:26 +02003973 * Outputs, ie state before passing to flight_append() or write_record():
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003974 * - ssl->out_msglen: the length of the record contents
3975 * (including handshake headers but excluding record headers)
3976 * - ssl->out_msg: the record contents (handshake headers + content)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003977 */
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003978int mbedtls_ssl_write_handshake_msg( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003979{
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003980 int ret;
3981 const size_t hs_len = ssl->out_msglen - 4;
3982 const unsigned char hs_type = ssl->out_msg[0];
Paul Bakker5121ce52009-01-03 21:22:43 +00003983
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003984 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write handshake message" ) );
3985
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003986 /*
3987 * Sanity checks
3988 */
Hanno Beckerc83d2b32018-08-22 16:05:47 +01003989 if( ssl->out_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003990 ssl->out_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
3991 {
Hanno Beckerc83d2b32018-08-22 16:05:47 +01003992 /* In SSLv3, the client might send a NoCertificate alert. */
3993#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
3994 if( ! ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
3995 ssl->out_msgtype == MBEDTLS_SSL_MSG_ALERT &&
3996 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) )
3997#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
3998 {
3999 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4000 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4001 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004002 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004003
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05004004 /* Whenever we send anything different from a
4005 * HelloRequest we should be in a handshake - double check. */
4006 if( ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4007 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) &&
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004008 ssl->handshake == NULL )
4009 {
4010 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4011 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4012 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004013
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004014#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004015 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004016 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004017 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004018 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004019 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4020 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004021 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004022#endif
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004023
Hanno Beckerb50a2532018-08-06 11:52:54 +01004024 /* Double-check that we did not exceed the bounds
4025 * of the outgoing record buffer.
4026 * This should never fail as the various message
4027 * writing functions must obey the bounds of the
4028 * outgoing record buffer, but better be safe.
4029 *
4030 * Note: We deliberately do not check for the MTU or MFL here.
4031 */
4032 if( ssl->out_msglen > MBEDTLS_SSL_OUT_CONTENT_LEN )
4033 {
4034 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record too large: "
4035 "size %u, maximum %u",
4036 (unsigned) ssl->out_msglen,
4037 (unsigned) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
4038 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4039 }
4040
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004041 /*
4042 * Fill handshake headers
4043 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004044 if( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00004045 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004046 ssl->out_msg[1] = (unsigned char)( hs_len >> 16 );
4047 ssl->out_msg[2] = (unsigned char)( hs_len >> 8 );
4048 ssl->out_msg[3] = (unsigned char)( hs_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00004049
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004050 /*
4051 * DTLS has additional fields in the Handshake layer,
4052 * between the length field and the actual payload:
4053 * uint16 message_seq;
4054 * uint24 fragment_offset;
4055 * uint24 fragment_length;
4056 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004057#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004058 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004059 {
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01004060 /* Make room for the additional DTLS fields */
Angus Grattond8213d02016-05-25 20:56:48 +10004061 if( MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen < 8 )
Hanno Becker9648f8b2017-09-18 10:55:54 +01004062 {
4063 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS handshake message too large: "
4064 "size %u, maximum %u",
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004065 (unsigned) ( hs_len ),
Angus Grattond8213d02016-05-25 20:56:48 +10004066 (unsigned) ( MBEDTLS_SSL_OUT_CONTENT_LEN - 12 ) ) );
Hanno Becker9648f8b2017-09-18 10:55:54 +01004067 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4068 }
4069
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004070 memmove( ssl->out_msg + 12, ssl->out_msg + 4, hs_len );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004071 ssl->out_msglen += 8;
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004072
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02004073 /* Write message_seq and update it, except for HelloRequest */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004074 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02004075 {
Manuel Pégourié-Gonnardd9ba0d92014-09-02 18:30:26 +02004076 ssl->out_msg[4] = ( ssl->handshake->out_msg_seq >> 8 ) & 0xFF;
4077 ssl->out_msg[5] = ( ssl->handshake->out_msg_seq ) & 0xFF;
4078 ++( ssl->handshake->out_msg_seq );
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02004079 }
4080 else
4081 {
4082 ssl->out_msg[4] = 0;
4083 ssl->out_msg[5] = 0;
4084 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01004085
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004086 /* Handshake hashes are computed without fragmentation,
4087 * so set frag_offset = 0 and frag_len = hs_len for now */
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01004088 memset( ssl->out_msg + 6, 0x00, 3 );
4089 memcpy( ssl->out_msg + 9, ssl->out_msg + 1, 3 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004090 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004091#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004092
Hanno Becker0207e532018-08-28 10:28:28 +01004093 /* Update running hashes of handshake messages seen */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004094 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
4095 ssl->handshake->update_checksum( ssl, ssl->out_msg, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00004096 }
4097
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004098 /* Either send now, or just save to be sent (and resent) later */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004099#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004100 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05004101 ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4102 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004103 {
4104 if( ( ret = ssl_flight_append( ssl ) ) != 0 )
4105 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004106 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_flight_append", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004107 return( ret );
4108 }
4109 }
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004110 else
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004111#endif
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004112 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004113 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004114 {
4115 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4116 return( ret );
4117 }
4118 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004119
4120 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write handshake message" ) );
4121
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004122 return( 0 );
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004123}
4124
4125/*
4126 * Record layer functions
4127 */
4128
4129/*
4130 * Write current record.
4131 *
4132 * Uses:
4133 * - ssl->out_msgtype: type of the message (AppData, Handshake, Alert, CCS)
4134 * - ssl->out_msglen: length of the record content (excl headers)
4135 * - ssl->out_msg: record content
4136 */
Hanno Becker67bc7c32018-08-06 11:33:50 +01004137int mbedtls_ssl_write_record( mbedtls_ssl_context *ssl, uint8_t force_flush )
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004138{
4139 int ret, done = 0;
4140 size_t len = ssl->out_msglen;
Hanno Becker67bc7c32018-08-06 11:33:50 +01004141 uint8_t flush = force_flush;
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004142
4143 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write record" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004144
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004145#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00004146 if( ssl->transform_out != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004147 ssl->session_out->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00004148 {
4149 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
4150 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004151 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00004152 return( ret );
4153 }
4154
4155 len = ssl->out_msglen;
4156 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004157#endif /*MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00004158
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004159#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
4160 if( mbedtls_ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004161 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004162 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004163
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004164 ret = mbedtls_ssl_hw_record_write( ssl );
4165 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00004166 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004167 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_write", ret );
4168 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00004169 }
Paul Bakkerc7878112012-12-19 14:41:14 +01004170
4171 if( ret == 0 )
4172 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00004173 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004174#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00004175 if( !done )
4176 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01004177 unsigned i;
4178 size_t protected_record_size;
4179
Hanno Becker6430faf2019-05-08 11:57:13 +01004180 /* Skip writing the record content type to after the encryption,
4181 * as it may change when using the CID extension. */
4182
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004183 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004184 ssl->conf->transport, ssl->out_hdr + 1 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01004185
Hanno Becker19859472018-08-06 09:40:20 +01004186 memcpy( ssl->out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01004187 ssl->out_len[0] = (unsigned char)( len >> 8 );
4188 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00004189
Paul Bakker48916f92012-09-16 19:57:18 +00004190 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00004191 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00004192 mbedtls_record rec;
4193
4194 rec.buf = ssl->out_iv;
4195 rec.buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN -
4196 ( ssl->out_iv - ssl->out_buf );
4197 rec.data_len = ssl->out_msglen;
4198 rec.data_offset = ssl->out_msg - rec.buf;
4199
4200 memcpy( &rec.ctr[0], ssl->out_ctr, 8 );
4201 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
4202 ssl->conf->transport, rec.ver );
4203 rec.type = ssl->out_msgtype;
4204
Hanno Beckera0e20d02019-05-15 14:03:01 +01004205#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker43c24b82019-05-01 09:45:57 +01004206 /* The CID is set by mbedtls_ssl_encrypt_buf(). */
Hanno Beckercab87e62019-04-29 13:52:53 +01004207 rec.cid_len = 0;
Hanno Beckera0e20d02019-05-15 14:03:01 +01004208#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckercab87e62019-04-29 13:52:53 +01004209
Hanno Beckera18d1322018-01-03 14:27:32 +00004210 if( ( ret = mbedtls_ssl_encrypt_buf( ssl, ssl->transform_out, &rec,
Hanno Becker9eddaeb2017-12-27 21:37:21 +00004211 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00004212 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004213 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
Paul Bakker05ef8352012-05-08 09:17:57 +00004214 return( ret );
4215 }
4216
Hanno Becker9eddaeb2017-12-27 21:37:21 +00004217 if( rec.data_offset != 0 )
4218 {
4219 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4220 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4221 }
4222
Hanno Becker6430faf2019-05-08 11:57:13 +01004223 /* Update the record content type and CID. */
4224 ssl->out_msgtype = rec.type;
Hanno Beckera0e20d02019-05-15 14:03:01 +01004225#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID )
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01004226 memcpy( ssl->out_cid, rec.cid, rec.cid_len );
Hanno Beckera0e20d02019-05-15 14:03:01 +01004227#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker78f839d2019-03-14 12:56:23 +00004228 ssl->out_msglen = len = rec.data_len;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00004229 ssl->out_len[0] = (unsigned char)( rec.data_len >> 8 );
4230 ssl->out_len[1] = (unsigned char)( rec.data_len );
Paul Bakker05ef8352012-05-08 09:17:57 +00004231 }
4232
Hanno Becker5903de42019-05-03 14:46:38 +01004233 protected_record_size = len + mbedtls_ssl_out_hdr_len( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01004234
4235#if defined(MBEDTLS_SSL_PROTO_DTLS)
4236 /* In case of DTLS, double-check that we don't exceed
4237 * the remaining space in the datagram. */
4238 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4239 {
Hanno Becker554b0af2018-08-22 20:33:41 +01004240 ret = ssl_get_remaining_space_in_datagram( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01004241 if( ret < 0 )
4242 return( ret );
4243
4244 if( protected_record_size > (size_t) ret )
4245 {
4246 /* Should never happen */
4247 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4248 }
4249 }
4250#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker05ef8352012-05-08 09:17:57 +00004251
Hanno Becker6430faf2019-05-08 11:57:13 +01004252 /* Now write the potentially updated record content type. */
4253 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
4254
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004255 MBEDTLS_SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01004256 "version = [%d:%d], msglen = %d",
4257 ssl->out_hdr[0], ssl->out_hdr[1],
4258 ssl->out_hdr[2], len ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00004259
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004260 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01004261 ssl->out_hdr, protected_record_size );
Hanno Becker2b1e3542018-08-06 11:19:13 +01004262
4263 ssl->out_left += protected_record_size;
4264 ssl->out_hdr += protected_record_size;
4265 ssl_update_out_pointers( ssl, ssl->transform_out );
4266
Hanno Becker04484622018-08-06 09:49:38 +01004267 for( i = 8; i > ssl_ep_len( ssl ); i-- )
4268 if( ++ssl->cur_out_ctr[i - 1] != 0 )
4269 break;
4270
4271 /* The loop goes to its end iff the counter is wrapping */
4272 if( i == ssl_ep_len( ssl ) )
4273 {
4274 MBEDTLS_SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
4275 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
4276 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004277 }
4278
Hanno Becker67bc7c32018-08-06 11:33:50 +01004279#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker47db8772018-08-21 13:32:13 +01004280 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
4281 flush == SSL_DONT_FORCE_FLUSH )
Hanno Becker67bc7c32018-08-06 11:33:50 +01004282 {
Hanno Becker1f5a15d2018-08-21 13:31:31 +01004283 size_t remaining;
4284 ret = ssl_get_remaining_payload_in_datagram( ssl );
4285 if( ret < 0 )
4286 {
4287 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_get_remaining_payload_in_datagram",
4288 ret );
4289 return( ret );
4290 }
4291
4292 remaining = (size_t) ret;
Hanno Becker67bc7c32018-08-06 11:33:50 +01004293 if( remaining == 0 )
Hanno Beckerf0da6672018-08-28 09:55:10 +01004294 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004295 flush = SSL_FORCE_FLUSH;
Hanno Beckerf0da6672018-08-28 09:55:10 +01004296 }
Hanno Becker67bc7c32018-08-06 11:33:50 +01004297 else
4298 {
Hanno Becker513815a2018-08-20 11:56:09 +01004299 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Still %u bytes available in current datagram", (unsigned) remaining ) );
Hanno Becker67bc7c32018-08-06 11:33:50 +01004300 }
4301 }
4302#endif /* MBEDTLS_SSL_PROTO_DTLS */
4303
4304 if( ( flush == SSL_FORCE_FLUSH ) &&
4305 ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004306 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004307 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004308 return( ret );
4309 }
4310
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004311 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write record" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004312
4313 return( 0 );
4314}
4315
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004316#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckere25e3b72018-08-16 09:30:53 +01004317
4318static int ssl_hs_is_proper_fragment( mbedtls_ssl_context *ssl )
4319{
4320 if( ssl->in_msglen < ssl->in_hslen ||
4321 memcmp( ssl->in_msg + 6, "\0\0\0", 3 ) != 0 ||
4322 memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 )
4323 {
4324 return( 1 );
4325 }
4326 return( 0 );
4327}
Hanno Becker44650b72018-08-16 12:51:11 +01004328
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004329static uint32_t ssl_get_hs_frag_len( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004330{
4331 return( ( ssl->in_msg[9] << 16 ) |
4332 ( ssl->in_msg[10] << 8 ) |
4333 ssl->in_msg[11] );
4334}
4335
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004336static uint32_t ssl_get_hs_frag_off( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004337{
4338 return( ( ssl->in_msg[6] << 16 ) |
4339 ( ssl->in_msg[7] << 8 ) |
4340 ssl->in_msg[8] );
4341}
4342
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004343static int ssl_check_hs_header( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004344{
4345 uint32_t msg_len, frag_off, frag_len;
4346
4347 msg_len = ssl_get_hs_total_len( ssl );
4348 frag_off = ssl_get_hs_frag_off( ssl );
4349 frag_len = ssl_get_hs_frag_len( ssl );
4350
4351 if( frag_off > msg_len )
4352 return( -1 );
4353
4354 if( frag_len > msg_len - frag_off )
4355 return( -1 );
4356
4357 if( frag_len + 12 > ssl->in_msglen )
4358 return( -1 );
4359
4360 return( 0 );
4361}
4362
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004363/*
4364 * Mark bits in bitmask (used for DTLS HS reassembly)
4365 */
4366static void ssl_bitmask_set( unsigned char *mask, size_t offset, size_t len )
4367{
4368 unsigned int start_bits, end_bits;
4369
4370 start_bits = 8 - ( offset % 8 );
4371 if( start_bits != 8 )
4372 {
4373 size_t first_byte_idx = offset / 8;
4374
Manuel Pégourié-Gonnardac030522014-09-02 14:23:40 +02004375 /* Special case */
4376 if( len <= start_bits )
4377 {
4378 for( ; len != 0; len-- )
4379 mask[first_byte_idx] |= 1 << ( start_bits - len );
4380
4381 /* Avoid potential issues with offset or len becoming invalid */
4382 return;
4383 }
4384
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004385 offset += start_bits; /* Now offset % 8 == 0 */
4386 len -= start_bits;
4387
4388 for( ; start_bits != 0; start_bits-- )
4389 mask[first_byte_idx] |= 1 << ( start_bits - 1 );
4390 }
4391
4392 end_bits = len % 8;
4393 if( end_bits != 0 )
4394 {
4395 size_t last_byte_idx = ( offset + len ) / 8;
4396
4397 len -= end_bits; /* Now len % 8 == 0 */
4398
4399 for( ; end_bits != 0; end_bits-- )
4400 mask[last_byte_idx] |= 1 << ( 8 - end_bits );
4401 }
4402
4403 memset( mask + offset / 8, 0xFF, len / 8 );
4404}
4405
4406/*
4407 * Check that bitmask is full
4408 */
4409static int ssl_bitmask_check( unsigned char *mask, size_t len )
4410{
4411 size_t i;
4412
4413 for( i = 0; i < len / 8; i++ )
4414 if( mask[i] != 0xFF )
4415 return( -1 );
4416
4417 for( i = 0; i < len % 8; i++ )
4418 if( ( mask[len / 8] & ( 1 << ( 7 - i ) ) ) == 0 )
4419 return( -1 );
4420
4421 return( 0 );
4422}
4423
Hanno Becker56e205e2018-08-16 09:06:12 +01004424/* msg_len does not include the handshake header */
Hanno Becker65dc8852018-08-23 09:40:49 +01004425static size_t ssl_get_reassembly_buffer_size( size_t msg_len,
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004426 unsigned add_bitmap )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004427{
Hanno Becker56e205e2018-08-16 09:06:12 +01004428 size_t alloc_len;
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004429
Hanno Becker56e205e2018-08-16 09:06:12 +01004430 alloc_len = 12; /* Handshake header */
4431 alloc_len += msg_len; /* Content buffer */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004432
Hanno Beckerd07df862018-08-16 09:14:58 +01004433 if( add_bitmap )
4434 alloc_len += msg_len / 8 + ( msg_len % 8 != 0 ); /* Bitmap */
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004435
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004436 return( alloc_len );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004437}
Hanno Becker56e205e2018-08-16 09:06:12 +01004438
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004439#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004440
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004441static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl )
Hanno Becker12555c62018-08-16 12:47:53 +01004442{
4443 return( ( ssl->in_msg[1] << 16 ) |
4444 ( ssl->in_msg[2] << 8 ) |
4445 ssl->in_msg[3] );
4446}
Hanno Beckere25e3b72018-08-16 09:30:53 +01004447
Simon Butcher99000142016-10-13 17:21:01 +01004448int mbedtls_ssl_prepare_handshake_record( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004449{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004450 if( ssl->in_msglen < mbedtls_ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004451 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004452 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too short: %d",
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004453 ssl->in_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004454 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004455 }
4456
Hanno Becker12555c62018-08-16 12:47:53 +01004457 ssl->in_hslen = mbedtls_ssl_hs_hdr_len( ssl ) + ssl_get_hs_total_len( ssl );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004458
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004459 MBEDTLS_SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004460 " %d, type = %d, hslen = %d",
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004461 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004462
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004463#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004464 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004465 {
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004466 int ret;
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004467 unsigned int recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004468
Hanno Becker44650b72018-08-16 12:51:11 +01004469 if( ssl_check_hs_header( ssl ) != 0 )
4470 {
4471 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid handshake header" ) );
4472 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4473 }
4474
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004475 if( ssl->handshake != NULL &&
Hanno Beckerc76c6192017-06-06 10:03:17 +01004476 ( ( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER &&
4477 recv_msg_seq != ssl->handshake->in_msg_seq ) ||
4478 ( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
4479 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO ) ) )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004480 {
Hanno Becker9e1ec222018-08-15 15:54:43 +01004481 if( recv_msg_seq > ssl->handshake->in_msg_seq )
4482 {
4483 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received future handshake message of sequence number %u (next %u)",
4484 recv_msg_seq,
4485 ssl->handshake->in_msg_seq ) );
4486 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4487 }
4488
Manuel Pégourié-Gonnardfc572dd2014-10-09 17:56:57 +02004489 /* Retransmit only on last message from previous flight, to avoid
4490 * too many retransmissions.
4491 * Besides, No sane server ever retransmits HelloVerifyRequest */
4492 if( recv_msg_seq == ssl->handshake->in_flight_start_seq - 1 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004493 ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004494 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004495 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received message from last flight, "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004496 "message_seq = %d, start_of_flight = %d",
4497 recv_msg_seq,
4498 ssl->handshake->in_flight_start_seq ) );
4499
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004500 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004501 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004502 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004503 return( ret );
4504 }
4505 }
4506 else
4507 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004508 MBEDTLS_SSL_DEBUG_MSG( 2, ( "dropping out-of-sequence message: "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004509 "message_seq = %d, expected = %d",
4510 recv_msg_seq,
4511 ssl->handshake->in_msg_seq ) );
4512 }
4513
Hanno Becker90333da2017-10-10 11:27:13 +01004514 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004515 }
4516 /* Wait until message completion to increment in_msg_seq */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004517
Hanno Becker6d97ef52018-08-16 13:09:04 +01004518 /* Message reassembly is handled alongside buffering of future
4519 * messages; the commonality is that both handshake fragments and
Hanno Becker83ab41c2018-08-28 17:19:38 +01004520 * future messages cannot be forwarded immediately to the
Hanno Becker6d97ef52018-08-16 13:09:04 +01004521 * handshake logic layer. */
Hanno Beckere25e3b72018-08-16 09:30:53 +01004522 if( ssl_hs_is_proper_fragment( ssl ) == 1 )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004523 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004524 MBEDTLS_SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) );
Hanno Becker6d97ef52018-08-16 13:09:04 +01004525 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004526 }
4527 }
4528 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004529#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004530 /* With TLS we don't handle fragmentation (for now) */
4531 if( ssl->in_msglen < ssl->in_hslen )
4532 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004533 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) );
4534 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004535 }
4536
Simon Butcher99000142016-10-13 17:21:01 +01004537 return( 0 );
4538}
4539
4540void mbedtls_ssl_update_handshake_status( mbedtls_ssl_context *ssl )
4541{
Hanno Becker0271f962018-08-16 13:23:47 +01004542 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Simon Butcher99000142016-10-13 17:21:01 +01004543
Hanno Becker0271f962018-08-16 13:23:47 +01004544 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER && hs != NULL )
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02004545 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004546 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02004547 }
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004548
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004549 /* Handshake message is complete, increment counter */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004550#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004551 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004552 ssl->handshake != NULL )
4553 {
Hanno Becker0271f962018-08-16 13:23:47 +01004554 unsigned offset;
4555 mbedtls_ssl_hs_buffer *hs_buf;
Hanno Beckere25e3b72018-08-16 09:30:53 +01004556
Hanno Becker0271f962018-08-16 13:23:47 +01004557 /* Increment handshake sequence number */
4558 hs->in_msg_seq++;
4559
4560 /*
4561 * Clear up handshake buffering and reassembly structure.
4562 */
4563
4564 /* Free first entry */
Hanno Beckere605b192018-08-21 15:59:07 +01004565 ssl_buffering_free_slot( ssl, 0 );
Hanno Becker0271f962018-08-16 13:23:47 +01004566
4567 /* Shift all other entries */
Hanno Beckere605b192018-08-21 15:59:07 +01004568 for( offset = 0, hs_buf = &hs->buffering.hs[0];
4569 offset + 1 < MBEDTLS_SSL_MAX_BUFFERED_HS;
Hanno Becker0271f962018-08-16 13:23:47 +01004570 offset++, hs_buf++ )
4571 {
4572 *hs_buf = *(hs_buf + 1);
4573 }
4574
4575 /* Create a fresh last entry */
4576 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004577 }
4578#endif
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004579}
4580
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004581/*
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004582 * DTLS anti-replay: RFC 6347 4.1.2.6
4583 *
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004584 * in_window is a field of bits numbered from 0 (lsb) to 63 (msb).
4585 * Bit n is set iff record number in_window_top - n has been seen.
4586 *
4587 * Usually, in_window_top is the last record number seen and the lsb of
4588 * in_window is set. The only exception is the initial state (record number 0
4589 * not seen yet).
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004590 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004591#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4592static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004593{
4594 ssl->in_window_top = 0;
4595 ssl->in_window = 0;
4596}
4597
4598static inline uint64_t ssl_load_six_bytes( unsigned char *buf )
4599{
4600 return( ( (uint64_t) buf[0] << 40 ) |
4601 ( (uint64_t) buf[1] << 32 ) |
4602 ( (uint64_t) buf[2] << 24 ) |
4603 ( (uint64_t) buf[3] << 16 ) |
4604 ( (uint64_t) buf[4] << 8 ) |
4605 ( (uint64_t) buf[5] ) );
4606}
4607
4608/*
4609 * Return 0 if sequence number is acceptable, -1 otherwise
4610 */
Hanno Becker0183d692019-07-12 08:50:37 +01004611int mbedtls_ssl_dtls_replay_check( mbedtls_ssl_context const *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004612{
4613 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
4614 uint64_t bit;
4615
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004616 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004617 return( 0 );
4618
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004619 if( rec_seqnum > ssl->in_window_top )
4620 return( 0 );
4621
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004622 bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004623
4624 if( bit >= 64 )
4625 return( -1 );
4626
4627 if( ( ssl->in_window & ( (uint64_t) 1 << bit ) ) != 0 )
4628 return( -1 );
4629
4630 return( 0 );
4631}
4632
4633/*
4634 * Update replay window on new validated record
4635 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004636void mbedtls_ssl_dtls_replay_update( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004637{
4638 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
4639
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004640 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004641 return;
4642
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004643 if( rec_seqnum > ssl->in_window_top )
4644 {
4645 /* Update window_top and the contents of the window */
4646 uint64_t shift = rec_seqnum - ssl->in_window_top;
4647
4648 if( shift >= 64 )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004649 ssl->in_window = 1;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004650 else
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004651 {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004652 ssl->in_window <<= shift;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004653 ssl->in_window |= 1;
4654 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004655
4656 ssl->in_window_top = rec_seqnum;
4657 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004658 else
4659 {
4660 /* Mark that number as seen in the current window */
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004661 uint64_t bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004662
4663 if( bit < 64 ) /* Always true, but be extra sure */
4664 ssl->in_window |= (uint64_t) 1 << bit;
4665 }
4666}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004667#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004668
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004669#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004670/* Forward declaration */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02004671static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial );
4672
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004673/*
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004674 * Without any SSL context, check if a datagram looks like a ClientHello with
4675 * a valid cookie, and if it doesn't, generate a HelloVerifyRequest message.
Simon Butcher0789aed2015-09-11 17:15:17 +01004676 * Both input and output include full DTLS headers.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004677 *
4678 * - if cookie is valid, return 0
4679 * - if ClientHello looks superficially valid but cookie is not,
4680 * fill obuf and set olen, then
4681 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
4682 * - otherwise return a specific error code
4683 */
4684static int ssl_check_dtls_clihlo_cookie(
4685 mbedtls_ssl_cookie_write_t *f_cookie_write,
4686 mbedtls_ssl_cookie_check_t *f_cookie_check,
4687 void *p_cookie,
4688 const unsigned char *cli_id, size_t cli_id_len,
4689 const unsigned char *in, size_t in_len,
4690 unsigned char *obuf, size_t buf_len, size_t *olen )
4691{
4692 size_t sid_len, cookie_len;
4693 unsigned char *p;
4694
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004695 /*
4696 * Structure of ClientHello with record and handshake headers,
4697 * and expected values. We don't need to check a lot, more checks will be
4698 * done when actually parsing the ClientHello - skipping those checks
4699 * avoids code duplication and does not make cookie forging any easier.
4700 *
4701 * 0-0 ContentType type; copied, must be handshake
4702 * 1-2 ProtocolVersion version; copied
4703 * 3-4 uint16 epoch; copied, must be 0
4704 * 5-10 uint48 sequence_number; copied
4705 * 11-12 uint16 length; (ignored)
4706 *
4707 * 13-13 HandshakeType msg_type; (ignored)
4708 * 14-16 uint24 length; (ignored)
4709 * 17-18 uint16 message_seq; copied
4710 * 19-21 uint24 fragment_offset; copied, must be 0
4711 * 22-24 uint24 fragment_length; (ignored)
4712 *
4713 * 25-26 ProtocolVersion client_version; (ignored)
4714 * 27-58 Random random; (ignored)
4715 * 59-xx SessionID session_id; 1 byte len + sid_len content
4716 * 60+ opaque cookie<0..2^8-1>; 1 byte len + content
4717 * ...
4718 *
4719 * Minimum length is 61 bytes.
4720 */
4721 if( in_len < 61 ||
4722 in[0] != MBEDTLS_SSL_MSG_HANDSHAKE ||
4723 in[3] != 0 || in[4] != 0 ||
4724 in[19] != 0 || in[20] != 0 || in[21] != 0 )
4725 {
4726 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4727 }
4728
4729 sid_len = in[59];
4730 if( sid_len > in_len - 61 )
4731 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4732
4733 cookie_len = in[60 + sid_len];
4734 if( cookie_len > in_len - 60 )
4735 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4736
4737 if( f_cookie_check( p_cookie, in + sid_len + 61, cookie_len,
4738 cli_id, cli_id_len ) == 0 )
4739 {
4740 /* Valid cookie */
4741 return( 0 );
4742 }
4743
4744 /*
4745 * If we get here, we've got an invalid cookie, let's prepare HVR.
4746 *
4747 * 0-0 ContentType type; copied
4748 * 1-2 ProtocolVersion version; copied
4749 * 3-4 uint16 epoch; copied
4750 * 5-10 uint48 sequence_number; copied
4751 * 11-12 uint16 length; olen - 13
4752 *
4753 * 13-13 HandshakeType msg_type; hello_verify_request
4754 * 14-16 uint24 length; olen - 25
4755 * 17-18 uint16 message_seq; copied
4756 * 19-21 uint24 fragment_offset; copied
4757 * 22-24 uint24 fragment_length; olen - 25
4758 *
4759 * 25-26 ProtocolVersion server_version; 0xfe 0xff
4760 * 27-27 opaque cookie<0..2^8-1>; cookie_len = olen - 27, cookie
4761 *
4762 * Minimum length is 28.
4763 */
4764 if( buf_len < 28 )
4765 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
4766
4767 /* Copy most fields and adapt others */
4768 memcpy( obuf, in, 25 );
4769 obuf[13] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST;
4770 obuf[25] = 0xfe;
4771 obuf[26] = 0xff;
4772
4773 /* Generate and write actual cookie */
4774 p = obuf + 28;
4775 if( f_cookie_write( p_cookie,
4776 &p, obuf + buf_len, cli_id, cli_id_len ) != 0 )
4777 {
4778 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4779 }
4780
4781 *olen = p - obuf;
4782
4783 /* Go back and fill length fields */
4784 obuf[27] = (unsigned char)( *olen - 28 );
4785
4786 obuf[14] = obuf[22] = (unsigned char)( ( *olen - 25 ) >> 16 );
4787 obuf[15] = obuf[23] = (unsigned char)( ( *olen - 25 ) >> 8 );
4788 obuf[16] = obuf[24] = (unsigned char)( ( *olen - 25 ) );
4789
4790 obuf[11] = (unsigned char)( ( *olen - 13 ) >> 8 );
4791 obuf[12] = (unsigned char)( ( *olen - 13 ) );
4792
4793 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
4794}
4795
4796/*
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004797 * Handle possible client reconnect with the same UDP quadruplet
4798 * (RFC 6347 Section 4.2.8).
4799 *
4800 * Called by ssl_parse_record_header() in case we receive an epoch 0 record
4801 * that looks like a ClientHello.
4802 *
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004803 * - if the input looks like a ClientHello without cookies,
4804 * send back HelloVerifyRequest, then
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004805 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004806 * - if the input looks like a ClientHello with a valid cookie,
4807 * reset the session of the current context, and
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02004808 * return MBEDTLS_ERR_SSL_CLIENT_RECONNECT
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004809 * - if anything goes wrong, return a specific error code
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004810 *
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004811 * mbedtls_ssl_read_record() will ignore the record if anything else than
Simon Butcherd0bf6a32015-09-11 17:34:49 +01004812 * MBEDTLS_ERR_SSL_CLIENT_RECONNECT or 0 is returned, although this function
4813 * cannot not return 0.
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004814 */
4815static int ssl_handle_possible_reconnect( mbedtls_ssl_context *ssl )
4816{
4817 int ret;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004818 size_t len;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004819
Hanno Becker2fddd372019-07-10 14:37:41 +01004820 if( ssl->conf->f_cookie_write == NULL ||
4821 ssl->conf->f_cookie_check == NULL )
4822 {
4823 /* If we can't use cookies to verify reachability of the peer,
4824 * drop the record. */
4825 return( 0 );
4826 }
4827
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004828 ret = ssl_check_dtls_clihlo_cookie(
4829 ssl->conf->f_cookie_write,
4830 ssl->conf->f_cookie_check,
4831 ssl->conf->p_cookie,
4832 ssl->cli_id, ssl->cli_id_len,
4833 ssl->in_buf, ssl->in_left,
Angus Grattond8213d02016-05-25 20:56:48 +10004834 ssl->out_buf, MBEDTLS_SSL_OUT_CONTENT_LEN, &len );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004835
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004836 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_check_dtls_clihlo_cookie", ret );
4837
4838 if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004839 {
Brian J Murray1903fb32016-11-06 04:45:15 -08004840 /* Don't check write errors as we can't do anything here.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004841 * If the error is permanent we'll catch it later,
4842 * if it's not, then hopefully it'll work next time. */
4843 (void) ssl->f_send( ssl->p_bio, ssl->out_buf, len );
Hanno Becker2fddd372019-07-10 14:37:41 +01004844 ret = 0;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004845 }
4846
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004847 if( ret == 0 )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004848 {
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004849 /* Got a valid cookie, partially reset context */
4850 if( ( ret = ssl_session_reset_int( ssl, 1 ) ) != 0 )
4851 {
4852 MBEDTLS_SSL_DEBUG_RET( 1, "reset", ret );
4853 return( ret );
4854 }
4855
4856 return( MBEDTLS_ERR_SSL_CLIENT_RECONNECT );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004857 }
4858
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004859 return( ret );
4860}
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004861#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004862
Hanno Beckerf661c9c2019-05-03 13:25:54 +01004863static int ssl_check_record_type( uint8_t record_type )
4864{
4865 if( record_type != MBEDTLS_SSL_MSG_HANDSHAKE &&
4866 record_type != MBEDTLS_SSL_MSG_ALERT &&
4867 record_type != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC &&
4868 record_type != MBEDTLS_SSL_MSG_APPLICATION_DATA )
4869 {
4870 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4871 }
4872
4873 return( 0 );
4874}
4875
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004876/*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004877 * ContentType type;
4878 * ProtocolVersion version;
4879 * uint16 epoch; // DTLS only
4880 * uint48 sequence_number; // DTLS only
4881 * uint16 length;
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004882 *
4883 * Return 0 if header looks sane (and, for DTLS, the record is expected)
Simon Butcher207990d2015-12-16 01:51:30 +00004884 * MBEDTLS_ERR_SSL_INVALID_RECORD if the header looks bad,
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004885 * MBEDTLS_ERR_SSL_UNEXPECTED_RECORD (DTLS only) if sane but unexpected.
4886 *
4887 * With DTLS, mbedtls_ssl_read_record() will:
Simon Butcher207990d2015-12-16 01:51:30 +00004888 * 1. proceed with the record if this function returns 0
4889 * 2. drop only the current record if this function returns UNEXPECTED_RECORD
4890 * 3. return CLIENT_RECONNECT if this function return that value
4891 * 4. drop the whole datagram if this function returns anything else.
4892 * Point 2 is needed when the peer is resending, and we have already received
4893 * the first record from a datagram but are still waiting for the others.
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004894 */
Hanno Beckere5e7e782019-07-11 12:29:35 +01004895static int ssl_parse_record_header( mbedtls_ssl_context *ssl,
4896 unsigned char *buf,
4897 size_t len,
4898 mbedtls_record *rec )
Paul Bakker5121ce52009-01-03 21:22:43 +00004899{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01004900 int major_ver, minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00004901
Hanno Beckere5e7e782019-07-11 12:29:35 +01004902 size_t const rec_hdr_type_offset = 0;
4903 size_t const rec_hdr_type_len = 1;
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02004904
Hanno Beckere5e7e782019-07-11 12:29:35 +01004905 size_t const rec_hdr_version_offset = rec_hdr_type_offset +
4906 rec_hdr_type_len;
4907 size_t const rec_hdr_version_len = 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00004908
Hanno Beckere5e7e782019-07-11 12:29:35 +01004909 size_t const rec_hdr_ctr_len = 8;
4910#if defined(MBEDTLS_SSL_PROTO_DTLS)
4911 uint32_t rec_epoch;
4912 size_t const rec_hdr_ctr_offset = rec_hdr_version_offset +
4913 rec_hdr_version_len;
4914
Hanno Beckera0e20d02019-05-15 14:03:01 +01004915#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckere5e7e782019-07-11 12:29:35 +01004916 size_t const rec_hdr_cid_offset = rec_hdr_ctr_offset +
4917 rec_hdr_ctr_len;
4918 size_t rec_hdr_cid_len = 0;
4919#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
4920#endif /* MBEDTLS_SSL_PROTO_DTLS */
4921
4922 size_t rec_hdr_len_offset; /* To be determined */
4923 size_t const rec_hdr_len_len = 2;
4924
4925 /*
4926 * Check minimum lengths for record header.
4927 */
4928
4929#if defined(MBEDTLS_SSL_PROTO_DTLS)
4930 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4931 {
4932 rec_hdr_len_offset = rec_hdr_ctr_offset + rec_hdr_ctr_len;
4933 }
4934 else
4935#endif /* MBEDTLS_SSL_PROTO_DTLS */
4936 {
4937 rec_hdr_len_offset = rec_hdr_version_offset + rec_hdr_version_len;
4938 }
4939
4940 if( len < rec_hdr_len_offset + rec_hdr_len_len )
4941 {
4942 MBEDTLS_SSL_DEBUG_MSG( 1, ( "datagram of length %u too small to hold DTLS record header of length %u",
4943 (unsigned) len,
4944 (unsigned)( rec_hdr_len_len + rec_hdr_len_len ) ) );
4945 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4946 }
4947
4948 /*
4949 * Parse and validate record content type
4950 */
4951
4952 rec->type = buf[ rec_hdr_type_offset ];
Hanno Beckere5e7e782019-07-11 12:29:35 +01004953
4954 /* Check record content type */
4955#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
4956 rec->cid_len = 0;
4957
Hanno Beckerca59c2b2019-05-08 12:03:28 +01004958 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Hanno Beckere5e7e782019-07-11 12:29:35 +01004959 ssl->conf->cid_len != 0 &&
4960 rec->type == MBEDTLS_SSL_MSG_CID )
Hanno Beckerca59c2b2019-05-08 12:03:28 +01004961 {
4962 /* Shift pointers to account for record header including CID
4963 * struct {
4964 * ContentType special_type = tls12_cid;
4965 * ProtocolVersion version;
4966 * uint16 epoch;
4967 * uint48 sequence_number;
Hanno Becker8e55b0f2019-05-23 17:03:19 +01004968 * opaque cid[cid_length]; // Additional field compared to
4969 * // default DTLS record format
Hanno Beckerca59c2b2019-05-08 12:03:28 +01004970 * uint16 length;
4971 * opaque enc_content[DTLSCiphertext.length];
4972 * } DTLSCiphertext;
4973 */
4974
4975 /* So far, we only support static CID lengths
4976 * fixed in the configuration. */
Hanno Beckere5e7e782019-07-11 12:29:35 +01004977 rec_hdr_cid_len = ssl->conf->cid_len;
4978 rec_hdr_len_offset += rec_hdr_cid_len;
Hanno Beckere538d822019-07-10 14:50:10 +01004979
Hanno Beckere5e7e782019-07-11 12:29:35 +01004980 if( len < rec_hdr_len_offset + rec_hdr_len_len )
Hanno Beckere538d822019-07-10 14:50:10 +01004981 {
Hanno Beckere5e7e782019-07-11 12:29:35 +01004982 MBEDTLS_SSL_DEBUG_MSG( 1, ( "datagram of length %u too small to hold DTLS record header including CID, length %u",
4983 (unsigned) len,
4984 (unsigned)( rec_hdr_len_offset + rec_hdr_len_len ) ) );
Hanno Becker59be60e2019-07-10 14:53:43 +01004985 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Beckere538d822019-07-10 14:50:10 +01004986 }
Hanno Beckere5e7e782019-07-11 12:29:35 +01004987
4988 rec->cid_len = rec_hdr_cid_len;
4989 memcpy( rec->cid, buf + rec_hdr_cid_offset, rec_hdr_cid_len );
Hanno Beckerca59c2b2019-05-08 12:03:28 +01004990 }
4991 else
Hanno Beckera0e20d02019-05-15 14:03:01 +01004992#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004993 {
Hanno Beckere5e7e782019-07-11 12:29:35 +01004994 if( ssl_check_record_type( rec->type ) )
4995 {
4996 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
4997 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4998 }
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004999 }
5000
Hanno Beckere5e7e782019-07-11 12:29:35 +01005001 /*
5002 * Parse and validate record version
5003 */
5004
5005 memcpy( &rec->ver[0],
5006 buf + rec_hdr_version_offset,
5007 rec_hdr_version_len );
5008
5009 mbedtls_ssl_read_version( &major_ver, &minor_ver,
5010 ssl->conf->transport,
5011 buf + rec_hdr_version_offset );
5012
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005013 if( major_ver != ssl->major_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00005014 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005015 MBEDTLS_SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
5016 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00005017 }
5018
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005019 if( minor_ver > ssl->conf->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00005020 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005021 MBEDTLS_SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
5022 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00005023 }
5024
Hanno Beckere5e7e782019-07-11 12:29:35 +01005025 /*
5026 * Parse/Copy record sequence number.
5027 */
Hanno Beckerca59c2b2019-05-08 12:03:28 +01005028
Hanno Beckere5e7e782019-07-11 12:29:35 +01005029#if defined(MBEDTLS_SSL_PROTO_DTLS)
5030 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5031 {
5032 /* Copy explicit record sequence number from input buffer. */
5033 memcpy( &rec->ctr[0], buf + rec_hdr_ctr_offset,
5034 rec_hdr_ctr_len );
5035 }
5036 else
5037#endif /* MBEDTLS_SSL_PROTO_DTLS */
5038 {
5039 /* Copy implicit record sequence number from SSL context structure. */
5040 memcpy( &rec->ctr[0], ssl->in_ctr, rec_hdr_ctr_len );
5041 }
Hanno Beckerca59c2b2019-05-08 12:03:28 +01005042
Hanno Beckere5e7e782019-07-11 12:29:35 +01005043 /*
5044 * Parse record length.
5045 */
5046
5047#define READ_UINT16_BE( p ) \
5048 ( ( *( (unsigned char*)( p ) + 0 ) << 8 ) | \
5049 ( *( (unsigned char*)( p ) + 1 ) << 0 ) )
5050
5051 rec->data_offset = rec_hdr_len_offset + rec_hdr_len_len;
5052 rec->data_len = (size_t) READ_UINT16_BE( buf + rec_hdr_len_offset );
5053 MBEDTLS_SSL_DEBUG_BUF( 4, "input record header", buf, rec->data_offset );
5054
Hanno Beckerca59c2b2019-05-08 12:03:28 +01005055 MBEDTLS_SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
Hanno Becker92d30f52019-05-23 17:03:44 +01005056 "version = [%d:%d], msglen = %d",
Hanno Beckere5e7e782019-07-11 12:29:35 +01005057 rec->type,
5058 major_ver, minor_ver, rec->data_len ) );
5059
5060 rec->buf = buf;
5061 rec->buf_len = rec->data_offset + rec->data_len;
Hanno Beckerca59c2b2019-05-08 12:03:28 +01005062
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005063 /*
Hanno Becker52c6dc62017-05-26 16:07:36 +01005064 * DTLS-related tests.
5065 * Check epoch before checking length constraint because
5066 * the latter varies with the epoch. E.g., if a ChangeCipherSpec
5067 * message gets duplicated before the corresponding Finished message,
5068 * the second ChangeCipherSpec should be discarded because it belongs
5069 * to an old epoch, but not because its length is shorter than
5070 * the minimum record length for packets using the new record transform.
5071 * Note that these two kinds of failures are handled differently,
5072 * as an unexpected record is silently skipped but an invalid
5073 * record leads to the entire datagram being dropped.
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005074 */
5075#if defined(MBEDTLS_SSL_PROTO_DTLS)
5076 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5077 {
Hanno Beckere5e7e782019-07-11 12:29:35 +01005078 rec_epoch = ( rec->ctr[0] << 8 ) | rec->ctr[1];
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005079
Hanno Becker955a5c92019-07-10 17:12:07 +01005080 /* Check that the datagram is large enough to contain a record
5081 * of the advertised length. */
Hanno Beckere5e7e782019-07-11 12:29:35 +01005082 if( len < rec->data_offset + rec->data_len )
Hanno Becker955a5c92019-07-10 17:12:07 +01005083 {
Hanno Beckere5e7e782019-07-11 12:29:35 +01005084 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Datagram of length %u too small to contain record of advertised length %u.",
5085 (unsigned) len,
5086 (unsigned)( rec->data_offset + rec->data_len ) ) );
Hanno Becker955a5c92019-07-10 17:12:07 +01005087 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5088 }
Hanno Becker37cfe732019-07-10 17:20:01 +01005089
5090 /* Records from the next epoch are considered for buffering
5091 * (concretely: early Finished messages). */
Hanno Becker2fddd372019-07-10 14:37:41 +01005092 if( rec_epoch == (unsigned) ssl->in_epoch + 1 )
5093 {
Hanno Becker2fddd372019-07-10 14:37:41 +01005094 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Consider record for buffering" ) );
5095 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
5096 }
Hanno Becker37cfe732019-07-10 17:20:01 +01005097 /* Records from other, non-matching epochs are silently discarded.
5098 * (The case of same-port Client reconnects must be considered in
5099 * the caller). */
Hanno Becker2fddd372019-07-10 14:37:41 +01005100 else if( rec_epoch != ssl->in_epoch )
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005101 {
5102 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record from another epoch: "
5103 "expected %d, received %d",
5104 ssl->in_epoch, rec_epoch ) );
Hanno Becker2fddd372019-07-10 14:37:41 +01005105 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005106 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005107#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Hanno Becker37cfe732019-07-10 17:20:01 +01005108 /* For records from the correct epoch, check whether their
5109 * sequence number has been seen before. */
Hanno Becker2fddd372019-07-10 14:37:41 +01005110 else if( mbedtls_ssl_dtls_replay_check( ssl ) != 0 )
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005111 {
5112 MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record" ) );
5113 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
5114 }
5115#endif
5116 }
5117#endif /* MBEDTLS_SSL_PROTO_DTLS */
5118
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005119 return( 0 );
5120}
Paul Bakker5121ce52009-01-03 21:22:43 +00005121
Hanno Becker2fddd372019-07-10 14:37:41 +01005122
5123#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
5124static int ssl_check_client_reconnect( mbedtls_ssl_context *ssl )
5125{
5126 unsigned int rec_epoch = ( ssl->in_ctr[0] << 8 ) | ssl->in_ctr[1];
5127
5128 /*
5129 * Check for an epoch 0 ClientHello. We can't use in_msg here to
5130 * access the first byte of record content (handshake type), as we
5131 * have an active transform (possibly iv_len != 0), so use the
5132 * fact that the record header len is 13 instead.
5133 */
5134 if( rec_epoch == 0 &&
5135 ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
5136 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
5137 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
5138 ssl->in_left > 13 &&
5139 ssl->in_buf[13] == MBEDTLS_SSL_HS_CLIENT_HELLO )
5140 {
5141 MBEDTLS_SSL_DEBUG_MSG( 1, ( "possible client reconnect "
5142 "from the same port" ) );
5143 return( ssl_handle_possible_reconnect( ssl ) );
5144 }
5145
5146 return( 0 );
5147}
5148#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
5149
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005150/*
5151 * If applicable, decrypt (and decompress) record content
5152 */
Hanno Beckerfdf66042019-07-11 13:07:45 +01005153static int ssl_prepare_record_content( mbedtls_ssl_context *ssl,
5154 mbedtls_record *rec )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005155{
5156 int ret, done = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02005157
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005158 MBEDTLS_SSL_DEBUG_BUF( 4, "input record from network",
Hanno Beckerfdf66042019-07-11 13:07:45 +01005159 rec->buf, rec->buf_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00005160
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005161#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
5162 if( mbedtls_ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00005163 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005164 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_read()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00005165
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005166 ret = mbedtls_ssl_hw_record_read( ssl );
5167 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00005168 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005169 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_read", ret );
5170 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00005171 }
Paul Bakkerc7878112012-12-19 14:41:14 +01005172
5173 if( ret == 0 )
5174 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00005175 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005176#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00005177 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005178 {
Hanno Becker58ef0bf2019-07-12 09:35:58 +01005179 unsigned char const old_msg_type = rec->type;
5180
Hanno Beckera18d1322018-01-03 14:27:32 +00005181 if( ( ret = mbedtls_ssl_decrypt_buf( ssl, ssl->transform_in,
Hanno Beckerfdf66042019-07-11 13:07:45 +01005182 rec ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005183 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005184 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
Hanno Becker8367ccc2019-05-14 11:30:10 +01005185
Hanno Beckera0e20d02019-05-15 14:03:01 +01005186#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker8367ccc2019-05-14 11:30:10 +01005187 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_CID &&
5188 ssl->conf->ignore_unexpected_cid
5189 == MBEDTLS_SSL_UNEXPECTED_CID_IGNORE )
5190 {
Hanno Beckere8d6afd2019-05-24 10:11:06 +01005191 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ignoring unexpected CID" ) );
Hanno Becker16ded982019-05-08 13:02:55 +01005192 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
Hanno Becker8367ccc2019-05-14 11:30:10 +01005193 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01005194#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker16ded982019-05-08 13:02:55 +01005195
Paul Bakker5121ce52009-01-03 21:22:43 +00005196 return( ret );
5197 }
5198
Hanno Becker58ef0bf2019-07-12 09:35:58 +01005199 if( old_msg_type != rec->type )
Hanno Becker6430faf2019-05-08 11:57:13 +01005200 {
5201 MBEDTLS_SSL_DEBUG_MSG( 4, ( "record type after decrypt (before %d): %d",
Hanno Becker58ef0bf2019-07-12 09:35:58 +01005202 old_msg_type, rec->type ) );
Hanno Becker6430faf2019-05-08 11:57:13 +01005203 }
5204
Hanno Becker1c0c37f2018-08-07 14:29:29 +01005205 MBEDTLS_SSL_DEBUG_BUF( 4, "input payload after decrypt",
Hanno Becker58ef0bf2019-07-12 09:35:58 +01005206 rec->buf + rec->data_offset, rec->data_len );
Hanno Becker1c0c37f2018-08-07 14:29:29 +01005207
Hanno Beckera0e20d02019-05-15 14:03:01 +01005208#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker6430faf2019-05-08 11:57:13 +01005209 /* We have already checked the record content type
5210 * in ssl_parse_record_header(), failing or silently
5211 * dropping the record in the case of an unknown type.
5212 *
5213 * Since with the use of CIDs, the record content type
5214 * might change during decryption, re-check the record
5215 * content type, but treat a failure as fatal this time. */
Hanno Becker58ef0bf2019-07-12 09:35:58 +01005216 if( ssl_check_record_type( rec->type ) )
Hanno Becker6430faf2019-05-08 11:57:13 +01005217 {
5218 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
5219 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5220 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01005221#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker6430faf2019-05-08 11:57:13 +01005222
Hanno Becker58ef0bf2019-07-12 09:35:58 +01005223 if( rec->data_len == 0 )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00005224 {
5225#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5226 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3
Hanno Becker58ef0bf2019-07-12 09:35:58 +01005227 && rec->type != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00005228 {
5229 /* TLS v1.2 explicitly disallows zero-length messages which are not application data */
5230 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid zero-length message type: %d", ssl->in_msgtype ) );
5231 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5232 }
5233#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
5234
5235 ssl->nb_zero++;
5236
5237 /*
5238 * Three or more empty messages may be a DoS attack
5239 * (excessive CPU consumption).
5240 */
5241 if( ssl->nb_zero > 3 )
5242 {
5243 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
Hanno Becker6e7700d2019-05-08 10:38:32 +01005244 "messages, possible DoS attack" ) );
5245 /* Treat the records as if they were not properly authenticated,
5246 * thereby failing the connection if we see more than allowed
5247 * by the configured bad MAC threshold. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00005248 return( MBEDTLS_ERR_SSL_INVALID_MAC );
5249 }
5250 }
5251 else
5252 ssl->nb_zero = 0;
5253
5254#if defined(MBEDTLS_SSL_PROTO_DTLS)
5255 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5256 {
5257 ; /* in_ctr read from peer, not maintained internally */
5258 }
5259 else
5260#endif
5261 {
5262 unsigned i;
5263 for( i = 8; i > ssl_ep_len( ssl ); i-- )
5264 if( ++ssl->in_ctr[i - 1] != 0 )
5265 break;
5266
5267 /* The loop goes to its end iff the counter is wrapping */
5268 if( i == ssl_ep_len( ssl ) )
5269 {
5270 MBEDTLS_SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
5271 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
5272 }
5273 }
5274
Paul Bakker5121ce52009-01-03 21:22:43 +00005275 }
5276
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005277#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00005278 if( ssl->transform_in != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005279 ssl->session_in->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00005280 {
5281 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
5282 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005283 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00005284 return( ret );
5285 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00005286 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005287#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00005288
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005289#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005290 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02005291 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005292 mbedtls_ssl_dtls_replay_update( ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02005293 }
5294#endif
5295
Hanno Beckerd96e10b2019-07-09 17:30:02 +01005296 /* Check actual (decrypted) record content length against
5297 * configured maximum. */
5298 if( ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
5299 {
5300 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
5301 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5302 }
5303
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005304 return( 0 );
5305}
5306
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005307static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005308
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005309/*
5310 * Read a record.
5311 *
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005312 * Silently ignore non-fatal alert (and for DTLS, invalid records as well,
5313 * RFC 6347 4.1.2.7) and continue reading until a valid record is found.
5314 *
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005315 */
Hanno Becker1097b342018-08-15 14:09:41 +01005316
5317/* Helper functions for mbedtls_ssl_read_record(). */
5318static int ssl_consume_current_message( mbedtls_ssl_context *ssl );
Hanno Beckere74d5562018-08-15 14:26:08 +01005319static int ssl_get_next_record( mbedtls_ssl_context *ssl );
5320static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl );
Hanno Becker4162b112018-08-15 14:05:04 +01005321
Hanno Becker327c93b2018-08-15 13:56:18 +01005322int mbedtls_ssl_read_record( mbedtls_ssl_context *ssl,
Hanno Becker3a0aad12018-08-20 09:44:02 +01005323 unsigned update_hs_digest )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005324{
5325 int ret;
5326
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005327 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read record" ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005328
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005329 if( ssl->keep_current_message == 0 )
5330 {
5331 do {
Simon Butcher99000142016-10-13 17:21:01 +01005332
Hanno Becker26994592018-08-15 14:14:59 +01005333 ret = ssl_consume_current_message( ssl );
Hanno Becker90333da2017-10-10 11:27:13 +01005334 if( ret != 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005335 return( ret );
Hanno Becker26994592018-08-15 14:14:59 +01005336
Hanno Beckere74d5562018-08-15 14:26:08 +01005337 if( ssl_record_is_in_progress( ssl ) == 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005338 {
Hanno Becker40f50842018-08-15 14:48:01 +01005339#if defined(MBEDTLS_SSL_PROTO_DTLS)
5340 int have_buffered = 0;
Hanno Beckere74d5562018-08-15 14:26:08 +01005341
Hanno Becker40f50842018-08-15 14:48:01 +01005342 /* We only check for buffered messages if the
5343 * current datagram is fully consumed. */
5344 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005345 ssl_next_record_is_in_datagram( ssl ) == 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01005346 {
Hanno Becker40f50842018-08-15 14:48:01 +01005347 if( ssl_load_buffered_message( ssl ) == 0 )
5348 have_buffered = 1;
5349 }
5350
5351 if( have_buffered == 0 )
5352#endif /* MBEDTLS_SSL_PROTO_DTLS */
5353 {
5354 ret = ssl_get_next_record( ssl );
5355 if( ret == MBEDTLS_ERR_SSL_CONTINUE_PROCESSING )
5356 continue;
5357
5358 if( ret != 0 )
5359 {
Hanno Beckerc573ac32018-08-28 17:15:25 +01005360 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_get_next_record" ), ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005361 return( ret );
5362 }
Hanno Beckere74d5562018-08-15 14:26:08 +01005363 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005364 }
5365
5366 ret = mbedtls_ssl_handle_message_type( ssl );
5367
Hanno Becker40f50842018-08-15 14:48:01 +01005368#if defined(MBEDTLS_SSL_PROTO_DTLS)
5369 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
5370 {
5371 /* Buffer future message */
5372 ret = ssl_buffer_message( ssl );
5373 if( ret != 0 )
5374 return( ret );
5375
5376 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
5377 }
5378#endif /* MBEDTLS_SSL_PROTO_DTLS */
5379
Hanno Becker90333da2017-10-10 11:27:13 +01005380 } while( MBEDTLS_ERR_SSL_NON_FATAL == ret ||
5381 MBEDTLS_ERR_SSL_CONTINUE_PROCESSING == ret );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005382
5383 if( 0 != ret )
Simon Butcher99000142016-10-13 17:21:01 +01005384 {
Hanno Becker05c4fc82017-11-09 14:34:06 +00005385 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_handle_message_type" ), ret );
Simon Butcher99000142016-10-13 17:21:01 +01005386 return( ret );
5387 }
5388
Hanno Becker327c93b2018-08-15 13:56:18 +01005389 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
Hanno Becker3a0aad12018-08-20 09:44:02 +01005390 update_hs_digest == 1 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005391 {
5392 mbedtls_ssl_update_handshake_status( ssl );
5393 }
Simon Butcher99000142016-10-13 17:21:01 +01005394 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005395 else
Simon Butcher99000142016-10-13 17:21:01 +01005396 {
Hanno Becker02f59072018-08-15 14:00:24 +01005397 MBEDTLS_SSL_DEBUG_MSG( 2, ( "reuse previously read message" ) );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005398 ssl->keep_current_message = 0;
Simon Butcher99000142016-10-13 17:21:01 +01005399 }
5400
5401 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read record" ) );
5402
5403 return( 0 );
5404}
5405
Hanno Becker40f50842018-08-15 14:48:01 +01005406#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005407static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl )
Simon Butcher99000142016-10-13 17:21:01 +01005408{
Hanno Becker40f50842018-08-15 14:48:01 +01005409 if( ssl->in_left > ssl->next_record_offset )
5410 return( 1 );
Simon Butcher99000142016-10-13 17:21:01 +01005411
Hanno Becker40f50842018-08-15 14:48:01 +01005412 return( 0 );
5413}
5414
5415static int ssl_load_buffered_message( mbedtls_ssl_context *ssl )
5416{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005417 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker37f95322018-08-16 13:55:32 +01005418 mbedtls_ssl_hs_buffer * hs_buf;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005419 int ret = 0;
5420
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005421 if( hs == NULL )
5422 return( -1 );
5423
Hanno Beckere00ae372018-08-20 09:39:42 +01005424 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_messsage" ) );
5425
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005426 if( ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC ||
5427 ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
5428 {
5429 /* Check if we have seen a ChangeCipherSpec before.
5430 * If yes, synthesize a CCS record. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01005431 if( !hs->buffering.seen_ccs )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005432 {
5433 MBEDTLS_SSL_DEBUG_MSG( 2, ( "CCS not seen in the current flight" ) );
5434 ret = -1;
Hanno Becker0d4b3762018-08-20 09:36:59 +01005435 goto exit;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005436 }
5437
Hanno Becker39b8bc92018-08-28 17:17:13 +01005438 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Injecting buffered CCS message" ) );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005439 ssl->in_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
5440 ssl->in_msglen = 1;
5441 ssl->in_msg[0] = 1;
5442
5443 /* As long as they are equal, the exact value doesn't matter. */
5444 ssl->in_left = 0;
5445 ssl->next_record_offset = 0;
5446
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01005447 hs->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005448 goto exit;
5449 }
Hanno Becker37f95322018-08-16 13:55:32 +01005450
Hanno Beckerb8f50142018-08-28 10:01:34 +01005451#if defined(MBEDTLS_DEBUG_C)
Hanno Becker37f95322018-08-16 13:55:32 +01005452 /* Debug only */
5453 {
5454 unsigned offset;
5455 for( offset = 1; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
5456 {
5457 hs_buf = &hs->buffering.hs[offset];
5458 if( hs_buf->is_valid == 1 )
5459 {
5460 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Future message with sequence number %u %s buffered.",
5461 hs->in_msg_seq + offset,
Hanno Beckera591c482018-08-28 17:20:00 +01005462 hs_buf->is_complete ? "fully" : "partially" ) );
Hanno Becker37f95322018-08-16 13:55:32 +01005463 }
5464 }
5465 }
Hanno Beckerb8f50142018-08-28 10:01:34 +01005466#endif /* MBEDTLS_DEBUG_C */
Hanno Becker37f95322018-08-16 13:55:32 +01005467
5468 /* Check if we have buffered and/or fully reassembled the
5469 * next handshake message. */
5470 hs_buf = &hs->buffering.hs[0];
5471 if( ( hs_buf->is_valid == 1 ) && ( hs_buf->is_complete == 1 ) )
5472 {
5473 /* Synthesize a record containing the buffered HS message. */
5474 size_t msg_len = ( hs_buf->data[1] << 16 ) |
5475 ( hs_buf->data[2] << 8 ) |
5476 hs_buf->data[3];
5477
5478 /* Double-check that we haven't accidentally buffered
5479 * a message that doesn't fit into the input buffer. */
5480 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
5481 {
5482 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5483 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5484 }
5485
5486 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message has been buffered - load" ) );
5487 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered handshake message (incl. header)",
5488 hs_buf->data, msg_len + 12 );
5489
5490 ssl->in_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
5491 ssl->in_hslen = msg_len + 12;
5492 ssl->in_msglen = msg_len + 12;
5493 memcpy( ssl->in_msg, hs_buf->data, ssl->in_hslen );
5494
5495 ret = 0;
5496 goto exit;
5497 }
5498 else
5499 {
5500 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message %u not or only partially bufffered",
5501 hs->in_msg_seq ) );
5502 }
5503
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005504 ret = -1;
5505
5506exit:
5507
5508 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_message" ) );
5509 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005510}
5511
Hanno Beckera02b0b42018-08-21 17:20:27 +01005512static int ssl_buffer_make_space( mbedtls_ssl_context *ssl,
5513 size_t desired )
5514{
5515 int offset;
5516 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005517 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Attempt to free buffered messages to have %u bytes available",
5518 (unsigned) desired ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005519
Hanno Becker01315ea2018-08-21 17:22:17 +01005520 /* Get rid of future records epoch first, if such exist. */
5521 ssl_free_buffered_record( ssl );
5522
5523 /* Check if we have enough space available now. */
5524 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5525 hs->buffering.total_bytes_buffered ) )
5526 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005527 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing future epoch record" ) );
Hanno Becker01315ea2018-08-21 17:22:17 +01005528 return( 0 );
5529 }
Hanno Beckera02b0b42018-08-21 17:20:27 +01005530
Hanno Becker4f432ad2018-08-28 10:02:32 +01005531 /* We don't have enough space to buffer the next expected handshake
5532 * message. Remove buffers used for future messages to gain space,
5533 * starting with the most distant one. */
Hanno Beckera02b0b42018-08-21 17:20:27 +01005534 for( offset = MBEDTLS_SSL_MAX_BUFFERED_HS - 1;
5535 offset >= 0; offset-- )
5536 {
5537 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Free buffering slot %d to make space for reassembly of next handshake message",
5538 offset ) );
5539
Hanno Beckerb309b922018-08-23 13:18:05 +01005540 ssl_buffering_free_slot( ssl, (uint8_t) offset );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005541
5542 /* Check if we have enough space available now. */
5543 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5544 hs->buffering.total_bytes_buffered ) )
5545 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005546 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing buffered HS messages" ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005547 return( 0 );
5548 }
5549 }
5550
5551 return( -1 );
5552}
5553
Hanno Becker40f50842018-08-15 14:48:01 +01005554static int ssl_buffer_message( mbedtls_ssl_context *ssl )
5555{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005556 int ret = 0;
5557 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5558
5559 if( hs == NULL )
5560 return( 0 );
5561
5562 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_buffer_message" ) );
5563
5564 switch( ssl->in_msgtype )
5565 {
5566 case MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC:
5567 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Remember CCS message" ) );
Hanno Beckere678eaa2018-08-21 14:57:46 +01005568
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01005569 hs->buffering.seen_ccs = 1;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005570 break;
5571
5572 case MBEDTLS_SSL_MSG_HANDSHAKE:
Hanno Becker37f95322018-08-16 13:55:32 +01005573 {
5574 unsigned recv_msg_seq_offset;
5575 unsigned recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
5576 mbedtls_ssl_hs_buffer *hs_buf;
5577 size_t msg_len = ssl->in_hslen - 12;
5578
5579 /* We should never receive an old handshake
5580 * message - double-check nonetheless. */
5581 if( recv_msg_seq < ssl->handshake->in_msg_seq )
5582 {
5583 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5584 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5585 }
5586
5587 recv_msg_seq_offset = recv_msg_seq - ssl->handshake->in_msg_seq;
5588 if( recv_msg_seq_offset >= MBEDTLS_SSL_MAX_BUFFERED_HS )
5589 {
5590 /* Silently ignore -- message too far in the future */
5591 MBEDTLS_SSL_DEBUG_MSG( 2,
5592 ( "Ignore future HS message with sequence number %u, "
5593 "buffering window %u - %u",
5594 recv_msg_seq, ssl->handshake->in_msg_seq,
5595 ssl->handshake->in_msg_seq + MBEDTLS_SSL_MAX_BUFFERED_HS - 1 ) );
5596
5597 goto exit;
5598 }
5599
5600 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering HS message with sequence number %u, offset %u ",
5601 recv_msg_seq, recv_msg_seq_offset ) );
5602
5603 hs_buf = &hs->buffering.hs[ recv_msg_seq_offset ];
5604
5605 /* Check if the buffering for this seq nr has already commenced. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01005606 if( !hs_buf->is_valid )
Hanno Becker37f95322018-08-16 13:55:32 +01005607 {
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005608 size_t reassembly_buf_sz;
5609
Hanno Becker37f95322018-08-16 13:55:32 +01005610 hs_buf->is_fragmented =
5611 ( ssl_hs_is_proper_fragment( ssl ) == 1 );
5612
5613 /* We copy the message back into the input buffer
5614 * after reassembly, so check that it's not too large.
5615 * This is an implementation-specific limitation
5616 * and not one from the standard, hence it is not
5617 * checked in ssl_check_hs_header(). */
Hanno Becker96a6c692018-08-21 15:56:03 +01005618 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker37f95322018-08-16 13:55:32 +01005619 {
5620 /* Ignore message */
5621 goto exit;
5622 }
5623
Hanno Beckere0b150f2018-08-21 15:51:03 +01005624 /* Check if we have enough space to buffer the message. */
5625 if( hs->buffering.total_bytes_buffered >
5626 MBEDTLS_SSL_DTLS_MAX_BUFFERING )
5627 {
5628 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5629 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5630 }
5631
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005632 reassembly_buf_sz = ssl_get_reassembly_buffer_size( msg_len,
5633 hs_buf->is_fragmented );
Hanno Beckere0b150f2018-08-21 15:51:03 +01005634
5635 if( reassembly_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5636 hs->buffering.total_bytes_buffered ) )
5637 {
5638 if( recv_msg_seq_offset > 0 )
5639 {
5640 /* If we can't buffer a future message because
5641 * of space limitations -- ignore. */
5642 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",
5643 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5644 (unsigned) hs->buffering.total_bytes_buffered ) );
5645 goto exit;
5646 }
Hanno Beckere1801392018-08-21 16:51:05 +01005647 else
5648 {
5649 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",
5650 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5651 (unsigned) hs->buffering.total_bytes_buffered ) );
5652 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005653
Hanno Beckera02b0b42018-08-21 17:20:27 +01005654 if( ssl_buffer_make_space( ssl, reassembly_buf_sz ) != 0 )
Hanno Becker55e9e2a2018-08-21 16:07:55 +01005655 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005656 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",
5657 (unsigned) msg_len,
5658 (unsigned) reassembly_buf_sz,
5659 MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Hanno Beckere0b150f2018-08-21 15:51:03 +01005660 (unsigned) hs->buffering.total_bytes_buffered ) );
Hanno Becker55e9e2a2018-08-21 16:07:55 +01005661 ret = MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
5662 goto exit;
5663 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005664 }
5665
5666 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %d",
5667 msg_len ) );
5668
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005669 hs_buf->data = mbedtls_calloc( 1, reassembly_buf_sz );
5670 if( hs_buf->data == NULL )
Hanno Becker37f95322018-08-16 13:55:32 +01005671 {
Hanno Beckere0b150f2018-08-21 15:51:03 +01005672 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
Hanno Becker37f95322018-08-16 13:55:32 +01005673 goto exit;
5674 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005675 hs_buf->data_len = reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01005676
5677 /* Prepare final header: copy msg_type, length and message_seq,
5678 * then add standardised fragment_offset and fragment_length */
5679 memcpy( hs_buf->data, ssl->in_msg, 6 );
5680 memset( hs_buf->data + 6, 0, 3 );
5681 memcpy( hs_buf->data + 9, hs_buf->data + 1, 3 );
5682
5683 hs_buf->is_valid = 1;
Hanno Beckere0b150f2018-08-21 15:51:03 +01005684
5685 hs->buffering.total_bytes_buffered += reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01005686 }
5687 else
5688 {
5689 /* Make sure msg_type and length are consistent */
5690 if( memcmp( hs_buf->data, ssl->in_msg, 4 ) != 0 )
5691 {
5692 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Fragment header mismatch - ignore" ) );
5693 /* Ignore */
5694 goto exit;
5695 }
5696 }
5697
Hanno Becker4422bbb2018-08-20 09:40:19 +01005698 if( !hs_buf->is_complete )
Hanno Becker37f95322018-08-16 13:55:32 +01005699 {
5700 size_t frag_len, frag_off;
5701 unsigned char * const msg = hs_buf->data + 12;
5702
5703 /*
5704 * Check and copy current fragment
5705 */
5706
5707 /* Validation of header fields already done in
5708 * mbedtls_ssl_prepare_handshake_record(). */
5709 frag_off = ssl_get_hs_frag_off( ssl );
5710 frag_len = ssl_get_hs_frag_len( ssl );
5711
5712 MBEDTLS_SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %d, length = %d",
5713 frag_off, frag_len ) );
5714 memcpy( msg + frag_off, ssl->in_msg + 12, frag_len );
5715
5716 if( hs_buf->is_fragmented )
5717 {
5718 unsigned char * const bitmask = msg + msg_len;
5719 ssl_bitmask_set( bitmask, frag_off, frag_len );
5720 hs_buf->is_complete = ( ssl_bitmask_check( bitmask,
5721 msg_len ) == 0 );
5722 }
5723 else
5724 {
5725 hs_buf->is_complete = 1;
5726 }
5727
5728 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message %scomplete",
5729 hs_buf->is_complete ? "" : "not yet " ) );
5730 }
5731
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005732 break;
Hanno Becker37f95322018-08-16 13:55:32 +01005733 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005734
5735 default:
Hanno Becker360bef32018-08-28 10:04:33 +01005736 /* We don't buffer other types of messages. */
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005737 break;
5738 }
5739
5740exit:
5741
5742 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_buffer_message" ) );
5743 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005744}
5745#endif /* MBEDTLS_SSL_PROTO_DTLS */
5746
Hanno Becker1097b342018-08-15 14:09:41 +01005747static int ssl_consume_current_message( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005748{
Hanno Becker4a810fb2017-05-24 16:27:30 +01005749 /*
Hanno Becker4a810fb2017-05-24 16:27:30 +01005750 * Consume last content-layer message and potentially
5751 * update in_msglen which keeps track of the contents'
5752 * consumption state.
5753 *
5754 * (1) Handshake messages:
5755 * Remove last handshake message, move content
5756 * and adapt in_msglen.
5757 *
5758 * (2) Alert messages:
5759 * Consume whole record content, in_msglen = 0.
5760 *
Hanno Becker4a810fb2017-05-24 16:27:30 +01005761 * (3) Change cipher spec:
5762 * Consume whole record content, in_msglen = 0.
5763 *
5764 * (4) Application data:
5765 * Don't do anything - the record layer provides
5766 * the application data as a stream transport
5767 * and consumes through mbedtls_ssl_read only.
5768 *
5769 */
5770
5771 /* Case (1): Handshake messages */
5772 if( ssl->in_hslen != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005773 {
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01005774 /* Hard assertion to be sure that no application data
5775 * is in flight, as corrupting ssl->in_msglen during
5776 * ssl->in_offt != NULL is fatal. */
5777 if( ssl->in_offt != NULL )
5778 {
5779 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5780 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5781 }
5782
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005783 /*
5784 * Get next Handshake message in the current record
5785 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005786
Hanno Becker4a810fb2017-05-24 16:27:30 +01005787 /* Notes:
Hanno Beckere72489d2017-10-23 13:23:50 +01005788 * (1) in_hslen is not necessarily the size of the
Hanno Becker4a810fb2017-05-24 16:27:30 +01005789 * current handshake content: If DTLS handshake
5790 * fragmentation is used, that's the fragment
5791 * size instead. Using the total handshake message
Hanno Beckere72489d2017-10-23 13:23:50 +01005792 * size here is faulty and should be changed at
5793 * some point.
Hanno Becker4a810fb2017-05-24 16:27:30 +01005794 * (2) While it doesn't seem to cause problems, one
5795 * has to be very careful not to assume that in_hslen
5796 * is always <= in_msglen in a sensible communication.
5797 * Again, it's wrong for DTLS handshake fragmentation.
5798 * The following check is therefore mandatory, and
5799 * should not be treated as a silently corrected assertion.
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01005800 * Additionally, ssl->in_hslen might be arbitrarily out of
5801 * bounds after handling a DTLS message with an unexpected
5802 * sequence number, see mbedtls_ssl_prepare_handshake_record.
Hanno Becker4a810fb2017-05-24 16:27:30 +01005803 */
5804 if( ssl->in_hslen < ssl->in_msglen )
5805 {
5806 ssl->in_msglen -= ssl->in_hslen;
5807 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
5808 ssl->in_msglen );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005809
Hanno Becker4a810fb2017-05-24 16:27:30 +01005810 MBEDTLS_SSL_DEBUG_BUF( 4, "remaining content in record",
5811 ssl->in_msg, ssl->in_msglen );
5812 }
5813 else
5814 {
5815 ssl->in_msglen = 0;
5816 }
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02005817
Hanno Becker4a810fb2017-05-24 16:27:30 +01005818 ssl->in_hslen = 0;
5819 }
5820 /* Case (4): Application data */
5821 else if( ssl->in_offt != NULL )
5822 {
5823 return( 0 );
5824 }
5825 /* Everything else (CCS & Alerts) */
5826 else
5827 {
5828 ssl->in_msglen = 0;
5829 }
5830
Hanno Becker1097b342018-08-15 14:09:41 +01005831 return( 0 );
5832}
Hanno Becker4a810fb2017-05-24 16:27:30 +01005833
Hanno Beckere74d5562018-08-15 14:26:08 +01005834static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl )
5835{
Hanno Becker4a810fb2017-05-24 16:27:30 +01005836 if( ssl->in_msglen > 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01005837 return( 1 );
5838
5839 return( 0 );
5840}
5841
Hanno Becker5f066e72018-08-16 14:56:31 +01005842#if defined(MBEDTLS_SSL_PROTO_DTLS)
5843
5844static void ssl_free_buffered_record( mbedtls_ssl_context *ssl )
5845{
5846 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5847 if( hs == NULL )
5848 return;
5849
Hanno Becker01315ea2018-08-21 17:22:17 +01005850 if( hs->buffering.future_record.data != NULL )
Hanno Becker4a810fb2017-05-24 16:27:30 +01005851 {
Hanno Becker01315ea2018-08-21 17:22:17 +01005852 hs->buffering.total_bytes_buffered -=
5853 hs->buffering.future_record.len;
5854
5855 mbedtls_free( hs->buffering.future_record.data );
5856 hs->buffering.future_record.data = NULL;
5857 }
Hanno Becker5f066e72018-08-16 14:56:31 +01005858}
5859
5860static int ssl_load_buffered_record( mbedtls_ssl_context *ssl )
5861{
5862 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5863 unsigned char * rec;
5864 size_t rec_len;
5865 unsigned rec_epoch;
5866
5867 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5868 return( 0 );
5869
5870 if( hs == NULL )
5871 return( 0 );
5872
Hanno Becker5f066e72018-08-16 14:56:31 +01005873 rec = hs->buffering.future_record.data;
5874 rec_len = hs->buffering.future_record.len;
5875 rec_epoch = hs->buffering.future_record.epoch;
5876
5877 if( rec == NULL )
5878 return( 0 );
5879
Hanno Becker4cb782d2018-08-20 11:19:05 +01005880 /* Only consider loading future records if the
5881 * input buffer is empty. */
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005882 if( ssl_next_record_is_in_datagram( ssl ) == 1 )
Hanno Becker4cb782d2018-08-20 11:19:05 +01005883 return( 0 );
5884
Hanno Becker5f066e72018-08-16 14:56:31 +01005885 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_record" ) );
5886
5887 if( rec_epoch != ssl->in_epoch )
5888 {
5889 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffered record not from current epoch." ) );
5890 goto exit;
5891 }
5892
5893 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Found buffered record from current epoch - load" ) );
5894
5895 /* Double-check that the record is not too large */
5896 if( rec_len > MBEDTLS_SSL_IN_BUFFER_LEN -
5897 (size_t)( ssl->in_hdr - ssl->in_buf ) )
5898 {
5899 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5900 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5901 }
5902
5903 memcpy( ssl->in_hdr, rec, rec_len );
5904 ssl->in_left = rec_len;
5905 ssl->next_record_offset = 0;
5906
5907 ssl_free_buffered_record( ssl );
5908
5909exit:
5910 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_record" ) );
5911 return( 0 );
5912}
5913
Hanno Becker519f15d2019-07-11 12:43:20 +01005914static int ssl_buffer_future_record( mbedtls_ssl_context *ssl,
5915 mbedtls_record const *rec )
Hanno Becker5f066e72018-08-16 14:56:31 +01005916{
5917 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker5f066e72018-08-16 14:56:31 +01005918
5919 /* Don't buffer future records outside handshakes. */
5920 if( hs == NULL )
5921 return( 0 );
5922
5923 /* Only buffer handshake records (we are only interested
5924 * in Finished messages). */
Hanno Becker519f15d2019-07-11 12:43:20 +01005925 if( rec->type != MBEDTLS_SSL_MSG_HANDSHAKE )
Hanno Becker5f066e72018-08-16 14:56:31 +01005926 return( 0 );
5927
5928 /* Don't buffer more than one future epoch record. */
5929 if( hs->buffering.future_record.data != NULL )
5930 return( 0 );
5931
Hanno Becker01315ea2018-08-21 17:22:17 +01005932 /* Don't buffer record if there's not enough buffering space remaining. */
Hanno Becker519f15d2019-07-11 12:43:20 +01005933 if( rec->buf_len > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
Hanno Becker01315ea2018-08-21 17:22:17 +01005934 hs->buffering.total_bytes_buffered ) )
5935 {
5936 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering of future epoch record of size %u would exceed the compile-time limit %u (already %u bytes buffered) -- ignore\n",
Hanno Becker519f15d2019-07-11 12:43:20 +01005937 (unsigned) rec->buf_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Hanno Becker01315ea2018-08-21 17:22:17 +01005938 (unsigned) hs->buffering.total_bytes_buffered ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005939 return( 0 );
5940 }
5941
Hanno Becker5f066e72018-08-16 14:56:31 +01005942 /* Buffer record */
5943 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffer record from epoch %u",
5944 ssl->in_epoch + 1 ) );
Hanno Becker519f15d2019-07-11 12:43:20 +01005945 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered record", rec->buf, rec->buf_len );
Hanno Becker5f066e72018-08-16 14:56:31 +01005946
5947 /* ssl_parse_record_header() only considers records
5948 * of the next epoch as candidates for buffering. */
5949 hs->buffering.future_record.epoch = ssl->in_epoch + 1;
Hanno Becker519f15d2019-07-11 12:43:20 +01005950 hs->buffering.future_record.len = rec->buf_len;
Hanno Becker5f066e72018-08-16 14:56:31 +01005951
5952 hs->buffering.future_record.data =
5953 mbedtls_calloc( 1, hs->buffering.future_record.len );
5954 if( hs->buffering.future_record.data == NULL )
5955 {
5956 /* If we run out of RAM trying to buffer a
5957 * record from the next epoch, just ignore. */
5958 return( 0 );
5959 }
5960
Hanno Becker519f15d2019-07-11 12:43:20 +01005961 memcpy( hs->buffering.future_record.data, rec->buf, rec->buf_len );
Hanno Becker5f066e72018-08-16 14:56:31 +01005962
Hanno Becker519f15d2019-07-11 12:43:20 +01005963 hs->buffering.total_bytes_buffered += rec->buf_len;
Hanno Becker5f066e72018-08-16 14:56:31 +01005964 return( 0 );
5965}
5966
5967#endif /* MBEDTLS_SSL_PROTO_DTLS */
5968
Hanno Beckere74d5562018-08-15 14:26:08 +01005969static int ssl_get_next_record( mbedtls_ssl_context *ssl )
Hanno Becker1097b342018-08-15 14:09:41 +01005970{
5971 int ret;
Hanno Beckere5e7e782019-07-11 12:29:35 +01005972 mbedtls_record rec;
Hanno Becker1097b342018-08-15 14:09:41 +01005973
Hanno Becker5f066e72018-08-16 14:56:31 +01005974#if defined(MBEDTLS_SSL_PROTO_DTLS)
5975 /* We might have buffered a future record; if so,
5976 * and if the epoch matches now, load it.
5977 * On success, this call will set ssl->in_left to
5978 * the length of the buffered record, so that
5979 * the calls to ssl_fetch_input() below will
5980 * essentially be no-ops. */
5981 ret = ssl_load_buffered_record( ssl );
5982 if( ret != 0 )
5983 return( ret );
5984#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker4a810fb2017-05-24 16:27:30 +01005985
Hanno Beckerca59c2b2019-05-08 12:03:28 +01005986 /* Ensure that we have enough space available for the default form
5987 * of TLS / DTLS record headers (5 Bytes for TLS, 13 Bytes for DTLS,
5988 * with no space for CIDs counted in). */
5989 ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_in_hdr_len( ssl ) );
5990 if( ret != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005991 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005992 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005993 return( ret );
5994 }
5995
Hanno Beckere5e7e782019-07-11 12:29:35 +01005996 ret = ssl_parse_record_header( ssl, ssl->in_hdr, ssl->in_left, &rec );
5997 if( ret != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005998 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005999#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker2fddd372019-07-10 14:37:41 +01006000 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006001 {
Hanno Becker5f066e72018-08-16 14:56:31 +01006002 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
6003 {
Hanno Becker519f15d2019-07-11 12:43:20 +01006004 ret = ssl_buffer_future_record( ssl, &rec );
Hanno Becker5f066e72018-08-16 14:56:31 +01006005 if( ret != 0 )
6006 return( ret );
6007
6008 /* Fall through to handling of unexpected records */
6009 ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
6010 }
6011
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01006012 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD )
6013 {
Hanno Becker2fddd372019-07-10 14:37:41 +01006014#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Hanno Beckerd8bf8ce2019-07-12 09:23:47 +01006015 /* Reset in pointers to default state for TLS/DTLS records,
6016 * assuming no CID and no offset between record content and
6017 * record plaintext. */
6018 ssl_update_in_pointers( ssl );
6019
Hanno Becker7ae20e02019-07-12 08:33:49 +01006020 /* Setup internal message pointers from record structure. */
6021 ssl->in_msgtype = rec.type;
6022#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
6023 ssl->in_len = ssl->in_cid + rec.cid_len;
6024#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
6025 ssl->in_iv = ssl->in_msg = ssl->in_len + 2;
6026 ssl->in_msglen = rec.data_len;
6027
Hanno Becker2fddd372019-07-10 14:37:41 +01006028 ret = ssl_check_client_reconnect( ssl );
6029 if( ret != 0 )
6030 return( ret );
6031#endif
6032
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01006033 /* Skip unexpected record (but not whole datagram) */
Hanno Becker4acada32019-07-11 12:48:53 +01006034 ssl->next_record_offset = rec.buf_len;
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006035
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01006036 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding unexpected record "
6037 "(header)" ) );
6038 }
6039 else
6040 {
6041 /* Skip invalid record and the rest of the datagram */
6042 ssl->next_record_offset = 0;
6043 ssl->in_left = 0;
6044
6045 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record "
6046 "(header)" ) );
6047 }
6048
6049 /* Get next record */
Hanno Becker90333da2017-10-10 11:27:13 +01006050 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006051 }
Hanno Becker2fddd372019-07-10 14:37:41 +01006052 else
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006053#endif
Hanno Becker2fddd372019-07-10 14:37:41 +01006054 {
6055 return( ret );
6056 }
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006057 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006058
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006059#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006060 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Beckere65ce782017-05-22 14:47:48 +01006061 {
Hanno Beckera8814792019-07-10 15:01:45 +01006062 /* Remember offset of next record within datagram. */
Hanno Beckerf50da502019-07-11 12:50:10 +01006063 ssl->next_record_offset = rec.buf_len;
Hanno Beckere65ce782017-05-22 14:47:48 +01006064 if( ssl->next_record_offset < ssl->in_left )
6065 {
6066 MBEDTLS_SSL_DEBUG_MSG( 3, ( "more than one record within datagram" ) );
6067 }
6068 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006069 else
6070#endif
Hanno Beckera8814792019-07-10 15:01:45 +01006071 {
Hanno Becker955a5c92019-07-10 17:12:07 +01006072 /*
6073 * Fetch record contents from underlying transport.
6074 */
Hanno Beckera3175662019-07-11 12:50:29 +01006075 ret = mbedtls_ssl_fetch_input( ssl, rec.buf_len );
Hanno Beckera8814792019-07-10 15:01:45 +01006076 if( ret != 0 )
6077 {
6078 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
6079 return( ret );
6080 }
6081
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006082 ssl->in_left = 0;
Hanno Beckera8814792019-07-10 15:01:45 +01006083 }
6084
6085 /*
6086 * Decrypt record contents.
6087 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006088
Hanno Beckerfdf66042019-07-11 13:07:45 +01006089 if( ( ret = ssl_prepare_record_content( ssl, &rec ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006090 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006091#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006092 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006093 {
6094 /* Silently discard invalid records */
Hanno Becker82e2a392019-05-03 16:36:59 +01006095 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006096 {
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02006097 /* Except when waiting for Finished as a bad mac here
6098 * probably means something went wrong in the handshake
6099 * (eg wrong psk used, mitm downgrade attempt, etc.) */
6100 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED ||
6101 ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
6102 {
6103#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
6104 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
6105 {
6106 mbedtls_ssl_send_alert_message( ssl,
6107 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6108 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
6109 }
6110#endif
6111 return( ret );
6112 }
6113
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006114#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006115 if( ssl->conf->badmac_limit != 0 &&
6116 ++ssl->badmac_seen >= ssl->conf->badmac_limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02006117 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006118 MBEDTLS_SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) );
6119 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02006120 }
6121#endif
6122
Hanno Becker4a810fb2017-05-24 16:27:30 +01006123 /* As above, invalid records cause
6124 * dismissal of the whole datagram. */
6125
6126 ssl->next_record_offset = 0;
6127 ssl->in_left = 0;
6128
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006129 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record (mac)" ) );
Hanno Becker90333da2017-10-10 11:27:13 +01006130 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006131 }
6132
6133 return( ret );
6134 }
6135 else
6136#endif
6137 {
6138 /* Error out (and send alert) on invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006139#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
6140 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006141 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006142 mbedtls_ssl_send_alert_message( ssl,
6143 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6144 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006145 }
6146#endif
6147 return( ret );
6148 }
6149 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006150
Hanno Becker44d89b22019-07-12 09:40:44 +01006151
6152 /* Reset in pointers to default state for TLS/DTLS records,
6153 * assuming no CID and no offset between record content and
6154 * record plaintext. */
6155 ssl_update_in_pointers( ssl );
6156
6157 /* Setup internal message pointers from record structure. */
6158 ssl->in_msgtype = rec.type;
6159#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
6160 ssl->in_len = ssl->in_cid + rec.cid_len;
6161#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
6162 ssl->in_iv = ssl->in_msg = ssl->in_len + 2;
6163 ssl->in_msglen = rec.data_len;
6164
Hanno Becker8685c822019-07-12 09:37:30 +01006165 /* The record content type may change during decryption,
6166 * so re-read it. */
6167 ssl->in_msgtype = rec.type;
6168 /* Also update the input buffer, because unfortunately
6169 * the server-side ssl_parse_client_hello() reparses the
6170 * record header when receiving a ClientHello initiating
6171 * a renegotiation. */
6172 ssl->in_hdr[0] = rec.type;
6173 ssl->in_msg = rec.buf + rec.data_offset;
6174 ssl->in_msglen = rec.data_len;
6175 ssl->in_len[0] = (unsigned char)( rec.data_len >> 8 );
6176 ssl->in_len[1] = (unsigned char)( rec.data_len );
6177
Simon Butcher99000142016-10-13 17:21:01 +01006178 return( 0 );
6179}
6180
6181int mbedtls_ssl_handle_message_type( mbedtls_ssl_context *ssl )
6182{
6183 int ret;
6184
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006185 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006186 * Handle particular types of records
6187 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006188 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00006189 {
Simon Butcher99000142016-10-13 17:21:01 +01006190 if( ( ret = mbedtls_ssl_prepare_handshake_record( ssl ) ) != 0 )
6191 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01006192 return( ret );
Simon Butcher99000142016-10-13 17:21:01 +01006193 }
Paul Bakker5121ce52009-01-03 21:22:43 +00006194 }
6195
Hanno Beckere678eaa2018-08-21 14:57:46 +01006196 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006197 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01006198 if( ssl->in_msglen != 1 )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006199 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01006200 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, len: %d",
6201 ssl->in_msglen ) );
6202 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006203 }
6204
Hanno Beckere678eaa2018-08-21 14:57:46 +01006205 if( ssl->in_msg[0] != 1 )
6206 {
6207 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, content: %02x",
6208 ssl->in_msg[0] ) );
6209 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
6210 }
6211
6212#if defined(MBEDTLS_SSL_PROTO_DTLS)
6213 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
6214 ssl->state != MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC &&
6215 ssl->state != MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
6216 {
6217 if( ssl->handshake == NULL )
6218 {
6219 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping ChangeCipherSpec outside handshake" ) );
6220 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
6221 }
6222
6223 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received out-of-order ChangeCipherSpec - remember" ) );
6224 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
6225 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006226#endif
Hanno Beckere678eaa2018-08-21 14:57:46 +01006227 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006228
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006229 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00006230 {
Angus Gratton1a7a17e2018-06-20 15:43:50 +10006231 if( ssl->in_msglen != 2 )
6232 {
6233 /* Note: Standard allows for more than one 2 byte alert
6234 to be packed in a single message, but Mbed TLS doesn't
6235 currently support this. */
6236 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid alert message, len: %d",
6237 ssl->in_msglen ) );
6238 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
6239 }
6240
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006241 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
Paul Bakker5121ce52009-01-03 21:22:43 +00006242 ssl->in_msg[0], ssl->in_msg[1] ) );
6243
6244 /*
Simon Butcher459a9502015-10-27 16:09:03 +00006245 * Ignore non-fatal alerts, except close_notify and no_renegotiation
Paul Bakker5121ce52009-01-03 21:22:43 +00006246 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006247 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006248 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006249 MBEDTLS_SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
Paul Bakker2770fbd2012-07-03 13:30:23 +00006250 ssl->in_msg[1] ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006251 return( MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006252 }
6253
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006254 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6255 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00006256 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006257 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
6258 return( MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00006259 }
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02006260
6261#if defined(MBEDTLS_SSL_RENEGOTIATION_ENABLED)
6262 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6263 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION )
6264 {
Hanno Becker90333da2017-10-10 11:27:13 +01006265 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no renegotiation alert" ) );
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02006266 /* Will be handled when trying to parse ServerHello */
6267 return( 0 );
6268 }
6269#endif
6270
6271#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_SRV_C)
6272 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
6273 ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
6274 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6275 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
6276 {
6277 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no_cert" ) );
6278 /* Will be handled in mbedtls_ssl_parse_certificate() */
6279 return( 0 );
6280 }
6281#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
6282
6283 /* Silently ignore: fetch new message */
Simon Butcher99000142016-10-13 17:21:01 +01006284 return MBEDTLS_ERR_SSL_NON_FATAL;
Paul Bakker5121ce52009-01-03 21:22:43 +00006285 }
6286
Hanno Beckerc76c6192017-06-06 10:03:17 +01006287#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker37ae9522019-05-03 16:54:26 +01006288 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Beckerc76c6192017-06-06 10:03:17 +01006289 {
Hanno Becker37ae9522019-05-03 16:54:26 +01006290 /* Drop unexpected ApplicationData records,
6291 * except at the beginning of renegotiations */
6292 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA &&
6293 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER
6294#if defined(MBEDTLS_SSL_RENEGOTIATION)
6295 && ! ( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
6296 ssl->state == MBEDTLS_SSL_SERVER_HELLO )
Hanno Beckerc76c6192017-06-06 10:03:17 +01006297#endif
Hanno Becker37ae9522019-05-03 16:54:26 +01006298 )
6299 {
6300 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) );
6301 return( MBEDTLS_ERR_SSL_NON_FATAL );
6302 }
6303
6304 if( ssl->handshake != NULL &&
6305 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
6306 {
6307 ssl_handshake_wrapup_free_hs_transform( ssl );
6308 }
6309 }
Hanno Becker4a4af9f2019-05-08 16:26:21 +01006310#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Beckerc76c6192017-06-06 10:03:17 +01006311
Paul Bakker5121ce52009-01-03 21:22:43 +00006312 return( 0 );
6313}
6314
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006315int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00006316{
6317 int ret;
6318
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006319 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
6320 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6321 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00006322 {
6323 return( ret );
6324 }
6325
6326 return( 0 );
6327}
6328
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006329int mbedtls_ssl_send_alert_message( mbedtls_ssl_context *ssl,
Paul Bakker0a925182012-04-16 06:46:41 +00006330 unsigned char level,
6331 unsigned char message )
6332{
6333 int ret;
6334
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02006335 if( ssl == NULL || ssl->conf == NULL )
6336 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6337
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006338 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006339 MBEDTLS_SSL_DEBUG_MSG( 3, ( "send alert level=%u message=%u", level, message ));
Paul Bakker0a925182012-04-16 06:46:41 +00006340
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006341 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
Paul Bakker0a925182012-04-16 06:46:41 +00006342 ssl->out_msglen = 2;
6343 ssl->out_msg[0] = level;
6344 ssl->out_msg[1] = message;
6345
Hanno Becker67bc7c32018-08-06 11:33:50 +01006346 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00006347 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006348 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker0a925182012-04-16 06:46:41 +00006349 return( ret );
6350 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006351 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
Paul Bakker0a925182012-04-16 06:46:41 +00006352
6353 return( 0 );
6354}
6355
Hanno Beckerb9d44792019-02-08 07:19:04 +00006356#if defined(MBEDTLS_X509_CRT_PARSE_C)
6357static void ssl_clear_peer_cert( mbedtls_ssl_session *session )
6358{
6359#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6360 if( session->peer_cert != NULL )
6361 {
6362 mbedtls_x509_crt_free( session->peer_cert );
6363 mbedtls_free( session->peer_cert );
6364 session->peer_cert = NULL;
6365 }
6366#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6367 if( session->peer_cert_digest != NULL )
6368 {
6369 /* Zeroization is not necessary. */
6370 mbedtls_free( session->peer_cert_digest );
6371 session->peer_cert_digest = NULL;
6372 session->peer_cert_digest_type = MBEDTLS_MD_NONE;
6373 session->peer_cert_digest_len = 0;
6374 }
6375#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6376}
6377#endif /* MBEDTLS_X509_CRT_PARSE_C */
6378
Paul Bakker5121ce52009-01-03 21:22:43 +00006379/*
6380 * Handshake functions
6381 */
Hanno Becker21489932019-02-05 13:20:55 +00006382#if !defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Gilles Peskinef9828522017-05-03 12:28:43 +02006383/* No certificate support -> dummy functions */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006384int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006385{
Hanno Beckere694c3e2017-12-27 21:34:08 +00006386 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6387 ssl->handshake->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00006388
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006389 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006390
Hanno Becker7177a882019-02-05 13:36:46 +00006391 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02006392 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006393 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02006394 ssl->state++;
6395 return( 0 );
6396 }
6397
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006398 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6399 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006400}
6401
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006402int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006403{
Hanno Beckere694c3e2017-12-27 21:34:08 +00006404 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6405 ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006406
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006407 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006408
Hanno Becker7177a882019-02-05 13:36:46 +00006409 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006410 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006411 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006412 ssl->state++;
6413 return( 0 );
6414 }
6415
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006416 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6417 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006418}
Gilles Peskinef9828522017-05-03 12:28:43 +02006419
Hanno Becker21489932019-02-05 13:20:55 +00006420#else /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Gilles Peskinef9828522017-05-03 12:28:43 +02006421/* Some certificate support -> implement write and parse */
6422
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006423int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006424{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006425 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006426 size_t i, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006427 const mbedtls_x509_crt *crt;
Hanno Beckere694c3e2017-12-27 21:34:08 +00006428 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6429 ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006430
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006431 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006432
Hanno Becker7177a882019-02-05 13:36:46 +00006433 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006434 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006435 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006436 ssl->state++;
6437 return( 0 );
6438 }
6439
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006440#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006441 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00006442 {
6443 if( ssl->client_auth == 0 )
6444 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006445 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006446 ssl->state++;
6447 return( 0 );
6448 }
6449
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006450#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00006451 /*
6452 * If using SSLv3 and got no cert, send an Alert message
6453 * (otherwise an empty Certificate message will be sent).
6454 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006455 if( mbedtls_ssl_own_cert( ssl ) == NULL &&
6456 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006457 {
6458 ssl->out_msglen = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006459 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
6460 ssl->out_msg[0] = MBEDTLS_SSL_ALERT_LEVEL_WARNING;
6461 ssl->out_msg[1] = MBEDTLS_SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00006462
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006463 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006464 goto write_msg;
6465 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006466#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006467 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006468#endif /* MBEDTLS_SSL_CLI_C */
6469#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006470 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00006471 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006472 if( mbedtls_ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006473 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006474 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
6475 return( MBEDTLS_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00006476 }
6477 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006478#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006479
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006480 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006481
6482 /*
6483 * 0 . 0 handshake type
6484 * 1 . 3 handshake length
6485 * 4 . 6 length of all certs
6486 * 7 . 9 length of cert. 1
6487 * 10 . n-1 peer certificate
6488 * n . n+2 length of cert. 2
6489 * n+3 . ... upper level cert, etc.
6490 */
6491 i = 7;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006492 crt = mbedtls_ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00006493
Paul Bakker29087132010-03-21 21:03:34 +00006494 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006495 {
6496 n = crt->raw.len;
Angus Grattond8213d02016-05-25 20:56:48 +10006497 if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00006498 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006499 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
Angus Grattond8213d02016-05-25 20:56:48 +10006500 i + 3 + n, MBEDTLS_SSL_OUT_CONTENT_LEN ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006501 return( MBEDTLS_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006502 }
6503
6504 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
6505 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
6506 ssl->out_msg[i + 2] = (unsigned char)( n );
6507
6508 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
6509 i += n; crt = crt->next;
6510 }
6511
6512 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
6513 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
6514 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
6515
6516 ssl->out_msglen = i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006517 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
6518 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
Paul Bakker5121ce52009-01-03 21:22:43 +00006519
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02006520#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00006521write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006522#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006523
6524 ssl->state++;
6525
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006526 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006527 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006528 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006529 return( ret );
6530 }
6531
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006532 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006533
Paul Bakkered27a042013-04-18 22:46:23 +02006534 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006535}
6536
Hanno Becker84879e32019-01-31 07:44:03 +00006537#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Hanno Becker177475a2019-02-05 17:02:46 +00006538
6539#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006540static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6541 unsigned char *crt_buf,
6542 size_t crt_buf_len )
6543{
6544 mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert;
6545
6546 if( peer_crt == NULL )
6547 return( -1 );
6548
6549 if( peer_crt->raw.len != crt_buf_len )
6550 return( -1 );
6551
Hanno Becker46f34d02019-02-08 14:00:04 +00006552 return( memcmp( peer_crt->raw.p, crt_buf, crt_buf_len ) );
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006553}
Hanno Becker177475a2019-02-05 17:02:46 +00006554#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6555static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6556 unsigned char *crt_buf,
6557 size_t crt_buf_len )
6558{
6559 int ret;
6560 unsigned char const * const peer_cert_digest =
6561 ssl->session->peer_cert_digest;
6562 mbedtls_md_type_t const peer_cert_digest_type =
6563 ssl->session->peer_cert_digest_type;
6564 mbedtls_md_info_t const * const digest_info =
6565 mbedtls_md_info_from_type( peer_cert_digest_type );
6566 unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN];
6567 size_t digest_len;
6568
6569 if( peer_cert_digest == NULL || digest_info == NULL )
6570 return( -1 );
6571
6572 digest_len = mbedtls_md_get_size( digest_info );
6573 if( digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN )
6574 return( -1 );
6575
6576 ret = mbedtls_md( digest_info, crt_buf, crt_buf_len, tmp_digest );
6577 if( ret != 0 )
6578 return( -1 );
6579
6580 return( memcmp( tmp_digest, peer_cert_digest, digest_len ) );
6581}
6582#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker84879e32019-01-31 07:44:03 +00006583#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006584
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006585/*
6586 * Once the certificate message is read, parse it into a cert chain and
6587 * perform basic checks, but leave actual verification to the caller
6588 */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006589static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl,
6590 mbedtls_x509_crt *chain )
Paul Bakker5121ce52009-01-03 21:22:43 +00006591{
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006592 int ret;
6593#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6594 int crt_cnt=0;
6595#endif
Paul Bakker23986e52011-04-24 08:57:21 +00006596 size_t i, n;
Gilles Peskine064a85c2017-05-10 10:46:40 +02006597 uint8_t alert;
Paul Bakker5121ce52009-01-03 21:22:43 +00006598
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006599 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00006600 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006601 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006602 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6603 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006604 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006605 }
6606
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006607 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE ||
6608 ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006609 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006610 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006611 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6612 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006613 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006614 }
6615
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006616 i = mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006617
Paul Bakker5121ce52009-01-03 21:22:43 +00006618 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006619 * Same message structure as in mbedtls_ssl_write_certificate()
Paul Bakker5121ce52009-01-03 21:22:43 +00006620 */
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006621 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
Paul Bakker5121ce52009-01-03 21:22:43 +00006622
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006623 if( ssl->in_msg[i] != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006624 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00006625 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006626 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006627 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6628 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006629 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006630 }
6631
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006632 /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */
6633 i += 3;
6634
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006635 /* Iterate through and parse the CRTs in the provided chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006636 while( i < ssl->in_hslen )
6637 {
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006638 /* Check that there's room for the next CRT's length fields. */
Philippe Antoine747fd532018-05-30 09:13:21 +02006639 if ( i + 3 > ssl->in_hslen ) {
6640 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckere2734e22019-01-31 07:44:17 +00006641 mbedtls_ssl_send_alert_message( ssl,
6642 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6643 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Philippe Antoine747fd532018-05-30 09:13:21 +02006644 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
6645 }
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006646 /* In theory, the CRT can be up to 2**24 Bytes, but we don't support
6647 * anything beyond 2**16 ~ 64K. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006648 if( ssl->in_msg[i] != 0 )
6649 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006650 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckere2734e22019-01-31 07:44:17 +00006651 mbedtls_ssl_send_alert_message( ssl,
6652 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6653 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006654 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006655 }
6656
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006657 /* Read length of the next CRT in the chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006658 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
6659 | (unsigned int) ssl->in_msg[i + 2];
6660 i += 3;
6661
6662 if( n < 128 || i + n > ssl->in_hslen )
6663 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006664 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckere2734e22019-01-31 07:44:17 +00006665 mbedtls_ssl_send_alert_message( ssl,
6666 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6667 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006668 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006669 }
6670
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006671 /* Check if we're handling the first CRT in the chain. */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006672#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6673 if( crt_cnt++ == 0 &&
6674 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
6675 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006676 {
Hanno Becker46f34d02019-02-08 14:00:04 +00006677 /* During client-side renegotiation, check that the server's
6678 * end-CRTs hasn't changed compared to the initial handshake,
6679 * mitigating the triple handshake attack. On success, reuse
6680 * the original end-CRT instead of parsing it again. */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006681 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Check that peer CRT hasn't changed during renegotiation" ) );
6682 if( ssl_check_peer_crt_unchanged( ssl,
6683 &ssl->in_msg[i],
6684 n ) != 0 )
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006685 {
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006686 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
6687 mbedtls_ssl_send_alert_message( ssl,
6688 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6689 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
6690 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006691 }
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006692
6693 /* Now we can safely free the original chain. */
6694 ssl_clear_peer_cert( ssl->session );
6695 }
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006696#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
6697
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006698 /* Parse the next certificate in the chain. */
Hanno Becker0056eab2019-02-08 14:39:16 +00006699#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006700 ret = mbedtls_x509_crt_parse_der( chain, ssl->in_msg + i, n );
Hanno Becker0056eab2019-02-08 14:39:16 +00006701#else
Hanno Becker353a6f02019-02-26 11:51:34 +00006702 /* If we don't need to store the CRT chain permanently, parse
Hanno Becker0056eab2019-02-08 14:39:16 +00006703 * it in-place from the input buffer instead of making a copy. */
6704 ret = mbedtls_x509_crt_parse_der_nocopy( chain, ssl->in_msg + i, n );
6705#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006706 switch( ret )
Paul Bakker5121ce52009-01-03 21:22:43 +00006707 {
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006708 case 0: /*ok*/
6709 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
6710 /* Ignore certificate with an unknown algorithm: maybe a
6711 prior certificate was already trusted. */
6712 break;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006713
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006714 case MBEDTLS_ERR_X509_ALLOC_FAILED:
6715 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
6716 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006717
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006718 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
6719 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6720 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006721
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006722 default:
6723 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6724 crt_parse_der_failed:
6725 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert );
6726 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
6727 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006728 }
6729
6730 i += n;
6731 }
6732
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006733 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", chain );
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006734 return( 0 );
6735}
6736
Hanno Becker4a55f632019-02-05 12:49:06 +00006737#if defined(MBEDTLS_SSL_SRV_C)
6738static int ssl_srv_check_client_no_crt_notification( mbedtls_ssl_context *ssl )
6739{
6740 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
6741 return( -1 );
6742
6743#if defined(MBEDTLS_SSL_PROTO_SSL3)
6744 /*
6745 * Check if the client sent an empty certificate
6746 */
6747 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
6748 {
6749 if( ssl->in_msglen == 2 &&
6750 ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT &&
6751 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6752 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
6753 {
6754 MBEDTLS_SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
6755 return( 0 );
6756 }
6757
6758 return( -1 );
6759 }
6760#endif /* MBEDTLS_SSL_PROTO_SSL3 */
6761
6762#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
6763 defined(MBEDTLS_SSL_PROTO_TLS1_2)
6764 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
6765 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
6766 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
6767 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
6768 {
6769 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
6770 return( 0 );
6771 }
6772
6773 return( -1 );
6774#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
6775 MBEDTLS_SSL_PROTO_TLS1_2 */
6776}
6777#endif /* MBEDTLS_SSL_SRV_C */
6778
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006779/* Check if a certificate message is expected.
6780 * Return either
6781 * - SSL_CERTIFICATE_EXPECTED, or
6782 * - SSL_CERTIFICATE_SKIP
6783 * indicating whether a Certificate message is expected or not.
6784 */
6785#define SSL_CERTIFICATE_EXPECTED 0
6786#define SSL_CERTIFICATE_SKIP 1
6787static int ssl_parse_certificate_coordinate( mbedtls_ssl_context *ssl,
6788 int authmode )
6789{
6790 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00006791 ssl->handshake->ciphersuite_info;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006792
6793 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6794 return( SSL_CERTIFICATE_SKIP );
6795
6796#if defined(MBEDTLS_SSL_SRV_C)
6797 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
6798 {
6799 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
6800 return( SSL_CERTIFICATE_SKIP );
6801
6802 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6803 {
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006804 ssl->session_negotiate->verify_result =
6805 MBEDTLS_X509_BADCERT_SKIP_VERIFY;
6806 return( SSL_CERTIFICATE_SKIP );
6807 }
6808 }
Hanno Becker84d9d272019-03-01 08:10:46 +00006809#else
6810 ((void) authmode);
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006811#endif /* MBEDTLS_SSL_SRV_C */
6812
6813 return( SSL_CERTIFICATE_EXPECTED );
6814}
6815
Hanno Becker68636192019-02-05 14:36:34 +00006816static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl,
6817 int authmode,
6818 mbedtls_x509_crt *chain,
6819 void *rs_ctx )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006820{
Hanno Becker6bdfab22019-02-05 13:11:17 +00006821 int ret = 0;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006822 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00006823 ssl->handshake->ciphersuite_info;
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006824 int have_ca_chain = 0;
Hanno Becker68636192019-02-05 14:36:34 +00006825
Hanno Becker8927c832019-04-03 12:52:50 +01006826 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *);
6827 void *p_vrfy;
6828
Hanno Becker68636192019-02-05 14:36:34 +00006829 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6830 return( 0 );
6831
Hanno Becker8927c832019-04-03 12:52:50 +01006832 if( ssl->f_vrfy != NULL )
6833 {
Hanno Beckerefb440a2019-04-03 13:04:33 +01006834 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use context-specific verification callback" ) );
Hanno Becker8927c832019-04-03 12:52:50 +01006835 f_vrfy = ssl->f_vrfy;
6836 p_vrfy = ssl->p_vrfy;
6837 }
6838 else
6839 {
Hanno Beckerefb440a2019-04-03 13:04:33 +01006840 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use configuration-specific verification callback" ) );
Hanno Becker8927c832019-04-03 12:52:50 +01006841 f_vrfy = ssl->conf->f_vrfy;
6842 p_vrfy = ssl->conf->p_vrfy;
6843 }
6844
Hanno Becker68636192019-02-05 14:36:34 +00006845 /*
6846 * Main check: verify certificate
6847 */
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006848#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
6849 if( ssl->conf->f_ca_cb != NULL )
6850 {
6851 ((void) rs_ctx);
6852 have_ca_chain = 1;
6853
6854 MBEDTLS_SSL_DEBUG_MSG( 3, ( "use CA callback for X.509 CRT verification" ) );
Jarno Lamsa9822c0d2019-04-01 16:59:48 +03006855 ret = mbedtls_x509_crt_verify_with_ca_cb(
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006856 chain,
6857 ssl->conf->f_ca_cb,
6858 ssl->conf->p_ca_cb,
6859 ssl->conf->cert_profile,
6860 ssl->hostname,
6861 &ssl->session_negotiate->verify_result,
Jaeden Amerofe710672019-04-16 15:03:12 +01006862 f_vrfy, p_vrfy );
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006863 }
6864 else
6865#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
6866 {
6867 mbedtls_x509_crt *ca_chain;
6868 mbedtls_x509_crl *ca_crl;
6869
6870#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6871 if( ssl->handshake->sni_ca_chain != NULL )
6872 {
6873 ca_chain = ssl->handshake->sni_ca_chain;
6874 ca_crl = ssl->handshake->sni_ca_crl;
6875 }
6876 else
6877#endif
6878 {
6879 ca_chain = ssl->conf->ca_chain;
6880 ca_crl = ssl->conf->ca_crl;
6881 }
6882
6883 if( ca_chain != NULL )
6884 have_ca_chain = 1;
6885
6886 ret = mbedtls_x509_crt_verify_restartable(
6887 chain,
6888 ca_chain, ca_crl,
6889 ssl->conf->cert_profile,
6890 ssl->hostname,
6891 &ssl->session_negotiate->verify_result,
Jaeden Amerofe710672019-04-16 15:03:12 +01006892 f_vrfy, p_vrfy, rs_ctx );
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006893 }
Hanno Becker68636192019-02-05 14:36:34 +00006894
6895 if( ret != 0 )
6896 {
6897 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
6898 }
6899
6900#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6901 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
6902 return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS );
6903#endif
6904
6905 /*
6906 * Secondary checks: always done, but change 'ret' only if it was 0
6907 */
6908
6909#if defined(MBEDTLS_ECP_C)
6910 {
6911 const mbedtls_pk_context *pk = &chain->pk;
6912
6913 /* If certificate uses an EC key, make sure the curve is OK */
6914 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) &&
6915 mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id ) != 0 )
6916 {
6917 ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
6918
6919 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
6920 if( ret == 0 )
6921 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
6922 }
6923 }
6924#endif /* MBEDTLS_ECP_C */
6925
6926 if( mbedtls_ssl_check_cert_usage( chain,
6927 ciphersuite_info,
6928 ! ssl->conf->endpoint,
6929 &ssl->session_negotiate->verify_result ) != 0 )
6930 {
6931 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
6932 if( ret == 0 )
6933 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
6934 }
6935
6936 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
6937 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
6938 * with details encoded in the verification flags. All other kinds
6939 * of error codes, including those from the user provided f_vrfy
6940 * functions, are treated as fatal and lead to a failure of
6941 * ssl_parse_certificate even if verification was optional. */
6942 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
6943 ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
6944 ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ) )
6945 {
6946 ret = 0;
6947 }
6948
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006949 if( have_ca_chain == 0 && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
Hanno Becker68636192019-02-05 14:36:34 +00006950 {
6951 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
6952 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
6953 }
6954
6955 if( ret != 0 )
6956 {
6957 uint8_t alert;
6958
6959 /* The certificate may have been rejected for several reasons.
6960 Pick one and send the corresponding alert. Which alert to send
6961 may be a subject of debate in some cases. */
6962 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
6963 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
6964 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
6965 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6966 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
6967 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6968 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
6969 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6970 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
6971 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6972 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
6973 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6974 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
6975 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6976 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
6977 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
6978 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
6979 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
6980 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
6981 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
6982 else
6983 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
6984 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6985 alert );
6986 }
6987
6988#if defined(MBEDTLS_DEBUG_C)
6989 if( ssl->session_negotiate->verify_result != 0 )
6990 {
6991 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %x",
6992 ssl->session_negotiate->verify_result ) );
6993 }
6994 else
6995 {
6996 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
6997 }
6998#endif /* MBEDTLS_DEBUG_C */
6999
7000 return( ret );
7001}
7002
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007003#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
7004static int ssl_remember_peer_crt_digest( mbedtls_ssl_context *ssl,
7005 unsigned char *start, size_t len )
7006{
7007 int ret;
7008 /* Remember digest of the peer's end-CRT. */
7009 ssl->session_negotiate->peer_cert_digest =
7010 mbedtls_calloc( 1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN );
7011 if( ssl->session_negotiate->peer_cert_digest == NULL )
7012 {
7013 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
7014 sizeof( MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ) ) );
7015 mbedtls_ssl_send_alert_message( ssl,
7016 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7017 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
7018
7019 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
7020 }
7021
7022 ret = mbedtls_md( mbedtls_md_info_from_type(
7023 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE ),
7024 start, len,
7025 ssl->session_negotiate->peer_cert_digest );
7026
7027 ssl->session_negotiate->peer_cert_digest_type =
7028 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
7029 ssl->session_negotiate->peer_cert_digest_len =
7030 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
7031
7032 return( ret );
7033}
7034
7035static int ssl_remember_peer_pubkey( mbedtls_ssl_context *ssl,
7036 unsigned char *start, size_t len )
7037{
7038 unsigned char *end = start + len;
7039 int ret;
7040
7041 /* Make a copy of the peer's raw public key. */
7042 mbedtls_pk_init( &ssl->handshake->peer_pubkey );
7043 ret = mbedtls_pk_parse_subpubkey( &start, end,
7044 &ssl->handshake->peer_pubkey );
7045 if( ret != 0 )
7046 {
7047 /* We should have parsed the public key before. */
7048 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
7049 }
7050
7051 return( 0 );
7052}
7053#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7054
Hanno Becker68636192019-02-05 14:36:34 +00007055int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
7056{
7057 int ret = 0;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00007058 int crt_expected;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007059#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7060 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
7061 ? ssl->handshake->sni_authmode
7062 : ssl->conf->authmode;
7063#else
7064 const int authmode = ssl->conf->authmode;
7065#endif
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007066 void *rs_ctx = NULL;
Hanno Becker3dad3112019-02-05 17:19:52 +00007067 mbedtls_x509_crt *chain = NULL;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007068
7069 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
7070
Hanno Becker28f2fcd2019-02-07 10:11:07 +00007071 crt_expected = ssl_parse_certificate_coordinate( ssl, authmode );
7072 if( crt_expected == SSL_CERTIFICATE_SKIP )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007073 {
7074 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Hanno Becker6bdfab22019-02-05 13:11:17 +00007075 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007076 }
7077
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007078#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
7079 if( ssl->handshake->ecrs_enabled &&
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02007080 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify )
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007081 {
Hanno Becker3dad3112019-02-05 17:19:52 +00007082 chain = ssl->handshake->ecrs_peer_cert;
7083 ssl->handshake->ecrs_peer_cert = NULL;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007084 goto crt_verify;
7085 }
7086#endif
7087
Manuel Pégourié-Gonnard125af942018-09-11 11:08:12 +02007088 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007089 {
7090 /* mbedtls_ssl_read_record may have sent an alert already. We
7091 let it decide whether to alert. */
7092 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Hanno Becker3dad3112019-02-05 17:19:52 +00007093 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007094 }
7095
Hanno Becker4a55f632019-02-05 12:49:06 +00007096#if defined(MBEDTLS_SSL_SRV_C)
7097 if( ssl_srv_check_client_no_crt_notification( ssl ) == 0 )
7098 {
7099 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Hanno Becker4a55f632019-02-05 12:49:06 +00007100
7101 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
Hanno Becker6bdfab22019-02-05 13:11:17 +00007102 ret = 0;
7103 else
7104 ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
Hanno Becker4a55f632019-02-05 12:49:06 +00007105
Hanno Becker6bdfab22019-02-05 13:11:17 +00007106 goto exit;
Hanno Becker4a55f632019-02-05 12:49:06 +00007107 }
7108#endif /* MBEDTLS_SSL_SRV_C */
7109
Hanno Beckerc7bd7802019-02-05 15:37:23 +00007110 /* Clear existing peer CRT structure in case we tried to
7111 * reuse a session but it failed, and allocate a new one. */
Hanno Becker7a955a02019-02-05 13:08:01 +00007112 ssl_clear_peer_cert( ssl->session_negotiate );
Hanno Becker3dad3112019-02-05 17:19:52 +00007113
7114 chain = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
7115 if( chain == NULL )
Hanno Beckerc7bd7802019-02-05 15:37:23 +00007116 {
7117 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
7118 sizeof( mbedtls_x509_crt ) ) );
7119 mbedtls_ssl_send_alert_message( ssl,
7120 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7121 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Hanno Becker7a955a02019-02-05 13:08:01 +00007122
Hanno Becker3dad3112019-02-05 17:19:52 +00007123 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
7124 goto exit;
7125 }
7126 mbedtls_x509_crt_init( chain );
7127
7128 ret = ssl_parse_certificate_chain( ssl, chain );
Hanno Beckerc7bd7802019-02-05 15:37:23 +00007129 if( ret != 0 )
Hanno Becker3dad3112019-02-05 17:19:52 +00007130 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007131
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007132#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
7133 if( ssl->handshake->ecrs_enabled)
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02007134 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007135
7136crt_verify:
7137 if( ssl->handshake->ecrs_enabled)
7138 rs_ctx = &ssl->handshake->ecrs_ctx;
7139#endif
7140
Hanno Becker68636192019-02-05 14:36:34 +00007141 ret = ssl_parse_certificate_verify( ssl, authmode,
Hanno Becker3dad3112019-02-05 17:19:52 +00007142 chain, rs_ctx );
Hanno Becker68636192019-02-05 14:36:34 +00007143 if( ret != 0 )
Hanno Becker3dad3112019-02-05 17:19:52 +00007144 goto exit;
Paul Bakker5121ce52009-01-03 21:22:43 +00007145
Hanno Becker6bbd94c2019-02-05 17:02:28 +00007146#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Becker6bbd94c2019-02-05 17:02:28 +00007147 {
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007148 unsigned char *crt_start, *pk_start;
7149 size_t crt_len, pk_len;
Hanno Becker3dad3112019-02-05 17:19:52 +00007150
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007151 /* We parse the CRT chain without copying, so
7152 * these pointers point into the input buffer,
7153 * and are hence still valid after freeing the
7154 * CRT chain. */
Hanno Becker6bbd94c2019-02-05 17:02:28 +00007155
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007156 crt_start = chain->raw.p;
7157 crt_len = chain->raw.len;
Hanno Becker6bbd94c2019-02-05 17:02:28 +00007158
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007159 pk_start = chain->pk_raw.p;
7160 pk_len = chain->pk_raw.len;
7161
7162 /* Free the CRT structures before computing
7163 * digest and copying the peer's public key. */
7164 mbedtls_x509_crt_free( chain );
7165 mbedtls_free( chain );
7166 chain = NULL;
7167
7168 ret = ssl_remember_peer_crt_digest( ssl, crt_start, crt_len );
Hanno Beckera2747532019-02-06 16:19:04 +00007169 if( ret != 0 )
Hanno Beckera2747532019-02-06 16:19:04 +00007170 goto exit;
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007171
7172 ret = ssl_remember_peer_pubkey( ssl, pk_start, pk_len );
7173 if( ret != 0 )
7174 goto exit;
Hanno Beckera2747532019-02-06 16:19:04 +00007175 }
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007176#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7177 /* Pass ownership to session structure. */
Hanno Becker3dad3112019-02-05 17:19:52 +00007178 ssl->session_negotiate->peer_cert = chain;
7179 chain = NULL;
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007180#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker3dad3112019-02-05 17:19:52 +00007181
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007182 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007183
Hanno Becker6bdfab22019-02-05 13:11:17 +00007184exit:
7185
Hanno Becker3dad3112019-02-05 17:19:52 +00007186 if( ret == 0 )
7187 ssl->state++;
7188
7189#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
7190 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
7191 {
7192 ssl->handshake->ecrs_peer_cert = chain;
7193 chain = NULL;
7194 }
7195#endif
7196
7197 if( chain != NULL )
7198 {
7199 mbedtls_x509_crt_free( chain );
7200 mbedtls_free( chain );
7201 }
7202
Paul Bakker5121ce52009-01-03 21:22:43 +00007203 return( ret );
7204}
Hanno Becker21489932019-02-05 13:20:55 +00007205#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00007206
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007207int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007208{
7209 int ret;
7210
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007211 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007212
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007213 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00007214 ssl->out_msglen = 1;
7215 ssl->out_msg[0] = 1;
7216
Paul Bakker5121ce52009-01-03 21:22:43 +00007217 ssl->state++;
7218
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007219 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007220 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007221 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007222 return( ret );
7223 }
7224
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007225 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007226
7227 return( 0 );
7228}
7229
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007230int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007231{
7232 int ret;
7233
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007234 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007235
Hanno Becker327c93b2018-08-15 13:56:18 +01007236 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007237 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007238 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007239 return( ret );
7240 }
7241
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007242 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Paul Bakker5121ce52009-01-03 21:22:43 +00007243 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007244 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007245 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7246 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007247 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007248 }
7249
Hanno Beckere678eaa2018-08-21 14:57:46 +01007250 /* CCS records are only accepted if they have length 1 and content '1',
7251 * so we don't need to check this here. */
Paul Bakker5121ce52009-01-03 21:22:43 +00007252
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007253 /*
7254 * Switch to our negotiated transform and session parameters for inbound
7255 * data.
7256 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007257 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007258 ssl->transform_in = ssl->transform_negotiate;
7259 ssl->session_in = ssl->session_negotiate;
7260
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007261#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007262 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007263 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007264#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007265 ssl_dtls_replay_reset( ssl );
7266#endif
7267
7268 /* Increment epoch */
7269 if( ++ssl->in_epoch == 0 )
7270 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007271 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007272 /* This is highly unlikely to happen for legitimate reasons, so
7273 treat it as an attack and don't send an alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007274 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007275 }
7276 }
7277 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007278#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007279 memset( ssl->in_ctr, 0, 8 );
7280
Hanno Becker79594fd2019-05-08 09:38:41 +01007281 ssl_update_in_pointers( ssl );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007282
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007283#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7284 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007285 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007286 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_INBOUND ) ) != 0 )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007287 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007288 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007289 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7290 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007291 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007292 }
7293 }
7294#endif
7295
Paul Bakker5121ce52009-01-03 21:22:43 +00007296 ssl->state++;
7297
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007298 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007299
7300 return( 0 );
7301}
7302
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007303void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl,
7304 const mbedtls_ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00007305{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02007306 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01007307
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007308#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7309 defined(MBEDTLS_SSL_PROTO_TLS1_1)
7310 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00007311 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00007312 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007313#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007314#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7315#if defined(MBEDTLS_SHA512_C)
7316 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007317 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
7318 else
7319#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007320#if defined(MBEDTLS_SHA256_C)
7321 if( ciphersuite_info->mac != MBEDTLS_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00007322 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007323 else
7324#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007325#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02007326 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007327 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007328 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02007329 }
Paul Bakker380da532012-04-18 16:10:25 +00007330}
Paul Bakkerf7abd422013-04-16 13:15:56 +02007331
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007332void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007333{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007334#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7335 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007336 mbedtls_md5_starts_ret( &ssl->handshake->fin_md5 );
7337 mbedtls_sha1_starts_ret( &ssl->handshake->fin_sha1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007338#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007339#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7340#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007341#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek2ad22972019-01-30 03:32:12 -05007342 psa_hash_abort( &ssl->handshake->fin_sha256_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007343 psa_hash_setup( &ssl->handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
7344#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007345 mbedtls_sha256_starts_ret( &ssl->handshake->fin_sha256, 0 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007346#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007347#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007348#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007349#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek2ad22972019-01-30 03:32:12 -05007350 psa_hash_abort( &ssl->handshake->fin_sha384_psa );
Andrzej Kurek972fba52019-01-30 03:29:12 -05007351 psa_hash_setup( &ssl->handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007352#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007353 mbedtls_sha512_starts_ret( &ssl->handshake->fin_sha512, 1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007354#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007355#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007356#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007357}
7358
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007359static void ssl_update_checksum_start( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007360 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00007361{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007362#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7363 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007364 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
7365 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007366#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007367#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7368#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007369#if defined(MBEDTLS_USE_PSA_CRYPTO)
7370 psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len );
7371#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007372 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007373#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007374#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007375#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007376#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05007377 psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007378#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007379 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01007380#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007381#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007382#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00007383}
7384
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007385#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7386 defined(MBEDTLS_SSL_PROTO_TLS1_1)
7387static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007388 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00007389{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007390 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
7391 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00007392}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007393#endif
Paul Bakker380da532012-04-18 16:10:25 +00007394
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007395#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7396#if defined(MBEDTLS_SHA256_C)
7397static void ssl_update_checksum_sha256( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007398 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00007399{
Andrzej Kurekeb342242019-01-29 09:14:33 -05007400#if defined(MBEDTLS_USE_PSA_CRYPTO)
7401 psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len );
7402#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007403 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007404#endif
Paul Bakker380da532012-04-18 16:10:25 +00007405}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007406#endif
Paul Bakker380da532012-04-18 16:10:25 +00007407
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007408#if defined(MBEDTLS_SHA512_C)
7409static void ssl_update_checksum_sha384( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007410 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00007411{
Andrzej Kurekeb342242019-01-29 09:14:33 -05007412#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05007413 psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007414#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007415 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007416#endif
Paul Bakker380da532012-04-18 16:10:25 +00007417}
Paul Bakker769075d2012-11-24 11:26:46 +01007418#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007419#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00007420
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007421#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00007422static void ssl_calc_finished_ssl(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007423 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00007424{
Paul Bakker3c2122f2013-06-24 19:03:14 +02007425 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007426 mbedtls_md5_context md5;
7427 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007428
Paul Bakker5121ce52009-01-03 21:22:43 +00007429 unsigned char padbuf[48];
7430 unsigned char md5sum[16];
7431 unsigned char sha1sum[20];
7432
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007433 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007434 if( !session )
7435 session = ssl->session;
7436
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007437 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007438
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007439 mbedtls_md5_init( &md5 );
7440 mbedtls_sha1_init( &sha1 );
7441
7442 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
7443 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007444
7445 /*
7446 * SSLv3:
7447 * hash =
7448 * MD5( master + pad2 +
7449 * MD5( handshake + sender + master + pad1 ) )
7450 * + SHA1( master + pad2 +
7451 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00007452 */
7453
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007454#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007455 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
7456 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007457#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007458
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007459#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007460 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
7461 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007462#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007463
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007464 sender = ( from == MBEDTLS_SSL_IS_CLIENT ) ? "CLNT"
Paul Bakker3c2122f2013-06-24 19:03:14 +02007465 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00007466
Paul Bakker1ef83d62012-04-11 12:09:53 +00007467 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007468
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007469 mbedtls_md5_update_ret( &md5, (const unsigned char *) sender, 4 );
7470 mbedtls_md5_update_ret( &md5, session->master, 48 );
7471 mbedtls_md5_update_ret( &md5, padbuf, 48 );
7472 mbedtls_md5_finish_ret( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00007473
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007474 mbedtls_sha1_update_ret( &sha1, (const unsigned char *) sender, 4 );
7475 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
7476 mbedtls_sha1_update_ret( &sha1, padbuf, 40 );
7477 mbedtls_sha1_finish_ret( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00007478
Paul Bakker1ef83d62012-04-11 12:09:53 +00007479 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007480
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007481 mbedtls_md5_starts_ret( &md5 );
7482 mbedtls_md5_update_ret( &md5, session->master, 48 );
7483 mbedtls_md5_update_ret( &md5, padbuf, 48 );
7484 mbedtls_md5_update_ret( &md5, md5sum, 16 );
7485 mbedtls_md5_finish_ret( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00007486
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007487 mbedtls_sha1_starts_ret( &sha1 );
7488 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
7489 mbedtls_sha1_update_ret( &sha1, padbuf , 40 );
7490 mbedtls_sha1_update_ret( &sha1, sha1sum, 20 );
7491 mbedtls_sha1_finish_ret( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007492
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007493 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007494
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007495 mbedtls_md5_free( &md5 );
7496 mbedtls_sha1_free( &sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007497
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007498 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
7499 mbedtls_platform_zeroize( md5sum, sizeof( md5sum ) );
7500 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007501
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007502 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007503}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007504#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00007505
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007506#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00007507static void ssl_calc_finished_tls(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007508 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00007509{
Paul Bakker1ef83d62012-04-11 12:09:53 +00007510 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02007511 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007512 mbedtls_md5_context md5;
7513 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007514 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00007515
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007516 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007517 if( !session )
7518 session = ssl->session;
7519
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007520 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007521
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007522 mbedtls_md5_init( &md5 );
7523 mbedtls_sha1_init( &sha1 );
7524
7525 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
7526 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007527
Paul Bakker1ef83d62012-04-11 12:09:53 +00007528 /*
7529 * TLSv1:
7530 * hash = PRF( master, finished_label,
7531 * MD5( handshake ) + SHA1( handshake ) )[0..11]
7532 */
Paul Bakker5121ce52009-01-03 21:22:43 +00007533
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007534#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007535 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
7536 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007537#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007538
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007539#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007540 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
7541 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007542#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007543
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007544 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02007545 ? "client finished"
7546 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00007547
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007548 mbedtls_md5_finish_ret( &md5, padbuf );
7549 mbedtls_sha1_finish_ret( &sha1, padbuf + 16 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007550
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007551 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00007552 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007553
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007554 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007555
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007556 mbedtls_md5_free( &md5 );
7557 mbedtls_sha1_free( &sha1 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007558
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007559 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007560
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007561 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007562}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007563#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00007564
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007565#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7566#if defined(MBEDTLS_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00007567static void ssl_calc_finished_tls_sha256(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007568 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker1ef83d62012-04-11 12:09:53 +00007569{
7570 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02007571 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007572 unsigned char padbuf[32];
Andrzej Kurekeb342242019-01-29 09:14:33 -05007573#if defined(MBEDTLS_USE_PSA_CRYPTO)
7574 size_t hash_size;
Jaeden Amero34973232019-02-20 10:32:28 +00007575 psa_hash_operation_t sha256_psa = PSA_HASH_OPERATION_INIT;
Andrzej Kurekeb342242019-01-29 09:14:33 -05007576 psa_status_t status;
7577#else
7578 mbedtls_sha256_context sha256;
7579#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007580
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007581 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007582 if( !session )
7583 session = ssl->session;
7584
Andrzej Kurekeb342242019-01-29 09:14:33 -05007585 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
7586 ? "client finished"
7587 : "server finished";
7588
7589#if defined(MBEDTLS_USE_PSA_CRYPTO)
7590 sha256_psa = psa_hash_operation_init();
7591
7592 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha256" ) );
7593
7594 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
7595 if( status != PSA_SUCCESS )
7596 {
7597 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
7598 return;
7599 }
7600
7601 status = psa_hash_finish( &sha256_psa, padbuf, sizeof( padbuf ), &hash_size );
7602 if( status != PSA_SUCCESS )
7603 {
7604 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
7605 return;
7606 }
7607 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 32 );
7608#else
7609
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007610 mbedtls_sha256_init( &sha256 );
7611
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007612 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007613
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007614 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007615
7616 /*
7617 * TLSv1.2:
7618 * hash = PRF( master, finished_label,
7619 * Hash( handshake ) )[0.11]
7620 */
7621
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007622#if !defined(MBEDTLS_SHA256_ALT)
7623 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007624 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007625#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007626
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007627 mbedtls_sha256_finish_ret( &sha256, padbuf );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007628 mbedtls_sha256_free( &sha256 );
7629#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker1ef83d62012-04-11 12:09:53 +00007630
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007631 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00007632 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007633
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007634 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007635
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007636 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007637
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007638 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007639}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007640#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00007641
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007642#if defined(MBEDTLS_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00007643static void ssl_calc_finished_tls_sha384(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007644 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakkerca4ab492012-04-18 14:23:57 +00007645{
7646 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02007647 const char *sender;
Paul Bakkerca4ab492012-04-18 14:23:57 +00007648 unsigned char padbuf[48];
Andrzej Kurekeb342242019-01-29 09:14:33 -05007649#if defined(MBEDTLS_USE_PSA_CRYPTO)
7650 size_t hash_size;
Jaeden Amero34973232019-02-20 10:32:28 +00007651 psa_hash_operation_t sha384_psa = PSA_HASH_OPERATION_INIT;
Andrzej Kurekeb342242019-01-29 09:14:33 -05007652 psa_status_t status;
7653#else
7654 mbedtls_sha512_context sha512;
7655#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00007656
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007657 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007658 if( !session )
7659 session = ssl->session;
7660
Andrzej Kurekeb342242019-01-29 09:14:33 -05007661 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
7662 ? "client finished"
7663 : "server finished";
7664
7665#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05007666 sha384_psa = psa_hash_operation_init();
Andrzej Kurekeb342242019-01-29 09:14:33 -05007667
7668 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha384" ) );
7669
Andrzej Kurek972fba52019-01-30 03:29:12 -05007670 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007671 if( status != PSA_SUCCESS )
7672 {
7673 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
7674 return;
7675 }
7676
Andrzej Kurek972fba52019-01-30 03:29:12 -05007677 status = psa_hash_finish( &sha384_psa, padbuf, sizeof( padbuf ), &hash_size );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007678 if( status != PSA_SUCCESS )
7679 {
7680 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
7681 return;
7682 }
7683 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 48 );
7684#else
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007685 mbedtls_sha512_init( &sha512 );
7686
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007687 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007688
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007689 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007690
7691 /*
7692 * TLSv1.2:
7693 * hash = PRF( master, finished_label,
7694 * Hash( handshake ) )[0.11]
7695 */
7696
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007697#if !defined(MBEDTLS_SHA512_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007698 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
7699 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007700#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00007701
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007702 mbedtls_sha512_finish_ret( &sha512, padbuf );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007703 mbedtls_sha512_free( &sha512 );
7704#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00007705
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007706 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00007707 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007708
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007709 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007710
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007711 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007712
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007713 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007714}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007715#endif /* MBEDTLS_SHA512_C */
7716#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00007717
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007718static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00007719{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007720 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007721
7722 /*
7723 * Free our handshake params
7724 */
Gilles Peskine9b562d52018-04-25 20:32:43 +02007725 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007726 mbedtls_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00007727 ssl->handshake = NULL;
7728
7729 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007730 * Free the previous transform and swith in the current one
Paul Bakker48916f92012-09-16 19:57:18 +00007731 */
7732 if( ssl->transform )
7733 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007734 mbedtls_ssl_transform_free( ssl->transform );
7735 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00007736 }
7737 ssl->transform = ssl->transform_negotiate;
7738 ssl->transform_negotiate = NULL;
7739
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007740 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007741}
7742
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007743void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007744{
7745 int resume = ssl->handshake->resume;
7746
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007747 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007748
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007749#if defined(MBEDTLS_SSL_RENEGOTIATION)
7750 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007751 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007752 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007753 ssl->renego_records_seen = 0;
7754 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007755#endif
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007756
7757 /*
7758 * Free the previous session and switch in the current one
7759 */
Paul Bakker0a597072012-09-25 21:55:46 +00007760 if( ssl->session )
7761 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007762#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01007763 /* RFC 7366 3.1: keep the EtM state */
7764 ssl->session_negotiate->encrypt_then_mac =
7765 ssl->session->encrypt_then_mac;
7766#endif
7767
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007768 mbedtls_ssl_session_free( ssl->session );
7769 mbedtls_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00007770 }
7771 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007772 ssl->session_negotiate = NULL;
7773
Paul Bakker0a597072012-09-25 21:55:46 +00007774 /*
7775 * Add cache entry
7776 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007777 if( ssl->conf->f_set_cache != NULL &&
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02007778 ssl->session->id_len != 0 &&
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02007779 resume == 0 )
7780 {
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007781 if( ssl->conf->f_set_cache( ssl->conf->p_cache, ssl->session ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007782 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02007783 }
Paul Bakker0a597072012-09-25 21:55:46 +00007784
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007785#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007786 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007787 ssl->handshake->flight != NULL )
7788 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02007789 /* Cancel handshake timer */
7790 ssl_set_timer( ssl, 0 );
7791
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007792 /* Keep last flight around in case we need to resend it:
7793 * we need the handshake and transform structures for that */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007794 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007795 }
7796 else
7797#endif
7798 ssl_handshake_wrapup_free_hs_transform( ssl );
7799
Paul Bakker48916f92012-09-16 19:57:18 +00007800 ssl->state++;
7801
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007802 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007803}
7804
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007805int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
Paul Bakker1ef83d62012-04-11 12:09:53 +00007806{
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007807 int ret, hash_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007808
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007809 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007810
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007811 ssl_update_out_pointers( ssl, ssl->transform_negotiate );
Paul Bakker92be97b2013-01-02 17:30:03 +01007812
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007813 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007814
Manuel Pégourié-Gonnard214a8482016-02-22 11:27:26 +01007815 /*
7816 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
7817 * may define some other value. Currently (early 2016), no defined
7818 * ciphersuite does this (and this is unlikely to change as activity has
7819 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
7820 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007821 hash_len = ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) ? 36 : 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00007822
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007823#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00007824 ssl->verify_data_len = hash_len;
7825 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007826#endif
Paul Bakker48916f92012-09-16 19:57:18 +00007827
Paul Bakker5121ce52009-01-03 21:22:43 +00007828 ssl->out_msglen = 4 + hash_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007829 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
7830 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
Paul Bakker5121ce52009-01-03 21:22:43 +00007831
7832 /*
7833 * In case of session resuming, invert the client and server
7834 * ChangeCipherSpec messages order.
7835 */
Paul Bakker0a597072012-09-25 21:55:46 +00007836 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007837 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007838#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007839 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007840 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007841#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007842#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007843 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007844 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007845#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007846 }
7847 else
7848 ssl->state++;
7849
Paul Bakker48916f92012-09-16 19:57:18 +00007850 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02007851 * Switch to our negotiated transform and session parameters for outbound
7852 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00007853 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007854 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01007855
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007856#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007857 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007858 {
7859 unsigned char i;
7860
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007861 /* Remember current epoch settings for resending */
7862 ssl->handshake->alt_transform_out = ssl->transform_out;
Hanno Becker19859472018-08-06 09:40:20 +01007863 memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007864
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007865 /* Set sequence_number to zero */
Hanno Becker19859472018-08-06 09:40:20 +01007866 memset( ssl->cur_out_ctr + 2, 0, 6 );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007867
7868 /* Increment epoch */
7869 for( i = 2; i > 0; i-- )
Hanno Becker19859472018-08-06 09:40:20 +01007870 if( ++ssl->cur_out_ctr[i - 1] != 0 )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007871 break;
7872
7873 /* The loop goes to its end iff the counter is wrapping */
7874 if( i == 0 )
7875 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007876 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
7877 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007878 }
7879 }
7880 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007881#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker19859472018-08-06 09:40:20 +01007882 memset( ssl->cur_out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007883
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007884 ssl->transform_out = ssl->transform_negotiate;
7885 ssl->session_out = ssl->session_negotiate;
7886
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007887#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7888 if( mbedtls_ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01007889 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007890 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Paul Bakker07eb38b2012-12-19 14:42:06 +01007891 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007892 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
7893 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker07eb38b2012-12-19 14:42:06 +01007894 }
7895 }
7896#endif
7897
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007898#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007899 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007900 mbedtls_ssl_send_flight_completed( ssl );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02007901#endif
7902
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007903 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007904 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007905 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007906 return( ret );
7907 }
7908
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02007909#if defined(MBEDTLS_SSL_PROTO_DTLS)
7910 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7911 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
7912 {
7913 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
7914 return( ret );
7915 }
7916#endif
7917
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007918 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007919
7920 return( 0 );
7921}
7922
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007923#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007924#define SSL_MAX_HASH_LEN 36
7925#else
7926#define SSL_MAX_HASH_LEN 12
7927#endif
7928
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007929int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007930{
Paul Bakker23986e52011-04-24 08:57:21 +00007931 int ret;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007932 unsigned int hash_len;
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007933 unsigned char buf[SSL_MAX_HASH_LEN];
Paul Bakker5121ce52009-01-03 21:22:43 +00007934
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007935 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007936
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007937 ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007938
Hanno Becker327c93b2018-08-15 13:56:18 +01007939 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007940 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007941 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007942 return( ret );
7943 }
7944
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007945 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00007946 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007947 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007948 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7949 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007950 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007951 }
7952
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007953 /* There is currently no ciphersuite using another length with TLS 1.2 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007954#if defined(MBEDTLS_SSL_PROTO_SSL3)
7955 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007956 hash_len = 36;
7957 else
7958#endif
7959 hash_len = 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00007960
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007961 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED ||
7962 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00007963 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007964 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007965 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7966 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007967 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00007968 }
7969
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007970 if( mbedtls_ssl_safer_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00007971 buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007972 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007973 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007974 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7975 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007976 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00007977 }
7978
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007979#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00007980 ssl->verify_data_len = hash_len;
7981 memcpy( ssl->peer_verify_data, buf, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007982#endif
Paul Bakker48916f92012-09-16 19:57:18 +00007983
Paul Bakker0a597072012-09-25 21:55:46 +00007984 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007985 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007986#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007987 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007988 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007989#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007990#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007991 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007992 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007993#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007994 }
7995 else
7996 ssl->state++;
7997
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007998#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007999 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008000 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008001#endif
8002
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008003 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008004
8005 return( 0 );
8006}
8007
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008008static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008009{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008010 memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008011
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008012#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
8013 defined(MBEDTLS_SSL_PROTO_TLS1_1)
8014 mbedtls_md5_init( &handshake->fin_md5 );
8015 mbedtls_sha1_init( &handshake->fin_sha1 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008016 mbedtls_md5_starts_ret( &handshake->fin_md5 );
8017 mbedtls_sha1_starts_ret( &handshake->fin_sha1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008018#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008019#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
8020#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05008021#if defined(MBEDTLS_USE_PSA_CRYPTO)
8022 handshake->fin_sha256_psa = psa_hash_operation_init();
8023 psa_hash_setup( &handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
8024#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008025 mbedtls_sha256_init( &handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008026 mbedtls_sha256_starts_ret( &handshake->fin_sha256, 0 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008027#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05008028#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008029#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05008030#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05008031 handshake->fin_sha384_psa = psa_hash_operation_init();
8032 psa_hash_setup( &handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05008033#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008034 mbedtls_sha512_init( &handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008035 mbedtls_sha512_starts_ret( &handshake->fin_sha512, 1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008036#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05008037#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008038#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008039
8040 handshake->update_checksum = ssl_update_checksum_start;
Hanno Becker7e5437a2017-04-28 17:15:26 +01008041
8042#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
8043 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
8044 mbedtls_ssl_sig_hash_set_init( &handshake->hash_algs );
8045#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008046
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008047#if defined(MBEDTLS_DHM_C)
8048 mbedtls_dhm_init( &handshake->dhm_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008049#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008050#if defined(MBEDTLS_ECDH_C)
8051 mbedtls_ecdh_init( &handshake->ecdh_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008052#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008053#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02008054 mbedtls_ecjpake_init( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02008055#if defined(MBEDTLS_SSL_CLI_C)
8056 handshake->ecjpake_cache = NULL;
8057 handshake->ecjpake_cache_len = 0;
8058#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02008059#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02008060
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02008061#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02008062 mbedtls_x509_crt_restart_init( &handshake->ecrs_ctx );
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02008063#endif
8064
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02008065#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
8066 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
8067#endif
Hanno Becker75173122019-02-06 16:18:31 +00008068
8069#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
8070 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8071 mbedtls_pk_init( &handshake->peer_pubkey );
8072#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008073}
8074
Hanno Beckera18d1322018-01-03 14:27:32 +00008075void mbedtls_ssl_transform_init( mbedtls_ssl_transform *transform )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008076{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008077 memset( transform, 0, sizeof(mbedtls_ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02008078
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008079 mbedtls_cipher_init( &transform->cipher_ctx_enc );
8080 mbedtls_cipher_init( &transform->cipher_ctx_dec );
Paul Bakker84bbeb52014-07-01 14:53:22 +02008081
Hanno Beckerd56ed242018-01-03 15:32:51 +00008082#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008083 mbedtls_md_init( &transform->md_ctx_enc );
8084 mbedtls_md_init( &transform->md_ctx_dec );
Hanno Beckerd56ed242018-01-03 15:32:51 +00008085#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008086}
8087
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008088void mbedtls_ssl_session_init( mbedtls_ssl_session *session )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008089{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008090 memset( session, 0, sizeof(mbedtls_ssl_session) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008091}
8092
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008093static int ssl_handshake_init( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00008094{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008095 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00008096 if( ssl->transform_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008097 mbedtls_ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008098 if( ssl->session_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008099 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008100 if( ssl->handshake )
Gilles Peskine9b562d52018-04-25 20:32:43 +02008101 mbedtls_ssl_handshake_free( ssl );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008102
8103 /*
8104 * Either the pointers are now NULL or cleared properly and can be freed.
8105 * Now allocate missing structures.
8106 */
8107 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008108 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008109 ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008110 }
Paul Bakker48916f92012-09-16 19:57:18 +00008111
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008112 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008113 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008114 ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008115 }
Paul Bakker48916f92012-09-16 19:57:18 +00008116
Paul Bakker82788fb2014-10-20 13:59:19 +02008117 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008118 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008119 ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008120 }
Paul Bakker48916f92012-09-16 19:57:18 +00008121
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008122 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00008123 if( ssl->handshake == NULL ||
8124 ssl->transform_negotiate == NULL ||
8125 ssl->session_negotiate == NULL )
8126 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02008127 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008128
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008129 mbedtls_free( ssl->handshake );
8130 mbedtls_free( ssl->transform_negotiate );
8131 mbedtls_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008132
8133 ssl->handshake = NULL;
8134 ssl->transform_negotiate = NULL;
8135 ssl->session_negotiate = NULL;
8136
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008137 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker48916f92012-09-16 19:57:18 +00008138 }
8139
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008140 /* Initialize structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008141 mbedtls_ssl_session_init( ssl->session_negotiate );
Hanno Beckera18d1322018-01-03 14:27:32 +00008142 mbedtls_ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02008143 ssl_handshake_params_init( ssl->handshake );
8144
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008145#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02008146 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
8147 {
8148 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008149
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02008150 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
8151 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
8152 else
8153 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008154
8155 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02008156 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008157#endif
8158
Paul Bakker48916f92012-09-16 19:57:18 +00008159 return( 0 );
8160}
8161
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008162#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008163/* Dummy cookie callbacks for defaults */
8164static int ssl_cookie_write_dummy( void *ctx,
8165 unsigned char **p, unsigned char *end,
8166 const unsigned char *cli_id, size_t cli_id_len )
8167{
8168 ((void) ctx);
8169 ((void) p);
8170 ((void) end);
8171 ((void) cli_id);
8172 ((void) cli_id_len);
8173
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008174 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008175}
8176
8177static int ssl_cookie_check_dummy( void *ctx,
8178 const unsigned char *cookie, size_t cookie_len,
8179 const unsigned char *cli_id, size_t cli_id_len )
8180{
8181 ((void) ctx);
8182 ((void) cookie);
8183 ((void) cookie_len);
8184 ((void) cli_id);
8185 ((void) cli_id_len);
8186
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008187 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008188}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008189#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008190
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008191/* Once ssl->out_hdr as the address of the beginning of the
8192 * next outgoing record is set, deduce the other pointers.
8193 *
8194 * Note: For TLS, we save the implicit record sequence number
8195 * (entering MAC computation) in the 8 bytes before ssl->out_hdr,
8196 * and the caller has to make sure there's space for this.
8197 */
8198
8199static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
8200 mbedtls_ssl_transform *transform )
8201{
8202#if defined(MBEDTLS_SSL_PROTO_DTLS)
8203 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
8204 {
8205 ssl->out_ctr = ssl->out_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008206#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008207 ssl->out_cid = ssl->out_ctr + 8;
8208 ssl->out_len = ssl->out_cid;
8209 if( transform != NULL )
8210 ssl->out_len += transform->out_cid_len;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008211#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008212 ssl->out_len = ssl->out_ctr + 8;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008213#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008214 ssl->out_iv = ssl->out_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008215 }
8216 else
8217#endif
8218 {
8219 ssl->out_ctr = ssl->out_hdr - 8;
8220 ssl->out_len = ssl->out_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008221#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker4c3eb7c2019-05-08 16:43:21 +01008222 ssl->out_cid = ssl->out_len;
8223#endif
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008224 ssl->out_iv = ssl->out_hdr + 5;
8225 }
8226
8227 /* Adjust out_msg to make space for explicit IV, if used. */
8228 if( transform != NULL &&
8229 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
8230 {
8231 ssl->out_msg = ssl->out_iv + transform->ivlen - transform->fixed_ivlen;
8232 }
8233 else
8234 ssl->out_msg = ssl->out_iv;
8235}
8236
8237/* Once ssl->in_hdr as the address of the beginning of the
8238 * next incoming record is set, deduce the other pointers.
8239 *
8240 * Note: For TLS, we save the implicit record sequence number
8241 * (entering MAC computation) in the 8 bytes before ssl->in_hdr,
8242 * and the caller has to make sure there's space for this.
8243 */
8244
Hanno Becker79594fd2019-05-08 09:38:41 +01008245static void ssl_update_in_pointers( mbedtls_ssl_context *ssl )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008246{
Hanno Becker79594fd2019-05-08 09:38:41 +01008247 /* This function sets the pointers to match the case
8248 * of unprotected TLS/DTLS records, with both ssl->in_iv
8249 * and ssl->in_msg pointing to the beginning of the record
8250 * content.
8251 *
8252 * When decrypting a protected record, ssl->in_msg
8253 * will be shifted to point to the beginning of the
8254 * record plaintext.
8255 */
8256
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008257#if defined(MBEDTLS_SSL_PROTO_DTLS)
8258 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
8259 {
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008260 /* This sets the header pointers to match records
8261 * without CID. When we receive a record containing
8262 * a CID, the fields are shifted accordingly in
8263 * ssl_parse_record_header(). */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008264 ssl->in_ctr = ssl->in_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008265#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008266 ssl->in_cid = ssl->in_ctr + 8;
8267 ssl->in_len = ssl->in_cid; /* Default: no CID */
Hanno Beckera0e20d02019-05-15 14:03:01 +01008268#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008269 ssl->in_len = ssl->in_ctr + 8;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008270#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008271 ssl->in_iv = ssl->in_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008272 }
8273 else
8274#endif
8275 {
8276 ssl->in_ctr = ssl->in_hdr - 8;
8277 ssl->in_len = ssl->in_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008278#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker4c3eb7c2019-05-08 16:43:21 +01008279 ssl->in_cid = ssl->in_len;
8280#endif
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008281 ssl->in_iv = ssl->in_hdr + 5;
8282 }
8283
Hanno Becker79594fd2019-05-08 09:38:41 +01008284 /* This will be adjusted at record decryption time. */
8285 ssl->in_msg = ssl->in_iv;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008286}
8287
Paul Bakker5121ce52009-01-03 21:22:43 +00008288/*
8289 * Initialize an SSL context
8290 */
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02008291void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
8292{
8293 memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
8294}
8295
8296/*
8297 * Setup an SSL context
8298 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008299
8300static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl )
8301{
8302 /* Set the incoming and outgoing record pointers. */
8303#if defined(MBEDTLS_SSL_PROTO_DTLS)
8304 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
8305 {
8306 ssl->out_hdr = ssl->out_buf;
8307 ssl->in_hdr = ssl->in_buf;
8308 }
8309 else
8310#endif /* MBEDTLS_SSL_PROTO_DTLS */
8311 {
8312 ssl->out_hdr = ssl->out_buf + 8;
8313 ssl->in_hdr = ssl->in_buf + 8;
8314 }
8315
8316 /* Derive other internal pointers. */
8317 ssl_update_out_pointers( ssl, NULL /* no transform enabled */ );
Hanno Becker79594fd2019-05-08 09:38:41 +01008318 ssl_update_in_pointers ( ssl );
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008319}
8320
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02008321int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +02008322 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +00008323{
Paul Bakker48916f92012-09-16 19:57:18 +00008324 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00008325
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02008326 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00008327
8328 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01008329 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00008330 */
k-stachowiakc9a5f022018-07-24 13:53:31 +02008331
8332 /* Set to NULL in case of an error condition */
8333 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02008334
Angus Grattond8213d02016-05-25 20:56:48 +10008335 ssl->in_buf = mbedtls_calloc( 1, MBEDTLS_SSL_IN_BUFFER_LEN );
8336 if( ssl->in_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00008337 {
Angus Grattond8213d02016-05-25 20:56:48 +10008338 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_IN_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02008339 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02008340 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +10008341 }
8342
8343 ssl->out_buf = mbedtls_calloc( 1, MBEDTLS_SSL_OUT_BUFFER_LEN );
8344 if( ssl->out_buf == NULL )
8345 {
8346 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_OUT_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02008347 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02008348 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00008349 }
8350
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008351 ssl_reset_in_out_pointers( ssl );
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02008352
Paul Bakker48916f92012-09-16 19:57:18 +00008353 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
k-stachowiaka47911c2018-07-04 17:41:58 +02008354 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00008355
8356 return( 0 );
k-stachowiaka47911c2018-07-04 17:41:58 +02008357
8358error:
8359 mbedtls_free( ssl->in_buf );
8360 mbedtls_free( ssl->out_buf );
8361
8362 ssl->conf = NULL;
8363
8364 ssl->in_buf = NULL;
8365 ssl->out_buf = NULL;
8366
8367 ssl->in_hdr = NULL;
8368 ssl->in_ctr = NULL;
8369 ssl->in_len = NULL;
8370 ssl->in_iv = NULL;
8371 ssl->in_msg = NULL;
8372
8373 ssl->out_hdr = NULL;
8374 ssl->out_ctr = NULL;
8375 ssl->out_len = NULL;
8376 ssl->out_iv = NULL;
8377 ssl->out_msg = NULL;
8378
k-stachowiak9f7798e2018-07-31 16:52:32 +02008379 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008380}
8381
8382/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00008383 * Reset an initialized and used SSL context for re-use while retaining
8384 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008385 *
8386 * If partial is non-zero, keep data in the input buffer and client ID.
8387 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00008388 */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008389static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +00008390{
Paul Bakker48916f92012-09-16 19:57:18 +00008391 int ret;
8392
Hanno Becker7e772132018-08-10 12:38:21 +01008393#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || \
8394 !defined(MBEDTLS_SSL_SRV_C)
8395 ((void) partial);
8396#endif
8397
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008398 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008399
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008400 /* Cancel any possibly running timer */
8401 ssl_set_timer( ssl, 0 );
8402
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008403#if defined(MBEDTLS_SSL_RENEGOTIATION)
8404 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008405 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00008406
8407 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008408 memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
8409 memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008410#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008411 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00008412
Paul Bakker7eb013f2011-10-06 12:37:39 +00008413 ssl->in_offt = NULL;
Hanno Beckerf29d4702018-08-10 11:31:15 +01008414 ssl_reset_in_out_pointers( ssl );
Paul Bakker7eb013f2011-10-06 12:37:39 +00008415
8416 ssl->in_msgtype = 0;
8417 ssl->in_msglen = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008418#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02008419 ssl->next_record_offset = 0;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02008420 ssl->in_epoch = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02008421#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008422#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02008423 ssl_dtls_replay_reset( ssl );
8424#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00008425
8426 ssl->in_hslen = 0;
8427 ssl->nb_zero = 0;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01008428
8429 ssl->keep_current_message = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00008430
8431 ssl->out_msgtype = 0;
8432 ssl->out_msglen = 0;
8433 ssl->out_left = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008434#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
8435 if( ssl->split_done != MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008436 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008437#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00008438
Hanno Becker19859472018-08-06 09:40:20 +01008439 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
8440
Paul Bakker48916f92012-09-16 19:57:18 +00008441 ssl->transform_in = NULL;
8442 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00008443
Hanno Becker78640902018-08-13 16:35:15 +01008444 ssl->session_in = NULL;
8445 ssl->session_out = NULL;
8446
Angus Grattond8213d02016-05-25 20:56:48 +10008447 memset( ssl->out_buf, 0, MBEDTLS_SSL_OUT_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01008448
8449#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008450 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01008451#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
8452 {
8453 ssl->in_left = 0;
Angus Grattond8213d02016-05-25 20:56:48 +10008454 memset( ssl->in_buf, 0, MBEDTLS_SSL_IN_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01008455 }
Paul Bakker05ef8352012-05-08 09:17:57 +00008456
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008457#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
8458 if( mbedtls_ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00008459 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008460 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_reset()" ) );
8461 if( ( ret = mbedtls_ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00008462 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008463 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_reset", ret );
8464 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00008465 }
Paul Bakker05ef8352012-05-08 09:17:57 +00008466 }
8467#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00008468
Paul Bakker48916f92012-09-16 19:57:18 +00008469 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00008470 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008471 mbedtls_ssl_transform_free( ssl->transform );
8472 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00008473 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00008474 }
Paul Bakker48916f92012-09-16 19:57:18 +00008475
Paul Bakkerc0463502013-02-14 11:19:38 +01008476 if( ssl->session )
8477 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008478 mbedtls_ssl_session_free( ssl->session );
8479 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01008480 ssl->session = NULL;
8481 }
8482
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008483#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008484 ssl->alpn_chosen = NULL;
8485#endif
8486
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008487#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker4ccbf062018-08-10 11:20:38 +01008488#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008489 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01008490#endif
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008491 {
8492 mbedtls_free( ssl->cli_id );
8493 ssl->cli_id = NULL;
8494 ssl->cli_id_len = 0;
8495 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02008496#endif
8497
Paul Bakker48916f92012-09-16 19:57:18 +00008498 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
8499 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00008500
8501 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00008502}
8503
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02008504/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008505 * Reset an initialized and used SSL context for re-use while retaining
8506 * all application-set variables, function pointers and data.
8507 */
8508int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
8509{
8510 return( ssl_session_reset_int( ssl, 0 ) );
8511}
8512
8513/*
Paul Bakker5121ce52009-01-03 21:22:43 +00008514 * SSL set accessors
8515 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008516void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00008517{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008518 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00008519}
8520
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008521void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01008522{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008523 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01008524}
8525
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008526#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008527void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02008528{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008529 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02008530}
8531#endif
8532
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008533#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008534void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf, unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02008535{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008536 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02008537}
8538#endif
8539
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008540#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01008541
Hanno Becker1841b0a2018-08-24 11:13:57 +01008542void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl,
8543 unsigned allow_packing )
Hanno Becker04da1892018-08-14 13:22:10 +01008544{
8545 ssl->disable_datagram_packing = !allow_packing;
8546}
8547
8548void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
8549 uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02008550{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008551 conf->hs_timeout_min = min;
8552 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02008553}
8554#endif
8555
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008556void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00008557{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008558 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00008559}
8560
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008561#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008562void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02008563 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008564 void *p_vrfy )
8565{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008566 conf->f_vrfy = f_vrfy;
8567 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008568}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008569#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008570
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008571void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00008572 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00008573 void *p_rng )
8574{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01008575 conf->f_rng = f_rng;
8576 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00008577}
8578
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008579void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02008580 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00008581 void *p_dbg )
8582{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008583 conf->f_dbg = f_dbg;
8584 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00008585}
8586
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008587void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008588 void *p_bio,
Simon Butchere846b512016-03-01 17:31:49 +00008589 mbedtls_ssl_send_t *f_send,
8590 mbedtls_ssl_recv_t *f_recv,
8591 mbedtls_ssl_recv_timeout_t *f_recv_timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008592{
8593 ssl->p_bio = p_bio;
8594 ssl->f_send = f_send;
8595 ssl->f_recv = f_recv;
8596 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01008597}
8598
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02008599#if defined(MBEDTLS_SSL_PROTO_DTLS)
8600void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu )
8601{
8602 ssl->mtu = mtu;
8603}
8604#endif
8605
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008606void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01008607{
8608 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008609}
8610
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008611void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
8612 void *p_timer,
Simon Butchere846b512016-03-01 17:31:49 +00008613 mbedtls_ssl_set_timer_t *f_set_timer,
8614 mbedtls_ssl_get_timer_t *f_get_timer )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008615{
8616 ssl->p_timer = p_timer;
8617 ssl->f_set_timer = f_set_timer;
8618 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008619
8620 /* Make sure we start with no timer running */
8621 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008622}
8623
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008624#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008625void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01008626 void *p_cache,
8627 int (*f_get_cache)(void *, mbedtls_ssl_session *),
8628 int (*f_set_cache)(void *, const mbedtls_ssl_session *) )
Paul Bakker5121ce52009-01-03 21:22:43 +00008629{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01008630 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008631 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008632 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00008633}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008634#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008635
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008636#if defined(MBEDTLS_SSL_CLI_C)
8637int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00008638{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008639 int ret;
8640
8641 if( ssl == NULL ||
8642 session == NULL ||
8643 ssl->session_negotiate == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008644 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008645 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008646 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008647 }
8648
Hanno Becker52055ae2019-02-06 14:30:46 +00008649 if( ( ret = mbedtls_ssl_session_copy( ssl->session_negotiate,
8650 session ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008651 return( ret );
8652
Paul Bakker0a597072012-09-25 21:55:46 +00008653 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008654
8655 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00008656}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008657#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008658
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008659void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008660 const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00008661{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008662 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = ciphersuites;
8663 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = ciphersuites;
8664 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = ciphersuites;
8665 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = ciphersuites;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008666}
8667
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008668void mbedtls_ssl_conf_ciphersuites_for_version( mbedtls_ssl_config *conf,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02008669 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008670 int major, int minor )
8671{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008672 if( major != MBEDTLS_SSL_MAJOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008673 return;
8674
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008675 if( minor < MBEDTLS_SSL_MINOR_VERSION_0 || minor > MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008676 return;
8677
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008678 conf->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00008679}
8680
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008681#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02008682void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01008683 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02008684{
8685 conf->cert_profile = profile;
8686}
8687
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008688/* Append a new keycert entry to a (possibly empty) list */
8689static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
8690 mbedtls_x509_crt *cert,
8691 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008692{
niisato8ee24222018-06-25 19:05:48 +09008693 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008694
niisato8ee24222018-06-25 19:05:48 +09008695 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
8696 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008697 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008698
niisato8ee24222018-06-25 19:05:48 +09008699 new_cert->cert = cert;
8700 new_cert->key = key;
8701 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008702
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008703 /* Update head is the list was null, else add to the end */
8704 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01008705 {
niisato8ee24222018-06-25 19:05:48 +09008706 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01008707 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008708 else
8709 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008710 mbedtls_ssl_key_cert *cur = *head;
8711 while( cur->next != NULL )
8712 cur = cur->next;
niisato8ee24222018-06-25 19:05:48 +09008713 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008714 }
8715
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008716 return( 0 );
8717}
8718
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008719int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008720 mbedtls_x509_crt *own_cert,
8721 mbedtls_pk_context *pk_key )
8722{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02008723 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008724}
8725
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008726void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008727 mbedtls_x509_crt *ca_chain,
8728 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008729{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008730 conf->ca_chain = ca_chain;
8731 conf->ca_crl = ca_crl;
Hanno Becker5adaad92019-03-27 16:54:37 +00008732
8733#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
8734 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
8735 * cannot be used together. */
8736 conf->f_ca_cb = NULL;
8737 conf->p_ca_cb = NULL;
8738#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Paul Bakker5121ce52009-01-03 21:22:43 +00008739}
Hanno Becker5adaad92019-03-27 16:54:37 +00008740
8741#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
8742void mbedtls_ssl_conf_ca_cb( mbedtls_ssl_config *conf,
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00008743 mbedtls_x509_crt_ca_cb_t f_ca_cb,
Hanno Becker5adaad92019-03-27 16:54:37 +00008744 void *p_ca_cb )
8745{
8746 conf->f_ca_cb = f_ca_cb;
8747 conf->p_ca_cb = p_ca_cb;
8748
8749 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
8750 * cannot be used together. */
8751 conf->ca_chain = NULL;
8752 conf->ca_crl = NULL;
8753}
8754#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008755#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00008756
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02008757#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
8758int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
8759 mbedtls_x509_crt *own_cert,
8760 mbedtls_pk_context *pk_key )
8761{
8762 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
8763 own_cert, pk_key ) );
8764}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02008765
8766void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
8767 mbedtls_x509_crt *ca_chain,
8768 mbedtls_x509_crl *ca_crl )
8769{
8770 ssl->handshake->sni_ca_chain = ca_chain;
8771 ssl->handshake->sni_ca_crl = ca_crl;
8772}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02008773
8774void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
8775 int authmode )
8776{
8777 ssl->handshake->sni_authmode = authmode;
8778}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02008779#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
8780
Hanno Becker8927c832019-04-03 12:52:50 +01008781#if defined(MBEDTLS_X509_CRT_PARSE_C)
8782void mbedtls_ssl_set_verify( mbedtls_ssl_context *ssl,
8783 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
8784 void *p_vrfy )
8785{
8786 ssl->f_vrfy = f_vrfy;
8787 ssl->p_vrfy = p_vrfy;
8788}
8789#endif
8790
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008791#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008792/*
8793 * Set EC J-PAKE password for current handshake
8794 */
8795int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
8796 const unsigned char *pw,
8797 size_t pw_len )
8798{
8799 mbedtls_ecjpake_role role;
8800
Janos Follath8eb64132016-06-03 15:40:57 +01008801 if( ssl->handshake == NULL || ssl->conf == NULL )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008802 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8803
8804 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
8805 role = MBEDTLS_ECJPAKE_SERVER;
8806 else
8807 role = MBEDTLS_ECJPAKE_CLIENT;
8808
8809 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
8810 role,
8811 MBEDTLS_MD_SHA256,
8812 MBEDTLS_ECP_DP_SECP256R1,
8813 pw, pw_len ) );
8814}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008815#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008816
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008817#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008818
8819static void ssl_conf_remove_psk( mbedtls_ssl_config *conf )
8820{
8821 /* Remove reference to existing PSK, if any. */
8822#if defined(MBEDTLS_USE_PSA_CRYPTO)
8823 if( conf->psk_opaque != 0 )
8824 {
8825 /* The maintenance of the PSK key slot is the
8826 * user's responsibility. */
8827 conf->psk_opaque = 0;
8828 }
Hanno Beckera63ac3f2018-11-05 12:47:16 +00008829 /* This and the following branch should never
8830 * be taken simultaenously as we maintain the
8831 * invariant that raw and opaque PSKs are never
8832 * configured simultaneously. As a safeguard,
8833 * though, `else` is omitted here. */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008834#endif /* MBEDTLS_USE_PSA_CRYPTO */
8835 if( conf->psk != NULL )
8836 {
8837 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
8838
8839 mbedtls_free( conf->psk );
8840 conf->psk = NULL;
8841 conf->psk_len = 0;
8842 }
8843
8844 /* Remove reference to PSK identity, if any. */
8845 if( conf->psk_identity != NULL )
8846 {
8847 mbedtls_free( conf->psk_identity );
8848 conf->psk_identity = NULL;
8849 conf->psk_identity_len = 0;
8850 }
8851}
8852
Hanno Becker7390c712018-11-15 13:33:04 +00008853/* This function assumes that PSK identity in the SSL config is unset.
8854 * It checks that the provided identity is well-formed and attempts
8855 * to make a copy of it in the SSL config.
8856 * On failure, the PSK identity in the config remains unset. */
8857static int ssl_conf_set_psk_identity( mbedtls_ssl_config *conf,
8858 unsigned char const *psk_identity,
8859 size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02008860{
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02008861 /* Identity len will be encoded on two bytes */
Hanno Becker7390c712018-11-15 13:33:04 +00008862 if( psk_identity == NULL ||
8863 ( psk_identity_len >> 16 ) != 0 ||
Angus Grattond8213d02016-05-25 20:56:48 +10008864 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02008865 {
8866 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8867 }
8868
Hanno Becker7390c712018-11-15 13:33:04 +00008869 conf->psk_identity = mbedtls_calloc( 1, psk_identity_len );
8870 if( conf->psk_identity == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008871 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker6db455e2013-09-18 17:29:31 +02008872
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008873 conf->psk_identity_len = psk_identity_len;
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008874 memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker5ad403f2013-09-18 21:21:30 +02008875
8876 return( 0 );
Paul Bakker6db455e2013-09-18 17:29:31 +02008877}
8878
Hanno Becker7390c712018-11-15 13:33:04 +00008879int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
8880 const unsigned char *psk, size_t psk_len,
8881 const unsigned char *psk_identity, size_t psk_identity_len )
8882{
8883 int ret;
8884 /* Remove opaque/raw PSK + PSK Identity */
8885 ssl_conf_remove_psk( conf );
8886
8887 /* Check and set raw PSK */
8888 if( psk == NULL || psk_len > MBEDTLS_PSK_MAX_LEN )
8889 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8890 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
8891 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
8892 conf->psk_len = psk_len;
8893 memcpy( conf->psk, psk, conf->psk_len );
8894
8895 /* Check and set PSK Identity */
8896 ret = ssl_conf_set_psk_identity( conf, psk_identity, psk_identity_len );
8897 if( ret != 0 )
8898 ssl_conf_remove_psk( conf );
8899
8900 return( ret );
8901}
8902
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008903static void ssl_remove_psk( mbedtls_ssl_context *ssl )
8904{
8905#if defined(MBEDTLS_USE_PSA_CRYPTO)
8906 if( ssl->handshake->psk_opaque != 0 )
8907 {
8908 ssl->handshake->psk_opaque = 0;
8909 }
8910 else
8911#endif /* MBEDTLS_USE_PSA_CRYPTO */
8912 if( ssl->handshake->psk != NULL )
8913 {
8914 mbedtls_platform_zeroize( ssl->handshake->psk,
8915 ssl->handshake->psk_len );
8916 mbedtls_free( ssl->handshake->psk );
8917 ssl->handshake->psk_len = 0;
8918 }
8919}
8920
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008921int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
8922 const unsigned char *psk, size_t psk_len )
8923{
8924 if( psk == NULL || ssl->handshake == NULL )
8925 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8926
8927 if( psk_len > MBEDTLS_PSK_MAX_LEN )
8928 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8929
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008930 ssl_remove_psk( ssl );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008931
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008932 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008933 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008934
8935 ssl->handshake->psk_len = psk_len;
8936 memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
8937
8938 return( 0 );
8939}
8940
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008941#if defined(MBEDTLS_USE_PSA_CRYPTO)
8942int mbedtls_ssl_conf_psk_opaque( mbedtls_ssl_config *conf,
Andrzej Kurek2349c4d2019-01-08 09:36:01 -05008943 psa_key_handle_t psk_slot,
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008944 const unsigned char *psk_identity,
8945 size_t psk_identity_len )
8946{
Hanno Becker7390c712018-11-15 13:33:04 +00008947 int ret;
8948 /* Clear opaque/raw PSK + PSK Identity, if present. */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008949 ssl_conf_remove_psk( conf );
8950
Hanno Becker7390c712018-11-15 13:33:04 +00008951 /* Check and set opaque PSK */
8952 if( psk_slot == 0 )
8953 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008954 conf->psk_opaque = psk_slot;
Hanno Becker7390c712018-11-15 13:33:04 +00008955
8956 /* Check and set PSK Identity */
8957 ret = ssl_conf_set_psk_identity( conf, psk_identity,
8958 psk_identity_len );
8959 if( ret != 0 )
8960 ssl_conf_remove_psk( conf );
8961
8962 return( ret );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008963}
8964
8965int mbedtls_ssl_set_hs_psk_opaque( mbedtls_ssl_context *ssl,
Andrzej Kurek2349c4d2019-01-08 09:36:01 -05008966 psa_key_handle_t psk_slot )
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008967{
8968 if( psk_slot == 0 || ssl->handshake == NULL )
8969 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8970
8971 ssl_remove_psk( ssl );
8972 ssl->handshake->psk_opaque = psk_slot;
8973 return( 0 );
8974}
8975#endif /* MBEDTLS_USE_PSA_CRYPTO */
8976
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008977void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008978 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
Paul Bakker6db455e2013-09-18 17:29:31 +02008979 size_t),
8980 void *p_psk )
8981{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008982 conf->f_psk = f_psk;
8983 conf->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02008984}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008985#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00008986
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02008987#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker470a8c42017-10-04 15:28:46 +01008988
8989#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008990int mbedtls_ssl_conf_dh_param( mbedtls_ssl_config *conf, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00008991{
8992 int ret;
8993
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008994 if( ( ret = mbedtls_mpi_read_string( &conf->dhm_P, 16, dhm_P ) ) != 0 ||
8995 ( ret = mbedtls_mpi_read_string( &conf->dhm_G, 16, dhm_G ) ) != 0 )
8996 {
8997 mbedtls_mpi_free( &conf->dhm_P );
8998 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00008999 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01009000 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009001
9002 return( 0 );
9003}
Hanno Becker470a8c42017-10-04 15:28:46 +01009004#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +00009005
Hanno Beckera90658f2017-10-04 15:29:08 +01009006int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
9007 const unsigned char *dhm_P, size_t P_len,
9008 const unsigned char *dhm_G, size_t G_len )
9009{
9010 int ret;
9011
9012 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
9013 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
9014 {
9015 mbedtls_mpi_free( &conf->dhm_P );
9016 mbedtls_mpi_free( &conf->dhm_G );
9017 return( ret );
9018 }
9019
9020 return( 0 );
9021}
Paul Bakker5121ce52009-01-03 21:22:43 +00009022
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009023int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00009024{
9025 int ret;
9026
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01009027 if( ( ret = mbedtls_mpi_copy( &conf->dhm_P, &dhm_ctx->P ) ) != 0 ||
9028 ( ret = mbedtls_mpi_copy( &conf->dhm_G, &dhm_ctx->G ) ) != 0 )
9029 {
9030 mbedtls_mpi_free( &conf->dhm_P );
9031 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00009032 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01009033 }
Paul Bakker1b57b062011-01-06 15:48:19 +00009034
9035 return( 0 );
9036}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02009037#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00009038
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02009039#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
9040/*
9041 * Set the minimum length for Diffie-Hellman parameters
9042 */
9043void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
9044 unsigned int bitlen )
9045{
9046 conf->dhm_min_bitlen = bitlen;
9047}
9048#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
9049
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009050#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02009051/*
9052 * Set allowed/preferred hashes for handshake signatures
9053 */
9054void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
9055 const int *hashes )
9056{
9057 conf->sig_hashes = hashes;
9058}
Hanno Becker947194e2017-04-07 13:25:49 +01009059#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02009060
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02009061#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01009062/*
9063 * Set the allowed elliptic curves
9064 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009065void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009066 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01009067{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009068 conf->curve_list = curve_list;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01009069}
Hanno Becker947194e2017-04-07 13:25:49 +01009070#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01009071
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01009072#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009073int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00009074{
Hanno Becker947194e2017-04-07 13:25:49 +01009075 /* Initialize to suppress unnecessary compiler warning */
9076 size_t hostname_len = 0;
9077
9078 /* Check if new hostname is valid before
9079 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01009080 if( hostname != NULL )
9081 {
9082 hostname_len = strlen( hostname );
9083
9084 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
9085 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9086 }
9087
9088 /* Now it's clear that we will overwrite the old hostname,
9089 * so we can free it safely */
9090
9091 if( ssl->hostname != NULL )
9092 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009093 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Hanno Becker947194e2017-04-07 13:25:49 +01009094 mbedtls_free( ssl->hostname );
9095 }
9096
9097 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01009098
Paul Bakker5121ce52009-01-03 21:22:43 +00009099 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01009100 {
9101 ssl->hostname = NULL;
9102 }
9103 else
9104 {
9105 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01009106 if( ssl->hostname == NULL )
9107 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02009108
Hanno Becker947194e2017-04-07 13:25:49 +01009109 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02009110
Hanno Becker947194e2017-04-07 13:25:49 +01009111 ssl->hostname[hostname_len] = '\0';
9112 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009113
9114 return( 0 );
9115}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01009116#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00009117
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01009118#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009119void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009120 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00009121 const unsigned char *, size_t),
9122 void *p_sni )
9123{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009124 conf->f_sni = f_sni;
9125 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00009126}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009127#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00009128
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009129#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009130int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009131{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009132 size_t cur_len, tot_len;
9133 const char **p;
9134
9135 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08009136 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
9137 * MUST NOT be truncated."
9138 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009139 */
9140 tot_len = 0;
9141 for( p = protos; *p != NULL; p++ )
9142 {
9143 cur_len = strlen( *p );
9144 tot_len += cur_len;
9145
9146 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009147 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009148 }
9149
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009150 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009151
9152 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009153}
9154
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009155const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009156{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009157 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009158}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009159#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009160
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02009161void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00009162{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009163 conf->max_major_ver = major;
9164 conf->max_minor_ver = minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00009165}
9166
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02009167void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00009168{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009169 conf->min_major_ver = major;
9170 conf->min_minor_ver = minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00009171}
9172
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009173#if defined(MBEDTLS_SSL_FALLBACK_SCSV) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009174void mbedtls_ssl_conf_fallback( mbedtls_ssl_config *conf, char fallback )
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02009175{
Manuel Pégourié-Gonnard684b0592015-05-06 09:27:31 +01009176 conf->fallback = fallback;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02009177}
9178#endif
9179
Janos Follath088ce432017-04-10 12:42:31 +01009180#if defined(MBEDTLS_SSL_SRV_C)
9181void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
9182 char cert_req_ca_list )
9183{
9184 conf->cert_req_ca_list = cert_req_ca_list;
9185}
9186#endif
9187
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009188#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009189void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01009190{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009191 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01009192}
9193#endif
9194
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009195#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009196void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02009197{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009198 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02009199}
9200#endif
9201
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02009202#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009203void mbedtls_ssl_conf_arc4_support( mbedtls_ssl_config *conf, char arc4 )
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01009204{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009205 conf->arc4_disabled = arc4;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01009206}
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02009207#endif
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01009208
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009209#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009210int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009211{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009212 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
Angus Grattond8213d02016-05-25 20:56:48 +10009213 ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009214 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009215 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009216 }
9217
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01009218 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009219
9220 return( 0 );
9221}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009222#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009223
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009224#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02009225void mbedtls_ssl_conf_truncated_hmac( mbedtls_ssl_config *conf, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02009226{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009227 conf->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02009228}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009229#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02009230
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009231#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009232void mbedtls_ssl_conf_cbc_record_splitting( mbedtls_ssl_config *conf, char split )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009233{
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01009234 conf->cbc_record_splitting = split;
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009235}
9236#endif
9237
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009238void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00009239{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009240 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00009241}
9242
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009243#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009244void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01009245{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009246 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01009247}
9248
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009249void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009250{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009251 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009252}
9253
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009254void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01009255 const unsigned char period[8] )
9256{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009257 memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01009258}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009259#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00009260
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009261#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009262#if defined(MBEDTLS_SSL_CLI_C)
9263void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02009264{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01009265 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02009266}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009267#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02009268
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009269#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02009270void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
9271 mbedtls_ssl_ticket_write_t *f_ticket_write,
9272 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
9273 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02009274{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02009275 conf->f_ticket_write = f_ticket_write;
9276 conf->f_ticket_parse = f_ticket_parse;
9277 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02009278}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009279#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009280#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02009281
Robert Cragie4feb7ae2015-10-02 13:33:37 +01009282#if defined(MBEDTLS_SSL_EXPORT_KEYS)
9283void mbedtls_ssl_conf_export_keys_cb( mbedtls_ssl_config *conf,
9284 mbedtls_ssl_export_keys_t *f_export_keys,
9285 void *p_export_keys )
9286{
9287 conf->f_export_keys = f_export_keys;
9288 conf->p_export_keys = p_export_keys;
9289}
Ron Eldorf5cc10d2019-05-07 18:33:40 +03009290
9291void mbedtls_ssl_conf_export_keys_ext_cb( mbedtls_ssl_config *conf,
9292 mbedtls_ssl_export_keys_ext_t *f_export_keys_ext,
9293 void *p_export_keys )
9294{
9295 conf->f_export_keys_ext = f_export_keys_ext;
9296 conf->p_export_keys = p_export_keys;
9297}
Robert Cragie4feb7ae2015-10-02 13:33:37 +01009298#endif
9299
Gilles Peskineb74a1c72018-04-24 13:09:22 +02009300#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009301void mbedtls_ssl_conf_async_private_cb(
9302 mbedtls_ssl_config *conf,
9303 mbedtls_ssl_async_sign_t *f_async_sign,
9304 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
9305 mbedtls_ssl_async_resume_t *f_async_resume,
9306 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009307 void *async_config_data )
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009308{
9309 conf->f_async_sign_start = f_async_sign;
9310 conf->f_async_decrypt_start = f_async_decrypt;
9311 conf->f_async_resume = f_async_resume;
9312 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009313 conf->p_async_config_data = async_config_data;
9314}
9315
Gilles Peskine8f97af72018-04-26 11:46:10 +02009316void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
9317{
9318 return( conf->p_async_config_data );
9319}
9320
Gilles Peskine1febfef2018-04-30 11:54:39 +02009321void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009322{
9323 if( ssl->handshake == NULL )
9324 return( NULL );
9325 else
9326 return( ssl->handshake->user_async_ctx );
9327}
9328
Gilles Peskine1febfef2018-04-30 11:54:39 +02009329void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009330 void *ctx )
9331{
9332 if( ssl->handshake != NULL )
9333 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009334}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02009335#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009336
Paul Bakker5121ce52009-01-03 21:22:43 +00009337/*
9338 * SSL get accessors
9339 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009340size_t mbedtls_ssl_get_bytes_avail( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009341{
9342 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
9343}
9344
Hanno Becker8b170a02017-10-10 11:51:19 +01009345int mbedtls_ssl_check_pending( const mbedtls_ssl_context *ssl )
9346{
9347 /*
9348 * Case A: We're currently holding back
9349 * a message for further processing.
9350 */
9351
9352 if( ssl->keep_current_message == 1 )
9353 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009354 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: record held back for processing" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009355 return( 1 );
9356 }
9357
9358 /*
9359 * Case B: Further records are pending in the current datagram.
9360 */
9361
9362#if defined(MBEDTLS_SSL_PROTO_DTLS)
9363 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
9364 ssl->in_left > ssl->next_record_offset )
9365 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009366 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more records within current datagram" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009367 return( 1 );
9368 }
9369#endif /* MBEDTLS_SSL_PROTO_DTLS */
9370
9371 /*
9372 * Case C: A handshake message is being processed.
9373 */
9374
Hanno Becker8b170a02017-10-10 11:51:19 +01009375 if( ssl->in_hslen > 0 && ssl->in_hslen < ssl->in_msglen )
9376 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009377 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more handshake messages within current record" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009378 return( 1 );
9379 }
9380
9381 /*
9382 * Case D: An application data message is being processed
9383 */
9384 if( ssl->in_offt != NULL )
9385 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009386 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: application data record is being processed" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009387 return( 1 );
9388 }
9389
9390 /*
9391 * In all other cases, the rest of the message can be dropped.
Hanno Beckerc573ac32018-08-28 17:15:25 +01009392 * As in ssl_get_next_record, this needs to be adapted if
Hanno Becker8b170a02017-10-10 11:51:19 +01009393 * we implement support for multiple alerts in single records.
9394 */
9395
9396 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: nothing pending" ) );
9397 return( 0 );
9398}
9399
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02009400uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009401{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00009402 if( ssl->session != NULL )
9403 return( ssl->session->verify_result );
9404
9405 if( ssl->session_negotiate != NULL )
9406 return( ssl->session_negotiate->verify_result );
9407
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02009408 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00009409}
9410
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009411const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00009412{
Paul Bakker926c8e42013-03-06 10:23:34 +01009413 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009414 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01009415
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009416 return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00009417}
9418
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009419const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00009420{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009421#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009422 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009423 {
9424 switch( ssl->minor_ver )
9425 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009426 case MBEDTLS_SSL_MINOR_VERSION_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009427 return( "DTLSv1.0" );
9428
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009429 case MBEDTLS_SSL_MINOR_VERSION_3:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009430 return( "DTLSv1.2" );
9431
9432 default:
9433 return( "unknown (DTLS)" );
9434 }
9435 }
9436#endif
9437
Paul Bakker43ca69c2011-01-15 17:35:19 +00009438 switch( ssl->minor_ver )
9439 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009440 case MBEDTLS_SSL_MINOR_VERSION_0:
Paul Bakker43ca69c2011-01-15 17:35:19 +00009441 return( "SSLv3.0" );
9442
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009443 case MBEDTLS_SSL_MINOR_VERSION_1:
Paul Bakker43ca69c2011-01-15 17:35:19 +00009444 return( "TLSv1.0" );
9445
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009446 case MBEDTLS_SSL_MINOR_VERSION_2:
Paul Bakker43ca69c2011-01-15 17:35:19 +00009447 return( "TLSv1.1" );
9448
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009449 case MBEDTLS_SSL_MINOR_VERSION_3:
Paul Bakker1ef83d62012-04-11 12:09:53 +00009450 return( "TLSv1.2" );
9451
Paul Bakker43ca69c2011-01-15 17:35:19 +00009452 default:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009453 return( "unknown" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00009454 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00009455}
9456
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009457int mbedtls_ssl_get_record_expansion( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009458{
Hanno Becker3136ede2018-08-17 15:28:19 +01009459 size_t transform_expansion = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009460 const mbedtls_ssl_transform *transform = ssl->transform_out;
Hanno Becker5b559ac2018-08-03 09:40:07 +01009461 unsigned block_size;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009462
Hanno Becker5903de42019-05-03 14:46:38 +01009463 size_t out_hdr_len = mbedtls_ssl_out_hdr_len( ssl );
9464
Hanno Becker78640902018-08-13 16:35:15 +01009465 if( transform == NULL )
Hanno Becker5903de42019-05-03 14:46:38 +01009466 return( (int) out_hdr_len );
Hanno Becker78640902018-08-13 16:35:15 +01009467
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009468#if defined(MBEDTLS_ZLIB_SUPPORT)
9469 if( ssl->session_out->compression != MBEDTLS_SSL_COMPRESS_NULL )
9470 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009471#endif
9472
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009473 switch( mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009474 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009475 case MBEDTLS_MODE_GCM:
9476 case MBEDTLS_MODE_CCM:
Hanno Becker5b559ac2018-08-03 09:40:07 +01009477 case MBEDTLS_MODE_CHACHAPOLY:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009478 case MBEDTLS_MODE_STREAM:
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009479 transform_expansion = transform->minlen;
9480 break;
9481
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009482 case MBEDTLS_MODE_CBC:
Hanno Becker5b559ac2018-08-03 09:40:07 +01009483
9484 block_size = mbedtls_cipher_get_block_size(
9485 &transform->cipher_ctx_enc );
9486
Hanno Becker3136ede2018-08-17 15:28:19 +01009487 /* Expansion due to the addition of the MAC. */
9488 transform_expansion += transform->maclen;
9489
9490 /* Expansion due to the addition of CBC padding;
9491 * Theoretically up to 256 bytes, but we never use
9492 * more than the block size of the underlying cipher. */
9493 transform_expansion += block_size;
9494
9495 /* For TLS 1.1 or higher, an explicit IV is added
9496 * after the record header. */
Hanno Becker5b559ac2018-08-03 09:40:07 +01009497#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
9498 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Hanno Becker3136ede2018-08-17 15:28:19 +01009499 transform_expansion += block_size;
Hanno Becker5b559ac2018-08-03 09:40:07 +01009500#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker3136ede2018-08-17 15:28:19 +01009501
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009502 break;
9503
9504 default:
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02009505 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009506 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009507 }
9508
Hanno Beckera0e20d02019-05-15 14:03:01 +01009509#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker6cbad552019-05-08 15:40:11 +01009510 if( transform->out_cid_len != 0 )
9511 transform_expansion += MBEDTLS_SSL_MAX_CID_EXPANSION;
Hanno Beckera0e20d02019-05-15 14:03:01 +01009512#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker6cbad552019-05-08 15:40:11 +01009513
Hanno Becker5903de42019-05-03 14:46:38 +01009514 return( (int)( out_hdr_len + transform_expansion ) );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009515}
9516
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009517#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9518size_t mbedtls_ssl_get_max_frag_len( const mbedtls_ssl_context *ssl )
9519{
9520 size_t max_len;
9521
9522 /*
9523 * Assume mfl_code is correct since it was checked when set
9524 */
Angus Grattond8213d02016-05-25 20:56:48 +10009525 max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009526
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02009527 /* Check if a smaller max length was negotiated */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009528 if( ssl->session_out != NULL &&
Angus Grattond8213d02016-05-25 20:56:48 +10009529 ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009530 {
Angus Grattond8213d02016-05-25 20:56:48 +10009531 max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009532 }
9533
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02009534 /* During a handshake, use the value being negotiated */
9535 if( ssl->session_negotiate != NULL &&
9536 ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
9537 {
9538 max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
9539 }
9540
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009541 return( max_len );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009542}
9543#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
9544
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009545#if defined(MBEDTLS_SSL_PROTO_DTLS)
9546static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl )
9547{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04009548 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
9549 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
9550 ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
9551 ssl->state == MBEDTLS_SSL_SERVER_HELLO ) )
9552 return ( 0 );
9553
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009554 if( ssl->handshake == NULL || ssl->handshake->mtu == 0 )
9555 return( ssl->mtu );
9556
9557 if( ssl->mtu == 0 )
9558 return( ssl->handshake->mtu );
9559
9560 return( ssl->mtu < ssl->handshake->mtu ?
9561 ssl->mtu : ssl->handshake->mtu );
9562}
9563#endif /* MBEDTLS_SSL_PROTO_DTLS */
9564
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009565int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
9566{
9567 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
9568
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02009569#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
9570 !defined(MBEDTLS_SSL_PROTO_DTLS)
9571 (void) ssl;
9572#endif
9573
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009574#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9575 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
9576
9577 if( max_len > mfl )
9578 max_len = mfl;
9579#endif
9580
9581#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009582 if( ssl_get_current_mtu( ssl ) != 0 )
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009583 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009584 const size_t mtu = ssl_get_current_mtu( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009585 const int ret = mbedtls_ssl_get_record_expansion( ssl );
9586 const size_t overhead = (size_t) ret;
9587
9588 if( ret < 0 )
9589 return( ret );
9590
9591 if( mtu <= overhead )
9592 {
9593 MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
9594 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
9595 }
9596
9597 if( max_len > mtu - overhead )
9598 max_len = mtu - overhead;
9599 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009600#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009601
Hanno Becker0defedb2018-08-10 12:35:02 +01009602#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
9603 !defined(MBEDTLS_SSL_PROTO_DTLS)
9604 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009605#endif
9606
9607 return( (int) max_len );
9608}
9609
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009610#if defined(MBEDTLS_X509_CRT_PARSE_C)
9611const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00009612{
9613 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009614 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00009615
Hanno Beckere6824572019-02-07 13:18:46 +00009616#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009617 return( ssl->session->peer_cert );
Hanno Beckere6824572019-02-07 13:18:46 +00009618#else
9619 return( NULL );
9620#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakkerb0550d92012-10-30 07:51:03 +00009621}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009622#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00009623
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009624#if defined(MBEDTLS_SSL_CLI_C)
Hanno Beckerf852b1c2019-02-05 11:42:30 +00009625int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl,
9626 mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009627{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009628 if( ssl == NULL ||
9629 dst == NULL ||
9630 ssl->session == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009631 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009632 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009633 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009634 }
9635
Hanno Becker52055ae2019-02-06 14:30:46 +00009636 return( mbedtls_ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009637}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009638#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009639
Paul Bakker5121ce52009-01-03 21:22:43 +00009640/*
Paul Bakker1961b702013-01-25 14:49:24 +01009641 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00009642 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009643int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009644{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009645 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00009646
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009647 if( ssl == NULL || ssl->conf == NULL )
9648 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9649
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009650#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009651 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009652 ret = mbedtls_ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00009653#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009654#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009655 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009656 ret = mbedtls_ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00009657#endif
9658
Paul Bakker1961b702013-01-25 14:49:24 +01009659 return( ret );
9660}
9661
9662/*
9663 * Perform the SSL handshake
9664 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009665int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +01009666{
9667 int ret = 0;
9668
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009669 if( ssl == NULL || ssl->conf == NULL )
9670 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9671
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009672 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +01009673
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009674 while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker1961b702013-01-25 14:49:24 +01009675 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009676 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01009677
9678 if( ret != 0 )
9679 break;
9680 }
9681
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009682 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009683
9684 return( ret );
9685}
9686
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009687#if defined(MBEDTLS_SSL_RENEGOTIATION)
9688#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00009689/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009690 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00009691 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009692static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009693{
9694 int ret;
9695
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009696 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009697
9698 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009699 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
9700 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009701
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02009702 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009703 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02009704 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009705 return( ret );
9706 }
9707
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009708 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009709
9710 return( 0 );
9711}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009712#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009713
9714/*
9715 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009716 * - any side: calling mbedtls_ssl_renegotiate(),
9717 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
9718 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02009719 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009720 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009721 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009722 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009723static int ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00009724{
9725 int ret;
9726
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009727 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009728
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009729 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
9730 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00009731
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009732 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
9733 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009734#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009735 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009736 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009737 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009738 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02009739 ssl->handshake->out_msg_seq = 1;
9740 else
9741 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009742 }
9743#endif
9744
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009745 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
9746 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00009747
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009748 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00009749 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009750 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00009751 return( ret );
9752 }
9753
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009754 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009755
9756 return( 0 );
9757}
9758
9759/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009760 * Renegotiate current connection on client,
9761 * or request renegotiation on server
9762 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009763int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009764{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009765 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009766
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009767 if( ssl == NULL || ssl->conf == NULL )
9768 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9769
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009770#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009771 /* On server, just send the request */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009772 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009773 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009774 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
9775 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009776
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009777 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02009778
9779 /* Did we already try/start sending HelloRequest? */
9780 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009781 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02009782
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009783 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009784 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009785#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009786
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009787#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009788 /*
9789 * On client, either start the renegotiation process or,
9790 * if already in progress, continue the handshake
9791 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009792 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009793 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009794 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
9795 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009796
9797 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
9798 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009799 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009800 return( ret );
9801 }
9802 }
9803 else
9804 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009805 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009806 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009807 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009808 return( ret );
9809 }
9810 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009811#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009812
Paul Bakker37ce0ff2013-10-31 14:32:04 +01009813 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009814}
9815
9816/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009817 * Check record counters and renegotiate if they're above the limit.
9818 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009819static int ssl_check_ctr_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009820{
Andres AG2196c7f2016-12-15 17:01:16 +00009821 size_t ep_len = ssl_ep_len( ssl );
9822 int in_ctr_cmp;
9823 int out_ctr_cmp;
9824
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009825 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ||
9826 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009827 ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009828 {
9829 return( 0 );
9830 }
9831
Andres AG2196c7f2016-12-15 17:01:16 +00009832 in_ctr_cmp = memcmp( ssl->in_ctr + ep_len,
9833 ssl->conf->renego_period + ep_len, 8 - ep_len );
Hanno Becker19859472018-08-06 09:40:20 +01009834 out_ctr_cmp = memcmp( ssl->cur_out_ctr + ep_len,
Andres AG2196c7f2016-12-15 17:01:16 +00009835 ssl->conf->renego_period + ep_len, 8 - ep_len );
9836
9837 if( in_ctr_cmp <= 0 && out_ctr_cmp <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009838 {
9839 return( 0 );
9840 }
9841
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02009842 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009843 return( mbedtls_ssl_renegotiate( ssl ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009844}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009845#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00009846
9847/*
9848 * Receive application data decrypted from the SSL layer
9849 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009850int mbedtls_ssl_read( mbedtls_ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00009851{
Hanno Becker4a810fb2017-05-24 16:27:30 +01009852 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +00009853 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00009854
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009855 if( ssl == NULL || ssl->conf == NULL )
9856 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9857
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009858 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009859
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009860#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009861 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009862 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009863 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009864 return( ret );
9865
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009866 if( ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009867 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009868 {
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02009869 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009870 return( ret );
9871 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009872 }
9873#endif
9874
Hanno Becker4a810fb2017-05-24 16:27:30 +01009875 /*
9876 * Check if renegotiation is necessary and/or handshake is
9877 * in process. If yes, perform/continue, and fall through
9878 * if an unexpected packet is received while the client
9879 * is waiting for the ServerHello.
9880 *
9881 * (There is no equivalent to the last condition on
9882 * the server-side as it is not treated as within
9883 * a handshake while waiting for the ClientHello
9884 * after a renegotiation request.)
9885 */
9886
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009887#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01009888 ret = ssl_check_ctr_renegotiate( ssl );
9889 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
9890 ret != 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009891 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009892 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009893 return( ret );
9894 }
9895#endif
9896
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009897 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00009898 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009899 ret = mbedtls_ssl_handshake( ssl );
Hanno Becker4a810fb2017-05-24 16:27:30 +01009900 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
9901 ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009902 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009903 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009904 return( ret );
9905 }
9906 }
9907
Hanno Beckere41158b2017-10-23 13:30:32 +01009908 /* Loop as long as no application data record is available */
Hanno Becker90333da2017-10-10 11:27:13 +01009909 while( ssl->in_offt == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00009910 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02009911 /* Start timer if not already running */
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02009912 if( ssl->f_get_timer != NULL &&
9913 ssl->f_get_timer( ssl->p_timer ) == -1 )
9914 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009915 ssl_set_timer( ssl, ssl->conf->read_timeout );
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02009916 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02009917
Hanno Becker327c93b2018-08-15 13:56:18 +01009918 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009919 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01009920 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
9921 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +00009922
Hanno Becker4a810fb2017-05-24 16:27:30 +01009923 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
9924 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009925 }
9926
9927 if( ssl->in_msglen == 0 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009928 ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00009929 {
9930 /*
9931 * OpenSSL sends empty messages to randomize the IV
9932 */
Hanno Becker327c93b2018-08-15 13:56:18 +01009933 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009934 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009935 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
Paul Bakker831a7552011-05-18 13:32:51 +00009936 return( 0 );
9937
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009938 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009939 return( ret );
9940 }
9941 }
9942
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009943 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +00009944 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009945 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009946
Hanno Becker4a810fb2017-05-24 16:27:30 +01009947 /*
9948 * - For client-side, expect SERVER_HELLO_REQUEST.
9949 * - For server-side, expect CLIENT_HELLO.
9950 * - Fail (TLS) or silently drop record (DTLS) in other cases.
9951 */
9952
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009953#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009954 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009955 ( ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST ||
Hanno Becker4a810fb2017-05-24 16:27:30 +01009956 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ) )
Paul Bakker48916f92012-09-16 19:57:18 +00009957 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009958 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009959
9960 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009961#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009962 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01009963 {
9964 continue;
9965 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009966#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009967 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009968 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01009969#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009970
Hanno Becker4a810fb2017-05-24 16:27:30 +01009971#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009972 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009973 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO )
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009974 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009975 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009976
9977 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009978#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009979 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01009980 {
9981 continue;
9982 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009983#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009984 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker48916f92012-09-16 19:57:18 +00009985 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01009986#endif /* MBEDTLS_SSL_SRV_C */
9987
Hanno Becker21df7f92017-10-17 11:03:26 +01009988#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01009989 /* Determine whether renegotiation attempt should be accepted */
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01009990 if( ! ( ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
9991 ( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
9992 ssl->conf->allow_legacy_renegotiation ==
9993 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) ) )
9994 {
9995 /*
9996 * Accept renegotiation request
9997 */
Paul Bakker48916f92012-09-16 19:57:18 +00009998
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01009999 /* DTLS clients need to know renego is server-initiated */
10000#if defined(MBEDTLS_SSL_PROTO_DTLS)
10001 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
10002 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
10003 {
10004 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
10005 }
10006#endif
10007 ret = ssl_start_renegotiation( ssl );
10008 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
10009 ret != 0 )
10010 {
10011 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
10012 return( ret );
10013 }
10014 }
10015 else
Hanno Becker21df7f92017-10-17 11:03:26 +010010016#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +000010017 {
Hanno Becker4a810fb2017-05-24 16:27:30 +010010018 /*
10019 * Refuse renegotiation
10020 */
10021
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010022 MBEDTLS_SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010023
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010024#if defined(MBEDTLS_SSL_PROTO_SSL3)
10025 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +000010026 {
Gilles Peskine92e44262017-05-10 17:27:49 +020010027 /* SSLv3 does not have a "no_renegotiation" warning, so
10028 we send a fatal alert and abort the connection. */
10029 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
10030 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
10031 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +000010032 }
10033 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010034#endif /* MBEDTLS_SSL_PROTO_SSL3 */
10035#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
10036 defined(MBEDTLS_SSL_PROTO_TLS1_2)
10037 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +000010038 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010039 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
10040 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
10041 MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +000010042 {
10043 return( ret );
10044 }
Paul Bakker48916f92012-09-16 19:57:18 +000010045 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +020010046 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010047#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 ||
10048 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +020010049 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010050 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
10051 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +020010052 }
Paul Bakker48916f92012-09-16 19:57:18 +000010053 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020010054
Hanno Becker90333da2017-10-10 11:27:13 +010010055 /* At this point, we don't know whether the renegotiation has been
10056 * completed or not. The cases to consider are the following:
10057 * 1) The renegotiation is complete. In this case, no new record
10058 * has been read yet.
10059 * 2) The renegotiation is incomplete because the client received
10060 * an application data record while awaiting the ServerHello.
10061 * 3) The renegotiation is incomplete because the client received
10062 * a non-handshake, non-application data message while awaiting
10063 * the ServerHello.
10064 * In each of these case, looping will be the proper action:
10065 * - For 1), the next iteration will read a new record and check
10066 * if it's application data.
10067 * - For 2), the loop condition isn't satisfied as application data
10068 * is present, hence continue is the same as break
10069 * - For 3), the loop condition is satisfied and read_record
10070 * will re-deliver the message that was held back by the client
10071 * when expecting the ServerHello.
10072 */
10073 continue;
Paul Bakker48916f92012-09-16 19:57:18 +000010074 }
Hanno Becker21df7f92017-10-17 11:03:26 +010010075#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010076 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +010010077 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010078 if( ssl->conf->renego_max_records >= 0 )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +020010079 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010080 if( ++ssl->renego_records_seen > ssl->conf->renego_max_records )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020010081 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010082 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020010083 "but not honored by client" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010084 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020010085 }
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +020010086 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +010010087 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010088#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020010089
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010090 /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */
10091 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020010092 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010093 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +010010094 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020010095 }
10096
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010097 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +000010098 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010099 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
10100 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +000010101 }
10102
10103 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +020010104
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +020010105 /* We're going to return something now, cancel timer,
10106 * except if handshake (renegotiation) is in progress */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010107 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +020010108 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010109
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +020010110#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010111 /* If we requested renego but received AppData, resend HelloRequest.
10112 * Do it now, after setting in_offt, to avoid taking this branch
10113 * again if ssl_write_hello_request() returns WANT_WRITE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010114#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010115 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010116 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010117 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020010118 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010119 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010120 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010121 return( ret );
10122 }
10123 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010124#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker4a810fb2017-05-24 16:27:30 +010010125#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +000010126 }
10127
10128 n = ( len < ssl->in_msglen )
10129 ? len : ssl->in_msglen;
10130
10131 memcpy( buf, ssl->in_offt, n );
10132 ssl->in_msglen -= n;
10133
10134 if( ssl->in_msglen == 0 )
Hanno Becker4a810fb2017-05-24 16:27:30 +010010135 {
10136 /* all bytes consumed */
Paul Bakker5121ce52009-01-03 21:22:43 +000010137 ssl->in_offt = NULL;
Hanno Beckerbdf39052017-06-09 10:42:03 +010010138 ssl->keep_current_message = 0;
Hanno Becker4a810fb2017-05-24 16:27:30 +010010139 }
Paul Bakker5121ce52009-01-03 21:22:43 +000010140 else
Hanno Becker4a810fb2017-05-24 16:27:30 +010010141 {
Paul Bakker5121ce52009-01-03 21:22:43 +000010142 /* more data available */
10143 ssl->in_offt += n;
Hanno Becker4a810fb2017-05-24 16:27:30 +010010144 }
Paul Bakker5121ce52009-01-03 21:22:43 +000010145
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010146 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010147
Paul Bakker23986e52011-04-24 08:57:21 +000010148 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +000010149}
10150
10151/*
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010010152 * Send application data to be encrypted by the SSL layer, taking care of max
10153 * fragment length and buffer size.
10154 *
10155 * According to RFC 5246 Section 6.2.1:
10156 *
10157 * Zero-length fragments of Application data MAY be sent as they are
10158 * potentially useful as a traffic analysis countermeasure.
10159 *
10160 * Therefore, it is possible that the input message length is 0 and the
10161 * corresponding return code is 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +000010162 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010163static int ssl_write_real( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010164 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +000010165{
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +020010166 int ret = mbedtls_ssl_get_max_out_record_payload( ssl );
10167 const size_t max_len = (size_t) ret;
10168
10169 if( ret < 0 )
10170 {
10171 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_get_max_out_record_payload", ret );
10172 return( ret );
10173 }
10174
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010175 if( len > max_len )
10176 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010177#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010178 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010179 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010180 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) "
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010181 "maximum fragment length: %d > %d",
10182 len, max_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010183 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010184 }
10185 else
10186#endif
10187 len = max_len;
10188 }
Paul Bakker887bd502011-06-08 13:10:54 +000010189
Paul Bakker5121ce52009-01-03 21:22:43 +000010190 if( ssl->out_left != 0 )
10191 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010010192 /*
10193 * The user has previously tried to send the data and
10194 * MBEDTLS_ERR_SSL_WANT_WRITE or the message was only partially
10195 * written. In this case, we expect the high-level write function
10196 * (e.g. mbedtls_ssl_write()) to be called with the same parameters
10197 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010198 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010199 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010200 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010201 return( ret );
10202 }
10203 }
Paul Bakker887bd502011-06-08 13:10:54 +000010204 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +000010205 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010010206 /*
10207 * The user is trying to send a message the first time, so we need to
10208 * copy the data into the internal buffers and setup the data structure
10209 * to keep track of partial writes
10210 */
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010211 ssl->out_msglen = len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010212 ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA;
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010213 memcpy( ssl->out_msg, buf, len );
Paul Bakker887bd502011-06-08 13:10:54 +000010214
Hanno Becker67bc7c32018-08-06 11:33:50 +010010215 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker887bd502011-06-08 13:10:54 +000010216 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010217 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker887bd502011-06-08 13:10:54 +000010218 return( ret );
10219 }
Paul Bakker5121ce52009-01-03 21:22:43 +000010220 }
10221
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010222 return( (int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +000010223}
10224
10225/*
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010226 * Write application data, doing 1/n-1 splitting if necessary.
10227 *
10228 * With non-blocking I/O, ssl_write_real() may return WANT_WRITE,
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +010010229 * then the caller will call us again with the same arguments, so
Hanno Becker2b187c42017-09-18 14:58:11 +010010230 * remember whether we already did the split or not.
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010231 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010232#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010233static int ssl_write_split( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010234 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010235{
10236 int ret;
10237
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +010010238 if( ssl->conf->cbc_record_splitting ==
10239 MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED ||
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +010010240 len <= 1 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010241 ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_1 ||
10242 mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc )
10243 != MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010244 {
10245 return( ssl_write_real( ssl, buf, len ) );
10246 }
10247
10248 if( ssl->split_done == 0 )
10249 {
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010010250 if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010251 return( ret );
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010010252 ssl->split_done = 1;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010253 }
10254
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010010255 if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 )
10256 return( ret );
10257 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010258
10259 return( ret + 1 );
10260}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010261#endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010262
10263/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010264 * Write application data (public-facing wrapper)
10265 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010266int mbedtls_ssl_write( mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010267{
10268 int ret;
10269
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010270 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010271
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010272 if( ssl == NULL || ssl->conf == NULL )
10273 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10274
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010275#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010276 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
10277 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010278 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010279 return( ret );
10280 }
10281#endif
10282
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010283 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010284 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010285 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010286 {
Manuel Pégourié-Gonnard151dc772015-05-14 13:55:51 +020010287 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010288 return( ret );
10289 }
10290 }
10291
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010292#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010293 ret = ssl_write_split( ssl, buf, len );
10294#else
10295 ret = ssl_write_real( ssl, buf, len );
10296#endif
10297
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010298 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010299
10300 return( ret );
10301}
10302
10303/*
Paul Bakker5121ce52009-01-03 21:22:43 +000010304 * Notify the peer that the connection is being closed
10305 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010306int mbedtls_ssl_close_notify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000010307{
10308 int ret;
10309
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010310 if( ssl == NULL || ssl->conf == NULL )
10311 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10312
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010313 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010314
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +020010315 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010316 return( mbedtls_ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010317
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010318 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +000010319 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010320 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
10321 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
10322 MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010323 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010324 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010325 return( ret );
10326 }
10327 }
10328
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010329 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010330
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +020010331 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +000010332}
10333
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010334void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform )
Paul Bakker48916f92012-09-16 19:57:18 +000010335{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010336 if( transform == NULL )
10337 return;
10338
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010339#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +000010340 deflateEnd( &transform->ctx_deflate );
10341 inflateEnd( &transform->ctx_inflate );
10342#endif
10343
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010344 mbedtls_cipher_free( &transform->cipher_ctx_enc );
10345 mbedtls_cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +020010346
Hanno Beckerd56ed242018-01-03 15:32:51 +000010347#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010348 mbedtls_md_free( &transform->md_ctx_enc );
10349 mbedtls_md_free( &transform->md_ctx_dec );
Hanno Beckerd56ed242018-01-03 15:32:51 +000010350#endif
Paul Bakker61d113b2013-07-04 11:51:43 +020010351
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010352 mbedtls_platform_zeroize( transform, sizeof( mbedtls_ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010353}
10354
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010355#if defined(MBEDTLS_X509_CRT_PARSE_C)
10356static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010357{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010358 mbedtls_ssl_key_cert *cur = key_cert, *next;
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010359
10360 while( cur != NULL )
10361 {
10362 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010363 mbedtls_free( cur );
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010364 cur = next;
10365 }
10366}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010367#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010368
Hanno Becker0271f962018-08-16 13:23:47 +010010369#if defined(MBEDTLS_SSL_PROTO_DTLS)
10370
10371static void ssl_buffering_free( mbedtls_ssl_context *ssl )
10372{
10373 unsigned offset;
10374 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
10375
10376 if( hs == NULL )
10377 return;
10378
Hanno Becker283f5ef2018-08-24 09:34:47 +010010379 ssl_free_buffered_record( ssl );
10380
Hanno Becker0271f962018-08-16 13:23:47 +010010381 for( offset = 0; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
Hanno Beckere605b192018-08-21 15:59:07 +010010382 ssl_buffering_free_slot( ssl, offset );
10383}
10384
10385static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
10386 uint8_t slot )
10387{
10388 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
10389 mbedtls_ssl_hs_buffer * const hs_buf = &hs->buffering.hs[slot];
Hanno Beckerb309b922018-08-23 13:18:05 +010010390
10391 if( slot >= MBEDTLS_SSL_MAX_BUFFERED_HS )
10392 return;
10393
Hanno Beckere605b192018-08-21 15:59:07 +010010394 if( hs_buf->is_valid == 1 )
Hanno Becker0271f962018-08-16 13:23:47 +010010395 {
Hanno Beckere605b192018-08-21 15:59:07 +010010396 hs->buffering.total_bytes_buffered -= hs_buf->data_len;
Hanno Becker805f2e12018-10-12 16:31:41 +010010397 mbedtls_platform_zeroize( hs_buf->data, hs_buf->data_len );
Hanno Beckere605b192018-08-21 15:59:07 +010010398 mbedtls_free( hs_buf->data );
10399 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Hanno Becker0271f962018-08-16 13:23:47 +010010400 }
10401}
10402
10403#endif /* MBEDTLS_SSL_PROTO_DTLS */
10404
Gilles Peskine9b562d52018-04-25 20:32:43 +020010405void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +000010406{
Gilles Peskine9b562d52018-04-25 20:32:43 +020010407 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
10408
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010409 if( handshake == NULL )
10410 return;
10411
Gilles Peskinedf13d5c2018-04-25 20:39:48 +020010412#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
10413 if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
10414 {
Gilles Peskine8f97af72018-04-26 11:46:10 +020010415 ssl->conf->f_async_cancel( ssl );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +020010416 handshake->async_in_progress = 0;
10417 }
10418#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
10419
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010420#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
10421 defined(MBEDTLS_SSL_PROTO_TLS1_1)
10422 mbedtls_md5_free( &handshake->fin_md5 );
10423 mbedtls_sha1_free( &handshake->fin_sha1 );
10424#endif
10425#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
10426#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -050010427#if defined(MBEDTLS_USE_PSA_CRYPTO)
10428 psa_hash_abort( &handshake->fin_sha256_psa );
10429#else
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010430 mbedtls_sha256_free( &handshake->fin_sha256 );
10431#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -050010432#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010433#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -050010434#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -050010435 psa_hash_abort( &handshake->fin_sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -050010436#else
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010437 mbedtls_sha512_free( &handshake->fin_sha512 );
10438#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -050010439#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010440#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
10441
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010442#if defined(MBEDTLS_DHM_C)
10443 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +000010444#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010445#if defined(MBEDTLS_ECDH_C)
10446 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +020010447#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +020010448#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +020010449 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +020010450#if defined(MBEDTLS_SSL_CLI_C)
10451 mbedtls_free( handshake->ecjpake_cache );
10452 handshake->ecjpake_cache = NULL;
10453 handshake->ecjpake_cache_len = 0;
10454#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +020010455#endif
Paul Bakker61d113b2013-07-04 11:51:43 +020010456
Janos Follath4ae5c292016-02-10 11:27:43 +000010457#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
10458 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +020010459 /* explicit void pointer cast for buggy MS compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010460 mbedtls_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +020010461#endif
10462
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +010010463#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
10464 if( handshake->psk != NULL )
10465 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010466 mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +010010467 mbedtls_free( handshake->psk );
10468 }
10469#endif
10470
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010471#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
10472 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020010473 /*
10474 * Free only the linked list wrapper, not the keys themselves
10475 * since the belong to the SNI callback
10476 */
10477 if( handshake->sni_key_cert != NULL )
10478 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010479 mbedtls_ssl_key_cert *cur = handshake->sni_key_cert, *next;
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020010480
10481 while( cur != NULL )
10482 {
10483 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010484 mbedtls_free( cur );
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020010485 cur = next;
10486 }
10487 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010488#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010489
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +020010490#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +020010491 mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx );
Hanno Becker3dad3112019-02-05 17:19:52 +000010492 if( handshake->ecrs_peer_cert != NULL )
10493 {
10494 mbedtls_x509_crt_free( handshake->ecrs_peer_cert );
10495 mbedtls_free( handshake->ecrs_peer_cert );
10496 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +020010497#endif
10498
Hanno Becker75173122019-02-06 16:18:31 +000010499#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
10500 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
10501 mbedtls_pk_free( &handshake->peer_pubkey );
10502#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
10503
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010504#if defined(MBEDTLS_SSL_PROTO_DTLS)
10505 mbedtls_free( handshake->verify_cookie );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +020010506 ssl_flight_free( handshake->flight );
Hanno Becker0271f962018-08-16 13:23:47 +010010507 ssl_buffering_free( ssl );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +020010508#endif
10509
Hanno Becker4a63ed42019-01-08 11:39:35 +000010510#if defined(MBEDTLS_ECDH_C) && \
10511 defined(MBEDTLS_USE_PSA_CRYPTO)
10512 psa_destroy_key( handshake->ecdh_psa_privkey );
10513#endif /* MBEDTLS_ECDH_C && MBEDTLS_USE_PSA_CRYPTO */
10514
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010515 mbedtls_platform_zeroize( handshake,
10516 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010517}
10518
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010519void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +000010520{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010521 if( session == NULL )
10522 return;
10523
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010524#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker1294a0b2019-02-05 12:38:15 +000010525 ssl_clear_peer_cert( session );
Paul Bakkered27a042013-04-18 22:46:23 +020010526#endif
Paul Bakker0a597072012-09-25 21:55:46 +000010527
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +020010528#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010529 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +020010530#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +020010531
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010532 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010533}
10534
Paul Bakker5121ce52009-01-03 21:22:43 +000010535/*
10536 * Free an SSL context
10537 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010538void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000010539{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010540 if( ssl == NULL )
10541 return;
10542
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010543 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010544
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +010010545 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010546 {
Angus Grattond8213d02016-05-25 20:56:48 +100010547 mbedtls_platform_zeroize( ssl->out_buf, MBEDTLS_SSL_OUT_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010548 mbedtls_free( ssl->out_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +000010549 }
10550
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +010010551 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010552 {
Angus Grattond8213d02016-05-25 20:56:48 +100010553 mbedtls_platform_zeroize( ssl->in_buf, MBEDTLS_SSL_IN_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010554 mbedtls_free( ssl->in_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +000010555 }
10556
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010557#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker16770332013-10-11 09:59:44 +020010558 if( ssl->compress_buf != NULL )
10559 {
Angus Grattond8213d02016-05-25 20:56:48 +100010560 mbedtls_platform_zeroize( ssl->compress_buf, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010561 mbedtls_free( ssl->compress_buf );
Paul Bakker16770332013-10-11 09:59:44 +020010562 }
10563#endif
10564
Paul Bakker48916f92012-09-16 19:57:18 +000010565 if( ssl->transform )
10566 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010567 mbedtls_ssl_transform_free( ssl->transform );
10568 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +000010569 }
10570
10571 if( ssl->handshake )
10572 {
Gilles Peskine9b562d52018-04-25 20:32:43 +020010573 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010574 mbedtls_ssl_transform_free( ssl->transform_negotiate );
10575 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +000010576
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010577 mbedtls_free( ssl->handshake );
10578 mbedtls_free( ssl->transform_negotiate );
10579 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +000010580 }
10581
Paul Bakkerc0463502013-02-14 11:19:38 +010010582 if( ssl->session )
10583 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010584 mbedtls_ssl_session_free( ssl->session );
10585 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +010010586 }
10587
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +020010588#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker66d5d072014-06-17 16:39:18 +020010589 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010590 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010591 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010592 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +000010593 }
Paul Bakker0be444a2013-08-27 21:55:01 +020010594#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000010595
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010596#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
10597 if( mbedtls_ssl_hw_record_finish != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +000010598 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010599 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_finish()" ) );
10600 mbedtls_ssl_hw_record_finish( ssl );
Paul Bakker05ef8352012-05-08 09:17:57 +000010601 }
10602#endif
10603
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +020010604#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010605 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +020010606#endif
10607
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010608 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +000010609
Paul Bakker86f04f42013-02-14 11:20:09 +010010610 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010611 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010612}
10613
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010614/*
10615 * Initialze mbedtls_ssl_config
10616 */
10617void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
10618{
10619 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
10620}
10621
Simon Butcherc97b6972015-12-27 23:48:17 +000010622#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010623static int ssl_preset_default_hashes[] = {
10624#if defined(MBEDTLS_SHA512_C)
10625 MBEDTLS_MD_SHA512,
10626 MBEDTLS_MD_SHA384,
10627#endif
10628#if defined(MBEDTLS_SHA256_C)
10629 MBEDTLS_MD_SHA256,
10630 MBEDTLS_MD_SHA224,
10631#endif
Gilles Peskine5d2511c2017-05-12 13:16:40 +020010632#if defined(MBEDTLS_SHA1_C) && defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_KEY_EXCHANGE)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010633 MBEDTLS_MD_SHA1,
10634#endif
10635 MBEDTLS_MD_NONE
10636};
Simon Butcherc97b6972015-12-27 23:48:17 +000010637#endif
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010638
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010639static int ssl_preset_suiteb_ciphersuites[] = {
10640 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
10641 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
10642 0
10643};
10644
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010645#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010646static int ssl_preset_suiteb_hashes[] = {
10647 MBEDTLS_MD_SHA256,
10648 MBEDTLS_MD_SHA384,
10649 MBEDTLS_MD_NONE
10650};
10651#endif
10652
10653#if defined(MBEDTLS_ECP_C)
10654static mbedtls_ecp_group_id ssl_preset_suiteb_curves[] = {
Jaeden Amerod4311042019-06-03 08:27:16 +010010655#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010656 MBEDTLS_ECP_DP_SECP256R1,
Jaeden Amerod4311042019-06-03 08:27:16 +010010657#endif
10658#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010659 MBEDTLS_ECP_DP_SECP384R1,
Jaeden Amerod4311042019-06-03 08:27:16 +010010660#endif
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010661 MBEDTLS_ECP_DP_NONE
10662};
10663#endif
10664
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010665/*
Tillmann Karras588ad502015-09-25 04:27:22 +020010666 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010667 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020010668int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010669 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010670{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +020010671#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010672 int ret;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +020010673#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010674
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +020010675 /* Use the functions here so that they are covered in tests,
10676 * but otherwise access member directly for efficiency */
10677 mbedtls_ssl_conf_endpoint( conf, endpoint );
10678 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010679
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010680 /*
10681 * Things that are common to all presets
10682 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020010683#if defined(MBEDTLS_SSL_CLI_C)
10684 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
10685 {
10686 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
10687#if defined(MBEDTLS_SSL_SESSION_TICKETS)
10688 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
10689#endif
10690 }
10691#endif
10692
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +020010693#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010694 conf->arc4_disabled = MBEDTLS_SSL_ARC4_DISABLED;
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +020010695#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010696
10697#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
10698 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
10699#endif
10700
10701#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
10702 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
10703#endif
10704
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +010010705#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
10706 conf->cbc_record_splitting = MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED;
10707#endif
10708
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +020010709#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010710 conf->f_cookie_write = ssl_cookie_write_dummy;
10711 conf->f_cookie_check = ssl_cookie_check_dummy;
10712#endif
10713
10714#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
10715 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
10716#endif
10717
Janos Follath088ce432017-04-10 12:42:31 +010010718#if defined(MBEDTLS_SSL_SRV_C)
10719 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
10720#endif
10721
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010722#if defined(MBEDTLS_SSL_PROTO_DTLS)
10723 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
10724 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
10725#endif
10726
10727#if defined(MBEDTLS_SSL_RENEGOTIATION)
10728 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Andres AG2196c7f2016-12-15 17:01:16 +000010729 memset( conf->renego_period, 0x00, 2 );
10730 memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010731#endif
10732
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010733#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
10734 if( endpoint == MBEDTLS_SSL_IS_SERVER )
10735 {
Hanno Becker00d0a682017-10-04 13:14:29 +010010736 const unsigned char dhm_p[] =
10737 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
10738 const unsigned char dhm_g[] =
10739 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
10740
Hanno Beckera90658f2017-10-04 15:29:08 +010010741 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
10742 dhm_p, sizeof( dhm_p ),
10743 dhm_g, sizeof( dhm_g ) ) ) != 0 )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010744 {
10745 return( ret );
10746 }
10747 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +020010748#endif
10749
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010750 /*
10751 * Preset-specific defaults
10752 */
10753 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010754 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010755 /*
10756 * NSA Suite B
10757 */
10758 case MBEDTLS_SSL_PRESET_SUITEB:
10759 conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
10760 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */
10761 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
10762 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
10763
10764 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
10765 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
10766 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
10767 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
10768 ssl_preset_suiteb_ciphersuites;
10769
10770#if defined(MBEDTLS_X509_CRT_PARSE_C)
10771 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010772#endif
10773
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010774#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010775 conf->sig_hashes = ssl_preset_suiteb_hashes;
10776#endif
10777
10778#if defined(MBEDTLS_ECP_C)
10779 conf->curve_list = ssl_preset_suiteb_curves;
10780#endif
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +020010781 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010782
10783 /*
10784 * Default
10785 */
10786 default:
Ron Eldor5e9f14d2017-05-28 10:46:38 +030010787 conf->min_major_ver = ( MBEDTLS_SSL_MIN_MAJOR_VERSION >
10788 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION ) ?
10789 MBEDTLS_SSL_MIN_MAJOR_VERSION :
10790 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION;
10791 conf->min_minor_ver = ( MBEDTLS_SSL_MIN_MINOR_VERSION >
10792 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION ) ?
10793 MBEDTLS_SSL_MIN_MINOR_VERSION :
10794 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010795 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
10796 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
10797
10798#if defined(MBEDTLS_SSL_PROTO_DTLS)
10799 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
10800 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_2;
10801#endif
10802
10803 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
10804 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
10805 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
10806 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
10807 mbedtls_ssl_list_ciphersuites();
10808
10809#if defined(MBEDTLS_X509_CRT_PARSE_C)
10810 conf->cert_profile = &mbedtls_x509_crt_profile_default;
10811#endif
10812
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010813#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010814 conf->sig_hashes = ssl_preset_default_hashes;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010815#endif
10816
10817#if defined(MBEDTLS_ECP_C)
10818 conf->curve_list = mbedtls_ecp_grp_id_list();
10819#endif
10820
10821#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
10822 conf->dhm_min_bitlen = 1024;
10823#endif
10824 }
10825
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010826 return( 0 );
10827}
10828
10829/*
10830 * Free mbedtls_ssl_config
10831 */
10832void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
10833{
10834#if defined(MBEDTLS_DHM_C)
10835 mbedtls_mpi_free( &conf->dhm_P );
10836 mbedtls_mpi_free( &conf->dhm_G );
10837#endif
10838
10839#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
10840 if( conf->psk != NULL )
10841 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010842 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010843 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +000010844 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010845 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +090010846 }
10847
10848 if( conf->psk_identity != NULL )
10849 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010850 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +090010851 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +000010852 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010853 conf->psk_identity_len = 0;
10854 }
10855#endif
10856
10857#if defined(MBEDTLS_X509_CRT_PARSE_C)
10858 ssl_key_cert_free( conf->key_cert );
10859#endif
10860
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010861 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010862}
10863
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020010864#if defined(MBEDTLS_PK_C) && \
10865 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010866/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010867 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010868 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010869unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010870{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010871#if defined(MBEDTLS_RSA_C)
10872 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
10873 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010874#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010875#if defined(MBEDTLS_ECDSA_C)
10876 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
10877 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010878#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010879 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010880}
10881
Hanno Becker7e5437a2017-04-28 17:15:26 +010010882unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
10883{
10884 switch( type ) {
10885 case MBEDTLS_PK_RSA:
10886 return( MBEDTLS_SSL_SIG_RSA );
10887 case MBEDTLS_PK_ECDSA:
10888 case MBEDTLS_PK_ECKEY:
10889 return( MBEDTLS_SSL_SIG_ECDSA );
10890 default:
10891 return( MBEDTLS_SSL_SIG_ANON );
10892 }
10893}
10894
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010895mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010896{
10897 switch( sig )
10898 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010899#if defined(MBEDTLS_RSA_C)
10900 case MBEDTLS_SSL_SIG_RSA:
10901 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010902#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010903#if defined(MBEDTLS_ECDSA_C)
10904 case MBEDTLS_SSL_SIG_ECDSA:
10905 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010906#endif
10907 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010908 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010909 }
10910}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020010911#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010912
Hanno Becker7e5437a2017-04-28 17:15:26 +010010913#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
10914 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
10915
10916/* Find an entry in a signature-hash set matching a given hash algorithm. */
10917mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set,
10918 mbedtls_pk_type_t sig_alg )
10919{
10920 switch( sig_alg )
10921 {
10922 case MBEDTLS_PK_RSA:
10923 return( set->rsa );
10924 case MBEDTLS_PK_ECDSA:
10925 return( set->ecdsa );
10926 default:
10927 return( MBEDTLS_MD_NONE );
10928 }
10929}
10930
10931/* Add a signature-hash-pair to a signature-hash set */
10932void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set,
10933 mbedtls_pk_type_t sig_alg,
10934 mbedtls_md_type_t md_alg )
10935{
10936 switch( sig_alg )
10937 {
10938 case MBEDTLS_PK_RSA:
10939 if( set->rsa == MBEDTLS_MD_NONE )
10940 set->rsa = md_alg;
10941 break;
10942
10943 case MBEDTLS_PK_ECDSA:
10944 if( set->ecdsa == MBEDTLS_MD_NONE )
10945 set->ecdsa = md_alg;
10946 break;
10947
10948 default:
10949 break;
10950 }
10951}
10952
10953/* Allow exactly one hash algorithm for each signature. */
10954void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set,
10955 mbedtls_md_type_t md_alg )
10956{
10957 set->rsa = md_alg;
10958 set->ecdsa = md_alg;
10959}
10960
10961#endif /* MBEDTLS_SSL_PROTO_TLS1_2) &&
10962 MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
10963
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020010964/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010965 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020010966 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010967mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010968{
10969 switch( hash )
10970 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010971#if defined(MBEDTLS_MD5_C)
10972 case MBEDTLS_SSL_HASH_MD5:
10973 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010974#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010975#if defined(MBEDTLS_SHA1_C)
10976 case MBEDTLS_SSL_HASH_SHA1:
10977 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010978#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010979#if defined(MBEDTLS_SHA256_C)
10980 case MBEDTLS_SSL_HASH_SHA224:
10981 return( MBEDTLS_MD_SHA224 );
10982 case MBEDTLS_SSL_HASH_SHA256:
10983 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010984#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010985#if defined(MBEDTLS_SHA512_C)
10986 case MBEDTLS_SSL_HASH_SHA384:
10987 return( MBEDTLS_MD_SHA384 );
10988 case MBEDTLS_SSL_HASH_SHA512:
10989 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010990#endif
10991 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010992 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010993 }
10994}
10995
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010996/*
10997 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
10998 */
10999unsigned char mbedtls_ssl_hash_from_md_alg( int md )
11000{
11001 switch( md )
11002 {
11003#if defined(MBEDTLS_MD5_C)
11004 case MBEDTLS_MD_MD5:
11005 return( MBEDTLS_SSL_HASH_MD5 );
11006#endif
11007#if defined(MBEDTLS_SHA1_C)
11008 case MBEDTLS_MD_SHA1:
11009 return( MBEDTLS_SSL_HASH_SHA1 );
11010#endif
11011#if defined(MBEDTLS_SHA256_C)
11012 case MBEDTLS_MD_SHA224:
11013 return( MBEDTLS_SSL_HASH_SHA224 );
11014 case MBEDTLS_MD_SHA256:
11015 return( MBEDTLS_SSL_HASH_SHA256 );
11016#endif
11017#if defined(MBEDTLS_SHA512_C)
11018 case MBEDTLS_MD_SHA384:
11019 return( MBEDTLS_SSL_HASH_SHA384 );
11020 case MBEDTLS_MD_SHA512:
11021 return( MBEDTLS_SSL_HASH_SHA512 );
11022#endif
11023 default:
11024 return( MBEDTLS_SSL_HASH_NONE );
11025 }
11026}
11027
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020011028#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011029/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020011030 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020011031 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011032 */
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020011033int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011034{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011035 const mbedtls_ecp_group_id *gid;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011036
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020011037 if( ssl->conf->curve_list == NULL )
11038 return( -1 );
11039
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020011040 for( gid = ssl->conf->curve_list; *gid != MBEDTLS_ECP_DP_NONE; gid++ )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011041 if( *gid == grp_id )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020011042 return( 0 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011043
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020011044 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011045}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020011046#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011047
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020011048#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020011049/*
11050 * Check if a hash proposed by the peer is in our list.
11051 * Return 0 if we're willing to use it, -1 otherwise.
11052 */
11053int mbedtls_ssl_check_sig_hash( const mbedtls_ssl_context *ssl,
11054 mbedtls_md_type_t md )
11055{
11056 const int *cur;
11057
11058 if( ssl->conf->sig_hashes == NULL )
11059 return( -1 );
11060
11061 for( cur = ssl->conf->sig_hashes; *cur != MBEDTLS_MD_NONE; cur++ )
11062 if( *cur == (int) md )
11063 return( 0 );
11064
11065 return( -1 );
11066}
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020011067#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020011068
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011069#if defined(MBEDTLS_X509_CRT_PARSE_C)
11070int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
11071 const mbedtls_ssl_ciphersuite_t *ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011072 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +020011073 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011074{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011075 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011076#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011077 int usage = 0;
11078#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011079#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011080 const char *ext_oid;
11081 size_t ext_len;
11082#endif
11083
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011084#if !defined(MBEDTLS_X509_CHECK_KEY_USAGE) && \
11085 !defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011086 ((void) cert);
11087 ((void) cert_endpoint);
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011088 ((void) flags);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011089#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011090
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011091#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
11092 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011093 {
11094 /* Server part of the key exchange */
11095 switch( ciphersuite->key_exchange )
11096 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011097 case MBEDTLS_KEY_EXCHANGE_RSA:
11098 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010011099 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011100 break;
11101
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011102 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
11103 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
11104 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
11105 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011106 break;
11107
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011108 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
11109 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010011110 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011111 break;
11112
11113 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011114 case MBEDTLS_KEY_EXCHANGE_NONE:
11115 case MBEDTLS_KEY_EXCHANGE_PSK:
11116 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
11117 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +020011118 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011119 usage = 0;
11120 }
11121 }
11122 else
11123 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011124 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
11125 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011126 }
11127
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011128 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011129 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010011130 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011131 ret = -1;
11132 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011133#else
11134 ((void) ciphersuite);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011135#endif /* MBEDTLS_X509_CHECK_KEY_USAGE */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011136
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011137#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
11138 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011139 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011140 ext_oid = MBEDTLS_OID_SERVER_AUTH;
11141 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011142 }
11143 else
11144 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011145 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
11146 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011147 }
11148
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011149 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011150 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010011151 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011152 ret = -1;
11153 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011154#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011155
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011156 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011157}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011158#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +020011159
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011160/*
11161 * Convert version numbers to/from wire format
11162 * and, for DTLS, to/from TLS equivalent.
11163 *
11164 * For TLS this is the identity.
Brian J Murray1903fb32016-11-06 04:45:15 -080011165 * For DTLS, use 1's complement (v -> 255 - v, and then map as follows:
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011166 * 1.0 <-> 3.2 (DTLS 1.0 is based on TLS 1.1)
11167 * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2)
11168 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011169void mbedtls_ssl_write_version( int major, int minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011170 unsigned char ver[2] )
11171{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011172#if defined(MBEDTLS_SSL_PROTO_DTLS)
11173 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011174 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011175 if( minor == MBEDTLS_SSL_MINOR_VERSION_2 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011176 --minor; /* DTLS 1.0 stored as TLS 1.1 internally */
11177
11178 ver[0] = (unsigned char)( 255 - ( major - 2 ) );
11179 ver[1] = (unsigned char)( 255 - ( minor - 1 ) );
11180 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010011181 else
11182#else
11183 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011184#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010011185 {
11186 ver[0] = (unsigned char) major;
11187 ver[1] = (unsigned char) minor;
11188 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011189}
11190
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011191void mbedtls_ssl_read_version( int *major, int *minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011192 const unsigned char ver[2] )
11193{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011194#if defined(MBEDTLS_SSL_PROTO_DTLS)
11195 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011196 {
11197 *major = 255 - ver[0] + 2;
11198 *minor = 255 - ver[1] + 1;
11199
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011200 if( *minor == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011201 ++*minor; /* DTLS 1.0 stored as TLS 1.1 internally */
11202 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010011203 else
11204#else
11205 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011206#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010011207 {
11208 *major = ver[0];
11209 *minor = ver[1];
11210 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011211}
11212
Simon Butcher99000142016-10-13 17:21:01 +010011213int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md )
11214{
11215#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
11216 if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 )
11217 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
11218
11219 switch( md )
11220 {
11221#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
11222#if defined(MBEDTLS_MD5_C)
11223 case MBEDTLS_SSL_HASH_MD5:
Janos Follath182013f2016-10-25 10:50:22 +010011224 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
Simon Butcher99000142016-10-13 17:21:01 +010011225#endif
11226#if defined(MBEDTLS_SHA1_C)
11227 case MBEDTLS_SSL_HASH_SHA1:
11228 ssl->handshake->calc_verify = ssl_calc_verify_tls;
11229 break;
11230#endif
11231#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
11232#if defined(MBEDTLS_SHA512_C)
11233 case MBEDTLS_SSL_HASH_SHA384:
11234 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
11235 break;
11236#endif
11237#if defined(MBEDTLS_SHA256_C)
11238 case MBEDTLS_SSL_HASH_SHA256:
11239 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
11240 break;
11241#endif
11242 default:
11243 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
11244 }
11245
11246 return 0;
11247#else /* !MBEDTLS_SSL_PROTO_TLS1_2 */
11248 (void) ssl;
11249 (void) md;
11250
11251 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
11252#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
11253}
11254
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011255#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
11256 defined(MBEDTLS_SSL_PROTO_TLS1_1)
11257int mbedtls_ssl_get_key_exchange_md_ssl_tls( mbedtls_ssl_context *ssl,
11258 unsigned char *output,
11259 unsigned char *data, size_t data_len )
11260{
11261 int ret = 0;
11262 mbedtls_md5_context mbedtls_md5;
11263 mbedtls_sha1_context mbedtls_sha1;
11264
11265 mbedtls_md5_init( &mbedtls_md5 );
11266 mbedtls_sha1_init( &mbedtls_sha1 );
11267
11268 /*
11269 * digitally-signed struct {
11270 * opaque md5_hash[16];
11271 * opaque sha_hash[20];
11272 * };
11273 *
11274 * md5_hash
11275 * MD5(ClientHello.random + ServerHello.random
11276 * + ServerParams);
11277 * sha_hash
11278 * SHA(ClientHello.random + ServerHello.random
11279 * + ServerParams);
11280 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011281 if( ( ret = mbedtls_md5_starts_ret( &mbedtls_md5 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011282 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011283 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011284 goto exit;
11285 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011286 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011287 ssl->handshake->randbytes, 64 ) ) != 0 )
11288 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011289 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011290 goto exit;
11291 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011292 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5, data, data_len ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011293 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011294 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011295 goto exit;
11296 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011297 if( ( ret = mbedtls_md5_finish_ret( &mbedtls_md5, output ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011298 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011299 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011300 goto exit;
11301 }
11302
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011303 if( ( ret = mbedtls_sha1_starts_ret( &mbedtls_sha1 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011304 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011305 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011306 goto exit;
11307 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011308 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011309 ssl->handshake->randbytes, 64 ) ) != 0 )
11310 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011311 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011312 goto exit;
11313 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011314 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1, data,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011315 data_len ) ) != 0 )
11316 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011317 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011318 goto exit;
11319 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011320 if( ( ret = mbedtls_sha1_finish_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011321 output + 16 ) ) != 0 )
11322 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011323 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011324 goto exit;
11325 }
11326
11327exit:
11328 mbedtls_md5_free( &mbedtls_md5 );
11329 mbedtls_sha1_free( &mbedtls_sha1 );
11330
11331 if( ret != 0 )
11332 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
11333 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
11334
11335 return( ret );
11336
11337}
11338#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
11339 MBEDTLS_SSL_PROTO_TLS1_1 */
11340
11341#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
11342 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011343
11344#if defined(MBEDTLS_USE_PSA_CRYPTO)
11345int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
11346 unsigned char *hash, size_t *hashlen,
11347 unsigned char *data, size_t data_len,
11348 mbedtls_md_type_t md_alg )
11349{
Andrzej Kurek814feff2019-01-14 04:35:19 -050011350 psa_status_t status;
Jaeden Amero34973232019-02-20 10:32:28 +000011351 psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT;
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011352 psa_algorithm_t hash_alg = mbedtls_psa_translate_md( md_alg );
11353
Hanno Becker4c8c7aa2019-04-10 09:25:41 +010011354 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform PSA-based computation of digest of ServerKeyExchange" ) );
Andrzej Kurek814feff2019-01-14 04:35:19 -050011355
11356 if( ( status = psa_hash_setup( &hash_operation,
11357 hash_alg ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011358 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050011359 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_setup", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011360 goto exit;
11361 }
11362
Andrzej Kurek814feff2019-01-14 04:35:19 -050011363 if( ( status = psa_hash_update( &hash_operation, ssl->handshake->randbytes,
11364 64 ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011365 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050011366 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011367 goto exit;
11368 }
11369
Andrzej Kurek814feff2019-01-14 04:35:19 -050011370 if( ( status = psa_hash_update( &hash_operation,
11371 data, data_len ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011372 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050011373 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011374 goto exit;
11375 }
11376
Andrzej Kurek814feff2019-01-14 04:35:19 -050011377 if( ( status = psa_hash_finish( &hash_operation, hash, MBEDTLS_MD_MAX_SIZE,
11378 hashlen ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011379 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050011380 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_finish", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011381 goto exit;
11382 }
11383
11384exit:
Andrzej Kurek814feff2019-01-14 04:35:19 -050011385 if( status != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011386 {
11387 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
11388 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Andrzej Kurek814feff2019-01-14 04:35:19 -050011389 switch( status )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011390 {
11391 case PSA_ERROR_NOT_SUPPORTED:
11392 return( MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE );
Andrzej Kurek814feff2019-01-14 04:35:19 -050011393 case PSA_ERROR_BAD_STATE: /* Intentional fallthrough */
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011394 case PSA_ERROR_BUFFER_TOO_SMALL:
11395 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
11396 case PSA_ERROR_INSUFFICIENT_MEMORY:
11397 return( MBEDTLS_ERR_MD_ALLOC_FAILED );
11398 default:
11399 return( MBEDTLS_ERR_MD_HW_ACCEL_FAILED );
11400 }
11401 }
11402 return( 0 );
11403}
11404
11405#else
11406
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011407int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
Gilles Peskineca1d7422018-04-24 11:53:22 +020011408 unsigned char *hash, size_t *hashlen,
11409 unsigned char *data, size_t data_len,
11410 mbedtls_md_type_t md_alg )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011411{
11412 int ret = 0;
11413 mbedtls_md_context_t ctx;
11414 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
Gilles Peskineca1d7422018-04-24 11:53:22 +020011415 *hashlen = mbedtls_md_get_size( md_info );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011416
Hanno Becker4c8c7aa2019-04-10 09:25:41 +010011417 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform mbedtls-based computation of digest of ServerKeyExchange" ) );
Andrzej Kurek814feff2019-01-14 04:35:19 -050011418
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011419 mbedtls_md_init( &ctx );
11420
11421 /*
11422 * digitally-signed struct {
11423 * opaque client_random[32];
11424 * opaque server_random[32];
11425 * ServerDHParams params;
11426 * };
11427 */
11428 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
11429 {
11430 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
11431 goto exit;
11432 }
11433 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
11434 {
11435 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
11436 goto exit;
11437 }
11438 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
11439 {
11440 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
11441 goto exit;
11442 }
11443 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
11444 {
11445 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
11446 goto exit;
11447 }
Gilles Peskineca1d7422018-04-24 11:53:22 +020011448 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011449 {
11450 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
11451 goto exit;
11452 }
11453
11454exit:
11455 mbedtls_md_free( &ctx );
11456
11457 if( ret != 0 )
11458 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
11459 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
11460
11461 return( ret );
11462}
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011463#endif /* MBEDTLS_USE_PSA_CRYPTO */
11464
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011465#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
11466 MBEDTLS_SSL_PROTO_TLS1_2 */
11467
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011468#endif /* MBEDTLS_SSL_TLS_C */