2009-01-03 22:22:43 +01:00
|
|
|
/*
|
|
|
|
* SSLv3/TLSv1 shared functions
|
|
|
|
*
|
2015-07-27 11:11:48 +02:00
|
|
|
* Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
|
2015-09-04 14:21:07 +02:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
2010-07-18 22:36:00 +02:00
|
|
|
*
|
2015-09-04 14:21:07 +02:00
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); you may
|
|
|
|
* not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
2009-01-04 17:27:10 +01:00
|
|
|
*
|
2015-09-04 14:21:07 +02:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2009-01-03 22:22:43 +01:00
|
|
|
*
|
2015-09-04 14:21:07 +02:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
2009-01-03 22:22:43 +01:00
|
|
|
*
|
2015-09-04 14:21:07 +02:00
|
|
|
* This file is part of mbed TLS (https://tls.mbed.org)
|
2009-01-03 22:22:43 +01:00
|
|
|
*/
|
|
|
|
/*
|
|
|
|
* The SSL 3.0 specification was drafted by Netscape in 1996,
|
|
|
|
* and became an IETF standard in 1999.
|
|
|
|
*
|
|
|
|
* http://wp.netscape.com/eng/ssl3/
|
|
|
|
* http://www.ietf.org/rfc/rfc2246.txt
|
|
|
|
* http://www.ietf.org/rfc/rfc4346.txt
|
|
|
|
*/
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#if !defined(MBEDTLS_CONFIG_FILE)
|
2015-03-09 18:05:11 +01:00
|
|
|
#include "mbedtls/config.h"
|
2014-04-29 12:39:06 +02:00
|
|
|
#else
|
2015-04-08 12:49:31 +02:00
|
|
|
#include MBEDTLS_CONFIG_FILE
|
2014-04-29 12:39:06 +02:00
|
|
|
#endif
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_SSL_TLS_C)
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2016-04-26 08:43:27 +02:00
|
|
|
#if defined(MBEDTLS_PLATFORM_C)
|
|
|
|
#include "mbedtls/platform.h"
|
|
|
|
#else
|
|
|
|
#include <stdlib.h>
|
|
|
|
#define mbedtls_calloc calloc
|
|
|
|
#define mbedtls_free free
|
|
|
|
#endif
|
|
|
|
|
2015-03-09 18:05:11 +01:00
|
|
|
#include "mbedtls/ssl.h"
|
2015-05-26 11:57:05 +02:00
|
|
|
#include "mbedtls/ssl_internal.h"
|
2019-12-18 16:07:04 +01:00
|
|
|
#include "mbedtls/debug.h"
|
|
|
|
#include "mbedtls/error.h"
|
2018-04-17 16:51:09 +02:00
|
|
|
#include "mbedtls/platform_util.h"
|
2019-05-16 13:39:07 +02:00
|
|
|
#include "mbedtls/version.h"
|
2013-08-27 21:55:01 +02:00
|
|
|
|
2015-02-06 14:43:58 +01:00
|
|
|
#include <string.h>
|
|
|
|
|
2019-01-10 11:27:10 +01:00
|
|
|
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
|
|
|
#include "mbedtls/psa_util.h"
|
|
|
|
#include "psa/crypto.h"
|
|
|
|
#endif
|
|
|
|
|
2016-10-07 15:47:14 +02:00
|
|
|
#if defined(MBEDTLS_X509_CRT_PARSE_C)
|
2015-03-09 18:05:11 +01:00
|
|
|
#include "mbedtls/oid.h"
|
2014-04-11 11:06:22 +02:00
|
|
|
#endif
|
|
|
|
|
2015-05-13 16:22:05 +02:00
|
|
|
#if defined(MBEDTLS_SSL_PROTO_DTLS)
|
2018-08-06 12:19:13 +02:00
|
|
|
|
2019-05-15 15:03:01 +02:00
|
|
|
#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
|
2019-04-09 16:22:03 +02:00
|
|
|
/* Top-level Connection ID API */
|
|
|
|
|
2019-05-14 12:30:10 +02:00
|
|
|
int mbedtls_ssl_conf_cid( mbedtls_ssl_config *conf,
|
|
|
|
size_t len,
|
|
|
|
int ignore_other_cid )
|
Add CID configuration API
Context:
The CID draft does not require that the length of CIDs used for incoming
records must not change in the course of a connection. Since the record
header does not contain a length field for the CID, this means that if
CIDs of varying lengths are used, the CID length must be inferred from
other aspects of the record header (such as the epoch) and/or by means
outside of the protocol, e.g. by coding its length in the CID itself.
Inferring the CID length from the record's epoch is theoretically possible
in DTLS 1.2, but it requires the information about the epoch to be present
even if the epoch is no longer used: That's because one should silently drop
records from old epochs, but not the entire datagrams to which they belong
(there might be entire flights in a single datagram, including a change of
epoch); however, in order to do so, one needs to parse the record's content
length, the position of which is only known once the CID length for the epoch
is known. In conclusion, it puts a significant burden on the implementation
to infer the CID length from the record epoch, which moreover mangles record
processing with the high-level logic of the protocol (determining which epochs
are in use in which flights, when they are changed, etc. -- this would normally
determine when we drop epochs).
Moreover, with DTLS 1.3, CIDs are no longer uniquely associated to epochs,
but every epoch may use a set of CIDs of varying lengths -- in that case,
it's even theoretically impossible to do record header parsing based on
the epoch configuration only.
We must therefore seek a way for standalone record header parsing, which
means that we must either (a) fix the CID lengths for incoming records,
or (b) allow the application-code to configure a callback to implement
an application-specific CID parsing which would somehow infer the length
of the CID from the CID itself.
Supporting multiple lengths for incoming CIDs significantly increases
complexity while, on the other hand, the restriction to a fixed CID length
for incoming CIDs (which the application controls - in contrast to the
lengths of the CIDs used when writing messages to the peer) doesn't
appear to severely limit the usefulness of the CID extension.
Therefore, the initial implementation of the CID feature will require
a fixed length for incoming CIDs, which is what this commit enforces,
in the following way:
In order to avoid a change of API in case support for variable lengths
CIDs shall be added at some point, we keep mbedtls_ssl_set_cid(), which
includes a CID length parameter, but add a new API mbedtls_ssl_conf_cid_len()
which applies to an SSL configuration, and which fixes the CID length that
any call to mbetls_ssl_set_cid() which applies to an SSL context that is bound
to the given SSL configuration must use.
While this creates a slight redundancy of parameters, it allows to
potentially add an API like mbedtls_ssl_conf_cid_len_cb() later which
could allow users to register a callback which dynamically infers the
length of a CID at record header parsing time, without changing the
rest of the API.
2019-05-03 14:06:44 +02:00
|
|
|
{
|
|
|
|
if( len > MBEDTLS_SSL_CID_IN_LEN_MAX )
|
|
|
|
return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
|
|
|
|
|
2019-05-14 12:45:26 +02:00
|
|
|
if( ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_FAIL &&
|
|
|
|
ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_IGNORE )
|
|
|
|
{
|
|
|
|
return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
|
|
|
|
}
|
|
|
|
|
|
|
|
conf->ignore_unexpected_cid = ignore_other_cid;
|
Add CID configuration API
Context:
The CID draft does not require that the length of CIDs used for incoming
records must not change in the course of a connection. Since the record
header does not contain a length field for the CID, this means that if
CIDs of varying lengths are used, the CID length must be inferred from
other aspects of the record header (such as the epoch) and/or by means
outside of the protocol, e.g. by coding its length in the CID itself.
Inferring the CID length from the record's epoch is theoretically possible
in DTLS 1.2, but it requires the information about the epoch to be present
even if the epoch is no longer used: That's because one should silently drop
records from old epochs, but not the entire datagrams to which they belong
(there might be entire flights in a single datagram, including a change of
epoch); however, in order to do so, one needs to parse the record's content
length, the position of which is only known once the CID length for the epoch
is known. In conclusion, it puts a significant burden on the implementation
to infer the CID length from the record epoch, which moreover mangles record
processing with the high-level logic of the protocol (determining which epochs
are in use in which flights, when they are changed, etc. -- this would normally
determine when we drop epochs).
Moreover, with DTLS 1.3, CIDs are no longer uniquely associated to epochs,
but every epoch may use a set of CIDs of varying lengths -- in that case,
it's even theoretically impossible to do record header parsing based on
the epoch configuration only.
We must therefore seek a way for standalone record header parsing, which
means that we must either (a) fix the CID lengths for incoming records,
or (b) allow the application-code to configure a callback to implement
an application-specific CID parsing which would somehow infer the length
of the CID from the CID itself.
Supporting multiple lengths for incoming CIDs significantly increases
complexity while, on the other hand, the restriction to a fixed CID length
for incoming CIDs (which the application controls - in contrast to the
lengths of the CIDs used when writing messages to the peer) doesn't
appear to severely limit the usefulness of the CID extension.
Therefore, the initial implementation of the CID feature will require
a fixed length for incoming CIDs, which is what this commit enforces,
in the following way:
In order to avoid a change of API in case support for variable lengths
CIDs shall be added at some point, we keep mbedtls_ssl_set_cid(), which
includes a CID length parameter, but add a new API mbedtls_ssl_conf_cid_len()
which applies to an SSL configuration, and which fixes the CID length that
any call to mbetls_ssl_set_cid() which applies to an SSL context that is bound
to the given SSL configuration must use.
While this creates a slight redundancy of parameters, it allows to
potentially add an API like mbedtls_ssl_conf_cid_len_cb() later which
could allow users to register a callback which dynamically infers the
length of a CID at record header parsing time, without changing the
rest of the API.
2019-05-03 14:06:44 +02:00
|
|
|
conf->cid_len = len;
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
2019-04-09 16:22:03 +02:00
|
|
|
int mbedtls_ssl_set_cid( mbedtls_ssl_context *ssl,
|
|
|
|
int enable,
|
|
|
|
unsigned char const *own_cid,
|
|
|
|
size_t own_cid_len )
|
|
|
|
{
|
2019-05-03 15:38:32 +02:00
|
|
|
if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
|
|
|
|
return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
|
|
|
|
|
2019-04-25 17:01:49 +02:00
|
|
|
ssl->negotiate_cid = enable;
|
|
|
|
if( enable == MBEDTLS_SSL_CID_DISABLED )
|
|
|
|
{
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 3, ( "Disable use of CID extension." ) );
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 3, ( "Enable use of CID extension." ) );
|
Add CID configuration API
Context:
The CID draft does not require that the length of CIDs used for incoming
records must not change in the course of a connection. Since the record
header does not contain a length field for the CID, this means that if
CIDs of varying lengths are used, the CID length must be inferred from
other aspects of the record header (such as the epoch) and/or by means
outside of the protocol, e.g. by coding its length in the CID itself.
Inferring the CID length from the record's epoch is theoretically possible
in DTLS 1.2, but it requires the information about the epoch to be present
even if the epoch is no longer used: That's because one should silently drop
records from old epochs, but not the entire datagrams to which they belong
(there might be entire flights in a single datagram, including a change of
epoch); however, in order to do so, one needs to parse the record's content
length, the position of which is only known once the CID length for the epoch
is known. In conclusion, it puts a significant burden on the implementation
to infer the CID length from the record epoch, which moreover mangles record
processing with the high-level logic of the protocol (determining which epochs
are in use in which flights, when they are changed, etc. -- this would normally
determine when we drop epochs).
Moreover, with DTLS 1.3, CIDs are no longer uniquely associated to epochs,
but every epoch may use a set of CIDs of varying lengths -- in that case,
it's even theoretically impossible to do record header parsing based on
the epoch configuration only.
We must therefore seek a way for standalone record header parsing, which
means that we must either (a) fix the CID lengths for incoming records,
or (b) allow the application-code to configure a callback to implement
an application-specific CID parsing which would somehow infer the length
of the CID from the CID itself.
Supporting multiple lengths for incoming CIDs significantly increases
complexity while, on the other hand, the restriction to a fixed CID length
for incoming CIDs (which the application controls - in contrast to the
lengths of the CIDs used when writing messages to the peer) doesn't
appear to severely limit the usefulness of the CID extension.
Therefore, the initial implementation of the CID feature will require
a fixed length for incoming CIDs, which is what this commit enforces,
in the following way:
In order to avoid a change of API in case support for variable lengths
CIDs shall be added at some point, we keep mbedtls_ssl_set_cid(), which
includes a CID length parameter, but add a new API mbedtls_ssl_conf_cid_len()
which applies to an SSL configuration, and which fixes the CID length that
any call to mbetls_ssl_set_cid() which applies to an SSL context that is bound
to the given SSL configuration must use.
While this creates a slight redundancy of parameters, it allows to
potentially add an API like mbedtls_ssl_conf_cid_len_cb() later which
could allow users to register a callback which dynamically infers the
length of a CID at record header parsing time, without changing the
rest of the API.
2019-05-03 14:06:44 +02:00
|
|
|
MBEDTLS_SSL_DEBUG_BUF( 3, "Own CID", own_cid, own_cid_len );
|
2019-04-25 17:01:49 +02:00
|
|
|
|
Add CID configuration API
Context:
The CID draft does not require that the length of CIDs used for incoming
records must not change in the course of a connection. Since the record
header does not contain a length field for the CID, this means that if
CIDs of varying lengths are used, the CID length must be inferred from
other aspects of the record header (such as the epoch) and/or by means
outside of the protocol, e.g. by coding its length in the CID itself.
Inferring the CID length from the record's epoch is theoretically possible
in DTLS 1.2, but it requires the information about the epoch to be present
even if the epoch is no longer used: That's because one should silently drop
records from old epochs, but not the entire datagrams to which they belong
(there might be entire flights in a single datagram, including a change of
epoch); however, in order to do so, one needs to parse the record's content
length, the position of which is only known once the CID length for the epoch
is known. In conclusion, it puts a significant burden on the implementation
to infer the CID length from the record epoch, which moreover mangles record
processing with the high-level logic of the protocol (determining which epochs
are in use in which flights, when they are changed, etc. -- this would normally
determine when we drop epochs).
Moreover, with DTLS 1.3, CIDs are no longer uniquely associated to epochs,
but every epoch may use a set of CIDs of varying lengths -- in that case,
it's even theoretically impossible to do record header parsing based on
the epoch configuration only.
We must therefore seek a way for standalone record header parsing, which
means that we must either (a) fix the CID lengths for incoming records,
or (b) allow the application-code to configure a callback to implement
an application-specific CID parsing which would somehow infer the length
of the CID from the CID itself.
Supporting multiple lengths for incoming CIDs significantly increases
complexity while, on the other hand, the restriction to a fixed CID length
for incoming CIDs (which the application controls - in contrast to the
lengths of the CIDs used when writing messages to the peer) doesn't
appear to severely limit the usefulness of the CID extension.
Therefore, the initial implementation of the CID feature will require
a fixed length for incoming CIDs, which is what this commit enforces,
in the following way:
In order to avoid a change of API in case support for variable lengths
CIDs shall be added at some point, we keep mbedtls_ssl_set_cid(), which
includes a CID length parameter, but add a new API mbedtls_ssl_conf_cid_len()
which applies to an SSL configuration, and which fixes the CID length that
any call to mbetls_ssl_set_cid() which applies to an SSL context that is bound
to the given SSL configuration must use.
While this creates a slight redundancy of parameters, it allows to
potentially add an API like mbedtls_ssl_conf_cid_len_cb() later which
could allow users to register a callback which dynamically infers the
length of a CID at record header parsing time, without changing the
rest of the API.
2019-05-03 14:06:44 +02:00
|
|
|
if( own_cid_len != ssl->conf->cid_len )
|
2019-04-25 17:01:49 +02:00
|
|
|
{
|
Add CID configuration API
Context:
The CID draft does not require that the length of CIDs used for incoming
records must not change in the course of a connection. Since the record
header does not contain a length field for the CID, this means that if
CIDs of varying lengths are used, the CID length must be inferred from
other aspects of the record header (such as the epoch) and/or by means
outside of the protocol, e.g. by coding its length in the CID itself.
Inferring the CID length from the record's epoch is theoretically possible
in DTLS 1.2, but it requires the information about the epoch to be present
even if the epoch is no longer used: That's because one should silently drop
records from old epochs, but not the entire datagrams to which they belong
(there might be entire flights in a single datagram, including a change of
epoch); however, in order to do so, one needs to parse the record's content
length, the position of which is only known once the CID length for the epoch
is known. In conclusion, it puts a significant burden on the implementation
to infer the CID length from the record epoch, which moreover mangles record
processing with the high-level logic of the protocol (determining which epochs
are in use in which flights, when they are changed, etc. -- this would normally
determine when we drop epochs).
Moreover, with DTLS 1.3, CIDs are no longer uniquely associated to epochs,
but every epoch may use a set of CIDs of varying lengths -- in that case,
it's even theoretically impossible to do record header parsing based on
the epoch configuration only.
We must therefore seek a way for standalone record header parsing, which
means that we must either (a) fix the CID lengths for incoming records,
or (b) allow the application-code to configure a callback to implement
an application-specific CID parsing which would somehow infer the length
of the CID from the CID itself.
Supporting multiple lengths for incoming CIDs significantly increases
complexity while, on the other hand, the restriction to a fixed CID length
for incoming CIDs (which the application controls - in contrast to the
lengths of the CIDs used when writing messages to the peer) doesn't
appear to severely limit the usefulness of the CID extension.
Therefore, the initial implementation of the CID feature will require
a fixed length for incoming CIDs, which is what this commit enforces,
in the following way:
In order to avoid a change of API in case support for variable lengths
CIDs shall be added at some point, we keep mbedtls_ssl_set_cid(), which
includes a CID length parameter, but add a new API mbedtls_ssl_conf_cid_len()
which applies to an SSL configuration, and which fixes the CID length that
any call to mbetls_ssl_set_cid() which applies to an SSL context that is bound
to the given SSL configuration must use.
While this creates a slight redundancy of parameters, it allows to
potentially add an API like mbedtls_ssl_conf_cid_len_cb() later which
could allow users to register a callback which dynamically infers the
length of a CID at record header parsing time, without changing the
rest of the API.
2019-05-03 14:06:44 +02:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 3, ( "CID length %u does not match CID length %u in config",
|
|
|
|
(unsigned) own_cid_len,
|
|
|
|
(unsigned) ssl->conf->cid_len ) );
|
2019-04-25 17:01:49 +02:00
|
|
|
return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy( ssl->own_cid, own_cid, own_cid_len );
|
2019-04-30 15:07:31 +02:00
|
|
|
/* Truncation is not an issue here because
|
|
|
|
* MBEDTLS_SSL_CID_IN_LEN_MAX at most 255. */
|
|
|
|
ssl->own_cid_len = (uint8_t) own_cid_len;
|
2019-04-25 17:01:49 +02:00
|
|
|
|
2019-04-09 16:22:03 +02:00
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
|
|
|
int mbedtls_ssl_get_peer_cid( mbedtls_ssl_context *ssl,
|
|
|
|
int *enabled,
|
|
|
|
unsigned char peer_cid[ MBEDTLS_SSL_CID_OUT_LEN_MAX ],
|
|
|
|
size_t *peer_cid_len )
|
|
|
|
{
|
|
|
|
*enabled = MBEDTLS_SSL_CID_DISABLED;
|
2019-04-26 18:08:02 +02:00
|
|
|
|
2019-05-03 15:38:32 +02:00
|
|
|
if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
|
|
|
|
ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
|
|
|
|
{
|
2019-04-26 18:08:02 +02:00
|
|
|
return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
|
2019-05-03 15:38:32 +02:00
|
|
|
}
|
2019-04-26 18:08:02 +02:00
|
|
|
|
2019-05-03 13:54:52 +02:00
|
|
|
/* We report MBEDTLS_SSL_CID_DISABLED in case the CID extensions
|
|
|
|
* were used, but client and server requested the empty CID.
|
|
|
|
* This is indistinguishable from not using the CID extension
|
|
|
|
* in the first place. */
|
2019-04-26 18:08:02 +02:00
|
|
|
if( ssl->transform_in->in_cid_len == 0 &&
|
|
|
|
ssl->transform_in->out_cid_len == 0 )
|
|
|
|
{
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
2019-05-22 17:50:35 +02:00
|
|
|
if( peer_cid_len != NULL )
|
|
|
|
{
|
|
|
|
*peer_cid_len = ssl->transform_in->out_cid_len;
|
|
|
|
if( peer_cid != NULL )
|
|
|
|
{
|
|
|
|
memcpy( peer_cid, ssl->transform_in->out_cid,
|
|
|
|
ssl->transform_in->out_cid_len );
|
|
|
|
}
|
|
|
|
}
|
2019-04-26 18:08:02 +02:00
|
|
|
|
|
|
|
*enabled = MBEDTLS_SSL_CID_ENABLED;
|
|
|
|
|
2019-04-09 16:22:03 +02:00
|
|
|
return( 0 );
|
|
|
|
}
|
2019-05-15 15:03:01 +02:00
|
|
|
#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
|
2019-04-09 16:22:03 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#endif /* MBEDTLS_SSL_PROTO_DTLS */
|
2014-09-30 22:21:31 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
|
2013-07-18 12:32:27 +02:00
|
|
|
/*
|
|
|
|
* Convert max_fragment_length codes to length.
|
|
|
|
* RFC 6066 says:
|
|
|
|
* enum{
|
|
|
|
* 2^9(1), 2^10(2), 2^11(3), 2^12(4), (255)
|
|
|
|
* } MaxFragmentLength;
|
|
|
|
* and we add 0 -> extension unused
|
|
|
|
*/
|
2016-05-25 12:56:48 +02:00
|
|
|
static unsigned int ssl_mfl_code_to_length( int mfl )
|
|
|
|
{
|
|
|
|
switch( mfl )
|
|
|
|
{
|
|
|
|
case MBEDTLS_SSL_MAX_FRAG_LEN_NONE:
|
|
|
|
return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
|
|
|
|
case MBEDTLS_SSL_MAX_FRAG_LEN_512:
|
|
|
|
return 512;
|
|
|
|
case MBEDTLS_SSL_MAX_FRAG_LEN_1024:
|
|
|
|
return 1024;
|
|
|
|
case MBEDTLS_SSL_MAX_FRAG_LEN_2048:
|
|
|
|
return 2048;
|
|
|
|
case MBEDTLS_SSL_MAX_FRAG_LEN_4096:
|
|
|
|
return 4096;
|
|
|
|
default:
|
|
|
|
return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
|
|
|
|
}
|
|
|
|
}
|
2015-04-08 12:49:31 +02:00
|
|
|
#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
|
2013-07-18 12:32:27 +02:00
|
|
|
|
2019-02-06 15:30:46 +01:00
|
|
|
int mbedtls_ssl_session_copy( mbedtls_ssl_session *dst,
|
|
|
|
const mbedtls_ssl_session *src )
|
2013-08-02 15:34:52 +02:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_ssl_session_free( dst );
|
|
|
|
memcpy( dst, src, sizeof( mbedtls_ssl_session ) );
|
2013-08-02 15:34:52 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_X509_CRT_PARSE_C)
|
2019-02-07 13:27:42 +01:00
|
|
|
|
|
|
|
#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
|
2013-08-02 15:34:52 +02:00
|
|
|
if( src->peer_cert != NULL )
|
|
|
|
{
|
2019-12-16 12:46:15 +01:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2013-09-15 17:06:49 +02:00
|
|
|
|
2015-05-26 16:04:06 +02:00
|
|
|
dst->peer_cert = mbedtls_calloc( 1, sizeof(mbedtls_x509_crt) );
|
2013-10-11 18:58:55 +02:00
|
|
|
if( dst->peer_cert == NULL )
|
2015-05-28 09:33:39 +02:00
|
|
|
return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
|
2013-08-02 15:34:52 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_x509_crt_init( dst->peer_cert );
|
2013-08-02 15:34:52 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
if( ( ret = mbedtls_x509_crt_parse_der( dst->peer_cert, src->peer_cert->raw.p,
|
2014-06-13 20:33:27 +02:00
|
|
|
src->peer_cert->raw.len ) ) != 0 )
|
2013-08-02 15:34:52 +02:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_free( dst->peer_cert );
|
2013-08-02 15:34:52 +02:00
|
|
|
dst->peer_cert = NULL;
|
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
}
|
2019-02-07 13:27:42 +01:00
|
|
|
#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
|
2019-02-05 18:00:50 +01:00
|
|
|
if( src->peer_cert_digest != NULL )
|
|
|
|
{
|
|
|
|
dst->peer_cert_digest =
|
2019-02-25 11:06:59 +01:00
|
|
|
mbedtls_calloc( 1, src->peer_cert_digest_len );
|
2019-02-05 18:00:50 +01:00
|
|
|
if( dst->peer_cert_digest == NULL )
|
|
|
|
return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
|
|
|
|
|
|
|
|
memcpy( dst->peer_cert_digest, src->peer_cert_digest,
|
|
|
|
src->peer_cert_digest_len );
|
|
|
|
dst->peer_cert_digest_type = src->peer_cert_digest_type;
|
2019-02-25 11:06:59 +01:00
|
|
|
dst->peer_cert_digest_len = src->peer_cert_digest_len;
|
2019-02-05 18:00:50 +01:00
|
|
|
}
|
|
|
|
#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#endif /* MBEDTLS_X509_CRT_PARSE_C */
|
2013-08-02 15:34:52 +02:00
|
|
|
|
2015-05-20 10:45:29 +02:00
|
|
|
#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
|
2013-08-02 15:34:52 +02:00
|
|
|
if( src->ticket != NULL )
|
|
|
|
{
|
2015-05-26 16:04:06 +02:00
|
|
|
dst->ticket = mbedtls_calloc( 1, src->ticket_len );
|
2013-10-11 18:58:55 +02:00
|
|
|
if( dst->ticket == NULL )
|
2015-05-28 09:33:39 +02:00
|
|
|
return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
|
2013-08-02 15:34:52 +02:00
|
|
|
|
|
|
|
memcpy( dst->ticket, src->ticket, src->ticket_len );
|
|
|
|
}
|
2015-05-20 10:45:29 +02:00
|
|
|
#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
|
2013-08-02 15:34:52 +02:00
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
2020-03-03 16:39:58 +01:00
|
|
|
#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
|
|
|
|
static int resize_buffer( unsigned char **buffer, size_t len_new, size_t *len_old )
|
|
|
|
{
|
|
|
|
unsigned char* resized_buffer = mbedtls_calloc( 1, len_new );
|
|
|
|
if( resized_buffer == NULL )
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
/* We want to copy len_new bytes when downsizing the buffer, and
|
|
|
|
* len_old bytes when upsizing, so we choose the smaller of two sizes,
|
|
|
|
* to fit one buffer into another. Size checks, ensuring that no data is
|
|
|
|
* lost, are done outside of this function. */
|
|
|
|
memcpy( resized_buffer, *buffer,
|
|
|
|
( len_new < *len_old ) ? len_new : *len_old );
|
|
|
|
mbedtls_platform_zeroize( *buffer, *len_old );
|
|
|
|
mbedtls_free( *buffer );
|
|
|
|
|
|
|
|
*buffer = resized_buffer;
|
|
|
|
*len_old = len_new;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif /* MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH */
|
|
|
|
|
2009-01-03 22:22:43 +01:00
|
|
|
/*
|
|
|
|
* Key material generation
|
|
|
|
*/
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_SSL_PROTO_SSL3)
|
2013-06-25 16:25:17 +02:00
|
|
|
static int ssl3_prf( const unsigned char *secret, size_t slen,
|
|
|
|
const char *label,
|
|
|
|
const unsigned char *random, size_t rlen,
|
2012-09-13 16:23:06 +02:00
|
|
|
unsigned char *dstbuf, size_t dlen )
|
|
|
|
{
|
2017-07-20 17:29:16 +02:00
|
|
|
int ret = 0;
|
2012-09-13 16:23:06 +02:00
|
|
|
size_t i;
|
2015-07-06 16:11:18 +02:00
|
|
|
mbedtls_md5_context md5;
|
|
|
|
mbedtls_sha1_context sha1;
|
2012-09-13 16:23:06 +02:00
|
|
|
unsigned char padding[16];
|
|
|
|
unsigned char sha1sum[20];
|
|
|
|
((void)label);
|
|
|
|
|
2015-07-06 16:11:18 +02:00
|
|
|
mbedtls_md5_init( &md5 );
|
|
|
|
mbedtls_sha1_init( &sha1 );
|
2014-06-26 12:09:34 +02:00
|
|
|
|
2012-09-13 16:23:06 +02:00
|
|
|
/*
|
|
|
|
* SSLv3:
|
|
|
|
* block =
|
|
|
|
* MD5( secret + SHA1( 'A' + secret + random ) ) +
|
|
|
|
* MD5( secret + SHA1( 'BB' + secret + random ) ) +
|
|
|
|
* MD5( secret + SHA1( 'CCC' + secret + random ) ) +
|
|
|
|
* ...
|
|
|
|
*/
|
|
|
|
for( i = 0; i < dlen / 16; i++ )
|
|
|
|
{
|
2013-10-11 18:58:55 +02:00
|
|
|
memset( padding, (unsigned char) ('A' + i), 1 + i );
|
2012-09-13 16:23:06 +02:00
|
|
|
|
2018-01-22 11:48:08 +01:00
|
|
|
if( ( ret = mbedtls_sha1_starts_ret( &sha1 ) ) != 0 )
|
2017-06-29 18:09:42 +02:00
|
|
|
goto exit;
|
2018-01-22 11:48:08 +01:00
|
|
|
if( ( ret = mbedtls_sha1_update_ret( &sha1, padding, 1 + i ) ) != 0 )
|
2017-06-29 18:09:42 +02:00
|
|
|
goto exit;
|
2018-01-22 11:48:08 +01:00
|
|
|
if( ( ret = mbedtls_sha1_update_ret( &sha1, secret, slen ) ) != 0 )
|
2017-06-29 18:09:42 +02:00
|
|
|
goto exit;
|
2018-01-22 11:48:08 +01:00
|
|
|
if( ( ret = mbedtls_sha1_update_ret( &sha1, random, rlen ) ) != 0 )
|
2017-06-29 18:09:42 +02:00
|
|
|
goto exit;
|
2018-01-22 11:48:08 +01:00
|
|
|
if( ( ret = mbedtls_sha1_finish_ret( &sha1, sha1sum ) ) != 0 )
|
2017-06-29 18:09:42 +02:00
|
|
|
goto exit;
|
|
|
|
|
2018-01-22 11:48:08 +01:00
|
|
|
if( ( ret = mbedtls_md5_starts_ret( &md5 ) ) != 0 )
|
2017-06-29 18:09:42 +02:00
|
|
|
goto exit;
|
2018-01-22 11:48:08 +01:00
|
|
|
if( ( ret = mbedtls_md5_update_ret( &md5, secret, slen ) ) != 0 )
|
2017-06-29 18:09:42 +02:00
|
|
|
goto exit;
|
2018-01-22 11:48:08 +01:00
|
|
|
if( ( ret = mbedtls_md5_update_ret( &md5, sha1sum, 20 ) ) != 0 )
|
2017-06-29 18:09:42 +02:00
|
|
|
goto exit;
|
2018-01-22 11:48:08 +01:00
|
|
|
if( ( ret = mbedtls_md5_finish_ret( &md5, dstbuf + i * 16 ) ) != 0 )
|
2017-06-29 18:09:42 +02:00
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
exit:
|
2015-07-06 16:11:18 +02:00
|
|
|
mbedtls_md5_free( &md5 );
|
|
|
|
mbedtls_sha1_free( &sha1 );
|
2012-09-13 16:23:06 +02:00
|
|
|
|
2018-04-17 16:51:09 +02:00
|
|
|
mbedtls_platform_zeroize( padding, sizeof( padding ) );
|
|
|
|
mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
|
2012-09-13 16:23:06 +02:00
|
|
|
|
2017-06-29 18:09:42 +02:00
|
|
|
return( ret );
|
2012-09-13 16:23:06 +02:00
|
|
|
}
|
2015-04-08 12:49:31 +02:00
|
|
|
#endif /* MBEDTLS_SSL_PROTO_SSL3 */
|
2012-09-13 16:23:06 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
|
2013-06-25 16:25:17 +02:00
|
|
|
static int tls1_prf( const unsigned char *secret, size_t slen,
|
|
|
|
const char *label,
|
|
|
|
const unsigned char *random, size_t rlen,
|
2011-04-24 10:57:21 +02:00
|
|
|
unsigned char *dstbuf, size_t dlen )
|
2009-01-03 22:22:43 +01:00
|
|
|
{
|
2011-04-24 10:57:21 +02:00
|
|
|
size_t nb, hs;
|
|
|
|
size_t i, j, k;
|
2013-06-25 16:25:17 +02:00
|
|
|
const unsigned char *S1, *S2;
|
2019-05-07 17:31:49 +02:00
|
|
|
unsigned char *tmp;
|
|
|
|
size_t tmp_len = 0;
|
2009-01-03 22:22:43 +01:00
|
|
|
unsigned char h_i[20];
|
2015-04-08 12:49:31 +02:00
|
|
|
const mbedtls_md_info_t *md_info;
|
|
|
|
mbedtls_md_context_t md_ctx;
|
2019-12-16 12:46:15 +01:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2015-03-26 11:41:28 +01:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_md_init( &md_ctx );
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2019-05-07 17:31:49 +02:00
|
|
|
tmp_len = 20 + strlen( label ) + rlen;
|
|
|
|
tmp = mbedtls_calloc( 1, tmp_len );
|
|
|
|
if( tmp == NULL )
|
|
|
|
{
|
|
|
|
ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
|
|
|
|
goto exit;
|
|
|
|
}
|
2009-01-03 22:22:43 +01:00
|
|
|
|
|
|
|
hs = ( slen + 1 ) / 2;
|
|
|
|
S1 = secret;
|
|
|
|
S2 = secret + slen - hs;
|
|
|
|
|
|
|
|
nb = strlen( label );
|
|
|
|
memcpy( tmp + 20, label, nb );
|
|
|
|
memcpy( tmp + 20 + nb, random, rlen );
|
|
|
|
nb += rlen;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* First compute P_md5(secret,label+random)[0..dlen]
|
|
|
|
*/
|
2015-04-08 12:49:31 +02:00
|
|
|
if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_MD5 ) ) == NULL )
|
2019-05-07 17:31:49 +02:00
|
|
|
{
|
|
|
|
ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
|
|
|
|
goto exit;
|
|
|
|
}
|
2015-03-24 18:08:19 +01:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
|
2019-05-07 17:31:49 +02:00
|
|
|
{
|
|
|
|
goto exit;
|
|
|
|
}
|
2015-03-26 11:41:28 +01:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_md_hmac_starts( &md_ctx, S1, hs );
|
|
|
|
mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb );
|
|
|
|
mbedtls_md_hmac_finish( &md_ctx, 4 + tmp );
|
2009-01-03 22:22:43 +01:00
|
|
|
|
|
|
|
for( i = 0; i < dlen; i += 16 )
|
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_md_hmac_reset ( &md_ctx );
|
|
|
|
mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 + nb );
|
|
|
|
mbedtls_md_hmac_finish( &md_ctx, h_i );
|
2015-03-26 11:41:28 +01:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_md_hmac_reset ( &md_ctx );
|
|
|
|
mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 );
|
|
|
|
mbedtls_md_hmac_finish( &md_ctx, 4 + tmp );
|
2009-01-03 22:22:43 +01:00
|
|
|
|
|
|
|
k = ( i + 16 > dlen ) ? dlen % 16 : 16;
|
|
|
|
|
|
|
|
for( j = 0; j < k; j++ )
|
|
|
|
dstbuf[i + j] = h_i[j];
|
|
|
|
}
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_md_free( &md_ctx );
|
2015-03-26 11:41:28 +01:00
|
|
|
|
2009-01-03 22:22:43 +01:00
|
|
|
/*
|
|
|
|
* XOR out with P_sha1(secret,label+random)[0..dlen]
|
|
|
|
*/
|
2015-04-08 12:49:31 +02:00
|
|
|
if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_SHA1 ) ) == NULL )
|
2019-05-07 17:31:49 +02:00
|
|
|
{
|
|
|
|
ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
|
|
|
|
goto exit;
|
|
|
|
}
|
2015-03-24 18:08:19 +01:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
|
2019-05-07 17:31:49 +02:00
|
|
|
{
|
|
|
|
goto exit;
|
|
|
|
}
|
2015-03-26 11:41:28 +01:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_md_hmac_starts( &md_ctx, S2, hs );
|
|
|
|
mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb );
|
|
|
|
mbedtls_md_hmac_finish( &md_ctx, tmp );
|
2009-01-03 22:22:43 +01:00
|
|
|
|
|
|
|
for( i = 0; i < dlen; i += 20 )
|
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_md_hmac_reset ( &md_ctx );
|
|
|
|
mbedtls_md_hmac_update( &md_ctx, tmp, 20 + nb );
|
|
|
|
mbedtls_md_hmac_finish( &md_ctx, h_i );
|
2015-03-26 11:41:28 +01:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_md_hmac_reset ( &md_ctx );
|
|
|
|
mbedtls_md_hmac_update( &md_ctx, tmp, 20 );
|
|
|
|
mbedtls_md_hmac_finish( &md_ctx, tmp );
|
2009-01-03 22:22:43 +01:00
|
|
|
|
|
|
|
k = ( i + 20 > dlen ) ? dlen % 20 : 20;
|
|
|
|
|
|
|
|
for( j = 0; j < k; j++ )
|
|
|
|
dstbuf[i + j] = (unsigned char)( dstbuf[i + j] ^ h_i[j] );
|
|
|
|
}
|
|
|
|
|
2019-05-07 17:31:49 +02:00
|
|
|
exit:
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_md_free( &md_ctx );
|
2015-03-26 11:41:28 +01:00
|
|
|
|
2019-05-07 17:31:49 +02:00
|
|
|
mbedtls_platform_zeroize( tmp, tmp_len );
|
2018-04-17 16:51:09 +02:00
|
|
|
mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2019-05-07 17:31:49 +02:00
|
|
|
mbedtls_free( tmp );
|
|
|
|
return( ret );
|
2009-01-03 22:22:43 +01:00
|
|
|
}
|
2015-04-08 12:49:31 +02:00
|
|
|
#endif /* MBEDTLS_SSL_PROTO_TLS1) || MBEDTLS_SSL_PROTO_TLS1_1 */
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
|
2019-01-14 09:51:11 +01:00
|
|
|
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
2019-08-17 10:30:28 +02:00
|
|
|
|
|
|
|
static psa_status_t setup_psa_key_derivation( psa_key_derivation_operation_t* derivation,
|
|
|
|
psa_key_handle_t slot,
|
|
|
|
psa_algorithm_t alg,
|
|
|
|
const unsigned char* seed, size_t seed_length,
|
|
|
|
const unsigned char* label, size_t label_length,
|
|
|
|
size_t capacity )
|
|
|
|
{
|
|
|
|
psa_status_t status;
|
|
|
|
|
|
|
|
status = psa_key_derivation_setup( derivation, alg );
|
|
|
|
if( status != PSA_SUCCESS )
|
|
|
|
return( status );
|
|
|
|
|
|
|
|
if( PSA_ALG_IS_TLS12_PRF( alg ) || PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
|
|
|
|
{
|
|
|
|
status = psa_key_derivation_input_bytes( derivation,
|
|
|
|
PSA_KEY_DERIVATION_INPUT_SEED,
|
|
|
|
seed, seed_length );
|
|
|
|
if( status != PSA_SUCCESS )
|
|
|
|
return( status );
|
|
|
|
|
2019-09-23 18:19:22 +02:00
|
|
|
if( slot == 0 )
|
|
|
|
{
|
|
|
|
status = psa_key_derivation_input_bytes(
|
|
|
|
derivation, PSA_KEY_DERIVATION_INPUT_SECRET,
|
|
|
|
NULL, 0 );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
status = psa_key_derivation_input_key(
|
|
|
|
derivation, PSA_KEY_DERIVATION_INPUT_SECRET,
|
|
|
|
slot );
|
|
|
|
}
|
2019-08-17 10:30:28 +02:00
|
|
|
if( status != PSA_SUCCESS )
|
|
|
|
return( status );
|
|
|
|
|
|
|
|
status = psa_key_derivation_input_bytes( derivation,
|
|
|
|
PSA_KEY_DERIVATION_INPUT_LABEL,
|
|
|
|
label, label_length );
|
|
|
|
if( status != PSA_SUCCESS )
|
|
|
|
return( status );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return( PSA_ERROR_NOT_SUPPORTED );
|
|
|
|
}
|
|
|
|
|
|
|
|
status = psa_key_derivation_set_capacity( derivation, capacity );
|
|
|
|
if( status != PSA_SUCCESS )
|
|
|
|
return( status );
|
|
|
|
|
|
|
|
return( PSA_SUCCESS );
|
|
|
|
}
|
|
|
|
|
2019-01-14 09:51:11 +01:00
|
|
|
static int tls_prf_generic( mbedtls_md_type_t md_type,
|
|
|
|
const unsigned char *secret, size_t slen,
|
|
|
|
const char *label,
|
|
|
|
const unsigned char *random, size_t rlen,
|
|
|
|
unsigned char *dstbuf, size_t dlen )
|
|
|
|
{
|
|
|
|
psa_status_t status;
|
|
|
|
psa_algorithm_t alg;
|
2019-09-23 18:19:22 +02:00
|
|
|
psa_key_handle_t master_slot = 0;
|
2019-08-16 14:47:29 +02:00
|
|
|
psa_key_derivation_operation_t derivation =
|
2019-07-30 13:53:32 +02:00
|
|
|
PSA_KEY_DERIVATION_OPERATION_INIT;
|
2019-01-14 09:51:11 +01:00
|
|
|
|
|
|
|
if( md_type == MBEDTLS_MD_SHA384 )
|
|
|
|
alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_384);
|
|
|
|
else
|
|
|
|
alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_256);
|
|
|
|
|
2019-09-23 18:19:22 +02:00
|
|
|
/* Normally a "secret" should be long enough to be impossible to
|
|
|
|
* find by brute force, and in particular should not be empty. But
|
|
|
|
* this PRF is also used to derive an IV, in particular in EAP-TLS,
|
|
|
|
* and for this use case it makes sense to have a 0-length "secret".
|
|
|
|
* Since the key API doesn't allow importing a key of length 0,
|
|
|
|
* keep master_slot=0, which setup_psa_key_derivation() understands
|
|
|
|
* to mean a 0-length "secret" input. */
|
|
|
|
if( slen != 0 )
|
|
|
|
{
|
|
|
|
psa_key_attributes_t key_attributes = psa_key_attributes_init();
|
|
|
|
psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_DERIVE );
|
|
|
|
psa_set_key_algorithm( &key_attributes, alg );
|
|
|
|
psa_set_key_type( &key_attributes, PSA_KEY_TYPE_DERIVE );
|
2019-01-14 09:51:11 +01:00
|
|
|
|
2019-09-23 18:19:22 +02:00
|
|
|
status = psa_import_key( &key_attributes, secret, slen, &master_slot );
|
|
|
|
if( status != PSA_SUCCESS )
|
|
|
|
return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
|
|
|
|
}
|
2019-01-14 09:51:11 +01:00
|
|
|
|
2019-08-17 10:30:28 +02:00
|
|
|
status = setup_psa_key_derivation( &derivation,
|
|
|
|
master_slot, alg,
|
|
|
|
random, rlen,
|
|
|
|
(unsigned char const *) label,
|
|
|
|
(size_t) strlen( label ),
|
|
|
|
dlen );
|
2019-01-14 09:51:11 +01:00
|
|
|
if( status != PSA_SUCCESS )
|
|
|
|
{
|
2019-08-16 14:47:29 +02:00
|
|
|
psa_key_derivation_abort( &derivation );
|
2019-01-14 09:51:11 +01:00
|
|
|
psa_destroy_key( master_slot );
|
|
|
|
return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
|
|
|
|
}
|
|
|
|
|
2019-08-16 14:47:29 +02:00
|
|
|
status = psa_key_derivation_output_bytes( &derivation, dstbuf, dlen );
|
2019-01-14 09:51:11 +01:00
|
|
|
if( status != PSA_SUCCESS )
|
|
|
|
{
|
2019-08-16 14:47:29 +02:00
|
|
|
psa_key_derivation_abort( &derivation );
|
2019-01-14 09:51:11 +01:00
|
|
|
psa_destroy_key( master_slot );
|
|
|
|
return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
|
|
|
|
}
|
|
|
|
|
2019-08-16 14:47:29 +02:00
|
|
|
status = psa_key_derivation_abort( &derivation );
|
2019-01-14 09:51:11 +01:00
|
|
|
if( status != PSA_SUCCESS )
|
2019-01-14 11:37:13 +01:00
|
|
|
{
|
|
|
|
psa_destroy_key( master_slot );
|
2019-01-14 09:51:11 +01:00
|
|
|
return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
|
2019-01-14 11:37:13 +01:00
|
|
|
}
|
2019-01-14 09:51:11 +01:00
|
|
|
|
2019-09-23 18:19:22 +02:00
|
|
|
if( master_slot != 0 )
|
|
|
|
status = psa_destroy_key( master_slot );
|
2019-01-14 09:51:11 +01:00
|
|
|
if( status != PSA_SUCCESS )
|
|
|
|
return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
|
|
|
|
|
2019-01-15 09:25:18 +01:00
|
|
|
return( 0 );
|
2019-01-14 09:51:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#else /* MBEDTLS_USE_PSA_CRYPTO */
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
static int tls_prf_generic( mbedtls_md_type_t md_type,
|
2015-03-26 11:11:49 +01:00
|
|
|
const unsigned char *secret, size_t slen,
|
|
|
|
const char *label,
|
|
|
|
const unsigned char *random, size_t rlen,
|
|
|
|
unsigned char *dstbuf, size_t dlen )
|
2012-04-11 14:09:53 +02:00
|
|
|
{
|
|
|
|
size_t nb;
|
2015-03-26 11:11:49 +01:00
|
|
|
size_t i, j, k, md_len;
|
2019-05-07 17:31:49 +02:00
|
|
|
unsigned char *tmp;
|
|
|
|
size_t tmp_len = 0;
|
2015-04-08 12:49:31 +02:00
|
|
|
unsigned char h_i[MBEDTLS_MD_MAX_SIZE];
|
|
|
|
const mbedtls_md_info_t *md_info;
|
|
|
|
mbedtls_md_context_t md_ctx;
|
2019-12-16 12:46:15 +01:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2015-03-26 11:41:28 +01:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_md_init( &md_ctx );
|
2012-04-11 14:09:53 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
if( ( md_info = mbedtls_md_info_from_type( md_type ) ) == NULL )
|
|
|
|
return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
|
2015-03-26 11:11:49 +01:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
md_len = mbedtls_md_get_size( md_info );
|
2015-03-26 11:11:49 +01:00
|
|
|
|
2019-05-07 17:31:49 +02:00
|
|
|
tmp_len = md_len + strlen( label ) + rlen;
|
|
|
|
tmp = mbedtls_calloc( 1, tmp_len );
|
|
|
|
if( tmp == NULL )
|
|
|
|
{
|
|
|
|
ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
|
|
|
|
goto exit;
|
|
|
|
}
|
2012-04-11 14:09:53 +02:00
|
|
|
|
|
|
|
nb = strlen( label );
|
2015-03-26 11:11:49 +01:00
|
|
|
memcpy( tmp + md_len, label, nb );
|
|
|
|
memcpy( tmp + md_len + nb, random, rlen );
|
2012-04-11 14:09:53 +02:00
|
|
|
nb += rlen;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Compute P_<hash>(secret, label + random)[0..dlen]
|
|
|
|
*/
|
2015-04-08 12:49:31 +02:00
|
|
|
if ( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
|
2019-05-07 17:31:49 +02:00
|
|
|
goto exit;
|
2015-03-26 11:41:28 +01:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_md_hmac_starts( &md_ctx, secret, slen );
|
|
|
|
mbedtls_md_hmac_update( &md_ctx, tmp + md_len, nb );
|
|
|
|
mbedtls_md_hmac_finish( &md_ctx, tmp );
|
2012-04-11 14:09:53 +02:00
|
|
|
|
2015-03-26 11:11:49 +01:00
|
|
|
for( i = 0; i < dlen; i += md_len )
|
2012-04-11 14:09:53 +02:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_md_hmac_reset ( &md_ctx );
|
|
|
|
mbedtls_md_hmac_update( &md_ctx, tmp, md_len + nb );
|
|
|
|
mbedtls_md_hmac_finish( &md_ctx, h_i );
|
2015-03-26 11:41:28 +01:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_md_hmac_reset ( &md_ctx );
|
|
|
|
mbedtls_md_hmac_update( &md_ctx, tmp, md_len );
|
|
|
|
mbedtls_md_hmac_finish( &md_ctx, tmp );
|
2012-04-11 14:09:53 +02:00
|
|
|
|
2015-03-26 11:11:49 +01:00
|
|
|
k = ( i + md_len > dlen ) ? dlen % md_len : md_len;
|
2012-04-11 14:09:53 +02:00
|
|
|
|
|
|
|
for( j = 0; j < k; j++ )
|
|
|
|
dstbuf[i + j] = h_i[j];
|
|
|
|
}
|
|
|
|
|
2019-05-07 17:31:49 +02:00
|
|
|
exit:
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_md_free( &md_ctx );
|
2015-03-26 11:41:28 +01:00
|
|
|
|
2019-05-07 17:31:49 +02:00
|
|
|
mbedtls_platform_zeroize( tmp, tmp_len );
|
2018-04-17 16:51:09 +02:00
|
|
|
mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
|
2012-04-11 14:09:53 +02:00
|
|
|
|
2019-05-07 17:31:49 +02:00
|
|
|
mbedtls_free( tmp );
|
|
|
|
|
|
|
|
return( ret );
|
2012-04-11 14:09:53 +02:00
|
|
|
}
|
2019-01-14 09:51:11 +01:00
|
|
|
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_SHA256_C)
|
2015-03-26 11:11:49 +01:00
|
|
|
static int tls_prf_sha256( const unsigned char *secret, size_t slen,
|
|
|
|
const char *label,
|
|
|
|
const unsigned char *random, size_t rlen,
|
|
|
|
unsigned char *dstbuf, size_t dlen )
|
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
return( tls_prf_generic( MBEDTLS_MD_SHA256, secret, slen,
|
2015-03-26 11:11:49 +01:00
|
|
|
label, random, rlen, dstbuf, dlen ) );
|
|
|
|
}
|
2015-04-08 12:49:31 +02:00
|
|
|
#endif /* MBEDTLS_SHA256_C */
|
2012-04-11 14:09:53 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_SHA512_C)
|
2013-06-25 16:25:17 +02:00
|
|
|
static int tls_prf_sha384( const unsigned char *secret, size_t slen,
|
|
|
|
const char *label,
|
|
|
|
const unsigned char *random, size_t rlen,
|
2012-04-18 16:23:57 +02:00
|
|
|
unsigned char *dstbuf, size_t dlen )
|
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
return( tls_prf_generic( MBEDTLS_MD_SHA384, secret, slen,
|
2015-03-26 11:11:49 +01:00
|
|
|
label, random, rlen, dstbuf, dlen ) );
|
2012-04-18 16:23:57 +02:00
|
|
|
}
|
2015-04-08 12:49:31 +02:00
|
|
|
#endif /* MBEDTLS_SHA512_C */
|
|
|
|
#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
|
2012-04-18 16:23:57 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
static void ssl_update_checksum_start( mbedtls_ssl_context *, const unsigned char *, size_t );
|
2013-08-27 21:19:20 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
|
|
|
|
defined(MBEDTLS_SSL_PROTO_TLS1_1)
|
|
|
|
static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *, const unsigned char *, size_t );
|
2013-08-27 21:19:20 +02:00
|
|
|
#endif
|
2012-04-18 18:10:25 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_SSL_PROTO_SSL3)
|
2019-05-03 11:43:28 +02:00
|
|
|
static void ssl_calc_verify_ssl( const mbedtls_ssl_context *, unsigned char *, size_t * );
|
2015-04-08 12:49:31 +02:00
|
|
|
static void ssl_calc_finished_ssl( mbedtls_ssl_context *, unsigned char *, int );
|
2013-08-27 21:19:20 +02:00
|
|
|
#endif
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
|
2019-05-03 11:43:28 +02:00
|
|
|
static void ssl_calc_verify_tls( const mbedtls_ssl_context *, unsigned char *, size_t * );
|
2015-04-08 12:49:31 +02:00
|
|
|
static void ssl_calc_finished_tls( mbedtls_ssl_context *, unsigned char *, int );
|
2013-08-27 21:19:20 +02:00
|
|
|
#endif
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
|
|
|
|
#if defined(MBEDTLS_SHA256_C)
|
|
|
|
static void ssl_update_checksum_sha256( mbedtls_ssl_context *, const unsigned char *, size_t );
|
2019-05-03 11:43:28 +02:00
|
|
|
static void ssl_calc_verify_tls_sha256( const mbedtls_ssl_context *,unsigned char *, size_t * );
|
2015-04-08 12:49:31 +02:00
|
|
|
static void ssl_calc_finished_tls_sha256( mbedtls_ssl_context *,unsigned char *, int );
|
2013-08-27 21:19:20 +02:00
|
|
|
#endif
|
2012-11-24 11:26:46 +01:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_SHA512_C)
|
|
|
|
static void ssl_update_checksum_sha384( mbedtls_ssl_context *, const unsigned char *, size_t );
|
2019-05-03 11:43:28 +02:00
|
|
|
static void ssl_calc_verify_tls_sha384( const mbedtls_ssl_context *, unsigned char *, size_t * );
|
2015-04-08 12:49:31 +02:00
|
|
|
static void ssl_calc_finished_tls_sha384( mbedtls_ssl_context *, unsigned char *, int );
|
2012-11-24 11:26:46 +01:00
|
|
|
#endif
|
2015-04-08 12:49:31 +02:00
|
|
|
#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
|
2012-04-11 14:09:53 +02:00
|
|
|
|
2019-02-18 23:35:14 +01:00
|
|
|
#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED) && \
|
2018-10-23 16:26:22 +02:00
|
|
|
defined(MBEDTLS_USE_PSA_CRYPTO)
|
|
|
|
static int ssl_use_opaque_psk( mbedtls_ssl_context const *ssl )
|
|
|
|
{
|
|
|
|
if( ssl->conf->f_psk != NULL )
|
|
|
|
{
|
|
|
|
/* If we've used a callback to select the PSK,
|
|
|
|
* the static configuration is irrelevant. */
|
|
|
|
if( ssl->handshake->psk_opaque != 0 )
|
|
|
|
return( 1 );
|
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
|
|
|
if( ssl->conf->psk_opaque != 0 )
|
|
|
|
return( 1 );
|
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
#endif /* MBEDTLS_USE_PSA_CRYPTO &&
|
2019-02-18 23:35:14 +01:00
|
|
|
MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
|
2018-10-23 16:26:22 +02:00
|
|
|
|
2019-05-14 19:19:13 +02:00
|
|
|
#if defined(MBEDTLS_SSL_EXPORT_KEYS)
|
|
|
|
static mbedtls_tls_prf_types tls_prf_get_type( mbedtls_ssl_tls_prf_cb *tls_prf )
|
|
|
|
{
|
|
|
|
#if defined(MBEDTLS_SSL_PROTO_SSL3)
|
|
|
|
if( tls_prf == ssl3_prf )
|
|
|
|
{
|
2019-05-15 11:32:32 +02:00
|
|
|
return( MBEDTLS_SSL_TLS_PRF_SSL3 );
|
2019-05-14 19:19:13 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
|
|
|
|
if( tls_prf == tls1_prf )
|
|
|
|
{
|
|
|
|
return( MBEDTLS_SSL_TLS_PRF_TLS1 );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
|
|
|
|
#if defined(MBEDTLS_SHA512_C)
|
|
|
|
if( tls_prf == tls_prf_sha384 )
|
|
|
|
{
|
|
|
|
return( MBEDTLS_SSL_TLS_PRF_SHA384 );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
#if defined(MBEDTLS_SHA256_C)
|
|
|
|
if( tls_prf == tls_prf_sha256 )
|
|
|
|
{
|
|
|
|
return( MBEDTLS_SSL_TLS_PRF_SHA256 );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
|
|
|
|
return( MBEDTLS_SSL_TLS_PRF_NONE );
|
|
|
|
}
|
|
|
|
#endif /* MBEDTLS_SSL_EXPORT_KEYS */
|
|
|
|
|
2019-05-12 13:54:30 +02:00
|
|
|
int mbedtls_ssl_tls_prf( const mbedtls_tls_prf_types prf,
|
|
|
|
const unsigned char *secret, size_t slen,
|
|
|
|
const char *label,
|
|
|
|
const unsigned char *random, size_t rlen,
|
|
|
|
unsigned char *dstbuf, size_t dlen )
|
|
|
|
{
|
|
|
|
mbedtls_ssl_tls_prf_cb *tls_prf = NULL;
|
|
|
|
|
|
|
|
switch( prf )
|
|
|
|
{
|
|
|
|
#if defined(MBEDTLS_SSL_PROTO_SSL3)
|
|
|
|
case MBEDTLS_SSL_TLS_PRF_SSL3:
|
|
|
|
tls_prf = ssl3_prf;
|
|
|
|
break;
|
2019-05-15 13:54:22 +02:00
|
|
|
#endif /* MBEDTLS_SSL_PROTO_SSL3 */
|
2019-05-12 13:54:30 +02:00
|
|
|
#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
|
|
|
|
case MBEDTLS_SSL_TLS_PRF_TLS1:
|
|
|
|
tls_prf = tls1_prf;
|
|
|
|
break;
|
2019-05-15 13:54:22 +02:00
|
|
|
#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
|
2019-05-12 13:54:30 +02:00
|
|
|
#if defined(MBEDTLS_SHA512_C)
|
|
|
|
case MBEDTLS_SSL_TLS_PRF_SHA384:
|
|
|
|
tls_prf = tls_prf_sha384;
|
|
|
|
break;
|
2019-05-15 13:54:22 +02:00
|
|
|
#endif /* MBEDTLS_SHA512_C */
|
2019-05-12 13:54:30 +02:00
|
|
|
#if defined(MBEDTLS_SHA256_C)
|
|
|
|
case MBEDTLS_SSL_TLS_PRF_SHA256:
|
|
|
|
tls_prf = tls_prf_sha256;
|
|
|
|
break;
|
2019-05-15 13:54:22 +02:00
|
|
|
#endif /* MBEDTLS_SHA256_C */
|
|
|
|
#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
|
2019-05-12 13:54:30 +02:00
|
|
|
default:
|
|
|
|
return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
|
|
|
|
}
|
|
|
|
|
|
|
|
return( tls_prf( secret, slen, label, random, rlen, dstbuf, dlen ) );
|
|
|
|
}
|
|
|
|
|
2019-05-06 13:32:17 +02:00
|
|
|
/* Type for the TLS PRF */
|
|
|
|
typedef int ssl_tls_prf_t(const unsigned char *, size_t, const char *,
|
|
|
|
const unsigned char *, size_t,
|
|
|
|
unsigned char *, size_t);
|
|
|
|
|
2019-04-30 11:41:40 +02:00
|
|
|
/*
|
2019-05-06 12:55:40 +02:00
|
|
|
* Populate a transform structure with session keys and all the other
|
|
|
|
* necessary information.
|
2019-04-30 11:41:40 +02:00
|
|
|
*
|
2019-05-06 12:55:40 +02:00
|
|
|
* Parameters:
|
|
|
|
* - [in/out]: transform: structure to populate
|
|
|
|
* [in] must be just initialised with mbedtls_ssl_transform_init()
|
2019-05-06 13:48:22 +02:00
|
|
|
* [out] fully populated, ready for use by mbedtls_ssl_{en,de}crypt_buf()
|
2019-05-10 10:50:04 +02:00
|
|
|
* - [in] ciphersuite
|
|
|
|
* - [in] master
|
|
|
|
* - [in] encrypt_then_mac
|
|
|
|
* - [in] trunc_hmac
|
|
|
|
* - [in] compression
|
2019-05-06 13:32:17 +02:00
|
|
|
* - [in] tls_prf: pointer to PRF to use for key derivation
|
|
|
|
* - [in] randbytes: buffer holding ServerHello.random + ClientHello.random
|
2019-05-06 13:48:22 +02:00
|
|
|
* - [in] minor_ver: SSL/TLS minor version
|
|
|
|
* - [in] endpoint: client or server
|
|
|
|
* - [in] ssl: optionally used for:
|
2020-02-26 09:53:42 +01:00
|
|
|
* - MBEDTLS_SSL_HW_RECORD_ACCEL: whole context (non-const)
|
2019-05-06 13:48:22 +02:00
|
|
|
* - MBEDTLS_SSL_EXPORT_KEYS: ssl->conf->{f,p}_export_keys
|
|
|
|
* - MBEDTLS_DEBUG_C: ssl->conf->{f,p}_dbg
|
2019-04-30 11:41:40 +02:00
|
|
|
*/
|
2019-05-06 12:55:40 +02:00
|
|
|
static int ssl_populate_transform( mbedtls_ssl_transform *transform,
|
2019-05-10 10:50:04 +02:00
|
|
|
int ciphersuite,
|
|
|
|
const unsigned char master[48],
|
2019-08-16 11:06:56 +02:00
|
|
|
#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
|
2019-05-10 10:50:04 +02:00
|
|
|
#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
|
|
|
|
int encrypt_then_mac,
|
2019-08-16 11:06:56 +02:00
|
|
|
#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
|
2019-05-10 10:50:04 +02:00
|
|
|
#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
|
|
|
|
int trunc_hmac,
|
2019-08-16 11:06:56 +02:00
|
|
|
#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
|
|
|
|
#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
|
2019-05-10 10:50:04 +02:00
|
|
|
#if defined(MBEDTLS_ZLIB_SUPPORT)
|
|
|
|
int compression,
|
|
|
|
#endif
|
2019-05-06 13:32:17 +02:00
|
|
|
ssl_tls_prf_t tls_prf,
|
|
|
|
const unsigned char randbytes[64],
|
2019-05-06 13:48:22 +02:00
|
|
|
int minor_ver,
|
|
|
|
unsigned endpoint,
|
2020-03-13 11:28:19 +01:00
|
|
|
#if !defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
|
|
|
|
const
|
|
|
|
#endif
|
2020-02-26 09:53:42 +01:00
|
|
|
mbedtls_ssl_context *ssl )
|
2009-01-03 22:22:43 +01:00
|
|
|
{
|
2013-08-31 17:25:14 +02:00
|
|
|
int ret = 0;
|
2018-11-17 23:27:38 +01:00
|
|
|
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
|
|
|
int psa_fallthrough;
|
|
|
|
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
2009-01-03 22:22:43 +01:00
|
|
|
unsigned char keyblk[256];
|
|
|
|
unsigned char *key1;
|
|
|
|
unsigned char *key2;
|
2013-01-07 18:20:04 +01:00
|
|
|
unsigned char *mac_enc;
|
|
|
|
unsigned char *mac_dec;
|
2020-05-06 21:49:46 +02:00
|
|
|
size_t mac_key_len = 0;
|
2013-10-11 18:58:55 +02:00
|
|
|
size_t iv_copy_len;
|
2017-12-27 09:17:40 +01:00
|
|
|
unsigned keylen;
|
2017-12-27 22:34:08 +01:00
|
|
|
const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
|
2015-04-08 12:49:31 +02:00
|
|
|
const mbedtls_cipher_info_t *cipher_info;
|
|
|
|
const mbedtls_md_info_t *md_info;
|
2013-01-07 18:20:04 +01:00
|
|
|
|
2019-05-06 13:48:22 +02:00
|
|
|
#if !defined(MBEDTLS_SSL_HW_RECORD_ACCEL) && \
|
|
|
|
!defined(MBEDTLS_SSL_EXPORT_KEYS) && \
|
|
|
|
!defined(MBEDTLS_DEBUG_C)
|
2019-05-07 10:17:56 +02:00
|
|
|
ssl = NULL; /* make sure we don't use it except for those cases */
|
2019-05-06 13:48:22 +02:00
|
|
|
(void) ssl;
|
|
|
|
#endif
|
|
|
|
|
2019-07-09 12:54:17 +02:00
|
|
|
/*
|
|
|
|
* Some data just needs copying into the structure
|
|
|
|
*/
|
2019-06-05 14:32:08 +02:00
|
|
|
#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) && \
|
|
|
|
defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
|
2019-05-10 10:50:04 +02:00
|
|
|
transform->encrypt_then_mac = encrypt_then_mac;
|
2017-12-27 22:37:21 +01:00
|
|
|
#endif
|
2019-05-06 13:48:22 +02:00
|
|
|
transform->minor_ver = minor_ver;
|
2017-12-27 22:34:08 +01:00
|
|
|
|
2019-07-09 12:54:17 +02:00
|
|
|
#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
|
|
|
|
memcpy( transform->randbytes, randbytes, sizeof( transform->randbytes ) );
|
|
|
|
#endif
|
|
|
|
|
2019-05-06 13:32:17 +02:00
|
|
|
/*
|
|
|
|
* Get various info structures
|
|
|
|
*/
|
2019-05-10 10:50:04 +02:00
|
|
|
ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( ciphersuite );
|
2019-05-06 13:32:17 +02:00
|
|
|
if( ciphersuite_info == NULL )
|
|
|
|
{
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 1, ( "ciphersuite info for %d not found",
|
2019-05-10 10:50:04 +02:00
|
|
|
ciphersuite ) );
|
2019-05-06 13:32:17 +02:00
|
|
|
return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
|
|
|
|
}
|
|
|
|
|
2017-12-27 22:34:08 +01:00
|
|
|
cipher_info = mbedtls_cipher_info_from_type( ciphersuite_info->cipher );
|
2013-01-07 18:20:04 +01:00
|
|
|
if( cipher_info == NULL )
|
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %d not found",
|
2017-12-27 22:34:08 +01:00
|
|
|
ciphersuite_info->cipher ) );
|
2015-04-08 12:49:31 +02:00
|
|
|
return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
|
2013-01-07 18:20:04 +01:00
|
|
|
}
|
|
|
|
|
2017-12-27 22:34:08 +01:00
|
|
|
md_info = mbedtls_md_info_from_type( ciphersuite_info->mac );
|
2013-01-07 18:20:04 +01:00
|
|
|
if( md_info == NULL )
|
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %d not found",
|
2017-12-27 22:34:08 +01:00
|
|
|
ciphersuite_info->mac ) );
|
2015-04-08 12:49:31 +02:00
|
|
|
return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
|
2013-01-07 18:20:04 +01:00
|
|
|
}
|
|
|
|
|
2019-05-15 15:03:01 +02:00
|
|
|
#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
|
2019-04-26 17:22:27 +02:00
|
|
|
/* Copy own and peer's CID if the use of the CID
|
|
|
|
* extension has been negotiated. */
|
|
|
|
if( ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_ENABLED )
|
|
|
|
{
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 3, ( "Copy CIDs into SSL transform" ) );
|
2019-04-30 14:52:29 +02:00
|
|
|
|
2019-05-03 16:23:51 +02:00
|
|
|
transform->in_cid_len = ssl->own_cid_len;
|
|
|
|
memcpy( transform->in_cid, ssl->own_cid, ssl->own_cid_len );
|
2019-05-15 11:21:55 +02:00
|
|
|
MBEDTLS_SSL_DEBUG_BUF( 3, "Incoming CID", transform->in_cid,
|
|
|
|
transform->in_cid_len );
|
|
|
|
|
|
|
|
transform->out_cid_len = ssl->handshake->peer_cid_len;
|
2019-05-03 16:23:51 +02:00
|
|
|
memcpy( transform->out_cid, ssl->handshake->peer_cid,
|
|
|
|
ssl->handshake->peer_cid_len );
|
2019-04-26 17:22:27 +02:00
|
|
|
MBEDTLS_SSL_DEBUG_BUF( 3, "Outgoing CID", transform->out_cid,
|
|
|
|
transform->out_cid_len );
|
|
|
|
}
|
2019-05-15 15:03:01 +02:00
|
|
|
#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
|
2019-04-26 17:22:27 +02:00
|
|
|
|
2009-01-03 22:22:43 +01:00
|
|
|
/*
|
2019-05-06 13:32:17 +02:00
|
|
|
* Compute key block using the PRF
|
2009-01-03 22:22:43 +01:00
|
|
|
*/
|
2019-05-10 10:50:04 +02:00
|
|
|
ret = tls_prf( master, 48, "key expansion", randbytes, 64, keyblk, 256 );
|
2015-03-26 11:47:47 +01:00
|
|
|
if( ret != 0 )
|
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
|
2015-03-26 11:47:47 +01:00
|
|
|
return( ret );
|
|
|
|
}
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
|
2019-05-20 10:27:20 +02:00
|
|
|
mbedtls_ssl_get_ciphersuite_name( ciphersuite ) ) );
|
2019-05-10 10:50:04 +02:00
|
|
|
MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", master, 48 );
|
2019-05-06 13:32:17 +02:00
|
|
|
MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", randbytes, 64 );
|
2015-04-08 12:49:31 +02:00
|
|
|
MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
|
2009-01-03 22:22:43 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Determine the appropriate key, IV and MAC length.
|
|
|
|
*/
|
2009-01-11 21:25:36 +01:00
|
|
|
|
2017-12-27 09:17:40 +01:00
|
|
|
keylen = cipher_info->key_bitlen / 8;
|
2014-06-18 15:34:40 +02:00
|
|
|
|
2018-01-03 16:32:31 +01:00
|
|
|
#if defined(MBEDTLS_GCM_C) || \
|
|
|
|
defined(MBEDTLS_CCM_C) || \
|
|
|
|
defined(MBEDTLS_CHACHAPOLY_C)
|
2015-04-08 12:49:31 +02:00
|
|
|
if( cipher_info->mode == MBEDTLS_MODE_GCM ||
|
2018-06-18 11:16:43 +02:00
|
|
|
cipher_info->mode == MBEDTLS_MODE_CCM ||
|
|
|
|
cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
|
2013-01-07 18:20:04 +01:00
|
|
|
{
|
2018-11-16 16:21:18 +01:00
|
|
|
size_t explicit_ivlen;
|
2018-06-18 11:16:43 +02:00
|
|
|
|
2014-06-18 15:34:40 +02:00
|
|
|
transform->maclen = 0;
|
2017-11-09 19:39:33 +01:00
|
|
|
mac_key_len = 0;
|
2017-12-27 22:34:08 +01:00
|
|
|
transform->taglen =
|
|
|
|
ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
|
2014-06-18 15:34:40 +02:00
|
|
|
|
2020-05-28 08:36:33 +02:00
|
|
|
/* All modes haves 96-bit IVs, but the length of the static parts vary
|
|
|
|
* with mode and version:
|
|
|
|
* - For GCM and CCM in TLS 1.2, there's a static IV of 4 Bytes
|
|
|
|
* (to be concatenated with a dynamically chosen IV of 8 Bytes)
|
2020-05-28 08:39:43 +02:00
|
|
|
* - For ChaChaPoly in TLS 1.2, and all modes in TLS 1.3, there's
|
|
|
|
* a static IV of 12 Bytes (to be XOR'ed with the 8 Byte record
|
|
|
|
* sequence number).
|
2018-06-18 11:16:43 +02:00
|
|
|
*/
|
2013-01-07 18:20:04 +01:00
|
|
|
transform->ivlen = 12;
|
2020-05-28 08:39:43 +02:00
|
|
|
#if defined(MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL)
|
|
|
|
if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_4 )
|
|
|
|
{
|
2018-06-18 11:16:43 +02:00
|
|
|
transform->fixed_ivlen = 12;
|
2020-05-28 08:39:43 +02:00
|
|
|
}
|
2018-06-18 11:16:43 +02:00
|
|
|
else
|
2020-05-28 08:39:43 +02:00
|
|
|
#endif /* MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */
|
|
|
|
{
|
|
|
|
if( cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
|
|
|
|
transform->fixed_ivlen = 12;
|
|
|
|
else
|
|
|
|
transform->fixed_ivlen = 4;
|
|
|
|
}
|
2018-06-18 11:16:43 +02:00
|
|
|
|
|
|
|
/* Minimum length of encrypted record */
|
|
|
|
explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
|
2017-12-27 22:34:08 +01:00
|
|
|
transform->minlen = explicit_ivlen + transform->taglen;
|
2013-01-07 18:20:04 +01:00
|
|
|
}
|
|
|
|
else
|
2018-01-03 16:32:31 +01:00
|
|
|
#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
|
|
|
|
#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
|
|
|
|
if( cipher_info->mode == MBEDTLS_MODE_STREAM ||
|
|
|
|
cipher_info->mode == MBEDTLS_MODE_CBC )
|
2013-01-07 18:20:04 +01:00
|
|
|
{
|
2014-06-18 15:34:40 +02:00
|
|
|
/* Initialize HMAC contexts */
|
2015-04-08 12:49:31 +02:00
|
|
|
if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 ||
|
|
|
|
( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 )
|
2014-06-18 15:34:40 +02:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
|
2019-05-07 17:27:13 +02:00
|
|
|
goto end;
|
2014-06-18 15:34:40 +02:00
|
|
|
}
|
2012-02-06 17:45:10 +01:00
|
|
|
|
2014-06-18 15:34:40 +02:00
|
|
|
/* Get MAC length */
|
2017-11-09 19:39:33 +01:00
|
|
|
mac_key_len = mbedtls_md_get_size( md_info );
|
|
|
|
transform->maclen = mac_key_len;
|
2013-07-19 12:19:21 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
|
2014-06-18 15:34:40 +02:00
|
|
|
/*
|
|
|
|
* If HMAC is to be truncated, we shall keep the leftmost bytes,
|
|
|
|
* (rfc 6066 page 13 or rfc 2104 section 4),
|
|
|
|
* so we only need to adjust the length here.
|
|
|
|
*/
|
2019-05-10 10:50:04 +02:00
|
|
|
if( trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_ENABLED )
|
2017-11-20 17:36:41 +01:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
transform->maclen = MBEDTLS_SSL_TRUNCATED_HMAC_LEN;
|
2017-11-20 17:36:41 +01:00
|
|
|
|
|
|
|
#if defined(MBEDTLS_SSL_TRUNCATED_HMAC_COMPAT)
|
|
|
|
/* Fall back to old, non-compliant version of the truncated
|
2017-11-21 18:20:17 +01:00
|
|
|
* HMAC implementation which also truncates the key
|
|
|
|
* (Mbed TLS versions from 1.3 to 2.6.0) */
|
2017-11-20 17:36:41 +01:00
|
|
|
mac_key_len = transform->maclen;
|
|
|
|
#endif
|
|
|
|
}
|
2015-04-08 12:49:31 +02:00
|
|
|
#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
|
2012-02-06 17:45:10 +01:00
|
|
|
|
2014-06-18 15:34:40 +02:00
|
|
|
/* IV length */
|
2013-01-07 18:20:04 +01:00
|
|
|
transform->ivlen = cipher_info->iv_size;
|
2012-02-06 17:45:10 +01:00
|
|
|
|
2014-06-18 16:06:02 +02:00
|
|
|
/* Minimum length */
|
2015-04-08 12:49:31 +02:00
|
|
|
if( cipher_info->mode == MBEDTLS_MODE_STREAM )
|
2014-06-18 16:06:02 +02:00
|
|
|
transform->minlen = transform->maclen;
|
|
|
|
else
|
2013-01-07 18:20:04 +01:00
|
|
|
{
|
2014-06-18 16:06:02 +02:00
|
|
|
/*
|
|
|
|
* GenericBlockCipher:
|
2014-11-04 16:15:39 +01:00
|
|
|
* 1. if EtM is in use: one block plus MAC
|
|
|
|
* otherwise: * first multiple of blocklen greater than maclen
|
|
|
|
* 2. IV except for SSL3 and TLS 1.0
|
2014-06-18 16:06:02 +02:00
|
|
|
*/
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
|
2019-05-10 10:50:04 +02:00
|
|
|
if( encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
|
2014-11-04 16:15:39 +01:00
|
|
|
{
|
|
|
|
transform->minlen = transform->maclen
|
|
|
|
+ cipher_info->block_size;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
transform->minlen = transform->maclen
|
|
|
|
+ cipher_info->block_size
|
|
|
|
- transform->maclen % cipher_info->block_size;
|
|
|
|
}
|
2014-06-18 16:06:02 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
|
2019-05-06 13:48:22 +02:00
|
|
|
if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ||
|
|
|
|
minor_ver == MBEDTLS_SSL_MINOR_VERSION_1 )
|
2014-06-18 16:06:02 +02:00
|
|
|
; /* No need to adjust minlen */
|
2013-01-07 18:20:04 +01:00
|
|
|
else
|
2014-06-18 16:06:02 +02:00
|
|
|
#endif
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
|
2019-05-06 13:48:22 +02:00
|
|
|
if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_2 ||
|
|
|
|
minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
|
2014-06-18 16:06:02 +02:00
|
|
|
{
|
|
|
|
transform->minlen += transform->ivlen;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
|
2019-05-07 17:27:13 +02:00
|
|
|
ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
|
|
|
|
goto end;
|
2014-06-18 16:06:02 +02:00
|
|
|
}
|
2013-01-07 18:20:04 +01:00
|
|
|
}
|
2009-01-03 22:22:43 +01:00
|
|
|
}
|
2018-01-03 16:32:31 +01:00
|
|
|
else
|
|
|
|
#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
|
|
|
|
{
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
|
|
|
|
return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
|
|
|
|
}
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2017-12-27 09:17:40 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %u, minlen: %u, ivlen: %u, maclen: %u",
|
|
|
|
(unsigned) keylen,
|
|
|
|
(unsigned) transform->minlen,
|
|
|
|
(unsigned) transform->ivlen,
|
|
|
|
(unsigned) transform->maclen ) );
|
2009-01-03 22:22:43 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Finally setup the cipher contexts, IVs and MAC secrets.
|
|
|
|
*/
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_SSL_CLI_C)
|
2019-05-06 13:48:22 +02:00
|
|
|
if( endpoint == MBEDTLS_SSL_IS_CLIENT )
|
2009-01-03 22:22:43 +01:00
|
|
|
{
|
2017-11-09 19:39:33 +01:00
|
|
|
key1 = keyblk + mac_key_len * 2;
|
2017-12-27 09:17:40 +01:00
|
|
|
key2 = keyblk + mac_key_len * 2 + keylen;
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2013-01-07 18:20:04 +01:00
|
|
|
mac_enc = keyblk;
|
2017-11-09 19:39:33 +01:00
|
|
|
mac_dec = keyblk + mac_key_len;
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2010-07-25 16:24:53 +02:00
|
|
|
/*
|
|
|
|
* This is not used in TLS v1.1.
|
|
|
|
*/
|
2012-09-16 21:57:18 +02:00
|
|
|
iv_copy_len = ( transform->fixed_ivlen ) ?
|
|
|
|
transform->fixed_ivlen : transform->ivlen;
|
2017-12-27 09:17:40 +01:00
|
|
|
memcpy( transform->iv_enc, key2 + keylen, iv_copy_len );
|
|
|
|
memcpy( transform->iv_dec, key2 + keylen + iv_copy_len,
|
2012-04-18 16:23:57 +02:00
|
|
|
iv_copy_len );
|
2009-01-03 22:22:43 +01:00
|
|
|
}
|
|
|
|
else
|
2015-04-08 12:49:31 +02:00
|
|
|
#endif /* MBEDTLS_SSL_CLI_C */
|
|
|
|
#if defined(MBEDTLS_SSL_SRV_C)
|
2019-05-06 13:48:22 +02:00
|
|
|
if( endpoint == MBEDTLS_SSL_IS_SERVER )
|
2009-01-03 22:22:43 +01:00
|
|
|
{
|
2017-12-27 09:17:40 +01:00
|
|
|
key1 = keyblk + mac_key_len * 2 + keylen;
|
2017-11-09 19:39:33 +01:00
|
|
|
key2 = keyblk + mac_key_len * 2;
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2017-11-09 19:39:33 +01:00
|
|
|
mac_enc = keyblk + mac_key_len;
|
2013-01-07 18:20:04 +01:00
|
|
|
mac_dec = keyblk;
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2010-07-25 16:24:53 +02:00
|
|
|
/*
|
|
|
|
* This is not used in TLS v1.1.
|
|
|
|
*/
|
2012-09-16 21:57:18 +02:00
|
|
|
iv_copy_len = ( transform->fixed_ivlen ) ?
|
|
|
|
transform->fixed_ivlen : transform->ivlen;
|
2017-12-27 09:17:40 +01:00
|
|
|
memcpy( transform->iv_dec, key1 + keylen, iv_copy_len );
|
|
|
|
memcpy( transform->iv_enc, key1 + keylen + iv_copy_len,
|
2012-04-18 16:23:57 +02:00
|
|
|
iv_copy_len );
|
2009-01-03 22:22:43 +01:00
|
|
|
}
|
2014-11-20 18:15:05 +01:00
|
|
|
else
|
2015-04-08 12:49:31 +02:00
|
|
|
#endif /* MBEDTLS_SSL_SRV_C */
|
2014-11-20 18:15:05 +01:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
|
2019-05-07 17:27:13 +02:00
|
|
|
ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
|
|
|
|
goto end;
|
2014-11-20 18:15:05 +01:00
|
|
|
}
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2018-01-03 16:32:51 +01:00
|
|
|
#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_SSL_PROTO_SSL3)
|
2019-05-06 13:48:22 +02:00
|
|
|
if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
|
2013-01-07 18:20:04 +01:00
|
|
|
{
|
2018-01-03 16:32:51 +01:00
|
|
|
if( mac_key_len > sizeof( transform->mac_enc ) )
|
2014-01-18 18:22:55 +01:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
|
2019-05-07 17:27:13 +02:00
|
|
|
ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
|
|
|
|
goto end;
|
2014-01-18 18:22:55 +01:00
|
|
|
}
|
|
|
|
|
2017-11-09 19:39:33 +01:00
|
|
|
memcpy( transform->mac_enc, mac_enc, mac_key_len );
|
|
|
|
memcpy( transform->mac_dec, mac_dec, mac_key_len );
|
2013-01-07 18:20:04 +01:00
|
|
|
}
|
|
|
|
else
|
2015-04-08 12:49:31 +02:00
|
|
|
#endif /* MBEDTLS_SSL_PROTO_SSL3 */
|
|
|
|
#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
|
|
|
|
defined(MBEDTLS_SSL_PROTO_TLS1_2)
|
2019-05-06 13:48:22 +02:00
|
|
|
if( minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
|
2013-01-07 18:20:04 +01:00
|
|
|
{
|
2018-03-19 19:06:08 +01:00
|
|
|
/* For HMAC-based ciphersuites, initialize the HMAC transforms.
|
|
|
|
For AEAD-based ciphersuites, there is nothing to do here. */
|
|
|
|
if( mac_key_len != 0 )
|
|
|
|
{
|
|
|
|
mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, mac_key_len );
|
|
|
|
mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, mac_key_len );
|
|
|
|
}
|
2013-01-07 18:20:04 +01:00
|
|
|
}
|
2013-08-27 21:19:20 +02:00
|
|
|
else
|
|
|
|
#endif
|
2013-08-28 11:57:20 +02:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
|
2019-05-07 17:27:13 +02:00
|
|
|
ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
|
|
|
|
goto end;
|
2013-08-28 11:57:20 +02:00
|
|
|
}
|
2018-01-03 16:32:51 +01:00
|
|
|
#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
|
2013-01-07 18:20:04 +01:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
|
|
|
|
if( mbedtls_ssl_hw_record_init != NULL )
|
2012-05-08 11:17:57 +02:00
|
|
|
{
|
2020-05-06 21:27:14 +02:00
|
|
|
ret = 0;
|
2012-05-08 11:17:57 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_init()" ) );
|
2012-05-08 11:17:57 +02:00
|
|
|
|
2017-12-27 09:17:40 +01:00
|
|
|
if( ( ret = mbedtls_ssl_hw_record_init( ssl, key1, key2, keylen,
|
2012-12-19 14:42:06 +01:00
|
|
|
transform->iv_enc, transform->iv_dec,
|
|
|
|
iv_copy_len,
|
2013-01-07 18:20:04 +01:00
|
|
|
mac_enc, mac_dec,
|
2017-11-09 19:39:33 +01:00
|
|
|
mac_key_len ) ) != 0 )
|
2012-05-08 11:17:57 +02:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_init", ret );
|
2019-05-07 17:27:13 +02:00
|
|
|
ret = MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
|
|
|
|
goto end;
|
2012-05-08 11:17:57 +02:00
|
|
|
}
|
|
|
|
}
|
2018-01-03 16:32:51 +01:00
|
|
|
#else
|
|
|
|
((void) mac_dec);
|
|
|
|
((void) mac_enc);
|
2015-04-08 12:49:31 +02:00
|
|
|
#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
|
2012-05-08 11:17:57 +02:00
|
|
|
|
2015-10-19 13:52:53 +02:00
|
|
|
#if defined(MBEDTLS_SSL_EXPORT_KEYS)
|
|
|
|
if( ssl->conf->f_export_keys != NULL )
|
2015-10-02 14:33:37 +02:00
|
|
|
{
|
2015-10-19 13:52:53 +02:00
|
|
|
ssl->conf->f_export_keys( ssl->conf->p_export_keys,
|
2019-05-10 10:50:04 +02:00
|
|
|
master, keyblk,
|
2017-12-27 09:17:40 +01:00
|
|
|
mac_key_len, keylen,
|
2015-10-02 14:33:37 +02:00
|
|
|
iv_copy_len );
|
|
|
|
}
|
2019-05-07 17:33:40 +02:00
|
|
|
|
|
|
|
if( ssl->conf->f_export_keys_ext != NULL )
|
|
|
|
{
|
|
|
|
ssl->conf->f_export_keys_ext( ssl->conf->p_export_keys,
|
2019-05-10 10:50:04 +02:00
|
|
|
master, keyblk,
|
2019-05-12 10:03:32 +02:00
|
|
|
mac_key_len, keylen,
|
2019-05-12 13:54:30 +02:00
|
|
|
iv_copy_len,
|
2019-09-12 11:47:37 +02:00
|
|
|
randbytes + 32,
|
|
|
|
randbytes,
|
2019-05-06 13:32:17 +02:00
|
|
|
tls_prf_get_type( tls_prf ) );
|
2019-05-07 17:33:40 +02:00
|
|
|
}
|
2015-10-02 14:33:37 +02:00
|
|
|
#endif
|
|
|
|
|
2018-11-16 16:21:18 +01:00
|
|
|
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
2018-11-17 23:27:38 +01:00
|
|
|
|
|
|
|
/* Only use PSA-based ciphers for TLS-1.2.
|
|
|
|
* That's relevant at least for TLS-1.0, where
|
|
|
|
* we assume that mbedtls_cipher_crypt() updates
|
|
|
|
* the structure field for the IV, which the PSA-based
|
|
|
|
* implementation currently doesn't. */
|
|
|
|
#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
|
|
|
|
if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
|
2018-11-16 16:21:18 +01:00
|
|
|
{
|
2018-11-17 23:27:38 +01:00
|
|
|
ret = mbedtls_cipher_setup_psa( &transform->cipher_ctx_enc,
|
2019-04-05 12:21:08 +02:00
|
|
|
cipher_info, transform->taglen );
|
2018-11-17 23:27:38 +01:00
|
|
|
if( ret != 0 && ret != MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE )
|
|
|
|
{
|
|
|
|
MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup_psa", ret );
|
2019-05-07 17:27:13 +02:00
|
|
|
goto end;
|
2018-11-17 23:27:38 +01:00
|
|
|
}
|
2018-11-16 16:21:18 +01:00
|
|
|
|
2018-11-17 23:27:38 +01:00
|
|
|
if( ret == 0 )
|
|
|
|
{
|
2019-04-10 10:25:41 +02:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 3, ( "Successfully setup PSA-based encryption cipher context" ) );
|
2018-11-17 23:27:38 +01:00
|
|
|
psa_fallthrough = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to setup PSA-based cipher context for record encryption - fall through to default setup." ) );
|
|
|
|
psa_fallthrough = 1;
|
|
|
|
}
|
|
|
|
}
|
2018-11-16 16:21:18 +01:00
|
|
|
else
|
2018-11-17 23:27:38 +01:00
|
|
|
psa_fallthrough = 1;
|
|
|
|
#else
|
|
|
|
psa_fallthrough = 1;
|
|
|
|
#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
|
2018-11-16 16:21:18 +01:00
|
|
|
|
2018-11-17 23:27:38 +01:00
|
|
|
if( psa_fallthrough == 1 )
|
2018-11-16 16:21:18 +01:00
|
|
|
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
2015-05-14 13:51:45 +02:00
|
|
|
if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc,
|
2013-10-25 18:42:44 +02:00
|
|
|
cipher_info ) ) != 0 )
|
2009-01-03 22:22:43 +01:00
|
|
|
{
|
2015-05-14 13:51:45 +02:00
|
|
|
MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
|
2019-05-07 17:27:13 +02:00
|
|
|
goto end;
|
2013-10-25 18:42:44 +02:00
|
|
|
}
|
2013-08-31 17:25:14 +02:00
|
|
|
|
2018-11-16 16:21:18 +01:00
|
|
|
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
2018-11-17 23:27:38 +01:00
|
|
|
/* Only use PSA-based ciphers for TLS-1.2.
|
|
|
|
* That's relevant at least for TLS-1.0, where
|
|
|
|
* we assume that mbedtls_cipher_crypt() updates
|
|
|
|
* the structure field for the IV, which the PSA-based
|
|
|
|
* implementation currently doesn't. */
|
|
|
|
#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
|
|
|
|
if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
|
2018-11-16 16:21:18 +01:00
|
|
|
{
|
2018-11-17 23:27:38 +01:00
|
|
|
ret = mbedtls_cipher_setup_psa( &transform->cipher_ctx_dec,
|
2019-04-05 12:21:08 +02:00
|
|
|
cipher_info, transform->taglen );
|
2018-11-17 23:27:38 +01:00
|
|
|
if( ret != 0 && ret != MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE )
|
|
|
|
{
|
|
|
|
MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup_psa", ret );
|
2019-05-07 17:27:13 +02:00
|
|
|
goto end;
|
2018-11-17 23:27:38 +01:00
|
|
|
}
|
2018-11-16 16:21:18 +01:00
|
|
|
|
2018-11-17 23:27:38 +01:00
|
|
|
if( ret == 0 )
|
|
|
|
{
|
2019-04-10 10:25:41 +02:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 3, ( "Successfully setup PSA-based decryption cipher context" ) );
|
2018-11-17 23:27:38 +01:00
|
|
|
psa_fallthrough = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to setup PSA-based cipher context for record decryption - fall through to default setup." ) );
|
|
|
|
psa_fallthrough = 1;
|
|
|
|
}
|
|
|
|
}
|
2018-11-16 16:21:18 +01:00
|
|
|
else
|
2018-11-17 23:27:38 +01:00
|
|
|
psa_fallthrough = 1;
|
|
|
|
#else
|
|
|
|
psa_fallthrough = 1;
|
|
|
|
#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
|
2018-11-16 16:21:18 +01:00
|
|
|
|
2018-11-17 23:27:38 +01:00
|
|
|
if( psa_fallthrough == 1 )
|
2018-11-16 16:21:18 +01:00
|
|
|
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
2015-05-14 13:51:45 +02:00
|
|
|
if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec,
|
2013-10-25 18:42:44 +02:00
|
|
|
cipher_info ) ) != 0 )
|
|
|
|
{
|
2015-05-14 13:51:45 +02:00
|
|
|
MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
|
2019-05-07 17:27:13 +02:00
|
|
|
goto end;
|
2013-10-25 18:42:44 +02:00
|
|
|
}
|
2013-08-31 17:25:14 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1,
|
2015-06-18 15:28:12 +02:00
|
|
|
cipher_info->key_bitlen,
|
2015-04-08 12:49:31 +02:00
|
|
|
MBEDTLS_ENCRYPT ) ) != 0 )
|
2013-10-25 18:42:44 +02:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
|
2019-05-07 17:27:13 +02:00
|
|
|
goto end;
|
2013-10-25 18:42:44 +02:00
|
|
|
}
|
2013-10-25 18:33:32 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2,
|
2015-06-18 15:28:12 +02:00
|
|
|
cipher_info->key_bitlen,
|
2015-04-08 12:49:31 +02:00
|
|
|
MBEDTLS_DECRYPT ) ) != 0 )
|
2013-10-25 18:42:44 +02:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
|
2019-05-07 17:27:13 +02:00
|
|
|
goto end;
|
2013-10-25 18:42:44 +02:00
|
|
|
}
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_CIPHER_MODE_CBC)
|
|
|
|
if( cipher_info->mode == MBEDTLS_MODE_CBC )
|
2013-10-25 18:42:44 +02:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc,
|
|
|
|
MBEDTLS_PADDING_NONE ) ) != 0 )
|
2013-10-25 18:33:32 +02:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
|
2019-05-07 17:27:13 +02:00
|
|
|
goto end;
|
2013-10-25 18:33:32 +02:00
|
|
|
}
|
2013-09-02 14:57:01 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec,
|
|
|
|
MBEDTLS_PADDING_NONE ) ) != 0 )
|
2013-10-25 18:33:32 +02:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
|
2019-05-07 17:27:13 +02:00
|
|
|
goto end;
|
2013-10-25 18:33:32 +02:00
|
|
|
}
|
2009-01-03 22:22:43 +01:00
|
|
|
}
|
2015-04-08 12:49:31 +02:00
|
|
|
#endif /* MBEDTLS_CIPHER_MODE_CBC */
|
2009-01-03 22:22:43 +01:00
|
|
|
|
|
|
|
|
2019-05-06 12:44:24 +02:00
|
|
|
/* Initialize Zlib contexts */
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_ZLIB_SUPPORT)
|
2019-05-10 10:50:04 +02:00
|
|
|
if( compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
|
2012-07-03 15:30:23 +02:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 3, ( "Initializing zlib states" ) );
|
2012-07-03 15:30:23 +02:00
|
|
|
|
2012-09-16 21:57:18 +02:00
|
|
|
memset( &transform->ctx_deflate, 0, sizeof( transform->ctx_deflate ) );
|
|
|
|
memset( &transform->ctx_inflate, 0, sizeof( transform->ctx_inflate ) );
|
2012-07-03 15:30:23 +02:00
|
|
|
|
2014-05-01 14:18:25 +02:00
|
|
|
if( deflateInit( &transform->ctx_deflate,
|
|
|
|
Z_DEFAULT_COMPRESSION ) != Z_OK ||
|
2012-09-16 21:57:18 +02:00
|
|
|
inflateInit( &transform->ctx_inflate ) != Z_OK )
|
2012-07-03 15:30:23 +02:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) );
|
2019-05-07 17:27:13 +02:00
|
|
|
ret = MBEDTLS_ERR_SSL_COMPRESSION_FAILED;
|
|
|
|
goto end;
|
2012-07-03 15:30:23 +02:00
|
|
|
}
|
|
|
|
}
|
2015-04-08 12:49:31 +02:00
|
|
|
#endif /* MBEDTLS_ZLIB_SUPPORT */
|
2012-07-03 15:30:23 +02:00
|
|
|
|
2019-05-07 17:27:13 +02:00
|
|
|
end:
|
2019-05-07 17:29:02 +02:00
|
|
|
mbedtls_platform_zeroize( keyblk, sizeof( keyblk ) );
|
2019-05-07 17:27:13 +02:00
|
|
|
return( ret );
|
2009-01-03 22:22:43 +01:00
|
|
|
}
|
|
|
|
|
2019-04-30 12:08:59 +02:00
|
|
|
/*
|
2019-05-20 10:10:17 +02:00
|
|
|
* Set appropriate PRF function and other SSL / TLS 1.0/1.1 / TLS1.2 functions
|
2019-04-30 12:08:59 +02:00
|
|
|
*
|
|
|
|
* Inputs:
|
|
|
|
* - SSL/TLS minor version
|
|
|
|
* - hash associated with the ciphersuite (only used by TLS 1.2)
|
|
|
|
*
|
2019-05-10 10:25:00 +02:00
|
|
|
* Outputs:
|
2019-04-30 12:08:59 +02:00
|
|
|
* - the tls_prf, calc_verify and calc_finished members of handshake structure
|
|
|
|
*/
|
|
|
|
static int ssl_set_handshake_prfs( mbedtls_ssl_handshake_params *handshake,
|
|
|
|
int minor_ver,
|
|
|
|
mbedtls_md_type_t hash )
|
2019-04-30 11:54:22 +02:00
|
|
|
{
|
2019-04-30 12:08:59 +02:00
|
|
|
#if !defined(MBEDTLS_SSL_PROTO_TLS1_2) || !defined(MBEDTLS_SHA512_C)
|
|
|
|
(void) hash;
|
|
|
|
#endif
|
2019-04-30 11:54:22 +02:00
|
|
|
|
|
|
|
#if defined(MBEDTLS_SSL_PROTO_SSL3)
|
2019-04-30 12:08:59 +02:00
|
|
|
if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
|
2019-04-30 11:54:22 +02:00
|
|
|
{
|
|
|
|
handshake->tls_prf = ssl3_prf;
|
|
|
|
handshake->calc_verify = ssl_calc_verify_ssl;
|
|
|
|
handshake->calc_finished = ssl_calc_finished_ssl;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
|
2019-04-30 12:08:59 +02:00
|
|
|
if( minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
|
2019-04-30 11:54:22 +02:00
|
|
|
{
|
|
|
|
handshake->tls_prf = tls1_prf;
|
|
|
|
handshake->calc_verify = ssl_calc_verify_tls;
|
|
|
|
handshake->calc_finished = ssl_calc_finished_tls;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
|
|
|
|
#if defined(MBEDTLS_SHA512_C)
|
2019-04-30 12:08:59 +02:00
|
|
|
if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
|
|
|
|
hash == MBEDTLS_MD_SHA384 )
|
2019-04-30 11:54:22 +02:00
|
|
|
{
|
|
|
|
handshake->tls_prf = tls_prf_sha384;
|
|
|
|
handshake->calc_verify = ssl_calc_verify_tls_sha384;
|
|
|
|
handshake->calc_finished = ssl_calc_finished_tls_sha384;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
#if defined(MBEDTLS_SHA256_C)
|
2019-04-30 12:08:59 +02:00
|
|
|
if( minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
|
2019-04-30 11:54:22 +02:00
|
|
|
{
|
|
|
|
handshake->tls_prf = tls_prf_sha256;
|
|
|
|
handshake->calc_verify = ssl_calc_verify_tls_sha256;
|
|
|
|
handshake->calc_finished = ssl_calc_finished_tls_sha256;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
|
|
|
|
{
|
|
|
|
return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
|
|
|
|
}
|
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
2019-04-30 12:08:59 +02:00
|
|
|
/*
|
2019-05-03 09:16:16 +02:00
|
|
|
* Compute master secret if needed
|
2019-05-03 09:46:14 +02:00
|
|
|
*
|
|
|
|
* Parameters:
|
|
|
|
* [in/out] handshake
|
|
|
|
* [in] resume, premaster, extended_ms, calc_verify, tls_prf
|
2019-05-03 11:43:28 +02:00
|
|
|
* (PSA-PSK) ciphersuite_info, psk_opaque
|
2019-05-03 09:46:14 +02:00
|
|
|
* [out] premaster (cleared)
|
|
|
|
* [out] master
|
|
|
|
* [in] ssl: optionally used for debugging, EMS and PSA-PSK
|
|
|
|
* debug: conf->f_dbg, conf->p_dbg
|
|
|
|
* EMS: passed to calc_verify (debug + (SSL3) session_negotiate)
|
2019-05-03 11:43:28 +02:00
|
|
|
* PSA-PSA: minor_ver, conf
|
2019-04-30 12:08:59 +02:00
|
|
|
*/
|
2019-05-03 09:46:14 +02:00
|
|
|
static int ssl_compute_master( mbedtls_ssl_handshake_params *handshake,
|
|
|
|
unsigned char *master,
|
2019-05-03 09:58:33 +02:00
|
|
|
const mbedtls_ssl_context *ssl )
|
2019-04-30 12:08:59 +02:00
|
|
|
{
|
2019-12-16 12:46:15 +01:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2019-04-30 12:08:59 +02:00
|
|
|
|
|
|
|
/* cf. RFC 5246, Section 8.1:
|
|
|
|
* "The master secret is always exactly 48 bytes in length." */
|
|
|
|
size_t const master_secret_len = 48;
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
|
|
|
|
unsigned char session_hash[48];
|
|
|
|
#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
|
|
|
|
|
2019-05-03 09:16:16 +02:00
|
|
|
/* The label for the KDF used for key expansion.
|
|
|
|
* This is either "master secret" or "extended master secret"
|
|
|
|
* depending on whether the Extended Master Secret extension
|
|
|
|
* is used. */
|
|
|
|
char const *lbl = "master secret";
|
|
|
|
|
|
|
|
/* The salt for the KDF used for key expansion.
|
|
|
|
* - If the Extended Master Secret extension is not used,
|
|
|
|
* this is ClientHello.Random + ServerHello.Random
|
|
|
|
* (see Sect. 8.1 in RFC 5246).
|
|
|
|
* - If the Extended Master Secret extension is used,
|
|
|
|
* this is the transcript of the handshake so far.
|
|
|
|
* (see Sect. 4 in RFC 7627). */
|
|
|
|
unsigned char const *salt = handshake->randbytes;
|
|
|
|
size_t salt_len = 64;
|
|
|
|
|
2019-05-03 09:46:14 +02:00
|
|
|
#if !defined(MBEDTLS_DEBUG_C) && \
|
|
|
|
!defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) && \
|
|
|
|
!(defined(MBEDTLS_USE_PSA_CRYPTO) && \
|
|
|
|
defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED))
|
2019-05-07 10:17:56 +02:00
|
|
|
ssl = NULL; /* make sure we don't use it except for those cases */
|
2019-05-03 09:46:14 +02:00
|
|
|
(void) ssl;
|
|
|
|
#endif
|
|
|
|
|
2019-04-30 12:08:59 +02:00
|
|
|
if( handshake->resume != 0 )
|
|
|
|
{
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
|
2019-05-03 09:16:16 +02:00
|
|
|
return( 0 );
|
2019-04-30 12:08:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
|
2019-05-03 09:46:14 +02:00
|
|
|
if( handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED )
|
2019-05-03 09:16:16 +02:00
|
|
|
{
|
|
|
|
lbl = "extended master secret";
|
|
|
|
salt = session_hash;
|
2019-05-03 11:43:28 +02:00
|
|
|
handshake->calc_verify( ssl, session_hash, &salt_len );
|
2019-04-30 12:08:59 +02:00
|
|
|
|
2019-05-20 12:09:50 +02:00
|
|
|
MBEDTLS_SSL_DEBUG_BUF( 3, "session hash for extended master secret",
|
|
|
|
session_hash, salt_len );
|
2019-05-03 09:16:16 +02:00
|
|
|
}
|
2019-04-30 12:08:59 +02:00
|
|
|
#endif /* MBEDTLS_SSL_EXTENDED_MS_ENABLED */
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
|
2019-05-03 09:46:14 +02:00
|
|
|
defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
|
|
|
|
if( handshake->ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK &&
|
2019-05-03 11:43:28 +02:00
|
|
|
ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
|
2019-05-03 09:16:16 +02:00
|
|
|
ssl_use_opaque_psk( ssl ) == 1 )
|
|
|
|
{
|
|
|
|
/* Perform PSK-to-MS expansion in a single step. */
|
|
|
|
psa_status_t status;
|
|
|
|
psa_algorithm_t alg;
|
|
|
|
psa_key_handle_t psk;
|
|
|
|
psa_key_derivation_operation_t derivation =
|
|
|
|
PSA_KEY_DERIVATION_OPERATION_INIT;
|
2019-05-03 11:43:28 +02:00
|
|
|
mbedtls_md_type_t hash_alg = handshake->ciphersuite_info->mac;
|
2019-04-30 12:08:59 +02:00
|
|
|
|
2019-05-03 09:16:16 +02:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 2, ( "perform PSA-based PSK-to-MS expansion" ) );
|
2019-04-30 12:08:59 +02:00
|
|
|
|
2020-03-25 18:08:15 +01:00
|
|
|
psk = mbedtls_ssl_get_opaque_psk( ssl );
|
2019-04-30 12:08:59 +02:00
|
|
|
|
2019-05-03 09:46:14 +02:00
|
|
|
if( hash_alg == MBEDTLS_MD_SHA384 )
|
2019-05-03 09:16:16 +02:00
|
|
|
alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384);
|
|
|
|
else
|
|
|
|
alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256);
|
|
|
|
|
2019-08-17 10:30:28 +02:00
|
|
|
status = setup_psa_key_derivation( &derivation, psk, alg,
|
|
|
|
salt, salt_len,
|
|
|
|
(unsigned char const *) lbl,
|
|
|
|
(size_t) strlen( lbl ),
|
|
|
|
master_secret_len );
|
2019-05-03 09:16:16 +02:00
|
|
|
if( status != PSA_SUCCESS )
|
|
|
|
{
|
|
|
|
psa_key_derivation_abort( &derivation );
|
|
|
|
return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
|
|
|
|
}
|
2019-04-30 12:08:59 +02:00
|
|
|
|
2019-05-03 09:16:16 +02:00
|
|
|
status = psa_key_derivation_output_bytes( &derivation,
|
2019-05-03 09:46:14 +02:00
|
|
|
master,
|
2019-05-03 09:16:16 +02:00
|
|
|
master_secret_len );
|
|
|
|
if( status != PSA_SUCCESS )
|
|
|
|
{
|
|
|
|
psa_key_derivation_abort( &derivation );
|
|
|
|
return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
|
2019-04-30 12:08:59 +02:00
|
|
|
}
|
2019-05-03 09:16:16 +02:00
|
|
|
|
|
|
|
status = psa_key_derivation_abort( &derivation );
|
|
|
|
if( status != PSA_SUCCESS )
|
|
|
|
return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
|
|
|
|
}
|
|
|
|
else
|
2019-04-30 12:08:59 +02:00
|
|
|
#endif
|
2019-05-03 09:16:16 +02:00
|
|
|
{
|
|
|
|
ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
|
|
|
|
lbl, salt, salt_len,
|
2019-05-03 09:46:14 +02:00
|
|
|
master,
|
2019-05-03 09:16:16 +02:00
|
|
|
master_secret_len );
|
|
|
|
if( ret != 0 )
|
2019-04-30 12:08:59 +02:00
|
|
|
{
|
2019-05-03 09:16:16 +02:00
|
|
|
MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
|
|
|
|
return( ret );
|
|
|
|
}
|
2019-04-30 12:08:59 +02:00
|
|
|
|
2019-05-03 09:16:16 +02:00
|
|
|
MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret",
|
|
|
|
handshake->premaster,
|
|
|
|
handshake->pmslen );
|
2019-04-30 12:08:59 +02:00
|
|
|
|
2019-05-03 09:16:16 +02:00
|
|
|
mbedtls_platform_zeroize( handshake->premaster,
|
|
|
|
sizeof(handshake->premaster) );
|
2019-04-30 12:08:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
2019-04-30 11:54:22 +02:00
|
|
|
|
2019-04-30 11:41:40 +02:00
|
|
|
int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl )
|
|
|
|
{
|
2019-12-16 12:46:15 +01:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2019-04-30 12:08:59 +02:00
|
|
|
const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
|
|
|
|
ssl->handshake->ciphersuite_info;
|
2019-04-30 11:54:22 +02:00
|
|
|
|
2019-05-06 12:05:58 +02:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
|
|
|
|
|
|
|
|
/* Set PRF, calc_verify and calc_finished function pointers */
|
2019-04-30 12:08:59 +02:00
|
|
|
ret = ssl_set_handshake_prfs( ssl->handshake,
|
|
|
|
ssl->minor_ver,
|
|
|
|
ciphersuite_info->mac );
|
2019-04-30 11:54:22 +02:00
|
|
|
if( ret != 0 )
|
2019-04-30 12:08:59 +02:00
|
|
|
{
|
|
|
|
MBEDTLS_SSL_DEBUG_RET( 1, "ssl_set_handshake_prfs", ret );
|
2019-04-30 11:54:22 +02:00
|
|
|
return( ret );
|
2019-04-30 12:08:59 +02:00
|
|
|
}
|
2019-04-30 11:54:22 +02:00
|
|
|
|
2019-05-06 12:05:58 +02:00
|
|
|
/* Compute master secret if needed */
|
2019-05-03 09:46:14 +02:00
|
|
|
ret = ssl_compute_master( ssl->handshake,
|
|
|
|
ssl->session_negotiate->master,
|
|
|
|
ssl );
|
2019-04-30 12:08:59 +02:00
|
|
|
if( ret != 0 )
|
|
|
|
{
|
|
|
|
MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compute_master", ret );
|
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
|
2019-05-06 12:05:58 +02:00
|
|
|
/* Swap the client and server random values:
|
|
|
|
* - MS derivation wanted client+server (RFC 5246 8.1)
|
|
|
|
* - key derivation wants server+client (RFC 5246 6.3) */
|
|
|
|
{
|
|
|
|
unsigned char tmp[64];
|
|
|
|
memcpy( tmp, ssl->handshake->randbytes, 64 );
|
|
|
|
memcpy( ssl->handshake->randbytes, tmp + 32, 32 );
|
|
|
|
memcpy( ssl->handshake->randbytes + 32, tmp, 32 );
|
|
|
|
mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Populate transform structure */
|
2019-05-06 12:55:40 +02:00
|
|
|
ret = ssl_populate_transform( ssl->transform_negotiate,
|
2019-05-10 10:50:04 +02:00
|
|
|
ssl->session_negotiate->ciphersuite,
|
|
|
|
ssl->session_negotiate->master,
|
2019-08-16 11:06:56 +02:00
|
|
|
#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
|
2019-05-10 10:50:04 +02:00
|
|
|
#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
|
|
|
|
ssl->session_negotiate->encrypt_then_mac,
|
2019-08-16 11:06:56 +02:00
|
|
|
#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
|
2019-05-10 10:50:04 +02:00
|
|
|
#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
|
|
|
|
ssl->session_negotiate->trunc_hmac,
|
2019-08-16 11:06:56 +02:00
|
|
|
#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
|
|
|
|
#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
|
2019-05-10 10:50:04 +02:00
|
|
|
#if defined(MBEDTLS_ZLIB_SUPPORT)
|
|
|
|
ssl->session_negotiate->compression,
|
|
|
|
#endif
|
2019-05-06 13:32:17 +02:00
|
|
|
ssl->handshake->tls_prf,
|
|
|
|
ssl->handshake->randbytes,
|
2019-05-06 13:48:22 +02:00
|
|
|
ssl->minor_ver,
|
|
|
|
ssl->conf->endpoint,
|
2019-05-06 12:55:40 +02:00
|
|
|
ssl );
|
2019-05-06 12:05:58 +02:00
|
|
|
if( ret != 0 )
|
|
|
|
{
|
|
|
|
MBEDTLS_SSL_DEBUG_RET( 1, "ssl_populate_transform", ret );
|
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
|
|
|
|
/* We no longer need Server/ClientHello.random values */
|
|
|
|
mbedtls_platform_zeroize( ssl->handshake->randbytes,
|
|
|
|
sizeof( ssl->handshake->randbytes ) );
|
|
|
|
|
2019-05-06 12:44:24 +02:00
|
|
|
/* Allocate compression buffer */
|
|
|
|
#if defined(MBEDTLS_ZLIB_SUPPORT)
|
2019-12-19 06:40:01 +01:00
|
|
|
if( ssl->session_negotiate->compression == MBEDTLS_SSL_COMPRESS_DEFLATE &&
|
2019-05-06 12:44:24 +02:00
|
|
|
ssl->compress_buf == NULL )
|
|
|
|
{
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 3, ( "Allocating compression buffer" ) );
|
|
|
|
ssl->compress_buf = mbedtls_calloc( 1, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
|
|
|
|
if( ssl->compress_buf == NULL )
|
|
|
|
{
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
|
2019-05-20 10:27:20 +02:00
|
|
|
MBEDTLS_SSL_COMPRESS_BUFFER_LEN ) );
|
2019-05-06 12:44:24 +02:00
|
|
|
return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2019-05-06 12:05:58 +02:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
|
|
|
|
|
|
|
|
return( 0 );
|
2019-04-30 11:41:40 +02:00
|
|
|
}
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_SSL_PROTO_SSL3)
|
2019-05-03 11:43:28 +02:00
|
|
|
void ssl_calc_verify_ssl( const mbedtls_ssl_context *ssl,
|
|
|
|
unsigned char hash[36],
|
|
|
|
size_t *hlen )
|
2009-01-03 22:22:43 +01:00
|
|
|
{
|
2015-07-06 16:11:18 +02:00
|
|
|
mbedtls_md5_context md5;
|
|
|
|
mbedtls_sha1_context sha1;
|
2009-01-03 22:22:43 +01:00
|
|
|
unsigned char pad_1[48];
|
|
|
|
unsigned char pad_2[48];
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) );
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2015-07-06 16:21:13 +02:00
|
|
|
mbedtls_md5_init( &md5 );
|
|
|
|
mbedtls_sha1_init( &sha1 );
|
|
|
|
|
|
|
|
mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
|
|
|
|
mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2012-04-18 18:10:25 +02:00
|
|
|
memset( pad_1, 0x36, 48 );
|
|
|
|
memset( pad_2, 0x5C, 48 );
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2018-01-22 11:48:08 +01:00
|
|
|
mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
|
|
|
|
mbedtls_md5_update_ret( &md5, pad_1, 48 );
|
|
|
|
mbedtls_md5_finish_ret( &md5, hash );
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2018-01-22 11:48:08 +01:00
|
|
|
mbedtls_md5_starts_ret( &md5 );
|
|
|
|
mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
|
|
|
|
mbedtls_md5_update_ret( &md5, pad_2, 48 );
|
|
|
|
mbedtls_md5_update_ret( &md5, hash, 16 );
|
|
|
|
mbedtls_md5_finish_ret( &md5, hash );
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2018-01-22 11:48:08 +01:00
|
|
|
mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
|
|
|
|
mbedtls_sha1_update_ret( &sha1, pad_1, 40 );
|
|
|
|
mbedtls_sha1_finish_ret( &sha1, hash + 16 );
|
2012-04-18 18:10:25 +02:00
|
|
|
|
2018-01-22 11:48:08 +01:00
|
|
|
mbedtls_sha1_starts_ret( &sha1 );
|
|
|
|
mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
|
|
|
|
mbedtls_sha1_update_ret( &sha1, pad_2, 40 );
|
|
|
|
mbedtls_sha1_update_ret( &sha1, hash + 16, 20 );
|
|
|
|
mbedtls_sha1_finish_ret( &sha1, hash + 16 );
|
2012-04-18 18:10:25 +02:00
|
|
|
|
2019-05-03 11:43:28 +02:00
|
|
|
*hlen = 36;
|
|
|
|
|
|
|
|
MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
|
2015-04-08 12:49:31 +02:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
|
2012-04-18 18:10:25 +02:00
|
|
|
|
2015-07-06 16:11:18 +02:00
|
|
|
mbedtls_md5_free( &md5 );
|
|
|
|
mbedtls_sha1_free( &sha1 );
|
2014-06-26 12:09:34 +02:00
|
|
|
|
2012-04-18 18:10:25 +02:00
|
|
|
return;
|
|
|
|
}
|
2015-04-08 12:49:31 +02:00
|
|
|
#endif /* MBEDTLS_SSL_PROTO_SSL3 */
|
2012-04-18 18:10:25 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
|
2019-05-03 11:43:28 +02:00
|
|
|
void ssl_calc_verify_tls( const mbedtls_ssl_context *ssl,
|
|
|
|
unsigned char hash[36],
|
|
|
|
size_t *hlen )
|
2012-04-18 18:10:25 +02:00
|
|
|
{
|
2015-07-06 16:11:18 +02:00
|
|
|
mbedtls_md5_context md5;
|
|
|
|
mbedtls_sha1_context sha1;
|
2012-04-18 18:10:25 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) );
|
2012-04-18 18:10:25 +02:00
|
|
|
|
2015-07-06 16:21:13 +02:00
|
|
|
mbedtls_md5_init( &md5 );
|
|
|
|
mbedtls_sha1_init( &sha1 );
|
|
|
|
|
|
|
|
mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
|
|
|
|
mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
|
2012-04-18 18:10:25 +02:00
|
|
|
|
2019-01-29 15:14:33 +01:00
|
|
|
mbedtls_md5_finish_ret( &md5, hash );
|
2018-01-22 11:48:08 +01:00
|
|
|
mbedtls_sha1_finish_ret( &sha1, hash + 16 );
|
2012-04-18 18:10:25 +02:00
|
|
|
|
2019-05-03 11:43:28 +02:00
|
|
|
*hlen = 36;
|
|
|
|
|
|
|
|
MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
|
2015-04-08 12:49:31 +02:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
|
2012-04-18 18:10:25 +02:00
|
|
|
|
2015-07-06 16:11:18 +02:00
|
|
|
mbedtls_md5_free( &md5 );
|
|
|
|
mbedtls_sha1_free( &sha1 );
|
2014-06-26 12:09:34 +02:00
|
|
|
|
2012-04-18 18:10:25 +02:00
|
|
|
return;
|
|
|
|
}
|
2015-04-08 12:49:31 +02:00
|
|
|
#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
|
2012-04-18 18:10:25 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
|
|
|
|
#if defined(MBEDTLS_SHA256_C)
|
2019-05-03 11:43:28 +02:00
|
|
|
void ssl_calc_verify_tls_sha256( const mbedtls_ssl_context *ssl,
|
|
|
|
unsigned char hash[32],
|
|
|
|
size_t *hlen )
|
2012-04-18 18:10:25 +02:00
|
|
|
{
|
2019-01-29 15:14:33 +01:00
|
|
|
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
|
|
|
size_t hash_size;
|
|
|
|
psa_status_t status;
|
|
|
|
psa_hash_operation_t sha256_psa = psa_hash_operation_init();
|
|
|
|
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha256" ) );
|
|
|
|
status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
|
|
|
|
if( status != PSA_SUCCESS )
|
|
|
|
{
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
status = psa_hash_finish( &sha256_psa, hash, 32, &hash_size );
|
|
|
|
if( status != PSA_SUCCESS )
|
|
|
|
{
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
|
|
|
|
return;
|
|
|
|
}
|
2019-05-03 11:43:28 +02:00
|
|
|
|
|
|
|
*hlen = 32;
|
|
|
|
MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, *hlen );
|
2019-01-29 15:14:33 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
|
|
|
|
#else
|
2015-07-06 16:11:18 +02:00
|
|
|
mbedtls_sha256_context sha256;
|
2012-04-18 18:10:25 +02:00
|
|
|
|
2015-07-06 16:21:13 +02:00
|
|
|
mbedtls_sha256_init( &sha256 );
|
|
|
|
|
2015-07-06 16:11:18 +02:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
|
2012-04-18 18:10:25 +02:00
|
|
|
|
2015-07-06 16:21:13 +02:00
|
|
|
mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
|
2018-01-22 11:48:08 +01:00
|
|
|
mbedtls_sha256_finish_ret( &sha256, hash );
|
2012-04-18 18:10:25 +02:00
|
|
|
|
2019-05-03 11:43:28 +02:00
|
|
|
*hlen = 32;
|
|
|
|
|
|
|
|
MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
|
2015-04-08 12:49:31 +02:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
|
2012-04-18 18:10:25 +02:00
|
|
|
|
2015-07-06 16:11:18 +02:00
|
|
|
mbedtls_sha256_free( &sha256 );
|
2019-01-29 15:14:33 +01:00
|
|
|
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
2012-04-18 18:10:25 +02:00
|
|
|
return;
|
|
|
|
}
|
2015-04-08 12:49:31 +02:00
|
|
|
#endif /* MBEDTLS_SHA256_C */
|
2012-04-18 18:10:25 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_SHA512_C)
|
2019-05-03 11:43:28 +02:00
|
|
|
void ssl_calc_verify_tls_sha384( const mbedtls_ssl_context *ssl,
|
|
|
|
unsigned char hash[48],
|
|
|
|
size_t *hlen )
|
2012-04-18 18:10:25 +02:00
|
|
|
{
|
2019-01-29 15:14:33 +01:00
|
|
|
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
|
|
|
size_t hash_size;
|
|
|
|
psa_status_t status;
|
2019-01-30 09:29:12 +01:00
|
|
|
psa_hash_operation_t sha384_psa = psa_hash_operation_init();
|
2019-01-29 15:14:33 +01:00
|
|
|
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha384" ) );
|
2019-01-30 09:29:12 +01:00
|
|
|
status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
|
2019-01-29 15:14:33 +01:00
|
|
|
if( status != PSA_SUCCESS )
|
|
|
|
{
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-01-30 09:29:12 +01:00
|
|
|
status = psa_hash_finish( &sha384_psa, hash, 48, &hash_size );
|
2019-01-29 15:14:33 +01:00
|
|
|
if( status != PSA_SUCCESS )
|
|
|
|
{
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
|
|
|
|
return;
|
|
|
|
}
|
2019-05-03 11:43:28 +02:00
|
|
|
|
|
|
|
*hlen = 48;
|
|
|
|
MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, *hlen );
|
2019-01-29 15:14:33 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
|
|
|
|
#else
|
2015-07-06 16:11:18 +02:00
|
|
|
mbedtls_sha512_context sha512;
|
2012-04-18 18:10:25 +02:00
|
|
|
|
2015-07-06 16:21:13 +02:00
|
|
|
mbedtls_sha512_init( &sha512 );
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
|
2012-04-18 18:10:25 +02:00
|
|
|
|
2015-07-06 16:11:18 +02:00
|
|
|
mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
|
2018-01-22 11:48:08 +01:00
|
|
|
mbedtls_sha512_finish_ret( &sha512, hash );
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2019-05-03 11:43:28 +02:00
|
|
|
*hlen = 48;
|
|
|
|
|
|
|
|
MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, *hlen );
|
2015-04-08 12:49:31 +02:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2015-07-06 16:11:18 +02:00
|
|
|
mbedtls_sha512_free( &sha512 );
|
2019-01-29 15:14:33 +01:00
|
|
|
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
2009-01-03 22:22:43 +01:00
|
|
|
return;
|
|
|
|
}
|
2015-04-08 12:49:31 +02:00
|
|
|
#endif /* MBEDTLS_SHA512_C */
|
|
|
|
#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2020-03-10 12:19:08 +01:00
|
|
|
#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
|
2015-04-08 12:49:31 +02:00
|
|
|
int mbedtls_ssl_psk_derive_premaster( mbedtls_ssl_context *ssl, mbedtls_key_exchange_type_t key_ex )
|
2013-10-14 13:09:25 +02:00
|
|
|
{
|
|
|
|
unsigned char *p = ssl->handshake->premaster;
|
|
|
|
unsigned char *end = p + sizeof( ssl->handshake->premaster );
|
2020-04-01 12:23:58 +02:00
|
|
|
const unsigned char *psk = NULL;
|
2020-03-27 12:12:12 +01:00
|
|
|
size_t psk_len = 0;
|
2015-05-07 16:59:54 +02:00
|
|
|
|
2020-03-25 18:08:15 +01:00
|
|
|
if( mbedtls_ssl_get_psk( ssl, &psk, &psk_len )
|
|
|
|
== MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED )
|
2015-05-07 16:59:54 +02:00
|
|
|
{
|
2020-03-25 18:08:15 +01:00
|
|
|
/*
|
|
|
|
* This should never happen because the existence of a PSK is always
|
|
|
|
* checked before calling this function
|
|
|
|
*/
|
2020-03-26 18:04:31 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
|
2020-04-08 12:02:38 +02:00
|
|
|
return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
|
2015-05-07 16:59:54 +02:00
|
|
|
}
|
2013-10-14 13:09:25 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* PMS = struct {
|
|
|
|
* opaque other_secret<0..2^16-1>;
|
|
|
|
* opaque psk<0..2^16-1>;
|
|
|
|
* };
|
|
|
|
* with "other_secret" depending on the particular key exchange
|
|
|
|
*/
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
|
|
|
|
if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK )
|
2013-10-14 13:09:25 +02:00
|
|
|
{
|
2015-10-21 12:35:29 +02:00
|
|
|
if( end - p < 2 )
|
2015-04-08 12:49:31 +02:00
|
|
|
return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
|
2013-10-14 13:09:25 +02:00
|
|
|
|
2015-05-07 16:59:54 +02:00
|
|
|
*(p++) = (unsigned char)( psk_len >> 8 );
|
|
|
|
*(p++) = (unsigned char)( psk_len );
|
2015-10-21 12:35:29 +02:00
|
|
|
|
|
|
|
if( end < p || (size_t)( end - p ) < psk_len )
|
|
|
|
return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
|
|
|
|
|
|
|
|
memset( p, 0, psk_len );
|
2015-05-07 16:59:54 +02:00
|
|
|
p += psk_len;
|
2013-10-14 13:09:25 +02:00
|
|
|
}
|
|
|
|
else
|
2015-04-08 12:49:31 +02:00
|
|
|
#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
|
|
|
|
#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
|
|
|
|
if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
|
2013-10-14 17:39:48 +02:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
* other_secret already set by the ClientKeyExchange message,
|
|
|
|
* and is 48 bytes long
|
|
|
|
*/
|
2018-05-30 09:13:21 +02:00
|
|
|
if( end - p < 2 )
|
|
|
|
return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
|
|
|
|
|
2013-10-14 17:39:48 +02:00
|
|
|
*p++ = 0;
|
|
|
|
*p++ = 48;
|
|
|
|
p += 48;
|
|
|
|
}
|
|
|
|
else
|
2015-04-08 12:49:31 +02:00
|
|
|
#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
|
|
|
|
#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
|
|
|
|
if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
|
2013-10-14 13:09:25 +02:00
|
|
|
{
|
2019-12-16 12:46:15 +01:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2015-06-02 17:17:08 +02:00
|
|
|
size_t len;
|
2013-10-14 13:09:25 +02:00
|
|
|
|
2014-06-23 17:56:08 +02:00
|
|
|
/* Write length only when we know the actual value */
|
2015-04-08 12:49:31 +02:00
|
|
|
if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
|
2015-06-02 17:17:08 +02:00
|
|
|
p + 2, end - ( p + 2 ), &len,
|
2015-05-07 13:35:38 +02:00
|
|
|
ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
|
2013-10-14 13:09:25 +02:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
|
2013-10-14 13:09:25 +02:00
|
|
|
return( ret );
|
|
|
|
}
|
2014-06-23 17:56:08 +02:00
|
|
|
*(p++) = (unsigned char)( len >> 8 );
|
|
|
|
*(p++) = (unsigned char)( len );
|
2013-10-14 13:09:25 +02:00
|
|
|
p += len;
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
|
2013-10-14 13:09:25 +02:00
|
|
|
}
|
|
|
|
else
|
2015-04-08 12:49:31 +02:00
|
|
|
#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
|
|
|
|
#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
|
|
|
|
if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
|
2013-10-14 13:09:25 +02:00
|
|
|
{
|
2019-12-16 12:46:15 +01:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2013-10-14 13:09:25 +02:00
|
|
|
size_t zlen;
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
|
2014-06-17 16:39:18 +02:00
|
|
|
p + 2, end - ( p + 2 ),
|
2015-05-07 13:35:38 +02:00
|
|
|
ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
|
2013-10-14 13:09:25 +02:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
|
2013-10-14 13:09:25 +02:00
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
|
|
|
|
*(p++) = (unsigned char)( zlen >> 8 );
|
|
|
|
*(p++) = (unsigned char)( zlen );
|
|
|
|
p += zlen;
|
|
|
|
|
2019-01-31 14:20:20 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
|
|
|
|
MBEDTLS_DEBUG_ECDH_Z );
|
2013-10-14 13:09:25 +02:00
|
|
|
}
|
|
|
|
else
|
2015-04-08 12:49:31 +02:00
|
|
|
#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
|
2013-10-14 13:09:25 +02:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
|
|
|
|
return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
|
2013-10-14 13:09:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* opaque psk<0..2^16-1>; */
|
2015-10-21 12:35:29 +02:00
|
|
|
if( end - p < 2 )
|
|
|
|
return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
|
2014-03-25 16:28:12 +01:00
|
|
|
|
2015-05-07 16:59:54 +02:00
|
|
|
*(p++) = (unsigned char)( psk_len >> 8 );
|
|
|
|
*(p++) = (unsigned char)( psk_len );
|
2015-10-21 12:35:29 +02:00
|
|
|
|
|
|
|
if( end < p || (size_t)( end - p ) < psk_len )
|
|
|
|
return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
|
|
|
|
|
2015-05-07 16:59:54 +02:00
|
|
|
memcpy( p, psk, psk_len );
|
|
|
|
p += psk_len;
|
2013-10-14 13:09:25 +02:00
|
|
|
|
|
|
|
ssl->handshake->pmslen = p - ssl->handshake->premaster;
|
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
2020-03-10 12:19:08 +01:00
|
|
|
#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
|
2013-10-14 13:09:25 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
|
|
|
|
static int ssl_write_hello_request( mbedtls_ssl_context *ssl );
|
2018-06-28 10:38:35 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
#if defined(MBEDTLS_SSL_PROTO_DTLS)
|
|
|
|
int mbedtls_ssl_resend_hello_request( mbedtls_ssl_context *ssl )
|
|
|
|
{
|
|
|
|
/* If renegotiation is not enforced, retransmit until we would reach max
|
|
|
|
* timeout if we were using the usual handshake doubling scheme */
|
|
|
|
if( ssl->conf->renego_max_records < 0 )
|
|
|
|
{
|
|
|
|
uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
|
|
|
|
unsigned char doublings = 1;
|
2017-12-27 22:37:21 +01:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
while( ratio != 0 )
|
|
|
|
{
|
|
|
|
++doublings;
|
|
|
|
ratio >>= 1;
|
|
|
|
}
|
2019-04-29 18:31:37 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
if( ++ssl->renego_records_seen > doublings )
|
|
|
|
{
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) );
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
}
|
2019-04-29 18:31:37 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
return( ssl_write_hello_request( ssl ) );
|
2019-04-29 18:31:37 +02:00
|
|
|
}
|
2020-02-05 15:44:10 +01:00
|
|
|
#endif
|
|
|
|
#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
|
2019-04-29 18:31:37 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
#if defined(MBEDTLS_X509_CRT_PARSE_C)
|
|
|
|
static void ssl_clear_peer_cert( mbedtls_ssl_session *session )
|
2019-04-29 18:31:37 +02:00
|
|
|
{
|
2020-02-05 15:44:10 +01:00
|
|
|
#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
|
|
|
|
if( session->peer_cert != NULL )
|
2019-04-29 18:31:37 +02:00
|
|
|
{
|
2020-02-05 15:44:10 +01:00
|
|
|
mbedtls_x509_crt_free( session->peer_cert );
|
|
|
|
mbedtls_free( session->peer_cert );
|
|
|
|
session->peer_cert = NULL;
|
|
|
|
}
|
|
|
|
#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
|
|
|
|
if( session->peer_cert_digest != NULL )
|
|
|
|
{
|
|
|
|
/* Zeroization is not necessary. */
|
|
|
|
mbedtls_free( session->peer_cert_digest );
|
|
|
|
session->peer_cert_digest = NULL;
|
|
|
|
session->peer_cert_digest_type = MBEDTLS_MD_NONE;
|
|
|
|
session->peer_cert_digest_len = 0;
|
|
|
|
}
|
|
|
|
#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
|
2019-04-29 18:31:37 +02:00
|
|
|
}
|
2020-02-05 15:44:10 +01:00
|
|
|
#endif /* MBEDTLS_X509_CRT_PARSE_C */
|
2019-04-29 18:31:37 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
/*
|
|
|
|
* Handshake functions
|
|
|
|
*/
|
2020-03-10 12:19:08 +01:00
|
|
|
#if !defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
|
2020-02-05 15:44:10 +01:00
|
|
|
/* No certificate support -> dummy functions */
|
|
|
|
int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
|
2017-12-27 22:37:21 +01:00
|
|
|
{
|
2020-02-05 15:44:10 +01:00
|
|
|
const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
|
|
|
|
ssl->handshake->ciphersuite_info;
|
2019-04-29 14:52:53 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
|
2019-04-29 14:52:53 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
|
2019-05-09 12:38:24 +02:00
|
|
|
{
|
2020-02-05 15:44:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
|
|
|
|
ssl->state++;
|
|
|
|
return( 0 );
|
2019-05-09 12:38:24 +02:00
|
|
|
}
|
2020-02-05 15:44:10 +01:00
|
|
|
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
|
|
|
|
return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
|
2017-12-27 22:37:21 +01:00
|
|
|
}
|
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
|
2009-01-03 22:22:43 +01:00
|
|
|
{
|
2020-02-05 15:44:10 +01:00
|
|
|
const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
|
|
|
|
ssl->handshake->ciphersuite_info;
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
|
2017-12-27 22:37:21 +01:00
|
|
|
{
|
2020-02-05 15:44:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
|
|
|
|
ssl->state++;
|
|
|
|
return( 0 );
|
2015-01-13 10:59:51 +01:00
|
|
|
}
|
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
|
|
|
|
return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
|
|
|
|
}
|
|
|
|
|
2020-03-10 12:19:08 +01:00
|
|
|
#else /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
|
2020-02-05 15:44:10 +01:00
|
|
|
/* Some certificate support -> implement write and parse */
|
|
|
|
|
|
|
|
int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
|
|
|
|
{
|
|
|
|
int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
|
|
|
|
size_t i, n;
|
|
|
|
const mbedtls_x509_crt *crt;
|
|
|
|
const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
|
|
|
|
ssl->handshake->ciphersuite_info;
|
2017-12-27 22:37:21 +01:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
|
2017-12-27 22:37:21 +01:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
|
2017-12-27 22:37:21 +01:00
|
|
|
{
|
2020-02-05 15:44:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
|
|
|
|
ssl->state++;
|
|
|
|
return( 0 );
|
2017-12-27 22:37:21 +01:00
|
|
|
}
|
2014-11-21 11:38:37 +01:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
#if defined(MBEDTLS_SSL_CLI_C)
|
|
|
|
if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
|
2019-04-29 18:31:37 +02:00
|
|
|
{
|
2020-02-05 15:44:10 +01:00
|
|
|
if( ssl->client_auth == 0 )
|
2019-04-29 18:31:37 +02:00
|
|
|
{
|
2020-02-05 15:44:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
|
|
|
|
ssl->state++;
|
|
|
|
return( 0 );
|
2017-12-27 22:37:21 +01:00
|
|
|
}
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_SSL_PROTO_SSL3)
|
2020-02-05 15:44:10 +01:00
|
|
|
/*
|
|
|
|
* If using SSLv3 and got no cert, send an Alert message
|
|
|
|
* (otherwise an empty Certificate message will be sent).
|
|
|
|
*/
|
|
|
|
if( mbedtls_ssl_own_cert( ssl ) == NULL &&
|
|
|
|
ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
|
2013-10-25 19:31:25 +02:00
|
|
|
{
|
2020-02-05 15:44:10 +01:00
|
|
|
ssl->out_msglen = 2;
|
|
|
|
ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
|
|
|
|
ssl->out_msg[0] = MBEDTLS_SSL_ALERT_LEVEL_WARNING;
|
|
|
|
ssl->out_msg[1] = MBEDTLS_SSL_ALERT_MSG_NO_CERT;
|
2017-11-09 19:57:39 +01:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
|
|
|
|
goto write_msg;
|
2013-10-25 19:31:25 +02:00
|
|
|
}
|
2020-02-05 15:44:10 +01:00
|
|
|
#endif /* MBEDTLS_SSL_PROTO_SSL3 */
|
2013-10-25 19:31:25 +02:00
|
|
|
}
|
2020-02-05 15:44:10 +01:00
|
|
|
#endif /* MBEDTLS_SSL_CLI_C */
|
|
|
|
#if defined(MBEDTLS_SSL_SRV_C)
|
|
|
|
if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
|
2009-01-03 22:22:43 +01:00
|
|
|
{
|
2020-02-05 15:44:10 +01:00
|
|
|
if( mbedtls_ssl_own_cert( ssl ) == NULL )
|
2013-09-02 14:57:01 +02:00
|
|
|
{
|
2020-02-05 15:44:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
|
|
|
|
return( MBEDTLS_ERR_SSL_CERTIFICATE_REQUIRED );
|
2013-09-02 14:57:01 +02:00
|
|
|
}
|
2009-01-03 22:22:43 +01:00
|
|
|
}
|
2020-02-05 15:44:10 +01:00
|
|
|
#endif
|
2017-12-27 22:37:21 +01:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
|
2013-09-05 16:10:41 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
/*
|
|
|
|
* 0 . 0 handshake type
|
|
|
|
* 1 . 3 handshake length
|
|
|
|
* 4 . 6 length of all certs
|
|
|
|
* 7 . 9 length of cert. 1
|
|
|
|
* 10 . n-1 peer certificate
|
|
|
|
* n . n+2 length of cert. 2
|
|
|
|
* n+3 . ... upper level cert, etc.
|
|
|
|
*/
|
|
|
|
i = 7;
|
|
|
|
crt = mbedtls_ssl_own_cert( ssl );
|
2013-09-05 16:10:41 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
while( crt != NULL )
|
2009-01-03 22:22:43 +01:00
|
|
|
{
|
2020-02-05 15:44:10 +01:00
|
|
|
n = crt->raw.len;
|
|
|
|
if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
|
2017-12-27 22:37:21 +01:00
|
|
|
{
|
2020-02-05 15:44:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
|
|
|
|
i + 3 + n, MBEDTLS_SSL_OUT_CONTENT_LEN ) );
|
|
|
|
return( MBEDTLS_ERR_SSL_CERTIFICATE_TOO_LARGE );
|
2017-12-27 22:37:21 +01:00
|
|
|
}
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
ssl->out_msg[i ] = (unsigned char)( n >> 16 );
|
|
|
|
ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
|
|
|
|
ssl->out_msg[i + 2] = (unsigned char)( n );
|
2017-12-27 22:37:21 +01:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
|
|
|
|
i += n; crt = crt->next;
|
|
|
|
}
|
2017-12-27 22:37:21 +01:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
|
|
|
|
ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
|
|
|
|
ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
|
2010-07-25 16:24:53 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
ssl->out_msglen = i;
|
|
|
|
ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
|
|
|
|
ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
|
2010-07-25 16:24:53 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
|
|
|
|
write_msg:
|
|
|
|
#endif
|
2013-08-31 17:40:26 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
ssl->state++;
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
|
|
|
|
{
|
|
|
|
MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
|
|
|
|
return( ret );
|
|
|
|
}
|
2014-10-29 12:07:57 +01:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
|
2018-01-05 17:24:22 +01:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
return( ret );
|
|
|
|
}
|
2014-10-29 12:07:57 +01:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
|
2015-01-13 10:59:51 +01:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
|
|
|
|
static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
|
|
|
|
unsigned char *crt_buf,
|
|
|
|
size_t crt_buf_len )
|
|
|
|
{
|
|
|
|
mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert;
|
2019-04-26 14:34:37 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
if( peer_crt == NULL )
|
|
|
|
return( -1 );
|
2014-11-04 14:40:21 +01:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
if( peer_crt->raw.len != crt_buf_len )
|
|
|
|
return( -1 );
|
2014-10-29 12:07:57 +01:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
return( memcmp( peer_crt->raw.p, crt_buf, peer_crt->raw.len ) );
|
|
|
|
}
|
|
|
|
#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
|
|
|
|
static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
|
|
|
|
unsigned char *crt_buf,
|
|
|
|
size_t crt_buf_len )
|
|
|
|
{
|
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
|
|
|
unsigned char const * const peer_cert_digest =
|
|
|
|
ssl->session->peer_cert_digest;
|
|
|
|
mbedtls_md_type_t const peer_cert_digest_type =
|
|
|
|
ssl->session->peer_cert_digest_type;
|
|
|
|
mbedtls_md_info_t const * const digest_info =
|
|
|
|
mbedtls_md_info_from_type( peer_cert_digest_type );
|
|
|
|
unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN];
|
|
|
|
size_t digest_len;
|
2018-01-05 17:24:22 +01:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
if( peer_cert_digest == NULL || digest_info == NULL )
|
|
|
|
return( -1 );
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
digest_len = mbedtls_md_get_size( digest_info );
|
|
|
|
if( digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN )
|
|
|
|
return( -1 );
|
2015-01-13 10:59:51 +01:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
ret = mbedtls_md( digest_info, crt_buf, crt_buf_len, tmp_digest );
|
|
|
|
if( ret != 0 )
|
|
|
|
return( -1 );
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
return( memcmp( tmp_digest, peer_cert_digest, digest_len ) );
|
2009-01-03 22:22:43 +01:00
|
|
|
}
|
2020-02-05 15:44:10 +01:00
|
|
|
#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
|
|
|
|
#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
/*
|
|
|
|
* Once the certificate message is read, parse it into a cert chain and
|
|
|
|
* perform basic checks, but leave actual verification to the caller
|
|
|
|
*/
|
|
|
|
static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl,
|
|
|
|
mbedtls_x509_crt *chain )
|
2009-01-03 22:22:43 +01:00
|
|
|
{
|
2020-02-05 15:44:10 +01:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
|
|
|
#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
|
|
|
|
int crt_cnt=0;
|
2013-12-19 16:40:57 +01:00
|
|
|
#endif
|
2020-02-05 15:44:10 +01:00
|
|
|
size_t i, n;
|
|
|
|
uint8_t alert;
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
|
|
|
|
{
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
|
|
|
|
mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
|
|
|
|
MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
|
|
|
|
return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
|
|
|
|
}
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE ||
|
|
|
|
ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
|
2009-01-03 22:22:43 +01:00
|
|
|
{
|
2020-02-05 15:44:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
|
|
|
|
mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
|
|
|
|
MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
|
|
|
|
return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
|
2009-01-03 22:22:43 +01:00
|
|
|
}
|
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
i = mbedtls_ssl_hs_hdr_len( ssl );
|
2017-12-27 22:28:58 +01:00
|
|
|
|
2019-04-29 14:52:53 +02:00
|
|
|
/*
|
2020-02-05 15:44:10 +01:00
|
|
|
* Same message structure as in mbedtls_ssl_write_certificate()
|
2019-04-29 14:52:53 +02:00
|
|
|
*/
|
2020-02-05 15:44:10 +01:00
|
|
|
n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
|
2019-04-29 14:52:53 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
if( ssl->in_msg[i] != 0 ||
|
|
|
|
ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
|
2013-01-07 18:20:04 +01:00
|
|
|
{
|
2020-02-05 15:44:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
|
|
|
|
mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
|
|
|
|
MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
|
|
|
|
return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
|
2009-01-03 22:22:43 +01:00
|
|
|
}
|
2013-01-07 18:20:04 +01:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
/* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */
|
|
|
|
i += 3;
|
2013-09-05 16:10:41 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
/* Iterate through and parse the CRTs in the provided chain. */
|
|
|
|
while( i < ssl->in_hslen )
|
|
|
|
{
|
|
|
|
/* Check that there's room for the next CRT's length fields. */
|
|
|
|
if ( i + 3 > ssl->in_hslen ) {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
|
|
|
|
mbedtls_ssl_send_alert_message( ssl,
|
|
|
|
MBEDTLS_SSL_ALERT_LEVEL_FATAL,
|
|
|
|
MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
|
|
|
|
return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
|
2013-09-05 16:10:41 +02:00
|
|
|
}
|
2020-02-05 15:44:10 +01:00
|
|
|
/* In theory, the CRT can be up to 2**24 Bytes, but we don't support
|
|
|
|
* anything beyond 2**16 ~ 64K. */
|
|
|
|
if( ssl->in_msg[i] != 0 )
|
2013-09-05 16:10:41 +02:00
|
|
|
{
|
2020-02-05 15:44:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
|
|
|
|
mbedtls_ssl_send_alert_message( ssl,
|
|
|
|
MBEDTLS_SSL_ALERT_LEVEL_FATAL,
|
|
|
|
MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
|
|
|
|
return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
|
2013-09-05 16:10:41 +02:00
|
|
|
}
|
2010-07-25 16:24:53 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
/* Read length of the next CRT in the chain. */
|
|
|
|
n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
|
|
|
|
| (unsigned int) ssl->in_msg[i + 2];
|
|
|
|
i += 3;
|
2013-01-03 14:52:21 +01:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
if( n < 128 || i + n > ssl->in_hslen )
|
2013-01-03 14:52:21 +01:00
|
|
|
{
|
2020-02-05 15:44:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
|
|
|
|
mbedtls_ssl_send_alert_message( ssl,
|
|
|
|
MBEDTLS_SSL_ALERT_LEVEL_FATAL,
|
|
|
|
MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
|
|
|
|
return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
|
2013-01-03 14:52:21 +01:00
|
|
|
}
|
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
/* Check if we're handling the first CRT in the chain. */
|
|
|
|
#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
|
|
|
|
if( crt_cnt++ == 0 &&
|
|
|
|
ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
|
|
|
|
ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
|
2014-10-29 12:07:57 +01:00
|
|
|
{
|
2020-02-05 15:44:10 +01:00
|
|
|
/* During client-side renegotiation, check that the server's
|
|
|
|
* end-CRTs hasn't changed compared to the initial handshake,
|
|
|
|
* mitigating the triple handshake attack. On success, reuse
|
|
|
|
* the original end-CRT instead of parsing it again. */
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 3, ( "Check that peer CRT hasn't changed during renegotiation" ) );
|
|
|
|
if( ssl_check_peer_crt_unchanged( ssl,
|
|
|
|
&ssl->in_msg[i],
|
|
|
|
n ) != 0 )
|
2014-10-29 12:07:57 +01:00
|
|
|
{
|
2020-02-05 15:44:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
|
|
|
|
mbedtls_ssl_send_alert_message( ssl,
|
|
|
|
MBEDTLS_SSL_ALERT_LEVEL_FATAL,
|
|
|
|
MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
|
|
|
|
return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
|
2014-10-29 12:07:57 +01:00
|
|
|
}
|
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
/* Now we can safely free the original chain. */
|
|
|
|
ssl_clear_peer_cert( ssl->session );
|
2013-08-31 17:40:26 +02:00
|
|
|
}
|
2020-02-05 15:44:10 +01:00
|
|
|
#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
|
2013-08-31 17:25:14 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
/* Parse the next certificate in the chain. */
|
|
|
|
#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
|
|
|
|
ret = mbedtls_x509_crt_parse_der( chain, ssl->in_msg + i, n );
|
|
|
|
#else
|
|
|
|
/* If we don't need to store the CRT chain permanently, parse
|
|
|
|
* it in-place from the input buffer instead of making a copy. */
|
|
|
|
ret = mbedtls_x509_crt_parse_der_nocopy( chain, ssl->in_msg + i, n );
|
|
|
|
#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
|
|
|
|
switch( ret )
|
2013-08-31 17:40:26 +02:00
|
|
|
{
|
2020-02-05 15:44:10 +01:00
|
|
|
case 0: /*ok*/
|
|
|
|
case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
|
|
|
|
/* Ignore certificate with an unknown algorithm: maybe a
|
|
|
|
prior certificate was already trusted. */
|
|
|
|
break;
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
case MBEDTLS_ERR_X509_ALLOC_FAILED:
|
|
|
|
alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
|
|
|
|
goto crt_parse_der_failed;
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
|
|
|
|
alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
|
|
|
|
goto crt_parse_der_failed;
|
2013-01-03 14:52:21 +01:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
default:
|
|
|
|
alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
|
|
|
|
crt_parse_der_failed:
|
|
|
|
mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert );
|
|
|
|
MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
|
|
|
|
return( ret );
|
2017-12-27 22:28:58 +01:00
|
|
|
}
|
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
i += n;
|
|
|
|
}
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", chain );
|
|
|
|
return( 0 );
|
|
|
|
}
|
2017-12-27 22:28:58 +01:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
#if defined(MBEDTLS_SSL_SRV_C)
|
|
|
|
static int ssl_srv_check_client_no_crt_notification( mbedtls_ssl_context *ssl )
|
|
|
|
{
|
|
|
|
if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
|
|
|
|
return( -1 );
|
2017-12-27 22:28:58 +01:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_SSL_PROTO_SSL3)
|
2020-02-05 15:44:10 +01:00
|
|
|
/*
|
|
|
|
* Check if the client sent an empty certificate
|
|
|
|
*/
|
|
|
|
if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
|
|
|
|
{
|
|
|
|
if( ssl->in_msglen == 2 &&
|
|
|
|
ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT &&
|
|
|
|
ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
|
|
|
|
ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
|
2009-01-03 22:22:43 +01:00
|
|
|
{
|
2020-02-05 15:44:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
|
|
|
|
return( 0 );
|
2009-01-03 22:22:43 +01:00
|
|
|
}
|
2020-02-05 15:44:10 +01:00
|
|
|
|
|
|
|
return( -1 );
|
|
|
|
}
|
2015-04-08 12:49:31 +02:00
|
|
|
#endif /* MBEDTLS_SSL_PROTO_SSL3 */
|
2020-02-05 15:44:10 +01:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
|
|
|
|
defined(MBEDTLS_SSL_PROTO_TLS1_2)
|
2020-02-05 15:44:10 +01:00
|
|
|
if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
|
|
|
|
ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
|
|
|
|
ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
|
|
|
|
memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
|
|
|
|
{
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
|
|
|
|
return( 0 );
|
|
|
|
}
|
2013-02-27 14:48:00 +01:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
return( -1 );
|
2015-04-08 12:49:31 +02:00
|
|
|
#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
|
|
|
|
MBEDTLS_SSL_PROTO_TLS1_2 */
|
2020-02-05 15:44:10 +01:00
|
|
|
}
|
|
|
|
#endif /* MBEDTLS_SSL_SRV_C */
|
2014-10-29 12:07:57 +01:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
/* Check if a certificate message is expected.
|
|
|
|
* Return either
|
|
|
|
* - SSL_CERTIFICATE_EXPECTED, or
|
|
|
|
* - SSL_CERTIFICATE_SKIP
|
|
|
|
* indicating whether a Certificate message is expected or not.
|
|
|
|
*/
|
|
|
|
#define SSL_CERTIFICATE_EXPECTED 0
|
|
|
|
#define SSL_CERTIFICATE_SKIP 1
|
|
|
|
static int ssl_parse_certificate_coordinate( mbedtls_ssl_context *ssl,
|
|
|
|
int authmode )
|
|
|
|
{
|
|
|
|
const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
|
|
|
|
ssl->handshake->ciphersuite_info;
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
|
|
|
|
return( SSL_CERTIFICATE_SKIP );
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
#if defined(MBEDTLS_SSL_SRV_C)
|
|
|
|
if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
|
|
|
|
{
|
|
|
|
if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
|
|
|
|
return( SSL_CERTIFICATE_SKIP );
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
if( authmode == MBEDTLS_SSL_VERIFY_NONE )
|
2013-10-25 19:31:25 +02:00
|
|
|
{
|
2020-02-05 15:44:10 +01:00
|
|
|
ssl->session_negotiate->verify_result =
|
|
|
|
MBEDTLS_X509_BADCERT_SKIP_VERIFY;
|
|
|
|
return( SSL_CERTIFICATE_SKIP );
|
2013-10-25 19:31:25 +02:00
|
|
|
}
|
2020-02-05 15:44:10 +01:00
|
|
|
}
|
|
|
|
#else
|
|
|
|
((void) authmode);
|
|
|
|
#endif /* MBEDTLS_SSL_SRV_C */
|
2018-06-28 10:38:35 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
return( SSL_CERTIFICATE_EXPECTED );
|
|
|
|
}
|
2017-12-27 22:28:58 +01:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl,
|
|
|
|
int authmode,
|
|
|
|
mbedtls_x509_crt *chain,
|
|
|
|
void *rs_ctx )
|
|
|
|
{
|
|
|
|
int ret = 0;
|
|
|
|
const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
|
|
|
|
ssl->handshake->ciphersuite_info;
|
|
|
|
int have_ca_chain = 0;
|
2013-02-27 14:48:00 +01:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *);
|
|
|
|
void *p_vrfy;
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
if( authmode == MBEDTLS_SSL_VERIFY_NONE )
|
|
|
|
return( 0 );
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
if( ssl->f_vrfy != NULL )
|
|
|
|
{
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use context-specific verification callback" ) );
|
|
|
|
f_vrfy = ssl->f_vrfy;
|
|
|
|
p_vrfy = ssl->p_vrfy;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use configuration-specific verification callback" ) );
|
|
|
|
f_vrfy = ssl->conf->f_vrfy;
|
|
|
|
p_vrfy = ssl->conf->p_vrfy;
|
2013-10-25 19:31:25 +02:00
|
|
|
}
|
2018-10-17 15:43:14 +02:00
|
|
|
|
|
|
|
/*
|
2020-02-05 15:44:10 +01:00
|
|
|
* Main check: verify certificate
|
2018-10-17 15:43:14 +02:00
|
|
|
*/
|
2020-02-05 15:44:10 +01:00
|
|
|
#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
|
|
|
|
if( ssl->conf->f_ca_cb != NULL )
|
2015-01-13 10:59:51 +01:00
|
|
|
{
|
2020-02-05 15:44:10 +01:00
|
|
|
((void) rs_ctx);
|
|
|
|
have_ca_chain = 1;
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 3, ( "use CA callback for X.509 CRT verification" ) );
|
|
|
|
ret = mbedtls_x509_crt_verify_with_ca_cb(
|
|
|
|
chain,
|
|
|
|
ssl->conf->f_ca_cb,
|
|
|
|
ssl->conf->p_ca_cb,
|
|
|
|
ssl->conf->cert_profile,
|
|
|
|
ssl->hostname,
|
|
|
|
&ssl->session_negotiate->verify_result,
|
|
|
|
f_vrfy, p_vrfy );
|
2019-04-29 18:31:37 +02:00
|
|
|
}
|
2020-02-05 15:44:10 +01:00
|
|
|
else
|
|
|
|
#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
|
|
|
|
{
|
|
|
|
mbedtls_x509_crt *ca_chain;
|
|
|
|
mbedtls_x509_crl *ca_crl;
|
2019-04-29 18:31:37 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
|
|
|
|
if( ssl->handshake->sni_ca_chain != NULL )
|
|
|
|
{
|
|
|
|
ca_chain = ssl->handshake->sni_ca_chain;
|
|
|
|
ca_crl = ssl->handshake->sni_ca_crl;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
ca_chain = ssl->conf->ca_chain;
|
|
|
|
ca_crl = ssl->conf->ca_crl;
|
|
|
|
}
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
if( ca_chain != NULL )
|
|
|
|
have_ca_chain = 1;
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
ret = mbedtls_x509_crt_verify_restartable(
|
|
|
|
chain,
|
|
|
|
ca_chain, ca_crl,
|
|
|
|
ssl->conf->cert_profile,
|
|
|
|
ssl->hostname,
|
|
|
|
&ssl->session_negotiate->verify_result,
|
|
|
|
f_vrfy, p_vrfy, rs_ctx );
|
|
|
|
}
|
2014-10-28 13:08:59 +01:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
if( ret != 0 )
|
|
|
|
{
|
|
|
|
MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
|
|
|
|
}
|
2012-07-03 15:30:23 +02:00
|
|
|
|
2020-03-10 12:19:08 +01:00
|
|
|
#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
|
2020-02-05 15:44:10 +01:00
|
|
|
if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
|
|
|
|
return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS );
|
|
|
|
#endif
|
2012-07-03 15:30:23 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
/*
|
|
|
|
* Secondary checks: always done, but change 'ret' only if it was 0
|
|
|
|
*/
|
2013-06-30 14:57:46 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
#if defined(MBEDTLS_ECP_C)
|
|
|
|
{
|
|
|
|
const mbedtls_pk_context *pk = &chain->pk;
|
2012-07-03 15:30:23 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
/* If certificate uses an EC key, make sure the curve is OK */
|
|
|
|
if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) &&
|
|
|
|
mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id ) != 0 )
|
|
|
|
{
|
|
|
|
ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
|
2012-07-03 15:30:23 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
|
|
|
|
if( ret == 0 )
|
|
|
|
ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif /* MBEDTLS_ECP_C */
|
2012-07-03 15:30:23 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
if( mbedtls_ssl_check_cert_usage( chain,
|
|
|
|
ciphersuite_info,
|
|
|
|
! ssl->conf->endpoint,
|
|
|
|
&ssl->session_negotiate->verify_result ) != 0 )
|
|
|
|
{
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
|
|
|
|
if( ret == 0 )
|
|
|
|
ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
|
|
|
|
}
|
2012-07-03 15:30:23 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
/* mbedtls_x509_crt_verify_with_profile is supposed to report a
|
|
|
|
* verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
|
|
|
|
* with details encoded in the verification flags. All other kinds
|
|
|
|
* of error codes, including those from the user provided f_vrfy
|
|
|
|
* functions, are treated as fatal and lead to a failure of
|
|
|
|
* ssl_parse_certificate even if verification was optional. */
|
|
|
|
if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
|
|
|
|
( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
|
|
|
|
ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ) )
|
2012-07-03 15:30:23 +02:00
|
|
|
{
|
2020-02-05 15:44:10 +01:00
|
|
|
ret = 0;
|
2012-07-03 15:30:23 +02:00
|
|
|
}
|
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
if( have_ca_chain == 0 && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
|
|
|
|
{
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
|
|
|
|
ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
|
|
|
|
}
|
2012-07-03 15:30:23 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
if( ret != 0 )
|
|
|
|
{
|
|
|
|
uint8_t alert;
|
2012-07-03 15:30:23 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
/* The certificate may have been rejected for several reasons.
|
|
|
|
Pick one and send the corresponding alert. Which alert to send
|
|
|
|
may be a subject of debate in some cases. */
|
|
|
|
if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
|
|
|
|
alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
|
|
|
|
else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
|
|
|
|
alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
|
|
|
|
else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
|
|
|
|
alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
|
|
|
|
else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
|
|
|
|
alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
|
|
|
|
else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
|
|
|
|
alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
|
|
|
|
else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
|
|
|
|
alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
|
|
|
|
else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
|
|
|
|
alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
|
|
|
|
else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
|
|
|
|
alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
|
|
|
|
else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
|
|
|
|
alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
|
|
|
|
else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
|
|
|
|
alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
|
|
|
|
else
|
|
|
|
alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
|
|
|
|
mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
|
|
|
|
alert );
|
|
|
|
}
|
2012-07-03 15:30:23 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
#if defined(MBEDTLS_DEBUG_C)
|
|
|
|
if( ssl->session_negotiate->verify_result != 0 )
|
|
|
|
{
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %x",
|
|
|
|
ssl->session_negotiate->verify_result ) );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
|
|
|
|
}
|
|
|
|
#endif /* MBEDTLS_DEBUG_C */
|
2012-07-03 15:30:23 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
return( ret );
|
2012-07-03 15:30:23 +02:00
|
|
|
}
|
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
|
|
|
|
static int ssl_remember_peer_crt_digest( mbedtls_ssl_context *ssl,
|
|
|
|
unsigned char *start, size_t len )
|
2012-07-03 15:30:23 +02:00
|
|
|
{
|
2019-12-16 12:46:15 +01:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2020-02-05 15:44:10 +01:00
|
|
|
/* Remember digest of the peer's end-CRT. */
|
|
|
|
ssl->session_negotiate->peer_cert_digest =
|
|
|
|
mbedtls_calloc( 1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN );
|
|
|
|
if( ssl->session_negotiate->peer_cert_digest == NULL )
|
|
|
|
{
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
|
|
|
|
MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ) );
|
|
|
|
mbedtls_ssl_send_alert_message( ssl,
|
|
|
|
MBEDTLS_SSL_ALERT_LEVEL_FATAL,
|
|
|
|
MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
|
2012-07-03 15:30:23 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
|
|
|
|
}
|
2013-06-30 14:57:46 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
ret = mbedtls_md( mbedtls_md_info_from_type(
|
|
|
|
MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE ),
|
|
|
|
start, len,
|
|
|
|
ssl->session_negotiate->peer_cert_digest );
|
2012-07-03 15:30:23 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
ssl->session_negotiate->peer_cert_digest_type =
|
|
|
|
MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
|
|
|
|
ssl->session_negotiate->peer_cert_digest_len =
|
|
|
|
MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
|
2012-07-03 15:30:23 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
return( ret );
|
|
|
|
}
|
2012-07-03 15:30:23 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
static int ssl_remember_peer_pubkey( mbedtls_ssl_context *ssl,
|
|
|
|
unsigned char *start, size_t len )
|
|
|
|
{
|
|
|
|
unsigned char *end = start + len;
|
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2012-07-03 15:30:23 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
/* Make a copy of the peer's raw public key. */
|
|
|
|
mbedtls_pk_init( &ssl->handshake->peer_pubkey );
|
|
|
|
ret = mbedtls_pk_parse_subpubkey( &start, end,
|
|
|
|
&ssl->handshake->peer_pubkey );
|
|
|
|
if( ret != 0 )
|
2012-07-03 15:30:23 +02:00
|
|
|
{
|
2020-02-05 15:44:10 +01:00
|
|
|
/* We should have parsed the public key before. */
|
|
|
|
return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
|
2012-07-03 15:30:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
2020-02-05 15:44:10 +01:00
|
|
|
#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
|
2014-10-15 15:07:45 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
|
2014-10-15 15:07:45 +02:00
|
|
|
{
|
2020-02-05 15:44:10 +01:00
|
|
|
int ret = 0;
|
|
|
|
int crt_expected;
|
|
|
|
#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
|
|
|
|
const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
|
|
|
|
? ssl->handshake->sni_authmode
|
|
|
|
: ssl->conf->authmode;
|
|
|
|
#else
|
|
|
|
const int authmode = ssl->conf->authmode;
|
2014-10-15 15:07:45 +02:00
|
|
|
#endif
|
2020-02-05 15:44:10 +01:00
|
|
|
void *rs_ctx = NULL;
|
|
|
|
mbedtls_x509_crt *chain = NULL;
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
crt_expected = ssl_parse_certificate_coordinate( ssl, authmode );
|
|
|
|
if( crt_expected == SSL_CERTIFICATE_SKIP )
|
2014-09-17 11:34:57 +02:00
|
|
|
{
|
2020-02-05 15:44:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
|
|
|
|
goto exit;
|
2014-09-17 11:34:57 +02:00
|
|
|
}
|
|
|
|
|
2020-03-10 12:19:08 +01:00
|
|
|
#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
|
2020-02-05 15:44:10 +01:00
|
|
|
if( ssl->handshake->ecrs_enabled &&
|
|
|
|
ssl->handshake->ecrs_state == ssl_ecrs_crt_verify )
|
2014-04-30 14:38:05 +02:00
|
|
|
{
|
2020-02-05 15:44:10 +01:00
|
|
|
chain = ssl->handshake->ecrs_peer_cert;
|
|
|
|
ssl->handshake->ecrs_peer_cert = NULL;
|
|
|
|
goto crt_verify;
|
2014-04-30 14:38:05 +02:00
|
|
|
}
|
2020-02-05 15:44:10 +01:00
|
|
|
#endif
|
2014-04-30 14:38:05 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
|
2009-01-03 22:22:43 +01:00
|
|
|
{
|
2020-02-05 15:44:10 +01:00
|
|
|
/* mbedtls_ssl_read_record may have sent an alert already. We
|
|
|
|
let it decide whether to alert. */
|
|
|
|
MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
|
|
|
|
goto exit;
|
|
|
|
}
|
2014-03-24 13:13:01 +01:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
#if defined(MBEDTLS_SSL_SRV_C)
|
|
|
|
if( ssl_srv_check_client_no_crt_notification( ssl ) == 0 )
|
|
|
|
{
|
|
|
|
ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
|
2014-03-24 13:13:01 +01:00
|
|
|
|
2020-04-21 21:26:05 +02:00
|
|
|
if( authmode != MBEDTLS_SSL_VERIFY_OPTIONAL )
|
2020-02-05 15:44:10 +01:00
|
|
|
ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
#endif /* MBEDTLS_SSL_SRV_C */
|
2011-05-18 15:32:51 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
/* Clear existing peer CRT structure in case we tried to
|
|
|
|
* reuse a session but it failed, and allocate a new one. */
|
|
|
|
ssl_clear_peer_cert( ssl->session_negotiate );
|
2014-10-02 16:51:56 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
chain = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
|
|
|
|
if( chain == NULL )
|
|
|
|
{
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
|
|
|
|
sizeof( mbedtls_x509_crt ) ) );
|
|
|
|
mbedtls_ssl_send_alert_message( ssl,
|
|
|
|
MBEDTLS_SSL_ALERT_LEVEL_FATAL,
|
|
|
|
MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
|
2014-09-19 21:18:23 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
mbedtls_x509_crt_init( chain );
|
2014-09-30 22:21:31 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
ret = ssl_parse_certificate_chain( ssl, chain );
|
|
|
|
if( ret != 0 )
|
|
|
|
goto exit;
|
2014-10-01 18:29:03 +02:00
|
|
|
|
2020-03-10 12:19:08 +01:00
|
|
|
#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
|
2020-02-05 15:44:10 +01:00
|
|
|
if( ssl->handshake->ecrs_enabled)
|
|
|
|
ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
|
2014-10-15 13:52:48 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
crt_verify:
|
|
|
|
if( ssl->handshake->ecrs_enabled)
|
|
|
|
rs_ctx = &ssl->handshake->ecrs_ctx;
|
|
|
|
#endif
|
2014-09-19 21:18:23 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
ret = ssl_parse_certificate_verify( ssl, authmode,
|
|
|
|
chain, rs_ctx );
|
|
|
|
if( ret != 0 )
|
|
|
|
goto exit;
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
|
2014-03-24 13:13:01 +01:00
|
|
|
{
|
2020-02-05 15:44:10 +01:00
|
|
|
unsigned char *crt_start, *pk_start;
|
|
|
|
size_t crt_len, pk_len;
|
2015-05-13 16:22:05 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
/* We parse the CRT chain without copying, so
|
|
|
|
* these pointers point into the input buffer,
|
|
|
|
* and are hence still valid after freeing the
|
|
|
|
* CRT chain. */
|
2014-03-24 13:13:01 +01:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
crt_start = chain->raw.p;
|
|
|
|
crt_len = chain->raw.len;
|
2014-03-24 13:13:01 +01:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
pk_start = chain->pk_raw.p;
|
|
|
|
pk_len = chain->pk_raw.len;
|
2014-03-24 13:13:01 +01:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
/* Free the CRT structures before computing
|
|
|
|
* digest and copying the peer's public key. */
|
|
|
|
mbedtls_x509_crt_free( chain );
|
|
|
|
mbedtls_free( chain );
|
|
|
|
chain = NULL;
|
2014-03-24 13:13:01 +01:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
ret = ssl_remember_peer_crt_digest( ssl, crt_start, crt_len );
|
|
|
|
if( ret != 0 )
|
|
|
|
goto exit;
|
2018-02-28 13:30:59 +01:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
ret = ssl_remember_peer_pubkey( ssl, pk_start, pk_len );
|
|
|
|
if( ret != 0 )
|
|
|
|
goto exit;
|
2009-01-03 22:22:43 +01:00
|
|
|
}
|
2020-02-05 15:44:10 +01:00
|
|
|
#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
|
|
|
|
/* Pass ownership to session structure. */
|
|
|
|
ssl->session_negotiate->peer_cert = chain;
|
|
|
|
chain = NULL;
|
|
|
|
#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
exit:
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
if( ret == 0 )
|
|
|
|
ssl->state++;
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2020-03-10 12:19:08 +01:00
|
|
|
#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
|
2020-02-05 15:44:10 +01:00
|
|
|
if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
|
2014-09-17 11:34:57 +02:00
|
|
|
{
|
2020-02-05 15:44:10 +01:00
|
|
|
ssl->handshake->ecrs_peer_cert = chain;
|
|
|
|
chain = NULL;
|
2014-09-17 11:34:57 +02:00
|
|
|
}
|
2020-02-05 15:44:10 +01:00
|
|
|
#endif
|
2014-09-17 11:34:57 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
if( chain != NULL )
|
2014-02-14 08:39:32 +01:00
|
|
|
{
|
2020-02-05 15:44:10 +01:00
|
|
|
mbedtls_x509_crt_free( chain );
|
|
|
|
mbedtls_free( chain );
|
2014-02-14 08:39:32 +01:00
|
|
|
}
|
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
return( ret );
|
|
|
|
}
|
2020-03-10 12:19:08 +01:00
|
|
|
#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
|
2018-02-22 13:29:04 +01:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl,
|
|
|
|
const mbedtls_ssl_ciphersuite_t *ciphersuite_info )
|
|
|
|
{
|
|
|
|
((void) ciphersuite_info);
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
|
|
|
|
defined(MBEDTLS_SSL_PROTO_TLS1_1)
|
|
|
|
if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
|
|
|
|
ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
|
2018-08-06 12:19:13 +02:00
|
|
|
else
|
|
|
|
#endif
|
2020-02-05 15:44:10 +01:00
|
|
|
#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
|
|
|
|
#if defined(MBEDTLS_SHA512_C)
|
|
|
|
if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
|
|
|
|
ssl->handshake->update_checksum = ssl_update_checksum_sha384;
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
#if defined(MBEDTLS_SHA256_C)
|
|
|
|
if( ciphersuite_info->mac != MBEDTLS_MD_SHA384 )
|
|
|
|
ssl->handshake->update_checksum = ssl_update_checksum_sha256;
|
2014-09-19 11:18:57 +02:00
|
|
|
else
|
2020-02-05 15:44:10 +01:00
|
|
|
#endif
|
|
|
|
#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
|
2014-09-19 11:18:57 +02:00
|
|
|
{
|
2020-02-05 15:44:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
|
|
|
|
return;
|
2014-09-19 11:18:57 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl )
|
2014-09-19 11:18:57 +02:00
|
|
|
{
|
2020-02-05 15:44:10 +01:00
|
|
|
#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
|
|
|
|
defined(MBEDTLS_SSL_PROTO_TLS1_1)
|
|
|
|
mbedtls_md5_starts_ret( &ssl->handshake->fin_md5 );
|
|
|
|
mbedtls_sha1_starts_ret( &ssl->handshake->fin_sha1 );
|
|
|
|
#endif
|
|
|
|
#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
|
|
|
|
#if defined(MBEDTLS_SHA256_C)
|
|
|
|
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
|
|
|
psa_hash_abort( &ssl->handshake->fin_sha256_psa );
|
|
|
|
psa_hash_setup( &ssl->handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
|
|
|
|
#else
|
|
|
|
mbedtls_sha256_starts_ret( &ssl->handshake->fin_sha256, 0 );
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
#if defined(MBEDTLS_SHA512_C)
|
|
|
|
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
|
|
|
psa_hash_abort( &ssl->handshake->fin_sha384_psa );
|
|
|
|
psa_hash_setup( &ssl->handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
|
|
|
|
#else
|
|
|
|
mbedtls_sha512_starts_ret( &ssl->handshake->fin_sha512, 1 );
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
|
2014-09-19 11:18:57 +02:00
|
|
|
}
|
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
static void ssl_update_checksum_start( mbedtls_ssl_context *ssl,
|
|
|
|
const unsigned char *buf, size_t len )
|
2014-09-19 15:09:21 +02:00
|
|
|
{
|
2020-02-05 15:44:10 +01:00
|
|
|
#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
|
|
|
|
defined(MBEDTLS_SSL_PROTO_TLS1_1)
|
|
|
|
mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
|
|
|
|
mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
|
|
|
|
#endif
|
|
|
|
#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
|
|
|
|
#if defined(MBEDTLS_SHA256_C)
|
|
|
|
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
|
|
|
psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len );
|
|
|
|
#else
|
|
|
|
mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
#if defined(MBEDTLS_SHA512_C)
|
|
|
|
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
|
|
|
psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len );
|
|
|
|
#else
|
|
|
|
mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
|
|
|
|
#endif
|
2014-09-19 21:39:13 +02:00
|
|
|
#endif
|
2020-02-05 15:44:10 +01:00
|
|
|
#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
|
2014-09-19 15:09:21 +02:00
|
|
|
}
|
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
|
|
|
|
defined(MBEDTLS_SSL_PROTO_TLS1_1)
|
|
|
|
static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *ssl,
|
|
|
|
const unsigned char *buf, size_t len )
|
2017-09-13 12:45:21 +02:00
|
|
|
{
|
2020-02-05 15:44:10 +01:00
|
|
|
mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
|
|
|
|
mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
|
|
|
|
}
|
|
|
|
#endif
|
2017-09-13 12:45:21 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
|
|
|
|
#if defined(MBEDTLS_SHA256_C)
|
|
|
|
static void ssl_update_checksum_sha256( mbedtls_ssl_context *ssl,
|
|
|
|
const unsigned char *buf, size_t len )
|
|
|
|
{
|
|
|
|
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
|
|
|
psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len );
|
|
|
|
#else
|
|
|
|
mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
#endif
|
2017-09-13 12:45:21 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
#if defined(MBEDTLS_SHA512_C)
|
|
|
|
static void ssl_update_checksum_sha384( mbedtls_ssl_context *ssl,
|
|
|
|
const unsigned char *buf, size_t len )
|
|
|
|
{
|
|
|
|
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
|
|
|
psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len );
|
|
|
|
#else
|
|
|
|
mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
|
|
|
|
#endif
|
2017-09-13 12:45:21 +02:00
|
|
|
}
|
2020-02-05 15:44:10 +01:00
|
|
|
#endif
|
|
|
|
#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
|
2017-09-13 12:45:21 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
#if defined(MBEDTLS_SSL_PROTO_SSL3)
|
|
|
|
static void ssl_calc_finished_ssl(
|
|
|
|
mbedtls_ssl_context *ssl, unsigned char *buf, int from )
|
2014-09-19 11:18:57 +02:00
|
|
|
{
|
2020-02-05 15:44:10 +01:00
|
|
|
const char *sender;
|
|
|
|
mbedtls_md5_context md5;
|
|
|
|
mbedtls_sha1_context sha1;
|
2014-09-19 11:18:57 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
unsigned char padbuf[48];
|
|
|
|
unsigned char md5sum[16];
|
|
|
|
unsigned char sha1sum[20];
|
2014-09-19 15:09:21 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
mbedtls_ssl_session *session = ssl->session_negotiate;
|
|
|
|
if( !session )
|
|
|
|
session = ssl->session;
|
2014-09-19 15:09:21 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
|
2014-09-19 11:18:57 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
mbedtls_md5_init( &md5 );
|
|
|
|
mbedtls_sha1_init( &sha1 );
|
2018-08-06 12:33:50 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
|
|
|
|
mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
|
2018-08-17 17:47:58 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
/*
|
|
|
|
* SSLv3:
|
|
|
|
* hash =
|
|
|
|
* MD5( master + pad2 +
|
|
|
|
* MD5( handshake + sender + master + pad1 ) )
|
|
|
|
* + SHA1( master + pad2 +
|
|
|
|
* SHA1( handshake + sender + master + pad1 ) )
|
|
|
|
*/
|
2018-08-14 14:22:10 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
#if !defined(MBEDTLS_MD5_ALT)
|
|
|
|
MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
|
|
|
|
md5.state, sizeof( md5.state ) );
|
|
|
|
#endif
|
2014-09-19 21:39:13 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
#if !defined(MBEDTLS_SHA1_ALT)
|
|
|
|
MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
|
|
|
|
sha1.state, sizeof( sha1.state ) );
|
|
|
|
#endif
|
2018-08-06 12:33:50 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
sender = ( from == MBEDTLS_SSL_IS_CLIENT ) ? "CLNT"
|
|
|
|
: "SRVR";
|
2018-08-06 12:33:50 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
memset( padbuf, 0x36, 48 );
|
2018-08-06 12:33:50 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
mbedtls_md5_update_ret( &md5, (const unsigned char *) sender, 4 );
|
|
|
|
mbedtls_md5_update_ret( &md5, session->master, 48 );
|
|
|
|
mbedtls_md5_update_ret( &md5, padbuf, 48 );
|
|
|
|
mbedtls_md5_finish_ret( &md5, md5sum );
|
2017-09-13 14:00:05 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
mbedtls_sha1_update_ret( &sha1, (const unsigned char *) sender, 4 );
|
|
|
|
mbedtls_sha1_update_ret( &sha1, session->master, 48 );
|
|
|
|
mbedtls_sha1_update_ret( &sha1, padbuf, 40 );
|
|
|
|
mbedtls_sha1_finish_ret( &sha1, sha1sum );
|
2017-09-13 14:00:05 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
memset( padbuf, 0x5C, 48 );
|
2018-08-06 12:33:50 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
mbedtls_md5_starts_ret( &md5 );
|
|
|
|
mbedtls_md5_update_ret( &md5, session->master, 48 );
|
|
|
|
mbedtls_md5_update_ret( &md5, padbuf, 48 );
|
|
|
|
mbedtls_md5_update_ret( &md5, md5sum, 16 );
|
|
|
|
mbedtls_md5_finish_ret( &md5, buf );
|
2018-08-06 12:33:50 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
mbedtls_sha1_starts_ret( &sha1 );
|
|
|
|
mbedtls_sha1_update_ret( &sha1, session->master, 48 );
|
|
|
|
mbedtls_sha1_update_ret( &sha1, padbuf , 40 );
|
|
|
|
mbedtls_sha1_update_ret( &sha1, sha1sum, 20 );
|
|
|
|
mbedtls_sha1_finish_ret( &sha1, buf + 16 );
|
2017-09-13 14:00:05 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
|
2018-08-20 11:56:14 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
mbedtls_md5_free( &md5 );
|
|
|
|
mbedtls_sha1_free( &sha1 );
|
2014-09-19 11:18:57 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
|
|
|
|
mbedtls_platform_zeroize( md5sum, sizeof( md5sum ) );
|
|
|
|
mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
|
2014-09-19 11:18:57 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
|
|
|
|
}
|
|
|
|
#endif /* MBEDTLS_SSL_PROTO_SSL3 */
|
2014-09-19 11:18:57 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
|
|
|
|
static void ssl_calc_finished_tls(
|
|
|
|
mbedtls_ssl_context *ssl, unsigned char *buf, int from )
|
|
|
|
{
|
|
|
|
int len = 12;
|
|
|
|
const char *sender;
|
|
|
|
mbedtls_md5_context md5;
|
|
|
|
mbedtls_sha1_context sha1;
|
|
|
|
unsigned char padbuf[36];
|
2014-09-19 11:18:57 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
mbedtls_ssl_session *session = ssl->session_negotiate;
|
|
|
|
if( !session )
|
|
|
|
session = ssl->session;
|
2017-09-13 14:00:05 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
|
2017-09-13 14:00:05 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
mbedtls_md5_init( &md5 );
|
|
|
|
mbedtls_sha1_init( &sha1 );
|
2014-09-19 11:18:57 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
|
|
|
|
mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
|
2014-09-19 11:18:57 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
/*
|
|
|
|
* TLSv1:
|
|
|
|
* hash = PRF( master, finished_label,
|
|
|
|
* MD5( handshake ) + SHA1( handshake ) )[0..11]
|
|
|
|
*/
|
2018-08-06 12:33:50 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
#if !defined(MBEDTLS_MD5_ALT)
|
|
|
|
MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
|
|
|
|
md5.state, sizeof( md5.state ) );
|
|
|
|
#endif
|
2014-09-29 15:29:48 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
#if !defined(MBEDTLS_SHA1_ALT)
|
|
|
|
MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
|
|
|
|
sha1.state, sizeof( sha1.state ) );
|
|
|
|
#endif
|
2014-09-19 11:18:57 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
sender = ( from == MBEDTLS_SSL_IS_CLIENT )
|
|
|
|
? "client finished"
|
|
|
|
: "server finished";
|
2014-09-19 15:09:21 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
mbedtls_md5_finish_ret( &md5, padbuf );
|
|
|
|
mbedtls_sha1_finish_ret( &sha1, padbuf + 16 );
|
2014-09-19 15:09:21 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
ssl->handshake->tls_prf( session->master, 48, sender,
|
|
|
|
padbuf, 36, buf, len );
|
2014-09-19 15:09:21 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
|
2018-08-15 16:11:57 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
mbedtls_md5_free( &md5 );
|
|
|
|
mbedtls_sha1_free( &sha1 );
|
2018-08-16 14:23:47 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
|
2014-09-29 15:29:48 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
|
2014-09-19 15:09:21 +02:00
|
|
|
}
|
2020-02-05 15:44:10 +01:00
|
|
|
#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
|
2014-09-29 15:29:48 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
|
|
|
|
#if defined(MBEDTLS_SHA256_C)
|
|
|
|
static void ssl_calc_finished_tls_sha256(
|
|
|
|
mbedtls_ssl_context *ssl, unsigned char *buf, int from )
|
2014-09-29 15:29:48 +02:00
|
|
|
{
|
2020-02-05 15:44:10 +01:00
|
|
|
int len = 12;
|
|
|
|
const char *sender;
|
|
|
|
unsigned char padbuf[32];
|
|
|
|
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
|
|
|
size_t hash_size;
|
|
|
|
psa_hash_operation_t sha256_psa = PSA_HASH_OPERATION_INIT;
|
|
|
|
psa_status_t status;
|
|
|
|
#else
|
|
|
|
mbedtls_sha256_context sha256;
|
|
|
|
#endif
|
2014-09-29 15:29:48 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
mbedtls_ssl_session *session = ssl->session_negotiate;
|
|
|
|
if( !session )
|
|
|
|
session = ssl->session;
|
2014-09-19 11:18:57 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
sender = ( from == MBEDTLS_SSL_IS_CLIENT )
|
|
|
|
? "client finished"
|
|
|
|
: "server finished";
|
2014-09-19 11:18:57 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
|
|
|
sha256_psa = psa_hash_operation_init();
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha256" ) );
|
2017-09-13 09:38:11 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
|
|
|
|
if( status != PSA_SUCCESS )
|
2017-09-13 09:38:11 +02:00
|
|
|
{
|
2020-02-05 15:44:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
|
|
|
|
return;
|
2017-09-13 09:38:11 +02:00
|
|
|
}
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
status = psa_hash_finish( &sha256_psa, padbuf, sizeof( padbuf ), &hash_size );
|
|
|
|
if( status != PSA_SUCCESS )
|
2017-09-13 09:54:27 +02:00
|
|
|
{
|
2020-02-05 15:44:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
|
|
|
|
return;
|
2017-09-13 09:54:27 +02:00
|
|
|
}
|
2020-02-05 15:44:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 32 );
|
|
|
|
#else
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
mbedtls_sha256_init( &sha256 );
|
2017-09-13 09:54:27 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
|
|
|
|
|
|
|
|
mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
|
2018-08-06 12:52:54 +02:00
|
|
|
|
2017-09-13 09:54:27 +02:00
|
|
|
/*
|
2020-02-05 15:44:10 +01:00
|
|
|
* TLSv1.2:
|
|
|
|
* hash = PRF( master, finished_label,
|
|
|
|
* Hash( handshake ) )[0.11]
|
2017-09-13 09:54:27 +02:00
|
|
|
*/
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
#if !defined(MBEDTLS_SHA256_ALT)
|
|
|
|
MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
|
|
|
|
sha256.state, sizeof( sha256.state ) );
|
|
|
|
#endif
|
2017-09-18 11:55:54 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
mbedtls_sha256_finish_ret( &sha256, padbuf );
|
|
|
|
mbedtls_sha256_free( &sha256 );
|
|
|
|
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
2014-02-18 17:40:52 +01:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
ssl->handshake->tls_prf( session->master, 48, sender,
|
|
|
|
padbuf, 32, buf, len );
|
2014-02-18 18:50:02 +01:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
|
2014-02-18 17:40:52 +01:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
|
|
|
|
}
|
|
|
|
#endif /* MBEDTLS_SHA256_C */
|
2017-09-13 09:38:11 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
#if defined(MBEDTLS_SHA512_C)
|
|
|
|
static void ssl_calc_finished_tls_sha384(
|
|
|
|
mbedtls_ssl_context *ssl, unsigned char *buf, int from )
|
|
|
|
{
|
|
|
|
int len = 12;
|
|
|
|
const char *sender;
|
|
|
|
unsigned char padbuf[48];
|
|
|
|
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
|
|
|
size_t hash_size;
|
|
|
|
psa_hash_operation_t sha384_psa = PSA_HASH_OPERATION_INIT;
|
|
|
|
psa_status_t status;
|
|
|
|
#else
|
|
|
|
mbedtls_sha512_context sha512;
|
|
|
|
#endif
|
2017-09-13 09:38:11 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
mbedtls_ssl_session *session = ssl->session_negotiate;
|
|
|
|
if( !session )
|
|
|
|
session = ssl->session;
|
2017-09-13 09:38:11 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
sender = ( from == MBEDTLS_SSL_IS_CLIENT )
|
|
|
|
? "client finished"
|
|
|
|
: "server finished";
|
2017-09-13 09:38:11 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
|
|
|
sha384_psa = psa_hash_operation_init();
|
2017-09-13 09:38:11 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha384" ) );
|
2014-09-19 11:18:57 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
|
|
|
|
if( status != PSA_SUCCESS )
|
2012-07-03 15:30:23 +02:00
|
|
|
{
|
2020-02-05 15:44:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
|
|
|
|
return;
|
2012-07-03 15:30:23 +02:00
|
|
|
}
|
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
status = psa_hash_finish( &sha384_psa, padbuf, sizeof( padbuf ), &hash_size );
|
|
|
|
if( status != PSA_SUCCESS )
|
2009-01-03 22:22:43 +01:00
|
|
|
{
|
2020-02-05 15:44:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
|
|
|
|
return;
|
2012-05-08 11:17:57 +02:00
|
|
|
}
|
2020-02-05 15:44:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 48 );
|
|
|
|
#else
|
|
|
|
mbedtls_sha512_init( &sha512 );
|
2019-05-08 12:57:13 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
|
2014-02-13 11:17:34 +01:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
/*
|
|
|
|
* TLSv1.2:
|
|
|
|
* hash = PRF( master, finished_label,
|
|
|
|
* Hash( handshake ) )[0.11]
|
|
|
|
*/
|
2017-12-27 22:37:21 +01:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
#if !defined(MBEDTLS_SHA512_ALT)
|
|
|
|
MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
|
|
|
|
sha512.state, sizeof( sha512.state ) );
|
|
|
|
#endif
|
2017-12-27 22:37:21 +01:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
mbedtls_sha512_finish_ret( &sha512, padbuf );
|
|
|
|
mbedtls_sha512_free( &sha512 );
|
|
|
|
#endif
|
2017-12-27 22:37:21 +01:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
ssl->handshake->tls_prf( session->master, 48, sender,
|
|
|
|
padbuf, 48, buf, len );
|
2019-04-29 14:52:53 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
|
2017-12-27 22:37:21 +01:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
|
|
|
|
}
|
|
|
|
#endif /* MBEDTLS_SHA512_C */
|
|
|
|
#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
|
2018-08-06 12:19:13 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
void mbedtls_ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
|
|
|
|
{
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
|
2012-05-08 11:17:57 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
/*
|
|
|
|
* Free our handshake params
|
|
|
|
*/
|
|
|
|
mbedtls_ssl_handshake_free( ssl );
|
|
|
|
mbedtls_free( ssl->handshake );
|
|
|
|
ssl->handshake = NULL;
|
2019-05-08 12:57:13 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
/*
|
|
|
|
* Free the previous transform and swith in the current one
|
|
|
|
*/
|
|
|
|
if( ssl->transform )
|
|
|
|
{
|
|
|
|
mbedtls_ssl_transform_free( ssl->transform );
|
|
|
|
mbedtls_free( ssl->transform );
|
|
|
|
}
|
|
|
|
ssl->transform = ssl->transform_negotiate;
|
|
|
|
ssl->transform_negotiate = NULL;
|
2012-05-08 11:17:57 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
|
|
|
|
}
|
2018-08-06 12:19:13 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
|
|
|
|
{
|
|
|
|
int resume = ssl->handshake->resume;
|
2018-08-06 12:19:13 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
|
2018-08-06 10:49:38 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
#if defined(MBEDTLS_SSL_RENEGOTIATION)
|
|
|
|
if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
|
|
|
|
{
|
|
|
|
ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
|
|
|
|
ssl->renego_records_seen = 0;
|
2012-05-08 11:17:57 +02:00
|
|
|
}
|
2020-02-05 15:44:10 +01:00
|
|
|
#endif
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
/*
|
|
|
|
* Free the previous session and switch in the current one
|
|
|
|
*/
|
|
|
|
if( ssl->session )
|
2018-08-06 12:33:50 +02:00
|
|
|
{
|
2020-02-05 15:44:10 +01:00
|
|
|
#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
|
|
|
|
/* RFC 7366 3.1: keep the EtM state */
|
|
|
|
ssl->session_negotiate->encrypt_then_mac =
|
|
|
|
ssl->session->encrypt_then_mac;
|
|
|
|
#endif
|
2018-08-21 14:31:31 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
mbedtls_ssl_session_free( ssl->session );
|
|
|
|
mbedtls_free( ssl->session );
|
2018-08-06 12:33:50 +02:00
|
|
|
}
|
2020-02-05 15:44:10 +01:00
|
|
|
ssl->session = ssl->session_negotiate;
|
|
|
|
ssl->session_negotiate = NULL;
|
2018-08-06 12:33:50 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
/*
|
|
|
|
* Add cache entry
|
|
|
|
*/
|
|
|
|
if( ssl->conf->f_set_cache != NULL &&
|
|
|
|
ssl->session->id_len != 0 &&
|
|
|
|
resume == 0 )
|
2009-01-03 22:22:43 +01:00
|
|
|
{
|
2020-02-05 15:44:10 +01:00
|
|
|
if( ssl->conf->f_set_cache( ssl->conf->p_cache, ssl->session ) != 0 )
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
|
2009-01-03 22:22:43 +01:00
|
|
|
}
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_SSL_PROTO_DTLS)
|
2020-02-05 15:44:10 +01:00
|
|
|
if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
|
|
|
|
ssl->handshake->flight != NULL )
|
2014-08-20 13:12:58 +02:00
|
|
|
{
|
2020-02-05 15:44:10 +01:00
|
|
|
/* Cancel handshake timer */
|
|
|
|
mbedtls_ssl_set_timer( ssl, 0 );
|
2014-08-20 13:12:58 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
/* Keep last flight around in case we need to resend it:
|
|
|
|
* we need the handshake and transform structures for that */
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
|
2014-08-20 13:12:58 +02:00
|
|
|
}
|
2020-02-05 15:44:10 +01:00
|
|
|
else
|
|
|
|
#endif
|
|
|
|
mbedtls_ssl_handshake_wrapup_free_hs_transform( ssl );
|
2014-08-20 13:12:58 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
ssl->state++;
|
2014-08-20 13:12:58 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
|
2014-08-20 13:12:58 +02:00
|
|
|
}
|
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
|
2014-08-20 13:12:58 +02:00
|
|
|
{
|
2020-02-05 15:44:10 +01:00
|
|
|
int ret, hash_len;
|
2014-08-20 13:12:58 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
|
2014-08-20 13:12:58 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
mbedtls_ssl_update_out_pointers( ssl, ssl->transform_negotiate );
|
2014-09-02 13:39:16 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint );
|
2014-08-20 13:12:58 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
/*
|
|
|
|
* RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
|
|
|
|
* may define some other value. Currently (early 2016), no defined
|
|
|
|
* ciphersuite does this (and this is unlikely to change as activity has
|
|
|
|
* moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
|
|
|
|
*/
|
|
|
|
hash_len = ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) ? 36 : 12;
|
2014-08-20 13:12:58 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
#if defined(MBEDTLS_SSL_RENEGOTIATION)
|
|
|
|
ssl->verify_data_len = hash_len;
|
|
|
|
memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
|
|
|
|
#endif
|
2014-08-20 13:12:58 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
ssl->out_msglen = 4 + hash_len;
|
|
|
|
ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
|
|
|
|
ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
|
2014-08-20 10:43:01 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
/*
|
|
|
|
* In case of session resuming, invert the client and server
|
|
|
|
* ChangeCipherSpec messages order.
|
|
|
|
*/
|
|
|
|
if( ssl->handshake->resume != 0 )
|
2014-09-03 11:01:14 +02:00
|
|
|
{
|
2020-02-05 15:44:10 +01:00
|
|
|
#if defined(MBEDTLS_SSL_CLI_C)
|
|
|
|
if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
|
|
|
|
ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
|
|
|
|
#endif
|
|
|
|
#if defined(MBEDTLS_SSL_SRV_C)
|
|
|
|
if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
|
|
|
|
ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
|
|
|
|
#endif
|
2014-09-03 11:01:14 +02:00
|
|
|
}
|
2020-02-05 15:44:10 +01:00
|
|
|
else
|
|
|
|
ssl->state++;
|
2014-09-03 11:01:14 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
/*
|
|
|
|
* Switch to our negotiated transform and session parameters for outbound
|
|
|
|
* data.
|
|
|
|
*/
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
|
2014-02-18 11:33:49 +01:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_SSL_PROTO_DTLS)
|
2015-05-04 10:55:58 +02:00
|
|
|
if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
|
2014-02-18 11:33:49 +01:00
|
|
|
{
|
2020-02-05 15:44:10 +01:00
|
|
|
unsigned char i;
|
2018-08-15 16:54:43 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
/* Remember current epoch settings for resending */
|
|
|
|
ssl->handshake->alt_transform_out = ssl->transform_out;
|
|
|
|
memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr, 8 );
|
2014-09-19 21:18:23 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
/* Set sequence_number to zero */
|
|
|
|
memset( ssl->cur_out_ctr + 2, 0, 6 );
|
2014-09-19 21:18:23 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
/* Increment epoch */
|
|
|
|
for( i = 2; i > 0; i-- )
|
|
|
|
if( ++ssl->cur_out_ctr[i - 1] != 0 )
|
|
|
|
break;
|
2014-08-20 10:43:01 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
/* The loop goes to its end iff the counter is wrapping */
|
|
|
|
if( i == 0 )
|
2014-08-20 10:43:01 +02:00
|
|
|
{
|
2020-02-05 15:44:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
|
|
|
|
return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
|
2014-08-20 10:43:01 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2015-04-08 12:49:31 +02:00
|
|
|
#endif /* MBEDTLS_SSL_PROTO_DTLS */
|
2020-02-05 15:44:10 +01:00
|
|
|
memset( ssl->cur_out_ctr, 0, 8 );
|
|
|
|
|
|
|
|
ssl->transform_out = ssl->transform_negotiate;
|
|
|
|
ssl->session_out = ssl->session_negotiate;
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
|
|
|
|
if( mbedtls_ssl_hw_record_activate != NULL )
|
2014-08-20 10:43:01 +02:00
|
|
|
{
|
2020-02-05 15:44:10 +01:00
|
|
|
if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
|
|
|
|
{
|
|
|
|
MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
|
|
|
|
return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
|
|
|
|
}
|
2014-02-18 11:33:49 +01:00
|
|
|
}
|
2020-02-05 15:44:10 +01:00
|
|
|
#endif
|
2014-02-18 11:33:49 +01:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
#if defined(MBEDTLS_SSL_PROTO_DTLS)
|
|
|
|
if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
|
|
|
|
mbedtls_ssl_send_flight_completed( ssl );
|
|
|
|
#endif
|
2016-10-13 18:21:01 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
|
2015-06-23 14:07:13 +02:00
|
|
|
{
|
2020-02-05 15:44:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
|
|
|
|
return( ret );
|
2015-06-23 14:07:13 +02:00
|
|
|
}
|
2014-02-18 11:33:49 +01:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_SSL_PROTO_DTLS)
|
2015-05-04 10:55:58 +02:00
|
|
|
if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
|
2020-02-05 15:44:10 +01:00
|
|
|
( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
|
2014-09-03 12:54:04 +02:00
|
|
|
{
|
2020-02-05 15:44:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
|
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
#endif
|
2018-08-16 10:30:53 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
|
2018-08-16 14:23:47 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_SSL_PROTO_SSL3)
|
|
|
|
#define SSL_MAX_HASH_LEN 36
|
|
|
|
#else
|
|
|
|
#define SSL_MAX_HASH_LEN 12
|
|
|
|
#endif
|
2018-08-16 14:23:47 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
|
|
|
|
{
|
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
|
|
|
unsigned int hash_len;
|
|
|
|
unsigned char buf[SSL_MAX_HASH_LEN];
|
2018-08-16 14:23:47 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
|
|
|
|
|
|
|
|
ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 );
|
2018-08-16 14:23:47 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
|
|
|
|
{
|
|
|
|
MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
|
|
|
|
return( ret );
|
2014-09-03 12:54:04 +02:00
|
|
|
}
|
2014-02-18 11:33:49 +01:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
|
|
|
|
{
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
|
|
|
|
mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
|
|
|
|
MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
|
|
|
|
return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
|
|
|
|
}
|
2014-09-24 10:52:58 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
/* There is currently no ciphersuite using another length with TLS 1.2 */
|
|
|
|
#if defined(MBEDTLS_SSL_PROTO_SSL3)
|
|
|
|
if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
|
|
|
|
hash_len = 36;
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
hash_len = 12;
|
2014-09-24 10:52:58 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED ||
|
|
|
|
ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
|
|
|
|
{
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
|
|
|
|
mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
|
|
|
|
MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
|
|
|
|
return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
|
|
|
|
}
|
|
|
|
|
|
|
|
if( mbedtls_ssl_safer_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
|
|
|
|
buf, hash_len ) != 0 )
|
|
|
|
{
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
|
|
|
|
mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
|
|
|
|
MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
|
|
|
|
return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
|
|
|
|
}
|
2019-10-29 10:13:33 +01:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
#if defined(MBEDTLS_SSL_RENEGOTIATION)
|
|
|
|
ssl->verify_data_len = hash_len;
|
|
|
|
memcpy( ssl->peer_verify_data, buf, hash_len );
|
|
|
|
#endif
|
2019-10-29 10:13:33 +01:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
if( ssl->handshake->resume != 0 )
|
|
|
|
{
|
|
|
|
#if defined(MBEDTLS_SSL_CLI_C)
|
|
|
|
if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
|
|
|
|
ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
|
|
|
|
#endif
|
|
|
|
#if defined(MBEDTLS_SSL_SRV_C)
|
|
|
|
if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
|
|
|
|
ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
else
|
|
|
|
ssl->state++;
|
2019-10-29 10:13:33 +01:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
#if defined(MBEDTLS_SSL_PROTO_DTLS)
|
|
|
|
if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
|
|
|
|
mbedtls_ssl_recv_flight_completed( ssl );
|
|
|
|
#endif
|
2019-10-29 10:13:33 +01:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
|
2019-10-29 10:13:33 +01:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
return( 0 );
|
2019-10-29 10:13:33 +01:00
|
|
|
}
|
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake )
|
2014-09-24 10:52:58 +02:00
|
|
|
{
|
2020-02-05 15:44:10 +01:00
|
|
|
memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) );
|
2014-09-24 10:52:58 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
|
|
|
|
defined(MBEDTLS_SSL_PROTO_TLS1_1)
|
|
|
|
mbedtls_md5_init( &handshake->fin_md5 );
|
|
|
|
mbedtls_sha1_init( &handshake->fin_sha1 );
|
|
|
|
mbedtls_md5_starts_ret( &handshake->fin_md5 );
|
|
|
|
mbedtls_sha1_starts_ret( &handshake->fin_sha1 );
|
|
|
|
#endif
|
|
|
|
#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
|
|
|
|
#if defined(MBEDTLS_SHA256_C)
|
|
|
|
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
|
|
|
handshake->fin_sha256_psa = psa_hash_operation_init();
|
|
|
|
psa_hash_setup( &handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
|
|
|
|
#else
|
|
|
|
mbedtls_sha256_init( &handshake->fin_sha256 );
|
|
|
|
mbedtls_sha256_starts_ret( &handshake->fin_sha256, 0 );
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
#if defined(MBEDTLS_SHA512_C)
|
|
|
|
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
|
|
|
handshake->fin_sha384_psa = psa_hash_operation_init();
|
|
|
|
psa_hash_setup( &handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
|
|
|
|
#else
|
|
|
|
mbedtls_sha512_init( &handshake->fin_sha512 );
|
|
|
|
mbedtls_sha512_starts_ret( &handshake->fin_sha512, 1 );
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
|
2014-09-24 14:41:11 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
handshake->update_checksum = ssl_update_checksum_start;
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
|
2020-03-10 12:19:08 +01:00
|
|
|
defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
|
2020-02-05 15:44:10 +01:00
|
|
|
mbedtls_ssl_sig_hash_set_init( &handshake->hash_algs );
|
|
|
|
#endif
|
2014-09-24 10:52:58 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
#if defined(MBEDTLS_DHM_C)
|
|
|
|
mbedtls_dhm_init( &handshake->dhm_ctx );
|
|
|
|
#endif
|
|
|
|
#if defined(MBEDTLS_ECDH_C)
|
|
|
|
mbedtls_ecdh_init( &handshake->ecdh_ctx );
|
|
|
|
#endif
|
|
|
|
#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
|
|
|
|
mbedtls_ecjpake_init( &handshake->ecjpake_ctx );
|
|
|
|
#if defined(MBEDTLS_SSL_CLI_C)
|
|
|
|
handshake->ecjpake_cache = NULL;
|
|
|
|
handshake->ecjpake_cache_len = 0;
|
|
|
|
#endif
|
|
|
|
#endif
|
2014-09-24 10:52:58 +02:00
|
|
|
|
2020-03-10 12:19:08 +01:00
|
|
|
#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
|
2020-02-05 15:44:10 +01:00
|
|
|
mbedtls_x509_crt_restart_init( &handshake->ecrs_ctx );
|
|
|
|
#endif
|
2014-09-24 10:52:58 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
|
|
|
|
handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
|
|
|
|
#endif
|
2014-09-24 10:52:58 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
|
|
|
|
!defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
|
|
|
|
mbedtls_pk_init( &handshake->peer_pubkey );
|
|
|
|
#endif
|
2014-09-24 10:52:58 +02:00
|
|
|
}
|
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
void mbedtls_ssl_transform_init( mbedtls_ssl_transform *transform )
|
2014-09-24 10:52:58 +02:00
|
|
|
{
|
2020-02-05 15:44:10 +01:00
|
|
|
memset( transform, 0, sizeof(mbedtls_ssl_transform) );
|
2014-09-24 10:52:58 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
mbedtls_cipher_init( &transform->cipher_ctx_enc );
|
|
|
|
mbedtls_cipher_init( &transform->cipher_ctx_dec );
|
2014-09-24 10:52:58 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
|
|
|
|
mbedtls_md_init( &transform->md_ctx_enc );
|
|
|
|
mbedtls_md_init( &transform->md_ctx_dec );
|
|
|
|
#endif
|
|
|
|
}
|
2014-09-24 10:52:58 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
void mbedtls_ssl_session_init( mbedtls_ssl_session *session )
|
|
|
|
{
|
|
|
|
memset( session, 0, sizeof(mbedtls_ssl_session) );
|
2014-09-24 10:52:58 +02:00
|
|
|
}
|
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
static int ssl_handshake_init( mbedtls_ssl_context *ssl )
|
2015-09-08 17:50:29 +02:00
|
|
|
{
|
2020-02-05 15:44:10 +01:00
|
|
|
/* Clear old handshake information if present */
|
|
|
|
if( ssl->transform_negotiate )
|
|
|
|
mbedtls_ssl_transform_free( ssl->transform_negotiate );
|
|
|
|
if( ssl->session_negotiate )
|
|
|
|
mbedtls_ssl_session_free( ssl->session_negotiate );
|
|
|
|
if( ssl->handshake )
|
|
|
|
mbedtls_ssl_handshake_free( ssl );
|
2015-09-08 17:50:29 +02:00
|
|
|
|
|
|
|
/*
|
2020-02-05 15:44:10 +01:00
|
|
|
* Either the pointers are now NULL or cleared properly and can be freed.
|
|
|
|
* Now allocate missing structures.
|
2015-09-08 17:50:29 +02:00
|
|
|
*/
|
2020-02-05 15:44:10 +01:00
|
|
|
if( ssl->transform_negotiate == NULL )
|
2015-09-08 17:50:29 +02:00
|
|
|
{
|
2020-02-05 15:44:10 +01:00
|
|
|
ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
|
2015-09-08 17:50:29 +02:00
|
|
|
}
|
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
if( ssl->session_negotiate == NULL )
|
2015-09-08 17:50:29 +02:00
|
|
|
{
|
2020-02-05 15:44:10 +01:00
|
|
|
ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
|
2015-09-08 17:50:29 +02:00
|
|
|
}
|
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
if( ssl->handshake == NULL )
|
2015-09-08 17:50:29 +02:00
|
|
|
{
|
2020-02-05 15:44:10 +01:00
|
|
|
ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
|
2015-09-08 17:50:29 +02:00
|
|
|
}
|
2020-03-03 16:39:58 +01:00
|
|
|
#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
|
|
|
|
/* If the buffers are too small - reallocate */
|
|
|
|
{
|
|
|
|
int modified = 0;
|
2020-06-08 13:08:03 +02:00
|
|
|
size_t written_in = 0, iv_offset_in = 0, len_offset_in = 0;
|
|
|
|
size_t written_out = 0, iv_offset_out = 0, len_offset_out = 0;
|
2020-04-03 12:40:47 +02:00
|
|
|
if( ssl->in_buf != NULL )
|
2020-03-03 16:39:58 +01:00
|
|
|
{
|
2020-04-03 12:40:47 +02:00
|
|
|
written_in = ssl->in_msg - ssl->in_buf;
|
2020-06-08 13:08:03 +02:00
|
|
|
iv_offset_in = ssl->in_iv - ssl->in_buf;
|
|
|
|
len_offset_in = ssl->in_len - ssl->in_buf;
|
2020-04-03 12:40:47 +02:00
|
|
|
if( ssl->in_buf_len < MBEDTLS_SSL_IN_BUFFER_LEN )
|
2020-03-03 16:39:58 +01:00
|
|
|
{
|
2020-04-03 12:40:47 +02:00
|
|
|
if( resize_buffer( &ssl->in_buf, MBEDTLS_SSL_IN_BUFFER_LEN,
|
|
|
|
&ssl->in_buf_len ) != 0 )
|
|
|
|
{
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 1, ( "input buffer resizing failed - out of memory" ) );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 2, ( "Reallocating in_buf to %d", MBEDTLS_SSL_IN_BUFFER_LEN ) );
|
|
|
|
modified = 1;
|
|
|
|
}
|
2020-03-03 16:39:58 +01:00
|
|
|
}
|
|
|
|
}
|
2020-04-03 12:40:47 +02:00
|
|
|
|
|
|
|
if( ssl->out_buf != NULL )
|
2020-03-03 16:39:58 +01:00
|
|
|
{
|
2020-04-03 12:40:47 +02:00
|
|
|
written_out = ssl->out_msg - ssl->out_buf;
|
2020-06-08 13:08:03 +02:00
|
|
|
iv_offset_out = ssl->out_iv - ssl->out_buf;
|
|
|
|
len_offset_out = ssl->out_len - ssl->out_buf;
|
2020-04-03 12:40:47 +02:00
|
|
|
if( ssl->out_buf_len < MBEDTLS_SSL_OUT_BUFFER_LEN )
|
2020-03-03 16:39:58 +01:00
|
|
|
{
|
2020-04-03 12:40:47 +02:00
|
|
|
if( resize_buffer( &ssl->out_buf, MBEDTLS_SSL_OUT_BUFFER_LEN,
|
|
|
|
&ssl->out_buf_len ) != 0 )
|
|
|
|
{
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 1, ( "output buffer resizing failed - out of memory" ) );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 2, ( "Reallocating out_buf to %d", MBEDTLS_SSL_OUT_BUFFER_LEN ) );
|
|
|
|
modified = 1;
|
|
|
|
}
|
2020-03-03 16:39:58 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if( modified )
|
|
|
|
{
|
|
|
|
/* Update pointers here to avoid doing it twice. */
|
|
|
|
mbedtls_ssl_reset_in_out_pointers( ssl );
|
2020-04-03 12:40:47 +02:00
|
|
|
/* Fields below might not be properly updated with record
|
2020-06-08 13:08:03 +02:00
|
|
|
* splitting or with CID, so they are manually updated here. */
|
2020-04-03 12:40:47 +02:00
|
|
|
ssl->out_msg = ssl->out_buf + written_out;
|
2020-06-08 13:08:03 +02:00
|
|
|
ssl->out_len = ssl->out_buf + len_offset_out;
|
|
|
|
ssl->out_iv = ssl->out_buf + iv_offset_out;
|
|
|
|
|
2020-04-03 12:40:47 +02:00
|
|
|
ssl->in_msg = ssl->in_buf + written_in;
|
2020-06-08 13:08:03 +02:00
|
|
|
ssl->in_len = ssl->in_buf + len_offset_in;
|
|
|
|
ssl->in_iv = ssl->in_buf + iv_offset_in;
|
2020-03-03 16:39:58 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2015-09-08 17:50:29 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
/* All pointers should exist and can be directly freed without issue */
|
|
|
|
if( ssl->handshake == NULL ||
|
|
|
|
ssl->transform_negotiate == NULL ||
|
|
|
|
ssl->session_negotiate == NULL )
|
|
|
|
{
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
|
2015-09-08 17:50:29 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
mbedtls_free( ssl->handshake );
|
|
|
|
mbedtls_free( ssl->transform_negotiate );
|
|
|
|
mbedtls_free( ssl->session_negotiate );
|
2015-09-08 17:50:29 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
ssl->handshake = NULL;
|
|
|
|
ssl->transform_negotiate = NULL;
|
|
|
|
ssl->session_negotiate = NULL;
|
2015-09-08 10:30:55 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
|
Check same-port-reconnect from client outside of record hdr parsing
Previously, `ssl_handle_possible_reconnect()` was part of
`ssl_parse_record_header()`, which was required to return a non-zero error
code to indicate a record which should not be further processed because it
was invalid, unexpected, duplicate, .... In this case, some error codes
would lead to some actions to be taken, e.g. `MBEDTLS_ERR_SSL_EARLY_MESSAGE`
to potential buffering of the record, but eventually, the record would be
dropped regardless of the precise value of the error code. The error code
`MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED` returned from
`ssl_handle_possible_reconnect()` did not receive any special treatment and
lead to silent dopping of the record - in particular, it was never returned
to the user.
In the new logic this commit introduces, `ssl_handle_possible_reconnect()` is
part of `ssl_check_client_reconnect()` which is triggered _after_
`ssl_parse_record_header()` found an unexpected record, which is already in
the code-path eventually dropping the record; we want to leave this code-path
only if a valid cookie has been found and we want to reset, but do nothing
otherwise. That's why `ssl_handle_possible_reconnect()` now returns `0` unless
a valid cookie has been found or a fatal error occurred.
2019-07-10 15:37:41 +02:00
|
|
|
}
|
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
/* Initialize structures */
|
|
|
|
mbedtls_ssl_session_init( ssl->session_negotiate );
|
|
|
|
mbedtls_ssl_transform_init( ssl->transform_negotiate );
|
|
|
|
ssl_handshake_params_init( ssl->handshake );
|
2015-09-08 10:30:55 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
#if defined(MBEDTLS_SSL_PROTO_DTLS)
|
|
|
|
if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
|
2015-09-08 10:30:55 +02:00
|
|
|
{
|
2020-02-05 15:44:10 +01:00
|
|
|
ssl->handshake->alt_transform_out = ssl->transform_out;
|
2015-09-08 10:30:55 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
|
|
|
|
ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
|
|
|
|
else
|
|
|
|
ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
|
2015-09-08 10:30:55 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
mbedtls_ssl_set_timer( ssl, 0 );
|
2015-09-08 11:58:14 +02:00
|
|
|
}
|
2020-02-05 15:44:10 +01:00
|
|
|
#endif
|
2015-09-08 10:30:55 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
return( 0 );
|
2015-09-08 10:30:55 +02:00
|
|
|
}
|
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
|
|
|
|
/* Dummy cookie callbacks for defaults */
|
|
|
|
static int ssl_cookie_write_dummy( void *ctx,
|
|
|
|
unsigned char **p, unsigned char *end,
|
|
|
|
const unsigned char *cli_id, size_t cli_id_len )
|
2019-05-03 14:25:54 +02:00
|
|
|
{
|
2020-02-05 15:44:10 +01:00
|
|
|
((void) ctx);
|
|
|
|
((void) p);
|
|
|
|
((void) end);
|
|
|
|
((void) cli_id);
|
|
|
|
((void) cli_id_len);
|
2019-05-03 14:25:54 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
|
2019-05-03 14:25:54 +02:00
|
|
|
}
|
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
static int ssl_cookie_check_dummy( void *ctx,
|
|
|
|
const unsigned char *cookie, size_t cookie_len,
|
|
|
|
const unsigned char *cli_id, size_t cli_id_len )
|
2009-01-03 22:22:43 +01:00
|
|
|
{
|
2020-02-05 15:44:10 +01:00
|
|
|
((void) ctx);
|
|
|
|
((void) cookie);
|
|
|
|
((void) cookie_len);
|
|
|
|
((void) cli_id);
|
|
|
|
((void) cli_id_len);
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
|
|
|
|
}
|
|
|
|
#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
|
2014-09-02 13:39:16 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
/*
|
|
|
|
* Initialize an SSL context
|
|
|
|
*/
|
|
|
|
void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
|
|
|
|
{
|
|
|
|
memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
|
|
|
|
}
|
2014-09-02 13:39:16 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
/*
|
|
|
|
* Setup an SSL context
|
|
|
|
*/
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
|
|
|
|
const mbedtls_ssl_config *conf )
|
|
|
|
{
|
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2019-11-28 15:29:44 +01:00
|
|
|
size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
|
|
|
|
size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
|
2019-07-11 13:29:35 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
ssl->conf = conf;
|
2019-07-11 13:29:35 +02:00
|
|
|
|
|
|
|
/*
|
2020-02-05 15:44:10 +01:00
|
|
|
* Prepare base structures
|
2019-07-11 13:29:35 +02:00
|
|
|
*/
|
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
/* Set to NULL in case of an error condition */
|
|
|
|
ssl->out_buf = NULL;
|
|
|
|
|
2019-11-28 15:29:44 +01:00
|
|
|
#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
|
|
|
|
ssl->in_buf_len = in_buf_len;
|
|
|
|
#endif
|
|
|
|
ssl->in_buf = mbedtls_calloc( 1, in_buf_len );
|
2020-02-05 15:44:10 +01:00
|
|
|
if( ssl->in_buf == NULL )
|
2019-07-11 13:29:35 +02:00
|
|
|
{
|
2019-11-28 15:29:44 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", in_buf_len ) );
|
2020-02-05 15:44:10 +01:00
|
|
|
ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
|
|
|
|
goto error;
|
2019-07-11 13:29:35 +02:00
|
|
|
}
|
|
|
|
|
2019-11-28 15:29:44 +01:00
|
|
|
#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
|
|
|
|
ssl->out_buf_len = out_buf_len;
|
|
|
|
#endif
|
|
|
|
ssl->out_buf = mbedtls_calloc( 1, out_buf_len );
|
2020-02-05 15:44:10 +01:00
|
|
|
if( ssl->out_buf == NULL )
|
2019-07-11 13:29:35 +02:00
|
|
|
{
|
2019-11-28 15:29:44 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", out_buf_len ) );
|
2020-02-05 15:44:10 +01:00
|
|
|
ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
|
|
|
|
goto error;
|
2019-07-11 13:29:35 +02:00
|
|
|
}
|
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
mbedtls_ssl_reset_in_out_pointers( ssl );
|
2019-07-11 13:29:35 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
|
|
|
|
goto error;
|
2019-07-11 13:29:35 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
return( 0 );
|
2019-07-11 13:29:35 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
error:
|
|
|
|
mbedtls_free( ssl->in_buf );
|
|
|
|
mbedtls_free( ssl->out_buf );
|
2019-05-08 13:03:28 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
ssl->conf = NULL;
|
2019-07-10 15:50:10 +02:00
|
|
|
|
2019-11-28 15:29:44 +01:00
|
|
|
#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
|
|
|
|
ssl->in_buf_len = 0;
|
|
|
|
ssl->out_buf_len = 0;
|
|
|
|
#endif
|
2020-02-05 15:44:10 +01:00
|
|
|
ssl->in_buf = NULL;
|
|
|
|
ssl->out_buf = NULL;
|
2019-07-11 13:29:35 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
ssl->in_hdr = NULL;
|
|
|
|
ssl->in_ctr = NULL;
|
|
|
|
ssl->in_len = NULL;
|
|
|
|
ssl->in_iv = NULL;
|
|
|
|
ssl->in_msg = NULL;
|
2017-06-26 16:11:59 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
ssl->out_hdr = NULL;
|
|
|
|
ssl->out_ctr = NULL;
|
|
|
|
ssl->out_len = NULL;
|
|
|
|
ssl->out_iv = NULL;
|
|
|
|
ssl->out_msg = NULL;
|
2017-06-26 16:11:59 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
return( ret );
|
|
|
|
}
|
2014-08-11 19:27:24 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
/*
|
|
|
|
* Reset an initialized and used SSL context for re-use while retaining
|
|
|
|
* all application-set variables, function pointers and data.
|
|
|
|
*
|
|
|
|
* If partial is non-zero, keep data in the input buffer and client ID.
|
|
|
|
* (Use when a DTLS client reconnects from the same port.)
|
|
|
|
*/
|
|
|
|
int mbedtls_ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
|
|
|
|
{
|
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2019-11-28 15:29:44 +01:00
|
|
|
#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
|
|
|
|
size_t in_buf_len = ssl->in_buf_len;
|
|
|
|
size_t out_buf_len = ssl->out_buf_len;
|
|
|
|
#else
|
|
|
|
size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
|
|
|
|
size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
|
|
|
|
#endif
|
2015-12-03 16:13:17 +01:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || \
|
|
|
|
!defined(MBEDTLS_SSL_SRV_C)
|
|
|
|
((void) partial);
|
|
|
|
#endif
|
2015-12-03 16:13:17 +01:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
|
2019-07-11 13:29:35 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
/* Cancel any possibly running timer */
|
|
|
|
mbedtls_ssl_set_timer( ssl, 0 );
|
2015-12-03 16:13:17 +01:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
#if defined(MBEDTLS_SSL_RENEGOTIATION)
|
|
|
|
ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
|
|
|
|
ssl->renego_records_seen = 0;
|
2019-05-08 13:03:28 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
ssl->verify_data_len = 0;
|
|
|
|
memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
|
|
|
|
memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
|
|
|
|
#endif
|
|
|
|
ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
|
2019-07-11 13:29:35 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
ssl->in_offt = NULL;
|
|
|
|
mbedtls_ssl_reset_in_out_pointers( ssl );
|
2019-07-11 13:29:35 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
ssl->in_msgtype = 0;
|
|
|
|
ssl->in_msglen = 0;
|
|
|
|
#if defined(MBEDTLS_SSL_PROTO_DTLS)
|
|
|
|
ssl->next_record_offset = 0;
|
|
|
|
ssl->in_epoch = 0;
|
|
|
|
#endif
|
|
|
|
#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
|
|
|
|
mbedtls_ssl_dtls_replay_reset( ssl );
|
|
|
|
#endif
|
2019-05-08 13:03:28 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
ssl->in_hslen = 0;
|
|
|
|
ssl->nb_zero = 0;
|
2019-05-08 13:03:28 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
ssl->keep_current_message = 0;
|
2015-12-03 16:13:17 +01:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
ssl->out_msgtype = 0;
|
|
|
|
ssl->out_msglen = 0;
|
|
|
|
ssl->out_left = 0;
|
|
|
|
#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
|
|
|
|
if( ssl->split_done != MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED )
|
|
|
|
ssl->split_done = 0;
|
|
|
|
#endif
|
2019-07-10 18:20:01 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
|
2015-09-04 12:58:55 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
ssl->transform_in = NULL;
|
|
|
|
ssl->transform_out = NULL;
|
2018-08-16 15:56:31 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
ssl->session_in = NULL;
|
|
|
|
ssl->session_out = NULL;
|
|
|
|
|
2019-11-28 15:29:44 +01:00
|
|
|
memset( ssl->out_buf, 0, out_buf_len );
|
2020-02-05 15:44:10 +01:00
|
|
|
|
|
|
|
#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
|
|
|
|
if( partial == 0 )
|
|
|
|
#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
|
|
|
|
{
|
|
|
|
ssl->in_left = 0;
|
2019-11-28 15:29:44 +01:00
|
|
|
memset( ssl->in_buf, 0, in_buf_len );
|
2020-02-05 15:44:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
|
|
|
|
if( mbedtls_ssl_hw_record_reset != NULL )
|
|
|
|
{
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_reset()" ) );
|
|
|
|
if( ( ret = mbedtls_ssl_hw_record_reset( ssl ) ) != 0 )
|
2014-09-24 13:29:58 +02:00
|
|
|
{
|
2020-02-05 15:44:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_reset", ret );
|
|
|
|
return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
|
2014-09-03 16:02:42 +02:00
|
|
|
}
|
2014-09-24 13:29:58 +02:00
|
|
|
}
|
2020-02-05 15:44:10 +01:00
|
|
|
#endif
|
2014-09-03 16:02:42 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
if( ssl->transform )
|
|
|
|
{
|
|
|
|
mbedtls_ssl_transform_free( ssl->transform );
|
|
|
|
mbedtls_free( ssl->transform );
|
|
|
|
ssl->transform = NULL;
|
|
|
|
}
|
2017-05-26 17:07:36 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
if( ssl->session )
|
|
|
|
{
|
|
|
|
mbedtls_ssl_session_free( ssl->session );
|
|
|
|
mbedtls_free( ssl->session );
|
|
|
|
ssl->session = NULL;
|
|
|
|
}
|
2017-05-26 17:07:36 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
#if defined(MBEDTLS_SSL_ALPN)
|
|
|
|
ssl->alpn_chosen = NULL;
|
|
|
|
#endif
|
Check same-port-reconnect from client outside of record hdr parsing
Previously, `ssl_handle_possible_reconnect()` was part of
`ssl_parse_record_header()`, which was required to return a non-zero error
code to indicate a record which should not be further processed because it
was invalid, unexpected, duplicate, .... In this case, some error codes
would lead to some actions to be taken, e.g. `MBEDTLS_ERR_SSL_EARLY_MESSAGE`
to potential buffering of the record, but eventually, the record would be
dropped regardless of the precise value of the error code. The error code
`MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED` returned from
`ssl_handle_possible_reconnect()` did not receive any special treatment and
lead to silent dopping of the record - in particular, it was never returned
to the user.
In the new logic this commit introduces, `ssl_handle_possible_reconnect()` is
part of `ssl_check_client_reconnect()` which is triggered _after_
`ssl_parse_record_header()` found an unexpected record, which is already in
the code-path eventually dropping the record; we want to leave this code-path
only if a valid cookie has been found and we want to reset, but do nothing
otherwise. That's why `ssl_handle_possible_reconnect()` now returns `0` unless
a valid cookie has been found or a fatal error occurred.
2019-07-10 15:37:41 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
|
|
|
|
#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
|
|
|
|
if( partial == 0 )
|
|
|
|
#endif
|
Check same-port-reconnect from client outside of record hdr parsing
Previously, `ssl_handle_possible_reconnect()` was part of
`ssl_parse_record_header()`, which was required to return a non-zero error
code to indicate a record which should not be further processed because it
was invalid, unexpected, duplicate, .... In this case, some error codes
would lead to some actions to be taken, e.g. `MBEDTLS_ERR_SSL_EARLY_MESSAGE`
to potential buffering of the record, but eventually, the record would be
dropped regardless of the precise value of the error code. The error code
`MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED` returned from
`ssl_handle_possible_reconnect()` did not receive any special treatment and
lead to silent dopping of the record - in particular, it was never returned
to the user.
In the new logic this commit introduces, `ssl_handle_possible_reconnect()` is
part of `ssl_check_client_reconnect()` which is triggered _after_
`ssl_parse_record_header()` found an unexpected record, which is already in
the code-path eventually dropping the record; we want to leave this code-path
only if a valid cookie has been found and we want to reset, but do nothing
otherwise. That's why `ssl_handle_possible_reconnect()` now returns `0` unless
a valid cookie has been found or a fatal error occurred.
2019-07-10 15:37:41 +02:00
|
|
|
{
|
2020-02-05 15:44:10 +01:00
|
|
|
mbedtls_free( ssl->cli_id );
|
|
|
|
ssl->cli_id = NULL;
|
|
|
|
ssl->cli_id_len = 0;
|
2017-05-26 17:07:36 +02:00
|
|
|
}
|
2020-02-05 15:44:10 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
|
|
|
|
return( ret );
|
2017-05-26 17:07:36 +02:00
|
|
|
|
2014-09-08 16:14:10 +02:00
|
|
|
return( 0 );
|
|
|
|
}
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2014-09-08 16:14:10 +02:00
|
|
|
/*
|
2020-02-05 15:44:10 +01:00
|
|
|
* Reset an initialized and used SSL context for re-use while retaining
|
|
|
|
* all application-set variables, function pointers and data.
|
2014-09-08 16:14:10 +02:00
|
|
|
*/
|
2020-02-05 15:44:10 +01:00
|
|
|
int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
|
2014-09-08 16:14:10 +02:00
|
|
|
{
|
2020-02-05 15:44:10 +01:00
|
|
|
return( mbedtls_ssl_session_reset_int( ssl, 0 ) );
|
|
|
|
}
|
2014-07-10 17:54:52 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
/*
|
|
|
|
* SSL set accessors
|
|
|
|
*/
|
|
|
|
void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
|
|
|
|
{
|
|
|
|
conf->endpoint = endpoint;
|
|
|
|
}
|
2012-05-08 11:17:57 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
|
|
|
|
{
|
|
|
|
conf->transport = transport;
|
|
|
|
}
|
2012-12-19 14:41:14 +01:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
|
|
|
|
void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
|
|
|
|
{
|
|
|
|
conf->anti_replay = mode;
|
|
|
|
}
|
|
|
|
#endif
|
2017-12-27 22:28:58 +01:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
|
|
|
|
void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf, unsigned limit )
|
|
|
|
{
|
|
|
|
conf->badmac_limit = limit;
|
|
|
|
}
|
|
|
|
#endif
|
2019-05-14 12:30:10 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
#if defined(MBEDTLS_SSL_PROTO_DTLS)
|
2019-05-08 14:02:55 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl,
|
|
|
|
unsigned allow_packing )
|
|
|
|
{
|
|
|
|
ssl->disable_datagram_packing = !allow_packing;
|
|
|
|
}
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
|
|
|
|
uint32_t min, uint32_t max )
|
|
|
|
{
|
|
|
|
conf->hs_timeout_min = min;
|
|
|
|
conf->hs_timeout_max = max;
|
|
|
|
}
|
|
|
|
#endif
|
2017-12-27 22:28:58 +01:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
|
|
|
|
{
|
|
|
|
conf->authmode = authmode;
|
|
|
|
}
|
2018-08-07 15:29:29 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
#if defined(MBEDTLS_X509_CRT_PARSE_C)
|
|
|
|
void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
|
|
|
|
int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
|
|
|
|
void *p_vrfy )
|
|
|
|
{
|
|
|
|
conf->f_vrfy = f_vrfy;
|
|
|
|
conf->p_vrfy = p_vrfy;
|
|
|
|
}
|
|
|
|
#endif /* MBEDTLS_X509_CRT_PARSE_C */
|
2019-05-08 12:57:13 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
|
|
|
|
int (*f_rng)(void *, unsigned char *, size_t),
|
|
|
|
void *p_rng )
|
|
|
|
{
|
|
|
|
conf->f_rng = f_rng;
|
|
|
|
conf->p_rng = p_rng;
|
|
|
|
}
|
2017-12-27 22:28:58 +01:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
|
|
|
|
void (*f_dbg)(void *, int, const char *, int, const char *),
|
|
|
|
void *p_dbg )
|
|
|
|
{
|
|
|
|
conf->f_dbg = f_dbg;
|
|
|
|
conf->p_dbg = p_dbg;
|
|
|
|
}
|
2017-12-27 22:28:58 +01:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
|
|
|
|
void *p_bio,
|
|
|
|
mbedtls_ssl_send_t *f_send,
|
|
|
|
mbedtls_ssl_recv_t *f_recv,
|
|
|
|
mbedtls_ssl_recv_timeout_t *f_recv_timeout )
|
|
|
|
{
|
|
|
|
ssl->p_bio = p_bio;
|
|
|
|
ssl->f_send = f_send;
|
|
|
|
ssl->f_recv = f_recv;
|
|
|
|
ssl->f_recv_timeout = f_recv_timeout;
|
|
|
|
}
|
2017-12-27 22:28:58 +01:00
|
|
|
|
|
|
|
#if defined(MBEDTLS_SSL_PROTO_DTLS)
|
2020-02-05 15:44:10 +01:00
|
|
|
void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu )
|
|
|
|
{
|
|
|
|
ssl->mtu = mtu;
|
|
|
|
}
|
2017-12-27 22:28:58 +01:00
|
|
|
#endif
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
|
|
|
|
{
|
|
|
|
conf->read_timeout = timeout;
|
|
|
|
}
|
2014-09-24 13:29:58 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
|
|
|
|
void *p_timer,
|
|
|
|
mbedtls_ssl_set_timer_t *f_set_timer,
|
|
|
|
mbedtls_ssl_get_timer_t *f_get_timer )
|
|
|
|
{
|
|
|
|
ssl->p_timer = p_timer;
|
|
|
|
ssl->f_set_timer = f_set_timer;
|
|
|
|
ssl->f_get_timer = f_get_timer;
|
2019-07-09 18:30:02 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
/* Make sure we start with no timer running */
|
|
|
|
mbedtls_ssl_set_timer( ssl, 0 );
|
2014-09-08 16:14:10 +02:00
|
|
|
}
|
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
#if defined(MBEDTLS_SSL_SRV_C)
|
|
|
|
void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
|
|
|
|
void *p_cache,
|
|
|
|
int (*f_get_cache)(void *, mbedtls_ssl_session *),
|
|
|
|
int (*f_set_cache)(void *, const mbedtls_ssl_session *) )
|
|
|
|
{
|
|
|
|
conf->p_cache = p_cache;
|
|
|
|
conf->f_get_cache = f_get_cache;
|
|
|
|
conf->f_set_cache = f_set_cache;
|
|
|
|
}
|
|
|
|
#endif /* MBEDTLS_SSL_SRV_C */
|
2018-08-15 15:05:04 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
#if defined(MBEDTLS_SSL_CLI_C)
|
|
|
|
int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
|
2014-09-08 16:14:10 +02:00
|
|
|
{
|
2019-12-16 12:46:15 +01:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2014-09-08 16:14:10 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
if( ssl == NULL ||
|
|
|
|
session == NULL ||
|
|
|
|
ssl->session_negotiate == NULL ||
|
|
|
|
ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
|
2017-05-24 10:16:26 +02:00
|
|
|
{
|
2020-02-05 15:44:10 +01:00
|
|
|
return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
|
|
|
|
}
|
2017-05-24 10:16:26 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
if( ( ret = mbedtls_ssl_session_copy( ssl->session_negotiate,
|
|
|
|
session ) ) != 0 )
|
|
|
|
return( ret );
|
2018-08-15 15:14:59 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
ssl->handshake->resume = 1;
|
2018-08-15 15:26:08 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
#endif /* MBEDTLS_SSL_CLI_C */
|
2018-08-15 15:48:01 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
|
|
|
|
const int *ciphersuites )
|
|
|
|
{
|
|
|
|
conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = ciphersuites;
|
|
|
|
conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = ciphersuites;
|
|
|
|
conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = ciphersuites;
|
|
|
|
conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = ciphersuites;
|
|
|
|
}
|
2016-10-13 18:21:01 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
void mbedtls_ssl_conf_ciphersuites_for_version( mbedtls_ssl_config *conf,
|
|
|
|
const int *ciphersuites,
|
|
|
|
int major, int minor )
|
|
|
|
{
|
|
|
|
if( major != MBEDTLS_SSL_MAJOR_VERSION_3 )
|
|
|
|
return;
|
2017-05-24 10:16:26 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
if( minor < MBEDTLS_SSL_MINOR_VERSION_0 || minor > MBEDTLS_SSL_MINOR_VERSION_3 )
|
|
|
|
return;
|
2018-08-15 15:48:01 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
conf->ciphersuite_list[minor] = ciphersuites;
|
|
|
|
}
|
2018-08-15 15:48:01 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
#if defined(MBEDTLS_X509_CRT_PARSE_C)
|
|
|
|
void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
|
|
|
|
const mbedtls_x509_crt_profile *profile )
|
|
|
|
{
|
|
|
|
conf->cert_profile = profile;
|
|
|
|
}
|
2017-05-24 10:16:26 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
/* Append a new keycert entry to a (possibly empty) list */
|
|
|
|
static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
|
|
|
|
mbedtls_x509_crt *cert,
|
|
|
|
mbedtls_pk_context *key )
|
|
|
|
{
|
|
|
|
mbedtls_ssl_key_cert *new_cert;
|
2016-10-13 18:21:01 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
|
|
|
|
if( new_cert == NULL )
|
|
|
|
return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
|
|
|
|
|
|
|
|
new_cert->cert = cert;
|
|
|
|
new_cert->key = key;
|
|
|
|
new_cert->next = NULL;
|
|
|
|
|
|
|
|
/* Update head is the list was null, else add to the end */
|
|
|
|
if( *head == NULL )
|
|
|
|
{
|
|
|
|
*head = new_cert;
|
2016-10-13 18:21:01 +02:00
|
|
|
}
|
2017-05-24 10:16:26 +02:00
|
|
|
else
|
2016-10-13 18:21:01 +02:00
|
|
|
{
|
2020-02-05 15:44:10 +01:00
|
|
|
mbedtls_ssl_key_cert *cur = *head;
|
|
|
|
while( cur->next != NULL )
|
|
|
|
cur = cur->next;
|
|
|
|
cur->next = new_cert;
|
2016-10-13 18:21:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
|
|
|
|
mbedtls_x509_crt *own_cert,
|
|
|
|
mbedtls_pk_context *pk_key )
|
2016-10-13 18:21:01 +02:00
|
|
|
{
|
2020-02-05 15:44:10 +01:00
|
|
|
return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
|
2018-08-15 15:48:01 +02:00
|
|
|
}
|
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
|
|
|
|
mbedtls_x509_crt *ca_chain,
|
|
|
|
mbedtls_x509_crl *ca_crl )
|
2018-08-15 15:48:01 +02:00
|
|
|
{
|
2020-02-05 15:44:10 +01:00
|
|
|
conf->ca_chain = ca_chain;
|
|
|
|
conf->ca_crl = ca_crl;
|
2018-08-20 10:39:42 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
|
|
|
|
/* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
|
|
|
|
* cannot be used together. */
|
|
|
|
conf->f_ca_cb = NULL;
|
|
|
|
conf->p_ca_cb = NULL;
|
|
|
|
#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
|
|
|
|
}
|
2018-08-15 16:11:57 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
|
|
|
|
void mbedtls_ssl_conf_ca_cb( mbedtls_ssl_config *conf,
|
|
|
|
mbedtls_x509_crt_ca_cb_t f_ca_cb,
|
|
|
|
void *p_ca_cb )
|
|
|
|
{
|
|
|
|
conf->f_ca_cb = f_ca_cb;
|
|
|
|
conf->p_ca_cb = p_ca_cb;
|
2018-08-15 16:11:57 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
/* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
|
|
|
|
* cannot be used together. */
|
|
|
|
conf->ca_chain = NULL;
|
|
|
|
conf->ca_crl = NULL;
|
|
|
|
}
|
|
|
|
#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
|
|
|
|
#endif /* MBEDTLS_X509_CRT_PARSE_C */
|
2018-08-15 16:11:57 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
|
|
|
|
int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
|
|
|
|
mbedtls_x509_crt *own_cert,
|
|
|
|
mbedtls_pk_context *pk_key )
|
|
|
|
{
|
|
|
|
return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
|
|
|
|
own_cert, pk_key ) );
|
|
|
|
}
|
Implement future message buffering and loading
This commit implements future handshake message buffering
and loading by implementing ssl_load_buffered_message()
and ssl_buffer_message().
Whenever a handshake message is received which is
- a future handshake message (i.e., the sequence number
is larger than the next expected one), or which is
- a proper fragment of the next expected handshake message,
ssl_buffer_message() is called, which does the following:
- Ignore message if its sequence number is too far ahead
of the next expected sequence number, as controlled by
the macro constant MBEDTLS_SSL_MAX_BUFFERED_HS.
- Otherwise, check if buffering for the message with the
respective sequence number has already commenced.
- If not, allocate space to back up the message within
the buffering substructure of mbedtls_ssl_handshake_params.
If the message is a proper fragment, allocate additional
space for a reassembly bitmap; if it is a full message,
omit the bitmap. In any case, fall throuh to the next case.
- If the message has already been buffered, check that
the header is the same, and add the current fragment
if the message is not yet complete (this excludes the
case where a future message has been received in a single
fragment, hence omitting the bitmap, and is afterwards
also received as a series of proper fragments; in this
case, the proper fragments will be ignored).
For loading buffered messages in ssl_load_buffered_message(),
the approach is the following:
- Check the first entry in the buffering window (the window
is always based at the next expected handshake message).
If buffering hasn't started or if reassembly is still
in progress, ignore. If the next expected message has been
fully received, copy it to the input buffer (which is empty,
as ssl_load_buffered_message() is only called in this case).
2018-08-16 14:55:32 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
|
|
|
|
mbedtls_x509_crt *ca_chain,
|
|
|
|
mbedtls_x509_crl *ca_crl )
|
|
|
|
{
|
|
|
|
ssl->handshake->sni_ca_chain = ca_chain;
|
|
|
|
ssl->handshake->sni_ca_crl = ca_crl;
|
|
|
|
}
|
Implement future message buffering and loading
This commit implements future handshake message buffering
and loading by implementing ssl_load_buffered_message()
and ssl_buffer_message().
Whenever a handshake message is received which is
- a future handshake message (i.e., the sequence number
is larger than the next expected one), or which is
- a proper fragment of the next expected handshake message,
ssl_buffer_message() is called, which does the following:
- Ignore message if its sequence number is too far ahead
of the next expected sequence number, as controlled by
the macro constant MBEDTLS_SSL_MAX_BUFFERED_HS.
- Otherwise, check if buffering for the message with the
respective sequence number has already commenced.
- If not, allocate space to back up the message within
the buffering substructure of mbedtls_ssl_handshake_params.
If the message is a proper fragment, allocate additional
space for a reassembly bitmap; if it is a full message,
omit the bitmap. In any case, fall throuh to the next case.
- If the message has already been buffered, check that
the header is the same, and add the current fragment
if the message is not yet complete (this excludes the
case where a future message has been received in a single
fragment, hence omitting the bitmap, and is afterwards
also received as a series of proper fragments; in this
case, the proper fragments will be ignored).
For loading buffered messages in ssl_load_buffered_message(),
the approach is the following:
- Check the first entry in the buffering window (the window
is always based at the next expected handshake message).
If buffering hasn't started or if reassembly is still
in progress, ignore. If the next expected message has been
fully received, copy it to the input buffer (which is empty,
as ssl_load_buffered_message() is only called in this case).
2018-08-16 14:55:32 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
|
|
|
|
int authmode )
|
|
|
|
{
|
|
|
|
ssl->handshake->sni_authmode = authmode;
|
|
|
|
}
|
|
|
|
#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
|
Implement future message buffering and loading
This commit implements future handshake message buffering
and loading by implementing ssl_load_buffered_message()
and ssl_buffer_message().
Whenever a handshake message is received which is
- a future handshake message (i.e., the sequence number
is larger than the next expected one), or which is
- a proper fragment of the next expected handshake message,
ssl_buffer_message() is called, which does the following:
- Ignore message if its sequence number is too far ahead
of the next expected sequence number, as controlled by
the macro constant MBEDTLS_SSL_MAX_BUFFERED_HS.
- Otherwise, check if buffering for the message with the
respective sequence number has already commenced.
- If not, allocate space to back up the message within
the buffering substructure of mbedtls_ssl_handshake_params.
If the message is a proper fragment, allocate additional
space for a reassembly bitmap; if it is a full message,
omit the bitmap. In any case, fall throuh to the next case.
- If the message has already been buffered, check that
the header is the same, and add the current fragment
if the message is not yet complete (this excludes the
case where a future message has been received in a single
fragment, hence omitting the bitmap, and is afterwards
also received as a series of proper fragments; in this
case, the proper fragments will be ignored).
For loading buffered messages in ssl_load_buffered_message(),
the approach is the following:
- Check the first entry in the buffering window (the window
is always based at the next expected handshake message).
If buffering hasn't started or if reassembly is still
in progress, ignore. If the next expected message has been
fully received, copy it to the input buffer (which is empty,
as ssl_load_buffered_message() is only called in this case).
2018-08-16 14:55:32 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
#if defined(MBEDTLS_X509_CRT_PARSE_C)
|
|
|
|
void mbedtls_ssl_set_verify( mbedtls_ssl_context *ssl,
|
|
|
|
int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
|
|
|
|
void *p_vrfy )
|
|
|
|
{
|
|
|
|
ssl->f_vrfy = f_vrfy;
|
|
|
|
ssl->p_vrfy = p_vrfy;
|
|
|
|
}
|
|
|
|
#endif
|
Implement future message buffering and loading
This commit implements future handshake message buffering
and loading by implementing ssl_load_buffered_message()
and ssl_buffer_message().
Whenever a handshake message is received which is
- a future handshake message (i.e., the sequence number
is larger than the next expected one), or which is
- a proper fragment of the next expected handshake message,
ssl_buffer_message() is called, which does the following:
- Ignore message if its sequence number is too far ahead
of the next expected sequence number, as controlled by
the macro constant MBEDTLS_SSL_MAX_BUFFERED_HS.
- Otherwise, check if buffering for the message with the
respective sequence number has already commenced.
- If not, allocate space to back up the message within
the buffering substructure of mbedtls_ssl_handshake_params.
If the message is a proper fragment, allocate additional
space for a reassembly bitmap; if it is a full message,
omit the bitmap. In any case, fall throuh to the next case.
- If the message has already been buffered, check that
the header is the same, and add the current fragment
if the message is not yet complete (this excludes the
case where a future message has been received in a single
fragment, hence omitting the bitmap, and is afterwards
also received as a series of proper fragments; in this
case, the proper fragments will be ignored).
For loading buffered messages in ssl_load_buffered_message(),
the approach is the following:
- Check the first entry in the buffering window (the window
is always based at the next expected handshake message).
If buffering hasn't started or if reassembly is still
in progress, ignore. If the next expected message has been
fully received, copy it to the input buffer (which is empty,
as ssl_load_buffered_message() is only called in this case).
2018-08-16 14:55:32 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
|
|
|
|
/*
|
|
|
|
* Set EC J-PAKE password for current handshake
|
|
|
|
*/
|
|
|
|
int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
|
|
|
|
const unsigned char *pw,
|
|
|
|
size_t pw_len )
|
|
|
|
{
|
|
|
|
mbedtls_ecjpake_role role;
|
Implement future message buffering and loading
This commit implements future handshake message buffering
and loading by implementing ssl_load_buffered_message()
and ssl_buffer_message().
Whenever a handshake message is received which is
- a future handshake message (i.e., the sequence number
is larger than the next expected one), or which is
- a proper fragment of the next expected handshake message,
ssl_buffer_message() is called, which does the following:
- Ignore message if its sequence number is too far ahead
of the next expected sequence number, as controlled by
the macro constant MBEDTLS_SSL_MAX_BUFFERED_HS.
- Otherwise, check if buffering for the message with the
respective sequence number has already commenced.
- If not, allocate space to back up the message within
the buffering substructure of mbedtls_ssl_handshake_params.
If the message is a proper fragment, allocate additional
space for a reassembly bitmap; if it is a full message,
omit the bitmap. In any case, fall throuh to the next case.
- If the message has already been buffered, check that
the header is the same, and add the current fragment
if the message is not yet complete (this excludes the
case where a future message has been received in a single
fragment, hence omitting the bitmap, and is afterwards
also received as a series of proper fragments; in this
case, the proper fragments will be ignored).
For loading buffered messages in ssl_load_buffered_message(),
the approach is the following:
- Check the first entry in the buffering window (the window
is always based at the next expected handshake message).
If buffering hasn't started or if reassembly is still
in progress, ignore. If the next expected message has been
fully received, copy it to the input buffer (which is empty,
as ssl_load_buffered_message() is only called in this case).
2018-08-16 14:55:32 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
if( ssl->handshake == NULL || ssl->conf == NULL )
|
|
|
|
return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
|
Implement future message buffering and loading
This commit implements future handshake message buffering
and loading by implementing ssl_load_buffered_message()
and ssl_buffer_message().
Whenever a handshake message is received which is
- a future handshake message (i.e., the sequence number
is larger than the next expected one), or which is
- a proper fragment of the next expected handshake message,
ssl_buffer_message() is called, which does the following:
- Ignore message if its sequence number is too far ahead
of the next expected sequence number, as controlled by
the macro constant MBEDTLS_SSL_MAX_BUFFERED_HS.
- Otherwise, check if buffering for the message with the
respective sequence number has already commenced.
- If not, allocate space to back up the message within
the buffering substructure of mbedtls_ssl_handshake_params.
If the message is a proper fragment, allocate additional
space for a reassembly bitmap; if it is a full message,
omit the bitmap. In any case, fall throuh to the next case.
- If the message has already been buffered, check that
the header is the same, and add the current fragment
if the message is not yet complete (this excludes the
case where a future message has been received in a single
fragment, hence omitting the bitmap, and is afterwards
also received as a series of proper fragments; in this
case, the proper fragments will be ignored).
For loading buffered messages in ssl_load_buffered_message(),
the approach is the following:
- Check the first entry in the buffering window (the window
is always based at the next expected handshake message).
If buffering hasn't started or if reassembly is still
in progress, ignore. If the next expected message has been
fully received, copy it to the input buffer (which is empty,
as ssl_load_buffered_message() is only called in this case).
2018-08-16 14:55:32 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
|
|
|
|
role = MBEDTLS_ECJPAKE_SERVER;
|
Implement future message buffering and loading
This commit implements future handshake message buffering
and loading by implementing ssl_load_buffered_message()
and ssl_buffer_message().
Whenever a handshake message is received which is
- a future handshake message (i.e., the sequence number
is larger than the next expected one), or which is
- a proper fragment of the next expected handshake message,
ssl_buffer_message() is called, which does the following:
- Ignore message if its sequence number is too far ahead
of the next expected sequence number, as controlled by
the macro constant MBEDTLS_SSL_MAX_BUFFERED_HS.
- Otherwise, check if buffering for the message with the
respective sequence number has already commenced.
- If not, allocate space to back up the message within
the buffering substructure of mbedtls_ssl_handshake_params.
If the message is a proper fragment, allocate additional
space for a reassembly bitmap; if it is a full message,
omit the bitmap. In any case, fall throuh to the next case.
- If the message has already been buffered, check that
the header is the same, and add the current fragment
if the message is not yet complete (this excludes the
case where a future message has been received in a single
fragment, hence omitting the bitmap, and is afterwards
also received as a series of proper fragments; in this
case, the proper fragments will be ignored).
For loading buffered messages in ssl_load_buffered_message(),
the approach is the following:
- Check the first entry in the buffering window (the window
is always based at the next expected handshake message).
If buffering hasn't started or if reassembly is still
in progress, ignore. If the next expected message has been
fully received, copy it to the input buffer (which is empty,
as ssl_load_buffered_message() is only called in this case).
2018-08-16 14:55:32 +02:00
|
|
|
else
|
2020-02-05 15:44:10 +01:00
|
|
|
role = MBEDTLS_ECJPAKE_CLIENT;
|
2018-08-15 16:11:57 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
|
|
|
|
role,
|
|
|
|
MBEDTLS_MD_SHA256,
|
|
|
|
MBEDTLS_ECP_DP_SECP256R1,
|
|
|
|
pw, pw_len ) );
|
2018-08-15 15:48:01 +02:00
|
|
|
}
|
2020-02-05 15:44:10 +01:00
|
|
|
#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
|
|
|
|
|
2020-03-10 12:19:08 +01:00
|
|
|
#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
|
2018-08-15 15:48:01 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
static void ssl_conf_remove_psk( mbedtls_ssl_config *conf )
|
2018-08-21 18:20:27 +02:00
|
|
|
{
|
2020-02-05 15:44:10 +01:00
|
|
|
/* Remove reference to existing PSK, if any. */
|
|
|
|
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
|
|
|
if( conf->psk_opaque != 0 )
|
|
|
|
{
|
|
|
|
/* The maintenance of the PSK key slot is the
|
|
|
|
* user's responsibility. */
|
|
|
|
conf->psk_opaque = 0;
|
|
|
|
}
|
|
|
|
/* This and the following branch should never
|
|
|
|
* be taken simultaenously as we maintain the
|
|
|
|
* invariant that raw and opaque PSKs are never
|
|
|
|
* configured simultaneously. As a safeguard,
|
|
|
|
* though, `else` is omitted here. */
|
|
|
|
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
|
|
|
if( conf->psk != NULL )
|
|
|
|
{
|
|
|
|
mbedtls_platform_zeroize( conf->psk, conf->psk_len );
|
2018-08-21 18:20:27 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
mbedtls_free( conf->psk );
|
|
|
|
conf->psk = NULL;
|
|
|
|
conf->psk_len = 0;
|
|
|
|
}
|
2018-08-21 18:22:17 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
/* Remove reference to PSK identity, if any. */
|
|
|
|
if( conf->psk_identity != NULL )
|
2018-08-21 18:22:17 +02:00
|
|
|
{
|
2020-02-05 15:44:10 +01:00
|
|
|
mbedtls_free( conf->psk_identity );
|
|
|
|
conf->psk_identity = NULL;
|
|
|
|
conf->psk_identity_len = 0;
|
2018-08-21 18:22:17 +02:00
|
|
|
}
|
2020-02-05 15:44:10 +01:00
|
|
|
}
|
2018-08-21 18:20:27 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
/* This function assumes that PSK identity in the SSL config is unset.
|
|
|
|
* It checks that the provided identity is well-formed and attempts
|
|
|
|
* to make a copy of it in the SSL config.
|
|
|
|
* On failure, the PSK identity in the config remains unset. */
|
|
|
|
static int ssl_conf_set_psk_identity( mbedtls_ssl_config *conf,
|
|
|
|
unsigned char const *psk_identity,
|
|
|
|
size_t psk_identity_len )
|
|
|
|
{
|
|
|
|
/* Identity len will be encoded on two bytes */
|
|
|
|
if( psk_identity == NULL ||
|
|
|
|
( psk_identity_len >> 16 ) != 0 ||
|
|
|
|
psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
|
2018-08-21 18:20:27 +02:00
|
|
|
{
|
2020-02-05 15:44:10 +01:00
|
|
|
return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
|
|
|
|
}
|
2018-08-21 18:20:27 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
conf->psk_identity = mbedtls_calloc( 1, psk_identity_len );
|
|
|
|
if( conf->psk_identity == NULL )
|
|
|
|
return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
|
2016-10-13 18:21:01 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
conf->psk_identity_len = psk_identity_len;
|
|
|
|
memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
|
2018-08-21 18:20:27 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
return( 0 );
|
2018-08-21 18:20:27 +02:00
|
|
|
}
|
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
|
|
|
|
const unsigned char *psk, size_t psk_len,
|
|
|
|
const unsigned char *psk_identity, size_t psk_identity_len )
|
2018-08-15 15:48:01 +02:00
|
|
|
{
|
2020-02-05 15:44:10 +01:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
|
|
|
/* Remove opaque/raw PSK + PSK Identity */
|
|
|
|
ssl_conf_remove_psk( conf );
|
2018-08-15 16:11:57 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
/* Check and set raw PSK */
|
|
|
|
if( psk == NULL )
|
|
|
|
return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
|
|
|
|
if( psk_len == 0 )
|
|
|
|
return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
|
|
|
|
if( psk_len > MBEDTLS_PSK_MAX_LEN )
|
|
|
|
return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
|
2018-08-15 16:11:57 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
|
|
|
|
return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
|
|
|
|
conf->psk_len = psk_len;
|
|
|
|
memcpy( conf->psk, psk, conf->psk_len );
|
2018-08-15 16:11:57 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
/* Check and set PSK Identity */
|
|
|
|
ret = ssl_conf_set_psk_identity( conf, psk_identity, psk_identity_len );
|
|
|
|
if( ret != 0 )
|
|
|
|
ssl_conf_remove_psk( conf );
|
2018-08-21 15:57:46 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
return( ret );
|
|
|
|
}
|
2018-08-15 16:11:57 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
static void ssl_remove_psk( mbedtls_ssl_context *ssl )
|
|
|
|
{
|
|
|
|
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
|
|
|
if( ssl->handshake->psk_opaque != 0 )
|
|
|
|
{
|
|
|
|
ssl->handshake->psk_opaque = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
|
|
|
if( ssl->handshake->psk != NULL )
|
|
|
|
{
|
|
|
|
mbedtls_platform_zeroize( ssl->handshake->psk,
|
|
|
|
ssl->handshake->psk_len );
|
|
|
|
mbedtls_free( ssl->handshake->psk );
|
|
|
|
ssl->handshake->psk_len = 0;
|
|
|
|
}
|
|
|
|
}
|
Implement future message buffering and loading
This commit implements future handshake message buffering
and loading by implementing ssl_load_buffered_message()
and ssl_buffer_message().
Whenever a handshake message is received which is
- a future handshake message (i.e., the sequence number
is larger than the next expected one), or which is
- a proper fragment of the next expected handshake message,
ssl_buffer_message() is called, which does the following:
- Ignore message if its sequence number is too far ahead
of the next expected sequence number, as controlled by
the macro constant MBEDTLS_SSL_MAX_BUFFERED_HS.
- Otherwise, check if buffering for the message with the
respective sequence number has already commenced.
- If not, allocate space to back up the message within
the buffering substructure of mbedtls_ssl_handshake_params.
If the message is a proper fragment, allocate additional
space for a reassembly bitmap; if it is a full message,
omit the bitmap. In any case, fall throuh to the next case.
- If the message has already been buffered, check that
the header is the same, and add the current fragment
if the message is not yet complete (this excludes the
case where a future message has been received in a single
fragment, hence omitting the bitmap, and is afterwards
also received as a series of proper fragments; in this
case, the proper fragments will be ignored).
For loading buffered messages in ssl_load_buffered_message(),
the approach is the following:
- Check the first entry in the buffering window (the window
is always based at the next expected handshake message).
If buffering hasn't started or if reassembly is still
in progress, ignore. If the next expected message has been
fully received, copy it to the input buffer (which is empty,
as ssl_load_buffered_message() is only called in this case).
2018-08-16 14:55:32 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
|
|
|
|
const unsigned char *psk, size_t psk_len )
|
|
|
|
{
|
|
|
|
if( psk == NULL || ssl->handshake == NULL )
|
|
|
|
return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
|
Implement future message buffering and loading
This commit implements future handshake message buffering
and loading by implementing ssl_load_buffered_message()
and ssl_buffer_message().
Whenever a handshake message is received which is
- a future handshake message (i.e., the sequence number
is larger than the next expected one), or which is
- a proper fragment of the next expected handshake message,
ssl_buffer_message() is called, which does the following:
- Ignore message if its sequence number is too far ahead
of the next expected sequence number, as controlled by
the macro constant MBEDTLS_SSL_MAX_BUFFERED_HS.
- Otherwise, check if buffering for the message with the
respective sequence number has already commenced.
- If not, allocate space to back up the message within
the buffering substructure of mbedtls_ssl_handshake_params.
If the message is a proper fragment, allocate additional
space for a reassembly bitmap; if it is a full message,
omit the bitmap. In any case, fall throuh to the next case.
- If the message has already been buffered, check that
the header is the same, and add the current fragment
if the message is not yet complete (this excludes the
case where a future message has been received in a single
fragment, hence omitting the bitmap, and is afterwards
also received as a series of proper fragments; in this
case, the proper fragments will be ignored).
For loading buffered messages in ssl_load_buffered_message(),
the approach is the following:
- Check the first entry in the buffering window (the window
is always based at the next expected handshake message).
If buffering hasn't started or if reassembly is still
in progress, ignore. If the next expected message has been
fully received, copy it to the input buffer (which is empty,
as ssl_load_buffered_message() is only called in this case).
2018-08-16 14:55:32 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
if( psk_len > MBEDTLS_PSK_MAX_LEN )
|
|
|
|
return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
|
Implement future message buffering and loading
This commit implements future handshake message buffering
and loading by implementing ssl_load_buffered_message()
and ssl_buffer_message().
Whenever a handshake message is received which is
- a future handshake message (i.e., the sequence number
is larger than the next expected one), or which is
- a proper fragment of the next expected handshake message,
ssl_buffer_message() is called, which does the following:
- Ignore message if its sequence number is too far ahead
of the next expected sequence number, as controlled by
the macro constant MBEDTLS_SSL_MAX_BUFFERED_HS.
- Otherwise, check if buffering for the message with the
respective sequence number has already commenced.
- If not, allocate space to back up the message within
the buffering substructure of mbedtls_ssl_handshake_params.
If the message is a proper fragment, allocate additional
space for a reassembly bitmap; if it is a full message,
omit the bitmap. In any case, fall throuh to the next case.
- If the message has already been buffered, check that
the header is the same, and add the current fragment
if the message is not yet complete (this excludes the
case where a future message has been received in a single
fragment, hence omitting the bitmap, and is afterwards
also received as a series of proper fragments; in this
case, the proper fragments will be ignored).
For loading buffered messages in ssl_load_buffered_message(),
the approach is the following:
- Check the first entry in the buffering window (the window
is always based at the next expected handshake message).
If buffering hasn't started or if reassembly is still
in progress, ignore. If the next expected message has been
fully received, copy it to the input buffer (which is empty,
as ssl_load_buffered_message() is only called in this case).
2018-08-16 14:55:32 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
ssl_remove_psk( ssl );
|
Implement future message buffering and loading
This commit implements future handshake message buffering
and loading by implementing ssl_load_buffered_message()
and ssl_buffer_message().
Whenever a handshake message is received which is
- a future handshake message (i.e., the sequence number
is larger than the next expected one), or which is
- a proper fragment of the next expected handshake message,
ssl_buffer_message() is called, which does the following:
- Ignore message if its sequence number is too far ahead
of the next expected sequence number, as controlled by
the macro constant MBEDTLS_SSL_MAX_BUFFERED_HS.
- Otherwise, check if buffering for the message with the
respective sequence number has already commenced.
- If not, allocate space to back up the message within
the buffering substructure of mbedtls_ssl_handshake_params.
If the message is a proper fragment, allocate additional
space for a reassembly bitmap; if it is a full message,
omit the bitmap. In any case, fall throuh to the next case.
- If the message has already been buffered, check that
the header is the same, and add the current fragment
if the message is not yet complete (this excludes the
case where a future message has been received in a single
fragment, hence omitting the bitmap, and is afterwards
also received as a series of proper fragments; in this
case, the proper fragments will be ignored).
For loading buffered messages in ssl_load_buffered_message(),
the approach is the following:
- Check the first entry in the buffering window (the window
is always based at the next expected handshake message).
If buffering hasn't started or if reassembly is still
in progress, ignore. If the next expected message has been
fully received, copy it to the input buffer (which is empty,
as ssl_load_buffered_message() is only called in this case).
2018-08-16 14:55:32 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
|
|
|
|
return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
|
2018-08-21 16:47:49 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
ssl->handshake->psk_len = psk_len;
|
|
|
|
memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
|
Implement future message buffering and loading
This commit implements future handshake message buffering
and loading by implementing ssl_load_buffered_message()
and ssl_buffer_message().
Whenever a handshake message is received which is
- a future handshake message (i.e., the sequence number
is larger than the next expected one), or which is
- a proper fragment of the next expected handshake message,
ssl_buffer_message() is called, which does the following:
- Ignore message if its sequence number is too far ahead
of the next expected sequence number, as controlled by
the macro constant MBEDTLS_SSL_MAX_BUFFERED_HS.
- Otherwise, check if buffering for the message with the
respective sequence number has already commenced.
- If not, allocate space to back up the message within
the buffering substructure of mbedtls_ssl_handshake_params.
If the message is a proper fragment, allocate additional
space for a reassembly bitmap; if it is a full message,
omit the bitmap. In any case, fall throuh to the next case.
- If the message has already been buffered, check that
the header is the same, and add the current fragment
if the message is not yet complete (this excludes the
case where a future message has been received in a single
fragment, hence omitting the bitmap, and is afterwards
also received as a series of proper fragments; in this
case, the proper fragments will be ignored).
For loading buffered messages in ssl_load_buffered_message(),
the approach is the following:
- Check the first entry in the buffering window (the window
is always based at the next expected handshake message).
If buffering hasn't started or if reassembly is still
in progress, ignore. If the next expected message has been
fully received, copy it to the input buffer (which is empty,
as ssl_load_buffered_message() is only called in this case).
2018-08-16 14:55:32 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
return( 0 );
|
|
|
|
}
|
Implement future message buffering and loading
This commit implements future handshake message buffering
and loading by implementing ssl_load_buffered_message()
and ssl_buffer_message().
Whenever a handshake message is received which is
- a future handshake message (i.e., the sequence number
is larger than the next expected one), or which is
- a proper fragment of the next expected handshake message,
ssl_buffer_message() is called, which does the following:
- Ignore message if its sequence number is too far ahead
of the next expected sequence number, as controlled by
the macro constant MBEDTLS_SSL_MAX_BUFFERED_HS.
- Otherwise, check if buffering for the message with the
respective sequence number has already commenced.
- If not, allocate space to back up the message within
the buffering substructure of mbedtls_ssl_handshake_params.
If the message is a proper fragment, allocate additional
space for a reassembly bitmap; if it is a full message,
omit the bitmap. In any case, fall throuh to the next case.
- If the message has already been buffered, check that
the header is the same, and add the current fragment
if the message is not yet complete (this excludes the
case where a future message has been received in a single
fragment, hence omitting the bitmap, and is afterwards
also received as a series of proper fragments; in this
case, the proper fragments will be ignored).
For loading buffered messages in ssl_load_buffered_message(),
the approach is the following:
- Check the first entry in the buffering window (the window
is always based at the next expected handshake message).
If buffering hasn't started or if reassembly is still
in progress, ignore. If the next expected message has been
fully received, copy it to the input buffer (which is empty,
as ssl_load_buffered_message() is only called in this case).
2018-08-16 14:55:32 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
|
|
|
int mbedtls_ssl_conf_psk_opaque( mbedtls_ssl_config *conf,
|
|
|
|
psa_key_handle_t psk_slot,
|
|
|
|
const unsigned char *psk_identity,
|
|
|
|
size_t psk_identity_len )
|
|
|
|
{
|
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
|
|
|
/* Clear opaque/raw PSK + PSK Identity, if present. */
|
|
|
|
ssl_conf_remove_psk( conf );
|
2018-08-21 16:51:03 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
/* Check and set opaque PSK */
|
|
|
|
if( psk_slot == 0 )
|
|
|
|
return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
|
|
|
|
conf->psk_opaque = psk_slot;
|
2018-08-21 16:51:03 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
/* Check and set PSK Identity */
|
|
|
|
ret = ssl_conf_set_psk_identity( conf, psk_identity,
|
|
|
|
psk_identity_len );
|
|
|
|
if( ret != 0 )
|
|
|
|
ssl_conf_remove_psk( conf );
|
2018-08-21 16:51:03 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
return( ret );
|
|
|
|
}
|
2018-08-21 16:51:03 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
int mbedtls_ssl_set_hs_psk_opaque( mbedtls_ssl_context *ssl,
|
|
|
|
psa_key_handle_t psk_slot )
|
|
|
|
{
|
|
|
|
if( psk_slot == 0 || ssl->handshake == NULL )
|
|
|
|
return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
|
Implement future message buffering and loading
This commit implements future handshake message buffering
and loading by implementing ssl_load_buffered_message()
and ssl_buffer_message().
Whenever a handshake message is received which is
- a future handshake message (i.e., the sequence number
is larger than the next expected one), or which is
- a proper fragment of the next expected handshake message,
ssl_buffer_message() is called, which does the following:
- Ignore message if its sequence number is too far ahead
of the next expected sequence number, as controlled by
the macro constant MBEDTLS_SSL_MAX_BUFFERED_HS.
- Otherwise, check if buffering for the message with the
respective sequence number has already commenced.
- If not, allocate space to back up the message within
the buffering substructure of mbedtls_ssl_handshake_params.
If the message is a proper fragment, allocate additional
space for a reassembly bitmap; if it is a full message,
omit the bitmap. In any case, fall throuh to the next case.
- If the message has already been buffered, check that
the header is the same, and add the current fragment
if the message is not yet complete (this excludes the
case where a future message has been received in a single
fragment, hence omitting the bitmap, and is afterwards
also received as a series of proper fragments; in this
case, the proper fragments will be ignored).
For loading buffered messages in ssl_load_buffered_message(),
the approach is the following:
- Check the first entry in the buffering window (the window
is always based at the next expected handshake message).
If buffering hasn't started or if reassembly is still
in progress, ignore. If the next expected message has been
fully received, copy it to the input buffer (which is empty,
as ssl_load_buffered_message() is only called in this case).
2018-08-16 14:55:32 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
ssl_remove_psk( ssl );
|
|
|
|
ssl->handshake->psk_opaque = psk_slot;
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
Implement future message buffering and loading
This commit implements future handshake message buffering
and loading by implementing ssl_load_buffered_message()
and ssl_buffer_message().
Whenever a handshake message is received which is
- a future handshake message (i.e., the sequence number
is larger than the next expected one), or which is
- a proper fragment of the next expected handshake message,
ssl_buffer_message() is called, which does the following:
- Ignore message if its sequence number is too far ahead
of the next expected sequence number, as controlled by
the macro constant MBEDTLS_SSL_MAX_BUFFERED_HS.
- Otherwise, check if buffering for the message with the
respective sequence number has already commenced.
- If not, allocate space to back up the message within
the buffering substructure of mbedtls_ssl_handshake_params.
If the message is a proper fragment, allocate additional
space for a reassembly bitmap; if it is a full message,
omit the bitmap. In any case, fall throuh to the next case.
- If the message has already been buffered, check that
the header is the same, and add the current fragment
if the message is not yet complete (this excludes the
case where a future message has been received in a single
fragment, hence omitting the bitmap, and is afterwards
also received as a series of proper fragments; in this
case, the proper fragments will be ignored).
For loading buffered messages in ssl_load_buffered_message(),
the approach is the following:
- Check the first entry in the buffering window (the window
is always based at the next expected handshake message).
If buffering hasn't started or if reassembly is still
in progress, ignore. If the next expected message has been
fully received, copy it to the input buffer (which is empty,
as ssl_load_buffered_message() is only called in this case).
2018-08-16 14:55:32 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
|
|
|
|
int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
|
|
|
|
size_t),
|
|
|
|
void *p_psk )
|
|
|
|
{
|
|
|
|
conf->f_psk = f_psk;
|
|
|
|
conf->p_psk = p_psk;
|
|
|
|
}
|
2020-03-10 12:19:08 +01:00
|
|
|
#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
|
Implement future message buffering and loading
This commit implements future handshake message buffering
and loading by implementing ssl_load_buffered_message()
and ssl_buffer_message().
Whenever a handshake message is received which is
- a future handshake message (i.e., the sequence number
is larger than the next expected one), or which is
- a proper fragment of the next expected handshake message,
ssl_buffer_message() is called, which does the following:
- Ignore message if its sequence number is too far ahead
of the next expected sequence number, as controlled by
the macro constant MBEDTLS_SSL_MAX_BUFFERED_HS.
- Otherwise, check if buffering for the message with the
respective sequence number has already commenced.
- If not, allocate space to back up the message within
the buffering substructure of mbedtls_ssl_handshake_params.
If the message is a proper fragment, allocate additional
space for a reassembly bitmap; if it is a full message,
omit the bitmap. In any case, fall throuh to the next case.
- If the message has already been buffered, check that
the header is the same, and add the current fragment
if the message is not yet complete (this excludes the
case where a future message has been received in a single
fragment, hence omitting the bitmap, and is afterwards
also received as a series of proper fragments; in this
case, the proper fragments will be ignored).
For loading buffered messages in ssl_load_buffered_message(),
the approach is the following:
- Check the first entry in the buffering window (the window
is always based at the next expected handshake message).
If buffering hasn't started or if reassembly is still
in progress, ignore. If the next expected message has been
fully received, copy it to the input buffer (which is empty,
as ssl_load_buffered_message() is only called in this case).
2018-08-16 14:55:32 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
|
Implement future message buffering and loading
This commit implements future handshake message buffering
and loading by implementing ssl_load_buffered_message()
and ssl_buffer_message().
Whenever a handshake message is received which is
- a future handshake message (i.e., the sequence number
is larger than the next expected one), or which is
- a proper fragment of the next expected handshake message,
ssl_buffer_message() is called, which does the following:
- Ignore message if its sequence number is too far ahead
of the next expected sequence number, as controlled by
the macro constant MBEDTLS_SSL_MAX_BUFFERED_HS.
- Otherwise, check if buffering for the message with the
respective sequence number has already commenced.
- If not, allocate space to back up the message within
the buffering substructure of mbedtls_ssl_handshake_params.
If the message is a proper fragment, allocate additional
space for a reassembly bitmap; if it is a full message,
omit the bitmap. In any case, fall throuh to the next case.
- If the message has already been buffered, check that
the header is the same, and add the current fragment
if the message is not yet complete (this excludes the
case where a future message has been received in a single
fragment, hence omitting the bitmap, and is afterwards
also received as a series of proper fragments; in this
case, the proper fragments will be ignored).
For loading buffered messages in ssl_load_buffered_message(),
the approach is the following:
- Check the first entry in the buffering window (the window
is always based at the next expected handshake message).
If buffering hasn't started or if reassembly is still
in progress, ignore. If the next expected message has been
fully received, copy it to the input buffer (which is empty,
as ssl_load_buffered_message() is only called in this case).
2018-08-16 14:55:32 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
#if !defined(MBEDTLS_DEPRECATED_REMOVED)
|
|
|
|
int mbedtls_ssl_conf_dh_param( mbedtls_ssl_config *conf, const char *dhm_P, const char *dhm_G )
|
|
|
|
{
|
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2018-08-15 16:11:57 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
if( ( ret = mbedtls_mpi_read_string( &conf->dhm_P, 16, dhm_P ) ) != 0 ||
|
|
|
|
( ret = mbedtls_mpi_read_string( &conf->dhm_G, 16, dhm_G ) ) != 0 )
|
|
|
|
{
|
|
|
|
mbedtls_mpi_free( &conf->dhm_P );
|
|
|
|
mbedtls_mpi_free( &conf->dhm_G );
|
|
|
|
return( ret );
|
2018-08-15 16:11:57 +02:00
|
|
|
}
|
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
return( 0 );
|
2018-08-15 15:48:01 +02:00
|
|
|
}
|
2020-02-05 15:44:10 +01:00
|
|
|
#endif /* MBEDTLS_DEPRECATED_REMOVED */
|
2018-08-15 15:48:01 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
|
|
|
|
const unsigned char *dhm_P, size_t P_len,
|
|
|
|
const unsigned char *dhm_G, size_t G_len )
|
2016-10-13 18:21:01 +02:00
|
|
|
{
|
2020-02-05 15:44:10 +01:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2017-05-24 17:27:30 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
|
|
|
|
( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
|
2014-09-08 16:14:10 +02:00
|
|
|
{
|
2020-02-05 15:44:10 +01:00
|
|
|
mbedtls_mpi_free( &conf->dhm_P );
|
|
|
|
mbedtls_mpi_free( &conf->dhm_G );
|
|
|
|
return( ret );
|
|
|
|
}
|
2014-09-08 16:14:10 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
return( 0 );
|
|
|
|
}
|
2014-09-08 16:14:10 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
|
|
|
|
{
|
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2014-09-09 17:45:31 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
if( ( ret = mbedtls_mpi_copy( &conf->dhm_P, &dhm_ctx->P ) ) != 0 ||
|
|
|
|
( ret = mbedtls_mpi_copy( &conf->dhm_G, &dhm_ctx->G ) ) != 0 )
|
2017-05-24 17:27:30 +02:00
|
|
|
{
|
2020-02-05 15:44:10 +01:00
|
|
|
mbedtls_mpi_free( &conf->dhm_P );
|
|
|
|
mbedtls_mpi_free( &conf->dhm_G );
|
|
|
|
return( ret );
|
2017-05-24 17:27:30 +02:00
|
|
|
}
|
2014-09-08 16:14:10 +02:00
|
|
|
|
2018-08-15 15:09:41 +02:00
|
|
|
return( 0 );
|
|
|
|
}
|
2020-02-05 15:44:10 +01:00
|
|
|
#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
|
2017-05-24 17:27:30 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
|
|
|
|
/*
|
|
|
|
* Set the minimum length for Diffie-Hellman parameters
|
|
|
|
*/
|
|
|
|
void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
|
|
|
|
unsigned int bitlen )
|
2018-08-15 15:26:08 +02:00
|
|
|
{
|
2020-02-05 15:44:10 +01:00
|
|
|
conf->dhm_min_bitlen = bitlen;
|
2018-08-15 15:26:08 +02:00
|
|
|
}
|
2020-02-05 15:44:10 +01:00
|
|
|
#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
|
2018-08-15 15:26:08 +02:00
|
|
|
|
2020-03-10 12:19:08 +01:00
|
|
|
#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
|
2020-02-05 15:44:10 +01:00
|
|
|
/*
|
|
|
|
* Set allowed/preferred hashes for handshake signatures
|
|
|
|
*/
|
|
|
|
void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
|
|
|
|
const int *hashes )
|
2018-08-16 15:56:31 +02:00
|
|
|
{
|
2020-02-05 15:44:10 +01:00
|
|
|
conf->sig_hashes = hashes;
|
2018-08-16 15:56:31 +02:00
|
|
|
}
|
2020-03-10 12:19:08 +01:00
|
|
|
#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
|
2018-08-16 15:56:31 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
#if defined(MBEDTLS_ECP_C)
|
|
|
|
/*
|
|
|
|
* Set the allowed elliptic curves
|
|
|
|
*/
|
|
|
|
void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
|
|
|
|
const mbedtls_ecp_group_id *curve_list )
|
2018-08-16 15:56:31 +02:00
|
|
|
{
|
2020-02-05 15:44:10 +01:00
|
|
|
conf->curve_list = curve_list;
|
|
|
|
}
|
|
|
|
#endif /* MBEDTLS_ECP_C */
|
2018-08-16 15:56:31 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
#if defined(MBEDTLS_X509_CRT_PARSE_C)
|
|
|
|
int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
|
|
|
|
{
|
|
|
|
/* Initialize to suppress unnecessary compiler warning */
|
|
|
|
size_t hostname_len = 0;
|
2018-08-16 15:56:31 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
/* Check if new hostname is valid before
|
|
|
|
* making any change to current one */
|
|
|
|
if( hostname != NULL )
|
|
|
|
{
|
|
|
|
hostname_len = strlen( hostname );
|
2018-08-16 15:56:31 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
|
|
|
|
return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
|
|
|
|
}
|
2018-08-20 12:19:05 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
/* Now it's clear that we will overwrite the old hostname,
|
|
|
|
* so we can free it safely */
|
2018-08-16 15:56:31 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
if( ssl->hostname != NULL )
|
2018-08-16 15:56:31 +02:00
|
|
|
{
|
2020-02-05 15:44:10 +01:00
|
|
|
mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
|
|
|
|
mbedtls_free( ssl->hostname );
|
2018-08-16 15:56:31 +02:00
|
|
|
}
|
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
/* Passing NULL as hostname shall clear the old one */
|
2018-08-16 15:56:31 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
if( hostname == NULL )
|
2018-08-16 15:56:31 +02:00
|
|
|
{
|
2020-02-05 15:44:10 +01:00
|
|
|
ssl->hostname = NULL;
|
2018-08-16 15:56:31 +02:00
|
|
|
}
|
2020-02-05 15:44:10 +01:00
|
|
|
else
|
|
|
|
{
|
|
|
|
ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
|
|
|
|
if( ssl->hostname == NULL )
|
|
|
|
return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
|
2018-08-16 15:56:31 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
memcpy( ssl->hostname, hostname, hostname_len );
|
2018-08-16 15:56:31 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
ssl->hostname[hostname_len] = '\0';
|
|
|
|
}
|
2018-08-16 15:56:31 +02:00
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
2020-02-05 15:44:10 +01:00
|
|
|
#endif /* MBEDTLS_X509_CRT_PARSE_C */
|
2018-08-16 15:56:31 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
|
|
|
|
void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
|
|
|
|
int (*f_sni)(void *, mbedtls_ssl_context *,
|
|
|
|
const unsigned char *, size_t),
|
|
|
|
void *p_sni )
|
2018-08-16 15:56:31 +02:00
|
|
|
{
|
2020-02-05 15:44:10 +01:00
|
|
|
conf->f_sni = f_sni;
|
|
|
|
conf->p_sni = p_sni;
|
|
|
|
}
|
|
|
|
#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
|
2018-08-16 15:56:31 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
#if defined(MBEDTLS_SSL_ALPN)
|
|
|
|
int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
|
|
|
|
{
|
|
|
|
size_t cur_len, tot_len;
|
|
|
|
const char **p;
|
2018-08-16 15:56:31 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
/*
|
|
|
|
* RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
|
|
|
|
* MUST NOT be truncated."
|
|
|
|
* We check lengths now rather than later.
|
|
|
|
*/
|
|
|
|
tot_len = 0;
|
|
|
|
for( p = protos; *p != NULL; p++ )
|
2018-08-21 18:22:17 +02:00
|
|
|
{
|
2020-02-05 15:44:10 +01:00
|
|
|
cur_len = strlen( *p );
|
|
|
|
tot_len += cur_len;
|
2018-08-16 15:56:31 +02:00
|
|
|
|
2020-04-23 16:41:44 +02:00
|
|
|
if( ( cur_len == 0 ) ||
|
|
|
|
( cur_len > MBEDTLS_SSL_MAX_ALPN_NAME_LEN ) ||
|
|
|
|
( tot_len > MBEDTLS_SSL_MAX_ALPN_LIST_LEN ) )
|
2020-02-05 15:44:10 +01:00
|
|
|
return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
|
2017-05-24 17:27:30 +02:00
|
|
|
}
|
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
conf->alpn_list = protos;
|
2018-08-16 15:56:31 +02:00
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
|
|
|
|
{
|
|
|
|
return( ssl->alpn_chosen );
|
|
|
|
}
|
|
|
|
#endif /* MBEDTLS_SSL_ALPN */
|
2018-08-16 15:56:31 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor )
|
2018-08-15 15:09:41 +02:00
|
|
|
{
|
2020-02-05 15:44:10 +01:00
|
|
|
conf->max_major_ver = major;
|
|
|
|
conf->max_minor_ver = minor;
|
|
|
|
}
|
2018-08-15 15:09:41 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, int major, int minor )
|
|
|
|
{
|
|
|
|
conf->min_major_ver = major;
|
|
|
|
conf->min_minor_ver = minor;
|
|
|
|
}
|
2017-05-24 17:27:30 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
#if defined(MBEDTLS_SSL_FALLBACK_SCSV) && defined(MBEDTLS_SSL_CLI_C)
|
|
|
|
void mbedtls_ssl_conf_fallback( mbedtls_ssl_config *conf, char fallback )
|
|
|
|
{
|
|
|
|
conf->fallback = fallback;
|
|
|
|
}
|
|
|
|
#endif
|
2014-09-08 16:14:10 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
#if defined(MBEDTLS_SSL_SRV_C)
|
|
|
|
void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
|
|
|
|
char cert_req_ca_list )
|
|
|
|
{
|
|
|
|
conf->cert_req_ca_list = cert_req_ca_list;
|
|
|
|
}
|
|
|
|
#endif
|
2017-04-10 13:42:31 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
|
2015-05-11 09:50:24 +02:00
|
|
|
void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
|
2014-10-27 13:57:03 +01:00
|
|
|
{
|
2015-05-05 10:45:39 +02:00
|
|
|
conf->encrypt_then_mac = etm;
|
2014-10-27 13:57:03 +01:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
|
2015-05-11 09:50:24 +02:00
|
|
|
void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
|
2014-10-20 18:40:56 +02:00
|
|
|
{
|
2015-05-05 10:45:39 +02:00
|
|
|
conf->extended_ms = ems;
|
2014-10-20 18:40:56 +02:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2015-05-14 12:28:21 +02:00
|
|
|
#if defined(MBEDTLS_ARC4_C)
|
2015-05-11 09:50:24 +02:00
|
|
|
void mbedtls_ssl_conf_arc4_support( mbedtls_ssl_config *conf, char arc4 )
|
2015-01-12 13:43:29 +01:00
|
|
|
{
|
2015-05-05 10:45:39 +02:00
|
|
|
conf->arc4_disabled = arc4;
|
2015-01-12 13:43:29 +01:00
|
|
|
}
|
2015-05-14 12:28:21 +02:00
|
|
|
#endif
|
2015-01-12 13:43:29 +01:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
|
2015-05-11 09:50:24 +02:00
|
|
|
int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
|
2013-07-16 12:45:26 +02:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
|
2016-05-25 12:56:48 +02:00
|
|
|
ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
|
2013-07-16 12:45:26 +02:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
|
2013-07-18 12:32:27 +02:00
|
|
|
}
|
2013-07-17 14:33:38 +02:00
|
|
|
|
2015-05-05 18:01:57 +02:00
|
|
|
conf->mfl_code = mfl_code;
|
2013-07-16 12:45:26 +02:00
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
2015-04-08 12:49:31 +02:00
|
|
|
#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
|
2013-07-16 12:45:26 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
|
2015-05-11 10:11:56 +02:00
|
|
|
void mbedtls_ssl_conf_truncated_hmac( mbedtls_ssl_config *conf, int truncate )
|
2013-07-19 11:08:52 +02:00
|
|
|
{
|
2015-05-05 10:45:39 +02:00
|
|
|
conf->trunc_hmac = truncate;
|
2013-07-19 11:08:52 +02:00
|
|
|
}
|
2015-04-08 12:49:31 +02:00
|
|
|
#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
|
2013-07-19 11:08:52 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
|
2015-05-11 09:50:24 +02:00
|
|
|
void mbedtls_ssl_conf_cbc_record_splitting( mbedtls_ssl_config *conf, char split )
|
2015-01-07 14:50:54 +01:00
|
|
|
{
|
2015-05-05 17:34:53 +02:00
|
|
|
conf->cbc_record_splitting = split;
|
2015-01-07 14:50:54 +01:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2015-05-11 09:50:24 +02:00
|
|
|
void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
|
2012-09-16 21:57:18 +02:00
|
|
|
{
|
2015-05-05 10:45:39 +02:00
|
|
|
conf->allow_legacy_renegotiation = allow_legacy;
|
2012-09-16 21:57:18 +02:00
|
|
|
}
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_SSL_RENEGOTIATION)
|
2015-05-11 09:50:24 +02:00
|
|
|
void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
|
2012-09-16 21:57:18 +02:00
|
|
|
{
|
2015-05-05 10:45:39 +02:00
|
|
|
conf->disable_renegotiation = renegotiation;
|
2012-09-16 21:57:18 +02:00
|
|
|
}
|
|
|
|
|
2015-05-11 09:50:24 +02:00
|
|
|
void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
|
2014-07-03 19:29:16 +02:00
|
|
|
{
|
2015-05-05 10:45:39 +02:00
|
|
|
conf->renego_max_records = max_records;
|
2014-07-03 19:29:16 +02:00
|
|
|
}
|
|
|
|
|
2015-05-11 09:50:24 +02:00
|
|
|
void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
|
2014-11-05 13:58:53 +01:00
|
|
|
const unsigned char period[8] )
|
|
|
|
{
|
2015-05-05 10:45:39 +02:00
|
|
|
memcpy( conf->renego_period, period, 8 );
|
2014-11-05 13:58:53 +01:00
|
|
|
}
|
2015-04-08 12:49:31 +02:00
|
|
|
#endif /* MBEDTLS_SSL_RENEGOTIATION */
|
2014-07-03 19:29:16 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_SSL_SESSION_TICKETS)
|
2015-05-20 10:45:29 +02:00
|
|
|
#if defined(MBEDTLS_SSL_CLI_C)
|
|
|
|
void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
|
2013-08-03 13:02:31 +02:00
|
|
|
{
|
2015-05-06 11:05:11 +02:00
|
|
|
conf->session_tickets = use_tickets;
|
2013-08-03 13:02:31 +02:00
|
|
|
}
|
2015-05-20 10:45:29 +02:00
|
|
|
#endif
|
2013-08-14 16:52:14 +02:00
|
|
|
|
2015-05-20 10:45:29 +02:00
|
|
|
#if defined(MBEDTLS_SSL_SRV_C)
|
2015-05-19 15:28:00 +02:00
|
|
|
void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
|
|
|
|
mbedtls_ssl_ticket_write_t *f_ticket_write,
|
|
|
|
mbedtls_ssl_ticket_parse_t *f_ticket_parse,
|
|
|
|
void *p_ticket )
|
2013-08-14 16:52:14 +02:00
|
|
|
{
|
2015-05-19 15:28:00 +02:00
|
|
|
conf->f_ticket_write = f_ticket_write;
|
|
|
|
conf->f_ticket_parse = f_ticket_parse;
|
|
|
|
conf->p_ticket = p_ticket;
|
2013-08-14 16:52:14 +02:00
|
|
|
}
|
2015-05-20 10:45:29 +02:00
|
|
|
#endif
|
2015-04-08 12:49:31 +02:00
|
|
|
#endif /* MBEDTLS_SSL_SESSION_TICKETS */
|
2013-08-03 13:02:31 +02:00
|
|
|
|
2015-10-02 14:33:37 +02:00
|
|
|
#if defined(MBEDTLS_SSL_EXPORT_KEYS)
|
|
|
|
void mbedtls_ssl_conf_export_keys_cb( mbedtls_ssl_config *conf,
|
|
|
|
mbedtls_ssl_export_keys_t *f_export_keys,
|
|
|
|
void *p_export_keys )
|
|
|
|
{
|
|
|
|
conf->f_export_keys = f_export_keys;
|
|
|
|
conf->p_export_keys = p_export_keys;
|
2019-05-07 17:33:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void mbedtls_ssl_conf_export_keys_ext_cb( mbedtls_ssl_config *conf,
|
|
|
|
mbedtls_ssl_export_keys_ext_t *f_export_keys_ext,
|
|
|
|
void *p_export_keys )
|
|
|
|
{
|
|
|
|
conf->f_export_keys_ext = f_export_keys_ext;
|
|
|
|
conf->p_export_keys = p_export_keys;
|
2015-10-02 14:33:37 +02:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2018-04-24 13:09:22 +02:00
|
|
|
#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
|
2018-01-05 21:11:53 +01:00
|
|
|
void mbedtls_ssl_conf_async_private_cb(
|
|
|
|
mbedtls_ssl_config *conf,
|
|
|
|
mbedtls_ssl_async_sign_t *f_async_sign,
|
|
|
|
mbedtls_ssl_async_decrypt_t *f_async_decrypt,
|
|
|
|
mbedtls_ssl_async_resume_t *f_async_resume,
|
|
|
|
mbedtls_ssl_async_cancel_t *f_async_cancel,
|
2018-04-25 20:39:48 +02:00
|
|
|
void *async_config_data )
|
2018-01-05 21:11:53 +01:00
|
|
|
{
|
|
|
|
conf->f_async_sign_start = f_async_sign;
|
|
|
|
conf->f_async_decrypt_start = f_async_decrypt;
|
|
|
|
conf->f_async_resume = f_async_resume;
|
|
|
|
conf->f_async_cancel = f_async_cancel;
|
2018-04-25 20:39:48 +02:00
|
|
|
conf->p_async_config_data = async_config_data;
|
|
|
|
}
|
|
|
|
|
2018-04-26 11:46:10 +02:00
|
|
|
void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
|
|
|
|
{
|
|
|
|
return( conf->p_async_config_data );
|
|
|
|
}
|
|
|
|
|
2018-04-30 11:54:39 +02:00
|
|
|
void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
|
2018-04-25 20:39:48 +02:00
|
|
|
{
|
|
|
|
if( ssl->handshake == NULL )
|
|
|
|
return( NULL );
|
|
|
|
else
|
|
|
|
return( ssl->handshake->user_async_ctx );
|
|
|
|
}
|
|
|
|
|
2018-04-30 11:54:39 +02:00
|
|
|
void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
|
2018-04-25 20:39:48 +02:00
|
|
|
void *ctx )
|
|
|
|
{
|
|
|
|
if( ssl->handshake != NULL )
|
|
|
|
ssl->handshake->user_async_ctx = ctx;
|
2018-01-05 21:11:53 +01:00
|
|
|
}
|
2018-04-24 13:09:22 +02:00
|
|
|
#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
|
2018-01-05 21:11:53 +01:00
|
|
|
|
2009-01-03 22:22:43 +01:00
|
|
|
/*
|
|
|
|
* SSL get accessors
|
|
|
|
*/
|
2015-05-11 19:54:43 +02:00
|
|
|
uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
|
2009-01-03 22:22:43 +01:00
|
|
|
{
|
2015-01-23 15:30:57 +01:00
|
|
|
if( ssl->session != NULL )
|
|
|
|
return( ssl->session->verify_result );
|
|
|
|
|
|
|
|
if( ssl->session_negotiate != NULL )
|
|
|
|
return( ssl->session_negotiate->verify_result );
|
|
|
|
|
2015-05-14 11:25:04 +02:00
|
|
|
return( 0xFFFFFFFF );
|
2009-01-03 22:22:43 +01:00
|
|
|
}
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
|
2011-01-16 22:27:44 +01:00
|
|
|
{
|
2013-03-06 10:23:34 +01:00
|
|
|
if( ssl == NULL || ssl->session == NULL )
|
2014-06-17 14:06:49 +02:00
|
|
|
return( NULL );
|
2013-03-06 10:23:34 +01:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite );
|
2011-01-16 22:27:44 +01:00
|
|
|
}
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
|
2011-01-15 18:35:19 +01:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_SSL_PROTO_DTLS)
|
2015-05-04 10:55:58 +02:00
|
|
|
if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
|
2014-02-10 13:43:33 +01:00
|
|
|
{
|
|
|
|
switch( ssl->minor_ver )
|
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
case MBEDTLS_SSL_MINOR_VERSION_2:
|
2014-02-10 13:43:33 +01:00
|
|
|
return( "DTLSv1.0" );
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
case MBEDTLS_SSL_MINOR_VERSION_3:
|
2014-02-10 13:43:33 +01:00
|
|
|
return( "DTLSv1.2" );
|
|
|
|
|
|
|
|
default:
|
|
|
|
return( "unknown (DTLS)" );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2011-01-15 18:35:19 +01:00
|
|
|
switch( ssl->minor_ver )
|
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
case MBEDTLS_SSL_MINOR_VERSION_0:
|
2020-02-05 15:44:10 +01:00
|
|
|
return( "SSLv3.0" );
|
2018-08-17 16:28:19 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
case MBEDTLS_SSL_MINOR_VERSION_1:
|
|
|
|
return( "TLSv1.0" );
|
2018-08-17 16:28:19 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
case MBEDTLS_SSL_MINOR_VERSION_2:
|
|
|
|
return( "TLSv1.1" );
|
2018-08-17 16:28:19 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
case MBEDTLS_SSL_MINOR_VERSION_3:
|
|
|
|
return( "TLSv1.2" );
|
2014-10-14 17:47:31 +02:00
|
|
|
|
|
|
|
default:
|
2020-02-05 15:44:10 +01:00
|
|
|
return( "unknown" );
|
2014-10-14 17:47:31 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-31 18:30:52 +02:00
|
|
|
#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
|
2020-04-03 11:25:29 +02:00
|
|
|
size_t mbedtls_ssl_get_input_max_frag_len( const mbedtls_ssl_context *ssl )
|
|
|
|
{
|
|
|
|
size_t max_len = MBEDTLS_SSL_MAX_CONTENT_LEN;
|
|
|
|
size_t read_mfl;
|
|
|
|
|
|
|
|
/* Use the configured MFL for the client if we're past SERVER_HELLO_DONE */
|
|
|
|
if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
|
|
|
|
ssl->state >= MBEDTLS_SSL_SERVER_HELLO_DONE )
|
|
|
|
{
|
|
|
|
return ssl_mfl_code_to_length( ssl->conf->mfl_code );
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check if a smaller max length was negotiated */
|
|
|
|
if( ssl->session_out != NULL )
|
|
|
|
{
|
|
|
|
read_mfl = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
|
|
|
|
if( read_mfl < max_len )
|
|
|
|
{
|
|
|
|
max_len = read_mfl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// During a handshake, use the value being negotiated
|
|
|
|
if( ssl->session_negotiate != NULL )
|
|
|
|
{
|
|
|
|
read_mfl = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
|
|
|
|
if( read_mfl < max_len )
|
|
|
|
{
|
|
|
|
max_len = read_mfl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return( max_len );
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t mbedtls_ssl_get_output_max_frag_len( const mbedtls_ssl_context *ssl )
|
2015-08-31 18:30:52 +02:00
|
|
|
{
|
|
|
|
size_t max_len;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Assume mfl_code is correct since it was checked when set
|
|
|
|
*/
|
2016-05-25 12:56:48 +02:00
|
|
|
max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
|
2015-08-31 18:30:52 +02:00
|
|
|
|
2017-09-19 13:00:47 +02:00
|
|
|
/* Check if a smaller max length was negotiated */
|
2015-08-31 18:30:52 +02:00
|
|
|
if( ssl->session_out != NULL &&
|
2016-05-25 12:56:48 +02:00
|
|
|
ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
|
2015-08-31 18:30:52 +02:00
|
|
|
{
|
2016-05-25 12:56:48 +02:00
|
|
|
max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
|
2015-08-31 18:30:52 +02:00
|
|
|
}
|
|
|
|
|
2017-09-19 13:00:47 +02:00
|
|
|
/* During a handshake, use the value being negotiated */
|
|
|
|
if( ssl->session_negotiate != NULL &&
|
|
|
|
ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
|
|
|
|
{
|
|
|
|
max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
|
|
|
|
}
|
|
|
|
|
2017-09-21 13:49:50 +02:00
|
|
|
return( max_len );
|
2015-08-31 18:30:52 +02:00
|
|
|
}
|
2020-04-03 11:25:29 +02:00
|
|
|
|
|
|
|
#if !defined(MBEDTLS_DEPRECATED_REMOVED)
|
|
|
|
size_t mbedtls_ssl_get_max_frag_len( const mbedtls_ssl_context *ssl )
|
|
|
|
{
|
|
|
|
return mbedtls_ssl_get_output_max_frag_len( ssl );
|
|
|
|
}
|
|
|
|
#endif /* !MBEDTLS_DEPRECATED_REMOVED */
|
2015-08-31 18:30:52 +02:00
|
|
|
#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
|
|
|
|
|
2018-08-20 09:34:02 +02:00
|
|
|
#if defined(MBEDTLS_SSL_PROTO_DTLS)
|
2020-02-05 11:50:12 +01:00
|
|
|
size_t mbedtls_ssl_get_current_mtu( const mbedtls_ssl_context *ssl )
|
2018-08-20 09:34:02 +02:00
|
|
|
{
|
2018-10-09 14:24:12 +02:00
|
|
|
/* Return unlimited mtu for client hello messages to avoid fragmentation. */
|
|
|
|
if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
|
|
|
|
( ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
|
|
|
|
ssl->state == MBEDTLS_SSL_SERVER_HELLO ) )
|
|
|
|
return ( 0 );
|
|
|
|
|
2018-08-20 09:34:02 +02:00
|
|
|
if( ssl->handshake == NULL || ssl->handshake->mtu == 0 )
|
|
|
|
return( ssl->mtu );
|
|
|
|
|
|
|
|
if( ssl->mtu == 0 )
|
|
|
|
return( ssl->handshake->mtu );
|
|
|
|
|
|
|
|
return( ssl->mtu < ssl->handshake->mtu ?
|
|
|
|
ssl->mtu : ssl->handshake->mtu );
|
|
|
|
}
|
|
|
|
#endif /* MBEDTLS_SSL_PROTO_DTLS */
|
|
|
|
|
2017-09-21 13:49:50 +02:00
|
|
|
int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
|
|
|
|
{
|
|
|
|
size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
|
|
|
|
|
2018-08-21 11:20:58 +02:00
|
|
|
#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
|
|
|
|
!defined(MBEDTLS_SSL_PROTO_DTLS)
|
|
|
|
(void) ssl;
|
|
|
|
#endif
|
|
|
|
|
2017-09-21 13:49:50 +02:00
|
|
|
#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
|
2020-04-03 11:25:29 +02:00
|
|
|
const size_t mfl = mbedtls_ssl_get_output_max_frag_len( ssl );
|
2017-09-21 13:49:50 +02:00
|
|
|
|
|
|
|
if( max_len > mfl )
|
|
|
|
max_len = mfl;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_SSL_PROTO_DTLS)
|
2020-02-05 11:50:12 +01:00
|
|
|
if( mbedtls_ssl_get_current_mtu( ssl ) != 0 )
|
2017-09-21 13:49:50 +02:00
|
|
|
{
|
2020-02-05 11:50:12 +01:00
|
|
|
const size_t mtu = mbedtls_ssl_get_current_mtu( ssl );
|
2017-09-21 13:49:50 +02:00
|
|
|
const int ret = mbedtls_ssl_get_record_expansion( ssl );
|
|
|
|
const size_t overhead = (size_t) ret;
|
|
|
|
|
|
|
|
if( ret < 0 )
|
|
|
|
return( ret );
|
|
|
|
|
|
|
|
if( mtu <= overhead )
|
|
|
|
{
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
|
|
|
|
return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
|
|
|
|
}
|
|
|
|
|
|
|
|
if( max_len > mtu - overhead )
|
|
|
|
max_len = mtu - overhead;
|
|
|
|
}
|
2018-08-20 09:34:02 +02:00
|
|
|
#endif /* MBEDTLS_SSL_PROTO_DTLS */
|
2017-09-21 13:49:50 +02:00
|
|
|
|
2018-08-10 13:35:02 +02:00
|
|
|
#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
|
|
|
|
!defined(MBEDTLS_SSL_PROTO_DTLS)
|
|
|
|
((void) ssl);
|
2017-09-21 13:49:50 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
return( (int) max_len );
|
|
|
|
}
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_X509_CRT_PARSE_C)
|
|
|
|
const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
|
2012-10-30 08:51:03 +01:00
|
|
|
{
|
|
|
|
if( ssl == NULL || ssl->session == NULL )
|
2014-06-17 14:06:49 +02:00
|
|
|
return( NULL );
|
2012-10-30 08:51:03 +01:00
|
|
|
|
2019-02-07 14:18:46 +01:00
|
|
|
#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
|
2014-06-17 14:06:49 +02:00
|
|
|
return( ssl->session->peer_cert );
|
2019-02-07 14:18:46 +01:00
|
|
|
#else
|
|
|
|
return( NULL );
|
|
|
|
#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
|
2012-10-30 08:51:03 +01:00
|
|
|
}
|
2015-04-08 12:49:31 +02:00
|
|
|
#endif /* MBEDTLS_X509_CRT_PARSE_C */
|
2012-10-30 08:51:03 +01:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_SSL_CLI_C)
|
2019-02-05 12:42:30 +01:00
|
|
|
int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl,
|
|
|
|
mbedtls_ssl_session *dst )
|
2013-07-30 12:41:56 +02:00
|
|
|
{
|
|
|
|
if( ssl == NULL ||
|
|
|
|
dst == NULL ||
|
|
|
|
ssl->session == NULL ||
|
2015-05-04 10:55:58 +02:00
|
|
|
ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
|
2013-07-30 12:41:56 +02:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
|
2013-07-30 12:41:56 +02:00
|
|
|
}
|
|
|
|
|
2019-02-06 15:30:46 +01:00
|
|
|
return( mbedtls_ssl_session_copy( dst, ssl->session ) );
|
2013-07-30 12:41:56 +02:00
|
|
|
}
|
2015-04-08 12:49:31 +02:00
|
|
|
#endif /* MBEDTLS_SSL_CLI_C */
|
2013-07-30 12:41:56 +02:00
|
|
|
|
2019-05-20 11:12:28 +02:00
|
|
|
const mbedtls_ssl_session *mbedtls_ssl_get_session_pointer( const mbedtls_ssl_context *ssl )
|
|
|
|
{
|
|
|
|
if( ssl == NULL )
|
|
|
|
return( NULL );
|
|
|
|
|
|
|
|
return( ssl->session );
|
|
|
|
}
|
|
|
|
|
2019-05-16 13:39:07 +02:00
|
|
|
/*
|
|
|
|
* Define ticket header determining Mbed TLS version
|
|
|
|
* and structure of the ticket.
|
|
|
|
*/
|
|
|
|
|
2019-05-16 13:50:45 +02:00
|
|
|
/*
|
2019-05-28 15:30:45 +02:00
|
|
|
* Define bitflag determining compile-time settings influencing
|
|
|
|
* structure of serialized SSL sessions.
|
2019-05-16 13:50:45 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_HAVE_TIME)
|
2019-05-29 12:10:18 +02:00
|
|
|
#define SSL_SERIALIZED_SESSION_CONFIG_TIME 1
|
2019-05-16 13:50:45 +02:00
|
|
|
#else
|
2019-05-29 12:10:18 +02:00
|
|
|
#define SSL_SERIALIZED_SESSION_CONFIG_TIME 0
|
2019-05-16 13:50:45 +02:00
|
|
|
#endif /* MBEDTLS_HAVE_TIME */
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_X509_CRT_PARSE_C)
|
2019-05-29 12:10:18 +02:00
|
|
|
#define SSL_SERIALIZED_SESSION_CONFIG_CRT 1
|
2019-05-16 13:50:45 +02:00
|
|
|
#else
|
2019-05-29 12:10:18 +02:00
|
|
|
#define SSL_SERIALIZED_SESSION_CONFIG_CRT 0
|
2019-05-16 13:50:45 +02:00
|
|
|
#endif /* MBEDTLS_X509_CRT_PARSE_C */
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_SESSION_TICKETS)
|
2019-05-29 12:10:18 +02:00
|
|
|
#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 1
|
2019-05-16 13:50:45 +02:00
|
|
|
#else
|
2019-05-29 12:10:18 +02:00
|
|
|
#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 0
|
2019-05-16 13:50:45 +02:00
|
|
|
#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_SESSION_TICKETS */
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
|
2019-05-29 12:10:18 +02:00
|
|
|
#define SSL_SERIALIZED_SESSION_CONFIG_MFL 1
|
2019-05-16 13:50:45 +02:00
|
|
|
#else
|
2019-05-29 12:10:18 +02:00
|
|
|
#define SSL_SERIALIZED_SESSION_CONFIG_MFL 0
|
2019-05-16 13:50:45 +02:00
|
|
|
#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
|
2019-05-29 12:10:18 +02:00
|
|
|
#define SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC 1
|
2019-05-16 13:50:45 +02:00
|
|
|
#else
|
2019-05-29 12:10:18 +02:00
|
|
|
#define SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC 0
|
2019-05-16 13:50:45 +02:00
|
|
|
#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
|
2019-05-29 12:10:18 +02:00
|
|
|
#define SSL_SERIALIZED_SESSION_CONFIG_ETM 1
|
2019-05-16 13:50:45 +02:00
|
|
|
#else
|
2019-05-29 12:10:18 +02:00
|
|
|
#define SSL_SERIALIZED_SESSION_CONFIG_ETM 0
|
2019-05-16 13:50:45 +02:00
|
|
|
#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_SSL_SESSION_TICKETS)
|
|
|
|
#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 1
|
|
|
|
#else
|
|
|
|
#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 0
|
|
|
|
#endif /* MBEDTLS_SSL_SESSION_TICKETS */
|
|
|
|
|
2019-05-29 12:10:18 +02:00
|
|
|
#define SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT 0
|
|
|
|
#define SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT 1
|
|
|
|
#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT 2
|
|
|
|
#define SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT 3
|
|
|
|
#define SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC_BIT 4
|
|
|
|
#define SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT 5
|
|
|
|
#define SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT 6
|
|
|
|
|
2019-05-28 15:30:45 +02:00
|
|
|
#define SSL_SERIALIZED_SESSION_CONFIG_BITFLAG \
|
2019-05-29 12:10:18 +02:00
|
|
|
( (uint16_t) ( \
|
|
|
|
( SSL_SERIALIZED_SESSION_CONFIG_TIME << SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT ) | \
|
|
|
|
( SSL_SERIALIZED_SESSION_CONFIG_CRT << SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT ) | \
|
|
|
|
( SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET << SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT ) | \
|
|
|
|
( SSL_SERIALIZED_SESSION_CONFIG_MFL << SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT ) | \
|
|
|
|
( SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC << SSL_SERIALIZED_SESSION_CONFIG_TRUNC_HMAC_BIT ) | \
|
|
|
|
( SSL_SERIALIZED_SESSION_CONFIG_ETM << SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT ) | \
|
2019-06-04 10:43:16 +02:00
|
|
|
( SSL_SERIALIZED_SESSION_CONFIG_TICKET << SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT ) ) )
|
2019-05-16 13:50:45 +02:00
|
|
|
|
2019-05-16 13:41:07 +02:00
|
|
|
static unsigned char ssl_serialized_session_header[] = {
|
2019-05-16 13:50:45 +02:00
|
|
|
MBEDTLS_VERSION_MAJOR,
|
|
|
|
MBEDTLS_VERSION_MINOR,
|
|
|
|
MBEDTLS_VERSION_PATCH,
|
2019-05-28 15:30:45 +02:00
|
|
|
( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG >> 8 ) & 0xFF,
|
|
|
|
( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG >> 0 ) & 0xFF,
|
2019-05-16 13:41:07 +02:00
|
|
|
};
|
2019-05-16 13:39:07 +02:00
|
|
|
|
2019-05-16 10:08:35 +02:00
|
|
|
/*
|
|
|
|
* Serialize a session in the following format:
|
2019-05-16 11:11:08 +02:00
|
|
|
* (in the presentation language of TLS, RFC 8446 section 3)
|
2019-05-16 10:08:35 +02:00
|
|
|
*
|
2019-05-28 15:30:45 +02:00
|
|
|
* opaque mbedtls_version[3]; // major, minor, patch
|
|
|
|
* opaque session_format[2]; // version-specific 16-bit field determining
|
|
|
|
* // the format of the remaining
|
|
|
|
* // serialized data.
|
2019-05-29 12:08:00 +02:00
|
|
|
*
|
|
|
|
* Note: When updating the format, remember to keep
|
|
|
|
* these version+format bytes.
|
|
|
|
*
|
2019-06-04 10:43:16 +02:00
|
|
|
* // In this version, `session_format` determines
|
|
|
|
* // the setting of those compile-time
|
|
|
|
* // configuration options which influence
|
2019-05-28 15:30:45 +02:00
|
|
|
* // the structure of mbedtls_ssl_session.
|
2019-05-24 12:06:29 +02:00
|
|
|
* uint64 start_time;
|
2019-05-28 15:30:45 +02:00
|
|
|
* uint8 ciphersuite[2]; // defined by the standard
|
|
|
|
* uint8 compression; // 0 or 1
|
|
|
|
* uint8 session_id_len; // at most 32
|
2019-05-24 12:06:29 +02:00
|
|
|
* opaque session_id[32];
|
2019-05-28 15:30:45 +02:00
|
|
|
* opaque master[48]; // fixed length in the standard
|
2019-05-24 12:06:29 +02:00
|
|
|
* uint32 verify_result;
|
2019-05-28 15:30:45 +02:00
|
|
|
* opaque peer_cert<0..2^24-1>; // length 0 means no peer cert
|
|
|
|
* opaque ticket<0..2^24-1>; // length 0 means no ticket
|
2019-05-24 12:06:29 +02:00
|
|
|
* uint32 ticket_lifetime;
|
2019-05-28 15:30:45 +02:00
|
|
|
* uint8 mfl_code; // up to 255 according to standard
|
|
|
|
* uint8 trunc_hmac; // 0 or 1
|
|
|
|
* uint8 encrypt_then_mac; // 0 or 1
|
2019-05-16 10:08:35 +02:00
|
|
|
*
|
2019-05-24 12:06:29 +02:00
|
|
|
* The order is the same as in the definition of the structure, except
|
|
|
|
* verify_result is put before peer_cert so that all mandatory fields come
|
|
|
|
* together in one block.
|
2019-05-16 10:08:35 +02:00
|
|
|
*/
|
2019-07-23 16:52:45 +02:00
|
|
|
static int ssl_session_save( const mbedtls_ssl_session *session,
|
|
|
|
unsigned char omit_header,
|
|
|
|
unsigned char *buf,
|
|
|
|
size_t buf_len,
|
|
|
|
size_t *olen )
|
2019-05-16 10:08:35 +02:00
|
|
|
{
|
|
|
|
unsigned char *p = buf;
|
2019-05-21 11:01:32 +02:00
|
|
|
size_t used = 0;
|
2019-05-24 12:06:29 +02:00
|
|
|
#if defined(MBEDTLS_HAVE_TIME)
|
|
|
|
uint64_t start;
|
|
|
|
#endif
|
2019-05-16 10:08:35 +02:00
|
|
|
#if defined(MBEDTLS_X509_CRT_PARSE_C)
|
|
|
|
#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
|
|
|
|
size_t cert_len;
|
|
|
|
#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
|
|
|
|
#endif /* MBEDTLS_X509_CRT_PARSE_C */
|
|
|
|
|
2019-05-24 12:06:29 +02:00
|
|
|
|
2019-07-23 16:52:45 +02:00
|
|
|
if( !omit_header )
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Add version identifier
|
|
|
|
*/
|
2019-05-16 13:39:07 +02:00
|
|
|
|
2019-07-23 16:52:45 +02:00
|
|
|
used += sizeof( ssl_serialized_session_header );
|
2019-05-16 13:39:07 +02:00
|
|
|
|
2019-07-23 16:52:45 +02:00
|
|
|
if( used <= buf_len )
|
|
|
|
{
|
|
|
|
memcpy( p, ssl_serialized_session_header,
|
|
|
|
sizeof( ssl_serialized_session_header ) );
|
|
|
|
p += sizeof( ssl_serialized_session_header );
|
|
|
|
}
|
2019-05-16 13:39:07 +02:00
|
|
|
}
|
|
|
|
|
2019-05-24 12:06:29 +02:00
|
|
|
/*
|
|
|
|
* Time
|
|
|
|
*/
|
|
|
|
#if defined(MBEDTLS_HAVE_TIME)
|
|
|
|
used += 8;
|
|
|
|
|
|
|
|
if( used <= buf_len )
|
|
|
|
{
|
|
|
|
start = (uint64_t) session->start;
|
|
|
|
|
|
|
|
*p++ = (unsigned char)( ( start >> 56 ) & 0xFF );
|
|
|
|
*p++ = (unsigned char)( ( start >> 48 ) & 0xFF );
|
|
|
|
*p++ = (unsigned char)( ( start >> 40 ) & 0xFF );
|
|
|
|
*p++ = (unsigned char)( ( start >> 32 ) & 0xFF );
|
|
|
|
*p++ = (unsigned char)( ( start >> 24 ) & 0xFF );
|
|
|
|
*p++ = (unsigned char)( ( start >> 16 ) & 0xFF );
|
|
|
|
*p++ = (unsigned char)( ( start >> 8 ) & 0xFF );
|
|
|
|
*p++ = (unsigned char)( ( start ) & 0xFF );
|
|
|
|
}
|
|
|
|
#endif /* MBEDTLS_HAVE_TIME */
|
|
|
|
|
2019-05-16 11:11:08 +02:00
|
|
|
/*
|
2019-05-24 12:06:29 +02:00
|
|
|
* Basic mandatory fields
|
2019-05-16 11:11:08 +02:00
|
|
|
*/
|
2019-05-24 12:06:29 +02:00
|
|
|
used += 2 /* ciphersuite */
|
|
|
|
+ 1 /* compression */
|
|
|
|
+ 1 /* id_len */
|
|
|
|
+ sizeof( session->id )
|
|
|
|
+ sizeof( session->master )
|
|
|
|
+ 4; /* verify_result */
|
2019-05-16 10:08:35 +02:00
|
|
|
|
2019-05-21 11:01:32 +02:00
|
|
|
if( used <= buf_len )
|
|
|
|
{
|
2019-05-24 12:06:29 +02:00
|
|
|
*p++ = (unsigned char)( ( session->ciphersuite >> 8 ) & 0xFF );
|
|
|
|
*p++ = (unsigned char)( ( session->ciphersuite ) & 0xFF );
|
|
|
|
|
|
|
|
*p++ = (unsigned char)( session->compression & 0xFF );
|
|
|
|
|
|
|
|
*p++ = (unsigned char)( session->id_len & 0xFF );
|
|
|
|
memcpy( p, session->id, 32 );
|
|
|
|
p += 32;
|
|
|
|
|
|
|
|
memcpy( p, session->master, 48 );
|
|
|
|
p += 48;
|
|
|
|
|
|
|
|
*p++ = (unsigned char)( ( session->verify_result >> 24 ) & 0xFF );
|
|
|
|
*p++ = (unsigned char)( ( session->verify_result >> 16 ) & 0xFF );
|
|
|
|
*p++ = (unsigned char)( ( session->verify_result >> 8 ) & 0xFF );
|
|
|
|
*p++ = (unsigned char)( ( session->verify_result ) & 0xFF );
|
2019-05-21 11:01:32 +02:00
|
|
|
}
|
2019-05-16 10:08:35 +02:00
|
|
|
|
2019-05-16 11:11:08 +02:00
|
|
|
/*
|
2019-05-24 12:06:29 +02:00
|
|
|
* Peer's end-entity certificate
|
2019-05-16 11:11:08 +02:00
|
|
|
*/
|
2019-05-16 10:08:35 +02:00
|
|
|
#if defined(MBEDTLS_X509_CRT_PARSE_C)
|
|
|
|
#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
|
|
|
|
if( session->peer_cert == NULL )
|
|
|
|
cert_len = 0;
|
|
|
|
else
|
|
|
|
cert_len = session->peer_cert->raw.len;
|
|
|
|
|
2019-05-21 11:01:32 +02:00
|
|
|
used += 3 + cert_len;
|
2019-05-16 10:08:35 +02:00
|
|
|
|
2019-05-21 11:01:32 +02:00
|
|
|
if( used <= buf_len )
|
2019-05-16 11:11:08 +02:00
|
|
|
{
|
2019-05-21 11:01:32 +02:00
|
|
|
*p++ = (unsigned char)( ( cert_len >> 16 ) & 0xFF );
|
|
|
|
*p++ = (unsigned char)( ( cert_len >> 8 ) & 0xFF );
|
|
|
|
*p++ = (unsigned char)( ( cert_len ) & 0xFF );
|
2019-05-16 10:08:35 +02:00
|
|
|
|
2019-05-21 11:01:32 +02:00
|
|
|
if( session->peer_cert != NULL )
|
|
|
|
{
|
|
|
|
memcpy( p, session->peer_cert->raw.p, cert_len );
|
|
|
|
p += cert_len;
|
|
|
|
}
|
|
|
|
}
|
2019-05-16 10:08:35 +02:00
|
|
|
#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
|
2019-05-24 12:06:29 +02:00
|
|
|
if( session->peer_cert_digest != NULL )
|
|
|
|
{
|
|
|
|
used += 1 /* type */ + 1 /* length */ + session->peer_cert_digest_len;
|
|
|
|
if( used <= buf_len )
|
|
|
|
{
|
|
|
|
*p++ = (unsigned char) session->peer_cert_digest_type;
|
|
|
|
*p++ = (unsigned char) session->peer_cert_digest_len;
|
|
|
|
memcpy( p, session->peer_cert_digest,
|
|
|
|
session->peer_cert_digest_len );
|
|
|
|
p += session->peer_cert_digest_len;
|
|
|
|
}
|
|
|
|
}
|
2019-05-16 10:08:35 +02:00
|
|
|
else
|
2019-05-21 11:01:32 +02:00
|
|
|
{
|
2019-05-24 12:06:29 +02:00
|
|
|
used += 2;
|
|
|
|
if( used <= buf_len )
|
|
|
|
{
|
|
|
|
*p++ = (unsigned char) MBEDTLS_MD_NONE;
|
|
|
|
*p++ = 0;
|
|
|
|
}
|
2019-05-21 11:01:32 +02:00
|
|
|
}
|
2019-05-16 10:08:35 +02:00
|
|
|
#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
|
|
|
|
#endif /* MBEDTLS_X509_CRT_PARSE_C */
|
|
|
|
|
2019-05-16 11:11:08 +02:00
|
|
|
/*
|
2019-05-24 12:06:29 +02:00
|
|
|
* Session ticket if any, plus associated data
|
2020-02-05 15:44:10 +01:00
|
|
|
*/
|
|
|
|
#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
|
|
|
|
used += 3 + session->ticket_len + 4; /* len + ticket + lifetime */
|
2019-05-16 11:11:08 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
if( used <= buf_len )
|
2019-05-16 11:11:08 +02:00
|
|
|
{
|
2020-02-05 15:44:10 +01:00
|
|
|
*p++ = (unsigned char)( ( session->ticket_len >> 16 ) & 0xFF );
|
|
|
|
*p++ = (unsigned char)( ( session->ticket_len >> 8 ) & 0xFF );
|
|
|
|
*p++ = (unsigned char)( ( session->ticket_len ) & 0xFF );
|
2019-05-16 11:11:08 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
if( session->ticket != NULL )
|
|
|
|
{
|
|
|
|
memcpy( p, session->ticket, session->ticket_len );
|
|
|
|
p += session->ticket_len;
|
|
|
|
}
|
2019-05-16 11:11:08 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
*p++ = (unsigned char)( ( session->ticket_lifetime >> 24 ) & 0xFF );
|
|
|
|
*p++ = (unsigned char)( ( session->ticket_lifetime >> 16 ) & 0xFF );
|
|
|
|
*p++ = (unsigned char)( ( session->ticket_lifetime >> 8 ) & 0xFF );
|
|
|
|
*p++ = (unsigned char)( ( session->ticket_lifetime ) & 0xFF );
|
2019-05-16 11:11:08 +02:00
|
|
|
}
|
|
|
|
#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
|
|
|
|
|
2019-05-24 12:06:29 +02:00
|
|
|
/*
|
|
|
|
* Misc extension-related info
|
|
|
|
*/
|
|
|
|
#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
|
2020-02-05 15:44:10 +01:00
|
|
|
used += 1;
|
2019-05-24 12:06:29 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
if( used <= buf_len )
|
|
|
|
*p++ = session->mfl_code;
|
2019-05-24 12:06:29 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
|
2020-02-05 15:44:10 +01:00
|
|
|
used += 1;
|
2019-05-24 12:06:29 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
if( used <= buf_len )
|
|
|
|
*p++ = (unsigned char)( ( session->trunc_hmac ) & 0xFF );
|
2019-05-24 12:06:29 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
|
2020-02-05 15:44:10 +01:00
|
|
|
used += 1;
|
2015-09-01 17:43:40 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
if( used <= buf_len )
|
|
|
|
*p++ = (unsigned char)( ( session->encrypt_then_mac ) & 0xFF );
|
2009-01-03 22:22:43 +01:00
|
|
|
#endif
|
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
/* Done */
|
|
|
|
*olen = used;
|
2013-10-30 13:06:54 +01:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
if( used > buf_len )
|
|
|
|
return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
|
2013-10-30 13:06:54 +01:00
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2020-02-05 15:44:10 +01:00
|
|
|
* Public wrapper for ssl_session_save()
|
2013-10-30 13:06:54 +01:00
|
|
|
*/
|
2020-02-05 15:44:10 +01:00
|
|
|
int mbedtls_ssl_session_save( const mbedtls_ssl_session *session,
|
|
|
|
unsigned char *buf,
|
|
|
|
size_t buf_len,
|
|
|
|
size_t *olen )
|
2012-09-16 21:57:18 +02:00
|
|
|
{
|
2020-02-05 15:44:10 +01:00
|
|
|
return( ssl_session_save( session, 0, buf, buf_len, olen ) );
|
2012-09-16 21:57:18 +02:00
|
|
|
}
|
|
|
|
|
2013-10-30 13:06:54 +01:00
|
|
|
/*
|
2020-02-05 15:44:10 +01:00
|
|
|
* Deserialize session, see mbedtls_ssl_session_save() for format.
|
|
|
|
*
|
|
|
|
* This internal version is wrapped by a public function that cleans up in
|
|
|
|
* case of error, and has an extra option omit_header.
|
2013-10-30 13:06:54 +01:00
|
|
|
*/
|
2020-02-05 15:44:10 +01:00
|
|
|
static int ssl_session_load( mbedtls_ssl_session *session,
|
|
|
|
unsigned char omit_header,
|
|
|
|
const unsigned char *buf,
|
|
|
|
size_t len )
|
2013-10-30 13:06:54 +01:00
|
|
|
{
|
2020-02-05 15:44:10 +01:00
|
|
|
const unsigned char *p = buf;
|
|
|
|
const unsigned char * const end = buf + len;
|
|
|
|
#if defined(MBEDTLS_HAVE_TIME)
|
|
|
|
uint64_t start;
|
|
|
|
#endif
|
|
|
|
#if defined(MBEDTLS_X509_CRT_PARSE_C)
|
|
|
|
#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
|
|
|
|
size_t cert_len;
|
|
|
|
#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
|
|
|
|
#endif /* MBEDTLS_X509_CRT_PARSE_C */
|
2015-09-01 17:43:40 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
if( !omit_header )
|
2013-10-30 16:41:21 +01:00
|
|
|
{
|
2020-02-05 15:44:10 +01:00
|
|
|
/*
|
|
|
|
* Check version identifier
|
|
|
|
*/
|
2013-10-30 16:41:21 +01:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
if( (size_t)( end - p ) < sizeof( ssl_serialized_session_header ) )
|
2015-04-08 12:49:31 +02:00
|
|
|
return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
|
2013-10-30 16:41:21 +01:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
if( memcmp( p, ssl_serialized_session_header,
|
|
|
|
sizeof( ssl_serialized_session_header ) ) != 0 )
|
2013-10-30 16:41:21 +01:00
|
|
|
{
|
2020-02-05 15:44:10 +01:00
|
|
|
return( MBEDTLS_ERR_SSL_VERSION_MISMATCH );
|
2013-10-30 16:41:21 +01:00
|
|
|
}
|
2020-02-05 15:44:10 +01:00
|
|
|
p += sizeof( ssl_serialized_session_header );
|
2013-10-30 16:41:21 +01:00
|
|
|
}
|
2013-10-30 13:06:54 +01:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
/*
|
|
|
|
* Time
|
|
|
|
*/
|
|
|
|
#if defined(MBEDTLS_HAVE_TIME)
|
|
|
|
if( 8 > (size_t)( end - p ) )
|
|
|
|
return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
|
2016-12-15 18:01:16 +01:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
start = ( (uint64_t) p[0] << 56 ) |
|
|
|
|
( (uint64_t) p[1] << 48 ) |
|
|
|
|
( (uint64_t) p[2] << 40 ) |
|
|
|
|
( (uint64_t) p[3] << 32 ) |
|
|
|
|
( (uint64_t) p[4] << 24 ) |
|
|
|
|
( (uint64_t) p[5] << 16 ) |
|
|
|
|
( (uint64_t) p[6] << 8 ) |
|
|
|
|
( (uint64_t) p[7] );
|
|
|
|
p += 8;
|
2014-11-04 21:04:22 +01:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
session->start = (time_t) start;
|
|
|
|
#endif /* MBEDTLS_HAVE_TIME */
|
2016-12-15 18:01:16 +01:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
/*
|
|
|
|
* Basic mandatory fields
|
|
|
|
*/
|
|
|
|
if( 2 + 1 + 1 + 32 + 48 + 4 > (size_t)( end - p ) )
|
|
|
|
return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
|
2014-11-04 21:04:22 +01:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
session->ciphersuite = ( p[0] << 8 ) | p[1];
|
|
|
|
p += 2;
|
2013-10-30 13:06:54 +01:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
session->compression = *p++;
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
session->id_len = *p++;
|
|
|
|
memcpy( session->id, p, 32 );
|
|
|
|
p += 32;
|
2015-09-01 17:43:40 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
memcpy( session->master, p, 48 );
|
|
|
|
p += 48;
|
2014-09-23 09:42:16 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
session->verify_result = ( (uint32_t) p[0] << 24 ) |
|
|
|
|
( (uint32_t) p[1] << 16 ) |
|
|
|
|
( (uint32_t) p[2] << 8 ) |
|
|
|
|
( (uint32_t) p[3] );
|
|
|
|
p += 4;
|
2014-09-23 09:42:16 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
/* Immediately clear invalid pointer values that have been read, in case
|
|
|
|
* we exit early before we replaced them with valid ones. */
|
|
|
|
#if defined(MBEDTLS_X509_CRT_PARSE_C)
|
|
|
|
#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
|
|
|
|
session->peer_cert = NULL;
|
|
|
|
#else
|
|
|
|
session->peer_cert_digest = NULL;
|
|
|
|
#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
|
|
|
|
#endif /* MBEDTLS_X509_CRT_PARSE_C */
|
|
|
|
#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
|
|
|
|
session->ticket = NULL;
|
|
|
|
#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2017-05-24 17:27:30 +02:00
|
|
|
/*
|
2020-02-05 15:44:10 +01:00
|
|
|
* Peer certificate
|
2017-05-24 17:27:30 +02:00
|
|
|
*/
|
2020-02-05 15:44:10 +01:00
|
|
|
#if defined(MBEDTLS_X509_CRT_PARSE_C)
|
|
|
|
#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
|
|
|
|
/* Deserialize CRT from the end of the ticket. */
|
|
|
|
if( 3 > (size_t)( end - p ) )
|
|
|
|
return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
|
2017-05-24 17:27:30 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
cert_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2];
|
|
|
|
p += 3;
|
2014-11-04 21:04:22 +01:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
if( cert_len != 0 )
|
2009-01-03 22:22:43 +01:00
|
|
|
{
|
2020-02-05 15:44:10 +01:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
|
|
|
|
|
|
|
if( cert_len > (size_t)( end - p ) )
|
|
|
|
return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
|
|
|
|
|
|
|
|
session->peer_cert = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
|
|
|
|
|
|
|
|
if( session->peer_cert == NULL )
|
|
|
|
return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
|
|
|
|
|
|
|
|
mbedtls_x509_crt_init( session->peer_cert );
|
|
|
|
|
|
|
|
if( ( ret = mbedtls_x509_crt_parse_der( session->peer_cert,
|
|
|
|
p, cert_len ) ) != 0 )
|
2009-01-03 22:22:43 +01:00
|
|
|
{
|
2020-02-05 15:44:10 +01:00
|
|
|
mbedtls_x509_crt_free( session->peer_cert );
|
|
|
|
mbedtls_free( session->peer_cert );
|
|
|
|
session->peer_cert = NULL;
|
2009-01-03 22:22:43 +01:00
|
|
|
return( ret );
|
|
|
|
}
|
2020-02-05 15:44:10 +01:00
|
|
|
|
|
|
|
p += cert_len;
|
2009-01-03 22:22:43 +01:00
|
|
|
}
|
2020-02-05 15:44:10 +01:00
|
|
|
#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
|
|
|
|
/* Deserialize CRT digest from the end of the ticket. */
|
|
|
|
if( 2 > (size_t)( end - p ) )
|
|
|
|
return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
|
|
|
|
|
|
|
|
session->peer_cert_digest_type = (mbedtls_md_type_t) *p++;
|
|
|
|
session->peer_cert_digest_len = (size_t) *p++;
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
if( session->peer_cert_digest_len != 0 )
|
2009-01-03 22:22:43 +01:00
|
|
|
{
|
2020-02-05 15:44:10 +01:00
|
|
|
const mbedtls_md_info_t *md_info =
|
|
|
|
mbedtls_md_info_from_type( session->peer_cert_digest_type );
|
|
|
|
if( md_info == NULL )
|
|
|
|
return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
|
|
|
|
if( session->peer_cert_digest_len != mbedtls_md_get_size( md_info ) )
|
|
|
|
return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
|
2014-10-01 18:29:03 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
if( session->peer_cert_digest_len > (size_t)( end - p ) )
|
|
|
|
return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
|
2011-05-18 15:32:51 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
session->peer_cert_digest =
|
|
|
|
mbedtls_calloc( 1, session->peer_cert_digest_len );
|
|
|
|
if( session->peer_cert_digest == NULL )
|
|
|
|
return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
memcpy( session->peer_cert_digest, p,
|
|
|
|
session->peer_cert_digest_len );
|
|
|
|
p += session->peer_cert_digest_len;
|
|
|
|
}
|
|
|
|
#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
|
|
|
|
#endif /* MBEDTLS_X509_CRT_PARSE_C */
|
2011-05-18 15:32:51 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
/*
|
|
|
|
* Session ticket and associated data
|
|
|
|
*/
|
|
|
|
#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
|
|
|
|
if( 3 > (size_t)( end - p ) )
|
|
|
|
return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
session->ticket_len = ( p[0] << 16 ) | ( p[1] << 8 ) | p[2];
|
|
|
|
p += 3;
|
2012-09-16 21:57:18 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
if( session->ticket_len != 0 )
|
|
|
|
{
|
|
|
|
if( session->ticket_len > (size_t)( end - p ) )
|
|
|
|
return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
|
2017-05-24 17:27:30 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
session->ticket = mbedtls_calloc( 1, session->ticket_len );
|
|
|
|
if( session->ticket == NULL )
|
|
|
|
return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
|
2014-09-06 12:27:02 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
memcpy( session->ticket, p, session->ticket_len );
|
|
|
|
p += session->ticket_len;
|
|
|
|
}
|
2014-09-06 12:27:02 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
if( 4 > (size_t)( end - p ) )
|
|
|
|
return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
|
2014-09-06 12:27:02 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
session->ticket_lifetime = ( (uint32_t) p[0] << 24 ) |
|
|
|
|
( (uint32_t) p[1] << 16 ) |
|
|
|
|
( (uint32_t) p[2] << 8 ) |
|
|
|
|
( (uint32_t) p[3] );
|
|
|
|
p += 4;
|
|
|
|
#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
|
2017-05-24 17:27:30 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
/*
|
|
|
|
* Misc extension-related info
|
|
|
|
*/
|
|
|
|
#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
|
|
|
|
if( 1 > (size_t)( end - p ) )
|
|
|
|
return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
|
2012-09-16 21:57:18 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
session->mfl_code = *p++;
|
2017-10-17 12:03:04 +02:00
|
|
|
#endif
|
2017-05-24 17:27:30 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
|
|
|
|
if( 1 > (size_t)( end - p ) )
|
|
|
|
return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
|
2012-09-16 21:57:18 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
session->trunc_hmac = *p++;
|
|
|
|
#endif
|
2014-08-19 12:28:50 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
|
|
|
|
if( 1 > (size_t)( end - p ) )
|
|
|
|
return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
|
2014-08-19 12:28:50 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
session->encrypt_then_mac = *p++;
|
|
|
|
#endif
|
2014-08-19 12:28:50 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
/* Done, should have consumed entire buffer */
|
|
|
|
if( p != end )
|
|
|
|
return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
return( 0 );
|
|
|
|
}
|
2014-10-01 18:29:03 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
/*
|
|
|
|
* Deserialize session: public wrapper for error cleaning
|
|
|
|
*/
|
|
|
|
int mbedtls_ssl_session_load( mbedtls_ssl_session *session,
|
|
|
|
const unsigned char *buf,
|
|
|
|
size_t len )
|
|
|
|
{
|
|
|
|
int ret = ssl_session_load( session, 0, buf, len );
|
2014-10-15 13:52:48 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
if( ret != 0 )
|
|
|
|
mbedtls_ssl_session_free( session );
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
return( ret );
|
|
|
|
}
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
/*
|
|
|
|
* Perform a single step of the SSL handshake
|
|
|
|
*/
|
|
|
|
int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
|
|
|
|
{
|
|
|
|
int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
if( ssl == NULL || ssl->conf == NULL )
|
|
|
|
return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
#if defined(MBEDTLS_SSL_CLI_C)
|
|
|
|
if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
|
|
|
|
ret = mbedtls_ssl_handshake_client_step( ssl );
|
|
|
|
#endif
|
|
|
|
#if defined(MBEDTLS_SSL_SRV_C)
|
|
|
|
if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
|
|
|
|
ret = mbedtls_ssl_handshake_server_step( ssl );
|
|
|
|
#endif
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
return( ret );
|
2009-01-03 22:22:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2020-02-05 15:44:10 +01:00
|
|
|
* Perform the SSL handshake
|
2009-01-03 22:22:43 +01:00
|
|
|
*/
|
2020-02-05 15:44:10 +01:00
|
|
|
int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
|
2009-01-03 22:22:43 +01:00
|
|
|
{
|
2020-02-05 15:44:10 +01:00
|
|
|
int ret = 0;
|
2017-09-21 13:49:50 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
if( ssl == NULL || ssl->conf == NULL )
|
|
|
|
return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
|
2017-09-21 13:49:50 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
|
2011-06-08 15:10:54 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
|
2011-03-14 21:50:15 +01:00
|
|
|
{
|
2020-02-05 15:44:10 +01:00
|
|
|
ret = mbedtls_ssl_handshake_step( ssl );
|
2011-06-08 15:10:54 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
if( ret != 0 )
|
|
|
|
break;
|
2009-01-03 22:22:43 +01:00
|
|
|
}
|
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
|
|
|
|
|
|
|
|
return( ret );
|
2009-01-03 22:22:43 +01:00
|
|
|
}
|
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
#if defined(MBEDTLS_SSL_RENEGOTIATION)
|
|
|
|
#if defined(MBEDTLS_SSL_SRV_C)
|
2015-01-07 12:39:44 +01:00
|
|
|
/*
|
2020-02-05 15:44:10 +01:00
|
|
|
* Write HelloRequest to request renegotiation on server
|
2015-01-07 12:39:44 +01:00
|
|
|
*/
|
2020-02-05 15:44:10 +01:00
|
|
|
static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
|
2015-01-07 12:39:44 +01:00
|
|
|
{
|
2019-12-16 12:46:15 +01:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2015-01-07 12:39:44 +01:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
|
|
|
|
|
|
|
|
ssl->out_msglen = 4;
|
|
|
|
ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
|
|
|
|
ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
|
2015-01-07 12:39:44 +01:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
|
2015-01-07 12:39:44 +01:00
|
|
|
{
|
2020-02-05 15:44:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
|
|
|
|
return( ret );
|
2015-01-07 12:39:44 +01:00
|
|
|
}
|
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
|
2015-01-07 12:39:44 +01:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
return( 0 );
|
2015-01-07 12:39:44 +01:00
|
|
|
}
|
2020-02-05 15:44:10 +01:00
|
|
|
#endif /* MBEDTLS_SSL_SRV_C */
|
2015-01-07 12:39:44 +01:00
|
|
|
|
2015-04-15 19:09:03 +02:00
|
|
|
/*
|
2020-02-05 15:44:10 +01:00
|
|
|
* Actually renegotiate current connection, triggered by either:
|
|
|
|
* - any side: calling mbedtls_ssl_renegotiate(),
|
|
|
|
* - client: receiving a HelloRequest during mbedtls_ssl_read(),
|
|
|
|
* - server: receiving any handshake message on server during mbedtls_ssl_read() after
|
|
|
|
* the initial handshake is completed.
|
|
|
|
* If the handshake doesn't complete due to waiting for I/O, it will continue
|
|
|
|
* during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
|
2015-04-15 19:09:03 +02:00
|
|
|
*/
|
2020-02-05 15:44:10 +01:00
|
|
|
int mbedtls_ssl_start_renegotiation( mbedtls_ssl_context *ssl )
|
2015-04-15 19:09:03 +02:00
|
|
|
{
|
2019-12-16 12:46:15 +01:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2015-04-15 19:09:03 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
|
2015-04-15 19:09:03 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
|
|
|
|
return( ret );
|
2015-09-01 17:43:40 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
/* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
|
|
|
|
* the ServerHello will have message_seq = 1" */
|
|
|
|
#if defined(MBEDTLS_SSL_PROTO_DTLS)
|
|
|
|
if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
|
|
|
|
ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
|
2015-04-15 19:09:03 +02:00
|
|
|
{
|
2020-02-05 15:44:10 +01:00
|
|
|
if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
|
|
|
|
ssl->handshake->out_msg_seq = 1;
|
|
|
|
else
|
|
|
|
ssl->handshake->in_msg_seq = 1;
|
2015-04-15 19:09:03 +02:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
|
|
|
|
ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
|
|
|
|
|
|
|
|
if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
|
2015-04-15 19:09:03 +02:00
|
|
|
{
|
2020-02-05 15:44:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
|
|
|
|
return( ret );
|
2015-04-15 19:09:03 +02:00
|
|
|
}
|
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
|
2015-04-15 19:09:03 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
return( 0 );
|
2015-04-15 19:09:03 +02:00
|
|
|
}
|
|
|
|
|
2009-01-03 22:22:43 +01:00
|
|
|
/*
|
2020-02-05 15:44:10 +01:00
|
|
|
* Renegotiate current connection on client,
|
|
|
|
* or request renegotiation on server
|
2009-01-03 22:22:43 +01:00
|
|
|
*/
|
2020-02-05 15:44:10 +01:00
|
|
|
int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
|
2009-01-03 22:22:43 +01:00
|
|
|
{
|
2020-02-05 15:44:10 +01:00
|
|
|
int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2015-09-01 17:43:40 +02:00
|
|
|
if( ssl == NULL || ssl->conf == NULL )
|
|
|
|
return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
|
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
#if defined(MBEDTLS_SSL_SRV_C)
|
|
|
|
/* On server, just send the request */
|
|
|
|
if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
|
|
|
|
{
|
|
|
|
if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
|
|
|
|
return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
|
|
|
|
|
|
|
|
ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
/* Did we already try/start sending HelloRequest? */
|
|
|
|
if( ssl->out_left != 0 )
|
|
|
|
return( mbedtls_ssl_flush_output( ssl ) );
|
|
|
|
|
|
|
|
return( ssl_write_hello_request( ssl ) );
|
|
|
|
}
|
|
|
|
#endif /* MBEDTLS_SSL_SRV_C */
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
#if defined(MBEDTLS_SSL_CLI_C)
|
|
|
|
/*
|
|
|
|
* On client, either start the renegotiation process or,
|
|
|
|
* if already in progress, continue the handshake
|
|
|
|
*/
|
|
|
|
if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
|
2009-01-03 22:22:43 +01:00
|
|
|
{
|
2020-02-05 15:44:10 +01:00
|
|
|
if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
|
|
|
|
return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
|
|
|
|
|
|
|
|
if( ( ret = mbedtls_ssl_start_renegotiation( ssl ) ) != 0 )
|
2009-01-03 22:22:43 +01:00
|
|
|
{
|
2020-02-05 15:44:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_start_renegotiation", ret );
|
2009-01-03 22:22:43 +01:00
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
}
|
2020-02-05 15:44:10 +01:00
|
|
|
else
|
|
|
|
{
|
|
|
|
if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
|
|
|
|
{
|
|
|
|
MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
|
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif /* MBEDTLS_SSL_CLI_C */
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
return( ret );
|
2012-09-16 21:57:18 +02:00
|
|
|
}
|
2020-02-05 15:44:10 +01:00
|
|
|
#endif /* MBEDTLS_SSL_RENEGOTIATION */
|
2012-09-16 21:57:18 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_X509_CRT_PARSE_C)
|
|
|
|
static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
|
2013-09-23 20:04:20 +02:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_ssl_key_cert *cur = key_cert, *next;
|
2013-09-23 20:04:20 +02:00
|
|
|
|
|
|
|
while( cur != NULL )
|
|
|
|
{
|
|
|
|
next = cur->next;
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_free( cur );
|
2013-09-23 20:04:20 +02:00
|
|
|
cur = next;
|
|
|
|
}
|
|
|
|
}
|
2015-04-08 12:49:31 +02:00
|
|
|
#endif /* MBEDTLS_X509_CRT_PARSE_C */
|
2013-09-23 20:04:20 +02:00
|
|
|
|
2018-04-25 20:32:43 +02:00
|
|
|
void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
|
2012-09-16 21:57:18 +02:00
|
|
|
{
|
2018-04-25 20:32:43 +02:00
|
|
|
mbedtls_ssl_handshake_params *handshake = ssl->handshake;
|
|
|
|
|
2014-06-26 13:37:14 +02:00
|
|
|
if( handshake == NULL )
|
|
|
|
return;
|
|
|
|
|
2018-04-25 20:39:48 +02:00
|
|
|
#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
|
|
|
|
if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
|
|
|
|
{
|
2018-04-26 11:46:10 +02:00
|
|
|
ssl->conf->f_async_cancel( ssl );
|
2018-04-25 20:39:48 +02:00
|
|
|
handshake->async_in_progress = 0;
|
|
|
|
}
|
|
|
|
#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
|
|
|
|
|
2015-07-06 14:18:56 +02:00
|
|
|
#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
|
|
|
|
defined(MBEDTLS_SSL_PROTO_TLS1_1)
|
|
|
|
mbedtls_md5_free( &handshake->fin_md5 );
|
|
|
|
mbedtls_sha1_free( &handshake->fin_sha1 );
|
|
|
|
#endif
|
|
|
|
#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
|
|
|
|
#if defined(MBEDTLS_SHA256_C)
|
2019-01-29 15:14:33 +01:00
|
|
|
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
|
|
|
psa_hash_abort( &handshake->fin_sha256_psa );
|
|
|
|
#else
|
2015-07-06 14:18:56 +02:00
|
|
|
mbedtls_sha256_free( &handshake->fin_sha256 );
|
|
|
|
#endif
|
2019-01-29 15:14:33 +01:00
|
|
|
#endif
|
2015-07-06 14:18:56 +02:00
|
|
|
#if defined(MBEDTLS_SHA512_C)
|
2019-01-29 15:14:33 +01:00
|
|
|
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
2019-01-30 09:29:12 +01:00
|
|
|
psa_hash_abort( &handshake->fin_sha384_psa );
|
2019-01-29 15:14:33 +01:00
|
|
|
#else
|
2015-07-06 14:18:56 +02:00
|
|
|
mbedtls_sha512_free( &handshake->fin_sha512 );
|
|
|
|
#endif
|
2019-01-29 15:14:33 +01:00
|
|
|
#endif
|
2015-07-06 14:18:56 +02:00
|
|
|
#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_DHM_C)
|
|
|
|
mbedtls_dhm_free( &handshake->dhm_ctx );
|
2012-09-16 21:57:18 +02:00
|
|
|
#endif
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_ECDH_C)
|
|
|
|
mbedtls_ecdh_free( &handshake->ecdh_ctx );
|
2013-07-04 11:51:43 +02:00
|
|
|
#endif
|
2015-09-16 10:05:04 +02:00
|
|
|
#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
|
2015-09-15 12:10:54 +02:00
|
|
|
mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
|
2015-09-17 13:59:49 +02:00
|
|
|
#if defined(MBEDTLS_SSL_CLI_C)
|
|
|
|
mbedtls_free( handshake->ecjpake_cache );
|
|
|
|
handshake->ecjpake_cache = NULL;
|
|
|
|
handshake->ecjpake_cache_len = 0;
|
|
|
|
#endif
|
2015-09-15 12:10:54 +02:00
|
|
|
#endif
|
2013-07-04 11:51:43 +02:00
|
|
|
|
2016-02-10 12:27:43 +01:00
|
|
|
#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
|
|
|
|
defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
|
2014-05-01 13:03:14 +02:00
|
|
|
/* explicit void pointer cast for buggy MS compiler */
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_free( (void *) handshake->curves );
|
2013-09-23 19:11:32 +02:00
|
|
|
#endif
|
|
|
|
|
2020-03-10 12:19:08 +01:00
|
|
|
#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
|
2015-05-07 16:59:54 +02:00
|
|
|
if( handshake->psk != NULL )
|
|
|
|
{
|
2018-04-17 16:51:09 +02:00
|
|
|
mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
|
2015-05-07 16:59:54 +02:00
|
|
|
mbedtls_free( handshake->psk );
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
|
|
|
|
defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
|
2013-09-24 22:30:56 +02:00
|
|
|
/*
|
|
|
|
* Free only the linked list wrapper, not the keys themselves
|
|
|
|
* since the belong to the SNI callback
|
|
|
|
*/
|
|
|
|
if( handshake->sni_key_cert != NULL )
|
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_ssl_key_cert *cur = handshake->sni_key_cert, *next;
|
2013-09-24 22:30:56 +02:00
|
|
|
|
|
|
|
while( cur != NULL )
|
|
|
|
{
|
|
|
|
next = cur->next;
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_free( cur );
|
2013-09-24 22:30:56 +02:00
|
|
|
cur = next;
|
|
|
|
}
|
|
|
|
}
|
2015-04-08 12:49:31 +02:00
|
|
|
#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
|
2013-09-23 20:04:20 +02:00
|
|
|
|
2020-03-10 12:19:08 +01:00
|
|
|
#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
|
2017-08-15 12:08:45 +02:00
|
|
|
mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx );
|
2019-02-05 18:19:52 +01:00
|
|
|
if( handshake->ecrs_peer_cert != NULL )
|
|
|
|
{
|
|
|
|
mbedtls_x509_crt_free( handshake->ecrs_peer_cert );
|
|
|
|
mbedtls_free( handshake->ecrs_peer_cert );
|
|
|
|
}
|
2017-05-17 11:56:15 +02:00
|
|
|
#endif
|
|
|
|
|
2019-02-06 17:18:31 +01:00
|
|
|
#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
|
|
|
|
!defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
|
|
|
|
mbedtls_pk_free( &handshake->peer_pubkey );
|
|
|
|
#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_SSL_PROTO_DTLS)
|
|
|
|
mbedtls_free( handshake->verify_cookie );
|
2020-02-05 11:49:13 +01:00
|
|
|
mbedtls_ssl_flight_free( handshake->flight );
|
|
|
|
mbedtls_ssl_buffering_free( ssl );
|
2014-07-11 02:43:49 +02:00
|
|
|
#endif
|
|
|
|
|
2019-01-08 12:39:35 +01:00
|
|
|
#if defined(MBEDTLS_ECDH_C) && \
|
|
|
|
defined(MBEDTLS_USE_PSA_CRYPTO)
|
|
|
|
psa_destroy_key( handshake->ecdh_psa_privkey );
|
|
|
|
#endif /* MBEDTLS_ECDH_C && MBEDTLS_USE_PSA_CRYPTO */
|
|
|
|
|
2018-04-17 16:51:09 +02:00
|
|
|
mbedtls_platform_zeroize( handshake,
|
|
|
|
sizeof( mbedtls_ssl_handshake_params ) );
|
2020-03-03 16:39:58 +01:00
|
|
|
|
|
|
|
#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
|
|
|
|
/* If the buffers are too big - reallocate. Because of the way Mbed TLS
|
|
|
|
* processes datagrams and the fact that a datagram is allowed to have
|
|
|
|
* several records in it, it is possible that the I/O buffers are not
|
|
|
|
* empty at this stage */
|
|
|
|
{
|
|
|
|
int modified = 0;
|
|
|
|
uint32_t buf_len = mbedtls_ssl_get_input_buflen( ssl );
|
2020-06-08 13:08:03 +02:00
|
|
|
size_t written_in = 0, iv_offset_in = 0, len_offset_in = 0;
|
|
|
|
size_t written_out = 0, iv_offset_out = 0, len_offset_out = 0;
|
2020-04-03 12:40:47 +02:00
|
|
|
if( ssl->in_buf != NULL )
|
2020-03-03 16:39:58 +01:00
|
|
|
{
|
|
|
|
written_in = ssl->in_msg - ssl->in_buf;
|
2020-06-08 13:08:03 +02:00
|
|
|
iv_offset_in = ssl->in_iv - ssl->in_buf;
|
|
|
|
len_offset_in = ssl->in_len - ssl->in_buf;
|
2020-04-03 12:40:47 +02:00
|
|
|
if( ssl->in_buf_len > buf_len && ssl->in_left < buf_len )
|
2020-03-03 16:39:58 +01:00
|
|
|
{
|
2020-04-03 12:40:47 +02:00
|
|
|
if( resize_buffer( &ssl->in_buf, buf_len, &ssl->in_buf_len ) != 0 )
|
|
|
|
{
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 1, ( "input buffer resizing failed - out of memory" ) );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 2, ( "Reallocating in_buf to %d", buf_len ) );
|
|
|
|
modified = 1;
|
|
|
|
}
|
2020-03-03 16:39:58 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-03 12:40:47 +02:00
|
|
|
|
2020-03-03 16:39:58 +01:00
|
|
|
buf_len = mbedtls_ssl_get_output_buflen( ssl );
|
2020-04-03 12:40:47 +02:00
|
|
|
if(ssl->out_buf != NULL )
|
2020-03-03 16:39:58 +01:00
|
|
|
{
|
|
|
|
written_out = ssl->out_msg - ssl->out_buf;
|
2020-06-08 13:08:03 +02:00
|
|
|
iv_offset_out = ssl->out_iv - ssl->out_buf;
|
|
|
|
len_offset_out = ssl->out_len - ssl->out_buf;
|
2020-04-03 12:40:47 +02:00
|
|
|
if( ssl->out_buf_len > mbedtls_ssl_get_output_buflen( ssl ) &&
|
|
|
|
ssl->out_left < buf_len )
|
2020-03-03 16:39:58 +01:00
|
|
|
{
|
2020-04-03 12:40:47 +02:00
|
|
|
if( resize_buffer( &ssl->out_buf, buf_len, &ssl->out_buf_len ) != 0 )
|
|
|
|
{
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 1, ( "output buffer resizing failed - out of memory" ) );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 2, ( "Reallocating out_buf to %d", buf_len ) );
|
|
|
|
modified = 1;
|
|
|
|
}
|
2020-03-03 16:39:58 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if( modified )
|
|
|
|
{
|
|
|
|
/* Update pointers here to avoid doing it twice. */
|
|
|
|
mbedtls_ssl_reset_in_out_pointers( ssl );
|
|
|
|
/* Fields below might not be properly updated with record
|
2020-06-08 13:08:03 +02:00
|
|
|
* splitting or with CID, so they are manually updated here. */
|
2020-03-03 16:39:58 +01:00
|
|
|
ssl->out_msg = ssl->out_buf + written_out;
|
2020-06-08 13:08:03 +02:00
|
|
|
ssl->out_len = ssl->out_buf + len_offset_out;
|
|
|
|
ssl->out_iv = ssl->out_buf + iv_offset_out;
|
|
|
|
|
2020-03-03 16:39:58 +01:00
|
|
|
ssl->in_msg = ssl->in_buf + written_in;
|
2020-06-08 13:08:03 +02:00
|
|
|
ssl->in_len = ssl->in_buf + len_offset_in;
|
|
|
|
ssl->in_iv = ssl->in_buf + iv_offset_in;
|
2020-03-03 16:39:58 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2012-09-16 21:57:18 +02:00
|
|
|
}
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
|
2012-09-16 21:57:18 +02:00
|
|
|
{
|
2014-06-26 13:37:14 +02:00
|
|
|
if( session == NULL )
|
|
|
|
return;
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_X509_CRT_PARSE_C)
|
2019-02-05 13:38:15 +01:00
|
|
|
ssl_clear_peer_cert( session );
|
2013-04-18 22:46:23 +02:00
|
|
|
#endif
|
2012-09-25 23:55:46 +02:00
|
|
|
|
2015-05-20 10:45:29 +02:00
|
|
|
#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_free( session->ticket );
|
2013-08-14 13:48:06 +02:00
|
|
|
#endif
|
2013-08-02 14:44:04 +02:00
|
|
|
|
2018-04-17 16:51:09 +02:00
|
|
|
mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
|
2012-09-16 21:57:18 +02:00
|
|
|
}
|
|
|
|
|
2019-07-23 16:13:17 +02:00
|
|
|
#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
|
2019-07-23 16:31:16 +02:00
|
|
|
|
|
|
|
#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
|
|
|
|
#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 1u
|
|
|
|
#else
|
|
|
|
#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 0u
|
|
|
|
#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
|
|
|
|
#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT 1u
|
|
|
|
#else
|
|
|
|
#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT 0u
|
|
|
|
#endif /* MBEDTLS_SSL_DTLS_BADMAC_LIMIT */
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
|
|
|
|
#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 1u
|
|
|
|
#else
|
|
|
|
#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 0u
|
|
|
|
#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_SSL_ALPN)
|
|
|
|
#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 1u
|
|
|
|
#else
|
|
|
|
#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 0u
|
|
|
|
#endif /* MBEDTLS_SSL_ALPN */
|
|
|
|
|
|
|
|
#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT 0
|
|
|
|
#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT 1
|
|
|
|
#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT 2
|
|
|
|
#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT 3
|
|
|
|
|
|
|
|
#define SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG \
|
|
|
|
( (uint32_t) ( \
|
|
|
|
( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT ) | \
|
|
|
|
( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT ) | \
|
|
|
|
( SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY << SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT ) | \
|
|
|
|
( SSL_SERIALIZED_CONTEXT_CONFIG_ALPN << SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT ) | \
|
|
|
|
0u ) )
|
|
|
|
|
2019-07-11 10:58:10 +02:00
|
|
|
static unsigned char ssl_serialized_context_header[] = {
|
|
|
|
MBEDTLS_VERSION_MAJOR,
|
|
|
|
MBEDTLS_VERSION_MINOR,
|
|
|
|
MBEDTLS_VERSION_PATCH,
|
|
|
|
( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG >> 8 ) & 0xFF,
|
|
|
|
( SSL_SERIALIZED_SESSION_CONFIG_BITFLAG >> 0 ) & 0xFF,
|
2019-07-23 16:31:16 +02:00
|
|
|
( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG >> 16 ) & 0xFF,
|
|
|
|
( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG >> 8 ) & 0xFF,
|
|
|
|
( SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG >> 0 ) & 0xFF,
|
2019-07-11 10:58:10 +02:00
|
|
|
};
|
|
|
|
|
2019-05-28 13:02:16 +02:00
|
|
|
/*
|
|
|
|
* Serialize a full SSL context
|
2019-07-10 14:58:45 +02:00
|
|
|
*
|
|
|
|
* The format of the serialized data is:
|
|
|
|
* (in the presentation language of TLS, RFC 8446 section 3)
|
|
|
|
*
|
|
|
|
* // header
|
|
|
|
* opaque mbedtls_version[3]; // major, minor, patch
|
2019-07-11 10:58:10 +02:00
|
|
|
* opaque context_format[5]; // version-specific field determining
|
2019-07-10 14:58:45 +02:00
|
|
|
* // the format of the remaining
|
2019-07-11 10:58:10 +02:00
|
|
|
* // serialized data.
|
2019-07-23 16:31:16 +02:00
|
|
|
* Note: When updating the format, remember to keep these
|
|
|
|
* version+format bytes. (We may make their size part of the API.)
|
2019-07-10 14:58:45 +02:00
|
|
|
*
|
|
|
|
* // session sub-structure
|
|
|
|
* opaque session<1..2^32-1>; // see mbedtls_ssl_session_save()
|
|
|
|
* // transform sub-structure
|
|
|
|
* uint8 random[64]; // ServerHello.random+ClientHello.random
|
|
|
|
* uint8 in_cid<0..2^8-1> // Connection ID: expected incoming value
|
|
|
|
* uint8 out_cid<0..2^8-1> // Connection ID: outgoing value to use
|
|
|
|
* // fields from ssl_context
|
|
|
|
* uint32 badmac_seen; // DTLS: number of records with failing MAC
|
|
|
|
* uint64 in_window_top; // DTLS: last validated record seq_num
|
|
|
|
* uint64 in_window; // DTLS: bitmask for replay protection
|
|
|
|
* uint8 disable_datagram_packing; // DTLS: only one record per datagram
|
|
|
|
* uint64 cur_out_ctr; // Record layer: outgoing sequence number
|
|
|
|
* uint16 mtu; // DTLS: path mtu (max outgoing fragment size)
|
|
|
|
* uint8 alpn_chosen<0..2^8-1> // ALPN: negotiated application protocol
|
|
|
|
*
|
|
|
|
* Note that many fields of the ssl_context or sub-structures are not
|
|
|
|
* serialized, as they fall in one of the following categories:
|
|
|
|
*
|
|
|
|
* 1. forced value (eg in_left must be 0)
|
|
|
|
* 2. pointer to dynamically-allocated memory (eg session, transform)
|
|
|
|
* 3. value can be re-derived from other data (eg session keys from MS)
|
|
|
|
* 4. value was temporary (eg content of input buffer)
|
|
|
|
* 5. value will be provided by the user again (eg I/O callbacks and context)
|
2019-05-28 13:02:16 +02:00
|
|
|
*/
|
|
|
|
int mbedtls_ssl_context_save( mbedtls_ssl_context *ssl,
|
|
|
|
unsigned char *buf,
|
|
|
|
size_t buf_len,
|
|
|
|
size_t *olen )
|
|
|
|
{
|
2019-07-11 10:58:10 +02:00
|
|
|
unsigned char *p = buf;
|
|
|
|
size_t used = 0;
|
2019-07-11 12:50:53 +02:00
|
|
|
size_t session_len;
|
|
|
|
int ret = 0;
|
2019-07-11 10:58:10 +02:00
|
|
|
|
2019-07-10 14:14:05 +02:00
|
|
|
/*
|
2019-07-29 12:28:52 +02:00
|
|
|
* Enforce usage restrictions, see "return BAD_INPUT_DATA" in
|
|
|
|
* this function's documentation.
|
|
|
|
*
|
|
|
|
* These are due to assumptions/limitations in the implementation. Some of
|
|
|
|
* them are likely to stay (no handshake in progress) some might go away
|
|
|
|
* (only DTLS) but are currently used to simplify the implementation.
|
2019-07-10 14:14:05 +02:00
|
|
|
*/
|
2019-07-29 12:28:52 +02:00
|
|
|
/* The initial handshake must be over */
|
|
|
|
if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
|
2019-08-21 12:45:05 +02:00
|
|
|
{
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 1, ( "Initial handshake isn't over" ) );
|
2019-07-29 12:28:52 +02:00
|
|
|
return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
|
2019-08-21 12:45:05 +02:00
|
|
|
}
|
2019-07-29 12:28:52 +02:00
|
|
|
if( ssl->handshake != NULL )
|
2019-08-21 12:45:05 +02:00
|
|
|
{
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 1, ( "Handshake isn't completed" ) );
|
2019-07-29 12:28:52 +02:00
|
|
|
return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
|
2019-08-21 12:45:05 +02:00
|
|
|
}
|
2019-07-29 12:28:52 +02:00
|
|
|
/* Double-check that sub-structures are indeed ready */
|
|
|
|
if( ssl->transform == NULL || ssl->session == NULL )
|
2019-08-21 12:45:05 +02:00
|
|
|
{
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 1, ( "Serialised structures aren't ready" ) );
|
2019-07-29 12:28:52 +02:00
|
|
|
return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
|
2019-08-21 12:45:05 +02:00
|
|
|
}
|
2019-07-29 12:28:52 +02:00
|
|
|
/* There must be no pending incoming or outgoing data */
|
|
|
|
if( mbedtls_ssl_check_pending( ssl ) != 0 )
|
2019-08-21 12:45:05 +02:00
|
|
|
{
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 1, ( "There is pending incoming data" ) );
|
2019-07-29 12:28:52 +02:00
|
|
|
return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
|
2019-08-21 12:45:05 +02:00
|
|
|
}
|
2019-07-29 12:28:52 +02:00
|
|
|
if( ssl->out_left != 0 )
|
2019-08-21 12:45:05 +02:00
|
|
|
{
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 1, ( "There is pending outgoing data" ) );
|
2019-07-29 12:28:52 +02:00
|
|
|
return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
|
2019-08-21 12:45:05 +02:00
|
|
|
}
|
2019-07-29 12:28:52 +02:00
|
|
|
/* Protocol must be DLTS, not TLS */
|
|
|
|
if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
|
2019-08-21 12:45:05 +02:00
|
|
|
{
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only DTLS is supported" ) );
|
2019-07-29 12:28:52 +02:00
|
|
|
return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
|
2019-08-21 12:45:05 +02:00
|
|
|
}
|
2019-07-29 12:28:52 +02:00
|
|
|
/* Version must be 1.2 */
|
|
|
|
if( ssl->major_ver != MBEDTLS_SSL_MAJOR_VERSION_3 )
|
2019-08-21 12:45:05 +02:00
|
|
|
{
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only version 1.2 supported" ) );
|
2019-07-29 12:28:52 +02:00
|
|
|
return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
|
2019-08-21 12:45:05 +02:00
|
|
|
}
|
2019-07-29 12:28:52 +02:00
|
|
|
if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 )
|
2019-08-21 12:45:05 +02:00
|
|
|
{
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only version 1.2 supported" ) );
|
2019-07-29 12:28:52 +02:00
|
|
|
return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
|
2019-08-21 12:45:05 +02:00
|
|
|
}
|
2019-07-29 12:28:52 +02:00
|
|
|
/* We must be using an AEAD ciphersuite */
|
|
|
|
if( mbedtls_ssl_transform_uses_aead( ssl->transform ) != 1 )
|
2019-08-21 12:45:05 +02:00
|
|
|
{
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 1, ( "Only AEAD ciphersuites supported" ) );
|
2019-07-29 12:28:52 +02:00
|
|
|
return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
|
2019-08-21 12:45:05 +02:00
|
|
|
}
|
2019-07-29 12:28:52 +02:00
|
|
|
/* Renegotiation must not be enabled */
|
2019-07-10 14:14:05 +02:00
|
|
|
#if defined(MBEDTLS_SSL_RENEGOTIATION)
|
2019-07-29 12:28:52 +02:00
|
|
|
if( ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED )
|
2019-08-21 12:45:05 +02:00
|
|
|
{
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 1, ( "Renegotiation must not be enabled" ) );
|
2019-07-10 14:14:05 +02:00
|
|
|
return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
|
2019-08-21 12:45:05 +02:00
|
|
|
}
|
2019-07-29 12:28:52 +02:00
|
|
|
#endif
|
2019-05-28 13:02:16 +02:00
|
|
|
|
2019-07-11 10:58:10 +02:00
|
|
|
/*
|
|
|
|
* Version and format identifier
|
|
|
|
*/
|
|
|
|
used += sizeof( ssl_serialized_context_header );
|
|
|
|
|
|
|
|
if( used <= buf_len )
|
|
|
|
{
|
|
|
|
memcpy( p, ssl_serialized_context_header,
|
|
|
|
sizeof( ssl_serialized_context_header ) );
|
|
|
|
p += sizeof( ssl_serialized_context_header );
|
|
|
|
}
|
2019-05-28 13:02:16 +02:00
|
|
|
|
2019-07-11 12:50:53 +02:00
|
|
|
/*
|
|
|
|
* Session (length + data)
|
|
|
|
*/
|
2019-07-23 16:52:45 +02:00
|
|
|
ret = ssl_session_save( ssl->session, 1, NULL, 0, &session_len );
|
2019-07-11 12:50:53 +02:00
|
|
|
if( ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL )
|
|
|
|
return( ret );
|
|
|
|
|
|
|
|
used += 4 + session_len;
|
|
|
|
if( used <= buf_len )
|
|
|
|
{
|
|
|
|
*p++ = (unsigned char)( ( session_len >> 24 ) & 0xFF );
|
|
|
|
*p++ = (unsigned char)( ( session_len >> 16 ) & 0xFF );
|
|
|
|
*p++ = (unsigned char)( ( session_len >> 8 ) & 0xFF );
|
|
|
|
*p++ = (unsigned char)( ( session_len ) & 0xFF );
|
|
|
|
|
2019-07-23 16:52:45 +02:00
|
|
|
ret = ssl_session_save( ssl->session, 1,
|
|
|
|
p, session_len, &session_len );
|
2019-07-11 12:50:53 +02:00
|
|
|
if( ret != 0 )
|
|
|
|
return( ret );
|
|
|
|
|
|
|
|
p += session_len;
|
|
|
|
}
|
|
|
|
|
2019-07-15 09:04:11 +02:00
|
|
|
/*
|
|
|
|
* Transform
|
|
|
|
*/
|
|
|
|
used += sizeof( ssl->transform->randbytes );
|
|
|
|
if( used <= buf_len )
|
|
|
|
{
|
|
|
|
memcpy( p, ssl->transform->randbytes,
|
|
|
|
sizeof( ssl->transform->randbytes ) );
|
|
|
|
p += sizeof( ssl->transform->randbytes );
|
|
|
|
}
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
|
|
|
|
used += 2 + ssl->transform->in_cid_len + ssl->transform->out_cid_len;
|
|
|
|
if( used <= buf_len )
|
|
|
|
{
|
|
|
|
*p++ = ssl->transform->in_cid_len;
|
|
|
|
memcpy( p, ssl->transform->in_cid, ssl->transform->in_cid_len );
|
|
|
|
p += ssl->transform->in_cid_len;
|
|
|
|
|
|
|
|
*p++ = ssl->transform->out_cid_len;
|
|
|
|
memcpy( p, ssl->transform->out_cid, ssl->transform->out_cid_len );
|
|
|
|
p += ssl->transform->out_cid_len;
|
|
|
|
}
|
|
|
|
#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
|
|
|
|
|
2019-07-15 11:23:03 +02:00
|
|
|
/*
|
|
|
|
* Saved fields from top-level ssl_context structure
|
|
|
|
*/
|
|
|
|
#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
|
|
|
|
used += 4;
|
|
|
|
if( used <= buf_len )
|
|
|
|
{
|
|
|
|
*p++ = (unsigned char)( ( ssl->badmac_seen >> 24 ) & 0xFF );
|
|
|
|
*p++ = (unsigned char)( ( ssl->badmac_seen >> 16 ) & 0xFF );
|
|
|
|
*p++ = (unsigned char)( ( ssl->badmac_seen >> 8 ) & 0xFF );
|
|
|
|
*p++ = (unsigned char)( ( ssl->badmac_seen ) & 0xFF );
|
|
|
|
}
|
|
|
|
#endif /* MBEDTLS_SSL_DTLS_BADMAC_LIMIT */
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
|
|
|
|
used += 16;
|
|
|
|
if( used <= buf_len )
|
|
|
|
{
|
|
|
|
*p++ = (unsigned char)( ( ssl->in_window_top >> 56 ) & 0xFF );
|
|
|
|
*p++ = (unsigned char)( ( ssl->in_window_top >> 48 ) & 0xFF );
|
|
|
|
*p++ = (unsigned char)( ( ssl->in_window_top >> 40 ) & 0xFF );
|
|
|
|
*p++ = (unsigned char)( ( ssl->in_window_top >> 32 ) & 0xFF );
|
|
|
|
*p++ = (unsigned char)( ( ssl->in_window_top >> 24 ) & 0xFF );
|
|
|
|
*p++ = (unsigned char)( ( ssl->in_window_top >> 16 ) & 0xFF );
|
|
|
|
*p++ = (unsigned char)( ( ssl->in_window_top >> 8 ) & 0xFF );
|
|
|
|
*p++ = (unsigned char)( ( ssl->in_window_top ) & 0xFF );
|
|
|
|
|
|
|
|
*p++ = (unsigned char)( ( ssl->in_window >> 56 ) & 0xFF );
|
|
|
|
*p++ = (unsigned char)( ( ssl->in_window >> 48 ) & 0xFF );
|
|
|
|
*p++ = (unsigned char)( ( ssl->in_window >> 40 ) & 0xFF );
|
|
|
|
*p++ = (unsigned char)( ( ssl->in_window >> 32 ) & 0xFF );
|
|
|
|
*p++ = (unsigned char)( ( ssl->in_window >> 24 ) & 0xFF );
|
|
|
|
*p++ = (unsigned char)( ( ssl->in_window >> 16 ) & 0xFF );
|
|
|
|
*p++ = (unsigned char)( ( ssl->in_window >> 8 ) & 0xFF );
|
|
|
|
*p++ = (unsigned char)( ( ssl->in_window ) & 0xFF );
|
|
|
|
}
|
|
|
|
#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_SSL_PROTO_DTLS)
|
|
|
|
used += 1;
|
|
|
|
if( used <= buf_len )
|
|
|
|
{
|
|
|
|
*p++ = ssl->disable_datagram_packing;
|
|
|
|
}
|
|
|
|
#endif /* MBEDTLS_SSL_PROTO_DTLS */
|
|
|
|
|
|
|
|
used += 8;
|
|
|
|
if( used <= buf_len )
|
|
|
|
{
|
|
|
|
memcpy( p, ssl->cur_out_ctr, 8 );
|
|
|
|
p += 8;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_SSL_PROTO_DTLS)
|
|
|
|
used += 2;
|
|
|
|
if( used <= buf_len )
|
|
|
|
{
|
|
|
|
*p++ = (unsigned char)( ( ssl->mtu >> 8 ) & 0xFF );
|
|
|
|
*p++ = (unsigned char)( ( ssl->mtu ) & 0xFF );
|
|
|
|
}
|
|
|
|
#endif /* MBEDTLS_SSL_PROTO_DTLS */
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_SSL_ALPN)
|
|
|
|
{
|
|
|
|
const uint8_t alpn_len = ssl->alpn_chosen
|
2019-07-24 00:58:27 +02:00
|
|
|
? (uint8_t) strlen( ssl->alpn_chosen )
|
2019-07-15 11:23:03 +02:00
|
|
|
: 0;
|
|
|
|
|
|
|
|
used += 1 + alpn_len;
|
|
|
|
if( used <= buf_len )
|
|
|
|
{
|
|
|
|
*p++ = alpn_len;
|
|
|
|
|
|
|
|
if( ssl->alpn_chosen != NULL )
|
|
|
|
{
|
|
|
|
memcpy( p, ssl->alpn_chosen, alpn_len );
|
|
|
|
p += alpn_len;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif /* MBEDTLS_SSL_ALPN */
|
|
|
|
|
2019-07-11 10:58:10 +02:00
|
|
|
/*
|
|
|
|
* Done
|
|
|
|
*/
|
|
|
|
*olen = used;
|
|
|
|
|
|
|
|
if( used > buf_len )
|
|
|
|
return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
|
2019-05-28 13:02:16 +02:00
|
|
|
|
2019-07-11 12:50:53 +02:00
|
|
|
MBEDTLS_SSL_DEBUG_BUF( 4, "saved context", buf, used );
|
|
|
|
|
2020-02-05 11:44:56 +01:00
|
|
|
return( mbedtls_ssl_session_reset_int( ssl, 0 ) );
|
2019-05-28 13:02:16 +02:00
|
|
|
}
|
|
|
|
|
2019-07-15 09:04:11 +02:00
|
|
|
/*
|
|
|
|
* Helper to get TLS 1.2 PRF from ciphersuite
|
|
|
|
* (Duplicates bits of logic from ssl_set_handshake_prfs().)
|
|
|
|
*/
|
|
|
|
typedef int (*tls_prf_fn)( const unsigned char *secret, size_t slen,
|
|
|
|
const char *label,
|
|
|
|
const unsigned char *random, size_t rlen,
|
|
|
|
unsigned char *dstbuf, size_t dlen );
|
|
|
|
static tls_prf_fn ssl_tls12prf_from_cs( int ciphersuite_id )
|
|
|
|
{
|
2019-08-21 14:30:44 +02:00
|
|
|
#if defined(MBEDTLS_SHA512_C)
|
2019-07-15 09:04:11 +02:00
|
|
|
const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
|
|
|
|
mbedtls_ssl_ciphersuite_from_id( ciphersuite_id );
|
|
|
|
|
2019-07-23 17:11:24 +02:00
|
|
|
if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
|
|
|
|
return( tls_prf_sha384 );
|
2019-08-21 14:30:44 +02:00
|
|
|
#else
|
|
|
|
(void) ciphersuite_id;
|
2019-07-23 17:11:24 +02:00
|
|
|
#endif
|
|
|
|
return( tls_prf_sha256 );
|
2019-07-15 09:04:11 +02:00
|
|
|
}
|
|
|
|
|
2019-05-28 13:02:16 +02:00
|
|
|
/*
|
2019-07-12 10:50:19 +02:00
|
|
|
* Deserialize context, see mbedtls_ssl_context_save() for format.
|
2019-07-11 12:50:53 +02:00
|
|
|
*
|
|
|
|
* This internal version is wrapped by a public function that cleans up in
|
|
|
|
* case of error.
|
2019-05-28 13:02:16 +02:00
|
|
|
*/
|
2019-07-11 12:50:53 +02:00
|
|
|
static int ssl_context_load( mbedtls_ssl_context *ssl,
|
|
|
|
const unsigned char *buf,
|
|
|
|
size_t len )
|
2019-05-28 13:02:16 +02:00
|
|
|
{
|
2019-07-11 10:58:10 +02:00
|
|
|
const unsigned char *p = buf;
|
|
|
|
const unsigned char * const end = buf + len;
|
2019-07-11 12:50:53 +02:00
|
|
|
size_t session_len;
|
2019-12-16 12:46:15 +01:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2019-07-11 10:58:10 +02:00
|
|
|
|
2019-07-11 09:56:30 +02:00
|
|
|
/*
|
|
|
|
* The context should have been freshly setup or reset.
|
|
|
|
* Give the user an error in case of obvious misuse.
|
2019-07-26 16:31:53 +02:00
|
|
|
* (Checking session is useful because it won't be NULL if we're
|
2019-07-11 09:56:30 +02:00
|
|
|
* renegotiating, or if the user mistakenly loaded a session first.)
|
|
|
|
*/
|
|
|
|
if( ssl->state != MBEDTLS_SSL_HELLO_REQUEST ||
|
|
|
|
ssl->session != NULL )
|
|
|
|
{
|
|
|
|
return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We can't check that the config matches the initial one, but we can at
|
|
|
|
* least check it matches the requirements for serializing.
|
|
|
|
*/
|
|
|
|
if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
|
|
|
|
ssl->conf->max_major_ver < MBEDTLS_SSL_MAJOR_VERSION_3 ||
|
|
|
|
ssl->conf->min_major_ver > MBEDTLS_SSL_MAJOR_VERSION_3 ||
|
|
|
|
ssl->conf->max_minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 ||
|
|
|
|
ssl->conf->min_minor_ver > MBEDTLS_SSL_MINOR_VERSION_3 ||
|
|
|
|
#if defined(MBEDTLS_SSL_RENEGOTIATION)
|
2019-07-23 17:11:24 +02:00
|
|
|
ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
|
2019-07-11 09:56:30 +02:00
|
|
|
#endif
|
2019-07-23 17:11:24 +02:00
|
|
|
0 )
|
2019-07-11 09:56:30 +02:00
|
|
|
{
|
|
|
|
return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
|
|
|
|
}
|
|
|
|
|
2019-07-11 12:50:53 +02:00
|
|
|
MBEDTLS_SSL_DEBUG_BUF( 4, "context to load", buf, len );
|
|
|
|
|
2019-07-11 10:58:10 +02:00
|
|
|
/*
|
|
|
|
* Check version identifier
|
|
|
|
*/
|
|
|
|
if( (size_t)( end - p ) < sizeof( ssl_serialized_context_header ) )
|
|
|
|
return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
|
|
|
|
|
|
|
|
if( memcmp( p, ssl_serialized_context_header,
|
|
|
|
sizeof( ssl_serialized_context_header ) ) != 0 )
|
|
|
|
{
|
|
|
|
return( MBEDTLS_ERR_SSL_VERSION_MISMATCH );
|
|
|
|
}
|
|
|
|
p += sizeof( ssl_serialized_context_header );
|
|
|
|
|
2019-07-11 12:50:53 +02:00
|
|
|
/*
|
|
|
|
* Session
|
|
|
|
*/
|
|
|
|
if( (size_t)( end - p ) < 4 )
|
|
|
|
return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
|
|
|
|
|
|
|
|
session_len = ( (size_t) p[0] << 24 ) |
|
|
|
|
( (size_t) p[1] << 16 ) |
|
|
|
|
( (size_t) p[2] << 8 ) |
|
|
|
|
( (size_t) p[3] );
|
|
|
|
p += 4;
|
|
|
|
|
2019-07-23 14:43:30 +02:00
|
|
|
/* This has been allocated by ssl_handshake_init(), called by
|
2020-02-05 11:44:56 +01:00
|
|
|
* by either mbedtls_ssl_session_reset_int() or mbedtls_ssl_setup(). */
|
2019-07-23 14:43:30 +02:00
|
|
|
ssl->session = ssl->session_negotiate;
|
2019-07-11 12:50:53 +02:00
|
|
|
ssl->session_in = ssl->session;
|
|
|
|
ssl->session_out = ssl->session;
|
2019-07-23 14:43:30 +02:00
|
|
|
ssl->session_negotiate = NULL;
|
2019-07-11 12:50:53 +02:00
|
|
|
|
|
|
|
if( (size_t)( end - p ) < session_len )
|
|
|
|
return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
|
|
|
|
|
2019-07-23 16:52:45 +02:00
|
|
|
ret = ssl_session_load( ssl->session, 1, p, session_len );
|
2019-07-11 12:50:53 +02:00
|
|
|
if( ret != 0 )
|
2019-07-23 16:52:45 +02:00
|
|
|
{
|
|
|
|
mbedtls_ssl_session_free( ssl->session );
|
2019-07-11 12:50:53 +02:00
|
|
|
return( ret );
|
2019-07-23 16:52:45 +02:00
|
|
|
}
|
2019-07-11 12:50:53 +02:00
|
|
|
|
|
|
|
p += session_len;
|
|
|
|
|
2019-07-15 09:04:11 +02:00
|
|
|
/*
|
|
|
|
* Transform
|
|
|
|
*/
|
|
|
|
|
2019-07-23 14:43:30 +02:00
|
|
|
/* This has been allocated by ssl_handshake_init(), called by
|
2020-02-05 11:44:56 +01:00
|
|
|
* by either mbedtls_ssl_session_reset_int() or mbedtls_ssl_setup(). */
|
2019-07-23 14:43:30 +02:00
|
|
|
ssl->transform = ssl->transform_negotiate;
|
2019-07-15 09:04:11 +02:00
|
|
|
ssl->transform_in = ssl->transform;
|
|
|
|
ssl->transform_out = ssl->transform;
|
2019-07-23 14:43:30 +02:00
|
|
|
ssl->transform_negotiate = NULL;
|
2019-07-15 09:04:11 +02:00
|
|
|
|
|
|
|
/* Read random bytes and populate structure */
|
|
|
|
if( (size_t)( end - p ) < sizeof( ssl->transform->randbytes ) )
|
|
|
|
return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
|
|
|
|
|
|
|
|
ret = ssl_populate_transform( ssl->transform,
|
|
|
|
ssl->session->ciphersuite,
|
|
|
|
ssl->session->master,
|
|
|
|
#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
|
|
|
|
#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
|
|
|
|
ssl->session->encrypt_then_mac,
|
|
|
|
#endif
|
|
|
|
#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
|
|
|
|
ssl->session->trunc_hmac,
|
|
|
|
#endif
|
|
|
|
#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
|
|
|
|
#if defined(MBEDTLS_ZLIB_SUPPORT)
|
|
|
|
ssl->session->compression,
|
|
|
|
#endif
|
|
|
|
ssl_tls12prf_from_cs( ssl->session->ciphersuite ),
|
|
|
|
p, /* currently pointing to randbytes */
|
|
|
|
MBEDTLS_SSL_MINOR_VERSION_3, /* (D)TLS 1.2 is forced */
|
|
|
|
ssl->conf->endpoint,
|
|
|
|
ssl );
|
|
|
|
if( ret != 0 )
|
|
|
|
return( ret );
|
|
|
|
|
|
|
|
p += sizeof( ssl->transform->randbytes );
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
|
|
|
|
/* Read connection IDs and store them */
|
|
|
|
if( (size_t)( end - p ) < 1 )
|
|
|
|
return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
|
|
|
|
|
|
|
|
ssl->transform->in_cid_len = *p++;
|
|
|
|
|
2019-07-23 15:02:54 +02:00
|
|
|
if( (size_t)( end - p ) < ssl->transform->in_cid_len + 1u )
|
2019-07-15 09:04:11 +02:00
|
|
|
return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
|
|
|
|
|
|
|
|
memcpy( ssl->transform->in_cid, p, ssl->transform->in_cid_len );
|
|
|
|
p += ssl->transform->in_cid_len;
|
|
|
|
|
|
|
|
ssl->transform->out_cid_len = *p++;
|
|
|
|
|
|
|
|
if( (size_t)( end - p ) < ssl->transform->out_cid_len )
|
|
|
|
return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
|
|
|
|
|
|
|
|
memcpy( ssl->transform->out_cid, p, ssl->transform->out_cid_len );
|
|
|
|
p += ssl->transform->out_cid_len;
|
|
|
|
#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
|
|
|
|
|
2019-07-15 11:23:03 +02:00
|
|
|
/*
|
|
|
|
* Saved fields from top-level ssl_context structure
|
|
|
|
*/
|
|
|
|
#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
|
|
|
|
if( (size_t)( end - p ) < 4 )
|
|
|
|
return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
|
|
|
|
|
|
|
|
ssl->badmac_seen = ( (uint32_t) p[0] << 24 ) |
|
|
|
|
( (uint32_t) p[1] << 16 ) |
|
|
|
|
( (uint32_t) p[2] << 8 ) |
|
|
|
|
( (uint32_t) p[3] );
|
|
|
|
p += 4;
|
|
|
|
#endif /* MBEDTLS_SSL_DTLS_BADMAC_LIMIT */
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
|
|
|
|
if( (size_t)( end - p ) < 16 )
|
|
|
|
return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
|
|
|
|
|
|
|
|
ssl->in_window_top = ( (uint64_t) p[0] << 56 ) |
|
|
|
|
( (uint64_t) p[1] << 48 ) |
|
|
|
|
( (uint64_t) p[2] << 40 ) |
|
|
|
|
( (uint64_t) p[3] << 32 ) |
|
|
|
|
( (uint64_t) p[4] << 24 ) |
|
|
|
|
( (uint64_t) p[5] << 16 ) |
|
|
|
|
( (uint64_t) p[6] << 8 ) |
|
|
|
|
( (uint64_t) p[7] );
|
|
|
|
p += 8;
|
|
|
|
|
|
|
|
ssl->in_window = ( (uint64_t) p[0] << 56 ) |
|
|
|
|
( (uint64_t) p[1] << 48 ) |
|
|
|
|
( (uint64_t) p[2] << 40 ) |
|
|
|
|
( (uint64_t) p[3] << 32 ) |
|
|
|
|
( (uint64_t) p[4] << 24 ) |
|
|
|
|
( (uint64_t) p[5] << 16 ) |
|
|
|
|
( (uint64_t) p[6] << 8 ) |
|
|
|
|
( (uint64_t) p[7] );
|
|
|
|
p += 8;
|
|
|
|
#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_SSL_PROTO_DTLS)
|
|
|
|
if( (size_t)( end - p ) < 1 )
|
|
|
|
return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
|
|
|
|
|
|
|
|
ssl->disable_datagram_packing = *p++;
|
|
|
|
#endif /* MBEDTLS_SSL_PROTO_DTLS */
|
|
|
|
|
|
|
|
if( (size_t)( end - p ) < 8 )
|
|
|
|
return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
|
|
|
|
|
|
|
|
memcpy( ssl->cur_out_ctr, p, 8 );
|
|
|
|
p += 8;
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_SSL_PROTO_DTLS)
|
|
|
|
if( (size_t)( end - p ) < 2 )
|
|
|
|
return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
|
|
|
|
|
|
|
|
ssl->mtu = ( p[0] << 8 ) | p[1];
|
|
|
|
p += 2;
|
|
|
|
#endif /* MBEDTLS_SSL_PROTO_DTLS */
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_SSL_ALPN)
|
|
|
|
{
|
|
|
|
uint8_t alpn_len;
|
|
|
|
const char **cur;
|
|
|
|
|
|
|
|
if( (size_t)( end - p ) < 1 )
|
|
|
|
return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
|
|
|
|
|
|
|
|
alpn_len = *p++;
|
|
|
|
|
|
|
|
if( alpn_len != 0 && ssl->conf->alpn_list != NULL )
|
|
|
|
{
|
|
|
|
/* alpn_chosen should point to an item in the configured list */
|
|
|
|
for( cur = ssl->conf->alpn_list; *cur != NULL; cur++ )
|
|
|
|
{
|
|
|
|
if( strlen( *cur ) == alpn_len &&
|
|
|
|
memcmp( p, cur, alpn_len ) == 0 )
|
|
|
|
{
|
|
|
|
ssl->alpn_chosen = *cur;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* can only happen on conf mismatch */
|
|
|
|
if( alpn_len != 0 && ssl->alpn_chosen == NULL )
|
|
|
|
return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
|
|
|
|
|
|
|
|
p += alpn_len;
|
|
|
|
}
|
|
|
|
#endif /* MBEDTLS_SSL_ALPN */
|
|
|
|
|
2019-07-15 11:53:51 +02:00
|
|
|
/*
|
|
|
|
* Forced fields from top-level ssl_context structure
|
|
|
|
*
|
|
|
|
* Most of them already set to the correct value by mbedtls_ssl_init() and
|
|
|
|
* mbedtls_ssl_reset(), so we only need to set the remaining ones.
|
|
|
|
*/
|
|
|
|
ssl->state = MBEDTLS_SSL_HANDSHAKE_OVER;
|
|
|
|
|
|
|
|
ssl->major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
|
|
|
|
ssl->minor_ver = MBEDTLS_SSL_MINOR_VERSION_3;
|
|
|
|
|
Fix SSL context deserialization
The SSL context maintains a set of 'out pointers' indicating the
address at which to write the header fields of the next outgoing
record. Some of these addresses have a static offset from the
beginning of the record header, while other offsets can vary
depending on the active record encryption mechanism: For example,
if an explicit IV is in use, there's an offset between the end
of the record header and the beginning of the encrypted data to
allow the explicit IV to be placed in between; also, if the DTLS
Connection ID (CID) feature is in use, the CID is part of the
record header, shifting all subsequent information (length, IV, data)
to the back.
When setting up an SSL context, the out pointers are initialized
according to the identity transform + no CID, and it is important
to keep them up to date whenever the record encryption mechanism
changes, which is done by the helper function ssl_update_out_pointers().
During context deserialization, updating the out pointers according
to the deserialized record transform went missing, leaving the out
pointers the initial state. When attemping to encrypt a record in
this state, this lead to failure if either a CID or an explicit IV
was in use. This wasn't caught in the tests by the bad luck that
they didn't use CID, _and_ used the default ciphersuite based on
ChaChaPoly, which doesn't have an explicit IV. Changing either of
this would have made the existing tests fail.
This commit fixes the bug by adding a call to ssl_update_out_pointers()
to ssl_context_load() implementing context deserialization.
Extending test coverage is left for a separate commit.
2019-08-30 11:42:49 +02:00
|
|
|
/* Adjust pointers for header fields of outgoing records to
|
|
|
|
* the given transform, accounting for explicit IV and CID. */
|
2020-02-05 11:40:57 +01:00
|
|
|
mbedtls_ssl_update_out_pointers( ssl, ssl->transform );
|
Fix SSL context deserialization
The SSL context maintains a set of 'out pointers' indicating the
address at which to write the header fields of the next outgoing
record. Some of these addresses have a static offset from the
beginning of the record header, while other offsets can vary
depending on the active record encryption mechanism: For example,
if an explicit IV is in use, there's an offset between the end
of the record header and the beginning of the encrypted data to
allow the explicit IV to be placed in between; also, if the DTLS
Connection ID (CID) feature is in use, the CID is part of the
record header, shifting all subsequent information (length, IV, data)
to the back.
When setting up an SSL context, the out pointers are initialized
according to the identity transform + no CID, and it is important
to keep them up to date whenever the record encryption mechanism
changes, which is done by the helper function ssl_update_out_pointers().
During context deserialization, updating the out pointers according
to the deserialized record transform went missing, leaving the out
pointers the initial state. When attemping to encrypt a record in
this state, this lead to failure if either a CID or an explicit IV
was in use. This wasn't caught in the tests by the bad luck that
they didn't use CID, _and_ used the default ciphersuite based on
ChaChaPoly, which doesn't have an explicit IV. Changing either of
this would have made the existing tests fail.
This commit fixes the bug by adding a call to ssl_update_out_pointers()
to ssl_context_load() implementing context deserialization.
Extending test coverage is left for a separate commit.
2019-08-30 11:42:49 +02:00
|
|
|
|
2019-07-15 11:53:51 +02:00
|
|
|
#if defined(MBEDTLS_SSL_PROTO_DTLS)
|
|
|
|
ssl->in_epoch = 1;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* mbedtls_ssl_reset() leaves the handshake sub-structure allocated,
|
|
|
|
* which we don't want - otherwise we'd end up freeing the wrong transform
|
2020-02-05 11:47:44 +01:00
|
|
|
* by calling mbedtls_ssl_handshake_wrapup_free_hs_transform()
|
|
|
|
* inappropriately. */
|
2019-07-15 11:53:51 +02:00
|
|
|
if( ssl->handshake != NULL )
|
|
|
|
{
|
|
|
|
mbedtls_ssl_handshake_free( ssl );
|
|
|
|
mbedtls_free( ssl->handshake );
|
|
|
|
ssl->handshake = NULL;
|
|
|
|
}
|
|
|
|
|
2019-07-11 10:58:10 +02:00
|
|
|
/*
|
|
|
|
* Done - should have consumed entire buffer
|
|
|
|
*/
|
|
|
|
if( p != end )
|
|
|
|
return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
|
2019-05-28 13:02:16 +02:00
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
2019-07-11 12:50:53 +02:00
|
|
|
/*
|
2019-07-12 10:50:19 +02:00
|
|
|
* Deserialize context: public wrapper for error cleaning
|
2019-07-11 12:50:53 +02:00
|
|
|
*/
|
|
|
|
int mbedtls_ssl_context_load( mbedtls_ssl_context *context,
|
|
|
|
const unsigned char *buf,
|
|
|
|
size_t len )
|
|
|
|
{
|
|
|
|
int ret = ssl_context_load( context, buf, len );
|
|
|
|
|
|
|
|
if( ret != 0 )
|
|
|
|
mbedtls_ssl_free( context );
|
|
|
|
|
|
|
|
return( ret );
|
|
|
|
}
|
2019-07-23 16:13:17 +02:00
|
|
|
#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
|
2019-07-11 12:50:53 +02:00
|
|
|
|
2009-01-03 22:22:43 +01:00
|
|
|
/*
|
|
|
|
* Free an SSL context
|
|
|
|
*/
|
2015-04-08 12:49:31 +02:00
|
|
|
void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
|
2009-01-03 22:22:43 +01:00
|
|
|
{
|
2014-06-26 13:37:14 +02:00
|
|
|
if( ssl == NULL )
|
|
|
|
return;
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2014-02-13 10:54:07 +01:00
|
|
|
if( ssl->out_buf != NULL )
|
2009-01-03 22:22:43 +01:00
|
|
|
{
|
2020-05-06 22:05:13 +02:00
|
|
|
#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
|
|
|
|
size_t out_buf_len = ssl->out_buf_len;
|
|
|
|
#else
|
|
|
|
size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
|
|
|
|
#endif
|
|
|
|
|
2019-11-28 15:29:44 +01:00
|
|
|
mbedtls_platform_zeroize( ssl->out_buf, out_buf_len );
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_free( ssl->out_buf );
|
2020-03-03 16:39:58 +01:00
|
|
|
ssl->out_buf = NULL;
|
2009-01-03 22:22:43 +01:00
|
|
|
}
|
|
|
|
|
2014-02-13 10:54:07 +01:00
|
|
|
if( ssl->in_buf != NULL )
|
2009-01-03 22:22:43 +01:00
|
|
|
{
|
2020-05-06 22:05:13 +02:00
|
|
|
#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
|
|
|
|
size_t in_buf_len = ssl->in_buf_len;
|
|
|
|
#else
|
|
|
|
size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
|
|
|
|
#endif
|
|
|
|
|
2019-11-28 15:29:44 +01:00
|
|
|
mbedtls_platform_zeroize( ssl->in_buf, in_buf_len );
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_free( ssl->in_buf );
|
2020-03-03 16:39:58 +01:00
|
|
|
ssl->in_buf = NULL;
|
2009-01-03 22:22:43 +01:00
|
|
|
}
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_ZLIB_SUPPORT)
|
2013-10-11 09:59:44 +02:00
|
|
|
if( ssl->compress_buf != NULL )
|
|
|
|
{
|
2016-05-25 12:56:48 +02:00
|
|
|
mbedtls_platform_zeroize( ssl->compress_buf, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_free( ssl->compress_buf );
|
2013-10-11 09:59:44 +02:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2012-09-16 21:57:18 +02:00
|
|
|
if( ssl->transform )
|
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_ssl_transform_free( ssl->transform );
|
|
|
|
mbedtls_free( ssl->transform );
|
2012-09-16 21:57:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if( ssl->handshake )
|
|
|
|
{
|
2018-04-25 20:32:43 +02:00
|
|
|
mbedtls_ssl_handshake_free( ssl );
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_ssl_transform_free( ssl->transform_negotiate );
|
|
|
|
mbedtls_ssl_session_free( ssl->session_negotiate );
|
2012-09-16 21:57:18 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_free( ssl->handshake );
|
|
|
|
mbedtls_free( ssl->transform_negotiate );
|
|
|
|
mbedtls_free( ssl->session_negotiate );
|
2012-09-16 21:57:18 +02:00
|
|
|
}
|
|
|
|
|
2013-02-14 11:19:38 +01:00
|
|
|
if( ssl->session )
|
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_ssl_session_free( ssl->session );
|
|
|
|
mbedtls_free( ssl->session );
|
2013-02-14 11:19:38 +01:00
|
|
|
}
|
|
|
|
|
2015-05-11 16:15:19 +02:00
|
|
|
#if defined(MBEDTLS_X509_CRT_PARSE_C)
|
2014-06-17 16:39:18 +02:00
|
|
|
if( ssl->hostname != NULL )
|
2009-01-03 22:22:43 +01:00
|
|
|
{
|
2018-04-17 16:51:09 +02:00
|
|
|
mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_free( ssl->hostname );
|
2009-01-03 22:22:43 +01:00
|
|
|
}
|
2013-08-27 21:55:01 +02:00
|
|
|
#endif
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
|
|
|
|
if( mbedtls_ssl_hw_record_finish != NULL )
|
2012-05-08 11:17:57 +02:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_finish()" ) );
|
|
|
|
mbedtls_ssl_hw_record_finish( ssl );
|
2012-05-08 11:17:57 +02:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2015-05-20 10:59:43 +02:00
|
|
|
#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_free( ssl->cli_id );
|
2014-07-22 17:32:01 +02:00
|
|
|
#endif
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
|
2009-02-05 19:00:28 +01:00
|
|
|
|
2013-02-14 11:20:09 +01:00
|
|
|
/* Actually clear after last debug message */
|
2018-04-17 16:51:09 +02:00
|
|
|
mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
|
2009-01-03 22:22:43 +01:00
|
|
|
}
|
|
|
|
|
2015-05-04 13:35:39 +02:00
|
|
|
/*
|
|
|
|
* Initialze mbedtls_ssl_config
|
|
|
|
*/
|
|
|
|
void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
|
|
|
|
{
|
|
|
|
memset( conf, 0, sizeof( mbedtls_ssl_config ) );
|
|
|
|
}
|
|
|
|
|
2020-03-10 12:19:08 +01:00
|
|
|
#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
|
2015-12-04 15:02:56 +01:00
|
|
|
static int ssl_preset_default_hashes[] = {
|
|
|
|
#if defined(MBEDTLS_SHA512_C)
|
|
|
|
MBEDTLS_MD_SHA512,
|
|
|
|
MBEDTLS_MD_SHA384,
|
|
|
|
#endif
|
|
|
|
#if defined(MBEDTLS_SHA256_C)
|
|
|
|
MBEDTLS_MD_SHA256,
|
|
|
|
MBEDTLS_MD_SHA224,
|
|
|
|
#endif
|
2017-05-12 13:16:40 +02:00
|
|
|
#if defined(MBEDTLS_SHA1_C) && defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_KEY_EXCHANGE)
|
2015-12-04 15:02:56 +01:00
|
|
|
MBEDTLS_MD_SHA1,
|
|
|
|
#endif
|
|
|
|
MBEDTLS_MD_NONE
|
|
|
|
};
|
2015-12-28 00:48:17 +01:00
|
|
|
#endif
|
2015-12-04 15:02:56 +01:00
|
|
|
|
2015-06-17 13:53:47 +02:00
|
|
|
static int ssl_preset_suiteb_ciphersuites[] = {
|
|
|
|
MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
|
|
|
|
MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
|
|
|
|
0
|
|
|
|
};
|
|
|
|
|
2020-03-10 12:19:08 +01:00
|
|
|
#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
|
2015-06-17 13:53:47 +02:00
|
|
|
static int ssl_preset_suiteb_hashes[] = {
|
|
|
|
MBEDTLS_MD_SHA256,
|
|
|
|
MBEDTLS_MD_SHA384,
|
|
|
|
MBEDTLS_MD_NONE
|
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_ECP_C)
|
|
|
|
static mbedtls_ecp_group_id ssl_preset_suiteb_curves[] = {
|
2019-06-03 09:27:16 +02:00
|
|
|
#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
|
2015-06-17 13:53:47 +02:00
|
|
|
MBEDTLS_ECP_DP_SECP256R1,
|
2019-06-03 09:27:16 +02:00
|
|
|
#endif
|
|
|
|
#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
|
2015-06-17 13:53:47 +02:00
|
|
|
MBEDTLS_ECP_DP_SECP384R1,
|
2019-06-03 09:27:16 +02:00
|
|
|
#endif
|
2015-06-17 13:53:47 +02:00
|
|
|
MBEDTLS_ECP_DP_NONE
|
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
2015-05-04 13:35:39 +02:00
|
|
|
/*
|
2015-09-25 04:27:22 +02:00
|
|
|
* Load default in mbedtls_ssl_config
|
2015-05-04 13:35:39 +02:00
|
|
|
*/
|
2015-05-04 19:32:36 +02:00
|
|
|
int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
|
2015-06-17 13:53:47 +02:00
|
|
|
int endpoint, int transport, int preset )
|
2015-05-04 13:35:39 +02:00
|
|
|
{
|
2015-05-11 12:54:52 +02:00
|
|
|
#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
|
2019-12-16 12:46:15 +01:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2015-05-11 12:54:52 +02:00
|
|
|
#endif
|
2015-05-04 13:35:39 +02:00
|
|
|
|
2015-05-14 12:58:01 +02:00
|
|
|
/* Use the functions here so that they are covered in tests,
|
|
|
|
* but otherwise access member directly for efficiency */
|
|
|
|
mbedtls_ssl_conf_endpoint( conf, endpoint );
|
|
|
|
mbedtls_ssl_conf_transport( conf, transport );
|
2015-05-04 13:35:39 +02:00
|
|
|
|
2015-06-17 13:53:47 +02:00
|
|
|
/*
|
|
|
|
* Things that are common to all presets
|
|
|
|
*/
|
2015-05-04 19:32:36 +02:00
|
|
|
#if defined(MBEDTLS_SSL_CLI_C)
|
|
|
|
if( endpoint == MBEDTLS_SSL_IS_CLIENT )
|
|
|
|
{
|
|
|
|
conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
|
|
|
|
#if defined(MBEDTLS_SSL_SESSION_TICKETS)
|
|
|
|
conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2015-05-14 12:28:21 +02:00
|
|
|
#if defined(MBEDTLS_ARC4_C)
|
2015-05-04 13:35:39 +02:00
|
|
|
conf->arc4_disabled = MBEDTLS_SSL_ARC4_DISABLED;
|
2015-05-14 12:28:21 +02:00
|
|
|
#endif
|
2015-05-04 13:35:39 +02:00
|
|
|
|
|
|
|
#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
|
|
|
|
conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
|
|
|
|
conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
|
|
|
|
#endif
|
|
|
|
|
2015-05-05 17:34:53 +02:00
|
|
|
#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
|
|
|
|
conf->cbc_record_splitting = MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED;
|
|
|
|
#endif
|
|
|
|
|
2015-05-20 10:59:43 +02:00
|
|
|
#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
|
2015-05-04 13:35:39 +02:00
|
|
|
conf->f_cookie_write = ssl_cookie_write_dummy;
|
|
|
|
conf->f_cookie_check = ssl_cookie_check_dummy;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
|
|
|
|
conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
|
|
|
|
#endif
|
|
|
|
|
2017-04-10 13:42:31 +02:00
|
|
|
#if defined(MBEDTLS_SSL_SRV_C)
|
|
|
|
conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
|
|
|
|
#endif
|
|
|
|
|
2015-05-04 13:35:39 +02:00
|
|
|
#if defined(MBEDTLS_SSL_PROTO_DTLS)
|
|
|
|
conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
|
|
|
|
conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_SSL_RENEGOTIATION)
|
|
|
|
conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
|
2016-12-15 18:01:16 +01:00
|
|
|
memset( conf->renego_period, 0x00, 2 );
|
|
|
|
memset( conf->renego_period + 2, 0xFF, 6 );
|
2015-05-04 13:35:39 +02:00
|
|
|
#endif
|
|
|
|
|
2015-06-17 13:53:47 +02:00
|
|
|
#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
|
|
|
|
if( endpoint == MBEDTLS_SSL_IS_SERVER )
|
|
|
|
{
|
2017-10-04 14:14:29 +02:00
|
|
|
const unsigned char dhm_p[] =
|
|
|
|
MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
|
|
|
|
const unsigned char dhm_g[] =
|
|
|
|
MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
|
|
|
|
|
2017-10-04 16:29:08 +02:00
|
|
|
if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
|
|
|
|
dhm_p, sizeof( dhm_p ),
|
|
|
|
dhm_g, sizeof( dhm_g ) ) ) != 0 )
|
2015-06-17 13:53:47 +02:00
|
|
|
{
|
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
}
|
2015-06-11 14:49:42 +02:00
|
|
|
#endif
|
|
|
|
|
2015-06-17 13:53:47 +02:00
|
|
|
/*
|
|
|
|
* Preset-specific defaults
|
|
|
|
*/
|
|
|
|
switch( preset )
|
2015-05-04 13:35:39 +02:00
|
|
|
{
|
2015-06-17 13:53:47 +02:00
|
|
|
/*
|
|
|
|
* NSA Suite B
|
|
|
|
*/
|
|
|
|
case MBEDTLS_SSL_PRESET_SUITEB:
|
|
|
|
conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
|
|
|
|
conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */
|
|
|
|
conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
|
|
|
|
conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
|
|
|
|
|
|
|
|
conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
|
|
|
|
conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
|
|
|
|
conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
|
|
|
|
conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
|
|
|
|
ssl_preset_suiteb_ciphersuites;
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_X509_CRT_PARSE_C)
|
|
|
|
conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
|
|
|
|
#endif
|
|
|
|
|
2020-03-10 12:19:08 +01:00
|
|
|
#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
|
2015-06-17 13:53:47 +02:00
|
|
|
conf->sig_hashes = ssl_preset_suiteb_hashes;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_ECP_C)
|
|
|
|
conf->curve_list = ssl_preset_suiteb_curves;
|
|
|
|
#endif
|
2015-08-11 04:21:01 +02:00
|
|
|
break;
|
2015-06-17 13:53:47 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Default
|
|
|
|
*/
|
|
|
|
default:
|
2017-05-28 09:46:38 +02:00
|
|
|
conf->min_major_ver = ( MBEDTLS_SSL_MIN_MAJOR_VERSION >
|
|
|
|
MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION ) ?
|
|
|
|
MBEDTLS_SSL_MIN_MAJOR_VERSION :
|
|
|
|
MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION;
|
|
|
|
conf->min_minor_ver = ( MBEDTLS_SSL_MIN_MINOR_VERSION >
|
|
|
|
MBEDTLS_SSL_MIN_VALID_MINOR_VERSION ) ?
|
|
|
|
MBEDTLS_SSL_MIN_MINOR_VERSION :
|
|
|
|
MBEDTLS_SSL_MIN_VALID_MINOR_VERSION;
|
2015-06-17 13:53:47 +02:00
|
|
|
conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
|
|
|
|
conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_SSL_PROTO_DTLS)
|
|
|
|
if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
|
|
|
|
conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_2;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
|
|
|
|
conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
|
|
|
|
conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
|
|
|
|
conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
|
|
|
|
mbedtls_ssl_list_ciphersuites();
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_X509_CRT_PARSE_C)
|
|
|
|
conf->cert_profile = &mbedtls_x509_crt_profile_default;
|
|
|
|
#endif
|
|
|
|
|
2020-03-10 12:19:08 +01:00
|
|
|
#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
|
2015-12-04 15:02:56 +01:00
|
|
|
conf->sig_hashes = ssl_preset_default_hashes;
|
2015-06-17 13:53:47 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_ECP_C)
|
|
|
|
conf->curve_list = mbedtls_ecp_grp_id_list();
|
2015-05-04 13:35:39 +02:00
|
|
|
#endif
|
|
|
|
|
2015-06-17 13:53:47 +02:00
|
|
|
#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
|
|
|
|
conf->dhm_min_bitlen = 1024;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2015-05-04 13:35:39 +02:00
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Free mbedtls_ssl_config
|
|
|
|
*/
|
|
|
|
void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
|
|
|
|
{
|
|
|
|
#if defined(MBEDTLS_DHM_C)
|
|
|
|
mbedtls_mpi_free( &conf->dhm_P );
|
|
|
|
mbedtls_mpi_free( &conf->dhm_G );
|
|
|
|
#endif
|
|
|
|
|
2020-03-10 12:19:08 +01:00
|
|
|
#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
|
2015-05-04 13:35:39 +02:00
|
|
|
if( conf->psk != NULL )
|
|
|
|
{
|
2018-04-17 16:51:09 +02:00
|
|
|
mbedtls_platform_zeroize( conf->psk, conf->psk_len );
|
2015-05-04 13:35:39 +02:00
|
|
|
mbedtls_free( conf->psk );
|
2018-03-21 15:24:11 +01:00
|
|
|
conf->psk = NULL;
|
2015-05-04 13:35:39 +02:00
|
|
|
conf->psk_len = 0;
|
2017-12-20 08:29:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if( conf->psk_identity != NULL )
|
|
|
|
{
|
2018-04-17 16:51:09 +02:00
|
|
|
mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
|
2017-12-20 08:29:30 +01:00
|
|
|
mbedtls_free( conf->psk_identity );
|
2018-03-21 15:24:11 +01:00
|
|
|
conf->psk_identity = NULL;
|
2015-05-04 13:35:39 +02:00
|
|
|
conf->psk_identity_len = 0;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_X509_CRT_PARSE_C)
|
|
|
|
ssl_key_cert_free( conf->key_cert );
|
|
|
|
#endif
|
|
|
|
|
2018-04-17 16:51:09 +02:00
|
|
|
mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
|
2015-05-04 13:35:39 +02:00
|
|
|
}
|
|
|
|
|
2015-10-19 15:14:03 +02:00
|
|
|
#if defined(MBEDTLS_PK_C) && \
|
|
|
|
( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) )
|
2013-08-21 16:14:26 +02:00
|
|
|
/*
|
2015-04-08 12:49:31 +02:00
|
|
|
* Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
|
2013-08-21 16:14:26 +02:00
|
|
|
*/
|
2015-04-08 12:49:31 +02:00
|
|
|
unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
|
2013-08-21 16:14:26 +02:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_RSA_C)
|
|
|
|
if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
|
|
|
|
return( MBEDTLS_SSL_SIG_RSA );
|
2013-08-21 16:14:26 +02:00
|
|
|
#endif
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_ECDSA_C)
|
|
|
|
if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
|
|
|
|
return( MBEDTLS_SSL_SIG_ECDSA );
|
2013-08-21 16:14:26 +02:00
|
|
|
#endif
|
2015-04-08 12:49:31 +02:00
|
|
|
return( MBEDTLS_SSL_SIG_ANON );
|
2013-08-21 16:14:26 +02:00
|
|
|
}
|
|
|
|
|
2017-04-28 18:15:26 +02:00
|
|
|
unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
|
|
|
|
{
|
|
|
|
switch( type ) {
|
|
|
|
case MBEDTLS_PK_RSA:
|
|
|
|
return( MBEDTLS_SSL_SIG_RSA );
|
|
|
|
case MBEDTLS_PK_ECDSA:
|
|
|
|
case MBEDTLS_PK_ECKEY:
|
|
|
|
return( MBEDTLS_SSL_SIG_ECDSA );
|
|
|
|
default:
|
|
|
|
return( MBEDTLS_SSL_SIG_ANON );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
|
2013-08-22 13:52:48 +02:00
|
|
|
{
|
|
|
|
switch( sig )
|
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_RSA_C)
|
|
|
|
case MBEDTLS_SSL_SIG_RSA:
|
|
|
|
return( MBEDTLS_PK_RSA );
|
2013-08-22 13:52:48 +02:00
|
|
|
#endif
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_ECDSA_C)
|
|
|
|
case MBEDTLS_SSL_SIG_ECDSA:
|
|
|
|
return( MBEDTLS_PK_ECDSA );
|
2013-08-22 13:52:48 +02:00
|
|
|
#endif
|
|
|
|
default:
|
2015-04-08 12:49:31 +02:00
|
|
|
return( MBEDTLS_PK_NONE );
|
2013-08-22 13:52:48 +02:00
|
|
|
}
|
|
|
|
}
|
2015-10-19 15:14:03 +02:00
|
|
|
#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
|
2013-08-22 13:52:48 +02:00
|
|
|
|
2017-04-28 18:15:26 +02:00
|
|
|
#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
|
2020-03-10 12:19:08 +01:00
|
|
|
defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
|
2017-04-28 18:15:26 +02:00
|
|
|
|
|
|
|
/* Find an entry in a signature-hash set matching a given hash algorithm. */
|
|
|
|
mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set,
|
|
|
|
mbedtls_pk_type_t sig_alg )
|
|
|
|
{
|
|
|
|
switch( sig_alg )
|
|
|
|
{
|
|
|
|
case MBEDTLS_PK_RSA:
|
|
|
|
return( set->rsa );
|
|
|
|
case MBEDTLS_PK_ECDSA:
|
|
|
|
return( set->ecdsa );
|
|
|
|
default:
|
|
|
|
return( MBEDTLS_MD_NONE );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Add a signature-hash-pair to a signature-hash set */
|
|
|
|
void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set,
|
|
|
|
mbedtls_pk_type_t sig_alg,
|
|
|
|
mbedtls_md_type_t md_alg )
|
|
|
|
{
|
|
|
|
switch( sig_alg )
|
|
|
|
{
|
|
|
|
case MBEDTLS_PK_RSA:
|
|
|
|
if( set->rsa == MBEDTLS_MD_NONE )
|
|
|
|
set->rsa = md_alg;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case MBEDTLS_PK_ECDSA:
|
|
|
|
if( set->ecdsa == MBEDTLS_MD_NONE )
|
|
|
|
set->ecdsa = md_alg;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Allow exactly one hash algorithm for each signature. */
|
|
|
|
void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set,
|
|
|
|
mbedtls_md_type_t md_alg )
|
|
|
|
{
|
|
|
|
set->rsa = md_alg;
|
|
|
|
set->ecdsa = md_alg;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* MBEDTLS_SSL_PROTO_TLS1_2) &&
|
2020-03-10 12:19:08 +01:00
|
|
|
MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
|
2017-04-28 18:15:26 +02:00
|
|
|
|
2013-09-20 12:29:15 +02:00
|
|
|
/*
|
2015-06-17 14:34:48 +02:00
|
|
|
* Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
|
2013-09-20 12:29:15 +02:00
|
|
|
*/
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
|
2013-08-22 13:52:48 +02:00
|
|
|
{
|
|
|
|
switch( hash )
|
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_MD5_C)
|
|
|
|
case MBEDTLS_SSL_HASH_MD5:
|
|
|
|
return( MBEDTLS_MD_MD5 );
|
2013-08-22 13:52:48 +02:00
|
|
|
#endif
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_SHA1_C)
|
|
|
|
case MBEDTLS_SSL_HASH_SHA1:
|
|
|
|
return( MBEDTLS_MD_SHA1 );
|
2013-08-22 13:52:48 +02:00
|
|
|
#endif
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_SHA256_C)
|
|
|
|
case MBEDTLS_SSL_HASH_SHA224:
|
|
|
|
return( MBEDTLS_MD_SHA224 );
|
|
|
|
case MBEDTLS_SSL_HASH_SHA256:
|
|
|
|
return( MBEDTLS_MD_SHA256 );
|
2013-08-22 13:52:48 +02:00
|
|
|
#endif
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_SHA512_C)
|
|
|
|
case MBEDTLS_SSL_HASH_SHA384:
|
|
|
|
return( MBEDTLS_MD_SHA384 );
|
|
|
|
case MBEDTLS_SSL_HASH_SHA512:
|
|
|
|
return( MBEDTLS_MD_SHA512 );
|
2013-08-22 13:52:48 +02:00
|
|
|
#endif
|
|
|
|
default:
|
2015-04-08 12:49:31 +02:00
|
|
|
return( MBEDTLS_MD_NONE );
|
2013-08-22 13:52:48 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-17 14:34:48 +02:00
|
|
|
/*
|
|
|
|
* Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
|
|
|
|
*/
|
|
|
|
unsigned char mbedtls_ssl_hash_from_md_alg( int md )
|
|
|
|
{
|
|
|
|
switch( md )
|
|
|
|
{
|
|
|
|
#if defined(MBEDTLS_MD5_C)
|
|
|
|
case MBEDTLS_MD_MD5:
|
|
|
|
return( MBEDTLS_SSL_HASH_MD5 );
|
|
|
|
#endif
|
|
|
|
#if defined(MBEDTLS_SHA1_C)
|
|
|
|
case MBEDTLS_MD_SHA1:
|
|
|
|
return( MBEDTLS_SSL_HASH_SHA1 );
|
|
|
|
#endif
|
|
|
|
#if defined(MBEDTLS_SHA256_C)
|
|
|
|
case MBEDTLS_MD_SHA224:
|
|
|
|
return( MBEDTLS_SSL_HASH_SHA224 );
|
|
|
|
case MBEDTLS_MD_SHA256:
|
|
|
|
return( MBEDTLS_SSL_HASH_SHA256 );
|
|
|
|
#endif
|
|
|
|
#if defined(MBEDTLS_SHA512_C)
|
|
|
|
case MBEDTLS_MD_SHA384:
|
|
|
|
return( MBEDTLS_SSL_HASH_SHA384 );
|
|
|
|
case MBEDTLS_MD_SHA512:
|
|
|
|
return( MBEDTLS_SSL_HASH_SHA512 );
|
|
|
|
#endif
|
|
|
|
default:
|
|
|
|
return( MBEDTLS_SSL_HASH_NONE );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-17 11:43:30 +02:00
|
|
|
#if defined(MBEDTLS_ECP_C)
|
2014-02-04 16:18:07 +01:00
|
|
|
/*
|
2015-06-17 14:34:48 +02:00
|
|
|
* Check if a curve proposed by the peer is in our list.
|
2015-06-17 12:10:46 +02:00
|
|
|
* Return 0 if we're willing to use it, -1 otherwise.
|
2014-02-04 16:18:07 +01:00
|
|
|
*/
|
2015-06-17 12:10:46 +02:00
|
|
|
int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id )
|
2014-02-04 16:18:07 +01:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
const mbedtls_ecp_group_id *gid;
|
2014-02-04 16:18:07 +01:00
|
|
|
|
2015-06-17 12:10:46 +02:00
|
|
|
if( ssl->conf->curve_list == NULL )
|
|
|
|
return( -1 );
|
|
|
|
|
2015-05-04 10:55:58 +02:00
|
|
|
for( gid = ssl->conf->curve_list; *gid != MBEDTLS_ECP_DP_NONE; gid++ )
|
2014-02-04 16:18:07 +01:00
|
|
|
if( *gid == grp_id )
|
2015-06-17 12:10:46 +02:00
|
|
|
return( 0 );
|
2014-02-04 16:18:07 +01:00
|
|
|
|
2015-06-17 12:10:46 +02:00
|
|
|
return( -1 );
|
2014-02-04 16:18:07 +01:00
|
|
|
}
|
2015-06-17 11:43:30 +02:00
|
|
|
#endif /* MBEDTLS_ECP_C */
|
2014-04-09 09:50:57 +02:00
|
|
|
|
2020-03-10 12:19:08 +01:00
|
|
|
#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
|
2015-06-17 14:34:48 +02:00
|
|
|
/*
|
|
|
|
* Check if a hash proposed by the peer is in our list.
|
|
|
|
* Return 0 if we're willing to use it, -1 otherwise.
|
|
|
|
*/
|
|
|
|
int mbedtls_ssl_check_sig_hash( const mbedtls_ssl_context *ssl,
|
|
|
|
mbedtls_md_type_t md )
|
|
|
|
{
|
|
|
|
const int *cur;
|
|
|
|
|
|
|
|
if( ssl->conf->sig_hashes == NULL )
|
|
|
|
return( -1 );
|
|
|
|
|
|
|
|
for( cur = ssl->conf->sig_hashes; *cur != MBEDTLS_MD_NONE; cur++ )
|
|
|
|
if( *cur == (int) md )
|
|
|
|
return( 0 );
|
|
|
|
|
|
|
|
return( -1 );
|
|
|
|
}
|
2020-03-10 12:19:08 +01:00
|
|
|
#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
|
2015-06-17 14:34:48 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_X509_CRT_PARSE_C)
|
|
|
|
int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
|
|
|
|
const mbedtls_ssl_ciphersuite_t *ciphersuite,
|
2015-04-20 12:01:48 +02:00
|
|
|
int cert_endpoint,
|
2015-05-11 19:54:43 +02:00
|
|
|
uint32_t *flags )
|
2014-04-09 09:50:57 +02:00
|
|
|
{
|
2015-04-20 12:01:48 +02:00
|
|
|
int ret = 0;
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
|
2014-04-09 09:50:57 +02:00
|
|
|
int usage = 0;
|
|
|
|
#endif
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
|
2014-04-11 11:06:22 +02:00
|
|
|
const char *ext_oid;
|
|
|
|
size_t ext_len;
|
|
|
|
#endif
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#if !defined(MBEDTLS_X509_CHECK_KEY_USAGE) && \
|
|
|
|
!defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
|
2014-04-11 11:06:22 +02:00
|
|
|
((void) cert);
|
|
|
|
((void) cert_endpoint);
|
2015-04-20 12:01:48 +02:00
|
|
|
((void) flags);
|
2014-04-11 11:06:22 +02:00
|
|
|
#endif
|
2014-04-09 09:50:57 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
|
|
|
|
if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
|
2014-04-09 09:50:57 +02:00
|
|
|
{
|
|
|
|
/* Server part of the key exchange */
|
|
|
|
switch( ciphersuite->key_exchange )
|
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
case MBEDTLS_KEY_EXCHANGE_RSA:
|
|
|
|
case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
|
2015-04-20 13:19:02 +02:00
|
|
|
usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
|
2014-04-09 09:50:57 +02:00
|
|
|
break;
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
|
|
|
|
case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
|
|
|
|
case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
|
|
|
|
usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
|
2014-04-09 09:50:57 +02:00
|
|
|
break;
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
|
|
|
|
case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
|
2015-04-20 13:19:02 +02:00
|
|
|
usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
|
2014-04-09 09:50:57 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
/* Don't use default: we want warnings when adding new values */
|
2015-04-08 12:49:31 +02:00
|
|
|
case MBEDTLS_KEY_EXCHANGE_NONE:
|
|
|
|
case MBEDTLS_KEY_EXCHANGE_PSK:
|
|
|
|
case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
|
|
|
|
case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
|
2015-09-15 17:53:32 +02:00
|
|
|
case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
|
2014-04-09 09:50:57 +02:00
|
|
|
usage = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
/* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
|
|
|
|
usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
|
2014-04-09 09:50:57 +02:00
|
|
|
}
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
|
2015-04-20 12:01:48 +02:00
|
|
|
{
|
2015-04-20 13:19:02 +02:00
|
|
|
*flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
|
2015-04-20 12:01:48 +02:00
|
|
|
ret = -1;
|
|
|
|
}
|
2014-04-11 11:06:22 +02:00
|
|
|
#else
|
|
|
|
((void) ciphersuite);
|
2015-04-08 12:49:31 +02:00
|
|
|
#endif /* MBEDTLS_X509_CHECK_KEY_USAGE */
|
2014-04-09 09:50:57 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
|
|
|
|
if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
|
2014-04-11 11:06:22 +02:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
ext_oid = MBEDTLS_OID_SERVER_AUTH;
|
|
|
|
ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
|
2014-04-11 11:06:22 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
ext_oid = MBEDTLS_OID_CLIENT_AUTH;
|
|
|
|
ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
|
2014-04-11 11:06:22 +02:00
|
|
|
}
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
|
2015-04-20 12:01:48 +02:00
|
|
|
{
|
2015-04-20 13:19:02 +02:00
|
|
|
*flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
|
2015-04-20 12:01:48 +02:00
|
|
|
ret = -1;
|
|
|
|
}
|
2015-04-08 12:49:31 +02:00
|
|
|
#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
|
2014-04-11 11:06:22 +02:00
|
|
|
|
2015-04-20 12:01:48 +02:00
|
|
|
return( ret );
|
2014-04-09 09:50:57 +02:00
|
|
|
}
|
2015-04-08 12:49:31 +02:00
|
|
|
#endif /* MBEDTLS_X509_CRT_PARSE_C */
|
2014-04-29 15:11:17 +02:00
|
|
|
|
2016-10-13 18:21:01 +02:00
|
|
|
int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md )
|
|
|
|
{
|
|
|
|
#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
|
|
|
|
if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 )
|
|
|
|
return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
|
|
|
|
|
|
|
|
switch( md )
|
|
|
|
{
|
|
|
|
#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
|
|
|
|
#if defined(MBEDTLS_MD5_C)
|
|
|
|
case MBEDTLS_SSL_HASH_MD5:
|
2016-10-25 11:50:22 +02:00
|
|
|
return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
|
2016-10-13 18:21:01 +02:00
|
|
|
#endif
|
|
|
|
#if defined(MBEDTLS_SHA1_C)
|
|
|
|
case MBEDTLS_SSL_HASH_SHA1:
|
|
|
|
ssl->handshake->calc_verify = ssl_calc_verify_tls;
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
|
|
|
|
#if defined(MBEDTLS_SHA512_C)
|
|
|
|
case MBEDTLS_SSL_HASH_SHA384:
|
|
|
|
ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
#if defined(MBEDTLS_SHA256_C)
|
|
|
|
case MBEDTLS_SSL_HASH_SHA256:
|
|
|
|
ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
default:
|
|
|
|
return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
#else /* !MBEDTLS_SSL_PROTO_TLS1_2 */
|
|
|
|
(void) ssl;
|
|
|
|
(void) md;
|
|
|
|
|
|
|
|
return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
|
|
|
|
#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
|
|
|
|
}
|
|
|
|
|
2017-07-20 17:17:51 +02:00
|
|
|
#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
|
|
|
|
defined(MBEDTLS_SSL_PROTO_TLS1_1)
|
|
|
|
int mbedtls_ssl_get_key_exchange_md_ssl_tls( mbedtls_ssl_context *ssl,
|
|
|
|
unsigned char *output,
|
|
|
|
unsigned char *data, size_t data_len )
|
|
|
|
{
|
|
|
|
int ret = 0;
|
|
|
|
mbedtls_md5_context mbedtls_md5;
|
|
|
|
mbedtls_sha1_context mbedtls_sha1;
|
|
|
|
|
|
|
|
mbedtls_md5_init( &mbedtls_md5 );
|
|
|
|
mbedtls_sha1_init( &mbedtls_sha1 );
|
|
|
|
|
|
|
|
/*
|
|
|
|
* digitally-signed struct {
|
|
|
|
* opaque md5_hash[16];
|
|
|
|
* opaque sha_hash[20];
|
|
|
|
* };
|
|
|
|
*
|
|
|
|
* md5_hash
|
|
|
|
* MD5(ClientHello.random + ServerHello.random
|
|
|
|
* + ServerParams);
|
|
|
|
* sha_hash
|
|
|
|
* SHA(ClientHello.random + ServerHello.random
|
|
|
|
* + ServerParams);
|
|
|
|
*/
|
2018-01-22 11:48:08 +01:00
|
|
|
if( ( ret = mbedtls_md5_starts_ret( &mbedtls_md5 ) ) != 0 )
|
2017-07-20 17:17:51 +02:00
|
|
|
{
|
2018-01-22 11:48:08 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_starts_ret", ret );
|
2017-07-20 17:17:51 +02:00
|
|
|
goto exit;
|
|
|
|
}
|
2018-01-22 11:48:08 +01:00
|
|
|
if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5,
|
2017-07-20 17:17:51 +02:00
|
|
|
ssl->handshake->randbytes, 64 ) ) != 0 )
|
|
|
|
{
|
2018-01-22 11:48:08 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
|
2017-07-20 17:17:51 +02:00
|
|
|
goto exit;
|
|
|
|
}
|
2018-01-22 11:48:08 +01:00
|
|
|
if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5, data, data_len ) ) != 0 )
|
2017-07-20 17:17:51 +02:00
|
|
|
{
|
2018-01-22 11:48:08 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
|
2017-07-20 17:17:51 +02:00
|
|
|
goto exit;
|
|
|
|
}
|
2018-01-22 11:48:08 +01:00
|
|
|
if( ( ret = mbedtls_md5_finish_ret( &mbedtls_md5, output ) ) != 0 )
|
2017-07-20 17:17:51 +02:00
|
|
|
{
|
2018-01-22 11:48:08 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_finish_ret", ret );
|
2017-07-20 17:17:51 +02:00
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
2018-01-22 11:48:08 +01:00
|
|
|
if( ( ret = mbedtls_sha1_starts_ret( &mbedtls_sha1 ) ) != 0 )
|
2017-07-20 17:17:51 +02:00
|
|
|
{
|
2018-01-22 11:48:08 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_starts_ret", ret );
|
2017-07-20 17:17:51 +02:00
|
|
|
goto exit;
|
|
|
|
}
|
2018-01-22 11:48:08 +01:00
|
|
|
if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1,
|
2017-07-20 17:17:51 +02:00
|
|
|
ssl->handshake->randbytes, 64 ) ) != 0 )
|
|
|
|
{
|
2018-01-22 11:48:08 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
|
2017-07-20 17:17:51 +02:00
|
|
|
goto exit;
|
|
|
|
}
|
2018-01-22 11:48:08 +01:00
|
|
|
if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1, data,
|
2017-07-20 17:17:51 +02:00
|
|
|
data_len ) ) != 0 )
|
|
|
|
{
|
2018-01-22 11:48:08 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
|
2017-07-20 17:17:51 +02:00
|
|
|
goto exit;
|
|
|
|
}
|
2018-01-22 11:48:08 +01:00
|
|
|
if( ( ret = mbedtls_sha1_finish_ret( &mbedtls_sha1,
|
2017-07-20 17:17:51 +02:00
|
|
|
output + 16 ) ) != 0 )
|
|
|
|
{
|
2018-01-22 11:48:08 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_finish_ret", ret );
|
2017-07-20 17:17:51 +02:00
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
exit:
|
|
|
|
mbedtls_md5_free( &mbedtls_md5 );
|
|
|
|
mbedtls_sha1_free( &mbedtls_sha1 );
|
|
|
|
|
|
|
|
if( ret != 0 )
|
|
|
|
mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
|
|
|
|
MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
|
|
|
|
|
|
|
|
return( ret );
|
|
|
|
|
|
|
|
}
|
|
|
|
#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
|
|
|
|
MBEDTLS_SSL_PROTO_TLS1_1 */
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
|
|
|
|
defined(MBEDTLS_SSL_PROTO_TLS1_2)
|
2019-01-10 11:27:10 +01:00
|
|
|
|
|
|
|
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
|
|
|
int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
|
|
|
|
unsigned char *hash, size_t *hashlen,
|
|
|
|
unsigned char *data, size_t data_len,
|
|
|
|
mbedtls_md_type_t md_alg )
|
|
|
|
{
|
2019-01-14 10:35:19 +01:00
|
|
|
psa_status_t status;
|
2019-02-20 11:32:28 +01:00
|
|
|
psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT;
|
2019-01-10 11:27:10 +01:00
|
|
|
psa_algorithm_t hash_alg = mbedtls_psa_translate_md( md_alg );
|
|
|
|
|
2019-04-10 10:25:41 +02:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform PSA-based computation of digest of ServerKeyExchange" ) );
|
2019-01-14 10:35:19 +01:00
|
|
|
|
|
|
|
if( ( status = psa_hash_setup( &hash_operation,
|
|
|
|
hash_alg ) ) != PSA_SUCCESS )
|
2019-01-10 11:27:10 +01:00
|
|
|
{
|
2019-01-14 10:35:19 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_setup", status );
|
2019-01-10 11:27:10 +01:00
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
2019-01-14 10:35:19 +01:00
|
|
|
if( ( status = psa_hash_update( &hash_operation, ssl->handshake->randbytes,
|
|
|
|
64 ) ) != PSA_SUCCESS )
|
2019-01-10 11:27:10 +01:00
|
|
|
{
|
2019-01-14 10:35:19 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
|
2019-01-10 11:27:10 +01:00
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
2019-01-14 10:35:19 +01:00
|
|
|
if( ( status = psa_hash_update( &hash_operation,
|
|
|
|
data, data_len ) ) != PSA_SUCCESS )
|
2019-01-10 11:27:10 +01:00
|
|
|
{
|
2019-01-14 10:35:19 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
|
2019-01-10 11:27:10 +01:00
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
2019-01-14 10:35:19 +01:00
|
|
|
if( ( status = psa_hash_finish( &hash_operation, hash, MBEDTLS_MD_MAX_SIZE,
|
|
|
|
hashlen ) ) != PSA_SUCCESS )
|
2019-01-10 11:27:10 +01:00
|
|
|
{
|
2019-01-14 10:35:19 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_finish", status );
|
2019-01-10 11:27:10 +01:00
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
exit:
|
2019-01-14 10:35:19 +01:00
|
|
|
if( status != PSA_SUCCESS )
|
2019-01-10 11:27:10 +01:00
|
|
|
{
|
|
|
|
mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
|
|
|
|
MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
|
2019-01-14 10:35:19 +01:00
|
|
|
switch( status )
|
2019-01-10 11:27:10 +01:00
|
|
|
{
|
|
|
|
case PSA_ERROR_NOT_SUPPORTED:
|
|
|
|
return( MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE );
|
2019-01-14 10:35:19 +01:00
|
|
|
case PSA_ERROR_BAD_STATE: /* Intentional fallthrough */
|
2019-01-10 11:27:10 +01:00
|
|
|
case PSA_ERROR_BUFFER_TOO_SMALL:
|
|
|
|
return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
|
|
|
|
case PSA_ERROR_INSUFFICIENT_MEMORY:
|
|
|
|
return( MBEDTLS_ERR_MD_ALLOC_FAILED );
|
|
|
|
default:
|
|
|
|
return( MBEDTLS_ERR_MD_HW_ACCEL_FAILED );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
2017-07-20 17:17:51 +02:00
|
|
|
int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
|
2018-04-24 11:53:22 +02:00
|
|
|
unsigned char *hash, size_t *hashlen,
|
|
|
|
unsigned char *data, size_t data_len,
|
|
|
|
mbedtls_md_type_t md_alg )
|
2017-07-20 17:17:51 +02:00
|
|
|
{
|
|
|
|
int ret = 0;
|
|
|
|
mbedtls_md_context_t ctx;
|
|
|
|
const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
|
2018-04-24 11:53:22 +02:00
|
|
|
*hashlen = mbedtls_md_get_size( md_info );
|
2017-07-20 17:17:51 +02:00
|
|
|
|
2019-04-10 10:25:41 +02:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform mbedtls-based computation of digest of ServerKeyExchange" ) );
|
2019-01-14 10:35:19 +01:00
|
|
|
|
2017-07-20 17:17:51 +02:00
|
|
|
mbedtls_md_init( &ctx );
|
|
|
|
|
|
|
|
/*
|
|
|
|
* digitally-signed struct {
|
|
|
|
* opaque client_random[32];
|
|
|
|
* opaque server_random[32];
|
|
|
|
* ServerDHParams params;
|
|
|
|
* };
|
|
|
|
*/
|
|
|
|
if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
|
|
|
|
{
|
|
|
|
MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
|
|
|
|
{
|
|
|
|
MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
|
|
|
|
{
|
|
|
|
MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
|
|
|
|
{
|
|
|
|
MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
|
|
|
|
goto exit;
|
|
|
|
}
|
2018-04-24 11:53:22 +02:00
|
|
|
if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
|
2017-07-20 17:17:51 +02:00
|
|
|
{
|
|
|
|
MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
exit:
|
|
|
|
mbedtls_md_free( &ctx );
|
|
|
|
|
|
|
|
if( ret != 0 )
|
|
|
|
mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
|
|
|
|
MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
|
|
|
|
|
|
|
|
return( ret );
|
|
|
|
}
|
2019-01-10 11:27:10 +01:00
|
|
|
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
|
|
|
|
2017-07-20 17:17:51 +02:00
|
|
|
#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
|
|
|
|
MBEDTLS_SSL_PROTO_TLS1_2 */
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#endif /* MBEDTLS_SSL_TLS_C */
|