blob: b6c585f4d5169541abd0112c13f0828c10a8ec70 [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"
Hanno Beckera835da52019-05-16 12:39:07 +010050#include "mbedtls/version.h"
Paul Bakker0be444a2013-08-27 21:55:01 +020051
Rich Evans00ab4702015-02-06 13:43:58 +000052#include <string.h>
53
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050054#if defined(MBEDTLS_USE_PSA_CRYPTO)
55#include "mbedtls/psa_util.h"
56#include "psa/crypto.h"
57#endif
58
Janos Follath23bdca02016-10-07 14:47:14 +010059#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000060#include "mbedtls/oid.h"
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020061#endif
62
Hanno Becker2a43f6f2018-08-10 11:12:52 +010063static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl );
Hanno Beckercd9dcda2018-08-28 17:18:56 +010064static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl );
Hanno Becker2a43f6f2018-08-10 11:12:52 +010065
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010066/* Length of the "epoch" field in the record header */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020067static inline size_t ssl_ep_len( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010068{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020069#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020070 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010071 return( 2 );
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010072#else
73 ((void) ssl);
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010074#endif
75 return( 0 );
76}
77
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020078/*
79 * Start a timer.
80 * Passing millisecs = 0 cancels a running timer.
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020081 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020082static void ssl_set_timer( mbedtls_ssl_context *ssl, uint32_t millisecs )
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020083{
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +020084 if( ssl->f_set_timer == NULL )
85 return;
86
87 MBEDTLS_SSL_DEBUG_MSG( 3, ( "set_timer to %d ms", (int) millisecs ) );
88 ssl->f_set_timer( ssl->p_timer, millisecs / 4, millisecs );
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020089}
90
91/*
92 * Return -1 is timer is expired, 0 if it isn't.
93 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020094static int ssl_check_timer( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020095{
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +020096 if( ssl->f_get_timer == NULL )
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +020097 return( 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +020098
99 if( ssl->f_get_timer( ssl->p_timer ) == 2 )
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200100 {
101 MBEDTLS_SSL_DEBUG_MSG( 3, ( "timer expired" ) );
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200102 return( -1 );
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200103 }
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200104
105 return( 0 );
106}
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200107
Hanno Becker5aa4e2c2018-08-06 09:26:08 +0100108static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
109 mbedtls_ssl_transform *transform );
Hanno Becker79594fd2019-05-08 09:38:41 +0100110static void ssl_update_in_pointers( mbedtls_ssl_context *ssl );
Hanno Becker67bc7c32018-08-06 11:33:50 +0100111
Hanno Beckercfe45792019-07-03 16:13:00 +0100112#if defined(MBEDTLS_SSL_RECORD_CHECKING)
Hanno Becker54229812019-07-12 14:40:00 +0100113static int ssl_parse_record_header( mbedtls_ssl_context const *ssl,
114 unsigned char *buf,
115 size_t len,
116 mbedtls_record *rec );
117
Hanno Beckercfe45792019-07-03 16:13:00 +0100118int mbedtls_ssl_check_record( mbedtls_ssl_context const *ssl,
119 unsigned char *buf,
120 size_t buflen )
121{
Hanno Becker54229812019-07-12 14:40:00 +0100122 int ret = 0;
123 mbedtls_record rec;
124 MBEDTLS_SSL_DEBUG_MSG( 1, ( "=> mbedtls_ssl_check_record" ) );
125 MBEDTLS_SSL_DEBUG_BUF( 3, "record buffer", buf, buflen );
126
127 /* We don't support record checking in TLS because
128 * (a) there doesn't seem to be a usecase for it, and
129 * (b) In SSLv3 and TLS 1.0, CBC record decryption has state
130 * and we'd need to backup the transform here.
131 */
132 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_STREAM )
133 {
134 ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
135 goto exit;
136 }
137#if defined(MBEDTLS_SSL_PROTO_DTLS)
138 else
139 {
140 ret = ssl_parse_record_header( ssl, buf, buflen, &rec );
141 if( ret != 0 )
142 {
143 MBEDTLS_SSL_DEBUG_RET( 3, "ssl_parse_record_header", ret );
144 goto exit;
145 }
146
147 if( ssl->transform_in != NULL )
148 {
149 ret = mbedtls_ssl_decrypt_buf( ssl, ssl->transform_in, &rec );
150 if( ret != 0 )
151 {
152 MBEDTLS_SSL_DEBUG_RET( 3, "mbedtls_ssl_decrypt_buf", ret );
153 goto exit;
154 }
155 }
156 }
157#endif /* MBEDTLS_SSL_PROTO_DTLS */
158
159exit:
160 /* On success, we have decrypted the buffer in-place, so make
161 * sure we don't leak any plaintext data. */
162 mbedtls_platform_zeroize( buf, buflen );
163
164 /* For the purpose of this API, treat messages with unexpected CID
165 * as well as such from future epochs as unexpected. */
166 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_CID ||
167 ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
168 {
169 ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
170 }
171
172 MBEDTLS_SSL_DEBUG_MSG( 1, ( "<= mbedtls_ssl_check_record" ) );
173 return( ret );
Hanno Beckercfe45792019-07-03 16:13:00 +0100174}
175#endif /* MBEDTLS_SSL_RECORD_CHECKING */
176
Hanno Becker67bc7c32018-08-06 11:33:50 +0100177#define SSL_DONT_FORCE_FLUSH 0
178#define SSL_FORCE_FLUSH 1
179
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200180#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker2b1e3542018-08-06 11:19:13 +0100181
Hanno Beckera0e20d02019-05-15 14:03:01 +0100182#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100183/* Top-level Connection ID API */
184
Hanno Becker8367ccc2019-05-14 11:30:10 +0100185int mbedtls_ssl_conf_cid( mbedtls_ssl_config *conf,
186 size_t len,
187 int ignore_other_cid )
Hanno Beckerad4a1372019-05-03 13:06:44 +0100188{
189 if( len > MBEDTLS_SSL_CID_IN_LEN_MAX )
190 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
191
Hanno Becker611ac772019-05-14 11:45:26 +0100192 if( ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_FAIL &&
193 ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_IGNORE )
194 {
195 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
196 }
197
198 conf->ignore_unexpected_cid = ignore_other_cid;
Hanno Beckerad4a1372019-05-03 13:06:44 +0100199 conf->cid_len = len;
200 return( 0 );
201}
202
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100203int mbedtls_ssl_set_cid( mbedtls_ssl_context *ssl,
204 int enable,
205 unsigned char const *own_cid,
206 size_t own_cid_len )
207{
Hanno Becker76a79ab2019-05-03 14:38:32 +0100208 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
209 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
210
Hanno Beckerca092242019-04-25 16:01:49 +0100211 ssl->negotiate_cid = enable;
212 if( enable == MBEDTLS_SSL_CID_DISABLED )
213 {
214 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Disable use of CID extension." ) );
215 return( 0 );
216 }
217 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Enable use of CID extension." ) );
Hanno Beckerad4a1372019-05-03 13:06:44 +0100218 MBEDTLS_SSL_DEBUG_BUF( 3, "Own CID", own_cid, own_cid_len );
Hanno Beckerca092242019-04-25 16:01:49 +0100219
Hanno Beckerad4a1372019-05-03 13:06:44 +0100220 if( own_cid_len != ssl->conf->cid_len )
Hanno Beckerca092242019-04-25 16:01:49 +0100221 {
Hanno Beckerad4a1372019-05-03 13:06:44 +0100222 MBEDTLS_SSL_DEBUG_MSG( 3, ( "CID length %u does not match CID length %u in config",
223 (unsigned) own_cid_len,
224 (unsigned) ssl->conf->cid_len ) );
Hanno Beckerca092242019-04-25 16:01:49 +0100225 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
226 }
227
228 memcpy( ssl->own_cid, own_cid, own_cid_len );
Hanno Beckerb7ee0cf2019-04-30 14:07:31 +0100229 /* Truncation is not an issue here because
230 * MBEDTLS_SSL_CID_IN_LEN_MAX at most 255. */
231 ssl->own_cid_len = (uint8_t) own_cid_len;
Hanno Beckerca092242019-04-25 16:01:49 +0100232
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100233 return( 0 );
234}
235
236int mbedtls_ssl_get_peer_cid( mbedtls_ssl_context *ssl,
237 int *enabled,
238 unsigned char peer_cid[ MBEDTLS_SSL_CID_OUT_LEN_MAX ],
239 size_t *peer_cid_len )
240{
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100241 *enabled = MBEDTLS_SSL_CID_DISABLED;
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100242
Hanno Becker76a79ab2019-05-03 14:38:32 +0100243 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
244 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
245 {
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100246 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Becker76a79ab2019-05-03 14:38:32 +0100247 }
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100248
Hanno Beckerc5f24222019-05-03 12:54:52 +0100249 /* We report MBEDTLS_SSL_CID_DISABLED in case the CID extensions
250 * were used, but client and server requested the empty CID.
251 * This is indistinguishable from not using the CID extension
252 * in the first place. */
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100253 if( ssl->transform_in->in_cid_len == 0 &&
254 ssl->transform_in->out_cid_len == 0 )
255 {
256 return( 0 );
257 }
258
Hanno Becker615ef172019-05-22 16:50:35 +0100259 if( peer_cid_len != NULL )
260 {
261 *peer_cid_len = ssl->transform_in->out_cid_len;
262 if( peer_cid != NULL )
263 {
264 memcpy( peer_cid, ssl->transform_in->out_cid,
265 ssl->transform_in->out_cid_len );
266 }
267 }
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100268
269 *enabled = MBEDTLS_SSL_CID_ENABLED;
270
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100271 return( 0 );
272}
Hanno Beckera0e20d02019-05-15 14:03:01 +0100273#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100274
Hanno Beckerd5847772018-08-28 10:09:23 +0100275/* Forward declarations for functions related to message buffering. */
276static void ssl_buffering_free( mbedtls_ssl_context *ssl );
277static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
278 uint8_t slot );
279static void ssl_free_buffered_record( mbedtls_ssl_context *ssl );
280static int ssl_load_buffered_message( mbedtls_ssl_context *ssl );
281static int ssl_load_buffered_record( mbedtls_ssl_context *ssl );
282static int ssl_buffer_message( mbedtls_ssl_context *ssl );
Hanno Becker519f15d2019-07-11 12:43:20 +0100283static int ssl_buffer_future_record( mbedtls_ssl_context *ssl,
284 mbedtls_record const *rec );
Hanno Beckeref7afdf2018-08-28 17:16:31 +0100285static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl );
Hanno Beckerd5847772018-08-28 10:09:23 +0100286
Hanno Beckera67dee22018-08-22 10:05:20 +0100287static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl );
Hanno Becker11682cc2018-08-22 14:41:02 +0100288static size_t ssl_get_maximum_datagram_size( mbedtls_ssl_context const *ssl )
Hanno Becker2b1e3542018-08-06 11:19:13 +0100289{
Hanno Becker11682cc2018-08-22 14:41:02 +0100290 size_t mtu = ssl_get_current_mtu( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +0100291
292 if( mtu != 0 && mtu < MBEDTLS_SSL_OUT_BUFFER_LEN )
Hanno Becker11682cc2018-08-22 14:41:02 +0100293 return( mtu );
Hanno Becker2b1e3542018-08-06 11:19:13 +0100294
295 return( MBEDTLS_SSL_OUT_BUFFER_LEN );
296}
297
Hanno Becker67bc7c32018-08-06 11:33:50 +0100298static int ssl_get_remaining_space_in_datagram( mbedtls_ssl_context const *ssl )
299{
Hanno Becker11682cc2018-08-22 14:41:02 +0100300 size_t const bytes_written = ssl->out_left;
301 size_t const mtu = ssl_get_maximum_datagram_size( ssl );
Hanno Becker67bc7c32018-08-06 11:33:50 +0100302
303 /* Double-check that the write-index hasn't gone
304 * past what we can transmit in a single datagram. */
Hanno Becker11682cc2018-08-22 14:41:02 +0100305 if( bytes_written > mtu )
Hanno Becker67bc7c32018-08-06 11:33:50 +0100306 {
307 /* Should never happen... */
308 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
309 }
310
311 return( (int) ( mtu - bytes_written ) );
312}
313
314static int ssl_get_remaining_payload_in_datagram( mbedtls_ssl_context const *ssl )
315{
316 int ret;
317 size_t remaining, expansion;
Andrzej Kurek748face2018-10-11 07:20:19 -0400318 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
Hanno Becker67bc7c32018-08-06 11:33:50 +0100319
320#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
321 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
322
323 if( max_len > mfl )
324 max_len = mfl;
Hanno Beckerf4b010e2018-08-24 10:47:29 +0100325
326 /* By the standard (RFC 6066 Sect. 4), the MFL extension
327 * only limits the maximum record payload size, so in theory
328 * we would be allowed to pack multiple records of payload size
329 * MFL into a single datagram. However, this would mean that there's
330 * no way to explicitly communicate MTU restrictions to the peer.
331 *
332 * The following reduction of max_len makes sure that we never
333 * write datagrams larger than MFL + Record Expansion Overhead.
334 */
335 if( max_len <= ssl->out_left )
336 return( 0 );
337
338 max_len -= ssl->out_left;
Hanno Becker67bc7c32018-08-06 11:33:50 +0100339#endif
340
341 ret = ssl_get_remaining_space_in_datagram( ssl );
342 if( ret < 0 )
343 return( ret );
344 remaining = (size_t) ret;
345
346 ret = mbedtls_ssl_get_record_expansion( ssl );
347 if( ret < 0 )
348 return( ret );
349 expansion = (size_t) ret;
350
351 if( remaining <= expansion )
352 return( 0 );
353
354 remaining -= expansion;
355 if( remaining >= max_len )
356 remaining = max_len;
357
358 return( (int) remaining );
359}
360
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200361/*
362 * Double the retransmit timeout value, within the allowed range,
363 * returning -1 if the maximum value has already been reached.
364 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200365static int ssl_double_retransmit_timeout( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200366{
367 uint32_t new_timeout;
368
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200369 if( ssl->handshake->retransmit_timeout >= ssl->conf->hs_timeout_max )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200370 return( -1 );
371
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200372 /* Implement the final paragraph of RFC 6347 section 4.1.1.1
373 * in the following way: after the initial transmission and a first
374 * retransmission, back off to a temporary estimated MTU of 508 bytes.
375 * This value is guaranteed to be deliverable (if not guaranteed to be
376 * delivered) of any compliant IPv4 (and IPv6) network, and should work
377 * on most non-IP stacks too. */
378 if( ssl->handshake->retransmit_timeout != ssl->conf->hs_timeout_min )
Andrzej Kurek6290dae2018-10-05 08:06:01 -0400379 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200380 ssl->handshake->mtu = 508;
Andrzej Kurek6290dae2018-10-05 08:06:01 -0400381 MBEDTLS_SSL_DEBUG_MSG( 2, ( "mtu autoreduction to %d bytes", ssl->handshake->mtu ) );
382 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200383
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200384 new_timeout = 2 * ssl->handshake->retransmit_timeout;
385
386 /* Avoid arithmetic overflow and range overflow */
387 if( new_timeout < ssl->handshake->retransmit_timeout ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200388 new_timeout > ssl->conf->hs_timeout_max )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200389 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200390 new_timeout = ssl->conf->hs_timeout_max;
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200391 }
392
393 ssl->handshake->retransmit_timeout = new_timeout;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200394 MBEDTLS_SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs",
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200395 ssl->handshake->retransmit_timeout ) );
396
397 return( 0 );
398}
399
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200400static void ssl_reset_retransmit_timeout( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200401{
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200402 ssl->handshake->retransmit_timeout = ssl->conf->hs_timeout_min;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200403 MBEDTLS_SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs",
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200404 ssl->handshake->retransmit_timeout ) );
405}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200406#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200407
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200408#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200409/*
410 * Convert max_fragment_length codes to length.
411 * RFC 6066 says:
412 * enum{
413 * 2^9(1), 2^10(2), 2^11(3), 2^12(4), (255)
414 * } MaxFragmentLength;
415 * and we add 0 -> extension unused
416 */
Angus Grattond8213d02016-05-25 20:56:48 +1000417static unsigned int ssl_mfl_code_to_length( int mfl )
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200418{
Angus Grattond8213d02016-05-25 20:56:48 +1000419 switch( mfl )
420 {
421 case MBEDTLS_SSL_MAX_FRAG_LEN_NONE:
422 return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
423 case MBEDTLS_SSL_MAX_FRAG_LEN_512:
424 return 512;
425 case MBEDTLS_SSL_MAX_FRAG_LEN_1024:
426 return 1024;
427 case MBEDTLS_SSL_MAX_FRAG_LEN_2048:
428 return 2048;
429 case MBEDTLS_SSL_MAX_FRAG_LEN_4096:
430 return 4096;
431 default:
432 return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
433 }
434}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200435#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200436
Hanno Becker52055ae2019-02-06 14:30:46 +0000437int mbedtls_ssl_session_copy( mbedtls_ssl_session *dst,
438 const mbedtls_ssl_session *src )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200439{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200440 mbedtls_ssl_session_free( dst );
441 memcpy( dst, src, sizeof( mbedtls_ssl_session ) );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200442
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200443#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker6d1986e2019-02-07 12:27:42 +0000444
445#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200446 if( src->peer_cert != NULL )
447 {
Paul Bakker2292d1f2013-09-15 17:06:49 +0200448 int ret;
449
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200450 dst->peer_cert = mbedtls_calloc( 1, sizeof(mbedtls_x509_crt) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200451 if( dst->peer_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200452 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200453
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200454 mbedtls_x509_crt_init( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200455
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200456 if( ( ret = mbedtls_x509_crt_parse_der( dst->peer_cert, src->peer_cert->raw.p,
Manuel Pégourié-Gonnard4d2a8eb2014-06-13 20:33:27 +0200457 src->peer_cert->raw.len ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200458 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200459 mbedtls_free( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200460 dst->peer_cert = NULL;
461 return( ret );
462 }
463 }
Hanno Becker6d1986e2019-02-07 12:27:42 +0000464#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker9198ad12019-02-05 17:00:50 +0000465 if( src->peer_cert_digest != NULL )
466 {
Hanno Becker9198ad12019-02-05 17:00:50 +0000467 dst->peer_cert_digest =
Hanno Beckeraccc5992019-02-25 10:06:59 +0000468 mbedtls_calloc( 1, src->peer_cert_digest_len );
Hanno Becker9198ad12019-02-05 17:00:50 +0000469 if( dst->peer_cert_digest == NULL )
470 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
471
472 memcpy( dst->peer_cert_digest, src->peer_cert_digest,
473 src->peer_cert_digest_len );
474 dst->peer_cert_digest_type = src->peer_cert_digest_type;
Hanno Beckeraccc5992019-02-25 10:06:59 +0000475 dst->peer_cert_digest_len = src->peer_cert_digest_len;
Hanno Becker9198ad12019-02-05 17:00:50 +0000476 }
477#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
478
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200479#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200480
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200481#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200482 if( src->ticket != NULL )
483 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200484 dst->ticket = mbedtls_calloc( 1, src->ticket_len );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200485 if( dst->ticket == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200486 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200487
488 memcpy( dst->ticket, src->ticket, src->ticket_len );
489 }
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200490#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200491
492 return( 0 );
493}
494
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200495#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
496int (*mbedtls_ssl_hw_record_init)( mbedtls_ssl_context *ssl,
Paul Bakker9af723c2014-05-01 13:03:14 +0200497 const unsigned char *key_enc, const unsigned char *key_dec,
498 size_t keylen,
499 const unsigned char *iv_enc, const unsigned char *iv_dec,
500 size_t ivlen,
501 const unsigned char *mac_enc, const unsigned char *mac_dec,
Paul Bakker66d5d072014-06-17 16:39:18 +0200502 size_t maclen ) = NULL;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200503int (*mbedtls_ssl_hw_record_activate)( mbedtls_ssl_context *ssl, int direction) = NULL;
504int (*mbedtls_ssl_hw_record_reset)( mbedtls_ssl_context *ssl ) = NULL;
505int (*mbedtls_ssl_hw_record_write)( mbedtls_ssl_context *ssl ) = NULL;
506int (*mbedtls_ssl_hw_record_read)( mbedtls_ssl_context *ssl ) = NULL;
507int (*mbedtls_ssl_hw_record_finish)( mbedtls_ssl_context *ssl ) = NULL;
508#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +0000509
Paul Bakker5121ce52009-01-03 21:22:43 +0000510/*
511 * Key material generation
512 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200513#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200514static int ssl3_prf( const unsigned char *secret, size_t slen,
515 const char *label,
516 const unsigned char *random, size_t rlen,
Paul Bakker5f70b252012-09-13 14:23:06 +0000517 unsigned char *dstbuf, size_t dlen )
518{
Andres Amaya Garcia33952502017-07-20 16:29:16 +0100519 int ret = 0;
Paul Bakker5f70b252012-09-13 14:23:06 +0000520 size_t i;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200521 mbedtls_md5_context md5;
522 mbedtls_sha1_context sha1;
Paul Bakker5f70b252012-09-13 14:23:06 +0000523 unsigned char padding[16];
524 unsigned char sha1sum[20];
525 ((void)label);
526
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200527 mbedtls_md5_init( &md5 );
528 mbedtls_sha1_init( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +0200529
Paul Bakker5f70b252012-09-13 14:23:06 +0000530 /*
531 * SSLv3:
532 * block =
533 * MD5( secret + SHA1( 'A' + secret + random ) ) +
534 * MD5( secret + SHA1( 'BB' + secret + random ) ) +
535 * MD5( secret + SHA1( 'CCC' + secret + random ) ) +
536 * ...
537 */
538 for( i = 0; i < dlen / 16; i++ )
539 {
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200540 memset( padding, (unsigned char) ('A' + i), 1 + i );
Paul Bakker5f70b252012-09-13 14:23:06 +0000541
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100542 if( ( ret = mbedtls_sha1_starts_ret( &sha1 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100543 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100544 if( ( ret = mbedtls_sha1_update_ret( &sha1, padding, 1 + i ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100545 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100546 if( ( ret = mbedtls_sha1_update_ret( &sha1, secret, slen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100547 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100548 if( ( ret = mbedtls_sha1_update_ret( &sha1, random, rlen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100549 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100550 if( ( ret = mbedtls_sha1_finish_ret( &sha1, sha1sum ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100551 goto exit;
Paul Bakker5f70b252012-09-13 14:23:06 +0000552
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100553 if( ( ret = mbedtls_md5_starts_ret( &md5 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100554 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100555 if( ( ret = mbedtls_md5_update_ret( &md5, secret, slen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100556 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100557 if( ( ret = mbedtls_md5_update_ret( &md5, sha1sum, 20 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100558 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100559 if( ( ret = mbedtls_md5_finish_ret( &md5, dstbuf + i * 16 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100560 goto exit;
Paul Bakker5f70b252012-09-13 14:23:06 +0000561 }
562
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100563exit:
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200564 mbedtls_md5_free( &md5 );
565 mbedtls_sha1_free( &sha1 );
Paul Bakker5f70b252012-09-13 14:23:06 +0000566
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500567 mbedtls_platform_zeroize( padding, sizeof( padding ) );
568 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5f70b252012-09-13 14:23:06 +0000569
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100570 return( ret );
Paul Bakker5f70b252012-09-13 14:23:06 +0000571}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200572#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +0000573
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200574#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200575static int tls1_prf( const unsigned char *secret, size_t slen,
576 const char *label,
577 const unsigned char *random, size_t rlen,
Paul Bakker23986e52011-04-24 08:57:21 +0000578 unsigned char *dstbuf, size_t dlen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000579{
Paul Bakker23986e52011-04-24 08:57:21 +0000580 size_t nb, hs;
581 size_t i, j, k;
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200582 const unsigned char *S1, *S2;
Ron Eldor3b350852019-05-07 18:31:49 +0300583 unsigned char *tmp;
584 size_t tmp_len = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000585 unsigned char h_i[20];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200586 const mbedtls_md_info_t *md_info;
587 mbedtls_md_context_t md_ctx;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100588 int ret;
589
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200590 mbedtls_md_init( &md_ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000591
Ron Eldor3b350852019-05-07 18:31:49 +0300592 tmp_len = 20 + strlen( label ) + rlen;
593 tmp = mbedtls_calloc( 1, tmp_len );
594 if( tmp == NULL )
595 {
596 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
597 goto exit;
598 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000599
600 hs = ( slen + 1 ) / 2;
601 S1 = secret;
602 S2 = secret + slen - hs;
603
604 nb = strlen( label );
605 memcpy( tmp + 20, label, nb );
606 memcpy( tmp + 20 + nb, random, rlen );
607 nb += rlen;
608
609 /*
610 * First compute P_md5(secret,label+random)[0..dlen]
611 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200612 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_MD5 ) ) == NULL )
Ron Eldor3b350852019-05-07 18:31:49 +0300613 {
614 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
615 goto exit;
616 }
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100617
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200618 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Ron Eldor3b350852019-05-07 18:31:49 +0300619 {
620 goto exit;
621 }
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100622
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200623 mbedtls_md_hmac_starts( &md_ctx, S1, hs );
624 mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb );
625 mbedtls_md_hmac_finish( &md_ctx, 4 + tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000626
627 for( i = 0; i < dlen; i += 16 )
628 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200629 mbedtls_md_hmac_reset ( &md_ctx );
630 mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 + nb );
631 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100632
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200633 mbedtls_md_hmac_reset ( &md_ctx );
634 mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 );
635 mbedtls_md_hmac_finish( &md_ctx, 4 + tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000636
637 k = ( i + 16 > dlen ) ? dlen % 16 : 16;
638
639 for( j = 0; j < k; j++ )
640 dstbuf[i + j] = h_i[j];
641 }
642
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200643 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100644
Paul Bakker5121ce52009-01-03 21:22:43 +0000645 /*
646 * XOR out with P_sha1(secret,label+random)[0..dlen]
647 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200648 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_SHA1 ) ) == NULL )
Ron Eldor3b350852019-05-07 18:31:49 +0300649 {
650 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
651 goto exit;
652 }
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100653
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200654 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Ron Eldor3b350852019-05-07 18:31:49 +0300655 {
656 goto exit;
657 }
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100658
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200659 mbedtls_md_hmac_starts( &md_ctx, S2, hs );
660 mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb );
661 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000662
663 for( i = 0; i < dlen; i += 20 )
664 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200665 mbedtls_md_hmac_reset ( &md_ctx );
666 mbedtls_md_hmac_update( &md_ctx, tmp, 20 + nb );
667 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100668
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200669 mbedtls_md_hmac_reset ( &md_ctx );
670 mbedtls_md_hmac_update( &md_ctx, tmp, 20 );
671 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000672
673 k = ( i + 20 > dlen ) ? dlen % 20 : 20;
674
675 for( j = 0; j < k; j++ )
676 dstbuf[i + j] = (unsigned char)( dstbuf[i + j] ^ h_i[j] );
677 }
678
Ron Eldor3b350852019-05-07 18:31:49 +0300679exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200680 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100681
Ron Eldor3b350852019-05-07 18:31:49 +0300682 mbedtls_platform_zeroize( tmp, tmp_len );
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500683 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000684
Ron Eldor3b350852019-05-07 18:31:49 +0300685 mbedtls_free( tmp );
686 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000687}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200688#endif /* MBEDTLS_SSL_PROTO_TLS1) || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000689
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200690#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurekc929a822019-01-14 03:51:11 -0500691#if defined(MBEDTLS_USE_PSA_CRYPTO)
692static int tls_prf_generic( mbedtls_md_type_t md_type,
693 const unsigned char *secret, size_t slen,
694 const char *label,
695 const unsigned char *random, size_t rlen,
696 unsigned char *dstbuf, size_t dlen )
697{
698 psa_status_t status;
699 psa_algorithm_t alg;
Janos Follath53b8ec22019-08-08 10:28:27 +0100700 psa_key_attributes_t key_attributes;
Andrzej Kurekac5dc342019-01-23 06:57:34 -0500701 psa_key_handle_t master_slot;
Janos Follathda6ac012019-08-16 13:47:29 +0100702 psa_key_derivation_operation_t derivation =
Janos Follath8dee8772019-07-30 12:53:32 +0100703 PSA_KEY_DERIVATION_OPERATION_INIT;
Andrzej Kurekc929a822019-01-14 03:51:11 -0500704
Andrzej Kurekc929a822019-01-14 03:51:11 -0500705 if( md_type == MBEDTLS_MD_SHA384 )
706 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_384);
707 else
708 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_256);
709
Janos Follath53b8ec22019-08-08 10:28:27 +0100710 key_attributes = psa_key_attributes_init();
711 psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_DERIVE );
712 psa_set_key_algorithm( &key_attributes, alg );
713 psa_set_key_type( &key_attributes, PSA_KEY_TYPE_DERIVE );
Andrzej Kurekc929a822019-01-14 03:51:11 -0500714
Janos Follath53b8ec22019-08-08 10:28:27 +0100715 status = psa_import_key( &key_attributes, secret, slen, &master_slot );
Andrzej Kurekc929a822019-01-14 03:51:11 -0500716 if( status != PSA_SUCCESS )
717 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
718
Janos Follathda6ac012019-08-16 13:47:29 +0100719 status = psa_key_derivation( &derivation,
Andrzej Kurekc929a822019-01-14 03:51:11 -0500720 master_slot, alg,
721 random, rlen,
722 (unsigned char const *) label,
723 (size_t) strlen( label ),
724 dlen );
725 if( status != PSA_SUCCESS )
726 {
Janos Follathda6ac012019-08-16 13:47:29 +0100727 psa_key_derivation_abort( &derivation );
Andrzej Kurekc929a822019-01-14 03:51:11 -0500728 psa_destroy_key( master_slot );
729 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
730 }
731
Janos Follathda6ac012019-08-16 13:47:29 +0100732 status = psa_key_derivation_output_bytes( &derivation, dstbuf, dlen );
Andrzej Kurekc929a822019-01-14 03:51:11 -0500733 if( status != PSA_SUCCESS )
734 {
Janos Follathda6ac012019-08-16 13:47:29 +0100735 psa_key_derivation_abort( &derivation );
Andrzej Kurekc929a822019-01-14 03:51:11 -0500736 psa_destroy_key( master_slot );
737 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
738 }
739
Janos Follathda6ac012019-08-16 13:47:29 +0100740 status = psa_key_derivation_abort( &derivation );
Andrzej Kurekc929a822019-01-14 03:51:11 -0500741 if( status != PSA_SUCCESS )
Andrzej Kurek70737ca2019-01-14 05:37:13 -0500742 {
743 psa_destroy_key( master_slot );
Andrzej Kurekc929a822019-01-14 03:51:11 -0500744 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Andrzej Kurek70737ca2019-01-14 05:37:13 -0500745 }
Andrzej Kurekc929a822019-01-14 03:51:11 -0500746
747 status = psa_destroy_key( master_slot );
748 if( status != PSA_SUCCESS )
749 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
750
Andrzej Kurek33171262019-01-15 03:25:18 -0500751 return( 0 );
Andrzej Kurekc929a822019-01-14 03:51:11 -0500752}
753
754#else /* MBEDTLS_USE_PSA_CRYPTO */
755
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200756static int tls_prf_generic( mbedtls_md_type_t md_type,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100757 const unsigned char *secret, size_t slen,
758 const char *label,
759 const unsigned char *random, size_t rlen,
760 unsigned char *dstbuf, size_t dlen )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000761{
762 size_t nb;
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100763 size_t i, j, k, md_len;
Ron Eldor3b350852019-05-07 18:31:49 +0300764 unsigned char *tmp;
765 size_t tmp_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200766 unsigned char h_i[MBEDTLS_MD_MAX_SIZE];
767 const mbedtls_md_info_t *md_info;
768 mbedtls_md_context_t md_ctx;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100769 int ret;
770
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200771 mbedtls_md_init( &md_ctx );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000772
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200773 if( ( md_info = mbedtls_md_info_from_type( md_type ) ) == NULL )
774 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100775
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200776 md_len = mbedtls_md_get_size( md_info );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100777
Ron Eldor3b350852019-05-07 18:31:49 +0300778 tmp_len = md_len + strlen( label ) + rlen;
779 tmp = mbedtls_calloc( 1, tmp_len );
780 if( tmp == NULL )
781 {
782 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
783 goto exit;
784 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000785
786 nb = strlen( label );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100787 memcpy( tmp + md_len, label, nb );
788 memcpy( tmp + md_len + nb, random, rlen );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000789 nb += rlen;
790
791 /*
792 * Compute P_<hash>(secret, label + random)[0..dlen]
793 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200794 if ( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Ron Eldor3b350852019-05-07 18:31:49 +0300795 goto exit;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100796
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200797 mbedtls_md_hmac_starts( &md_ctx, secret, slen );
798 mbedtls_md_hmac_update( &md_ctx, tmp + md_len, nb );
799 mbedtls_md_hmac_finish( &md_ctx, tmp );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100800
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100801 for( i = 0; i < dlen; i += md_len )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000802 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200803 mbedtls_md_hmac_reset ( &md_ctx );
804 mbedtls_md_hmac_update( &md_ctx, tmp, md_len + nb );
805 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100806
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200807 mbedtls_md_hmac_reset ( &md_ctx );
808 mbedtls_md_hmac_update( &md_ctx, tmp, md_len );
809 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000810
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100811 k = ( i + md_len > dlen ) ? dlen % md_len : md_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +0000812
813 for( j = 0; j < k; j++ )
814 dstbuf[i + j] = h_i[j];
815 }
816
Ron Eldor3b350852019-05-07 18:31:49 +0300817exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200818 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100819
Ron Eldor3b350852019-05-07 18:31:49 +0300820 mbedtls_platform_zeroize( tmp, tmp_len );
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500821 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000822
Ron Eldor3b350852019-05-07 18:31:49 +0300823 mbedtls_free( tmp );
824
825 return( ret );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000826}
Andrzej Kurekc929a822019-01-14 03:51:11 -0500827#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200828#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100829static int tls_prf_sha256( const unsigned char *secret, size_t slen,
830 const char *label,
831 const unsigned char *random, size_t rlen,
832 unsigned char *dstbuf, size_t dlen )
833{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200834 return( tls_prf_generic( MBEDTLS_MD_SHA256, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100835 label, random, rlen, dstbuf, dlen ) );
836}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200837#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000838
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200839#if defined(MBEDTLS_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200840static int tls_prf_sha384( const unsigned char *secret, size_t slen,
841 const char *label,
842 const unsigned char *random, size_t rlen,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000843 unsigned char *dstbuf, size_t dlen )
844{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200845 return( tls_prf_generic( MBEDTLS_MD_SHA384, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100846 label, random, rlen, dstbuf, dlen ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000847}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200848#endif /* MBEDTLS_SHA512_C */
849#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +0000850
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200851static void ssl_update_checksum_start( mbedtls_ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200852
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200853#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
854 defined(MBEDTLS_SSL_PROTO_TLS1_1)
855static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200856#endif
Paul Bakker380da532012-04-18 16:10:25 +0000857
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200858#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +0200859static void ssl_calc_verify_ssl( const mbedtls_ssl_context *, unsigned char *, size_t * );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200860static void ssl_calc_finished_ssl( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200861#endif
862
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200863#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +0200864static void ssl_calc_verify_tls( const mbedtls_ssl_context *, unsigned char *, size_t * );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200865static void ssl_calc_finished_tls( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200866#endif
867
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200868#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
869#if defined(MBEDTLS_SHA256_C)
870static void ssl_update_checksum_sha256( mbedtls_ssl_context *, const unsigned char *, size_t );
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +0200871static void ssl_calc_verify_tls_sha256( const mbedtls_ssl_context *,unsigned char *, size_t * );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200872static void ssl_calc_finished_tls_sha256( mbedtls_ssl_context *,unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200873#endif
Paul Bakker769075d2012-11-24 11:26:46 +0100874
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200875#if defined(MBEDTLS_SHA512_C)
876static void ssl_update_checksum_sha384( mbedtls_ssl_context *, const unsigned char *, size_t );
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +0200877static void ssl_calc_verify_tls_sha384( const mbedtls_ssl_context *, unsigned char *, size_t * );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200878static void ssl_calc_finished_tls_sha384( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakker769075d2012-11-24 11:26:46 +0100879#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200880#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000881
Manuel Pégourié-Gonnard45be3d82019-02-18 23:35:14 +0100882#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED) && \
Hanno Becker7d0a5692018-10-23 15:26:22 +0100883 defined(MBEDTLS_USE_PSA_CRYPTO)
884static int ssl_use_opaque_psk( mbedtls_ssl_context const *ssl )
885{
886 if( ssl->conf->f_psk != NULL )
887 {
888 /* If we've used a callback to select the PSK,
889 * the static configuration is irrelevant. */
890 if( ssl->handshake->psk_opaque != 0 )
891 return( 1 );
892
893 return( 0 );
894 }
895
896 if( ssl->conf->psk_opaque != 0 )
897 return( 1 );
898
899 return( 0 );
900}
901#endif /* MBEDTLS_USE_PSA_CRYPTO &&
Manuel Pégourié-Gonnard45be3d82019-02-18 23:35:14 +0100902 MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
Hanno Becker7d0a5692018-10-23 15:26:22 +0100903
Ron Eldorcf280092019-05-14 20:19:13 +0300904#if defined(MBEDTLS_SSL_EXPORT_KEYS)
905static mbedtls_tls_prf_types tls_prf_get_type( mbedtls_ssl_tls_prf_cb *tls_prf )
906{
907#if defined(MBEDTLS_SSL_PROTO_SSL3)
908 if( tls_prf == ssl3_prf )
909 {
Ron Eldor0810f0b2019-05-15 12:32:32 +0300910 return( MBEDTLS_SSL_TLS_PRF_SSL3 );
Ron Eldorcf280092019-05-14 20:19:13 +0300911 }
912 else
913#endif
914#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
915 if( tls_prf == tls1_prf )
916 {
917 return( MBEDTLS_SSL_TLS_PRF_TLS1 );
918 }
919 else
920#endif
921#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
922#if defined(MBEDTLS_SHA512_C)
923 if( tls_prf == tls_prf_sha384 )
924 {
925 return( MBEDTLS_SSL_TLS_PRF_SHA384 );
926 }
927 else
928#endif
929#if defined(MBEDTLS_SHA256_C)
930 if( tls_prf == tls_prf_sha256 )
931 {
932 return( MBEDTLS_SSL_TLS_PRF_SHA256 );
933 }
934 else
935#endif
936#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
937 return( MBEDTLS_SSL_TLS_PRF_NONE );
938}
939#endif /* MBEDTLS_SSL_EXPORT_KEYS */
940
Ron Eldor51d3ab52019-05-12 14:54:30 +0300941int mbedtls_ssl_tls_prf( const mbedtls_tls_prf_types prf,
942 const unsigned char *secret, size_t slen,
943 const char *label,
944 const unsigned char *random, size_t rlen,
945 unsigned char *dstbuf, size_t dlen )
946{
947 mbedtls_ssl_tls_prf_cb *tls_prf = NULL;
948
949 switch( prf )
950 {
951#if defined(MBEDTLS_SSL_PROTO_SSL3)
952 case MBEDTLS_SSL_TLS_PRF_SSL3:
953 tls_prf = ssl3_prf;
954 break;
Ron Eldord2f25f72019-05-15 14:54:22 +0300955#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Ron Eldor51d3ab52019-05-12 14:54:30 +0300956#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
957 case MBEDTLS_SSL_TLS_PRF_TLS1:
958 tls_prf = tls1_prf;
959 break;
Ron Eldord2f25f72019-05-15 14:54:22 +0300960#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
961
962#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Ron Eldor51d3ab52019-05-12 14:54:30 +0300963#if defined(MBEDTLS_SHA512_C)
964 case MBEDTLS_SSL_TLS_PRF_SHA384:
965 tls_prf = tls_prf_sha384;
966 break;
Ron Eldord2f25f72019-05-15 14:54:22 +0300967#endif /* MBEDTLS_SHA512_C */
Ron Eldor51d3ab52019-05-12 14:54:30 +0300968#if defined(MBEDTLS_SHA256_C)
969 case MBEDTLS_SSL_TLS_PRF_SHA256:
970 tls_prf = tls_prf_sha256;
971 break;
Ron Eldord2f25f72019-05-15 14:54:22 +0300972#endif /* MBEDTLS_SHA256_C */
973#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Ron Eldor51d3ab52019-05-12 14:54:30 +0300974 default:
975 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
976 }
977
978 return( tls_prf( secret, slen, label, random, rlen, dstbuf, dlen ) );
979}
980
Manuel Pégourié-Gonnard9b108c22019-05-06 13:32:17 +0200981/* Type for the TLS PRF */
982typedef int ssl_tls_prf_t(const unsigned char *, size_t, const char *,
983 const unsigned char *, size_t,
984 unsigned char *, size_t);
985
Manuel Pégourié-Gonnarde59ae232019-04-30 11:41:40 +0200986/*
Manuel Pégourié-Gonnardcba40d92019-05-06 12:55:40 +0200987 * Populate a transform structure with session keys and all the other
988 * necessary information.
Manuel Pégourié-Gonnarde59ae232019-04-30 11:41:40 +0200989 *
Manuel Pégourié-Gonnardcba40d92019-05-06 12:55:40 +0200990 * Parameters:
991 * - [in/out]: transform: structure to populate
992 * [in] must be just initialised with mbedtls_ssl_transform_init()
Manuel Pégourié-Gonnardc864f6a2019-05-06 13:48:22 +0200993 * [out] fully populated, ready for use by mbedtls_ssl_{en,de}crypt_buf()
Manuel Pégourié-Gonnard6fa57bf2019-05-10 10:50:04 +0200994 * - [in] ciphersuite
995 * - [in] master
996 * - [in] encrypt_then_mac
997 * - [in] trunc_hmac
998 * - [in] compression
Manuel Pégourié-Gonnard9b108c22019-05-06 13:32:17 +0200999 * - [in] tls_prf: pointer to PRF to use for key derivation
1000 * - [in] randbytes: buffer holding ServerHello.random + ClientHello.random
Manuel Pégourié-Gonnardc864f6a2019-05-06 13:48:22 +02001001 * - [in] minor_ver: SSL/TLS minor version
1002 * - [in] endpoint: client or server
1003 * - [in] ssl: optionally used for:
1004 * - MBEDTLS_SSL_HW_RECORD_ACCEL: whole context
1005 * - MBEDTLS_SSL_EXPORT_KEYS: ssl->conf->{f,p}_export_keys
1006 * - MBEDTLS_DEBUG_C: ssl->conf->{f,p}_dbg
Manuel Pégourié-Gonnarde59ae232019-04-30 11:41:40 +02001007 */
Manuel Pégourié-Gonnardcba40d92019-05-06 12:55:40 +02001008static int ssl_populate_transform( mbedtls_ssl_transform *transform,
Manuel Pégourié-Gonnard6fa57bf2019-05-10 10:50:04 +02001009 int ciphersuite,
1010 const unsigned char master[48],
1011#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1012 int encrypt_then_mac,
1013#endif
1014#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
1015 int trunc_hmac,
1016#endif
1017#if defined(MBEDTLS_ZLIB_SUPPORT)
1018 int compression,
1019#endif
Manuel Pégourié-Gonnard9b108c22019-05-06 13:32:17 +02001020 ssl_tls_prf_t tls_prf,
1021 const unsigned char randbytes[64],
Manuel Pégourié-Gonnardc864f6a2019-05-06 13:48:22 +02001022 int minor_ver,
1023 unsigned endpoint,
Manuel Pégourié-Gonnardcba40d92019-05-06 12:55:40 +02001024 const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00001025{
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001026 int ret = 0;
Hanno Beckercb1cc802018-11-17 22:27:38 +00001027#if defined(MBEDTLS_USE_PSA_CRYPTO)
1028 int psa_fallthrough;
1029#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker5121ce52009-01-03 21:22:43 +00001030 unsigned char keyblk[256];
1031 unsigned char *key1;
1032 unsigned char *key2;
Paul Bakker68884e32013-01-07 18:20:04 +01001033 unsigned char *mac_enc;
1034 unsigned char *mac_dec;
Hanno Becker81c7b182017-11-09 18:39:33 +00001035 size_t mac_key_len;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001036 size_t iv_copy_len;
Hanno Becker88aaf652017-12-27 08:17:40 +00001037 unsigned keylen;
Hanno Beckere694c3e2017-12-27 21:34:08 +00001038 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001039 const mbedtls_cipher_info_t *cipher_info;
1040 const mbedtls_md_info_t *md_info;
Paul Bakker68884e32013-01-07 18:20:04 +01001041
Manuel Pégourié-Gonnardc864f6a2019-05-06 13:48:22 +02001042#if !defined(MBEDTLS_SSL_HW_RECORD_ACCEL) && \
1043 !defined(MBEDTLS_SSL_EXPORT_KEYS) && \
1044 !defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnarda7505d12019-05-07 10:17:56 +02001045 ssl = NULL; /* make sure we don't use it except for those cases */
Manuel Pégourié-Gonnardc864f6a2019-05-06 13:48:22 +02001046 (void) ssl;
1047#endif
1048
Manuel Pégourié-Gonnard9b108c22019-05-06 13:32:17 +02001049 /* Copy info about negotiated version and extensions */
Jaeden Amero2de07f12019-06-05 13:32:08 +01001050#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) && \
1051 defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard6fa57bf2019-05-10 10:50:04 +02001052 transform->encrypt_then_mac = encrypt_then_mac;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001053#endif
Manuel Pégourié-Gonnardc864f6a2019-05-06 13:48:22 +02001054 transform->minor_ver = minor_ver;
Hanno Beckere694c3e2017-12-27 21:34:08 +00001055
Manuel Pégourié-Gonnard9b108c22019-05-06 13:32:17 +02001056 /*
1057 * Get various info structures
1058 */
Manuel Pégourié-Gonnard6fa57bf2019-05-10 10:50:04 +02001059 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( ciphersuite );
Manuel Pégourié-Gonnard9b108c22019-05-06 13:32:17 +02001060 if( ciphersuite_info == NULL )
1061 {
1062 MBEDTLS_SSL_DEBUG_MSG( 1, ( "ciphersuite info for %d not found",
Manuel Pégourié-Gonnard6fa57bf2019-05-10 10:50:04 +02001063 ciphersuite ) );
Manuel Pégourié-Gonnard9b108c22019-05-06 13:32:17 +02001064 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1065 }
1066
Hanno Beckere694c3e2017-12-27 21:34:08 +00001067 cipher_info = mbedtls_cipher_info_from_type( ciphersuite_info->cipher );
Paul Bakker68884e32013-01-07 18:20:04 +01001068 if( cipher_info == NULL )
1069 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001070 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %d not found",
Hanno Beckere694c3e2017-12-27 21:34:08 +00001071 ciphersuite_info->cipher ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001072 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +01001073 }
1074
Hanno Beckere694c3e2017-12-27 21:34:08 +00001075 md_info = mbedtls_md_info_from_type( ciphersuite_info->mac );
Paul Bakker68884e32013-01-07 18:20:04 +01001076 if( md_info == NULL )
1077 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001078 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %d not found",
Hanno Beckere694c3e2017-12-27 21:34:08 +00001079 ciphersuite_info->mac ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001080 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +01001081 }
1082
Hanno Beckera0e20d02019-05-15 14:03:01 +01001083#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker4bf74652019-04-26 16:22:27 +01001084 /* Copy own and peer's CID if the use of the CID
1085 * extension has been negotiated. */
1086 if( ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_ENABLED )
1087 {
1088 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Copy CIDs into SSL transform" ) );
Hanno Becker8a7f9722019-04-30 13:52:29 +01001089
Hanno Becker05154c32019-05-03 15:23:51 +01001090 transform->in_cid_len = ssl->own_cid_len;
Hanno Becker05154c32019-05-03 15:23:51 +01001091 memcpy( transform->in_cid, ssl->own_cid, ssl->own_cid_len );
Hanno Becker1c1f0462019-05-03 12:55:51 +01001092 MBEDTLS_SSL_DEBUG_BUF( 3, "Incoming CID", transform->in_cid,
Hanno Becker4bf74652019-04-26 16:22:27 +01001093 transform->in_cid_len );
Hanno Beckerd1f20352019-05-15 10:21:55 +01001094
1095 transform->out_cid_len = ssl->handshake->peer_cid_len;
1096 memcpy( transform->out_cid, ssl->handshake->peer_cid,
1097 ssl->handshake->peer_cid_len );
1098 MBEDTLS_SSL_DEBUG_BUF( 3, "Outgoing CID", transform->out_cid,
1099 transform->out_cid_len );
Hanno Becker4bf74652019-04-26 16:22:27 +01001100 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01001101#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker4bf74652019-04-26 16:22:27 +01001102
Paul Bakker5121ce52009-01-03 21:22:43 +00001103 /*
Manuel Pégourié-Gonnard9b108c22019-05-06 13:32:17 +02001104 * Compute key block using the PRF
Paul Bakker5121ce52009-01-03 21:22:43 +00001105 */
Manuel Pégourié-Gonnard6fa57bf2019-05-10 10:50:04 +02001106 ret = tls_prf( master, 48, "key expansion", randbytes, 64, keyblk, 256 );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +01001107 if( ret != 0 )
1108 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001109 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +01001110 return( ret );
1111 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001112
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001113 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
Manuel Pégourié-Gonnardd91efa42019-05-20 10:27:20 +02001114 mbedtls_ssl_get_ciphersuite_name( ciphersuite ) ) );
Manuel Pégourié-Gonnard6fa57bf2019-05-10 10:50:04 +02001115 MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", master, 48 );
Manuel Pégourié-Gonnard9b108c22019-05-06 13:32:17 +02001116 MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", randbytes, 64 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001117 MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001118
Paul Bakker5121ce52009-01-03 21:22:43 +00001119 /*
1120 * Determine the appropriate key, IV and MAC length.
1121 */
Paul Bakker68884e32013-01-07 18:20:04 +01001122
Hanno Becker88aaf652017-12-27 08:17:40 +00001123 keylen = cipher_info->key_bitlen / 8;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001124
Hanno Becker8031d062018-01-03 15:32:31 +00001125#if defined(MBEDTLS_GCM_C) || \
1126 defined(MBEDTLS_CCM_C) || \
1127 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001128 if( cipher_info->mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001129 cipher_info->mode == MBEDTLS_MODE_CCM ||
1130 cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakker5121ce52009-01-03 21:22:43 +00001131 {
Hanno Beckerf704bef2018-11-16 15:21:18 +00001132 size_t explicit_ivlen;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001133
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001134 transform->maclen = 0;
Hanno Becker81c7b182017-11-09 18:39:33 +00001135 mac_key_len = 0;
Hanno Beckere694c3e2017-12-27 21:34:08 +00001136 transform->taglen =
1137 ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001138
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001139 /* All modes haves 96-bit IVs;
1140 * GCM and CCM has 4 implicit and 8 explicit bytes
1141 * ChachaPoly has all 12 bytes implicit
1142 */
Paul Bakker68884e32013-01-07 18:20:04 +01001143 transform->ivlen = 12;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001144 if( cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
1145 transform->fixed_ivlen = 12;
1146 else
1147 transform->fixed_ivlen = 4;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001148
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001149 /* Minimum length of encrypted record */
1150 explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
Hanno Beckere694c3e2017-12-27 21:34:08 +00001151 transform->minlen = explicit_ivlen + transform->taglen;
Paul Bakker68884e32013-01-07 18:20:04 +01001152 }
1153 else
Hanno Becker8031d062018-01-03 15:32:31 +00001154#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
1155#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
1156 if( cipher_info->mode == MBEDTLS_MODE_STREAM ||
1157 cipher_info->mode == MBEDTLS_MODE_CBC )
Paul Bakker68884e32013-01-07 18:20:04 +01001158 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001159 /* Initialize HMAC contexts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001160 if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 ||
1161 ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 )
Paul Bakker68884e32013-01-07 18:20:04 +01001162 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001163 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001164 goto end;
Paul Bakker68884e32013-01-07 18:20:04 +01001165 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001166
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001167 /* Get MAC length */
Hanno Becker81c7b182017-11-09 18:39:33 +00001168 mac_key_len = mbedtls_md_get_size( md_info );
1169 transform->maclen = mac_key_len;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001170
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001171#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001172 /*
1173 * If HMAC is to be truncated, we shall keep the leftmost bytes,
1174 * (rfc 6066 page 13 or rfc 2104 section 4),
1175 * so we only need to adjust the length here.
1176 */
Manuel Pégourié-Gonnard6fa57bf2019-05-10 10:50:04 +02001177 if( trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_ENABLED )
Hanno Beckere89353a2017-11-20 16:36:41 +00001178 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001179 transform->maclen = MBEDTLS_SSL_TRUNCATED_HMAC_LEN;
Hanno Beckere89353a2017-11-20 16:36:41 +00001180
1181#if defined(MBEDTLS_SSL_TRUNCATED_HMAC_COMPAT)
1182 /* Fall back to old, non-compliant version of the truncated
Hanno Becker563423f2017-11-21 17:20:17 +00001183 * HMAC implementation which also truncates the key
1184 * (Mbed TLS versions from 1.3 to 2.6.0) */
Hanno Beckere89353a2017-11-20 16:36:41 +00001185 mac_key_len = transform->maclen;
1186#endif
1187 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001188#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001189
1190 /* IV length */
Paul Bakker68884e32013-01-07 18:20:04 +01001191 transform->ivlen = cipher_info->iv_size;
Paul Bakker5121ce52009-01-03 21:22:43 +00001192
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001193 /* Minimum length */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001194 if( cipher_info->mode == MBEDTLS_MODE_STREAM )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001195 transform->minlen = transform->maclen;
1196 else
Paul Bakker68884e32013-01-07 18:20:04 +01001197 {
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001198 /*
1199 * GenericBlockCipher:
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +01001200 * 1. if EtM is in use: one block plus MAC
1201 * otherwise: * first multiple of blocklen greater than maclen
1202 * 2. IV except for SSL3 and TLS 1.0
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001203 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001204#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6fa57bf2019-05-10 10:50:04 +02001205 if( encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +01001206 {
1207 transform->minlen = transform->maclen
1208 + cipher_info->block_size;
1209 }
1210 else
1211#endif
1212 {
1213 transform->minlen = transform->maclen
1214 + cipher_info->block_size
1215 - transform->maclen % cipher_info->block_size;
1216 }
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001217
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001218#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Manuel Pégourié-Gonnardc864f6a2019-05-06 13:48:22 +02001219 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ||
1220 minor_ver == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001221 ; /* No need to adjust minlen */
Paul Bakker68884e32013-01-07 18:20:04 +01001222 else
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001223#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001224#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnardc864f6a2019-05-06 13:48:22 +02001225 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_2 ||
1226 minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001227 {
1228 transform->minlen += transform->ivlen;
1229 }
1230 else
1231#endif
1232 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001233 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001234 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1235 goto end;
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001236 }
Paul Bakker68884e32013-01-07 18:20:04 +01001237 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001238 }
Hanno Becker8031d062018-01-03 15:32:31 +00001239 else
1240#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
1241 {
1242 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1243 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1244 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001245
Hanno Becker88aaf652017-12-27 08:17:40 +00001246 MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %u, minlen: %u, ivlen: %u, maclen: %u",
1247 (unsigned) keylen,
1248 (unsigned) transform->minlen,
1249 (unsigned) transform->ivlen,
1250 (unsigned) transform->maclen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001251
1252 /*
1253 * Finally setup the cipher contexts, IVs and MAC secrets.
1254 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001255#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnardc864f6a2019-05-06 13:48:22 +02001256 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00001257 {
Hanno Becker81c7b182017-11-09 18:39:33 +00001258 key1 = keyblk + mac_key_len * 2;
Hanno Becker88aaf652017-12-27 08:17:40 +00001259 key2 = keyblk + mac_key_len * 2 + keylen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001260
Paul Bakker68884e32013-01-07 18:20:04 +01001261 mac_enc = keyblk;
Hanno Becker81c7b182017-11-09 18:39:33 +00001262 mac_dec = keyblk + mac_key_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001263
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001264 /*
1265 * This is not used in TLS v1.1.
1266 */
Paul Bakker48916f92012-09-16 19:57:18 +00001267 iv_copy_len = ( transform->fixed_ivlen ) ?
1268 transform->fixed_ivlen : transform->ivlen;
Hanno Becker88aaf652017-12-27 08:17:40 +00001269 memcpy( transform->iv_enc, key2 + keylen, iv_copy_len );
1270 memcpy( transform->iv_dec, key2 + keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +00001271 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001272 }
1273 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001274#endif /* MBEDTLS_SSL_CLI_C */
1275#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardc864f6a2019-05-06 13:48:22 +02001276 if( endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00001277 {
Hanno Becker88aaf652017-12-27 08:17:40 +00001278 key1 = keyblk + mac_key_len * 2 + keylen;
Hanno Becker81c7b182017-11-09 18:39:33 +00001279 key2 = keyblk + mac_key_len * 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00001280
Hanno Becker81c7b182017-11-09 18:39:33 +00001281 mac_enc = keyblk + mac_key_len;
Paul Bakker68884e32013-01-07 18:20:04 +01001282 mac_dec = keyblk;
Paul Bakker5121ce52009-01-03 21:22:43 +00001283
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001284 /*
1285 * This is not used in TLS v1.1.
1286 */
Paul Bakker48916f92012-09-16 19:57:18 +00001287 iv_copy_len = ( transform->fixed_ivlen ) ?
1288 transform->fixed_ivlen : transform->ivlen;
Hanno Becker88aaf652017-12-27 08:17:40 +00001289 memcpy( transform->iv_dec, key1 + keylen, iv_copy_len );
1290 memcpy( transform->iv_enc, key1 + keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +00001291 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001292 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001293 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001294#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001295 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001296 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001297 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1298 goto end;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001299 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001300
Hanno Beckerd56ed242018-01-03 15:32:51 +00001301#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001302#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardc864f6a2019-05-06 13:48:22 +02001303 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker68884e32013-01-07 18:20:04 +01001304 {
Hanno Beckerd56ed242018-01-03 15:32:51 +00001305 if( mac_key_len > sizeof( transform->mac_enc ) )
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001306 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001307 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001308 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1309 goto end;
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001310 }
1311
Hanno Becker81c7b182017-11-09 18:39:33 +00001312 memcpy( transform->mac_enc, mac_enc, mac_key_len );
1313 memcpy( transform->mac_dec, mac_dec, mac_key_len );
Paul Bakker68884e32013-01-07 18:20:04 +01001314 }
1315 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001316#endif /* MBEDTLS_SSL_PROTO_SSL3 */
1317#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1318 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnardc864f6a2019-05-06 13:48:22 +02001319 if( minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakker68884e32013-01-07 18:20:04 +01001320 {
Gilles Peskine039fd122018-03-19 19:06:08 +01001321 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
1322 For AEAD-based ciphersuites, there is nothing to do here. */
1323 if( mac_key_len != 0 )
1324 {
1325 mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, mac_key_len );
1326 mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, mac_key_len );
1327 }
Paul Bakker68884e32013-01-07 18:20:04 +01001328 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001329 else
1330#endif
Paul Bakker577e0062013-08-28 11:57:20 +02001331 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001332 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001333 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1334 goto end;
Paul Bakker577e0062013-08-28 11:57:20 +02001335 }
Hanno Beckerd56ed242018-01-03 15:32:51 +00001336#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker68884e32013-01-07 18:20:04 +01001337
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001338#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
1339 if( mbedtls_ssl_hw_record_init != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00001340 {
1341 int ret = 0;
1342
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001343 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_init()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00001344
Hanno Becker88aaf652017-12-27 08:17:40 +00001345 if( ( ret = mbedtls_ssl_hw_record_init( ssl, key1, key2, keylen,
Paul Bakker07eb38b2012-12-19 14:42:06 +01001346 transform->iv_enc, transform->iv_dec,
1347 iv_copy_len,
Paul Bakker68884e32013-01-07 18:20:04 +01001348 mac_enc, mac_dec,
Hanno Becker81c7b182017-11-09 18:39:33 +00001349 mac_key_len ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00001350 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001351 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_init", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001352 ret = MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
1353 goto end;
Paul Bakker05ef8352012-05-08 09:17:57 +00001354 }
1355 }
Hanno Beckerd56ed242018-01-03 15:32:51 +00001356#else
1357 ((void) mac_dec);
1358 ((void) mac_enc);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001359#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00001360
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001361#if defined(MBEDTLS_SSL_EXPORT_KEYS)
1362 if( ssl->conf->f_export_keys != NULL )
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001363 {
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001364 ssl->conf->f_export_keys( ssl->conf->p_export_keys,
Manuel Pégourié-Gonnard6fa57bf2019-05-10 10:50:04 +02001365 master, keyblk,
Hanno Becker88aaf652017-12-27 08:17:40 +00001366 mac_key_len, keylen,
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001367 iv_copy_len );
1368 }
Ron Eldorf5cc10d2019-05-07 18:33:40 +03001369
1370 if( ssl->conf->f_export_keys_ext != NULL )
1371 {
1372 ssl->conf->f_export_keys_ext( ssl->conf->p_export_keys,
Manuel Pégourié-Gonnard6fa57bf2019-05-10 10:50:04 +02001373 master, keyblk,
Ron Eldorb7fd64c2019-05-12 11:03:32 +03001374 mac_key_len, keylen,
Ron Eldor51d3ab52019-05-12 14:54:30 +03001375 iv_copy_len,
Manuel Pégourié-Gonnard344460c2019-07-25 13:17:38 +02001376 /* work around bug in exporter type */
Manuel Pégourié-Gonnard9b108c22019-05-06 13:32:17 +02001377 (unsigned char *) randbytes + 32,
1378 (unsigned char *) randbytes,
1379 tls_prf_get_type( tls_prf ) );
Ron Eldorf5cc10d2019-05-07 18:33:40 +03001380 }
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001381#endif
1382
Hanno Beckerf704bef2018-11-16 15:21:18 +00001383#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Beckercb1cc802018-11-17 22:27:38 +00001384
1385 /* Only use PSA-based ciphers for TLS-1.2.
1386 * That's relevant at least for TLS-1.0, where
1387 * we assume that mbedtls_cipher_crypt() updates
1388 * the structure field for the IV, which the PSA-based
1389 * implementation currently doesn't. */
1390#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1391 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001392 {
Hanno Beckercb1cc802018-11-17 22:27:38 +00001393 ret = mbedtls_cipher_setup_psa( &transform->cipher_ctx_enc,
Hanno Becker22bf1452019-04-05 11:21:08 +01001394 cipher_info, transform->taglen );
Hanno Beckercb1cc802018-11-17 22:27:38 +00001395 if( ret != 0 && ret != MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE )
1396 {
1397 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup_psa", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001398 goto end;
Hanno Beckercb1cc802018-11-17 22:27:38 +00001399 }
1400
1401 if( ret == 0 )
1402 {
Hanno Becker4c8c7aa2019-04-10 09:25:41 +01001403 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Successfully setup PSA-based encryption cipher context" ) );
Hanno Beckercb1cc802018-11-17 22:27:38 +00001404 psa_fallthrough = 0;
1405 }
1406 else
1407 {
1408 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to setup PSA-based cipher context for record encryption - fall through to default setup." ) );
1409 psa_fallthrough = 1;
1410 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001411 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001412 else
Hanno Beckercb1cc802018-11-17 22:27:38 +00001413 psa_fallthrough = 1;
1414#else
1415 psa_fallthrough = 1;
1416#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Beckerf704bef2018-11-16 15:21:18 +00001417
Hanno Beckercb1cc802018-11-17 22:27:38 +00001418 if( psa_fallthrough == 1 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001419#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001420 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001421 cipher_info ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001422 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001423 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001424 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001425 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001426
Hanno Beckerf704bef2018-11-16 15:21:18 +00001427#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Beckercb1cc802018-11-17 22:27:38 +00001428 /* Only use PSA-based ciphers for TLS-1.2.
1429 * That's relevant at least for TLS-1.0, where
1430 * we assume that mbedtls_cipher_crypt() updates
1431 * the structure field for the IV, which the PSA-based
1432 * implementation currently doesn't. */
1433#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1434 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001435 {
Hanno Beckercb1cc802018-11-17 22:27:38 +00001436 ret = mbedtls_cipher_setup_psa( &transform->cipher_ctx_dec,
Hanno Becker22bf1452019-04-05 11:21:08 +01001437 cipher_info, transform->taglen );
Hanno Beckercb1cc802018-11-17 22:27:38 +00001438 if( ret != 0 && ret != MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE )
1439 {
1440 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup_psa", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001441 goto end;
Hanno Beckercb1cc802018-11-17 22:27:38 +00001442 }
1443
1444 if( ret == 0 )
1445 {
Hanno Becker4c8c7aa2019-04-10 09:25:41 +01001446 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Successfully setup PSA-based decryption cipher context" ) );
Hanno Beckercb1cc802018-11-17 22:27:38 +00001447 psa_fallthrough = 0;
1448 }
1449 else
1450 {
1451 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to setup PSA-based cipher context for record decryption - fall through to default setup." ) );
1452 psa_fallthrough = 1;
1453 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001454 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001455 else
Hanno Beckercb1cc802018-11-17 22:27:38 +00001456 psa_fallthrough = 1;
1457#else
1458 psa_fallthrough = 1;
1459#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Beckerf704bef2018-11-16 15:21:18 +00001460
Hanno Beckercb1cc802018-11-17 22:27:38 +00001461 if( psa_fallthrough == 1 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001462#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001463 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001464 cipher_info ) ) != 0 )
1465 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001466 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001467 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001468 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001469
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001470 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001471 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001472 MBEDTLS_ENCRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001473 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001474 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001475 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001476 }
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001477
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001478 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001479 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001480 MBEDTLS_DECRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001481 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001482 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001483 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001484 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001485
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001486#if defined(MBEDTLS_CIPHER_MODE_CBC)
1487 if( cipher_info->mode == MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001488 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001489 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc,
1490 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001491 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001492 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001493 goto end;
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001494 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001495
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001496 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec,
1497 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001498 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001499 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001500 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001501 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001502 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001503#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001504
Paul Bakker5121ce52009-01-03 21:22:43 +00001505
Manuel Pégourié-Gonnardd73b47f2019-05-06 12:44:24 +02001506 /* Initialize Zlib contexts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001507#if defined(MBEDTLS_ZLIB_SUPPORT)
Manuel Pégourié-Gonnard6fa57bf2019-05-10 10:50:04 +02001508 if( compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001509 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001510 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Initializing zlib states" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001511
Paul Bakker48916f92012-09-16 19:57:18 +00001512 memset( &transform->ctx_deflate, 0, sizeof( transform->ctx_deflate ) );
1513 memset( &transform->ctx_inflate, 0, sizeof( transform->ctx_inflate ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001514
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001515 if( deflateInit( &transform->ctx_deflate,
1516 Z_DEFAULT_COMPRESSION ) != Z_OK ||
Paul Bakker48916f92012-09-16 19:57:18 +00001517 inflateInit( &transform->ctx_inflate ) != Z_OK )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001518 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001519 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001520 ret = MBEDTLS_ERR_SSL_COMPRESSION_FAILED;
1521 goto end;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001522 }
1523 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001524#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00001525
Ron Eldore6992702019-05-07 18:27:13 +03001526end:
Ron Eldora9f9a732019-05-07 18:29:02 +03001527 mbedtls_platform_zeroize( keyblk, sizeof( keyblk ) );
Ron Eldore6992702019-05-07 18:27:13 +03001528 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001529}
1530
Manuel Pégourié-Gonnard8d2805c2019-04-30 12:08:59 +02001531/*
Manuel Pégourié-Gonnard47e33e12019-05-20 10:10:17 +02001532 * Set appropriate PRF function and other SSL / TLS 1.0/1.1 / TLS1.2 functions
Manuel Pégourié-Gonnard8d2805c2019-04-30 12:08:59 +02001533 *
1534 * Inputs:
1535 * - SSL/TLS minor version
1536 * - hash associated with the ciphersuite (only used by TLS 1.2)
1537 *
Manuel Pégourié-Gonnard31d3ef12019-05-10 10:25:00 +02001538 * Outputs:
Manuel Pégourié-Gonnard8d2805c2019-04-30 12:08:59 +02001539 * - the tls_prf, calc_verify and calc_finished members of handshake structure
1540 */
1541static int ssl_set_handshake_prfs( mbedtls_ssl_handshake_params *handshake,
1542 int minor_ver,
1543 mbedtls_md_type_t hash )
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001544{
Manuel Pégourié-Gonnard8d2805c2019-04-30 12:08:59 +02001545#if !defined(MBEDTLS_SSL_PROTO_TLS1_2) || !defined(MBEDTLS_SHA512_C)
1546 (void) hash;
1547#endif
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001548
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001549#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnard8d2805c2019-04-30 12:08:59 +02001550 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001551 {
1552 handshake->tls_prf = ssl3_prf;
1553 handshake->calc_verify = ssl_calc_verify_ssl;
1554 handshake->calc_finished = ssl_calc_finished_ssl;
1555 }
1556 else
1557#endif
1558#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Manuel Pégourié-Gonnard8d2805c2019-04-30 12:08:59 +02001559 if( minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001560 {
1561 handshake->tls_prf = tls1_prf;
1562 handshake->calc_verify = ssl_calc_verify_tls;
1563 handshake->calc_finished = ssl_calc_finished_tls;
1564 }
1565 else
1566#endif
1567#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1568#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard8d2805c2019-04-30 12:08:59 +02001569 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
1570 hash == MBEDTLS_MD_SHA384 )
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001571 {
1572 handshake->tls_prf = tls_prf_sha384;
1573 handshake->calc_verify = ssl_calc_verify_tls_sha384;
1574 handshake->calc_finished = ssl_calc_finished_tls_sha384;
1575 }
1576 else
1577#endif
1578#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnard8d2805c2019-04-30 12:08:59 +02001579 if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001580 {
1581 handshake->tls_prf = tls_prf_sha256;
1582 handshake->calc_verify = ssl_calc_verify_tls_sha256;
1583 handshake->calc_finished = ssl_calc_finished_tls_sha256;
1584 }
1585 else
1586#endif
1587#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
1588 {
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001589 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1590 }
1591
1592 return( 0 );
1593}
1594
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001595/*
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001596 * Compute master secret if needed
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001597 *
1598 * Parameters:
1599 * [in/out] handshake
1600 * [in] resume, premaster, extended_ms, calc_verify, tls_prf
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001601 * (PSA-PSK) ciphersuite_info, psk_opaque
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001602 * [out] premaster (cleared)
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001603 * [out] master
1604 * [in] ssl: optionally used for debugging, EMS and PSA-PSK
1605 * debug: conf->f_dbg, conf->p_dbg
1606 * EMS: passed to calc_verify (debug + (SSL3) session_negotiate)
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001607 * PSA-PSA: minor_ver, conf
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001608 */
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001609static int ssl_compute_master( mbedtls_ssl_handshake_params *handshake,
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001610 unsigned char *master,
Manuel Pégourié-Gonnard0d56aaa2019-05-03 09:58:33 +02001611 const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001612{
1613 int ret;
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001614
1615 /* cf. RFC 5246, Section 8.1:
1616 * "The master secret is always exactly 48 bytes in length." */
1617 size_t const master_secret_len = 48;
1618
1619#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
1620 unsigned char session_hash[48];
1621#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
1622
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001623 /* The label for the KDF used for key expansion.
1624 * This is either "master secret" or "extended master secret"
1625 * depending on whether the Extended Master Secret extension
1626 * is used. */
1627 char const *lbl = "master secret";
1628
1629 /* The salt for the KDF used for key expansion.
1630 * - If the Extended Master Secret extension is not used,
1631 * this is ClientHello.Random + ServerHello.Random
1632 * (see Sect. 8.1 in RFC 5246).
1633 * - If the Extended Master Secret extension is used,
1634 * this is the transcript of the handshake so far.
1635 * (see Sect. 4 in RFC 7627). */
1636 unsigned char const *salt = handshake->randbytes;
1637 size_t salt_len = 64;
1638
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001639#if !defined(MBEDTLS_DEBUG_C) && \
1640 !defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) && \
1641 !(defined(MBEDTLS_USE_PSA_CRYPTO) && \
1642 defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED))
Manuel Pégourié-Gonnarda7505d12019-05-07 10:17:56 +02001643 ssl = NULL; /* make sure we don't use it except for those cases */
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001644 (void) ssl;
1645#endif
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001646
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001647 if( handshake->resume != 0 )
1648 {
1649 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001650 return( 0 );
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001651 }
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001652
1653#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001654 if( handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED )
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001655 {
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001656 lbl = "extended master secret";
1657 salt = session_hash;
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001658 handshake->calc_verify( ssl, session_hash, &salt_len );
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001659
Manuel Pégourié-Gonnard8faa70e2019-05-20 12:09:50 +02001660 MBEDTLS_SSL_DEBUG_BUF( 3, "session hash for extended master secret",
1661 session_hash, salt_len );
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001662 }
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001663#endif /* MBEDTLS_SSL_EXTENDED_MS_ENABLED */
1664
1665#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001666 defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
1667 if( handshake->ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK &&
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001668 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001669 ssl_use_opaque_psk( ssl ) == 1 )
1670 {
1671 /* Perform PSK-to-MS expansion in a single step. */
1672 psa_status_t status;
1673 psa_algorithm_t alg;
1674 psa_key_handle_t psk;
1675 psa_key_derivation_operation_t derivation =
1676 PSA_KEY_DERIVATION_OPERATION_INIT;
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001677 mbedtls_md_type_t hash_alg = handshake->ciphersuite_info->mac;
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001678
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001679 MBEDTLS_SSL_DEBUG_MSG( 2, ( "perform PSA-based PSK-to-MS expansion" ) );
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001680
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001681 psk = ssl->conf->psk_opaque;
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001682 if( handshake->psk_opaque != 0 )
1683 psk = handshake->psk_opaque;
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001684
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001685 if( hash_alg == MBEDTLS_MD_SHA384 )
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001686 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384);
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001687 else
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001688 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256);
1689
1690 status = psa_key_derivation( &derivation, psk, alg,
1691 salt, salt_len,
1692 (unsigned char const *) lbl,
1693 (size_t) strlen( lbl ),
1694 master_secret_len );
1695 if( status != PSA_SUCCESS )
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001696 {
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001697 psa_key_derivation_abort( &derivation );
1698 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001699 }
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001700
1701 status = psa_key_derivation_output_bytes( &derivation,
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001702 master,
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001703 master_secret_len );
1704 if( status != PSA_SUCCESS )
1705 {
1706 psa_key_derivation_abort( &derivation );
1707 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
1708 }
1709
1710 status = psa_key_derivation_abort( &derivation );
1711 if( status != PSA_SUCCESS )
1712 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
1713 }
1714 else
1715#endif
1716 {
1717 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
1718 lbl, salt, salt_len,
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001719 master,
Manuel Pégourié-Gonnard85680c42019-05-03 09:16:16 +02001720 master_secret_len );
1721 if( ret != 0 )
1722 {
1723 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
1724 return( ret );
1725 }
1726
1727 MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret",
1728 handshake->premaster,
1729 handshake->pmslen );
1730
1731 mbedtls_platform_zeroize( handshake->premaster,
1732 sizeof(handshake->premaster) );
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001733 }
1734
1735 return( 0 );
1736}
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001737
Manuel Pégourié-Gonnarde59ae232019-04-30 11:41:40 +02001738int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl )
1739{
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001740 int ret;
Manuel Pégourié-Gonnard8d2805c2019-04-30 12:08:59 +02001741 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
1742 ssl->handshake->ciphersuite_info;
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001743
Manuel Pégourié-Gonnard040a9512019-05-06 12:05:58 +02001744 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
1745
1746 /* Set PRF, calc_verify and calc_finished function pointers */
Manuel Pégourié-Gonnard8d2805c2019-04-30 12:08:59 +02001747 ret = ssl_set_handshake_prfs( ssl->handshake,
1748 ssl->minor_ver,
1749 ciphersuite_info->mac );
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001750 if( ret != 0 )
Manuel Pégourié-Gonnard8d2805c2019-04-30 12:08:59 +02001751 {
1752 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_set_handshake_prfs", ret );
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001753 return( ret );
Manuel Pégourié-Gonnard8d2805c2019-04-30 12:08:59 +02001754 }
Manuel Pégourié-Gonnard1b00c4f2019-04-30 11:54:22 +02001755
Manuel Pégourié-Gonnard040a9512019-05-06 12:05:58 +02001756 /* Compute master secret if needed */
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001757 ret = ssl_compute_master( ssl->handshake,
Manuel Pégourié-Gonnardde047ad2019-05-03 09:46:14 +02001758 ssl->session_negotiate->master,
1759 ssl );
Manuel Pégourié-Gonnard9951b712019-04-30 12:08:59 +02001760 if( ret != 0 )
1761 {
1762 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compute_master", ret );
1763 return( ret );
1764 }
1765
Manuel Pégourié-Gonnard040a9512019-05-06 12:05:58 +02001766 /* Swap the client and server random values:
1767 * - MS derivation wanted client+server (RFC 5246 8.1)
1768 * - key derivation wants server+client (RFC 5246 6.3) */
1769 {
1770 unsigned char tmp[64];
1771 memcpy( tmp, ssl->handshake->randbytes, 64 );
1772 memcpy( ssl->handshake->randbytes, tmp + 32, 32 );
1773 memcpy( ssl->handshake->randbytes + 32, tmp, 32 );
1774 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
1775 }
1776
1777 /* Populate transform structure */
Manuel Pégourié-Gonnardcba40d92019-05-06 12:55:40 +02001778 ret = ssl_populate_transform( ssl->transform_negotiate,
Manuel Pégourié-Gonnard6fa57bf2019-05-10 10:50:04 +02001779 ssl->session_negotiate->ciphersuite,
1780 ssl->session_negotiate->master,
1781#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1782 ssl->session_negotiate->encrypt_then_mac,
1783#endif
1784#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
1785 ssl->session_negotiate->trunc_hmac,
1786#endif
1787#if defined(MBEDTLS_ZLIB_SUPPORT)
1788 ssl->session_negotiate->compression,
1789#endif
Manuel Pégourié-Gonnard9b108c22019-05-06 13:32:17 +02001790 ssl->handshake->tls_prf,
1791 ssl->handshake->randbytes,
Manuel Pégourié-Gonnardc864f6a2019-05-06 13:48:22 +02001792 ssl->minor_ver,
1793 ssl->conf->endpoint,
Manuel Pégourié-Gonnardcba40d92019-05-06 12:55:40 +02001794 ssl );
Manuel Pégourié-Gonnard040a9512019-05-06 12:05:58 +02001795 if( ret != 0 )
1796 {
1797 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_populate_transform", ret );
1798 return( ret );
1799 }
1800
1801 /* We no longer need Server/ClientHello.random values */
1802 mbedtls_platform_zeroize( ssl->handshake->randbytes,
1803 sizeof( ssl->handshake->randbytes ) );
1804
Manuel Pégourié-Gonnardd73b47f2019-05-06 12:44:24 +02001805 /* Allocate compression buffer */
1806#if defined(MBEDTLS_ZLIB_SUPPORT)
1807 if( session->compression == MBEDTLS_SSL_COMPRESS_DEFLATE &&
1808 ssl->compress_buf == NULL )
1809 {
1810 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Allocating compression buffer" ) );
1811 ssl->compress_buf = mbedtls_calloc( 1, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
1812 if( ssl->compress_buf == NULL )
1813 {
1814 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
Manuel Pégourié-Gonnardd91efa42019-05-20 10:27:20 +02001815 MBEDTLS_SSL_COMPRESS_BUFFER_LEN ) );
Manuel Pégourié-Gonnardd73b47f2019-05-06 12:44:24 +02001816 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
1817 }
1818 }
1819#endif
1820
Manuel Pégourié-Gonnard040a9512019-05-06 12:05:58 +02001821 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
1822
1823 return( 0 );
Manuel Pégourié-Gonnarde59ae232019-04-30 11:41:40 +02001824}
1825
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001826#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001827void ssl_calc_verify_ssl( const mbedtls_ssl_context *ssl,
1828 unsigned char hash[36],
1829 size_t *hlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00001830{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001831 mbedtls_md5_context md5;
1832 mbedtls_sha1_context sha1;
Paul Bakker5121ce52009-01-03 21:22:43 +00001833 unsigned char pad_1[48];
1834 unsigned char pad_2[48];
1835
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001836 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001837
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001838 mbedtls_md5_init( &md5 );
1839 mbedtls_sha1_init( &sha1 );
1840
1841 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1842 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001843
Paul Bakker380da532012-04-18 16:10:25 +00001844 memset( pad_1, 0x36, 48 );
1845 memset( pad_2, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001846
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001847 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1848 mbedtls_md5_update_ret( &md5, pad_1, 48 );
1849 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001850
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001851 mbedtls_md5_starts_ret( &md5 );
1852 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1853 mbedtls_md5_update_ret( &md5, pad_2, 48 );
1854 mbedtls_md5_update_ret( &md5, hash, 16 );
1855 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001856
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001857 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1858 mbedtls_sha1_update_ret( &sha1, pad_1, 40 );
1859 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001860
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001861 mbedtls_sha1_starts_ret( &sha1 );
1862 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1863 mbedtls_sha1_update_ret( &sha1, pad_2, 40 );
1864 mbedtls_sha1_update_ret( &sha1, hash + 16, 20 );
1865 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001866
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001867 *hlen = 36;
1868
1869 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001870 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001871
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001872 mbedtls_md5_free( &md5 );
1873 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001874
Paul Bakker380da532012-04-18 16:10:25 +00001875 return;
1876}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001877#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker380da532012-04-18 16:10:25 +00001878
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001879#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001880void ssl_calc_verify_tls( const mbedtls_ssl_context *ssl,
1881 unsigned char hash[36],
1882 size_t *hlen )
Paul Bakker380da532012-04-18 16:10:25 +00001883{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001884 mbedtls_md5_context md5;
1885 mbedtls_sha1_context sha1;
Paul Bakker380da532012-04-18 16:10:25 +00001886
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001887 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001888
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001889 mbedtls_md5_init( &md5 );
1890 mbedtls_sha1_init( &sha1 );
1891
1892 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1893 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker380da532012-04-18 16:10:25 +00001894
Andrzej Kurekeb342242019-01-29 09:14:33 -05001895 mbedtls_md5_finish_ret( &md5, hash );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001896 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001897
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001898 *hlen = 36;
1899
1900 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001901 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001902
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001903 mbedtls_md5_free( &md5 );
1904 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001905
Paul Bakker380da532012-04-18 16:10:25 +00001906 return;
1907}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001908#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker380da532012-04-18 16:10:25 +00001909
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001910#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1911#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001912void ssl_calc_verify_tls_sha256( const mbedtls_ssl_context *ssl,
1913 unsigned char hash[32],
1914 size_t *hlen )
Paul Bakker380da532012-04-18 16:10:25 +00001915{
Andrzej Kurekeb342242019-01-29 09:14:33 -05001916#if defined(MBEDTLS_USE_PSA_CRYPTO)
1917 size_t hash_size;
1918 psa_status_t status;
1919 psa_hash_operation_t sha256_psa = psa_hash_operation_init();
1920
1921 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha256" ) );
1922 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
1923 if( status != PSA_SUCCESS )
1924 {
1925 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
1926 return;
1927 }
1928
1929 status = psa_hash_finish( &sha256_psa, hash, 32, &hash_size );
1930 if( status != PSA_SUCCESS )
1931 {
1932 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
1933 return;
1934 }
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001935
1936 *hlen = 32;
1937 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, *hlen );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001938 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
1939#else
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001940 mbedtls_sha256_context sha256;
Paul Bakker380da532012-04-18 16:10:25 +00001941
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001942 mbedtls_sha256_init( &sha256 );
1943
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001944 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001945
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001946 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001947 mbedtls_sha256_finish_ret( &sha256, hash );
Paul Bakker380da532012-04-18 16:10:25 +00001948
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001949 *hlen = 32;
1950
1951 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001952 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001953
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001954 mbedtls_sha256_free( &sha256 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001955#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker380da532012-04-18 16:10:25 +00001956 return;
1957}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001958#endif /* MBEDTLS_SHA256_C */
Paul Bakker380da532012-04-18 16:10:25 +00001959
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001960#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001961void ssl_calc_verify_tls_sha384( const mbedtls_ssl_context *ssl,
1962 unsigned char hash[48],
1963 size_t *hlen )
Paul Bakker380da532012-04-18 16:10:25 +00001964{
Andrzej Kurekeb342242019-01-29 09:14:33 -05001965#if defined(MBEDTLS_USE_PSA_CRYPTO)
1966 size_t hash_size;
1967 psa_status_t status;
Andrzej Kurek972fba52019-01-30 03:29:12 -05001968 psa_hash_operation_t sha384_psa = psa_hash_operation_init();
Andrzej Kurekeb342242019-01-29 09:14:33 -05001969
1970 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha384" ) );
Andrzej Kurek972fba52019-01-30 03:29:12 -05001971 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001972 if( status != PSA_SUCCESS )
1973 {
1974 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
1975 return;
1976 }
1977
Andrzej Kurek972fba52019-01-30 03:29:12 -05001978 status = psa_hash_finish( &sha384_psa, hash, 48, &hash_size );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001979 if( status != PSA_SUCCESS )
1980 {
1981 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
1982 return;
1983 }
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001984
1985 *hlen = 48;
1986 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, *hlen );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001987 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
1988#else
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001989 mbedtls_sha512_context sha512;
Paul Bakker380da532012-04-18 16:10:25 +00001990
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001991 mbedtls_sha512_init( &sha512 );
1992
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001993 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001994
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001995 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001996 mbedtls_sha512_finish_ret( &sha512, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001997
Manuel Pégourié-Gonnardde718b92019-05-03 11:43:28 +02001998 *hlen = 48;
1999
2000 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002001 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002002
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02002003 mbedtls_sha512_free( &sha512 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05002004#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker5121ce52009-01-03 21:22:43 +00002005 return;
2006}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002007#endif /* MBEDTLS_SHA512_C */
2008#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00002009
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002010#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
2011int 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 +02002012{
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002013 unsigned char *p = ssl->handshake->premaster;
2014 unsigned char *end = p + sizeof( ssl->handshake->premaster );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002015 const unsigned char *psk = ssl->conf->psk;
2016 size_t psk_len = ssl->conf->psk_len;
2017
2018 /* If the psk callback was called, use its result */
2019 if( ssl->handshake->psk != NULL )
2020 {
2021 psk = ssl->handshake->psk;
2022 psk_len = ssl->handshake->psk_len;
2023 }
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002024
2025 /*
2026 * PMS = struct {
2027 * opaque other_secret<0..2^16-1>;
2028 * opaque psk<0..2^16-1>;
2029 * };
2030 * with "other_secret" depending on the particular key exchange
2031 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002032#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
2033 if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002034 {
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02002035 if( end - p < 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002036 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002037
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002038 *(p++) = (unsigned char)( psk_len >> 8 );
2039 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02002040
2041 if( end < p || (size_t)( end - p ) < psk_len )
2042 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2043
2044 memset( p, 0, psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002045 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002046 }
2047 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002048#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
2049#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
2050 if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002051 {
2052 /*
2053 * other_secret already set by the ClientKeyExchange message,
2054 * and is 48 bytes long
2055 */
Philippe Antoine747fd532018-05-30 09:13:21 +02002056 if( end - p < 2 )
2057 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2058
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02002059 *p++ = 0;
2060 *p++ = 48;
2061 p += 48;
2062 }
2063 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002064#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
2065#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
2066 if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002067 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02002068 int ret;
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01002069 size_t len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002070
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02002071 /* Write length only when we know the actual value */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002072 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01002073 p + 2, end - ( p + 2 ), &len,
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01002074 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002075 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002076 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002077 return( ret );
2078 }
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02002079 *(p++) = (unsigned char)( len >> 8 );
2080 *(p++) = (unsigned char)( len );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002081 p += len;
2082
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002083 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002084 }
2085 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002086#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
2087#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
2088 if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002089 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02002090 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002091 size_t zlen;
2092
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002093 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
Paul Bakker66d5d072014-06-17 16:39:18 +02002094 p + 2, end - ( p + 2 ),
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01002095 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002096 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002097 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002098 return( ret );
2099 }
2100
2101 *(p++) = (unsigned char)( zlen >> 8 );
2102 *(p++) = (unsigned char)( zlen );
2103 p += zlen;
2104
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05002105 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
2106 MBEDTLS_DEBUG_ECDH_Z );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002107 }
2108 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002109#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002110 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002111 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2112 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002113 }
2114
2115 /* opaque psk<0..2^16-1>; */
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02002116 if( end - p < 2 )
2117 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01002118
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002119 *(p++) = (unsigned char)( psk_len >> 8 );
2120 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02002121
2122 if( end < p || (size_t)( end - p ) < psk_len )
2123 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2124
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002125 memcpy( p, psk, psk_len );
2126 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002127
2128 ssl->handshake->pmslen = p - ssl->handshake->premaster;
2129
2130 return( 0 );
2131}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002132#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02002133
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002134#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00002135/*
2136 * SSLv3.0 MAC functions
2137 */
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01002138#define SSL_MAC_MAX_BYTES 20 /* MD-5 or SHA-1 */
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01002139static void ssl_mac( mbedtls_md_context_t *md_ctx,
2140 const unsigned char *secret,
2141 const unsigned char *buf, size_t len,
2142 const unsigned char *ctr, int type,
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01002143 unsigned char out[SSL_MAC_MAX_BYTES] )
Paul Bakker5121ce52009-01-03 21:22:43 +00002144{
2145 unsigned char header[11];
2146 unsigned char padding[48];
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02002147 int padlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002148 int md_size = mbedtls_md_get_size( md_ctx->md_info );
2149 int md_type = mbedtls_md_get_type( md_ctx->md_info );
Paul Bakker68884e32013-01-07 18:20:04 +01002150
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02002151 /* Only MD5 and SHA-1 supported */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002152 if( md_type == MBEDTLS_MD_MD5 )
Paul Bakker68884e32013-01-07 18:20:04 +01002153 padlen = 48;
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02002154 else
Paul Bakker68884e32013-01-07 18:20:04 +01002155 padlen = 40;
Paul Bakker5121ce52009-01-03 21:22:43 +00002156
2157 memcpy( header, ctr, 8 );
2158 header[ 8] = (unsigned char) type;
2159 header[ 9] = (unsigned char)( len >> 8 );
2160 header[10] = (unsigned char)( len );
2161
Paul Bakker68884e32013-01-07 18:20:04 +01002162 memset( padding, 0x36, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002163 mbedtls_md_starts( md_ctx );
2164 mbedtls_md_update( md_ctx, secret, md_size );
2165 mbedtls_md_update( md_ctx, padding, padlen );
2166 mbedtls_md_update( md_ctx, header, 11 );
2167 mbedtls_md_update( md_ctx, buf, len );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01002168 mbedtls_md_finish( md_ctx, out );
Paul Bakker5121ce52009-01-03 21:22:43 +00002169
Paul Bakker68884e32013-01-07 18:20:04 +01002170 memset( padding, 0x5C, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002171 mbedtls_md_starts( md_ctx );
2172 mbedtls_md_update( md_ctx, secret, md_size );
2173 mbedtls_md_update( md_ctx, padding, padlen );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01002174 mbedtls_md_update( md_ctx, out, md_size );
2175 mbedtls_md_finish( md_ctx, out );
Paul Bakker5f70b252012-09-13 14:23:06 +00002176}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002177#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +00002178
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002179/* The function below is only used in the Lucky 13 counter-measure in
Hanno Beckerb2ca87d2018-10-18 15:43:13 +01002180 * mbedtls_ssl_decrypt_buf(). These are the defines that guard the call site. */
Hanno Becker52344c22018-01-03 15:24:20 +00002181#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) && \
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02002182 ( defined(MBEDTLS_SSL_PROTO_TLS1) || \
2183 defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2184 defined(MBEDTLS_SSL_PROTO_TLS1_2) )
2185/* This function makes sure every byte in the memory region is accessed
2186 * (in ascending addresses order) */
2187static void ssl_read_memory( unsigned char *p, size_t len )
2188{
2189 unsigned char acc = 0;
2190 volatile unsigned char force;
2191
2192 for( ; len != 0; p++, len-- )
2193 acc ^= *p;
2194
2195 force = acc;
2196 (void) force;
2197}
2198#endif /* SSL_SOME_MODES_USE_MAC && ( TLS1 || TLS1_1 || TLS1_2 ) */
2199
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01002200/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002201 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +02002202 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002203
Hanno Beckera0e20d02019-05-15 14:03:01 +01002204#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerd3f8c792019-05-20 15:06:12 +01002205/* This functions transforms a DTLS plaintext fragment and a record content
2206 * type into an instance of the DTLSInnerPlaintext structure:
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002207 *
2208 * struct {
2209 * opaque content[DTLSPlaintext.length];
2210 * ContentType real_type;
2211 * uint8 zeros[length_of_padding];
2212 * } DTLSInnerPlaintext;
2213 *
2214 * Input:
2215 * - `content`: The beginning of the buffer holding the
2216 * plaintext to be wrapped.
2217 * - `*content_size`: The length of the plaintext in Bytes.
2218 * - `max_len`: The number of Bytes available starting from
2219 * `content`. This must be `>= *content_size`.
2220 * - `rec_type`: The desired record content type.
2221 *
2222 * Output:
2223 * - `content`: The beginning of the resulting DTLSInnerPlaintext structure.
2224 * - `*content_size`: The length of the resulting DTLSInnerPlaintext structure.
2225 *
2226 * Returns:
2227 * - `0` on success.
2228 * - A negative error code if `max_len` didn't offer enough space
2229 * for the expansion.
2230 */
2231static int ssl_cid_build_inner_plaintext( unsigned char *content,
2232 size_t *content_size,
2233 size_t remaining,
2234 uint8_t rec_type )
2235{
2236 size_t len = *content_size;
Hanno Beckerb9ec44f2019-05-13 15:31:17 +01002237 size_t pad = ( MBEDTLS_SSL_CID_PADDING_GRANULARITY -
2238 ( len + 1 ) % MBEDTLS_SSL_CID_PADDING_GRANULARITY ) %
2239 MBEDTLS_SSL_CID_PADDING_GRANULARITY;
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002240
2241 /* Write real content type */
2242 if( remaining == 0 )
2243 return( -1 );
2244 content[ len ] = rec_type;
2245 len++;
2246 remaining--;
2247
2248 if( remaining < pad )
2249 return( -1 );
2250 memset( content + len, 0, pad );
2251 len += pad;
2252 remaining -= pad;
2253
2254 *content_size = len;
2255 return( 0 );
2256}
2257
Hanno Becker07dc97d2019-05-20 15:08:01 +01002258/* This function parses a DTLSInnerPlaintext structure.
2259 * See ssl_cid_build_inner_plaintext() for details. */
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002260static int ssl_cid_parse_inner_plaintext( unsigned char const *content,
2261 size_t *content_size,
2262 uint8_t *rec_type )
2263{
2264 size_t remaining = *content_size;
2265
2266 /* Determine length of padding by skipping zeroes from the back. */
2267 do
2268 {
2269 if( remaining == 0 )
2270 return( -1 );
2271 remaining--;
2272 } while( content[ remaining ] == 0 );
2273
2274 *content_size = remaining;
2275 *rec_type = content[ remaining ];
2276
2277 return( 0 );
2278}
Hanno Beckera0e20d02019-05-15 14:03:01 +01002279#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002280
Hanno Beckerd5aeab12019-05-20 14:50:53 +01002281/* `add_data` must have size 13 Bytes if the CID extension is disabled,
Hanno Beckerc4a190b2019-05-08 18:15:21 +01002282 * and 13 + 1 + CID-length Bytes if the CID extension is enabled. */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002283static void ssl_extract_add_data_from_record( unsigned char* add_data,
Hanno Beckercab87e62019-04-29 13:52:53 +01002284 size_t *add_data_len,
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002285 mbedtls_record *rec )
2286{
Hanno Beckerd5aeab12019-05-20 14:50:53 +01002287 /* Quoting RFC 5246 (TLS 1.2):
Hanno Beckercab87e62019-04-29 13:52:53 +01002288 *
2289 * additional_data = seq_num + TLSCompressed.type +
2290 * TLSCompressed.version + TLSCompressed.length;
2291 *
Hanno Beckerd5aeab12019-05-20 14:50:53 +01002292 * For the CID extension, this is extended as follows
2293 * (quoting draft-ietf-tls-dtls-connection-id-05,
2294 * https://tools.ietf.org/html/draft-ietf-tls-dtls-connection-id-05):
Hanno Beckercab87e62019-04-29 13:52:53 +01002295 *
2296 * additional_data = seq_num + DTLSPlaintext.type +
2297 * DTLSPlaintext.version +
Hanno Beckerd5aeab12019-05-20 14:50:53 +01002298 * cid +
2299 * cid_length +
Hanno Beckercab87e62019-04-29 13:52:53 +01002300 * length_of_DTLSInnerPlaintext;
2301 */
2302
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002303 memcpy( add_data, rec->ctr, sizeof( rec->ctr ) );
2304 add_data[8] = rec->type;
Hanno Beckeredb24f82019-05-20 15:01:46 +01002305 memcpy( add_data + 9, rec->ver, sizeof( rec->ver ) );
Hanno Beckercab87e62019-04-29 13:52:53 +01002306
Hanno Beckera0e20d02019-05-15 14:03:01 +01002307#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker95e4bbc2019-05-09 11:38:24 +01002308 if( rec->cid_len != 0 )
2309 {
2310 memcpy( add_data + 11, rec->cid, rec->cid_len );
2311 add_data[11 + rec->cid_len + 0] = rec->cid_len;
2312 add_data[11 + rec->cid_len + 1] = ( rec->data_len >> 8 ) & 0xFF;
2313 add_data[11 + rec->cid_len + 2] = ( rec->data_len >> 0 ) & 0xFF;
2314 *add_data_len = 13 + 1 + rec->cid_len;
2315 }
2316 else
Hanno Beckera0e20d02019-05-15 14:03:01 +01002317#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker95e4bbc2019-05-09 11:38:24 +01002318 {
2319 add_data[11 + 0] = ( rec->data_len >> 8 ) & 0xFF;
2320 add_data[11 + 1] = ( rec->data_len >> 0 ) & 0xFF;
2321 *add_data_len = 13;
2322 }
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002323}
2324
Hanno Beckera18d1322018-01-03 14:27:32 +00002325int mbedtls_ssl_encrypt_buf( mbedtls_ssl_context *ssl,
2326 mbedtls_ssl_transform *transform,
2327 mbedtls_record *rec,
2328 int (*f_rng)(void *, unsigned char *, size_t),
2329 void *p_rng )
Paul Bakker5121ce52009-01-03 21:22:43 +00002330{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002331 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002332 int auth_done = 0;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002333 unsigned char * data;
Hanno Becker92fb4fa2019-05-20 14:54:26 +01002334 unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_OUT_LEN_MAX ];
Hanno Beckercab87e62019-04-29 13:52:53 +01002335 size_t add_data_len;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002336 size_t post_avail;
2337
2338 /* The SSL context is only used for debugging purposes! */
Hanno Beckera18d1322018-01-03 14:27:32 +00002339#if !defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnarda7505d12019-05-07 10:17:56 +02002340 ssl = NULL; /* make sure we don't use it except for debug */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002341 ((void) ssl);
2342#endif
2343
2344 /* The PRNG is used for dynamic IV generation that's used
2345 * for CBC transformations in TLS 1.1 and TLS 1.2. */
2346#if !( defined(MBEDTLS_CIPHER_MODE_CBC) && \
2347 ( defined(MBEDTLS_AES_C) || \
2348 defined(MBEDTLS_ARIA_C) || \
2349 defined(MBEDTLS_CAMELLIA_C) ) && \
2350 ( defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2) ) )
2351 ((void) f_rng);
2352 ((void) p_rng);
2353#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002354
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002355 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002356
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002357 if( transform == NULL )
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002358 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002359 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no transform provided to encrypt_buf" ) );
2360 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2361 }
Hanno Becker43c24b82019-05-01 09:45:57 +01002362 if( rec == NULL
2363 || rec->buf == NULL
2364 || rec->buf_len < rec->data_offset
2365 || rec->buf_len - rec->data_offset < rec->data_len
Hanno Beckera0e20d02019-05-15 14:03:01 +01002366#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker43c24b82019-05-01 09:45:57 +01002367 || rec->cid_len != 0
2368#endif
2369 )
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002370 {
2371 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to encrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002372 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002373 }
2374
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002375 data = rec->buf + rec->data_offset;
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002376 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002377 MBEDTLS_SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002378 data, rec->data_len );
2379
2380 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc );
2381
2382 if( rec->data_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
2383 {
2384 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record content %u too large, maximum %d",
2385 (unsigned) rec->data_len,
2386 MBEDTLS_SSL_OUT_CONTENT_LEN ) );
2387 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2388 }
Manuel Pégourié-Gonnard60346be2014-11-21 11:38:37 +01002389
Hanno Beckera0e20d02019-05-15 14:03:01 +01002390#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckercab87e62019-04-29 13:52:53 +01002391 /*
2392 * Add CID information
2393 */
2394 rec->cid_len = transform->out_cid_len;
2395 memcpy( rec->cid, transform->out_cid, transform->out_cid_len );
2396 MBEDTLS_SSL_DEBUG_BUF( 3, "CID", rec->cid, rec->cid_len );
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002397
2398 if( rec->cid_len != 0 )
2399 {
2400 /*
Hanno Becker07dc97d2019-05-20 15:08:01 +01002401 * Wrap plaintext into DTLSInnerPlaintext structure.
2402 * See ssl_cid_build_inner_plaintext() for more information.
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002403 *
Hanno Becker07dc97d2019-05-20 15:08:01 +01002404 * Note that this changes `rec->data_len`, and hence
2405 * `post_avail` needs to be recalculated afterwards.
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002406 */
2407 if( ssl_cid_build_inner_plaintext( data,
2408 &rec->data_len,
2409 post_avail,
2410 rec->type ) != 0 )
2411 {
2412 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2413 }
2414
2415 rec->type = MBEDTLS_SSL_MSG_CID;
2416 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01002417#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckercab87e62019-04-29 13:52:53 +01002418
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002419 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
2420
Paul Bakker5121ce52009-01-03 21:22:43 +00002421 /*
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01002422 * Add MAC before if needed
Paul Bakker5121ce52009-01-03 21:22:43 +00002423 */
Hanno Becker52344c22018-01-03 15:24:20 +00002424#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002425 if( mode == MBEDTLS_MODE_STREAM ||
2426 ( mode == MBEDTLS_MODE_CBC
2427#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002428 && transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002429#endif
2430 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00002431 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002432 if( post_avail < transform->maclen )
2433 {
2434 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2435 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2436 }
2437
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002438#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002439 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002440 {
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01002441 unsigned char mac[SSL_MAC_MAX_BYTES];
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002442 ssl_mac( &transform->md_ctx_enc, transform->mac_enc,
2443 data, rec->data_len, rec->ctr, rec->type, mac );
2444 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002445 }
2446 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002447#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002448#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2449 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002450 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002451 {
Hanno Becker992b6872017-11-09 18:57:39 +00002452 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
2453
Hanno Beckercab87e62019-04-29 13:52:53 +01002454 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker992b6872017-11-09 18:57:39 +00002455
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002456 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
Hanno Beckercab87e62019-04-29 13:52:53 +01002457 add_data_len );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002458 mbedtls_md_hmac_update( &transform->md_ctx_enc,
2459 data, rec->data_len );
2460 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
2461 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
2462
2463 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002464 }
2465 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002466#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002467 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002468 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2469 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002470 }
2471
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002472 MBEDTLS_SSL_DEBUG_BUF( 4, "computed mac", data + rec->data_len,
2473 transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002474
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002475 rec->data_len += transform->maclen;
2476 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002477 auth_done++;
Paul Bakker577e0062013-08-28 11:57:20 +02002478 }
Hanno Becker52344c22018-01-03 15:24:20 +00002479#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00002480
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002481 /*
2482 * Encrypt
2483 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002484#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
2485 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00002486 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002487 int ret;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002488 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002489 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002490 "including %d bytes of padding",
2491 rec->data_len, 0 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002492
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002493 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
2494 transform->iv_enc, transform->ivlen,
2495 data, rec->data_len,
2496 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002497 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002498 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002499 return( ret );
2500 }
2501
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002502 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002503 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002504 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2505 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002506 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002507 }
Paul Bakker68884e32013-01-07 18:20:04 +01002508 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002509#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002510
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002511#if defined(MBEDTLS_GCM_C) || \
2512 defined(MBEDTLS_CCM_C) || \
2513 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002514 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002515 mode == MBEDTLS_MODE_CCM ||
2516 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002517 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02002518 int ret;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002519 unsigned char iv[12];
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002520 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002521
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002522 /* Check that there's space for both the authentication tag
2523 * and the explicit IV before and after the record content. */
2524 if( post_avail < transform->taglen ||
2525 rec->data_offset < explicit_iv_len )
2526 {
2527 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2528 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2529 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00002530
Paul Bakker68884e32013-01-07 18:20:04 +01002531 /*
2532 * Generate IV
2533 */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002534 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
2535 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002536 /* GCM and CCM: fixed || explicit (=seqnum) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002537 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002538 memcpy( iv + transform->fixed_ivlen, rec->ctr,
2539 explicit_iv_len );
2540 /* Prefix record content with explicit IV. */
2541 memcpy( data - explicit_iv_len, rec->ctr, explicit_iv_len );
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002542 }
2543 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
2544 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002545 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002546 unsigned char i;
2547
2548 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
2549
2550 for( i = 0; i < 8; i++ )
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002551 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002552 }
2553 else
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01002554 {
2555 /* Reminder if we ever add an AEAD mode with a different size */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002556 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2557 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01002558 }
2559
Hanno Beckercab87e62019-04-29 13:52:53 +01002560 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker1f10d762019-04-26 13:34:37 +01002561
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002562 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (internal)",
2563 iv, transform->ivlen );
2564 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (transmitted)",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002565 data - explicit_iv_len, explicit_iv_len );
2566 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Hanno Beckercab87e62019-04-29 13:52:53 +01002567 add_data, add_data_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002568 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002569 "including 0 bytes of padding",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002570 rec->data_len ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00002571
Paul Bakker68884e32013-01-07 18:20:04 +01002572 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002573 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002574 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002575
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002576 if( ( ret = mbedtls_cipher_auth_encrypt( &transform->cipher_ctx_enc,
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002577 iv, transform->ivlen,
Hanno Beckercab87e62019-04-29 13:52:53 +01002578 add_data, add_data_len, /* add data */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002579 data, rec->data_len, /* source */
2580 data, &rec->data_len, /* destination */
2581 data + rec->data_len, transform->taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002582 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002583 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_encrypt", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002584 return( ret );
2585 }
2586
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002587 MBEDTLS_SSL_DEBUG_BUF( 4, "after encrypt: tag",
2588 data + rec->data_len, transform->taglen );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002589
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002590 rec->data_len += transform->taglen + explicit_iv_len;
2591 rec->data_offset -= explicit_iv_len;
2592 post_avail -= transform->taglen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002593 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002594 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002595 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002596#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2597#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002598 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002599 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00002600 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002601 int ret;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002602 size_t padlen, i;
2603 size_t olen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002604
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002605 /* Currently we're always using minimal padding
2606 * (up to 255 bytes would be allowed). */
2607 padlen = transform->ivlen - ( rec->data_len + 1 ) % transform->ivlen;
2608 if( padlen == transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002609 padlen = 0;
2610
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002611 /* Check there's enough space in the buffer for the padding. */
2612 if( post_avail < padlen + 1 )
2613 {
2614 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2615 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2616 }
2617
Paul Bakker5121ce52009-01-03 21:22:43 +00002618 for( i = 0; i <= padlen; i++ )
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002619 data[rec->data_len + i] = (unsigned char) padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002620
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002621 rec->data_len += padlen + 1;
2622 post_avail -= padlen + 1;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002623
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002624#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002625 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00002626 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
2627 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002628 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002629 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002630 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002631 if( f_rng == NULL )
2632 {
2633 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No PRNG provided to encrypt_record routine" ) );
2634 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2635 }
2636
2637 if( rec->data_offset < transform->ivlen )
2638 {
2639 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2640 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2641 }
2642
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002643 /*
2644 * Generate IV
2645 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002646 ret = f_rng( p_rng, transform->iv_enc, transform->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00002647 if( ret != 0 )
2648 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002649
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002650 memcpy( data - transform->ivlen, transform->iv_enc,
2651 transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002652
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002653 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002654#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002655
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002656 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002657 "including %d bytes of IV and %d bytes of padding",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002658 rec->data_len, transform->ivlen,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002659 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002660
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002661 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
2662 transform->iv_enc,
2663 transform->ivlen,
2664 data, rec->data_len,
2665 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002666 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002667 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002668 return( ret );
2669 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002670
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002671 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02002672 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002673 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2674 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002675 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002676
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002677#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002678 if( transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02002679 {
2680 /*
2681 * Save IV in SSL3 and TLS1
2682 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002683 memcpy( transform->iv_enc, transform->cipher_ctx_enc.iv,
2684 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002685 }
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002686 else
Paul Bakkercca5b812013-08-31 17:40:26 +02002687#endif
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002688 {
2689 data -= transform->ivlen;
2690 rec->data_offset -= transform->ivlen;
2691 rec->data_len += transform->ivlen;
2692 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002693
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002694#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002695 if( auth_done == 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002696 {
Hanno Becker3d8c9072018-01-05 16:24:22 +00002697 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
2698
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002699 /*
2700 * MAC(MAC_write_key, seq_num +
2701 * TLSCipherText.type +
2702 * TLSCipherText.version +
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002703 * length_of( (IV +) ENC(...) ) +
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002704 * IV + // except for TLS 1.0
2705 * ENC(content + padding + padding_length));
2706 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002707
2708 if( post_avail < transform->maclen)
2709 {
2710 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2711 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2712 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002713
Hanno Beckercab87e62019-04-29 13:52:53 +01002714 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker1f10d762019-04-26 13:34:37 +01002715
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002716 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002717 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
Hanno Beckercab87e62019-04-29 13:52:53 +01002718 add_data_len );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002719
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002720 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
Hanno Beckercab87e62019-04-29 13:52:53 +01002721 add_data_len );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002722 mbedtls_md_hmac_update( &transform->md_ctx_enc,
2723 data, rec->data_len );
2724 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
2725 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002726
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002727 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002728
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002729 rec->data_len += transform->maclen;
2730 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002731 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002732 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002733#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00002734 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002735 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002736#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002737 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002738 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002739 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2740 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002741 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002742
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002743 /* Make extra sure authentication was performed, exactly once */
2744 if( auth_done != 1 )
2745 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002746 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2747 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002748 }
2749
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002750 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002751
2752 return( 0 );
2753}
2754
Hanno Becker605949f2019-07-12 08:23:59 +01002755int mbedtls_ssl_decrypt_buf( mbedtls_ssl_context const *ssl,
Hanno Beckera18d1322018-01-03 14:27:32 +00002756 mbedtls_ssl_transform *transform,
2757 mbedtls_record *rec )
Paul Bakker5121ce52009-01-03 21:22:43 +00002758{
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002759 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002760 mbedtls_cipher_mode_t mode;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002761 int ret, auth_done = 0;
Hanno Becker52344c22018-01-03 15:24:20 +00002762#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Paul Bakker1e5369c2013-12-19 16:40:57 +01002763 size_t padlen = 0, correct = 1;
2764#endif
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002765 unsigned char* data;
Hanno Becker92fb4fa2019-05-20 14:54:26 +01002766 unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_IN_LEN_MAX ];
Hanno Beckercab87e62019-04-29 13:52:53 +01002767 size_t add_data_len;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002768
Hanno Beckera18d1322018-01-03 14:27:32 +00002769#if !defined(MBEDTLS_DEBUG_C)
Manuel Pégourié-Gonnarda7505d12019-05-07 10:17:56 +02002770 ssl = NULL; /* make sure we don't use it except for debug */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002771 ((void) ssl);
2772#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002773
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002774 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002775 if( rec == NULL ||
2776 rec->buf == NULL ||
2777 rec->buf_len < rec->data_offset ||
2778 rec->buf_len - rec->data_offset < rec->data_len )
2779 {
2780 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to decrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002781 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002782 }
2783
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002784 data = rec->buf + rec->data_offset;
2785 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_dec );
Paul Bakker5121ce52009-01-03 21:22:43 +00002786
Hanno Beckera0e20d02019-05-15 14:03:01 +01002787#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckercab87e62019-04-29 13:52:53 +01002788 /*
2789 * Match record's CID with incoming CID.
2790 */
Hanno Becker938489a2019-05-08 13:02:22 +01002791 if( rec->cid_len != transform->in_cid_len ||
2792 memcmp( rec->cid, transform->in_cid, rec->cid_len ) != 0 )
2793 {
Hanno Becker8367ccc2019-05-14 11:30:10 +01002794 return( MBEDTLS_ERR_SSL_UNEXPECTED_CID );
Hanno Becker938489a2019-05-08 13:02:22 +01002795 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01002796#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckercab87e62019-04-29 13:52:53 +01002797
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002798#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
2799 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01002800 {
2801 padlen = 0;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002802 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
2803 transform->iv_dec,
2804 transform->ivlen,
2805 data, rec->data_len,
2806 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002807 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002808 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002809 return( ret );
2810 }
2811
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002812 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002813 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002814 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2815 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002816 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002817 }
Paul Bakker68884e32013-01-07 18:20:04 +01002818 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002819#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002820#if defined(MBEDTLS_GCM_C) || \
2821 defined(MBEDTLS_CCM_C) || \
2822 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002823 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002824 mode == MBEDTLS_MODE_CCM ||
2825 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002826 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002827 unsigned char iv[12];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002828 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002829
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002830 /*
Hanno Beckerd96a6522019-07-10 13:55:25 +01002831 * Prepare IV from explicit and implicit data.
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002832 */
Hanno Beckerd96a6522019-07-10 13:55:25 +01002833
2834 /* Check that there's enough space for the explicit IV
2835 * (at the beginning of the record) and the MAC (at the
2836 * end of the record). */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002837 if( rec->data_len < explicit_iv_len + transform->taglen )
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002838 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002839 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002840 "+ taglen (%d)", rec->data_len,
2841 explicit_iv_len, transform->taglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002842 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002843 }
Paul Bakker68884e32013-01-07 18:20:04 +01002844
Hanno Beckerd96a6522019-07-10 13:55:25 +01002845#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CCM_C)
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002846 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
2847 {
Hanno Beckerd96a6522019-07-10 13:55:25 +01002848 /* GCM and CCM: fixed || explicit */
Paul Bakker68884e32013-01-07 18:20:04 +01002849
Hanno Beckerd96a6522019-07-10 13:55:25 +01002850 /* Fixed */
2851 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
2852 /* Explicit */
2853 memcpy( iv + transform->fixed_ivlen, data, 8 );
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002854 }
Hanno Beckerd96a6522019-07-10 13:55:25 +01002855 else
2856#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2857#if defined(MBEDTLS_CHACHAPOLY_C)
2858 if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002859 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002860 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002861 unsigned char i;
2862
2863 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
2864
2865 for( i = 0; i < 8; i++ )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002866 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002867 }
2868 else
Hanno Beckerd96a6522019-07-10 13:55:25 +01002869#endif /* MBEDTLS_CHACHAPOLY_C */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002870 {
2871 /* Reminder if we ever add an AEAD mode with a different size */
2872 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2873 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2874 }
2875
Hanno Beckerd96a6522019-07-10 13:55:25 +01002876 /* Group changes to data, data_len, and add_data, because
2877 * add_data depends on data_len. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002878 data += explicit_iv_len;
2879 rec->data_offset += explicit_iv_len;
2880 rec->data_len -= explicit_iv_len + transform->taglen;
2881
Hanno Beckercab87e62019-04-29 13:52:53 +01002882 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002883 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Hanno Beckercab87e62019-04-29 13:52:53 +01002884 add_data, add_data_len );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002885
Hanno Beckerd96a6522019-07-10 13:55:25 +01002886 /* Because of the check above, we know that there are
2887 * explicit_iv_len Bytes preceeding data, and taglen
2888 * bytes following data + data_len. This justifies
Hanno Becker20016652019-07-10 11:44:13 +01002889 * the debug message and the invocation of
Hanno Beckerd96a6522019-07-10 13:55:25 +01002890 * mbedtls_cipher_auth_decrypt() below. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002891
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002892 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used", iv, transform->ivlen );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002893 MBEDTLS_SSL_DEBUG_BUF( 4, "TAG used", data + rec->data_len,
Hanno Beckere694c3e2017-12-27 21:34:08 +00002894 transform->taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01002895
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002896 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002897 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002898 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002899 if( ( ret = mbedtls_cipher_auth_decrypt( &transform->cipher_ctx_dec,
2900 iv, transform->ivlen,
Hanno Beckercab87e62019-04-29 13:52:53 +01002901 add_data, add_data_len,
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002902 data, rec->data_len,
2903 data, &olen,
2904 data + rec->data_len,
2905 transform->taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002906 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002907 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_decrypt", ret );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002908
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002909 if( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED )
2910 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002911
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002912 return( ret );
2913 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002914 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002915
Hanno Beckerd96a6522019-07-10 13:55:25 +01002916 /* Double-check that AEAD decryption doesn't change content length. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002917 if( olen != rec->data_len )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002918 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002919 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2920 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002921 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00002922 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002923 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002924#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2925#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002926 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002927 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00002928 {
Paul Bakkere47b34b2013-02-27 14:48:00 +01002929 size_t minlen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002930
Paul Bakker5121ce52009-01-03 21:22:43 +00002931 /*
Paul Bakker45829992013-01-03 14:52:21 +01002932 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00002933 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002934#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002935 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
2936 {
2937 /* The ciphertext is prefixed with the CBC IV. */
2938 minlen += transform->ivlen;
2939 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002940#endif
Paul Bakker45829992013-01-03 14:52:21 +01002941
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002942 /* Size considerations:
2943 *
2944 * - The CBC cipher text must not be empty and hence
2945 * at least of size transform->ivlen.
2946 *
2947 * Together with the potential IV-prefix, this explains
2948 * the first of the two checks below.
2949 *
2950 * - The record must contain a MAC, either in plain or
2951 * encrypted, depending on whether Encrypt-then-MAC
2952 * is used or not.
2953 * - If it is, the message contains the IV-prefix,
2954 * the CBC ciphertext, and the MAC.
2955 * - If it is not, the padded plaintext, and hence
2956 * the CBC ciphertext, has at least length maclen + 1
2957 * because there is at least the padding length byte.
2958 *
2959 * As the CBC ciphertext is not empty, both cases give the
2960 * lower bound minlen + maclen + 1 on the record size, which
2961 * we test for in the second check below.
2962 */
2963 if( rec->data_len < minlen + transform->ivlen ||
2964 rec->data_len < minlen + transform->maclen + 1 )
Paul Bakker45829992013-01-03 14:52:21 +01002965 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002966 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002967 "+ 1 ) ( + expl IV )", rec->data_len,
2968 transform->ivlen,
2969 transform->maclen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002970 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker45829992013-01-03 14:52:21 +01002971 }
2972
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002973 /*
2974 * Authenticate before decrypt if enabled
2975 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002976#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002977 if( transform->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002978 {
Hanno Becker992b6872017-11-09 18:57:39 +00002979 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002980
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002981 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002982
Hanno Beckerd96a6522019-07-10 13:55:25 +01002983 /* Update data_len in tandem with add_data.
2984 *
2985 * The subtraction is safe because of the previous check
2986 * data_len >= minlen + maclen + 1.
2987 *
2988 * Afterwards, we know that data + data_len is followed by at
2989 * least maclen Bytes, which justifies the call to
2990 * mbedtls_ssl_safer_memcmp() below.
2991 *
2992 * Further, we still know that data_len > minlen */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002993 rec->data_len -= transform->maclen;
Hanno Beckercab87e62019-04-29 13:52:53 +01002994 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002995
Hanno Beckerd96a6522019-07-10 13:55:25 +01002996 /* Calculate expected MAC. */
Hanno Beckercab87e62019-04-29 13:52:53 +01002997 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
2998 add_data_len );
2999 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data,
3000 add_data_len );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003001 mbedtls_md_hmac_update( &transform->md_ctx_dec,
3002 data, rec->data_len );
3003 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
3004 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01003005
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003006 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len,
3007 transform->maclen );
Hanno Becker992b6872017-11-09 18:57:39 +00003008 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect,
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003009 transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003010
Hanno Beckerd96a6522019-07-10 13:55:25 +01003011 /* Compare expected MAC with MAC at the end of the record. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003012 if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect,
3013 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003014 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003015 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003016 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003017 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003018 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003019 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003020#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003021
3022 /*
3023 * Check length sanity
3024 */
Hanno Beckerd96a6522019-07-10 13:55:25 +01003025
3026 /* We know from above that data_len > minlen >= 0,
3027 * so the following check in particular implies that
3028 * data_len >= minlen + ivlen ( = minlen or 2 * minlen ). */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003029 if( rec->data_len % transform->ivlen != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003030 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003031 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003032 rec->data_len, transform->ivlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003033 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003034 }
3035
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003036#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003037 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00003038 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003039 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003040 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003041 {
Hanno Beckerd96a6522019-07-10 13:55:25 +01003042 /* Safe because data_len >= minlen + ivlen = 2 * ivlen. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003043 memcpy( transform->iv_dec, data, transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003044
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003045 data += transform->ivlen;
3046 rec->data_offset += transform->ivlen;
3047 rec->data_len -= transform->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003048 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003049#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00003050
Hanno Beckerd96a6522019-07-10 13:55:25 +01003051 /* We still have data_len % ivlen == 0 and data_len >= ivlen here. */
3052
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003053 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
3054 transform->iv_dec, transform->ivlen,
3055 data, rec->data_len, data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02003056 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003057 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02003058 return( ret );
3059 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02003060
Hanno Beckerd96a6522019-07-10 13:55:25 +01003061 /* Double-check that length hasn't changed during decryption. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003062 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02003063 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003064 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3065 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02003066 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02003067
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003068#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003069 if( transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02003070 {
3071 /*
Hanno Beckerd96a6522019-07-10 13:55:25 +01003072 * Save IV in SSL3 and TLS1, where CBC decryption of consecutive
3073 * records is equivalent to CBC decryption of the concatenation
3074 * of the records; in other words, IVs are maintained across
3075 * record decryptions.
Paul Bakkercca5b812013-08-31 17:40:26 +02003076 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003077 memcpy( transform->iv_dec, transform->cipher_ctx_dec.iv,
3078 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00003079 }
Paul Bakkercca5b812013-08-31 17:40:26 +02003080#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003081
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003082 /* Safe since data_len >= minlen + maclen + 1, so after having
3083 * subtracted at most minlen and maclen up to this point,
Hanno Beckerd96a6522019-07-10 13:55:25 +01003084 * data_len > 0 (because of data_len % ivlen == 0, it's actually
3085 * >= ivlen ). */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003086 padlen = data[rec->data_len - 1];
Paul Bakker45829992013-01-03 14:52:21 +01003087
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003088 if( auth_done == 1 )
3089 {
3090 correct *= ( rec->data_len >= padlen + 1 );
3091 padlen *= ( rec->data_len >= padlen + 1 );
3092 }
3093 else
Paul Bakker45829992013-01-03 14:52:21 +01003094 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003095#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003096 if( rec->data_len < transform->maclen + padlen + 1 )
3097 {
3098 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
3099 rec->data_len,
3100 transform->maclen,
3101 padlen + 1 ) );
3102 }
Paul Bakkerd66f0702013-01-31 16:57:45 +01003103#endif
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003104
3105 correct *= ( rec->data_len >= transform->maclen + padlen + 1 );
3106 padlen *= ( rec->data_len >= transform->maclen + padlen + 1 );
Paul Bakker45829992013-01-03 14:52:21 +01003107 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003108
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003109 padlen++;
3110
3111 /* Regardless of the validity of the padding,
3112 * we have data_len >= padlen here. */
3113
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003114#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003115 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003116 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003117 if( padlen > transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00003118 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003119#if defined(MBEDTLS_SSL_DEBUG_ALL)
3120 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003121 "should be no more than %d",
3122 padlen, transform->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01003123#endif
Paul Bakker45829992013-01-03 14:52:21 +01003124 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00003125 }
3126 }
3127 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003128#endif /* MBEDTLS_SSL_PROTO_SSL3 */
3129#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
3130 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003131 if( transform->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00003132 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003133 /* The padding check involves a series of up to 256
3134 * consecutive memory reads at the end of the record
3135 * plaintext buffer. In order to hide the length and
3136 * validity of the padding, always perform exactly
3137 * `min(256,plaintext_len)` reads (but take into account
3138 * only the last `padlen` bytes for the padding check). */
3139 size_t pad_count = 0;
3140 size_t real_count = 0;
3141 volatile unsigned char* const check = data;
Paul Bakkere47b34b2013-02-27 14:48:00 +01003142
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003143 /* Index of first padding byte; it has been ensured above
3144 * that the subtraction is safe. */
3145 size_t const padding_idx = rec->data_len - padlen;
3146 size_t const num_checks = rec->data_len <= 256 ? rec->data_len : 256;
3147 size_t const start_idx = rec->data_len - num_checks;
3148 size_t idx;
Paul Bakker956c9e02013-12-19 14:42:28 +01003149
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003150 for( idx = start_idx; idx < rec->data_len; idx++ )
Paul Bakkerca9c87e2013-09-25 18:52:37 +02003151 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003152 real_count |= ( idx >= padding_idx );
3153 pad_count += real_count * ( check[idx] == padlen - 1 );
Paul Bakkerca9c87e2013-09-25 18:52:37 +02003154 }
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003155 correct &= ( pad_count == padlen );
Paul Bakkere47b34b2013-02-27 14:48:00 +01003156
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003157#if defined(MBEDTLS_SSL_DEBUG_ALL)
Paul Bakker66d5d072014-06-17 16:39:18 +02003158 if( padlen > 0 && correct == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003159 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01003160#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01003161 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00003162 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02003163 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003164#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
3165 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02003166 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003167 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3168 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02003169 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003170
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003171 /* If the padding was found to be invalid, padlen == 0
3172 * and the subtraction is safe. If the padding was found valid,
3173 * padlen hasn't been changed and the previous assertion
3174 * data_len >= padlen still holds. */
3175 rec->data_len -= padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00003176 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02003177 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003178#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00003179 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02003180 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003181 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3182 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02003183 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003184
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02003185#if defined(MBEDTLS_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003186 MBEDTLS_SSL_DEBUG_BUF( 4, "raw buffer after decryption",
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003187 data, rec->data_len );
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02003188#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003189
3190 /*
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01003191 * Authenticate if not done yet.
3192 * Compute the MAC regardless of the padding result (RFC4346, CBCTIME).
Paul Bakker5121ce52009-01-03 21:22:43 +00003193 */
Hanno Becker52344c22018-01-03 15:24:20 +00003194#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003195 if( auth_done == 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003196 {
Hanno Becker992b6872017-11-09 18:57:39 +00003197 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Paul Bakker1e5369c2013-12-19 16:40:57 +01003198
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003199 /* If the initial value of padlen was such that
3200 * data_len < maclen + padlen + 1, then padlen
3201 * got reset to 1, and the initial check
3202 * data_len >= minlen + maclen + 1
3203 * guarantees that at this point we still
3204 * have at least data_len >= maclen.
3205 *
3206 * If the initial value of padlen was such that
3207 * data_len >= maclen + padlen + 1, then we have
3208 * subtracted either padlen + 1 (if the padding was correct)
3209 * or 0 (if the padding was incorrect) since then,
3210 * hence data_len >= maclen in any case.
3211 */
3212 rec->data_len -= transform->maclen;
Hanno Beckercab87e62019-04-29 13:52:53 +01003213 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Paul Bakker5121ce52009-01-03 21:22:43 +00003214
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003215#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003216 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003217 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003218 ssl_mac( &transform->md_ctx_dec,
3219 transform->mac_dec,
3220 data, rec->data_len,
3221 rec->ctr, rec->type,
3222 mac_expect );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003223 }
3224 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003225#endif /* MBEDTLS_SSL_PROTO_SSL3 */
3226#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
3227 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003228 if( transform->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003229 {
3230 /*
3231 * Process MAC and always update for padlen afterwards to make
Gilles Peskine20b44082018-05-29 14:06:49 +02003232 * total time independent of padlen.
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003233 *
3234 * Known timing attacks:
3235 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
3236 *
Gilles Peskine20b44082018-05-29 14:06:49 +02003237 * To compensate for different timings for the MAC calculation
3238 * depending on how much padding was removed (which is determined
3239 * by padlen), process extra_run more blocks through the hash
3240 * function.
3241 *
3242 * The formula in the paper is
3243 * extra_run = ceil( (L1-55) / 64 ) - ceil( (L2-55) / 64 )
3244 * where L1 is the size of the header plus the decrypted message
3245 * plus CBC padding and L2 is the size of the header plus the
3246 * decrypted message. This is for an underlying hash function
3247 * with 64-byte blocks.
3248 * We use ( (Lx+8) / 64 ) to handle 'negative Lx' values
3249 * correctly. We round down instead of up, so -56 is the correct
3250 * value for our calculations instead of -55.
3251 *
Gilles Peskine1bd9d582018-06-04 11:58:44 +02003252 * Repeat the formula rather than defining a block_size variable.
3253 * This avoids requiring division by a variable at runtime
3254 * (which would be marginally less efficient and would require
3255 * linking an extra division function in some builds).
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003256 */
3257 size_t j, extra_run = 0;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003258 unsigned char tmp[MBEDTLS_MD_MAX_BLOCK_SIZE];
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003259
3260 /*
3261 * The next two sizes are the minimum and maximum values of
3262 * in_msglen over all padlen values.
3263 *
3264 * They're independent of padlen, since we previously did
Hanno Beckerd96a6522019-07-10 13:55:25 +01003265 * data_len -= padlen.
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003266 *
3267 * Note that max_len + maclen is never more than the buffer
3268 * length, as we previously did in_msglen -= maclen too.
3269 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003270 const size_t max_len = rec->data_len + padlen;
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003271 const size_t min_len = ( max_len > 256 ) ? max_len - 256 : 0;
3272
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003273 memset( tmp, 0, sizeof( tmp ) );
3274
3275 switch( mbedtls_md_get_type( transform->md_ctx_dec.md_info ) )
Gilles Peskine20b44082018-05-29 14:06:49 +02003276 {
Gilles Peskined0e55a42018-06-04 12:03:30 +02003277#if defined(MBEDTLS_MD5_C) || defined(MBEDTLS_SHA1_C) || \
3278 defined(MBEDTLS_SHA256_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02003279 case MBEDTLS_MD_MD5:
3280 case MBEDTLS_MD_SHA1:
Gilles Peskine20b44082018-05-29 14:06:49 +02003281 case MBEDTLS_MD_SHA256:
Gilles Peskine20b44082018-05-29 14:06:49 +02003282 /* 8 bytes of message size, 64-byte compression blocks */
Hanno Beckercab87e62019-04-29 13:52:53 +01003283 extra_run =
3284 ( add_data_len + rec->data_len + padlen + 8 ) / 64 -
3285 ( add_data_len + rec->data_len + 8 ) / 64;
Gilles Peskine20b44082018-05-29 14:06:49 +02003286 break;
3287#endif
Gilles Peskinea7fe25d2018-06-04 12:01:18 +02003288#if defined(MBEDTLS_SHA512_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02003289 case MBEDTLS_MD_SHA384:
Gilles Peskine20b44082018-05-29 14:06:49 +02003290 /* 16 bytes of message size, 128-byte compression blocks */
Hanno Beckercab87e62019-04-29 13:52:53 +01003291 extra_run =
3292 ( add_data_len + rec->data_len + padlen + 16 ) / 128 -
3293 ( add_data_len + rec->data_len + 16 ) / 128;
Gilles Peskine20b44082018-05-29 14:06:49 +02003294 break;
3295#endif
3296 default:
Gilles Peskine5c389842018-06-04 12:02:43 +02003297 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Gilles Peskine20b44082018-05-29 14:06:49 +02003298 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3299 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01003300
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003301 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01003302
Hanno Beckercab87e62019-04-29 13:52:53 +01003303 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data,
3304 add_data_len );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003305 mbedtls_md_hmac_update( &transform->md_ctx_dec, data,
3306 rec->data_len );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003307 /* Make sure we access everything even when padlen > 0. This
3308 * makes the synchronisation requirements for just-in-time
3309 * Prime+Probe attacks much tighter and hopefully impractical. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003310 ssl_read_memory( data + rec->data_len, padlen );
3311 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003312
3313 /* Call mbedtls_md_process at least once due to cache attacks
3314 * that observe whether md_process() was called of not */
Manuel Pégourié-Gonnard47fede02015-04-29 01:35:48 +02003315 for( j = 0; j < extra_run + 1; j++ )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003316 mbedtls_md_process( &transform->md_ctx_dec, tmp );
Paul Bakkere47b34b2013-02-27 14:48:00 +01003317
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003318 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003319
3320 /* Make sure we access all the memory that could contain the MAC,
3321 * before we check it in the next code block. This makes the
3322 * synchronisation requirements for just-in-time Prime+Probe
3323 * attacks much tighter and hopefully impractical. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003324 ssl_read_memory( data + min_len,
3325 max_len - min_len + transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003326 }
3327 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003328#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
3329 MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003330 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003331 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3332 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003333 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003334
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003335#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003336 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect, transform->maclen );
3337 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len, transform->maclen );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003338#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003339
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003340 if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect,
3341 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003342 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003343#if defined(MBEDTLS_SSL_DEBUG_ALL)
3344 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01003345#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003346 correct = 0;
3347 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003348 auth_done++;
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003349 }
Hanno Beckerdd3ab132018-10-17 14:43:14 +01003350
3351 /*
3352 * Finally check the correct flag
3353 */
3354 if( correct == 0 )
3355 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Hanno Becker52344c22018-01-03 15:24:20 +00003356#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003357
3358 /* Make extra sure authentication was performed, exactly once */
3359 if( auth_done != 1 )
3360 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003361 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3362 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003363 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003364
Hanno Beckera0e20d02019-05-15 14:03:01 +01003365#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01003366 if( rec->cid_len != 0 )
3367 {
3368 ret = ssl_cid_parse_inner_plaintext( data, &rec->data_len,
3369 &rec->type );
3370 if( ret != 0 )
3371 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3372 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01003373#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01003374
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003375 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003376
3377 return( 0 );
3378}
3379
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01003380#undef MAC_NONE
3381#undef MAC_PLAINTEXT
3382#undef MAC_CIPHERTEXT
3383
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003384#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00003385/*
3386 * Compression/decompression functions
3387 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003388static int ssl_compress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003389{
3390 int ret;
3391 unsigned char *msg_post = ssl->out_msg;
Andrzej Kurek5462e022018-04-20 07:58:53 -04003392 ptrdiff_t bytes_written = ssl->out_msg - ssl->out_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003393 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02003394 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003395
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003396 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003397
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02003398 if( len_pre == 0 )
3399 return( 0 );
3400
Paul Bakker2770fbd2012-07-03 13:30:23 +00003401 memcpy( msg_pre, ssl->out_msg, len_pre );
3402
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003403 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003404 ssl->out_msglen ) );
3405
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003406 MBEDTLS_SSL_DEBUG_BUF( 4, "before compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003407 ssl->out_msg, ssl->out_msglen );
3408
Paul Bakker48916f92012-09-16 19:57:18 +00003409 ssl->transform_out->ctx_deflate.next_in = msg_pre;
3410 ssl->transform_out->ctx_deflate.avail_in = len_pre;
3411 ssl->transform_out->ctx_deflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10003412 ssl->transform_out->ctx_deflate.avail_out = MBEDTLS_SSL_OUT_BUFFER_LEN - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003413
Paul Bakker48916f92012-09-16 19:57:18 +00003414 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003415 if( ret != Z_OK )
3416 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003417 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
3418 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003419 }
3420
Angus Grattond8213d02016-05-25 20:56:48 +10003421 ssl->out_msglen = MBEDTLS_SSL_OUT_BUFFER_LEN -
Andrzej Kurek5462e022018-04-20 07:58:53 -04003422 ssl->transform_out->ctx_deflate.avail_out - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003423
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003424 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003425 ssl->out_msglen ) );
3426
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003427 MBEDTLS_SSL_DEBUG_BUF( 4, "after compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003428 ssl->out_msg, ssl->out_msglen );
3429
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003430 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003431
3432 return( 0 );
3433}
3434
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003435static int ssl_decompress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003436{
3437 int ret;
3438 unsigned char *msg_post = ssl->in_msg;
Andrzej Kureka9ceef82018-04-24 06:32:44 -04003439 ptrdiff_t header_bytes = ssl->in_msg - ssl->in_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003440 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02003441 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003442
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003443 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003444
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02003445 if( len_pre == 0 )
3446 return( 0 );
3447
Paul Bakker2770fbd2012-07-03 13:30:23 +00003448 memcpy( msg_pre, ssl->in_msg, len_pre );
3449
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003450 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003451 ssl->in_msglen ) );
3452
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003453 MBEDTLS_SSL_DEBUG_BUF( 4, "before decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003454 ssl->in_msg, ssl->in_msglen );
3455
Paul Bakker48916f92012-09-16 19:57:18 +00003456 ssl->transform_in->ctx_inflate.next_in = msg_pre;
3457 ssl->transform_in->ctx_inflate.avail_in = len_pre;
3458 ssl->transform_in->ctx_inflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10003459 ssl->transform_in->ctx_inflate.avail_out = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04003460 header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003461
Paul Bakker48916f92012-09-16 19:57:18 +00003462 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003463 if( ret != Z_OK )
3464 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003465 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
3466 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003467 }
3468
Angus Grattond8213d02016-05-25 20:56:48 +10003469 ssl->in_msglen = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04003470 ssl->transform_in->ctx_inflate.avail_out - header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003471
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003472 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003473 ssl->in_msglen ) );
3474
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003475 MBEDTLS_SSL_DEBUG_BUF( 4, "after decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003476 ssl->in_msg, ssl->in_msglen );
3477
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003478 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003479
3480 return( 0 );
3481}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003482#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003483
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003484#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
3485static int ssl_write_hello_request( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003486
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003487#if defined(MBEDTLS_SSL_PROTO_DTLS)
3488static int ssl_resend_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003489{
3490 /* If renegotiation is not enforced, retransmit until we would reach max
3491 * timeout if we were using the usual handshake doubling scheme */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003492 if( ssl->conf->renego_max_records < 0 )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003493 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003494 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003495 unsigned char doublings = 1;
3496
3497 while( ratio != 0 )
3498 {
3499 ++doublings;
3500 ratio >>= 1;
3501 }
3502
3503 if( ++ssl->renego_records_seen > doublings )
3504 {
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02003505 MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003506 return( 0 );
3507 }
3508 }
3509
3510 return( ssl_write_hello_request( ssl ) );
3511}
3512#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003513#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003514
Paul Bakker5121ce52009-01-03 21:22:43 +00003515/*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003516 * Fill the input message buffer by appending data to it.
3517 * The amount of data already fetched is in ssl->in_left.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003518 *
3519 * If we return 0, is it guaranteed that (at least) nb_want bytes are
3520 * available (from this read and/or a previous one). Otherwise, an error code
3521 * is returned (possibly EOF or WANT_READ).
3522 *
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003523 * With stream transport (TLS) on success ssl->in_left == nb_want, but
3524 * with datagram transport (DTLS) on success ssl->in_left >= nb_want,
3525 * since we always read a whole datagram at once.
3526 *
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02003527 * For DTLS, it is up to the caller to set ssl->next_record_offset when
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003528 * they're done reading a record.
Paul Bakker5121ce52009-01-03 21:22:43 +00003529 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003530int mbedtls_ssl_fetch_input( mbedtls_ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00003531{
Paul Bakker23986e52011-04-24 08:57:21 +00003532 int ret;
3533 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00003534
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003535 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003536
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003537 if( ssl->f_recv == NULL && ssl->f_recv_timeout == NULL )
3538 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003539 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01003540 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003541 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003542 }
3543
Angus Grattond8213d02016-05-25 20:56:48 +10003544 if( nb_want > MBEDTLS_SSL_IN_BUFFER_LEN - (size_t)( ssl->in_hdr - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003545 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003546 MBEDTLS_SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
3547 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003548 }
3549
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003550#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003551 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00003552 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003553 uint32_t timeout;
3554
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02003555 /* Just to be sure */
3556 if( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL )
3557 {
3558 MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
3559 "mbedtls_ssl_set_timer_cb() for DTLS" ) );
3560 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3561 }
3562
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003563 /*
3564 * The point is, we need to always read a full datagram at once, so we
3565 * sometimes read more then requested, and handle the additional data.
3566 * It could be the rest of the current record (while fetching the
3567 * header) and/or some other records in the same datagram.
3568 */
3569
3570 /*
3571 * Move to the next record in the already read datagram if applicable
3572 */
3573 if( ssl->next_record_offset != 0 )
3574 {
3575 if( ssl->in_left < ssl->next_record_offset )
3576 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003577 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3578 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003579 }
3580
3581 ssl->in_left -= ssl->next_record_offset;
3582
3583 if( ssl->in_left != 0 )
3584 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003585 MBEDTLS_SSL_DEBUG_MSG( 2, ( "next record in same datagram, offset: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003586 ssl->next_record_offset ) );
3587 memmove( ssl->in_hdr,
3588 ssl->in_hdr + ssl->next_record_offset,
3589 ssl->in_left );
3590 }
3591
3592 ssl->next_record_offset = 0;
3593 }
3594
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003595 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Paul Bakker5121ce52009-01-03 21:22:43 +00003596 ssl->in_left, nb_want ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003597
3598 /*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003599 * Done if we already have enough data.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003600 */
3601 if( nb_want <= ssl->in_left)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003602 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003603 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003604 return( 0 );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003605 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003606
3607 /*
Antonin Décimo36e89b52019-01-23 15:24:37 +01003608 * A record can't be split across datagrams. If we need to read but
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003609 * are not at the beginning of a new record, the caller did something
3610 * wrong.
3611 */
3612 if( ssl->in_left != 0 )
3613 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003614 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3615 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003616 }
3617
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003618 /*
3619 * Don't even try to read if time's out already.
3620 * This avoids by-passing the timer when repeatedly receiving messages
3621 * that will end up being dropped.
3622 */
3623 if( ssl_check_timer( ssl ) != 0 )
Hanno Beckere65ce782017-05-22 14:47:48 +01003624 {
3625 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timer has expired" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003626 ret = MBEDTLS_ERR_SSL_TIMEOUT;
Hanno Beckere65ce782017-05-22 14:47:48 +01003627 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003628 else
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003629 {
Angus Grattond8213d02016-05-25 20:56:48 +10003630 len = MBEDTLS_SSL_IN_BUFFER_LEN - ( ssl->in_hdr - ssl->in_buf );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003631
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003632 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003633 timeout = ssl->handshake->retransmit_timeout;
3634 else
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003635 timeout = ssl->conf->read_timeout;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003636
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003637 MBEDTLS_SSL_DEBUG_MSG( 3, ( "f_recv_timeout: %u ms", timeout ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003638
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003639 if( ssl->f_recv_timeout != NULL )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003640 ret = ssl->f_recv_timeout( ssl->p_bio, ssl->in_hdr, len,
3641 timeout );
3642 else
3643 ret = ssl->f_recv( ssl->p_bio, ssl->in_hdr, len );
3644
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003645 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003646
3647 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003648 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003649 }
3650
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003651 if( ret == MBEDTLS_ERR_SSL_TIMEOUT )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003652 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003653 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timeout" ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003654 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003655
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003656 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003657 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003658 if( ssl_double_retransmit_timeout( ssl ) != 0 )
3659 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003660 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake timeout" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003661 return( MBEDTLS_ERR_SSL_TIMEOUT );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003662 }
3663
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003664 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003665 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003666 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003667 return( ret );
3668 }
3669
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003670 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003671 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003672#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003673 else if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003674 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003675 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003676 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003677 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003678 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003679 return( ret );
3680 }
3681
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003682 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003683 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003684#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003685 }
3686
Paul Bakker5121ce52009-01-03 21:22:43 +00003687 if( ret < 0 )
3688 return( ret );
3689
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003690 ssl->in_left = ret;
3691 }
3692 else
3693#endif
3694 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003695 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003696 ssl->in_left, nb_want ) );
3697
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003698 while( ssl->in_left < nb_want )
3699 {
3700 len = nb_want - ssl->in_left;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02003701
3702 if( ssl_check_timer( ssl ) != 0 )
3703 ret = MBEDTLS_ERR_SSL_TIMEOUT;
3704 else
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003705 {
3706 if( ssl->f_recv_timeout != NULL )
3707 {
3708 ret = ssl->f_recv_timeout( ssl->p_bio,
3709 ssl->in_hdr + ssl->in_left, len,
3710 ssl->conf->read_timeout );
3711 }
3712 else
3713 {
3714 ret = ssl->f_recv( ssl->p_bio,
3715 ssl->in_hdr + ssl->in_left, len );
3716 }
3717 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003718
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003719 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003720 ssl->in_left, nb_want ) );
3721 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003722
3723 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003724 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003725
3726 if( ret < 0 )
3727 return( ret );
3728
mohammad160352aecb92018-03-28 23:41:40 -07003729 if ( (size_t)ret > len || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16035bd15cb2018-02-28 04:30:59 -08003730 {
Darryl Green11999bb2018-03-13 15:22:58 +00003731 MBEDTLS_SSL_DEBUG_MSG( 1,
3732 ( "f_recv returned %d bytes but only %lu were requested",
mohammad160319d392b2018-04-02 07:25:26 -07003733 ret, (unsigned long)len ) );
mohammad16035bd15cb2018-02-28 04:30:59 -08003734 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3735 }
3736
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003737 ssl->in_left += ret;
3738 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003739 }
3740
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003741 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003742
3743 return( 0 );
3744}
3745
3746/*
3747 * Flush any data not yet written
3748 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003749int mbedtls_ssl_flush_output( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003750{
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01003751 int ret;
Hanno Becker04484622018-08-06 09:49:38 +01003752 unsigned char *buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00003753
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003754 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003755
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003756 if( ssl->f_send == NULL )
3757 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003758 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01003759 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003760 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003761 }
3762
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003763 /* Avoid incrementing counter if data is flushed */
3764 if( ssl->out_left == 0 )
3765 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003766 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003767 return( 0 );
3768 }
3769
Paul Bakker5121ce52009-01-03 21:22:43 +00003770 while( ssl->out_left > 0 )
3771 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003772 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
Hanno Becker5903de42019-05-03 14:46:38 +01003773 mbedtls_ssl_out_hdr_len( ssl ) + ssl->out_msglen, ssl->out_left ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003774
Hanno Becker2b1e3542018-08-06 11:19:13 +01003775 buf = ssl->out_hdr - ssl->out_left;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003776 ret = ssl->f_send( ssl->p_bio, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00003777
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003778 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003779
3780 if( ret <= 0 )
3781 return( ret );
3782
mohammad160352aecb92018-03-28 23:41:40 -07003783 if( (size_t)ret > ssl->out_left || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16034bbaeb42018-02-22 04:29:04 -08003784 {
Darryl Green11999bb2018-03-13 15:22:58 +00003785 MBEDTLS_SSL_DEBUG_MSG( 1,
3786 ( "f_send returned %d bytes but only %lu bytes were sent",
mohammad160319d392b2018-04-02 07:25:26 -07003787 ret, (unsigned long)ssl->out_left ) );
mohammad16034bbaeb42018-02-22 04:29:04 -08003788 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3789 }
3790
Paul Bakker5121ce52009-01-03 21:22:43 +00003791 ssl->out_left -= ret;
3792 }
3793
Hanno Becker2b1e3542018-08-06 11:19:13 +01003794#if defined(MBEDTLS_SSL_PROTO_DTLS)
3795 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003796 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01003797 ssl->out_hdr = ssl->out_buf;
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003798 }
Hanno Becker2b1e3542018-08-06 11:19:13 +01003799 else
3800#endif
3801 {
3802 ssl->out_hdr = ssl->out_buf + 8;
3803 }
3804 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003805
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003806 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003807
3808 return( 0 );
3809}
3810
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003811/*
3812 * Functions to handle the DTLS retransmission state machine
3813 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003814#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003815/*
3816 * Append current handshake message to current outgoing flight
3817 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003818static int ssl_flight_append( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003819{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003820 mbedtls_ssl_flight_item *msg;
Hanno Becker3b235902018-08-06 09:54:53 +01003821 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_flight_append" ) );
3822 MBEDTLS_SSL_DEBUG_BUF( 4, "message appended to flight",
3823 ssl->out_msg, ssl->out_msglen );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003824
3825 /* Allocate space for current message */
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003826 if( ( msg = mbedtls_calloc( 1, sizeof( mbedtls_ssl_flight_item ) ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003827 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003828 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003829 sizeof( mbedtls_ssl_flight_item ) ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003830 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003831 }
3832
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003833 if( ( msg->p = mbedtls_calloc( 1, ssl->out_msglen ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003834 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003835 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed", ssl->out_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003836 mbedtls_free( msg );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003837 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003838 }
3839
3840 /* Copy current handshake message with headers */
3841 memcpy( msg->p, ssl->out_msg, ssl->out_msglen );
3842 msg->len = ssl->out_msglen;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003843 msg->type = ssl->out_msgtype;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003844 msg->next = NULL;
3845
3846 /* Append to the current flight */
3847 if( ssl->handshake->flight == NULL )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003848 ssl->handshake->flight = msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003849 else
3850 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003851 mbedtls_ssl_flight_item *cur = ssl->handshake->flight;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003852 while( cur->next != NULL )
3853 cur = cur->next;
3854 cur->next = msg;
3855 }
3856
Hanno Becker3b235902018-08-06 09:54:53 +01003857 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_flight_append" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003858 return( 0 );
3859}
3860
3861/*
3862 * Free the current flight of handshake messages
3863 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003864static void ssl_flight_free( mbedtls_ssl_flight_item *flight )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003865{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003866 mbedtls_ssl_flight_item *cur = flight;
3867 mbedtls_ssl_flight_item *next;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003868
3869 while( cur != NULL )
3870 {
3871 next = cur->next;
3872
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003873 mbedtls_free( cur->p );
3874 mbedtls_free( cur );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003875
3876 cur = next;
3877 }
3878}
3879
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003880#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3881static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003882#endif
3883
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003884/*
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003885 * Swap transform_out and out_ctr with the alternative ones
3886 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003887static void ssl_swap_epochs( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003888{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003889 mbedtls_ssl_transform *tmp_transform;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003890 unsigned char tmp_out_ctr[8];
3891
3892 if( ssl->transform_out == ssl->handshake->alt_transform_out )
3893 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003894 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003895 return;
3896 }
3897
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003898 MBEDTLS_SSL_DEBUG_MSG( 3, ( "swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003899
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003900 /* Swap transforms */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003901 tmp_transform = ssl->transform_out;
3902 ssl->transform_out = ssl->handshake->alt_transform_out;
3903 ssl->handshake->alt_transform_out = tmp_transform;
3904
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003905 /* Swap epoch + sequence_number */
Hanno Becker19859472018-08-06 09:40:20 +01003906 memcpy( tmp_out_ctr, ssl->cur_out_ctr, 8 );
3907 memcpy( ssl->cur_out_ctr, ssl->handshake->alt_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003908 memcpy( ssl->handshake->alt_out_ctr, tmp_out_ctr, 8 );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003909
3910 /* Adjust to the newly activated transform */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01003911 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003912
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003913#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3914 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003915 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003916 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003917 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003918 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
3919 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003920 }
3921 }
3922#endif
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003923}
3924
3925/*
3926 * Retransmit the current flight of messages.
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003927 */
3928int mbedtls_ssl_resend( mbedtls_ssl_context *ssl )
3929{
3930 int ret = 0;
3931
3932 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_resend" ) );
3933
3934 ret = mbedtls_ssl_flight_transmit( ssl );
3935
3936 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_resend" ) );
3937
3938 return( ret );
3939}
3940
3941/*
3942 * Transmit or retransmit the current flight of messages.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003943 *
3944 * Need to remember the current message in case flush_output returns
3945 * WANT_WRITE, causing us to exit this function and come back later.
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003946 * This function must be called until state is no longer SENDING.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003947 */
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003948int mbedtls_ssl_flight_transmit( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003949{
Hanno Becker67bc7c32018-08-06 11:33:50 +01003950 int ret;
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003951 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003952
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003953 if( ssl->handshake->retransmit_state != MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003954 {
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003955 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialise flight transmission" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003956
3957 ssl->handshake->cur_msg = ssl->handshake->flight;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003958 ssl->handshake->cur_msg_p = ssl->handshake->flight->p + 12;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003959 ssl_swap_epochs( ssl );
3960
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003961 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_SENDING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003962 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003963
3964 while( ssl->handshake->cur_msg != NULL )
3965 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003966 size_t max_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003967 const mbedtls_ssl_flight_item * const cur = ssl->handshake->cur_msg;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003968
Hanno Beckere1dcb032018-08-17 16:47:58 +01003969 int const is_finished =
3970 ( cur->type == MBEDTLS_SSL_MSG_HANDSHAKE &&
3971 cur->p[0] == MBEDTLS_SSL_HS_FINISHED );
3972
Hanno Becker04da1892018-08-14 13:22:10 +01003973 uint8_t const force_flush = ssl->disable_datagram_packing == 1 ?
3974 SSL_FORCE_FLUSH : SSL_DONT_FORCE_FLUSH;
3975
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003976 /* Swap epochs before sending Finished: we can't do it after
3977 * sending ChangeCipherSpec, in case write returns WANT_READ.
3978 * Must be done before copying, may change out_msg pointer */
Hanno Beckere1dcb032018-08-17 16:47:58 +01003979 if( is_finished && ssl->handshake->cur_msg_p == ( cur->p + 12 ) )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003980 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003981 MBEDTLS_SSL_DEBUG_MSG( 2, ( "swap epochs to send finished message" ) );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003982 ssl_swap_epochs( ssl );
3983 }
3984
Hanno Becker67bc7c32018-08-06 11:33:50 +01003985 ret = ssl_get_remaining_payload_in_datagram( ssl );
3986 if( ret < 0 )
3987 return( ret );
3988 max_frag_len = (size_t) ret;
3989
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003990 /* CCS is copied as is, while HS messages may need fragmentation */
3991 if( cur->type == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
3992 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003993 if( max_frag_len == 0 )
3994 {
3995 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3996 return( ret );
3997
3998 continue;
3999 }
4000
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004001 memcpy( ssl->out_msg, cur->p, cur->len );
Hanno Becker67bc7c32018-08-06 11:33:50 +01004002 ssl->out_msglen = cur->len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004003 ssl->out_msgtype = cur->type;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004004
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004005 /* Update position inside current message */
4006 ssl->handshake->cur_msg_p += cur->len;
4007 }
4008 else
4009 {
4010 const unsigned char * const p = ssl->handshake->cur_msg_p;
4011 const size_t hs_len = cur->len - 12;
4012 const size_t frag_off = p - ( cur->p + 12 );
4013 const size_t rem_len = hs_len - frag_off;
Hanno Becker67bc7c32018-08-06 11:33:50 +01004014 size_t cur_hs_frag_len, max_hs_frag_len;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004015
Hanno Beckere1dcb032018-08-17 16:47:58 +01004016 if( ( max_frag_len < 12 ) || ( max_frag_len == 12 && hs_len != 0 ) )
Manuel Pégourié-Gonnarda1071a52018-08-20 11:56:14 +02004017 {
Hanno Beckere1dcb032018-08-17 16:47:58 +01004018 if( is_finished )
Hanno Becker67bc7c32018-08-06 11:33:50 +01004019 ssl_swap_epochs( ssl );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004020
Hanno Becker67bc7c32018-08-06 11:33:50 +01004021 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
4022 return( ret );
4023
4024 continue;
4025 }
4026 max_hs_frag_len = max_frag_len - 12;
4027
4028 cur_hs_frag_len = rem_len > max_hs_frag_len ?
4029 max_hs_frag_len : rem_len;
4030
4031 if( frag_off == 0 && cur_hs_frag_len != hs_len )
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02004032 {
4033 MBEDTLS_SSL_DEBUG_MSG( 2, ( "fragmenting handshake message (%u > %u)",
Hanno Becker67bc7c32018-08-06 11:33:50 +01004034 (unsigned) cur_hs_frag_len,
4035 (unsigned) max_hs_frag_len ) );
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02004036 }
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02004037
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004038 /* Messages are stored with handshake headers as if not fragmented,
4039 * copy beginning of headers then fill fragmentation fields.
4040 * Handshake headers: type(1) len(3) seq(2) f_off(3) f_len(3) */
4041 memcpy( ssl->out_msg, cur->p, 6 );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004042
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004043 ssl->out_msg[6] = ( ( frag_off >> 16 ) & 0xff );
4044 ssl->out_msg[7] = ( ( frag_off >> 8 ) & 0xff );
4045 ssl->out_msg[8] = ( ( frag_off ) & 0xff );
4046
Hanno Becker67bc7c32018-08-06 11:33:50 +01004047 ssl->out_msg[ 9] = ( ( cur_hs_frag_len >> 16 ) & 0xff );
4048 ssl->out_msg[10] = ( ( cur_hs_frag_len >> 8 ) & 0xff );
4049 ssl->out_msg[11] = ( ( cur_hs_frag_len ) & 0xff );
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004050
4051 MBEDTLS_SSL_DEBUG_BUF( 3, "handshake header", ssl->out_msg, 12 );
4052
Hanno Becker3f7b9732018-08-28 09:53:25 +01004053 /* Copy the handshake message content and set records fields */
Hanno Becker67bc7c32018-08-06 11:33:50 +01004054 memcpy( ssl->out_msg + 12, p, cur_hs_frag_len );
4055 ssl->out_msglen = cur_hs_frag_len + 12;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004056 ssl->out_msgtype = cur->type;
4057
4058 /* Update position inside current message */
Hanno Becker67bc7c32018-08-06 11:33:50 +01004059 ssl->handshake->cur_msg_p += cur_hs_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004060 }
4061
4062 /* If done with the current message move to the next one if any */
4063 if( ssl->handshake->cur_msg_p >= cur->p + cur->len )
4064 {
4065 if( cur->next != NULL )
4066 {
4067 ssl->handshake->cur_msg = cur->next;
4068 ssl->handshake->cur_msg_p = cur->next->p + 12;
4069 }
4070 else
4071 {
4072 ssl->handshake->cur_msg = NULL;
4073 ssl->handshake->cur_msg_p = NULL;
4074 }
4075 }
4076
4077 /* Actually send the message out */
Hanno Becker04da1892018-08-14 13:22:10 +01004078 if( ( ret = mbedtls_ssl_write_record( ssl, force_flush ) ) != 0 )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004079 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004080 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004081 return( ret );
4082 }
4083 }
4084
Hanno Becker67bc7c32018-08-06 11:33:50 +01004085 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
4086 return( ret );
4087
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02004088 /* Update state and set timer */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004089 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
4090 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard23b7b702014-09-25 13:50:12 +02004091 else
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02004092 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004093 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02004094 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
4095 }
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004096
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004097 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004098
4099 return( 0 );
4100}
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004101
4102/*
4103 * To be called when the last message of an incoming flight is received.
4104 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004105void mbedtls_ssl_recv_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004106{
4107 /* We won't need to resend that one any more */
4108 ssl_flight_free( ssl->handshake->flight );
4109 ssl->handshake->flight = NULL;
4110 ssl->handshake->cur_msg = NULL;
4111
4112 /* The next incoming flight will start with this msg_seq */
4113 ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq;
4114
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004115 /* We don't want to remember CCS's across flight boundaries. */
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01004116 ssl->handshake->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01004117
Hanno Becker0271f962018-08-16 13:23:47 +01004118 /* Clear future message buffering structure. */
4119 ssl_buffering_free( ssl );
4120
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02004121 /* Cancel timer */
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004122 ssl_set_timer( ssl, 0 );
4123
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004124 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4125 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004126 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004127 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004128 }
4129 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004130 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02004131}
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004132
4133/*
4134 * To be called when the last message of an outgoing flight is send.
4135 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004136void mbedtls_ssl_send_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004137{
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02004138 ssl_reset_retransmit_timeout( ssl );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02004139 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004140
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004141 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4142 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004143 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004144 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004145 }
4146 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004147 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02004148}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004149#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004150
Paul Bakker5121ce52009-01-03 21:22:43 +00004151/*
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004152 * Handshake layer functions
Paul Bakker5121ce52009-01-03 21:22:43 +00004153 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004154
4155/*
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004156 * Write (DTLS: or queue) current handshake (including CCS) message.
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004157 *
4158 * - fill in handshake headers
4159 * - update handshake checksum
4160 * - DTLS: save message for resending
4161 * - then pass to the record layer
4162 *
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004163 * DTLS: except for HelloRequest, messages are only queued, and will only be
4164 * actually sent when calling flight_transmit() or resend().
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004165 *
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004166 * Inputs:
4167 * - ssl->out_msglen: 4 + actual handshake message len
4168 * (4 is the size of handshake headers for TLS)
4169 * - ssl->out_msg[0]: the handshake type (ClientHello, ServerHello, etc)
4170 * - ssl->out_msg + 4: the handshake message body
4171 *
Manuel Pégourié-Gonnard065a2a32018-08-20 11:09:26 +02004172 * Outputs, ie state before passing to flight_append() or write_record():
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004173 * - ssl->out_msglen: the length of the record contents
4174 * (including handshake headers but excluding record headers)
4175 * - ssl->out_msg: the record contents (handshake headers + content)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004176 */
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004177int mbedtls_ssl_write_handshake_msg( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004178{
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004179 int ret;
4180 const size_t hs_len = ssl->out_msglen - 4;
4181 const unsigned char hs_type = ssl->out_msg[0];
Paul Bakker5121ce52009-01-03 21:22:43 +00004182
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004183 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write handshake message" ) );
4184
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004185 /*
4186 * Sanity checks
4187 */
Hanno Beckerc83d2b32018-08-22 16:05:47 +01004188 if( ssl->out_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004189 ssl->out_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
4190 {
Hanno Beckerc83d2b32018-08-22 16:05:47 +01004191 /* In SSLv3, the client might send a NoCertificate alert. */
4192#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
4193 if( ! ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
4194 ssl->out_msgtype == MBEDTLS_SSL_MSG_ALERT &&
4195 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) )
4196#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
4197 {
4198 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4199 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4200 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004201 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004202
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05004203 /* Whenever we send anything different from a
4204 * HelloRequest we should be in a handshake - double check. */
4205 if( ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4206 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) &&
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004207 ssl->handshake == NULL )
4208 {
4209 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4210 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4211 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004212
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004213#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004214 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004215 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004216 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004217 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004218 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4219 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004220 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004221#endif
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004222
Hanno Beckerb50a2532018-08-06 11:52:54 +01004223 /* Double-check that we did not exceed the bounds
4224 * of the outgoing record buffer.
4225 * This should never fail as the various message
4226 * writing functions must obey the bounds of the
4227 * outgoing record buffer, but better be safe.
4228 *
4229 * Note: We deliberately do not check for the MTU or MFL here.
4230 */
4231 if( ssl->out_msglen > MBEDTLS_SSL_OUT_CONTENT_LEN )
4232 {
4233 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record too large: "
4234 "size %u, maximum %u",
4235 (unsigned) ssl->out_msglen,
4236 (unsigned) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
4237 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4238 }
4239
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004240 /*
4241 * Fill handshake headers
4242 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004243 if( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00004244 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004245 ssl->out_msg[1] = (unsigned char)( hs_len >> 16 );
4246 ssl->out_msg[2] = (unsigned char)( hs_len >> 8 );
4247 ssl->out_msg[3] = (unsigned char)( hs_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00004248
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004249 /*
4250 * DTLS has additional fields in the Handshake layer,
4251 * between the length field and the actual payload:
4252 * uint16 message_seq;
4253 * uint24 fragment_offset;
4254 * uint24 fragment_length;
4255 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004256#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004257 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004258 {
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01004259 /* Make room for the additional DTLS fields */
Angus Grattond8213d02016-05-25 20:56:48 +10004260 if( MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen < 8 )
Hanno Becker9648f8b2017-09-18 10:55:54 +01004261 {
4262 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS handshake message too large: "
4263 "size %u, maximum %u",
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004264 (unsigned) ( hs_len ),
Angus Grattond8213d02016-05-25 20:56:48 +10004265 (unsigned) ( MBEDTLS_SSL_OUT_CONTENT_LEN - 12 ) ) );
Hanno Becker9648f8b2017-09-18 10:55:54 +01004266 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4267 }
4268
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004269 memmove( ssl->out_msg + 12, ssl->out_msg + 4, hs_len );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004270 ssl->out_msglen += 8;
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004271
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02004272 /* Write message_seq and update it, except for HelloRequest */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004273 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02004274 {
Manuel Pégourié-Gonnardd9ba0d92014-09-02 18:30:26 +02004275 ssl->out_msg[4] = ( ssl->handshake->out_msg_seq >> 8 ) & 0xFF;
4276 ssl->out_msg[5] = ( ssl->handshake->out_msg_seq ) & 0xFF;
4277 ++( ssl->handshake->out_msg_seq );
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02004278 }
4279 else
4280 {
4281 ssl->out_msg[4] = 0;
4282 ssl->out_msg[5] = 0;
4283 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01004284
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004285 /* Handshake hashes are computed without fragmentation,
4286 * so set frag_offset = 0 and frag_len = hs_len for now */
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01004287 memset( ssl->out_msg + 6, 0x00, 3 );
4288 memcpy( ssl->out_msg + 9, ssl->out_msg + 1, 3 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004289 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004290#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004291
Hanno Becker0207e532018-08-28 10:28:28 +01004292 /* Update running hashes of handshake messages seen */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004293 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
4294 ssl->handshake->update_checksum( ssl, ssl->out_msg, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00004295 }
4296
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004297 /* Either send now, or just save to be sent (and resent) later */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004298#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004299 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05004300 ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4301 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004302 {
4303 if( ( ret = ssl_flight_append( ssl ) ) != 0 )
4304 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004305 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_flight_append", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004306 return( ret );
4307 }
4308 }
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004309 else
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004310#endif
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004311 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004312 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004313 {
4314 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4315 return( ret );
4316 }
4317 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004318
4319 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write handshake message" ) );
4320
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004321 return( 0 );
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004322}
4323
4324/*
4325 * Record layer functions
4326 */
4327
4328/*
4329 * Write current record.
4330 *
4331 * Uses:
4332 * - ssl->out_msgtype: type of the message (AppData, Handshake, Alert, CCS)
4333 * - ssl->out_msglen: length of the record content (excl headers)
4334 * - ssl->out_msg: record content
4335 */
Hanno Becker67bc7c32018-08-06 11:33:50 +01004336int mbedtls_ssl_write_record( mbedtls_ssl_context *ssl, uint8_t force_flush )
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004337{
4338 int ret, done = 0;
4339 size_t len = ssl->out_msglen;
Hanno Becker67bc7c32018-08-06 11:33:50 +01004340 uint8_t flush = force_flush;
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004341
4342 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write record" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004343
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004344#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00004345 if( ssl->transform_out != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004346 ssl->session_out->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00004347 {
4348 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
4349 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004350 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00004351 return( ret );
4352 }
4353
4354 len = ssl->out_msglen;
4355 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004356#endif /*MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00004357
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004358#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
4359 if( mbedtls_ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004360 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004361 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004362
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004363 ret = mbedtls_ssl_hw_record_write( ssl );
4364 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00004365 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004366 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_write", ret );
4367 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00004368 }
Paul Bakkerc7878112012-12-19 14:41:14 +01004369
4370 if( ret == 0 )
4371 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00004372 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004373#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00004374 if( !done )
4375 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01004376 unsigned i;
4377 size_t protected_record_size;
4378
Hanno Becker6430faf2019-05-08 11:57:13 +01004379 /* Skip writing the record content type to after the encryption,
4380 * as it may change when using the CID extension. */
4381
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004382 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004383 ssl->conf->transport, ssl->out_hdr + 1 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01004384
Hanno Becker19859472018-08-06 09:40:20 +01004385 memcpy( ssl->out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01004386 ssl->out_len[0] = (unsigned char)( len >> 8 );
4387 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00004388
Paul Bakker48916f92012-09-16 19:57:18 +00004389 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00004390 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00004391 mbedtls_record rec;
4392
4393 rec.buf = ssl->out_iv;
4394 rec.buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN -
4395 ( ssl->out_iv - ssl->out_buf );
4396 rec.data_len = ssl->out_msglen;
4397 rec.data_offset = ssl->out_msg - rec.buf;
4398
4399 memcpy( &rec.ctr[0], ssl->out_ctr, 8 );
4400 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
4401 ssl->conf->transport, rec.ver );
4402 rec.type = ssl->out_msgtype;
4403
Hanno Beckera0e20d02019-05-15 14:03:01 +01004404#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker43c24b82019-05-01 09:45:57 +01004405 /* The CID is set by mbedtls_ssl_encrypt_buf(). */
Hanno Beckercab87e62019-04-29 13:52:53 +01004406 rec.cid_len = 0;
Hanno Beckera0e20d02019-05-15 14:03:01 +01004407#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckercab87e62019-04-29 13:52:53 +01004408
Hanno Beckera18d1322018-01-03 14:27:32 +00004409 if( ( ret = mbedtls_ssl_encrypt_buf( ssl, ssl->transform_out, &rec,
Hanno Becker9eddaeb2017-12-27 21:37:21 +00004410 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00004411 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004412 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
Paul Bakker05ef8352012-05-08 09:17:57 +00004413 return( ret );
4414 }
4415
Hanno Becker9eddaeb2017-12-27 21:37:21 +00004416 if( rec.data_offset != 0 )
4417 {
4418 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4419 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4420 }
4421
Hanno Becker6430faf2019-05-08 11:57:13 +01004422 /* Update the record content type and CID. */
4423 ssl->out_msgtype = rec.type;
Hanno Beckera0e20d02019-05-15 14:03:01 +01004424#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID )
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01004425 memcpy( ssl->out_cid, rec.cid, rec.cid_len );
Hanno Beckera0e20d02019-05-15 14:03:01 +01004426#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker78f839d2019-03-14 12:56:23 +00004427 ssl->out_msglen = len = rec.data_len;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00004428 ssl->out_len[0] = (unsigned char)( rec.data_len >> 8 );
4429 ssl->out_len[1] = (unsigned char)( rec.data_len );
Paul Bakker05ef8352012-05-08 09:17:57 +00004430 }
4431
Hanno Becker5903de42019-05-03 14:46:38 +01004432 protected_record_size = len + mbedtls_ssl_out_hdr_len( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01004433
4434#if defined(MBEDTLS_SSL_PROTO_DTLS)
4435 /* In case of DTLS, double-check that we don't exceed
4436 * the remaining space in the datagram. */
4437 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4438 {
Hanno Becker554b0af2018-08-22 20:33:41 +01004439 ret = ssl_get_remaining_space_in_datagram( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01004440 if( ret < 0 )
4441 return( ret );
4442
4443 if( protected_record_size > (size_t) ret )
4444 {
4445 /* Should never happen */
4446 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4447 }
4448 }
4449#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker05ef8352012-05-08 09:17:57 +00004450
Hanno Becker6430faf2019-05-08 11:57:13 +01004451 /* Now write the potentially updated record content type. */
4452 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
4453
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004454 MBEDTLS_SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01004455 "version = [%d:%d], msglen = %d",
4456 ssl->out_hdr[0], ssl->out_hdr[1],
4457 ssl->out_hdr[2], len ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00004458
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004459 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01004460 ssl->out_hdr, protected_record_size );
Hanno Becker2b1e3542018-08-06 11:19:13 +01004461
4462 ssl->out_left += protected_record_size;
4463 ssl->out_hdr += protected_record_size;
4464 ssl_update_out_pointers( ssl, ssl->transform_out );
4465
Hanno Becker04484622018-08-06 09:49:38 +01004466 for( i = 8; i > ssl_ep_len( ssl ); i-- )
4467 if( ++ssl->cur_out_ctr[i - 1] != 0 )
4468 break;
4469
4470 /* The loop goes to its end iff the counter is wrapping */
4471 if( i == ssl_ep_len( ssl ) )
4472 {
4473 MBEDTLS_SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
4474 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
4475 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004476 }
4477
Hanno Becker67bc7c32018-08-06 11:33:50 +01004478#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker47db8772018-08-21 13:32:13 +01004479 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
4480 flush == SSL_DONT_FORCE_FLUSH )
Hanno Becker67bc7c32018-08-06 11:33:50 +01004481 {
Hanno Becker1f5a15d2018-08-21 13:31:31 +01004482 size_t remaining;
4483 ret = ssl_get_remaining_payload_in_datagram( ssl );
4484 if( ret < 0 )
4485 {
4486 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_get_remaining_payload_in_datagram",
4487 ret );
4488 return( ret );
4489 }
4490
4491 remaining = (size_t) ret;
Hanno Becker67bc7c32018-08-06 11:33:50 +01004492 if( remaining == 0 )
Hanno Beckerf0da6672018-08-28 09:55:10 +01004493 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004494 flush = SSL_FORCE_FLUSH;
Hanno Beckerf0da6672018-08-28 09:55:10 +01004495 }
Hanno Becker67bc7c32018-08-06 11:33:50 +01004496 else
4497 {
Hanno Becker513815a2018-08-20 11:56:09 +01004498 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Still %u bytes available in current datagram", (unsigned) remaining ) );
Hanno Becker67bc7c32018-08-06 11:33:50 +01004499 }
4500 }
4501#endif /* MBEDTLS_SSL_PROTO_DTLS */
4502
4503 if( ( flush == SSL_FORCE_FLUSH ) &&
4504 ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004505 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004506 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004507 return( ret );
4508 }
4509
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004510 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write record" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004511
4512 return( 0 );
4513}
4514
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004515#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckere25e3b72018-08-16 09:30:53 +01004516
4517static int ssl_hs_is_proper_fragment( mbedtls_ssl_context *ssl )
4518{
4519 if( ssl->in_msglen < ssl->in_hslen ||
4520 memcmp( ssl->in_msg + 6, "\0\0\0", 3 ) != 0 ||
4521 memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 )
4522 {
4523 return( 1 );
4524 }
4525 return( 0 );
4526}
Hanno Becker44650b72018-08-16 12:51:11 +01004527
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004528static uint32_t ssl_get_hs_frag_len( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004529{
4530 return( ( ssl->in_msg[9] << 16 ) |
4531 ( ssl->in_msg[10] << 8 ) |
4532 ssl->in_msg[11] );
4533}
4534
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004535static uint32_t ssl_get_hs_frag_off( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004536{
4537 return( ( ssl->in_msg[6] << 16 ) |
4538 ( ssl->in_msg[7] << 8 ) |
4539 ssl->in_msg[8] );
4540}
4541
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004542static int ssl_check_hs_header( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004543{
4544 uint32_t msg_len, frag_off, frag_len;
4545
4546 msg_len = ssl_get_hs_total_len( ssl );
4547 frag_off = ssl_get_hs_frag_off( ssl );
4548 frag_len = ssl_get_hs_frag_len( ssl );
4549
4550 if( frag_off > msg_len )
4551 return( -1 );
4552
4553 if( frag_len > msg_len - frag_off )
4554 return( -1 );
4555
4556 if( frag_len + 12 > ssl->in_msglen )
4557 return( -1 );
4558
4559 return( 0 );
4560}
4561
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004562/*
4563 * Mark bits in bitmask (used for DTLS HS reassembly)
4564 */
4565static void ssl_bitmask_set( unsigned char *mask, size_t offset, size_t len )
4566{
4567 unsigned int start_bits, end_bits;
4568
4569 start_bits = 8 - ( offset % 8 );
4570 if( start_bits != 8 )
4571 {
4572 size_t first_byte_idx = offset / 8;
4573
Manuel Pégourié-Gonnardac030522014-09-02 14:23:40 +02004574 /* Special case */
4575 if( len <= start_bits )
4576 {
4577 for( ; len != 0; len-- )
4578 mask[first_byte_idx] |= 1 << ( start_bits - len );
4579
4580 /* Avoid potential issues with offset or len becoming invalid */
4581 return;
4582 }
4583
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004584 offset += start_bits; /* Now offset % 8 == 0 */
4585 len -= start_bits;
4586
4587 for( ; start_bits != 0; start_bits-- )
4588 mask[first_byte_idx] |= 1 << ( start_bits - 1 );
4589 }
4590
4591 end_bits = len % 8;
4592 if( end_bits != 0 )
4593 {
4594 size_t last_byte_idx = ( offset + len ) / 8;
4595
4596 len -= end_bits; /* Now len % 8 == 0 */
4597
4598 for( ; end_bits != 0; end_bits-- )
4599 mask[last_byte_idx] |= 1 << ( 8 - end_bits );
4600 }
4601
4602 memset( mask + offset / 8, 0xFF, len / 8 );
4603}
4604
4605/*
4606 * Check that bitmask is full
4607 */
4608static int ssl_bitmask_check( unsigned char *mask, size_t len )
4609{
4610 size_t i;
4611
4612 for( i = 0; i < len / 8; i++ )
4613 if( mask[i] != 0xFF )
4614 return( -1 );
4615
4616 for( i = 0; i < len % 8; i++ )
4617 if( ( mask[len / 8] & ( 1 << ( 7 - i ) ) ) == 0 )
4618 return( -1 );
4619
4620 return( 0 );
4621}
4622
Hanno Becker56e205e2018-08-16 09:06:12 +01004623/* msg_len does not include the handshake header */
Hanno Becker65dc8852018-08-23 09:40:49 +01004624static size_t ssl_get_reassembly_buffer_size( size_t msg_len,
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004625 unsigned add_bitmap )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004626{
Hanno Becker56e205e2018-08-16 09:06:12 +01004627 size_t alloc_len;
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004628
Hanno Becker56e205e2018-08-16 09:06:12 +01004629 alloc_len = 12; /* Handshake header */
4630 alloc_len += msg_len; /* Content buffer */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004631
Hanno Beckerd07df862018-08-16 09:14:58 +01004632 if( add_bitmap )
4633 alloc_len += msg_len / 8 + ( msg_len % 8 != 0 ); /* Bitmap */
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004634
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004635 return( alloc_len );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004636}
Hanno Becker56e205e2018-08-16 09:06:12 +01004637
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004638#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004639
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004640static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl )
Hanno Becker12555c62018-08-16 12:47:53 +01004641{
4642 return( ( ssl->in_msg[1] << 16 ) |
4643 ( ssl->in_msg[2] << 8 ) |
4644 ssl->in_msg[3] );
4645}
Hanno Beckere25e3b72018-08-16 09:30:53 +01004646
Simon Butcher99000142016-10-13 17:21:01 +01004647int mbedtls_ssl_prepare_handshake_record( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004648{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004649 if( ssl->in_msglen < mbedtls_ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004650 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004651 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too short: %d",
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004652 ssl->in_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004653 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004654 }
4655
Hanno Becker12555c62018-08-16 12:47:53 +01004656 ssl->in_hslen = mbedtls_ssl_hs_hdr_len( ssl ) + ssl_get_hs_total_len( ssl );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004657
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004658 MBEDTLS_SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004659 " %d, type = %d, hslen = %d",
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004660 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004661
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004662#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004663 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004664 {
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004665 int ret;
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004666 unsigned int recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004667
Hanno Becker44650b72018-08-16 12:51:11 +01004668 if( ssl_check_hs_header( ssl ) != 0 )
4669 {
4670 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid handshake header" ) );
4671 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4672 }
4673
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004674 if( ssl->handshake != NULL &&
Hanno Beckerc76c6192017-06-06 10:03:17 +01004675 ( ( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER &&
4676 recv_msg_seq != ssl->handshake->in_msg_seq ) ||
4677 ( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
4678 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO ) ) )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004679 {
Hanno Becker9e1ec222018-08-15 15:54:43 +01004680 if( recv_msg_seq > ssl->handshake->in_msg_seq )
4681 {
4682 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received future handshake message of sequence number %u (next %u)",
4683 recv_msg_seq,
4684 ssl->handshake->in_msg_seq ) );
4685 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4686 }
4687
Manuel Pégourié-Gonnardfc572dd2014-10-09 17:56:57 +02004688 /* Retransmit only on last message from previous flight, to avoid
4689 * too many retransmissions.
4690 * Besides, No sane server ever retransmits HelloVerifyRequest */
4691 if( recv_msg_seq == ssl->handshake->in_flight_start_seq - 1 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004692 ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004693 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004694 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received message from last flight, "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004695 "message_seq = %d, start_of_flight = %d",
4696 recv_msg_seq,
4697 ssl->handshake->in_flight_start_seq ) );
4698
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004699 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004700 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004701 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004702 return( ret );
4703 }
4704 }
4705 else
4706 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004707 MBEDTLS_SSL_DEBUG_MSG( 2, ( "dropping out-of-sequence message: "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004708 "message_seq = %d, expected = %d",
4709 recv_msg_seq,
4710 ssl->handshake->in_msg_seq ) );
4711 }
4712
Hanno Becker90333da2017-10-10 11:27:13 +01004713 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004714 }
4715 /* Wait until message completion to increment in_msg_seq */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004716
Hanno Becker6d97ef52018-08-16 13:09:04 +01004717 /* Message reassembly is handled alongside buffering of future
4718 * messages; the commonality is that both handshake fragments and
Hanno Becker83ab41c2018-08-28 17:19:38 +01004719 * future messages cannot be forwarded immediately to the
Hanno Becker6d97ef52018-08-16 13:09:04 +01004720 * handshake logic layer. */
Hanno Beckere25e3b72018-08-16 09:30:53 +01004721 if( ssl_hs_is_proper_fragment( ssl ) == 1 )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004722 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004723 MBEDTLS_SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) );
Hanno Becker6d97ef52018-08-16 13:09:04 +01004724 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004725 }
4726 }
4727 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004728#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004729 /* With TLS we don't handle fragmentation (for now) */
4730 if( ssl->in_msglen < ssl->in_hslen )
4731 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004732 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) );
4733 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004734 }
4735
Simon Butcher99000142016-10-13 17:21:01 +01004736 return( 0 );
4737}
4738
4739void mbedtls_ssl_update_handshake_status( mbedtls_ssl_context *ssl )
4740{
Hanno Becker0271f962018-08-16 13:23:47 +01004741 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Simon Butcher99000142016-10-13 17:21:01 +01004742
Hanno Becker0271f962018-08-16 13:23:47 +01004743 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER && hs != NULL )
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02004744 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004745 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02004746 }
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004747
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004748 /* Handshake message is complete, increment counter */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004749#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004750 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004751 ssl->handshake != NULL )
4752 {
Hanno Becker0271f962018-08-16 13:23:47 +01004753 unsigned offset;
4754 mbedtls_ssl_hs_buffer *hs_buf;
Hanno Beckere25e3b72018-08-16 09:30:53 +01004755
Hanno Becker0271f962018-08-16 13:23:47 +01004756 /* Increment handshake sequence number */
4757 hs->in_msg_seq++;
4758
4759 /*
4760 * Clear up handshake buffering and reassembly structure.
4761 */
4762
4763 /* Free first entry */
Hanno Beckere605b192018-08-21 15:59:07 +01004764 ssl_buffering_free_slot( ssl, 0 );
Hanno Becker0271f962018-08-16 13:23:47 +01004765
4766 /* Shift all other entries */
Hanno Beckere605b192018-08-21 15:59:07 +01004767 for( offset = 0, hs_buf = &hs->buffering.hs[0];
4768 offset + 1 < MBEDTLS_SSL_MAX_BUFFERED_HS;
Hanno Becker0271f962018-08-16 13:23:47 +01004769 offset++, hs_buf++ )
4770 {
4771 *hs_buf = *(hs_buf + 1);
4772 }
4773
4774 /* Create a fresh last entry */
4775 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004776 }
4777#endif
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004778}
4779
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004780/*
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004781 * DTLS anti-replay: RFC 6347 4.1.2.6
4782 *
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004783 * in_window is a field of bits numbered from 0 (lsb) to 63 (msb).
4784 * Bit n is set iff record number in_window_top - n has been seen.
4785 *
4786 * Usually, in_window_top is the last record number seen and the lsb of
4787 * in_window is set. The only exception is the initial state (record number 0
4788 * not seen yet).
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004789 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004790#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4791static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004792{
4793 ssl->in_window_top = 0;
4794 ssl->in_window = 0;
4795}
4796
4797static inline uint64_t ssl_load_six_bytes( unsigned char *buf )
4798{
4799 return( ( (uint64_t) buf[0] << 40 ) |
4800 ( (uint64_t) buf[1] << 32 ) |
4801 ( (uint64_t) buf[2] << 24 ) |
4802 ( (uint64_t) buf[3] << 16 ) |
4803 ( (uint64_t) buf[4] << 8 ) |
4804 ( (uint64_t) buf[5] ) );
4805}
4806
4807/*
4808 * Return 0 if sequence number is acceptable, -1 otherwise
4809 */
Hanno Becker0183d692019-07-12 08:50:37 +01004810int mbedtls_ssl_dtls_replay_check( mbedtls_ssl_context const *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004811{
4812 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
4813 uint64_t bit;
4814
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004815 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004816 return( 0 );
4817
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004818 if( rec_seqnum > ssl->in_window_top )
4819 return( 0 );
4820
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004821 bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004822
4823 if( bit >= 64 )
4824 return( -1 );
4825
4826 if( ( ssl->in_window & ( (uint64_t) 1 << bit ) ) != 0 )
4827 return( -1 );
4828
4829 return( 0 );
4830}
4831
4832/*
4833 * Update replay window on new validated record
4834 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004835void mbedtls_ssl_dtls_replay_update( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004836{
4837 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
4838
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004839 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004840 return;
4841
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004842 if( rec_seqnum > ssl->in_window_top )
4843 {
4844 /* Update window_top and the contents of the window */
4845 uint64_t shift = rec_seqnum - ssl->in_window_top;
4846
4847 if( shift >= 64 )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004848 ssl->in_window = 1;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004849 else
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004850 {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004851 ssl->in_window <<= shift;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004852 ssl->in_window |= 1;
4853 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004854
4855 ssl->in_window_top = rec_seqnum;
4856 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004857 else
4858 {
4859 /* Mark that number as seen in the current window */
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004860 uint64_t bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004861
4862 if( bit < 64 ) /* Always true, but be extra sure */
4863 ssl->in_window |= (uint64_t) 1 << bit;
4864 }
4865}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004866#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004867
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004868#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004869/* Forward declaration */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02004870static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial );
4871
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004872/*
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004873 * Without any SSL context, check if a datagram looks like a ClientHello with
4874 * a valid cookie, and if it doesn't, generate a HelloVerifyRequest message.
Simon Butcher0789aed2015-09-11 17:15:17 +01004875 * Both input and output include full DTLS headers.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004876 *
4877 * - if cookie is valid, return 0
4878 * - if ClientHello looks superficially valid but cookie is not,
4879 * fill obuf and set olen, then
4880 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
4881 * - otherwise return a specific error code
4882 */
4883static int ssl_check_dtls_clihlo_cookie(
4884 mbedtls_ssl_cookie_write_t *f_cookie_write,
4885 mbedtls_ssl_cookie_check_t *f_cookie_check,
4886 void *p_cookie,
4887 const unsigned char *cli_id, size_t cli_id_len,
4888 const unsigned char *in, size_t in_len,
4889 unsigned char *obuf, size_t buf_len, size_t *olen )
4890{
4891 size_t sid_len, cookie_len;
4892 unsigned char *p;
4893
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004894 /*
4895 * Structure of ClientHello with record and handshake headers,
4896 * and expected values. We don't need to check a lot, more checks will be
4897 * done when actually parsing the ClientHello - skipping those checks
4898 * avoids code duplication and does not make cookie forging any easier.
4899 *
4900 * 0-0 ContentType type; copied, must be handshake
4901 * 1-2 ProtocolVersion version; copied
4902 * 3-4 uint16 epoch; copied, must be 0
4903 * 5-10 uint48 sequence_number; copied
4904 * 11-12 uint16 length; (ignored)
4905 *
4906 * 13-13 HandshakeType msg_type; (ignored)
4907 * 14-16 uint24 length; (ignored)
4908 * 17-18 uint16 message_seq; copied
4909 * 19-21 uint24 fragment_offset; copied, must be 0
4910 * 22-24 uint24 fragment_length; (ignored)
4911 *
4912 * 25-26 ProtocolVersion client_version; (ignored)
4913 * 27-58 Random random; (ignored)
4914 * 59-xx SessionID session_id; 1 byte len + sid_len content
4915 * 60+ opaque cookie<0..2^8-1>; 1 byte len + content
4916 * ...
4917 *
4918 * Minimum length is 61 bytes.
4919 */
4920 if( in_len < 61 ||
4921 in[0] != MBEDTLS_SSL_MSG_HANDSHAKE ||
4922 in[3] != 0 || in[4] != 0 ||
4923 in[19] != 0 || in[20] != 0 || in[21] != 0 )
4924 {
4925 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4926 }
4927
4928 sid_len = in[59];
4929 if( sid_len > in_len - 61 )
4930 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4931
4932 cookie_len = in[60 + sid_len];
4933 if( cookie_len > in_len - 60 )
4934 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4935
4936 if( f_cookie_check( p_cookie, in + sid_len + 61, cookie_len,
4937 cli_id, cli_id_len ) == 0 )
4938 {
4939 /* Valid cookie */
4940 return( 0 );
4941 }
4942
4943 /*
4944 * If we get here, we've got an invalid cookie, let's prepare HVR.
4945 *
4946 * 0-0 ContentType type; copied
4947 * 1-2 ProtocolVersion version; copied
4948 * 3-4 uint16 epoch; copied
4949 * 5-10 uint48 sequence_number; copied
4950 * 11-12 uint16 length; olen - 13
4951 *
4952 * 13-13 HandshakeType msg_type; hello_verify_request
4953 * 14-16 uint24 length; olen - 25
4954 * 17-18 uint16 message_seq; copied
4955 * 19-21 uint24 fragment_offset; copied
4956 * 22-24 uint24 fragment_length; olen - 25
4957 *
4958 * 25-26 ProtocolVersion server_version; 0xfe 0xff
4959 * 27-27 opaque cookie<0..2^8-1>; cookie_len = olen - 27, cookie
4960 *
4961 * Minimum length is 28.
4962 */
4963 if( buf_len < 28 )
4964 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
4965
4966 /* Copy most fields and adapt others */
4967 memcpy( obuf, in, 25 );
4968 obuf[13] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST;
4969 obuf[25] = 0xfe;
4970 obuf[26] = 0xff;
4971
4972 /* Generate and write actual cookie */
4973 p = obuf + 28;
4974 if( f_cookie_write( p_cookie,
4975 &p, obuf + buf_len, cli_id, cli_id_len ) != 0 )
4976 {
4977 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4978 }
4979
4980 *olen = p - obuf;
4981
4982 /* Go back and fill length fields */
4983 obuf[27] = (unsigned char)( *olen - 28 );
4984
4985 obuf[14] = obuf[22] = (unsigned char)( ( *olen - 25 ) >> 16 );
4986 obuf[15] = obuf[23] = (unsigned char)( ( *olen - 25 ) >> 8 );
4987 obuf[16] = obuf[24] = (unsigned char)( ( *olen - 25 ) );
4988
4989 obuf[11] = (unsigned char)( ( *olen - 13 ) >> 8 );
4990 obuf[12] = (unsigned char)( ( *olen - 13 ) );
4991
4992 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
4993}
4994
4995/*
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004996 * Handle possible client reconnect with the same UDP quadruplet
4997 * (RFC 6347 Section 4.2.8).
4998 *
4999 * Called by ssl_parse_record_header() in case we receive an epoch 0 record
5000 * that looks like a ClientHello.
5001 *
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005002 * - if the input looks like a ClientHello without cookies,
5003 * send back HelloVerifyRequest, then
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005004 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005005 * - if the input looks like a ClientHello with a valid cookie,
5006 * reset the session of the current context, and
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02005007 * return MBEDTLS_ERR_SSL_CLIENT_RECONNECT
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005008 * - if anything goes wrong, return a specific error code
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005009 *
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005010 * mbedtls_ssl_read_record() will ignore the record if anything else than
Simon Butcherd0bf6a32015-09-11 17:34:49 +01005011 * MBEDTLS_ERR_SSL_CLIENT_RECONNECT or 0 is returned, although this function
5012 * cannot not return 0.
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005013 */
5014static int ssl_handle_possible_reconnect( mbedtls_ssl_context *ssl )
5015{
5016 int ret;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005017 size_t len;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005018
Hanno Becker2fddd372019-07-10 14:37:41 +01005019 if( ssl->conf->f_cookie_write == NULL ||
5020 ssl->conf->f_cookie_check == NULL )
5021 {
5022 /* If we can't use cookies to verify reachability of the peer,
5023 * drop the record. */
5024 return( 0 );
5025 }
5026
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005027 ret = ssl_check_dtls_clihlo_cookie(
5028 ssl->conf->f_cookie_write,
5029 ssl->conf->f_cookie_check,
5030 ssl->conf->p_cookie,
5031 ssl->cli_id, ssl->cli_id_len,
5032 ssl->in_buf, ssl->in_left,
Angus Grattond8213d02016-05-25 20:56:48 +10005033 ssl->out_buf, MBEDTLS_SSL_OUT_CONTENT_LEN, &len );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005034
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005035 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_check_dtls_clihlo_cookie", ret );
5036
5037 if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005038 {
Brian J Murray1903fb32016-11-06 04:45:15 -08005039 /* Don't check write errors as we can't do anything here.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005040 * If the error is permanent we'll catch it later,
5041 * if it's not, then hopefully it'll work next time. */
5042 (void) ssl->f_send( ssl->p_bio, ssl->out_buf, len );
Hanno Becker2fddd372019-07-10 14:37:41 +01005043 ret = 0;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005044 }
5045
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005046 if( ret == 0 )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005047 {
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02005048 /* Got a valid cookie, partially reset context */
5049 if( ( ret = ssl_session_reset_int( ssl, 1 ) ) != 0 )
5050 {
5051 MBEDTLS_SSL_DEBUG_RET( 1, "reset", ret );
5052 return( ret );
5053 }
5054
5055 return( MBEDTLS_ERR_SSL_CLIENT_RECONNECT );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005056 }
5057
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005058 return( ret );
5059}
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02005060#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02005061
Hanno Beckerf661c9c2019-05-03 13:25:54 +01005062static int ssl_check_record_type( uint8_t record_type )
5063{
5064 if( record_type != MBEDTLS_SSL_MSG_HANDSHAKE &&
5065 record_type != MBEDTLS_SSL_MSG_ALERT &&
5066 record_type != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC &&
5067 record_type != MBEDTLS_SSL_MSG_APPLICATION_DATA )
5068 {
5069 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5070 }
5071
5072 return( 0 );
5073}
5074
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02005075/*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005076 * ContentType type;
5077 * ProtocolVersion version;
5078 * uint16 epoch; // DTLS only
5079 * uint48 sequence_number; // DTLS only
5080 * uint16 length;
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005081 *
5082 * Return 0 if header looks sane (and, for DTLS, the record is expected)
Simon Butcher207990d2015-12-16 01:51:30 +00005083 * MBEDTLS_ERR_SSL_INVALID_RECORD if the header looks bad,
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005084 * MBEDTLS_ERR_SSL_UNEXPECTED_RECORD (DTLS only) if sane but unexpected.
5085 *
5086 * With DTLS, mbedtls_ssl_read_record() will:
Simon Butcher207990d2015-12-16 01:51:30 +00005087 * 1. proceed with the record if this function returns 0
5088 * 2. drop only the current record if this function returns UNEXPECTED_RECORD
5089 * 3. return CLIENT_RECONNECT if this function return that value
5090 * 4. drop the whole datagram if this function returns anything else.
5091 * Point 2 is needed when the peer is resending, and we have already received
5092 * the first record from a datagram but are still waiting for the others.
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005093 */
Hanno Becker331de3d2019-07-12 11:10:16 +01005094static int ssl_parse_record_header( mbedtls_ssl_context const *ssl,
Hanno Beckere5e7e782019-07-11 12:29:35 +01005095 unsigned char *buf,
5096 size_t len,
5097 mbedtls_record *rec )
Paul Bakker5121ce52009-01-03 21:22:43 +00005098{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005099 int major_ver, minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00005100
Hanno Beckere5e7e782019-07-11 12:29:35 +01005101 size_t const rec_hdr_type_offset = 0;
5102 size_t const rec_hdr_type_len = 1;
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02005103
Hanno Beckere5e7e782019-07-11 12:29:35 +01005104 size_t const rec_hdr_version_offset = rec_hdr_type_offset +
5105 rec_hdr_type_len;
5106 size_t const rec_hdr_version_len = 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00005107
Hanno Beckere5e7e782019-07-11 12:29:35 +01005108 size_t const rec_hdr_ctr_len = 8;
5109#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckerf5466252019-07-25 10:13:02 +01005110 uint32_t rec_epoch;
Hanno Beckere5e7e782019-07-11 12:29:35 +01005111 size_t const rec_hdr_ctr_offset = rec_hdr_version_offset +
5112 rec_hdr_version_len;
5113
Hanno Beckera0e20d02019-05-15 14:03:01 +01005114#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckere5e7e782019-07-11 12:29:35 +01005115 size_t const rec_hdr_cid_offset = rec_hdr_ctr_offset +
5116 rec_hdr_ctr_len;
Hanno Beckerf5466252019-07-25 10:13:02 +01005117 size_t rec_hdr_cid_len = 0;
Hanno Beckere5e7e782019-07-11 12:29:35 +01005118#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
5119#endif /* MBEDTLS_SSL_PROTO_DTLS */
5120
5121 size_t rec_hdr_len_offset; /* To be determined */
5122 size_t const rec_hdr_len_len = 2;
5123
5124 /*
5125 * Check minimum lengths for record header.
5126 */
5127
5128#if defined(MBEDTLS_SSL_PROTO_DTLS)
5129 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5130 {
5131 rec_hdr_len_offset = rec_hdr_ctr_offset + rec_hdr_ctr_len;
5132 }
5133 else
5134#endif /* MBEDTLS_SSL_PROTO_DTLS */
5135 {
5136 rec_hdr_len_offset = rec_hdr_version_offset + rec_hdr_version_len;
5137 }
5138
5139 if( len < rec_hdr_len_offset + rec_hdr_len_len )
5140 {
5141 MBEDTLS_SSL_DEBUG_MSG( 1, ( "datagram of length %u too small to hold DTLS record header of length %u",
5142 (unsigned) len,
5143 (unsigned)( rec_hdr_len_len + rec_hdr_len_len ) ) );
5144 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5145 }
5146
5147 /*
5148 * Parse and validate record content type
5149 */
5150
5151 rec->type = buf[ rec_hdr_type_offset ];
Hanno Beckere5e7e782019-07-11 12:29:35 +01005152
5153 /* Check record content type */
5154#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
5155 rec->cid_len = 0;
5156
Hanno Beckerca59c2b2019-05-08 12:03:28 +01005157 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Hanno Beckere5e7e782019-07-11 12:29:35 +01005158 ssl->conf->cid_len != 0 &&
5159 rec->type == MBEDTLS_SSL_MSG_CID )
Hanno Beckerca59c2b2019-05-08 12:03:28 +01005160 {
5161 /* Shift pointers to account for record header including CID
5162 * struct {
5163 * ContentType special_type = tls12_cid;
5164 * ProtocolVersion version;
5165 * uint16 epoch;
5166 * uint48 sequence_number;
Hanno Becker8e55b0f2019-05-23 17:03:19 +01005167 * opaque cid[cid_length]; // Additional field compared to
5168 * // default DTLS record format
Hanno Beckerca59c2b2019-05-08 12:03:28 +01005169 * uint16 length;
5170 * opaque enc_content[DTLSCiphertext.length];
5171 * } DTLSCiphertext;
5172 */
5173
5174 /* So far, we only support static CID lengths
5175 * fixed in the configuration. */
Hanno Beckere5e7e782019-07-11 12:29:35 +01005176 rec_hdr_cid_len = ssl->conf->cid_len;
5177 rec_hdr_len_offset += rec_hdr_cid_len;
Hanno Beckere538d822019-07-10 14:50:10 +01005178
Hanno Beckere5e7e782019-07-11 12:29:35 +01005179 if( len < rec_hdr_len_offset + rec_hdr_len_len )
Hanno Beckere538d822019-07-10 14:50:10 +01005180 {
Hanno Beckere5e7e782019-07-11 12:29:35 +01005181 MBEDTLS_SSL_DEBUG_MSG( 1, ( "datagram of length %u too small to hold DTLS record header including CID, length %u",
5182 (unsigned) len,
5183 (unsigned)( rec_hdr_len_offset + rec_hdr_len_len ) ) );
Hanno Becker59be60e2019-07-10 14:53:43 +01005184 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Beckere538d822019-07-10 14:50:10 +01005185 }
Hanno Beckere5e7e782019-07-11 12:29:35 +01005186
Manuel Pégourié-Gonnard7e821b52019-08-02 10:17:15 +02005187 /* configured CID len is guaranteed at most 255, see
5188 * MBEDTLS_SSL_CID_OUT_LEN_MAX in check_config.h */
5189 rec->cid_len = (uint8_t) rec_hdr_cid_len;
Hanno Beckere5e7e782019-07-11 12:29:35 +01005190 memcpy( rec->cid, buf + rec_hdr_cid_offset, rec_hdr_cid_len );
Hanno Beckerca59c2b2019-05-08 12:03:28 +01005191 }
5192 else
Hanno Beckera0e20d02019-05-15 14:03:01 +01005193#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02005194 {
Hanno Beckere5e7e782019-07-11 12:29:35 +01005195 if( ssl_check_record_type( rec->type ) )
5196 {
Hanno Becker54229812019-07-12 14:40:00 +01005197 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type %u",
5198 (unsigned) rec->type ) );
Hanno Beckere5e7e782019-07-11 12:29:35 +01005199 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5200 }
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02005201 }
5202
Hanno Beckere5e7e782019-07-11 12:29:35 +01005203 /*
5204 * Parse and validate record version
5205 */
5206
Hanno Beckerd0b66d02019-07-26 08:07:03 +01005207 rec->ver[0] = buf[ rec_hdr_version_offset + 0 ];
5208 rec->ver[1] = buf[ rec_hdr_version_offset + 1 ];
Hanno Beckere5e7e782019-07-11 12:29:35 +01005209 mbedtls_ssl_read_version( &major_ver, &minor_ver,
5210 ssl->conf->transport,
Hanno Beckerd0b66d02019-07-26 08:07:03 +01005211 &rec->ver[0] );
Hanno Beckere5e7e782019-07-11 12:29:35 +01005212
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01005213 if( major_ver != ssl->major_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00005214 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005215 MBEDTLS_SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
5216 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00005217 }
5218
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005219 if( minor_ver > ssl->conf->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00005220 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005221 MBEDTLS_SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
5222 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00005223 }
5224
Hanno Beckere5e7e782019-07-11 12:29:35 +01005225 /*
5226 * Parse/Copy record sequence number.
5227 */
Hanno Beckerca59c2b2019-05-08 12:03:28 +01005228
Hanno Beckere5e7e782019-07-11 12:29:35 +01005229#if defined(MBEDTLS_SSL_PROTO_DTLS)
5230 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02005231 {
Hanno Beckere5e7e782019-07-11 12:29:35 +01005232 /* Copy explicit record sequence number from input buffer. */
5233 memcpy( &rec->ctr[0], buf + rec_hdr_ctr_offset,
5234 rec_hdr_ctr_len );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02005235 }
Hanno Beckere5e7e782019-07-11 12:29:35 +01005236 else
5237#endif /* MBEDTLS_SSL_PROTO_DTLS */
5238 {
5239 /* Copy implicit record sequence number from SSL context structure. */
5240 memcpy( &rec->ctr[0], ssl->in_ctr, rec_hdr_ctr_len );
5241 }
Paul Bakker40e46942009-01-03 21:51:57 +00005242
Hanno Beckere5e7e782019-07-11 12:29:35 +01005243 /*
5244 * Parse record length.
5245 */
5246
Hanno Beckere5e7e782019-07-11 12:29:35 +01005247 rec->data_offset = rec_hdr_len_offset + rec_hdr_len_len;
Hanno Becker9eca2762019-07-25 10:16:37 +01005248 rec->data_len = ( (size_t) buf[ rec_hdr_len_offset + 0 ] << 8 ) |
5249 ( (size_t) buf[ rec_hdr_len_offset + 1 ] << 0 );
Hanno Beckere5e7e782019-07-11 12:29:35 +01005250 MBEDTLS_SSL_DEBUG_BUF( 4, "input record header", buf, rec->data_offset );
Paul Bakker5121ce52009-01-03 21:22:43 +00005251
Hanno Beckerca59c2b2019-05-08 12:03:28 +01005252 MBEDTLS_SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
Hanno Becker92d30f52019-05-23 17:03:44 +01005253 "version = [%d:%d], msglen = %d",
Hanno Beckere5e7e782019-07-11 12:29:35 +01005254 rec->type,
5255 major_ver, minor_ver, rec->data_len ) );
5256
5257 rec->buf = buf;
5258 rec->buf_len = rec->data_offset + rec->data_len;
Hanno Beckerca59c2b2019-05-08 12:03:28 +01005259
Hanno Beckerd417cc92019-07-26 08:20:27 +01005260 if( rec->data_len == 0 )
5261 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00005262
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005263 /*
Hanno Becker52c6dc62017-05-26 16:07:36 +01005264 * DTLS-related tests.
5265 * Check epoch before checking length constraint because
5266 * the latter varies with the epoch. E.g., if a ChangeCipherSpec
5267 * message gets duplicated before the corresponding Finished message,
5268 * the second ChangeCipherSpec should be discarded because it belongs
5269 * to an old epoch, but not because its length is shorter than
5270 * the minimum record length for packets using the new record transform.
5271 * Note that these two kinds of failures are handled differently,
5272 * as an unexpected record is silently skipped but an invalid
5273 * record leads to the entire datagram being dropped.
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005274 */
5275#if defined(MBEDTLS_SSL_PROTO_DTLS)
5276 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5277 {
Hanno Beckere5e7e782019-07-11 12:29:35 +01005278 rec_epoch = ( rec->ctr[0] << 8 ) | rec->ctr[1];
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005279
Hanno Becker955a5c92019-07-10 17:12:07 +01005280 /* Check that the datagram is large enough to contain a record
5281 * of the advertised length. */
Hanno Beckere5e7e782019-07-11 12:29:35 +01005282 if( len < rec->data_offset + rec->data_len )
Hanno Becker955a5c92019-07-10 17:12:07 +01005283 {
Hanno Beckere5e7e782019-07-11 12:29:35 +01005284 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Datagram of length %u too small to contain record of advertised length %u.",
5285 (unsigned) len,
5286 (unsigned)( rec->data_offset + rec->data_len ) ) );
Hanno Becker955a5c92019-07-10 17:12:07 +01005287 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5288 }
Hanno Becker37cfe732019-07-10 17:20:01 +01005289
Hanno Becker37cfe732019-07-10 17:20:01 +01005290 /* Records from other, non-matching epochs are silently discarded.
5291 * (The case of same-port Client reconnects must be considered in
5292 * the caller). */
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005293 if( rec_epoch != ssl->in_epoch )
5294 {
5295 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record from another epoch: "
5296 "expected %d, received %d",
5297 ssl->in_epoch, rec_epoch ) );
5298
Hanno Becker552f7472019-07-19 10:59:12 +01005299 /* Records from the next epoch are considered for buffering
5300 * (concretely: early Finished messages). */
5301 if( rec_epoch == (unsigned) ssl->in_epoch + 1 )
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005302 {
Hanno Becker552f7472019-07-19 10:59:12 +01005303 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Consider record for buffering" ) );
5304 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005305 }
Hanno Becker5f066e72018-08-16 14:56:31 +01005306
Hanno Becker2fddd372019-07-10 14:37:41 +01005307 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005308 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005309#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Hanno Becker37cfe732019-07-10 17:20:01 +01005310 /* For records from the correct epoch, check whether their
5311 * sequence number has been seen before. */
Hanno Becker2fddd372019-07-10 14:37:41 +01005312 else if( mbedtls_ssl_dtls_replay_check( ssl ) != 0 )
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005313 {
5314 MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record" ) );
5315 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
5316 }
5317#endif
5318 }
5319#endif /* MBEDTLS_SSL_PROTO_DTLS */
5320
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005321 return( 0 );
5322}
Paul Bakker5121ce52009-01-03 21:22:43 +00005323
Paul Bakker5121ce52009-01-03 21:22:43 +00005324
Hanno Becker2fddd372019-07-10 14:37:41 +01005325#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
5326static int ssl_check_client_reconnect( mbedtls_ssl_context *ssl )
5327{
5328 unsigned int rec_epoch = ( ssl->in_ctr[0] << 8 ) | ssl->in_ctr[1];
5329
5330 /*
5331 * Check for an epoch 0 ClientHello. We can't use in_msg here to
5332 * access the first byte of record content (handshake type), as we
5333 * have an active transform (possibly iv_len != 0), so use the
5334 * fact that the record header len is 13 instead.
5335 */
5336 if( rec_epoch == 0 &&
5337 ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
5338 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
5339 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
5340 ssl->in_left > 13 &&
5341 ssl->in_buf[13] == MBEDTLS_SSL_HS_CLIENT_HELLO )
5342 {
5343 MBEDTLS_SSL_DEBUG_MSG( 1, ( "possible client reconnect "
5344 "from the same port" ) );
5345 return( ssl_handle_possible_reconnect( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00005346 }
5347
5348 return( 0 );
5349}
Hanno Becker2fddd372019-07-10 14:37:41 +01005350#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00005351
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005352/*
5353 * If applicable, decrypt (and decompress) record content
5354 */
Hanno Beckerfdf66042019-07-11 13:07:45 +01005355static int ssl_prepare_record_content( mbedtls_ssl_context *ssl,
5356 mbedtls_record *rec )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005357{
5358 int ret, done = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02005359
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005360 MBEDTLS_SSL_DEBUG_BUF( 4, "input record from network",
Hanno Beckerfdf66042019-07-11 13:07:45 +01005361 rec->buf, rec->buf_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00005362
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005363#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
5364 if( mbedtls_ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00005365 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005366 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_read()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00005367
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005368 ret = mbedtls_ssl_hw_record_read( ssl );
5369 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00005370 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005371 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_read", ret );
5372 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00005373 }
Paul Bakkerc7878112012-12-19 14:41:14 +01005374
5375 if( ret == 0 )
5376 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00005377 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005378#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00005379 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005380 {
Hanno Becker58ef0bf2019-07-12 09:35:58 +01005381 unsigned char const old_msg_type = rec->type;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00005382
Hanno Beckera18d1322018-01-03 14:27:32 +00005383 if( ( ret = mbedtls_ssl_decrypt_buf( ssl, ssl->transform_in,
Hanno Beckerfdf66042019-07-11 13:07:45 +01005384 rec ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005385 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005386 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
Hanno Becker8367ccc2019-05-14 11:30:10 +01005387
Hanno Beckera0e20d02019-05-15 14:03:01 +01005388#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker8367ccc2019-05-14 11:30:10 +01005389 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_CID &&
5390 ssl->conf->ignore_unexpected_cid
5391 == MBEDTLS_SSL_UNEXPECTED_CID_IGNORE )
5392 {
Hanno Beckere8d6afd2019-05-24 10:11:06 +01005393 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ignoring unexpected CID" ) );
Hanno Becker16ded982019-05-08 13:02:55 +01005394 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
Hanno Becker8367ccc2019-05-14 11:30:10 +01005395 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01005396#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker16ded982019-05-08 13:02:55 +01005397
Paul Bakker5121ce52009-01-03 21:22:43 +00005398 return( ret );
5399 }
5400
Hanno Becker58ef0bf2019-07-12 09:35:58 +01005401 if( old_msg_type != rec->type )
Hanno Becker6430faf2019-05-08 11:57:13 +01005402 {
5403 MBEDTLS_SSL_DEBUG_MSG( 4, ( "record type after decrypt (before %d): %d",
Hanno Becker58ef0bf2019-07-12 09:35:58 +01005404 old_msg_type, rec->type ) );
Hanno Becker6430faf2019-05-08 11:57:13 +01005405 }
5406
Hanno Becker1c0c37f2018-08-07 14:29:29 +01005407 MBEDTLS_SSL_DEBUG_BUF( 4, "input payload after decrypt",
Hanno Becker58ef0bf2019-07-12 09:35:58 +01005408 rec->buf + rec->data_offset, rec->data_len );
Hanno Becker1c0c37f2018-08-07 14:29:29 +01005409
Hanno Beckera0e20d02019-05-15 14:03:01 +01005410#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker6430faf2019-05-08 11:57:13 +01005411 /* We have already checked the record content type
5412 * in ssl_parse_record_header(), failing or silently
5413 * dropping the record in the case of an unknown type.
5414 *
5415 * Since with the use of CIDs, the record content type
5416 * might change during decryption, re-check the record
5417 * content type, but treat a failure as fatal this time. */
Hanno Becker58ef0bf2019-07-12 09:35:58 +01005418 if( ssl_check_record_type( rec->type ) )
Hanno Becker6430faf2019-05-08 11:57:13 +01005419 {
5420 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
5421 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5422 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01005423#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker6430faf2019-05-08 11:57:13 +01005424
Hanno Becker58ef0bf2019-07-12 09:35:58 +01005425 if( rec->data_len == 0 )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00005426 {
5427#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5428 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3
Hanno Becker58ef0bf2019-07-12 09:35:58 +01005429 && rec->type != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00005430 {
5431 /* TLS v1.2 explicitly disallows zero-length messages which are not application data */
5432 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid zero-length message type: %d", ssl->in_msgtype ) );
5433 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5434 }
5435#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
5436
5437 ssl->nb_zero++;
5438
5439 /*
5440 * Three or more empty messages may be a DoS attack
5441 * (excessive CPU consumption).
5442 */
5443 if( ssl->nb_zero > 3 )
5444 {
5445 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
Hanno Becker6e7700d2019-05-08 10:38:32 +01005446 "messages, possible DoS attack" ) );
5447 /* Treat the records as if they were not properly authenticated,
5448 * thereby failing the connection if we see more than allowed
5449 * by the configured bad MAC threshold. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00005450 return( MBEDTLS_ERR_SSL_INVALID_MAC );
5451 }
5452 }
5453 else
5454 ssl->nb_zero = 0;
5455
5456#if defined(MBEDTLS_SSL_PROTO_DTLS)
5457 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5458 {
5459 ; /* in_ctr read from peer, not maintained internally */
5460 }
5461 else
5462#endif
5463 {
5464 unsigned i;
5465 for( i = 8; i > ssl_ep_len( ssl ); i-- )
5466 if( ++ssl->in_ctr[i - 1] != 0 )
5467 break;
5468
5469 /* The loop goes to its end iff the counter is wrapping */
5470 if( i == ssl_ep_len( ssl ) )
5471 {
5472 MBEDTLS_SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
5473 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
5474 }
5475 }
5476
Paul Bakker5121ce52009-01-03 21:22:43 +00005477 }
5478
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005479#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00005480 if( ssl->transform_in != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005481 ssl->session_in->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00005482 {
5483 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
5484 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005485 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00005486 return( ret );
5487 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00005488 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005489#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00005490
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005491#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005492 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02005493 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005494 mbedtls_ssl_dtls_replay_update( ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02005495 }
5496#endif
5497
Hanno Beckerd96e10b2019-07-09 17:30:02 +01005498 /* Check actual (decrypted) record content length against
5499 * configured maximum. */
5500 if( ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
5501 {
5502 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
5503 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5504 }
5505
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005506 return( 0 );
5507}
5508
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005509static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005510
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005511/*
5512 * Read a record.
5513 *
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005514 * Silently ignore non-fatal alert (and for DTLS, invalid records as well,
5515 * RFC 6347 4.1.2.7) and continue reading until a valid record is found.
5516 *
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005517 */
Hanno Becker1097b342018-08-15 14:09:41 +01005518
5519/* Helper functions for mbedtls_ssl_read_record(). */
5520static int ssl_consume_current_message( mbedtls_ssl_context *ssl );
Hanno Beckere74d5562018-08-15 14:26:08 +01005521static int ssl_get_next_record( mbedtls_ssl_context *ssl );
5522static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl );
Hanno Becker4162b112018-08-15 14:05:04 +01005523
Hanno Becker327c93b2018-08-15 13:56:18 +01005524int mbedtls_ssl_read_record( mbedtls_ssl_context *ssl,
Hanno Becker3a0aad12018-08-20 09:44:02 +01005525 unsigned update_hs_digest )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005526{
5527 int ret;
5528
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005529 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read record" ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005530
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005531 if( ssl->keep_current_message == 0 )
5532 {
5533 do {
Simon Butcher99000142016-10-13 17:21:01 +01005534
Hanno Becker26994592018-08-15 14:14:59 +01005535 ret = ssl_consume_current_message( ssl );
Hanno Becker90333da2017-10-10 11:27:13 +01005536 if( ret != 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005537 return( ret );
Hanno Becker26994592018-08-15 14:14:59 +01005538
Hanno Beckere74d5562018-08-15 14:26:08 +01005539 if( ssl_record_is_in_progress( ssl ) == 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005540 {
Hanno Becker40f50842018-08-15 14:48:01 +01005541#if defined(MBEDTLS_SSL_PROTO_DTLS)
5542 int have_buffered = 0;
Hanno Beckere74d5562018-08-15 14:26:08 +01005543
Hanno Becker40f50842018-08-15 14:48:01 +01005544 /* We only check for buffered messages if the
5545 * current datagram is fully consumed. */
5546 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005547 ssl_next_record_is_in_datagram( ssl ) == 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01005548 {
Hanno Becker40f50842018-08-15 14:48:01 +01005549 if( ssl_load_buffered_message( ssl ) == 0 )
5550 have_buffered = 1;
5551 }
5552
5553 if( have_buffered == 0 )
5554#endif /* MBEDTLS_SSL_PROTO_DTLS */
5555 {
5556 ret = ssl_get_next_record( ssl );
5557 if( ret == MBEDTLS_ERR_SSL_CONTINUE_PROCESSING )
5558 continue;
5559
5560 if( ret != 0 )
5561 {
Hanno Beckerc573ac32018-08-28 17:15:25 +01005562 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_get_next_record" ), ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005563 return( ret );
5564 }
Hanno Beckere74d5562018-08-15 14:26:08 +01005565 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005566 }
5567
5568 ret = mbedtls_ssl_handle_message_type( ssl );
5569
Hanno Becker40f50842018-08-15 14:48:01 +01005570#if defined(MBEDTLS_SSL_PROTO_DTLS)
5571 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
5572 {
5573 /* Buffer future message */
5574 ret = ssl_buffer_message( ssl );
5575 if( ret != 0 )
5576 return( ret );
5577
5578 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
5579 }
5580#endif /* MBEDTLS_SSL_PROTO_DTLS */
5581
Hanno Becker90333da2017-10-10 11:27:13 +01005582 } while( MBEDTLS_ERR_SSL_NON_FATAL == ret ||
5583 MBEDTLS_ERR_SSL_CONTINUE_PROCESSING == ret );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005584
5585 if( 0 != ret )
Simon Butcher99000142016-10-13 17:21:01 +01005586 {
Hanno Becker05c4fc82017-11-09 14:34:06 +00005587 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_handle_message_type" ), ret );
Simon Butcher99000142016-10-13 17:21:01 +01005588 return( ret );
5589 }
5590
Hanno Becker327c93b2018-08-15 13:56:18 +01005591 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
Hanno Becker3a0aad12018-08-20 09:44:02 +01005592 update_hs_digest == 1 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005593 {
5594 mbedtls_ssl_update_handshake_status( ssl );
5595 }
Simon Butcher99000142016-10-13 17:21:01 +01005596 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005597 else
Simon Butcher99000142016-10-13 17:21:01 +01005598 {
Hanno Becker02f59072018-08-15 14:00:24 +01005599 MBEDTLS_SSL_DEBUG_MSG( 2, ( "reuse previously read message" ) );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005600 ssl->keep_current_message = 0;
Simon Butcher99000142016-10-13 17:21:01 +01005601 }
5602
5603 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read record" ) );
5604
5605 return( 0 );
5606}
5607
Hanno Becker40f50842018-08-15 14:48:01 +01005608#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005609static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl )
Simon Butcher99000142016-10-13 17:21:01 +01005610{
Hanno Becker40f50842018-08-15 14:48:01 +01005611 if( ssl->in_left > ssl->next_record_offset )
5612 return( 1 );
Simon Butcher99000142016-10-13 17:21:01 +01005613
Hanno Becker40f50842018-08-15 14:48:01 +01005614 return( 0 );
5615}
5616
5617static int ssl_load_buffered_message( mbedtls_ssl_context *ssl )
5618{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005619 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker37f95322018-08-16 13:55:32 +01005620 mbedtls_ssl_hs_buffer * hs_buf;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005621 int ret = 0;
5622
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005623 if( hs == NULL )
5624 return( -1 );
5625
Hanno Beckere00ae372018-08-20 09:39:42 +01005626 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_messsage" ) );
5627
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005628 if( ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC ||
5629 ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
5630 {
5631 /* Check if we have seen a ChangeCipherSpec before.
5632 * If yes, synthesize a CCS record. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01005633 if( !hs->buffering.seen_ccs )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005634 {
5635 MBEDTLS_SSL_DEBUG_MSG( 2, ( "CCS not seen in the current flight" ) );
5636 ret = -1;
Hanno Becker0d4b3762018-08-20 09:36:59 +01005637 goto exit;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005638 }
5639
Hanno Becker39b8bc92018-08-28 17:17:13 +01005640 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Injecting buffered CCS message" ) );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005641 ssl->in_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
5642 ssl->in_msglen = 1;
5643 ssl->in_msg[0] = 1;
5644
5645 /* As long as they are equal, the exact value doesn't matter. */
5646 ssl->in_left = 0;
5647 ssl->next_record_offset = 0;
5648
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01005649 hs->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005650 goto exit;
5651 }
Hanno Becker37f95322018-08-16 13:55:32 +01005652
Hanno Beckerb8f50142018-08-28 10:01:34 +01005653#if defined(MBEDTLS_DEBUG_C)
Hanno Becker37f95322018-08-16 13:55:32 +01005654 /* Debug only */
5655 {
5656 unsigned offset;
5657 for( offset = 1; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
5658 {
5659 hs_buf = &hs->buffering.hs[offset];
5660 if( hs_buf->is_valid == 1 )
5661 {
5662 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Future message with sequence number %u %s buffered.",
5663 hs->in_msg_seq + offset,
Hanno Beckera591c482018-08-28 17:20:00 +01005664 hs_buf->is_complete ? "fully" : "partially" ) );
Hanno Becker37f95322018-08-16 13:55:32 +01005665 }
5666 }
5667 }
Hanno Beckerb8f50142018-08-28 10:01:34 +01005668#endif /* MBEDTLS_DEBUG_C */
Hanno Becker37f95322018-08-16 13:55:32 +01005669
5670 /* Check if we have buffered and/or fully reassembled the
5671 * next handshake message. */
5672 hs_buf = &hs->buffering.hs[0];
5673 if( ( hs_buf->is_valid == 1 ) && ( hs_buf->is_complete == 1 ) )
5674 {
5675 /* Synthesize a record containing the buffered HS message. */
5676 size_t msg_len = ( hs_buf->data[1] << 16 ) |
5677 ( hs_buf->data[2] << 8 ) |
5678 hs_buf->data[3];
5679
5680 /* Double-check that we haven't accidentally buffered
5681 * a message that doesn't fit into the input buffer. */
5682 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
5683 {
5684 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5685 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5686 }
5687
5688 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message has been buffered - load" ) );
5689 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered handshake message (incl. header)",
5690 hs_buf->data, msg_len + 12 );
5691
5692 ssl->in_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
5693 ssl->in_hslen = msg_len + 12;
5694 ssl->in_msglen = msg_len + 12;
5695 memcpy( ssl->in_msg, hs_buf->data, ssl->in_hslen );
5696
5697 ret = 0;
5698 goto exit;
5699 }
5700 else
5701 {
5702 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message %u not or only partially bufffered",
5703 hs->in_msg_seq ) );
5704 }
5705
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005706 ret = -1;
5707
5708exit:
5709
5710 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_message" ) );
5711 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005712}
5713
Hanno Beckera02b0b42018-08-21 17:20:27 +01005714static int ssl_buffer_make_space( mbedtls_ssl_context *ssl,
5715 size_t desired )
5716{
5717 int offset;
5718 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005719 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Attempt to free buffered messages to have %u bytes available",
5720 (unsigned) desired ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005721
Hanno Becker01315ea2018-08-21 17:22:17 +01005722 /* Get rid of future records epoch first, if such exist. */
5723 ssl_free_buffered_record( ssl );
5724
5725 /* Check if we have enough space available now. */
5726 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5727 hs->buffering.total_bytes_buffered ) )
5728 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005729 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing future epoch record" ) );
Hanno Becker01315ea2018-08-21 17:22:17 +01005730 return( 0 );
5731 }
Hanno Beckera02b0b42018-08-21 17:20:27 +01005732
Hanno Becker4f432ad2018-08-28 10:02:32 +01005733 /* We don't have enough space to buffer the next expected handshake
5734 * message. Remove buffers used for future messages to gain space,
5735 * starting with the most distant one. */
Hanno Beckera02b0b42018-08-21 17:20:27 +01005736 for( offset = MBEDTLS_SSL_MAX_BUFFERED_HS - 1;
5737 offset >= 0; offset-- )
5738 {
5739 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Free buffering slot %d to make space for reassembly of next handshake message",
5740 offset ) );
5741
Hanno Beckerb309b922018-08-23 13:18:05 +01005742 ssl_buffering_free_slot( ssl, (uint8_t) offset );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005743
5744 /* Check if we have enough space available now. */
5745 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5746 hs->buffering.total_bytes_buffered ) )
5747 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005748 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing buffered HS messages" ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005749 return( 0 );
5750 }
5751 }
5752
5753 return( -1 );
5754}
5755
Hanno Becker40f50842018-08-15 14:48:01 +01005756static int ssl_buffer_message( mbedtls_ssl_context *ssl )
5757{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005758 int ret = 0;
5759 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5760
5761 if( hs == NULL )
5762 return( 0 );
5763
5764 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_buffer_message" ) );
5765
5766 switch( ssl->in_msgtype )
5767 {
5768 case MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC:
5769 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Remember CCS message" ) );
Hanno Beckere678eaa2018-08-21 14:57:46 +01005770
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01005771 hs->buffering.seen_ccs = 1;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005772 break;
5773
5774 case MBEDTLS_SSL_MSG_HANDSHAKE:
Hanno Becker37f95322018-08-16 13:55:32 +01005775 {
5776 unsigned recv_msg_seq_offset;
5777 unsigned recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
5778 mbedtls_ssl_hs_buffer *hs_buf;
5779 size_t msg_len = ssl->in_hslen - 12;
5780
5781 /* We should never receive an old handshake
5782 * message - double-check nonetheless. */
5783 if( recv_msg_seq < ssl->handshake->in_msg_seq )
5784 {
5785 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5786 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5787 }
5788
5789 recv_msg_seq_offset = recv_msg_seq - ssl->handshake->in_msg_seq;
5790 if( recv_msg_seq_offset >= MBEDTLS_SSL_MAX_BUFFERED_HS )
5791 {
5792 /* Silently ignore -- message too far in the future */
5793 MBEDTLS_SSL_DEBUG_MSG( 2,
5794 ( "Ignore future HS message with sequence number %u, "
5795 "buffering window %u - %u",
5796 recv_msg_seq, ssl->handshake->in_msg_seq,
5797 ssl->handshake->in_msg_seq + MBEDTLS_SSL_MAX_BUFFERED_HS - 1 ) );
5798
5799 goto exit;
5800 }
5801
5802 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering HS message with sequence number %u, offset %u ",
5803 recv_msg_seq, recv_msg_seq_offset ) );
5804
5805 hs_buf = &hs->buffering.hs[ recv_msg_seq_offset ];
5806
5807 /* Check if the buffering for this seq nr has already commenced. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01005808 if( !hs_buf->is_valid )
Hanno Becker37f95322018-08-16 13:55:32 +01005809 {
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005810 size_t reassembly_buf_sz;
5811
Hanno Becker37f95322018-08-16 13:55:32 +01005812 hs_buf->is_fragmented =
5813 ( ssl_hs_is_proper_fragment( ssl ) == 1 );
5814
5815 /* We copy the message back into the input buffer
5816 * after reassembly, so check that it's not too large.
5817 * This is an implementation-specific limitation
5818 * and not one from the standard, hence it is not
5819 * checked in ssl_check_hs_header(). */
Hanno Becker96a6c692018-08-21 15:56:03 +01005820 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker37f95322018-08-16 13:55:32 +01005821 {
5822 /* Ignore message */
5823 goto exit;
5824 }
5825
Hanno Beckere0b150f2018-08-21 15:51:03 +01005826 /* Check if we have enough space to buffer the message. */
5827 if( hs->buffering.total_bytes_buffered >
5828 MBEDTLS_SSL_DTLS_MAX_BUFFERING )
5829 {
5830 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5831 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5832 }
5833
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005834 reassembly_buf_sz = ssl_get_reassembly_buffer_size( msg_len,
5835 hs_buf->is_fragmented );
Hanno Beckere0b150f2018-08-21 15:51:03 +01005836
5837 if( reassembly_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5838 hs->buffering.total_bytes_buffered ) )
5839 {
5840 if( recv_msg_seq_offset > 0 )
5841 {
5842 /* If we can't buffer a future message because
5843 * of space limitations -- ignore. */
5844 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",
5845 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5846 (unsigned) hs->buffering.total_bytes_buffered ) );
5847 goto exit;
5848 }
Hanno Beckere1801392018-08-21 16:51:05 +01005849 else
5850 {
5851 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",
5852 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5853 (unsigned) hs->buffering.total_bytes_buffered ) );
5854 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005855
Hanno Beckera02b0b42018-08-21 17:20:27 +01005856 if( ssl_buffer_make_space( ssl, reassembly_buf_sz ) != 0 )
Hanno Becker55e9e2a2018-08-21 16:07:55 +01005857 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005858 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",
5859 (unsigned) msg_len,
5860 (unsigned) reassembly_buf_sz,
5861 MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Hanno Beckere0b150f2018-08-21 15:51:03 +01005862 (unsigned) hs->buffering.total_bytes_buffered ) );
Hanno Becker55e9e2a2018-08-21 16:07:55 +01005863 ret = MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
5864 goto exit;
5865 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005866 }
5867
5868 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %d",
5869 msg_len ) );
5870
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005871 hs_buf->data = mbedtls_calloc( 1, reassembly_buf_sz );
5872 if( hs_buf->data == NULL )
Hanno Becker37f95322018-08-16 13:55:32 +01005873 {
Hanno Beckere0b150f2018-08-21 15:51:03 +01005874 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
Hanno Becker37f95322018-08-16 13:55:32 +01005875 goto exit;
5876 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005877 hs_buf->data_len = reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01005878
5879 /* Prepare final header: copy msg_type, length and message_seq,
5880 * then add standardised fragment_offset and fragment_length */
5881 memcpy( hs_buf->data, ssl->in_msg, 6 );
5882 memset( hs_buf->data + 6, 0, 3 );
5883 memcpy( hs_buf->data + 9, hs_buf->data + 1, 3 );
5884
5885 hs_buf->is_valid = 1;
Hanno Beckere0b150f2018-08-21 15:51:03 +01005886
5887 hs->buffering.total_bytes_buffered += reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01005888 }
5889 else
5890 {
5891 /* Make sure msg_type and length are consistent */
5892 if( memcmp( hs_buf->data, ssl->in_msg, 4 ) != 0 )
5893 {
5894 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Fragment header mismatch - ignore" ) );
5895 /* Ignore */
5896 goto exit;
5897 }
5898 }
5899
Hanno Becker4422bbb2018-08-20 09:40:19 +01005900 if( !hs_buf->is_complete )
Hanno Becker37f95322018-08-16 13:55:32 +01005901 {
5902 size_t frag_len, frag_off;
5903 unsigned char * const msg = hs_buf->data + 12;
5904
5905 /*
5906 * Check and copy current fragment
5907 */
5908
5909 /* Validation of header fields already done in
5910 * mbedtls_ssl_prepare_handshake_record(). */
5911 frag_off = ssl_get_hs_frag_off( ssl );
5912 frag_len = ssl_get_hs_frag_len( ssl );
5913
5914 MBEDTLS_SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %d, length = %d",
5915 frag_off, frag_len ) );
5916 memcpy( msg + frag_off, ssl->in_msg + 12, frag_len );
5917
5918 if( hs_buf->is_fragmented )
5919 {
5920 unsigned char * const bitmask = msg + msg_len;
5921 ssl_bitmask_set( bitmask, frag_off, frag_len );
5922 hs_buf->is_complete = ( ssl_bitmask_check( bitmask,
5923 msg_len ) == 0 );
5924 }
5925 else
5926 {
5927 hs_buf->is_complete = 1;
5928 }
5929
5930 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message %scomplete",
5931 hs_buf->is_complete ? "" : "not yet " ) );
5932 }
5933
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005934 break;
Hanno Becker37f95322018-08-16 13:55:32 +01005935 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005936
5937 default:
Hanno Becker360bef32018-08-28 10:04:33 +01005938 /* We don't buffer other types of messages. */
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005939 break;
5940 }
5941
5942exit:
5943
5944 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_buffer_message" ) );
5945 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005946}
5947#endif /* MBEDTLS_SSL_PROTO_DTLS */
5948
Hanno Becker1097b342018-08-15 14:09:41 +01005949static int ssl_consume_current_message( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005950{
Hanno Becker4a810fb2017-05-24 16:27:30 +01005951 /*
Hanno Becker4a810fb2017-05-24 16:27:30 +01005952 * Consume last content-layer message and potentially
5953 * update in_msglen which keeps track of the contents'
5954 * consumption state.
5955 *
5956 * (1) Handshake messages:
5957 * Remove last handshake message, move content
5958 * and adapt in_msglen.
5959 *
5960 * (2) Alert messages:
5961 * Consume whole record content, in_msglen = 0.
5962 *
Hanno Becker4a810fb2017-05-24 16:27:30 +01005963 * (3) Change cipher spec:
5964 * Consume whole record content, in_msglen = 0.
5965 *
5966 * (4) Application data:
5967 * Don't do anything - the record layer provides
5968 * the application data as a stream transport
5969 * and consumes through mbedtls_ssl_read only.
5970 *
5971 */
5972
5973 /* Case (1): Handshake messages */
5974 if( ssl->in_hslen != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005975 {
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01005976 /* Hard assertion to be sure that no application data
5977 * is in flight, as corrupting ssl->in_msglen during
5978 * ssl->in_offt != NULL is fatal. */
5979 if( ssl->in_offt != NULL )
5980 {
5981 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5982 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5983 }
5984
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005985 /*
5986 * Get next Handshake message in the current record
5987 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005988
Hanno Becker4a810fb2017-05-24 16:27:30 +01005989 /* Notes:
Hanno Beckere72489d2017-10-23 13:23:50 +01005990 * (1) in_hslen is not necessarily the size of the
Hanno Becker4a810fb2017-05-24 16:27:30 +01005991 * current handshake content: If DTLS handshake
5992 * fragmentation is used, that's the fragment
5993 * size instead. Using the total handshake message
Hanno Beckere72489d2017-10-23 13:23:50 +01005994 * size here is faulty and should be changed at
5995 * some point.
Hanno Becker4a810fb2017-05-24 16:27:30 +01005996 * (2) While it doesn't seem to cause problems, one
5997 * has to be very careful not to assume that in_hslen
5998 * is always <= in_msglen in a sensible communication.
5999 * Again, it's wrong for DTLS handshake fragmentation.
6000 * The following check is therefore mandatory, and
6001 * should not be treated as a silently corrected assertion.
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01006002 * Additionally, ssl->in_hslen might be arbitrarily out of
6003 * bounds after handling a DTLS message with an unexpected
6004 * sequence number, see mbedtls_ssl_prepare_handshake_record.
Hanno Becker4a810fb2017-05-24 16:27:30 +01006005 */
6006 if( ssl->in_hslen < ssl->in_msglen )
6007 {
6008 ssl->in_msglen -= ssl->in_hslen;
6009 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
6010 ssl->in_msglen );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006011
Hanno Becker4a810fb2017-05-24 16:27:30 +01006012 MBEDTLS_SSL_DEBUG_BUF( 4, "remaining content in record",
6013 ssl->in_msg, ssl->in_msglen );
6014 }
6015 else
6016 {
6017 ssl->in_msglen = 0;
6018 }
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02006019
Hanno Becker4a810fb2017-05-24 16:27:30 +01006020 ssl->in_hslen = 0;
6021 }
6022 /* Case (4): Application data */
6023 else if( ssl->in_offt != NULL )
6024 {
6025 return( 0 );
6026 }
6027 /* Everything else (CCS & Alerts) */
6028 else
6029 {
6030 ssl->in_msglen = 0;
6031 }
6032
Hanno Becker1097b342018-08-15 14:09:41 +01006033 return( 0 );
6034}
Hanno Becker4a810fb2017-05-24 16:27:30 +01006035
Hanno Beckere74d5562018-08-15 14:26:08 +01006036static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl )
6037{
Hanno Becker4a810fb2017-05-24 16:27:30 +01006038 if( ssl->in_msglen > 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01006039 return( 1 );
6040
6041 return( 0 );
6042}
6043
Hanno Becker5f066e72018-08-16 14:56:31 +01006044#if defined(MBEDTLS_SSL_PROTO_DTLS)
6045
6046static void ssl_free_buffered_record( mbedtls_ssl_context *ssl )
6047{
6048 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
6049 if( hs == NULL )
6050 return;
6051
Hanno Becker01315ea2018-08-21 17:22:17 +01006052 if( hs->buffering.future_record.data != NULL )
Hanno Becker4a810fb2017-05-24 16:27:30 +01006053 {
Hanno Becker01315ea2018-08-21 17:22:17 +01006054 hs->buffering.total_bytes_buffered -=
6055 hs->buffering.future_record.len;
6056
6057 mbedtls_free( hs->buffering.future_record.data );
6058 hs->buffering.future_record.data = NULL;
6059 }
Hanno Becker5f066e72018-08-16 14:56:31 +01006060}
6061
6062static int ssl_load_buffered_record( mbedtls_ssl_context *ssl )
6063{
6064 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
6065 unsigned char * rec;
6066 size_t rec_len;
6067 unsigned rec_epoch;
6068
6069 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
6070 return( 0 );
6071
6072 if( hs == NULL )
6073 return( 0 );
6074
Hanno Becker5f066e72018-08-16 14:56:31 +01006075 rec = hs->buffering.future_record.data;
6076 rec_len = hs->buffering.future_record.len;
6077 rec_epoch = hs->buffering.future_record.epoch;
6078
6079 if( rec == NULL )
6080 return( 0 );
6081
Hanno Becker4cb782d2018-08-20 11:19:05 +01006082 /* Only consider loading future records if the
6083 * input buffer is empty. */
Hanno Beckeref7afdf2018-08-28 17:16:31 +01006084 if( ssl_next_record_is_in_datagram( ssl ) == 1 )
Hanno Becker4cb782d2018-08-20 11:19:05 +01006085 return( 0 );
6086
Hanno Becker5f066e72018-08-16 14:56:31 +01006087 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_record" ) );
6088
6089 if( rec_epoch != ssl->in_epoch )
6090 {
6091 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffered record not from current epoch." ) );
6092 goto exit;
6093 }
6094
6095 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Found buffered record from current epoch - load" ) );
6096
6097 /* Double-check that the record is not too large */
6098 if( rec_len > MBEDTLS_SSL_IN_BUFFER_LEN -
6099 (size_t)( ssl->in_hdr - ssl->in_buf ) )
6100 {
6101 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6102 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6103 }
6104
6105 memcpy( ssl->in_hdr, rec, rec_len );
6106 ssl->in_left = rec_len;
6107 ssl->next_record_offset = 0;
6108
6109 ssl_free_buffered_record( ssl );
6110
6111exit:
6112 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_record" ) );
6113 return( 0 );
6114}
6115
Hanno Becker519f15d2019-07-11 12:43:20 +01006116static int ssl_buffer_future_record( mbedtls_ssl_context *ssl,
6117 mbedtls_record const *rec )
Hanno Becker5f066e72018-08-16 14:56:31 +01006118{
6119 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker5f066e72018-08-16 14:56:31 +01006120
6121 /* Don't buffer future records outside handshakes. */
6122 if( hs == NULL )
6123 return( 0 );
6124
6125 /* Only buffer handshake records (we are only interested
6126 * in Finished messages). */
Hanno Becker519f15d2019-07-11 12:43:20 +01006127 if( rec->type != MBEDTLS_SSL_MSG_HANDSHAKE )
Hanno Becker5f066e72018-08-16 14:56:31 +01006128 return( 0 );
6129
6130 /* Don't buffer more than one future epoch record. */
6131 if( hs->buffering.future_record.data != NULL )
6132 return( 0 );
6133
Hanno Becker01315ea2018-08-21 17:22:17 +01006134 /* Don't buffer record if there's not enough buffering space remaining. */
Hanno Becker519f15d2019-07-11 12:43:20 +01006135 if( rec->buf_len > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
Hanno Becker01315ea2018-08-21 17:22:17 +01006136 hs->buffering.total_bytes_buffered ) )
6137 {
6138 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 +01006139 (unsigned) rec->buf_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Hanno Becker01315ea2018-08-21 17:22:17 +01006140 (unsigned) hs->buffering.total_bytes_buffered ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006141 return( 0 );
6142 }
6143
Hanno Becker5f066e72018-08-16 14:56:31 +01006144 /* Buffer record */
6145 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffer record from epoch %u",
6146 ssl->in_epoch + 1 ) );
Hanno Becker519f15d2019-07-11 12:43:20 +01006147 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered record", rec->buf, rec->buf_len );
Hanno Becker5f066e72018-08-16 14:56:31 +01006148
6149 /* ssl_parse_record_header() only considers records
6150 * of the next epoch as candidates for buffering. */
6151 hs->buffering.future_record.epoch = ssl->in_epoch + 1;
Hanno Becker519f15d2019-07-11 12:43:20 +01006152 hs->buffering.future_record.len = rec->buf_len;
Hanno Becker5f066e72018-08-16 14:56:31 +01006153
6154 hs->buffering.future_record.data =
6155 mbedtls_calloc( 1, hs->buffering.future_record.len );
6156 if( hs->buffering.future_record.data == NULL )
6157 {
6158 /* If we run out of RAM trying to buffer a
6159 * record from the next epoch, just ignore. */
6160 return( 0 );
6161 }
6162
Hanno Becker519f15d2019-07-11 12:43:20 +01006163 memcpy( hs->buffering.future_record.data, rec->buf, rec->buf_len );
Hanno Becker5f066e72018-08-16 14:56:31 +01006164
Hanno Becker519f15d2019-07-11 12:43:20 +01006165 hs->buffering.total_bytes_buffered += rec->buf_len;
Hanno Becker5f066e72018-08-16 14:56:31 +01006166 return( 0 );
6167}
6168
6169#endif /* MBEDTLS_SSL_PROTO_DTLS */
6170
Hanno Beckere74d5562018-08-15 14:26:08 +01006171static int ssl_get_next_record( mbedtls_ssl_context *ssl )
Hanno Becker1097b342018-08-15 14:09:41 +01006172{
6173 int ret;
Hanno Beckere5e7e782019-07-11 12:29:35 +01006174 mbedtls_record rec;
Hanno Becker1097b342018-08-15 14:09:41 +01006175
Hanno Becker5f066e72018-08-16 14:56:31 +01006176#if defined(MBEDTLS_SSL_PROTO_DTLS)
6177 /* We might have buffered a future record; if so,
6178 * and if the epoch matches now, load it.
6179 * On success, this call will set ssl->in_left to
6180 * the length of the buffered record, so that
6181 * the calls to ssl_fetch_input() below will
6182 * essentially be no-ops. */
6183 ret = ssl_load_buffered_record( ssl );
6184 if( ret != 0 )
6185 return( ret );
6186#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker4a810fb2017-05-24 16:27:30 +01006187
Hanno Beckerca59c2b2019-05-08 12:03:28 +01006188 /* Ensure that we have enough space available for the default form
6189 * of TLS / DTLS record headers (5 Bytes for TLS, 13 Bytes for DTLS,
6190 * with no space for CIDs counted in). */
6191 ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_in_hdr_len( ssl ) );
6192 if( ret != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006193 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006194 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006195 return( ret );
6196 }
6197
Hanno Beckere5e7e782019-07-11 12:29:35 +01006198 ret = ssl_parse_record_header( ssl, ssl->in_hdr, ssl->in_left, &rec );
6199 if( ret != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006200 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006201#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker2fddd372019-07-10 14:37:41 +01006202 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006203 {
Hanno Becker5f066e72018-08-16 14:56:31 +01006204 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
6205 {
Hanno Becker519f15d2019-07-11 12:43:20 +01006206 ret = ssl_buffer_future_record( ssl, &rec );
Hanno Becker5f066e72018-08-16 14:56:31 +01006207 if( ret != 0 )
6208 return( ret );
6209
6210 /* Fall through to handling of unexpected records */
6211 ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
6212 }
6213
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01006214 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD )
6215 {
Hanno Becker2fddd372019-07-10 14:37:41 +01006216#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Hanno Beckerd8bf8ce2019-07-12 09:23:47 +01006217 /* Reset in pointers to default state for TLS/DTLS records,
6218 * assuming no CID and no offset between record content and
6219 * record plaintext. */
6220 ssl_update_in_pointers( ssl );
6221
Hanno Becker7ae20e02019-07-12 08:33:49 +01006222 /* Setup internal message pointers from record structure. */
6223 ssl->in_msgtype = rec.type;
6224#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
6225 ssl->in_len = ssl->in_cid + rec.cid_len;
6226#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
6227 ssl->in_iv = ssl->in_msg = ssl->in_len + 2;
6228 ssl->in_msglen = rec.data_len;
6229
Hanno Becker2fddd372019-07-10 14:37:41 +01006230 ret = ssl_check_client_reconnect( ssl );
6231 if( ret != 0 )
6232 return( ret );
6233#endif
6234
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01006235 /* Skip unexpected record (but not whole datagram) */
Hanno Becker4acada32019-07-11 12:48:53 +01006236 ssl->next_record_offset = rec.buf_len;
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006237
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01006238 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding unexpected record "
6239 "(header)" ) );
6240 }
6241 else
6242 {
6243 /* Skip invalid record and the rest of the datagram */
6244 ssl->next_record_offset = 0;
6245 ssl->in_left = 0;
6246
6247 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record "
6248 "(header)" ) );
6249 }
6250
6251 /* Get next record */
Hanno Becker90333da2017-10-10 11:27:13 +01006252 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006253 }
Hanno Becker2fddd372019-07-10 14:37:41 +01006254 else
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006255#endif
Hanno Becker2fddd372019-07-10 14:37:41 +01006256 {
6257 return( ret );
6258 }
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006259 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006260
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006261#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006262 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Beckere65ce782017-05-22 14:47:48 +01006263 {
Hanno Beckera8814792019-07-10 15:01:45 +01006264 /* Remember offset of next record within datagram. */
Hanno Beckerf50da502019-07-11 12:50:10 +01006265 ssl->next_record_offset = rec.buf_len;
Hanno Beckere65ce782017-05-22 14:47:48 +01006266 if( ssl->next_record_offset < ssl->in_left )
6267 {
6268 MBEDTLS_SSL_DEBUG_MSG( 3, ( "more than one record within datagram" ) );
6269 }
6270 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006271 else
6272#endif
Hanno Beckera8814792019-07-10 15:01:45 +01006273 {
Hanno Becker955a5c92019-07-10 17:12:07 +01006274 /*
6275 * Fetch record contents from underlying transport.
6276 */
Hanno Beckera3175662019-07-11 12:50:29 +01006277 ret = mbedtls_ssl_fetch_input( ssl, rec.buf_len );
Hanno Beckera8814792019-07-10 15:01:45 +01006278 if( ret != 0 )
6279 {
6280 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
6281 return( ret );
6282 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006283
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006284 ssl->in_left = 0;
Hanno Beckera8814792019-07-10 15:01:45 +01006285 }
6286
6287 /*
6288 * Decrypt record contents.
6289 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006290
Hanno Beckerfdf66042019-07-11 13:07:45 +01006291 if( ( ret = ssl_prepare_record_content( ssl, &rec ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006292 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006293#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006294 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006295 {
6296 /* Silently discard invalid records */
Hanno Becker82e2a392019-05-03 16:36:59 +01006297 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006298 {
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02006299 /* Except when waiting for Finished as a bad mac here
6300 * probably means something went wrong in the handshake
6301 * (eg wrong psk used, mitm downgrade attempt, etc.) */
6302 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED ||
6303 ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
6304 {
6305#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
6306 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
6307 {
6308 mbedtls_ssl_send_alert_message( ssl,
6309 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6310 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
6311 }
6312#endif
6313 return( ret );
6314 }
6315
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006316#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006317 if( ssl->conf->badmac_limit != 0 &&
6318 ++ssl->badmac_seen >= ssl->conf->badmac_limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02006319 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006320 MBEDTLS_SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) );
6321 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02006322 }
6323#endif
6324
Hanno Becker4a810fb2017-05-24 16:27:30 +01006325 /* As above, invalid records cause
6326 * dismissal of the whole datagram. */
6327
6328 ssl->next_record_offset = 0;
6329 ssl->in_left = 0;
6330
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006331 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record (mac)" ) );
Hanno Becker90333da2017-10-10 11:27:13 +01006332 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006333 }
6334
6335 return( ret );
6336 }
6337 else
6338#endif
6339 {
6340 /* Error out (and send alert) on invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006341#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
6342 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006343 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006344 mbedtls_ssl_send_alert_message( ssl,
6345 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6346 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006347 }
6348#endif
6349 return( ret );
6350 }
6351 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006352
Hanno Becker44d89b22019-07-12 09:40:44 +01006353
6354 /* Reset in pointers to default state for TLS/DTLS records,
6355 * assuming no CID and no offset between record content and
6356 * record plaintext. */
6357 ssl_update_in_pointers( ssl );
Hanno Becker44d89b22019-07-12 09:40:44 +01006358#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
6359 ssl->in_len = ssl->in_cid + rec.cid_len;
6360#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
6361 ssl->in_iv = ssl->in_msg = ssl->in_len + 2;
Hanno Becker44d89b22019-07-12 09:40:44 +01006362
Hanno Becker8685c822019-07-12 09:37:30 +01006363 /* The record content type may change during decryption,
6364 * so re-read it. */
6365 ssl->in_msgtype = rec.type;
6366 /* Also update the input buffer, because unfortunately
6367 * the server-side ssl_parse_client_hello() reparses the
6368 * record header when receiving a ClientHello initiating
6369 * a renegotiation. */
6370 ssl->in_hdr[0] = rec.type;
6371 ssl->in_msg = rec.buf + rec.data_offset;
6372 ssl->in_msglen = rec.data_len;
6373 ssl->in_len[0] = (unsigned char)( rec.data_len >> 8 );
6374 ssl->in_len[1] = (unsigned char)( rec.data_len );
6375
Simon Butcher99000142016-10-13 17:21:01 +01006376 return( 0 );
6377}
6378
6379int mbedtls_ssl_handle_message_type( mbedtls_ssl_context *ssl )
6380{
6381 int ret;
6382
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006383 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006384 * Handle particular types of records
6385 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006386 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00006387 {
Simon Butcher99000142016-10-13 17:21:01 +01006388 if( ( ret = mbedtls_ssl_prepare_handshake_record( ssl ) ) != 0 )
6389 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01006390 return( ret );
Simon Butcher99000142016-10-13 17:21:01 +01006391 }
Paul Bakker5121ce52009-01-03 21:22:43 +00006392 }
6393
Hanno Beckere678eaa2018-08-21 14:57:46 +01006394 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006395 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01006396 if( ssl->in_msglen != 1 )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006397 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01006398 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, len: %d",
6399 ssl->in_msglen ) );
6400 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006401 }
6402
Hanno Beckere678eaa2018-08-21 14:57:46 +01006403 if( ssl->in_msg[0] != 1 )
6404 {
6405 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, content: %02x",
6406 ssl->in_msg[0] ) );
6407 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
6408 }
6409
6410#if defined(MBEDTLS_SSL_PROTO_DTLS)
6411 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
6412 ssl->state != MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC &&
6413 ssl->state != MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
6414 {
6415 if( ssl->handshake == NULL )
6416 {
6417 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping ChangeCipherSpec outside handshake" ) );
6418 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
6419 }
6420
6421 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received out-of-order ChangeCipherSpec - remember" ) );
6422 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
6423 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006424#endif
Hanno Beckere678eaa2018-08-21 14:57:46 +01006425 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006426
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006427 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00006428 {
Angus Gratton1a7a17e2018-06-20 15:43:50 +10006429 if( ssl->in_msglen != 2 )
6430 {
6431 /* Note: Standard allows for more than one 2 byte alert
6432 to be packed in a single message, but Mbed TLS doesn't
6433 currently support this. */
6434 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid alert message, len: %d",
6435 ssl->in_msglen ) );
6436 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
6437 }
6438
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006439 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
Paul Bakker5121ce52009-01-03 21:22:43 +00006440 ssl->in_msg[0], ssl->in_msg[1] ) );
6441
6442 /*
Simon Butcher459a9502015-10-27 16:09:03 +00006443 * Ignore non-fatal alerts, except close_notify and no_renegotiation
Paul Bakker5121ce52009-01-03 21:22:43 +00006444 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006445 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006446 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006447 MBEDTLS_SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
Paul Bakker2770fbd2012-07-03 13:30:23 +00006448 ssl->in_msg[1] ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006449 return( MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006450 }
6451
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006452 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6453 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00006454 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006455 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
6456 return( MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00006457 }
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02006458
6459#if defined(MBEDTLS_SSL_RENEGOTIATION_ENABLED)
6460 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6461 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION )
6462 {
Hanno Becker90333da2017-10-10 11:27:13 +01006463 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no renegotiation alert" ) );
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02006464 /* Will be handled when trying to parse ServerHello */
6465 return( 0 );
6466 }
6467#endif
6468
6469#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_SRV_C)
6470 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
6471 ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
6472 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6473 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
6474 {
6475 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no_cert" ) );
6476 /* Will be handled in mbedtls_ssl_parse_certificate() */
6477 return( 0 );
6478 }
6479#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
6480
6481 /* Silently ignore: fetch new message */
Simon Butcher99000142016-10-13 17:21:01 +01006482 return MBEDTLS_ERR_SSL_NON_FATAL;
Paul Bakker5121ce52009-01-03 21:22:43 +00006483 }
6484
Hanno Beckerc76c6192017-06-06 10:03:17 +01006485#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker37ae9522019-05-03 16:54:26 +01006486 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Beckerc76c6192017-06-06 10:03:17 +01006487 {
Hanno Becker37ae9522019-05-03 16:54:26 +01006488 /* Drop unexpected ApplicationData records,
6489 * except at the beginning of renegotiations */
6490 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA &&
6491 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER
6492#if defined(MBEDTLS_SSL_RENEGOTIATION)
6493 && ! ( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
6494 ssl->state == MBEDTLS_SSL_SERVER_HELLO )
Hanno Beckerc76c6192017-06-06 10:03:17 +01006495#endif
Hanno Becker37ae9522019-05-03 16:54:26 +01006496 )
6497 {
6498 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) );
6499 return( MBEDTLS_ERR_SSL_NON_FATAL );
6500 }
6501
6502 if( ssl->handshake != NULL &&
6503 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
6504 {
6505 ssl_handshake_wrapup_free_hs_transform( ssl );
6506 }
6507 }
Hanno Becker4a4af9f2019-05-08 16:26:21 +01006508#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Beckerc76c6192017-06-06 10:03:17 +01006509
Paul Bakker5121ce52009-01-03 21:22:43 +00006510 return( 0 );
6511}
6512
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006513int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00006514{
6515 int ret;
6516
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006517 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
6518 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6519 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00006520 {
6521 return( ret );
6522 }
6523
6524 return( 0 );
6525}
6526
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006527int mbedtls_ssl_send_alert_message( mbedtls_ssl_context *ssl,
Paul Bakker0a925182012-04-16 06:46:41 +00006528 unsigned char level,
6529 unsigned char message )
6530{
6531 int ret;
6532
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02006533 if( ssl == NULL || ssl->conf == NULL )
6534 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6535
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006536 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006537 MBEDTLS_SSL_DEBUG_MSG( 3, ( "send alert level=%u message=%u", level, message ));
Paul Bakker0a925182012-04-16 06:46:41 +00006538
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006539 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
Paul Bakker0a925182012-04-16 06:46:41 +00006540 ssl->out_msglen = 2;
6541 ssl->out_msg[0] = level;
6542 ssl->out_msg[1] = message;
6543
Hanno Becker67bc7c32018-08-06 11:33:50 +01006544 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00006545 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006546 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker0a925182012-04-16 06:46:41 +00006547 return( ret );
6548 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006549 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
Paul Bakker0a925182012-04-16 06:46:41 +00006550
6551 return( 0 );
6552}
6553
Hanno Beckerb9d44792019-02-08 07:19:04 +00006554#if defined(MBEDTLS_X509_CRT_PARSE_C)
6555static void ssl_clear_peer_cert( mbedtls_ssl_session *session )
6556{
6557#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6558 if( session->peer_cert != NULL )
6559 {
6560 mbedtls_x509_crt_free( session->peer_cert );
6561 mbedtls_free( session->peer_cert );
6562 session->peer_cert = NULL;
6563 }
6564#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6565 if( session->peer_cert_digest != NULL )
6566 {
6567 /* Zeroization is not necessary. */
6568 mbedtls_free( session->peer_cert_digest );
6569 session->peer_cert_digest = NULL;
6570 session->peer_cert_digest_type = MBEDTLS_MD_NONE;
6571 session->peer_cert_digest_len = 0;
6572 }
6573#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6574}
6575#endif /* MBEDTLS_X509_CRT_PARSE_C */
6576
Paul Bakker5121ce52009-01-03 21:22:43 +00006577/*
6578 * Handshake functions
6579 */
Hanno Becker21489932019-02-05 13:20:55 +00006580#if !defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Gilles Peskinef9828522017-05-03 12:28:43 +02006581/* No certificate support -> dummy functions */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006582int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006583{
Hanno Beckere694c3e2017-12-27 21:34:08 +00006584 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6585 ssl->handshake->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00006586
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006587 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006588
Hanno Becker7177a882019-02-05 13:36:46 +00006589 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02006590 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006591 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02006592 ssl->state++;
6593 return( 0 );
6594 }
6595
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006596 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6597 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006598}
6599
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006600int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006601{
Hanno Beckere694c3e2017-12-27 21:34:08 +00006602 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6603 ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006604
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006605 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006606
Hanno Becker7177a882019-02-05 13:36:46 +00006607 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006608 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006609 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006610 ssl->state++;
6611 return( 0 );
6612 }
6613
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006614 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6615 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006616}
Gilles Peskinef9828522017-05-03 12:28:43 +02006617
Hanno Becker21489932019-02-05 13:20:55 +00006618#else /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Gilles Peskinef9828522017-05-03 12:28:43 +02006619/* Some certificate support -> implement write and parse */
6620
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006621int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006622{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006623 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006624 size_t i, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006625 const mbedtls_x509_crt *crt;
Hanno Beckere694c3e2017-12-27 21:34:08 +00006626 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6627 ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006628
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006629 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006630
Hanno Becker7177a882019-02-05 13:36:46 +00006631 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006632 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006633 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006634 ssl->state++;
6635 return( 0 );
6636 }
6637
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006638#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006639 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00006640 {
6641 if( ssl->client_auth == 0 )
6642 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006643 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006644 ssl->state++;
6645 return( 0 );
6646 }
6647
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006648#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00006649 /*
6650 * If using SSLv3 and got no cert, send an Alert message
6651 * (otherwise an empty Certificate message will be sent).
6652 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006653 if( mbedtls_ssl_own_cert( ssl ) == NULL &&
6654 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006655 {
6656 ssl->out_msglen = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006657 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
6658 ssl->out_msg[0] = MBEDTLS_SSL_ALERT_LEVEL_WARNING;
6659 ssl->out_msg[1] = MBEDTLS_SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00006660
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006661 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006662 goto write_msg;
6663 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006664#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006665 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006666#endif /* MBEDTLS_SSL_CLI_C */
6667#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006668 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00006669 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006670 if( mbedtls_ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006671 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006672 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
6673 return( MBEDTLS_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00006674 }
6675 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006676#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006677
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006678 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006679
6680 /*
6681 * 0 . 0 handshake type
6682 * 1 . 3 handshake length
6683 * 4 . 6 length of all certs
6684 * 7 . 9 length of cert. 1
6685 * 10 . n-1 peer certificate
6686 * n . n+2 length of cert. 2
6687 * n+3 . ... upper level cert, etc.
6688 */
6689 i = 7;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006690 crt = mbedtls_ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00006691
Paul Bakker29087132010-03-21 21:03:34 +00006692 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006693 {
6694 n = crt->raw.len;
Angus Grattond8213d02016-05-25 20:56:48 +10006695 if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00006696 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006697 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
Angus Grattond8213d02016-05-25 20:56:48 +10006698 i + 3 + n, MBEDTLS_SSL_OUT_CONTENT_LEN ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006699 return( MBEDTLS_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006700 }
6701
6702 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
6703 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
6704 ssl->out_msg[i + 2] = (unsigned char)( n );
6705
6706 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
6707 i += n; crt = crt->next;
6708 }
6709
6710 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
6711 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
6712 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
6713
6714 ssl->out_msglen = i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006715 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
6716 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
Paul Bakker5121ce52009-01-03 21:22:43 +00006717
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02006718#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00006719write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006720#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006721
6722 ssl->state++;
6723
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006724 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006725 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006726 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006727 return( ret );
6728 }
6729
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006730 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006731
Paul Bakkered27a042013-04-18 22:46:23 +02006732 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006733}
6734
Hanno Becker84879e32019-01-31 07:44:03 +00006735#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Hanno Becker177475a2019-02-05 17:02:46 +00006736
6737#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006738static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6739 unsigned char *crt_buf,
6740 size_t crt_buf_len )
6741{
6742 mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert;
6743
6744 if( peer_crt == NULL )
6745 return( -1 );
6746
6747 if( peer_crt->raw.len != crt_buf_len )
6748 return( -1 );
6749
Hanno Becker46f34d02019-02-08 14:00:04 +00006750 return( memcmp( peer_crt->raw.p, crt_buf, crt_buf_len ) );
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006751}
Hanno Becker177475a2019-02-05 17:02:46 +00006752#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6753static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6754 unsigned char *crt_buf,
6755 size_t crt_buf_len )
6756{
6757 int ret;
6758 unsigned char const * const peer_cert_digest =
6759 ssl->session->peer_cert_digest;
6760 mbedtls_md_type_t const peer_cert_digest_type =
6761 ssl->session->peer_cert_digest_type;
6762 mbedtls_md_info_t const * const digest_info =
6763 mbedtls_md_info_from_type( peer_cert_digest_type );
6764 unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN];
6765 size_t digest_len;
6766
6767 if( peer_cert_digest == NULL || digest_info == NULL )
6768 return( -1 );
6769
6770 digest_len = mbedtls_md_get_size( digest_info );
6771 if( digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN )
6772 return( -1 );
6773
6774 ret = mbedtls_md( digest_info, crt_buf, crt_buf_len, tmp_digest );
6775 if( ret != 0 )
6776 return( -1 );
6777
6778 return( memcmp( tmp_digest, peer_cert_digest, digest_len ) );
6779}
6780#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker84879e32019-01-31 07:44:03 +00006781#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006782
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006783/*
6784 * Once the certificate message is read, parse it into a cert chain and
6785 * perform basic checks, but leave actual verification to the caller
6786 */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006787static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl,
6788 mbedtls_x509_crt *chain )
Paul Bakker5121ce52009-01-03 21:22:43 +00006789{
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006790 int ret;
6791#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6792 int crt_cnt=0;
6793#endif
Paul Bakker23986e52011-04-24 08:57:21 +00006794 size_t i, n;
Gilles Peskine064a85c2017-05-10 10:46:40 +02006795 uint8_t alert;
Paul Bakker5121ce52009-01-03 21:22:43 +00006796
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006797 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00006798 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006799 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006800 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6801 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006802 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006803 }
6804
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006805 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE ||
6806 ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006807 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006808 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006809 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6810 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006811 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006812 }
6813
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006814 i = mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006815
Paul Bakker5121ce52009-01-03 21:22:43 +00006816 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006817 * Same message structure as in mbedtls_ssl_write_certificate()
Paul Bakker5121ce52009-01-03 21:22:43 +00006818 */
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006819 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
Paul Bakker5121ce52009-01-03 21:22:43 +00006820
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006821 if( ssl->in_msg[i] != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006822 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00006823 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006824 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006825 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6826 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006827 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006828 }
6829
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006830 /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */
6831 i += 3;
6832
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006833 /* Iterate through and parse the CRTs in the provided chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006834 while( i < ssl->in_hslen )
6835 {
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006836 /* Check that there's room for the next CRT's length fields. */
Philippe Antoine747fd532018-05-30 09:13:21 +02006837 if ( i + 3 > ssl->in_hslen ) {
6838 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckere2734e22019-01-31 07:44:17 +00006839 mbedtls_ssl_send_alert_message( ssl,
6840 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6841 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Philippe Antoine747fd532018-05-30 09:13:21 +02006842 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
6843 }
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006844 /* In theory, the CRT can be up to 2**24 Bytes, but we don't support
6845 * anything beyond 2**16 ~ 64K. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006846 if( ssl->in_msg[i] != 0 )
6847 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006848 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckere2734e22019-01-31 07:44:17 +00006849 mbedtls_ssl_send_alert_message( ssl,
6850 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6851 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006852 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006853 }
6854
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006855 /* Read length of the next CRT in the chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006856 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
6857 | (unsigned int) ssl->in_msg[i + 2];
6858 i += 3;
6859
6860 if( n < 128 || i + n > ssl->in_hslen )
6861 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006862 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckere2734e22019-01-31 07:44:17 +00006863 mbedtls_ssl_send_alert_message( ssl,
6864 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6865 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006866 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006867 }
6868
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006869 /* Check if we're handling the first CRT in the chain. */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006870#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6871 if( crt_cnt++ == 0 &&
6872 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
6873 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006874 {
Hanno Becker46f34d02019-02-08 14:00:04 +00006875 /* During client-side renegotiation, check that the server's
6876 * end-CRTs hasn't changed compared to the initial handshake,
6877 * mitigating the triple handshake attack. On success, reuse
6878 * the original end-CRT instead of parsing it again. */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006879 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Check that peer CRT hasn't changed during renegotiation" ) );
6880 if( ssl_check_peer_crt_unchanged( ssl,
6881 &ssl->in_msg[i],
6882 n ) != 0 )
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006883 {
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006884 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
6885 mbedtls_ssl_send_alert_message( ssl,
6886 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6887 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
6888 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006889 }
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006890
6891 /* Now we can safely free the original chain. */
6892 ssl_clear_peer_cert( ssl->session );
6893 }
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006894#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
6895
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006896 /* Parse the next certificate in the chain. */
Hanno Becker0056eab2019-02-08 14:39:16 +00006897#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006898 ret = mbedtls_x509_crt_parse_der( chain, ssl->in_msg + i, n );
Hanno Becker0056eab2019-02-08 14:39:16 +00006899#else
Hanno Becker353a6f02019-02-26 11:51:34 +00006900 /* If we don't need to store the CRT chain permanently, parse
Hanno Becker0056eab2019-02-08 14:39:16 +00006901 * it in-place from the input buffer instead of making a copy. */
6902 ret = mbedtls_x509_crt_parse_der_nocopy( chain, ssl->in_msg + i, n );
6903#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006904 switch( ret )
Paul Bakker5121ce52009-01-03 21:22:43 +00006905 {
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006906 case 0: /*ok*/
6907 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
6908 /* Ignore certificate with an unknown algorithm: maybe a
6909 prior certificate was already trusted. */
6910 break;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006911
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006912 case MBEDTLS_ERR_X509_ALLOC_FAILED:
6913 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
6914 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006915
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006916 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
6917 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6918 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006919
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006920 default:
6921 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6922 crt_parse_der_failed:
6923 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert );
6924 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
6925 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006926 }
6927
6928 i += n;
6929 }
6930
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006931 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", chain );
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006932 return( 0 );
6933}
6934
Hanno Becker4a55f632019-02-05 12:49:06 +00006935#if defined(MBEDTLS_SSL_SRV_C)
6936static int ssl_srv_check_client_no_crt_notification( mbedtls_ssl_context *ssl )
6937{
6938 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
6939 return( -1 );
6940
6941#if defined(MBEDTLS_SSL_PROTO_SSL3)
6942 /*
6943 * Check if the client sent an empty certificate
6944 */
6945 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
6946 {
6947 if( ssl->in_msglen == 2 &&
6948 ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT &&
6949 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6950 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
6951 {
6952 MBEDTLS_SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
6953 return( 0 );
6954 }
6955
6956 return( -1 );
6957 }
6958#endif /* MBEDTLS_SSL_PROTO_SSL3 */
6959
6960#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
6961 defined(MBEDTLS_SSL_PROTO_TLS1_2)
6962 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
6963 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
6964 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
6965 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
6966 {
6967 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
6968 return( 0 );
6969 }
6970
6971 return( -1 );
6972#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
6973 MBEDTLS_SSL_PROTO_TLS1_2 */
6974}
6975#endif /* MBEDTLS_SSL_SRV_C */
6976
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006977/* Check if a certificate message is expected.
6978 * Return either
6979 * - SSL_CERTIFICATE_EXPECTED, or
6980 * - SSL_CERTIFICATE_SKIP
6981 * indicating whether a Certificate message is expected or not.
6982 */
6983#define SSL_CERTIFICATE_EXPECTED 0
6984#define SSL_CERTIFICATE_SKIP 1
6985static int ssl_parse_certificate_coordinate( mbedtls_ssl_context *ssl,
6986 int authmode )
6987{
6988 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00006989 ssl->handshake->ciphersuite_info;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006990
6991 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6992 return( SSL_CERTIFICATE_SKIP );
6993
6994#if defined(MBEDTLS_SSL_SRV_C)
6995 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
6996 {
6997 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
6998 return( SSL_CERTIFICATE_SKIP );
6999
7000 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
7001 {
Hanno Becker28f2fcd2019-02-07 10:11:07 +00007002 ssl->session_negotiate->verify_result =
7003 MBEDTLS_X509_BADCERT_SKIP_VERIFY;
7004 return( SSL_CERTIFICATE_SKIP );
7005 }
7006 }
Hanno Becker84d9d272019-03-01 08:10:46 +00007007#else
7008 ((void) authmode);
Hanno Becker28f2fcd2019-02-07 10:11:07 +00007009#endif /* MBEDTLS_SSL_SRV_C */
7010
7011 return( SSL_CERTIFICATE_EXPECTED );
7012}
7013
Hanno Becker68636192019-02-05 14:36:34 +00007014static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl,
7015 int authmode,
7016 mbedtls_x509_crt *chain,
7017 void *rs_ctx )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007018{
Hanno Becker6bdfab22019-02-05 13:11:17 +00007019 int ret = 0;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00007020 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00007021 ssl->handshake->ciphersuite_info;
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00007022 int have_ca_chain = 0;
Hanno Becker68636192019-02-05 14:36:34 +00007023
Hanno Becker8927c832019-04-03 12:52:50 +01007024 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *);
7025 void *p_vrfy;
7026
Hanno Becker68636192019-02-05 14:36:34 +00007027 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
7028 return( 0 );
7029
Hanno Becker8927c832019-04-03 12:52:50 +01007030 if( ssl->f_vrfy != NULL )
7031 {
Hanno Beckerefb440a2019-04-03 13:04:33 +01007032 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use context-specific verification callback" ) );
Hanno Becker8927c832019-04-03 12:52:50 +01007033 f_vrfy = ssl->f_vrfy;
7034 p_vrfy = ssl->p_vrfy;
7035 }
7036 else
7037 {
Hanno Beckerefb440a2019-04-03 13:04:33 +01007038 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use configuration-specific verification callback" ) );
Hanno Becker8927c832019-04-03 12:52:50 +01007039 f_vrfy = ssl->conf->f_vrfy;
7040 p_vrfy = ssl->conf->p_vrfy;
7041 }
7042
Hanno Becker68636192019-02-05 14:36:34 +00007043 /*
7044 * Main check: verify certificate
7045 */
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00007046#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
7047 if( ssl->conf->f_ca_cb != NULL )
7048 {
7049 ((void) rs_ctx);
7050 have_ca_chain = 1;
7051
7052 MBEDTLS_SSL_DEBUG_MSG( 3, ( "use CA callback for X.509 CRT verification" ) );
Jarno Lamsa9822c0d2019-04-01 16:59:48 +03007053 ret = mbedtls_x509_crt_verify_with_ca_cb(
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00007054 chain,
7055 ssl->conf->f_ca_cb,
7056 ssl->conf->p_ca_cb,
7057 ssl->conf->cert_profile,
7058 ssl->hostname,
7059 &ssl->session_negotiate->verify_result,
Jaeden Amerofe710672019-04-16 15:03:12 +01007060 f_vrfy, p_vrfy );
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00007061 }
7062 else
7063#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
7064 {
7065 mbedtls_x509_crt *ca_chain;
7066 mbedtls_x509_crl *ca_crl;
7067
7068#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7069 if( ssl->handshake->sni_ca_chain != NULL )
7070 {
7071 ca_chain = ssl->handshake->sni_ca_chain;
7072 ca_crl = ssl->handshake->sni_ca_crl;
7073 }
7074 else
7075#endif
7076 {
7077 ca_chain = ssl->conf->ca_chain;
7078 ca_crl = ssl->conf->ca_crl;
7079 }
7080
7081 if( ca_chain != NULL )
7082 have_ca_chain = 1;
7083
7084 ret = mbedtls_x509_crt_verify_restartable(
7085 chain,
7086 ca_chain, ca_crl,
7087 ssl->conf->cert_profile,
7088 ssl->hostname,
7089 &ssl->session_negotiate->verify_result,
Jaeden Amerofe710672019-04-16 15:03:12 +01007090 f_vrfy, p_vrfy, rs_ctx );
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00007091 }
Hanno Becker68636192019-02-05 14:36:34 +00007092
7093 if( ret != 0 )
7094 {
7095 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
7096 }
7097
7098#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
7099 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
7100 return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS );
7101#endif
7102
7103 /*
7104 * Secondary checks: always done, but change 'ret' only if it was 0
7105 */
7106
7107#if defined(MBEDTLS_ECP_C)
7108 {
7109 const mbedtls_pk_context *pk = &chain->pk;
7110
7111 /* If certificate uses an EC key, make sure the curve is OK */
7112 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) &&
7113 mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id ) != 0 )
7114 {
7115 ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
7116
7117 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
7118 if( ret == 0 )
7119 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
7120 }
7121 }
7122#endif /* MBEDTLS_ECP_C */
7123
7124 if( mbedtls_ssl_check_cert_usage( chain,
7125 ciphersuite_info,
7126 ! ssl->conf->endpoint,
7127 &ssl->session_negotiate->verify_result ) != 0 )
7128 {
7129 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
7130 if( ret == 0 )
7131 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
7132 }
7133
7134 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
7135 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
7136 * with details encoded in the verification flags. All other kinds
7137 * of error codes, including those from the user provided f_vrfy
7138 * functions, are treated as fatal and lead to a failure of
7139 * ssl_parse_certificate even if verification was optional. */
7140 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
7141 ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
7142 ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ) )
7143 {
7144 ret = 0;
7145 }
7146
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00007147 if( have_ca_chain == 0 && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
Hanno Becker68636192019-02-05 14:36:34 +00007148 {
7149 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
7150 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
7151 }
7152
7153 if( ret != 0 )
7154 {
7155 uint8_t alert;
7156
7157 /* The certificate may have been rejected for several reasons.
7158 Pick one and send the corresponding alert. Which alert to send
7159 may be a subject of debate in some cases. */
7160 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
7161 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
7162 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
7163 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
7164 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
7165 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7166 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
7167 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7168 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
7169 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7170 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
7171 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7172 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
7173 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7174 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
7175 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
7176 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
7177 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
7178 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
7179 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
7180 else
7181 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
7182 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7183 alert );
7184 }
7185
7186#if defined(MBEDTLS_DEBUG_C)
7187 if( ssl->session_negotiate->verify_result != 0 )
7188 {
7189 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %x",
7190 ssl->session_negotiate->verify_result ) );
7191 }
7192 else
7193 {
7194 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
7195 }
7196#endif /* MBEDTLS_DEBUG_C */
7197
7198 return( ret );
7199}
7200
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007201#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
7202static int ssl_remember_peer_crt_digest( mbedtls_ssl_context *ssl,
7203 unsigned char *start, size_t len )
7204{
7205 int ret;
7206 /* Remember digest of the peer's end-CRT. */
7207 ssl->session_negotiate->peer_cert_digest =
7208 mbedtls_calloc( 1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN );
7209 if( ssl->session_negotiate->peer_cert_digest == NULL )
7210 {
7211 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
7212 sizeof( MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ) ) );
7213 mbedtls_ssl_send_alert_message( ssl,
7214 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7215 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
7216
7217 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
7218 }
7219
7220 ret = mbedtls_md( mbedtls_md_info_from_type(
7221 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE ),
7222 start, len,
7223 ssl->session_negotiate->peer_cert_digest );
7224
7225 ssl->session_negotiate->peer_cert_digest_type =
7226 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
7227 ssl->session_negotiate->peer_cert_digest_len =
7228 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
7229
7230 return( ret );
7231}
7232
7233static int ssl_remember_peer_pubkey( mbedtls_ssl_context *ssl,
7234 unsigned char *start, size_t len )
7235{
7236 unsigned char *end = start + len;
7237 int ret;
7238
7239 /* Make a copy of the peer's raw public key. */
7240 mbedtls_pk_init( &ssl->handshake->peer_pubkey );
7241 ret = mbedtls_pk_parse_subpubkey( &start, end,
7242 &ssl->handshake->peer_pubkey );
7243 if( ret != 0 )
7244 {
7245 /* We should have parsed the public key before. */
7246 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
7247 }
7248
7249 return( 0 );
7250}
7251#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7252
Hanno Becker68636192019-02-05 14:36:34 +00007253int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
7254{
7255 int ret = 0;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00007256 int crt_expected;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007257#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7258 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
7259 ? ssl->handshake->sni_authmode
7260 : ssl->conf->authmode;
7261#else
7262 const int authmode = ssl->conf->authmode;
7263#endif
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007264 void *rs_ctx = NULL;
Hanno Becker3dad3112019-02-05 17:19:52 +00007265 mbedtls_x509_crt *chain = NULL;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007266
7267 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
7268
Hanno Becker28f2fcd2019-02-07 10:11:07 +00007269 crt_expected = ssl_parse_certificate_coordinate( ssl, authmode );
7270 if( crt_expected == SSL_CERTIFICATE_SKIP )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007271 {
7272 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Hanno Becker6bdfab22019-02-05 13:11:17 +00007273 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007274 }
7275
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007276#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
7277 if( ssl->handshake->ecrs_enabled &&
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02007278 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify )
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007279 {
Hanno Becker3dad3112019-02-05 17:19:52 +00007280 chain = ssl->handshake->ecrs_peer_cert;
7281 ssl->handshake->ecrs_peer_cert = NULL;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007282 goto crt_verify;
7283 }
7284#endif
7285
Manuel Pégourié-Gonnard125af942018-09-11 11:08:12 +02007286 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007287 {
7288 /* mbedtls_ssl_read_record may have sent an alert already. We
7289 let it decide whether to alert. */
7290 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Hanno Becker3dad3112019-02-05 17:19:52 +00007291 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007292 }
7293
Hanno Becker4a55f632019-02-05 12:49:06 +00007294#if defined(MBEDTLS_SSL_SRV_C)
7295 if( ssl_srv_check_client_no_crt_notification( ssl ) == 0 )
7296 {
7297 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Hanno Becker4a55f632019-02-05 12:49:06 +00007298
7299 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
Hanno Becker6bdfab22019-02-05 13:11:17 +00007300 ret = 0;
7301 else
7302 ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
Hanno Becker4a55f632019-02-05 12:49:06 +00007303
Hanno Becker6bdfab22019-02-05 13:11:17 +00007304 goto exit;
Hanno Becker4a55f632019-02-05 12:49:06 +00007305 }
7306#endif /* MBEDTLS_SSL_SRV_C */
7307
Hanno Beckerc7bd7802019-02-05 15:37:23 +00007308 /* Clear existing peer CRT structure in case we tried to
7309 * reuse a session but it failed, and allocate a new one. */
Hanno Becker7a955a02019-02-05 13:08:01 +00007310 ssl_clear_peer_cert( ssl->session_negotiate );
Hanno Becker3dad3112019-02-05 17:19:52 +00007311
7312 chain = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
7313 if( chain == NULL )
Hanno Beckerc7bd7802019-02-05 15:37:23 +00007314 {
7315 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
7316 sizeof( mbedtls_x509_crt ) ) );
7317 mbedtls_ssl_send_alert_message( ssl,
7318 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7319 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Hanno Becker7a955a02019-02-05 13:08:01 +00007320
Hanno Becker3dad3112019-02-05 17:19:52 +00007321 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
7322 goto exit;
7323 }
7324 mbedtls_x509_crt_init( chain );
7325
7326 ret = ssl_parse_certificate_chain( ssl, chain );
Hanno Beckerc7bd7802019-02-05 15:37:23 +00007327 if( ret != 0 )
Hanno Becker3dad3112019-02-05 17:19:52 +00007328 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007329
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007330#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
7331 if( ssl->handshake->ecrs_enabled)
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02007332 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007333
7334crt_verify:
7335 if( ssl->handshake->ecrs_enabled)
7336 rs_ctx = &ssl->handshake->ecrs_ctx;
7337#endif
7338
Hanno Becker68636192019-02-05 14:36:34 +00007339 ret = ssl_parse_certificate_verify( ssl, authmode,
Hanno Becker3dad3112019-02-05 17:19:52 +00007340 chain, rs_ctx );
Hanno Becker68636192019-02-05 14:36:34 +00007341 if( ret != 0 )
Hanno Becker3dad3112019-02-05 17:19:52 +00007342 goto exit;
Paul Bakker5121ce52009-01-03 21:22:43 +00007343
Hanno Becker6bbd94c2019-02-05 17:02:28 +00007344#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Becker6bbd94c2019-02-05 17:02:28 +00007345 {
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007346 unsigned char *crt_start, *pk_start;
7347 size_t crt_len, pk_len;
Hanno Becker3dad3112019-02-05 17:19:52 +00007348
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007349 /* We parse the CRT chain without copying, so
7350 * these pointers point into the input buffer,
7351 * and are hence still valid after freeing the
7352 * CRT chain. */
Hanno Becker6bbd94c2019-02-05 17:02:28 +00007353
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007354 crt_start = chain->raw.p;
7355 crt_len = chain->raw.len;
Hanno Becker6bbd94c2019-02-05 17:02:28 +00007356
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007357 pk_start = chain->pk_raw.p;
7358 pk_len = chain->pk_raw.len;
7359
7360 /* Free the CRT structures before computing
7361 * digest and copying the peer's public key. */
7362 mbedtls_x509_crt_free( chain );
7363 mbedtls_free( chain );
7364 chain = NULL;
7365
7366 ret = ssl_remember_peer_crt_digest( ssl, crt_start, crt_len );
Hanno Beckera2747532019-02-06 16:19:04 +00007367 if( ret != 0 )
Hanno Beckera2747532019-02-06 16:19:04 +00007368 goto exit;
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007369
7370 ret = ssl_remember_peer_pubkey( ssl, pk_start, pk_len );
7371 if( ret != 0 )
7372 goto exit;
Hanno Beckera2747532019-02-06 16:19:04 +00007373 }
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007374#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7375 /* Pass ownership to session structure. */
Hanno Becker3dad3112019-02-05 17:19:52 +00007376 ssl->session_negotiate->peer_cert = chain;
7377 chain = NULL;
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007378#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker3dad3112019-02-05 17:19:52 +00007379
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007380 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007381
Hanno Becker6bdfab22019-02-05 13:11:17 +00007382exit:
7383
Hanno Becker3dad3112019-02-05 17:19:52 +00007384 if( ret == 0 )
7385 ssl->state++;
7386
7387#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
7388 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
7389 {
7390 ssl->handshake->ecrs_peer_cert = chain;
7391 chain = NULL;
7392 }
7393#endif
7394
7395 if( chain != NULL )
7396 {
7397 mbedtls_x509_crt_free( chain );
7398 mbedtls_free( chain );
7399 }
7400
Paul Bakker5121ce52009-01-03 21:22:43 +00007401 return( ret );
7402}
Hanno Becker21489932019-02-05 13:20:55 +00007403#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00007404
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007405int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007406{
7407 int ret;
7408
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007409 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007410
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007411 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00007412 ssl->out_msglen = 1;
7413 ssl->out_msg[0] = 1;
7414
Paul Bakker5121ce52009-01-03 21:22:43 +00007415 ssl->state++;
7416
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007417 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007418 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007419 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007420 return( ret );
7421 }
7422
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007423 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007424
7425 return( 0 );
7426}
7427
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007428int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007429{
7430 int ret;
7431
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007432 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007433
Hanno Becker327c93b2018-08-15 13:56:18 +01007434 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007435 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007436 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007437 return( ret );
7438 }
7439
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007440 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Paul Bakker5121ce52009-01-03 21:22:43 +00007441 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007442 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007443 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7444 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007445 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007446 }
7447
Hanno Beckere678eaa2018-08-21 14:57:46 +01007448 /* CCS records are only accepted if they have length 1 and content '1',
7449 * so we don't need to check this here. */
Paul Bakker5121ce52009-01-03 21:22:43 +00007450
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007451 /*
7452 * Switch to our negotiated transform and session parameters for inbound
7453 * data.
7454 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007455 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007456 ssl->transform_in = ssl->transform_negotiate;
7457 ssl->session_in = ssl->session_negotiate;
7458
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007459#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007460 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007461 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007462#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007463 ssl_dtls_replay_reset( ssl );
7464#endif
7465
7466 /* Increment epoch */
7467 if( ++ssl->in_epoch == 0 )
7468 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007469 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007470 /* This is highly unlikely to happen for legitimate reasons, so
7471 treat it as an attack and don't send an alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007472 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007473 }
7474 }
7475 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007476#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007477 memset( ssl->in_ctr, 0, 8 );
7478
Hanno Becker79594fd2019-05-08 09:38:41 +01007479 ssl_update_in_pointers( ssl );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007480
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007481#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7482 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007483 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007484 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_INBOUND ) ) != 0 )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007485 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007486 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007487 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7488 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007489 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007490 }
7491 }
7492#endif
7493
Paul Bakker5121ce52009-01-03 21:22:43 +00007494 ssl->state++;
7495
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007496 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007497
7498 return( 0 );
7499}
7500
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007501void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl,
7502 const mbedtls_ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00007503{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02007504 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01007505
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007506#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7507 defined(MBEDTLS_SSL_PROTO_TLS1_1)
7508 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00007509 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00007510 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007511#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007512#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7513#if defined(MBEDTLS_SHA512_C)
7514 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007515 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
7516 else
7517#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007518#if defined(MBEDTLS_SHA256_C)
7519 if( ciphersuite_info->mac != MBEDTLS_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00007520 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007521 else
7522#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007523#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02007524 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007525 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007526 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02007527 }
Paul Bakker380da532012-04-18 16:10:25 +00007528}
Paul Bakkerf7abd422013-04-16 13:15:56 +02007529
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007530void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007531{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007532#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7533 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007534 mbedtls_md5_starts_ret( &ssl->handshake->fin_md5 );
7535 mbedtls_sha1_starts_ret( &ssl->handshake->fin_sha1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007536#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007537#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7538#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007539#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek2ad22972019-01-30 03:32:12 -05007540 psa_hash_abort( &ssl->handshake->fin_sha256_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007541 psa_hash_setup( &ssl->handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
7542#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007543 mbedtls_sha256_starts_ret( &ssl->handshake->fin_sha256, 0 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007544#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007545#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007546#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007547#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek2ad22972019-01-30 03:32:12 -05007548 psa_hash_abort( &ssl->handshake->fin_sha384_psa );
Andrzej Kurek972fba52019-01-30 03:29:12 -05007549 psa_hash_setup( &ssl->handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007550#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007551 mbedtls_sha512_starts_ret( &ssl->handshake->fin_sha512, 1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007552#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007553#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007554#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007555}
7556
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007557static void ssl_update_checksum_start( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007558 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00007559{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007560#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7561 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007562 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
7563 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007564#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007565#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7566#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007567#if defined(MBEDTLS_USE_PSA_CRYPTO)
7568 psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len );
7569#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007570 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007571#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007572#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007573#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007574#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05007575 psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007576#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007577 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01007578#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007579#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007580#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00007581}
7582
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007583#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7584 defined(MBEDTLS_SSL_PROTO_TLS1_1)
7585static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007586 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00007587{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007588 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
7589 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00007590}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007591#endif
Paul Bakker380da532012-04-18 16:10:25 +00007592
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007593#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7594#if defined(MBEDTLS_SHA256_C)
7595static void ssl_update_checksum_sha256( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007596 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00007597{
Andrzej Kurekeb342242019-01-29 09:14:33 -05007598#if defined(MBEDTLS_USE_PSA_CRYPTO)
7599 psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len );
7600#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007601 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007602#endif
Paul Bakker380da532012-04-18 16:10:25 +00007603}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007604#endif
Paul Bakker380da532012-04-18 16:10:25 +00007605
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007606#if defined(MBEDTLS_SHA512_C)
7607static void ssl_update_checksum_sha384( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007608 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00007609{
Andrzej Kurekeb342242019-01-29 09:14:33 -05007610#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05007611 psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007612#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007613 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007614#endif
Paul Bakker380da532012-04-18 16:10:25 +00007615}
Paul Bakker769075d2012-11-24 11:26:46 +01007616#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007617#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00007618
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007619#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00007620static void ssl_calc_finished_ssl(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007621 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00007622{
Paul Bakker3c2122f2013-06-24 19:03:14 +02007623 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007624 mbedtls_md5_context md5;
7625 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007626
Paul Bakker5121ce52009-01-03 21:22:43 +00007627 unsigned char padbuf[48];
7628 unsigned char md5sum[16];
7629 unsigned char sha1sum[20];
7630
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007631 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007632 if( !session )
7633 session = ssl->session;
7634
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007635 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007636
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007637 mbedtls_md5_init( &md5 );
7638 mbedtls_sha1_init( &sha1 );
7639
7640 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
7641 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007642
7643 /*
7644 * SSLv3:
7645 * hash =
7646 * MD5( master + pad2 +
7647 * MD5( handshake + sender + master + pad1 ) )
7648 * + SHA1( master + pad2 +
7649 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00007650 */
7651
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007652#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007653 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
7654 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007655#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007656
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007657#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007658 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
7659 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007660#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007661
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007662 sender = ( from == MBEDTLS_SSL_IS_CLIENT ) ? "CLNT"
Paul Bakker3c2122f2013-06-24 19:03:14 +02007663 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00007664
Paul Bakker1ef83d62012-04-11 12:09:53 +00007665 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007666
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007667 mbedtls_md5_update_ret( &md5, (const unsigned char *) sender, 4 );
7668 mbedtls_md5_update_ret( &md5, session->master, 48 );
7669 mbedtls_md5_update_ret( &md5, padbuf, 48 );
7670 mbedtls_md5_finish_ret( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00007671
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007672 mbedtls_sha1_update_ret( &sha1, (const unsigned char *) sender, 4 );
7673 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
7674 mbedtls_sha1_update_ret( &sha1, padbuf, 40 );
7675 mbedtls_sha1_finish_ret( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00007676
Paul Bakker1ef83d62012-04-11 12:09:53 +00007677 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007678
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007679 mbedtls_md5_starts_ret( &md5 );
7680 mbedtls_md5_update_ret( &md5, session->master, 48 );
7681 mbedtls_md5_update_ret( &md5, padbuf, 48 );
7682 mbedtls_md5_update_ret( &md5, md5sum, 16 );
7683 mbedtls_md5_finish_ret( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00007684
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007685 mbedtls_sha1_starts_ret( &sha1 );
7686 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
7687 mbedtls_sha1_update_ret( &sha1, padbuf , 40 );
7688 mbedtls_sha1_update_ret( &sha1, sha1sum, 20 );
7689 mbedtls_sha1_finish_ret( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007690
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007691 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007692
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007693 mbedtls_md5_free( &md5 );
7694 mbedtls_sha1_free( &sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007695
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007696 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
7697 mbedtls_platform_zeroize( md5sum, sizeof( md5sum ) );
7698 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007699
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007700 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007701}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007702#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00007703
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007704#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00007705static void ssl_calc_finished_tls(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007706 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00007707{
Paul Bakker1ef83d62012-04-11 12:09:53 +00007708 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02007709 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007710 mbedtls_md5_context md5;
7711 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007712 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00007713
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007714 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007715 if( !session )
7716 session = ssl->session;
7717
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007718 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007719
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007720 mbedtls_md5_init( &md5 );
7721 mbedtls_sha1_init( &sha1 );
7722
7723 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
7724 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007725
Paul Bakker1ef83d62012-04-11 12:09:53 +00007726 /*
7727 * TLSv1:
7728 * hash = PRF( master, finished_label,
7729 * MD5( handshake ) + SHA1( handshake ) )[0..11]
7730 */
Paul Bakker5121ce52009-01-03 21:22:43 +00007731
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007732#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007733 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
7734 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007735#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007736
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007737#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007738 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
7739 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007740#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007741
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007742 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02007743 ? "client finished"
7744 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00007745
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007746 mbedtls_md5_finish_ret( &md5, padbuf );
7747 mbedtls_sha1_finish_ret( &sha1, padbuf + 16 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007748
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007749 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00007750 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007751
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007752 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007753
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007754 mbedtls_md5_free( &md5 );
7755 mbedtls_sha1_free( &sha1 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007756
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007757 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007758
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007759 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007760}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007761#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00007762
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007763#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7764#if defined(MBEDTLS_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00007765static void ssl_calc_finished_tls_sha256(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007766 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker1ef83d62012-04-11 12:09:53 +00007767{
7768 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02007769 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007770 unsigned char padbuf[32];
Andrzej Kurekeb342242019-01-29 09:14:33 -05007771#if defined(MBEDTLS_USE_PSA_CRYPTO)
7772 size_t hash_size;
Jaeden Amero34973232019-02-20 10:32:28 +00007773 psa_hash_operation_t sha256_psa = PSA_HASH_OPERATION_INIT;
Andrzej Kurekeb342242019-01-29 09:14:33 -05007774 psa_status_t status;
7775#else
7776 mbedtls_sha256_context sha256;
7777#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007778
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007779 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007780 if( !session )
7781 session = ssl->session;
7782
Andrzej Kurekeb342242019-01-29 09:14:33 -05007783 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
7784 ? "client finished"
7785 : "server finished";
7786
7787#if defined(MBEDTLS_USE_PSA_CRYPTO)
7788 sha256_psa = psa_hash_operation_init();
7789
7790 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha256" ) );
7791
7792 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
7793 if( status != PSA_SUCCESS )
7794 {
7795 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
7796 return;
7797 }
7798
7799 status = psa_hash_finish( &sha256_psa, padbuf, sizeof( padbuf ), &hash_size );
7800 if( status != PSA_SUCCESS )
7801 {
7802 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
7803 return;
7804 }
7805 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 32 );
7806#else
7807
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007808 mbedtls_sha256_init( &sha256 );
7809
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007810 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007811
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007812 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007813
7814 /*
7815 * TLSv1.2:
7816 * hash = PRF( master, finished_label,
7817 * Hash( handshake ) )[0.11]
7818 */
7819
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007820#if !defined(MBEDTLS_SHA256_ALT)
7821 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007822 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007823#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007824
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007825 mbedtls_sha256_finish_ret( &sha256, padbuf );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007826 mbedtls_sha256_free( &sha256 );
7827#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker1ef83d62012-04-11 12:09:53 +00007828
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007829 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00007830 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007831
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007832 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007833
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007834 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007835
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007836 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007837}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007838#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00007839
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007840#if defined(MBEDTLS_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00007841static void ssl_calc_finished_tls_sha384(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007842 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakkerca4ab492012-04-18 14:23:57 +00007843{
7844 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02007845 const char *sender;
Paul Bakkerca4ab492012-04-18 14:23:57 +00007846 unsigned char padbuf[48];
Andrzej Kurekeb342242019-01-29 09:14:33 -05007847#if defined(MBEDTLS_USE_PSA_CRYPTO)
7848 size_t hash_size;
Jaeden Amero34973232019-02-20 10:32:28 +00007849 psa_hash_operation_t sha384_psa = PSA_HASH_OPERATION_INIT;
Andrzej Kurekeb342242019-01-29 09:14:33 -05007850 psa_status_t status;
7851#else
7852 mbedtls_sha512_context sha512;
7853#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00007854
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007855 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007856 if( !session )
7857 session = ssl->session;
7858
Andrzej Kurekeb342242019-01-29 09:14:33 -05007859 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
7860 ? "client finished"
7861 : "server finished";
7862
7863#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05007864 sha384_psa = psa_hash_operation_init();
Andrzej Kurekeb342242019-01-29 09:14:33 -05007865
7866 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha384" ) );
7867
Andrzej Kurek972fba52019-01-30 03:29:12 -05007868 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007869 if( status != PSA_SUCCESS )
7870 {
7871 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
7872 return;
7873 }
7874
Andrzej Kurek972fba52019-01-30 03:29:12 -05007875 status = psa_hash_finish( &sha384_psa, padbuf, sizeof( padbuf ), &hash_size );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007876 if( status != PSA_SUCCESS )
7877 {
7878 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
7879 return;
7880 }
7881 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 48 );
7882#else
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007883 mbedtls_sha512_init( &sha512 );
7884
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007885 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007886
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007887 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007888
7889 /*
7890 * TLSv1.2:
7891 * hash = PRF( master, finished_label,
7892 * Hash( handshake ) )[0.11]
7893 */
7894
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007895#if !defined(MBEDTLS_SHA512_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007896 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
7897 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007898#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00007899
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007900 mbedtls_sha512_finish_ret( &sha512, padbuf );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007901 mbedtls_sha512_free( &sha512 );
7902#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00007903
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007904 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00007905 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007906
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007907 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007908
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007909 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007910
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007911 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007912}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007913#endif /* MBEDTLS_SHA512_C */
7914#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00007915
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007916static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00007917{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007918 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007919
7920 /*
7921 * Free our handshake params
7922 */
Gilles Peskine9b562d52018-04-25 20:32:43 +02007923 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007924 mbedtls_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00007925 ssl->handshake = NULL;
7926
7927 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007928 * Free the previous transform and swith in the current one
Paul Bakker48916f92012-09-16 19:57:18 +00007929 */
7930 if( ssl->transform )
7931 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007932 mbedtls_ssl_transform_free( ssl->transform );
7933 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00007934 }
7935 ssl->transform = ssl->transform_negotiate;
7936 ssl->transform_negotiate = NULL;
7937
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007938 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007939}
7940
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007941void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007942{
7943 int resume = ssl->handshake->resume;
7944
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007945 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007946
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007947#if defined(MBEDTLS_SSL_RENEGOTIATION)
7948 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007949 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007950 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007951 ssl->renego_records_seen = 0;
7952 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007953#endif
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007954
7955 /*
7956 * Free the previous session and switch in the current one
7957 */
Paul Bakker0a597072012-09-25 21:55:46 +00007958 if( ssl->session )
7959 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007960#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01007961 /* RFC 7366 3.1: keep the EtM state */
7962 ssl->session_negotiate->encrypt_then_mac =
7963 ssl->session->encrypt_then_mac;
7964#endif
7965
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007966 mbedtls_ssl_session_free( ssl->session );
7967 mbedtls_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00007968 }
7969 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007970 ssl->session_negotiate = NULL;
7971
Paul Bakker0a597072012-09-25 21:55:46 +00007972 /*
7973 * Add cache entry
7974 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007975 if( ssl->conf->f_set_cache != NULL &&
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02007976 ssl->session->id_len != 0 &&
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02007977 resume == 0 )
7978 {
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007979 if( ssl->conf->f_set_cache( ssl->conf->p_cache, ssl->session ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007980 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02007981 }
Paul Bakker0a597072012-09-25 21:55:46 +00007982
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007983#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007984 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007985 ssl->handshake->flight != NULL )
7986 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02007987 /* Cancel handshake timer */
7988 ssl_set_timer( ssl, 0 );
7989
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007990 /* Keep last flight around in case we need to resend it:
7991 * we need the handshake and transform structures for that */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007992 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007993 }
7994 else
7995#endif
7996 ssl_handshake_wrapup_free_hs_transform( ssl );
7997
Paul Bakker48916f92012-09-16 19:57:18 +00007998 ssl->state++;
7999
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008000 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00008001}
8002
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008003int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
Paul Bakker1ef83d62012-04-11 12:09:53 +00008004{
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02008005 int ret, hash_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00008006
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008007 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00008008
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008009 ssl_update_out_pointers( ssl, ssl->transform_negotiate );
Paul Bakker92be97b2013-01-02 17:30:03 +01008010
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008011 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00008012
Manuel Pégourié-Gonnard214a8482016-02-22 11:27:26 +01008013 /*
8014 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
8015 * may define some other value. Currently (early 2016), no defined
8016 * ciphersuite does this (and this is unlikely to change as activity has
8017 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
8018 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008019 hash_len = ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) ? 36 : 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00008020
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008021#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00008022 ssl->verify_data_len = hash_len;
8023 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008024#endif
Paul Bakker48916f92012-09-16 19:57:18 +00008025
Paul Bakker5121ce52009-01-03 21:22:43 +00008026 ssl->out_msglen = 4 + hash_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008027 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
8028 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
Paul Bakker5121ce52009-01-03 21:22:43 +00008029
8030 /*
8031 * In case of session resuming, invert the client and server
8032 * ChangeCipherSpec messages order.
8033 */
Paul Bakker0a597072012-09-25 21:55:46 +00008034 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008035 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008036#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008037 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008038 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01008039#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008040#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008041 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008042 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01008043#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00008044 }
8045 else
8046 ssl->state++;
8047
Paul Bakker48916f92012-09-16 19:57:18 +00008048 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02008049 * Switch to our negotiated transform and session parameters for outbound
8050 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00008051 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008052 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01008053
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008054#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008055 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02008056 {
8057 unsigned char i;
8058
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008059 /* Remember current epoch settings for resending */
8060 ssl->handshake->alt_transform_out = ssl->transform_out;
Hanno Becker19859472018-08-06 09:40:20 +01008061 memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008062
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02008063 /* Set sequence_number to zero */
Hanno Becker19859472018-08-06 09:40:20 +01008064 memset( ssl->cur_out_ctr + 2, 0, 6 );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02008065
8066 /* Increment epoch */
8067 for( i = 2; i > 0; i-- )
Hanno Becker19859472018-08-06 09:40:20 +01008068 if( ++ssl->cur_out_ctr[i - 1] != 0 )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02008069 break;
8070
8071 /* The loop goes to its end iff the counter is wrapping */
8072 if( i == 0 )
8073 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008074 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
8075 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02008076 }
8077 }
8078 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008079#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker19859472018-08-06 09:40:20 +01008080 memset( ssl->cur_out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00008081
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008082 ssl->transform_out = ssl->transform_negotiate;
8083 ssl->session_out = ssl->session_negotiate;
8084
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008085#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
8086 if( mbedtls_ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01008087 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008088 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Paul Bakker07eb38b2012-12-19 14:42:06 +01008089 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008090 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
8091 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker07eb38b2012-12-19 14:42:06 +01008092 }
8093 }
8094#endif
8095
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008096#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008097 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008098 mbedtls_ssl_send_flight_completed( ssl );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02008099#endif
8100
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02008101 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008102 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02008103 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008104 return( ret );
8105 }
8106
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02008107#if defined(MBEDTLS_SSL_PROTO_DTLS)
8108 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
8109 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
8110 {
8111 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
8112 return( ret );
8113 }
8114#endif
8115
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008116 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008117
8118 return( 0 );
8119}
8120
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008121#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00008122#define SSL_MAX_HASH_LEN 36
8123#else
8124#define SSL_MAX_HASH_LEN 12
8125#endif
8126
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008127int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008128{
Paul Bakker23986e52011-04-24 08:57:21 +00008129 int ret;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02008130 unsigned int hash_len;
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00008131 unsigned char buf[SSL_MAX_HASH_LEN];
Paul Bakker5121ce52009-01-03 21:22:43 +00008132
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008133 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008134
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008135 ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00008136
Hanno Becker327c93b2018-08-15 13:56:18 +01008137 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008138 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008139 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008140 return( ret );
8141 }
8142
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008143 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00008144 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008145 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02008146 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8147 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008148 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00008149 }
8150
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00008151 /* There is currently no ciphersuite using another length with TLS 1.2 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008152#if defined(MBEDTLS_SSL_PROTO_SSL3)
8153 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00008154 hash_len = 36;
8155 else
8156#endif
8157 hash_len = 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00008158
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008159 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED ||
8160 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00008161 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008162 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02008163 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8164 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008165 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00008166 }
8167
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008168 if( mbedtls_ssl_safer_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00008169 buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008170 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008171 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02008172 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8173 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008174 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00008175 }
8176
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008177#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00008178 ssl->verify_data_len = hash_len;
8179 memcpy( ssl->peer_verify_data, buf, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008180#endif
Paul Bakker48916f92012-09-16 19:57:18 +00008181
Paul Bakker0a597072012-09-25 21:55:46 +00008182 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00008183 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008184#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008185 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008186 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01008187#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008188#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008189 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008190 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01008191#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00008192 }
8193 else
8194 ssl->state++;
8195
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008196#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008197 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008198 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008199#endif
8200
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008201 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00008202
8203 return( 0 );
8204}
8205
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008206static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008207{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008208 memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008209
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008210#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
8211 defined(MBEDTLS_SSL_PROTO_TLS1_1)
8212 mbedtls_md5_init( &handshake->fin_md5 );
8213 mbedtls_sha1_init( &handshake->fin_sha1 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008214 mbedtls_md5_starts_ret( &handshake->fin_md5 );
8215 mbedtls_sha1_starts_ret( &handshake->fin_sha1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008216#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008217#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
8218#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05008219#if defined(MBEDTLS_USE_PSA_CRYPTO)
8220 handshake->fin_sha256_psa = psa_hash_operation_init();
8221 psa_hash_setup( &handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
8222#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008223 mbedtls_sha256_init( &handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008224 mbedtls_sha256_starts_ret( &handshake->fin_sha256, 0 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008225#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05008226#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008227#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05008228#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05008229 handshake->fin_sha384_psa = psa_hash_operation_init();
8230 psa_hash_setup( &handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05008231#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008232 mbedtls_sha512_init( &handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01008233 mbedtls_sha512_starts_ret( &handshake->fin_sha512, 1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008234#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05008235#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008236#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008237
8238 handshake->update_checksum = ssl_update_checksum_start;
Hanno Becker7e5437a2017-04-28 17:15:26 +01008239
8240#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
8241 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
8242 mbedtls_ssl_sig_hash_set_init( &handshake->hash_algs );
8243#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008244
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008245#if defined(MBEDTLS_DHM_C)
8246 mbedtls_dhm_init( &handshake->dhm_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008247#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008248#if defined(MBEDTLS_ECDH_C)
8249 mbedtls_ecdh_init( &handshake->ecdh_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008250#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008251#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02008252 mbedtls_ecjpake_init( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02008253#if defined(MBEDTLS_SSL_CLI_C)
8254 handshake->ecjpake_cache = NULL;
8255 handshake->ecjpake_cache_len = 0;
8256#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02008257#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02008258
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02008259#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02008260 mbedtls_x509_crt_restart_init( &handshake->ecrs_ctx );
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02008261#endif
8262
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02008263#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
8264 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
8265#endif
Hanno Becker75173122019-02-06 16:18:31 +00008266
8267#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
8268 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8269 mbedtls_pk_init( &handshake->peer_pubkey );
8270#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008271}
8272
Hanno Beckera18d1322018-01-03 14:27:32 +00008273void mbedtls_ssl_transform_init( mbedtls_ssl_transform *transform )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008274{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008275 memset( transform, 0, sizeof(mbedtls_ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02008276
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008277 mbedtls_cipher_init( &transform->cipher_ctx_enc );
8278 mbedtls_cipher_init( &transform->cipher_ctx_dec );
Paul Bakker84bbeb52014-07-01 14:53:22 +02008279
Hanno Beckerd56ed242018-01-03 15:32:51 +00008280#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008281 mbedtls_md_init( &transform->md_ctx_enc );
8282 mbedtls_md_init( &transform->md_ctx_dec );
Hanno Beckerd56ed242018-01-03 15:32:51 +00008283#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008284}
8285
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008286void mbedtls_ssl_session_init( mbedtls_ssl_session *session )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008287{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008288 memset( session, 0, sizeof(mbedtls_ssl_session) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008289}
8290
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008291static int ssl_handshake_init( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00008292{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008293 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00008294 if( ssl->transform_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008295 mbedtls_ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008296 if( ssl->session_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008297 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008298 if( ssl->handshake )
Gilles Peskine9b562d52018-04-25 20:32:43 +02008299 mbedtls_ssl_handshake_free( ssl );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008300
8301 /*
8302 * Either the pointers are now NULL or cleared properly and can be freed.
8303 * Now allocate missing structures.
8304 */
8305 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008306 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008307 ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008308 }
Paul Bakker48916f92012-09-16 19:57:18 +00008309
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008310 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008311 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008312 ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008313 }
Paul Bakker48916f92012-09-16 19:57:18 +00008314
Paul Bakker82788fb2014-10-20 13:59:19 +02008315 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008316 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008317 ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008318 }
Paul Bakker48916f92012-09-16 19:57:18 +00008319
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008320 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00008321 if( ssl->handshake == NULL ||
8322 ssl->transform_negotiate == NULL ||
8323 ssl->session_negotiate == NULL )
8324 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02008325 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008326
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008327 mbedtls_free( ssl->handshake );
8328 mbedtls_free( ssl->transform_negotiate );
8329 mbedtls_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008330
8331 ssl->handshake = NULL;
8332 ssl->transform_negotiate = NULL;
8333 ssl->session_negotiate = NULL;
8334
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008335 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker48916f92012-09-16 19:57:18 +00008336 }
8337
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008338 /* Initialize structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008339 mbedtls_ssl_session_init( ssl->session_negotiate );
Hanno Beckera18d1322018-01-03 14:27:32 +00008340 mbedtls_ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02008341 ssl_handshake_params_init( ssl->handshake );
8342
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008343#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02008344 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
8345 {
8346 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008347
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02008348 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
8349 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
8350 else
8351 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008352
8353 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02008354 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008355#endif
8356
Paul Bakker48916f92012-09-16 19:57:18 +00008357 return( 0 );
8358}
8359
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008360#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008361/* Dummy cookie callbacks for defaults */
8362static int ssl_cookie_write_dummy( void *ctx,
8363 unsigned char **p, unsigned char *end,
8364 const unsigned char *cli_id, size_t cli_id_len )
8365{
8366 ((void) ctx);
8367 ((void) p);
8368 ((void) end);
8369 ((void) cli_id);
8370 ((void) cli_id_len);
8371
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008372 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008373}
8374
8375static int ssl_cookie_check_dummy( void *ctx,
8376 const unsigned char *cookie, size_t cookie_len,
8377 const unsigned char *cli_id, size_t cli_id_len )
8378{
8379 ((void) ctx);
8380 ((void) cookie);
8381 ((void) cookie_len);
8382 ((void) cli_id);
8383 ((void) cli_id_len);
8384
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008385 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008386}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008387#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008388
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008389/* Once ssl->out_hdr as the address of the beginning of the
8390 * next outgoing record is set, deduce the other pointers.
8391 *
8392 * Note: For TLS, we save the implicit record sequence number
8393 * (entering MAC computation) in the 8 bytes before ssl->out_hdr,
8394 * and the caller has to make sure there's space for this.
8395 */
8396
8397static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
8398 mbedtls_ssl_transform *transform )
8399{
8400#if defined(MBEDTLS_SSL_PROTO_DTLS)
8401 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
8402 {
8403 ssl->out_ctr = ssl->out_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008404#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008405 ssl->out_cid = ssl->out_ctr + 8;
8406 ssl->out_len = ssl->out_cid;
8407 if( transform != NULL )
8408 ssl->out_len += transform->out_cid_len;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008409#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008410 ssl->out_len = ssl->out_ctr + 8;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008411#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008412 ssl->out_iv = ssl->out_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008413 }
8414 else
8415#endif
8416 {
8417 ssl->out_ctr = ssl->out_hdr - 8;
8418 ssl->out_len = ssl->out_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008419#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker4c3eb7c2019-05-08 16:43:21 +01008420 ssl->out_cid = ssl->out_len;
8421#endif
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008422 ssl->out_iv = ssl->out_hdr + 5;
8423 }
8424
8425 /* Adjust out_msg to make space for explicit IV, if used. */
8426 if( transform != NULL &&
8427 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
8428 {
8429 ssl->out_msg = ssl->out_iv + transform->ivlen - transform->fixed_ivlen;
8430 }
8431 else
8432 ssl->out_msg = ssl->out_iv;
8433}
8434
8435/* Once ssl->in_hdr as the address of the beginning of the
8436 * next incoming record is set, deduce the other pointers.
8437 *
8438 * Note: For TLS, we save the implicit record sequence number
8439 * (entering MAC computation) in the 8 bytes before ssl->in_hdr,
8440 * and the caller has to make sure there's space for this.
8441 */
8442
Hanno Becker79594fd2019-05-08 09:38:41 +01008443static void ssl_update_in_pointers( mbedtls_ssl_context *ssl )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008444{
Hanno Becker79594fd2019-05-08 09:38:41 +01008445 /* This function sets the pointers to match the case
8446 * of unprotected TLS/DTLS records, with both ssl->in_iv
8447 * and ssl->in_msg pointing to the beginning of the record
8448 * content.
8449 *
8450 * When decrypting a protected record, ssl->in_msg
8451 * will be shifted to point to the beginning of the
8452 * record plaintext.
8453 */
8454
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008455#if defined(MBEDTLS_SSL_PROTO_DTLS)
8456 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
8457 {
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008458 /* This sets the header pointers to match records
8459 * without CID. When we receive a record containing
8460 * a CID, the fields are shifted accordingly in
8461 * ssl_parse_record_header(). */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008462 ssl->in_ctr = ssl->in_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008463#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008464 ssl->in_cid = ssl->in_ctr + 8;
8465 ssl->in_len = ssl->in_cid; /* Default: no CID */
Hanno Beckera0e20d02019-05-15 14:03:01 +01008466#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008467 ssl->in_len = ssl->in_ctr + 8;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008468#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008469 ssl->in_iv = ssl->in_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008470 }
8471 else
8472#endif
8473 {
8474 ssl->in_ctr = ssl->in_hdr - 8;
8475 ssl->in_len = ssl->in_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008476#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker4c3eb7c2019-05-08 16:43:21 +01008477 ssl->in_cid = ssl->in_len;
8478#endif
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008479 ssl->in_iv = ssl->in_hdr + 5;
8480 }
8481
Hanno Becker79594fd2019-05-08 09:38:41 +01008482 /* This will be adjusted at record decryption time. */
8483 ssl->in_msg = ssl->in_iv;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008484}
8485
Paul Bakker5121ce52009-01-03 21:22:43 +00008486/*
8487 * Initialize an SSL context
8488 */
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02008489void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
8490{
8491 memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
8492}
8493
8494/*
8495 * Setup an SSL context
8496 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008497
8498static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl )
8499{
8500 /* Set the incoming and outgoing record pointers. */
8501#if defined(MBEDTLS_SSL_PROTO_DTLS)
8502 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
8503 {
8504 ssl->out_hdr = ssl->out_buf;
8505 ssl->in_hdr = ssl->in_buf;
8506 }
8507 else
8508#endif /* MBEDTLS_SSL_PROTO_DTLS */
8509 {
8510 ssl->out_hdr = ssl->out_buf + 8;
8511 ssl->in_hdr = ssl->in_buf + 8;
8512 }
8513
8514 /* Derive other internal pointers. */
8515 ssl_update_out_pointers( ssl, NULL /* no transform enabled */ );
Hanno Becker79594fd2019-05-08 09:38:41 +01008516 ssl_update_in_pointers ( ssl );
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008517}
8518
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02008519int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +02008520 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +00008521{
Paul Bakker48916f92012-09-16 19:57:18 +00008522 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00008523
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02008524 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00008525
8526 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01008527 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00008528 */
k-stachowiakc9a5f022018-07-24 13:53:31 +02008529
8530 /* Set to NULL in case of an error condition */
8531 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02008532
Angus Grattond8213d02016-05-25 20:56:48 +10008533 ssl->in_buf = mbedtls_calloc( 1, MBEDTLS_SSL_IN_BUFFER_LEN );
8534 if( ssl->in_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00008535 {
Angus Grattond8213d02016-05-25 20:56:48 +10008536 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_IN_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02008537 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02008538 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +10008539 }
8540
8541 ssl->out_buf = mbedtls_calloc( 1, MBEDTLS_SSL_OUT_BUFFER_LEN );
8542 if( ssl->out_buf == NULL )
8543 {
8544 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_OUT_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02008545 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02008546 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00008547 }
8548
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008549 ssl_reset_in_out_pointers( ssl );
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02008550
Paul Bakker48916f92012-09-16 19:57:18 +00008551 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
k-stachowiaka47911c2018-07-04 17:41:58 +02008552 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00008553
8554 return( 0 );
k-stachowiaka47911c2018-07-04 17:41:58 +02008555
8556error:
8557 mbedtls_free( ssl->in_buf );
8558 mbedtls_free( ssl->out_buf );
8559
8560 ssl->conf = NULL;
8561
8562 ssl->in_buf = NULL;
8563 ssl->out_buf = NULL;
8564
8565 ssl->in_hdr = NULL;
8566 ssl->in_ctr = NULL;
8567 ssl->in_len = NULL;
8568 ssl->in_iv = NULL;
8569 ssl->in_msg = NULL;
8570
8571 ssl->out_hdr = NULL;
8572 ssl->out_ctr = NULL;
8573 ssl->out_len = NULL;
8574 ssl->out_iv = NULL;
8575 ssl->out_msg = NULL;
8576
k-stachowiak9f7798e2018-07-31 16:52:32 +02008577 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008578}
8579
8580/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00008581 * Reset an initialized and used SSL context for re-use while retaining
8582 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008583 *
8584 * If partial is non-zero, keep data in the input buffer and client ID.
8585 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00008586 */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008587static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +00008588{
Paul Bakker48916f92012-09-16 19:57:18 +00008589 int ret;
8590
Hanno Becker7e772132018-08-10 12:38:21 +01008591#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || \
8592 !defined(MBEDTLS_SSL_SRV_C)
8593 ((void) partial);
8594#endif
8595
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008596 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008597
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008598 /* Cancel any possibly running timer */
8599 ssl_set_timer( ssl, 0 );
8600
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008601#if defined(MBEDTLS_SSL_RENEGOTIATION)
8602 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008603 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00008604
8605 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008606 memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
8607 memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008608#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008609 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00008610
Paul Bakker7eb013f2011-10-06 12:37:39 +00008611 ssl->in_offt = NULL;
Hanno Beckerf29d4702018-08-10 11:31:15 +01008612 ssl_reset_in_out_pointers( ssl );
Paul Bakker7eb013f2011-10-06 12:37:39 +00008613
8614 ssl->in_msgtype = 0;
8615 ssl->in_msglen = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008616#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02008617 ssl->next_record_offset = 0;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02008618 ssl->in_epoch = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02008619#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008620#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02008621 ssl_dtls_replay_reset( ssl );
8622#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00008623
8624 ssl->in_hslen = 0;
8625 ssl->nb_zero = 0;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01008626
8627 ssl->keep_current_message = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00008628
8629 ssl->out_msgtype = 0;
8630 ssl->out_msglen = 0;
8631 ssl->out_left = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008632#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
8633 if( ssl->split_done != MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008634 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008635#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00008636
Hanno Becker19859472018-08-06 09:40:20 +01008637 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
8638
Paul Bakker48916f92012-09-16 19:57:18 +00008639 ssl->transform_in = NULL;
8640 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00008641
Hanno Becker78640902018-08-13 16:35:15 +01008642 ssl->session_in = NULL;
8643 ssl->session_out = NULL;
8644
Angus Grattond8213d02016-05-25 20:56:48 +10008645 memset( ssl->out_buf, 0, MBEDTLS_SSL_OUT_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01008646
8647#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008648 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01008649#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
8650 {
8651 ssl->in_left = 0;
Angus Grattond8213d02016-05-25 20:56:48 +10008652 memset( ssl->in_buf, 0, MBEDTLS_SSL_IN_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01008653 }
Paul Bakker05ef8352012-05-08 09:17:57 +00008654
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008655#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
8656 if( mbedtls_ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00008657 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008658 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_reset()" ) );
8659 if( ( ret = mbedtls_ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00008660 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008661 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_reset", ret );
8662 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00008663 }
Paul Bakker05ef8352012-05-08 09:17:57 +00008664 }
8665#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00008666
Paul Bakker48916f92012-09-16 19:57:18 +00008667 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00008668 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008669 mbedtls_ssl_transform_free( ssl->transform );
8670 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00008671 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00008672 }
Paul Bakker48916f92012-09-16 19:57:18 +00008673
Paul Bakkerc0463502013-02-14 11:19:38 +01008674 if( ssl->session )
8675 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008676 mbedtls_ssl_session_free( ssl->session );
8677 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01008678 ssl->session = NULL;
8679 }
8680
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008681#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008682 ssl->alpn_chosen = NULL;
8683#endif
8684
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008685#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker4ccbf062018-08-10 11:20:38 +01008686#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008687 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01008688#endif
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008689 {
8690 mbedtls_free( ssl->cli_id );
8691 ssl->cli_id = NULL;
8692 ssl->cli_id_len = 0;
8693 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02008694#endif
8695
Paul Bakker48916f92012-09-16 19:57:18 +00008696 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
8697 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00008698
8699 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00008700}
8701
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02008702/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008703 * Reset an initialized and used SSL context for re-use while retaining
8704 * all application-set variables, function pointers and data.
8705 */
8706int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
8707{
8708 return( ssl_session_reset_int( ssl, 0 ) );
8709}
8710
8711/*
Paul Bakker5121ce52009-01-03 21:22:43 +00008712 * SSL set accessors
8713 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008714void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00008715{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008716 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00008717}
8718
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008719void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01008720{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008721 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01008722}
8723
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008724#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008725void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02008726{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008727 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02008728}
8729#endif
8730
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008731#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008732void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf, unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02008733{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008734 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02008735}
8736#endif
8737
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008738#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01008739
Hanno Becker1841b0a2018-08-24 11:13:57 +01008740void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl,
8741 unsigned allow_packing )
Hanno Becker04da1892018-08-14 13:22:10 +01008742{
8743 ssl->disable_datagram_packing = !allow_packing;
8744}
8745
8746void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
8747 uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02008748{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008749 conf->hs_timeout_min = min;
8750 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02008751}
8752#endif
8753
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008754void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00008755{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008756 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00008757}
8758
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008759#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008760void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02008761 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008762 void *p_vrfy )
8763{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008764 conf->f_vrfy = f_vrfy;
8765 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008766}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008767#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008768
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008769void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00008770 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00008771 void *p_rng )
8772{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01008773 conf->f_rng = f_rng;
8774 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00008775}
8776
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008777void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02008778 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00008779 void *p_dbg )
8780{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008781 conf->f_dbg = f_dbg;
8782 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00008783}
8784
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008785void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008786 void *p_bio,
Simon Butchere846b512016-03-01 17:31:49 +00008787 mbedtls_ssl_send_t *f_send,
8788 mbedtls_ssl_recv_t *f_recv,
8789 mbedtls_ssl_recv_timeout_t *f_recv_timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008790{
8791 ssl->p_bio = p_bio;
8792 ssl->f_send = f_send;
8793 ssl->f_recv = f_recv;
8794 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01008795}
8796
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02008797#if defined(MBEDTLS_SSL_PROTO_DTLS)
8798void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu )
8799{
8800 ssl->mtu = mtu;
8801}
8802#endif
8803
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008804void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01008805{
8806 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008807}
8808
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008809void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
8810 void *p_timer,
Simon Butchere846b512016-03-01 17:31:49 +00008811 mbedtls_ssl_set_timer_t *f_set_timer,
8812 mbedtls_ssl_get_timer_t *f_get_timer )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008813{
8814 ssl->p_timer = p_timer;
8815 ssl->f_set_timer = f_set_timer;
8816 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008817
8818 /* Make sure we start with no timer running */
8819 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008820}
8821
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008822#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008823void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01008824 void *p_cache,
8825 int (*f_get_cache)(void *, mbedtls_ssl_session *),
8826 int (*f_set_cache)(void *, const mbedtls_ssl_session *) )
Paul Bakker5121ce52009-01-03 21:22:43 +00008827{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01008828 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008829 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008830 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00008831}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008832#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008833
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008834#if defined(MBEDTLS_SSL_CLI_C)
8835int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00008836{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008837 int ret;
8838
8839 if( ssl == NULL ||
8840 session == NULL ||
8841 ssl->session_negotiate == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008842 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008843 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008844 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008845 }
8846
Hanno Becker52055ae2019-02-06 14:30:46 +00008847 if( ( ret = mbedtls_ssl_session_copy( ssl->session_negotiate,
8848 session ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008849 return( ret );
8850
Paul Bakker0a597072012-09-25 21:55:46 +00008851 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008852
8853 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00008854}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008855#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008856
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008857void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008858 const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00008859{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008860 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = ciphersuites;
8861 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = ciphersuites;
8862 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = ciphersuites;
8863 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = ciphersuites;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008864}
8865
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008866void mbedtls_ssl_conf_ciphersuites_for_version( mbedtls_ssl_config *conf,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02008867 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008868 int major, int minor )
8869{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008870 if( major != MBEDTLS_SSL_MAJOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008871 return;
8872
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008873 if( minor < MBEDTLS_SSL_MINOR_VERSION_0 || minor > MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008874 return;
8875
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008876 conf->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00008877}
8878
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008879#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02008880void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01008881 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02008882{
8883 conf->cert_profile = profile;
8884}
8885
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008886/* Append a new keycert entry to a (possibly empty) list */
8887static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
8888 mbedtls_x509_crt *cert,
8889 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008890{
niisato8ee24222018-06-25 19:05:48 +09008891 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008892
niisato8ee24222018-06-25 19:05:48 +09008893 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
8894 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008895 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008896
niisato8ee24222018-06-25 19:05:48 +09008897 new_cert->cert = cert;
8898 new_cert->key = key;
8899 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008900
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008901 /* Update head is the list was null, else add to the end */
8902 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01008903 {
niisato8ee24222018-06-25 19:05:48 +09008904 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01008905 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008906 else
8907 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008908 mbedtls_ssl_key_cert *cur = *head;
8909 while( cur->next != NULL )
8910 cur = cur->next;
niisato8ee24222018-06-25 19:05:48 +09008911 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008912 }
8913
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008914 return( 0 );
8915}
8916
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008917int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008918 mbedtls_x509_crt *own_cert,
8919 mbedtls_pk_context *pk_key )
8920{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02008921 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008922}
8923
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008924void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008925 mbedtls_x509_crt *ca_chain,
8926 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008927{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008928 conf->ca_chain = ca_chain;
8929 conf->ca_crl = ca_crl;
Hanno Becker5adaad92019-03-27 16:54:37 +00008930
8931#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
8932 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
8933 * cannot be used together. */
8934 conf->f_ca_cb = NULL;
8935 conf->p_ca_cb = NULL;
8936#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Paul Bakker5121ce52009-01-03 21:22:43 +00008937}
Hanno Becker5adaad92019-03-27 16:54:37 +00008938
8939#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
8940void mbedtls_ssl_conf_ca_cb( mbedtls_ssl_config *conf,
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00008941 mbedtls_x509_crt_ca_cb_t f_ca_cb,
Hanno Becker5adaad92019-03-27 16:54:37 +00008942 void *p_ca_cb )
8943{
8944 conf->f_ca_cb = f_ca_cb;
8945 conf->p_ca_cb = p_ca_cb;
8946
8947 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
8948 * cannot be used together. */
8949 conf->ca_chain = NULL;
8950 conf->ca_crl = NULL;
8951}
8952#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008953#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00008954
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02008955#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
8956int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
8957 mbedtls_x509_crt *own_cert,
8958 mbedtls_pk_context *pk_key )
8959{
8960 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
8961 own_cert, pk_key ) );
8962}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02008963
8964void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
8965 mbedtls_x509_crt *ca_chain,
8966 mbedtls_x509_crl *ca_crl )
8967{
8968 ssl->handshake->sni_ca_chain = ca_chain;
8969 ssl->handshake->sni_ca_crl = ca_crl;
8970}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02008971
8972void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
8973 int authmode )
8974{
8975 ssl->handshake->sni_authmode = authmode;
8976}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02008977#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
8978
Hanno Becker8927c832019-04-03 12:52:50 +01008979#if defined(MBEDTLS_X509_CRT_PARSE_C)
8980void mbedtls_ssl_set_verify( mbedtls_ssl_context *ssl,
8981 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
8982 void *p_vrfy )
8983{
8984 ssl->f_vrfy = f_vrfy;
8985 ssl->p_vrfy = p_vrfy;
8986}
8987#endif
8988
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008989#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008990/*
8991 * Set EC J-PAKE password for current handshake
8992 */
8993int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
8994 const unsigned char *pw,
8995 size_t pw_len )
8996{
8997 mbedtls_ecjpake_role role;
8998
Janos Follath8eb64132016-06-03 15:40:57 +01008999 if( ssl->handshake == NULL || ssl->conf == NULL )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02009000 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9001
9002 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
9003 role = MBEDTLS_ECJPAKE_SERVER;
9004 else
9005 role = MBEDTLS_ECJPAKE_CLIENT;
9006
9007 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
9008 role,
9009 MBEDTLS_MD_SHA256,
9010 MBEDTLS_ECP_DP_SECP256R1,
9011 pw, pw_len ) );
9012}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02009013#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02009014
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009015#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01009016
9017static void ssl_conf_remove_psk( mbedtls_ssl_config *conf )
9018{
9019 /* Remove reference to existing PSK, if any. */
9020#if defined(MBEDTLS_USE_PSA_CRYPTO)
9021 if( conf->psk_opaque != 0 )
9022 {
9023 /* The maintenance of the PSK key slot is the
9024 * user's responsibility. */
9025 conf->psk_opaque = 0;
9026 }
Hanno Beckera63ac3f2018-11-05 12:47:16 +00009027 /* This and the following branch should never
9028 * be taken simultaenously as we maintain the
9029 * invariant that raw and opaque PSKs are never
9030 * configured simultaneously. As a safeguard,
9031 * though, `else` is omitted here. */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01009032#endif /* MBEDTLS_USE_PSA_CRYPTO */
9033 if( conf->psk != NULL )
9034 {
9035 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
9036
9037 mbedtls_free( conf->psk );
9038 conf->psk = NULL;
9039 conf->psk_len = 0;
9040 }
9041
9042 /* Remove reference to PSK identity, if any. */
9043 if( conf->psk_identity != NULL )
9044 {
9045 mbedtls_free( conf->psk_identity );
9046 conf->psk_identity = NULL;
9047 conf->psk_identity_len = 0;
9048 }
9049}
9050
Hanno Becker7390c712018-11-15 13:33:04 +00009051/* This function assumes that PSK identity in the SSL config is unset.
9052 * It checks that the provided identity is well-formed and attempts
9053 * to make a copy of it in the SSL config.
9054 * On failure, the PSK identity in the config remains unset. */
9055static int ssl_conf_set_psk_identity( mbedtls_ssl_config *conf,
9056 unsigned char const *psk_identity,
9057 size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02009058{
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02009059 /* Identity len will be encoded on two bytes */
Hanno Becker7390c712018-11-15 13:33:04 +00009060 if( psk_identity == NULL ||
9061 ( psk_identity_len >> 16 ) != 0 ||
Angus Grattond8213d02016-05-25 20:56:48 +10009062 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02009063 {
9064 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9065 }
9066
Hanno Becker7390c712018-11-15 13:33:04 +00009067 conf->psk_identity = mbedtls_calloc( 1, psk_identity_len );
9068 if( conf->psk_identity == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02009069 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker6db455e2013-09-18 17:29:31 +02009070
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01009071 conf->psk_identity_len = psk_identity_len;
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01009072 memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker5ad403f2013-09-18 21:21:30 +02009073
9074 return( 0 );
Paul Bakker6db455e2013-09-18 17:29:31 +02009075}
9076
Hanno Becker7390c712018-11-15 13:33:04 +00009077int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
9078 const unsigned char *psk, size_t psk_len,
9079 const unsigned char *psk_identity, size_t psk_identity_len )
9080{
9081 int ret;
9082 /* Remove opaque/raw PSK + PSK Identity */
9083 ssl_conf_remove_psk( conf );
9084
9085 /* Check and set raw PSK */
9086 if( psk == NULL || psk_len > MBEDTLS_PSK_MAX_LEN )
9087 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9088 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
9089 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
9090 conf->psk_len = psk_len;
9091 memcpy( conf->psk, psk, conf->psk_len );
9092
9093 /* Check and set PSK Identity */
9094 ret = ssl_conf_set_psk_identity( conf, psk_identity, psk_identity_len );
9095 if( ret != 0 )
9096 ssl_conf_remove_psk( conf );
9097
9098 return( ret );
9099}
9100
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01009101static void ssl_remove_psk( mbedtls_ssl_context *ssl )
9102{
9103#if defined(MBEDTLS_USE_PSA_CRYPTO)
9104 if( ssl->handshake->psk_opaque != 0 )
9105 {
9106 ssl->handshake->psk_opaque = 0;
9107 }
9108 else
9109#endif /* MBEDTLS_USE_PSA_CRYPTO */
9110 if( ssl->handshake->psk != NULL )
9111 {
9112 mbedtls_platform_zeroize( ssl->handshake->psk,
9113 ssl->handshake->psk_len );
9114 mbedtls_free( ssl->handshake->psk );
9115 ssl->handshake->psk_len = 0;
9116 }
9117}
9118
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01009119int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
9120 const unsigned char *psk, size_t psk_len )
9121{
9122 if( psk == NULL || ssl->handshake == NULL )
9123 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9124
9125 if( psk_len > MBEDTLS_PSK_MAX_LEN )
9126 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9127
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01009128 ssl_remove_psk( ssl );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01009129
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02009130 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02009131 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01009132
9133 ssl->handshake->psk_len = psk_len;
9134 memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
9135
9136 return( 0 );
9137}
9138
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01009139#if defined(MBEDTLS_USE_PSA_CRYPTO)
9140int mbedtls_ssl_conf_psk_opaque( mbedtls_ssl_config *conf,
Andrzej Kurek2349c4d2019-01-08 09:36:01 -05009141 psa_key_handle_t psk_slot,
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01009142 const unsigned char *psk_identity,
9143 size_t psk_identity_len )
9144{
Hanno Becker7390c712018-11-15 13:33:04 +00009145 int ret;
9146 /* Clear opaque/raw PSK + PSK Identity, if present. */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01009147 ssl_conf_remove_psk( conf );
9148
Hanno Becker7390c712018-11-15 13:33:04 +00009149 /* Check and set opaque PSK */
9150 if( psk_slot == 0 )
9151 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01009152 conf->psk_opaque = psk_slot;
Hanno Becker7390c712018-11-15 13:33:04 +00009153
9154 /* Check and set PSK Identity */
9155 ret = ssl_conf_set_psk_identity( conf, psk_identity,
9156 psk_identity_len );
9157 if( ret != 0 )
9158 ssl_conf_remove_psk( conf );
9159
9160 return( ret );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01009161}
9162
9163int mbedtls_ssl_set_hs_psk_opaque( mbedtls_ssl_context *ssl,
Andrzej Kurek2349c4d2019-01-08 09:36:01 -05009164 psa_key_handle_t psk_slot )
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01009165{
9166 if( psk_slot == 0 || ssl->handshake == NULL )
9167 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9168
9169 ssl_remove_psk( ssl );
9170 ssl->handshake->psk_opaque = psk_slot;
9171 return( 0 );
9172}
9173#endif /* MBEDTLS_USE_PSA_CRYPTO */
9174
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009175void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009176 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
Paul Bakker6db455e2013-09-18 17:29:31 +02009177 size_t),
9178 void *p_psk )
9179{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009180 conf->f_psk = f_psk;
9181 conf->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02009182}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009183#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00009184
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02009185#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker470a8c42017-10-04 15:28:46 +01009186
9187#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009188int mbedtls_ssl_conf_dh_param( mbedtls_ssl_config *conf, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00009189{
9190 int ret;
9191
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01009192 if( ( ret = mbedtls_mpi_read_string( &conf->dhm_P, 16, dhm_P ) ) != 0 ||
9193 ( ret = mbedtls_mpi_read_string( &conf->dhm_G, 16, dhm_G ) ) != 0 )
9194 {
9195 mbedtls_mpi_free( &conf->dhm_P );
9196 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00009197 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01009198 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009199
9200 return( 0 );
9201}
Hanno Becker470a8c42017-10-04 15:28:46 +01009202#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +00009203
Hanno Beckera90658f2017-10-04 15:29:08 +01009204int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
9205 const unsigned char *dhm_P, size_t P_len,
9206 const unsigned char *dhm_G, size_t G_len )
9207{
9208 int ret;
9209
9210 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
9211 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
9212 {
9213 mbedtls_mpi_free( &conf->dhm_P );
9214 mbedtls_mpi_free( &conf->dhm_G );
9215 return( ret );
9216 }
9217
9218 return( 0 );
9219}
Paul Bakker5121ce52009-01-03 21:22:43 +00009220
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009221int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00009222{
9223 int ret;
9224
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01009225 if( ( ret = mbedtls_mpi_copy( &conf->dhm_P, &dhm_ctx->P ) ) != 0 ||
9226 ( ret = mbedtls_mpi_copy( &conf->dhm_G, &dhm_ctx->G ) ) != 0 )
9227 {
9228 mbedtls_mpi_free( &conf->dhm_P );
9229 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00009230 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01009231 }
Paul Bakker1b57b062011-01-06 15:48:19 +00009232
9233 return( 0 );
9234}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02009235#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00009236
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02009237#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
9238/*
9239 * Set the minimum length for Diffie-Hellman parameters
9240 */
9241void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
9242 unsigned int bitlen )
9243{
9244 conf->dhm_min_bitlen = bitlen;
9245}
9246#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
9247
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02009248#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02009249/*
9250 * Set allowed/preferred hashes for handshake signatures
9251 */
9252void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
9253 const int *hashes )
9254{
9255 conf->sig_hashes = hashes;
9256}
Hanno Becker947194e2017-04-07 13:25:49 +01009257#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02009258
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02009259#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01009260/*
9261 * Set the allowed elliptic curves
9262 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009263void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009264 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01009265{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009266 conf->curve_list = curve_list;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01009267}
Hanno Becker947194e2017-04-07 13:25:49 +01009268#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01009269
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01009270#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009271int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00009272{
Hanno Becker947194e2017-04-07 13:25:49 +01009273 /* Initialize to suppress unnecessary compiler warning */
9274 size_t hostname_len = 0;
9275
9276 /* Check if new hostname is valid before
9277 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01009278 if( hostname != NULL )
9279 {
9280 hostname_len = strlen( hostname );
9281
9282 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
9283 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9284 }
9285
9286 /* Now it's clear that we will overwrite the old hostname,
9287 * so we can free it safely */
9288
9289 if( ssl->hostname != NULL )
9290 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05009291 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Hanno Becker947194e2017-04-07 13:25:49 +01009292 mbedtls_free( ssl->hostname );
9293 }
9294
9295 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01009296
Paul Bakker5121ce52009-01-03 21:22:43 +00009297 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01009298 {
9299 ssl->hostname = NULL;
9300 }
9301 else
9302 {
9303 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01009304 if( ssl->hostname == NULL )
9305 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02009306
Hanno Becker947194e2017-04-07 13:25:49 +01009307 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02009308
Hanno Becker947194e2017-04-07 13:25:49 +01009309 ssl->hostname[hostname_len] = '\0';
9310 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009311
9312 return( 0 );
9313}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01009314#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00009315
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01009316#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009317void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009318 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00009319 const unsigned char *, size_t),
9320 void *p_sni )
9321{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009322 conf->f_sni = f_sni;
9323 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00009324}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009325#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00009326
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009327#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009328int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009329{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009330 size_t cur_len, tot_len;
9331 const char **p;
9332
9333 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08009334 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
9335 * MUST NOT be truncated."
9336 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009337 */
9338 tot_len = 0;
9339 for( p = protos; *p != NULL; p++ )
9340 {
9341 cur_len = strlen( *p );
9342 tot_len += cur_len;
9343
9344 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009345 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009346 }
9347
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009348 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009349
9350 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009351}
9352
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009353const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009354{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009355 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009356}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009357#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009358
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02009359void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00009360{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009361 conf->max_major_ver = major;
9362 conf->max_minor_ver = minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00009363}
9364
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02009365void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00009366{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009367 conf->min_major_ver = major;
9368 conf->min_minor_ver = minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00009369}
9370
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009371#if defined(MBEDTLS_SSL_FALLBACK_SCSV) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009372void mbedtls_ssl_conf_fallback( mbedtls_ssl_config *conf, char fallback )
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02009373{
Manuel Pégourié-Gonnard684b0592015-05-06 09:27:31 +01009374 conf->fallback = fallback;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02009375}
9376#endif
9377
Janos Follath088ce432017-04-10 12:42:31 +01009378#if defined(MBEDTLS_SSL_SRV_C)
9379void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
9380 char cert_req_ca_list )
9381{
9382 conf->cert_req_ca_list = cert_req_ca_list;
9383}
9384#endif
9385
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009386#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009387void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01009388{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009389 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01009390}
9391#endif
9392
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009393#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009394void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02009395{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009396 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02009397}
9398#endif
9399
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02009400#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009401void mbedtls_ssl_conf_arc4_support( mbedtls_ssl_config *conf, char arc4 )
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01009402{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009403 conf->arc4_disabled = arc4;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01009404}
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02009405#endif
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01009406
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009407#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009408int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009409{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009410 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
Angus Grattond8213d02016-05-25 20:56:48 +10009411 ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009412 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009413 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009414 }
9415
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01009416 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009417
9418 return( 0 );
9419}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009420#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009421
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009422#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02009423void mbedtls_ssl_conf_truncated_hmac( mbedtls_ssl_config *conf, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02009424{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009425 conf->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02009426}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009427#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02009428
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009429#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009430void mbedtls_ssl_conf_cbc_record_splitting( mbedtls_ssl_config *conf, char split )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009431{
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01009432 conf->cbc_record_splitting = split;
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009433}
9434#endif
9435
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009436void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00009437{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009438 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00009439}
9440
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009441#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009442void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01009443{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009444 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01009445}
9446
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009447void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009448{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009449 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009450}
9451
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009452void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01009453 const unsigned char period[8] )
9454{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009455 memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01009456}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009457#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00009458
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009459#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009460#if defined(MBEDTLS_SSL_CLI_C)
9461void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02009462{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01009463 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02009464}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009465#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02009466
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009467#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02009468void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
9469 mbedtls_ssl_ticket_write_t *f_ticket_write,
9470 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
9471 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02009472{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02009473 conf->f_ticket_write = f_ticket_write;
9474 conf->f_ticket_parse = f_ticket_parse;
9475 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02009476}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009477#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009478#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02009479
Robert Cragie4feb7ae2015-10-02 13:33:37 +01009480#if defined(MBEDTLS_SSL_EXPORT_KEYS)
9481void mbedtls_ssl_conf_export_keys_cb( mbedtls_ssl_config *conf,
9482 mbedtls_ssl_export_keys_t *f_export_keys,
9483 void *p_export_keys )
9484{
9485 conf->f_export_keys = f_export_keys;
9486 conf->p_export_keys = p_export_keys;
9487}
Ron Eldorf5cc10d2019-05-07 18:33:40 +03009488
9489void mbedtls_ssl_conf_export_keys_ext_cb( mbedtls_ssl_config *conf,
9490 mbedtls_ssl_export_keys_ext_t *f_export_keys_ext,
9491 void *p_export_keys )
9492{
9493 conf->f_export_keys_ext = f_export_keys_ext;
9494 conf->p_export_keys = p_export_keys;
9495}
Robert Cragie4feb7ae2015-10-02 13:33:37 +01009496#endif
9497
Gilles Peskineb74a1c72018-04-24 13:09:22 +02009498#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009499void mbedtls_ssl_conf_async_private_cb(
9500 mbedtls_ssl_config *conf,
9501 mbedtls_ssl_async_sign_t *f_async_sign,
9502 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
9503 mbedtls_ssl_async_resume_t *f_async_resume,
9504 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009505 void *async_config_data )
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009506{
9507 conf->f_async_sign_start = f_async_sign;
9508 conf->f_async_decrypt_start = f_async_decrypt;
9509 conf->f_async_resume = f_async_resume;
9510 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009511 conf->p_async_config_data = async_config_data;
9512}
9513
Gilles Peskine8f97af72018-04-26 11:46:10 +02009514void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
9515{
9516 return( conf->p_async_config_data );
9517}
9518
Gilles Peskine1febfef2018-04-30 11:54:39 +02009519void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009520{
9521 if( ssl->handshake == NULL )
9522 return( NULL );
9523 else
9524 return( ssl->handshake->user_async_ctx );
9525}
9526
Gilles Peskine1febfef2018-04-30 11:54:39 +02009527void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009528 void *ctx )
9529{
9530 if( ssl->handshake != NULL )
9531 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009532}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02009533#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009534
Paul Bakker5121ce52009-01-03 21:22:43 +00009535/*
9536 * SSL get accessors
9537 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009538size_t mbedtls_ssl_get_bytes_avail( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009539{
9540 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
9541}
9542
Hanno Becker8b170a02017-10-10 11:51:19 +01009543int mbedtls_ssl_check_pending( const mbedtls_ssl_context *ssl )
9544{
9545 /*
9546 * Case A: We're currently holding back
9547 * a message for further processing.
9548 */
9549
9550 if( ssl->keep_current_message == 1 )
9551 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009552 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: record held back for processing" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009553 return( 1 );
9554 }
9555
9556 /*
9557 * Case B: Further records are pending in the current datagram.
9558 */
9559
9560#if defined(MBEDTLS_SSL_PROTO_DTLS)
9561 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
9562 ssl->in_left > ssl->next_record_offset )
9563 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009564 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more records within current datagram" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009565 return( 1 );
9566 }
9567#endif /* MBEDTLS_SSL_PROTO_DTLS */
9568
9569 /*
9570 * Case C: A handshake message is being processed.
9571 */
9572
Hanno Becker8b170a02017-10-10 11:51:19 +01009573 if( ssl->in_hslen > 0 && ssl->in_hslen < ssl->in_msglen )
9574 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009575 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more handshake messages within current record" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009576 return( 1 );
9577 }
9578
9579 /*
9580 * Case D: An application data message is being processed
9581 */
9582 if( ssl->in_offt != NULL )
9583 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009584 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: application data record is being processed" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009585 return( 1 );
9586 }
9587
9588 /*
9589 * In all other cases, the rest of the message can be dropped.
Hanno Beckerc573ac32018-08-28 17:15:25 +01009590 * As in ssl_get_next_record, this needs to be adapted if
Hanno Becker8b170a02017-10-10 11:51:19 +01009591 * we implement support for multiple alerts in single records.
9592 */
9593
9594 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: nothing pending" ) );
9595 return( 0 );
9596}
9597
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02009598uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009599{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00009600 if( ssl->session != NULL )
9601 return( ssl->session->verify_result );
9602
9603 if( ssl->session_negotiate != NULL )
9604 return( ssl->session_negotiate->verify_result );
9605
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02009606 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00009607}
9608
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009609const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00009610{
Paul Bakker926c8e42013-03-06 10:23:34 +01009611 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009612 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01009613
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009614 return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00009615}
9616
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009617const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00009618{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009619#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009620 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009621 {
9622 switch( ssl->minor_ver )
9623 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009624 case MBEDTLS_SSL_MINOR_VERSION_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009625 return( "DTLSv1.0" );
9626
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009627 case MBEDTLS_SSL_MINOR_VERSION_3:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009628 return( "DTLSv1.2" );
9629
9630 default:
9631 return( "unknown (DTLS)" );
9632 }
9633 }
9634#endif
9635
Paul Bakker43ca69c2011-01-15 17:35:19 +00009636 switch( ssl->minor_ver )
9637 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009638 case MBEDTLS_SSL_MINOR_VERSION_0:
Paul Bakker43ca69c2011-01-15 17:35:19 +00009639 return( "SSLv3.0" );
9640
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009641 case MBEDTLS_SSL_MINOR_VERSION_1:
Paul Bakker43ca69c2011-01-15 17:35:19 +00009642 return( "TLSv1.0" );
9643
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009644 case MBEDTLS_SSL_MINOR_VERSION_2:
Paul Bakker43ca69c2011-01-15 17:35:19 +00009645 return( "TLSv1.1" );
9646
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009647 case MBEDTLS_SSL_MINOR_VERSION_3:
Paul Bakker1ef83d62012-04-11 12:09:53 +00009648 return( "TLSv1.2" );
9649
Paul Bakker43ca69c2011-01-15 17:35:19 +00009650 default:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009651 return( "unknown" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00009652 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00009653}
9654
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009655int mbedtls_ssl_get_record_expansion( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009656{
Hanno Becker3136ede2018-08-17 15:28:19 +01009657 size_t transform_expansion = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009658 const mbedtls_ssl_transform *transform = ssl->transform_out;
Hanno Becker5b559ac2018-08-03 09:40:07 +01009659 unsigned block_size;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009660
Hanno Becker5903de42019-05-03 14:46:38 +01009661 size_t out_hdr_len = mbedtls_ssl_out_hdr_len( ssl );
9662
Hanno Becker78640902018-08-13 16:35:15 +01009663 if( transform == NULL )
Hanno Becker5903de42019-05-03 14:46:38 +01009664 return( (int) out_hdr_len );
Hanno Becker78640902018-08-13 16:35:15 +01009665
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009666#if defined(MBEDTLS_ZLIB_SUPPORT)
9667 if( ssl->session_out->compression != MBEDTLS_SSL_COMPRESS_NULL )
9668 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009669#endif
9670
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009671 switch( mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009672 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009673 case MBEDTLS_MODE_GCM:
9674 case MBEDTLS_MODE_CCM:
Hanno Becker5b559ac2018-08-03 09:40:07 +01009675 case MBEDTLS_MODE_CHACHAPOLY:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009676 case MBEDTLS_MODE_STREAM:
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009677 transform_expansion = transform->minlen;
9678 break;
9679
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009680 case MBEDTLS_MODE_CBC:
Hanno Becker5b559ac2018-08-03 09:40:07 +01009681
9682 block_size = mbedtls_cipher_get_block_size(
9683 &transform->cipher_ctx_enc );
9684
Hanno Becker3136ede2018-08-17 15:28:19 +01009685 /* Expansion due to the addition of the MAC. */
9686 transform_expansion += transform->maclen;
9687
9688 /* Expansion due to the addition of CBC padding;
9689 * Theoretically up to 256 bytes, but we never use
9690 * more than the block size of the underlying cipher. */
9691 transform_expansion += block_size;
9692
9693 /* For TLS 1.1 or higher, an explicit IV is added
9694 * after the record header. */
Hanno Becker5b559ac2018-08-03 09:40:07 +01009695#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
9696 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Hanno Becker3136ede2018-08-17 15:28:19 +01009697 transform_expansion += block_size;
Hanno Becker5b559ac2018-08-03 09:40:07 +01009698#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker3136ede2018-08-17 15:28:19 +01009699
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009700 break;
9701
9702 default:
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02009703 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009704 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009705 }
9706
Hanno Beckera0e20d02019-05-15 14:03:01 +01009707#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker6cbad552019-05-08 15:40:11 +01009708 if( transform->out_cid_len != 0 )
9709 transform_expansion += MBEDTLS_SSL_MAX_CID_EXPANSION;
Hanno Beckera0e20d02019-05-15 14:03:01 +01009710#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker6cbad552019-05-08 15:40:11 +01009711
Hanno Becker5903de42019-05-03 14:46:38 +01009712 return( (int)( out_hdr_len + transform_expansion ) );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009713}
9714
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009715#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9716size_t mbedtls_ssl_get_max_frag_len( const mbedtls_ssl_context *ssl )
9717{
9718 size_t max_len;
9719
9720 /*
9721 * Assume mfl_code is correct since it was checked when set
9722 */
Angus Grattond8213d02016-05-25 20:56:48 +10009723 max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009724
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02009725 /* Check if a smaller max length was negotiated */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009726 if( ssl->session_out != NULL &&
Angus Grattond8213d02016-05-25 20:56:48 +10009727 ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009728 {
Angus Grattond8213d02016-05-25 20:56:48 +10009729 max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009730 }
9731
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02009732 /* During a handshake, use the value being negotiated */
9733 if( ssl->session_negotiate != NULL &&
9734 ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
9735 {
9736 max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
9737 }
9738
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009739 return( max_len );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009740}
9741#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
9742
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009743#if defined(MBEDTLS_SSL_PROTO_DTLS)
9744static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl )
9745{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04009746 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
9747 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
9748 ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
9749 ssl->state == MBEDTLS_SSL_SERVER_HELLO ) )
9750 return ( 0 );
9751
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009752 if( ssl->handshake == NULL || ssl->handshake->mtu == 0 )
9753 return( ssl->mtu );
9754
9755 if( ssl->mtu == 0 )
9756 return( ssl->handshake->mtu );
9757
9758 return( ssl->mtu < ssl->handshake->mtu ?
9759 ssl->mtu : ssl->handshake->mtu );
9760}
9761#endif /* MBEDTLS_SSL_PROTO_DTLS */
9762
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009763int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
9764{
9765 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
9766
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02009767#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
9768 !defined(MBEDTLS_SSL_PROTO_DTLS)
9769 (void) ssl;
9770#endif
9771
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009772#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9773 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
9774
9775 if( max_len > mfl )
9776 max_len = mfl;
9777#endif
9778
9779#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009780 if( ssl_get_current_mtu( ssl ) != 0 )
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009781 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009782 const size_t mtu = ssl_get_current_mtu( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009783 const int ret = mbedtls_ssl_get_record_expansion( ssl );
9784 const size_t overhead = (size_t) ret;
9785
9786 if( ret < 0 )
9787 return( ret );
9788
9789 if( mtu <= overhead )
9790 {
9791 MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
9792 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
9793 }
9794
9795 if( max_len > mtu - overhead )
9796 max_len = mtu - overhead;
9797 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009798#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009799
Hanno Becker0defedb2018-08-10 12:35:02 +01009800#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
9801 !defined(MBEDTLS_SSL_PROTO_DTLS)
9802 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009803#endif
9804
9805 return( (int) max_len );
9806}
9807
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009808#if defined(MBEDTLS_X509_CRT_PARSE_C)
9809const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00009810{
9811 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009812 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00009813
Hanno Beckere6824572019-02-07 13:18:46 +00009814#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009815 return( ssl->session->peer_cert );
Hanno Beckere6824572019-02-07 13:18:46 +00009816#else
9817 return( NULL );
9818#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakkerb0550d92012-10-30 07:51:03 +00009819}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009820#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00009821
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009822#if defined(MBEDTLS_SSL_CLI_C)
Hanno Beckerf852b1c2019-02-05 11:42:30 +00009823int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl,
9824 mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009825{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009826 if( ssl == NULL ||
9827 dst == NULL ||
9828 ssl->session == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009829 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009830 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009831 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009832 }
9833
Hanno Becker52055ae2019-02-06 14:30:46 +00009834 return( mbedtls_ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009835}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009836#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009837
Manuel Pégourié-Gonnardb5e4e0a2019-05-20 11:12:28 +02009838const mbedtls_ssl_session *mbedtls_ssl_get_session_pointer( const mbedtls_ssl_context *ssl )
9839{
9840 if( ssl == NULL )
9841 return( NULL );
9842
9843 return( ssl->session );
9844}
9845
Paul Bakker5121ce52009-01-03 21:22:43 +00009846/*
Hanno Beckera835da52019-05-16 12:39:07 +01009847 * Define ticket header determining Mbed TLS version
9848 * and structure of the ticket.
9849 */
9850
9851 static unsigned char ssl_serialized_session_header[] = {
9852 MBEDTLS_VERSION_MAJOR,
9853 MBEDTLS_VERSION_MINOR,
9854 MBEDTLS_VERSION_PATCH,
9855 };
9856
9857/*
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02009858 * Serialize a session in the following format:
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02009859 * (in the presentation language of TLS, RFC 8446 section 3)
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02009860 *
Hanno Beckera835da52019-05-16 12:39:07 +01009861 * opaque mbedtls_version[3]; // major, minor, patch
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02009862 * uint64 start_time;
9863 * uint8 ciphersuite[2]; // defined by the standard
9864 * uint8 compression; // 0 or 1
9865 * uint8 session_id_len; // at most 32
9866 * opaque session_id[32];
9867 * opaque master[48]; // fixed length in the standard
9868 * uint32 verify_result;
9869 * opaque peer_cert<0..2^24-1>; // length 0 means no peer cert
9870 * opaque ticket<0..2^24-1>; // length 0 means no ticket
9871 * uint32 ticket_lifetime;
9872 * uint8 mfl_code; // up to 255 according to standard
9873 * uint8 trunc_hmac; // 0 or 1
9874 * uint8 encrypt_then_mac; // 0 or 1
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02009875 *
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02009876 * The order is the same as in the definition of the structure, except
9877 * verify_result is put before peer_cert so that all mandatory fields come
9878 * together in one block.
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02009879 */
9880int mbedtls_ssl_session_save( const mbedtls_ssl_session *session,
9881 unsigned char *buf,
9882 size_t buf_len,
9883 size_t *olen )
9884{
9885 unsigned char *p = buf;
Manuel Pégourié-Gonnard26f982f2019-05-21 11:01:32 +02009886 size_t used = 0;
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02009887#if defined(MBEDTLS_HAVE_TIME)
9888 uint64_t start;
9889#endif
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02009890#if defined(MBEDTLS_X509_CRT_PARSE_C)
9891#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
9892 size_t cert_len;
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02009893#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
9894#endif /* MBEDTLS_X509_CRT_PARSE_C */
9895
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02009896
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02009897 /*
Hanno Beckera835da52019-05-16 12:39:07 +01009898 * Add version identifier
9899 */
9900
9901 used += sizeof( ssl_serialized_session_header );
9902
9903 if( used <= buf_len )
9904 {
9905 memcpy( p, ssl_serialized_session_header,
9906 sizeof( ssl_serialized_session_header ) );
9907 p += sizeof( ssl_serialized_session_header );
9908 }
9909
9910 /*
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02009911 * Time
9912 */
9913#if defined(MBEDTLS_HAVE_TIME)
9914 used += 8;
9915
Manuel Pégourié-Gonnard26f982f2019-05-21 11:01:32 +02009916 if( used <= buf_len )
9917 {
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02009918 start = (uint64_t) session->start;
9919
9920 *p++ = (unsigned char)( ( start >> 56 ) & 0xFF );
9921 *p++ = (unsigned char)( ( start >> 48 ) & 0xFF );
9922 *p++ = (unsigned char)( ( start >> 40 ) & 0xFF );
9923 *p++ = (unsigned char)( ( start >> 32 ) & 0xFF );
9924 *p++ = (unsigned char)( ( start >> 24 ) & 0xFF );
9925 *p++ = (unsigned char)( ( start >> 16 ) & 0xFF );
9926 *p++ = (unsigned char)( ( start >> 8 ) & 0xFF );
9927 *p++ = (unsigned char)( ( start ) & 0xFF );
9928 }
9929#endif /* MBEDTLS_HAVE_TIME */
9930
9931 /*
9932 * Basic mandatory fields
9933 */
9934 used += 2 /* ciphersuite */
9935 + 1 /* compression */
9936 + 1 /* id_len */
9937 + sizeof( session->id )
9938 + sizeof( session->master )
9939 + 4; /* verify_result */
9940
9941 if( used <= buf_len )
9942 {
9943 *p++ = (unsigned char)( ( session->ciphersuite >> 8 ) & 0xFF );
9944 *p++ = (unsigned char)( ( session->ciphersuite ) & 0xFF );
9945
9946 *p++ = (unsigned char)( session->compression & 0xFF );
9947
9948 *p++ = (unsigned char)( session->id_len & 0xFF );
9949 memcpy( p, session->id, 32 );
9950 p += 32;
9951
9952 memcpy( p, session->master, 48 );
9953 p += 48;
9954
9955 *p++ = (unsigned char)( ( session->verify_result >> 24 ) & 0xFF );
9956 *p++ = (unsigned char)( ( session->verify_result >> 16 ) & 0xFF );
9957 *p++ = (unsigned char)( ( session->verify_result >> 8 ) & 0xFF );
9958 *p++ = (unsigned char)( ( session->verify_result ) & 0xFF );
Manuel Pégourié-Gonnard26f982f2019-05-21 11:01:32 +02009959 }
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02009960
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02009961 /*
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02009962 * Peer's end-entity certificate
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02009963 */
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02009964#if defined(MBEDTLS_X509_CRT_PARSE_C)
9965#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
9966 if( session->peer_cert == NULL )
9967 cert_len = 0;
9968 else
9969 cert_len = session->peer_cert->raw.len;
9970
Manuel Pégourié-Gonnard26f982f2019-05-21 11:01:32 +02009971 used += 3 + cert_len;
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02009972
Manuel Pégourié-Gonnard26f982f2019-05-21 11:01:32 +02009973 if( used <= buf_len )
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02009974 {
Manuel Pégourié-Gonnard26f982f2019-05-21 11:01:32 +02009975 *p++ = (unsigned char)( ( cert_len >> 16 ) & 0xFF );
9976 *p++ = (unsigned char)( ( cert_len >> 8 ) & 0xFF );
9977 *p++ = (unsigned char)( ( cert_len ) & 0xFF );
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02009978
Manuel Pégourié-Gonnard26f982f2019-05-21 11:01:32 +02009979 if( session->peer_cert != NULL )
9980 {
9981 memcpy( p, session->peer_cert->raw.p, cert_len );
9982 p += cert_len;
9983 }
9984 }
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02009985#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02009986 if( session->peer_cert_digest != NULL )
Manuel Pégourié-Gonnard26f982f2019-05-21 11:01:32 +02009987 {
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +02009988 used += 1 /* type */ + 1 /* length */ + session->peer_cert_digest_len;
9989 if( used <= buf_len )
9990 {
9991 *p++ = (unsigned char) session->peer_cert_digest_type;
9992 *p++ = (unsigned char) session->peer_cert_digest_len;
9993 memcpy( p, session->peer_cert_digest,
9994 session->peer_cert_digest_len );
9995 p += session->peer_cert_digest_len;
9996 }
9997 }
9998 else
9999 {
10000 used += 2;
10001 if( used <= buf_len )
10002 {
10003 *p++ = (unsigned char) MBEDTLS_MD_NONE;
10004 *p++ = 0;
10005 }
Manuel Pégourié-Gonnard26f982f2019-05-21 11:01:32 +020010006 }
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +020010007#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
10008#endif /* MBEDTLS_X509_CRT_PARSE_C */
10009
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +020010010 /*
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +020010011 * Session ticket if any, plus associated data
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +020010012 */
10013#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +020010014 used += 3 + session->ticket_len + 4; /* len + ticket + lifetime */
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +020010015
Manuel Pégourié-Gonnard26f982f2019-05-21 11:01:32 +020010016 if( used <= buf_len )
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +020010017 {
Manuel Pégourié-Gonnard26f982f2019-05-21 11:01:32 +020010018 *p++ = (unsigned char)( ( session->ticket_len >> 16 ) & 0xFF );
10019 *p++ = (unsigned char)( ( session->ticket_len >> 8 ) & 0xFF );
10020 *p++ = (unsigned char)( ( session->ticket_len ) & 0xFF );
10021
10022 if( session->ticket != NULL )
10023 {
10024 memcpy( p, session->ticket, session->ticket_len );
10025 p += session->ticket_len;
10026 }
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +020010027
10028 *p++ = (unsigned char)( ( session->ticket_lifetime >> 24 ) & 0xFF );
10029 *p++ = (unsigned char)( ( session->ticket_lifetime >> 16 ) & 0xFF );
10030 *p++ = (unsigned char)( ( session->ticket_lifetime >> 8 ) & 0xFF );
10031 *p++ = (unsigned char)( ( session->ticket_lifetime ) & 0xFF );
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +020010032 }
10033#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
10034
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +020010035 /*
10036 * Misc extension-related info
10037 */
10038#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
10039 used += 1;
10040
10041 if( used <= buf_len )
10042 *p++ = session->mfl_code;
10043#endif
10044
10045#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
10046 used += 1;
10047
10048 if( used <= buf_len )
10049 *p++ = (unsigned char)( ( session->trunc_hmac ) & 0xFF );
10050#endif
10051
10052#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
10053 used += 1;
10054
10055 if( used <= buf_len )
10056 *p++ = (unsigned char)( ( session->encrypt_then_mac ) & 0xFF );
10057#endif
10058
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +020010059 /* Done */
Manuel Pégourié-Gonnard26f982f2019-05-21 11:01:32 +020010060 *olen = used;
10061
10062 if( used > buf_len )
10063 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +020010064
10065 return( 0 );
10066}
10067
10068/*
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +020010069 * Unserialize session, see mbedtls_ssl_session_save() for format.
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +020010070 *
10071 * This internal version is wrapped by a public function that cleans up in
10072 * case of error.
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +020010073 */
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +020010074static int ssl_session_load( mbedtls_ssl_session *session,
10075 const unsigned char *buf,
10076 size_t len )
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +020010077{
10078 const unsigned char *p = buf;
10079 const unsigned char * const end = buf + len;
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +020010080#if defined(MBEDTLS_HAVE_TIME)
10081 uint64_t start;
10082#endif
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +020010083#if defined(MBEDTLS_X509_CRT_PARSE_C)
10084#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
10085 size_t cert_len;
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +020010086#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
10087#endif /* MBEDTLS_X509_CRT_PARSE_C */
10088
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +020010089 /*
Hanno Beckera835da52019-05-16 12:39:07 +010010090 * Check version identifier
10091 */
10092
10093 if( (size_t)( end - p ) < sizeof( ssl_serialized_session_header ) )
10094 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
10095
10096 if( memcmp( p, ssl_serialized_session_header,
10097 sizeof( ssl_serialized_session_header ) ) != 0 )
10098 {
10099 /* A more specific error code might be used here. */
10100 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10101 }
10102 p += sizeof( ssl_serialized_session_header );
10103
10104 /*
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +020010105 * Time
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +020010106 */
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +020010107#if defined(MBEDTLS_HAVE_TIME)
10108 if( 8 > (size_t)( end - p ) )
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +020010109 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10110
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +020010111 start = ( (uint64_t) p[0] << 56 ) |
10112 ( (uint64_t) p[1] << 48 ) |
10113 ( (uint64_t) p[2] << 40 ) |
10114 ( (uint64_t) p[3] << 32 ) |
10115 ( (uint64_t) p[4] << 24 ) |
10116 ( (uint64_t) p[5] << 16 ) |
10117 ( (uint64_t) p[6] << 8 ) |
10118 ( (uint64_t) p[7] );
10119 p += 8;
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +020010120
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +020010121 session->start = (time_t) start;
10122#endif /* MBEDTLS_HAVE_TIME */
10123
10124 /*
10125 * Basic mandatory fields
10126 */
10127 if( 2 + 1 + 1 + 32 + 48 + 4 > (size_t)( end - p ) )
10128 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10129
10130 session->ciphersuite = ( p[0] << 8 ) | p[1];
10131 p += 2;
10132
10133 session->compression = *p++;
10134
10135 session->id_len = *p++;
10136 memcpy( session->id, p, 32 );
10137 p += 32;
10138
10139 memcpy( session->master, p, 48 );
10140 p += 48;
10141
10142 session->verify_result = ( (uint32_t) p[0] << 24 ) |
10143 ( (uint32_t) p[1] << 16 ) |
10144 ( (uint32_t) p[2] << 8 ) |
10145 ( (uint32_t) p[3] );
10146 p += 4;
10147
10148 /* Immediately clear invalid pointer values that have been read, in case
10149 * we exit early before we replaced them with valid ones. */
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +020010150#if defined(MBEDTLS_X509_CRT_PARSE_C)
10151#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
10152 session->peer_cert = NULL;
10153#else
10154 session->peer_cert_digest = NULL;
10155#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
10156#endif /* MBEDTLS_X509_CRT_PARSE_C */
10157#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
10158 session->ticket = NULL;
10159#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
10160
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +020010161 /*
10162 * Peer certificate
10163 */
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +020010164#if defined(MBEDTLS_X509_CRT_PARSE_C)
10165#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
10166 /* Deserialize CRT from the end of the ticket. */
10167 if( 3 > (size_t)( end - p ) )
10168 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10169
10170 cert_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2];
10171 p += 3;
10172
10173 if( cert_len != 0 )
10174 {
10175 int ret;
10176
10177 if( cert_len > (size_t)( end - p ) )
10178 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10179
10180 session->peer_cert = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
10181
10182 if( session->peer_cert == NULL )
10183 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
10184
10185 mbedtls_x509_crt_init( session->peer_cert );
10186
10187 if( ( ret = mbedtls_x509_crt_parse_der( session->peer_cert,
10188 p, cert_len ) ) != 0 )
10189 {
10190 mbedtls_x509_crt_free( session->peer_cert );
10191 mbedtls_free( session->peer_cert );
10192 session->peer_cert = NULL;
10193 return( ret );
10194 }
10195
10196 p += cert_len;
10197 }
10198#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
10199 /* Deserialize CRT digest from the end of the ticket. */
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +020010200 if( 2 > (size_t)( end - p ) )
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +020010201 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10202
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +020010203 session->peer_cert_digest_type = (mbedtls_md_type_t) *p++;
10204 session->peer_cert_digest_len = (size_t) *p++;
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +020010205
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +020010206 if( session->peer_cert_digest_len != 0 )
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +020010207 {
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +020010208 const mbedtls_md_info_t *md_info =
10209 mbedtls_md_info_from_type( session->peer_cert_digest_type );
10210 if( md_info == NULL )
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +020010211 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +020010212 if( session->peer_cert_digest_len != mbedtls_md_get_size( md_info ) )
10213 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +020010214
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +020010215 if( session->peer_cert_digest_len > (size_t)( end - p ) )
10216 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10217
10218 session->peer_cert_digest =
10219 mbedtls_calloc( 1, session->peer_cert_digest_len );
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +020010220 if( session->peer_cert_digest == NULL )
10221 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
10222
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +020010223 memcpy( session->peer_cert_digest, p,
10224 session->peer_cert_digest_len );
10225 p += session->peer_cert_digest_len;
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +020010226 }
10227#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
10228#endif /* MBEDTLS_X509_CRT_PARSE_C */
10229
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +020010230 /*
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +020010231 * Session ticket and associated data
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +020010232 */
10233#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
10234 if( 3 > (size_t)( end - p ) )
10235 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10236
10237 session->ticket_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2];
10238 p += 3;
10239
10240 if( session->ticket_len != 0 )
10241 {
10242 if( session->ticket_len > (size_t)( end - p ) )
10243 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10244
10245 session->ticket = mbedtls_calloc( 1, session->ticket_len );
10246 if( session->ticket == NULL )
10247 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
10248
10249 memcpy( session->ticket, p, session->ticket_len );
10250 p += session->ticket_len;
10251 }
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +020010252
10253 if( 4 > (size_t)( end - p ) )
10254 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10255
10256 session->ticket_lifetime = ( (uint32_t) p[0] << 24 ) |
10257 ( (uint32_t) p[1] << 16 ) |
10258 ( (uint32_t) p[2] << 8 ) |
10259 ( (uint32_t) p[3] );
10260 p += 4;
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +020010261#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
10262
Manuel Pégourié-Gonnardf743c032019-05-24 12:06:29 +020010263 /*
10264 * Misc extension-related info
10265 */
10266#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
10267 if( 1 > (size_t)( end - p ) )
10268 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10269
10270 session->mfl_code = *p++;
10271#endif
10272
10273#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
10274 if( 1 > (size_t)( end - p ) )
10275 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10276
10277 session->trunc_hmac = *p++;
10278#endif
10279
10280#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
10281 if( 1 > (size_t)( end - p ) )
10282 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10283
10284 session->encrypt_then_mac = *p++;
10285#endif
10286
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +020010287 /* Done, should have consumed entire buffer */
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +020010288 if( p != end )
10289 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10290
10291 return( 0 );
10292}
10293
10294/*
Manuel Pégourié-Gonnard686adb42019-06-03 09:55:16 +020010295 * Unserialize session: public wrapper for error cleaning
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +020010296 */
10297int mbedtls_ssl_session_load( mbedtls_ssl_session *session,
10298 const unsigned char *buf,
10299 size_t len )
10300{
10301 int ret = ssl_session_load( session, buf, len );
10302
10303 if( ret != 0 )
10304 mbedtls_ssl_session_free( session );
10305
10306 return( ret );
10307}
10308
10309/*
Paul Bakker1961b702013-01-25 14:49:24 +010010310 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +000010311 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010312int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000010313{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010314 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +000010315
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010316 if( ssl == NULL || ssl->conf == NULL )
10317 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10318
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010319#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010320 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010321 ret = mbedtls_ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +000010322#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010323#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010324 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010325 ret = mbedtls_ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +000010326#endif
10327
Paul Bakker1961b702013-01-25 14:49:24 +010010328 return( ret );
10329}
10330
10331/*
10332 * Perform the SSL handshake
10333 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010334int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +010010335{
10336 int ret = 0;
10337
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010338 if( ssl == NULL || ssl->conf == NULL )
10339 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10340
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010341 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +010010342
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010343 while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker1961b702013-01-25 14:49:24 +010010344 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010345 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +010010346
10347 if( ret != 0 )
10348 break;
10349 }
10350
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010351 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010352
10353 return( ret );
10354}
10355
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010356#if defined(MBEDTLS_SSL_RENEGOTIATION)
10357#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000010358/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010359 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +000010360 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010361static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010362{
10363 int ret;
10364
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010365 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010366
10367 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010368 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
10369 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010370
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +020010371 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010372 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +020010373 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010374 return( ret );
10375 }
10376
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010377 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010378
10379 return( 0 );
10380}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010381#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010382
10383/*
10384 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010385 * - any side: calling mbedtls_ssl_renegotiate(),
10386 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
10387 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +020010388 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010389 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010390 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010391 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010392static int ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +000010393{
10394 int ret;
10395
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010396 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010397
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010398 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
10399 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +000010400
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +020010401 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
10402 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010403#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010404 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010405 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +020010406 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010407 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +020010408 ssl->handshake->out_msg_seq = 1;
10409 else
10410 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +020010411 }
10412#endif
10413
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010414 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
10415 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +000010416
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010417 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +000010418 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010419 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +000010420 return( ret );
10421 }
10422
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010423 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010424
10425 return( 0 );
10426}
10427
10428/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010429 * Renegotiate current connection on client,
10430 * or request renegotiation on server
10431 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010432int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010433{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010434 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010435
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010436 if( ssl == NULL || ssl->conf == NULL )
10437 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10438
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010439#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010440 /* On server, just send the request */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010441 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010442 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010443 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
10444 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010445
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010446 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +020010447
10448 /* Did we already try/start sending HelloRequest? */
10449 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010450 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +020010451
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010452 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010453 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010454#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010455
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010456#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010457 /*
10458 * On client, either start the renegotiation process or,
10459 * if already in progress, continue the handshake
10460 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010461 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010462 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010463 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
10464 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010465
10466 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
10467 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010468 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010469 return( ret );
10470 }
10471 }
10472 else
10473 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010474 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010475 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010476 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010477 return( ret );
10478 }
10479 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010480#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +010010481
Paul Bakker37ce0ff2013-10-31 14:32:04 +010010482 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +010010483}
10484
10485/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010486 * Check record counters and renegotiate if they're above the limit.
10487 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010488static int ssl_check_ctr_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010489{
Andres AG2196c7f2016-12-15 17:01:16 +000010490 size_t ep_len = ssl_ep_len( ssl );
10491 int in_ctr_cmp;
10492 int out_ctr_cmp;
10493
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010494 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ||
10495 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010496 ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010497 {
10498 return( 0 );
10499 }
10500
Andres AG2196c7f2016-12-15 17:01:16 +000010501 in_ctr_cmp = memcmp( ssl->in_ctr + ep_len,
10502 ssl->conf->renego_period + ep_len, 8 - ep_len );
Hanno Becker19859472018-08-06 09:40:20 +010010503 out_ctr_cmp = memcmp( ssl->cur_out_ctr + ep_len,
Andres AG2196c7f2016-12-15 17:01:16 +000010504 ssl->conf->renego_period + ep_len, 8 - ep_len );
10505
10506 if( in_ctr_cmp <= 0 && out_ctr_cmp <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010507 {
10508 return( 0 );
10509 }
10510
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +020010511 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010512 return( mbedtls_ssl_renegotiate( ssl ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010513}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010514#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +000010515
10516/*
10517 * Receive application data decrypted from the SSL layer
10518 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010519int mbedtls_ssl_read( mbedtls_ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +000010520{
Hanno Becker4a810fb2017-05-24 16:27:30 +010010521 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +000010522 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +000010523
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010524 if( ssl == NULL || ssl->conf == NULL )
10525 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10526
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010527 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010528
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010529#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010530 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +020010531 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010532 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +020010533 return( ret );
10534
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010535 if( ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010536 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010537 {
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +020010538 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010539 return( ret );
10540 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +020010541 }
10542#endif
10543
Hanno Becker4a810fb2017-05-24 16:27:30 +010010544 /*
10545 * Check if renegotiation is necessary and/or handshake is
10546 * in process. If yes, perform/continue, and fall through
10547 * if an unexpected packet is received while the client
10548 * is waiting for the ServerHello.
10549 *
10550 * (There is no equivalent to the last condition on
10551 * the server-side as it is not treated as within
10552 * a handshake while waiting for the ClientHello
10553 * after a renegotiation request.)
10554 */
10555
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010556#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +010010557 ret = ssl_check_ctr_renegotiate( ssl );
10558 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
10559 ret != 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010560 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010561 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +010010562 return( ret );
10563 }
10564#endif
10565
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010566 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +000010567 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010568 ret = mbedtls_ssl_handshake( ssl );
Hanno Becker4a810fb2017-05-24 16:27:30 +010010569 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
10570 ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010571 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010572 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010573 return( ret );
10574 }
10575 }
10576
Hanno Beckere41158b2017-10-23 13:30:32 +010010577 /* Loop as long as no application data record is available */
Hanno Becker90333da2017-10-10 11:27:13 +010010578 while( ssl->in_offt == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010579 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +020010580 /* Start timer if not already running */
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +020010581 if( ssl->f_get_timer != NULL &&
10582 ssl->f_get_timer( ssl->p_timer ) == -1 )
10583 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010584 ssl_set_timer( ssl, ssl->conf->read_timeout );
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +020010585 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +020010586
Hanno Becker327c93b2018-08-15 13:56:18 +010010587 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010588 {
Hanno Becker4a810fb2017-05-24 16:27:30 +010010589 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
10590 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +000010591
Hanno Becker4a810fb2017-05-24 16:27:30 +010010592 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
10593 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010594 }
10595
10596 if( ssl->in_msglen == 0 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010597 ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +000010598 {
10599 /*
10600 * OpenSSL sends empty messages to randomize the IV
10601 */
Hanno Becker327c93b2018-08-15 13:56:18 +010010602 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010603 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010604 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
Paul Bakker831a7552011-05-18 13:32:51 +000010605 return( 0 );
10606
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010607 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010608 return( ret );
10609 }
10610 }
10611
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010612 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +000010613 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010614 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010615
Hanno Becker4a810fb2017-05-24 16:27:30 +010010616 /*
10617 * - For client-side, expect SERVER_HELLO_REQUEST.
10618 * - For server-side, expect CLIENT_HELLO.
10619 * - Fail (TLS) or silently drop record (DTLS) in other cases.
10620 */
10621
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010622#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010623 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010624 ( ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST ||
Hanno Becker4a810fb2017-05-24 16:27:30 +010010625 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ) )
Paul Bakker48916f92012-09-16 19:57:18 +000010626 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010627 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010628
10629 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010630#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010631 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +010010632 {
10633 continue;
10634 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010635#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010636 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010637 }
Hanno Becker4a810fb2017-05-24 16:27:30 +010010638#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010639
Hanno Becker4a810fb2017-05-24 16:27:30 +010010640#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010641 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010642 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO )
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010643 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010644 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010645
10646 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010647#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010648 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +010010649 {
10650 continue;
10651 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +020010652#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010653 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker48916f92012-09-16 19:57:18 +000010654 }
Hanno Becker4a810fb2017-05-24 16:27:30 +010010655#endif /* MBEDTLS_SSL_SRV_C */
10656
Hanno Becker21df7f92017-10-17 11:03:26 +010010657#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +010010658 /* Determine whether renegotiation attempt should be accepted */
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +010010659 if( ! ( ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
10660 ( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
10661 ssl->conf->allow_legacy_renegotiation ==
10662 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) ) )
10663 {
10664 /*
10665 * Accept renegotiation request
10666 */
Paul Bakker48916f92012-09-16 19:57:18 +000010667
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +010010668 /* DTLS clients need to know renego is server-initiated */
10669#if defined(MBEDTLS_SSL_PROTO_DTLS)
10670 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
10671 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
10672 {
10673 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
10674 }
10675#endif
10676 ret = ssl_start_renegotiation( ssl );
10677 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
10678 ret != 0 )
10679 {
10680 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
10681 return( ret );
10682 }
10683 }
10684 else
Hanno Becker21df7f92017-10-17 11:03:26 +010010685#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +000010686 {
Hanno Becker4a810fb2017-05-24 16:27:30 +010010687 /*
10688 * Refuse renegotiation
10689 */
10690
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010691 MBEDTLS_SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010692
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010693#if defined(MBEDTLS_SSL_PROTO_SSL3)
10694 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +000010695 {
Gilles Peskine92e44262017-05-10 17:27:49 +020010696 /* SSLv3 does not have a "no_renegotiation" warning, so
10697 we send a fatal alert and abort the connection. */
10698 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
10699 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
10700 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +000010701 }
10702 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010703#endif /* MBEDTLS_SSL_PROTO_SSL3 */
10704#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
10705 defined(MBEDTLS_SSL_PROTO_TLS1_2)
10706 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +000010707 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010708 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
10709 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
10710 MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +000010711 {
10712 return( ret );
10713 }
Paul Bakker48916f92012-09-16 19:57:18 +000010714 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +020010715 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010716#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 ||
10717 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +020010718 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010719 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
10720 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +020010721 }
Paul Bakker48916f92012-09-16 19:57:18 +000010722 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020010723
Hanno Becker90333da2017-10-10 11:27:13 +010010724 /* At this point, we don't know whether the renegotiation has been
10725 * completed or not. The cases to consider are the following:
10726 * 1) The renegotiation is complete. In this case, no new record
10727 * has been read yet.
10728 * 2) The renegotiation is incomplete because the client received
10729 * an application data record while awaiting the ServerHello.
10730 * 3) The renegotiation is incomplete because the client received
10731 * a non-handshake, non-application data message while awaiting
10732 * the ServerHello.
10733 * In each of these case, looping will be the proper action:
10734 * - For 1), the next iteration will read a new record and check
10735 * if it's application data.
10736 * - For 2), the loop condition isn't satisfied as application data
10737 * is present, hence continue is the same as break
10738 * - For 3), the loop condition is satisfied and read_record
10739 * will re-deliver the message that was held back by the client
10740 * when expecting the ServerHello.
10741 */
10742 continue;
Paul Bakker48916f92012-09-16 19:57:18 +000010743 }
Hanno Becker21df7f92017-10-17 11:03:26 +010010744#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010745 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +010010746 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010747 if( ssl->conf->renego_max_records >= 0 )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +020010748 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010749 if( ++ssl->renego_records_seen > ssl->conf->renego_max_records )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020010750 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010751 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020010752 "but not honored by client" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010753 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020010754 }
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +020010755 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +010010756 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010757#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020010758
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010759 /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */
10760 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020010761 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010762 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +010010763 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +020010764 }
10765
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010766 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +000010767 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010768 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
10769 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +000010770 }
10771
10772 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +020010773
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +020010774 /* We're going to return something now, cancel timer,
10775 * except if handshake (renegotiation) is in progress */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010776 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +020010777 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010778
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +020010779#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010780 /* If we requested renego but received AppData, resend HelloRequest.
10781 * Do it now, after setting in_offt, to avoid taking this branch
10782 * again if ssl_write_hello_request() returns WANT_WRITE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010783#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010784 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010785 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010786 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020010787 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010788 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010789 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010790 return( ret );
10791 }
10792 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010793#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker4a810fb2017-05-24 16:27:30 +010010794#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +000010795 }
10796
10797 n = ( len < ssl->in_msglen )
10798 ? len : ssl->in_msglen;
10799
10800 memcpy( buf, ssl->in_offt, n );
10801 ssl->in_msglen -= n;
10802
10803 if( ssl->in_msglen == 0 )
Hanno Becker4a810fb2017-05-24 16:27:30 +010010804 {
10805 /* all bytes consumed */
Paul Bakker5121ce52009-01-03 21:22:43 +000010806 ssl->in_offt = NULL;
Hanno Beckerbdf39052017-06-09 10:42:03 +010010807 ssl->keep_current_message = 0;
Hanno Becker4a810fb2017-05-24 16:27:30 +010010808 }
Paul Bakker5121ce52009-01-03 21:22:43 +000010809 else
Hanno Becker4a810fb2017-05-24 16:27:30 +010010810 {
Paul Bakker5121ce52009-01-03 21:22:43 +000010811 /* more data available */
10812 ssl->in_offt += n;
Hanno Becker4a810fb2017-05-24 16:27:30 +010010813 }
Paul Bakker5121ce52009-01-03 21:22:43 +000010814
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010815 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010816
Paul Bakker23986e52011-04-24 08:57:21 +000010817 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +000010818}
10819
10820/*
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010010821 * Send application data to be encrypted by the SSL layer, taking care of max
10822 * fragment length and buffer size.
10823 *
10824 * According to RFC 5246 Section 6.2.1:
10825 *
10826 * Zero-length fragments of Application data MAY be sent as they are
10827 * potentially useful as a traffic analysis countermeasure.
10828 *
10829 * Therefore, it is possible that the input message length is 0 and the
10830 * corresponding return code is 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +000010831 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010832static int ssl_write_real( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010833 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +000010834{
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +020010835 int ret = mbedtls_ssl_get_max_out_record_payload( ssl );
10836 const size_t max_len = (size_t) ret;
10837
10838 if( ret < 0 )
10839 {
10840 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_get_max_out_record_payload", ret );
10841 return( ret );
10842 }
10843
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010844 if( len > max_len )
10845 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010846#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010847 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010848 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010849 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) "
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010850 "maximum fragment length: %d > %d",
10851 len, max_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010852 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010853 }
10854 else
10855#endif
10856 len = max_len;
10857 }
Paul Bakker887bd502011-06-08 13:10:54 +000010858
Paul Bakker5121ce52009-01-03 21:22:43 +000010859 if( ssl->out_left != 0 )
10860 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010010861 /*
10862 * The user has previously tried to send the data and
10863 * MBEDTLS_ERR_SSL_WANT_WRITE or the message was only partially
10864 * written. In this case, we expect the high-level write function
10865 * (e.g. mbedtls_ssl_write()) to be called with the same parameters
10866 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010867 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010868 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010869 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010870 return( ret );
10871 }
10872 }
Paul Bakker887bd502011-06-08 13:10:54 +000010873 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +000010874 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010010875 /*
10876 * The user is trying to send a message the first time, so we need to
10877 * copy the data into the internal buffers and setup the data structure
10878 * to keep track of partial writes
10879 */
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010880 ssl->out_msglen = len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010881 ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA;
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010882 memcpy( ssl->out_msg, buf, len );
Paul Bakker887bd502011-06-08 13:10:54 +000010883
Hanno Becker67bc7c32018-08-06 11:33:50 +010010884 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker887bd502011-06-08 13:10:54 +000010885 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010886 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker887bd502011-06-08 13:10:54 +000010887 return( ret );
10888 }
Paul Bakker5121ce52009-01-03 21:22:43 +000010889 }
10890
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010891 return( (int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +000010892}
10893
10894/*
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010895 * Write application data, doing 1/n-1 splitting if necessary.
10896 *
10897 * With non-blocking I/O, ssl_write_real() may return WANT_WRITE,
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +010010898 * then the caller will call us again with the same arguments, so
Hanno Becker2b187c42017-09-18 14:58:11 +010010899 * remember whether we already did the split or not.
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010900 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010901#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010902static int ssl_write_split( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010903 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010904{
10905 int ret;
10906
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +010010907 if( ssl->conf->cbc_record_splitting ==
10908 MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED ||
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +010010909 len <= 1 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010910 ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_1 ||
10911 mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc )
10912 != MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010913 {
10914 return( ssl_write_real( ssl, buf, len ) );
10915 }
10916
10917 if( ssl->split_done == 0 )
10918 {
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010010919 if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010920 return( ret );
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010010921 ssl->split_done = 1;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010922 }
10923
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010010924 if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 )
10925 return( ret );
10926 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010927
10928 return( ret + 1 );
10929}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010930#endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010931
10932/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010933 * Write application data (public-facing wrapper)
10934 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010935int mbedtls_ssl_write( mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010936{
10937 int ret;
10938
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010939 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010940
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010941 if( ssl == NULL || ssl->conf == NULL )
10942 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10943
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010944#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010945 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
10946 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010947 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010948 return( ret );
10949 }
10950#endif
10951
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010952 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010953 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010954 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010955 {
Manuel Pégourié-Gonnard151dc772015-05-14 13:55:51 +020010956 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010957 return( ret );
10958 }
10959 }
10960
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010961#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010962 ret = ssl_write_split( ssl, buf, len );
10963#else
10964 ret = ssl_write_real( ssl, buf, len );
10965#endif
10966
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010967 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010968
10969 return( ret );
10970}
10971
10972/*
Paul Bakker5121ce52009-01-03 21:22:43 +000010973 * Notify the peer that the connection is being closed
10974 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010975int mbedtls_ssl_close_notify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000010976{
10977 int ret;
10978
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010979 if( ssl == NULL || ssl->conf == NULL )
10980 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10981
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010982 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010983
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +020010984 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010985 return( mbedtls_ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010986
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010987 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +000010988 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010989 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
10990 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
10991 MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010992 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010993 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010994 return( ret );
10995 }
10996 }
10997
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010998 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010999
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +020011000 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +000011001}
11002
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011003void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform )
Paul Bakker48916f92012-09-16 19:57:18 +000011004{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020011005 if( transform == NULL )
11006 return;
11007
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011008#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +000011009 deflateEnd( &transform->ctx_deflate );
11010 inflateEnd( &transform->ctx_inflate );
11011#endif
11012
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011013 mbedtls_cipher_free( &transform->cipher_ctx_enc );
11014 mbedtls_cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +020011015
Hanno Beckerd56ed242018-01-03 15:32:51 +000011016#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011017 mbedtls_md_free( &transform->md_ctx_enc );
11018 mbedtls_md_free( &transform->md_ctx_dec );
Hanno Beckerd56ed242018-01-03 15:32:51 +000011019#endif
Paul Bakker61d113b2013-07-04 11:51:43 +020011020
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050011021 mbedtls_platform_zeroize( transform, sizeof( mbedtls_ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +000011022}
11023
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011024#if defined(MBEDTLS_X509_CRT_PARSE_C)
11025static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020011026{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011027 mbedtls_ssl_key_cert *cur = key_cert, *next;
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020011028
11029 while( cur != NULL )
11030 {
11031 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011032 mbedtls_free( cur );
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020011033 cur = next;
11034 }
11035}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011036#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020011037
Hanno Becker0271f962018-08-16 13:23:47 +010011038#if defined(MBEDTLS_SSL_PROTO_DTLS)
11039
11040static void ssl_buffering_free( mbedtls_ssl_context *ssl )
11041{
11042 unsigned offset;
11043 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
11044
11045 if( hs == NULL )
11046 return;
11047
Hanno Becker283f5ef2018-08-24 09:34:47 +010011048 ssl_free_buffered_record( ssl );
11049
Hanno Becker0271f962018-08-16 13:23:47 +010011050 for( offset = 0; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
Hanno Beckere605b192018-08-21 15:59:07 +010011051 ssl_buffering_free_slot( ssl, offset );
11052}
11053
11054static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
11055 uint8_t slot )
11056{
11057 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
11058 mbedtls_ssl_hs_buffer * const hs_buf = &hs->buffering.hs[slot];
Hanno Beckerb309b922018-08-23 13:18:05 +010011059
11060 if( slot >= MBEDTLS_SSL_MAX_BUFFERED_HS )
11061 return;
11062
Hanno Beckere605b192018-08-21 15:59:07 +010011063 if( hs_buf->is_valid == 1 )
Hanno Becker0271f962018-08-16 13:23:47 +010011064 {
Hanno Beckere605b192018-08-21 15:59:07 +010011065 hs->buffering.total_bytes_buffered -= hs_buf->data_len;
Hanno Becker805f2e12018-10-12 16:31:41 +010011066 mbedtls_platform_zeroize( hs_buf->data, hs_buf->data_len );
Hanno Beckere605b192018-08-21 15:59:07 +010011067 mbedtls_free( hs_buf->data );
11068 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Hanno Becker0271f962018-08-16 13:23:47 +010011069 }
11070}
11071
11072#endif /* MBEDTLS_SSL_PROTO_DTLS */
11073
Gilles Peskine9b562d52018-04-25 20:32:43 +020011074void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +000011075{
Gilles Peskine9b562d52018-04-25 20:32:43 +020011076 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
11077
Paul Bakkeraccaffe2014-06-26 13:37:14 +020011078 if( handshake == NULL )
11079 return;
11080
Gilles Peskinedf13d5c2018-04-25 20:39:48 +020011081#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
11082 if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
11083 {
Gilles Peskine8f97af72018-04-26 11:46:10 +020011084 ssl->conf->f_async_cancel( ssl );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +020011085 handshake->async_in_progress = 0;
11086 }
11087#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
11088
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020011089#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
11090 defined(MBEDTLS_SSL_PROTO_TLS1_1)
11091 mbedtls_md5_free( &handshake->fin_md5 );
11092 mbedtls_sha1_free( &handshake->fin_sha1 );
11093#endif
11094#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
11095#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -050011096#if defined(MBEDTLS_USE_PSA_CRYPTO)
11097 psa_hash_abort( &handshake->fin_sha256_psa );
11098#else
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020011099 mbedtls_sha256_free( &handshake->fin_sha256 );
11100#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -050011101#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020011102#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -050011103#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -050011104 psa_hash_abort( &handshake->fin_sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -050011105#else
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020011106 mbedtls_sha512_free( &handshake->fin_sha512 );
11107#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -050011108#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020011109#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
11110
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011111#if defined(MBEDTLS_DHM_C)
11112 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +000011113#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011114#if defined(MBEDTLS_ECDH_C)
11115 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +020011116#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +020011117#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +020011118 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +020011119#if defined(MBEDTLS_SSL_CLI_C)
11120 mbedtls_free( handshake->ecjpake_cache );
11121 handshake->ecjpake_cache = NULL;
11122 handshake->ecjpake_cache_len = 0;
11123#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +020011124#endif
Paul Bakker61d113b2013-07-04 11:51:43 +020011125
Janos Follath4ae5c292016-02-10 11:27:43 +000011126#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
11127 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +020011128 /* explicit void pointer cast for buggy MS compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011129 mbedtls_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +020011130#endif
11131
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +010011132#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
11133 if( handshake->psk != NULL )
11134 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050011135 mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +010011136 mbedtls_free( handshake->psk );
11137 }
11138#endif
11139
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011140#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
11141 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020011142 /*
11143 * Free only the linked list wrapper, not the keys themselves
11144 * since the belong to the SNI callback
11145 */
11146 if( handshake->sni_key_cert != NULL )
11147 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011148 mbedtls_ssl_key_cert *cur = handshake->sni_key_cert, *next;
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020011149
11150 while( cur != NULL )
11151 {
11152 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011153 mbedtls_free( cur );
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020011154 cur = next;
11155 }
11156 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011157#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020011158
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +020011159#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +020011160 mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx );
Hanno Becker3dad3112019-02-05 17:19:52 +000011161 if( handshake->ecrs_peer_cert != NULL )
11162 {
11163 mbedtls_x509_crt_free( handshake->ecrs_peer_cert );
11164 mbedtls_free( handshake->ecrs_peer_cert );
11165 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +020011166#endif
11167
Hanno Becker75173122019-02-06 16:18:31 +000011168#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
11169 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
11170 mbedtls_pk_free( &handshake->peer_pubkey );
11171#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
11172
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011173#if defined(MBEDTLS_SSL_PROTO_DTLS)
11174 mbedtls_free( handshake->verify_cookie );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +020011175 ssl_flight_free( handshake->flight );
Hanno Becker0271f962018-08-16 13:23:47 +010011176 ssl_buffering_free( ssl );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +020011177#endif
11178
Hanno Becker4a63ed42019-01-08 11:39:35 +000011179#if defined(MBEDTLS_ECDH_C) && \
11180 defined(MBEDTLS_USE_PSA_CRYPTO)
11181 psa_destroy_key( handshake->ecdh_psa_privkey );
11182#endif /* MBEDTLS_ECDH_C && MBEDTLS_USE_PSA_CRYPTO */
11183
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050011184 mbedtls_platform_zeroize( handshake,
11185 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +000011186}
11187
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011188void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +000011189{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020011190 if( session == NULL )
11191 return;
11192
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011193#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker1294a0b2019-02-05 12:38:15 +000011194 ssl_clear_peer_cert( session );
Paul Bakkered27a042013-04-18 22:46:23 +020011195#endif
Paul Bakker0a597072012-09-25 21:55:46 +000011196
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +020011197#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011198 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +020011199#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +020011200
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050011201 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +000011202}
11203
Paul Bakker5121ce52009-01-03 21:22:43 +000011204/*
11205 * Free an SSL context
11206 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011207void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000011208{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020011209 if( ssl == NULL )
11210 return;
11211
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011212 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000011213
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +010011214 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000011215 {
Angus Grattond8213d02016-05-25 20:56:48 +100011216 mbedtls_platform_zeroize( ssl->out_buf, MBEDTLS_SSL_OUT_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011217 mbedtls_free( ssl->out_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +000011218 }
11219
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +010011220 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000011221 {
Angus Grattond8213d02016-05-25 20:56:48 +100011222 mbedtls_platform_zeroize( ssl->in_buf, MBEDTLS_SSL_IN_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011223 mbedtls_free( ssl->in_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +000011224 }
11225
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011226#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker16770332013-10-11 09:59:44 +020011227 if( ssl->compress_buf != NULL )
11228 {
Angus Grattond8213d02016-05-25 20:56:48 +100011229 mbedtls_platform_zeroize( ssl->compress_buf, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011230 mbedtls_free( ssl->compress_buf );
Paul Bakker16770332013-10-11 09:59:44 +020011231 }
11232#endif
11233
Paul Bakker48916f92012-09-16 19:57:18 +000011234 if( ssl->transform )
11235 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011236 mbedtls_ssl_transform_free( ssl->transform );
11237 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +000011238 }
11239
11240 if( ssl->handshake )
11241 {
Gilles Peskine9b562d52018-04-25 20:32:43 +020011242 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011243 mbedtls_ssl_transform_free( ssl->transform_negotiate );
11244 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +000011245
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011246 mbedtls_free( ssl->handshake );
11247 mbedtls_free( ssl->transform_negotiate );
11248 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +000011249 }
11250
Paul Bakkerc0463502013-02-14 11:19:38 +010011251 if( ssl->session )
11252 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011253 mbedtls_ssl_session_free( ssl->session );
11254 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +010011255 }
11256
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +020011257#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker66d5d072014-06-17 16:39:18 +020011258 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000011259 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050011260 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011261 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +000011262 }
Paul Bakker0be444a2013-08-27 21:55:01 +020011263#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000011264
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011265#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
11266 if( mbedtls_ssl_hw_record_finish != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +000011267 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011268 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_finish()" ) );
11269 mbedtls_ssl_hw_record_finish( ssl );
Paul Bakker05ef8352012-05-08 09:17:57 +000011270 }
11271#endif
11272
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +020011273#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011274 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +020011275#endif
11276
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011277 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +000011278
Paul Bakker86f04f42013-02-14 11:20:09 +010011279 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050011280 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000011281}
11282
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011283/*
11284 * Initialze mbedtls_ssl_config
11285 */
11286void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
11287{
11288 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
11289}
11290
Simon Butcherc97b6972015-12-27 23:48:17 +000011291#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010011292static int ssl_preset_default_hashes[] = {
11293#if defined(MBEDTLS_SHA512_C)
11294 MBEDTLS_MD_SHA512,
11295 MBEDTLS_MD_SHA384,
11296#endif
11297#if defined(MBEDTLS_SHA256_C)
11298 MBEDTLS_MD_SHA256,
11299 MBEDTLS_MD_SHA224,
11300#endif
Gilles Peskine5d2511c2017-05-12 13:16:40 +020011301#if defined(MBEDTLS_SHA1_C) && defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_KEY_EXCHANGE)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010011302 MBEDTLS_MD_SHA1,
11303#endif
11304 MBEDTLS_MD_NONE
11305};
Simon Butcherc97b6972015-12-27 23:48:17 +000011306#endif
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010011307
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011308static int ssl_preset_suiteb_ciphersuites[] = {
11309 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
11310 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
11311 0
11312};
11313
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020011314#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011315static int ssl_preset_suiteb_hashes[] = {
11316 MBEDTLS_MD_SHA256,
11317 MBEDTLS_MD_SHA384,
11318 MBEDTLS_MD_NONE
11319};
11320#endif
11321
11322#if defined(MBEDTLS_ECP_C)
11323static mbedtls_ecp_group_id ssl_preset_suiteb_curves[] = {
Jaeden Amerod4311042019-06-03 08:27:16 +010011324#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011325 MBEDTLS_ECP_DP_SECP256R1,
Jaeden Amerod4311042019-06-03 08:27:16 +010011326#endif
11327#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011328 MBEDTLS_ECP_DP_SECP384R1,
Jaeden Amerod4311042019-06-03 08:27:16 +010011329#endif
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011330 MBEDTLS_ECP_DP_NONE
11331};
11332#endif
11333
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011334/*
Tillmann Karras588ad502015-09-25 04:27:22 +020011335 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011336 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020011337int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011338 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011339{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +020011340#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011341 int ret;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +020011342#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011343
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +020011344 /* Use the functions here so that they are covered in tests,
11345 * but otherwise access member directly for efficiency */
11346 mbedtls_ssl_conf_endpoint( conf, endpoint );
11347 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011348
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011349 /*
11350 * Things that are common to all presets
11351 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020011352#if defined(MBEDTLS_SSL_CLI_C)
11353 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
11354 {
11355 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
11356#if defined(MBEDTLS_SSL_SESSION_TICKETS)
11357 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
11358#endif
11359 }
11360#endif
11361
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +020011362#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011363 conf->arc4_disabled = MBEDTLS_SSL_ARC4_DISABLED;
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +020011364#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011365
11366#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
11367 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
11368#endif
11369
11370#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
11371 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
11372#endif
11373
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +010011374#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
11375 conf->cbc_record_splitting = MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED;
11376#endif
11377
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +020011378#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011379 conf->f_cookie_write = ssl_cookie_write_dummy;
11380 conf->f_cookie_check = ssl_cookie_check_dummy;
11381#endif
11382
11383#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
11384 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
11385#endif
11386
Janos Follath088ce432017-04-10 12:42:31 +010011387#if defined(MBEDTLS_SSL_SRV_C)
11388 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
11389#endif
11390
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011391#if defined(MBEDTLS_SSL_PROTO_DTLS)
11392 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
11393 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
11394#endif
11395
11396#if defined(MBEDTLS_SSL_RENEGOTIATION)
11397 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Andres AG2196c7f2016-12-15 17:01:16 +000011398 memset( conf->renego_period, 0x00, 2 );
11399 memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011400#endif
11401
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011402#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
11403 if( endpoint == MBEDTLS_SSL_IS_SERVER )
11404 {
Hanno Becker00d0a682017-10-04 13:14:29 +010011405 const unsigned char dhm_p[] =
11406 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
11407 const unsigned char dhm_g[] =
11408 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
11409
Hanno Beckera90658f2017-10-04 15:29:08 +010011410 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
11411 dhm_p, sizeof( dhm_p ),
11412 dhm_g, sizeof( dhm_g ) ) ) != 0 )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011413 {
11414 return( ret );
11415 }
11416 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +020011417#endif
11418
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011419 /*
11420 * Preset-specific defaults
11421 */
11422 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011423 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011424 /*
11425 * NSA Suite B
11426 */
11427 case MBEDTLS_SSL_PRESET_SUITEB:
11428 conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
11429 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */
11430 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
11431 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
11432
11433 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
11434 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
11435 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
11436 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
11437 ssl_preset_suiteb_ciphersuites;
11438
11439#if defined(MBEDTLS_X509_CRT_PARSE_C)
11440 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011441#endif
11442
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020011443#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011444 conf->sig_hashes = ssl_preset_suiteb_hashes;
11445#endif
11446
11447#if defined(MBEDTLS_ECP_C)
11448 conf->curve_list = ssl_preset_suiteb_curves;
11449#endif
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +020011450 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011451
11452 /*
11453 * Default
11454 */
11455 default:
Ron Eldor5e9f14d2017-05-28 10:46:38 +030011456 conf->min_major_ver = ( MBEDTLS_SSL_MIN_MAJOR_VERSION >
11457 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION ) ?
11458 MBEDTLS_SSL_MIN_MAJOR_VERSION :
11459 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION;
11460 conf->min_minor_ver = ( MBEDTLS_SSL_MIN_MINOR_VERSION >
11461 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION ) ?
11462 MBEDTLS_SSL_MIN_MINOR_VERSION :
11463 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011464 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
11465 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
11466
11467#if defined(MBEDTLS_SSL_PROTO_DTLS)
11468 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
11469 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_2;
11470#endif
11471
11472 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
11473 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
11474 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
11475 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
11476 mbedtls_ssl_list_ciphersuites();
11477
11478#if defined(MBEDTLS_X509_CRT_PARSE_C)
11479 conf->cert_profile = &mbedtls_x509_crt_profile_default;
11480#endif
11481
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020011482#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010011483 conf->sig_hashes = ssl_preset_default_hashes;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020011484#endif
11485
11486#if defined(MBEDTLS_ECP_C)
11487 conf->curve_list = mbedtls_ecp_grp_id_list();
11488#endif
11489
11490#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
11491 conf->dhm_min_bitlen = 1024;
11492#endif
11493 }
11494
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011495 return( 0 );
11496}
11497
11498/*
11499 * Free mbedtls_ssl_config
11500 */
11501void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
11502{
11503#if defined(MBEDTLS_DHM_C)
11504 mbedtls_mpi_free( &conf->dhm_P );
11505 mbedtls_mpi_free( &conf->dhm_G );
11506#endif
11507
11508#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
11509 if( conf->psk != NULL )
11510 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050011511 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011512 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +000011513 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011514 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +090011515 }
11516
11517 if( conf->psk_identity != NULL )
11518 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050011519 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +090011520 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +000011521 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011522 conf->psk_identity_len = 0;
11523 }
11524#endif
11525
11526#if defined(MBEDTLS_X509_CRT_PARSE_C)
11527 ssl_key_cert_free( conf->key_cert );
11528#endif
11529
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050011530 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020011531}
11532
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020011533#if defined(MBEDTLS_PK_C) && \
11534 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020011535/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011536 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020011537 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011538unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020011539{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011540#if defined(MBEDTLS_RSA_C)
11541 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
11542 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020011543#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011544#if defined(MBEDTLS_ECDSA_C)
11545 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
11546 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020011547#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011548 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020011549}
11550
Hanno Becker7e5437a2017-04-28 17:15:26 +010011551unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
11552{
11553 switch( type ) {
11554 case MBEDTLS_PK_RSA:
11555 return( MBEDTLS_SSL_SIG_RSA );
11556 case MBEDTLS_PK_ECDSA:
11557 case MBEDTLS_PK_ECKEY:
11558 return( MBEDTLS_SSL_SIG_ECDSA );
11559 default:
11560 return( MBEDTLS_SSL_SIG_ANON );
11561 }
11562}
11563
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011564mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011565{
11566 switch( sig )
11567 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011568#if defined(MBEDTLS_RSA_C)
11569 case MBEDTLS_SSL_SIG_RSA:
11570 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011571#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011572#if defined(MBEDTLS_ECDSA_C)
11573 case MBEDTLS_SSL_SIG_ECDSA:
11574 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011575#endif
11576 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011577 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011578 }
11579}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020011580#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011581
Hanno Becker7e5437a2017-04-28 17:15:26 +010011582#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
11583 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
11584
11585/* Find an entry in a signature-hash set matching a given hash algorithm. */
11586mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set,
11587 mbedtls_pk_type_t sig_alg )
11588{
11589 switch( sig_alg )
11590 {
11591 case MBEDTLS_PK_RSA:
11592 return( set->rsa );
11593 case MBEDTLS_PK_ECDSA:
11594 return( set->ecdsa );
11595 default:
11596 return( MBEDTLS_MD_NONE );
11597 }
11598}
11599
11600/* Add a signature-hash-pair to a signature-hash set */
11601void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set,
11602 mbedtls_pk_type_t sig_alg,
11603 mbedtls_md_type_t md_alg )
11604{
11605 switch( sig_alg )
11606 {
11607 case MBEDTLS_PK_RSA:
11608 if( set->rsa == MBEDTLS_MD_NONE )
11609 set->rsa = md_alg;
11610 break;
11611
11612 case MBEDTLS_PK_ECDSA:
11613 if( set->ecdsa == MBEDTLS_MD_NONE )
11614 set->ecdsa = md_alg;
11615 break;
11616
11617 default:
11618 break;
11619 }
11620}
11621
11622/* Allow exactly one hash algorithm for each signature. */
11623void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set,
11624 mbedtls_md_type_t md_alg )
11625{
11626 set->rsa = md_alg;
11627 set->ecdsa = md_alg;
11628}
11629
11630#endif /* MBEDTLS_SSL_PROTO_TLS1_2) &&
11631 MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
11632
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020011633/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020011634 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020011635 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011636mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011637{
11638 switch( hash )
11639 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011640#if defined(MBEDTLS_MD5_C)
11641 case MBEDTLS_SSL_HASH_MD5:
11642 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011643#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011644#if defined(MBEDTLS_SHA1_C)
11645 case MBEDTLS_SSL_HASH_SHA1:
11646 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011647#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011648#if defined(MBEDTLS_SHA256_C)
11649 case MBEDTLS_SSL_HASH_SHA224:
11650 return( MBEDTLS_MD_SHA224 );
11651 case MBEDTLS_SSL_HASH_SHA256:
11652 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011653#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011654#if defined(MBEDTLS_SHA512_C)
11655 case MBEDTLS_SSL_HASH_SHA384:
11656 return( MBEDTLS_MD_SHA384 );
11657 case MBEDTLS_SSL_HASH_SHA512:
11658 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011659#endif
11660 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011661 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020011662 }
11663}
11664
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020011665/*
11666 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
11667 */
11668unsigned char mbedtls_ssl_hash_from_md_alg( int md )
11669{
11670 switch( md )
11671 {
11672#if defined(MBEDTLS_MD5_C)
11673 case MBEDTLS_MD_MD5:
11674 return( MBEDTLS_SSL_HASH_MD5 );
11675#endif
11676#if defined(MBEDTLS_SHA1_C)
11677 case MBEDTLS_MD_SHA1:
11678 return( MBEDTLS_SSL_HASH_SHA1 );
11679#endif
11680#if defined(MBEDTLS_SHA256_C)
11681 case MBEDTLS_MD_SHA224:
11682 return( MBEDTLS_SSL_HASH_SHA224 );
11683 case MBEDTLS_MD_SHA256:
11684 return( MBEDTLS_SSL_HASH_SHA256 );
11685#endif
11686#if defined(MBEDTLS_SHA512_C)
11687 case MBEDTLS_MD_SHA384:
11688 return( MBEDTLS_SSL_HASH_SHA384 );
11689 case MBEDTLS_MD_SHA512:
11690 return( MBEDTLS_SSL_HASH_SHA512 );
11691#endif
11692 default:
11693 return( MBEDTLS_SSL_HASH_NONE );
11694 }
11695}
11696
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020011697#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011698/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020011699 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020011700 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011701 */
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020011702int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011703{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011704 const mbedtls_ecp_group_id *gid;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011705
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020011706 if( ssl->conf->curve_list == NULL )
11707 return( -1 );
11708
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020011709 for( gid = ssl->conf->curve_list; *gid != MBEDTLS_ECP_DP_NONE; gid++ )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011710 if( *gid == grp_id )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020011711 return( 0 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011712
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020011713 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010011714}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020011715#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011716
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020011717#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020011718/*
11719 * Check if a hash proposed by the peer is in our list.
11720 * Return 0 if we're willing to use it, -1 otherwise.
11721 */
11722int mbedtls_ssl_check_sig_hash( const mbedtls_ssl_context *ssl,
11723 mbedtls_md_type_t md )
11724{
11725 const int *cur;
11726
11727 if( ssl->conf->sig_hashes == NULL )
11728 return( -1 );
11729
11730 for( cur = ssl->conf->sig_hashes; *cur != MBEDTLS_MD_NONE; cur++ )
11731 if( *cur == (int) md )
11732 return( 0 );
11733
11734 return( -1 );
11735}
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020011736#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020011737
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011738#if defined(MBEDTLS_X509_CRT_PARSE_C)
11739int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
11740 const mbedtls_ssl_ciphersuite_t *ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011741 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +020011742 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011743{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011744 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011745#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011746 int usage = 0;
11747#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011748#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011749 const char *ext_oid;
11750 size_t ext_len;
11751#endif
11752
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011753#if !defined(MBEDTLS_X509_CHECK_KEY_USAGE) && \
11754 !defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011755 ((void) cert);
11756 ((void) cert_endpoint);
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011757 ((void) flags);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011758#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011759
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011760#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
11761 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011762 {
11763 /* Server part of the key exchange */
11764 switch( ciphersuite->key_exchange )
11765 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011766 case MBEDTLS_KEY_EXCHANGE_RSA:
11767 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010011768 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011769 break;
11770
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011771 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
11772 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
11773 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
11774 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011775 break;
11776
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011777 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
11778 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010011779 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011780 break;
11781
11782 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011783 case MBEDTLS_KEY_EXCHANGE_NONE:
11784 case MBEDTLS_KEY_EXCHANGE_PSK:
11785 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
11786 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +020011787 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011788 usage = 0;
11789 }
11790 }
11791 else
11792 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011793 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
11794 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011795 }
11796
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011797 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011798 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010011799 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011800 ret = -1;
11801 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011802#else
11803 ((void) ciphersuite);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011804#endif /* MBEDTLS_X509_CHECK_KEY_USAGE */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011805
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011806#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
11807 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011808 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011809 ext_oid = MBEDTLS_OID_SERVER_AUTH;
11810 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011811 }
11812 else
11813 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011814 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
11815 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011816 }
11817
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011818 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011819 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010011820 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011821 ret = -1;
11822 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011823#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011824
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011825 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011826}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011827#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +020011828
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011829/*
11830 * Convert version numbers to/from wire format
11831 * and, for DTLS, to/from TLS equivalent.
11832 *
11833 * For TLS this is the identity.
Brian J Murray1903fb32016-11-06 04:45:15 -080011834 * For DTLS, use 1's complement (v -> 255 - v, and then map as follows:
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011835 * 1.0 <-> 3.2 (DTLS 1.0 is based on TLS 1.1)
11836 * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2)
11837 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011838void mbedtls_ssl_write_version( int major, int minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011839 unsigned char ver[2] )
11840{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011841#if defined(MBEDTLS_SSL_PROTO_DTLS)
11842 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011843 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011844 if( minor == MBEDTLS_SSL_MINOR_VERSION_2 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011845 --minor; /* DTLS 1.0 stored as TLS 1.1 internally */
11846
11847 ver[0] = (unsigned char)( 255 - ( major - 2 ) );
11848 ver[1] = (unsigned char)( 255 - ( minor - 1 ) );
11849 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010011850 else
11851#else
11852 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011853#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010011854 {
11855 ver[0] = (unsigned char) major;
11856 ver[1] = (unsigned char) minor;
11857 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011858}
11859
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011860void mbedtls_ssl_read_version( int *major, int *minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011861 const unsigned char ver[2] )
11862{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011863#if defined(MBEDTLS_SSL_PROTO_DTLS)
11864 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011865 {
11866 *major = 255 - ver[0] + 2;
11867 *minor = 255 - ver[1] + 1;
11868
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011869 if( *minor == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011870 ++*minor; /* DTLS 1.0 stored as TLS 1.1 internally */
11871 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010011872 else
11873#else
11874 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011875#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010011876 {
11877 *major = ver[0];
11878 *minor = ver[1];
11879 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011880}
11881
Simon Butcher99000142016-10-13 17:21:01 +010011882int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md )
11883{
11884#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
11885 if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 )
11886 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
11887
11888 switch( md )
11889 {
11890#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
11891#if defined(MBEDTLS_MD5_C)
11892 case MBEDTLS_SSL_HASH_MD5:
Janos Follath182013f2016-10-25 10:50:22 +010011893 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
Simon Butcher99000142016-10-13 17:21:01 +010011894#endif
11895#if defined(MBEDTLS_SHA1_C)
11896 case MBEDTLS_SSL_HASH_SHA1:
11897 ssl->handshake->calc_verify = ssl_calc_verify_tls;
11898 break;
11899#endif
11900#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
11901#if defined(MBEDTLS_SHA512_C)
11902 case MBEDTLS_SSL_HASH_SHA384:
11903 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
11904 break;
11905#endif
11906#if defined(MBEDTLS_SHA256_C)
11907 case MBEDTLS_SSL_HASH_SHA256:
11908 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
11909 break;
11910#endif
11911 default:
11912 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
11913 }
11914
11915 return 0;
11916#else /* !MBEDTLS_SSL_PROTO_TLS1_2 */
11917 (void) ssl;
11918 (void) md;
11919
11920 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
11921#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
11922}
11923
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011924#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
11925 defined(MBEDTLS_SSL_PROTO_TLS1_1)
11926int mbedtls_ssl_get_key_exchange_md_ssl_tls( mbedtls_ssl_context *ssl,
11927 unsigned char *output,
11928 unsigned char *data, size_t data_len )
11929{
11930 int ret = 0;
11931 mbedtls_md5_context mbedtls_md5;
11932 mbedtls_sha1_context mbedtls_sha1;
11933
11934 mbedtls_md5_init( &mbedtls_md5 );
11935 mbedtls_sha1_init( &mbedtls_sha1 );
11936
11937 /*
11938 * digitally-signed struct {
11939 * opaque md5_hash[16];
11940 * opaque sha_hash[20];
11941 * };
11942 *
11943 * md5_hash
11944 * MD5(ClientHello.random + ServerHello.random
11945 * + ServerParams);
11946 * sha_hash
11947 * SHA(ClientHello.random + ServerHello.random
11948 * + ServerParams);
11949 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011950 if( ( ret = mbedtls_md5_starts_ret( &mbedtls_md5 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011951 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011952 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011953 goto exit;
11954 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011955 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011956 ssl->handshake->randbytes, 64 ) ) != 0 )
11957 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011958 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011959 goto exit;
11960 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011961 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5, data, data_len ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011962 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011963 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011964 goto exit;
11965 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011966 if( ( ret = mbedtls_md5_finish_ret( &mbedtls_md5, output ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011967 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011968 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011969 goto exit;
11970 }
11971
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011972 if( ( ret = mbedtls_sha1_starts_ret( &mbedtls_sha1 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011973 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011974 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011975 goto exit;
11976 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011977 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011978 ssl->handshake->randbytes, 64 ) ) != 0 )
11979 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011980 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011981 goto exit;
11982 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011983 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1, data,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011984 data_len ) ) != 0 )
11985 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011986 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011987 goto exit;
11988 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011989 if( ( ret = mbedtls_sha1_finish_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011990 output + 16 ) ) != 0 )
11991 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011992 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011993 goto exit;
11994 }
11995
11996exit:
11997 mbedtls_md5_free( &mbedtls_md5 );
11998 mbedtls_sha1_free( &mbedtls_sha1 );
11999
12000 if( ret != 0 )
12001 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
12002 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
12003
12004 return( ret );
12005
12006}
12007#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
12008 MBEDTLS_SSL_PROTO_TLS1_1 */
12009
12010#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
12011 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050012012
12013#if defined(MBEDTLS_USE_PSA_CRYPTO)
12014int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
12015 unsigned char *hash, size_t *hashlen,
12016 unsigned char *data, size_t data_len,
12017 mbedtls_md_type_t md_alg )
12018{
Andrzej Kurek814feff2019-01-14 04:35:19 -050012019 psa_status_t status;
Jaeden Amero34973232019-02-20 10:32:28 +000012020 psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT;
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050012021 psa_algorithm_t hash_alg = mbedtls_psa_translate_md( md_alg );
12022
Hanno Becker4c8c7aa2019-04-10 09:25:41 +010012023 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform PSA-based computation of digest of ServerKeyExchange" ) );
Andrzej Kurek814feff2019-01-14 04:35:19 -050012024
12025 if( ( status = psa_hash_setup( &hash_operation,
12026 hash_alg ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050012027 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050012028 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_setup", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050012029 goto exit;
12030 }
12031
Andrzej Kurek814feff2019-01-14 04:35:19 -050012032 if( ( status = psa_hash_update( &hash_operation, ssl->handshake->randbytes,
12033 64 ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050012034 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050012035 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050012036 goto exit;
12037 }
12038
Andrzej Kurek814feff2019-01-14 04:35:19 -050012039 if( ( status = psa_hash_update( &hash_operation,
12040 data, data_len ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050012041 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050012042 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050012043 goto exit;
12044 }
12045
Andrzej Kurek814feff2019-01-14 04:35:19 -050012046 if( ( status = psa_hash_finish( &hash_operation, hash, MBEDTLS_MD_MAX_SIZE,
12047 hashlen ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050012048 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050012049 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_finish", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050012050 goto exit;
12051 }
12052
12053exit:
Andrzej Kurek814feff2019-01-14 04:35:19 -050012054 if( status != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050012055 {
12056 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
12057 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Andrzej Kurek814feff2019-01-14 04:35:19 -050012058 switch( status )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050012059 {
12060 case PSA_ERROR_NOT_SUPPORTED:
12061 return( MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE );
Andrzej Kurek814feff2019-01-14 04:35:19 -050012062 case PSA_ERROR_BAD_STATE: /* Intentional fallthrough */
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050012063 case PSA_ERROR_BUFFER_TOO_SMALL:
12064 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
12065 case PSA_ERROR_INSUFFICIENT_MEMORY:
12066 return( MBEDTLS_ERR_MD_ALLOC_FAILED );
12067 default:
12068 return( MBEDTLS_ERR_MD_HW_ACCEL_FAILED );
12069 }
12070 }
12071 return( 0 );
12072}
12073
12074#else
12075
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012076int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
Gilles Peskineca1d7422018-04-24 11:53:22 +020012077 unsigned char *hash, size_t *hashlen,
12078 unsigned char *data, size_t data_len,
12079 mbedtls_md_type_t md_alg )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012080{
12081 int ret = 0;
12082 mbedtls_md_context_t ctx;
12083 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
Gilles Peskineca1d7422018-04-24 11:53:22 +020012084 *hashlen = mbedtls_md_get_size( md_info );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012085
Hanno Becker4c8c7aa2019-04-10 09:25:41 +010012086 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform mbedtls-based computation of digest of ServerKeyExchange" ) );
Andrzej Kurek814feff2019-01-14 04:35:19 -050012087
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012088 mbedtls_md_init( &ctx );
12089
12090 /*
12091 * digitally-signed struct {
12092 * opaque client_random[32];
12093 * opaque server_random[32];
12094 * ServerDHParams params;
12095 * };
12096 */
12097 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
12098 {
12099 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
12100 goto exit;
12101 }
12102 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
12103 {
12104 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
12105 goto exit;
12106 }
12107 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
12108 {
12109 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
12110 goto exit;
12111 }
12112 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
12113 {
12114 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
12115 goto exit;
12116 }
Gilles Peskineca1d7422018-04-24 11:53:22 +020012117 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012118 {
12119 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
12120 goto exit;
12121 }
12122
12123exit:
12124 mbedtls_md_free( &ctx );
12125
12126 if( ret != 0 )
12127 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
12128 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
12129
12130 return( ret );
12131}
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050012132#endif /* MBEDTLS_USE_PSA_CRYPTO */
12133
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010012134#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
12135 MBEDTLS_SSL_PROTO_TLS1_2 */
12136
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012137#endif /* MBEDTLS_SSL_TLS_C */