2009-01-03 22:22:43 +01:00
|
|
|
/*
|
2021-02-18 13:55:21 +01:00
|
|
|
* TLS shared functions
|
2009-01-03 22:22:43 +01:00
|
|
|
*
|
2020-08-07 13:07:28 +02:00
|
|
|
* Copyright The Mbed TLS Contributors
|
2023-11-02 20:47:20 +01:00
|
|
|
* SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
|
2009-01-03 22:22:43 +01:00
|
|
|
*/
|
|
|
|
/*
|
|
|
|
* http://www.ietf.org/rfc/rfc2246.txt
|
|
|
|
* http://www.ietf.org/rfc/rfc4346.txt
|
|
|
|
*/
|
|
|
|
|
2020-06-03 01:43:33 +02:00
|
|
|
#include "common.h"
|
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
|
|
|
#include "mbedtls/platform.h"
|
|
|
|
|
2015-03-09 18:05:11 +01:00
|
|
|
#include "mbedtls/ssl.h"
|
2022-02-10 16:45:15 +01:00
|
|
|
#include "ssl_client.h"
|
2022-03-08 11:37:55 +01:00
|
|
|
#include "ssl_debug_helpers.h"
|
2021-03-05 19:38:47 +01:00
|
|
|
#include "ssl_misc.h"
|
2022-08-17 22:09:31 +02:00
|
|
|
|
2024-01-17 10:24:52 +01:00
|
|
|
#include "debug_internal.h"
|
2019-12-18 16:07:04 +01:00
|
|
|
#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"
|
2021-10-19 12:22:25 +02:00
|
|
|
#include "mbedtls/constant_time.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)
|
2024-01-02 13:26:40 +01:00
|
|
|
#include "mbedtls/psa_util.h"
|
2023-03-28 12:33:20 +02:00
|
|
|
#include "md_psa.h"
|
2023-06-07 13:07:21 +02:00
|
|
|
#include "psa_util_internal.h"
|
2019-01-10 11:27:10 +01:00
|
|
|
#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
|
|
|
|
|
2022-12-23 17:00:06 +01:00
|
|
|
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
2023-05-30 11:45:00 +02:00
|
|
|
/* Define local translating functions to save code size by not using too many
|
|
|
|
* arguments in each translating place. */
|
|
|
|
static int local_err_translation(psa_status_t status)
|
|
|
|
{
|
|
|
|
return psa_status_to_mbedtls(status, psa_to_ssl_errors,
|
2023-05-30 15:45:17 +02:00
|
|
|
ARRAY_LENGTH(psa_to_ssl_errors),
|
2023-05-30 11:45:00 +02:00
|
|
|
psa_generic_status_to_mbedtls);
|
|
|
|
}
|
|
|
|
#define PSA_TO_MBEDTLS_ERR(status) local_err_translation(status)
|
2022-12-23 17:00:06 +01:00
|
|
|
#endif
|
|
|
|
|
2022-06-10 17:18:09 +02:00
|
|
|
#if defined(MBEDTLS_TEST_HOOKS)
|
|
|
|
static mbedtls_ssl_chk_buf_ptr_args chk_buf_ptr_fail_args;
|
|
|
|
|
|
|
|
void mbedtls_ssl_set_chk_buf_ptr_fail_args(
|
2023-01-11 14:50:10 +01:00
|
|
|
const uint8_t *cur, const uint8_t *end, size_t need)
|
2022-06-10 17:18:09 +02:00
|
|
|
{
|
|
|
|
chk_buf_ptr_fail_args.cur = cur;
|
|
|
|
chk_buf_ptr_fail_args.end = end;
|
|
|
|
chk_buf_ptr_fail_args.need = need;
|
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
void mbedtls_ssl_reset_chk_buf_ptr_fail_args(void)
|
2022-06-10 17:18:09 +02:00
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
memset(&chk_buf_ptr_fail_args, 0, sizeof(chk_buf_ptr_fail_args));
|
2022-06-10 17:18:09 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_ssl_cmp_chk_buf_ptr_fail_args(mbedtls_ssl_chk_buf_ptr_args *args)
|
2022-06-10 17:18:09 +02:00
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
return (chk_buf_ptr_fail_args.cur != args->cur) ||
|
|
|
|
(chk_buf_ptr_fail_args.end != args->end) ||
|
|
|
|
(chk_buf_ptr_fail_args.need != args->need);
|
2022-06-10 17:18:09 +02:00
|
|
|
}
|
|
|
|
#endif /* MBEDTLS_TEST_HOOKS */
|
|
|
|
|
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 */
|
|
|
|
|
2023-01-11 14:50:10 +01: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
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
if (len > MBEDTLS_SSL_CID_IN_LEN_MAX) {
|
|
|
|
return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
|
|
|
|
}
|
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
|
|
|
|
2023-01-11 14:50:10 +01: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;
|
2019-05-14 12:45:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
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
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_ssl_set_cid(mbedtls_ssl_context *ssl,
|
|
|
|
int enable,
|
|
|
|
unsigned char const *own_cid,
|
|
|
|
size_t own_cid_len)
|
2019-04-09 16:22:03 +02:00
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
|
|
|
|
return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
|
|
|
|
}
|
2019-05-03 15:38:32 +02:00
|
|
|
|
2019-04-25 17:01:49 +02:00
|
|
|
ssl->negotiate_cid = enable;
|
2023-01-11 14:50:10 +01:00
|
|
|
if (enable == MBEDTLS_SSL_CID_DISABLED) {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(3, ("Disable use of CID extension."));
|
|
|
|
return 0;
|
2019-04-25 17:01:49 +02:00
|
|
|
}
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(3, ("Enable use of CID extension."));
|
|
|
|
MBEDTLS_SSL_DEBUG_BUF(3, "Own CID", own_cid, own_cid_len);
|
2019-04-25 17:01:49 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (own_cid_len != ssl->conf->cid_len) {
|
|
|
|
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));
|
|
|
|
return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
|
2019-04-25 17:01:49 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
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
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2019-04-09 16:22:03 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_ssl_get_own_cid(mbedtls_ssl_context *ssl,
|
|
|
|
int *enabled,
|
|
|
|
unsigned char own_cid[MBEDTLS_SSL_CID_OUT_LEN_MAX],
|
|
|
|
size_t *own_cid_len)
|
2022-03-11 21:26:47 +01:00
|
|
|
{
|
|
|
|
*enabled = MBEDTLS_SSL_CID_DISABLED;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
|
|
|
|
return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
|
|
|
|
}
|
2022-03-11 21:26:47 +01:00
|
|
|
|
|
|
|
/* We report MBEDTLS_SSL_CID_DISABLED in case the CID length is
|
|
|
|
* zero as this is indistinguishable from not requesting to use
|
|
|
|
* the CID extension. */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->own_cid_len == 0 || ssl->negotiate_cid == MBEDTLS_SSL_CID_DISABLED) {
|
|
|
|
return 0;
|
|
|
|
}
|
2022-03-11 21:26:47 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (own_cid_len != NULL) {
|
2022-03-11 21:26:47 +01:00
|
|
|
*own_cid_len = ssl->own_cid_len;
|
2023-01-11 14:50:10 +01:00
|
|
|
if (own_cid != NULL) {
|
|
|
|
memcpy(own_cid, ssl->own_cid, ssl->own_cid_len);
|
|
|
|
}
|
2022-03-11 21:26:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
*enabled = MBEDTLS_SSL_CID_ENABLED;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2022-03-11 21:26:47 +01:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
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)
|
2019-04-09 16:22:03 +02:00
|
|
|
{
|
|
|
|
*enabled = MBEDTLS_SSL_CID_DISABLED;
|
2019-04-26 18:08:02 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
|
|
|
|
mbedtls_ssl_is_handshake_over(ssl) == 0) {
|
|
|
|
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. */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->transform_in->in_cid_len == 0 &&
|
|
|
|
ssl->transform_in->out_cid_len == 0) {
|
|
|
|
return 0;
|
2019-04-26 18:08:02 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (peer_cid_len != NULL) {
|
2019-05-22 17:50:35 +02:00
|
|
|
*peer_cid_len = ssl->transform_in->out_cid_len;
|
2023-01-11 14:50:10 +01:00
|
|
|
if (peer_cid != NULL) {
|
|
|
|
memcpy(peer_cid, ssl->transform_in->out_cid,
|
|
|
|
ssl->transform_in->out_cid_len);
|
2019-05-22 17:50:35 +02:00
|
|
|
}
|
|
|
|
}
|
2019-04-26 18:08:02 +02:00
|
|
|
|
|
|
|
*enabled = MBEDTLS_SSL_CID_ENABLED;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2019-04-09 16:22:03 +02:00
|
|
|
}
|
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
|
|
|
|
*/
|
2023-01-11 14:50:10 +01: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;
|
2016-05-25 12:56:48 +02:00
|
|
|
}
|
|
|
|
}
|
2015-04-08 12:49:31 +02:00
|
|
|
#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
|
2013-07-18 12:32:27 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_ssl_session_copy(mbedtls_ssl_session *dst,
|
|
|
|
const mbedtls_ssl_session *src)
|
2013-08-02 15:34:52 +02:00
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_ssl_session_free(dst);
|
|
|
|
memcpy(dst, src, sizeof(mbedtls_ssl_session));
|
2021-11-29 03:46:35 +01:00
|
|
|
#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
|
|
|
|
dst->ticket = NULL;
|
2022-10-12 11:47:38 +02:00
|
|
|
#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
|
|
|
|
defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
|
2022-10-13 04:22:40 +02:00
|
|
|
dst->hostname = NULL;
|
2021-11-29 03:46:35 +01:00
|
|
|
#endif
|
2022-10-12 11:47:38 +02:00
|
|
|
#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
|
2021-11-29 03:46:35 +01: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)
|
2023-01-11 14:50:10 +01: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
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
dst->peer_cert = mbedtls_calloc(1, sizeof(mbedtls_x509_crt));
|
|
|
|
if (dst->peer_cert == NULL) {
|
|
|
|
return MBEDTLS_ERR_SSL_ALLOC_FAILED;
|
|
|
|
}
|
2013-08-02 15:34:52 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_x509_crt_init(dst->peer_cert);
|
2013-08-02 15:34:52 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if ((ret = mbedtls_x509_crt_parse_der(dst->peer_cert, src->peer_cert->raw.p,
|
|
|
|
src->peer_cert->raw.len)) != 0) {
|
|
|
|
mbedtls_free(dst->peer_cert);
|
2013-08-02 15:34:52 +02:00
|
|
|
dst->peer_cert = NULL;
|
2023-01-11 14:50:10 +01:00
|
|
|
return ret;
|
2013-08-02 15:34:52 +02:00
|
|
|
}
|
|
|
|
}
|
2019-02-07 13:27:42 +01:00
|
|
|
#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (src->peer_cert_digest != NULL) {
|
2019-02-05 18:00:50 +01:00
|
|
|
dst->peer_cert_digest =
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_calloc(1, src->peer_cert_digest_len);
|
|
|
|
if (dst->peer_cert_digest == NULL) {
|
|
|
|
return MBEDTLS_ERR_SSL_ALLOC_FAILED;
|
|
|
|
}
|
2019-02-05 18:00:50 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
memcpy(dst->peer_cert_digest, src->peer_cert_digest,
|
|
|
|
src->peer_cert_digest_len);
|
2019-02-05 18:00:50 +01:00
|
|
|
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)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (src->ticket != NULL) {
|
|
|
|
dst->ticket = mbedtls_calloc(1, src->ticket_len);
|
|
|
|
if (dst->ticket == NULL) {
|
|
|
|
return MBEDTLS_ERR_SSL_ALLOC_FAILED;
|
|
|
|
}
|
2013-08-02 15:34:52 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
memcpy(dst->ticket, src->ticket, src->ticket_len);
|
2013-08-02 15:34:52 +02:00
|
|
|
}
|
2022-10-13 04:22:40 +02:00
|
|
|
|
|
|
|
#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
|
|
|
|
defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (src->endpoint == MBEDTLS_SSL_IS_CLIENT) {
|
2022-10-13 04:22:40 +02:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = mbedtls_ssl_session_set_hostname(dst, src->hostname);
|
|
|
|
if (ret != 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
2022-10-13 04:22:40 +02:00
|
|
|
}
|
|
|
|
#endif /* MBEDTLS_SSL_PROTO_TLS1_3 &&
|
|
|
|
MBEDTLS_SSL_SERVER_NAME_INDICATION */
|
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
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2013-08-02 15:34:52 +02:00
|
|
|
}
|
|
|
|
|
2020-03-03 16:39:58 +01:00
|
|
|
#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
|
2022-06-17 10:52:54 +02:00
|
|
|
MBEDTLS_CHECK_RETURN_CRITICAL
|
2023-01-11 14:50:10 +01:00
|
|
|
static int resize_buffer(unsigned char **buffer, size_t len_new, size_t *len_old)
|
2020-03-03 16:39:58 +01:00
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
unsigned char *resized_buffer = mbedtls_calloc(1, len_new);
|
|
|
|
if (resized_buffer == NULL) {
|
2023-11-22 03:28:28 +01:00
|
|
|
return MBEDTLS_ERR_SSL_ALLOC_FAILED;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2020-03-03 16:39:58 +01:00
|
|
|
|
|
|
|
/* 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. */
|
2023-01-11 14:50:10 +01:00
|
|
|
memcpy(resized_buffer, *buffer,
|
|
|
|
(len_new < *len_old) ? len_new : *len_old);
|
2023-07-17 16:17:40 +02:00
|
|
|
mbedtls_zeroize_and_free(*buffer, *len_old);
|
2020-03-03 16:39:58 +01:00
|
|
|
|
|
|
|
*buffer = resized_buffer;
|
|
|
|
*len_old = len_new;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2020-10-21 15:08:44 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
static void handle_buffer_resizing(mbedtls_ssl_context *ssl, int downsizing,
|
|
|
|
size_t in_buf_new_len,
|
|
|
|
size_t out_buf_new_len)
|
2020-10-21 15:08:44 +02:00
|
|
|
{
|
|
|
|
int modified = 0;
|
|
|
|
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;
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->in_buf != NULL) {
|
2020-10-21 15:08:44 +02:00
|
|
|
written_in = ssl->in_msg - ssl->in_buf;
|
|
|
|
iv_offset_in = ssl->in_iv - ssl->in_buf;
|
|
|
|
len_offset_in = ssl->in_len - ssl->in_buf;
|
2023-01-11 14:50:10 +01:00
|
|
|
if (downsizing ?
|
2020-10-21 15:08:44 +02:00
|
|
|
ssl->in_buf_len > in_buf_new_len && ssl->in_left < in_buf_new_len :
|
2023-01-11 14:50:10 +01:00
|
|
|
ssl->in_buf_len < in_buf_new_len) {
|
|
|
|
if (resize_buffer(&ssl->in_buf, in_buf_new_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 %" MBEDTLS_PRINTF_SIZET,
|
|
|
|
in_buf_new_len));
|
2020-10-21 15:08:44 +02:00
|
|
|
modified = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->out_buf != NULL) {
|
2020-10-21 15:08:44 +02:00
|
|
|
written_out = ssl->out_msg - ssl->out_buf;
|
|
|
|
iv_offset_out = ssl->out_iv - ssl->out_buf;
|
|
|
|
len_offset_out = ssl->out_len - ssl->out_buf;
|
2023-01-11 14:50:10 +01:00
|
|
|
if (downsizing ?
|
2020-10-21 15:08:44 +02:00
|
|
|
ssl->out_buf_len > out_buf_new_len && ssl->out_left < out_buf_new_len :
|
2023-01-11 14:50:10 +01:00
|
|
|
ssl->out_buf_len < out_buf_new_len) {
|
|
|
|
if (resize_buffer(&ssl->out_buf, out_buf_new_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 %" MBEDTLS_PRINTF_SIZET,
|
|
|
|
out_buf_new_len));
|
2020-10-21 15:08:44 +02:00
|
|
|
modified = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-01-11 14:50:10 +01:00
|
|
|
if (modified) {
|
2020-10-21 15:08:44 +02:00
|
|
|
/* Update pointers here to avoid doing it twice. */
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_ssl_reset_in_out_pointers(ssl);
|
2020-10-21 15:08:44 +02:00
|
|
|
/* Fields below might not be properly updated with record
|
|
|
|
* splitting or with CID, so they are manually updated here. */
|
|
|
|
ssl->out_msg = ssl->out_buf + written_out;
|
|
|
|
ssl->out_len = ssl->out_buf + len_offset_out;
|
|
|
|
ssl->out_iv = ssl->out_buf + iv_offset_out;
|
|
|
|
|
|
|
|
ssl->in_msg = ssl->in_buf + written_in;
|
|
|
|
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 /* MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH */
|
|
|
|
|
2022-02-17 06:40:45 +01:00
|
|
|
#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
|
2023-01-11 14:50:10 +01:00
|
|
|
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);
|
2022-02-17 06:40:45 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
static tls_prf_fn ssl_tls12prf_from_cs(int ciphersuite_id);
|
2022-02-17 06:40:45 +01:00
|
|
|
|
|
|
|
#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
|
|
|
|
|
|
|
|
/* 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);
|
|
|
|
|
2022-06-17 10:52:54 +02:00
|
|
|
MBEDTLS_CHECK_RETURN_CRITICAL
|
2023-01-11 14:50:10 +01:00
|
|
|
static int ssl_tls12_populate_transform(mbedtls_ssl_transform *transform,
|
|
|
|
int ciphersuite,
|
|
|
|
const unsigned char master[48],
|
2022-04-05 11:16:53 +02:00
|
|
|
#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
|
2023-01-11 14:50:10 +01:00
|
|
|
int encrypt_then_mac,
|
2022-04-05 11:16:53 +02:00
|
|
|
#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
|
2023-01-11 14:50:10 +01:00
|
|
|
ssl_tls_prf_t tls_prf,
|
|
|
|
const unsigned char randbytes[64],
|
|
|
|
mbedtls_ssl_protocol_version tls_version,
|
|
|
|
unsigned endpoint,
|
|
|
|
const mbedtls_ssl_context *ssl);
|
2022-02-17 06:40:45 +01:00
|
|
|
|
2023-03-17 12:50:01 +01:00
|
|
|
#if defined(MBEDTLS_MD_CAN_SHA256)
|
2022-06-17 10:52:54 +02:00
|
|
|
MBEDTLS_CHECK_RETURN_CRITICAL
|
2023-01-11 14:50:10 +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);
|
Make handshake hashing functions return int
There are three family of functions: update_checksum, calc_verify,
calc_finished, that perform hashing operations and were returning void
so far. This is not correct, as hashing functions can return errors (for
example, on hardware failure when accelerated). Change them to return
int.
This commit just changes the types: for now the functions always return
0, and their return value is not checked; this will be fixed in the
next few commits.
There is a related function in TLS 1.3,
mbedtls_ssl_reset_transcript_for_hrr, which also handles hashes, and
already returns int but does not correctly check for errors from hashing
functions so far, it will also be handled in the next few commits.
There's a special case with handshake_params_init: _init functions
should return void, so we'll need to split out the part that can return
errors, see the next commit.
Signed-off-by: Manuel Pégourié-Gonnard <manuel.pegourie-gonnard@arm.com>
2023-02-05 09:46:59 +01:00
|
|
|
static int ssl_calc_verify_tls_sha256(const mbedtls_ssl_context *, unsigned char *, size_t *);
|
|
|
|
static int ssl_calc_finished_tls_sha256(mbedtls_ssl_context *, unsigned char *, int);
|
2022-02-17 06:40:45 +01:00
|
|
|
|
2023-03-17 12:50:01 +01:00
|
|
|
#endif /* MBEDTLS_MD_CAN_SHA256*/
|
2022-02-17 06:40:45 +01:00
|
|
|
|
2023-03-17 12:50:01 +01:00
|
|
|
#if defined(MBEDTLS_MD_CAN_SHA384)
|
2022-06-17 10:52:54 +02:00
|
|
|
MBEDTLS_CHECK_RETURN_CRITICAL
|
2023-01-11 14:50:10 +01:00
|
|
|
static int tls_prf_sha384(const unsigned char *secret, size_t slen,
|
|
|
|
const char *label,
|
|
|
|
const unsigned char *random, size_t rlen,
|
|
|
|
unsigned char *dstbuf, size_t dlen);
|
2022-02-17 06:40:45 +01:00
|
|
|
|
Make handshake hashing functions return int
There are three family of functions: update_checksum, calc_verify,
calc_finished, that perform hashing operations and were returning void
so far. This is not correct, as hashing functions can return errors (for
example, on hardware failure when accelerated). Change them to return
int.
This commit just changes the types: for now the functions always return
0, and their return value is not checked; this will be fixed in the
next few commits.
There is a related function in TLS 1.3,
mbedtls_ssl_reset_transcript_for_hrr, which also handles hashes, and
already returns int but does not correctly check for errors from hashing
functions so far, it will also be handled in the next few commits.
There's a special case with handshake_params_init: _init functions
should return void, so we'll need to split out the part that can return
errors, see the next commit.
Signed-off-by: Manuel Pégourié-Gonnard <manuel.pegourie-gonnard@arm.com>
2023-02-05 09:46:59 +01:00
|
|
|
static int ssl_calc_verify_tls_sha384(const mbedtls_ssl_context *, unsigned char *, size_t *);
|
|
|
|
static int ssl_calc_finished_tls_sha384(mbedtls_ssl_context *, unsigned char *, int);
|
2023-03-17 12:50:01 +01:00
|
|
|
#endif /* MBEDTLS_MD_CAN_SHA384*/
|
2022-02-17 06:40:45 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
static size_t ssl_tls12_session_save(const mbedtls_ssl_session *session,
|
|
|
|
unsigned char *buf,
|
|
|
|
size_t buf_len);
|
2022-07-13 09:15:48 +02:00
|
|
|
|
2022-06-17 10:52:54 +02:00
|
|
|
MBEDTLS_CHECK_RETURN_CRITICAL
|
2023-01-11 14:50:10 +01:00
|
|
|
static int ssl_tls12_session_load(mbedtls_ssl_session *session,
|
|
|
|
const unsigned char *buf,
|
|
|
|
size_t len);
|
2022-02-17 06:40:45 +01:00
|
|
|
#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
|
|
|
|
|
Make handshake hashing functions return int
There are three family of functions: update_checksum, calc_verify,
calc_finished, that perform hashing operations and were returning void
so far. This is not correct, as hashing functions can return errors (for
example, on hardware failure when accelerated). Change them to return
int.
This commit just changes the types: for now the functions always return
0, and their return value is not checked; this will be fixed in the
next few commits.
There is a related function in TLS 1.3,
mbedtls_ssl_reset_transcript_for_hrr, which also handles hashes, and
already returns int but does not correctly check for errors from hashing
functions so far, it will also be handled in the next few commits.
There's a special case with handshake_params_init: _init functions
should return void, so we'll need to split out the part that can return
errors, see the next commit.
Signed-off-by: Manuel Pégourié-Gonnard <manuel.pegourie-gonnard@arm.com>
2023-02-05 09:46:59 +01:00
|
|
|
static int ssl_update_checksum_start(mbedtls_ssl_context *, const unsigned char *, size_t);
|
2022-02-09 09:25:09 +01:00
|
|
|
|
2023-03-17 12:50:01 +01:00
|
|
|
#if defined(MBEDTLS_MD_CAN_SHA256)
|
Make handshake hashing functions return int
There are three family of functions: update_checksum, calc_verify,
calc_finished, that perform hashing operations and were returning void
so far. This is not correct, as hashing functions can return errors (for
example, on hardware failure when accelerated). Change them to return
int.
This commit just changes the types: for now the functions always return
0, and their return value is not checked; this will be fixed in the
next few commits.
There is a related function in TLS 1.3,
mbedtls_ssl_reset_transcript_for_hrr, which also handles hashes, and
already returns int but does not correctly check for errors from hashing
functions so far, it will also be handled in the next few commits.
There's a special case with handshake_params_init: _init functions
should return void, so we'll need to split out the part that can return
errors, see the next commit.
Signed-off-by: Manuel Pégourié-Gonnard <manuel.pegourie-gonnard@arm.com>
2023-02-05 09:46:59 +01:00
|
|
|
static int ssl_update_checksum_sha256(mbedtls_ssl_context *, const unsigned char *, size_t);
|
2023-03-17 12:50:01 +01:00
|
|
|
#endif /* MBEDTLS_MD_CAN_SHA256*/
|
2022-01-27 07:54:54 +01:00
|
|
|
|
2023-03-17 12:50:01 +01:00
|
|
|
#if defined(MBEDTLS_MD_CAN_SHA384)
|
Make handshake hashing functions return int
There are three family of functions: update_checksum, calc_verify,
calc_finished, that perform hashing operations and were returning void
so far. This is not correct, as hashing functions can return errors (for
example, on hardware failure when accelerated). Change them to return
int.
This commit just changes the types: for now the functions always return
0, and their return value is not checked; this will be fixed in the
next few commits.
There is a related function in TLS 1.3,
mbedtls_ssl_reset_transcript_for_hrr, which also handles hashes, and
already returns int but does not correctly check for errors from hashing
functions so far, it will also be handled in the next few commits.
There's a special case with handshake_params_init: _init functions
should return void, so we'll need to split out the part that can return
errors, see the next commit.
Signed-off-by: Manuel Pégourié-Gonnard <manuel.pegourie-gonnard@arm.com>
2023-02-05 09:46:59 +01:00
|
|
|
static int ssl_update_checksum_sha384(mbedtls_ssl_context *, const unsigned char *, size_t);
|
2023-03-17 12:50:01 +01:00
|
|
|
#endif /* MBEDTLS_MD_CAN_SHA384*/
|
2012-04-11 14:09:53 +02:00
|
|
|
|
2023-01-11 14:50:10 +01: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)
|
2019-05-12 13:54:30 +02:00
|
|
|
{
|
|
|
|
mbedtls_ssl_tls_prf_cb *tls_prf = NULL;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
switch (prf) {
|
2019-05-15 13:54:22 +02:00
|
|
|
#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
|
2023-03-17 12:50:01 +01:00
|
|
|
#if defined(MBEDTLS_MD_CAN_SHA384)
|
2019-05-12 13:54:30 +02:00
|
|
|
case MBEDTLS_SSL_TLS_PRF_SHA384:
|
|
|
|
tls_prf = tls_prf_sha384;
|
2023-01-11 14:50:10 +01:00
|
|
|
break;
|
2023-03-17 12:50:01 +01:00
|
|
|
#endif /* MBEDTLS_MD_CAN_SHA384*/
|
|
|
|
#if defined(MBEDTLS_MD_CAN_SHA256)
|
2019-05-12 13:54:30 +02:00
|
|
|
case MBEDTLS_SSL_TLS_PRF_SHA256:
|
|
|
|
tls_prf = tls_prf_sha256;
|
2023-01-11 14:50:10 +01:00
|
|
|
break;
|
2023-03-17 12:50:01 +01:00
|
|
|
#endif /* MBEDTLS_MD_CAN_SHA256*/
|
2019-05-15 13:54:22 +02:00
|
|
|
#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
|
2023-01-11 14:50:10 +01:00
|
|
|
default:
|
|
|
|
return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
|
2019-05-12 13:54:30 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return tls_prf(secret, slen, label, random, rlen, dstbuf, dlen);
|
2019-05-12 13:54:30 +02:00
|
|
|
}
|
|
|
|
|
2022-02-08 13:29:25 +01:00
|
|
|
#if defined(MBEDTLS_X509_CRT_PARSE_C)
|
2023-01-11 14:50:10 +01:00
|
|
|
static void ssl_clear_peer_cert(mbedtls_ssl_session *session)
|
2022-02-08 13:29:25 +01:00
|
|
|
{
|
|
|
|
#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (session->peer_cert != NULL) {
|
|
|
|
mbedtls_x509_crt_free(session->peer_cert);
|
|
|
|
mbedtls_free(session->peer_cert);
|
2022-02-08 13:29:25 +01:00
|
|
|
session->peer_cert = NULL;
|
|
|
|
}
|
|
|
|
#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (session->peer_cert_digest != NULL) {
|
2022-02-08 13:29:25 +01:00
|
|
|
/* Zeroization is not necessary. */
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_free(session->peer_cert_digest);
|
2022-02-08 13:29:25 +01:00
|
|
|
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 */
|
|
|
|
}
|
|
|
|
#endif /* MBEDTLS_X509_CRT_PARSE_C */
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
uint32_t mbedtls_ssl_get_extension_id(unsigned int extension_type)
|
2022-10-31 06:08:18 +01:00
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
switch (extension_type) {
|
2022-10-31 06:08:18 +01:00
|
|
|
case MBEDTLS_TLS_EXT_SERVERNAME:
|
2023-01-11 14:50:10 +01:00
|
|
|
return MBEDTLS_SSL_EXT_ID_SERVERNAME;
|
2022-10-31 06:08:18 +01:00
|
|
|
|
|
|
|
case MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH:
|
2023-01-11 14:50:10 +01:00
|
|
|
return MBEDTLS_SSL_EXT_ID_MAX_FRAGMENT_LENGTH;
|
2022-10-31 06:08:18 +01:00
|
|
|
|
|
|
|
case MBEDTLS_TLS_EXT_STATUS_REQUEST:
|
2023-01-11 14:50:10 +01:00
|
|
|
return MBEDTLS_SSL_EXT_ID_STATUS_REQUEST;
|
2022-10-31 06:08:18 +01:00
|
|
|
|
|
|
|
case MBEDTLS_TLS_EXT_SUPPORTED_GROUPS:
|
2023-01-11 14:50:10 +01:00
|
|
|
return MBEDTLS_SSL_EXT_ID_SUPPORTED_GROUPS;
|
2022-10-31 06:08:18 +01:00
|
|
|
|
|
|
|
case MBEDTLS_TLS_EXT_SIG_ALG:
|
2023-01-11 14:50:10 +01:00
|
|
|
return MBEDTLS_SSL_EXT_ID_SIG_ALG;
|
2022-10-31 06:08:18 +01:00
|
|
|
|
|
|
|
case MBEDTLS_TLS_EXT_USE_SRTP:
|
2023-01-11 14:50:10 +01:00
|
|
|
return MBEDTLS_SSL_EXT_ID_USE_SRTP;
|
2022-10-31 06:08:18 +01:00
|
|
|
|
|
|
|
case MBEDTLS_TLS_EXT_HEARTBEAT:
|
2023-01-11 14:50:10 +01:00
|
|
|
return MBEDTLS_SSL_EXT_ID_HEARTBEAT;
|
2022-10-31 06:08:18 +01:00
|
|
|
|
|
|
|
case MBEDTLS_TLS_EXT_ALPN:
|
2023-01-11 14:50:10 +01:00
|
|
|
return MBEDTLS_SSL_EXT_ID_ALPN;
|
2022-10-31 06:08:18 +01:00
|
|
|
|
|
|
|
case MBEDTLS_TLS_EXT_SCT:
|
2023-01-11 14:50:10 +01:00
|
|
|
return MBEDTLS_SSL_EXT_ID_SCT;
|
2022-10-31 06:08:18 +01:00
|
|
|
|
|
|
|
case MBEDTLS_TLS_EXT_CLI_CERT_TYPE:
|
2023-01-11 14:50:10 +01:00
|
|
|
return MBEDTLS_SSL_EXT_ID_CLI_CERT_TYPE;
|
2022-10-31 06:08:18 +01:00
|
|
|
|
|
|
|
case MBEDTLS_TLS_EXT_SERV_CERT_TYPE:
|
2023-01-11 14:50:10 +01:00
|
|
|
return MBEDTLS_SSL_EXT_ID_SERV_CERT_TYPE;
|
2022-10-31 06:08:18 +01:00
|
|
|
|
|
|
|
case MBEDTLS_TLS_EXT_PADDING:
|
2023-01-11 14:50:10 +01:00
|
|
|
return MBEDTLS_SSL_EXT_ID_PADDING;
|
2022-10-31 06:08:18 +01:00
|
|
|
|
|
|
|
case MBEDTLS_TLS_EXT_PRE_SHARED_KEY:
|
2023-01-11 14:50:10 +01:00
|
|
|
return MBEDTLS_SSL_EXT_ID_PRE_SHARED_KEY;
|
2022-10-31 06:08:18 +01:00
|
|
|
|
|
|
|
case MBEDTLS_TLS_EXT_EARLY_DATA:
|
2023-01-11 14:50:10 +01:00
|
|
|
return MBEDTLS_SSL_EXT_ID_EARLY_DATA;
|
2022-10-31 06:08:18 +01:00
|
|
|
|
|
|
|
case MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS:
|
2023-01-11 14:50:10 +01:00
|
|
|
return MBEDTLS_SSL_EXT_ID_SUPPORTED_VERSIONS;
|
2022-10-31 06:08:18 +01:00
|
|
|
|
|
|
|
case MBEDTLS_TLS_EXT_COOKIE:
|
2023-01-11 14:50:10 +01:00
|
|
|
return MBEDTLS_SSL_EXT_ID_COOKIE;
|
2022-10-31 06:08:18 +01:00
|
|
|
|
|
|
|
case MBEDTLS_TLS_EXT_PSK_KEY_EXCHANGE_MODES:
|
2023-01-11 14:50:10 +01:00
|
|
|
return MBEDTLS_SSL_EXT_ID_PSK_KEY_EXCHANGE_MODES;
|
2022-10-31 06:08:18 +01:00
|
|
|
|
|
|
|
case MBEDTLS_TLS_EXT_CERT_AUTH:
|
2023-01-11 14:50:10 +01:00
|
|
|
return MBEDTLS_SSL_EXT_ID_CERT_AUTH;
|
2022-10-31 06:08:18 +01:00
|
|
|
|
|
|
|
case MBEDTLS_TLS_EXT_OID_FILTERS:
|
2023-01-11 14:50:10 +01:00
|
|
|
return MBEDTLS_SSL_EXT_ID_OID_FILTERS;
|
2022-10-31 06:08:18 +01:00
|
|
|
|
|
|
|
case MBEDTLS_TLS_EXT_POST_HANDSHAKE_AUTH:
|
2023-01-11 14:50:10 +01:00
|
|
|
return MBEDTLS_SSL_EXT_ID_POST_HANDSHAKE_AUTH;
|
2022-10-31 06:08:18 +01:00
|
|
|
|
|
|
|
case MBEDTLS_TLS_EXT_SIG_ALG_CERT:
|
2023-01-11 14:50:10 +01:00
|
|
|
return MBEDTLS_SSL_EXT_ID_SIG_ALG_CERT;
|
2022-10-31 06:08:18 +01:00
|
|
|
|
|
|
|
case MBEDTLS_TLS_EXT_KEY_SHARE:
|
2023-01-11 14:50:10 +01:00
|
|
|
return MBEDTLS_SSL_EXT_ID_KEY_SHARE;
|
2022-10-31 06:08:18 +01:00
|
|
|
|
|
|
|
case MBEDTLS_TLS_EXT_TRUNCATED_HMAC:
|
2023-01-11 14:50:10 +01:00
|
|
|
return MBEDTLS_SSL_EXT_ID_TRUNCATED_HMAC;
|
2022-10-31 06:08:18 +01:00
|
|
|
|
|
|
|
case MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS:
|
2023-01-11 14:50:10 +01:00
|
|
|
return MBEDTLS_SSL_EXT_ID_SUPPORTED_POINT_FORMATS;
|
2022-10-31 06:08:18 +01:00
|
|
|
|
|
|
|
case MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC:
|
2023-01-11 14:50:10 +01:00
|
|
|
return MBEDTLS_SSL_EXT_ID_ENCRYPT_THEN_MAC;
|
2022-10-31 06:08:18 +01:00
|
|
|
|
|
|
|
case MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET:
|
2023-01-11 14:50:10 +01:00
|
|
|
return MBEDTLS_SSL_EXT_ID_EXTENDED_MASTER_SECRET;
|
2022-10-31 06:08:18 +01:00
|
|
|
|
2023-02-10 12:45:19 +01:00
|
|
|
case MBEDTLS_TLS_EXT_RECORD_SIZE_LIMIT:
|
|
|
|
return MBEDTLS_SSL_EXT_ID_RECORD_SIZE_LIMIT;
|
|
|
|
|
2022-10-31 06:08:18 +01:00
|
|
|
case MBEDTLS_TLS_EXT_SESSION_TICKET:
|
2023-01-11 14:50:10 +01:00
|
|
|
return MBEDTLS_SSL_EXT_ID_SESSION_TICKET;
|
2022-10-31 06:08:18 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return MBEDTLS_SSL_EXT_ID_UNRECOGNIZED;
|
2022-10-31 06:08:18 +01:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
uint32_t mbedtls_ssl_get_extension_mask(unsigned int extension_type)
|
2022-10-31 06:08:18 +01:00
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
return 1 << mbedtls_ssl_get_extension_id(extension_type);
|
2022-10-31 06:08:18 +01:00
|
|
|
}
|
|
|
|
|
2022-10-31 05:48:30 +01:00
|
|
|
#if defined(MBEDTLS_DEBUG_C)
|
|
|
|
static const char *extension_name_table[] = {
|
2022-11-08 14:01:17 +01:00
|
|
|
[MBEDTLS_SSL_EXT_ID_UNRECOGNIZED] = "unrecognized",
|
2022-10-31 05:48:30 +01:00
|
|
|
[MBEDTLS_SSL_EXT_ID_SERVERNAME] = "server_name",
|
|
|
|
[MBEDTLS_SSL_EXT_ID_MAX_FRAGMENT_LENGTH] = "max_fragment_length",
|
|
|
|
[MBEDTLS_SSL_EXT_ID_STATUS_REQUEST] = "status_request",
|
|
|
|
[MBEDTLS_SSL_EXT_ID_SUPPORTED_GROUPS] = "supported_groups",
|
|
|
|
[MBEDTLS_SSL_EXT_ID_SIG_ALG] = "signature_algorithms",
|
|
|
|
[MBEDTLS_SSL_EXT_ID_USE_SRTP] = "use_srtp",
|
|
|
|
[MBEDTLS_SSL_EXT_ID_HEARTBEAT] = "heartbeat",
|
|
|
|
[MBEDTLS_SSL_EXT_ID_ALPN] = "application_layer_protocol_negotiation",
|
|
|
|
[MBEDTLS_SSL_EXT_ID_SCT] = "signed_certificate_timestamp",
|
|
|
|
[MBEDTLS_SSL_EXT_ID_CLI_CERT_TYPE] = "client_certificate_type",
|
|
|
|
[MBEDTLS_SSL_EXT_ID_SERV_CERT_TYPE] = "server_certificate_type",
|
|
|
|
[MBEDTLS_SSL_EXT_ID_PADDING] = "padding",
|
|
|
|
[MBEDTLS_SSL_EXT_ID_PRE_SHARED_KEY] = "pre_shared_key",
|
|
|
|
[MBEDTLS_SSL_EXT_ID_EARLY_DATA] = "early_data",
|
|
|
|
[MBEDTLS_SSL_EXT_ID_SUPPORTED_VERSIONS] = "supported_versions",
|
|
|
|
[MBEDTLS_SSL_EXT_ID_COOKIE] = "cookie",
|
|
|
|
[MBEDTLS_SSL_EXT_ID_PSK_KEY_EXCHANGE_MODES] = "psk_key_exchange_modes",
|
|
|
|
[MBEDTLS_SSL_EXT_ID_CERT_AUTH] = "certificate_authorities",
|
|
|
|
[MBEDTLS_SSL_EXT_ID_OID_FILTERS] = "oid_filters",
|
|
|
|
[MBEDTLS_SSL_EXT_ID_POST_HANDSHAKE_AUTH] = "post_handshake_auth",
|
|
|
|
[MBEDTLS_SSL_EXT_ID_SIG_ALG_CERT] = "signature_algorithms_cert",
|
|
|
|
[MBEDTLS_SSL_EXT_ID_KEY_SHARE] = "key_share",
|
|
|
|
[MBEDTLS_SSL_EXT_ID_TRUNCATED_HMAC] = "truncated_hmac",
|
|
|
|
[MBEDTLS_SSL_EXT_ID_SUPPORTED_POINT_FORMATS] = "supported_point_formats",
|
|
|
|
[MBEDTLS_SSL_EXT_ID_ENCRYPT_THEN_MAC] = "encrypt_then_mac",
|
|
|
|
[MBEDTLS_SSL_EXT_ID_EXTENDED_MASTER_SECRET] = "extended_master_secret",
|
2023-02-10 12:45:19 +01:00
|
|
|
[MBEDTLS_SSL_EXT_ID_SESSION_TICKET] = "session_ticket",
|
|
|
|
[MBEDTLS_SSL_EXT_ID_RECORD_SIZE_LIMIT] = "record_size_limit"
|
2022-10-31 05:48:30 +01:00
|
|
|
};
|
|
|
|
|
2023-12-28 10:07:43 +01:00
|
|
|
static const unsigned int extension_type_table[] = {
|
2022-10-31 05:48:30 +01:00
|
|
|
[MBEDTLS_SSL_EXT_ID_UNRECOGNIZED] = 0xff,
|
|
|
|
[MBEDTLS_SSL_EXT_ID_SERVERNAME] = MBEDTLS_TLS_EXT_SERVERNAME,
|
|
|
|
[MBEDTLS_SSL_EXT_ID_MAX_FRAGMENT_LENGTH] = MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH,
|
|
|
|
[MBEDTLS_SSL_EXT_ID_STATUS_REQUEST] = MBEDTLS_TLS_EXT_STATUS_REQUEST,
|
|
|
|
[MBEDTLS_SSL_EXT_ID_SUPPORTED_GROUPS] = MBEDTLS_TLS_EXT_SUPPORTED_GROUPS,
|
|
|
|
[MBEDTLS_SSL_EXT_ID_SIG_ALG] = MBEDTLS_TLS_EXT_SIG_ALG,
|
|
|
|
[MBEDTLS_SSL_EXT_ID_USE_SRTP] = MBEDTLS_TLS_EXT_USE_SRTP,
|
|
|
|
[MBEDTLS_SSL_EXT_ID_HEARTBEAT] = MBEDTLS_TLS_EXT_HEARTBEAT,
|
|
|
|
[MBEDTLS_SSL_EXT_ID_ALPN] = MBEDTLS_TLS_EXT_ALPN,
|
|
|
|
[MBEDTLS_SSL_EXT_ID_SCT] = MBEDTLS_TLS_EXT_SCT,
|
|
|
|
[MBEDTLS_SSL_EXT_ID_CLI_CERT_TYPE] = MBEDTLS_TLS_EXT_CLI_CERT_TYPE,
|
|
|
|
[MBEDTLS_SSL_EXT_ID_SERV_CERT_TYPE] = MBEDTLS_TLS_EXT_SERV_CERT_TYPE,
|
|
|
|
[MBEDTLS_SSL_EXT_ID_PADDING] = MBEDTLS_TLS_EXT_PADDING,
|
|
|
|
[MBEDTLS_SSL_EXT_ID_PRE_SHARED_KEY] = MBEDTLS_TLS_EXT_PRE_SHARED_KEY,
|
|
|
|
[MBEDTLS_SSL_EXT_ID_EARLY_DATA] = MBEDTLS_TLS_EXT_EARLY_DATA,
|
|
|
|
[MBEDTLS_SSL_EXT_ID_SUPPORTED_VERSIONS] = MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS,
|
|
|
|
[MBEDTLS_SSL_EXT_ID_COOKIE] = MBEDTLS_TLS_EXT_COOKIE,
|
|
|
|
[MBEDTLS_SSL_EXT_ID_PSK_KEY_EXCHANGE_MODES] = MBEDTLS_TLS_EXT_PSK_KEY_EXCHANGE_MODES,
|
|
|
|
[MBEDTLS_SSL_EXT_ID_CERT_AUTH] = MBEDTLS_TLS_EXT_CERT_AUTH,
|
|
|
|
[MBEDTLS_SSL_EXT_ID_OID_FILTERS] = MBEDTLS_TLS_EXT_OID_FILTERS,
|
|
|
|
[MBEDTLS_SSL_EXT_ID_POST_HANDSHAKE_AUTH] = MBEDTLS_TLS_EXT_POST_HANDSHAKE_AUTH,
|
|
|
|
[MBEDTLS_SSL_EXT_ID_SIG_ALG_CERT] = MBEDTLS_TLS_EXT_SIG_ALG_CERT,
|
|
|
|
[MBEDTLS_SSL_EXT_ID_KEY_SHARE] = MBEDTLS_TLS_EXT_KEY_SHARE,
|
|
|
|
[MBEDTLS_SSL_EXT_ID_TRUNCATED_HMAC] = MBEDTLS_TLS_EXT_TRUNCATED_HMAC,
|
|
|
|
[MBEDTLS_SSL_EXT_ID_SUPPORTED_POINT_FORMATS] = MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS,
|
|
|
|
[MBEDTLS_SSL_EXT_ID_ENCRYPT_THEN_MAC] = MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC,
|
|
|
|
[MBEDTLS_SSL_EXT_ID_EXTENDED_MASTER_SECRET] = MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET,
|
2023-02-10 12:45:19 +01:00
|
|
|
[MBEDTLS_SSL_EXT_ID_SESSION_TICKET] = MBEDTLS_TLS_EXT_SESSION_TICKET,
|
|
|
|
[MBEDTLS_SSL_EXT_ID_RECORD_SIZE_LIMIT] = MBEDTLS_TLS_EXT_RECORD_SIZE_LIMIT
|
2022-10-31 05:48:30 +01:00
|
|
|
};
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
const char *mbedtls_ssl_get_extension_name(unsigned int extension_type)
|
2022-10-31 05:48:30 +01:00
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
return extension_name_table[
|
|
|
|
mbedtls_ssl_get_extension_id(extension_type)];
|
2022-10-31 05:48:30 +01:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
static const char *ssl_tls13_get_hs_msg_name(int hs_msg_type)
|
2022-10-31 05:48:30 +01:00
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
switch (hs_msg_type) {
|
2022-10-31 05:48:30 +01:00
|
|
|
case MBEDTLS_SSL_HS_CLIENT_HELLO:
|
2023-01-11 14:50:10 +01:00
|
|
|
return "ClientHello";
|
2022-10-31 05:48:30 +01:00
|
|
|
case MBEDTLS_SSL_HS_SERVER_HELLO:
|
2023-01-11 14:50:10 +01:00
|
|
|
return "ServerHello";
|
2022-10-31 05:48:30 +01:00
|
|
|
case MBEDTLS_SSL_TLS1_3_HS_HELLO_RETRY_REQUEST:
|
2023-01-11 14:50:10 +01:00
|
|
|
return "HelloRetryRequest";
|
2022-10-31 05:48:30 +01:00
|
|
|
case MBEDTLS_SSL_HS_NEW_SESSION_TICKET:
|
2023-01-11 14:50:10 +01:00
|
|
|
return "NewSessionTicket";
|
2022-10-31 05:48:30 +01:00
|
|
|
case MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS:
|
2023-01-11 14:50:10 +01:00
|
|
|
return "EncryptedExtensions";
|
2022-10-31 05:48:30 +01:00
|
|
|
case MBEDTLS_SSL_HS_CERTIFICATE:
|
2023-01-11 14:50:10 +01:00
|
|
|
return "Certificate";
|
2022-10-31 05:48:30 +01:00
|
|
|
case MBEDTLS_SSL_HS_CERTIFICATE_REQUEST:
|
2023-01-11 14:50:10 +01:00
|
|
|
return "CertificateRequest";
|
2022-10-31 05:48:30 +01:00
|
|
|
}
|
2023-01-11 14:50:10 +01:00
|
|
|
return "Unknown";
|
2022-10-31 05:48:30 +01:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
void mbedtls_ssl_print_extension(const mbedtls_ssl_context *ssl,
|
|
|
|
int level, const char *file, int line,
|
|
|
|
int hs_msg_type, unsigned int extension_type,
|
|
|
|
const char *extra_msg0, const char *extra_msg1)
|
2022-10-31 05:48:30 +01:00
|
|
|
{
|
|
|
|
const char *extra_msg;
|
2023-01-11 14:50:10 +01:00
|
|
|
if (extra_msg0 && extra_msg1) {
|
2022-10-31 05:48:30 +01:00
|
|
|
mbedtls_debug_print_msg(
|
|
|
|
ssl, level, file, line,
|
|
|
|
"%s: %s(%u) extension %s %s.",
|
2023-01-11 14:50:10 +01:00
|
|
|
ssl_tls13_get_hs_msg_name(hs_msg_type),
|
|
|
|
mbedtls_ssl_get_extension_name(extension_type),
|
2022-10-31 05:48:30 +01:00
|
|
|
extension_type,
|
2023-01-11 14:50:10 +01:00
|
|
|
extra_msg0, extra_msg1);
|
2022-10-31 05:48:30 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
extra_msg = extra_msg0 ? extra_msg0 : extra_msg1;
|
2023-01-11 14:50:10 +01:00
|
|
|
if (extra_msg) {
|
2022-10-31 05:48:30 +01:00
|
|
|
mbedtls_debug_print_msg(
|
|
|
|
ssl, level, file, line,
|
2023-01-11 14:50:10 +01:00
|
|
|
"%s: %s(%u) extension %s.", ssl_tls13_get_hs_msg_name(hs_msg_type),
|
|
|
|
mbedtls_ssl_get_extension_name(extension_type), extension_type,
|
|
|
|
extra_msg);
|
2022-10-31 05:48:30 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
mbedtls_debug_print_msg(
|
|
|
|
ssl, level, file, line,
|
2023-01-11 14:50:10 +01:00
|
|
|
"%s: %s(%u) extension.", ssl_tls13_get_hs_msg_name(hs_msg_type),
|
|
|
|
mbedtls_ssl_get_extension_name(extension_type), extension_type);
|
2022-10-31 05:48:30 +01:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
void mbedtls_ssl_print_extensions(const mbedtls_ssl_context *ssl,
|
|
|
|
int level, const char *file, int line,
|
|
|
|
int hs_msg_type, uint32_t extensions_mask,
|
|
|
|
const char *extra)
|
2022-10-31 05:48:30 +01:00
|
|
|
{
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
for (unsigned i = 0;
|
|
|
|
i < sizeof(extension_name_table) / sizeof(extension_name_table[0]);
|
|
|
|
i++) {
|
2022-11-08 14:30:21 +01:00
|
|
|
mbedtls_ssl_print_extension(
|
2022-11-08 14:01:17 +01:00
|
|
|
ssl, level, file, line, hs_msg_type, extension_type_table[i],
|
2023-01-11 14:50:10 +01:00
|
|
|
extensions_mask & (1 << i) ? "exists" : "does not exist", extra);
|
2022-10-31 05:48:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-12 07:37:24 +01:00
|
|
|
#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && defined(MBEDTLS_SSL_SESSION_TICKETS)
|
|
|
|
static const char *ticket_flag_name_table[] =
|
|
|
|
{
|
|
|
|
[0] = "ALLOW_PSK_RESUMPTION",
|
|
|
|
[2] = "ALLOW_PSK_EPHEMERAL_RESUMPTION",
|
|
|
|
[3] = "ALLOW_EARLY_DATA",
|
|
|
|
};
|
|
|
|
|
2023-01-16 04:28:49 +01:00
|
|
|
void mbedtls_ssl_print_ticket_flags(const mbedtls_ssl_context *ssl,
|
|
|
|
int level, const char *file, int line,
|
|
|
|
unsigned int flags)
|
2023-01-12 07:37:24 +01:00
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
mbedtls_debug_print_msg(ssl, level, file, line,
|
2023-01-16 04:28:49 +01:00
|
|
|
"print ticket_flags (0x%02x)", flags);
|
|
|
|
|
|
|
|
flags = flags & MBEDTLS_SSL_TLS1_3_TICKET_FLAGS_MASK;
|
2023-01-12 07:37:24 +01:00
|
|
|
|
|
|
|
for (i = 0; i < ARRAY_LENGTH(ticket_flag_name_table); i++) {
|
2023-01-16 04:28:49 +01:00
|
|
|
if ((flags & (1 << i))) {
|
2023-01-12 07:37:24 +01:00
|
|
|
mbedtls_debug_print_msg(ssl, level, file, line, "- %s is set.",
|
|
|
|
ticket_flag_name_table[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif /* MBEDTLS_SSL_PROTO_TLS1_3 && MBEDTLS_SSL_SESSION_TICKETS */
|
|
|
|
|
2022-10-31 05:48:30 +01:00
|
|
|
#endif /* MBEDTLS_DEBUG_C */
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
void mbedtls_ssl_optimize_checksum(mbedtls_ssl_context *ssl,
|
|
|
|
const mbedtls_ssl_ciphersuite_t *ciphersuite_info)
|
2022-02-08 13:29:25 +01:00
|
|
|
{
|
|
|
|
((void) ciphersuite_info);
|
|
|
|
|
2023-03-17 12:50:01 +01:00
|
|
|
#if defined(MBEDTLS_MD_CAN_SHA384)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ciphersuite_info->mac == MBEDTLS_MD_SHA384) {
|
2022-02-08 13:29:25 +01:00
|
|
|
ssl->handshake->update_checksum = ssl_update_checksum_sha384;
|
2023-01-11 14:50:10 +01:00
|
|
|
} else
|
2022-02-08 13:29:25 +01:00
|
|
|
#endif
|
2023-03-17 12:50:01 +01:00
|
|
|
#if defined(MBEDTLS_MD_CAN_SHA256)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ciphersuite_info->mac != MBEDTLS_MD_SHA384) {
|
2022-02-08 13:29:25 +01:00
|
|
|
ssl->handshake->update_checksum = ssl_update_checksum_sha256;
|
2023-01-11 14:50:10 +01:00
|
|
|
} else
|
2022-02-08 13:29:25 +01:00
|
|
|
#endif
|
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
|
2022-02-08 13:29:25 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-06 00:34:21 +01:00
|
|
|
int mbedtls_ssl_add_hs_hdr_to_checksum(mbedtls_ssl_context *ssl,
|
2023-02-06 11:48:19 +01:00
|
|
|
unsigned hs_type,
|
|
|
|
size_t total_hs_len)
|
2022-03-10 18:56:50 +01:00
|
|
|
{
|
|
|
|
unsigned char hs_hdr[4];
|
|
|
|
|
|
|
|
/* Build HS header for checksum update. */
|
2023-01-11 14:50:10 +01:00
|
|
|
hs_hdr[0] = MBEDTLS_BYTE_0(hs_type);
|
|
|
|
hs_hdr[1] = MBEDTLS_BYTE_2(total_hs_len);
|
|
|
|
hs_hdr[2] = MBEDTLS_BYTE_1(total_hs_len);
|
|
|
|
hs_hdr[3] = MBEDTLS_BYTE_0(total_hs_len);
|
2022-03-10 18:56:50 +01:00
|
|
|
|
2023-02-06 00:34:21 +01:00
|
|
|
return ssl->handshake->update_checksum(ssl, hs_hdr, sizeof(hs_hdr));
|
2022-03-10 18:56:50 +01:00
|
|
|
}
|
|
|
|
|
2023-02-06 00:34:21 +01:00
|
|
|
int mbedtls_ssl_add_hs_msg_to_checksum(mbedtls_ssl_context *ssl,
|
2023-02-06 11:48:19 +01:00
|
|
|
unsigned hs_type,
|
|
|
|
unsigned char const *msg,
|
|
|
|
size_t msg_len)
|
2022-03-10 18:56:50 +01:00
|
|
|
{
|
2023-02-06 00:34:21 +01:00
|
|
|
int ret;
|
|
|
|
ret = mbedtls_ssl_add_hs_hdr_to_checksum(ssl, hs_type, msg_len);
|
2023-02-06 11:48:19 +01:00
|
|
|
if (ret != 0) {
|
2023-02-06 00:34:21 +01:00
|
|
|
return ret;
|
2023-02-06 11:48:19 +01:00
|
|
|
}
|
2023-02-06 00:34:21 +01:00
|
|
|
return ssl->handshake->update_checksum(ssl, msg, msg_len);
|
2022-03-10 18:56:50 +01:00
|
|
|
}
|
|
|
|
|
Make handshake hashing functions return int
There are three family of functions: update_checksum, calc_verify,
calc_finished, that perform hashing operations and were returning void
so far. This is not correct, as hashing functions can return errors (for
example, on hardware failure when accelerated). Change them to return
int.
This commit just changes the types: for now the functions always return
0, and their return value is not checked; this will be fixed in the
next few commits.
There is a related function in TLS 1.3,
mbedtls_ssl_reset_transcript_for_hrr, which also handles hashes, and
already returns int but does not correctly check for errors from hashing
functions so far, it will also be handled in the next few commits.
There's a special case with handshake_params_init: _init functions
should return void, so we'll need to split out the part that can return
errors, see the next commit.
Signed-off-by: Manuel Pégourié-Gonnard <manuel.pegourie-gonnard@arm.com>
2023-02-05 09:46:59 +01:00
|
|
|
int mbedtls_ssl_reset_checksum(mbedtls_ssl_context *ssl)
|
2022-02-08 13:29:25 +01:00
|
|
|
{
|
2023-03-17 12:50:01 +01:00
|
|
|
#if defined(MBEDTLS_MD_CAN_SHA256) || \
|
|
|
|
defined(MBEDTLS_MD_CAN_SHA384)
|
2023-02-06 09:54:49 +01:00
|
|
|
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
|
|
|
psa_status_t status;
|
|
|
|
#else
|
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
|
|
|
#endif
|
2023-02-06 22:03:06 +01:00
|
|
|
#else /* SHA-256 or SHA-384 */
|
2022-02-08 13:29:25 +01:00
|
|
|
((void) ssl);
|
2023-02-06 22:03:06 +01:00
|
|
|
#endif /* SHA-256 or SHA-384 */
|
2023-03-17 12:50:01 +01:00
|
|
|
#if defined(MBEDTLS_MD_CAN_SHA256)
|
2022-02-08 13:29:25 +01:00
|
|
|
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
2023-02-06 09:54:49 +01:00
|
|
|
status = psa_hash_abort(&ssl->handshake->fin_sha256_psa);
|
|
|
|
if (status != PSA_SUCCESS) {
|
2023-03-28 12:54:56 +02:00
|
|
|
return mbedtls_md_error_from_psa(status);
|
2023-02-06 09:54:49 +01:00
|
|
|
}
|
|
|
|
status = psa_hash_setup(&ssl->handshake->fin_sha256_psa, PSA_ALG_SHA_256);
|
|
|
|
if (status != PSA_SUCCESS) {
|
2023-03-28 12:54:56 +02:00
|
|
|
return mbedtls_md_error_from_psa(status);
|
2023-02-06 09:54:49 +01:00
|
|
|
}
|
2022-02-08 13:29:25 +01:00
|
|
|
#else
|
2023-03-06 11:59:59 +01:00
|
|
|
mbedtls_md_free(&ssl->handshake->fin_sha256);
|
|
|
|
mbedtls_md_init(&ssl->handshake->fin_sha256);
|
2023-02-24 13:19:17 +01:00
|
|
|
ret = mbedtls_md_setup(&ssl->handshake->fin_sha256,
|
|
|
|
mbedtls_md_info_from_type(MBEDTLS_MD_SHA256),
|
|
|
|
0);
|
|
|
|
if (ret != 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
ret = mbedtls_md_starts(&ssl->handshake->fin_sha256);
|
2023-02-06 09:54:49 +01:00
|
|
|
if (ret != 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
2022-02-08 13:29:25 +01:00
|
|
|
#endif
|
|
|
|
#endif
|
2023-03-17 12:50:01 +01:00
|
|
|
#if defined(MBEDTLS_MD_CAN_SHA384)
|
2022-02-08 13:29:25 +01:00
|
|
|
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
2023-02-06 09:54:49 +01:00
|
|
|
status = psa_hash_abort(&ssl->handshake->fin_sha384_psa);
|
|
|
|
if (status != PSA_SUCCESS) {
|
2023-03-28 12:54:56 +02:00
|
|
|
return mbedtls_md_error_from_psa(status);
|
2023-02-06 09:54:49 +01:00
|
|
|
}
|
|
|
|
status = psa_hash_setup(&ssl->handshake->fin_sha384_psa, PSA_ALG_SHA_384);
|
|
|
|
if (status != PSA_SUCCESS) {
|
2023-03-28 12:54:56 +02:00
|
|
|
return mbedtls_md_error_from_psa(status);
|
2023-02-06 09:54:49 +01:00
|
|
|
}
|
2022-02-08 13:29:25 +01:00
|
|
|
#else
|
2023-03-06 11:59:59 +01:00
|
|
|
mbedtls_md_free(&ssl->handshake->fin_sha384);
|
|
|
|
mbedtls_md_init(&ssl->handshake->fin_sha384);
|
2023-02-24 13:19:17 +01:00
|
|
|
ret = mbedtls_md_setup(&ssl->handshake->fin_sha384,
|
|
|
|
mbedtls_md_info_from_type(MBEDTLS_MD_SHA384), 0);
|
|
|
|
if (ret != 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
ret = mbedtls_md_starts(&ssl->handshake->fin_sha384);
|
2023-02-06 09:54:49 +01:00
|
|
|
if (ret != 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
2022-02-08 13:29:25 +01:00
|
|
|
#endif
|
|
|
|
#endif
|
Make handshake hashing functions return int
There are three family of functions: update_checksum, calc_verify,
calc_finished, that perform hashing operations and were returning void
so far. This is not correct, as hashing functions can return errors (for
example, on hardware failure when accelerated). Change them to return
int.
This commit just changes the types: for now the functions always return
0, and their return value is not checked; this will be fixed in the
next few commits.
There is a related function in TLS 1.3,
mbedtls_ssl_reset_transcript_for_hrr, which also handles hashes, and
already returns int but does not correctly check for errors from hashing
functions so far, it will also be handled in the next few commits.
There's a special case with handshake_params_init: _init functions
should return void, so we'll need to split out the part that can return
errors, see the next commit.
Signed-off-by: Manuel Pégourié-Gonnard <manuel.pegourie-gonnard@arm.com>
2023-02-05 09:46:59 +01:00
|
|
|
return 0;
|
2022-02-08 13:29:25 +01:00
|
|
|
}
|
|
|
|
|
Make handshake hashing functions return int
There are three family of functions: update_checksum, calc_verify,
calc_finished, that perform hashing operations and were returning void
so far. This is not correct, as hashing functions can return errors (for
example, on hardware failure when accelerated). Change them to return
int.
This commit just changes the types: for now the functions always return
0, and their return value is not checked; this will be fixed in the
next few commits.
There is a related function in TLS 1.3,
mbedtls_ssl_reset_transcript_for_hrr, which also handles hashes, and
already returns int but does not correctly check for errors from hashing
functions so far, it will also be handled in the next few commits.
There's a special case with handshake_params_init: _init functions
should return void, so we'll need to split out the part that can return
errors, see the next commit.
Signed-off-by: Manuel Pégourié-Gonnard <manuel.pegourie-gonnard@arm.com>
2023-02-05 09:46:59 +01:00
|
|
|
static int ssl_update_checksum_start(mbedtls_ssl_context *ssl,
|
2023-02-06 11:48:19 +01:00
|
|
|
const unsigned char *buf, size_t len)
|
2022-02-08 13:29:25 +01:00
|
|
|
{
|
2023-03-17 12:50:01 +01:00
|
|
|
#if defined(MBEDTLS_MD_CAN_SHA256) || \
|
|
|
|
defined(MBEDTLS_MD_CAN_SHA384)
|
2023-02-06 10:00:52 +01:00
|
|
|
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
|
|
|
psa_status_t status;
|
|
|
|
#else
|
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
|
|
|
#endif
|
2023-02-06 22:03:06 +01:00
|
|
|
#else /* SHA-256 or SHA-384 */
|
|
|
|
((void) ssl);
|
|
|
|
(void) buf;
|
|
|
|
(void) len;
|
|
|
|
#endif /* SHA-256 or SHA-384 */
|
2023-03-17 12:50:01 +01:00
|
|
|
#if defined(MBEDTLS_MD_CAN_SHA256)
|
2022-02-08 13:29:25 +01:00
|
|
|
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
2023-02-06 10:00:52 +01:00
|
|
|
status = psa_hash_update(&ssl->handshake->fin_sha256_psa, buf, len);
|
|
|
|
if (status != PSA_SUCCESS) {
|
2023-03-28 12:54:56 +02:00
|
|
|
return mbedtls_md_error_from_psa(status);
|
2023-02-06 10:00:52 +01:00
|
|
|
}
|
2022-02-08 13:29:25 +01:00
|
|
|
#else
|
2023-02-24 13:19:17 +01:00
|
|
|
ret = mbedtls_md_update(&ssl->handshake->fin_sha256, buf, len);
|
2023-02-06 10:00:52 +01:00
|
|
|
if (ret != 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
2022-02-08 13:29:25 +01:00
|
|
|
#endif
|
|
|
|
#endif
|
2023-03-17 12:50:01 +01:00
|
|
|
#if defined(MBEDTLS_MD_CAN_SHA384)
|
2022-02-08 13:29:25 +01:00
|
|
|
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
2023-02-06 10:00:52 +01:00
|
|
|
status = psa_hash_update(&ssl->handshake->fin_sha384_psa, buf, len);
|
|
|
|
if (status != PSA_SUCCESS) {
|
2023-03-28 12:54:56 +02:00
|
|
|
return mbedtls_md_error_from_psa(status);
|
2023-02-06 10:00:52 +01:00
|
|
|
}
|
2022-02-08 13:29:25 +01:00
|
|
|
#else
|
2023-02-24 13:19:17 +01:00
|
|
|
ret = mbedtls_md_update(&ssl->handshake->fin_sha384, buf, len);
|
2023-02-06 10:00:52 +01:00
|
|
|
if (ret != 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
2022-02-08 13:29:25 +01:00
|
|
|
#endif
|
2022-10-17 13:52:51 +02:00
|
|
|
#endif
|
Make handshake hashing functions return int
There are three family of functions: update_checksum, calc_verify,
calc_finished, that perform hashing operations and were returning void
so far. This is not correct, as hashing functions can return errors (for
example, on hardware failure when accelerated). Change them to return
int.
This commit just changes the types: for now the functions always return
0, and their return value is not checked; this will be fixed in the
next few commits.
There is a related function in TLS 1.3,
mbedtls_ssl_reset_transcript_for_hrr, which also handles hashes, and
already returns int but does not correctly check for errors from hashing
functions so far, it will also be handled in the next few commits.
There's a special case with handshake_params_init: _init functions
should return void, so we'll need to split out the part that can return
errors, see the next commit.
Signed-off-by: Manuel Pégourié-Gonnard <manuel.pegourie-gonnard@arm.com>
2023-02-05 09:46:59 +01:00
|
|
|
return 0;
|
2022-02-08 13:29:25 +01:00
|
|
|
}
|
|
|
|
|
2023-03-17 12:50:01 +01:00
|
|
|
#if defined(MBEDTLS_MD_CAN_SHA256)
|
Make handshake hashing functions return int
There are three family of functions: update_checksum, calc_verify,
calc_finished, that perform hashing operations and were returning void
so far. This is not correct, as hashing functions can return errors (for
example, on hardware failure when accelerated). Change them to return
int.
This commit just changes the types: for now the functions always return
0, and their return value is not checked; this will be fixed in the
next few commits.
There is a related function in TLS 1.3,
mbedtls_ssl_reset_transcript_for_hrr, which also handles hashes, and
already returns int but does not correctly check for errors from hashing
functions so far, it will also be handled in the next few commits.
There's a special case with handshake_params_init: _init functions
should return void, so we'll need to split out the part that can return
errors, see the next commit.
Signed-off-by: Manuel Pégourié-Gonnard <manuel.pegourie-gonnard@arm.com>
2023-02-05 09:46:59 +01:00
|
|
|
static int ssl_update_checksum_sha256(mbedtls_ssl_context *ssl,
|
2023-02-06 11:48:19 +01:00
|
|
|
const unsigned char *buf, size_t len)
|
2022-02-08 13:29:25 +01:00
|
|
|
{
|
|
|
|
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
2023-03-28 12:54:56 +02:00
|
|
|
return mbedtls_md_error_from_psa(psa_hash_update(
|
|
|
|
&ssl->handshake->fin_sha256_psa, buf, len));
|
2022-02-08 13:29:25 +01:00
|
|
|
#else
|
2023-02-24 13:19:17 +01:00
|
|
|
return mbedtls_md_update(&ssl->handshake->fin_sha256, buf, len);
|
2022-02-08 13:29:25 +01:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2023-03-17 12:50:01 +01:00
|
|
|
#if defined(MBEDTLS_MD_CAN_SHA384)
|
Make handshake hashing functions return int
There are three family of functions: update_checksum, calc_verify,
calc_finished, that perform hashing operations and were returning void
so far. This is not correct, as hashing functions can return errors (for
example, on hardware failure when accelerated). Change them to return
int.
This commit just changes the types: for now the functions always return
0, and their return value is not checked; this will be fixed in the
next few commits.
There is a related function in TLS 1.3,
mbedtls_ssl_reset_transcript_for_hrr, which also handles hashes, and
already returns int but does not correctly check for errors from hashing
functions so far, it will also be handled in the next few commits.
There's a special case with handshake_params_init: _init functions
should return void, so we'll need to split out the part that can return
errors, see the next commit.
Signed-off-by: Manuel Pégourié-Gonnard <manuel.pegourie-gonnard@arm.com>
2023-02-05 09:46:59 +01:00
|
|
|
static int ssl_update_checksum_sha384(mbedtls_ssl_context *ssl,
|
2023-02-06 11:48:19 +01:00
|
|
|
const unsigned char *buf, size_t len)
|
2022-02-08 13:29:25 +01:00
|
|
|
{
|
|
|
|
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
2023-03-28 12:54:56 +02:00
|
|
|
return mbedtls_md_error_from_psa(psa_hash_update(
|
|
|
|
&ssl->handshake->fin_sha384_psa, buf, len));
|
2022-02-08 13:29:25 +01:00
|
|
|
#else
|
2023-02-24 13:19:17 +01:00
|
|
|
return mbedtls_md_update(&ssl->handshake->fin_sha384, buf, len);
|
2022-02-08 13:29:25 +01:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
static void ssl_handshake_params_init(mbedtls_ssl_handshake_params *handshake)
|
2022-02-17 07:23:47 +01:00
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
memset(handshake, 0, sizeof(mbedtls_ssl_handshake_params));
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2023-03-17 12:50:01 +01:00
|
|
|
#if defined(MBEDTLS_MD_CAN_SHA256)
|
2022-02-17 07:23:47 +01:00
|
|
|
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
|
|
|
handshake->fin_sha256_psa = psa_hash_operation_init();
|
|
|
|
#else
|
2023-02-24 13:19:17 +01:00
|
|
|
mbedtls_md_init(&handshake->fin_sha256);
|
2022-02-17 07:23:47 +01:00
|
|
|
#endif
|
|
|
|
#endif
|
2023-03-17 12:50:01 +01:00
|
|
|
#if defined(MBEDTLS_MD_CAN_SHA384)
|
2022-02-17 07:23:47 +01:00
|
|
|
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
|
|
|
handshake->fin_sha384_psa = psa_hash_operation_init();
|
|
|
|
#else
|
2023-02-24 13:19:17 +01:00
|
|
|
mbedtls_md_init(&handshake->fin_sha384);
|
2022-02-17 07:23:47 +01:00
|
|
|
#endif
|
|
|
|
#endif
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2022-02-17 07:23:47 +01:00
|
|
|
handshake->update_checksum = ssl_update_checksum_start;
|
2013-01-03 14:52:21 +01:00
|
|
|
|
2022-02-17 07:23:47 +01:00
|
|
|
#if defined(MBEDTLS_DHM_C)
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_dhm_init(&handshake->dhm_ctx);
|
2022-02-17 07:23:47 +01:00
|
|
|
#endif
|
2023-07-05 18:57:21 +02:00
|
|
|
#if !defined(MBEDTLS_USE_PSA_CRYPTO) && \
|
2023-07-07 17:04:24 +02:00
|
|
|
defined(MBEDTLS_KEY_EXCHANGE_SOME_ECDH_OR_ECDHE_1_2_ENABLED)
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_ecdh_init(&handshake->ecdh_ctx);
|
2022-02-17 07:23:47 +01:00
|
|
|
#endif
|
|
|
|
#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
|
2022-05-31 14:43:23 +02:00
|
|
|
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
|
|
|
handshake->psa_pake_ctx = psa_pake_operation_init();
|
|
|
|
handshake->psa_pake_password = MBEDTLS_SVC_KEY_ID_INIT;
|
|
|
|
#else
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_ecjpake_init(&handshake->ecjpake_ctx);
|
2022-05-31 14:43:23 +02:00
|
|
|
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
2022-02-17 07:23:47 +01:00
|
|
|
#if defined(MBEDTLS_SSL_CLI_C)
|
|
|
|
handshake->ecjpake_cache = NULL;
|
|
|
|
handshake->ecjpake_cache_len = 0;
|
|
|
|
#endif
|
|
|
|
#endif
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2022-02-17 07:23:47 +01:00
|
|
|
#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_x509_crt_restart_init(&handshake->ecrs_ctx);
|
2022-02-17 07:23:47 +01:00
|
|
|
#endif
|
2017-12-27 22:28:58 +01:00
|
|
|
|
2022-02-17 07:23:47 +01:00
|
|
|
#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
|
|
|
|
handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
|
|
|
|
#endif
|
2017-12-27 22:28:58 +01:00
|
|
|
|
2022-02-17 07:23:47 +01:00
|
|
|
#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
|
|
|
|
!defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_pk_init(&handshake->peer_pubkey);
|
2022-02-17 07:23:47 +01:00
|
|
|
#endif
|
2020-02-05 15:44:10 +01:00
|
|
|
}
|
2014-10-29 12:07:57 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
void mbedtls_ssl_transform_init(mbedtls_ssl_transform *transform)
|
2020-02-05 15:44:10 +01:00
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
memset(transform, 0, sizeof(mbedtls_ssl_transform));
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2022-02-17 07:23:47 +01:00
|
|
|
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
|
|
|
transform->psa_key_enc = MBEDTLS_SVC_KEY_ID_INIT;
|
|
|
|
transform->psa_key_dec = MBEDTLS_SVC_KEY_ID_INIT;
|
2020-02-05 15:44:10 +01:00
|
|
|
#else
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_cipher_init(&transform->cipher_ctx_enc);
|
|
|
|
mbedtls_cipher_init(&transform->cipher_ctx_dec);
|
2022-02-17 07:23:47 +01:00
|
|
|
#endif
|
2018-06-28 10:38:35 +02:00
|
|
|
|
2022-02-17 07:23:47 +01:00
|
|
|
#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
|
2022-02-23 09:24:45 +01:00
|
|
|
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
|
|
|
transform->psa_mac_enc = MBEDTLS_SVC_KEY_ID_INIT;
|
|
|
|
transform->psa_mac_dec = MBEDTLS_SVC_KEY_ID_INIT;
|
2022-02-24 11:17:45 +01:00
|
|
|
#else
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_md_init(&transform->md_ctx_enc);
|
|
|
|
mbedtls_md_init(&transform->md_ctx_dec);
|
2022-02-17 07:23:47 +01:00
|
|
|
#endif
|
2022-02-24 11:17:45 +01:00
|
|
|
#endif
|
2020-02-05 15:44:10 +01:00
|
|
|
}
|
2017-12-27 22:28:58 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
void mbedtls_ssl_session_init(mbedtls_ssl_session *session)
|
2020-02-05 15:44:10 +01:00
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
memset(session, 0, sizeof(mbedtls_ssl_session));
|
2022-02-17 07:23:47 +01:00
|
|
|
}
|
2013-02-27 14:48:00 +01:00
|
|
|
|
2022-06-17 10:52:54 +02:00
|
|
|
MBEDTLS_CHECK_RETURN_CRITICAL
|
2023-01-11 14:50:10 +01:00
|
|
|
static int ssl_handshake_init(mbedtls_ssl_context *ssl)
|
2022-02-17 07:23:47 +01:00
|
|
|
{
|
2023-02-06 00:34:21 +01:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
|
|
|
|
2022-02-17 07:23:47 +01:00
|
|
|
/* Clear old handshake information if present */
|
2022-12-01 11:57:19 +01:00
|
|
|
#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->transform_negotiate) {
|
|
|
|
mbedtls_ssl_transform_free(ssl->transform_negotiate);
|
|
|
|
}
|
2022-12-01 11:57:19 +01:00
|
|
|
#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->session_negotiate) {
|
|
|
|
mbedtls_ssl_session_free(ssl->session_negotiate);
|
|
|
|
}
|
|
|
|
if (ssl->handshake) {
|
|
|
|
mbedtls_ssl_handshake_free(ssl);
|
|
|
|
}
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2022-12-01 11:57:19 +01:00
|
|
|
#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
|
2022-02-17 07:23:47 +01:00
|
|
|
/*
|
|
|
|
* Either the pointers are now NULL or cleared properly and can be freed.
|
|
|
|
* Now allocate missing structures.
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->transform_negotiate == NULL) {
|
|
|
|
ssl->transform_negotiate = mbedtls_calloc(1, sizeof(mbedtls_ssl_transform));
|
2022-02-17 07:23:47 +01:00
|
|
|
}
|
2022-12-01 11:57:19 +01:00
|
|
|
#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->session_negotiate == NULL) {
|
|
|
|
ssl->session_negotiate = mbedtls_calloc(1, sizeof(mbedtls_ssl_session));
|
2020-02-05 15:44:10 +01:00
|
|
|
}
|
2022-02-17 07:23:47 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->handshake == NULL) {
|
|
|
|
ssl->handshake = mbedtls_calloc(1, sizeof(mbedtls_ssl_handshake_params));
|
2013-10-25 19:31:25 +02:00
|
|
|
}
|
2022-02-17 07:23:47 +01:00
|
|
|
#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
|
|
|
|
/* If the buffers are too small - reallocate */
|
2018-10-17 15:43:14 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
handle_buffer_resizing(ssl, 0, MBEDTLS_SSL_IN_BUFFER_LEN,
|
|
|
|
MBEDTLS_SSL_OUT_BUFFER_LEN);
|
2022-02-17 07:23:47 +01:00
|
|
|
#endif
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2022-02-17 07:23:47 +01:00
|
|
|
/* All pointers should exist and can be directly freed without issue */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->handshake == NULL ||
|
2022-12-01 11:57:19 +01:00
|
|
|
#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
|
2022-02-17 07:23:47 +01:00
|
|
|
ssl->transform_negotiate == NULL ||
|
2022-12-01 11:57:19 +01:00
|
|
|
#endif
|
2023-01-11 14:50:10 +01:00
|
|
|
ssl->session_negotiate == NULL) {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(1, ("alloc() of ssl sub-contexts failed"));
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_free(ssl->handshake);
|
2022-02-17 07:23:47 +01:00
|
|
|
ssl->handshake = NULL;
|
2022-12-01 11:57:19 +01:00
|
|
|
|
|
|
|
#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_free(ssl->transform_negotiate);
|
2022-02-17 07:23:47 +01:00
|
|
|
ssl->transform_negotiate = NULL;
|
2022-12-01 11:57:19 +01:00
|
|
|
#endif
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_free(ssl->session_negotiate);
|
2022-02-17 07:23:47 +01:00
|
|
|
ssl->session_negotiate = NULL;
|
2014-10-28 13:08:59 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return MBEDTLS_ERR_SSL_ALLOC_FAILED;
|
2020-02-05 15:44:10 +01:00
|
|
|
}
|
2012-07-03 15:30:23 +02:00
|
|
|
|
2023-11-15 09:13:47 +01:00
|
|
|
#if defined(MBEDTLS_SSL_EARLY_DATA)
|
|
|
|
#if defined(MBEDTLS_SSL_CLI_C)
|
2024-01-22 15:24:21 +01:00
|
|
|
ssl->early_data_status = MBEDTLS_SSL_EARLY_DATA_STATUS_UNKNOWN;
|
2024-01-05 14:20:35 +01:00
|
|
|
#endif
|
2023-11-15 09:13:47 +01:00
|
|
|
#if defined(MBEDTLS_SSL_SRV_C)
|
|
|
|
ssl->discard_early_data_record = MBEDTLS_SSL_EARLY_DATA_NO_DISCARD;
|
|
|
|
#endif
|
|
|
|
#endif /* MBEDTLS_SSL_EARLY_DATA */
|
2024-01-05 14:20:35 +01:00
|
|
|
|
2022-02-17 07:23:47 +01:00
|
|
|
/* Initialize structures */
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_ssl_session_init(ssl->session_negotiate);
|
|
|
|
ssl_handshake_params_init(ssl->handshake);
|
2013-06-30 14:57:46 +02:00
|
|
|
|
2022-12-01 11:57:19 +01:00
|
|
|
#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_ssl_transform_init(ssl->transform_negotiate);
|
2022-12-01 11:57:19 +01:00
|
|
|
#endif
|
|
|
|
|
2023-02-05 10:17:45 +01:00
|
|
|
/* Setup handshake checksums */
|
2023-02-06 00:34:21 +01:00
|
|
|
ret = mbedtls_ssl_reset_checksum(ssl);
|
|
|
|
if (ret != 0) {
|
|
|
|
MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_reset_checksum", ret);
|
|
|
|
return ret;
|
|
|
|
}
|
2023-02-05 10:17:45 +01:00
|
|
|
|
2022-09-22 04:46:57 +02:00
|
|
|
#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
|
|
|
|
defined(MBEDTLS_SSL_SRV_C) && \
|
|
|
|
defined(MBEDTLS_SSL_SESSION_TICKETS)
|
|
|
|
ssl->handshake->new_session_tickets_count =
|
2023-01-11 14:50:10 +01:00
|
|
|
ssl->conf->new_session_tickets_count;
|
2022-09-22 04:46:57 +02:00
|
|
|
#endif
|
|
|
|
|
2022-02-17 07:23:47 +01:00
|
|
|
#if defined(MBEDTLS_SSL_PROTO_DTLS)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
|
2022-02-17 07:23:47 +01:00
|
|
|
ssl->handshake->alt_transform_out = ssl->transform_out;
|
2012-07-03 15:30:23 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
|
2022-02-17 07:23:47 +01:00
|
|
|
ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
|
2023-01-11 14:50:10 +01:00
|
|
|
} else {
|
2022-02-17 07:23:47 +01:00
|
|
|
ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2012-07-03 15:30:23 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_ssl_set_timer(ssl, 0);
|
2020-02-05 15:44:10 +01:00
|
|
|
}
|
2022-02-17 07:23:47 +01:00
|
|
|
#endif
|
2012-07-03 15:30:23 +02:00
|
|
|
|
2022-02-17 07:23:47 +01:00
|
|
|
/*
|
|
|
|
* curve_list is translated to IANA TLS group identifiers here because
|
|
|
|
* mbedtls_ssl_conf_curves returns void and so can't return
|
|
|
|
* any error codes.
|
|
|
|
*/
|
2023-09-22 11:53:41 +02:00
|
|
|
#if defined(MBEDTLS_ECP_C)
|
2022-02-17 07:23:47 +01:00
|
|
|
#if !defined(MBEDTLS_DEPRECATED_REMOVED)
|
|
|
|
/* Heap allocate and translate curve_list from internal to IANA group ids */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->conf->curve_list != NULL) {
|
2022-02-17 07:23:47 +01:00
|
|
|
size_t length;
|
|
|
|
const mbedtls_ecp_group_id *curve_list = ssl->conf->curve_list;
|
2012-07-03 15:30:23 +02:00
|
|
|
|
2023-05-22 13:05:03 +02:00
|
|
|
for (length = 0; (curve_list[length] != MBEDTLS_ECP_DP_NONE); length++) {
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2012-07-03 15:30:23 +02:00
|
|
|
|
2022-02-17 07:23:47 +01:00
|
|
|
/* Leave room for zero termination */
|
2023-01-11 14:50:10 +01:00
|
|
|
uint16_t *group_list = mbedtls_calloc(length + 1, sizeof(uint16_t));
|
|
|
|
if (group_list == NULL) {
|
|
|
|
return MBEDTLS_ERR_SSL_ALLOC_FAILED;
|
|
|
|
}
|
2012-07-03 15:30:23 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
for (size_t i = 0; i < length; i++) {
|
2022-12-30 17:44:24 +01:00
|
|
|
uint16_t tls_id = mbedtls_ssl_get_tls_id_from_ecp_group_id(
|
2023-01-11 14:50:10 +01:00
|
|
|
curve_list[i]);
|
|
|
|
if (tls_id == 0) {
|
|
|
|
mbedtls_free(group_list);
|
|
|
|
return MBEDTLS_ERR_SSL_BAD_CONFIG;
|
2022-02-17 07:23:47 +01:00
|
|
|
}
|
2022-12-30 17:44:24 +01:00
|
|
|
group_list[i] = tls_id;
|
2022-02-17 07:23:47 +01:00
|
|
|
}
|
2012-07-03 15:30:23 +02:00
|
|
|
|
2022-02-17 07:23:47 +01:00
|
|
|
group_list[length] = 0;
|
2012-07-03 15:30:23 +02:00
|
|
|
|
2022-02-17 07:23:47 +01:00
|
|
|
ssl->handshake->group_list = group_list;
|
|
|
|
ssl->handshake->group_list_heap_allocated = 1;
|
2023-01-11 14:50:10 +01:00
|
|
|
} else {
|
2022-02-17 07:23:47 +01:00
|
|
|
ssl->handshake->group_list = ssl->conf->group_list;
|
|
|
|
ssl->handshake->group_list_heap_allocated = 0;
|
2020-02-05 15:44:10 +01:00
|
|
|
}
|
2022-02-17 07:23:47 +01:00
|
|
|
#endif /* MBEDTLS_DEPRECATED_REMOVED */
|
2023-09-22 11:53:41 +02:00
|
|
|
#endif /* MBEDTLS_ECP_C */
|
2012-07-03 15:30:23 +02:00
|
|
|
|
2022-10-05 12:46:29 +02:00
|
|
|
#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
|
2022-02-17 07:23:47 +01:00
|
|
|
#if !defined(MBEDTLS_DEPRECATED_REMOVED)
|
|
|
|
#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
|
|
|
|
/* Heap allocate and translate sig_hashes from internal hash identifiers to
|
|
|
|
signature algorithms IANA identifiers. */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (mbedtls_ssl_conf_is_tls12_only(ssl->conf) &&
|
|
|
|
ssl->conf->sig_hashes != NULL) {
|
2022-02-17 07:23:47 +01:00
|
|
|
const int *md;
|
|
|
|
const int *sig_hashes = ssl->conf->sig_hashes;
|
|
|
|
size_t sig_algs_len = 0;
|
|
|
|
uint16_t *p;
|
2012-07-03 15:30:23 +02:00
|
|
|
|
2023-03-08 15:19:51 +01:00
|
|
|
MBEDTLS_STATIC_ASSERT(MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN
|
|
|
|
<= (SIZE_MAX - (2 * sizeof(uint16_t))),
|
|
|
|
"MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN too big");
|
2013-06-30 14:57:46 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
for (md = sig_hashes; *md != MBEDTLS_MD_NONE; md++) {
|
|
|
|
if (mbedtls_ssl_hash_from_md_alg(*md) == MBEDTLS_SSL_HASH_NONE) {
|
2022-02-17 07:23:47 +01:00
|
|
|
continue;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2023-08-02 20:02:28 +02:00
|
|
|
#if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ALLOWED_ENABLED)
|
2023-01-11 14:50:10 +01:00
|
|
|
sig_algs_len += sizeof(uint16_t);
|
2022-02-17 07:23:47 +01:00
|
|
|
#endif
|
2012-07-03 15:30:23 +02:00
|
|
|
|
2022-02-17 07:23:47 +01:00
|
|
|
#if defined(MBEDTLS_RSA_C)
|
2023-01-11 14:50:10 +01:00
|
|
|
sig_algs_len += sizeof(uint16_t);
|
2022-02-17 07:23:47 +01:00
|
|
|
#endif
|
2023-01-11 14:50:10 +01:00
|
|
|
if (sig_algs_len > MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN) {
|
|
|
|
return MBEDTLS_ERR_SSL_BAD_CONFIG;
|
|
|
|
}
|
2022-02-17 07:23:47 +01:00
|
|
|
}
|
2012-07-03 15:30:23 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (sig_algs_len < MBEDTLS_SSL_MIN_SIG_ALG_LIST_LEN) {
|
|
|
|
return MBEDTLS_ERR_SSL_BAD_CONFIG;
|
|
|
|
}
|
2012-07-03 15:30:23 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
ssl->handshake->sig_algs = mbedtls_calloc(1, sig_algs_len +
|
|
|
|
sizeof(uint16_t));
|
|
|
|
if (ssl->handshake->sig_algs == NULL) {
|
|
|
|
return MBEDTLS_ERR_SSL_ALLOC_FAILED;
|
|
|
|
}
|
2012-07-03 15:30:23 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
p = (uint16_t *) ssl->handshake->sig_algs;
|
|
|
|
for (md = sig_hashes; *md != MBEDTLS_MD_NONE; md++) {
|
|
|
|
unsigned char hash = mbedtls_ssl_hash_from_md_alg(*md);
|
|
|
|
if (hash == MBEDTLS_SSL_HASH_NONE) {
|
2022-02-17 07:23:47 +01:00
|
|
|
continue;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2023-08-02 20:02:28 +02:00
|
|
|
#if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ALLOWED_ENABLED)
|
2023-01-11 14:50:10 +01:00
|
|
|
*p = ((hash << 8) | MBEDTLS_SSL_SIG_ECDSA);
|
2022-02-17 07:23:47 +01:00
|
|
|
p++;
|
|
|
|
#endif
|
|
|
|
#if defined(MBEDTLS_RSA_C)
|
2023-01-11 14:50:10 +01:00
|
|
|
*p = ((hash << 8) | MBEDTLS_SSL_SIG_RSA);
|
2022-02-17 07:23:47 +01:00
|
|
|
p++;
|
|
|
|
#endif
|
|
|
|
}
|
2022-05-09 16:37:58 +02:00
|
|
|
*p = MBEDTLS_TLS_SIG_NONE;
|
2022-02-17 07:23:47 +01:00
|
|
|
ssl->handshake->sig_algs_heap_allocated = 1;
|
2023-01-11 14:50:10 +01:00
|
|
|
} else
|
2022-02-17 07:23:47 +01:00
|
|
|
#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
|
2012-07-03 15:30:23 +02:00
|
|
|
{
|
2022-02-17 07:23:47 +01:00
|
|
|
ssl->handshake->sig_algs_heap_allocated = 0;
|
2012-07-03 15:30:23 +02:00
|
|
|
}
|
2022-06-27 10:27:35 +02:00
|
|
|
#endif /* !MBEDTLS_DEPRECATED_REMOVED */
|
2022-10-05 12:46:29 +02:00
|
|
|
#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2012-07-03 15:30:23 +02:00
|
|
|
}
|
2014-10-15 15:07:45 +02:00
|
|
|
|
2022-02-17 07:23:47 +01:00
|
|
|
#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
|
|
|
|
/* Dummy cookie callbacks for defaults */
|
2022-06-17 10:52:54 +02:00
|
|
|
MBEDTLS_CHECK_RETURN_CRITICAL
|
2023-01-11 14:50:10 +01:00
|
|
|
static int ssl_cookie_write_dummy(void *ctx,
|
|
|
|
unsigned char **p, unsigned char *end,
|
|
|
|
const unsigned char *cli_id, size_t cli_id_len)
|
2014-10-15 15:07:45 +02:00
|
|
|
{
|
2022-02-17 07:23:47 +01:00
|
|
|
((void) ctx);
|
|
|
|
((void) p);
|
|
|
|
((void) end);
|
|
|
|
((void) cli_id);
|
|
|
|
((void) cli_id_len);
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
|
2022-02-17 07:23:47 +01:00
|
|
|
}
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2022-06-17 10:52:54 +02:00
|
|
|
MBEDTLS_CHECK_RETURN_CRITICAL
|
2023-01-11 14:50: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)
|
2022-02-17 07:23:47 +01:00
|
|
|
{
|
|
|
|
((void) ctx);
|
|
|
|
((void) cookie);
|
|
|
|
((void) cookie_len);
|
|
|
|
((void) cli_id);
|
|
|
|
((void) cli_id_len);
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
|
2022-02-17 07:23:47 +01:00
|
|
|
}
|
|
|
|
#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Initialize an SSL context
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
void mbedtls_ssl_init(mbedtls_ssl_context *ssl)
|
2022-02-17 07:23:47 +01:00
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
memset(ssl, 0, sizeof(mbedtls_ssl_context));
|
2022-02-17 07:23:47 +01:00
|
|
|
}
|
|
|
|
|
2022-06-17 10:52:54 +02:00
|
|
|
MBEDTLS_CHECK_RETURN_CRITICAL
|
2023-01-11 14:50:10 +01:00
|
|
|
static int ssl_conf_version_check(const mbedtls_ssl_context *ssl)
|
2022-02-17 07:23:47 +01:00
|
|
|
{
|
2022-03-15 15:18:51 +01:00
|
|
|
const mbedtls_ssl_config *conf = ssl->conf;
|
|
|
|
|
2022-02-17 07:23:47 +01:00
|
|
|
#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (mbedtls_ssl_conf_is_tls13_only(conf)) {
|
|
|
|
if (conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(1, ("DTLS 1.3 is not yet supported."));
|
|
|
|
return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
|
2022-04-13 10:21:05 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(4, ("The SSL configuration is tls13 only."));
|
|
|
|
return 0;
|
2014-09-17 11:34:57 +02:00
|
|
|
}
|
2022-02-17 07:23:47 +01:00
|
|
|
#endif
|
2014-09-17 11:34:57 +02:00
|
|
|
|
2022-02-17 07:23:47 +01:00
|
|
|
#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (mbedtls_ssl_conf_is_tls12_only(conf)) {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(4, ("The SSL configuration is tls12 only."));
|
|
|
|
return 0;
|
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
|
|
|
|
2022-02-17 07:23:47 +01:00
|
|
|
#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (mbedtls_ssl_conf_is_hybrid_tls12_tls13(conf)) {
|
|
|
|
if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(1, ("DTLS not yet supported in Hybrid TLS 1.3 + TLS 1.2"));
|
|
|
|
return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
|
2022-04-13 10:21:05 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(4, ("The SSL configuration is TLS 1.3 or TLS 1.2."));
|
|
|
|
return 0;
|
2020-02-05 15:44:10 +01:00
|
|
|
}
|
2022-02-17 07:23:47 +01:00
|
|
|
#endif
|
2014-03-24 13:13:01 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(1, ("The SSL configuration is invalid."));
|
|
|
|
return MBEDTLS_ERR_SSL_BAD_CONFIG;
|
2022-02-17 07:23:47 +01:00
|
|
|
}
|
2014-03-24 13:13:01 +01:00
|
|
|
|
2022-06-17 10:52:54 +02:00
|
|
|
MBEDTLS_CHECK_RETURN_CRITICAL
|
2022-02-17 07:23:47 +01:00
|
|
|
static int ssl_conf_check(const mbedtls_ssl_context *ssl)
|
|
|
|
{
|
|
|
|
int ret;
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = ssl_conf_version_check(ssl);
|
|
|
|
if (ret != 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2022-10-30 07:13:19 +01:00
|
|
|
#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
|
|
|
|
/* RFC 8446 section 4.4.3
|
|
|
|
*
|
|
|
|
* If the verification fails, the receiver MUST terminate the handshake with
|
|
|
|
* a "decrypt_error" alert.
|
|
|
|
*
|
|
|
|
* If the client is configured as TLS 1.3 only with optional verify, return
|
|
|
|
* bad config.
|
|
|
|
*
|
|
|
|
*/
|
2023-11-14 04:03:32 +01:00
|
|
|
if (mbedtls_ssl_conf_tls13_is_ephemeral_enabled(
|
2023-01-11 14:50:10 +01:00
|
|
|
(mbedtls_ssl_context *) ssl) &&
|
2022-10-30 07:13:19 +01:00
|
|
|
ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
|
|
|
|
ssl->conf->max_tls_version == MBEDTLS_SSL_VERSION_TLS1_3 &&
|
|
|
|
ssl->conf->min_tls_version == MBEDTLS_SSL_VERSION_TLS1_3 &&
|
2023-01-11 14:50:10 +01:00
|
|
|
ssl->conf->authmode == MBEDTLS_SSL_VERIFY_OPTIONAL) {
|
2022-10-30 07:13:19 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(
|
2023-01-11 14:50:10 +01:00
|
|
|
1, ("Optional verify auth mode "
|
|
|
|
"is not available for TLS 1.3 client"));
|
|
|
|
return MBEDTLS_ERR_SSL_BAD_CONFIG;
|
2022-10-30 07:13:19 +01:00
|
|
|
}
|
|
|
|
#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
|
|
|
|
|
2022-02-17 07:23:47 +01:00
|
|
|
/* Space for further checks */
|
2011-05-18 15:32:51 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2022-02-17 07:23:47 +01:00
|
|
|
}
|
2014-10-02 16:51:56 +02:00
|
|
|
|
2022-02-17 07:23:47 +01:00
|
|
|
/*
|
|
|
|
* Setup an SSL context
|
|
|
|
*/
|
2014-09-19 21:18:23 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_ssl_setup(mbedtls_ssl_context *ssl,
|
|
|
|
const mbedtls_ssl_config *conf)
|
2022-02-17 07:23:47 +01:00
|
|
|
{
|
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
|
|
|
size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
|
|
|
|
size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
|
2014-09-30 22:21:31 +02:00
|
|
|
|
2022-02-17 07:23:47 +01:00
|
|
|
ssl->conf = conf;
|
2014-10-01 18:29:03 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if ((ret = ssl_conf_check(ssl)) != 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
2023-03-08 15:30:43 +01:00
|
|
|
ssl->tls_version = ssl->conf->max_tls_version;
|
2014-10-15 13:52:48 +02:00
|
|
|
|
2022-02-17 07:23:47 +01:00
|
|
|
/*
|
|
|
|
* Prepare base structures
|
|
|
|
*/
|
2014-09-19 21:18:23 +02:00
|
|
|
|
2022-02-17 07:23:47 +01:00
|
|
|
/* Set to NULL in case of an error condition */
|
|
|
|
ssl->out_buf = NULL;
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2022-02-17 07:23:47 +01:00
|
|
|
#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
|
|
|
|
ssl->in_buf_len = in_buf_len;
|
|
|
|
#endif
|
2023-01-11 14:50:10 +01:00
|
|
|
ssl->in_buf = mbedtls_calloc(1, in_buf_len);
|
|
|
|
if (ssl->in_buf == NULL) {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(1, ("alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed", in_buf_len));
|
2022-02-17 07:23:47 +01:00
|
|
|
ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
|
|
|
|
goto error;
|
|
|
|
}
|
2015-05-13 16:22:05 +02:00
|
|
|
|
2022-02-17 07:23:47 +01:00
|
|
|
#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
|
|
|
|
ssl->out_buf_len = out_buf_len;
|
|
|
|
#endif
|
2023-01-11 14:50:10 +01:00
|
|
|
ssl->out_buf = mbedtls_calloc(1, out_buf_len);
|
|
|
|
if (ssl->out_buf == NULL) {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(1, ("alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed", out_buf_len));
|
2022-02-17 07:23:47 +01:00
|
|
|
ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
|
|
|
|
goto error;
|
|
|
|
}
|
2014-03-24 13:13:01 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_ssl_reset_in_out_pointers(ssl);
|
2014-03-24 13:13:01 +01:00
|
|
|
|
2022-02-17 07:23:47 +01:00
|
|
|
#if defined(MBEDTLS_SSL_DTLS_SRTP)
|
2023-01-11 14:50:10 +01:00
|
|
|
memset(&ssl->dtls_srtp_info, 0, sizeof(ssl->dtls_srtp_info));
|
2022-02-17 07:23:47 +01:00
|
|
|
#endif
|
2014-03-24 13:13:01 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if ((ret = ssl_handshake_init(ssl)) != 0) {
|
2022-02-17 07:23:47 +01:00
|
|
|
goto error;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2014-03-24 13:13:01 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2018-02-28 13:30:59 +01:00
|
|
|
|
2022-02-17 07:23:47 +01:00
|
|
|
error:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_free(ssl->in_buf);
|
|
|
|
mbedtls_free(ssl->out_buf);
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2022-02-17 07:23:47 +01:00
|
|
|
ssl->conf = NULL;
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2022-02-17 07:23:47 +01:00
|
|
|
#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
|
|
|
|
ssl->in_buf_len = 0;
|
|
|
|
ssl->out_buf_len = 0;
|
2020-02-05 15:44:10 +01:00
|
|
|
#endif
|
2022-02-17 07:23:47 +01:00
|
|
|
ssl->in_buf = NULL;
|
|
|
|
ssl->out_buf = NULL;
|
2014-09-17 11:34:57 +02:00
|
|
|
|
2022-02-17 07:23:47 +01:00
|
|
|
ssl->in_hdr = NULL;
|
|
|
|
ssl->in_ctr = NULL;
|
|
|
|
ssl->in_len = NULL;
|
|
|
|
ssl->in_iv = NULL;
|
|
|
|
ssl->in_msg = NULL;
|
|
|
|
|
|
|
|
ssl->out_hdr = NULL;
|
|
|
|
ssl->out_ctr = NULL;
|
|
|
|
ssl->out_len = NULL;
|
|
|
|
ssl->out_iv = NULL;
|
|
|
|
ssl->out_msg = NULL;
|
2014-02-14 08:39:32 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return ret;
|
2020-02-05 15:44:10 +01:00
|
|
|
}
|
2018-02-22 13:29:04 +01:00
|
|
|
|
2022-02-17 07:23:47 +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.)
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
void mbedtls_ssl_session_reset_msg_layer(mbedtls_ssl_context *ssl,
|
|
|
|
int partial)
|
2014-09-29 15:29:48 +02:00
|
|
|
{
|
2022-02-17 07:23:47 +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;
|
2020-02-05 15:44:10 +01:00
|
|
|
#else
|
2022-02-17 07:23:47 +01:00
|
|
|
size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
|
|
|
|
size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
|
2020-02-05 15:44:10 +01:00
|
|
|
#endif
|
2014-09-29 15:29:48 +02:00
|
|
|
|
2022-02-17 07:23:47 +01:00
|
|
|
#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || !defined(MBEDTLS_SSL_SRV_C)
|
|
|
|
partial = 0;
|
|
|
|
#endif
|
2014-09-19 11:18:57 +02:00
|
|
|
|
2022-02-17 07:23:47 +01:00
|
|
|
/* Cancel any possibly running timer */
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_ssl_set_timer(ssl, 0);
|
2014-09-19 11:18:57 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_ssl_reset_in_out_pointers(ssl);
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2022-02-17 07:23:47 +01:00
|
|
|
/* Reset incoming message parsing */
|
|
|
|
ssl->in_offt = NULL;
|
|
|
|
ssl->nb_zero = 0;
|
|
|
|
ssl->in_msgtype = 0;
|
|
|
|
ssl->in_msglen = 0;
|
|
|
|
ssl->in_hslen = 0;
|
|
|
|
ssl->keep_current_message = 0;
|
|
|
|
ssl->transform_in = NULL;
|
2017-09-13 09:38:11 +02:00
|
|
|
|
2022-02-17 07:23:47 +01:00
|
|
|
#if defined(MBEDTLS_SSL_PROTO_DTLS)
|
|
|
|
ssl->next_record_offset = 0;
|
|
|
|
ssl->in_epoch = 0;
|
|
|
|
#endif
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2022-02-17 07:23:47 +01:00
|
|
|
/* Keep current datagram if partial == 1 */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (partial == 0) {
|
2022-02-17 07:23:47 +01:00
|
|
|
ssl->in_left = 0;
|
2023-01-11 14:50:10 +01:00
|
|
|
memset(ssl->in_buf, 0, in_buf_len);
|
2017-09-13 09:54:27 +02:00
|
|
|
}
|
2020-02-05 15:44:10 +01:00
|
|
|
|
2022-06-10 17:18:09 +02:00
|
|
|
ssl->send_alert = 0;
|
|
|
|
|
2022-02-17 07:23:47 +01:00
|
|
|
/* Reset outgoing message writing */
|
|
|
|
ssl->out_msgtype = 0;
|
|
|
|
ssl->out_msglen = 0;
|
|
|
|
ssl->out_left = 0;
|
2023-01-11 14:50:10 +01:00
|
|
|
memset(ssl->out_buf, 0, out_buf_len);
|
|
|
|
memset(ssl->cur_out_ctr, 0, sizeof(ssl->cur_out_ctr));
|
2022-02-17 07:23:47 +01:00
|
|
|
ssl->transform_out = NULL;
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2022-02-17 07:23:47 +01:00
|
|
|
#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_ssl_dtls_replay_reset(ssl);
|
2020-02-05 15:44:10 +01:00
|
|
|
#endif
|
2017-09-18 11:55:54 +02:00
|
|
|
|
2022-02-17 07:23:47 +01:00
|
|
|
#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->transform) {
|
|
|
|
mbedtls_ssl_transform_free(ssl->transform);
|
|
|
|
mbedtls_free(ssl->transform);
|
2022-02-17 07:23:47 +01:00
|
|
|
ssl->transform = NULL;
|
|
|
|
}
|
|
|
|
#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
|
2014-02-18 17:40:52 +01:00
|
|
|
|
2022-02-17 07:23:47 +01:00
|
|
|
#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_ssl_transform_free(ssl->transform_application);
|
|
|
|
mbedtls_free(ssl->transform_application);
|
2022-02-17 07:23:47 +01:00
|
|
|
ssl->transform_application = NULL;
|
2014-02-18 18:50:02 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->handshake != NULL) {
|
2022-11-04 07:07:25 +01:00
|
|
|
#if defined(MBEDTLS_SSL_EARLY_DATA)
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_ssl_transform_free(ssl->handshake->transform_earlydata);
|
|
|
|
mbedtls_free(ssl->handshake->transform_earlydata);
|
2022-02-17 07:23:47 +01:00
|
|
|
ssl->handshake->transform_earlydata = NULL;
|
2022-11-04 07:07:25 +01:00
|
|
|
#endif
|
2014-02-18 17:40:52 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_ssl_transform_free(ssl->handshake->transform_handshake);
|
|
|
|
mbedtls_free(ssl->handshake->transform_handshake);
|
2022-02-17 07:23:47 +01:00
|
|
|
ssl->handshake->transform_handshake = NULL;
|
|
|
|
}
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2022-02-17 07:23:47 +01:00
|
|
|
#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
|
2020-02-05 15:44:10 +01:00
|
|
|
}
|
2020-11-25 04:42:28 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_ssl_session_reset_int(mbedtls_ssl_context *ssl, int partial)
|
2020-02-05 15:44:10 +01:00
|
|
|
{
|
2022-02-17 07:23:47 +01:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2017-09-13 09:38:11 +02:00
|
|
|
|
2022-02-17 07:23:47 +01:00
|
|
|
ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
|
2017-09-13 09:38:11 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_ssl_session_reset_msg_layer(ssl, partial);
|
2017-09-13 09:38:11 +02:00
|
|
|
|
2022-02-17 07:23:47 +01:00
|
|
|
/* Reset renegotiation state */
|
|
|
|
#if defined(MBEDTLS_SSL_RENEGOTIATION)
|
|
|
|
ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
|
|
|
|
ssl->renego_records_seen = 0;
|
2014-09-19 11:18:57 +02:00
|
|
|
|
2022-02-17 07:23:47 +01:00
|
|
|
ssl->verify_data_len = 0;
|
2023-01-11 14:50:10 +01:00
|
|
|
memset(ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN);
|
|
|
|
memset(ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN);
|
2022-02-17 07:23:47 +01:00
|
|
|
#endif
|
|
|
|
ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
|
2012-07-03 15:30:23 +02:00
|
|
|
|
2022-02-17 07:23:47 +01:00
|
|
|
ssl->session_in = NULL;
|
|
|
|
ssl->session_out = NULL;
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->session) {
|
|
|
|
mbedtls_ssl_session_free(ssl->session);
|
|
|
|
mbedtls_free(ssl->session);
|
2022-02-17 07:23:47 +01:00
|
|
|
ssl->session = NULL;
|
2012-05-08 11:17:57 +02:00
|
|
|
}
|
2017-12-27 22:37:21 +01:00
|
|
|
|
2022-02-17 07:23:47 +01:00
|
|
|
#if defined(MBEDTLS_SSL_ALPN)
|
|
|
|
ssl->alpn_chosen = NULL;
|
2020-02-05 15:44:10 +01:00
|
|
|
#endif
|
2017-12-27 22:37:21 +01:00
|
|
|
|
2022-02-17 07:23:47 +01:00
|
|
|
#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
|
2022-10-06 15:49:08 +02:00
|
|
|
int free_cli_id = 1;
|
2022-02-17 07:23:47 +01:00
|
|
|
#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
|
2023-01-11 14:50:10 +01:00
|
|
|
free_cli_id = (partial == 0);
|
2022-02-17 07:23:47 +01:00
|
|
|
#endif
|
2023-01-11 14:50:10 +01:00
|
|
|
if (free_cli_id) {
|
|
|
|
mbedtls_free(ssl->cli_id);
|
2022-02-17 07:23:47 +01:00
|
|
|
ssl->cli_id = NULL;
|
|
|
|
ssl->cli_id_len = 0;
|
|
|
|
}
|
2020-02-05 15:44:10 +01:00
|
|
|
#endif
|
2017-12-27 22:37:21 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if ((ret = ssl_handshake_init(ssl)) != 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
2017-12-27 22:37:21 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2020-02-05 15:44:10 +01:00
|
|
|
}
|
2022-02-08 13:29:25 +01:00
|
|
|
|
2022-02-17 07:23:47 +01:00
|
|
|
/*
|
|
|
|
* Reset an initialized and used SSL context for re-use while retaining
|
|
|
|
* all application-set variables, function pointers and data.
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_ssl_session_reset(mbedtls_ssl_context *ssl)
|
2020-02-05 15:44:10 +01:00
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
return mbedtls_ssl_session_reset_int(ssl, 0);
|
2022-02-17 07:23:47 +01:00
|
|
|
}
|
2012-05-08 11:17:57 +02:00
|
|
|
|
2022-02-17 07:23:47 +01:00
|
|
|
/*
|
|
|
|
* SSL set accessors
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
void mbedtls_ssl_conf_endpoint(mbedtls_ssl_config *conf, int endpoint)
|
2022-02-17 07:23:47 +01:00
|
|
|
{
|
|
|
|
conf->endpoint = endpoint;
|
2020-02-05 15:44:10 +01:00
|
|
|
}
|
2018-08-06 12:19:13 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
void mbedtls_ssl_conf_transport(mbedtls_ssl_config *conf, int transport)
|
2020-02-05 15:44:10 +01:00
|
|
|
{
|
2022-02-17 07:23:47 +01:00
|
|
|
conf->transport = transport;
|
|
|
|
}
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2022-02-17 07:23:47 +01:00
|
|
|
#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
|
2023-01-11 14:50:10 +01:00
|
|
|
void mbedtls_ssl_conf_dtls_anti_replay(mbedtls_ssl_config *conf, char mode)
|
2022-02-17 07:23:47 +01:00
|
|
|
{
|
|
|
|
conf->anti_replay = mode;
|
|
|
|
}
|
2020-02-05 15:44:10 +01:00
|
|
|
#endif
|
2018-08-21 14:31:31 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
void mbedtls_ssl_conf_dtls_badmac_limit(mbedtls_ssl_config *conf, unsigned limit)
|
2022-02-17 07:23:47 +01:00
|
|
|
{
|
|
|
|
conf->badmac_limit = limit;
|
|
|
|
}
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_SSL_PROTO_DTLS)
|
2014-08-20 13:12:58 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
void mbedtls_ssl_set_datagram_packing(mbedtls_ssl_context *ssl,
|
|
|
|
unsigned allow_packing)
|
2022-02-17 07:23:47 +01:00
|
|
|
{
|
|
|
|
ssl->disable_datagram_packing = !allow_packing;
|
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
void mbedtls_ssl_conf_handshake_timeout(mbedtls_ssl_config *conf,
|
|
|
|
uint32_t min, uint32_t max)
|
2022-02-17 07:23:47 +01:00
|
|
|
{
|
|
|
|
conf->hs_timeout_min = min;
|
|
|
|
conf->hs_timeout_max = max;
|
|
|
|
}
|
2020-02-05 15:44:10 +01:00
|
|
|
#endif
|
2014-08-20 13:12:58 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
void mbedtls_ssl_conf_authmode(mbedtls_ssl_config *conf, int authmode)
|
2022-02-17 07:23:47 +01:00
|
|
|
{
|
|
|
|
conf->authmode = authmode;
|
|
|
|
}
|
2014-08-20 13:12:58 +02:00
|
|
|
|
2022-02-17 07:23:47 +01:00
|
|
|
#if defined(MBEDTLS_X509_CRT_PARSE_C)
|
2023-01-11 14:50:10 +01:00
|
|
|
void mbedtls_ssl_conf_verify(mbedtls_ssl_config *conf,
|
|
|
|
int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
|
|
|
|
void *p_vrfy)
|
2022-02-17 07:23:47 +01:00
|
|
|
{
|
|
|
|
conf->f_vrfy = f_vrfy;
|
|
|
|
conf->p_vrfy = p_vrfy;
|
2014-08-20 13:12:58 +02:00
|
|
|
}
|
2022-02-17 07:23:47 +01:00
|
|
|
#endif /* MBEDTLS_X509_CRT_PARSE_C */
|
2014-08-20 13:12:58 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
void mbedtls_ssl_conf_rng(mbedtls_ssl_config *conf,
|
|
|
|
int (*f_rng)(void *, unsigned char *, size_t),
|
|
|
|
void *p_rng)
|
2014-08-20 13:12:58 +02:00
|
|
|
{
|
2022-02-17 07:23:47 +01:00
|
|
|
conf->f_rng = f_rng;
|
|
|
|
conf->p_rng = p_rng;
|
|
|
|
}
|
2014-08-20 13:12:58 +02:00
|
|
|
|
2023-01-11 14:50: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)
|
2022-02-17 07:23:47 +01:00
|
|
|
{
|
|
|
|
conf->f_dbg = f_dbg;
|
|
|
|
conf->p_dbg = p_dbg;
|
|
|
|
}
|
2014-08-20 13:12:58 +02:00
|
|
|
|
2023-01-11 14:50: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)
|
2022-02-17 07:23:47 +01:00
|
|
|
{
|
|
|
|
ssl->p_bio = p_bio;
|
|
|
|
ssl->f_send = f_send;
|
|
|
|
ssl->f_recv = f_recv;
|
|
|
|
ssl->f_recv_timeout = f_recv_timeout;
|
|
|
|
}
|
2014-09-02 13:39:16 +02:00
|
|
|
|
2022-02-17 07:23:47 +01:00
|
|
|
#if defined(MBEDTLS_SSL_PROTO_DTLS)
|
2023-01-11 14:50:10 +01:00
|
|
|
void mbedtls_ssl_set_mtu(mbedtls_ssl_context *ssl, uint16_t mtu)
|
2022-02-17 07:23:47 +01:00
|
|
|
{
|
|
|
|
ssl->mtu = mtu;
|
|
|
|
}
|
|
|
|
#endif
|
2014-08-20 13:12:58 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
void mbedtls_ssl_conf_read_timeout(mbedtls_ssl_config *conf, uint32_t timeout)
|
2022-02-17 07:23:47 +01:00
|
|
|
{
|
|
|
|
conf->read_timeout = timeout;
|
|
|
|
}
|
2014-08-20 13:12:58 +02:00
|
|
|
|
2023-01-11 14:50: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)
|
2022-02-17 07:23:47 +01:00
|
|
|
{
|
|
|
|
ssl->p_timer = p_timer;
|
|
|
|
ssl->f_set_timer = f_set_timer;
|
|
|
|
ssl->f_get_timer = f_get_timer;
|
2014-08-20 13:12:58 +02:00
|
|
|
|
2022-02-17 07:23:47 +01:00
|
|
|
/* Make sure we start with no timer running */
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_ssl_set_timer(ssl, 0);
|
2022-02-17 07:23:47 +01:00
|
|
|
}
|
2014-08-20 10:43:01 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
#if defined(MBEDTLS_SSL_SRV_C)
|
2023-01-11 14:50:10 +01:00
|
|
|
void mbedtls_ssl_conf_session_cache(mbedtls_ssl_config *conf,
|
|
|
|
void *p_cache,
|
|
|
|
mbedtls_ssl_cache_get_t *f_get_cache,
|
|
|
|
mbedtls_ssl_cache_set_t *f_set_cache)
|
2022-02-17 07:23:47 +01:00
|
|
|
{
|
|
|
|
conf->p_cache = p_cache;
|
|
|
|
conf->f_get_cache = f_get_cache;
|
|
|
|
conf->f_set_cache = f_set_cache;
|
|
|
|
}
|
|
|
|
#endif /* MBEDTLS_SSL_SRV_C */
|
2014-09-03 11:01:14 +02:00
|
|
|
|
2022-02-17 07:23:47 +01:00
|
|
|
#if defined(MBEDTLS_SSL_CLI_C)
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_ssl_set_session(mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session)
|
2022-02-17 07:23:47 +01:00
|
|
|
{
|
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2014-02-18 11:33:49 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl == NULL ||
|
2022-02-17 07:23:47 +01:00
|
|
|
session == NULL ||
|
|
|
|
ssl->session_negotiate == NULL ||
|
2023-01-11 14:50:10 +01:00
|
|
|
ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT) {
|
|
|
|
return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
|
2022-02-17 07:23:47 +01:00
|
|
|
}
|
2018-08-15 16:54:43 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->handshake->resume == 1) {
|
|
|
|
return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
|
|
|
|
}
|
2014-09-19 21:18:23 +02:00
|
|
|
|
2022-10-10 15:21:31 +02:00
|
|
|
#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (session->tls_version == MBEDTLS_SSL_VERSION_TLS1_3) {
|
2022-10-10 15:21:31 +02:00
|
|
|
const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_ssl_ciphersuite_from_id(session->ciphersuite);
|
2022-10-10 15:21:31 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (mbedtls_ssl_validate_ciphersuite(
|
2022-10-10 15:21:31 +02:00
|
|
|
ssl, ciphersuite_info, MBEDTLS_SSL_VERSION_TLS1_3,
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_VERSION_TLS1_3) != 0) {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(4, ("%d is not a valid TLS 1.3 ciphersuite.",
|
|
|
|
session->ciphersuite));
|
|
|
|
return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
|
2022-10-10 15:21:31 +02:00
|
|
|
}
|
2022-10-08 04:42:13 +02:00
|
|
|
}
|
2022-10-10 15:21:31 +02:00
|
|
|
#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
|
2022-10-08 04:42:13 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if ((ret = mbedtls_ssl_session_copy(ssl->session_negotiate,
|
|
|
|
session)) != 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
2021-09-28 12:58:59 +02:00
|
|
|
|
2022-02-17 07:23:47 +01:00
|
|
|
ssl->handshake->resume = 1;
|
2014-09-19 21:18:23 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2022-02-17 07:23:47 +01:00
|
|
|
}
|
|
|
|
#endif /* MBEDTLS_SSL_CLI_C */
|
2014-08-20 10:43:01 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
void mbedtls_ssl_conf_ciphersuites(mbedtls_ssl_config *conf,
|
|
|
|
const int *ciphersuites)
|
2022-02-17 07:23:47 +01:00
|
|
|
{
|
|
|
|
conf->ciphersuite_list = ciphersuites;
|
|
|
|
}
|
2020-02-05 15:44:10 +01:00
|
|
|
|
2022-02-17 07:23:47 +01:00
|
|
|
#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
|
2023-01-11 14:50:10 +01:00
|
|
|
void mbedtls_ssl_conf_tls13_key_exchange_modes(mbedtls_ssl_config *conf,
|
|
|
|
const int kex_modes)
|
2022-02-17 07:23:47 +01:00
|
|
|
{
|
|
|
|
conf->tls13_kex_modes = kex_modes & MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_ALL;
|
|
|
|
}
|
2022-10-25 04:54:33 +02:00
|
|
|
|
|
|
|
#if defined(MBEDTLS_SSL_EARLY_DATA)
|
2023-11-07 04:40:43 +01:00
|
|
|
void mbedtls_ssl_conf_early_data(mbedtls_ssl_config *conf,
|
|
|
|
int early_data_enabled)
|
2022-10-25 04:54:33 +02:00
|
|
|
{
|
|
|
|
conf->early_data_enabled = early_data_enabled;
|
|
|
|
}
|
2022-11-22 10:22:22 +01:00
|
|
|
|
|
|
|
#if defined(MBEDTLS_SSL_SRV_C)
|
2023-11-07 04:47:36 +01:00
|
|
|
void mbedtls_ssl_conf_max_early_data_size(
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_ssl_config *conf, uint32_t max_early_data_size)
|
2022-11-22 10:22:22 +01:00
|
|
|
{
|
2022-12-06 09:58:36 +01:00
|
|
|
conf->max_early_data_size = max_early_data_size;
|
2022-11-22 10:22:22 +01:00
|
|
|
}
|
|
|
|
#endif /* MBEDTLS_SSL_SRV_C */
|
|
|
|
|
2022-10-25 04:54:33 +02:00
|
|
|
#endif /* MBEDTLS_SSL_EARLY_DATA */
|
2022-02-17 07:23:47 +01:00
|
|
|
#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
|
2020-02-05 15:44:10 +01:00
|
|
|
|
2022-02-17 07:23:47 +01:00
|
|
|
#if defined(MBEDTLS_X509_CRT_PARSE_C)
|
2023-01-11 14:50:10 +01:00
|
|
|
void mbedtls_ssl_conf_cert_profile(mbedtls_ssl_config *conf,
|
|
|
|
const mbedtls_x509_crt_profile *profile)
|
2022-02-17 07:23:47 +01:00
|
|
|
{
|
|
|
|
conf->cert_profile = profile;
|
|
|
|
}
|
2016-10-13 18:21:01 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
static void ssl_key_cert_free(mbedtls_ssl_key_cert *key_cert)
|
2022-01-22 11:06:31 +01:00
|
|
|
{
|
|
|
|
mbedtls_ssl_key_cert *cur = key_cert, *next;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
while (cur != NULL) {
|
2022-01-22 11:06:31 +01:00
|
|
|
next = cur->next;
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_free(cur);
|
2022-01-22 11:06:31 +01:00
|
|
|
cur = next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-17 07:23:47 +01:00
|
|
|
/* Append a new keycert entry to a (possibly empty) list */
|
2022-06-17 10:52:54 +02:00
|
|
|
MBEDTLS_CHECK_RETURN_CRITICAL
|
2023-01-11 14:50:10 +01:00
|
|
|
static int ssl_append_key_cert(mbedtls_ssl_key_cert **head,
|
|
|
|
mbedtls_x509_crt *cert,
|
|
|
|
mbedtls_pk_context *key)
|
2022-02-17 07:23:47 +01:00
|
|
|
{
|
|
|
|
mbedtls_ssl_key_cert *new_cert;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (cert == NULL) {
|
2022-01-22 11:06:31 +01:00
|
|
|
/* Free list if cert is null */
|
2023-01-11 14:50:10 +01:00
|
|
|
ssl_key_cert_free(*head);
|
2022-01-22 11:06:31 +01:00
|
|
|
*head = NULL;
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2022-01-22 11:06:31 +01:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
new_cert = mbedtls_calloc(1, sizeof(mbedtls_ssl_key_cert));
|
|
|
|
if (new_cert == NULL) {
|
|
|
|
return MBEDTLS_ERR_SSL_ALLOC_FAILED;
|
|
|
|
}
|
2022-02-17 07:23:47 +01:00
|
|
|
|
|
|
|
new_cert->cert = cert;
|
|
|
|
new_cert->key = key;
|
|
|
|
new_cert->next = NULL;
|
|
|
|
|
2022-01-22 11:06:31 +01:00
|
|
|
/* Update head if the list was null, else add to the end */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (*head == NULL) {
|
2022-02-17 07:23:47 +01:00
|
|
|
*head = new_cert;
|
2023-01-11 14:50:10 +01:00
|
|
|
} else {
|
2022-02-17 07:23:47 +01:00
|
|
|
mbedtls_ssl_key_cert *cur = *head;
|
2023-01-11 14:50:10 +01:00
|
|
|
while (cur->next != NULL) {
|
2022-02-17 07:23:47 +01:00
|
|
|
cur = cur->next;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2022-02-17 07:23:47 +01:00
|
|
|
cur->next = new_cert;
|
2020-02-05 15:44:10 +01:00
|
|
|
}
|
2018-08-16 14:23:47 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2020-02-05 15:44:10 +01:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_ssl_conf_own_cert(mbedtls_ssl_config *conf,
|
2022-02-17 07:23:47 +01:00
|
|
|
mbedtls_x509_crt *own_cert,
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_pk_context *pk_key)
|
2020-02-05 15:44:10 +01:00
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
return ssl_append_key_cert(&conf->key_cert, own_cert, pk_key);
|
2022-02-17 07:23:47 +01:00
|
|
|
}
|
2018-08-16 14:23:47 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
void mbedtls_ssl_conf_ca_chain(mbedtls_ssl_config *conf,
|
2022-02-17 07:23:47 +01:00
|
|
|
mbedtls_x509_crt *ca_chain,
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_x509_crl *ca_crl)
|
2022-02-17 07:23:47 +01:00
|
|
|
{
|
|
|
|
conf->ca_chain = ca_chain;
|
|
|
|
conf->ca_crl = ca_crl;
|
2020-02-05 15:44:10 +01:00
|
|
|
|
2022-02-17 07:23:47 +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-16 14:23:47 +02:00
|
|
|
|
2022-02-17 07:23:47 +01:00
|
|
|
#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
|
2023-01-11 14:50:10 +01:00
|
|
|
void mbedtls_ssl_conf_ca_cb(mbedtls_ssl_config *conf,
|
|
|
|
mbedtls_x509_crt_ca_cb_t f_ca_cb,
|
|
|
|
void *p_ca_cb)
|
2022-02-17 07:23:47 +01:00
|
|
|
{
|
|
|
|
conf->f_ca_cb = f_ca_cb;
|
|
|
|
conf->p_ca_cb = p_ca_cb;
|
2014-02-18 11:33:49 +01:00
|
|
|
|
2022-02-17 07:23:47 +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 */
|
2014-09-24 10:52:58 +02:00
|
|
|
|
2022-02-17 07:23:47 +01:00
|
|
|
#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
|
2023-01-11 14:50:10 +01:00
|
|
|
const unsigned char *mbedtls_ssl_get_hs_sni(mbedtls_ssl_context *ssl,
|
|
|
|
size_t *name_len)
|
2022-01-24 18:58:00 +01:00
|
|
|
{
|
|
|
|
*name_len = ssl->handshake->sni_name_len;
|
2023-01-11 14:50:10 +01:00
|
|
|
return ssl->handshake->sni_name;
|
2022-01-24 18:58:00 +01:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_ssl_set_hs_own_cert(mbedtls_ssl_context *ssl,
|
|
|
|
mbedtls_x509_crt *own_cert,
|
|
|
|
mbedtls_pk_context *pk_key)
|
2022-02-17 07:23:47 +01:00
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
return ssl_append_key_cert(&ssl->handshake->sni_key_cert,
|
|
|
|
own_cert, pk_key);
|
2022-02-17 07:23:47 +01:00
|
|
|
}
|
2021-06-24 11:27:37 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
void mbedtls_ssl_set_hs_ca_chain(mbedtls_ssl_context *ssl,
|
|
|
|
mbedtls_x509_crt *ca_chain,
|
|
|
|
mbedtls_x509_crl *ca_crl)
|
2022-02-17 07:23:47 +01:00
|
|
|
{
|
|
|
|
ssl->handshake->sni_ca_chain = ca_chain;
|
|
|
|
ssl->handshake->sni_ca_crl = ca_crl;
|
|
|
|
}
|
2020-02-05 15:44:10 +01:00
|
|
|
|
2022-03-11 07:37:23 +01:00
|
|
|
#if defined(MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED)
|
2023-01-11 14:50:10 +01:00
|
|
|
void mbedtls_ssl_set_hs_dn_hints(mbedtls_ssl_context *ssl,
|
|
|
|
const mbedtls_x509_crt *crt)
|
2022-03-11 07:37:23 +01:00
|
|
|
{
|
|
|
|
ssl->handshake->dn_hints = crt;
|
|
|
|
}
|
|
|
|
#endif /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
void mbedtls_ssl_set_hs_authmode(mbedtls_ssl_context *ssl,
|
|
|
|
int authmode)
|
2022-02-17 07:23:47 +01:00
|
|
|
{
|
|
|
|
ssl->handshake->sni_authmode = authmode;
|
|
|
|
}
|
|
|
|
#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
|
2019-10-29 10:13:33 +01:00
|
|
|
|
2022-02-17 07:23:47 +01:00
|
|
|
#if defined(MBEDTLS_X509_CRT_PARSE_C)
|
2023-01-11 14:50:10 +01:00
|
|
|
void mbedtls_ssl_set_verify(mbedtls_ssl_context *ssl,
|
|
|
|
int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
|
|
|
|
void *p_vrfy)
|
2022-02-17 07:23:47 +01:00
|
|
|
{
|
|
|
|
ssl->f_vrfy = f_vrfy;
|
|
|
|
ssl->p_vrfy = p_vrfy;
|
|
|
|
}
|
2020-02-05 15:44:10 +01:00
|
|
|
#endif
|
2019-10-29 10:13:33 +01:00
|
|
|
|
2022-02-17 07:23:47 +01:00
|
|
|
#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
|
2022-12-09 14:17:50 +01:00
|
|
|
|
2022-11-15 14:08:42 +01:00
|
|
|
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
2023-03-13 16:06:09 +01:00
|
|
|
static const uint8_t jpake_server_id[] = { 's', 'e', 'r', 'v', 'e', 'r' };
|
|
|
|
static const uint8_t jpake_client_id[] = { 'c', 'l', 'i', 'e', 'n', 't' };
|
|
|
|
|
2022-12-09 14:17:50 +01:00
|
|
|
static psa_status_t mbedtls_ssl_set_hs_ecjpake_password_common(
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_ssl_context *ssl,
|
|
|
|
mbedtls_svc_key_id_t pwd)
|
2022-12-09 14:17:50 +01:00
|
|
|
{
|
|
|
|
psa_status_t status;
|
|
|
|
psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init();
|
2023-03-13 16:06:09 +01:00
|
|
|
const uint8_t *user = NULL;
|
2023-03-01 11:11:28 +01:00
|
|
|
size_t user_len = 0;
|
2023-03-13 16:06:09 +01:00
|
|
|
const uint8_t *peer = NULL;
|
2023-03-01 11:11:28 +01:00
|
|
|
size_t peer_len = 0;
|
2023-01-11 14:50:10 +01:00
|
|
|
psa_pake_cs_set_algorithm(&cipher_suite, PSA_ALG_JPAKE);
|
|
|
|
psa_pake_cs_set_primitive(&cipher_suite,
|
|
|
|
PSA_PAKE_PRIMITIVE(PSA_PAKE_PRIMITIVE_TYPE_ECC,
|
|
|
|
PSA_ECC_FAMILY_SECP_R1,
|
|
|
|
256));
|
|
|
|
psa_pake_cs_set_hash(&cipher_suite, PSA_ALG_SHA_256);
|
2022-12-09 14:17:50 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
status = psa_pake_setup(&ssl->handshake->psa_pake_ctx, &cipher_suite);
|
|
|
|
if (status != PSA_SUCCESS) {
|
2022-12-09 14:17:50 +01:00
|
|
|
return status;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2022-12-09 14:17:50 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
|
2023-03-13 16:06:09 +01:00
|
|
|
user = jpake_server_id;
|
|
|
|
user_len = sizeof(jpake_server_id);
|
|
|
|
peer = jpake_client_id;
|
|
|
|
peer_len = sizeof(jpake_client_id);
|
2023-01-11 14:50:10 +01:00
|
|
|
} else {
|
2023-03-13 16:06:09 +01:00
|
|
|
user = jpake_client_id;
|
|
|
|
user_len = sizeof(jpake_client_id);
|
|
|
|
peer = jpake_server_id;
|
|
|
|
peer_len = sizeof(jpake_server_id);
|
2023-03-01 11:11:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
status = psa_pake_set_user(&ssl->handshake->psa_pake_ctx, user, user_len);
|
|
|
|
if (status != PSA_SUCCESS) {
|
|
|
|
return status;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2022-12-09 14:17:50 +01:00
|
|
|
|
2023-03-01 11:11:28 +01:00
|
|
|
status = psa_pake_set_peer(&ssl->handshake->psa_pake_ctx, peer, peer_len);
|
2023-01-11 14:50:10 +01:00
|
|
|
if (status != PSA_SUCCESS) {
|
2022-12-09 14:17:50 +01:00
|
|
|
return status;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2022-12-09 14:17:50 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
status = psa_pake_set_password_key(&ssl->handshake->psa_pake_ctx, pwd);
|
|
|
|
if (status != PSA_SUCCESS) {
|
2022-12-09 14:17:50 +01:00
|
|
|
return status;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2022-12-09 14:17:50 +01:00
|
|
|
|
|
|
|
ssl->handshake->psa_pake_ctx_is_ok = 1;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return PSA_SUCCESS;
|
2022-12-09 14:17:50 +01:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_ssl_set_hs_ecjpake_password(mbedtls_ssl_context *ssl,
|
|
|
|
const unsigned char *pw,
|
|
|
|
size_t pw_len)
|
2022-02-17 07:23:47 +01:00
|
|
|
{
|
2022-05-31 14:43:23 +02:00
|
|
|
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
|
|
|
|
psa_status_t status;
|
2019-10-29 10:13:33 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->handshake == NULL || ssl->conf == NULL) {
|
|
|
|
return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
|
|
|
|
}
|
2019-10-29 10:13:33 +01:00
|
|
|
|
2022-11-17 18:17:01 +01:00
|
|
|
/* Empty password is not valid */
|
2023-01-11 14:50:10 +01:00
|
|
|
if ((pw == NULL) || (pw_len == 0)) {
|
|
|
|
return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
|
|
|
|
}
|
2022-05-31 14:43:23 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
|
|
|
|
psa_set_key_algorithm(&attributes, PSA_ALG_JPAKE);
|
|
|
|
psa_set_key_type(&attributes, PSA_KEY_TYPE_PASSWORD);
|
2022-11-17 18:17:01 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
status = psa_import_key(&attributes, pw, pw_len,
|
|
|
|
&ssl->handshake->psa_pake_password);
|
|
|
|
if (status != PSA_SUCCESS) {
|
|
|
|
return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
|
|
|
|
}
|
2022-05-31 14:43:23 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
status = mbedtls_ssl_set_hs_ecjpake_password_common(ssl,
|
|
|
|
ssl->handshake->psa_pake_password);
|
|
|
|
if (status != PSA_SUCCESS) {
|
|
|
|
psa_destroy_key(ssl->handshake->psa_pake_password);
|
|
|
|
psa_pake_abort(&ssl->handshake->psa_pake_ctx);
|
|
|
|
return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
|
2022-05-31 14:43:23 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2022-11-28 18:26:16 +01:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_ssl_set_hs_ecjpake_password_opaque(mbedtls_ssl_context *ssl,
|
|
|
|
mbedtls_svc_key_id_t pwd)
|
2022-11-28 18:26:16 +01:00
|
|
|
{
|
|
|
|
psa_status_t status;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->handshake == NULL || ssl->conf == NULL) {
|
|
|
|
return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
|
|
|
|
}
|
2022-11-28 18:26:16 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (mbedtls_svc_key_id_is_null(pwd)) {
|
|
|
|
return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
|
|
|
|
}
|
2022-11-28 18:26:16 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
status = mbedtls_ssl_set_hs_ecjpake_password_common(ssl, pwd);
|
|
|
|
if (status != PSA_SUCCESS) {
|
|
|
|
psa_pake_abort(&ssl->handshake->psa_pake_ctx);
|
|
|
|
return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
|
2022-05-31 14:43:23 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2022-11-15 14:08:42 +01:00
|
|
|
}
|
|
|
|
#else /* MBEDTLS_USE_PSA_CRYPTO */
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_ssl_set_hs_ecjpake_password(mbedtls_ssl_context *ssl,
|
|
|
|
const unsigned char *pw,
|
|
|
|
size_t pw_len)
|
2022-02-17 07:23:47 +01:00
|
|
|
{
|
|
|
|
mbedtls_ecjpake_role role;
|
2019-10-29 10:13:33 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->handshake == NULL || ssl->conf == NULL) {
|
|
|
|
return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
|
|
|
|
}
|
2019-10-29 10:13:33 +01:00
|
|
|
|
2022-12-07 14:40:38 +01:00
|
|
|
/* Empty password is not valid */
|
2023-01-11 14:50:10 +01:00
|
|
|
if ((pw == NULL) || (pw_len == 0)) {
|
|
|
|
return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
|
|
|
|
}
|
2022-12-07 14:40:38 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
|
2022-02-17 07:23:47 +01:00
|
|
|
role = MBEDTLS_ECJPAKE_SERVER;
|
2023-01-11 14:50:10 +01:00
|
|
|
} else {
|
2022-02-17 07:23:47 +01:00
|
|
|
role = MBEDTLS_ECJPAKE_CLIENT;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2019-10-29 10:13:33 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return mbedtls_ecjpake_setup(&ssl->handshake->ecjpake_ctx,
|
|
|
|
role,
|
|
|
|
MBEDTLS_MD_SHA256,
|
|
|
|
MBEDTLS_ECP_DP_SECP256R1,
|
|
|
|
pw, pw_len);
|
2019-10-29 10:13:33 +01:00
|
|
|
}
|
2022-11-15 14:08:42 +01:00
|
|
|
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
2022-02-17 07:23:47 +01:00
|
|
|
#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
|
2019-10-29 10:13:33 +01:00
|
|
|
|
2022-10-05 14:31:43 +02:00
|
|
|
#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED)
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_ssl_conf_has_static_psk(mbedtls_ssl_config const *conf)
|
2014-09-24 10:52:58 +02:00
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
if (conf->psk_identity == NULL ||
|
|
|
|
conf->psk_identity_len == 0) {
|
|
|
|
return 0;
|
2022-10-19 10:33:48 +02:00
|
|
|
}
|
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (!mbedtls_svc_key_id_is_null(conf->psk_opaque)) {
|
|
|
|
return 1;
|
|
|
|
}
|
2022-02-17 07:23:47 +01:00
|
|
|
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
2022-10-19 10:33:48 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (conf->psk != NULL && conf->psk_len != 0) {
|
|
|
|
return 1;
|
|
|
|
}
|
2014-09-24 10:52:58 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2022-02-17 07:23:47 +01:00
|
|
|
}
|
2014-09-24 10:52:58 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
static void ssl_conf_remove_psk(mbedtls_ssl_config *conf)
|
2014-09-24 10:52:58 +02:00
|
|
|
{
|
2022-02-17 07:23:47 +01:00
|
|
|
/* Remove reference to existing PSK, if any. */
|
2022-01-11 08:28:13 +01:00
|
|
|
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (!mbedtls_svc_key_id_is_null(conf->psk_opaque)) {
|
2022-02-17 07:23:47 +01:00
|
|
|
/* The maintenance of the PSK key slot is the
|
|
|
|
* user's responsibility. */
|
|
|
|
conf->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
|
2015-09-08 17:50:29 +02:00
|
|
|
}
|
2022-02-17 07:23:47 +01:00
|
|
|
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (conf->psk != NULL) {
|
2023-07-17 16:17:40 +02:00
|
|
|
mbedtls_zeroize_and_free(conf->psk, conf->psk_len);
|
2022-02-17 07:23:47 +01:00
|
|
|
conf->psk = NULL;
|
|
|
|
conf->psk_len = 0;
|
2015-09-08 17:50:29 +02:00
|
|
|
}
|
|
|
|
|
2022-02-17 07:23:47 +01:00
|
|
|
/* Remove reference to PSK identity, if any. */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (conf->psk_identity != NULL) {
|
|
|
|
mbedtls_free(conf->psk_identity);
|
2022-02-17 07:23:47 +01:00
|
|
|
conf->psk_identity = NULL;
|
|
|
|
conf->psk_identity_len = 0;
|
2015-09-08 17:50:29 +02:00
|
|
|
}
|
2022-02-17 07:23:47 +01:00
|
|
|
}
|
2015-09-08 17:50:29 +02:00
|
|
|
|
2022-02-17 07:23:47 +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. */
|
2022-06-17 10:52:54 +02:00
|
|
|
MBEDTLS_CHECK_RETURN_CRITICAL
|
2023-01-11 14:50:10 +01:00
|
|
|
static int ssl_conf_set_psk_identity(mbedtls_ssl_config *conf,
|
|
|
|
unsigned char const *psk_identity,
|
|
|
|
size_t psk_identity_len)
|
2022-02-17 07:23:47 +01:00
|
|
|
{
|
|
|
|
/* Identity len will be encoded on two bytes */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (psk_identity == NULL ||
|
2022-10-19 10:55:26 +02:00
|
|
|
psk_identity_len == 0 ||
|
2023-01-11 14:50:10 +01:00
|
|
|
(psk_identity_len >> 16) != 0 ||
|
|
|
|
psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN) {
|
|
|
|
return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
|
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
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
conf->psk_identity = mbedtls_calloc(1, psk_identity_len);
|
|
|
|
if (conf->psk_identity == NULL) {
|
|
|
|
return MBEDTLS_ERR_SSL_ALLOC_FAILED;
|
|
|
|
}
|
2015-09-08 10:30:55 +02:00
|
|
|
|
2022-02-17 07:23:47 +01:00
|
|
|
conf->psk_identity_len = psk_identity_len;
|
2023-01-11 14:50:10 +01:00
|
|
|
memcpy(conf->psk_identity, psk_identity, conf->psk_identity_len);
|
2015-09-08 10:30:55 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2022-02-17 07:23:47 +01:00
|
|
|
}
|
2015-09-08 10:30:55 +02:00
|
|
|
|
2023-01-11 14:50: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)
|
2022-02-17 07:23:47 +01:00
|
|
|
{
|
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2015-09-08 10:30:55 +02:00
|
|
|
|
2022-02-17 07:23:47 +01:00
|
|
|
/* We currently only support one PSK, raw or opaque. */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (mbedtls_ssl_conf_has_static_psk(conf)) {
|
|
|
|
return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
|
|
|
|
}
|
2021-08-17 10:53:13 +02:00
|
|
|
|
2022-02-17 07:23:47 +01:00
|
|
|
/* Check and set raw PSK */
|
2023-01-11 14:50:10 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((conf->psk = mbedtls_calloc(1, psk_len)) == NULL) {
|
|
|
|
return MBEDTLS_ERR_SSL_ALLOC_FAILED;
|
|
|
|
}
|
2022-02-17 07:23:47 +01:00
|
|
|
conf->psk_len = psk_len;
|
2023-01-11 14:50:10 +01:00
|
|
|
memcpy(conf->psk, psk, conf->psk_len);
|
2021-08-17 10:53:13 +02:00
|
|
|
|
2022-02-17 07:23:47 +01:00
|
|
|
/* Check and set PSK Identity */
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = ssl_conf_set_psk_identity(conf, psk_identity, psk_identity_len);
|
|
|
|
if (ret != 0) {
|
|
|
|
ssl_conf_remove_psk(conf);
|
|
|
|
}
|
2021-08-17 10:53:13 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return ret;
|
2022-02-17 07:23:47 +01:00
|
|
|
}
|
2021-08-17 10:53:13 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
static void ssl_remove_psk(mbedtls_ssl_context *ssl)
|
2022-02-17 07:23:47 +01:00
|
|
|
{
|
|
|
|
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (!mbedtls_svc_key_id_is_null(ssl->handshake->psk_opaque)) {
|
2022-05-03 09:35:09 +02:00
|
|
|
/* The maintenance of the external PSK key slot is the
|
|
|
|
* user's responsibility. */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->handshake->psk_opaque_is_internal) {
|
|
|
|
psa_destroy_key(ssl->handshake->psk_opaque);
|
2022-05-03 09:35:09 +02:00
|
|
|
ssl->handshake->psk_opaque_is_internal = 0;
|
|
|
|
}
|
2022-02-17 07:23:47 +01:00
|
|
|
ssl->handshake->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
|
2021-08-17 10:53:13 +02:00
|
|
|
}
|
2022-05-03 10:22:14 +02:00
|
|
|
#else
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->handshake->psk != NULL) {
|
2023-07-17 16:17:40 +02:00
|
|
|
mbedtls_zeroize_and_free(ssl->handshake->psk,
|
2023-01-11 14:50:10 +01:00
|
|
|
ssl->handshake->psk_len);
|
2022-02-17 07:23:47 +01:00
|
|
|
ssl->handshake->psk_len = 0;
|
2021-08-17 10:53:13 +02:00
|
|
|
}
|
2022-05-03 10:22:14 +02:00
|
|
|
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
2022-02-17 07:23:47 +01:00
|
|
|
}
|
2021-08-17 10:53:13 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_ssl_set_hs_psk(mbedtls_ssl_context *ssl,
|
|
|
|
const unsigned char *psk, size_t psk_len)
|
2022-02-17 07:23:47 +01:00
|
|
|
{
|
2022-05-03 09:35:09 +02:00
|
|
|
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
|
|
|
psa_key_attributes_t key_attributes = psa_key_attributes_init();
|
2022-08-17 04:18:10 +02:00
|
|
|
psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
|
2022-08-20 13:06:56 +02:00
|
|
|
psa_algorithm_t alg = PSA_ALG_NONE;
|
2022-08-17 04:18:10 +02:00
|
|
|
mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
|
2022-05-03 09:35:09 +02:00
|
|
|
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (psk == NULL || ssl->handshake == NULL) {
|
|
|
|
return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
|
|
|
|
}
|
2022-01-12 08:49:48 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (psk_len > MBEDTLS_PSK_MAX_LEN) {
|
|
|
|
return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
|
|
|
|
}
|
2022-01-20 09:28:27 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
ssl_remove_psk(ssl);
|
2022-01-20 09:28:27 +01:00
|
|
|
|
2022-05-03 09:35:09 +02:00
|
|
|
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
2022-07-26 10:39:20 +02:00
|
|
|
#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2) {
|
|
|
|
if (ssl->handshake->ciphersuite_info->mac == MBEDTLS_MD_SHA384) {
|
|
|
|
alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384);
|
|
|
|
} else {
|
|
|
|
alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256);
|
|
|
|
}
|
|
|
|
psa_set_key_usage_flags(&key_attributes, PSA_KEY_USAGE_DERIVE);
|
2022-07-26 10:39:20 +02:00
|
|
|
}
|
|
|
|
#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
|
2022-05-03 09:35:09 +02:00
|
|
|
|
2022-07-22 15:27:34 +02:00
|
|
|
#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3) {
|
|
|
|
alg = PSA_ALG_HKDF_EXTRACT(PSA_ALG_ANY_HASH);
|
|
|
|
psa_set_key_usage_flags(&key_attributes,
|
|
|
|
PSA_KEY_USAGE_DERIVE | PSA_KEY_USAGE_EXPORT);
|
2022-07-26 10:39:20 +02:00
|
|
|
}
|
|
|
|
#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
psa_set_key_algorithm(&key_attributes, alg);
|
|
|
|
psa_set_key_type(&key_attributes, PSA_KEY_TYPE_DERIVE);
|
2022-05-03 09:35:09 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
status = psa_import_key(&key_attributes, psk, psk_len, &key);
|
|
|
|
if (status != PSA_SUCCESS) {
|
|
|
|
return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
|
|
|
|
}
|
2022-05-03 09:35:09 +02:00
|
|
|
|
|
|
|
/* Allow calling psa_destroy_key() on psk remove */
|
|
|
|
ssl->handshake->psk_opaque_is_internal = 1;
|
2023-01-11 14:50:10 +01:00
|
|
|
return mbedtls_ssl_set_hs_psk_opaque(ssl, key);
|
2022-05-03 09:35:09 +02:00
|
|
|
#else
|
2023-01-11 14:50:10 +01:00
|
|
|
if ((ssl->handshake->psk = mbedtls_calloc(1, psk_len)) == NULL) {
|
|
|
|
return MBEDTLS_ERR_SSL_ALLOC_FAILED;
|
|
|
|
}
|
2022-01-12 08:49:48 +01:00
|
|
|
|
2022-02-17 07:23:47 +01:00
|
|
|
ssl->handshake->psk_len = psk_len;
|
2023-01-11 14:50:10 +01:00
|
|
|
memcpy(ssl->handshake->psk, psk, ssl->handshake->psk_len);
|
2022-01-12 08:49:48 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2022-05-03 09:35:09 +02:00
|
|
|
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
2015-09-08 10:30:55 +02:00
|
|
|
}
|
|
|
|
|
2022-02-17 07:23:47 +01:00
|
|
|
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_ssl_conf_psk_opaque(mbedtls_ssl_config *conf,
|
|
|
|
mbedtls_svc_key_id_t psk,
|
|
|
|
const unsigned char *psk_identity,
|
|
|
|
size_t psk_identity_len)
|
2021-08-04 04:13:52 +02:00
|
|
|
{
|
2022-02-17 07:23:47 +01:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2021-08-04 04:13:52 +02:00
|
|
|
|
2022-02-17 07:23:47 +01:00
|
|
|
/* We currently only support one PSK, raw or opaque. */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (mbedtls_ssl_conf_has_static_psk(conf)) {
|
|
|
|
return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
|
|
|
|
}
|
2021-08-04 04:13:52 +02:00
|
|
|
|
2022-02-17 07:23:47 +01:00
|
|
|
/* Check and set opaque PSK */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (mbedtls_svc_key_id_is_null(psk)) {
|
|
|
|
return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
|
|
|
|
}
|
2022-02-17 07:23:47 +01:00
|
|
|
conf->psk_opaque = psk;
|
2021-08-04 04:13:52 +02:00
|
|
|
|
2022-02-17 07:23:47 +01:00
|
|
|
/* Check and set PSK Identity */
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = ssl_conf_set_psk_identity(conf, psk_identity,
|
|
|
|
psk_identity_len);
|
|
|
|
if (ret != 0) {
|
|
|
|
ssl_conf_remove_psk(conf);
|
|
|
|
}
|
2021-08-04 04:13:52 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return ret;
|
2021-08-04 04:13:52 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_ssl_set_hs_psk_opaque(mbedtls_ssl_context *ssl,
|
|
|
|
mbedtls_svc_key_id_t psk)
|
2020-02-05 15:44:10 +01:00
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
if ((mbedtls_svc_key_id_is_null(psk)) ||
|
|
|
|
(ssl->handshake == NULL)) {
|
|
|
|
return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
|
|
|
|
}
|
2019-07-11 13:29:35 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
ssl_remove_psk(ssl);
|
2022-02-17 07:23:47 +01:00
|
|
|
ssl->handshake->psk_opaque = psk;
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2020-02-05 15:44:10 +01:00
|
|
|
}
|
2022-02-17 07:23:47 +01:00
|
|
|
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
2014-08-11 19:27:24 +02:00
|
|
|
|
2022-08-12 07:56:53 +02:00
|
|
|
#if defined(MBEDTLS_SSL_SRV_C)
|
2023-01-11 14:50: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)
|
2020-02-05 15:44:10 +01:00
|
|
|
{
|
2022-02-17 07:23:47 +01:00
|
|
|
conf->f_psk = f_psk;
|
|
|
|
conf->p_psk = p_psk;
|
|
|
|
}
|
2022-08-12 07:56:53 +02:00
|
|
|
#endif /* MBEDTLS_SSL_SRV_C */
|
|
|
|
|
2022-10-05 14:31:43 +02:00
|
|
|
#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED */
|
2015-12-03 16:13:17 +01:00
|
|
|
|
2022-04-01 10:35:48 +02:00
|
|
|
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
2022-04-26 16:57:05 +02:00
|
|
|
static mbedtls_ssl_mode_t mbedtls_ssl_get_base_mode(
|
2023-01-11 14:50:10 +01:00
|
|
|
psa_algorithm_t alg)
|
2022-03-30 10:57:37 +02:00
|
|
|
{
|
|
|
|
#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (alg == PSA_ALG_CBC_NO_PADDING) {
|
|
|
|
return MBEDTLS_SSL_MODE_CBC;
|
|
|
|
}
|
2022-04-26 16:57:05 +02:00
|
|
|
#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (PSA_ALG_IS_AEAD(alg)) {
|
|
|
|
return MBEDTLS_SSL_MODE_AEAD;
|
|
|
|
}
|
|
|
|
return MBEDTLS_SSL_MODE_STREAM;
|
2022-04-26 16:57:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#else /* MBEDTLS_USE_PSA_CRYPTO */
|
|
|
|
|
|
|
|
static mbedtls_ssl_mode_t mbedtls_ssl_get_base_mode(
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_cipher_mode_t mode)
|
2022-04-26 16:57:05 +02:00
|
|
|
{
|
|
|
|
#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (mode == MBEDTLS_MODE_CBC) {
|
|
|
|
return MBEDTLS_SSL_MODE_CBC;
|
|
|
|
}
|
2022-03-30 10:57:37 +02:00
|
|
|
#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_GCM_C) || \
|
|
|
|
defined(MBEDTLS_CCM_C) || \
|
|
|
|
defined(MBEDTLS_CHACHAPOLY_C)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (mode == MBEDTLS_MODE_GCM ||
|
2022-03-30 10:57:37 +02:00
|
|
|
mode == MBEDTLS_MODE_CCM ||
|
2023-01-11 14:50:10 +01:00
|
|
|
mode == MBEDTLS_MODE_CHACHAPOLY) {
|
|
|
|
return MBEDTLS_SSL_MODE_AEAD;
|
|
|
|
}
|
2022-03-30 10:57:37 +02:00
|
|
|
#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return MBEDTLS_SSL_MODE_STREAM;
|
2022-03-30 10:57:37 +02:00
|
|
|
}
|
2022-04-26 16:57:05 +02:00
|
|
|
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
2022-03-30 10:57:37 +02:00
|
|
|
|
2022-04-26 16:50:40 +02:00
|
|
|
static mbedtls_ssl_mode_t mbedtls_ssl_get_actual_mode(
|
|
|
|
mbedtls_ssl_mode_t base_mode,
|
2023-01-11 14:50:10 +01:00
|
|
|
int encrypt_then_mac)
|
2022-04-26 16:50:40 +02:00
|
|
|
{
|
|
|
|
#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED &&
|
|
|
|
base_mode == MBEDTLS_SSL_MODE_CBC) {
|
|
|
|
return MBEDTLS_SSL_MODE_CBC_ETM;
|
2022-04-26 16:50:40 +02:00
|
|
|
}
|
|
|
|
#else
|
|
|
|
(void) encrypt_then_mac;
|
|
|
|
#endif
|
2023-01-11 14:50:10 +01:00
|
|
|
return base_mode;
|
2022-04-26 16:50:40 +02:00
|
|
|
}
|
|
|
|
|
2022-04-04 11:07:59 +02:00
|
|
|
mbedtls_ssl_mode_t mbedtls_ssl_get_mode_from_transform(
|
2023-01-11 14:50:10 +01:00
|
|
|
const mbedtls_ssl_transform *transform)
|
2022-04-01 10:35:48 +02:00
|
|
|
{
|
2022-04-26 16:50:40 +02:00
|
|
|
mbedtls_ssl_mode_t base_mode = mbedtls_ssl_get_base_mode(
|
2022-04-01 10:35:48 +02:00
|
|
|
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
2023-01-11 14:50:10 +01:00
|
|
|
transform->psa_alg
|
2022-04-01 10:35:48 +02:00
|
|
|
#else
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_cipher_get_cipher_mode(&transform->cipher_ctx_enc)
|
2022-04-26 16:50:40 +02:00
|
|
|
#endif
|
|
|
|
);
|
2022-04-01 10:35:48 +02:00
|
|
|
|
2022-04-26 16:50:40 +02:00
|
|
|
int encrypt_then_mac = 0;
|
2022-04-05 11:16:53 +02:00
|
|
|
#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
|
2022-04-26 16:50:40 +02:00
|
|
|
encrypt_then_mac = transform->encrypt_then_mac;
|
|
|
|
#endif
|
2023-01-11 14:50:10 +01:00
|
|
|
return mbedtls_ssl_get_actual_mode(base_mode, encrypt_then_mac);
|
2022-04-01 10:35:48 +02:00
|
|
|
}
|
|
|
|
|
2022-04-04 11:07:59 +02:00
|
|
|
mbedtls_ssl_mode_t mbedtls_ssl_get_mode_from_ciphersuite(
|
2022-04-05 11:16:53 +02:00
|
|
|
#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
|
2023-01-11 14:50:10 +01:00
|
|
|
int encrypt_then_mac,
|
2022-04-05 11:16:53 +02:00
|
|
|
#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
|
2023-01-11 14:50:10 +01:00
|
|
|
const mbedtls_ssl_ciphersuite_t *suite)
|
2022-04-01 10:35:48 +02:00
|
|
|
{
|
2022-04-26 16:50:40 +02:00
|
|
|
mbedtls_ssl_mode_t base_mode = MBEDTLS_SSL_MODE_STREAM;
|
|
|
|
|
2022-04-01 10:35:48 +02:00
|
|
|
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
|
|
|
psa_status_t status;
|
|
|
|
psa_algorithm_t alg;
|
|
|
|
psa_key_type_t type;
|
|
|
|
size_t size;
|
2023-10-05 14:30:37 +02:00
|
|
|
status = mbedtls_ssl_cipher_to_psa((mbedtls_cipher_type_t) suite->cipher,
|
|
|
|
0, &alg, &type, &size);
|
2023-01-11 14:50:10 +01:00
|
|
|
if (status == PSA_SUCCESS) {
|
|
|
|
base_mode = mbedtls_ssl_get_base_mode(alg);
|
|
|
|
}
|
2022-04-01 10:35:48 +02:00
|
|
|
#else
|
|
|
|
const mbedtls_cipher_info_t *cipher =
|
2023-07-13 14:12:40 +02:00
|
|
|
mbedtls_cipher_info_from_type((mbedtls_cipher_type_t) suite->cipher);
|
2023-01-11 14:50:10 +01:00
|
|
|
if (cipher != NULL) {
|
2022-04-26 16:50:40 +02:00
|
|
|
base_mode =
|
|
|
|
mbedtls_ssl_get_base_mode(
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_cipher_info_get_mode(cipher));
|
2022-04-26 16:50:40 +02:00
|
|
|
}
|
2022-04-01 10:35:48 +02:00
|
|
|
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
|
|
|
|
2022-04-26 16:50:40 +02:00
|
|
|
#if !defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
|
|
|
|
int encrypt_then_mac = 0;
|
|
|
|
#endif
|
2023-01-11 14:50:10 +01:00
|
|
|
return mbedtls_ssl_get_actual_mode(base_mode, encrypt_then_mac);
|
2022-04-01 10:35:48 +02:00
|
|
|
}
|
|
|
|
|
2022-05-18 11:44:56 +02:00
|
|
|
#if defined(MBEDTLS_USE_PSA_CRYPTO) || defined(MBEDTLS_SSL_PROTO_TLS1_3)
|
2022-07-13 09:15:48 +02:00
|
|
|
|
|
|
|
#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
|
2022-07-07 08:55:50 +02:00
|
|
|
/* Serialization of TLS 1.3 sessions:
|
|
|
|
*
|
|
|
|
* struct {
|
2022-10-13 04:22:40 +02:00
|
|
|
* opaque hostname<0..2^16-1>;
|
2023-11-10 07:23:39 +01:00
|
|
|
* uint64 ticket_reception_time;
|
2022-07-07 08:55:50 +02:00
|
|
|
* uint32 ticket_lifetime;
|
2022-08-18 04:32:09 +02:00
|
|
|
* opaque ticket<1..2^16-1>;
|
2022-07-07 08:55:50 +02:00
|
|
|
* } ClientOnlyData;
|
|
|
|
*
|
|
|
|
* struct {
|
|
|
|
* uint32 ticket_age_add;
|
|
|
|
* uint8 ticket_flags;
|
|
|
|
* opaque resumption_key<0..255>;
|
2022-12-12 08:13:20 +01:00
|
|
|
* uint32 max_early_data_size;
|
2023-03-15 09:09:06 +01:00
|
|
|
* uint16 record_size_limit;
|
2022-07-07 08:55:50 +02:00
|
|
|
* select ( endpoint ) {
|
|
|
|
* case client: ClientOnlyData;
|
2023-11-14 04:06:52 +01:00
|
|
|
* case server: uint64 ticket_creation_time;
|
2022-07-07 08:55:50 +02:00
|
|
|
* };
|
|
|
|
* } serialized_session_tls13;
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
#if defined(MBEDTLS_SSL_SESSION_TICKETS)
|
2022-08-17 15:31:36 +02:00
|
|
|
MBEDTLS_CHECK_RETURN_CRITICAL
|
2023-01-11 14:50:10 +01:00
|
|
|
static int ssl_tls13_session_save(const mbedtls_ssl_session *session,
|
|
|
|
unsigned char *buf,
|
|
|
|
size_t buf_len,
|
|
|
|
size_t *olen)
|
2022-07-07 08:55:50 +02:00
|
|
|
{
|
|
|
|
unsigned char *p = buf;
|
2022-10-11 08:20:56 +02:00
|
|
|
#if defined(MBEDTLS_SSL_CLI_C) && \
|
|
|
|
defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
|
2023-01-11 14:50:10 +01:00
|
|
|
size_t hostname_len = (session->hostname == NULL) ?
|
|
|
|
0 : strlen(session->hostname) + 1;
|
2022-10-11 08:20:56 +02:00
|
|
|
#endif
|
2024-01-15 10:21:30 +01:00
|
|
|
size_t needed = 4 /* ticket_age_add */
|
|
|
|
+ 1 /* ticket_flags */
|
|
|
|
+ 1; /* resumption_key length */
|
2022-08-17 15:31:36 +02:00
|
|
|
*olen = 0;
|
2022-08-18 04:32:09 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (session->resumption_key_len > MBEDTLS_SSL_TLS1_3_TICKET_RESUMPTION_KEY_LEN) {
|
|
|
|
return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
|
|
|
|
}
|
2022-08-18 04:32:09 +02:00
|
|
|
needed += session->resumption_key_len; /* resumption_key */
|
|
|
|
|
2022-12-12 08:13:20 +01:00
|
|
|
#if defined(MBEDTLS_SSL_EARLY_DATA)
|
|
|
|
needed += 4; /* max_early_data_size */
|
|
|
|
#endif
|
2023-03-15 09:09:06 +01:00
|
|
|
#if defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT)
|
2023-12-20 18:28:31 +01:00
|
|
|
needed += 2; /* record_size_limit */
|
2023-03-15 09:09:06 +01:00
|
|
|
#endif /* MBEDTLS_SSL_RECORD_SIZE_LIMIT */
|
2022-12-12 08:13:20 +01:00
|
|
|
|
2022-07-07 08:55:50 +02:00
|
|
|
#if defined(MBEDTLS_HAVE_TIME)
|
2023-11-14 04:06:52 +01:00
|
|
|
needed += 8; /* ticket_creation_time or ticket_reception_time */
|
2022-07-07 08:55:50 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_SSL_CLI_C)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (session->endpoint == MBEDTLS_SSL_IS_CLIENT) {
|
2022-10-13 04:22:40 +02:00
|
|
|
#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
|
2022-10-09 13:14:39 +02:00
|
|
|
needed += 2 /* hostname_len */
|
2023-01-11 14:50:10 +01:00
|
|
|
+ hostname_len; /* hostname */
|
2022-09-20 13:35:41 +02:00
|
|
|
#endif
|
|
|
|
|
2022-07-07 08:55:50 +02:00
|
|
|
needed += 4 /* ticket_lifetime */
|
2022-08-17 15:31:36 +02:00
|
|
|
+ 2; /* ticket_len */
|
2022-08-18 04:32:09 +02:00
|
|
|
|
|
|
|
/* Check size_t overflow */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (session->ticket_len > SIZE_MAX - needed) {
|
|
|
|
return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
|
|
|
|
}
|
2022-08-18 04:32:09 +02:00
|
|
|
|
2022-08-18 09:44:03 +02:00
|
|
|
needed += session->ticket_len; /* ticket */
|
2022-07-07 08:55:50 +02:00
|
|
|
}
|
|
|
|
#endif /* MBEDTLS_SSL_CLI_C */
|
|
|
|
|
2022-08-17 15:31:36 +02:00
|
|
|
*olen = needed;
|
2023-01-11 14:50:10 +01:00
|
|
|
if (needed > buf_len) {
|
|
|
|
return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
|
|
|
|
}
|
2022-07-07 08:55:50 +02:00
|
|
|
|
2024-01-15 10:21:30 +01:00
|
|
|
MBEDTLS_PUT_UINT32_BE(session->ticket_age_add, p, 0);
|
|
|
|
p[4] = session->ticket_flags;
|
2022-07-07 08:55:50 +02:00
|
|
|
|
|
|
|
/* save resumption_key */
|
2024-01-15 10:21:30 +01:00
|
|
|
p[5] = session->resumption_key_len;
|
|
|
|
p += 6;
|
2023-01-11 14:50:10 +01:00
|
|
|
memcpy(p, session->resumption_key, session->resumption_key_len);
|
2022-07-21 16:57:37 +02:00
|
|
|
p += session->resumption_key_len;
|
2022-07-07 08:55:50 +02:00
|
|
|
|
2022-12-12 08:13:20 +01:00
|
|
|
#if defined(MBEDTLS_SSL_EARLY_DATA)
|
|
|
|
MBEDTLS_PUT_UINT32_BE(session->max_early_data_size, p, 0);
|
|
|
|
p += 4;
|
|
|
|
#endif
|
2023-03-15 09:09:06 +01:00
|
|
|
#if defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT)
|
|
|
|
MBEDTLS_PUT_UINT16_BE(session->record_size_limit, p, 0);
|
|
|
|
p += 2;
|
|
|
|
#endif /* MBEDTLS_SSL_RECORD_SIZE_LIMIT */
|
2022-12-12 08:13:20 +01:00
|
|
|
|
2022-07-07 08:55:50 +02:00
|
|
|
#if defined(MBEDTLS_HAVE_TIME) && defined(MBEDTLS_SSL_SRV_C)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (session->endpoint == MBEDTLS_SSL_IS_SERVER) {
|
2023-11-10 07:12:20 +01:00
|
|
|
MBEDTLS_PUT_UINT64_BE((uint64_t) session->ticket_creation_time, p, 0);
|
2022-07-07 08:55:50 +02:00
|
|
|
p += 8;
|
|
|
|
}
|
|
|
|
#endif /* MBEDTLS_HAVE_TIME */
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_SSL_CLI_C)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (session->endpoint == MBEDTLS_SSL_IS_CLIENT) {
|
2022-10-12 08:58:13 +02:00
|
|
|
#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_PUT_UINT16_BE(hostname_len, p, 0);
|
2022-10-12 08:58:13 +02:00
|
|
|
p += 2;
|
2023-01-11 14:50:10 +01:00
|
|
|
if (hostname_len > 0) {
|
2022-10-12 08:58:13 +02:00
|
|
|
/* save host name */
|
2023-01-11 14:50:10 +01:00
|
|
|
memcpy(p, session->hostname, hostname_len);
|
2022-10-12 08:58:13 +02:00
|
|
|
p += hostname_len;
|
|
|
|
}
|
|
|
|
#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
|
|
|
|
|
2022-07-07 08:55:50 +02:00
|
|
|
#if defined(MBEDTLS_HAVE_TIME)
|
2023-11-10 07:23:39 +01:00
|
|
|
MBEDTLS_PUT_UINT64_BE((uint64_t) session->ticket_reception_time, p, 0);
|
2022-07-07 08:55:50 +02:00
|
|
|
p += 8;
|
|
|
|
#endif
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_PUT_UINT32_BE(session->ticket_lifetime, p, 0);
|
2022-07-07 08:55:50 +02:00
|
|
|
p += 4;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_PUT_UINT16_BE(session->ticket_len, p, 0);
|
2022-07-07 08:55:50 +02:00
|
|
|
p += 2;
|
2022-08-18 04:32:09 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (session->ticket != NULL && session->ticket_len > 0) {
|
|
|
|
memcpy(p, session->ticket, session->ticket_len);
|
2022-07-07 08:55:50 +02:00
|
|
|
p += session->ticket_len;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif /* MBEDTLS_SSL_CLI_C */
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2022-07-07 08:55:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
MBEDTLS_CHECK_RETURN_CRITICAL
|
2023-01-11 14:50:10 +01:00
|
|
|
static int ssl_tls13_session_load(mbedtls_ssl_session *session,
|
|
|
|
const unsigned char *buf,
|
|
|
|
size_t len)
|
2022-07-07 08:55:50 +02:00
|
|
|
{
|
|
|
|
const unsigned char *p = buf;
|
|
|
|
const unsigned char *end = buf + len;
|
|
|
|
|
2024-01-15 10:21:30 +01:00
|
|
|
if (end - p < 6) {
|
2023-01-11 14:50:10 +01:00
|
|
|
return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
|
|
|
|
}
|
2024-01-15 10:21:30 +01:00
|
|
|
session->ticket_age_add = MBEDTLS_GET_UINT32_BE(p, 0);
|
|
|
|
session->ticket_flags = p[4];
|
2022-07-07 08:55:50 +02:00
|
|
|
|
|
|
|
/* load resumption_key */
|
2024-01-15 10:21:30 +01:00
|
|
|
session->resumption_key_len = p[5];
|
|
|
|
p += 6;
|
2022-07-07 08:55:50 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (end - p < session->resumption_key_len) {
|
|
|
|
return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
|
|
|
|
}
|
2022-07-07 08:55:50 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (sizeof(session->resumption_key) < session->resumption_key_len) {
|
|
|
|
return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
|
|
|
|
}
|
|
|
|
memcpy(session->resumption_key, p, session->resumption_key_len);
|
2022-07-21 16:57:37 +02:00
|
|
|
p += session->resumption_key_len;
|
2022-07-07 08:55:50 +02:00
|
|
|
|
2022-12-12 08:13:20 +01:00
|
|
|
#if defined(MBEDTLS_SSL_EARLY_DATA)
|
|
|
|
if (end - p < 4) {
|
|
|
|
return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
|
|
|
|
}
|
|
|
|
session->max_early_data_size = MBEDTLS_GET_UINT32_BE(p, 0);
|
|
|
|
p += 4;
|
|
|
|
#endif
|
2023-03-15 09:09:06 +01:00
|
|
|
#if defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT)
|
|
|
|
if (end - p < 2) {
|
|
|
|
return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
|
|
|
|
}
|
|
|
|
session->record_size_limit = MBEDTLS_GET_UINT16_BE(p, 0);
|
|
|
|
p += 2;
|
|
|
|
#endif /* MBEDTLS_SSL_RECORD_SIZE_LIMIT */
|
2022-12-12 08:13:20 +01:00
|
|
|
|
2022-07-07 08:55:50 +02:00
|
|
|
#if defined(MBEDTLS_HAVE_TIME) && defined(MBEDTLS_SSL_SRV_C)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (session->endpoint == MBEDTLS_SSL_IS_SERVER) {
|
|
|
|
if (end - p < 8) {
|
|
|
|
return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
|
|
|
|
}
|
2023-11-10 07:12:20 +01:00
|
|
|
session->ticket_creation_time = MBEDTLS_GET_UINT64_BE(p, 0);
|
2022-07-07 08:55:50 +02:00
|
|
|
p += 8;
|
|
|
|
}
|
|
|
|
#endif /* MBEDTLS_HAVE_TIME */
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_SSL_CLI_C)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (session->endpoint == MBEDTLS_SSL_IS_CLIENT) {
|
2023-11-22 03:32:03 +01:00
|
|
|
#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
|
2022-10-10 13:24:08 +02:00
|
|
|
size_t hostname_len;
|
2022-09-20 13:35:41 +02:00
|
|
|
/* load host name */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (end - p < 2) {
|
|
|
|
return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
|
|
|
|
}
|
|
|
|
hostname_len = MBEDTLS_GET_UINT16_BE(p, 0);
|
2022-10-09 13:14:39 +02:00
|
|
|
p += 2;
|
2022-10-08 12:50:19 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (end - p < (long int) hostname_len) {
|
|
|
|
return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
|
|
|
|
}
|
|
|
|
if (hostname_len > 0) {
|
|
|
|
session->hostname = mbedtls_calloc(1, hostname_len);
|
|
|
|
if (session->hostname == NULL) {
|
|
|
|
return MBEDTLS_ERR_SSL_ALLOC_FAILED;
|
|
|
|
}
|
|
|
|
memcpy(session->hostname, p, hostname_len);
|
2022-10-10 13:24:08 +02:00
|
|
|
p += hostname_len;
|
2022-09-28 09:11:02 +02:00
|
|
|
}
|
2023-11-22 03:32:03 +01:00
|
|
|
#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
|
2022-09-20 13:35:41 +02:00
|
|
|
|
2022-07-07 08:55:50 +02:00
|
|
|
#if defined(MBEDTLS_HAVE_TIME)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (end - p < 8) {
|
|
|
|
return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
|
|
|
|
}
|
2023-11-10 07:23:39 +01:00
|
|
|
session->ticket_reception_time = MBEDTLS_GET_UINT64_BE(p, 0);
|
2022-07-07 08:55:50 +02:00
|
|
|
p += 8;
|
|
|
|
#endif
|
2023-01-11 14:50:10 +01:00
|
|
|
if (end - p < 4) {
|
|
|
|
return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
|
|
|
|
}
|
|
|
|
session->ticket_lifetime = MBEDTLS_GET_UINT32_BE(p, 0);
|
2022-07-07 08:55:50 +02:00
|
|
|
p += 4;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (end - p < 2) {
|
|
|
|
return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
|
|
|
|
}
|
|
|
|
session->ticket_len = MBEDTLS_GET_UINT16_BE(p, 0);
|
2022-07-07 08:55:50 +02:00
|
|
|
p += 2;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (end - p < (long int) session->ticket_len) {
|
|
|
|
return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
|
|
|
|
}
|
|
|
|
if (session->ticket_len > 0) {
|
|
|
|
session->ticket = mbedtls_calloc(1, session->ticket_len);
|
|
|
|
if (session->ticket == NULL) {
|
|
|
|
return MBEDTLS_ERR_SSL_ALLOC_FAILED;
|
|
|
|
}
|
|
|
|
memcpy(session->ticket, p, session->ticket_len);
|
2022-07-07 08:55:50 +02:00
|
|
|
p += session->ticket_len;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif /* MBEDTLS_SSL_CLI_C */
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2022-07-07 08:55:50 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
#else /* MBEDTLS_SSL_SESSION_TICKETS */
|
2022-08-17 15:31:36 +02:00
|
|
|
MBEDTLS_CHECK_RETURN_CRITICAL
|
2023-01-11 14:50:10 +01:00
|
|
|
static int ssl_tls13_session_save(const mbedtls_ssl_session *session,
|
|
|
|
unsigned char *buf,
|
|
|
|
size_t buf_len,
|
|
|
|
size_t *olen)
|
2022-07-13 09:15:48 +02:00
|
|
|
{
|
|
|
|
((void) session);
|
|
|
|
((void) buf);
|
|
|
|
((void) buf_len);
|
2022-08-17 15:31:36 +02:00
|
|
|
*olen = 0;
|
2023-01-11 14:50:10 +01:00
|
|
|
return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
|
2022-07-13 09:15:48 +02:00
|
|
|
}
|
2022-07-07 08:55:50 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
static int ssl_tls13_session_load(const mbedtls_ssl_session *session,
|
|
|
|
unsigned char *buf,
|
|
|
|
size_t buf_len)
|
2022-07-07 08:55:50 +02:00
|
|
|
{
|
|
|
|
((void) session);
|
|
|
|
((void) buf);
|
|
|
|
((void) buf_len);
|
2023-01-11 14:50:10 +01:00
|
|
|
return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
|
2022-07-07 08:55:50 +02:00
|
|
|
}
|
|
|
|
#endif /* !MBEDTLS_SSL_SESSION_TICKETS */
|
2022-07-13 09:15:48 +02:00
|
|
|
#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
psa_status_t mbedtls_ssl_cipher_to_psa(mbedtls_cipher_type_t mbedtls_cipher_type,
|
|
|
|
size_t taglen,
|
|
|
|
psa_algorithm_t *alg,
|
|
|
|
psa_key_type_t *key_type,
|
|
|
|
size_t *key_size)
|
2022-02-17 07:23:47 +01:00
|
|
|
{
|
2023-11-16 11:02:05 +01:00
|
|
|
#if !defined(MBEDTLS_SSL_HAVE_CCM)
|
|
|
|
(void) taglen;
|
|
|
|
#endif
|
2023-01-11 14:50:10 +01:00
|
|
|
switch (mbedtls_cipher_type) {
|
2023-11-16 08:17:31 +01:00
|
|
|
#if defined(MBEDTLS_SSL_HAVE_AES) && defined(MBEDTLS_SSL_HAVE_CBC)
|
2022-02-17 07:23:47 +01:00
|
|
|
case MBEDTLS_CIPHER_AES_128_CBC:
|
|
|
|
*alg = PSA_ALG_CBC_NO_PADDING;
|
|
|
|
*key_type = PSA_KEY_TYPE_AES;
|
|
|
|
*key_size = 128;
|
|
|
|
break;
|
2023-11-16 08:17:31 +01:00
|
|
|
#endif
|
|
|
|
#if defined(MBEDTLS_SSL_HAVE_AES) && defined(MBEDTLS_SSL_HAVE_CCM)
|
2022-02-17 07:23:47 +01:00
|
|
|
case MBEDTLS_CIPHER_AES_128_CCM:
|
2023-01-11 14:50:10 +01:00
|
|
|
*alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
|
2022-02-17 07:23:47 +01:00
|
|
|
*key_type = PSA_KEY_TYPE_AES;
|
|
|
|
*key_size = 128;
|
|
|
|
break;
|
2023-11-16 08:17:31 +01:00
|
|
|
#endif
|
|
|
|
#if defined(MBEDTLS_SSL_HAVE_AES) && defined(MBEDTLS_SSL_HAVE_GCM)
|
2022-02-17 07:23:47 +01:00
|
|
|
case MBEDTLS_CIPHER_AES_128_GCM:
|
|
|
|
*alg = PSA_ALG_GCM;
|
|
|
|
*key_type = PSA_KEY_TYPE_AES;
|
|
|
|
*key_size = 128;
|
|
|
|
break;
|
2023-11-16 08:17:31 +01:00
|
|
|
#endif
|
|
|
|
#if defined(MBEDTLS_SSL_HAVE_AES) && defined(MBEDTLS_SSL_HAVE_CCM)
|
2022-02-17 07:23:47 +01:00
|
|
|
case MBEDTLS_CIPHER_AES_192_CCM:
|
2023-01-11 14:50:10 +01:00
|
|
|
*alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
|
2022-02-17 07:23:47 +01:00
|
|
|
*key_type = PSA_KEY_TYPE_AES;
|
|
|
|
*key_size = 192;
|
|
|
|
break;
|
2023-11-16 08:17:31 +01:00
|
|
|
#endif
|
|
|
|
#if defined(MBEDTLS_SSL_HAVE_AES) && defined(MBEDTLS_SSL_HAVE_GCM)
|
2022-02-17 07:23:47 +01:00
|
|
|
case MBEDTLS_CIPHER_AES_192_GCM:
|
|
|
|
*alg = PSA_ALG_GCM;
|
|
|
|
*key_type = PSA_KEY_TYPE_AES;
|
|
|
|
*key_size = 192;
|
|
|
|
break;
|
2023-11-16 08:17:31 +01:00
|
|
|
#endif
|
|
|
|
#if defined(MBEDTLS_SSL_HAVE_AES) && defined(MBEDTLS_SSL_HAVE_CBC)
|
2022-02-17 07:23:47 +01:00
|
|
|
case MBEDTLS_CIPHER_AES_256_CBC:
|
|
|
|
*alg = PSA_ALG_CBC_NO_PADDING;
|
|
|
|
*key_type = PSA_KEY_TYPE_AES;
|
|
|
|
*key_size = 256;
|
|
|
|
break;
|
2023-11-16 08:17:31 +01:00
|
|
|
#endif
|
|
|
|
#if defined(MBEDTLS_SSL_HAVE_AES) && defined(MBEDTLS_SSL_HAVE_CCM)
|
2022-02-17 07:23:47 +01:00
|
|
|
case MBEDTLS_CIPHER_AES_256_CCM:
|
2023-01-11 14:50:10 +01:00
|
|
|
*alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
|
2022-02-17 07:23:47 +01:00
|
|
|
*key_type = PSA_KEY_TYPE_AES;
|
|
|
|
*key_size = 256;
|
|
|
|
break;
|
2023-11-16 08:17:31 +01:00
|
|
|
#endif
|
|
|
|
#if defined(MBEDTLS_SSL_HAVE_AES) && defined(MBEDTLS_SSL_HAVE_GCM)
|
2022-02-17 07:23:47 +01:00
|
|
|
case MBEDTLS_CIPHER_AES_256_GCM:
|
|
|
|
*alg = PSA_ALG_GCM;
|
|
|
|
*key_type = PSA_KEY_TYPE_AES;
|
|
|
|
*key_size = 256;
|
|
|
|
break;
|
2023-11-16 08:17:31 +01:00
|
|
|
#endif
|
|
|
|
#if defined(MBEDTLS_SSL_HAVE_ARIA) && defined(MBEDTLS_SSL_HAVE_CBC)
|
2022-02-17 07:23:47 +01:00
|
|
|
case MBEDTLS_CIPHER_ARIA_128_CBC:
|
|
|
|
*alg = PSA_ALG_CBC_NO_PADDING;
|
|
|
|
*key_type = PSA_KEY_TYPE_ARIA;
|
|
|
|
*key_size = 128;
|
|
|
|
break;
|
2023-11-16 08:17:31 +01:00
|
|
|
#endif
|
|
|
|
#if defined(MBEDTLS_SSL_HAVE_ARIA) && defined(MBEDTLS_SSL_HAVE_CCM)
|
2022-02-17 07:23:47 +01:00
|
|
|
case MBEDTLS_CIPHER_ARIA_128_CCM:
|
2023-01-11 14:50:10 +01:00
|
|
|
*alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
|
2022-02-17 07:23:47 +01:00
|
|
|
*key_type = PSA_KEY_TYPE_ARIA;
|
|
|
|
*key_size = 128;
|
|
|
|
break;
|
2023-11-16 08:17:31 +01:00
|
|
|
#endif
|
|
|
|
#if defined(MBEDTLS_SSL_HAVE_ARIA) && defined(MBEDTLS_SSL_HAVE_GCM)
|
2022-02-17 07:23:47 +01:00
|
|
|
case MBEDTLS_CIPHER_ARIA_128_GCM:
|
|
|
|
*alg = PSA_ALG_GCM;
|
|
|
|
*key_type = PSA_KEY_TYPE_ARIA;
|
|
|
|
*key_size = 128;
|
|
|
|
break;
|
2023-11-16 08:17:31 +01:00
|
|
|
#endif
|
|
|
|
#if defined(MBEDTLS_SSL_HAVE_ARIA) && defined(MBEDTLS_SSL_HAVE_CCM)
|
2022-02-17 07:23:47 +01:00
|
|
|
case MBEDTLS_CIPHER_ARIA_192_CCM:
|
2023-01-11 14:50:10 +01:00
|
|
|
*alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
|
2022-02-17 07:23:47 +01:00
|
|
|
*key_type = PSA_KEY_TYPE_ARIA;
|
|
|
|
*key_size = 192;
|
|
|
|
break;
|
2023-11-16 08:17:31 +01:00
|
|
|
#endif
|
|
|
|
#if defined(MBEDTLS_SSL_HAVE_ARIA) && defined(MBEDTLS_SSL_HAVE_GCM)
|
2022-02-17 07:23:47 +01:00
|
|
|
case MBEDTLS_CIPHER_ARIA_192_GCM:
|
|
|
|
*alg = PSA_ALG_GCM;
|
|
|
|
*key_type = PSA_KEY_TYPE_ARIA;
|
|
|
|
*key_size = 192;
|
|
|
|
break;
|
2023-11-16 08:17:31 +01:00
|
|
|
#endif
|
|
|
|
#if defined(MBEDTLS_SSL_HAVE_ARIA) && defined(MBEDTLS_SSL_HAVE_CBC)
|
2022-02-17 07:23:47 +01:00
|
|
|
case MBEDTLS_CIPHER_ARIA_256_CBC:
|
|
|
|
*alg = PSA_ALG_CBC_NO_PADDING;
|
|
|
|
*key_type = PSA_KEY_TYPE_ARIA;
|
|
|
|
*key_size = 256;
|
|
|
|
break;
|
2023-11-16 08:17:31 +01:00
|
|
|
#endif
|
|
|
|
#if defined(MBEDTLS_SSL_HAVE_ARIA) && defined(MBEDTLS_SSL_HAVE_CCM)
|
2022-02-17 07:23:47 +01:00
|
|
|
case MBEDTLS_CIPHER_ARIA_256_CCM:
|
2023-01-11 14:50:10 +01:00
|
|
|
*alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
|
2022-02-17 07:23:47 +01:00
|
|
|
*key_type = PSA_KEY_TYPE_ARIA;
|
|
|
|
*key_size = 256;
|
|
|
|
break;
|
2023-11-16 08:17:31 +01:00
|
|
|
#endif
|
|
|
|
#if defined(MBEDTLS_SSL_HAVE_ARIA) && defined(MBEDTLS_SSL_HAVE_GCM)
|
2022-02-17 07:23:47 +01:00
|
|
|
case MBEDTLS_CIPHER_ARIA_256_GCM:
|
|
|
|
*alg = PSA_ALG_GCM;
|
|
|
|
*key_type = PSA_KEY_TYPE_ARIA;
|
|
|
|
*key_size = 256;
|
|
|
|
break;
|
2023-11-16 08:17:31 +01:00
|
|
|
#endif
|
|
|
|
#if defined(MBEDTLS_SSL_HAVE_CAMELLIA) && defined(MBEDTLS_SSL_HAVE_CBC)
|
2022-02-17 07:23:47 +01:00
|
|
|
case MBEDTLS_CIPHER_CAMELLIA_128_CBC:
|
|
|
|
*alg = PSA_ALG_CBC_NO_PADDING;
|
|
|
|
*key_type = PSA_KEY_TYPE_CAMELLIA;
|
|
|
|
*key_size = 128;
|
|
|
|
break;
|
2023-11-16 08:17:31 +01:00
|
|
|
#endif
|
|
|
|
#if defined(MBEDTLS_SSL_HAVE_CAMELLIA) && defined(MBEDTLS_SSL_HAVE_CCM)
|
2022-02-17 07:23:47 +01:00
|
|
|
case MBEDTLS_CIPHER_CAMELLIA_128_CCM:
|
2023-01-11 14:50:10 +01:00
|
|
|
*alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
|
2022-02-17 07:23:47 +01:00
|
|
|
*key_type = PSA_KEY_TYPE_CAMELLIA;
|
|
|
|
*key_size = 128;
|
|
|
|
break;
|
2023-11-16 08:17:31 +01:00
|
|
|
#endif
|
|
|
|
#if defined(MBEDTLS_SSL_HAVE_CAMELLIA) && defined(MBEDTLS_SSL_HAVE_GCM)
|
2022-02-17 07:23:47 +01:00
|
|
|
case MBEDTLS_CIPHER_CAMELLIA_128_GCM:
|
|
|
|
*alg = PSA_ALG_GCM;
|
|
|
|
*key_type = PSA_KEY_TYPE_CAMELLIA;
|
|
|
|
*key_size = 128;
|
|
|
|
break;
|
2023-11-16 08:17:31 +01:00
|
|
|
#endif
|
|
|
|
#if defined(MBEDTLS_SSL_HAVE_CAMELLIA) && defined(MBEDTLS_SSL_HAVE_CCM)
|
2022-02-17 07:23:47 +01:00
|
|
|
case MBEDTLS_CIPHER_CAMELLIA_192_CCM:
|
2023-01-11 14:50:10 +01:00
|
|
|
*alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
|
2022-02-17 07:23:47 +01:00
|
|
|
*key_type = PSA_KEY_TYPE_CAMELLIA;
|
|
|
|
*key_size = 192;
|
|
|
|
break;
|
2023-11-16 08:17:31 +01:00
|
|
|
#endif
|
|
|
|
#if defined(MBEDTLS_SSL_HAVE_CAMELLIA) && defined(MBEDTLS_SSL_HAVE_GCM)
|
2022-02-17 07:23:47 +01:00
|
|
|
case MBEDTLS_CIPHER_CAMELLIA_192_GCM:
|
|
|
|
*alg = PSA_ALG_GCM;
|
|
|
|
*key_type = PSA_KEY_TYPE_CAMELLIA;
|
|
|
|
*key_size = 192;
|
|
|
|
break;
|
2023-11-16 08:17:31 +01:00
|
|
|
#endif
|
|
|
|
#if defined(MBEDTLS_SSL_HAVE_CAMELLIA) && defined(MBEDTLS_SSL_HAVE_CBC)
|
2022-02-17 07:23:47 +01:00
|
|
|
case MBEDTLS_CIPHER_CAMELLIA_256_CBC:
|
|
|
|
*alg = PSA_ALG_CBC_NO_PADDING;
|
|
|
|
*key_type = PSA_KEY_TYPE_CAMELLIA;
|
|
|
|
*key_size = 256;
|
|
|
|
break;
|
2023-11-16 08:17:31 +01:00
|
|
|
#endif
|
|
|
|
#if defined(MBEDTLS_SSL_HAVE_CAMELLIA) && defined(MBEDTLS_SSL_HAVE_CCM)
|
2022-02-17 07:23:47 +01:00
|
|
|
case MBEDTLS_CIPHER_CAMELLIA_256_CCM:
|
2023-01-11 14:50:10 +01:00
|
|
|
*alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
|
2022-02-17 07:23:47 +01:00
|
|
|
*key_type = PSA_KEY_TYPE_CAMELLIA;
|
|
|
|
*key_size = 256;
|
|
|
|
break;
|
2023-11-16 08:17:31 +01:00
|
|
|
#endif
|
|
|
|
#if defined(MBEDTLS_SSL_HAVE_CAMELLIA) && defined(MBEDTLS_SSL_HAVE_GCM)
|
2022-02-17 07:23:47 +01:00
|
|
|
case MBEDTLS_CIPHER_CAMELLIA_256_GCM:
|
|
|
|
*alg = PSA_ALG_GCM;
|
|
|
|
*key_type = PSA_KEY_TYPE_CAMELLIA;
|
|
|
|
*key_size = 256;
|
|
|
|
break;
|
2023-11-16 08:17:31 +01:00
|
|
|
#endif
|
|
|
|
#if defined(MBEDTLS_SSL_HAVE_CHACHAPOLY)
|
2022-02-17 07:23:47 +01:00
|
|
|
case MBEDTLS_CIPHER_CHACHA20_POLY1305:
|
|
|
|
*alg = PSA_ALG_CHACHA20_POLY1305;
|
|
|
|
*key_type = PSA_KEY_TYPE_CHACHA20;
|
|
|
|
*key_size = 256;
|
|
|
|
break;
|
2023-11-16 08:17:31 +01:00
|
|
|
#endif
|
2022-02-17 07:23:47 +01:00
|
|
|
case MBEDTLS_CIPHER_NULL:
|
|
|
|
*alg = MBEDTLS_SSL_NULL_CIPHER;
|
|
|
|
*key_type = 0;
|
|
|
|
*key_size = 0;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return PSA_ERROR_NOT_SUPPORTED;
|
|
|
|
}
|
2015-12-03 16:13:17 +01:00
|
|
|
|
2022-02-17 07:23:47 +01:00
|
|
|
return PSA_SUCCESS;
|
|
|
|
}
|
2022-05-18 11:44:56 +02:00
|
|
|
#endif /* MBEDTLS_USE_PSA_CRYPTO || MBEDTLS_SSL_PROTO_TLS1_3 */
|
2019-07-11 13:29:35 +02:00
|
|
|
|
2022-02-17 07:23:47 +01:00
|
|
|
#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
|
2023-01-11 14:50: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)
|
2022-02-17 07:23:47 +01:00
|
|
|
{
|
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2021-08-03 10:39:42 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_mpi_free(&conf->dhm_P);
|
|
|
|
mbedtls_mpi_free(&conf->dhm_G);
|
2020-02-05 15:44:10 +01:00
|
|
|
|
2023-01-11 14:50: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) {
|
|
|
|
mbedtls_mpi_free(&conf->dhm_P);
|
|
|
|
mbedtls_mpi_free(&conf->dhm_G);
|
|
|
|
return ret;
|
2020-02-05 15:44:10 +01:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2022-02-17 07:23:47 +01:00
|
|
|
}
|
2021-08-03 10:39:42 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_ssl_conf_dh_param_ctx(mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx)
|
2022-02-17 07:23:47 +01:00
|
|
|
{
|
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2021-08-03 10:39:42 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_mpi_free(&conf->dhm_P);
|
|
|
|
mbedtls_mpi_free(&conf->dhm_G);
|
2022-02-17 07:23:47 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if ((ret = mbedtls_dhm_get_value(dhm_ctx, MBEDTLS_DHM_PARAM_P,
|
|
|
|
&conf->dhm_P)) != 0 ||
|
|
|
|
(ret = mbedtls_dhm_get_value(dhm_ctx, MBEDTLS_DHM_PARAM_G,
|
|
|
|
&conf->dhm_G)) != 0) {
|
|
|
|
mbedtls_mpi_free(&conf->dhm_P);
|
|
|
|
mbedtls_mpi_free(&conf->dhm_G);
|
|
|
|
return ret;
|
2020-02-05 15:44:10 +01:00
|
|
|
}
|
2022-01-21 03:53:13 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2022-02-17 07:23:47 +01:00
|
|
|
}
|
|
|
|
#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
|
2022-01-21 03:53:13 +01:00
|
|
|
|
2022-02-17 07:23:47 +01:00
|
|
|
#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
|
|
|
|
/*
|
|
|
|
* Set the minimum length for Diffie-Hellman parameters
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
void mbedtls_ssl_conf_dhm_min_bitlen(mbedtls_ssl_config *conf,
|
|
|
|
unsigned int bitlen)
|
2022-02-17 07:23:47 +01:00
|
|
|
{
|
|
|
|
conf->dhm_min_bitlen = bitlen;
|
|
|
|
}
|
|
|
|
#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
|
2022-01-21 03:53:13 +01:00
|
|
|
|
2022-10-05 12:46:29 +02:00
|
|
|
#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
|
2022-02-17 07:23:47 +01:00
|
|
|
#if !defined(MBEDTLS_DEPRECATED_REMOVED) && defined(MBEDTLS_SSL_PROTO_TLS1_2)
|
|
|
|
/*
|
|
|
|
* Set allowed/preferred hashes for handshake signatures
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
void mbedtls_ssl_conf_sig_hashes(mbedtls_ssl_config *conf,
|
|
|
|
const int *hashes)
|
2022-02-17 07:23:47 +01:00
|
|
|
{
|
|
|
|
conf->sig_hashes = hashes;
|
|
|
|
}
|
|
|
|
#endif /* !MBEDTLS_DEPRECATED_REMOVED && MBEDTLS_SSL_PROTO_TLS1_2 */
|
2022-01-21 03:53:13 +01:00
|
|
|
|
2022-02-17 07:23:47 +01:00
|
|
|
/* Configure allowed signature algorithms for handshake */
|
2023-01-11 14:50:10 +01:00
|
|
|
void mbedtls_ssl_conf_sig_algs(mbedtls_ssl_config *conf,
|
|
|
|
const uint16_t *sig_algs)
|
2022-02-17 07:23:47 +01:00
|
|
|
{
|
|
|
|
#if !defined(MBEDTLS_DEPRECATED_REMOVED)
|
|
|
|
conf->sig_hashes = NULL;
|
|
|
|
#endif /* !MBEDTLS_DEPRECATED_REMOVED */
|
|
|
|
conf->sig_algs = sig_algs;
|
2021-08-03 10:39:42 +02:00
|
|
|
}
|
2022-10-05 12:46:29 +02:00
|
|
|
#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
|
2021-08-03 10:39:42 +02:00
|
|
|
|
2023-09-22 11:53:41 +02:00
|
|
|
#if defined(MBEDTLS_ECP_C)
|
2022-02-17 07:23:47 +01:00
|
|
|
#if !defined(MBEDTLS_DEPRECATED_REMOVED)
|
|
|
|
/*
|
|
|
|
* Set the allowed elliptic curves
|
|
|
|
*
|
|
|
|
* mbedtls_ssl_setup() takes the provided list
|
|
|
|
* and translates it to a list of IANA TLS group identifiers,
|
|
|
|
* stored in ssl->handshake->group_list.
|
|
|
|
*
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
void mbedtls_ssl_conf_curves(mbedtls_ssl_config *conf,
|
|
|
|
const mbedtls_ecp_group_id *curve_list)
|
2021-08-03 10:39:42 +02:00
|
|
|
{
|
2022-02-17 07:23:47 +01:00
|
|
|
conf->curve_list = curve_list;
|
|
|
|
conf->group_list = NULL;
|
|
|
|
}
|
|
|
|
#endif /* MBEDTLS_DEPRECATED_REMOVED */
|
2023-09-22 11:53:41 +02:00
|
|
|
#endif /* MBEDTLS_ECP_C */
|
2021-08-03 10:39:42 +02:00
|
|
|
|
2022-02-17 07:23:47 +01:00
|
|
|
/*
|
|
|
|
* Set the allowed groups
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
void mbedtls_ssl_conf_groups(mbedtls_ssl_config *conf,
|
|
|
|
const uint16_t *group_list)
|
2022-02-17 07:23:47 +01:00
|
|
|
{
|
2023-09-22 11:53:41 +02:00
|
|
|
#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
|
2022-02-17 07:23:47 +01:00
|
|
|
conf->curve_list = NULL;
|
|
|
|
#endif
|
|
|
|
conf->group_list = group_list;
|
|
|
|
}
|
2021-08-03 10:39:42 +02:00
|
|
|
|
2022-02-17 07:23:47 +01:00
|
|
|
#if defined(MBEDTLS_X509_CRT_PARSE_C)
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_ssl_set_hostname(mbedtls_ssl_context *ssl, const char *hostname)
|
2022-02-17 07:23:47 +01:00
|
|
|
{
|
|
|
|
/* Initialize to suppress unnecessary compiler warning */
|
|
|
|
size_t hostname_len = 0;
|
2021-08-03 10:39:42 +02:00
|
|
|
|
2022-02-17 07:23:47 +01:00
|
|
|
/* Check if new hostname is valid before
|
|
|
|
* making any change to current one */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (hostname != NULL) {
|
|
|
|
hostname_len = strlen(hostname);
|
2017-05-26 17:07:36 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN) {
|
|
|
|
return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
|
|
|
|
}
|
2022-02-17 07:23:47 +01:00
|
|
|
}
|
2021-08-03 10:39:42 +02:00
|
|
|
|
2022-02-17 07:23:47 +01:00
|
|
|
/* Now it's clear that we will overwrite the old hostname,
|
|
|
|
* so we can free it safely */
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->hostname != NULL) {
|
2023-07-17 16:17:40 +02:00
|
|
|
mbedtls_zeroize_and_free(ssl->hostname, strlen(ssl->hostname));
|
2020-02-05 15:44:10 +01:00
|
|
|
}
|
2017-05-26 17:07:36 +02:00
|
|
|
|
2022-02-17 07:23:47 +01:00
|
|
|
/* Passing NULL as hostname shall clear the old one */
|
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
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (hostname == NULL) {
|
2022-02-17 07:23:47 +01:00
|
|
|
ssl->hostname = NULL;
|
2023-01-11 14:50:10 +01:00
|
|
|
} else {
|
|
|
|
ssl->hostname = mbedtls_calloc(1, hostname_len + 1);
|
|
|
|
if (ssl->hostname == NULL) {
|
|
|
|
return MBEDTLS_ERR_SSL_ALLOC_FAILED;
|
|
|
|
}
|
2020-02-05 15:44:10 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
memcpy(ssl->hostname, hostname, hostname_len);
|
2022-02-17 07:23:47 +01:00
|
|
|
|
|
|
|
ssl->hostname[hostname_len] = '\0';
|
|
|
|
}
|
2017-05-26 17:07:36 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2014-09-08 16:14:10 +02:00
|
|
|
}
|
2022-02-17 07:23:47 +01:00
|
|
|
#endif /* MBEDTLS_X509_CRT_PARSE_C */
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2022-02-17 07:23:47 +01:00
|
|
|
#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
|
2023-01-11 14:50:10 +01:00
|
|
|
void mbedtls_ssl_conf_sni(mbedtls_ssl_config *conf,
|
|
|
|
int (*f_sni)(void *, mbedtls_ssl_context *,
|
|
|
|
const unsigned char *, size_t),
|
|
|
|
void *p_sni)
|
2014-09-08 16:14:10 +02:00
|
|
|
{
|
2022-02-17 07:23:47 +01:00
|
|
|
conf->f_sni = f_sni;
|
|
|
|
conf->p_sni = p_sni;
|
2020-02-05 15:44:10 +01:00
|
|
|
}
|
2022-02-17 07:23:47 +01:00
|
|
|
#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_SSL_ALPN)
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_ssl_conf_alpn_protocols(mbedtls_ssl_config *conf, const char **protos)
|
2022-02-17 07:23:47 +01:00
|
|
|
{
|
|
|
|
size_t cur_len, tot_len;
|
|
|
|
const char **p;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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;
|
2023-01-11 14:50:10 +01:00
|
|
|
for (p = protos; *p != NULL; p++) {
|
|
|
|
cur_len = strlen(*p);
|
2022-02-17 07:23:47 +01:00
|
|
|
tot_len += cur_len;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if ((cur_len == 0) ||
|
|
|
|
(cur_len > MBEDTLS_SSL_MAX_ALPN_NAME_LEN) ||
|
|
|
|
(tot_len > MBEDTLS_SSL_MAX_ALPN_LIST_LEN)) {
|
|
|
|
return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
|
|
|
|
}
|
2022-02-17 07:23:47 +01:00
|
|
|
}
|
2014-07-10 17:54:52 +02:00
|
|
|
|
2022-02-17 07:23:47 +01:00
|
|
|
conf->alpn_list = protos;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2020-02-05 15:44:10 +01:00
|
|
|
}
|
2012-05-08 11:17:57 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
const char *mbedtls_ssl_get_alpn_protocol(const mbedtls_ssl_context *ssl)
|
2020-02-05 15:44:10 +01:00
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
return ssl->alpn_chosen;
|
2020-02-05 15:44:10 +01:00
|
|
|
}
|
2022-02-17 07:23:47 +01:00
|
|
|
#endif /* MBEDTLS_SSL_ALPN */
|
2012-12-19 14:41:14 +01:00
|
|
|
|
2022-02-17 07:23:47 +01:00
|
|
|
#if defined(MBEDTLS_SSL_DTLS_SRTP)
|
2023-01-11 14:50:10 +01:00
|
|
|
void mbedtls_ssl_conf_srtp_mki_value_supported(mbedtls_ssl_config *conf,
|
|
|
|
int support_mki_value)
|
2020-02-05 15:44:10 +01:00
|
|
|
{
|
2022-02-17 07:23:47 +01:00
|
|
|
conf->dtls_srtp_mki_support = support_mki_value;
|
2020-02-05 15:44:10 +01:00
|
|
|
}
|
2017-12-27 22:28:58 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_ssl_dtls_srtp_set_mki_value(mbedtls_ssl_context *ssl,
|
|
|
|
unsigned char *mki_value,
|
|
|
|
uint16_t mki_len)
|
2020-02-05 15:44:10 +01:00
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
if (mki_len > MBEDTLS_TLS_SRTP_MAX_MKI_LENGTH) {
|
|
|
|
return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
|
2022-02-17 07:23:47 +01:00
|
|
|
}
|
2019-05-14 12:30:10 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->conf->dtls_srtp_mki_support == MBEDTLS_SSL_DTLS_SRTP_MKI_UNSUPPORTED) {
|
|
|
|
return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
|
2022-02-17 07:23:47 +01:00
|
|
|
}
|
2019-05-08 14:02:55 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
memcpy(ssl->dtls_srtp_info.mki_value, mki_value, mki_len);
|
2022-02-17 07:23:47 +01:00
|
|
|
ssl->dtls_srtp_info.mki_len = mki_len;
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2020-02-05 15:44:10 +01:00
|
|
|
}
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_ssl_conf_dtls_srtp_protection_profiles(mbedtls_ssl_config *conf,
|
|
|
|
const mbedtls_ssl_srtp_profile *profiles)
|
2020-02-05 15:44:10 +01:00
|
|
|
{
|
2022-02-17 07:23:47 +01:00
|
|
|
const mbedtls_ssl_srtp_profile *p;
|
|
|
|
size_t list_size = 0;
|
2017-12-27 22:28:58 +01:00
|
|
|
|
2022-02-17 07:23:47 +01:00
|
|
|
/* check the profiles list: all entry must be valid,
|
|
|
|
* its size cannot be more than the total number of supported profiles, currently 4 */
|
2023-01-11 14:50:10 +01:00
|
|
|
for (p = profiles; *p != MBEDTLS_TLS_SRTP_UNSET &&
|
|
|
|
list_size <= MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH;
|
|
|
|
p++) {
|
|
|
|
if (mbedtls_ssl_check_srtp_profile_value(*p) != MBEDTLS_TLS_SRTP_UNSET) {
|
2022-02-17 07:23:47 +01:00
|
|
|
list_size++;
|
2023-01-11 14:50:10 +01:00
|
|
|
} else {
|
2022-02-17 07:23:47 +01:00
|
|
|
/* unsupported value, stop parsing and set the size to an error value */
|
|
|
|
list_size = MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH + 1;
|
|
|
|
}
|
|
|
|
}
|
2018-08-07 15:29:29 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (list_size > MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH) {
|
|
|
|
conf->dtls_srtp_profile_list = NULL;
|
|
|
|
conf->dtls_srtp_profile_list_len = 0;
|
|
|
|
return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
|
2022-02-17 07:23:47 +01:00
|
|
|
}
|
2019-05-08 12:57:13 +02:00
|
|
|
|
2022-02-17 07:23:47 +01:00
|
|
|
conf->dtls_srtp_profile_list = profiles;
|
|
|
|
conf->dtls_srtp_profile_list_len = list_size;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2020-02-05 15:44:10 +01:00
|
|
|
}
|
2017-12-27 22:28:58 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
void mbedtls_ssl_get_dtls_srtp_negotiation_result(const mbedtls_ssl_context *ssl,
|
|
|
|
mbedtls_dtls_srtp_info *dtls_srtp_info)
|
2020-02-05 15:44:10 +01:00
|
|
|
{
|
2022-02-17 07:23:47 +01:00
|
|
|
dtls_srtp_info->chosen_dtls_srtp_profile = ssl->dtls_srtp_info.chosen_dtls_srtp_profile;
|
|
|
|
/* do not copy the mki value if there is no chosen profile */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (dtls_srtp_info->chosen_dtls_srtp_profile == MBEDTLS_TLS_SRTP_UNSET) {
|
2022-02-17 07:23:47 +01:00
|
|
|
dtls_srtp_info->mki_len = 0;
|
2023-01-11 14:50:10 +01:00
|
|
|
} else {
|
2022-02-17 07:23:47 +01:00
|
|
|
dtls_srtp_info->mki_len = ssl->dtls_srtp_info.mki_len;
|
2023-01-11 14:50:10 +01:00
|
|
|
memcpy(dtls_srtp_info->mki_value, ssl->dtls_srtp_info.mki_value,
|
|
|
|
ssl->dtls_srtp_info.mki_len);
|
2022-02-17 07:23:47 +01:00
|
|
|
}
|
2020-02-05 15:44:10 +01:00
|
|
|
}
|
2022-02-17 07:23:47 +01:00
|
|
|
#endif /* MBEDTLS_SSL_DTLS_SRTP */
|
2017-12-27 22:28:58 +01:00
|
|
|
|
2022-07-26 11:01:46 +02:00
|
|
|
#if !defined(MBEDTLS_DEPRECATED_REMOVED)
|
2023-01-11 14:50:10 +01:00
|
|
|
void mbedtls_ssl_conf_max_version(mbedtls_ssl_config *conf, int major, int minor)
|
2020-02-05 15:44:10 +01:00
|
|
|
{
|
2023-07-13 14:12:40 +02:00
|
|
|
conf->max_tls_version = (mbedtls_ssl_protocol_version) ((major << 8) | minor);
|
2020-02-05 15:44:10 +01:00
|
|
|
}
|
2017-12-27 22:28:58 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
void mbedtls_ssl_conf_min_version(mbedtls_ssl_config *conf, int major, int minor)
|
2020-02-05 15:44:10 +01:00
|
|
|
{
|
2023-07-13 14:12:40 +02:00
|
|
|
conf->min_tls_version = (mbedtls_ssl_protocol_version) ((major << 8) | minor);
|
2020-02-05 15:44:10 +01:00
|
|
|
}
|
2022-07-26 11:01:46 +02:00
|
|
|
#endif /* MBEDTLS_DEPRECATED_REMOVED */
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2022-02-17 07:23:47 +01:00
|
|
|
#if defined(MBEDTLS_SSL_SRV_C)
|
2023-01-11 14:50:10 +01:00
|
|
|
void mbedtls_ssl_conf_cert_req_ca_list(mbedtls_ssl_config *conf,
|
|
|
|
char cert_req_ca_list)
|
2020-02-05 15:44:10 +01:00
|
|
|
{
|
2022-02-17 07:23:47 +01:00
|
|
|
conf->cert_req_ca_list = cert_req_ca_list;
|
2020-02-05 15:44:10 +01:00
|
|
|
}
|
2022-02-17 07:23:47 +01:00
|
|
|
#endif
|
2014-09-24 13:29:58 +02:00
|
|
|
|
2022-02-17 07:23:47 +01:00
|
|
|
#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
|
2023-01-11 14:50:10 +01:00
|
|
|
void mbedtls_ssl_conf_encrypt_then_mac(mbedtls_ssl_config *conf, char etm)
|
2020-02-05 15:44:10 +01:00
|
|
|
{
|
2022-02-17 07:23:47 +01:00
|
|
|
conf->encrypt_then_mac = etm;
|
2014-09-08 16:14:10 +02:00
|
|
|
}
|
2022-02-17 07:23:47 +01:00
|
|
|
#endif
|
2014-09-08 16:14:10 +02:00
|
|
|
|
2022-02-17 07:23:47 +01:00
|
|
|
#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
|
2023-01-11 14:50:10 +01:00
|
|
|
void mbedtls_ssl_conf_extended_master_secret(mbedtls_ssl_config *conf, char ems)
|
2020-02-05 15:44:10 +01:00
|
|
|
{
|
2022-02-17 07:23:47 +01:00
|
|
|
conf->extended_ms = ems;
|
2020-02-05 15:44:10 +01:00
|
|
|
}
|
2022-02-17 07:23:47 +01:00
|
|
|
#endif
|
2018-08-15 15:05:04 +02:00
|
|
|
|
2022-02-17 07:23:47 +01:00
|
|
|
#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_ssl_conf_max_frag_len(mbedtls_ssl_config *conf, unsigned char mfl_code)
|
2014-09-08 16:14:10 +02:00
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
if (mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
|
|
|
|
ssl_mfl_code_to_length(mfl_code) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN) {
|
|
|
|
return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
|
2020-02-05 15:44:10 +01:00
|
|
|
}
|
2017-05-24 10:16:26 +02:00
|
|
|
|
2022-02-17 07:23:47 +01:00
|
|
|
conf->mfl_code = mfl_code;
|
2018-08-15 15:26:08 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2020-02-05 15:44:10 +01:00
|
|
|
}
|
2022-02-17 07:23:47 +01:00
|
|
|
#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
|
2018-08-15 15:48:01 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
void mbedtls_ssl_conf_legacy_renegotiation(mbedtls_ssl_config *conf, int allow_legacy)
|
2020-02-05 15:44:10 +01:00
|
|
|
{
|
2022-02-17 07:23:47 +01:00
|
|
|
conf->allow_legacy_renegotiation = allow_legacy;
|
2020-02-05 15:44:10 +01:00
|
|
|
}
|
2018-08-15 15:48:01 +02:00
|
|
|
|
2022-02-17 07:23:47 +01:00
|
|
|
#if defined(MBEDTLS_SSL_RENEGOTIATION)
|
2023-01-11 14:50:10 +01:00
|
|
|
void mbedtls_ssl_conf_renegotiation(mbedtls_ssl_config *conf, int renegotiation)
|
2021-07-24 07:01:47 +02:00
|
|
|
{
|
2022-02-17 07:23:47 +01:00
|
|
|
conf->disable_renegotiation = renegotiation;
|
2021-07-24 07:01:47 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
void mbedtls_ssl_conf_renegotiation_enforced(mbedtls_ssl_config *conf, int max_records)
|
2020-02-05 15:44:10 +01:00
|
|
|
{
|
2022-02-17 07:23:47 +01:00
|
|
|
conf->renego_max_records = max_records;
|
2020-02-05 15:44:10 +01:00
|
|
|
}
|
2017-05-24 10:16:26 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
void mbedtls_ssl_conf_renegotiation_period(mbedtls_ssl_config *conf,
|
|
|
|
const unsigned char period[8])
|
2020-02-05 15:44:10 +01:00
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
memcpy(conf->renego_period, period, 8);
|
2016-10-13 18:21:01 +02:00
|
|
|
}
|
2022-02-17 07:23:47 +01:00
|
|
|
#endif /* MBEDTLS_SSL_RENEGOTIATION */
|
2016-10-13 18:21:01 +02:00
|
|
|
|
2022-02-17 07:23:47 +01:00
|
|
|
#if defined(MBEDTLS_SSL_SESSION_TICKETS)
|
|
|
|
#if defined(MBEDTLS_SSL_CLI_C)
|
2023-01-11 14:50:10 +01:00
|
|
|
void mbedtls_ssl_conf_session_tickets(mbedtls_ssl_config *conf, int use_tickets)
|
2016-10-13 18:21:01 +02:00
|
|
|
{
|
2022-02-17 07:23:47 +01:00
|
|
|
conf->session_tickets = use_tickets;
|
2018-08-15 15:48:01 +02:00
|
|
|
}
|
2022-02-17 07:23:47 +01:00
|
|
|
#endif
|
2018-08-15 15:48:01 +02:00
|
|
|
|
2022-02-17 07:23:47 +01:00
|
|
|
#if defined(MBEDTLS_SSL_SRV_C)
|
2022-08-09 07:28:39 +02:00
|
|
|
|
2022-09-22 04:46:57 +02:00
|
|
|
#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && defined(MBEDTLS_SSL_SESSION_TICKETS)
|
2023-01-11 14:50:10 +01:00
|
|
|
void mbedtls_ssl_conf_new_session_tickets(mbedtls_ssl_config *conf,
|
|
|
|
uint16_t num_tickets)
|
2022-08-09 07:28:39 +02:00
|
|
|
{
|
2022-09-22 04:46:57 +02:00
|
|
|
conf->new_session_tickets_count = num_tickets;
|
2022-08-09 07:28:39 +02:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2023-01-11 14:50:10 +01: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)
|
2018-08-15 15:48:01 +02:00
|
|
|
{
|
2022-02-17 07:23:47 +01:00
|
|
|
conf->f_ticket_write = f_ticket_write;
|
|
|
|
conf->f_ticket_parse = f_ticket_parse;
|
|
|
|
conf->p_ticket = p_ticket;
|
2020-02-05 15:44:10 +01:00
|
|
|
}
|
2022-02-17 07:23:47 +01:00
|
|
|
#endif
|
|
|
|
#endif /* MBEDTLS_SSL_SESSION_TICKETS */
|
2018-08-15 16:11:57 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
void mbedtls_ssl_set_export_keys_cb(mbedtls_ssl_context *ssl,
|
|
|
|
mbedtls_ssl_export_keys_t *f_export_keys,
|
|
|
|
void *p_export_keys)
|
2020-02-05 15:44:10 +01:00
|
|
|
{
|
2022-02-17 07:23:47 +01:00
|
|
|
ssl->f_export_keys = f_export_keys;
|
|
|
|
ssl->p_export_keys = p_export_keys;
|
2020-02-05 15:44:10 +01:00
|
|
|
}
|
2018-08-15 16:11:57 +02:00
|
|
|
|
2022-02-17 07:23:47 +01:00
|
|
|
#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
|
|
|
|
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,
|
2023-01-11 14:50:10 +01:00
|
|
|
void *async_config_data)
|
2020-02-05 15:44:10 +01:00
|
|
|
{
|
2022-02-17 07:23:47 +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;
|
|
|
|
conf->p_async_config_data = async_config_data;
|
2020-02-05 15:44:10 +01:00
|
|
|
}
|
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
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
void *mbedtls_ssl_conf_get_async_config_data(const mbedtls_ssl_config *conf)
|
2020-02-05 15:44:10 +01:00
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
return conf->p_async_config_data;
|
2020-02-05 15:44:10 +01:00
|
|
|
}
|
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
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
void *mbedtls_ssl_get_async_operation_data(const mbedtls_ssl_context *ssl)
|
2020-02-05 15:44:10 +01:00
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->handshake == NULL) {
|
|
|
|
return NULL;
|
|
|
|
} else {
|
|
|
|
return ssl->handshake->user_async_ctx;
|
|
|
|
}
|
2020-02-05 15:44:10 +01:00
|
|
|
}
|
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
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
void mbedtls_ssl_set_async_operation_data(mbedtls_ssl_context *ssl,
|
|
|
|
void *ctx)
|
2020-02-05 15:44:10 +01:00
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->handshake != NULL) {
|
2022-02-17 07:23:47 +01:00
|
|
|
ssl->handshake->user_async_ctx = ctx;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2020-02-05 15:44:10 +01:00
|
|
|
}
|
2022-02-17 07:23:47 +01:00
|
|
|
#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
|
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
|
|
|
/*
|
2022-02-17 07:23:47 +01:00
|
|
|
* SSL get accessors
|
2020-02-05 15:44:10 +01:00
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
uint32_t mbedtls_ssl_get_verify_result(const mbedtls_ssl_context *ssl)
|
2020-02-05 15:44:10 +01:00
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->session != NULL) {
|
|
|
|
return ssl->session->verify_result;
|
|
|
|
}
|
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
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->session_negotiate != NULL) {
|
|
|
|
return ssl->session_negotiate->verify_result;
|
|
|
|
}
|
2018-08-15 16:11:57 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0xFFFFFFFF;
|
2018-08-15 15:48:01 +02:00
|
|
|
}
|
2020-02-05 15:44:10 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_ssl_get_ciphersuite_id_from_ssl(const mbedtls_ssl_context *ssl)
|
2021-04-19 22:59:14 +02:00
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl == NULL || ssl->session == NULL) {
|
|
|
|
return 0;
|
|
|
|
}
|
2021-04-19 22:59:14 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return ssl->session->ciphersuite;
|
2021-04-19 22:59:14 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
const char *mbedtls_ssl_get_ciphersuite(const mbedtls_ssl_context *ssl)
|
2018-08-21 18:20:27 +02:00
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl == NULL || ssl->session == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2018-08-21 18:22:17 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return mbedtls_ssl_get_ciphersuite_name(ssl->session->ciphersuite);
|
2020-02-05 15:44:10 +01:00
|
|
|
}
|
2018-08-21 18:20:27 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
const char *mbedtls_ssl_get_version(const mbedtls_ssl_context *ssl)
|
2020-02-05 15:44:10 +01:00
|
|
|
{
|
2022-02-17 07:23:47 +01:00
|
|
|
#if defined(MBEDTLS_SSL_PROTO_DTLS)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
|
|
|
|
switch (ssl->tls_version) {
|
2022-03-15 00:04:24 +01:00
|
|
|
case MBEDTLS_SSL_VERSION_TLS1_2:
|
2023-01-11 14:50:10 +01:00
|
|
|
return "DTLSv1.2";
|
2022-02-17 07:23:47 +01:00
|
|
|
default:
|
2023-01-11 14:50:10 +01:00
|
|
|
return "unknown (DTLS)";
|
2022-02-17 07:23:47 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2016-10-13 18:21:01 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
switch (ssl->tls_version) {
|
2022-03-15 00:04:24 +01:00
|
|
|
case MBEDTLS_SSL_VERSION_TLS1_2:
|
2023-01-11 14:50:10 +01:00
|
|
|
return "TLSv1.2";
|
2022-03-15 00:04:24 +01:00
|
|
|
case MBEDTLS_SSL_VERSION_TLS1_3:
|
2023-01-11 14:50:10 +01:00
|
|
|
return "TLSv1.3";
|
2022-02-17 07:23:47 +01:00
|
|
|
default:
|
2023-01-11 14:50:10 +01:00
|
|
|
return "unknown";
|
2022-02-17 07:23:47 +01:00
|
|
|
}
|
2018-08-21 18:20:27 +02:00
|
|
|
}
|
|
|
|
|
2023-12-28 18:48:16 +01:00
|
|
|
#if defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT)
|
|
|
|
|
|
|
|
size_t mbedtls_ssl_get_output_record_size_limit(const mbedtls_ssl_context *ssl)
|
|
|
|
{
|
|
|
|
const size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
|
|
|
|
size_t record_size_limit = max_len;
|
|
|
|
|
|
|
|
if (ssl->session != NULL &&
|
|
|
|
ssl->session->record_size_limit >= MBEDTLS_SSL_RECORD_SIZE_LIMIT_MIN &&
|
|
|
|
ssl->session->record_size_limit < max_len) {
|
|
|
|
record_size_limit = ssl->session->record_size_limit;
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: this is currently untested
|
|
|
|
/* During a handshake, use the value being negotiated */
|
|
|
|
if (ssl->session_negotiate != NULL &&
|
|
|
|
ssl->session_negotiate->record_size_limit >= MBEDTLS_SSL_RECORD_SIZE_LIMIT_MIN &&
|
|
|
|
ssl->session_negotiate->record_size_limit < max_len) {
|
|
|
|
record_size_limit = ssl->session_negotiate->record_size_limit;
|
|
|
|
}
|
|
|
|
|
|
|
|
return record_size_limit;
|
|
|
|
}
|
|
|
|
#endif /* MBEDTLS_SSL_RECORD_SIZE_LIMIT */
|
|
|
|
|
2022-02-17 07:23:47 +01:00
|
|
|
#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
|
2023-01-11 14:50:10 +01:00
|
|
|
size_t mbedtls_ssl_get_input_max_frag_len(const mbedtls_ssl_context *ssl)
|
2018-08-15 15:48:01 +02:00
|
|
|
{
|
2022-02-17 07:23:47 +01:00
|
|
|
size_t max_len = MBEDTLS_SSL_IN_CONTENT_LEN;
|
|
|
|
size_t read_mfl;
|
2018-08-15 16:11:57 +02:00
|
|
|
|
2022-12-01 12:43:12 +01:00
|
|
|
#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
|
2022-02-17 07:23:47 +01:00
|
|
|
/* Use the configured MFL for the client if we're past SERVER_HELLO_DONE */
|
2023-01-11 14:50:10 +01:00
|
|
|
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);
|
2020-02-05 15:44:10 +01:00
|
|
|
}
|
2022-12-01 12:43:12 +01:00
|
|
|
#endif
|
2022-02-17 07:23:47 +01:00
|
|
|
|
|
|
|
/* Check if a smaller max length was negotiated */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->session_out != NULL) {
|
|
|
|
read_mfl = ssl_mfl_code_to_length(ssl->session_out->mfl_code);
|
|
|
|
if (read_mfl < max_len) {
|
2022-02-17 07:23:47 +01:00
|
|
|
max_len = read_mfl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-01 12:43:12 +01:00
|
|
|
/* During a handshake, use the value being negotiated */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->session_negotiate != NULL) {
|
|
|
|
read_mfl = ssl_mfl_code_to_length(ssl->session_negotiate->mfl_code);
|
|
|
|
if (read_mfl < max_len) {
|
2022-02-17 07:23:47 +01:00
|
|
|
max_len = read_mfl;
|
|
|
|
}
|
2020-02-05 15:44:10 +01:00
|
|
|
}
|
2022-02-17 07:23:47 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return max_len;
|
2020-02-05 15:44:10 +01:00
|
|
|
}
|
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
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
size_t mbedtls_ssl_get_output_max_frag_len(const mbedtls_ssl_context *ssl)
|
2020-02-05 15:44:10 +01:00
|
|
|
{
|
2022-02-17 07:23:47 +01:00
|
|
|
size_t max_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
|
|
|
|
2022-02-17 07:23:47 +01:00
|
|
|
/*
|
|
|
|
* Assume mfl_code is correct since it was checked when set
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
max_len = ssl_mfl_code_to_length(ssl->conf->mfl_code);
|
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
|
|
|
|
2022-02-17 07:23:47 +01:00
|
|
|
/* Check if a smaller max length was negotiated */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->session_out != NULL &&
|
|
|
|
ssl_mfl_code_to_length(ssl->session_out->mfl_code) < max_len) {
|
|
|
|
max_len = ssl_mfl_code_to_length(ssl->session_out->mfl_code);
|
2022-02-17 07:23:47 +01:00
|
|
|
}
|
2018-08-21 16:47:49 +02:00
|
|
|
|
2022-02-17 07:23:47 +01:00
|
|
|
/* During a handshake, use the value being negotiated */
|
2023-01-11 14:50:10 +01:00
|
|
|
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);
|
2022-02-17 07:23:47 +01:00
|
|
|
}
|
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
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return max_len;
|
2020-02-05 15:44:10 +01:00
|
|
|
}
|
2022-02-17 07:23:47 +01:00
|
|
|
#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
|
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
|
|
|
|
2022-02-17 07:23:47 +01:00
|
|
|
#if defined(MBEDTLS_SSL_PROTO_DTLS)
|
2023-01-11 14:50:10 +01:00
|
|
|
size_t mbedtls_ssl_get_current_mtu(const mbedtls_ssl_context *ssl)
|
2020-02-05 15:44:10 +01:00
|
|
|
{
|
2022-02-17 07:23:47 +01:00
|
|
|
/* Return unlimited mtu for client hello messages to avoid fragmentation. */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
|
|
|
|
(ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
|
|
|
|
ssl->state == MBEDTLS_SSL_SERVER_HELLO)) {
|
|
|
|
return 0;
|
|
|
|
}
|
2018-08-21 16:51:03 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->handshake == NULL || ssl->handshake->mtu == 0) {
|
|
|
|
return ssl->mtu;
|
|
|
|
}
|
2018-08-21 16:51:03 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->mtu == 0) {
|
|
|
|
return ssl->handshake->mtu;
|
|
|
|
}
|
2018-08-21 16:51:03 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return ssl->mtu < ssl->handshake->mtu ?
|
|
|
|
ssl->mtu : ssl->handshake->mtu;
|
2020-02-05 15:44:10 +01:00
|
|
|
}
|
2022-02-17 07:23:47 +01:00
|
|
|
#endif /* MBEDTLS_SSL_PROTO_DTLS */
|
2018-08-21 16:51:03 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_ssl_get_max_out_record_payload(const mbedtls_ssl_context *ssl)
|
2022-02-03 13:50:35 +01:00
|
|
|
{
|
2022-02-17 07:23:47 +01:00
|
|
|
size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
|
2022-02-03 13:50:35 +01:00
|
|
|
|
2022-02-17 07:23:47 +01:00
|
|
|
#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
|
2023-03-15 09:09:06 +01:00
|
|
|
!defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT) && \
|
2022-02-17 07:23:47 +01:00
|
|
|
!defined(MBEDTLS_SSL_PROTO_DTLS)
|
|
|
|
(void) ssl;
|
|
|
|
#endif
|
2022-02-03 13:50:35 +01:00
|
|
|
|
2022-02-17 07:23:47 +01:00
|
|
|
#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
|
2023-01-11 14:50:10 +01:00
|
|
|
const size_t mfl = mbedtls_ssl_get_output_max_frag_len(ssl);
|
2022-02-03 13:50:35 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (max_len > mfl) {
|
2022-02-17 07:23:47 +01:00
|
|
|
max_len = mfl;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2022-02-17 07:23:47 +01:00
|
|
|
#endif
|
|
|
|
|
2023-03-15 09:09:06 +01:00
|
|
|
#if defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT)
|
|
|
|
const size_t record_size_limit = mbedtls_ssl_get_output_record_size_limit(ssl);
|
|
|
|
|
|
|
|
if (max_len > record_size_limit) {
|
|
|
|
max_len = record_size_limit;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2023-12-28 18:48:16 +01:00
|
|
|
if (ssl->transform_out != NULL &&
|
|
|
|
ssl->transform_out->tls_version == MBEDTLS_SSL_VERSION_TLS1_3) {
|
2024-01-09 15:18:34 +01:00
|
|
|
/*
|
|
|
|
* In TLS 1.3 case, when records are protected, `max_len` as computed
|
|
|
|
* above is the maximum length of the TLSInnerPlaintext structure that
|
|
|
|
* along the plaintext payload contains the inner content type (one byte)
|
|
|
|
* and some zero padding. Given the algorithm used for padding
|
|
|
|
* in mbedtls_ssl_encrypt_buf(), compute the maximum length for
|
|
|
|
* the plaintext payload. Round down to a multiple of
|
|
|
|
* MBEDTLS_SSL_CID_TLS1_3_PADDING_GRANULARITY and
|
|
|
|
* subtract 1.
|
2023-12-28 18:48:16 +01:00
|
|
|
*/
|
|
|
|
max_len = ((max_len / MBEDTLS_SSL_CID_TLS1_3_PADDING_GRANULARITY) *
|
|
|
|
MBEDTLS_SSL_CID_TLS1_3_PADDING_GRANULARITY) - 1;
|
|
|
|
}
|
|
|
|
|
2022-02-17 07:23:47 +01:00
|
|
|
#if defined(MBEDTLS_SSL_PROTO_DTLS)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (mbedtls_ssl_get_current_mtu(ssl) != 0) {
|
|
|
|
const size_t mtu = mbedtls_ssl_get_current_mtu(ssl);
|
|
|
|
const int ret = mbedtls_ssl_get_record_expansion(ssl);
|
2022-02-17 07:23:47 +01:00
|
|
|
const size_t overhead = (size_t) ret;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ret < 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
2022-02-17 07:23:47 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (mtu <= overhead) {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(1, ("MTU too low for record expansion"));
|
|
|
|
return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
|
2022-02-17 07:23:47 +01:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (max_len > mtu - overhead) {
|
2022-02-17 07:23:47 +01:00
|
|
|
max_len = mtu - overhead;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2022-01-10 11:55:46 +01:00
|
|
|
}
|
2022-02-17 07:23:47 +01:00
|
|
|
#endif /* MBEDTLS_SSL_PROTO_DTLS */
|
2022-01-10 11:55:46 +01:00
|
|
|
|
2022-02-17 07:23:47 +01:00
|
|
|
#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
|
2023-12-20 18:28:31 +01:00
|
|
|
!defined(MBEDTLS_SSL_PROTO_DTLS) && \
|
|
|
|
!defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT)
|
2022-02-17 07:23:47 +01:00
|
|
|
((void) ssl);
|
|
|
|
#endif
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return (int) max_len;
|
2022-01-10 11:55:46 +01:00
|
|
|
}
|
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
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_ssl_get_max_in_record_payload(const mbedtls_ssl_context *ssl)
|
2016-10-13 18:21:01 +02:00
|
|
|
{
|
2022-02-17 07:23:47 +01:00
|
|
|
size_t max_len = MBEDTLS_SSL_IN_CONTENT_LEN;
|
2017-05-24 17:27:30 +02:00
|
|
|
|
2022-02-17 07:23:47 +01:00
|
|
|
#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
|
|
|
|
(void) ssl;
|
|
|
|
#endif
|
2021-12-20 07:43:17 +01:00
|
|
|
|
2022-02-17 07:23:47 +01:00
|
|
|
#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
|
2023-01-11 14:50:10 +01:00
|
|
|
const size_t mfl = mbedtls_ssl_get_input_max_frag_len(ssl);
|
2014-09-08 16:14:10 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (max_len > mfl) {
|
2022-02-17 07:23:47 +01:00
|
|
|
max_len = mfl;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2022-02-17 07:23:47 +01:00
|
|
|
#endif
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return (int) max_len;
|
2020-02-05 15:44:10 +01:00
|
|
|
}
|
2014-09-08 16:14:10 +02:00
|
|
|
|
2022-02-17 07:23:47 +01:00
|
|
|
#if defined(MBEDTLS_X509_CRT_PARSE_C)
|
2023-01-11 14:50:10 +01:00
|
|
|
const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert(const mbedtls_ssl_context *ssl)
|
2020-02-05 15:44:10 +01:00
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl == NULL || ssl->session == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2014-09-09 17:45:31 +02:00
|
|
|
|
2022-02-17 07:23:47 +01:00
|
|
|
#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
|
2023-01-11 14:50:10 +01:00
|
|
|
return ssl->session->peer_cert;
|
2022-02-17 07:23:47 +01:00
|
|
|
#else
|
2023-01-11 14:50:10 +01:00
|
|
|
return NULL;
|
2022-02-17 07:23:47 +01:00
|
|
|
#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
|
|
|
|
}
|
|
|
|
#endif /* MBEDTLS_X509_CRT_PARSE_C */
|
2021-12-20 07:43:17 +01:00
|
|
|
|
2022-02-17 07:23:47 +01:00
|
|
|
#if defined(MBEDTLS_SSL_CLI_C)
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_ssl_get_session(const mbedtls_ssl_context *ssl,
|
|
|
|
mbedtls_ssl_session *dst)
|
2022-02-17 07:23:47 +01:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl == NULL ||
|
2022-02-17 07:23:47 +01:00
|
|
|
dst == NULL ||
|
|
|
|
ssl->session == NULL ||
|
2023-01-11 14:50:10 +01:00
|
|
|
ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT) {
|
|
|
|
return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
|
2017-05-24 17:27:30 +02:00
|
|
|
}
|
2014-09-08 16:14:10 +02:00
|
|
|
|
2022-02-17 07:23:47 +01:00
|
|
|
/* Since Mbed TLS 3.0, mbedtls_ssl_get_session() is no longer
|
|
|
|
* idempotent: Each session can only be exported once.
|
|
|
|
*
|
|
|
|
* (This is in preparation for TLS 1.3 support where we will
|
|
|
|
* need the ability to export multiple sessions (aka tickets),
|
|
|
|
* which will be achieved by calling mbedtls_ssl_get_session()
|
|
|
|
* multiple times until it fails.)
|
|
|
|
*
|
|
|
|
* Check whether we have already exported the current session,
|
|
|
|
* and fail if so.
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->session->exported == 1) {
|
|
|
|
return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
|
|
|
|
}
|
2022-02-17 07:23:47 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = mbedtls_ssl_session_copy(dst, ssl->session);
|
|
|
|
if (ret != 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
2022-02-17 07:23:47 +01:00
|
|
|
|
|
|
|
/* Remember that we've exported the session. */
|
|
|
|
ssl->session->exported = 1;
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2018-08-15 15:09:41 +02:00
|
|
|
}
|
2022-02-17 07:23:47 +01:00
|
|
|
#endif /* MBEDTLS_SSL_CLI_C */
|
2017-05-24 17:27:30 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
/*
|
2022-02-17 07:23:47 +01:00
|
|
|
* Define ticket header determining Mbed TLS version
|
|
|
|
* and structure of the ticket.
|
2020-02-05 15:44:10 +01:00
|
|
|
*/
|
2018-08-15 15:26:08 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
/*
|
2022-02-17 07:23:47 +01:00
|
|
|
* Define bitflag determining compile-time settings influencing
|
|
|
|
* structure of serialized SSL sessions.
|
2020-02-05 15:44:10 +01:00
|
|
|
*/
|
2021-08-10 14:27:10 +02:00
|
|
|
|
2022-02-17 07:23:47 +01:00
|
|
|
#if defined(MBEDTLS_HAVE_TIME)
|
|
|
|
#define SSL_SERIALIZED_SESSION_CONFIG_TIME 1
|
|
|
|
#else
|
|
|
|
#define SSL_SERIALIZED_SESSION_CONFIG_TIME 0
|
|
|
|
#endif /* MBEDTLS_HAVE_TIME */
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_X509_CRT_PARSE_C)
|
|
|
|
#define SSL_SERIALIZED_SESSION_CONFIG_CRT 1
|
|
|
|
#else
|
|
|
|
#define SSL_SERIALIZED_SESSION_CONFIG_CRT 0
|
|
|
|
#endif /* MBEDTLS_X509_CRT_PARSE_C */
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_SESSION_TICKETS)
|
|
|
|
#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 1
|
|
|
|
#else
|
|
|
|
#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 0
|
|
|
|
#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_SESSION_TICKETS */
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
|
|
|
|
#define SSL_SERIALIZED_SESSION_CONFIG_MFL 1
|
|
|
|
#else
|
|
|
|
#define SSL_SERIALIZED_SESSION_CONFIG_MFL 0
|
|
|
|
#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
|
|
|
|
#define SSL_SERIALIZED_SESSION_CONFIG_ETM 1
|
|
|
|
#else
|
|
|
|
#define SSL_SERIALIZED_SESSION_CONFIG_ETM 0
|
|
|
|
#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 */
|
|
|
|
|
|
|
|
#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_ETM_BIT 4
|
|
|
|
#define SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT 5
|
|
|
|
|
|
|
|
#define SSL_SERIALIZED_SESSION_CONFIG_BITFLAG \
|
2023-01-11 14:50:10 +01: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_ETM << SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT) | \
|
|
|
|
(SSL_SERIALIZED_SESSION_CONFIG_TICKET << SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT)))
|
2022-02-17 07:23:47 +01:00
|
|
|
|
2023-12-28 10:07:43 +01:00
|
|
|
static const unsigned char ssl_serialized_session_header[] = {
|
2022-02-17 07:23:47 +01:00
|
|
|
MBEDTLS_VERSION_MAJOR,
|
|
|
|
MBEDTLS_VERSION_MINOR,
|
|
|
|
MBEDTLS_VERSION_PATCH,
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_BYTE_1(SSL_SERIALIZED_SESSION_CONFIG_BITFLAG),
|
|
|
|
MBEDTLS_BYTE_0(SSL_SERIALIZED_SESSION_CONFIG_BITFLAG),
|
2022-02-17 07:23:47 +01:00
|
|
|
};
|
2018-08-16 15:56:31 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
/*
|
2022-02-17 07:23:47 +01:00
|
|
|
* Serialize a session in the following format:
|
|
|
|
* (in the presentation language of TLS, RFC 8446 section 3)
|
2021-08-17 10:53:13 +02:00
|
|
|
*
|
2022-02-17 07:23:47 +01:00
|
|
|
* struct {
|
|
|
|
*
|
|
|
|
* opaque mbedtls_version[3]; // library version: major, minor, patch
|
|
|
|
* opaque session_format[2]; // library-version specific 16-bit field
|
|
|
|
* // determining the format of the remaining
|
|
|
|
* // serialized data.
|
|
|
|
*
|
|
|
|
* Note: When updating the format, remember to keep
|
|
|
|
* these version+format bytes.
|
|
|
|
*
|
|
|
|
* // In this version, `session_format` determines
|
|
|
|
* // the setting of those compile-time
|
|
|
|
* // configuration options which influence
|
|
|
|
* // the structure of mbedtls_ssl_session.
|
|
|
|
*
|
2022-03-14 18:29:48 +01:00
|
|
|
* uint8_t minor_ver; // Protocol minor version. Possible values:
|
2022-12-06 06:27:25 +01:00
|
|
|
* // - TLS 1.2 (0x0303)
|
|
|
|
* // - TLS 1.3 (0x0304)
|
2022-02-17 07:23:47 +01:00
|
|
|
*
|
2022-03-14 18:29:48 +01:00
|
|
|
* select (serialized_session.tls_version) {
|
2022-02-17 07:23:47 +01:00
|
|
|
*
|
2022-03-14 18:29:48 +01:00
|
|
|
* case MBEDTLS_SSL_VERSION_TLS1_2:
|
2022-02-17 07:23:47 +01:00
|
|
|
* serialized_session_tls12 data;
|
2022-12-06 06:27:25 +01:00
|
|
|
* case MBEDTLS_SSL_VERSION_TLS1_3:
|
2022-12-01 12:43:12 +01:00
|
|
|
* serialized_session_tls13 data;
|
2022-02-17 07:23:47 +01:00
|
|
|
*
|
|
|
|
* };
|
|
|
|
*
|
|
|
|
* } serialized_session;
|
2021-08-17 10:53:13 +02:00
|
|
|
*
|
2020-02-05 15:44:10 +01:00
|
|
|
*/
|
2018-08-16 15:56:31 +02:00
|
|
|
|
2022-06-17 10:52:54 +02:00
|
|
|
MBEDTLS_CHECK_RETURN_CRITICAL
|
2023-01-11 14:50:10 +01: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)
|
2013-07-16 12:45:26 +02:00
|
|
|
{
|
2022-02-17 07:23:47 +01:00
|
|
|
unsigned char *p = buf;
|
|
|
|
size_t used = 0;
|
2022-07-13 09:15:48 +02:00
|
|
|
size_t remaining_len;
|
2022-08-17 15:31:36 +02:00
|
|
|
#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
|
|
|
|
size_t out_len;
|
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
|
|
|
#endif
|
2023-01-11 14:50:10 +01:00
|
|
|
if (session == NULL) {
|
|
|
|
return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
|
|
|
|
}
|
2022-07-07 08:55:50 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (!omit_header) {
|
2022-02-17 07:23:47 +01:00
|
|
|
/*
|
|
|
|
* Add Mbed TLS version identifier
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
used += sizeof(ssl_serialized_session_header);
|
2022-02-17 07:23:47 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (used <= buf_len) {
|
|
|
|
memcpy(p, ssl_serialized_session_header,
|
|
|
|
sizeof(ssl_serialized_session_header));
|
|
|
|
p += sizeof(ssl_serialized_session_header);
|
2022-02-17 07:23:47 +01:00
|
|
|
}
|
2013-07-18 12:32:27 +02:00
|
|
|
}
|
2013-07-17 14:33:38 +02:00
|
|
|
|
2022-02-17 07:23:47 +01:00
|
|
|
/*
|
2024-01-15 10:21:30 +01:00
|
|
|
* TLS version identifier, endpoint, ciphersuite
|
2022-02-17 07:23:47 +01:00
|
|
|
*/
|
2024-01-15 10:21:30 +01:00
|
|
|
used += 1 /* TLS version */
|
|
|
|
+ 1 /* endpoint */
|
|
|
|
+ 2; /* ciphersuite */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (used <= buf_len) {
|
|
|
|
*p++ = MBEDTLS_BYTE_0(session->tls_version);
|
2024-01-15 10:21:30 +01:00
|
|
|
*p++ = session->endpoint;
|
|
|
|
MBEDTLS_PUT_UINT16_BE(session->ciphersuite, p, 0);
|
|
|
|
p += 2;
|
2022-02-17 07:23:47 +01:00
|
|
|
}
|
2013-07-16 12:45:26 +02:00
|
|
|
|
2022-02-17 07:23:47 +01:00
|
|
|
/* Forward to version-specific serialization routine. */
|
2022-07-21 04:37:48 +02:00
|
|
|
remaining_len = (buf_len >= used) ? buf_len - used : 0;
|
2023-01-11 14:50:10 +01:00
|
|
|
switch (session->tls_version) {
|
2022-02-17 07:23:47 +01:00
|
|
|
#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
|
2023-01-11 14:50:10 +01:00
|
|
|
case MBEDTLS_SSL_VERSION_TLS1_2:
|
|
|
|
used += ssl_tls12_session_save(session, p, remaining_len);
|
|
|
|
break;
|
2022-02-17 07:23:47 +01:00
|
|
|
#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
|
2013-07-16 12:45:26 +02:00
|
|
|
|
2022-07-13 09:15:48 +02:00
|
|
|
#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
|
2023-01-11 14:50:10 +01:00
|
|
|
case MBEDTLS_SSL_VERSION_TLS1_3:
|
|
|
|
ret = ssl_tls13_session_save(session, p, remaining_len, &out_len);
|
|
|
|
if (ret != 0 && ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL) {
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
used += out_len;
|
|
|
|
break;
|
2022-07-13 09:15:48 +02:00
|
|
|
#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
default:
|
|
|
|
return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
|
2022-02-17 07:23:47 +01:00
|
|
|
}
|
2012-09-16 21:57:18 +02:00
|
|
|
|
2022-02-17 07:23:47 +01:00
|
|
|
*olen = used;
|
2023-01-11 14:50:10 +01:00
|
|
|
if (used > buf_len) {
|
|
|
|
return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
|
|
|
|
}
|
2012-09-16 21:57:18 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2014-07-03 19:29:16 +02:00
|
|
|
}
|
|
|
|
|
2022-02-17 07:23:47 +01:00
|
|
|
/*
|
|
|
|
* Public wrapper for ssl_session_save()
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_ssl_session_save(const mbedtls_ssl_session *session,
|
|
|
|
unsigned char *buf,
|
|
|
|
size_t buf_len,
|
|
|
|
size_t *olen)
|
2014-11-05 13:58:53 +01:00
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
return ssl_session_save(session, 0, buf, buf_len, olen);
|
2014-11-05 13:58:53 +01:00
|
|
|
}
|
2013-08-14 16:52:14 +02:00
|
|
|
|
2022-02-18 04:48:47 +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.
|
|
|
|
*/
|
2022-06-17 10:52:54 +02:00
|
|
|
MBEDTLS_CHECK_RETURN_CRITICAL
|
2023-01-11 14:50:10 +01:00
|
|
|
static int ssl_session_load(mbedtls_ssl_session *session,
|
|
|
|
unsigned char omit_header,
|
|
|
|
const unsigned char *buf,
|
|
|
|
size_t len)
|
2022-02-17 07:58:27 +01:00
|
|
|
{
|
2022-02-17 07:23:47 +01:00
|
|
|
const unsigned char *p = buf;
|
|
|
|
const unsigned char * const end = buf + len;
|
2022-07-07 08:55:50 +02:00
|
|
|
size_t remaining_len;
|
|
|
|
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (session == NULL) {
|
|
|
|
return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
|
|
|
|
}
|
2013-08-03 13:02:31 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (!omit_header) {
|
2022-02-17 07:58:27 +01:00
|
|
|
/*
|
|
|
|
* Check Mbed TLS version identifier
|
|
|
|
*/
|
2015-10-02 14:33:37 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if ((size_t) (end - p) < sizeof(ssl_serialized_session_header)) {
|
|
|
|
return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
|
|
|
|
}
|
2018-04-25 20:39:48 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (memcmp(p, ssl_serialized_session_header,
|
|
|
|
sizeof(ssl_serialized_session_header)) != 0) {
|
|
|
|
return MBEDTLS_ERR_SSL_VERSION_MISMATCH;
|
2022-02-17 07:58:27 +01:00
|
|
|
}
|
2023-01-11 14:50:10 +01:00
|
|
|
p += sizeof(ssl_serialized_session_header);
|
2022-02-17 07:58:27 +01:00
|
|
|
}
|
2018-04-26 11:46:10 +02:00
|
|
|
|
2022-02-17 07:23:47 +01:00
|
|
|
/*
|
2024-01-15 10:21:30 +01:00
|
|
|
* TLS version identifier, endpoint, ciphersuite
|
2022-02-17 07:23:47 +01:00
|
|
|
*/
|
2024-01-15 10:21:30 +01:00
|
|
|
if (4 > (size_t) (end - p)) {
|
2023-01-11 14:50:10 +01:00
|
|
|
return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
|
|
|
|
}
|
2023-07-13 14:12:40 +02:00
|
|
|
session->tls_version = (mbedtls_ssl_protocol_version) (0x0300 | *p++);
|
2024-01-15 10:21:30 +01:00
|
|
|
session->endpoint = *p++;
|
|
|
|
session->ciphersuite = MBEDTLS_GET_UINT16_BE(p, 0);
|
|
|
|
p += 2;
|
2018-04-25 20:39:48 +02:00
|
|
|
|
2022-02-17 07:58:27 +01:00
|
|
|
/* Dispatch according to TLS version. */
|
2023-11-04 13:20:09 +01:00
|
|
|
remaining_len = (size_t) (end - p);
|
2023-01-11 14:50:10 +01:00
|
|
|
switch (session->tls_version) {
|
2022-02-17 07:58:27 +01:00
|
|
|
#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
|
2023-01-11 14:50:10 +01:00
|
|
|
case MBEDTLS_SSL_VERSION_TLS1_2:
|
|
|
|
return ssl_tls12_session_load(session, p, remaining_len);
|
2022-02-17 07:58:27 +01:00
|
|
|
#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
|
2015-01-23 15:30:57 +01:00
|
|
|
|
2022-07-07 08:55:50 +02:00
|
|
|
#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
|
2023-01-11 14:50:10 +01:00
|
|
|
case MBEDTLS_SSL_VERSION_TLS1_3:
|
|
|
|
return ssl_tls13_session_load(session, p, remaining_len);
|
2022-07-07 08:55:50 +02:00
|
|
|
#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
default:
|
|
|
|
return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
|
2022-02-17 07:58:27 +01:00
|
|
|
}
|
|
|
|
}
|
2021-04-21 07:19:50 +02:00
|
|
|
|
2022-02-17 07:23:47 +01:00
|
|
|
/*
|
|
|
|
* Deserialize session: public wrapper for error cleaning
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_ssl_session_load(mbedtls_ssl_session *session,
|
|
|
|
const unsigned char *buf,
|
|
|
|
size_t len)
|
2022-02-17 07:23:47 +01:00
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
int ret = ssl_session_load(session, 0, buf, len);
|
2021-04-21 07:19:50 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ret != 0) {
|
|
|
|
mbedtls_ssl_session_free(session);
|
|
|
|
}
|
2021-04-21 07:19:50 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return ret;
|
2021-04-21 07:19:50 +02:00
|
|
|
}
|
|
|
|
|
2022-02-17 07:23:47 +01:00
|
|
|
/*
|
|
|
|
* Perform a single step of the SSL handshake
|
|
|
|
*/
|
2022-06-17 10:52:54 +02:00
|
|
|
MBEDTLS_CHECK_RETURN_CRITICAL
|
2023-01-11 14:50:10 +01:00
|
|
|
static int ssl_prepare_handshake_step(mbedtls_ssl_context *ssl)
|
2012-10-30 08:51:03 +01:00
|
|
|
{
|
2022-02-17 07:23:47 +01:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2012-10-30 08:51:03 +01:00
|
|
|
|
2022-02-02 15:33:46 +01:00
|
|
|
/*
|
|
|
|
* We may have not been able to send to the peer all the handshake data
|
2022-03-08 16:00:02 +01:00
|
|
|
* that were written into the output buffer by the previous handshake step,
|
|
|
|
* if the write to the network callback returned with the
|
2022-02-02 15:33:46 +01:00
|
|
|
* #MBEDTLS_ERR_SSL_WANT_WRITE error code.
|
|
|
|
* We proceed to the next handshake step only when all data from the
|
|
|
|
* previous one have been sent to the peer, thus we make sure that this is
|
|
|
|
* the case here by calling `mbedtls_ssl_flush_output()`. The function may
|
|
|
|
* return with the #MBEDTLS_ERR_SSL_WANT_WRITE error code in which case
|
|
|
|
* we have to wait before to go ahead.
|
|
|
|
* In the case of TLS 1.3, handshake step handlers do not send data to the
|
|
|
|
* peer. Data are only sent here and through
|
|
|
|
* `mbedtls_ssl_handle_pending_alert` in case an error that triggered an
|
2022-04-13 20:28:52 +02:00
|
|
|
* alert occurred.
|
2022-02-02 15:33:46 +01:00
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
if ((ret = mbedtls_ssl_flush_output(ssl)) != 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
2022-02-17 07:23:47 +01:00
|
|
|
|
|
|
|
#if defined(MBEDTLS_SSL_PROTO_DTLS)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
|
|
|
|
ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING) {
|
|
|
|
if ((ret = mbedtls_ssl_flight_transmit(ssl)) != 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
2022-02-17 07:23:47 +01:00
|
|
|
}
|
|
|
|
#endif /* MBEDTLS_SSL_PROTO_DTLS */
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return ret;
|
2012-10-30 08:51:03 +01:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_ssl_handshake_step(mbedtls_ssl_context *ssl)
|
2013-07-30 12:41:56 +02:00
|
|
|
{
|
2022-02-17 07:23:47 +01:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
Implement 3.0-semantics for mbedtls_ssl_{get,set}_session()
mbedtls_ssl_{get,set}_session() exhibited idempotent behaviour
in Mbed TLS 2.x. Multiple calls to those functions are not useful
in TLS 1.2, and the idempotent nature is unsuitable for support of
TLS 1.3 which introduces the availabilty to offer multiple tickets
for resumption, as well as receive multiple tickets.
In preparation for TLS 1.3 support, this commit relaxes the semantics
of `mbedtls_ssl_{get,set}_session()` by allowing implementations to
fail gracefully, and leveraging this freedom by modifying the
existing TLS 1.2 implementation to only accept one call to
`mbedtls_ssl_{get,set}_session()` per context, and non-fatally
failing all subsequent invocations.
For TLS 1.3, it will be leveraged by making multiple calls to
`mbedtls_ssl_get_session()` issue one ticket a time until no more
tickets are available, and by using multiple calls to
`mbedtls_ssl_set_session()` to allow the client to offer multiple
tickets to the server.
Signed-off-by: Hanno Becker <hanno.becker@arm.com>
2021-05-14 17:01:05 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl == NULL ||
|
2022-02-17 07:23:47 +01:00
|
|
|
ssl->conf == NULL ||
|
|
|
|
ssl->handshake == NULL ||
|
2023-01-11 14:50:10 +01:00
|
|
|
ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER) {
|
|
|
|
return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
|
2013-07-30 12:41:56 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = ssl_prepare_handshake_step(ssl);
|
|
|
|
if (ret != 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
2019-05-20 11:12:28 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = mbedtls_ssl_handle_pending_alert(ssl);
|
|
|
|
if (ret != 0) {
|
2022-02-17 07:23:47 +01:00
|
|
|
goto cleanup;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2019-05-20 11:12:28 +02:00
|
|
|
|
2022-09-21 13:33:17 +02:00
|
|
|
/* If ssl->conf->endpoint is not one of MBEDTLS_SSL_IS_CLIENT or
|
|
|
|
* MBEDTLS_SSL_IS_SERVER, this is the return code we give */
|
|
|
|
ret = MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
|
|
|
|
|
2022-02-17 07:23:47 +01:00
|
|
|
#if defined(MBEDTLS_SSL_CLI_C)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(2, ("client state: %s",
|
2023-07-13 14:12:40 +02:00
|
|
|
mbedtls_ssl_states_str((mbedtls_ssl_states) ssl->state)));
|
2019-05-16 13:39:07 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
switch (ssl->state) {
|
2022-02-10 16:45:15 +01:00
|
|
|
case MBEDTLS_SSL_HELLO_REQUEST:
|
|
|
|
ssl->state = MBEDTLS_SSL_CLIENT_HELLO;
|
2022-09-22 10:27:56 +02:00
|
|
|
ret = 0;
|
2022-02-10 16:45:15 +01:00
|
|
|
break;
|
2019-05-16 13:39:07 +02:00
|
|
|
|
2022-02-10 16:45:15 +01:00
|
|
|
case MBEDTLS_SSL_CLIENT_HELLO:
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = mbedtls_ssl_write_client_hello(ssl);
|
2022-02-10 16:45:15 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3) {
|
|
|
|
ret = mbedtls_ssl_tls13_handshake_client_step(ssl);
|
|
|
|
} else {
|
|
|
|
ret = mbedtls_ssl_handshake_client_step(ssl);
|
|
|
|
}
|
2022-02-10 16:45:15 +01:00
|
|
|
#elif defined(MBEDTLS_SSL_PROTO_TLS1_2)
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = mbedtls_ssl_handshake_client_step(ssl);
|
2022-02-10 16:45:15 +01:00
|
|
|
#else
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = mbedtls_ssl_tls13_handshake_client_step(ssl);
|
2022-02-10 16:45:15 +01:00
|
|
|
#endif
|
|
|
|
}
|
2022-02-17 07:23:47 +01:00
|
|
|
}
|
2023-03-08 15:51:25 +01:00
|
|
|
#endif /* MBEDTLS_SSL_CLI_C */
|
|
|
|
|
2022-02-17 07:23:47 +01:00
|
|
|
#if defined(MBEDTLS_SSL_SRV_C)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
|
2023-03-08 15:51:25 +01:00
|
|
|
#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
|
|
|
|
if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3) {
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = mbedtls_ssl_tls13_handshake_server_step(ssl);
|
2023-03-08 15:51:25 +01:00
|
|
|
} else {
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = mbedtls_ssl_handshake_server_step(ssl);
|
|
|
|
}
|
2023-03-08 15:51:25 +01:00
|
|
|
#elif defined(MBEDTLS_SSL_PROTO_TLS1_2)
|
|
|
|
ret = mbedtls_ssl_handshake_server_step(ssl);
|
|
|
|
#else
|
|
|
|
ret = mbedtls_ssl_tls13_handshake_server_step(ssl);
|
2022-02-17 07:23:47 +01:00
|
|
|
#endif
|
2023-03-08 15:51:25 +01:00
|
|
|
}
|
|
|
|
#endif /* MBEDTLS_SSL_SRV_C */
|
2019-05-16 13:50:45 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ret != 0) {
|
2022-02-17 07:23:47 +01:00
|
|
|
/* handshake_step return error. And it is same
|
|
|
|
* with alert_reason.
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->send_alert) {
|
|
|
|
ret = mbedtls_ssl_handle_pending_alert(ssl);
|
2022-02-17 07:23:47 +01:00
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
}
|
2019-05-16 13:50:45 +02:00
|
|
|
|
2022-02-17 07:23:47 +01:00
|
|
|
cleanup:
|
2023-01-11 14:50:10 +01:00
|
|
|
return ret;
|
2022-02-17 07:23:47 +01:00
|
|
|
}
|
2019-05-16 13:50:45 +02:00
|
|
|
|
2022-02-17 07:23:47 +01:00
|
|
|
/*
|
|
|
|
* Perform the SSL handshake
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_ssl_handshake(mbedtls_ssl_context *ssl)
|
2022-02-17 07:23:47 +01:00
|
|
|
{
|
|
|
|
int ret = 0;
|
2019-05-16 13:50:45 +02:00
|
|
|
|
2022-02-17 07:23:47 +01:00
|
|
|
/* Sanity checks */
|
2019-05-16 13:50:45 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl == NULL || ssl->conf == NULL) {
|
|
|
|
return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
|
|
|
|
}
|
2019-05-16 13:50:45 +02:00
|
|
|
|
2022-02-17 07:23:47 +01:00
|
|
|
#if defined(MBEDTLS_SSL_PROTO_DTLS)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
|
|
|
|
(ssl->f_set_timer == NULL || ssl->f_get_timer == NULL)) {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(1, ("You must use "
|
|
|
|
"mbedtls_ssl_set_timer_cb() for DTLS"));
|
|
|
|
return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
|
2022-02-17 07:23:47 +01:00
|
|
|
}
|
|
|
|
#endif /* MBEDTLS_SSL_PROTO_DTLS */
|
2019-05-29 12:10:18 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(2, ("=> handshake"));
|
2019-05-16 13:50:45 +02:00
|
|
|
|
2022-02-17 07:23:47 +01:00
|
|
|
/* Main handshake loop */
|
2023-01-11 14:50:10 +01:00
|
|
|
while (ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER) {
|
|
|
|
ret = mbedtls_ssl_handshake_step(ssl);
|
2019-05-16 13:39:07 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ret != 0) {
|
2022-02-17 07:23:47 +01:00
|
|
|
break;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2022-02-17 07:23:47 +01:00
|
|
|
}
|
Store TLS version in SSL session structure
Instances of `mbedtls_ssl_session` represent data enabling session resumption.
With the introduction of TLS 1.3, the format of this data changes. We therefore
need TLS-version field as part of `mbedtlsl_ssl_session` which allows distinguish
1.2 and 1.3 sessions.
This commit introduces such a TLS-version field to mbedtls_ssl_session.
The change has a few ramifications:
- Session serialization/deserialization routines need to be adjusted.
This is achieved by adding the TLS-version after the header of
Mbed TLS version+config, and by having the subsequent structure
of the serialized data depend on the value of this field.
The details are described in terms of the RFC 8446 presentation language.
The 1.2 session (de)serialization are moved into static helper functions,
while the top-level session (de)serialization only parses the Mbed TLS
version+config header and the TLS-version field, and dispatches according
to the found version.
This way, it will be easy to add support for TLS 1.3 sessions in the future.
- Tests for session serialization need to be adjusted
- Once we add support for TLS 1.3, with runtime negotiation of 1.2 vs. 1.3,
we will need to have some logic comparing the TLS version of the proposed session
to the negotiated TLS version. For now, however, we only support TLS 1.2,
and no such logic is needed. Instead, we just store the TLS version in the
session structure at the same point when we populate mbedtls_ssl_context.minor_ver.
The change introduces some overlap between `mbedtls_ssl_session.minor_ver` and
`mbedtls_ssl_context.minor_ver`, which should be studied and potentially resolved.
However, with both fields being private and explicitly marked so, this can happen
in a later change.
Signed-off-by: Hanno Becker <hanno.becker@arm.com>
2021-07-23 07:25:48 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(2, ("<= handshake"));
|
2022-02-17 07:23:47 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return ret;
|
2022-02-17 07:23:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_SSL_RENEGOTIATION)
|
|
|
|
#if defined(MBEDTLS_SSL_SRV_C)
|
|
|
|
/*
|
|
|
|
* Write HelloRequest to request renegotiation on server
|
Store TLS version in SSL session structure
Instances of `mbedtls_ssl_session` represent data enabling session resumption.
With the introduction of TLS 1.3, the format of this data changes. We therefore
need TLS-version field as part of `mbedtlsl_ssl_session` which allows distinguish
1.2 and 1.3 sessions.
This commit introduces such a TLS-version field to mbedtls_ssl_session.
The change has a few ramifications:
- Session serialization/deserialization routines need to be adjusted.
This is achieved by adding the TLS-version after the header of
Mbed TLS version+config, and by having the subsequent structure
of the serialized data depend on the value of this field.
The details are described in terms of the RFC 8446 presentation language.
The 1.2 session (de)serialization are moved into static helper functions,
while the top-level session (de)serialization only parses the Mbed TLS
version+config header and the TLS-version field, and dispatches according
to the found version.
This way, it will be easy to add support for TLS 1.3 sessions in the future.
- Tests for session serialization need to be adjusted
- Once we add support for TLS 1.3, with runtime negotiation of 1.2 vs. 1.3,
we will need to have some logic comparing the TLS version of the proposed session
to the negotiated TLS version. For now, however, we only support TLS 1.2,
and no such logic is needed. Instead, we just store the TLS version in the
session structure at the same point when we populate mbedtls_ssl_context.minor_ver.
The change introduces some overlap between `mbedtls_ssl_session.minor_ver` and
`mbedtls_ssl_context.minor_ver`, which should be studied and potentially resolved.
However, with both fields being private and explicitly marked so, this can happen
in a later change.
Signed-off-by: Hanno Becker <hanno.becker@arm.com>
2021-07-23 07:25:48 +02:00
|
|
|
*/
|
2022-06-17 10:52:54 +02:00
|
|
|
MBEDTLS_CHECK_RETURN_CRITICAL
|
2023-01-11 14:50:10 +01:00
|
|
|
static int ssl_write_hello_request(mbedtls_ssl_context *ssl)
|
2019-05-16 10:08:35 +02:00
|
|
|
{
|
2022-02-17 07:23:47 +01:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
Store TLS version in SSL session structure
Instances of `mbedtls_ssl_session` represent data enabling session resumption.
With the introduction of TLS 1.3, the format of this data changes. We therefore
need TLS-version field as part of `mbedtlsl_ssl_session` which allows distinguish
1.2 and 1.3 sessions.
This commit introduces such a TLS-version field to mbedtls_ssl_session.
The change has a few ramifications:
- Session serialization/deserialization routines need to be adjusted.
This is achieved by adding the TLS-version after the header of
Mbed TLS version+config, and by having the subsequent structure
of the serialized data depend on the value of this field.
The details are described in terms of the RFC 8446 presentation language.
The 1.2 session (de)serialization are moved into static helper functions,
while the top-level session (de)serialization only parses the Mbed TLS
version+config header and the TLS-version field, and dispatches according
to the found version.
This way, it will be easy to add support for TLS 1.3 sessions in the future.
- Tests for session serialization need to be adjusted
- Once we add support for TLS 1.3, with runtime negotiation of 1.2 vs. 1.3,
we will need to have some logic comparing the TLS version of the proposed session
to the negotiated TLS version. For now, however, we only support TLS 1.2,
and no such logic is needed. Instead, we just store the TLS version in the
session structure at the same point when we populate mbedtls_ssl_context.minor_ver.
The change introduces some overlap between `mbedtls_ssl_session.minor_ver` and
`mbedtls_ssl_context.minor_ver`, which should be studied and potentially resolved.
However, with both fields being private and explicitly marked so, this can happen
in a later change.
Signed-off-by: Hanno Becker <hanno.becker@arm.com>
2021-07-23 07:25:48 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(2, ("=> write hello request"));
|
2019-05-16 10:08:35 +02:00
|
|
|
|
2022-02-17 07:23:47 +01:00
|
|
|
ssl->out_msglen = 4;
|
|
|
|
ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
|
|
|
|
ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
|
2019-05-24 12:06:29 +02:00
|
|
|
|
2023-01-11 14:50: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;
|
2022-02-17 07:23:47 +01:00
|
|
|
}
|
2019-05-24 12:06:29 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(2, ("<= write hello request"));
|
2022-02-17 07:23:47 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2022-02-17 07:23:47 +01:00
|
|
|
}
|
|
|
|
#endif /* MBEDTLS_SSL_SRV_C */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_ssl_start_renegotiation(mbedtls_ssl_context *ssl)
|
2022-02-17 07:23:47 +01:00
|
|
|
{
|
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(2, ("=> renegotiate"));
|
2022-02-17 07:23:47 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if ((ret = ssl_handshake_init(ssl)) != 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
2022-02-17 07:23:47 +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)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
|
|
|
|
ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING) {
|
|
|
|
if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
|
2022-02-17 07:23:47 +01:00
|
|
|
ssl->handshake->out_msg_seq = 1;
|
2023-01-11 14:50:10 +01:00
|
|
|
} else {
|
2022-02-17 07:23:47 +01:00
|
|
|
ssl->handshake->in_msg_seq = 1;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2019-05-24 12:06:29 +02:00
|
|
|
}
|
2022-02-17 07:23:47 +01:00
|
|
|
#endif
|
2019-05-24 12:06:29 +02:00
|
|
|
|
2022-02-17 07:23:47 +01:00
|
|
|
ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
|
|
|
|
ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
|
2019-05-16 10:08:35 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if ((ret = mbedtls_ssl_handshake(ssl)) != 0) {
|
|
|
|
MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_handshake", ret);
|
|
|
|
return ret;
|
2022-02-17 07:23:47 +01:00
|
|
|
}
|
2019-05-24 12:06:29 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(2, ("<= renegotiate"));
|
2019-05-24 12:06:29 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2022-02-17 07:23:47 +01:00
|
|
|
}
|
2019-05-24 12:06:29 +02:00
|
|
|
|
2022-02-17 07:23:47 +01:00
|
|
|
/*
|
|
|
|
* Renegotiate current connection on client,
|
|
|
|
* or request renegotiation on server
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_ssl_renegotiate(mbedtls_ssl_context *ssl)
|
2022-02-17 07:23:47 +01:00
|
|
|
{
|
|
|
|
int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
|
2019-05-24 12:06:29 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl == NULL || ssl->conf == NULL) {
|
|
|
|
return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
|
|
|
|
}
|
2022-02-17 07:23:47 +01:00
|
|
|
|
|
|
|
#if defined(MBEDTLS_SSL_SRV_C)
|
|
|
|
/* On server, just send the request */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
|
|
|
|
if (mbedtls_ssl_is_handshake_over(ssl) == 0) {
|
|
|
|
return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
|
|
|
|
}
|
2022-02-17 07:23:47 +01:00
|
|
|
|
|
|
|
ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
|
|
|
|
|
|
|
|
/* Did we already try/start sending HelloRequest? */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->out_left != 0) {
|
|
|
|
return mbedtls_ssl_flush_output(ssl);
|
|
|
|
}
|
2022-02-17 07:23:47 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return ssl_write_hello_request(ssl);
|
2019-05-21 11:01:32 +02:00
|
|
|
}
|
2022-02-17 07:23:47 +01:00
|
|
|
#endif /* MBEDTLS_SSL_SRV_C */
|
2019-05-16 10:08:35 +02:00
|
|
|
|
2022-02-17 07:23:47 +01:00
|
|
|
#if defined(MBEDTLS_SSL_CLI_C)
|
2019-05-16 11:11:08 +02:00
|
|
|
/*
|
2022-02-17 07:23:47 +01:00
|
|
|
* On client, either start the renegotiation process or,
|
|
|
|
* if already in progress, continue the handshake
|
2019-05-16 11:11:08 +02:00
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS) {
|
|
|
|
if (mbedtls_ssl_is_handshake_over(ssl) == 0) {
|
|
|
|
return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
|
|
|
|
}
|
2019-05-16 10:08:35 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if ((ret = mbedtls_ssl_start_renegotiation(ssl)) != 0) {
|
|
|
|
MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_start_renegotiation", ret);
|
|
|
|
return ret;
|
2019-05-21 11:01:32 +02:00
|
|
|
}
|
2023-01-11 14:50:10 +01:00
|
|
|
} else {
|
|
|
|
if ((ret = mbedtls_ssl_handshake(ssl)) != 0) {
|
|
|
|
MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_handshake", ret);
|
|
|
|
return ret;
|
2019-05-24 12:06:29 +02:00
|
|
|
}
|
|
|
|
}
|
2022-02-17 07:23:47 +01:00
|
|
|
#endif /* MBEDTLS_SSL_CLI_C */
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return ret;
|
2022-02-17 07:23:47 +01:00
|
|
|
}
|
|
|
|
#endif /* MBEDTLS_SSL_RENEGOTIATION */
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
void mbedtls_ssl_handshake_free(mbedtls_ssl_context *ssl)
|
2022-02-17 07:23:47 +01:00
|
|
|
{
|
|
|
|
mbedtls_ssl_handshake_params *handshake = ssl->handshake;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (handshake == NULL) {
|
2022-02-17 07:23:47 +01:00
|
|
|
return;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2022-02-17 07:23:47 +01:00
|
|
|
|
2023-07-03 12:11:36 +02:00
|
|
|
#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
|
2022-02-17 07:23:47 +01:00
|
|
|
#if !defined(MBEDTLS_DEPRECATED_REMOVED)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->handshake->group_list_heap_allocated) {
|
|
|
|
mbedtls_free((void *) handshake->group_list);
|
|
|
|
}
|
2022-02-17 07:23:47 +01:00
|
|
|
handshake->group_list = NULL;
|
|
|
|
#endif /* MBEDTLS_DEPRECATED_REMOVED */
|
2023-07-03 12:11:36 +02:00
|
|
|
#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
|
2022-02-17 07:23:47 +01:00
|
|
|
|
2022-10-05 12:46:29 +02:00
|
|
|
#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
|
2022-02-17 07:23:47 +01:00
|
|
|
#if !defined(MBEDTLS_DEPRECATED_REMOVED)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->handshake->sig_algs_heap_allocated) {
|
|
|
|
mbedtls_free((void *) handshake->sig_algs);
|
|
|
|
}
|
2022-02-17 07:23:47 +01:00
|
|
|
handshake->sig_algs = NULL;
|
|
|
|
#endif /* MBEDTLS_DEPRECATED_REMOVED */
|
|
|
|
#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->handshake->certificate_request_context) {
|
|
|
|
mbedtls_free((void *) handshake->certificate_request_context);
|
2022-02-17 07:23:47 +01:00
|
|
|
}
|
|
|
|
#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
|
2022-10-05 12:46:29 +02:00
|
|
|
#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
|
2022-02-17 07:23:47 +01:00
|
|
|
|
|
|
|
#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0) {
|
|
|
|
ssl->conf->f_async_cancel(ssl);
|
2022-02-17 07:23:47 +01:00
|
|
|
handshake->async_in_progress = 0;
|
|
|
|
}
|
|
|
|
#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
|
|
|
|
|
2023-03-17 12:50:01 +01:00
|
|
|
#if defined(MBEDTLS_MD_CAN_SHA256)
|
2022-02-17 07:23:47 +01:00
|
|
|
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
2023-01-11 14:50:10 +01:00
|
|
|
psa_hash_abort(&handshake->fin_sha256_psa);
|
2022-02-17 07:23:47 +01:00
|
|
|
#else
|
2023-02-24 13:19:17 +01:00
|
|
|
mbedtls_md_free(&handshake->fin_sha256);
|
2022-02-17 07:23:47 +01:00
|
|
|
#endif
|
|
|
|
#endif
|
2023-03-17 12:50:01 +01:00
|
|
|
#if defined(MBEDTLS_MD_CAN_SHA384)
|
2022-02-17 07:23:47 +01:00
|
|
|
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
2023-01-11 14:50:10 +01:00
|
|
|
psa_hash_abort(&handshake->fin_sha384_psa);
|
2022-02-17 07:23:47 +01:00
|
|
|
#else
|
2023-02-24 13:19:17 +01:00
|
|
|
mbedtls_md_free(&handshake->fin_sha384);
|
2022-02-17 07:23:47 +01:00
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_DHM_C)
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_dhm_free(&handshake->dhm_ctx);
|
2022-02-17 07:23:47 +01:00
|
|
|
#endif
|
2023-07-05 18:57:21 +02:00
|
|
|
#if !defined(MBEDTLS_USE_PSA_CRYPTO) && \
|
2023-07-07 17:04:24 +02:00
|
|
|
defined(MBEDTLS_KEY_EXCHANGE_SOME_ECDH_OR_ECDHE_1_2_ENABLED)
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_ecdh_free(&handshake->ecdh_ctx);
|
2022-02-17 07:23:47 +01:00
|
|
|
#endif
|
2022-11-15 14:08:42 +01:00
|
|
|
|
2022-02-17 07:23:47 +01:00
|
|
|
#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
|
2022-05-31 14:43:23 +02:00
|
|
|
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
2023-01-11 14:50:10 +01:00
|
|
|
psa_pake_abort(&handshake->psa_pake_ctx);
|
2022-12-08 18:42:58 +01:00
|
|
|
/*
|
|
|
|
* Opaque keys are not stored in the handshake's data and it's the user
|
|
|
|
* responsibility to destroy them. Clear ones, instead, are created by
|
|
|
|
* the TLS library and should be destroyed at the same level
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
if (!mbedtls_svc_key_id_is_null(handshake->psa_pake_password)) {
|
|
|
|
psa_destroy_key(handshake->psa_pake_password);
|
2022-12-08 18:42:58 +01:00
|
|
|
}
|
2022-05-31 14:43:23 +02:00
|
|
|
handshake->psa_pake_password = MBEDTLS_SVC_KEY_ID_INIT;
|
|
|
|
#else
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_ecjpake_free(&handshake->ecjpake_ctx);
|
2022-05-31 14:43:23 +02:00
|
|
|
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
2022-02-17 07:23:47 +01:00
|
|
|
#if defined(MBEDTLS_SSL_CLI_C)
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_free(handshake->ecjpake_cache);
|
2022-02-17 07:23:47 +01:00
|
|
|
handshake->ecjpake_cache = NULL;
|
|
|
|
handshake->ecjpake_cache_len = 0;
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
2023-07-05 18:57:21 +02:00
|
|
|
#if defined(MBEDTLS_KEY_EXCHANGE_SOME_ECDH_OR_ECDHE_ANY_ENABLED) || \
|
2023-07-13 17:23:20 +02:00
|
|
|
defined(MBEDTLS_KEY_EXCHANGE_WITH_ECDSA_ANY_ENABLED) || \
|
2022-02-17 07:23:47 +01:00
|
|
|
defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
|
|
|
|
/* explicit void pointer cast for buggy MS compiler */
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_free((void *) handshake->curves_tls_id);
|
2022-02-17 07:23:47 +01:00
|
|
|
#endif
|
|
|
|
|
2022-10-05 14:31:43 +02:00
|
|
|
#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED)
|
2022-05-03 09:35:09 +02:00
|
|
|
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (!mbedtls_svc_key_id_is_null(ssl->handshake->psk_opaque)) {
|
2022-05-03 09:35:09 +02:00
|
|
|
/* The maintenance of the external PSK key slot is the
|
|
|
|
* user's responsibility. */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->handshake->psk_opaque_is_internal) {
|
|
|
|
psa_destroy_key(ssl->handshake->psk_opaque);
|
2022-05-03 09:35:09 +02:00
|
|
|
ssl->handshake->psk_opaque_is_internal = 0;
|
|
|
|
}
|
|
|
|
ssl->handshake->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
|
|
|
|
}
|
2022-05-03 10:22:14 +02:00
|
|
|
#else
|
2023-01-11 14:50:10 +01:00
|
|
|
if (handshake->psk != NULL) {
|
2023-07-17 16:17:40 +02:00
|
|
|
mbedtls_zeroize_and_free(handshake->psk, handshake->psk_len);
|
2019-05-21 11:01:32 +02:00
|
|
|
}
|
2022-05-03 10:22:14 +02:00
|
|
|
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
2022-10-05 14:31:43 +02:00
|
|
|
#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED */
|
2019-05-16 10:08:35 +02:00
|
|
|
|
2022-02-17 07:23:47 +01:00
|
|
|
#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
|
|
|
|
defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
|
2019-05-16 11:11:08 +02:00
|
|
|
/*
|
2022-02-17 07:23:47 +01:00
|
|
|
* Free only the linked list wrapper, not the keys themselves
|
|
|
|
* since the belong to the SNI callback
|
2020-02-05 15:44:10 +01:00
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
ssl_key_cert_free(handshake->sni_key_cert);
|
2022-02-17 07:23:47 +01:00
|
|
|
#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
|
2019-05-16 11:11:08 +02:00
|
|
|
|
2022-02-17 07:23:47 +01:00
|
|
|
#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_x509_crt_restart_free(&handshake->ecrs_ctx);
|
|
|
|
if (handshake->ecrs_peer_cert != NULL) {
|
|
|
|
mbedtls_x509_crt_free(handshake->ecrs_peer_cert);
|
|
|
|
mbedtls_free(handshake->ecrs_peer_cert);
|
2019-05-16 11:11:08 +02:00
|
|
|
}
|
2022-02-17 07:23:47 +01:00
|
|
|
#endif
|
2019-05-16 11:11:08 +02:00
|
|
|
|
2022-02-17 07:23:47 +01:00
|
|
|
#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
|
|
|
|
!defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_pk_free(&handshake->peer_pubkey);
|
2022-02-17 07:23:47 +01:00
|
|
|
#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
|
2019-05-24 12:06:29 +02:00
|
|
|
|
2022-02-09 07:02:25 +01:00
|
|
|
#if defined(MBEDTLS_SSL_CLI_C) && \
|
2023-01-11 14:50:10 +01:00
|
|
|
(defined(MBEDTLS_SSL_PROTO_DTLS) || defined(MBEDTLS_SSL_PROTO_TLS1_3))
|
|
|
|
mbedtls_free(handshake->cookie);
|
2022-02-09 07:02:25 +01:00
|
|
|
#endif /* MBEDTLS_SSL_CLI_C &&
|
|
|
|
( MBEDTLS_SSL_PROTO_DTLS || MBEDTLS_SSL_PROTO_TLS1_3 ) */
|
2022-02-17 07:23:47 +01:00
|
|
|
|
|
|
|
#if defined(MBEDTLS_SSL_PROTO_DTLS)
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_ssl_flight_free(handshake->flight);
|
|
|
|
mbedtls_ssl_buffering_free(ssl);
|
2022-02-17 07:23:47 +01:00
|
|
|
#endif /* MBEDTLS_SSL_PROTO_DTLS */
|
|
|
|
|
2023-07-25 11:14:03 +02:00
|
|
|
#if defined(MBEDTLS_KEY_EXCHANGE_SOME_XXDH_PSA_ANY_ENABLED)
|
2023-07-04 10:02:38 +02:00
|
|
|
if (handshake->xxdh_psa_privkey_is_external == 0) {
|
|
|
|
psa_destroy_key(handshake->xxdh_psa_privkey);
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2023-07-25 11:14:03 +02:00
|
|
|
#endif /* MBEDTLS_KEY_EXCHANGE_SOME_XXDH_PSA_ANY_ENABLED */
|
2022-02-17 07:23:47 +01:00
|
|
|
|
|
|
|
#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_ssl_transform_free(handshake->transform_handshake);
|
|
|
|
mbedtls_free(handshake->transform_handshake);
|
2022-11-04 07:07:25 +01:00
|
|
|
#if defined(MBEDTLS_SSL_EARLY_DATA)
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_ssl_transform_free(handshake->transform_earlydata);
|
|
|
|
mbedtls_free(handshake->transform_earlydata);
|
2022-11-04 07:07:25 +01:00
|
|
|
#endif
|
2022-02-17 07:23:47 +01:00
|
|
|
#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
|
|
|
|
|
|
|
|
|
|
|
|
#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 */
|
2023-01-11 14:50:10 +01:00
|
|
|
handle_buffer_resizing(ssl, 1, mbedtls_ssl_get_input_buflen(ssl),
|
|
|
|
mbedtls_ssl_get_output_buflen(ssl));
|
2019-05-24 12:06:29 +02:00
|
|
|
#endif
|
|
|
|
|
2022-02-17 07:23:47 +01:00
|
|
|
/* mbedtls_platform_zeroize MUST be last one in this function */
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_platform_zeroize(handshake,
|
|
|
|
sizeof(mbedtls_ssl_handshake_params));
|
2022-02-17 07:23:47 +01:00
|
|
|
}
|
2015-09-01 17:43:40 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
void mbedtls_ssl_session_free(mbedtls_ssl_session *session)
|
2022-02-17 07:23:47 +01:00
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
if (session == NULL) {
|
2022-02-17 07:23:47 +01:00
|
|
|
return;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2022-02-17 07:23:47 +01:00
|
|
|
|
|
|
|
#if defined(MBEDTLS_X509_CRT_PARSE_C)
|
2023-01-11 14:50:10 +01:00
|
|
|
ssl_clear_peer_cert(session);
|
2009-01-03 22:22:43 +01:00
|
|
|
#endif
|
|
|
|
|
2022-02-17 07:23:47 +01:00
|
|
|
#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
|
2022-10-09 13:14:39 +02:00
|
|
|
#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
|
|
|
|
defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_free(session->hostname);
|
2022-09-20 13:35:41 +02:00
|
|
|
#endif
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_free(session->ticket);
|
2022-02-17 07:23:47 +01:00
|
|
|
#endif
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_platform_zeroize(session, sizeof(mbedtls_ssl_session));
|
Store TLS version in SSL session structure
Instances of `mbedtls_ssl_session` represent data enabling session resumption.
With the introduction of TLS 1.3, the format of this data changes. We therefore
need TLS-version field as part of `mbedtlsl_ssl_session` which allows distinguish
1.2 and 1.3 sessions.
This commit introduces such a TLS-version field to mbedtls_ssl_session.
The change has a few ramifications:
- Session serialization/deserialization routines need to be adjusted.
This is achieved by adding the TLS-version after the header of
Mbed TLS version+config, and by having the subsequent structure
of the serialized data depend on the value of this field.
The details are described in terms of the RFC 8446 presentation language.
The 1.2 session (de)serialization are moved into static helper functions,
while the top-level session (de)serialization only parses the Mbed TLS
version+config header and the TLS-version field, and dispatches according
to the found version.
This way, it will be easy to add support for TLS 1.3 sessions in the future.
- Tests for session serialization need to be adjusted
- Once we add support for TLS 1.3, with runtime negotiation of 1.2 vs. 1.3,
we will need to have some logic comparing the TLS version of the proposed session
to the negotiated TLS version. For now, however, we only support TLS 1.2,
and no such logic is needed. Instead, we just store the TLS version in the
session structure at the same point when we populate mbedtls_ssl_context.minor_ver.
The change introduces some overlap between `mbedtls_ssl_session.minor_ver` and
`mbedtls_ssl_context.minor_ver`, which should be studied and potentially resolved.
However, with both fields being private and explicitly marked so, this can happen
in a later change.
Signed-off-by: Hanno Becker <hanno.becker@arm.com>
2021-07-23 07:25:48 +02:00
|
|
|
}
|
|
|
|
|
2022-02-17 07:23:47 +01:00
|
|
|
#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
|
|
|
|
|
|
|
|
#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 */
|
|
|
|
|
|
|
|
#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT 1u
|
|
|
|
|
|
|
|
#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 \
|
2023-01-11 14:50:10 +01:00
|
|
|
((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))
|
2022-02-17 07:23:47 +01:00
|
|
|
|
2023-12-28 10:07:43 +01:00
|
|
|
static const unsigned char ssl_serialized_context_header[] = {
|
2022-02-17 07:23:47 +01:00
|
|
|
MBEDTLS_VERSION_MAJOR,
|
|
|
|
MBEDTLS_VERSION_MINOR,
|
|
|
|
MBEDTLS_VERSION_PATCH,
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_BYTE_1(SSL_SERIALIZED_SESSION_CONFIG_BITFLAG),
|
|
|
|
MBEDTLS_BYTE_0(SSL_SERIALIZED_SESSION_CONFIG_BITFLAG),
|
|
|
|
MBEDTLS_BYTE_2(SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG),
|
|
|
|
MBEDTLS_BYTE_1(SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG),
|
|
|
|
MBEDTLS_BYTE_0(SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG),
|
2022-02-17 07:23:47 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Serialize a full SSL context
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
* opaque context_format[5]; // version-specific field determining
|
|
|
|
* // the format of the remaining
|
|
|
|
* // serialized data.
|
|
|
|
* Note: When updating the format, remember to keep these
|
|
|
|
* version+format bytes. (We may make their size part of the API.)
|
|
|
|
*
|
|
|
|
* // 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)
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_ssl_context_save(mbedtls_ssl_context *ssl,
|
|
|
|
unsigned char *buf,
|
|
|
|
size_t buf_len,
|
|
|
|
size_t *olen)
|
Store TLS version in SSL session structure
Instances of `mbedtls_ssl_session` represent data enabling session resumption.
With the introduction of TLS 1.3, the format of this data changes. We therefore
need TLS-version field as part of `mbedtlsl_ssl_session` which allows distinguish
1.2 and 1.3 sessions.
This commit introduces such a TLS-version field to mbedtls_ssl_session.
The change has a few ramifications:
- Session serialization/deserialization routines need to be adjusted.
This is achieved by adding the TLS-version after the header of
Mbed TLS version+config, and by having the subsequent structure
of the serialized data depend on the value of this field.
The details are described in terms of the RFC 8446 presentation language.
The 1.2 session (de)serialization are moved into static helper functions,
while the top-level session (de)serialization only parses the Mbed TLS
version+config header and the TLS-version field, and dispatches according
to the found version.
This way, it will be easy to add support for TLS 1.3 sessions in the future.
- Tests for session serialization need to be adjusted
- Once we add support for TLS 1.3, with runtime negotiation of 1.2 vs. 1.3,
we will need to have some logic comparing the TLS version of the proposed session
to the negotiated TLS version. For now, however, we only support TLS 1.2,
and no such logic is needed. Instead, we just store the TLS version in the
session structure at the same point when we populate mbedtls_ssl_context.minor_ver.
The change introduces some overlap between `mbedtls_ssl_session.minor_ver` and
`mbedtls_ssl_context.minor_ver`, which should be studied and potentially resolved.
However, with both fields being private and explicitly marked so, this can happen
in a later change.
Signed-off-by: Hanno Becker <hanno.becker@arm.com>
2021-07-23 07:25:48 +02:00
|
|
|
{
|
|
|
|
unsigned char *p = buf;
|
|
|
|
size_t used = 0;
|
2022-02-17 07:23:47 +01:00
|
|
|
size_t session_len;
|
|
|
|
int ret = 0;
|
Store TLS version in SSL session structure
Instances of `mbedtls_ssl_session` represent data enabling session resumption.
With the introduction of TLS 1.3, the format of this data changes. We therefore
need TLS-version field as part of `mbedtlsl_ssl_session` which allows distinguish
1.2 and 1.3 sessions.
This commit introduces such a TLS-version field to mbedtls_ssl_session.
The change has a few ramifications:
- Session serialization/deserialization routines need to be adjusted.
This is achieved by adding the TLS-version after the header of
Mbed TLS version+config, and by having the subsequent structure
of the serialized data depend on the value of this field.
The details are described in terms of the RFC 8446 presentation language.
The 1.2 session (de)serialization are moved into static helper functions,
while the top-level session (de)serialization only parses the Mbed TLS
version+config header and the TLS-version field, and dispatches according
to the found version.
This way, it will be easy to add support for TLS 1.3 sessions in the future.
- Tests for session serialization need to be adjusted
- Once we add support for TLS 1.3, with runtime negotiation of 1.2 vs. 1.3,
we will need to have some logic comparing the TLS version of the proposed session
to the negotiated TLS version. For now, however, we only support TLS 1.2,
and no such logic is needed. Instead, we just store the TLS version in the
session structure at the same point when we populate mbedtls_ssl_context.minor_ver.
The change introduces some overlap between `mbedtls_ssl_session.minor_ver` and
`mbedtls_ssl_context.minor_ver`, which should be studied and potentially resolved.
However, with both fields being private and explicitly marked so, this can happen
in a later change.
Signed-off-by: Hanno Becker <hanno.becker@arm.com>
2021-07-23 07:25:48 +02:00
|
|
|
|
2022-02-17 07:23:47 +01: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.
|
|
|
|
*/
|
|
|
|
/* The initial handshake must be over */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (mbedtls_ssl_is_handshake_over(ssl) == 0) {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(1, ("Initial handshake isn't over"));
|
|
|
|
return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
|
2022-02-17 07:23:47 +01:00
|
|
|
}
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->handshake != NULL) {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(1, ("Handshake isn't completed"));
|
|
|
|
return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
|
2022-02-17 07:23:47 +01:00
|
|
|
}
|
|
|
|
/* Double-check that sub-structures are indeed ready */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->transform == NULL || ssl->session == NULL) {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(1, ("Serialised structures aren't ready"));
|
|
|
|
return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
|
2022-02-17 07:23:47 +01:00
|
|
|
}
|
|
|
|
/* There must be no pending incoming or outgoing data */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (mbedtls_ssl_check_pending(ssl) != 0) {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(1, ("There is pending incoming data"));
|
|
|
|
return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
|
2022-02-17 07:23:47 +01:00
|
|
|
}
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->out_left != 0) {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(1, ("There is pending outgoing data"));
|
|
|
|
return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
|
Store TLS version in SSL session structure
Instances of `mbedtls_ssl_session` represent data enabling session resumption.
With the introduction of TLS 1.3, the format of this data changes. We therefore
need TLS-version field as part of `mbedtlsl_ssl_session` which allows distinguish
1.2 and 1.3 sessions.
This commit introduces such a TLS-version field to mbedtls_ssl_session.
The change has a few ramifications:
- Session serialization/deserialization routines need to be adjusted.
This is achieved by adding the TLS-version after the header of
Mbed TLS version+config, and by having the subsequent structure
of the serialized data depend on the value of this field.
The details are described in terms of the RFC 8446 presentation language.
The 1.2 session (de)serialization are moved into static helper functions,
while the top-level session (de)serialization only parses the Mbed TLS
version+config header and the TLS-version field, and dispatches according
to the found version.
This way, it will be easy to add support for TLS 1.3 sessions in the future.
- Tests for session serialization need to be adjusted
- Once we add support for TLS 1.3, with runtime negotiation of 1.2 vs. 1.3,
we will need to have some logic comparing the TLS version of the proposed session
to the negotiated TLS version. For now, however, we only support TLS 1.2,
and no such logic is needed. Instead, we just store the TLS version in the
session structure at the same point when we populate mbedtls_ssl_context.minor_ver.
The change introduces some overlap between `mbedtls_ssl_session.minor_ver` and
`mbedtls_ssl_context.minor_ver`, which should be studied and potentially resolved.
However, with both fields being private and explicitly marked so, this can happen
in a later change.
Signed-off-by: Hanno Becker <hanno.becker@arm.com>
2021-07-23 07:25:48 +02:00
|
|
|
}
|
2022-12-06 17:31:25 +01:00
|
|
|
/* Protocol must be DTLS, not TLS */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(1, ("Only DTLS is supported"));
|
|
|
|
return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
|
Store TLS version in SSL session structure
Instances of `mbedtls_ssl_session` represent data enabling session resumption.
With the introduction of TLS 1.3, the format of this data changes. We therefore
need TLS-version field as part of `mbedtlsl_ssl_session` which allows distinguish
1.2 and 1.3 sessions.
This commit introduces such a TLS-version field to mbedtls_ssl_session.
The change has a few ramifications:
- Session serialization/deserialization routines need to be adjusted.
This is achieved by adding the TLS-version after the header of
Mbed TLS version+config, and by having the subsequent structure
of the serialized data depend on the value of this field.
The details are described in terms of the RFC 8446 presentation language.
The 1.2 session (de)serialization are moved into static helper functions,
while the top-level session (de)serialization only parses the Mbed TLS
version+config header and the TLS-version field, and dispatches according
to the found version.
This way, it will be easy to add support for TLS 1.3 sessions in the future.
- Tests for session serialization need to be adjusted
- Once we add support for TLS 1.3, with runtime negotiation of 1.2 vs. 1.3,
we will need to have some logic comparing the TLS version of the proposed session
to the negotiated TLS version. For now, however, we only support TLS 1.2,
and no such logic is needed. Instead, we just store the TLS version in the
session structure at the same point when we populate mbedtls_ssl_context.minor_ver.
The change introduces some overlap between `mbedtls_ssl_session.minor_ver` and
`mbedtls_ssl_context.minor_ver`, which should be studied and potentially resolved.
However, with both fields being private and explicitly marked so, this can happen
in a later change.
Signed-off-by: Hanno Becker <hanno.becker@arm.com>
2021-07-23 07:25:48 +02:00
|
|
|
}
|
2022-02-17 07:23:47 +01:00
|
|
|
/* Version must be 1.2 */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->tls_version != MBEDTLS_SSL_VERSION_TLS1_2) {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(1, ("Only version 1.2 supported"));
|
|
|
|
return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
|
Store TLS version in SSL session structure
Instances of `mbedtls_ssl_session` represent data enabling session resumption.
With the introduction of TLS 1.3, the format of this data changes. We therefore
need TLS-version field as part of `mbedtlsl_ssl_session` which allows distinguish
1.2 and 1.3 sessions.
This commit introduces such a TLS-version field to mbedtls_ssl_session.
The change has a few ramifications:
- Session serialization/deserialization routines need to be adjusted.
This is achieved by adding the TLS-version after the header of
Mbed TLS version+config, and by having the subsequent structure
of the serialized data depend on the value of this field.
The details are described in terms of the RFC 8446 presentation language.
The 1.2 session (de)serialization are moved into static helper functions,
while the top-level session (de)serialization only parses the Mbed TLS
version+config header and the TLS-version field, and dispatches according
to the found version.
This way, it will be easy to add support for TLS 1.3 sessions in the future.
- Tests for session serialization need to be adjusted
- Once we add support for TLS 1.3, with runtime negotiation of 1.2 vs. 1.3,
we will need to have some logic comparing the TLS version of the proposed session
to the negotiated TLS version. For now, however, we only support TLS 1.2,
and no such logic is needed. Instead, we just store the TLS version in the
session structure at the same point when we populate mbedtls_ssl_context.minor_ver.
The change introduces some overlap between `mbedtls_ssl_session.minor_ver` and
`mbedtls_ssl_context.minor_ver`, which should be studied and potentially resolved.
However, with both fields being private and explicitly marked so, this can happen
in a later change.
Signed-off-by: Hanno Becker <hanno.becker@arm.com>
2021-07-23 07:25:48 +02:00
|
|
|
}
|
2022-02-17 07:23:47 +01:00
|
|
|
/* We must be using an AEAD ciphersuite */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (mbedtls_ssl_transform_uses_aead(ssl->transform) != 1) {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(1, ("Only AEAD ciphersuites supported"));
|
|
|
|
return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
|
2022-02-17 07:23:47 +01:00
|
|
|
}
|
|
|
|
/* Renegotiation must not be enabled */
|
|
|
|
#if defined(MBEDTLS_SSL_RENEGOTIATION)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED) {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(1, ("Renegotiation must not be enabled"));
|
|
|
|
return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
|
Store TLS version in SSL session structure
Instances of `mbedtls_ssl_session` represent data enabling session resumption.
With the introduction of TLS 1.3, the format of this data changes. We therefore
need TLS-version field as part of `mbedtlsl_ssl_session` which allows distinguish
1.2 and 1.3 sessions.
This commit introduces such a TLS-version field to mbedtls_ssl_session.
The change has a few ramifications:
- Session serialization/deserialization routines need to be adjusted.
This is achieved by adding the TLS-version after the header of
Mbed TLS version+config, and by having the subsequent structure
of the serialized data depend on the value of this field.
The details are described in terms of the RFC 8446 presentation language.
The 1.2 session (de)serialization are moved into static helper functions,
while the top-level session (de)serialization only parses the Mbed TLS
version+config header and the TLS-version field, and dispatches according
to the found version.
This way, it will be easy to add support for TLS 1.3 sessions in the future.
- Tests for session serialization need to be adjusted
- Once we add support for TLS 1.3, with runtime negotiation of 1.2 vs. 1.3,
we will need to have some logic comparing the TLS version of the proposed session
to the negotiated TLS version. For now, however, we only support TLS 1.2,
and no such logic is needed. Instead, we just store the TLS version in the
session structure at the same point when we populate mbedtls_ssl_context.minor_ver.
The change introduces some overlap between `mbedtls_ssl_session.minor_ver` and
`mbedtls_ssl_context.minor_ver`, which should be studied and potentially resolved.
However, with both fields being private and explicitly marked so, this can happen
in a later change.
Signed-off-by: Hanno Becker <hanno.becker@arm.com>
2021-07-23 07:25:48 +02:00
|
|
|
}
|
2020-02-05 15:44:10 +01:00
|
|
|
#endif
|
2013-10-30 13:06:54 +01:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
/*
|
2022-02-17 07:23:47 +01:00
|
|
|
* Version and format identifier
|
2020-02-05 15:44:10 +01:00
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
used += sizeof(ssl_serialized_context_header);
|
2014-11-04 21:04:22 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (used <= buf_len) {
|
|
|
|
memcpy(p, ssl_serialized_context_header,
|
|
|
|
sizeof(ssl_serialized_context_header));
|
|
|
|
p += sizeof(ssl_serialized_context_header);
|
2022-02-17 07:23:47 +01:00
|
|
|
}
|
2016-12-15 18:01:16 +01:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
/*
|
2022-02-17 07:23:47 +01:00
|
|
|
* Session (length + data)
|
2020-02-05 15:44:10 +01:00
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = ssl_session_save(ssl->session, 1, NULL, 0, &session_len);
|
|
|
|
if (ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL) {
|
|
|
|
return ret;
|
|
|
|
}
|
2015-09-01 17:43:40 +02:00
|
|
|
|
2022-02-17 07:23:47 +01:00
|
|
|
used += 4 + session_len;
|
2023-01-11 14:50:10 +01:00
|
|
|
if (used <= buf_len) {
|
|
|
|
MBEDTLS_PUT_UINT32_BE(session_len, p, 0);
|
2022-02-17 07:23:47 +01:00
|
|
|
p += 4;
|
2014-09-23 09:42:16 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = ssl_session_save(ssl->session, 1,
|
|
|
|
p, session_len, &session_len);
|
|
|
|
if (ret != 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
2014-09-23 09:42:16 +02:00
|
|
|
|
2022-02-17 07:23:47 +01:00
|
|
|
p += session_len;
|
|
|
|
}
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2017-05-24 17:27:30 +02:00
|
|
|
/*
|
2022-02-17 07:23:47 +01:00
|
|
|
* Transform
|
2017-05-24 17:27:30 +02:00
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
used += sizeof(ssl->transform->randbytes);
|
|
|
|
if (used <= buf_len) {
|
|
|
|
memcpy(p, ssl->transform->randbytes,
|
|
|
|
sizeof(ssl->transform->randbytes));
|
|
|
|
p += sizeof(ssl->transform->randbytes);
|
2009-01-03 22:22:43 +01:00
|
|
|
}
|
2020-02-05 15:44:10 +01:00
|
|
|
|
2022-02-17 07:23:47 +01:00
|
|
|
#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
|
2023-11-04 00:36:06 +01:00
|
|
|
used += 2U + ssl->transform->in_cid_len + ssl->transform->out_cid_len;
|
2023-01-11 14:50:10 +01:00
|
|
|
if (used <= buf_len) {
|
2022-02-17 07:23:47 +01:00
|
|
|
*p++ = ssl->transform->in_cid_len;
|
2023-01-11 14:50:10 +01:00
|
|
|
memcpy(p, ssl->transform->in_cid, ssl->transform->in_cid_len);
|
2022-02-17 07:23:47 +01:00
|
|
|
p += ssl->transform->in_cid_len;
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2022-02-17 07:23:47 +01:00
|
|
|
*p++ = ssl->transform->out_cid_len;
|
2023-01-11 14:50:10 +01:00
|
|
|
memcpy(p, ssl->transform->out_cid, ssl->transform->out_cid_len);
|
2022-02-17 07:23:47 +01:00
|
|
|
p += ssl->transform->out_cid_len;
|
2020-02-05 15:44:10 +01:00
|
|
|
}
|
2022-02-17 07:23:47 +01:00
|
|
|
#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
|
2011-05-18 15:32:51 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
/*
|
2022-02-17 07:23:47 +01:00
|
|
|
* Saved fields from top-level ssl_context structure
|
2020-02-05 15:44:10 +01:00
|
|
|
*/
|
2022-02-17 07:23:47 +01:00
|
|
|
used += 4;
|
2023-01-11 14:50:10 +01:00
|
|
|
if (used <= buf_len) {
|
|
|
|
MBEDTLS_PUT_UINT32_BE(ssl->badmac_seen, p, 0);
|
2022-02-17 07:23:47 +01:00
|
|
|
p += 4;
|
|
|
|
}
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2022-02-17 07:23:47 +01:00
|
|
|
#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
|
|
|
|
used += 16;
|
2023-01-11 14:50:10 +01:00
|
|
|
if (used <= buf_len) {
|
|
|
|
MBEDTLS_PUT_UINT64_BE(ssl->in_window_top, p, 0);
|
2022-02-17 07:23:47 +01:00
|
|
|
p += 8;
|
2012-09-16 21:57:18 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_PUT_UINT64_BE(ssl->in_window, p, 0);
|
2022-02-17 07:23:47 +01:00
|
|
|
p += 8;
|
|
|
|
}
|
|
|
|
#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_SSL_PROTO_DTLS)
|
|
|
|
used += 1;
|
2023-01-11 14:50:10 +01:00
|
|
|
if (used <= buf_len) {
|
2022-02-17 07:23:47 +01:00
|
|
|
*p++ = ssl->disable_datagram_packing;
|
|
|
|
}
|
|
|
|
#endif /* MBEDTLS_SSL_PROTO_DTLS */
|
2017-05-24 17:27:30 +02:00
|
|
|
|
2022-02-17 07:23:47 +01:00
|
|
|
used += MBEDTLS_SSL_SEQUENCE_NUMBER_LEN;
|
2023-01-11 14:50:10 +01:00
|
|
|
if (used <= buf_len) {
|
|
|
|
memcpy(p, ssl->cur_out_ctr, MBEDTLS_SSL_SEQUENCE_NUMBER_LEN);
|
2022-02-17 07:23:47 +01:00
|
|
|
p += MBEDTLS_SSL_SEQUENCE_NUMBER_LEN;
|
|
|
|
}
|
2014-09-06 12:27:02 +02:00
|
|
|
|
2022-02-17 07:23:47 +01:00
|
|
|
#if defined(MBEDTLS_SSL_PROTO_DTLS)
|
|
|
|
used += 2;
|
2023-01-11 14:50:10 +01:00
|
|
|
if (used <= buf_len) {
|
|
|
|
MBEDTLS_PUT_UINT16_BE(ssl->mtu, p, 0);
|
2022-02-17 07:23:47 +01:00
|
|
|
p += 2;
|
2020-02-05 15:44:10 +01:00
|
|
|
}
|
2022-02-17 07:23:47 +01:00
|
|
|
#endif /* MBEDTLS_SSL_PROTO_DTLS */
|
2014-09-06 12:27:02 +02:00
|
|
|
|
2022-02-17 07:23:47 +01:00
|
|
|
#if defined(MBEDTLS_SSL_ALPN)
|
|
|
|
{
|
|
|
|
const uint8_t alpn_len = ssl->alpn_chosen
|
2023-01-11 14:50:10 +01:00
|
|
|
? (uint8_t) strlen(ssl->alpn_chosen)
|
2022-02-17 07:23:47 +01:00
|
|
|
: 0;
|
2014-09-06 12:27:02 +02:00
|
|
|
|
2022-02-17 07:23:47 +01:00
|
|
|
used += 1 + alpn_len;
|
2023-01-11 14:50:10 +01:00
|
|
|
if (used <= buf_len) {
|
2022-02-17 07:23:47 +01:00
|
|
|
*p++ = alpn_len;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->alpn_chosen != NULL) {
|
|
|
|
memcpy(p, ssl->alpn_chosen, alpn_len);
|
2022-02-17 07:23:47 +01:00
|
|
|
p += alpn_len;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif /* MBEDTLS_SSL_ALPN */
|
2017-05-24 17:27:30 +02:00
|
|
|
|
2020-02-05 15:44:10 +01:00
|
|
|
/*
|
2022-02-17 07:23:47 +01:00
|
|
|
* Done
|
2020-02-05 15:44:10 +01:00
|
|
|
*/
|
2022-02-17 07:23:47 +01:00
|
|
|
*olen = used;
|
2012-09-16 21:57:18 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (used > buf_len) {
|
|
|
|
return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
|
|
|
|
}
|
2017-05-24 17:27:30 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_BUF(4, "saved context", buf, used);
|
2014-08-19 12:28:50 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return mbedtls_ssl_session_reset_int(ssl, 0);
|
2022-02-17 07:23:47 +01:00
|
|
|
}
|
2014-08-19 12:28:50 +02:00
|
|
|
|
2022-02-17 07:23:47 +01:00
|
|
|
/*
|
|
|
|
* Deserialize context, see mbedtls_ssl_context_save() for format.
|
|
|
|
*
|
|
|
|
* This internal version is wrapped by a public function that cleans up in
|
|
|
|
* case of error.
|
|
|
|
*/
|
2022-06-17 10:52:54 +02:00
|
|
|
MBEDTLS_CHECK_RETURN_CRITICAL
|
2023-01-11 14:50:10 +01:00
|
|
|
static int ssl_context_load(mbedtls_ssl_context *ssl,
|
|
|
|
const unsigned char *buf,
|
|
|
|
size_t len)
|
Store TLS version in SSL session structure
Instances of `mbedtls_ssl_session` represent data enabling session resumption.
With the introduction of TLS 1.3, the format of this data changes. We therefore
need TLS-version field as part of `mbedtlsl_ssl_session` which allows distinguish
1.2 and 1.3 sessions.
This commit introduces such a TLS-version field to mbedtls_ssl_session.
The change has a few ramifications:
- Session serialization/deserialization routines need to be adjusted.
This is achieved by adding the TLS-version after the header of
Mbed TLS version+config, and by having the subsequent structure
of the serialized data depend on the value of this field.
The details are described in terms of the RFC 8446 presentation language.
The 1.2 session (de)serialization are moved into static helper functions,
while the top-level session (de)serialization only parses the Mbed TLS
version+config header and the TLS-version field, and dispatches according
to the found version.
This way, it will be easy to add support for TLS 1.3 sessions in the future.
- Tests for session serialization need to be adjusted
- Once we add support for TLS 1.3, with runtime negotiation of 1.2 vs. 1.3,
we will need to have some logic comparing the TLS version of the proposed session
to the negotiated TLS version. For now, however, we only support TLS 1.2,
and no such logic is needed. Instead, we just store the TLS version in the
session structure at the same point when we populate mbedtls_ssl_context.minor_ver.
The change introduces some overlap between `mbedtls_ssl_session.minor_ver` and
`mbedtls_ssl_context.minor_ver`, which should be studied and potentially resolved.
However, with both fields being private and explicitly marked so, this can happen
in a later change.
Signed-off-by: Hanno Becker <hanno.becker@arm.com>
2021-07-23 07:25:48 +02:00
|
|
|
{
|
|
|
|
const unsigned char *p = buf;
|
|
|
|
const unsigned char * const end = buf + len;
|
2022-02-17 07:23:47 +01:00
|
|
|
size_t session_len;
|
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2022-10-13 14:34:38 +02:00
|
|
|
#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
|
2022-09-29 12:31:14 +02:00
|
|
|
tls_prf_fn prf_func = NULL;
|
2022-10-13 14:34:38 +02:00
|
|
|
#endif
|
Store TLS version in SSL session structure
Instances of `mbedtls_ssl_session` represent data enabling session resumption.
With the introduction of TLS 1.3, the format of this data changes. We therefore
need TLS-version field as part of `mbedtlsl_ssl_session` which allows distinguish
1.2 and 1.3 sessions.
This commit introduces such a TLS-version field to mbedtls_ssl_session.
The change has a few ramifications:
- Session serialization/deserialization routines need to be adjusted.
This is achieved by adding the TLS-version after the header of
Mbed TLS version+config, and by having the subsequent structure
of the serialized data depend on the value of this field.
The details are described in terms of the RFC 8446 presentation language.
The 1.2 session (de)serialization are moved into static helper functions,
while the top-level session (de)serialization only parses the Mbed TLS
version+config header and the TLS-version field, and dispatches according
to the found version.
This way, it will be easy to add support for TLS 1.3 sessions in the future.
- Tests for session serialization need to be adjusted
- Once we add support for TLS 1.3, with runtime negotiation of 1.2 vs. 1.3,
we will need to have some logic comparing the TLS version of the proposed session
to the negotiated TLS version. For now, however, we only support TLS 1.2,
and no such logic is needed. Instead, we just store the TLS version in the
session structure at the same point when we populate mbedtls_ssl_context.minor_ver.
The change introduces some overlap between `mbedtls_ssl_session.minor_ver` and
`mbedtls_ssl_context.minor_ver`, which should be studied and potentially resolved.
However, with both fields being private and explicitly marked so, this can happen
in a later change.
Signed-off-by: Hanno Becker <hanno.becker@arm.com>
2021-07-23 07:25:48 +02:00
|
|
|
|
2022-02-17 07:23:47 +01:00
|
|
|
/*
|
|
|
|
* The context should have been freshly setup or reset.
|
|
|
|
* Give the user an error in case of obvious misuse.
|
|
|
|
* (Checking session is useful because it won't be NULL if we're
|
|
|
|
* renegotiating, or if the user mistakenly loaded a session first.)
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->state != MBEDTLS_SSL_HELLO_REQUEST ||
|
|
|
|
ssl->session != NULL) {
|
|
|
|
return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
|
2022-02-17 07:23:47 +01:00
|
|
|
}
|
Store TLS version in SSL session structure
Instances of `mbedtls_ssl_session` represent data enabling session resumption.
With the introduction of TLS 1.3, the format of this data changes. We therefore
need TLS-version field as part of `mbedtlsl_ssl_session` which allows distinguish
1.2 and 1.3 sessions.
This commit introduces such a TLS-version field to mbedtls_ssl_session.
The change has a few ramifications:
- Session serialization/deserialization routines need to be adjusted.
This is achieved by adding the TLS-version after the header of
Mbed TLS version+config, and by having the subsequent structure
of the serialized data depend on the value of this field.
The details are described in terms of the RFC 8446 presentation language.
The 1.2 session (de)serialization are moved into static helper functions,
while the top-level session (de)serialization only parses the Mbed TLS
version+config header and the TLS-version field, and dispatches according
to the found version.
This way, it will be easy to add support for TLS 1.3 sessions in the future.
- Tests for session serialization need to be adjusted
- Once we add support for TLS 1.3, with runtime negotiation of 1.2 vs. 1.3,
we will need to have some logic comparing the TLS version of the proposed session
to the negotiated TLS version. For now, however, we only support TLS 1.2,
and no such logic is needed. Instead, we just store the TLS version in the
session structure at the same point when we populate mbedtls_ssl_context.minor_ver.
The change introduces some overlap between `mbedtls_ssl_session.minor_ver` and
`mbedtls_ssl_context.minor_ver`, which should be studied and potentially resolved.
However, with both fields being private and explicitly marked so, this can happen
in a later change.
Signed-off-by: Hanno Becker <hanno.becker@arm.com>
2021-07-23 07:25:48 +02:00
|
|
|
|
2022-02-17 07:23:47 +01:00
|
|
|
/*
|
|
|
|
* We can't check that the config matches the initial one, but we can at
|
|
|
|
* least check it matches the requirements for serializing.
|
|
|
|
*/
|
2023-09-14 16:45:03 +02:00
|
|
|
if (
|
2022-02-17 07:23:47 +01:00
|
|
|
#if defined(MBEDTLS_SSL_RENEGOTIATION)
|
|
|
|
ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
|
|
|
|
#endif
|
2023-09-14 16:45:03 +02:00
|
|
|
ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
|
|
|
|
ssl->conf->max_tls_version < MBEDTLS_SSL_VERSION_TLS1_2 ||
|
|
|
|
ssl->conf->min_tls_version > MBEDTLS_SSL_VERSION_TLS1_2
|
|
|
|
) {
|
2023-01-11 14:50:10 +01:00
|
|
|
return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
|
Store TLS version in SSL session structure
Instances of `mbedtls_ssl_session` represent data enabling session resumption.
With the introduction of TLS 1.3, the format of this data changes. We therefore
need TLS-version field as part of `mbedtlsl_ssl_session` which allows distinguish
1.2 and 1.3 sessions.
This commit introduces such a TLS-version field to mbedtls_ssl_session.
The change has a few ramifications:
- Session serialization/deserialization routines need to be adjusted.
This is achieved by adding the TLS-version after the header of
Mbed TLS version+config, and by having the subsequent structure
of the serialized data depend on the value of this field.
The details are described in terms of the RFC 8446 presentation language.
The 1.2 session (de)serialization are moved into static helper functions,
while the top-level session (de)serialization only parses the Mbed TLS
version+config header and the TLS-version field, and dispatches according
to the found version.
This way, it will be easy to add support for TLS 1.3 sessions in the future.
- Tests for session serialization need to be adjusted
- Once we add support for TLS 1.3, with runtime negotiation of 1.2 vs. 1.3,
we will need to have some logic comparing the TLS version of the proposed session
to the negotiated TLS version. For now, however, we only support TLS 1.2,
and no such logic is needed. Instead, we just store the TLS version in the
session structure at the same point when we populate mbedtls_ssl_context.minor_ver.
The change introduces some overlap between `mbedtls_ssl_session.minor_ver` and
`mbedtls_ssl_context.minor_ver`, which should be studied and potentially resolved.
However, with both fields being private and explicitly marked so, this can happen
in a later change.
Signed-off-by: Hanno Becker <hanno.becker@arm.com>
2021-07-23 07:25:48 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_BUF(4, "context to load", buf, len);
|
2022-02-17 07:23:47 +01:00
|
|
|
|
Store TLS version in SSL session structure
Instances of `mbedtls_ssl_session` represent data enabling session resumption.
With the introduction of TLS 1.3, the format of this data changes. We therefore
need TLS-version field as part of `mbedtlsl_ssl_session` which allows distinguish
1.2 and 1.3 sessions.
This commit introduces such a TLS-version field to mbedtls_ssl_session.
The change has a few ramifications:
- Session serialization/deserialization routines need to be adjusted.
This is achieved by adding the TLS-version after the header of
Mbed TLS version+config, and by having the subsequent structure
of the serialized data depend on the value of this field.
The details are described in terms of the RFC 8446 presentation language.
The 1.2 session (de)serialization are moved into static helper functions,
while the top-level session (de)serialization only parses the Mbed TLS
version+config header and the TLS-version field, and dispatches according
to the found version.
This way, it will be easy to add support for TLS 1.3 sessions in the future.
- Tests for session serialization need to be adjusted
- Once we add support for TLS 1.3, with runtime negotiation of 1.2 vs. 1.3,
we will need to have some logic comparing the TLS version of the proposed session
to the negotiated TLS version. For now, however, we only support TLS 1.2,
and no such logic is needed. Instead, we just store the TLS version in the
session structure at the same point when we populate mbedtls_ssl_context.minor_ver.
The change introduces some overlap between `mbedtls_ssl_session.minor_ver` and
`mbedtls_ssl_context.minor_ver`, which should be studied and potentially resolved.
However, with both fields being private and explicitly marked so, this can happen
in a later change.
Signed-off-by: Hanno Becker <hanno.becker@arm.com>
2021-07-23 07:25:48 +02:00
|
|
|
/*
|
2022-02-17 07:23:47 +01:00
|
|
|
* Check version identifier
|
Store TLS version in SSL session structure
Instances of `mbedtls_ssl_session` represent data enabling session resumption.
With the introduction of TLS 1.3, the format of this data changes. We therefore
need TLS-version field as part of `mbedtlsl_ssl_session` which allows distinguish
1.2 and 1.3 sessions.
This commit introduces such a TLS-version field to mbedtls_ssl_session.
The change has a few ramifications:
- Session serialization/deserialization routines need to be adjusted.
This is achieved by adding the TLS-version after the header of
Mbed TLS version+config, and by having the subsequent structure
of the serialized data depend on the value of this field.
The details are described in terms of the RFC 8446 presentation language.
The 1.2 session (de)serialization are moved into static helper functions,
while the top-level session (de)serialization only parses the Mbed TLS
version+config header and the TLS-version field, and dispatches according
to the found version.
This way, it will be easy to add support for TLS 1.3 sessions in the future.
- Tests for session serialization need to be adjusted
- Once we add support for TLS 1.3, with runtime negotiation of 1.2 vs. 1.3,
we will need to have some logic comparing the TLS version of the proposed session
to the negotiated TLS version. For now, however, we only support TLS 1.2,
and no such logic is needed. Instead, we just store the TLS version in the
session structure at the same point when we populate mbedtls_ssl_context.minor_ver.
The change introduces some overlap between `mbedtls_ssl_session.minor_ver` and
`mbedtls_ssl_context.minor_ver`, which should be studied and potentially resolved.
However, with both fields being private and explicitly marked so, this can happen
in a later change.
Signed-off-by: Hanno Becker <hanno.becker@arm.com>
2021-07-23 07:25:48 +02:00
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
if ((size_t) (end - p) < sizeof(ssl_serialized_context_header)) {
|
|
|
|
return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
|
|
|
|
}
|
Store TLS version in SSL session structure
Instances of `mbedtls_ssl_session` represent data enabling session resumption.
With the introduction of TLS 1.3, the format of this data changes. We therefore
need TLS-version field as part of `mbedtlsl_ssl_session` which allows distinguish
1.2 and 1.3 sessions.
This commit introduces such a TLS-version field to mbedtls_ssl_session.
The change has a few ramifications:
- Session serialization/deserialization routines need to be adjusted.
This is achieved by adding the TLS-version after the header of
Mbed TLS version+config, and by having the subsequent structure
of the serialized data depend on the value of this field.
The details are described in terms of the RFC 8446 presentation language.
The 1.2 session (de)serialization are moved into static helper functions,
while the top-level session (de)serialization only parses the Mbed TLS
version+config header and the TLS-version field, and dispatches according
to the found version.
This way, it will be easy to add support for TLS 1.3 sessions in the future.
- Tests for session serialization need to be adjusted
- Once we add support for TLS 1.3, with runtime negotiation of 1.2 vs. 1.3,
we will need to have some logic comparing the TLS version of the proposed session
to the negotiated TLS version. For now, however, we only support TLS 1.2,
and no such logic is needed. Instead, we just store the TLS version in the
session structure at the same point when we populate mbedtls_ssl_context.minor_ver.
The change introduces some overlap between `mbedtls_ssl_session.minor_ver` and
`mbedtls_ssl_context.minor_ver`, which should be studied and potentially resolved.
However, with both fields being private and explicitly marked so, this can happen
in a later change.
Signed-off-by: Hanno Becker <hanno.becker@arm.com>
2021-07-23 07:25:48 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (memcmp(p, ssl_serialized_context_header,
|
|
|
|
sizeof(ssl_serialized_context_header)) != 0) {
|
|
|
|
return MBEDTLS_ERR_SSL_VERSION_MISMATCH;
|
Store TLS version in SSL session structure
Instances of `mbedtls_ssl_session` represent data enabling session resumption.
With the introduction of TLS 1.3, the format of this data changes. We therefore
need TLS-version field as part of `mbedtlsl_ssl_session` which allows distinguish
1.2 and 1.3 sessions.
This commit introduces such a TLS-version field to mbedtls_ssl_session.
The change has a few ramifications:
- Session serialization/deserialization routines need to be adjusted.
This is achieved by adding the TLS-version after the header of
Mbed TLS version+config, and by having the subsequent structure
of the serialized data depend on the value of this field.
The details are described in terms of the RFC 8446 presentation language.
The 1.2 session (de)serialization are moved into static helper functions,
while the top-level session (de)serialization only parses the Mbed TLS
version+config header and the TLS-version field, and dispatches according
to the found version.
This way, it will be easy to add support for TLS 1.3 sessions in the future.
- Tests for session serialization need to be adjusted
- Once we add support for TLS 1.3, with runtime negotiation of 1.2 vs. 1.3,
we will need to have some logic comparing the TLS version of the proposed session
to the negotiated TLS version. For now, however, we only support TLS 1.2,
and no such logic is needed. Instead, we just store the TLS version in the
session structure at the same point when we populate mbedtls_ssl_context.minor_ver.
The change introduces some overlap between `mbedtls_ssl_session.minor_ver` and
`mbedtls_ssl_context.minor_ver`, which should be studied and potentially resolved.
However, with both fields being private and explicitly marked so, this can happen
in a later change.
Signed-off-by: Hanno Becker <hanno.becker@arm.com>
2021-07-23 07:25:48 +02:00
|
|
|
}
|
2023-01-11 14:50:10 +01:00
|
|
|
p += sizeof(ssl_serialized_context_header);
|
Store TLS version in SSL session structure
Instances of `mbedtls_ssl_session` represent data enabling session resumption.
With the introduction of TLS 1.3, the format of this data changes. We therefore
need TLS-version field as part of `mbedtlsl_ssl_session` which allows distinguish
1.2 and 1.3 sessions.
This commit introduces such a TLS-version field to mbedtls_ssl_session.
The change has a few ramifications:
- Session serialization/deserialization routines need to be adjusted.
This is achieved by adding the TLS-version after the header of
Mbed TLS version+config, and by having the subsequent structure
of the serialized data depend on the value of this field.
The details are described in terms of the RFC 8446 presentation language.
The 1.2 session (de)serialization are moved into static helper functions,
while the top-level session (de)serialization only parses the Mbed TLS
version+config header and the TLS-version field, and dispatches according
to the found version.
This way, it will be easy to add support for TLS 1.3 sessions in the future.
- Tests for session serialization need to be adjusted
- Once we add support for TLS 1.3, with runtime negotiation of 1.2 vs. 1.3,
we will need to have some logic comparing the TLS version of the proposed session
to the negotiated TLS version. For now, however, we only support TLS 1.2,
and no such logic is needed. Instead, we just store the TLS version in the
session structure at the same point when we populate mbedtls_ssl_context.minor_ver.
The change introduces some overlap between `mbedtls_ssl_session.minor_ver` and
`mbedtls_ssl_context.minor_ver`, which should be studied and potentially resolved.
However, with both fields being private and explicitly marked so, this can happen
in a later change.
Signed-off-by: Hanno Becker <hanno.becker@arm.com>
2021-07-23 07:25:48 +02:00
|
|
|
|
2022-02-17 07:23:47 +01:00
|
|
|
/*
|
|
|
|
* Session
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
if ((size_t) (end - p) < 4) {
|
|
|
|
return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
|
|
|
|
}
|
Store TLS version in SSL session structure
Instances of `mbedtls_ssl_session` represent data enabling session resumption.
With the introduction of TLS 1.3, the format of this data changes. We therefore
need TLS-version field as part of `mbedtlsl_ssl_session` which allows distinguish
1.2 and 1.3 sessions.
This commit introduces such a TLS-version field to mbedtls_ssl_session.
The change has a few ramifications:
- Session serialization/deserialization routines need to be adjusted.
This is achieved by adding the TLS-version after the header of
Mbed TLS version+config, and by having the subsequent structure
of the serialized data depend on the value of this field.
The details are described in terms of the RFC 8446 presentation language.
The 1.2 session (de)serialization are moved into static helper functions,
while the top-level session (de)serialization only parses the Mbed TLS
version+config header and the TLS-version field, and dispatches according
to the found version.
This way, it will be easy to add support for TLS 1.3 sessions in the future.
- Tests for session serialization need to be adjusted
- Once we add support for TLS 1.3, with runtime negotiation of 1.2 vs. 1.3,
we will need to have some logic comparing the TLS version of the proposed session
to the negotiated TLS version. For now, however, we only support TLS 1.2,
and no such logic is needed. Instead, we just store the TLS version in the
session structure at the same point when we populate mbedtls_ssl_context.minor_ver.
The change introduces some overlap between `mbedtls_ssl_session.minor_ver` and
`mbedtls_ssl_context.minor_ver`, which should be studied and potentially resolved.
However, with both fields being private and explicitly marked so, this can happen
in a later change.
Signed-off-by: Hanno Becker <hanno.becker@arm.com>
2021-07-23 07:25:48 +02:00
|
|
|
|
2023-05-23 18:34:33 +02:00
|
|
|
session_len = MBEDTLS_GET_UINT32_BE(p, 0);
|
2022-02-17 07:23:47 +01:00
|
|
|
p += 4;
|
|
|
|
|
|
|
|
/* This has been allocated by ssl_handshake_init(), called by
|
|
|
|
* by either mbedtls_ssl_session_reset_int() or mbedtls_ssl_setup(). */
|
|
|
|
ssl->session = ssl->session_negotiate;
|
|
|
|
ssl->session_in = ssl->session;
|
|
|
|
ssl->session_out = ssl->session;
|
|
|
|
ssl->session_negotiate = NULL;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if ((size_t) (end - p) < session_len) {
|
|
|
|
return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
|
|
|
|
}
|
2014-10-15 13:52:48 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = ssl_session_load(ssl->session, 1, p, session_len);
|
|
|
|
if (ret != 0) {
|
|
|
|
mbedtls_ssl_session_free(ssl->session);
|
|
|
|
return ret;
|
2022-02-17 07:23:47 +01:00
|
|
|
}
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2022-02-17 07:23:47 +01:00
|
|
|
p += session_len;
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2022-02-17 07:23:47 +01:00
|
|
|
/*
|
|
|
|
* Transform
|
|
|
|
*/
|
2021-08-07 20:13:43 +02:00
|
|
|
|
2022-02-17 07:23:47 +01:00
|
|
|
/* This has been allocated by ssl_handshake_init(), called by
|
|
|
|
* by either mbedtls_ssl_session_reset_int() or mbedtls_ssl_setup(). */
|
2022-12-01 11:57:19 +01:00
|
|
|
#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
|
2022-02-17 07:23:47 +01:00
|
|
|
ssl->transform = ssl->transform_negotiate;
|
|
|
|
ssl->transform_in = ssl->transform;
|
|
|
|
ssl->transform_out = ssl->transform;
|
|
|
|
ssl->transform_negotiate = NULL;
|
2022-12-01 11:57:19 +01:00
|
|
|
#endif
|
2022-02-17 07:23:47 +01:00
|
|
|
|
2022-10-13 14:34:38 +02:00
|
|
|
#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
|
2023-01-11 14:50:10 +01:00
|
|
|
prf_func = ssl_tls12prf_from_cs(ssl->session->ciphersuite);
|
|
|
|
if (prf_func == NULL) {
|
|
|
|
return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
|
|
|
|
}
|
2022-09-29 12:31:14 +02:00
|
|
|
|
2022-02-17 07:23:47 +01:00
|
|
|
/* Read random bytes and populate structure */
|
2023-01-11 14:50:10 +01:00
|
|
|
if ((size_t) (end - p) < sizeof(ssl->transform->randbytes)) {
|
|
|
|
return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
|
|
|
|
}
|
2022-10-13 14:34:38 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = ssl_tls12_populate_transform(ssl->transform,
|
|
|
|
ssl->session->ciphersuite,
|
|
|
|
ssl->session->master,
|
2022-04-05 11:16:53 +02:00
|
|
|
#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
|
2023-01-11 14:50:10 +01:00
|
|
|
ssl->session->encrypt_then_mac,
|
2022-04-05 11:16:53 +02:00
|
|
|
#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
|
2023-01-11 14:50:10 +01:00
|
|
|
prf_func,
|
|
|
|
p, /* currently pointing to randbytes */
|
|
|
|
MBEDTLS_SSL_VERSION_TLS1_2, /* (D)TLS 1.2 is forced */
|
|
|
|
ssl->conf->endpoint,
|
|
|
|
ssl);
|
|
|
|
if (ret != 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
2022-02-17 07:59:29 +01:00
|
|
|
#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
|
2023-01-11 14:50:10 +01:00
|
|
|
p += sizeof(ssl->transform->randbytes);
|
2021-08-07 20:13:43 +02:00
|
|
|
|
2022-02-17 07:23:47 +01:00
|
|
|
#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
|
|
|
|
/* Read connection IDs and store them */
|
2023-01-11 14:50:10 +01:00
|
|
|
if ((size_t) (end - p) < 1) {
|
|
|
|
return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
|
|
|
|
}
|
2021-08-07 20:13:43 +02:00
|
|
|
|
2022-02-17 07:23:47 +01:00
|
|
|
ssl->transform->in_cid_len = *p++;
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if ((size_t) (end - p) < ssl->transform->in_cid_len + 1u) {
|
|
|
|
return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
|
|
|
|
}
|
2021-08-07 20:13:43 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
memcpy(ssl->transform->in_cid, p, ssl->transform->in_cid_len);
|
2022-02-17 07:23:47 +01:00
|
|
|
p += ssl->transform->in_cid_len;
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2022-02-17 07:23:47 +01:00
|
|
|
ssl->transform->out_cid_len = *p++;
|
2021-09-13 13:26:39 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if ((size_t) (end - p) < ssl->transform->out_cid_len) {
|
|
|
|
return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
|
|
|
|
}
|
2021-08-06 11:11:51 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
memcpy(ssl->transform->out_cid, p, ssl->transform->out_cid_len);
|
2022-02-17 07:23:47 +01:00
|
|
|
p += ssl->transform->out_cid_len;
|
|
|
|
#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
|
2021-08-06 11:11:51 +02:00
|
|
|
|
2022-02-17 07:23:47 +01:00
|
|
|
/*
|
|
|
|
* Saved fields from top-level ssl_context structure
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
if ((size_t) (end - p) < 4) {
|
|
|
|
return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
|
|
|
|
}
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2023-05-23 18:34:33 +02:00
|
|
|
ssl->badmac_seen = MBEDTLS_GET_UINT32_BE(p, 0);
|
2022-02-17 07:23:47 +01:00
|
|
|
p += 4;
|
2021-09-13 13:26:39 +02:00
|
|
|
|
2022-02-17 07:23:47 +01:00
|
|
|
#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
|
2023-01-11 14:50:10 +01:00
|
|
|
if ((size_t) (end - p) < 16) {
|
|
|
|
return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
|
|
|
|
}
|
|
|
|
|
2023-05-23 18:34:33 +02:00
|
|
|
ssl->in_window_top = MBEDTLS_GET_UINT64_BE(p, 0);
|
2022-02-17 07:23:47 +01:00
|
|
|
p += 8;
|
2017-09-21 13:49:50 +02:00
|
|
|
|
2023-05-23 18:34:33 +02:00
|
|
|
ssl->in_window = MBEDTLS_GET_UINT64_BE(p, 0);
|
2022-02-17 07:23:47 +01:00
|
|
|
p += 8;
|
|
|
|
#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
|
2020-10-20 16:20:23 +02:00
|
|
|
|
2022-02-17 07:23:47 +01:00
|
|
|
#if defined(MBEDTLS_SSL_PROTO_DTLS)
|
2023-01-11 14:50:10 +01:00
|
|
|
if ((size_t) (end - p) < 1) {
|
|
|
|
return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
|
|
|
|
}
|
2022-02-17 07:23:47 +01:00
|
|
|
|
|
|
|
ssl->disable_datagram_packing = *p++;
|
|
|
|
#endif /* MBEDTLS_SSL_PROTO_DTLS */
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if ((size_t) (end - p) < sizeof(ssl->cur_out_ctr)) {
|
|
|
|
return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
|
|
|
|
}
|
|
|
|
memcpy(ssl->cur_out_ctr, p, sizeof(ssl->cur_out_ctr));
|
|
|
|
p += sizeof(ssl->cur_out_ctr);
|
2017-09-21 13:49:50 +02:00
|
|
|
|
2020-10-20 16:20:23 +02:00
|
|
|
#if defined(MBEDTLS_SSL_PROTO_DTLS)
|
2023-01-11 14:50:10 +01:00
|
|
|
if ((size_t) (end - p) < 2) {
|
|
|
|
return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
|
|
|
|
}
|
2020-10-20 16:20:23 +02:00
|
|
|
|
2023-11-04 00:34:02 +01:00
|
|
|
ssl->mtu = MBEDTLS_GET_UINT16_BE(p, 0);
|
2022-02-17 07:23:47 +01:00
|
|
|
p += 2;
|
|
|
|
#endif /* MBEDTLS_SSL_PROTO_DTLS */
|
2011-06-08 15:10:54 +02:00
|
|
|
|
2022-02-17 07:23:47 +01:00
|
|
|
#if defined(MBEDTLS_SSL_ALPN)
|
2011-03-14 21:50:15 +01:00
|
|
|
{
|
2022-02-17 07:23:47 +01:00
|
|
|
uint8_t alpn_len;
|
|
|
|
const char **cur;
|
2011-06-08 15:10:54 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if ((size_t) (end - p) < 1) {
|
|
|
|
return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
|
|
|
|
}
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2022-02-17 07:23:47 +01:00
|
|
|
alpn_len = *p++;
|
2020-02-05 15:44:10 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (alpn_len != 0 && ssl->conf->alpn_list != NULL) {
|
2022-02-17 07:23:47 +01:00
|
|
|
/* alpn_chosen should point to an item in the configured list */
|
2023-01-11 14:50:10 +01:00
|
|
|
for (cur = ssl->conf->alpn_list; *cur != NULL; cur++) {
|
|
|
|
if (strlen(*cur) == alpn_len &&
|
|
|
|
memcmp(p, cur, alpn_len) == 0) {
|
2022-02-17 07:23:47 +01:00
|
|
|
ssl->alpn_chosen = *cur;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2022-02-17 07:23:47 +01:00
|
|
|
/* can only happen on conf mismatch */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (alpn_len != 0 && ssl->alpn_chosen == NULL) {
|
|
|
|
return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
|
|
|
|
}
|
2015-01-07 12:39:44 +01:00
|
|
|
|
2022-02-17 07:23:47 +01:00
|
|
|
p += alpn_len;
|
|
|
|
}
|
|
|
|
#endif /* MBEDTLS_SSL_ALPN */
|
2020-02-05 15:44:10 +01:00
|
|
|
|
2022-02-17 07:23:47 +01: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;
|
2022-03-15 00:04:24 +01:00
|
|
|
ssl->tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
|
2022-02-17 07:23:47 +01:00
|
|
|
|
|
|
|
/* Adjust pointers for header fields of outgoing records to
|
|
|
|
* the given transform, accounting for explicit IV and CID. */
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_ssl_update_out_pointers(ssl, ssl->transform);
|
2022-02-17 07:23:47 +01: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
|
|
|
|
* by calling mbedtls_ssl_handshake_wrapup_free_hs_transform()
|
|
|
|
* inappropriately. */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->handshake != NULL) {
|
|
|
|
mbedtls_ssl_handshake_free(ssl);
|
|
|
|
mbedtls_free(ssl->handshake);
|
2022-02-17 07:23:47 +01:00
|
|
|
ssl->handshake = NULL;
|
2015-01-07 12:39:44 +01:00
|
|
|
}
|
|
|
|
|
2022-02-17 07:23:47 +01:00
|
|
|
/*
|
|
|
|
* Done - should have consumed entire buffer
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
if (p != end) {
|
|
|
|
return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
|
|
|
|
}
|
2015-01-07 12:39:44 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2015-01-07 12:39:44 +01:00
|
|
|
}
|
|
|
|
|
2015-04-15 19:09:03 +02:00
|
|
|
/*
|
2022-02-17 07:23:47 +01:00
|
|
|
* Deserialize context: public wrapper for error cleaning
|
2015-04-15 19:09:03 +02:00
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_ssl_context_load(mbedtls_ssl_context *context,
|
|
|
|
const unsigned char *buf,
|
|
|
|
size_t len)
|
2015-04-15 19:09:03 +02:00
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
int ret = ssl_context_load(context, buf, len);
|
2015-04-15 19:09:03 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ret != 0) {
|
|
|
|
mbedtls_ssl_free(context);
|
|
|
|
}
|
2015-04-15 19:09:03 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return ret;
|
2015-04-15 19:09:03 +02:00
|
|
|
}
|
2022-02-17 07:23:47 +01:00
|
|
|
#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
|
2015-04-15 19:09:03 +02:00
|
|
|
|
2009-01-03 22:22:43 +01:00
|
|
|
/*
|
2022-02-17 07:23:47 +01:00
|
|
|
* Free an SSL context
|
2009-01-03 22:22:43 +01:00
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
void mbedtls_ssl_free(mbedtls_ssl_context *ssl)
|
2009-01-03 22:22:43 +01:00
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl == NULL) {
|
2022-02-17 07:23:47 +01:00
|
|
|
return;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(2, ("=> free"));
|
2015-09-01 17:43:40 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->out_buf != NULL) {
|
2022-02-17 07:23:47 +01: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
|
2020-02-05 15:44:10 +01:00
|
|
|
|
2023-07-17 16:17:40 +02:00
|
|
|
mbedtls_zeroize_and_free(ssl->out_buf, out_buf_len);
|
2022-02-17 07:23:47 +01:00
|
|
|
ssl->out_buf = NULL;
|
2020-02-05 15:44:10 +01:00
|
|
|
}
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->in_buf != NULL) {
|
2022-02-17 07:23:47 +01: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
|
2020-02-05 15:44:10 +01:00
|
|
|
|
2023-07-17 16:17:40 +02:00
|
|
|
mbedtls_zeroize_and_free(ssl->in_buf, in_buf_len);
|
2022-02-17 07:23:47 +01:00
|
|
|
ssl->in_buf = NULL;
|
2020-02-05 15:44:10 +01:00
|
|
|
}
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->transform) {
|
|
|
|
mbedtls_ssl_transform_free(ssl->transform);
|
|
|
|
mbedtls_free(ssl->transform);
|
2013-09-23 20:04:20 +02:00
|
|
|
}
|
2018-04-25 20:32:43 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->handshake) {
|
|
|
|
mbedtls_ssl_handshake_free(ssl);
|
|
|
|
mbedtls_free(ssl->handshake);
|
2022-12-01 11:57:19 +01:00
|
|
|
|
|
|
|
#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_ssl_transform_free(ssl->transform_negotiate);
|
|
|
|
mbedtls_free(ssl->transform_negotiate);
|
2022-12-01 11:57:19 +01:00
|
|
|
#endif
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_ssl_session_free(ssl->session_negotiate);
|
|
|
|
mbedtls_free(ssl->session_negotiate);
|
2022-02-17 07:23:47 +01:00
|
|
|
}
|
2021-08-17 10:53:13 +02:00
|
|
|
|
2022-02-08 10:59:23 +01:00
|
|
|
#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_ssl_transform_free(ssl->transform_application);
|
|
|
|
mbedtls_free(ssl->transform_application);
|
2022-02-17 07:23:47 +01:00
|
|
|
#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->session) {
|
|
|
|
mbedtls_ssl_session_free(ssl->session);
|
|
|
|
mbedtls_free(ssl->session);
|
2022-02-08 10:59:23 +01:00
|
|
|
}
|
2022-01-12 08:49:48 +01:00
|
|
|
|
2022-02-17 07:23:47 +01:00
|
|
|
#if defined(MBEDTLS_X509_CRT_PARSE_C)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->hostname != NULL) {
|
2023-07-17 16:17:40 +02:00
|
|
|
mbedtls_zeroize_and_free(ssl->hostname, strlen(ssl->hostname));
|
2018-04-25 20:39:48 +02:00
|
|
|
}
|
2022-02-17 07:23:47 +01:00
|
|
|
#endif
|
2018-04-25 20:39:48 +02:00
|
|
|
|
2022-02-17 07:23:47 +01:00
|
|
|
#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_free(ssl->cli_id);
|
2015-07-06 14:18:56 +02:00
|
|
|
#endif
|
2022-02-17 07:23:47 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(2, ("<= free"));
|
2022-02-17 07:23:47 +01:00
|
|
|
|
|
|
|
/* Actually clear after last debug message */
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_platform_zeroize(ssl, sizeof(mbedtls_ssl_context));
|
2022-02-17 07:23:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2021-12-21 06:14:10 +01:00
|
|
|
* Initialize mbedtls_ssl_config
|
2022-02-17 07:23:47 +01:00
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
void mbedtls_ssl_config_init(mbedtls_ssl_config *conf)
|
2022-02-17 07:23:47 +01:00
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
memset(conf, 0, sizeof(mbedtls_ssl_config));
|
2022-02-17 07:23:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* The selection should be the same as mbedtls_x509_crt_profile_default in
|
|
|
|
* x509_crt.c, plus Montgomery curves for ECDHE. Here, the order matters:
|
|
|
|
* curves with a lower resource usage come first.
|
|
|
|
* See the documentation of mbedtls_ssl_conf_curves() for what we promise
|
|
|
|
* about this list.
|
|
|
|
*/
|
2023-12-28 10:07:43 +01:00
|
|
|
static const uint16_t ssl_preset_default_groups[] = {
|
2023-09-01 09:20:51 +02:00
|
|
|
#if defined(MBEDTLS_ECP_HAVE_CURVE25519)
|
2022-02-17 07:23:47 +01:00
|
|
|
MBEDTLS_SSL_IANA_TLS_GROUP_X25519,
|
2012-09-16 21:57:18 +02:00
|
|
|
#endif
|
2023-09-01 09:20:51 +02:00
|
|
|
#if defined(MBEDTLS_ECP_HAVE_SECP256R1)
|
2022-02-17 07:23:47 +01:00
|
|
|
MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1,
|
2013-07-04 11:51:43 +02:00
|
|
|
#endif
|
2023-09-01 09:20:51 +02:00
|
|
|
#if defined(MBEDTLS_ECP_HAVE_SECP384R1)
|
2022-02-17 07:23:47 +01:00
|
|
|
MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1,
|
2015-09-17 13:59:49 +02:00
|
|
|
#endif
|
2023-09-01 09:20:51 +02:00
|
|
|
#if defined(MBEDTLS_ECP_HAVE_CURVE448)
|
2022-02-17 07:23:47 +01:00
|
|
|
MBEDTLS_SSL_IANA_TLS_GROUP_X448,
|
2015-09-15 12:10:54 +02:00
|
|
|
#endif
|
2023-09-01 09:20:51 +02:00
|
|
|
#if defined(MBEDTLS_ECP_HAVE_SECP521R1)
|
2022-02-17 07:23:47 +01:00
|
|
|
MBEDTLS_SSL_IANA_TLS_GROUP_SECP521R1,
|
2013-09-23 19:11:32 +02:00
|
|
|
#endif
|
2023-09-01 09:20:51 +02:00
|
|
|
#if defined(MBEDTLS_ECP_HAVE_BP256R1)
|
2022-02-17 07:23:47 +01:00
|
|
|
MBEDTLS_SSL_IANA_TLS_GROUP_BP256R1,
|
2015-05-07 16:59:54 +02:00
|
|
|
#endif
|
2023-09-01 09:20:51 +02:00
|
|
|
#if defined(MBEDTLS_ECP_HAVE_BP384R1)
|
2022-02-17 07:23:47 +01:00
|
|
|
MBEDTLS_SSL_IANA_TLS_GROUP_BP384R1,
|
2017-05-17 11:56:15 +02:00
|
|
|
#endif
|
2023-09-01 09:20:51 +02:00
|
|
|
#if defined(MBEDTLS_ECP_HAVE_BP512R1)
|
2022-02-17 07:23:47 +01:00
|
|
|
MBEDTLS_SSL_IANA_TLS_GROUP_BP512R1,
|
2022-12-12 14:48:57 +01:00
|
|
|
#endif
|
2023-05-31 15:25:11 +02:00
|
|
|
#if defined(PSA_WANT_ALG_FFDH)
|
2022-12-12 14:48:57 +01:00
|
|
|
MBEDTLS_SSL_IANA_TLS_GROUP_FFDHE2048,
|
|
|
|
MBEDTLS_SSL_IANA_TLS_GROUP_FFDHE3072,
|
|
|
|
MBEDTLS_SSL_IANA_TLS_GROUP_FFDHE4096,
|
|
|
|
MBEDTLS_SSL_IANA_TLS_GROUP_FFDHE6144,
|
|
|
|
MBEDTLS_SSL_IANA_TLS_GROUP_FFDHE8192,
|
2022-02-17 07:23:47 +01:00
|
|
|
#endif
|
|
|
|
MBEDTLS_SSL_IANA_TLS_GROUP_NONE
|
|
|
|
};
|
2017-05-17 11:56:15 +02:00
|
|
|
|
2022-06-07 09:21:05 +02:00
|
|
|
static const int ssl_preset_suiteb_ciphersuites[] = {
|
2022-02-17 07:23:47 +01:00
|
|
|
MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
|
|
|
|
MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
|
|
|
|
0
|
|
|
|
};
|
2019-02-06 17:18:31 +01:00
|
|
|
|
2022-10-05 12:46:29 +02:00
|
|
|
#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
|
2022-01-27 10:00:11 +01:00
|
|
|
|
2022-02-17 07:23:47 +01:00
|
|
|
/* NOTICE:
|
|
|
|
* For ssl_preset_*_sig_algs and ssl_tls12_preset_*_sig_algs, the following
|
|
|
|
* rules SHOULD be upheld.
|
|
|
|
* - No duplicate entries.
|
|
|
|
* - But if there is a good reason, do not change the order of the algorithms.
|
2022-07-28 08:22:17 +02:00
|
|
|
* - ssl_tls12_preset* is for TLS 1.2 use only.
|
2022-02-17 07:23:47 +01:00
|
|
|
* - ssl_preset_* is for TLS 1.3 only or hybrid TLS 1.3/1.2 handshakes.
|
|
|
|
*/
|
2023-12-28 10:07:43 +01:00
|
|
|
static const uint16_t ssl_preset_default_sig_algs[] = {
|
2014-07-11 02:43:49 +02:00
|
|
|
|
2023-09-18 11:19:20 +02:00
|
|
|
#if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ANY_ALLOWED_ENABLED) && \
|
2023-03-17 12:50:01 +01:00
|
|
|
defined(MBEDTLS_MD_CAN_SHA256) && \
|
2023-09-01 09:03:41 +02:00
|
|
|
defined(PSA_WANT_ECC_SECP_R1_256)
|
2022-02-17 07:23:47 +01:00
|
|
|
MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256,
|
2023-09-18 11:19:20 +02:00
|
|
|
// == MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA256)
|
|
|
|
#endif
|
2019-01-08 12:39:35 +01:00
|
|
|
|
2023-09-18 11:19:20 +02:00
|
|
|
#if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ANY_ALLOWED_ENABLED) && \
|
2023-03-17 12:50:01 +01:00
|
|
|
defined(MBEDTLS_MD_CAN_SHA384) && \
|
2023-09-01 09:03:41 +02:00
|
|
|
defined(PSA_WANT_ECC_SECP_R1_384)
|
2022-02-17 07:23:47 +01:00
|
|
|
MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384,
|
2023-09-18 11:19:20 +02:00
|
|
|
// == MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA384)
|
|
|
|
#endif
|
2021-10-30 05:54:10 +02:00
|
|
|
|
2023-09-18 11:19:20 +02:00
|
|
|
#if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ANY_ALLOWED_ENABLED) && \
|
2023-03-17 12:50:01 +01:00
|
|
|
defined(MBEDTLS_MD_CAN_SHA512) && \
|
2023-09-01 09:03:41 +02:00
|
|
|
defined(PSA_WANT_ECC_SECP_R1_521)
|
2022-02-17 07:23:47 +01:00
|
|
|
MBEDTLS_TLS1_3_SIG_ECDSA_SECP521R1_SHA512,
|
2023-09-18 11:19:20 +02:00
|
|
|
// == MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA512)
|
|
|
|
#endif
|
2020-03-03 16:39:58 +01:00
|
|
|
|
2023-11-22 09:54:31 +01:00
|
|
|
#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) && defined(MBEDTLS_MD_CAN_SHA512)
|
2022-06-23 04:16:33 +02:00
|
|
|
MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512,
|
2023-11-22 09:54:31 +01:00
|
|
|
#endif
|
2022-06-23 04:16:33 +02:00
|
|
|
|
2023-11-22 09:54:31 +01:00
|
|
|
#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) && defined(MBEDTLS_MD_CAN_SHA384)
|
2022-06-23 04:16:33 +02:00
|
|
|
MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384,
|
2023-11-22 09:54:31 +01:00
|
|
|
#endif
|
2022-06-23 04:16:33 +02:00
|
|
|
|
2023-11-22 09:54:31 +01:00
|
|
|
#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) && defined(MBEDTLS_MD_CAN_SHA256)
|
2022-06-23 04:16:33 +02:00
|
|
|
MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
|
2023-11-22 09:54:31 +01:00
|
|
|
#endif
|
2022-06-23 04:16:33 +02:00
|
|
|
|
2023-03-17 12:50:01 +01:00
|
|
|
#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_MD_CAN_SHA512)
|
2022-06-23 08:02:28 +02:00
|
|
|
MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA512,
|
2023-03-17 12:50:01 +01:00
|
|
|
#endif /* MBEDTLS_RSA_C && MBEDTLS_MD_CAN_SHA512 */
|
2022-06-23 08:02:28 +02:00
|
|
|
|
2023-03-17 12:50:01 +01:00
|
|
|
#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_MD_CAN_SHA384)
|
2022-06-23 08:02:28 +02:00
|
|
|
MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA384,
|
2023-03-17 12:50:01 +01:00
|
|
|
#endif /* MBEDTLS_RSA_C && MBEDTLS_MD_CAN_SHA384 */
|
2022-06-23 08:02:28 +02:00
|
|
|
|
2023-03-17 12:50:01 +01:00
|
|
|
#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_MD_CAN_SHA256)
|
2022-06-23 08:02:28 +02:00
|
|
|
MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256,
|
2023-03-17 12:50:01 +01:00
|
|
|
#endif /* MBEDTLS_RSA_C && MBEDTLS_MD_CAN_SHA256 */
|
2022-06-23 08:02:28 +02:00
|
|
|
|
2022-05-09 16:37:58 +02:00
|
|
|
MBEDTLS_TLS_SIG_NONE
|
2022-02-17 07:23:47 +01:00
|
|
|
};
|
2014-06-26 13:37:14 +02:00
|
|
|
|
2022-02-17 07:23:47 +01:00
|
|
|
/* NOTICE: see above */
|
|
|
|
#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
|
|
|
|
static uint16_t ssl_tls12_preset_default_sig_algs[] = {
|
2023-11-22 09:54:31 +01:00
|
|
|
|
2023-03-17 12:50:01 +01:00
|
|
|
#if defined(MBEDTLS_MD_CAN_SHA512)
|
2023-08-02 20:02:28 +02:00
|
|
|
#if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ALLOWED_ENABLED)
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA512),
|
2022-05-10 13:13:58 +02:00
|
|
|
#endif
|
2022-07-28 08:22:17 +02:00
|
|
|
#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
|
|
|
|
MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512,
|
2023-11-22 09:54:31 +01:00
|
|
|
#endif
|
2022-05-10 13:13:58 +02:00
|
|
|
#if defined(MBEDTLS_RSA_C)
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA512),
|
2013-04-18 22:46:23 +02:00
|
|
|
#endif
|
2023-11-22 09:54:31 +01:00
|
|
|
#endif /* MBEDTLS_MD_CAN_SHA512 */
|
|
|
|
|
2023-03-17 12:50:01 +01:00
|
|
|
#if defined(MBEDTLS_MD_CAN_SHA384)
|
2023-08-02 20:02:28 +02:00
|
|
|
#if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ALLOWED_ENABLED)
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA384),
|
2022-05-10 13:13:58 +02:00
|
|
|
#endif
|
2022-07-28 08:22:17 +02:00
|
|
|
#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
|
|
|
|
MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384,
|
2023-11-22 09:54:31 +01:00
|
|
|
#endif
|
2022-05-10 13:13:58 +02:00
|
|
|
#if defined(MBEDTLS_RSA_C)
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA384),
|
2013-08-14 13:48:06 +02:00
|
|
|
#endif
|
2023-11-22 09:54:31 +01:00
|
|
|
#endif /* MBEDTLS_MD_CAN_SHA384 */
|
|
|
|
|
2023-03-17 12:50:01 +01:00
|
|
|
#if defined(MBEDTLS_MD_CAN_SHA256)
|
2023-08-02 20:02:28 +02:00
|
|
|
#if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ALLOWED_ENABLED)
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA256),
|
2022-02-17 07:23:47 +01:00
|
|
|
#endif
|
2022-07-28 08:22:17 +02:00
|
|
|
#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
|
|
|
|
MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
|
2023-11-22 09:54:31 +01:00
|
|
|
#endif
|
2022-05-10 13:13:58 +02:00
|
|
|
#if defined(MBEDTLS_RSA_C)
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA256),
|
2022-02-17 07:23:47 +01:00
|
|
|
#endif
|
2023-11-22 09:54:31 +01:00
|
|
|
#endif /* MBEDTLS_MD_CAN_SHA256 */
|
|
|
|
|
2022-05-09 16:37:58 +02:00
|
|
|
MBEDTLS_TLS_SIG_NONE
|
2022-02-17 07:23:47 +01:00
|
|
|
};
|
|
|
|
#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
|
2023-11-22 09:54:31 +01:00
|
|
|
|
2022-02-17 07:23:47 +01:00
|
|
|
/* NOTICE: see above */
|
2023-12-28 10:07:43 +01:00
|
|
|
static const uint16_t ssl_preset_suiteb_sig_algs[] = {
|
2013-08-02 14:44:04 +02:00
|
|
|
|
2023-09-18 11:19:20 +02:00
|
|
|
#if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ANY_ALLOWED_ENABLED) && \
|
2023-07-13 17:23:20 +02:00
|
|
|
defined(MBEDTLS_MD_CAN_SHA256) && \
|
2023-09-01 09:20:51 +02:00
|
|
|
defined(MBEDTLS_ECP_HAVE_SECP256R1)
|
2022-02-17 07:23:47 +01:00
|
|
|
MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256,
|
2023-09-18 11:19:20 +02:00
|
|
|
// == MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA256)
|
|
|
|
#endif
|
2019-07-23 16:31:16 +02:00
|
|
|
|
2023-09-18 11:19:20 +02:00
|
|
|
#if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ANY_ALLOWED_ENABLED) && \
|
2023-07-13 17:23:20 +02:00
|
|
|
defined(MBEDTLS_MD_CAN_SHA384) && \
|
2023-09-01 09:20:51 +02:00
|
|
|
defined(MBEDTLS_ECP_HAVE_SECP384R1)
|
2022-02-17 07:23:47 +01:00
|
|
|
MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384,
|
2023-09-18 11:19:20 +02:00
|
|
|
// == MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA384)
|
|
|
|
#endif
|
2019-07-23 16:31:16 +02:00
|
|
|
|
2022-05-09 16:37:58 +02:00
|
|
|
MBEDTLS_TLS_SIG_NONE
|
2022-02-17 07:23:47 +01:00
|
|
|
};
|
2019-07-23 16:31:16 +02:00
|
|
|
|
2022-02-17 07:23:47 +01:00
|
|
|
/* NOTICE: see above */
|
|
|
|
#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
|
|
|
|
static uint16_t ssl_tls12_preset_suiteb_sig_algs[] = {
|
2023-11-22 09:54:31 +01:00
|
|
|
|
2023-03-17 12:50:01 +01:00
|
|
|
#if defined(MBEDTLS_MD_CAN_SHA256)
|
2023-08-02 20:02:28 +02:00
|
|
|
#if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ALLOWED_ENABLED)
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA256),
|
2022-02-17 07:23:47 +01:00
|
|
|
#endif
|
2023-11-22 09:54:31 +01:00
|
|
|
#endif /* MBEDTLS_MD_CAN_SHA256 */
|
2023-11-22 09:32:39 +01:00
|
|
|
|
2023-03-17 12:50:01 +01:00
|
|
|
#if defined(MBEDTLS_MD_CAN_SHA384)
|
2023-08-02 20:02:28 +02:00
|
|
|
#if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ALLOWED_ENABLED)
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA384),
|
2022-05-10 13:13:58 +02:00
|
|
|
#endif
|
2023-11-22 09:54:31 +01:00
|
|
|
#endif /* MBEDTLS_MD_CAN_SHA384 */
|
|
|
|
|
2022-05-09 16:37:58 +02:00
|
|
|
MBEDTLS_TLS_SIG_NONE
|
2022-02-17 07:23:47 +01:00
|
|
|
};
|
|
|
|
#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
|
2019-07-23 16:31:16 +02:00
|
|
|
|
2022-10-05 12:46:29 +02:00
|
|
|
#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
|
2019-07-23 16:31:16 +02:00
|
|
|
|
2023-12-28 10:07:43 +01:00
|
|
|
static const uint16_t ssl_preset_suiteb_groups[] = {
|
2023-09-01 09:20:51 +02:00
|
|
|
#if defined(MBEDTLS_ECP_HAVE_SECP256R1)
|
2022-02-17 07:23:47 +01:00
|
|
|
MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1,
|
|
|
|
#endif
|
2023-09-01 09:20:51 +02:00
|
|
|
#if defined(MBEDTLS_ECP_HAVE_SECP384R1)
|
2022-02-17 07:23:47 +01:00
|
|
|
MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1,
|
|
|
|
#endif
|
|
|
|
MBEDTLS_SSL_IANA_TLS_GROUP_NONE
|
2019-07-11 10:58:10 +02:00
|
|
|
};
|
|
|
|
|
2022-10-05 12:46:29 +02:00
|
|
|
#if defined(MBEDTLS_DEBUG_C) && defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
|
2022-02-17 07:23:47 +01:00
|
|
|
/* Function for checking `ssl_preset_*_sig_algs` and `ssl_tls12_preset_*_sig_algs`
|
|
|
|
* to make sure there are no duplicated signature algorithm entries. */
|
2022-06-17 10:52:54 +02:00
|
|
|
MBEDTLS_CHECK_RETURN_CRITICAL
|
2023-12-28 10:07:43 +01:00
|
|
|
static int ssl_check_no_sig_alg_duplication(const uint16_t *sig_algs)
|
2019-05-28 13:02:16 +02:00
|
|
|
{
|
2022-02-17 07:23:47 +01:00
|
|
|
size_t i, j;
|
|
|
|
int ret = 0;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
for (i = 0; sig_algs[i] != MBEDTLS_TLS_SIG_NONE; i++) {
|
|
|
|
for (j = 0; j < i; j++) {
|
|
|
|
if (sig_algs[i] != sig_algs[j]) {
|
2022-02-17 07:23:47 +01:00
|
|
|
continue;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
|
|
|
mbedtls_printf(" entry(%04x,%" MBEDTLS_PRINTF_SIZET
|
|
|
|
") is duplicated at %" MBEDTLS_PRINTF_SIZET "\n",
|
|
|
|
sig_algs[i], j, i);
|
2022-02-17 07:23:47 +01:00
|
|
|
ret = -1;
|
|
|
|
}
|
2019-08-21 12:45:05 +02:00
|
|
|
}
|
2023-01-11 14:50:10 +01:00
|
|
|
return ret;
|
2022-02-17 07:23:47 +01:00
|
|
|
}
|
|
|
|
|
2022-10-05 12:46:29 +02:00
|
|
|
#endif /* MBEDTLS_DEBUG_C && MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
|
2022-02-17 07:23:47 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Load default in mbedtls_ssl_config
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_ssl_config_defaults(mbedtls_ssl_config *conf,
|
|
|
|
int endpoint, int transport, int preset)
|
2022-02-17 07:23:47 +01:00
|
|
|
{
|
|
|
|
#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
|
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
|
|
|
#endif
|
|
|
|
|
2022-10-05 12:46:29 +02:00
|
|
|
#if defined(MBEDTLS_DEBUG_C) && defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl_check_no_sig_alg_duplication(ssl_preset_suiteb_sig_algs)) {
|
|
|
|
mbedtls_printf("ssl_preset_suiteb_sig_algs has duplicated entries\n");
|
|
|
|
return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2019-08-21 12:45:05 +02:00
|
|
|
}
|
2022-02-17 07:23:47 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl_check_no_sig_alg_duplication(ssl_preset_default_sig_algs)) {
|
|
|
|
mbedtls_printf("ssl_preset_default_sig_algs has duplicated entries\n");
|
|
|
|
return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2019-08-21 12:45:05 +02:00
|
|
|
}
|
2022-02-17 07:23:47 +01:00
|
|
|
|
|
|
|
#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl_check_no_sig_alg_duplication(ssl_tls12_preset_suiteb_sig_algs)) {
|
|
|
|
mbedtls_printf("ssl_tls12_preset_suiteb_sig_algs has duplicated entries\n");
|
|
|
|
return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2019-08-21 12:45:05 +02:00
|
|
|
}
|
2022-02-17 07:23:47 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl_check_no_sig_alg_duplication(ssl_tls12_preset_default_sig_algs)) {
|
|
|
|
mbedtls_printf("ssl_tls12_preset_default_sig_algs has duplicated entries\n");
|
|
|
|
return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2019-08-21 12:45:05 +02:00
|
|
|
}
|
2022-02-17 07:23:47 +01:00
|
|
|
#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
|
2022-10-05 12:46:29 +02:00
|
|
|
#endif /* MBEDTLS_DEBUG_C && MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
|
2022-02-17 07:23:47 +01:00
|
|
|
|
|
|
|
/* Use the functions here so that they are covered in tests,
|
|
|
|
* but otherwise access member directly for efficiency */
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_ssl_conf_endpoint(conf, endpoint);
|
|
|
|
mbedtls_ssl_conf_transport(conf, transport);
|
2022-02-17 07:23:47 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Things that are common to all presets
|
|
|
|
*/
|
|
|
|
#if defined(MBEDTLS_SSL_CLI_C)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (endpoint == MBEDTLS_SSL_IS_CLIENT) {
|
2022-02-17 07:23:47 +01:00
|
|
|
conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
|
|
|
|
#if defined(MBEDTLS_SSL_SESSION_TICKETS)
|
|
|
|
conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
|
|
|
|
#endif
|
2019-08-21 12:45:05 +02:00
|
|
|
}
|
2022-02-17 07:23:47 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#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
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
|
|
|
|
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
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_SSL_SRV_C)
|
|
|
|
conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
|
|
|
|
conf->respect_cli_pref = MBEDTLS_SSL_SRV_CIPHERSUITE_ORDER_SERVER;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#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
|
|
|
|
|
2019-07-10 14:14:05 +02:00
|
|
|
#if defined(MBEDTLS_SSL_RENEGOTIATION)
|
2022-02-17 07:23:47 +01:00
|
|
|
conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
|
2023-01-11 14:50:10 +01:00
|
|
|
memset(conf->renego_period, 0x00, 2);
|
|
|
|
memset(conf->renego_period + 2, 0xFF, 6);
|
2022-02-17 07:23:47 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (endpoint == MBEDTLS_SSL_IS_SERVER) {
|
2022-02-17 07:23:47 +01: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;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if ((ret = mbedtls_ssl_conf_dh_param_bin(conf,
|
|
|
|
dhm_p, sizeof(dhm_p),
|
|
|
|
dhm_g, sizeof(dhm_g))) != 0) {
|
|
|
|
return ret;
|
2022-02-17 07:23:47 +01:00
|
|
|
}
|
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
|
|
|
|
2022-02-17 07:23:47 +01:00
|
|
|
#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
|
2022-12-06 10:47:22 +01:00
|
|
|
|
|
|
|
#if defined(MBEDTLS_SSL_EARLY_DATA)
|
2023-11-07 04:40:43 +01:00
|
|
|
mbedtls_ssl_conf_early_data(conf, MBEDTLS_SSL_EARLY_DATA_DISABLED);
|
2022-12-06 10:47:22 +01:00
|
|
|
#if defined(MBEDTLS_SSL_SRV_C)
|
2023-11-07 04:47:36 +01:00
|
|
|
mbedtls_ssl_conf_max_early_data_size(conf, MBEDTLS_SSL_MAX_EARLY_DATA_SIZE);
|
2022-12-06 10:47:22 +01:00
|
|
|
#endif
|
|
|
|
#endif /* MBEDTLS_SSL_EARLY_DATA */
|
|
|
|
|
2022-09-22 04:46:57 +02:00
|
|
|
#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SESSION_TICKETS)
|
2022-08-09 07:28:39 +02:00
|
|
|
mbedtls_ssl_conf_new_session_tickets(
|
2023-01-11 14:50:10 +01:00
|
|
|
conf, MBEDTLS_SSL_TLS1_3_DEFAULT_NEW_SESSION_TICKETS);
|
2022-08-09 07:28:39 +02:00
|
|
|
#endif
|
2019-07-11 10:58:10 +02:00
|
|
|
/*
|
2022-02-17 07:23:47 +01:00
|
|
|
* Allow all TLS 1.3 key exchange modes by default.
|
2019-07-11 10:58:10 +02:00
|
|
|
*/
|
2022-02-17 07:23:47 +01:00
|
|
|
conf->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_ALL;
|
|
|
|
#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
|
2019-05-28 13:02:16 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
|
2022-04-21 15:46:17 +02:00
|
|
|
#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
|
|
|
|
conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
|
2022-04-21 11:24:56 +02:00
|
|
|
conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
|
2022-04-21 15:46:17 +02:00
|
|
|
#else
|
2023-01-11 14:50:10 +01:00
|
|
|
return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
|
2022-04-21 15:46:17 +02:00
|
|
|
#endif
|
2023-01-11 14:50:10 +01:00
|
|
|
} else {
|
2022-04-21 15:46:17 +02:00
|
|
|
#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
|
2023-03-08 16:18:00 +01:00
|
|
|
conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
|
|
|
|
conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
|
2022-03-14 22:26:42 +01:00
|
|
|
#elif defined(MBEDTLS_SSL_PROTO_TLS1_3)
|
2022-04-21 15:46:17 +02:00
|
|
|
conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
|
2022-03-14 22:26:42 +01:00
|
|
|
conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
|
2022-04-21 15:46:17 +02:00
|
|
|
#elif defined(MBEDTLS_SSL_PROTO_TLS1_2)
|
|
|
|
conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
|
|
|
|
conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
|
|
|
|
#else
|
2023-01-11 14:50:10 +01:00
|
|
|
return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
|
2022-04-21 11:24:56 +02:00
|
|
|
#endif
|
2022-04-21 15:46:17 +02:00
|
|
|
}
|
2022-03-14 22:26:42 +01:00
|
|
|
|
2019-07-11 12:50:53 +02:00
|
|
|
/*
|
2022-02-17 07:23:47 +01:00
|
|
|
* Preset-specific defaults
|
2019-07-11 12:50:53 +02:00
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
switch (preset) {
|
2022-02-17 07:23:47 +01:00
|
|
|
/*
|
|
|
|
* NSA Suite B
|
|
|
|
*/
|
|
|
|
case MBEDTLS_SSL_PRESET_SUITEB:
|
2019-07-11 12:50:53 +02:00
|
|
|
|
2022-02-17 07:23:47 +01:00
|
|
|
conf->ciphersuite_list = ssl_preset_suiteb_ciphersuites;
|
2019-07-11 12:50:53 +02:00
|
|
|
|
2022-02-17 07:23:47 +01:00
|
|
|
#if defined(MBEDTLS_X509_CRT_PARSE_C)
|
|
|
|
conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
|
|
|
|
#endif
|
2019-07-11 12:50:53 +02:00
|
|
|
|
2022-10-05 12:46:29 +02:00
|
|
|
#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
|
2022-02-17 07:23:47 +01:00
|
|
|
#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (mbedtls_ssl_conf_is_tls12_only(conf)) {
|
2022-02-17 07:23:47 +01:00
|
|
|
conf->sig_algs = ssl_tls12_preset_suiteb_sig_algs;
|
2023-01-11 14:50:10 +01:00
|
|
|
} else
|
2022-02-17 07:23:47 +01:00
|
|
|
#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
|
2023-01-11 14:50:10 +01:00
|
|
|
conf->sig_algs = ssl_preset_suiteb_sig_algs;
|
2022-10-05 12:46:29 +02:00
|
|
|
#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
|
2022-02-17 07:23:47 +01:00
|
|
|
|
2023-09-22 11:53:41 +02:00
|
|
|
#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
|
2022-02-17 07:23:47 +01:00
|
|
|
conf->curve_list = NULL;
|
|
|
|
#endif
|
|
|
|
conf->group_list = ssl_preset_suiteb_groups;
|
|
|
|
break;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Default
|
|
|
|
*/
|
|
|
|
default:
|
2022-03-15 11:23:25 +01:00
|
|
|
|
2022-02-17 07:23:47 +01:00
|
|
|
conf->ciphersuite_list = mbedtls_ssl_list_ciphersuites();
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_X509_CRT_PARSE_C)
|
|
|
|
conf->cert_profile = &mbedtls_x509_crt_profile_default;
|
|
|
|
#endif
|
|
|
|
|
2022-10-05 12:46:29 +02:00
|
|
|
#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
|
2022-02-17 07:23:47 +01:00
|
|
|
#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (mbedtls_ssl_conf_is_tls12_only(conf)) {
|
2022-02-17 07:23:47 +01:00
|
|
|
conf->sig_algs = ssl_tls12_preset_default_sig_algs;
|
2023-01-11 14:50:10 +01:00
|
|
|
} else
|
2022-02-17 07:23:47 +01:00
|
|
|
#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
|
2023-01-11 14:50:10 +01:00
|
|
|
conf->sig_algs = ssl_preset_default_sig_algs;
|
2022-10-05 12:46:29 +02:00
|
|
|
#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
|
2022-02-17 07:23:47 +01:00
|
|
|
|
2023-09-22 11:53:41 +02:00
|
|
|
#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
|
2022-02-17 07:23:47 +01:00
|
|
|
conf->curve_list = NULL;
|
|
|
|
#endif
|
|
|
|
conf->group_list = ssl_preset_default_groups;
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
|
|
|
|
conf->dhm_min_bitlen = 1024;
|
|
|
|
#endif
|
2019-07-15 09:04:11 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2022-02-17 07:23:47 +01:00
|
|
|
}
|
2019-07-15 09:04:11 +02:00
|
|
|
|
2022-02-17 07:23:47 +01:00
|
|
|
/*
|
|
|
|
* Free mbedtls_ssl_config
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
void mbedtls_ssl_config_free(mbedtls_ssl_config *conf)
|
2022-02-17 07:23:47 +01:00
|
|
|
{
|
|
|
|
#if defined(MBEDTLS_DHM_C)
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_mpi_free(&conf->dhm_P);
|
|
|
|
mbedtls_mpi_free(&conf->dhm_G);
|
2022-02-17 07:23:47 +01:00
|
|
|
#endif
|
2019-07-15 09:04:11 +02:00
|
|
|
|
2022-10-05 14:31:43 +02:00
|
|
|
#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED)
|
2022-05-03 09:35:09 +02:00
|
|
|
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (!mbedtls_svc_key_id_is_null(conf->psk_opaque)) {
|
2022-05-03 09:35:09 +02:00
|
|
|
conf->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
|
|
|
|
}
|
2022-05-05 11:40:35 +02:00
|
|
|
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (conf->psk != NULL) {
|
2023-07-17 16:17:40 +02:00
|
|
|
mbedtls_zeroize_and_free(conf->psk, conf->psk_len);
|
2022-02-17 07:23:47 +01:00
|
|
|
conf->psk = NULL;
|
|
|
|
conf->psk_len = 0;
|
2019-07-15 11:23:03 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (conf->psk_identity != NULL) {
|
2023-07-17 16:17:40 +02:00
|
|
|
mbedtls_zeroize_and_free(conf->psk_identity, conf->psk_identity_len);
|
2022-02-17 07:23:47 +01:00
|
|
|
conf->psk_identity = NULL;
|
|
|
|
conf->psk_identity_len = 0;
|
2019-07-15 11:23:03 +02:00
|
|
|
}
|
2022-10-05 14:31:43 +02:00
|
|
|
#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED */
|
2019-07-15 11:23:03 +02:00
|
|
|
|
2022-02-17 07:23:47 +01:00
|
|
|
#if defined(MBEDTLS_X509_CRT_PARSE_C)
|
2023-01-11 14:50:10 +01:00
|
|
|
ssl_key_cert_free(conf->key_cert);
|
2022-02-17 07:23:47 +01:00
|
|
|
#endif
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_platform_zeroize(conf, sizeof(mbedtls_ssl_config));
|
2022-02-17 07:23:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_PK_C) && \
|
2023-08-02 20:02:28 +02:00
|
|
|
(defined(MBEDTLS_RSA_C) || defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ANY_ALLOWED_ENABLED))
|
2022-02-17 07:23:47 +01:00
|
|
|
/*
|
|
|
|
* Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
unsigned char mbedtls_ssl_sig_from_pk(mbedtls_pk_context *pk)
|
2022-02-17 07:23:47 +01:00
|
|
|
{
|
|
|
|
#if defined(MBEDTLS_RSA_C)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (mbedtls_pk_can_do(pk, MBEDTLS_PK_RSA)) {
|
|
|
|
return MBEDTLS_SSL_SIG_RSA;
|
|
|
|
}
|
2022-02-17 07:23:47 +01:00
|
|
|
#endif
|
2023-08-02 20:02:28 +02:00
|
|
|
#if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ANY_ALLOWED_ENABLED)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (mbedtls_pk_can_do(pk, MBEDTLS_PK_ECDSA)) {
|
|
|
|
return MBEDTLS_SSL_SIG_ECDSA;
|
|
|
|
}
|
2022-02-17 07:23:47 +01:00
|
|
|
#endif
|
2023-01-11 14:50:10 +01:00
|
|
|
return MBEDTLS_SSL_SIG_ANON;
|
2022-02-17 07:23:47 +01:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
unsigned char mbedtls_ssl_sig_from_pk_alg(mbedtls_pk_type_t type)
|
2022-02-17 07:23:47 +01:00
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
switch (type) {
|
2022-02-17 07:23:47 +01:00
|
|
|
case MBEDTLS_PK_RSA:
|
2023-01-11 14:50:10 +01:00
|
|
|
return MBEDTLS_SSL_SIG_RSA;
|
2022-02-17 07:23:47 +01:00
|
|
|
case MBEDTLS_PK_ECDSA:
|
|
|
|
case MBEDTLS_PK_ECKEY:
|
2023-01-11 14:50:10 +01:00
|
|
|
return MBEDTLS_SSL_SIG_ECDSA;
|
2022-02-17 07:23:47 +01:00
|
|
|
default:
|
2023-01-11 14:50:10 +01:00
|
|
|
return MBEDTLS_SSL_SIG_ANON;
|
2019-07-15 11:23:03 +02:00
|
|
|
}
|
2022-02-17 07:23:47 +01:00
|
|
|
}
|
2019-07-15 11:23:03 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig(unsigned char sig)
|
2022-02-17 07:23:47 +01:00
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
switch (sig) {
|
2022-02-17 07:23:47 +01:00
|
|
|
#if defined(MBEDTLS_RSA_C)
|
|
|
|
case MBEDTLS_SSL_SIG_RSA:
|
2023-01-11 14:50:10 +01:00
|
|
|
return MBEDTLS_PK_RSA;
|
2022-02-17 07:23:47 +01:00
|
|
|
#endif
|
2023-08-02 20:02:28 +02:00
|
|
|
#if defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ANY_ALLOWED_ENABLED)
|
2022-02-17 07:23:47 +01:00
|
|
|
case MBEDTLS_SSL_SIG_ECDSA:
|
2023-01-11 14:50:10 +01:00
|
|
|
return MBEDTLS_PK_ECDSA;
|
2022-02-17 07:23:47 +01:00
|
|
|
#endif
|
|
|
|
default:
|
2023-01-11 14:50:10 +01:00
|
|
|
return MBEDTLS_PK_NONE;
|
2019-07-15 11:23:03 +02:00
|
|
|
}
|
2022-02-17 07:23:47 +01:00
|
|
|
}
|
2023-08-02 20:02:28 +02:00
|
|
|
#endif /* MBEDTLS_PK_C &&
|
|
|
|
( MBEDTLS_RSA_C || MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ANY_ALLOWED_ENABLED ) */
|
2019-07-15 11:23:03 +02:00
|
|
|
|
2022-02-17 07:23:47 +01:00
|
|
|
/*
|
|
|
|
* Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash(unsigned char hash)
|
2022-02-17 07:23:47 +01:00
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
switch (hash) {
|
2023-03-17 12:50:01 +01:00
|
|
|
#if defined(MBEDTLS_MD_CAN_MD5)
|
2022-02-17 07:23:47 +01:00
|
|
|
case MBEDTLS_SSL_HASH_MD5:
|
2023-01-11 14:50:10 +01:00
|
|
|
return MBEDTLS_MD_MD5;
|
2022-02-17 07:23:47 +01:00
|
|
|
#endif
|
2023-03-17 12:50:01 +01:00
|
|
|
#if defined(MBEDTLS_MD_CAN_SHA1)
|
2022-02-17 07:23:47 +01:00
|
|
|
case MBEDTLS_SSL_HASH_SHA1:
|
2023-01-11 14:50:10 +01:00
|
|
|
return MBEDTLS_MD_SHA1;
|
2022-02-17 07:23:47 +01:00
|
|
|
#endif
|
2023-03-17 12:50:01 +01:00
|
|
|
#if defined(MBEDTLS_MD_CAN_SHA224)
|
2022-02-17 07:23:47 +01:00
|
|
|
case MBEDTLS_SSL_HASH_SHA224:
|
2023-01-11 14:50:10 +01:00
|
|
|
return MBEDTLS_MD_SHA224;
|
2022-02-17 07:23:47 +01:00
|
|
|
#endif
|
2023-03-17 12:50:01 +01:00
|
|
|
#if defined(MBEDTLS_MD_CAN_SHA256)
|
2022-02-17 07:23:47 +01:00
|
|
|
case MBEDTLS_SSL_HASH_SHA256:
|
2023-01-11 14:50:10 +01:00
|
|
|
return MBEDTLS_MD_SHA256;
|
2022-02-17 07:23:47 +01:00
|
|
|
#endif
|
2023-03-17 12:50:01 +01:00
|
|
|
#if defined(MBEDTLS_MD_CAN_SHA384)
|
2022-02-17 07:23:47 +01:00
|
|
|
case MBEDTLS_SSL_HASH_SHA384:
|
2023-01-11 14:50:10 +01:00
|
|
|
return MBEDTLS_MD_SHA384;
|
2022-02-17 07:23:47 +01:00
|
|
|
#endif
|
2023-03-17 12:50:01 +01:00
|
|
|
#if defined(MBEDTLS_MD_CAN_SHA512)
|
2022-02-17 07:23:47 +01:00
|
|
|
case MBEDTLS_SSL_HASH_SHA512:
|
2023-01-11 14:50:10 +01:00
|
|
|
return MBEDTLS_MD_SHA512;
|
2022-02-17 07:23:47 +01:00
|
|
|
#endif
|
|
|
|
default:
|
2023-01-11 14:50:10 +01:00
|
|
|
return MBEDTLS_MD_NONE;
|
2022-02-17 07:23:47 +01:00
|
|
|
}
|
2019-05-28 13:02:16 +02:00
|
|
|
}
|
|
|
|
|
2019-07-15 09:04:11 +02:00
|
|
|
/*
|
2022-02-17 07:23:47 +01:00
|
|
|
* Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
|
2019-07-15 09:04:11 +02:00
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
unsigned char mbedtls_ssl_hash_from_md_alg(int md)
|
2019-07-15 09:04:11 +02:00
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
switch (md) {
|
2023-03-17 12:50:01 +01:00
|
|
|
#if defined(MBEDTLS_MD_CAN_MD5)
|
2022-02-17 07:23:47 +01:00
|
|
|
case MBEDTLS_MD_MD5:
|
2023-01-11 14:50:10 +01:00
|
|
|
return MBEDTLS_SSL_HASH_MD5;
|
2022-02-17 07:23:47 +01:00
|
|
|
#endif
|
2023-03-17 12:50:01 +01:00
|
|
|
#if defined(MBEDTLS_MD_CAN_SHA1)
|
2022-02-17 07:23:47 +01:00
|
|
|
case MBEDTLS_MD_SHA1:
|
2023-01-11 14:50:10 +01:00
|
|
|
return MBEDTLS_SSL_HASH_SHA1;
|
2022-02-17 07:23:47 +01:00
|
|
|
#endif
|
2023-03-17 12:50:01 +01:00
|
|
|
#if defined(MBEDTLS_MD_CAN_SHA224)
|
2022-02-17 07:23:47 +01:00
|
|
|
case MBEDTLS_MD_SHA224:
|
2023-01-11 14:50:10 +01:00
|
|
|
return MBEDTLS_SSL_HASH_SHA224;
|
2022-02-17 07:23:47 +01:00
|
|
|
#endif
|
2023-03-17 12:50:01 +01:00
|
|
|
#if defined(MBEDTLS_MD_CAN_SHA256)
|
2022-02-17 07:23:47 +01:00
|
|
|
case MBEDTLS_MD_SHA256:
|
2023-01-11 14:50:10 +01:00
|
|
|
return MBEDTLS_SSL_HASH_SHA256;
|
2022-02-17 07:23:47 +01:00
|
|
|
#endif
|
2023-03-17 12:50:01 +01:00
|
|
|
#if defined(MBEDTLS_MD_CAN_SHA384)
|
2022-02-17 07:23:47 +01:00
|
|
|
case MBEDTLS_MD_SHA384:
|
2023-01-11 14:50:10 +01:00
|
|
|
return MBEDTLS_SSL_HASH_SHA384;
|
2022-02-17 07:23:47 +01:00
|
|
|
#endif
|
2023-03-17 12:50:01 +01:00
|
|
|
#if defined(MBEDTLS_MD_CAN_SHA512)
|
2022-02-17 07:23:47 +01:00
|
|
|
case MBEDTLS_MD_SHA512:
|
2023-01-11 14:50:10 +01:00
|
|
|
return MBEDTLS_SSL_HASH_SHA512;
|
2019-07-23 17:11:24 +02:00
|
|
|
#endif
|
2022-02-17 07:23:47 +01:00
|
|
|
default:
|
2023-01-11 14:50:10 +01:00
|
|
|
return MBEDTLS_SSL_HASH_NONE;
|
2022-02-17 07:23:47 +01:00
|
|
|
}
|
2019-07-15 09:04:11 +02:00
|
|
|
}
|
|
|
|
|
2019-05-28 13:02:16 +02:00
|
|
|
/*
|
2022-02-17 07:23:47 +01:00
|
|
|
* Check if a curve proposed by the peer is in our list.
|
|
|
|
* Return 0 if we're willing to use it, -1 otherwise.
|
2019-05-28 13:02:16 +02:00
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_ssl_check_curve_tls_id(const mbedtls_ssl_context *ssl, uint16_t tls_id)
|
2019-05-28 13:02:16 +02:00
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
const uint16_t *group_list = mbedtls_ssl_get_groups(ssl);
|
2019-07-11 10:58:10 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (group_list == NULL) {
|
|
|
|
return -1;
|
|
|
|
}
|
2019-07-11 09:56:30 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
for (; *group_list != 0; group_list++) {
|
|
|
|
if (*group_list == tls_id) {
|
|
|
|
return 0;
|
|
|
|
}
|
2019-07-11 09:56:30 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return -1;
|
2022-02-17 07:23:47 +01:00
|
|
|
}
|
2019-07-11 10:58:10 +02:00
|
|
|
|
2023-06-27 17:27:51 +02:00
|
|
|
#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
|
2022-02-17 07:23:47 +01:00
|
|
|
/*
|
|
|
|
* Same as mbedtls_ssl_check_curve_tls_id() but with a mbedtls_ecp_group_id.
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_ssl_check_curve(const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id)
|
2022-02-17 07:23:47 +01:00
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
uint16_t tls_id = mbedtls_ssl_get_tls_id_from_ecp_group_id(grp_id);
|
2022-08-09 01:52:38 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (tls_id == 0) {
|
2022-08-09 01:52:38 +02:00
|
|
|
return -1;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2022-08-09 01:52:38 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return mbedtls_ssl_check_curve_tls_id(ssl, tls_id);
|
2022-02-17 07:23:47 +01:00
|
|
|
}
|
2023-06-27 17:27:51 +02:00
|
|
|
#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
|
2023-01-04 16:12:42 +01:00
|
|
|
|
2022-12-30 17:44:24 +01:00
|
|
|
static const struct {
|
|
|
|
uint16_t tls_id;
|
|
|
|
mbedtls_ecp_group_id ecp_group_id;
|
|
|
|
psa_ecc_family_t psa_family;
|
|
|
|
uint16_t bits;
|
|
|
|
} tls_id_match_table[] =
|
|
|
|
{
|
2023-09-01 09:20:51 +02:00
|
|
|
#if defined(MBEDTLS_ECP_HAVE_SECP521R1)
|
2023-06-29 18:06:29 +02:00
|
|
|
{ 25, MBEDTLS_ECP_DP_SECP521R1, PSA_ECC_FAMILY_SECP_R1, 521 },
|
2022-12-30 17:44:24 +01:00
|
|
|
#endif
|
2023-09-01 09:20:51 +02:00
|
|
|
#if defined(MBEDTLS_ECP_HAVE_BP512R1)
|
2023-06-29 18:06:29 +02:00
|
|
|
{ 28, MBEDTLS_ECP_DP_BP512R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1, 512 },
|
2022-12-30 17:44:24 +01:00
|
|
|
#endif
|
2023-09-01 09:20:51 +02:00
|
|
|
#if defined(MBEDTLS_ECP_HAVE_SECP384R1)
|
2023-06-29 18:06:29 +02:00
|
|
|
{ 24, MBEDTLS_ECP_DP_SECP384R1, PSA_ECC_FAMILY_SECP_R1, 384 },
|
2022-12-30 17:44:24 +01:00
|
|
|
#endif
|
2023-09-01 09:20:51 +02:00
|
|
|
#if defined(MBEDTLS_ECP_HAVE_BP384R1)
|
2023-06-29 18:06:29 +02:00
|
|
|
{ 27, MBEDTLS_ECP_DP_BP384R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1, 384 },
|
2022-12-30 17:44:24 +01:00
|
|
|
#endif
|
2023-09-01 09:20:51 +02:00
|
|
|
#if defined(MBEDTLS_ECP_HAVE_SECP256R1)
|
2023-06-29 18:06:29 +02:00
|
|
|
{ 23, MBEDTLS_ECP_DP_SECP256R1, PSA_ECC_FAMILY_SECP_R1, 256 },
|
2022-12-30 17:44:24 +01:00
|
|
|
#endif
|
2023-09-01 09:20:51 +02:00
|
|
|
#if defined(MBEDTLS_ECP_HAVE_SECP256K1)
|
2023-06-29 18:06:29 +02:00
|
|
|
{ 22, MBEDTLS_ECP_DP_SECP256K1, PSA_ECC_FAMILY_SECP_K1, 256 },
|
2022-12-30 17:44:24 +01:00
|
|
|
#endif
|
2023-09-01 09:20:51 +02:00
|
|
|
#if defined(MBEDTLS_ECP_HAVE_BP256R1)
|
2023-06-29 18:06:29 +02:00
|
|
|
{ 26, MBEDTLS_ECP_DP_BP256R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1, 256 },
|
2022-12-30 17:44:24 +01:00
|
|
|
#endif
|
2023-09-01 09:20:51 +02:00
|
|
|
#if defined(MBEDTLS_ECP_HAVE_SECP224R1)
|
2023-06-29 18:06:29 +02:00
|
|
|
{ 21, MBEDTLS_ECP_DP_SECP224R1, PSA_ECC_FAMILY_SECP_R1, 224 },
|
2022-12-30 17:44:24 +01:00
|
|
|
#endif
|
2023-09-01 09:20:51 +02:00
|
|
|
#if defined(MBEDTLS_ECP_HAVE_SECP224K1)
|
2023-06-29 18:06:29 +02:00
|
|
|
{ 20, MBEDTLS_ECP_DP_SECP224K1, PSA_ECC_FAMILY_SECP_K1, 224 },
|
2022-12-30 17:44:24 +01:00
|
|
|
#endif
|
2023-09-01 09:20:51 +02:00
|
|
|
#if defined(MBEDTLS_ECP_HAVE_SECP192R1)
|
2023-06-29 18:06:29 +02:00
|
|
|
{ 19, MBEDTLS_ECP_DP_SECP192R1, PSA_ECC_FAMILY_SECP_R1, 192 },
|
2022-12-30 17:44:24 +01:00
|
|
|
#endif
|
2023-09-01 09:20:51 +02:00
|
|
|
#if defined(MBEDTLS_ECP_HAVE_SECP192K1)
|
2023-06-29 18:06:29 +02:00
|
|
|
{ 18, MBEDTLS_ECP_DP_SECP192K1, PSA_ECC_FAMILY_SECP_K1, 192 },
|
2022-12-30 17:44:24 +01:00
|
|
|
#endif
|
2023-09-01 09:20:51 +02:00
|
|
|
#if defined(MBEDTLS_ECP_HAVE_CURVE25519)
|
2023-06-29 18:06:29 +02:00
|
|
|
{ 29, MBEDTLS_ECP_DP_CURVE25519, PSA_ECC_FAMILY_MONTGOMERY, 255 },
|
2022-12-30 17:44:24 +01:00
|
|
|
#endif
|
2023-09-01 09:20:51 +02:00
|
|
|
#if defined(MBEDTLS_ECP_HAVE_CURVE448)
|
2023-06-29 18:06:29 +02:00
|
|
|
{ 30, MBEDTLS_ECP_DP_CURVE448, PSA_ECC_FAMILY_MONTGOMERY, 448 },
|
2022-12-30 17:44:24 +01:00
|
|
|
#endif
|
2023-06-29 18:06:29 +02:00
|
|
|
{ 0, MBEDTLS_ECP_DP_NONE, 0, 0 },
|
2022-12-30 17:44:24 +01:00
|
|
|
};
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_ssl_get_psa_curve_info_from_tls_id(uint16_t tls_id,
|
2023-06-02 14:52:28 +02:00
|
|
|
psa_key_type_t *type,
|
2023-01-11 14:50:10 +01:00
|
|
|
size_t *bits)
|
2022-12-30 17:44:24 +01:00
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
for (int i = 0; tls_id_match_table[i].tls_id != 0; i++) {
|
|
|
|
if (tls_id_match_table[i].tls_id == tls_id) {
|
2023-06-02 14:52:28 +02:00
|
|
|
if (type != NULL) {
|
|
|
|
*type = PSA_KEY_TYPE_ECC_KEY_PAIR(tls_id_match_table[i].psa_family);
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
|
|
|
if (bits != NULL) {
|
2022-12-30 17:44:24 +01:00
|
|
|
*bits = tls_id_match_table[i].bits;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2022-12-30 17:44:24 +01:00
|
|
|
return PSA_SUCCESS;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return PSA_ERROR_NOT_SUPPORTED;
|
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_ecp_group_id mbedtls_ssl_get_ecp_group_id_from_tls_id(uint16_t tls_id)
|
2022-12-30 17:44:24 +01:00
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
for (int i = 0; tls_id_match_table[i].tls_id != 0; i++) {
|
|
|
|
if (tls_id_match_table[i].tls_id == tls_id) {
|
2022-12-30 17:44:24 +01:00
|
|
|
return tls_id_match_table[i].ecp_group_id;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2022-12-30 17:44:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return MBEDTLS_ECP_DP_NONE;
|
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
uint16_t mbedtls_ssl_get_tls_id_from_ecp_group_id(mbedtls_ecp_group_id grp_id)
|
2022-12-30 17:44:24 +01:00
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
for (int i = 0; tls_id_match_table[i].ecp_group_id != MBEDTLS_ECP_DP_NONE;
|
|
|
|
i++) {
|
|
|
|
if (tls_id_match_table[i].ecp_group_id == grp_id) {
|
2022-12-30 17:44:24 +01:00
|
|
|
return tls_id_match_table[i].tls_id;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2022-12-30 17:44:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-01-04 16:12:42 +01:00
|
|
|
#if defined(MBEDTLS_DEBUG_C)
|
2023-06-29 18:06:29 +02:00
|
|
|
static const struct {
|
|
|
|
uint16_t tls_id;
|
|
|
|
const char *name;
|
|
|
|
} tls_id_curve_name_table[] =
|
|
|
|
{
|
2023-07-07 10:49:27 +02:00
|
|
|
{ MBEDTLS_SSL_IANA_TLS_GROUP_SECP521R1, "secp521r1" },
|
|
|
|
{ MBEDTLS_SSL_IANA_TLS_GROUP_BP512R1, "brainpoolP512r1" },
|
|
|
|
{ MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1, "secp384r1" },
|
|
|
|
{ MBEDTLS_SSL_IANA_TLS_GROUP_BP384R1, "brainpoolP384r1" },
|
|
|
|
{ MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1, "secp256r1" },
|
|
|
|
{ MBEDTLS_SSL_IANA_TLS_GROUP_SECP256K1, "secp256k1" },
|
|
|
|
{ MBEDTLS_SSL_IANA_TLS_GROUP_BP256R1, "brainpoolP256r1" },
|
|
|
|
{ MBEDTLS_SSL_IANA_TLS_GROUP_SECP224R1, "secp224r1" },
|
|
|
|
{ MBEDTLS_SSL_IANA_TLS_GROUP_SECP224K1, "secp224k1" },
|
|
|
|
{ MBEDTLS_SSL_IANA_TLS_GROUP_SECP192R1, "secp192r1" },
|
|
|
|
{ MBEDTLS_SSL_IANA_TLS_GROUP_SECP192K1, "secp192k1" },
|
|
|
|
{ MBEDTLS_SSL_IANA_TLS_GROUP_X25519, "x25519" },
|
|
|
|
{ MBEDTLS_SSL_IANA_TLS_GROUP_X448, "x448" },
|
2023-06-29 18:06:29 +02:00
|
|
|
{ 0, NULL },
|
|
|
|
};
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
const char *mbedtls_ssl_get_curve_name_from_tls_id(uint16_t tls_id)
|
2022-12-30 17:44:24 +01:00
|
|
|
{
|
2023-06-29 18:06:29 +02:00
|
|
|
for (int i = 0; tls_id_curve_name_table[i].tls_id != 0; i++) {
|
|
|
|
if (tls_id_curve_name_table[i].tls_id == tls_id) {
|
|
|
|
return tls_id_curve_name_table[i].name;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2022-12-30 17:44:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
2023-01-04 16:12:42 +01:00
|
|
|
#endif
|
2022-12-30 17:44:24 +01:00
|
|
|
|
2022-02-17 07:23:47 +01:00
|
|
|
#if defined(MBEDTLS_X509_CRT_PARSE_C)
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_ssl_check_cert_usage(const mbedtls_x509_crt *cert,
|
|
|
|
const mbedtls_ssl_ciphersuite_t *ciphersuite,
|
|
|
|
int cert_endpoint,
|
|
|
|
uint32_t *flags)
|
2022-02-17 07:23:47 +01:00
|
|
|
{
|
|
|
|
int ret = 0;
|
2023-11-04 00:36:06 +01:00
|
|
|
unsigned int usage = 0;
|
2022-02-17 07:23:47 +01:00
|
|
|
const char *ext_oid;
|
|
|
|
size_t ext_len;
|
2019-07-11 12:50:53 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (cert_endpoint == MBEDTLS_SSL_IS_SERVER) {
|
2022-02-17 07:23:47 +01:00
|
|
|
/* Server part of the key exchange */
|
2023-01-11 14:50:10 +01:00
|
|
|
switch (ciphersuite->key_exchange) {
|
2022-02-17 07:23:47 +01:00
|
|
|
case MBEDTLS_KEY_EXCHANGE_RSA:
|
|
|
|
case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
|
|
|
|
usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
|
|
|
|
break;
|
2019-07-11 12:50:53 +02:00
|
|
|
|
2022-02-17 07:23:47 +01: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;
|
|
|
|
break;
|
2019-07-11 12:50:53 +02:00
|
|
|
|
2022-02-17 07:23:47 +01:00
|
|
|
case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
|
|
|
|
case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
|
|
|
|
usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
|
|
|
|
break;
|
2019-07-11 12:50:53 +02:00
|
|
|
|
2022-02-17 07:23:47 +01:00
|
|
|
/* Don't use default: we want warnings when adding new values */
|
|
|
|
case MBEDTLS_KEY_EXCHANGE_NONE:
|
|
|
|
case MBEDTLS_KEY_EXCHANGE_PSK:
|
|
|
|
case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
|
|
|
|
case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
|
|
|
|
case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
|
|
|
|
usage = 0;
|
|
|
|
}
|
2023-01-11 14:50:10 +01:00
|
|
|
} else {
|
2022-02-17 07:23:47 +01:00
|
|
|
/* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
|
|
|
|
usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
|
2019-07-23 16:52:45 +02:00
|
|
|
}
|
2019-07-11 12:50:53 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (mbedtls_x509_crt_check_key_usage(cert, usage) != 0) {
|
2022-02-17 07:23:47 +01:00
|
|
|
*flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
|
|
|
|
ret = -1;
|
|
|
|
}
|
2019-07-15 09:04:11 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (cert_endpoint == MBEDTLS_SSL_IS_SERVER) {
|
2022-02-17 07:23:47 +01:00
|
|
|
ext_oid = MBEDTLS_OID_SERVER_AUTH;
|
2023-01-11 14:50:10 +01:00
|
|
|
ext_len = MBEDTLS_OID_SIZE(MBEDTLS_OID_SERVER_AUTH);
|
|
|
|
} else {
|
2022-02-17 07:23:47 +01:00
|
|
|
ext_oid = MBEDTLS_OID_CLIENT_AUTH;
|
2023-01-11 14:50:10 +01:00
|
|
|
ext_len = MBEDTLS_OID_SIZE(MBEDTLS_OID_CLIENT_AUTH);
|
2022-02-17 07:23:47 +01:00
|
|
|
}
|
2019-07-15 09:04:11 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (mbedtls_x509_crt_check_extended_key_usage(cert, ext_oid, ext_len) != 0) {
|
2022-02-17 07:23:47 +01:00
|
|
|
*flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
|
|
|
|
ret = -1;
|
|
|
|
}
|
2019-07-15 11:23:03 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return ret;
|
2022-02-17 07:23:47 +01:00
|
|
|
}
|
|
|
|
#endif /* MBEDTLS_X509_CRT_PARSE_C */
|
2019-07-15 11:23:03 +02:00
|
|
|
|
2022-02-17 07:23:47 +01:00
|
|
|
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_ssl_get_handshake_transcript(mbedtls_ssl_context *ssl,
|
|
|
|
const mbedtls_md_type_t md,
|
|
|
|
unsigned char *dst,
|
|
|
|
size_t dst_len,
|
|
|
|
size_t *olen)
|
2009-01-03 22:22:43 +01:00
|
|
|
{
|
2022-02-17 07:23:47 +01:00
|
|
|
psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
|
|
|
|
psa_hash_operation_t *hash_operation_to_clone;
|
|
|
|
psa_hash_operation_t hash_operation = psa_hash_operation_init();
|
2014-06-26 13:37:14 +02:00
|
|
|
|
2022-02-17 07:23:47 +01:00
|
|
|
*olen = 0;
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
switch (md) {
|
2023-03-17 12:50:01 +01:00
|
|
|
#if defined(MBEDTLS_MD_CAN_SHA384)
|
2023-01-11 14:50:10 +01:00
|
|
|
case MBEDTLS_MD_SHA384:
|
|
|
|
hash_operation_to_clone = &ssl->handshake->fin_sha384_psa;
|
|
|
|
break;
|
2022-02-17 07:23:47 +01:00
|
|
|
#endif
|
|
|
|
|
2023-03-17 12:50:01 +01:00
|
|
|
#if defined(MBEDTLS_MD_CAN_SHA256)
|
2023-01-11 14:50:10 +01:00
|
|
|
case MBEDTLS_MD_SHA256:
|
|
|
|
hash_operation_to_clone = &ssl->handshake->fin_sha256_psa;
|
|
|
|
break;
|
2020-05-06 22:05:13 +02:00
|
|
|
#endif
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
default:
|
|
|
|
goto exit;
|
2022-02-17 07:23:47 +01:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
status = psa_hash_clone(hash_operation_to_clone, &hash_operation);
|
|
|
|
if (status != PSA_SUCCESS) {
|
2022-02-17 07:23:47 +01:00
|
|
|
goto exit;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2022-02-17 07:23:47 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
status = psa_hash_finish(&hash_operation, dst, dst_len, olen);
|
|
|
|
if (status != PSA_SUCCESS) {
|
2022-02-17 07:23:47 +01:00
|
|
|
goto exit;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2022-02-17 07:23:47 +01:00
|
|
|
|
|
|
|
exit:
|
2023-03-17 12:50:01 +01:00
|
|
|
#if !defined(MBEDTLS_MD_CAN_SHA384) && \
|
|
|
|
!defined(MBEDTLS_MD_CAN_SHA256)
|
2022-10-17 13:52:51 +02:00
|
|
|
(void) ssl;
|
|
|
|
#endif
|
2022-12-23 17:00:06 +01:00
|
|
|
return PSA_TO_MBEDTLS_ERR(status);
|
2022-02-17 07:23:47 +01:00
|
|
|
}
|
|
|
|
#else /* MBEDTLS_USE_PSA_CRYPTO */
|
|
|
|
|
2023-03-17 12:50:01 +01:00
|
|
|
#if defined(MBEDTLS_MD_CAN_SHA384)
|
2022-06-17 10:52:54 +02:00
|
|
|
MBEDTLS_CHECK_RETURN_CRITICAL
|
2023-01-11 14:50:10 +01:00
|
|
|
static int ssl_get_handshake_transcript_sha384(mbedtls_ssl_context *ssl,
|
|
|
|
unsigned char *dst,
|
|
|
|
size_t dst_len,
|
|
|
|
size_t *olen)
|
2022-02-17 07:23:47 +01:00
|
|
|
{
|
|
|
|
int ret;
|
2023-02-24 13:21:16 +01:00
|
|
|
mbedtls_md_context_t sha384;
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (dst_len < 48) {
|
|
|
|
return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
|
|
|
|
}
|
2020-05-06 22:05:13 +02:00
|
|
|
|
2023-02-24 13:21:16 +01:00
|
|
|
mbedtls_md_init(&sha384);
|
|
|
|
ret = mbedtls_md_setup(&sha384, mbedtls_md_info_from_type(MBEDTLS_MD_SHA384), 0);
|
2023-02-24 13:19:17 +01:00
|
|
|
if (ret != 0) {
|
|
|
|
goto exit;
|
|
|
|
}
|
2023-02-24 13:21:16 +01:00
|
|
|
ret = mbedtls_md_clone(&sha384, &ssl->handshake->fin_sha384);
|
2023-02-24 13:19:17 +01:00
|
|
|
if (ret != 0) {
|
|
|
|
goto exit;
|
|
|
|
}
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2023-02-24 13:21:16 +01:00
|
|
|
if ((ret = mbedtls_md_finish(&sha384, dst)) != 0) {
|
2023-02-24 13:19:17 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_finish", ret);
|
2022-02-17 07:23:47 +01:00
|
|
|
goto exit;
|
2012-09-16 21:57:18 +02:00
|
|
|
}
|
|
|
|
|
2022-02-17 07:23:47 +01:00
|
|
|
*olen = 48;
|
2012-09-16 21:57:18 +02:00
|
|
|
|
2022-02-17 07:23:47 +01:00
|
|
|
exit:
|
2012-09-16 21:57:18 +02:00
|
|
|
|
2023-02-24 13:21:16 +01:00
|
|
|
mbedtls_md_free(&sha384);
|
2023-01-11 14:50:10 +01:00
|
|
|
return ret;
|
2022-02-17 07:23:47 +01:00
|
|
|
}
|
2023-03-17 12:50:01 +01:00
|
|
|
#endif /* MBEDTLS_MD_CAN_SHA384 */
|
2021-08-10 10:24:19 +02:00
|
|
|
|
2023-03-17 12:50:01 +01:00
|
|
|
#if defined(MBEDTLS_MD_CAN_SHA256)
|
2022-06-17 10:52:54 +02:00
|
|
|
MBEDTLS_CHECK_RETURN_CRITICAL
|
2023-01-11 14:50:10 +01:00
|
|
|
static int ssl_get_handshake_transcript_sha256(mbedtls_ssl_context *ssl,
|
|
|
|
unsigned char *dst,
|
|
|
|
size_t dst_len,
|
|
|
|
size_t *olen)
|
2022-02-17 07:23:47 +01:00
|
|
|
{
|
|
|
|
int ret;
|
2023-02-24 13:19:17 +01:00
|
|
|
mbedtls_md_context_t sha256;
|
2013-02-14 11:19:38 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (dst_len < 32) {
|
|
|
|
return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
|
|
|
|
}
|
2022-02-17 07:23:47 +01:00
|
|
|
|
2023-02-24 13:19:17 +01:00
|
|
|
mbedtls_md_init(&sha256);
|
|
|
|
ret = mbedtls_md_setup(&sha256, mbedtls_md_info_from_type(MBEDTLS_MD_SHA256), 0);
|
|
|
|
if (ret != 0) {
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
ret = mbedtls_md_clone(&sha256, &ssl->handshake->fin_sha256);
|
|
|
|
if (ret != 0) {
|
|
|
|
goto exit;
|
|
|
|
}
|
2022-02-17 07:23:47 +01:00
|
|
|
|
2023-02-24 13:19:17 +01:00
|
|
|
if ((ret = mbedtls_md_finish(&sha256, dst)) != 0) {
|
|
|
|
MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_finish", ret);
|
2022-02-17 07:23:47 +01:00
|
|
|
goto exit;
|
2009-01-03 22:22:43 +01:00
|
|
|
}
|
|
|
|
|
2022-02-17 07:23:47 +01:00
|
|
|
*olen = 32;
|
2014-07-22 17:32:01 +02:00
|
|
|
|
2022-02-17 07:23:47 +01:00
|
|
|
exit:
|
2009-02-05 19:00:28 +01:00
|
|
|
|
2023-02-24 13:19:17 +01:00
|
|
|
mbedtls_md_free(&sha256);
|
2023-01-11 14:50:10 +01:00
|
|
|
return ret;
|
2009-01-03 22:22:43 +01:00
|
|
|
}
|
2023-03-17 12:50:01 +01:00
|
|
|
#endif /* MBEDTLS_MD_CAN_SHA256 */
|
2009-01-03 22:22:43 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_ssl_get_handshake_transcript(mbedtls_ssl_context *ssl,
|
|
|
|
const mbedtls_md_type_t md,
|
|
|
|
unsigned char *dst,
|
|
|
|
size_t dst_len,
|
|
|
|
size_t *olen)
|
2015-05-04 13:35:39 +02:00
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
switch (md) {
|
2015-05-04 13:35:39 +02:00
|
|
|
|
2023-03-17 12:50:01 +01:00
|
|
|
#if defined(MBEDTLS_MD_CAN_SHA384)
|
2023-01-11 14:50:10 +01:00
|
|
|
case MBEDTLS_MD_SHA384:
|
|
|
|
return ssl_get_handshake_transcript_sha384(ssl, dst, dst_len, olen);
|
2023-03-17 12:50:01 +01:00
|
|
|
#endif /* MBEDTLS_MD_CAN_SHA384*/
|
2015-06-17 13:53:47 +02:00
|
|
|
|
2023-03-17 12:50:01 +01:00
|
|
|
#if defined(MBEDTLS_MD_CAN_SHA256)
|
2023-01-11 14:50:10 +01:00
|
|
|
case MBEDTLS_MD_SHA256:
|
|
|
|
return ssl_get_handshake_transcript_sha256(ssl, dst, dst_len, olen);
|
2023-03-17 12:50:01 +01:00
|
|
|
#endif /* MBEDTLS_MD_CAN_SHA256*/
|
2022-01-22 04:56:27 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
default:
|
2023-03-17 12:50:01 +01:00
|
|
|
#if !defined(MBEDTLS_MD_CAN_SHA384) && \
|
|
|
|
!defined(MBEDTLS_MD_CAN_SHA256)
|
2023-01-11 14:50:10 +01:00
|
|
|
(void) ssl;
|
|
|
|
(void) dst;
|
|
|
|
(void) dst_len;
|
|
|
|
(void) olen;
|
2022-10-24 16:33:21 +02:00
|
|
|
#endif
|
2023-01-11 14:50:10 +01:00
|
|
|
break;
|
2022-02-17 07:23:47 +01:00
|
|
|
}
|
2023-01-11 14:50:10 +01:00
|
|
|
return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
|
2022-02-17 07:23:47 +01:00
|
|
|
}
|
2022-01-22 04:56:27 +01:00
|
|
|
|
2022-02-17 07:23:47 +01:00
|
|
|
#endif /* !MBEDTLS_USE_PSA_CRYPTO */
|
2022-01-22 04:56:27 +01:00
|
|
|
|
2022-10-05 12:46:29 +02:00
|
|
|
#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
|
2022-04-27 21:17:56 +02:00
|
|
|
/* mbedtls_ssl_parse_sig_alg_ext()
|
|
|
|
*
|
|
|
|
* The `extension_data` field of signature algorithm contains a `SignatureSchemeList`
|
|
|
|
* value (TLS 1.3 RFC8446):
|
|
|
|
* enum {
|
|
|
|
* ....
|
|
|
|
* ecdsa_secp256r1_sha256( 0x0403 ),
|
|
|
|
* ecdsa_secp384r1_sha384( 0x0503 ),
|
|
|
|
* ecdsa_secp521r1_sha512( 0x0603 ),
|
|
|
|
* ....
|
|
|
|
* } SignatureScheme;
|
|
|
|
*
|
|
|
|
* struct {
|
|
|
|
* SignatureScheme supported_signature_algorithms<2..2^16-2>;
|
|
|
|
* } SignatureSchemeList;
|
|
|
|
*
|
|
|
|
* The `extension_data` field of signature algorithm contains a `SignatureAndHashAlgorithm`
|
|
|
|
* value (TLS 1.2 RFC5246):
|
|
|
|
* enum {
|
|
|
|
* none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5),
|
|
|
|
* sha512(6), (255)
|
|
|
|
* } HashAlgorithm;
|
|
|
|
*
|
|
|
|
* enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) }
|
|
|
|
* SignatureAlgorithm;
|
|
|
|
*
|
|
|
|
* struct {
|
|
|
|
* HashAlgorithm hash;
|
|
|
|
* SignatureAlgorithm signature;
|
|
|
|
* } SignatureAndHashAlgorithm;
|
|
|
|
*
|
|
|
|
* SignatureAndHashAlgorithm
|
|
|
|
* supported_signature_algorithms<2..2^16-2>;
|
|
|
|
*
|
|
|
|
* The TLS 1.3 signature algorithm extension was defined to be a compatible
|
|
|
|
* generalization of the TLS 1.2 signature algorithm extension.
|
|
|
|
* `SignatureAndHashAlgorithm` field of TLS 1.2 can be represented by
|
|
|
|
* `SignatureScheme` field of TLS 1.3
|
|
|
|
*
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_ssl_parse_sig_alg_ext(mbedtls_ssl_context *ssl,
|
|
|
|
const unsigned char *buf,
|
|
|
|
const unsigned char *end)
|
2022-04-27 21:17:56 +02:00
|
|
|
{
|
|
|
|
const unsigned char *p = buf;
|
|
|
|
size_t supported_sig_algs_len = 0;
|
|
|
|
const unsigned char *supported_sig_algs_end;
|
|
|
|
uint16_t sig_alg;
|
|
|
|
uint32_t common_idx = 0;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
|
|
|
|
supported_sig_algs_len = MBEDTLS_GET_UINT16_BE(p, 0);
|
2022-04-27 21:17:56 +02:00
|
|
|
p += 2;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
memset(ssl->handshake->received_sig_algs, 0,
|
|
|
|
sizeof(ssl->handshake->received_sig_algs));
|
2022-04-27 21:17:56 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, supported_sig_algs_len);
|
2022-04-27 21:17:56 +02:00
|
|
|
supported_sig_algs_end = p + supported_sig_algs_len;
|
2023-01-11 14:50:10 +01:00
|
|
|
while (p < supported_sig_algs_end) {
|
|
|
|
MBEDTLS_SSL_CHK_BUF_READ_PTR(p, supported_sig_algs_end, 2);
|
|
|
|
sig_alg = MBEDTLS_GET_UINT16_BE(p, 0);
|
2022-04-27 21:17:56 +02:00
|
|
|
p += 2;
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(4, ("received signature algorithm: 0x%x %s",
|
|
|
|
sig_alg,
|
|
|
|
mbedtls_ssl_sig_alg_to_str(sig_alg)));
|
2022-06-29 04:02:38 +02:00
|
|
|
#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2 &&
|
|
|
|
(!(mbedtls_ssl_sig_alg_is_supported(ssl, sig_alg) &&
|
|
|
|
mbedtls_ssl_sig_alg_is_offered(ssl, sig_alg)))) {
|
2022-04-27 21:17:56 +02:00
|
|
|
continue;
|
2022-06-29 04:02:38 +02:00
|
|
|
}
|
|
|
|
#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
|
2022-04-27 21:17:56 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(4, ("valid signature algorithm: %s",
|
|
|
|
mbedtls_ssl_sig_alg_to_str(sig_alg)));
|
2022-04-27 21:17:56 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (common_idx + 1 < MBEDTLS_RECEIVED_SIG_ALGS_SIZE) {
|
2022-04-27 21:17:56 +02:00
|
|
|
ssl->handshake->received_sig_algs[common_idx] = sig_alg;
|
|
|
|
common_idx += 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* Check that we consumed all the message. */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (p != end) {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(1,
|
|
|
|
("Signature algorithms extension length misaligned"));
|
|
|
|
MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
|
|
|
|
MBEDTLS_ERR_SSL_DECODE_ERROR);
|
|
|
|
return MBEDTLS_ERR_SSL_DECODE_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (common_idx == 0) {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(3, ("no signature algorithm in common"));
|
|
|
|
MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
|
|
|
|
MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
|
|
|
|
return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
|
2022-04-27 21:17:56 +02:00
|
|
|
}
|
|
|
|
|
2022-05-09 16:37:58 +02:00
|
|
|
ssl->handshake->received_sig_algs[common_idx] = MBEDTLS_TLS_SIG_NONE;
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2022-04-27 21:17:56 +02:00
|
|
|
}
|
|
|
|
|
2022-10-05 12:46:29 +02:00
|
|
|
#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
|
2022-04-27 21:17:56 +02:00
|
|
|
|
2022-01-22 04:56:27 +01:00
|
|
|
#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
|
2015-06-17 13:53:47 +02:00
|
|
|
|
2022-02-17 07:23:47 +01:00
|
|
|
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
2015-05-04 13:35:39 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
static psa_status_t setup_psa_key_derivation(psa_key_derivation_operation_t *derivation,
|
|
|
|
mbedtls_svc_key_id_t key,
|
|
|
|
psa_algorithm_t alg,
|
|
|
|
const unsigned char *raw_psk, size_t raw_psk_length,
|
|
|
|
const unsigned char *seed, size_t seed_length,
|
|
|
|
const unsigned char *label, size_t label_length,
|
|
|
|
const unsigned char *other_secret,
|
|
|
|
size_t other_secret_length,
|
|
|
|
size_t capacity)
|
2015-05-04 13:35:39 +02:00
|
|
|
{
|
2022-02-17 07:23:47 +01:00
|
|
|
psa_status_t status;
|
2015-05-04 13:35:39 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
status = psa_key_derivation_setup(derivation, alg);
|
|
|
|
if (status != PSA_SUCCESS) {
|
|
|
|
return status;
|
|
|
|
}
|
2022-02-17 07:23:47 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
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;
|
|
|
|
}
|
2022-02-17 07:23:47 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (other_secret != NULL) {
|
|
|
|
status = psa_key_derivation_input_bytes(derivation,
|
|
|
|
PSA_KEY_DERIVATION_INPUT_OTHER_SECRET,
|
|
|
|
other_secret, other_secret_length);
|
|
|
|
if (status != PSA_SUCCESS) {
|
|
|
|
return status;
|
|
|
|
}
|
2022-04-05 17:12:11 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (mbedtls_svc_key_id_is_null(key)) {
|
2022-02-17 07:23:47 +01:00
|
|
|
status = psa_key_derivation_input_bytes(
|
|
|
|
derivation, PSA_KEY_DERIVATION_INPUT_SECRET,
|
2023-01-11 14:50:10 +01:00
|
|
|
raw_psk, raw_psk_length);
|
|
|
|
} else {
|
2022-02-17 07:23:47 +01:00
|
|
|
status = psa_key_derivation_input_key(
|
2023-01-11 14:50:10 +01:00
|
|
|
derivation, PSA_KEY_DERIVATION_INPUT_SECRET, key);
|
|
|
|
}
|
|
|
|
if (status != PSA_SUCCESS) {
|
|
|
|
return status;
|
2022-02-17 07:23:47 +01:00
|
|
|
}
|
2017-12-20 08:29:30 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
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;
|
2015-05-04 13:35:39 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
status = psa_key_derivation_set_capacity(derivation, capacity);
|
|
|
|
if (status != PSA_SUCCESS) {
|
|
|
|
return status;
|
|
|
|
}
|
2015-05-04 13:35:39 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return PSA_SUCCESS;
|
2015-05-04 13:35:39 +02:00
|
|
|
}
|
|
|
|
|
2022-10-24 16:32:01 +02:00
|
|
|
#if defined(PSA_WANT_ALG_SHA_384) || \
|
|
|
|
defined(PSA_WANT_ALG_SHA_256)
|
2022-06-17 10:52:54 +02:00
|
|
|
MBEDTLS_CHECK_RETURN_CRITICAL
|
2023-01-11 14:50:10 +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)
|
2013-08-21 16:14:26 +02:00
|
|
|
{
|
2022-02-17 07:23:47 +01:00
|
|
|
psa_status_t status;
|
|
|
|
psa_algorithm_t alg;
|
|
|
|
mbedtls_svc_key_id_t master_key = MBEDTLS_SVC_KEY_ID_INIT;
|
|
|
|
psa_key_derivation_operation_t derivation =
|
|
|
|
PSA_KEY_DERIVATION_OPERATION_INIT;
|
2013-08-21 16:14:26 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (md_type == MBEDTLS_MD_SHA384) {
|
2022-02-17 07:23:47 +01:00
|
|
|
alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_384);
|
2023-01-11 14:50:10 +01:00
|
|
|
} else {
|
2022-02-17 07:23:47 +01:00
|
|
|
alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_256);
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2017-04-28 18:15:26 +02:00
|
|
|
|
2022-02-17 07:23:47 +01: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_key=0, which setup_psa_key_derivation() understands
|
|
|
|
* to mean a 0-length "secret" input. */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (slen != 0) {
|
2022-02-17 07:23:47 +01:00
|
|
|
psa_key_attributes_t key_attributes = psa_key_attributes_init();
|
2023-01-11 14:50:10 +01:00
|
|
|
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);
|
|
|
|
|
|
|
|
status = psa_import_key(&key_attributes, secret, slen, &master_key);
|
|
|
|
if (status != PSA_SUCCESS) {
|
|
|
|
return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
|
|
|
|
}
|
2017-04-28 18:15:26 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
status = setup_psa_key_derivation(&derivation,
|
|
|
|
master_key, alg,
|
|
|
|
NULL, 0,
|
|
|
|
random, rlen,
|
|
|
|
(unsigned char const *) label,
|
|
|
|
(size_t) strlen(label),
|
|
|
|
NULL, 0,
|
|
|
|
dlen);
|
|
|
|
if (status != PSA_SUCCESS) {
|
|
|
|
psa_key_derivation_abort(&derivation);
|
|
|
|
psa_destroy_key(master_key);
|
|
|
|
return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
|
2013-08-22 13:52:48 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
status = psa_key_derivation_output_bytes(&derivation, dstbuf, dlen);
|
|
|
|
if (status != PSA_SUCCESS) {
|
|
|
|
psa_key_derivation_abort(&derivation);
|
|
|
|
psa_destroy_key(master_key);
|
|
|
|
return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
|
2015-06-17 14:34:48 +02:00
|
|
|
}
|
2022-02-17 07:23:47 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
status = psa_key_derivation_abort(&derivation);
|
|
|
|
if (status != PSA_SUCCESS) {
|
|
|
|
psa_destroy_key(master_key);
|
|
|
|
return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!mbedtls_svc_key_id_is_null(master_key)) {
|
|
|
|
status = psa_destroy_key(master_key);
|
|
|
|
}
|
|
|
|
if (status != PSA_SUCCESS) {
|
|
|
|
return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
|
|
|
|
}
|
2022-02-17 07:23:47 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2015-06-17 14:34:48 +02:00
|
|
|
}
|
2022-10-24 16:32:01 +02:00
|
|
|
#endif /* PSA_WANT_ALG_SHA_256 || PSA_WANT_ALG_SHA_384 */
|
2022-02-17 07:23:47 +01:00
|
|
|
#else /* MBEDTLS_USE_PSA_CRYPTO */
|
|
|
|
|
2022-10-24 16:32:01 +02:00
|
|
|
#if defined(MBEDTLS_MD_C) && \
|
2023-03-17 12:50:01 +01:00
|
|
|
(defined(MBEDTLS_MD_CAN_SHA256) || \
|
|
|
|
defined(MBEDTLS_MD_CAN_SHA384))
|
2022-06-17 10:52:54 +02:00
|
|
|
MBEDTLS_CHECK_RETURN_CRITICAL
|
2023-01-11 14:50:10 +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)
|
2014-02-04 16:18:07 +01:00
|
|
|
{
|
2022-02-17 07:23:47 +01:00
|
|
|
size_t nb;
|
|
|
|
size_t i, j, k, md_len;
|
|
|
|
unsigned char *tmp;
|
|
|
|
size_t tmp_len = 0;
|
|
|
|
unsigned char h_i[MBEDTLS_MD_MAX_SIZE];
|
|
|
|
const mbedtls_md_info_t *md_info;
|
|
|
|
mbedtls_md_context_t md_ctx;
|
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2014-02-04 16:18:07 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_md_init(&md_ctx);
|
2015-06-17 12:10:46 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if ((md_info = mbedtls_md_info_from_type(md_type)) == NULL) {
|
|
|
|
return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
|
|
|
|
}
|
2022-02-17 07:23:47 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
md_len = mbedtls_md_get_size(md_info);
|
2022-02-17 07:23:47 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
tmp_len = md_len + strlen(label) + rlen;
|
|
|
|
tmp = mbedtls_calloc(1, tmp_len);
|
|
|
|
if (tmp == NULL) {
|
2022-02-17 07:23:47 +01:00
|
|
|
ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
|
|
|
|
goto exit;
|
2021-08-17 10:53:13 +02:00
|
|
|
}
|
2014-02-04 16:18:07 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
nb = strlen(label);
|
|
|
|
memcpy(tmp + md_len, label, nb);
|
|
|
|
memcpy(tmp + md_len + nb, random, rlen);
|
2022-02-17 07:23:47 +01:00
|
|
|
nb += rlen;
|
2022-01-18 13:10:56 +01:00
|
|
|
|
2022-02-17 07:23:47 +01:00
|
|
|
/*
|
|
|
|
* Compute P_<hash>(secret, label + random)[0..dlen]
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
if ((ret = mbedtls_md_setup(&md_ctx, md_info, 1)) != 0) {
|
2022-02-17 07:23:47 +01:00
|
|
|
goto exit;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2014-04-09 09:50:57 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = mbedtls_md_hmac_starts(&md_ctx, secret, slen);
|
|
|
|
if (ret != 0) {
|
2022-02-17 07:23:47 +01:00
|
|
|
goto exit;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
|
|
|
ret = mbedtls_md_hmac_update(&md_ctx, tmp + md_len, nb);
|
|
|
|
if (ret != 0) {
|
2022-02-17 07:23:47 +01:00
|
|
|
goto exit;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
|
|
|
ret = mbedtls_md_hmac_finish(&md_ctx, tmp);
|
|
|
|
if (ret != 0) {
|
2022-02-17 07:23:47 +01:00
|
|
|
goto exit;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2014-04-11 11:06:22 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
for (i = 0; i < dlen; i += md_len) {
|
|
|
|
ret = mbedtls_md_hmac_reset(&md_ctx);
|
|
|
|
if (ret != 0) {
|
2022-02-17 07:23:47 +01:00
|
|
|
goto exit;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
|
|
|
ret = mbedtls_md_hmac_update(&md_ctx, tmp, md_len + nb);
|
|
|
|
if (ret != 0) {
|
2022-02-17 07:23:47 +01:00
|
|
|
goto exit;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
|
|
|
ret = mbedtls_md_hmac_finish(&md_ctx, h_i);
|
|
|
|
if (ret != 0) {
|
2022-02-17 07:23:47 +01:00
|
|
|
goto exit;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2014-04-09 09:50:57 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = mbedtls_md_hmac_reset(&md_ctx);
|
|
|
|
if (ret != 0) {
|
2022-02-17 07:23:47 +01:00
|
|
|
goto exit;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
|
|
|
ret = mbedtls_md_hmac_update(&md_ctx, tmp, md_len);
|
|
|
|
if (ret != 0) {
|
2022-02-17 07:23:47 +01:00
|
|
|
goto exit;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
|
|
|
ret = mbedtls_md_hmac_finish(&md_ctx, tmp);
|
|
|
|
if (ret != 0) {
|
2022-02-17 07:23:47 +01:00
|
|
|
goto exit;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2014-04-09 09:50:57 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
k = (i + md_len > dlen) ? dlen % md_len : md_len;
|
2014-04-09 09:50:57 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
for (j = 0; j < k; j++) {
|
2022-02-17 07:23:47 +01:00
|
|
|
dstbuf[i + j] = h_i[j];
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2014-04-09 09:50:57 +02:00
|
|
|
}
|
|
|
|
|
2022-02-17 07:23:47 +01:00
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_md_free(&md_ctx);
|
2014-04-09 09:50:57 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (tmp != NULL) {
|
|
|
|
mbedtls_platform_zeroize(tmp, tmp_len);
|
|
|
|
}
|
2022-11-01 17:08:14 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_platform_zeroize(h_i, sizeof(h_i));
|
2022-02-17 07:23:47 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_free(tmp);
|
2022-02-17 07:23:47 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return ret;
|
2022-02-17 07:23:47 +01:00
|
|
|
}
|
2023-03-17 12:50:01 +01:00
|
|
|
#endif /* MBEDTLS_MD_C && ( MBEDTLS_MD_CAN_SHA256 || MBEDTLS_MD_CAN_SHA384 ) */
|
2022-02-17 07:23:47 +01:00
|
|
|
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
|
|
|
|
2023-03-17 12:50:01 +01:00
|
|
|
#if defined(MBEDTLS_MD_CAN_SHA256)
|
2022-06-17 10:52:54 +02:00
|
|
|
MBEDTLS_CHECK_RETURN_CRITICAL
|
2023-01-11 14:50:10 +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)
|
2022-02-17 07:23:47 +01:00
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
return tls_prf_generic(MBEDTLS_MD_SHA256, secret, slen,
|
|
|
|
label, random, rlen, dstbuf, dlen);
|
2022-02-17 07:23:47 +01:00
|
|
|
}
|
2023-03-17 12:50:01 +01:00
|
|
|
#endif /* MBEDTLS_MD_CAN_SHA256*/
|
2022-02-17 07:23:47 +01:00
|
|
|
|
2023-03-17 12:50:01 +01:00
|
|
|
#if defined(MBEDTLS_MD_CAN_SHA384)
|
2022-06-17 10:52:54 +02:00
|
|
|
MBEDTLS_CHECK_RETURN_CRITICAL
|
2023-01-11 14:50:10 +01:00
|
|
|
static int tls_prf_sha384(const unsigned char *secret, size_t slen,
|
|
|
|
const char *label,
|
|
|
|
const unsigned char *random, size_t rlen,
|
|
|
|
unsigned char *dstbuf, size_t dlen)
|
2022-02-17 07:23:47 +01:00
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
return tls_prf_generic(MBEDTLS_MD_SHA384, secret, slen,
|
|
|
|
label, random, rlen, dstbuf, dlen);
|
2022-02-17 07:23:47 +01:00
|
|
|
}
|
2023-03-17 12:50:01 +01:00
|
|
|
#endif /* MBEDTLS_MD_CAN_SHA384*/
|
2022-02-17 07:23:47 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Set appropriate PRF function and other SSL / TLS1.2 functions
|
|
|
|
*
|
|
|
|
* Inputs:
|
|
|
|
* - hash associated with the ciphersuite (only used by TLS 1.2)
|
|
|
|
*
|
|
|
|
* Outputs:
|
|
|
|
* - the tls_prf, calc_verify and calc_finished members of handshake structure
|
|
|
|
*/
|
2022-06-17 10:52:54 +02:00
|
|
|
MBEDTLS_CHECK_RETURN_CRITICAL
|
2023-01-11 14:50:10 +01:00
|
|
|
static int ssl_set_handshake_prfs(mbedtls_ssl_handshake_params *handshake,
|
|
|
|
mbedtls_md_type_t hash)
|
2022-02-17 07:23:47 +01:00
|
|
|
{
|
2023-03-17 12:50:01 +01:00
|
|
|
#if defined(MBEDTLS_MD_CAN_SHA384)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (hash == MBEDTLS_MD_SHA384) {
|
2022-02-17 07:23:47 +01:00
|
|
|
handshake->tls_prf = tls_prf_sha384;
|
|
|
|
handshake->calc_verify = ssl_calc_verify_tls_sha384;
|
|
|
|
handshake->calc_finished = ssl_calc_finished_tls_sha384;
|
2023-01-11 14:50:10 +01:00
|
|
|
} else
|
2022-02-17 07:23:47 +01:00
|
|
|
#endif
|
2023-03-17 12:50:01 +01:00
|
|
|
#if defined(MBEDTLS_MD_CAN_SHA256)
|
2014-04-11 11:06:22 +02:00
|
|
|
{
|
2022-03-07 09:05:51 +01:00
|
|
|
(void) hash;
|
2022-02-17 07:23:47 +01:00
|
|
|
handshake->tls_prf = tls_prf_sha256;
|
|
|
|
handshake->calc_verify = ssl_calc_verify_tls_sha256;
|
|
|
|
handshake->calc_finished = ssl_calc_finished_tls_sha256;
|
2014-04-11 11:06:22 +02:00
|
|
|
}
|
2022-03-07 09:05:51 +01:00
|
|
|
#else
|
2015-04-20 12:01:48 +02:00
|
|
|
{
|
2022-02-17 07:23:47 +01:00
|
|
|
(void) handshake;
|
2022-03-07 09:05:51 +01:00
|
|
|
(void) hash;
|
2023-01-11 14:50:10 +01:00
|
|
|
return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
|
2015-04-20 12:01:48 +02:00
|
|
|
}
|
2022-03-07 09:05:51 +01:00
|
|
|
#endif
|
2014-04-11 11:06:22 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2014-04-09 09:50:57 +02:00
|
|
|
}
|
2014-04-29 15:11:17 +02:00
|
|
|
|
2022-02-17 07:23:47 +01:00
|
|
|
/*
|
|
|
|
* Compute master secret if needed
|
|
|
|
*
|
|
|
|
* Parameters:
|
|
|
|
* [in/out] handshake
|
|
|
|
* [in] resume, premaster, extended_ms, calc_verify, tls_prf
|
|
|
|
* (PSA-PSK) ciphersuite_info, psk_opaque
|
|
|
|
* [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 + session_negotiate)
|
2022-03-07 11:10:36 +01:00
|
|
|
* PSA-PSA: conf
|
2022-02-17 07:23:47 +01:00
|
|
|
*/
|
2022-06-17 10:52:54 +02:00
|
|
|
MBEDTLS_CHECK_RETURN_CRITICAL
|
2023-01-11 14:50:10 +01:00
|
|
|
static int ssl_compute_master(mbedtls_ssl_handshake_params *handshake,
|
|
|
|
unsigned char *master,
|
|
|
|
const mbedtls_ssl_context *ssl)
|
2022-02-17 07:23:47 +01:00
|
|
|
{
|
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
|
|
|
|
|
|
|
/* 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 */
|
2016-10-13 18:21:01 +02:00
|
|
|
|
2022-02-17 07:23:47 +01: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";
|
2019-01-10 11:27:10 +01:00
|
|
|
|
2022-04-05 17:15:55 +02:00
|
|
|
/* The seed for the KDF used for key expansion.
|
2022-02-17 07:23:47 +01:00
|
|
|
* - 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). */
|
2022-04-05 17:15:55 +02:00
|
|
|
unsigned char const *seed = handshake->randbytes;
|
|
|
|
size_t seed_len = 64;
|
2019-01-10 11:27:10 +01:00
|
|
|
|
2022-02-17 07:23:47 +01:00
|
|
|
#if !defined(MBEDTLS_DEBUG_C) && \
|
|
|
|
!defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) && \
|
|
|
|
!(defined(MBEDTLS_USE_PSA_CRYPTO) && \
|
2023-01-11 14:50:10 +01:00
|
|
|
defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED))
|
2022-02-17 07:23:47 +01:00
|
|
|
ssl = NULL; /* make sure we don't use it except for those cases */
|
|
|
|
(void) ssl;
|
|
|
|
#endif
|
2019-01-14 10:35:19 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (handshake->resume != 0) {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(3, ("no premaster (session resumed)"));
|
|
|
|
return 0;
|
2019-01-10 11:27:10 +01:00
|
|
|
}
|
|
|
|
|
2022-02-17 07:23:47 +01:00
|
|
|
#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED) {
|
2022-02-17 07:23:47 +01:00
|
|
|
lbl = "extended master secret";
|
2022-04-05 17:15:55 +02:00
|
|
|
seed = session_hash;
|
2023-02-06 00:34:21 +01:00
|
|
|
ret = handshake->calc_verify(ssl, session_hash, &seed_len);
|
|
|
|
if (ret != 0) {
|
|
|
|
MBEDTLS_SSL_DEBUG_RET(1, "calc_verify", ret);
|
|
|
|
}
|
2019-01-10 11:27:10 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_BUF(3, "session hash for extended master secret",
|
|
|
|
session_hash, seed_len);
|
2019-01-10 11:27:10 +01:00
|
|
|
}
|
2022-04-29 07:53:29 +02:00
|
|
|
#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
|
2019-01-10 11:27:10 +01:00
|
|
|
|
2022-04-22 11:20:09 +02:00
|
|
|
#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
|
2022-04-28 09:22:22 +02:00
|
|
|
defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (mbedtls_ssl_ciphersuite_uses_psk(handshake->ciphersuite_info) == 1) {
|
2022-02-17 07:23:47 +01:00
|
|
|
/* Perform PSK-to-MS expansion in a single step. */
|
|
|
|
psa_status_t status;
|
|
|
|
psa_algorithm_t alg;
|
|
|
|
mbedtls_svc_key_id_t psk;
|
|
|
|
psa_key_derivation_operation_t derivation =
|
|
|
|
PSA_KEY_DERIVATION_OPERATION_INIT;
|
2023-10-05 14:30:37 +02:00
|
|
|
mbedtls_md_type_t hash_alg = (mbedtls_md_type_t) handshake->ciphersuite_info->mac;
|
2019-01-10 11:27:10 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(2, ("perform PSA-based PSK-to-MS expansion"));
|
2019-01-10 11:27:10 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
psk = mbedtls_ssl_get_opaque_psk(ssl);
|
2019-01-10 11:27:10 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (hash_alg == MBEDTLS_MD_SHA384) {
|
2022-02-17 07:23:47 +01:00
|
|
|
alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384);
|
2023-01-11 14:50:10 +01:00
|
|
|
} else {
|
2022-02-17 07:23:47 +01:00
|
|
|
alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256);
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2017-07-20 17:17:51 +02:00
|
|
|
|
2022-04-13 08:57:06 +02:00
|
|
|
size_t other_secret_len = 0;
|
2023-01-11 14:50:10 +01:00
|
|
|
unsigned char *other_secret = NULL;
|
2022-04-05 17:19:41 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
switch (handshake->ciphersuite_info->key_exchange) {
|
2022-04-14 08:29:31 +02:00
|
|
|
/* Provide other secret.
|
2022-04-13 08:57:06 +02:00
|
|
|
* Other secret is stored in premaster, where first 2 bytes hold the
|
2022-04-14 08:29:31 +02:00
|
|
|
* length of the other key.
|
2022-04-05 17:19:41 +02:00
|
|
|
*/
|
2022-04-14 08:29:31 +02:00
|
|
|
case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
|
2022-04-28 09:16:28 +02:00
|
|
|
/* For RSA-PSK other key length is always 48 bytes. */
|
2022-04-14 08:29:31 +02:00
|
|
|
other_secret_len = 48;
|
|
|
|
other_secret = handshake->premaster + 2;
|
|
|
|
break;
|
|
|
|
case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
|
2022-04-19 12:22:38 +02:00
|
|
|
case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
|
2022-04-14 08:29:31 +02:00
|
|
|
other_secret_len = MBEDTLS_GET_UINT16_BE(handshake->premaster, 0);
|
|
|
|
other_secret = handshake->premaster + 2;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
2022-04-05 17:19:41 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
status = setup_psa_key_derivation(&derivation, psk, alg,
|
|
|
|
ssl->conf->psk, ssl->conf->psk_len,
|
|
|
|
seed, seed_len,
|
|
|
|
(unsigned char const *) lbl,
|
|
|
|
(size_t) strlen(lbl),
|
|
|
|
other_secret, other_secret_len,
|
|
|
|
master_secret_len);
|
|
|
|
if (status != PSA_SUCCESS) {
|
|
|
|
psa_key_derivation_abort(&derivation);
|
|
|
|
return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
|
2022-02-17 07:23:47 +01:00
|
|
|
}
|
2019-01-14 10:35:19 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
status = psa_key_derivation_output_bytes(&derivation,
|
|
|
|
master,
|
|
|
|
master_secret_len);
|
|
|
|
if (status != PSA_SUCCESS) {
|
|
|
|
psa_key_derivation_abort(&derivation);
|
|
|
|
return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
|
2022-02-17 07:23:47 +01:00
|
|
|
}
|
2017-07-20 17:17:51 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
status = psa_key_derivation_abort(&derivation);
|
|
|
|
if (status != PSA_SUCCESS) {
|
|
|
|
return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
|
|
|
|
}
|
|
|
|
} else
|
2022-02-17 07:23:47 +01:00
|
|
|
#endif
|
2017-07-20 17:17:51 +02:00
|
|
|
{
|
2022-05-31 14:43:23 +02:00
|
|
|
#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
|
2023-01-11 14:50:10 +01:00
|
|
|
defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
|
|
|
|
if (handshake->ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE) {
|
2022-05-31 14:43:23 +02:00
|
|
|
psa_status_t status;
|
|
|
|
psa_algorithm_t alg = PSA_ALG_TLS12_ECJPAKE_TO_PMS;
|
|
|
|
psa_key_derivation_operation_t derivation =
|
|
|
|
PSA_KEY_DERIVATION_OPERATION_INIT;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(2, ("perform PSA-based PMS KDF for ECJPAKE"));
|
2022-05-31 14:43:23 +02:00
|
|
|
|
|
|
|
handshake->pmslen = PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
status = psa_key_derivation_setup(&derivation, alg);
|
|
|
|
if (status != PSA_SUCCESS) {
|
|
|
|
return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
|
|
|
|
}
|
2022-05-31 14:43:23 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
status = psa_key_derivation_set_capacity(&derivation,
|
|
|
|
PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE);
|
|
|
|
if (status != PSA_SUCCESS) {
|
|
|
|
psa_key_derivation_abort(&derivation);
|
|
|
|
return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
|
2022-05-31 14:43:23 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
status = psa_pake_get_implicit_key(&handshake->psa_pake_ctx,
|
|
|
|
&derivation);
|
|
|
|
if (status != PSA_SUCCESS) {
|
|
|
|
psa_key_derivation_abort(&derivation);
|
|
|
|
return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
|
2022-05-31 14:43:23 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
status = psa_key_derivation_output_bytes(&derivation,
|
|
|
|
handshake->premaster,
|
|
|
|
handshake->pmslen);
|
|
|
|
if (status != PSA_SUCCESS) {
|
|
|
|
psa_key_derivation_abort(&derivation);
|
|
|
|
return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
|
2022-05-31 14:43:23 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
status = psa_key_derivation_abort(&derivation);
|
|
|
|
if (status != PSA_SUCCESS) {
|
|
|
|
return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
|
2022-05-31 14:43:23 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = handshake->tls_prf(handshake->premaster, handshake->pmslen,
|
|
|
|
lbl, seed, seed_len,
|
|
|
|
master,
|
|
|
|
master_secret_len);
|
|
|
|
if (ret != 0) {
|
|
|
|
MBEDTLS_SSL_DEBUG_RET(1, "prf", ret);
|
|
|
|
return ret;
|
2022-02-17 07:23:47 +01:00
|
|
|
}
|
2017-07-20 17:17:51 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_BUF(3, "premaster secret",
|
|
|
|
handshake->premaster,
|
|
|
|
handshake->pmslen);
|
2017-07-20 17:17:51 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_platform_zeroize(handshake->premaster,
|
|
|
|
sizeof(handshake->premaster));
|
2022-02-17 07:23:47 +01:00
|
|
|
}
|
2017-07-20 17:17:51 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2017-07-20 17:17:51 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_ssl_derive_keys(mbedtls_ssl_context *ssl)
|
2021-09-24 17:20:59 +02:00
|
|
|
{
|
2022-02-17 07:23:47 +01:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
|
|
|
const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
|
|
|
|
ssl->handshake->ciphersuite_info;
|
2022-01-07 22:09:01 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(2, ("=> derive keys"));
|
2022-01-07 22:09:01 +01:00
|
|
|
|
2022-02-17 07:23:47 +01:00
|
|
|
/* Set PRF, calc_verify and calc_finished function pointers */
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = ssl_set_handshake_prfs(ssl->handshake,
|
2023-07-13 14:12:40 +02:00
|
|
|
(mbedtls_md_type_t) ciphersuite_info->mac);
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ret != 0) {
|
|
|
|
MBEDTLS_SSL_DEBUG_RET(1, "ssl_set_handshake_prfs", ret);
|
|
|
|
return ret;
|
2022-01-07 22:09:01 +01:00
|
|
|
}
|
|
|
|
|
2022-02-17 07:23:47 +01:00
|
|
|
/* Compute master secret if needed */
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = ssl_compute_master(ssl->handshake,
|
|
|
|
ssl->session_negotiate->master,
|
|
|
|
ssl);
|
|
|
|
if (ret != 0) {
|
|
|
|
MBEDTLS_SSL_DEBUG_RET(1, "ssl_compute_master", ret);
|
|
|
|
return ret;
|
2022-02-17 07:23:47 +01:00
|
|
|
}
|
2021-09-09 08:21:07 +02:00
|
|
|
|
2022-02-17 07:23:47 +01: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];
|
2023-01-11 14:50:10 +01:00
|
|
|
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));
|
2022-02-17 07:23:47 +01:00
|
|
|
}
|
2021-09-09 08:21:07 +02:00
|
|
|
|
2022-02-17 07:23:47 +01:00
|
|
|
/* Populate transform structure */
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = ssl_tls12_populate_transform(ssl->transform_negotiate,
|
|
|
|
ssl->session_negotiate->ciphersuite,
|
|
|
|
ssl->session_negotiate->master,
|
2022-04-05 11:16:53 +02:00
|
|
|
#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
|
2023-01-11 14:50:10 +01:00
|
|
|
ssl->session_negotiate->encrypt_then_mac,
|
2022-04-05 11:16:53 +02:00
|
|
|
#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
|
2023-01-11 14:50:10 +01:00
|
|
|
ssl->handshake->tls_prf,
|
|
|
|
ssl->handshake->randbytes,
|
|
|
|
ssl->tls_version,
|
|
|
|
ssl->conf->endpoint,
|
|
|
|
ssl);
|
|
|
|
if (ret != 0) {
|
|
|
|
MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls12_populate_transform", ret);
|
|
|
|
return ret;
|
2021-09-09 08:21:07 +02:00
|
|
|
}
|
|
|
|
|
2022-02-17 07:23:47 +01:00
|
|
|
/* We no longer need Server/ClientHello.random values */
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_platform_zeroize(ssl->handshake->randbytes,
|
|
|
|
sizeof(ssl->handshake->randbytes));
|
2021-09-09 08:21:07 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(2, ("<= derive keys"));
|
2021-09-09 08:21:07 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2021-09-09 08:21:07 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_ssl_set_calc_verify_md(mbedtls_ssl_context *ssl, int md)
|
2022-03-07 10:21:40 +01:00
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
switch (md) {
|
2023-03-17 12:50:01 +01:00
|
|
|
#if defined(MBEDTLS_MD_CAN_SHA384)
|
2022-03-07 10:21:40 +01:00
|
|
|
case MBEDTLS_SSL_HASH_SHA384:
|
|
|
|
ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
|
|
|
|
break;
|
|
|
|
#endif
|
2023-03-17 12:50:01 +01:00
|
|
|
#if defined(MBEDTLS_MD_CAN_SHA256)
|
2022-03-07 10:21:40 +01:00
|
|
|
case MBEDTLS_SSL_HASH_SHA256:
|
|
|
|
ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
default:
|
2023-01-11 14:50:10 +01:00
|
|
|
return -1;
|
2022-03-07 10:21:40 +01:00
|
|
|
}
|
2023-03-17 12:50:01 +01:00
|
|
|
#if !defined(MBEDTLS_MD_CAN_SHA384) && \
|
|
|
|
!defined(MBEDTLS_MD_CAN_SHA256)
|
2022-10-17 13:52:51 +02:00
|
|
|
(void) ssl;
|
|
|
|
#endif
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2022-03-07 10:21:40 +01:00
|
|
|
}
|
|
|
|
|
2022-02-17 07:23:47 +01:00
|
|
|
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
2023-06-24 09:43:26 +02:00
|
|
|
static int ssl_calc_verify_tls_psa(const mbedtls_ssl_context *ssl,
|
|
|
|
const psa_hash_operation_t *hs_op,
|
|
|
|
size_t buffer_size,
|
|
|
|
unsigned char *hash,
|
|
|
|
size_t *hlen)
|
|
|
|
{
|
2022-02-17 07:23:47 +01:00
|
|
|
psa_status_t status;
|
2023-07-04 11:35:16 +02:00
|
|
|
psa_hash_operation_t cloned_op = psa_hash_operation_init();
|
2021-09-09 08:21:07 +02:00
|
|
|
|
2023-06-24 09:43:26 +02:00
|
|
|
#if !defined(MBEDTLS_DEBUG_C)
|
|
|
|
(void) ssl;
|
|
|
|
#endif
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(2, ("=> PSA calc verify"));
|
2023-07-04 11:35:16 +02:00
|
|
|
status = psa_hash_clone(hs_op, &cloned_op);
|
2023-01-11 14:50:10 +01:00
|
|
|
if (status != PSA_SUCCESS) {
|
2023-02-06 10:06:04 +01:00
|
|
|
goto exit;
|
2022-02-17 07:23:47 +01:00
|
|
|
}
|
2021-12-23 13:15:02 +01:00
|
|
|
|
2023-07-04 11:35:16 +02:00
|
|
|
status = psa_hash_finish(&cloned_op, hash, buffer_size, hlen);
|
2023-01-11 14:50:10 +01:00
|
|
|
if (status != PSA_SUCCESS) {
|
2023-02-06 10:06:04 +01:00
|
|
|
goto exit;
|
2021-09-09 08:21:07 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_BUF(3, "PSA calculated verify result", hash, *hlen);
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(2, ("<= PSA calc verify"));
|
2023-02-06 10:06:04 +01:00
|
|
|
|
|
|
|
exit:
|
2023-07-04 11:35:16 +02:00
|
|
|
psa_hash_abort(&cloned_op);
|
2023-03-28 12:54:56 +02:00
|
|
|
return mbedtls_md_error_from_psa(status);
|
2023-06-24 09:43:26 +02:00
|
|
|
}
|
2022-02-17 07:23:47 +01:00
|
|
|
#else
|
2023-06-24 09:43:26 +02:00
|
|
|
static int ssl_calc_verify_tls_legacy(const mbedtls_ssl_context *ssl,
|
|
|
|
const mbedtls_md_context_t *hs_ctx,
|
|
|
|
unsigned char *hash,
|
|
|
|
size_t *hlen)
|
|
|
|
{
|
2023-02-06 10:06:04 +01:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2023-07-04 11:35:16 +02:00
|
|
|
mbedtls_md_context_t cloned_ctx;
|
2021-09-09 08:21:07 +02:00
|
|
|
|
2023-07-04 11:35:16 +02:00
|
|
|
mbedtls_md_init(&cloned_ctx);
|
2021-09-09 08:21:07 +02:00
|
|
|
|
2023-06-24 09:43:26 +02:00
|
|
|
#if !defined(MBEDTLS_DEBUG_C)
|
|
|
|
(void) ssl;
|
|
|
|
#endif
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc verify"));
|
2021-09-09 08:21:07 +02:00
|
|
|
|
2023-07-04 11:35:16 +02:00
|
|
|
ret = mbedtls_md_setup(&cloned_ctx, mbedtls_md_info_from_ctx(hs_ctx), 0);
|
2023-02-24 13:19:17 +01:00
|
|
|
if (ret != 0) {
|
|
|
|
goto exit;
|
|
|
|
}
|
2023-07-04 11:35:16 +02:00
|
|
|
ret = mbedtls_md_clone(&cloned_ctx, hs_ctx);
|
2023-02-24 13:19:17 +01:00
|
|
|
if (ret != 0) {
|
|
|
|
goto exit;
|
|
|
|
}
|
2023-02-06 10:06:04 +01:00
|
|
|
|
2023-07-04 11:35:16 +02:00
|
|
|
ret = mbedtls_md_finish(&cloned_ctx, hash);
|
2023-02-06 10:06:04 +01:00
|
|
|
if (ret != 0) {
|
|
|
|
goto exit;
|
|
|
|
}
|
2021-10-08 09:14:45 +02:00
|
|
|
|
2023-06-24 09:43:26 +02:00
|
|
|
*hlen = mbedtls_md_get_size(mbedtls_md_info_from_ctx(hs_ctx));
|
2021-10-08 09:14:45 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_BUF(3, "calculated verify result", hash, *hlen);
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(2, ("<= calc verify"));
|
2021-10-08 09:14:45 +02:00
|
|
|
|
2023-02-06 10:06:04 +01:00
|
|
|
exit:
|
2023-07-04 11:35:16 +02:00
|
|
|
mbedtls_md_free(&cloned_ctx);
|
2023-02-09 10:33:54 +01:00
|
|
|
return ret;
|
2023-06-24 09:43:26 +02:00
|
|
|
}
|
|
|
|
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_MD_CAN_SHA256)
|
|
|
|
int ssl_calc_verify_tls_sha256(const mbedtls_ssl_context *ssl,
|
|
|
|
unsigned char *hash,
|
|
|
|
size_t *hlen)
|
|
|
|
{
|
|
|
|
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
|
|
|
return ssl_calc_verify_tls_psa(ssl, &ssl->handshake->fin_sha256_psa, 32,
|
|
|
|
hash, hlen);
|
|
|
|
#else
|
|
|
|
return ssl_calc_verify_tls_legacy(ssl, &ssl->handshake->fin_sha256,
|
|
|
|
hash, hlen);
|
2022-02-17 07:23:47 +01:00
|
|
|
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
2021-09-09 08:21:07 +02:00
|
|
|
}
|
2023-03-17 12:50:01 +01:00
|
|
|
#endif /* MBEDTLS_MD_CAN_SHA256 */
|
2021-12-07 10:16:29 +01:00
|
|
|
|
2023-03-17 12:50:01 +01:00
|
|
|
#if defined(MBEDTLS_MD_CAN_SHA384)
|
Make handshake hashing functions return int
There are three family of functions: update_checksum, calc_verify,
calc_finished, that perform hashing operations and were returning void
so far. This is not correct, as hashing functions can return errors (for
example, on hardware failure when accelerated). Change them to return
int.
This commit just changes the types: for now the functions always return
0, and their return value is not checked; this will be fixed in the
next few commits.
There is a related function in TLS 1.3,
mbedtls_ssl_reset_transcript_for_hrr, which also handles hashes, and
already returns int but does not correctly check for errors from hashing
functions so far, it will also be handled in the next few commits.
There's a special case with handshake_params_init: _init functions
should return void, so we'll need to split out the part that can return
errors, see the next commit.
Signed-off-by: Manuel Pégourié-Gonnard <manuel.pegourie-gonnard@arm.com>
2023-02-05 09:46:59 +01:00
|
|
|
int ssl_calc_verify_tls_sha384(const mbedtls_ssl_context *ssl,
|
|
|
|
unsigned char *hash,
|
|
|
|
size_t *hlen)
|
2021-12-20 15:22:15 +01:00
|
|
|
{
|
2022-02-17 07:23:47 +01:00
|
|
|
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
2023-06-24 09:43:26 +02:00
|
|
|
return ssl_calc_verify_tls_psa(ssl, &ssl->handshake->fin_sha384_psa, 48,
|
|
|
|
hash, hlen);
|
2022-02-17 07:23:47 +01:00
|
|
|
#else
|
2023-06-24 09:43:26 +02:00
|
|
|
return ssl_calc_verify_tls_legacy(ssl, &ssl->handshake->fin_sha384,
|
|
|
|
hash, hlen);
|
2022-02-17 07:23:47 +01:00
|
|
|
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
2021-12-20 15:22:15 +01:00
|
|
|
}
|
2023-03-17 12:50:01 +01:00
|
|
|
#endif /* MBEDTLS_MD_CAN_SHA384 */
|
2021-12-20 15:22:15 +01:00
|
|
|
|
2022-05-03 17:56:38 +02:00
|
|
|
#if !defined(MBEDTLS_USE_PSA_CRYPTO) && \
|
|
|
|
defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_ssl_psk_derive_premaster(mbedtls_ssl_context *ssl, mbedtls_key_exchange_type_t key_ex)
|
2022-01-12 08:49:48 +01:00
|
|
|
{
|
2022-02-17 07:23:47 +01:00
|
|
|
unsigned char *p = ssl->handshake->premaster;
|
2023-01-11 14:50:10 +01:00
|
|
|
unsigned char *end = p + sizeof(ssl->handshake->premaster);
|
2022-02-17 07:23:47 +01:00
|
|
|
const unsigned char *psk = NULL;
|
|
|
|
size_t psk_len = 0;
|
2023-01-11 14:50:10 +01:00
|
|
|
int psk_ret = mbedtls_ssl_get_psk(ssl, &psk, &psk_len);
|
2022-01-12 08:49:48 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (psk_ret == MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED) {
|
2022-02-17 07:23:47 +01:00
|
|
|
/*
|
|
|
|
* This should never happen because the existence of a PSK is always
|
2022-04-19 12:22:38 +02:00
|
|
|
* checked before calling this function.
|
|
|
|
*
|
|
|
|
* The exception is opaque DHE-PSK. For DHE-PSK fill premaster with
|
2022-04-28 09:16:28 +02:00
|
|
|
* the shared secret without PSK.
|
2022-02-17 07:23:47 +01:00
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
if (key_ex != MBEDTLS_KEY_EXCHANGE_DHE_PSK) {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
|
|
|
|
return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
|
2022-04-19 12:22:38 +02:00
|
|
|
}
|
2022-02-17 07:23:47 +01:00
|
|
|
}
|
2022-01-12 08:49:48 +01:00
|
|
|
|
|
|
|
/*
|
2022-02-17 07:23:47 +01: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
|
2022-01-12 08:49:48 +01:00
|
|
|
*/
|
2022-02-17 07:23:47 +01:00
|
|
|
#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (key_ex == MBEDTLS_KEY_EXCHANGE_PSK) {
|
|
|
|
if (end - p < 2) {
|
|
|
|
return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
|
|
|
|
}
|
2022-02-17 07:23:47 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_PUT_UINT16_BE(psk_len, p, 0);
|
2022-01-12 08:49:48 +01:00
|
|
|
p += 2;
|
2022-02-17 07:23:47 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (end < p || (size_t) (end - p) < psk_len) {
|
|
|
|
return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
|
|
|
|
}
|
2022-02-17 07:23:47 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
memset(p, 0, psk_len);
|
2022-02-17 07:23:47 +01:00
|
|
|
p += psk_len;
|
2023-01-11 14:50:10 +01:00
|
|
|
} else
|
2022-02-17 07:23:47 +01:00
|
|
|
#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
|
|
|
|
#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK) {
|
2022-02-17 07:23:47 +01:00
|
|
|
/*
|
|
|
|
* other_secret already set by the ClientKeyExchange message,
|
|
|
|
* and is 48 bytes long
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
if (end - p < 2) {
|
|
|
|
return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
|
|
|
|
}
|
2022-01-12 08:49:48 +01:00
|
|
|
|
2022-02-17 07:23:47 +01:00
|
|
|
*p++ = 0;
|
|
|
|
*p++ = 48;
|
|
|
|
p += 48;
|
2023-01-11 14:50:10 +01:00
|
|
|
} else
|
2022-02-17 07:23:47 +01:00
|
|
|
#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
|
|
|
|
#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK) {
|
2022-02-17 07:23:47 +01:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
|
|
|
size_t len;
|
|
|
|
|
|
|
|
/* Write length only when we know the actual value */
|
2023-01-11 14:50:10 +01:00
|
|
|
if ((ret = mbedtls_dhm_calc_secret(&ssl->handshake->dhm_ctx,
|
2023-11-04 13:20:09 +01:00
|
|
|
p + 2, (size_t) (end - (p + 2)), &len,
|
2023-01-11 14:50:10 +01:00
|
|
|
ssl->conf->f_rng, ssl->conf->p_rng)) != 0) {
|
|
|
|
MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_dhm_calc_secret", ret);
|
|
|
|
return ret;
|
2022-02-17 07:23:47 +01:00
|
|
|
}
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_PUT_UINT16_BE(len, p, 0);
|
2022-02-17 07:23:47 +01:00
|
|
|
p += 2 + len;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MPI(3, "DHM: K ", &ssl->handshake->dhm_ctx.K);
|
|
|
|
} else
|
2022-02-17 07:23:47 +01:00
|
|
|
#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
|
2022-05-03 17:56:38 +02:00
|
|
|
#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK) {
|
2022-02-17 07:23:47 +01:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
|
|
|
size_t zlen;
|
2022-01-12 08:49:48 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if ((ret = mbedtls_ecdh_calc_secret(&ssl->handshake->ecdh_ctx, &zlen,
|
2023-11-04 13:20:09 +01:00
|
|
|
p + 2, (size_t) (end - (p + 2)),
|
2023-01-11 14:50:10 +01:00
|
|
|
ssl->conf->f_rng, ssl->conf->p_rng)) != 0) {
|
|
|
|
MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecdh_calc_secret", ret);
|
|
|
|
return ret;
|
2022-02-17 07:23:47 +01:00
|
|
|
}
|
2022-01-12 08:49:48 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_PUT_UINT16_BE(zlen, p, 0);
|
2022-02-17 07:23:47 +01:00
|
|
|
p += 2 + zlen;
|
2022-01-12 08:49:48 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_ECDH(3, &ssl->handshake->ecdh_ctx,
|
|
|
|
MBEDTLS_DEBUG_ECDH_Z);
|
|
|
|
} else
|
2022-05-03 17:56:38 +02:00
|
|
|
#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
|
2022-02-17 07:23:47 +01:00
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
|
|
|
|
return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
|
2022-02-17 07:23:47 +01:00
|
|
|
}
|
2022-02-08 13:29:25 +01:00
|
|
|
|
2022-02-17 07:23:47 +01:00
|
|
|
/* opaque psk<0..2^16-1>; */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (end - p < 2) {
|
|
|
|
return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
|
|
|
|
}
|
2021-12-23 13:15:02 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_PUT_UINT16_BE(psk_len, p, 0);
|
2022-02-17 07:23:47 +01:00
|
|
|
p += 2;
|
2021-12-23 13:15:02 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (end < p || (size_t) (end - p) < psk_len) {
|
|
|
|
return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
|
|
|
|
}
|
2021-12-23 13:15:02 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
memcpy(p, psk, psk_len);
|
2022-02-17 07:23:47 +01:00
|
|
|
p += psk_len;
|
2021-12-23 13:15:02 +01:00
|
|
|
|
2023-11-04 13:20:09 +01:00
|
|
|
ssl->handshake->pmslen = (size_t) (p - ssl->handshake->premaster);
|
2021-12-23 13:15:02 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2022-02-17 07:23:47 +01:00
|
|
|
}
|
2022-05-03 17:56:38 +02:00
|
|
|
#endif /* !MBEDTLS_USE_PSA_CRYPTO && MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
|
2021-12-23 13:15:02 +01:00
|
|
|
|
2022-02-17 07:23:47 +01:00
|
|
|
#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
|
2022-06-17 10:52:54 +02:00
|
|
|
MBEDTLS_CHECK_RETURN_CRITICAL
|
2023-01-11 14:50:10 +01:00
|
|
|
static int ssl_write_hello_request(mbedtls_ssl_context *ssl);
|
2022-02-17 07:23:47 +01:00
|
|
|
|
|
|
|
#if defined(MBEDTLS_SSL_PROTO_DTLS)
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_ssl_resend_hello_request(mbedtls_ssl_context *ssl)
|
2022-02-17 07:23:47 +01:00
|
|
|
{
|
|
|
|
/* If renegotiation is not enforced, retransmit until we would reach max
|
|
|
|
* timeout if we were using the usual handshake doubling scheme */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->conf->renego_max_records < 0) {
|
2022-02-17 07:23:47 +01:00
|
|
|
uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
|
|
|
|
unsigned char doublings = 1;
|
2021-12-23 13:15:02 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
while (ratio != 0) {
|
2022-02-17 07:23:47 +01:00
|
|
|
++doublings;
|
|
|
|
ratio >>= 1;
|
|
|
|
}
|
2021-12-23 13:15:02 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (++ssl->renego_records_seen > doublings) {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(2, ("no longer retransmitting hello request"));
|
|
|
|
return 0;
|
2022-02-17 07:23:47 +01:00
|
|
|
}
|
|
|
|
}
|
2021-12-23 13:15:02 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return ssl_write_hello_request(ssl);
|
2022-02-17 07:23:47 +01:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
|
2021-12-23 13:15:02 +01:00
|
|
|
|
2022-02-17 07:23:47 +01:00
|
|
|
/*
|
|
|
|
* Handshake functions
|
|
|
|
*/
|
|
|
|
#if !defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
|
|
|
|
/* No certificate support -> dummy functions */
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_ssl_write_certificate(mbedtls_ssl_context *ssl)
|
2022-02-17 07:23:47 +01:00
|
|
|
{
|
|
|
|
const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
|
|
|
|
ssl->handshake->ciphersuite_info;
|
2021-12-23 13:15:02 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(2, ("=> write certificate"));
|
2021-12-23 13:15:02 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (!mbedtls_ssl_ciphersuite_uses_srv_cert(ciphersuite_info)) {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate"));
|
2022-02-17 07:23:47 +01:00
|
|
|
ssl->state++;
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2022-02-17 07:23:47 +01:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
|
|
|
|
return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
|
2021-12-23 13:15:02 +01:00
|
|
|
}
|
2022-01-12 08:49:48 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_ssl_parse_certificate(mbedtls_ssl_context *ssl)
|
2022-02-17 07:23:47 +01:00
|
|
|
{
|
|
|
|
const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
|
|
|
|
ssl->handshake->ciphersuite_info;
|
2022-02-17 06:44:15 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse certificate"));
|
2022-02-17 06:44:15 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (!mbedtls_ssl_ciphersuite_uses_srv_cert(ciphersuite_info)) {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip parse certificate"));
|
2022-02-17 07:23:47 +01:00
|
|
|
ssl->state++;
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2022-02-17 07:23:47 +01:00
|
|
|
}
|
2022-02-17 06:44:15 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
|
|
|
|
return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
|
2022-02-17 07:23:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#else /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
|
|
|
|
/* Some certificate support -> implement write and parse */
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_ssl_write_certificate(mbedtls_ssl_context *ssl)
|
2022-02-17 06:44:15 +01:00
|
|
|
{
|
2022-02-17 07:23:47 +01:00
|
|
|
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;
|
2022-02-17 06:44:15 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(2, ("=> write certificate"));
|
2022-02-17 06:44:15 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (!mbedtls_ssl_ciphersuite_uses_srv_cert(ciphersuite_info)) {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate"));
|
2022-02-17 07:23:47 +01:00
|
|
|
ssl->state++;
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2022-02-17 07:23:47 +01:00
|
|
|
}
|
2022-02-17 06:44:15 +01:00
|
|
|
|
2022-02-17 07:23:47 +01:00
|
|
|
#if defined(MBEDTLS_SSL_CLI_C)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
|
|
|
|
if (ssl->handshake->client_auth == 0) {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate"));
|
2022-02-17 07:23:47 +01:00
|
|
|
ssl->state++;
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2022-02-17 06:44:15 +01:00
|
|
|
}
|
2022-02-17 07:23:47 +01:00
|
|
|
}
|
|
|
|
#endif /* MBEDTLS_SSL_CLI_C */
|
|
|
|
#if defined(MBEDTLS_SSL_SRV_C)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
|
|
|
|
if (mbedtls_ssl_own_cert(ssl) == NULL) {
|
2022-02-17 07:23:47 +01:00
|
|
|
/* Should never happen because we shouldn't have picked the
|
|
|
|
* ciphersuite if we don't have a certificate. */
|
2023-01-11 14:50:10 +01:00
|
|
|
return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
|
2022-02-17 06:44:15 +01:00
|
|
|
}
|
2022-02-17 07:23:47 +01:00
|
|
|
}
|
|
|
|
#endif
|
2022-02-17 06:44:15 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_CRT(3, "own certificate", mbedtls_ssl_own_cert(ssl));
|
2022-02-17 07:23:47 +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;
|
2023-01-11 14:50:10 +01:00
|
|
|
crt = mbedtls_ssl_own_cert(ssl);
|
2022-02-17 07:23:47 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
while (crt != NULL) {
|
2022-02-17 07:23:47 +01:00
|
|
|
n = crt->raw.len;
|
2023-01-11 14:50:10 +01:00
|
|
|
if (n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i) {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(1, ("certificate too large, %" MBEDTLS_PRINTF_SIZET
|
|
|
|
" > %" MBEDTLS_PRINTF_SIZET,
|
|
|
|
i + 3 + n, (size_t) MBEDTLS_SSL_OUT_CONTENT_LEN));
|
|
|
|
return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
|
2022-02-17 07:23:47 +01:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
ssl->out_msg[i] = MBEDTLS_BYTE_2(n);
|
|
|
|
ssl->out_msg[i + 1] = MBEDTLS_BYTE_1(n);
|
|
|
|
ssl->out_msg[i + 2] = MBEDTLS_BYTE_0(n);
|
2022-02-17 07:23:47 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
i += 3; memcpy(ssl->out_msg + i, crt->raw.p, n);
|
2022-02-17 07:23:47 +01:00
|
|
|
i += n; crt = crt->next;
|
2022-02-17 06:44:15 +01:00
|
|
|
}
|
2022-02-17 07:23:47 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
ssl->out_msg[4] = MBEDTLS_BYTE_2(i - 7);
|
|
|
|
ssl->out_msg[5] = MBEDTLS_BYTE_1(i - 7);
|
|
|
|
ssl->out_msg[6] = MBEDTLS_BYTE_0(i - 7);
|
2022-02-17 07:23:47 +01:00
|
|
|
|
|
|
|
ssl->out_msglen = i;
|
|
|
|
ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
|
|
|
|
ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
|
|
|
|
|
|
|
|
ssl->state++;
|
|
|
|
|
2023-01-11 14:50: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;
|
2022-02-17 06:44:15 +01:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(2, ("<= write certificate"));
|
2022-02-17 06:44:15 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return ret;
|
2022-02-17 06:44:15 +01:00
|
|
|
}
|
|
|
|
|
2022-02-17 07:23:47 +01:00
|
|
|
#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
|
2022-02-17 06:44:15 +01:00
|
|
|
|
2022-02-17 07:23:47 +01:00
|
|
|
#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
|
2022-06-17 10:52:54 +02:00
|
|
|
MBEDTLS_CHECK_RETURN_CRITICAL
|
2023-01-11 14:50:10 +01:00
|
|
|
static int ssl_check_peer_crt_unchanged(mbedtls_ssl_context *ssl,
|
|
|
|
unsigned char *crt_buf,
|
|
|
|
size_t crt_buf_len)
|
2022-02-17 07:23:47 +01:00
|
|
|
{
|
|
|
|
mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert;
|
2022-02-17 06:44:15 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (peer_crt == NULL) {
|
|
|
|
return -1;
|
|
|
|
}
|
2022-02-17 06:44:15 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (peer_crt->raw.len != crt_buf_len) {
|
|
|
|
return -1;
|
|
|
|
}
|
2022-02-17 06:44:15 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return memcmp(peer_crt->raw.p, crt_buf, peer_crt->raw.len);
|
2022-02-17 07:23:47 +01:00
|
|
|
}
|
|
|
|
#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
|
2022-06-17 10:52:54 +02:00
|
|
|
MBEDTLS_CHECK_RETURN_CRITICAL
|
2023-01-11 14:50:10 +01:00
|
|
|
static int ssl_check_peer_crt_unchanged(mbedtls_ssl_context *ssl,
|
|
|
|
unsigned char *crt_buf,
|
|
|
|
size_t crt_buf_len)
|
2022-02-17 07:23:47 +01:00
|
|
|
{
|
|
|
|
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 =
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_md_info_from_type(peer_cert_digest_type);
|
2022-02-17 07:23:47 +01:00
|
|
|
unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN];
|
|
|
|
size_t digest_len;
|
2022-02-17 06:44:15 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (peer_cert_digest == NULL || digest_info == NULL) {
|
|
|
|
return -1;
|
|
|
|
}
|
2022-02-17 06:44:15 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
digest_len = mbedtls_md_get_size(digest_info);
|
|
|
|
if (digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN) {
|
|
|
|
return -1;
|
|
|
|
}
|
2022-02-17 06:44:15 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = mbedtls_md(digest_info, crt_buf, crt_buf_len, tmp_digest);
|
|
|
|
if (ret != 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
2022-02-17 06:44:15 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return memcmp(tmp_digest, peer_cert_digest, digest_len);
|
2022-02-17 06:44:15 +01:00
|
|
|
}
|
2022-02-17 07:23:47 +01:00
|
|
|
#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
|
|
|
|
#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
|
2022-02-17 06:44:15 +01:00
|
|
|
|
2022-02-17 07:23:47 +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
|
|
|
|
*/
|
2022-06-17 10:52:54 +02:00
|
|
|
MBEDTLS_CHECK_RETURN_CRITICAL
|
2023-01-11 14:50:10 +01:00
|
|
|
static int ssl_parse_certificate_chain(mbedtls_ssl_context *ssl,
|
|
|
|
mbedtls_x509_crt *chain)
|
2022-02-17 06:44:15 +01:00
|
|
|
{
|
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2022-02-17 07:23:47 +01:00
|
|
|
#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
|
2023-01-11 14:50:10 +01:00
|
|
|
int crt_cnt = 0;
|
2022-02-17 07:23:47 +01:00
|
|
|
#endif
|
|
|
|
size_t i, n;
|
|
|
|
uint8_t alert;
|
2022-02-17 06:44:15 +01:00
|
|
|
|
2023-01-11 14:50: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;
|
2022-02-17 07:23:47 +01:00
|
|
|
}
|
2022-02-17 06:44:15 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE) {
|
|
|
|
mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
|
|
|
|
MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
|
|
|
|
return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
|
2022-02-17 07:23:47 +01:00
|
|
|
}
|
2022-02-17 06:44:15 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->in_hslen < mbedtls_ssl_hs_hdr_len(ssl) + 3 + 3) {
|
|
|
|
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_DECODE_ERROR;
|
2022-02-17 06:44:15 +01:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
i = mbedtls_ssl_hs_hdr_len(ssl);
|
2022-02-17 06:44:15 +01:00
|
|
|
|
|
|
|
/*
|
2022-02-17 07:23:47 +01:00
|
|
|
* Same message structure as in mbedtls_ssl_write_certificate()
|
2022-02-17 06:44:15 +01:00
|
|
|
*/
|
2023-11-04 00:34:02 +01:00
|
|
|
n = MBEDTLS_GET_UINT16_BE(ssl->in_msg, i + 1);
|
2022-02-17 06:44:15 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->in_msg[i] != 0 ||
|
|
|
|
ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len(ssl)) {
|
|
|
|
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_DECODE_ERROR;
|
2022-02-17 07:23:47 +01:00
|
|
|
}
|
2022-02-17 06:44:15 +01:00
|
|
|
|
2022-02-17 07:23:47 +01:00
|
|
|
/* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */
|
|
|
|
i += 3;
|
2022-02-17 06:44:15 +01:00
|
|
|
|
2022-02-17 07:23:47 +01:00
|
|
|
/* Iterate through and parse the CRTs in the provided chain. */
|
2023-01-11 14:50:10 +01:00
|
|
|
while (i < ssl->in_hslen) {
|
2022-02-17 07:23:47 +01:00
|
|
|
/* Check that there's room for the next CRT's length fields. */
|
2023-01-11 14:50:10 +01:00
|
|
|
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_DECODE_ERROR;
|
2022-02-17 07:23:47 +01:00
|
|
|
}
|
|
|
|
/* In theory, the CRT can be up to 2**24 Bytes, but we don't support
|
|
|
|
* anything beyond 2**16 ~ 64K. */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->in_msg[i] != 0) {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
|
|
|
|
mbedtls_ssl_send_alert_message(ssl,
|
|
|
|
MBEDTLS_SSL_ALERT_LEVEL_FATAL,
|
|
|
|
MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT);
|
|
|
|
return MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
|
2022-02-17 07:23:47 +01:00
|
|
|
}
|
2022-02-17 06:44:15 +01:00
|
|
|
|
2022-02-17 07:23:47 +01:00
|
|
|
/* Read length of the next CRT in the chain. */
|
2023-11-04 00:34:02 +01:00
|
|
|
n = MBEDTLS_GET_UINT16_BE(ssl->in_msg, i + 1);
|
2022-02-17 07:23:47 +01:00
|
|
|
i += 3;
|
2022-02-17 06:44:15 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (n < 128 || i + n > 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_DECODE_ERROR;
|
2022-02-17 07:23:47 +01:00
|
|
|
}
|
2022-02-17 06:44:15 +01:00
|
|
|
|
2022-02-17 07:23:47 +01:00
|
|
|
/* Check if we're handling the first CRT in the chain. */
|
|
|
|
#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (crt_cnt++ == 0 &&
|
2022-02-17 07:23:47 +01:00
|
|
|
ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
|
2023-01-11 14:50:10 +01:00
|
|
|
ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS) {
|
2022-02-17 07:23:47 +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. */
|
2023-01-11 14:50:10 +01:00
|
|
|
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) {
|
|
|
|
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_CERTIFICATE;
|
2022-02-17 07:23:47 +01:00
|
|
|
}
|
2022-02-17 06:44:15 +01:00
|
|
|
|
2022-02-17 07:23:47 +01:00
|
|
|
/* Now we can safely free the original chain. */
|
2023-01-11 14:50:10 +01:00
|
|
|
ssl_clear_peer_cert(ssl->session);
|
2022-02-17 07:23:47 +01:00
|
|
|
}
|
|
|
|
#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
|
2022-02-17 06:44:15 +01:00
|
|
|
|
2022-02-17 07:23:47 +01:00
|
|
|
/* Parse the next certificate in the chain. */
|
|
|
|
#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = mbedtls_x509_crt_parse_der(chain, ssl->in_msg + i, n);
|
2022-02-17 07:23:47 +01:00
|
|
|
#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. */
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = mbedtls_x509_crt_parse_der_nocopy(chain, ssl->in_msg + i, n);
|
2022-02-17 07:23:47 +01:00
|
|
|
#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
|
2023-01-11 14:50:10 +01:00
|
|
|
switch (ret) {
|
2022-02-17 07:23:47 +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;
|
2022-02-17 06:44:15 +01:00
|
|
|
|
2022-02-17 07:23:47 +01:00
|
|
|
case MBEDTLS_ERR_X509_ALLOC_FAILED:
|
|
|
|
alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
|
|
|
|
goto crt_parse_der_failed;
|
2022-02-17 06:44:15 +01:00
|
|
|
|
2022-02-17 07:23:47 +01:00
|
|
|
case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
|
|
|
|
alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
|
|
|
|
goto crt_parse_der_failed;
|
2022-02-17 06:44:15 +01:00
|
|
|
|
2022-02-17 07:23:47 +01:00
|
|
|
default:
|
|
|
|
alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
|
2023-01-11 14:50:10 +01:00
|
|
|
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;
|
2022-02-17 07:23:47 +01:00
|
|
|
}
|
2022-02-17 07:01:37 +01:00
|
|
|
|
2022-02-17 07:23:47 +01:00
|
|
|
i += n;
|
2022-02-17 07:01:37 +01:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_CRT(3, "peer certificate", chain);
|
|
|
|
return 0;
|
2022-02-17 07:01:37 +01:00
|
|
|
}
|
2022-02-17 07:03:43 +01:00
|
|
|
|
2022-02-17 07:23:47 +01:00
|
|
|
#if defined(MBEDTLS_SSL_SRV_C)
|
2022-06-17 10:52:54 +02:00
|
|
|
MBEDTLS_CHECK_RETURN_CRITICAL
|
2023-01-11 14:50:10 +01:00
|
|
|
static int ssl_srv_check_client_no_crt_notification(mbedtls_ssl_context *ssl)
|
2022-02-17 07:03:43 +01:00
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
|
|
|
|
return -1;
|
|
|
|
}
|
2022-02-17 07:03:43 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len(ssl) &&
|
2022-02-17 07:23:47 +01:00
|
|
|
ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
|
|
|
|
ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
|
2023-01-11 14:50:10 +01:00
|
|
|
memcmp(ssl->in_msg + mbedtls_ssl_hs_hdr_len(ssl), "\0\0\0", 3) == 0) {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(1, ("peer has no certificate"));
|
|
|
|
return 0;
|
2022-02-17 07:03:43 +01:00
|
|
|
}
|
2023-01-11 14:50:10 +01:00
|
|
|
return -1;
|
2022-02-17 07:03:43 +01:00
|
|
|
}
|
2022-02-17 07:23:47 +01:00
|
|
|
#endif /* MBEDTLS_SSL_SRV_C */
|
2022-02-17 07:03:43 +01:00
|
|
|
|
2022-02-17 07:23:47 +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.
|
2022-02-17 07:07:00 +01:00
|
|
|
*/
|
2022-02-17 07:23:47 +01:00
|
|
|
#define SSL_CERTIFICATE_EXPECTED 0
|
|
|
|
#define SSL_CERTIFICATE_SKIP 1
|
2022-06-17 10:52:54 +02:00
|
|
|
MBEDTLS_CHECK_RETURN_CRITICAL
|
2023-01-11 14:50:10 +01:00
|
|
|
static int ssl_parse_certificate_coordinate(mbedtls_ssl_context *ssl,
|
|
|
|
int authmode)
|
2022-02-17 07:07:00 +01:00
|
|
|
{
|
2022-02-17 07:23:47 +01:00
|
|
|
const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
|
|
|
|
ssl->handshake->ciphersuite_info;
|
2022-02-17 07:07:00 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (!mbedtls_ssl_ciphersuite_uses_srv_cert(ciphersuite_info)) {
|
|
|
|
return SSL_CERTIFICATE_SKIP;
|
|
|
|
}
|
2022-02-17 07:07:00 +01:00
|
|
|
|
2022-02-17 07:23:47 +01:00
|
|
|
#if defined(MBEDTLS_SSL_SRV_C)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
|
|
|
|
if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK) {
|
|
|
|
return SSL_CERTIFICATE_SKIP;
|
|
|
|
}
|
2022-02-17 07:07:00 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (authmode == MBEDTLS_SSL_VERIFY_NONE) {
|
2022-02-17 07:23:47 +01:00
|
|
|
ssl->session_negotiate->verify_result =
|
|
|
|
MBEDTLS_X509_BADCERT_SKIP_VERIFY;
|
2023-01-11 14:50:10 +01:00
|
|
|
return SSL_CERTIFICATE_SKIP;
|
2022-02-17 07:23:47 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
((void) authmode);
|
|
|
|
#endif /* MBEDTLS_SSL_SRV_C */
|
2022-02-17 07:07:00 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return SSL_CERTIFICATE_EXPECTED;
|
2022-02-17 07:23:47 +01:00
|
|
|
}
|
2022-02-17 07:07:00 +01:00
|
|
|
|
2022-06-17 10:52:54 +02:00
|
|
|
MBEDTLS_CHECK_RETURN_CRITICAL
|
2023-01-11 14:50:10 +01:00
|
|
|
static int ssl_parse_certificate_verify(mbedtls_ssl_context *ssl,
|
|
|
|
int authmode,
|
|
|
|
mbedtls_x509_crt *chain,
|
|
|
|
void *rs_ctx)
|
2022-02-17 07:23:47 +01:00
|
|
|
{
|
|
|
|
int ret = 0;
|
|
|
|
const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
|
|
|
|
ssl->handshake->ciphersuite_info;
|
|
|
|
int have_ca_chain = 0;
|
2022-02-17 07:07:00 +01:00
|
|
|
|
2022-02-17 07:23:47 +01:00
|
|
|
int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *);
|
|
|
|
void *p_vrfy;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (authmode == MBEDTLS_SSL_VERIFY_NONE) {
|
|
|
|
return 0;
|
|
|
|
}
|
2022-02-17 07:07:00 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->f_vrfy != NULL) {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(3, ("Use context-specific verification callback"));
|
2022-02-17 07:23:47 +01:00
|
|
|
f_vrfy = ssl->f_vrfy;
|
|
|
|
p_vrfy = ssl->p_vrfy;
|
2023-01-11 14:50:10 +01:00
|
|
|
} else {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(3, ("Use configuration-specific verification callback"));
|
2022-02-17 07:23:47 +01:00
|
|
|
f_vrfy = ssl->conf->f_vrfy;
|
|
|
|
p_vrfy = ssl->conf->p_vrfy;
|
|
|
|
}
|
2022-02-17 07:07:00 +01:00
|
|
|
|
2022-02-17 07:23:47 +01:00
|
|
|
/*
|
|
|
|
* Main check: verify certificate
|
|
|
|
*/
|
|
|
|
#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->conf->f_ca_cb != NULL) {
|
2022-02-17 07:23:47 +01:00
|
|
|
((void) rs_ctx);
|
|
|
|
have_ca_chain = 1;
|
2022-02-17 07:07:00 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(3, ("use CA callback for X.509 CRT verification"));
|
2022-02-17 07:23:47 +01:00
|
|
|
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,
|
2023-01-11 14:50:10 +01:00
|
|
|
f_vrfy, p_vrfy);
|
|
|
|
} else
|
2022-02-17 07:23:47 +01:00
|
|
|
#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
|
2022-02-17 07:07:00 +01:00
|
|
|
{
|
2022-02-17 07:23:47 +01:00
|
|
|
mbedtls_x509_crt *ca_chain;
|
|
|
|
mbedtls_x509_crl *ca_crl;
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->handshake->sni_ca_chain != NULL) {
|
2022-02-17 07:23:47 +01:00
|
|
|
ca_chain = ssl->handshake->sni_ca_chain;
|
|
|
|
ca_crl = ssl->handshake->sni_ca_crl;
|
2023-01-11 14:50:10 +01:00
|
|
|
} else
|
2022-02-17 07:23:47 +01:00
|
|
|
#endif
|
|
|
|
{
|
|
|
|
ca_chain = ssl->conf->ca_chain;
|
|
|
|
ca_crl = ssl->conf->ca_crl;
|
2022-02-17 07:07:00 +01:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ca_chain != NULL) {
|
2022-02-17 07:23:47 +01:00
|
|
|
have_ca_chain = 1;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2022-02-17 07:23:47 +01:00
|
|
|
|
|
|
|
ret = mbedtls_x509_crt_verify_restartable(
|
|
|
|
chain,
|
|
|
|
ca_chain, ca_crl,
|
|
|
|
ssl->conf->cert_profile,
|
|
|
|
ssl->hostname,
|
|
|
|
&ssl->session_negotiate->verify_result,
|
2023-01-11 14:50:10 +01:00
|
|
|
f_vrfy, p_vrfy, rs_ctx);
|
2022-02-17 07:07:00 +01:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ret != 0) {
|
|
|
|
MBEDTLS_SSL_DEBUG_RET(1, "x509_verify_cert", ret);
|
2022-02-17 07:23:47 +01:00
|
|
|
}
|
2022-02-17 07:07:00 +01:00
|
|
|
|
2022-02-17 07:23:47 +01:00
|
|
|
#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
|
|
|
|
return MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
|
|
|
|
}
|
2022-02-17 07:23:47 +01:00
|
|
|
#endif
|
2022-02-17 07:09:02 +01:00
|
|
|
|
2022-02-17 07:23:47 +01:00
|
|
|
/*
|
|
|
|
* Secondary checks: always done, but change 'ret' only if it was 0
|
|
|
|
*/
|
2022-02-17 07:09:02 +01:00
|
|
|
|
2023-06-29 18:06:29 +02:00
|
|
|
#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
|
2022-02-17 07:09:02 +01:00
|
|
|
{
|
2022-02-17 07:23:47 +01:00
|
|
|
const mbedtls_pk_context *pk = &chain->pk;
|
|
|
|
|
2022-06-17 10:49:29 +02:00
|
|
|
/* If certificate uses an EC key, make sure the curve is OK.
|
|
|
|
* This is a public key, so it can't be opaque, so can_do() is a good
|
|
|
|
* enough check to ensure pk_ec() is safe to use here. */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (mbedtls_pk_can_do(pk, MBEDTLS_PK_ECKEY)) {
|
2022-08-09 01:52:38 +02:00
|
|
|
/* and in the unlikely case the above assumption no longer holds
|
|
|
|
* we are making sure that pk_ec() here does not return a NULL
|
|
|
|
*/
|
2023-11-29 08:42:27 +01:00
|
|
|
mbedtls_ecp_group_id grp_id = mbedtls_pk_get_ec_group_id(pk);
|
2023-05-24 13:16:40 +02:00
|
|
|
if (grp_id == MBEDTLS_ECP_DP_NONE) {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(1, ("invalid group ID"));
|
2023-01-11 14:50:10 +01:00
|
|
|
return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
|
2022-08-09 01:52:38 +02:00
|
|
|
}
|
2023-05-18 18:59:06 +02:00
|
|
|
if (mbedtls_ssl_check_curve(ssl, grp_id) != 0) {
|
2022-08-09 01:52:38 +02:00
|
|
|
ssl->session_negotiate->verify_result |=
|
|
|
|
MBEDTLS_X509_BADCERT_BAD_KEY;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate (EC key curve)"));
|
|
|
|
if (ret == 0) {
|
2022-08-09 01:52:38 +02:00
|
|
|
ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2022-08-09 01:52:38 +02:00
|
|
|
}
|
2022-02-17 07:23:47 +01:00
|
|
|
}
|
2022-02-17 07:09:02 +01:00
|
|
|
}
|
2023-06-29 18:06:29 +02:00
|
|
|
#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
|
2022-02-17 07:09:02 +01:00
|
|
|
|
2023-01-11 14:50: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) {
|
2022-02-17 07:23:47 +01:00
|
|
|
ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2022-02-17 07:09:02 +01:00
|
|
|
}
|
|
|
|
|
2022-02-17 07:23:47 +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. */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
|
|
|
|
(ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
|
|
|
|
ret == MBEDTLS_ERR_SSL_BAD_CERTIFICATE)) {
|
2022-02-17 07:23:47 +01:00
|
|
|
ret = 0;
|
2022-02-17 07:09:02 +01:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (have_ca_chain == 0 && authmode == MBEDTLS_SSL_VERIFY_REQUIRED) {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(1, ("got no CA chain"));
|
2022-02-17 07:23:47 +01:00
|
|
|
ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
|
2022-02-17 07:09:02 +01:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ret != 0) {
|
2022-02-17 07:23:47 +01:00
|
|
|
uint8_t alert;
|
2022-02-17 07:10:24 +01:00
|
|
|
|
2022-02-17 07:23:47 +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. */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER) {
|
2022-02-17 07:23:47 +01:00
|
|
|
alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
|
2023-01-11 14:50:10 +01:00
|
|
|
} else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH) {
|
2022-02-17 07:23:47 +01:00
|
|
|
alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
|
2023-01-11 14:50:10 +01:00
|
|
|
} else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE) {
|
2022-02-17 07:23:47 +01:00
|
|
|
alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
|
2023-01-11 14:50:10 +01:00
|
|
|
} else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE) {
|
2022-02-17 07:23:47 +01:00
|
|
|
alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
|
2023-01-11 14:50:10 +01:00
|
|
|
} else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE) {
|
2022-02-17 07:23:47 +01:00
|
|
|
alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
|
2023-01-11 14:50:10 +01:00
|
|
|
} else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK) {
|
2022-02-17 07:23:47 +01:00
|
|
|
alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
|
2023-01-11 14:50:10 +01:00
|
|
|
} else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY) {
|
2022-02-17 07:23:47 +01:00
|
|
|
alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
|
2023-01-11 14:50:10 +01:00
|
|
|
} else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED) {
|
2022-02-17 07:23:47 +01:00
|
|
|
alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
|
2023-01-11 14:50:10 +01:00
|
|
|
} else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED) {
|
2022-02-17 07:23:47 +01:00
|
|
|
alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
|
2023-01-11 14:50:10 +01:00
|
|
|
} else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED) {
|
2022-02-17 07:23:47 +01:00
|
|
|
alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
|
2023-01-11 14:50:10 +01:00
|
|
|
} else {
|
2022-02-17 07:23:47 +01:00
|
|
|
alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
|
|
|
mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
|
|
|
|
alert);
|
2022-02-17 07:23:47 +01:00
|
|
|
}
|
2022-02-17 07:10:24 +01:00
|
|
|
|
2022-02-17 07:23:47 +01:00
|
|
|
#if defined(MBEDTLS_DEBUG_C)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->session_negotiate->verify_result != 0) {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(3, ("! Certificate verification flags %08x",
|
|
|
|
(unsigned int) ssl->session_negotiate->verify_result));
|
|
|
|
} else {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(3, ("Certificate verification flags clear"));
|
2022-02-17 07:10:24 +01:00
|
|
|
}
|
2022-02-17 07:23:47 +01:00
|
|
|
#endif /* MBEDTLS_DEBUG_C */
|
2022-02-17 07:10:24 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return ret;
|
2022-02-17 07:10:24 +01:00
|
|
|
}
|
|
|
|
|
2022-02-17 07:23:47 +01:00
|
|
|
#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
|
2022-06-17 10:52:54 +02:00
|
|
|
MBEDTLS_CHECK_RETURN_CRITICAL
|
2023-01-11 14:50:10 +01:00
|
|
|
static int ssl_remember_peer_crt_digest(mbedtls_ssl_context *ssl,
|
|
|
|
unsigned char *start, size_t len)
|
2022-02-17 07:13:48 +01:00
|
|
|
{
|
2022-02-17 07:23:47 +01:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
|
|
|
/* Remember digest of the peer's end-CRT. */
|
|
|
|
ssl->session_negotiate->peer_cert_digest =
|
2023-01-11 14:50:10 +01:00
|
|
|
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);
|
2022-02-17 07:13:48 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return MBEDTLS_ERR_SSL_ALLOC_FAILED;
|
2022-02-17 07:13:48 +01:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50: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);
|
2022-02-17 07:13:48 +01:00
|
|
|
|
2022-02-17 07:23:47 +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;
|
2022-02-17 07:13:48 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return ret;
|
2022-02-17 07:13:48 +01:00
|
|
|
}
|
|
|
|
|
2022-06-17 10:52:54 +02:00
|
|
|
MBEDTLS_CHECK_RETURN_CRITICAL
|
2023-01-11 14:50:10 +01:00
|
|
|
static int ssl_remember_peer_pubkey(mbedtls_ssl_context *ssl,
|
|
|
|
unsigned char *start, size_t len)
|
2022-02-17 07:16:37 +01:00
|
|
|
{
|
2022-02-17 07:23:47 +01:00
|
|
|
unsigned char *end = start + len;
|
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2022-02-17 07:16:37 +01:00
|
|
|
|
2022-02-17 07:23:47 +01:00
|
|
|
/* Make a copy of the peer's raw public key. */
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_pk_init(&ssl->handshake->peer_pubkey);
|
|
|
|
ret = mbedtls_pk_parse_subpubkey(&start, end,
|
|
|
|
&ssl->handshake->peer_pubkey);
|
|
|
|
if (ret != 0) {
|
2022-02-17 07:23:47 +01:00
|
|
|
/* We should have parsed the public key before. */
|
2023-01-11 14:50:10 +01:00
|
|
|
return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
|
2022-02-17 07:16:37 +01:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2022-02-17 07:23:47 +01:00
|
|
|
}
|
|
|
|
#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
|
2022-02-17 07:16:37 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_ssl_parse_certificate(mbedtls_ssl_context *ssl)
|
2022-02-17 07:23:47 +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;
|
|
|
|
#endif
|
|
|
|
void *rs_ctx = NULL;
|
|
|
|
mbedtls_x509_crt *chain = NULL;
|
2022-02-17 07:16:37 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse certificate"));
|
2022-02-17 07:16:37 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
crt_expected = ssl_parse_certificate_coordinate(ssl, authmode);
|
|
|
|
if (crt_expected == SSL_CERTIFICATE_SKIP) {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip parse certificate"));
|
2022-02-17 07:23:47 +01:00
|
|
|
goto exit;
|
2022-02-17 07:16:37 +01:00
|
|
|
}
|
2022-02-17 07:23:47 +01:00
|
|
|
|
|
|
|
#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->handshake->ecrs_enabled &&
|
|
|
|
ssl->handshake->ecrs_state == ssl_ecrs_crt_verify) {
|
2022-02-17 07:23:47 +01:00
|
|
|
chain = ssl->handshake->ecrs_peer_cert;
|
|
|
|
ssl->handshake->ecrs_peer_cert = NULL;
|
|
|
|
goto crt_verify;
|
|
|
|
}
|
|
|
|
#endif
|
2022-02-17 07:16:37 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) {
|
2022-02-17 07:23:47 +01:00
|
|
|
/* mbedtls_ssl_read_record may have sent an alert already. We
|
|
|
|
let it decide whether to alert. */
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
|
2022-02-17 07:23:47 +01:00
|
|
|
goto exit;
|
2022-02-17 07:16:37 +01:00
|
|
|
}
|
2022-02-17 07:23:47 +01:00
|
|
|
|
|
|
|
#if defined(MBEDTLS_SSL_SRV_C)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl_srv_check_client_no_crt_notification(ssl) == 0) {
|
2022-02-17 07:23:47 +01:00
|
|
|
ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
|
2022-02-17 07:16:37 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (authmode != MBEDTLS_SSL_VERIFY_OPTIONAL) {
|
2022-02-17 07:23:47 +01:00
|
|
|
ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2022-02-17 07:16:37 +01:00
|
|
|
|
2022-02-17 07:23:47 +01:00
|
|
|
goto exit;
|
2022-02-17 07:16:37 +01:00
|
|
|
}
|
2022-02-17 07:23:47 +01:00
|
|
|
#endif /* MBEDTLS_SSL_SRV_C */
|
2022-02-17 07:16:37 +01:00
|
|
|
|
2022-02-17 07:23:47 +01:00
|
|
|
/* Clear existing peer CRT structure in case we tried to
|
|
|
|
* reuse a session but it failed, and allocate a new one. */
|
2023-01-11 14:50:10 +01:00
|
|
|
ssl_clear_peer_cert(ssl->session_negotiate);
|
2022-02-17 07:16:37 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
chain = mbedtls_calloc(1, sizeof(mbedtls_x509_crt));
|
|
|
|
if (chain == NULL) {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(1, ("alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed",
|
|
|
|
sizeof(mbedtls_x509_crt)));
|
|
|
|
mbedtls_ssl_send_alert_message(ssl,
|
|
|
|
MBEDTLS_SSL_ALERT_LEVEL_FATAL,
|
|
|
|
MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
|
2022-02-17 07:16:37 +01:00
|
|
|
|
2022-02-17 07:23:47 +01:00
|
|
|
ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
|
|
|
|
goto exit;
|
2022-02-17 07:16:37 +01:00
|
|
|
}
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_x509_crt_init(chain);
|
2022-02-17 07:23:47 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = ssl_parse_certificate_chain(ssl, chain);
|
|
|
|
if (ret != 0) {
|
2022-02-17 07:23:47 +01:00
|
|
|
goto exit;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2022-02-17 07:23:47 +01:00
|
|
|
|
|
|
|
#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->handshake->ecrs_enabled) {
|
2022-02-17 07:23:47 +01:00
|
|
|
ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2022-02-17 07:23:47 +01:00
|
|
|
|
|
|
|
crt_verify:
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->handshake->ecrs_enabled) {
|
2022-02-17 07:23:47 +01:00
|
|
|
rs_ctx = &ssl->handshake->ecrs_ctx;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2022-02-17 07:23:47 +01:00
|
|
|
#endif
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = ssl_parse_certificate_verify(ssl, authmode,
|
|
|
|
chain, rs_ctx);
|
|
|
|
if (ret != 0) {
|
2022-02-17 07:23:47 +01:00
|
|
|
goto exit;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2022-02-17 07:23:47 +01:00
|
|
|
|
|
|
|
#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
|
2022-02-17 07:16:37 +01:00
|
|
|
{
|
2022-02-17 07:23:47 +01:00
|
|
|
unsigned char *crt_start, *pk_start;
|
|
|
|
size_t crt_len, pk_len;
|
2022-02-17 07:16:37 +01:00
|
|
|
|
2022-02-17 07:23:47 +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. */
|
2022-02-17 07:16:37 +01:00
|
|
|
|
2022-02-17 07:23:47 +01:00
|
|
|
crt_start = chain->raw.p;
|
|
|
|
crt_len = chain->raw.len;
|
2022-02-17 07:16:37 +01:00
|
|
|
|
2022-02-17 07:23:47 +01:00
|
|
|
pk_start = chain->pk_raw.p;
|
|
|
|
pk_len = chain->pk_raw.len;
|
2022-02-17 07:16:37 +01:00
|
|
|
|
2022-02-17 07:23:47 +01:00
|
|
|
/* Free the CRT structures before computing
|
|
|
|
* digest and copying the peer's public key. */
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_x509_crt_free(chain);
|
|
|
|
mbedtls_free(chain);
|
2022-02-17 07:23:47 +01:00
|
|
|
chain = NULL;
|
2022-02-17 07:16:37 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = ssl_remember_peer_crt_digest(ssl, crt_start, crt_len);
|
|
|
|
if (ret != 0) {
|
2022-02-17 07:23:47 +01:00
|
|
|
goto exit;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2022-02-17 07:16:37 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = ssl_remember_peer_pubkey(ssl, pk_start, pk_len);
|
|
|
|
if (ret != 0) {
|
2022-02-17 07:23:47 +01:00
|
|
|
goto exit;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2022-02-17 07:23:47 +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 */
|
2022-02-17 07:20:39 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse certificate"));
|
2022-02-17 07:20:39 +01:00
|
|
|
|
2022-02-17 07:23:47 +01:00
|
|
|
exit:
|
2022-02-17 07:20:39 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ret == 0) {
|
2022-02-17 07:23:47 +01:00
|
|
|
ssl->state++;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2022-02-17 07:20:39 +01:00
|
|
|
|
2022-02-17 07:23:47 +01:00
|
|
|
#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS) {
|
2022-02-17 07:23:47 +01:00
|
|
|
ssl->handshake->ecrs_peer_cert = chain;
|
|
|
|
chain = NULL;
|
2022-02-17 07:20:39 +01:00
|
|
|
}
|
2022-02-17 07:23:47 +01:00
|
|
|
#endif
|
2022-02-17 07:20:39 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (chain != NULL) {
|
|
|
|
mbedtls_x509_crt_free(chain);
|
|
|
|
mbedtls_free(chain);
|
2022-02-17 07:38:28 +01:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return ret;
|
2022-02-17 07:38:28 +01:00
|
|
|
}
|
|
|
|
#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
|
|
|
|
|
2023-06-24 10:13:41 +02:00
|
|
|
static int ssl_calc_finished_tls_generic(mbedtls_ssl_context *ssl, void *ctx,
|
|
|
|
unsigned char *padbuf, size_t hlen,
|
|
|
|
unsigned char *buf, int from)
|
2022-02-17 07:38:28 +01:00
|
|
|
{
|
2023-11-04 00:36:06 +01:00
|
|
|
unsigned int len = 12;
|
2022-02-17 07:38:28 +01:00
|
|
|
const char *sender;
|
|
|
|
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
|
|
|
psa_status_t status;
|
2023-06-24 10:13:41 +02:00
|
|
|
psa_hash_operation_t *hs_op = ctx;
|
2023-07-04 11:35:16 +02:00
|
|
|
psa_hash_operation_t cloned_op = PSA_HASH_OPERATION_INIT;
|
2023-06-24 10:13:41 +02:00
|
|
|
size_t hash_size;
|
2022-02-17 07:38:28 +01:00
|
|
|
#else
|
2023-02-06 10:14:25 +01:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2023-06-24 10:13:41 +02:00
|
|
|
mbedtls_md_context_t *hs_ctx = ctx;
|
2023-07-04 11:35:16 +02:00
|
|
|
mbedtls_md_context_t cloned_ctx;
|
|
|
|
mbedtls_md_init(&cloned_ctx);
|
2022-02-17 07:38:28 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
mbedtls_ssl_session *session = ssl->session_negotiate;
|
2023-01-11 14:50:10 +01:00
|
|
|
if (!session) {
|
2022-02-17 07:38:28 +01:00
|
|
|
session = ssl->session;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2022-02-17 07:38:28 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
sender = (from == MBEDTLS_SSL_IS_CLIENT)
|
2022-02-17 07:38:28 +01:00
|
|
|
? "client finished"
|
|
|
|
: "server finished";
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
2023-06-24 10:13:41 +02:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc PSA finished tls"));
|
2022-02-17 07:38:28 +01:00
|
|
|
|
2023-07-04 11:35:16 +02:00
|
|
|
status = psa_hash_clone(hs_op, &cloned_op);
|
2023-01-11 14:50:10 +01:00
|
|
|
if (status != PSA_SUCCESS) {
|
2023-02-06 10:14:25 +01:00
|
|
|
goto exit;
|
2022-02-17 07:38:28 +01:00
|
|
|
}
|
|
|
|
|
2023-07-04 11:35:16 +02:00
|
|
|
status = psa_hash_finish(&cloned_op, padbuf, hlen, &hash_size);
|
2023-01-11 14:50:10 +01:00
|
|
|
if (status != PSA_SUCCESS) {
|
2023-02-06 10:14:25 +01:00
|
|
|
goto exit;
|
2022-02-17 07:38:28 +01:00
|
|
|
}
|
2023-06-24 10:13:41 +02:00
|
|
|
MBEDTLS_SSL_DEBUG_BUF(3, "PSA calculated padbuf", padbuf, hlen);
|
2022-02-17 07:38:28 +01:00
|
|
|
#else
|
2023-06-24 10:13:41 +02:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc finished tls"));
|
2022-02-17 07:38:28 +01:00
|
|
|
|
2023-07-04 11:35:16 +02:00
|
|
|
ret = mbedtls_md_setup(&cloned_ctx, mbedtls_md_info_from_ctx(hs_ctx), 0);
|
2023-02-24 13:19:17 +01:00
|
|
|
if (ret != 0) {
|
|
|
|
goto exit;
|
|
|
|
}
|
2023-07-04 11:35:16 +02:00
|
|
|
ret = mbedtls_md_clone(&cloned_ctx, hs_ctx);
|
2023-02-24 13:19:17 +01:00
|
|
|
if (ret != 0) {
|
|
|
|
goto exit;
|
|
|
|
}
|
2022-02-17 07:38:28 +01:00
|
|
|
|
2023-07-04 11:35:16 +02:00
|
|
|
ret = mbedtls_md_finish(&cloned_ctx, padbuf);
|
2023-02-06 10:14:25 +01:00
|
|
|
if (ret != 0) {
|
|
|
|
goto exit;
|
|
|
|
}
|
2022-02-17 07:38:28 +01:00
|
|
|
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
|
|
|
|
2023-06-24 10:13:41 +02:00
|
|
|
MBEDTLS_SSL_DEBUG_BUF(4, "finished output", padbuf, hlen);
|
2023-02-24 12:13:55 +01:00
|
|
|
|
2023-06-24 10:13:41 +02:00
|
|
|
/*
|
|
|
|
* TLSv1.2:
|
|
|
|
* hash = PRF( master, finished_label,
|
|
|
|
* Hash( handshake ) )[0.11]
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
ssl->handshake->tls_prf(session->master, 48, sender,
|
2023-06-24 10:13:41 +02:00
|
|
|
padbuf, hlen, buf, len);
|
2022-02-17 07:38:28 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_BUF(3, "calc finished result", buf, len);
|
2022-02-17 07:23:47 +01:00
|
|
|
|
2023-08-11 17:41:04 +02:00
|
|
|
mbedtls_platform_zeroize(padbuf, hlen);
|
2022-02-17 07:38:28 +01:00
|
|
|
|
2023-06-24 10:13:41 +02:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(2, ("<= calc finished"));
|
2023-02-06 10:14:25 +01:00
|
|
|
|
|
|
|
exit:
|
|
|
|
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
2023-07-04 11:35:16 +02:00
|
|
|
psa_hash_abort(&cloned_op);
|
2023-03-28 12:54:56 +02:00
|
|
|
return mbedtls_md_error_from_psa(status);
|
2023-02-06 10:14:25 +01:00
|
|
|
#else
|
2023-07-04 11:35:16 +02:00
|
|
|
mbedtls_md_free(&cloned_ctx);
|
2023-02-06 10:14:25 +01:00
|
|
|
return ret;
|
|
|
|
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
2022-02-17 07:20:39 +01:00
|
|
|
}
|
2022-02-17 07:38:28 +01:00
|
|
|
|
2023-06-24 10:13:41 +02:00
|
|
|
#if defined(MBEDTLS_MD_CAN_SHA256)
|
|
|
|
static int ssl_calc_finished_tls_sha256(
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_ssl_context *ssl, unsigned char *buf, int from)
|
2022-02-17 07:25:15 +01:00
|
|
|
{
|
2023-06-24 10:13:41 +02:00
|
|
|
unsigned char padbuf[32];
|
|
|
|
return ssl_calc_finished_tls_generic(ssl,
|
2022-02-17 07:25:15 +01:00
|
|
|
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
2023-06-24 10:13:41 +02:00
|
|
|
&ssl->handshake->fin_sha256_psa,
|
2022-02-17 07:25:15 +01:00
|
|
|
#else
|
2023-06-24 10:13:41 +02:00
|
|
|
&ssl->handshake->fin_sha256,
|
2022-02-17 07:25:15 +01:00
|
|
|
#endif
|
2023-06-24 10:13:41 +02:00
|
|
|
padbuf, sizeof(padbuf),
|
|
|
|
buf, from);
|
|
|
|
}
|
|
|
|
#endif /* MBEDTLS_MD_CAN_SHA256*/
|
2022-02-17 07:25:15 +01:00
|
|
|
|
|
|
|
|
2023-06-24 10:13:41 +02:00
|
|
|
#if defined(MBEDTLS_MD_CAN_SHA384)
|
|
|
|
static int ssl_calc_finished_tls_sha384(
|
|
|
|
mbedtls_ssl_context *ssl, unsigned char *buf, int from)
|
|
|
|
{
|
|
|
|
unsigned char padbuf[48];
|
|
|
|
return ssl_calc_finished_tls_generic(ssl,
|
2022-02-17 07:25:15 +01:00
|
|
|
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
2023-06-24 10:13:41 +02:00
|
|
|
&ssl->handshake->fin_sha384_psa,
|
2022-02-17 07:25:15 +01:00
|
|
|
#else
|
2023-06-24 10:13:41 +02:00
|
|
|
&ssl->handshake->fin_sha384,
|
2022-02-17 07:38:28 +01:00
|
|
|
#endif
|
2023-06-24 10:13:41 +02:00
|
|
|
padbuf, sizeof(padbuf),
|
|
|
|
buf, from);
|
2022-02-17 07:38:28 +01:00
|
|
|
}
|
2023-03-17 12:50:01 +01:00
|
|
|
#endif /* MBEDTLS_MD_CAN_SHA384*/
|
2022-02-17 07:38:28 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
void mbedtls_ssl_handshake_wrapup_free_hs_transform(mbedtls_ssl_context *ssl)
|
2022-02-17 07:38:28 +01:00
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(3, ("=> handshake wrapup: final free"));
|
2022-02-17 07:38:28 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Free our handshake params
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_ssl_handshake_free(ssl);
|
|
|
|
mbedtls_free(ssl->handshake);
|
2022-02-17 07:38:28 +01:00
|
|
|
ssl->handshake = NULL;
|
|
|
|
|
|
|
|
/*
|
2021-12-21 06:14:10 +01:00
|
|
|
* Free the previous transform and switch in the current one
|
2022-02-17 07:38:28 +01:00
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->transform) {
|
|
|
|
mbedtls_ssl_transform_free(ssl->transform);
|
|
|
|
mbedtls_free(ssl->transform);
|
2022-02-17 07:38:28 +01:00
|
|
|
}
|
|
|
|
ssl->transform = ssl->transform_negotiate;
|
|
|
|
ssl->transform_negotiate = NULL;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(3, ("<= handshake wrapup: final free"));
|
2022-02-17 07:38:28 +01:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
void mbedtls_ssl_handshake_wrapup(mbedtls_ssl_context *ssl)
|
2022-02-17 07:38:28 +01:00
|
|
|
{
|
|
|
|
int resume = ssl->handshake->resume;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(3, ("=> handshake wrapup"));
|
2022-02-17 07:38:28 +01:00
|
|
|
|
|
|
|
#if defined(MBEDTLS_SSL_RENEGOTIATION)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS) {
|
2022-02-17 07:38:28 +01:00
|
|
|
ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
|
|
|
|
ssl->renego_records_seen = 0;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Free the previous session and switch in the current one
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->session) {
|
2022-02-17 07:38:28 +01:00
|
|
|
#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
|
|
|
|
/* RFC 7366 3.1: keep the EtM state */
|
|
|
|
ssl->session_negotiate->encrypt_then_mac =
|
2023-01-11 14:50:10 +01:00
|
|
|
ssl->session->encrypt_then_mac;
|
2022-02-17 07:38:28 +01:00
|
|
|
#endif
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_ssl_session_free(ssl->session);
|
|
|
|
mbedtls_free(ssl->session);
|
2022-02-17 07:38:28 +01:00
|
|
|
}
|
|
|
|
ssl->session = ssl->session_negotiate;
|
|
|
|
ssl->session_negotiate = NULL;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Add cache entry
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->conf->f_set_cache != NULL &&
|
2022-02-17 07:38:28 +01:00
|
|
|
ssl->session->id_len != 0 &&
|
2023-01-11 14:50:10 +01:00
|
|
|
resume == 0) {
|
|
|
|
if (ssl->conf->f_set_cache(ssl->conf->p_cache,
|
|
|
|
ssl->session->id,
|
|
|
|
ssl->session->id_len,
|
|
|
|
ssl->session) != 0) {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(1, ("cache did not store session"));
|
|
|
|
}
|
2022-02-17 07:38:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_SSL_PROTO_DTLS)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
|
|
|
|
ssl->handshake->flight != NULL) {
|
2022-02-17 07:38:28 +01:00
|
|
|
/* Cancel handshake timer */
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_ssl_set_timer(ssl, 0);
|
2022-02-17 07:38:28 +01:00
|
|
|
|
|
|
|
/* Keep last flight around in case we need to resend it:
|
|
|
|
* we need the handshake and transform structures for that */
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(3, ("skip freeing handshake and transform"));
|
|
|
|
} else
|
2022-02-17 07:38:28 +01:00
|
|
|
#endif
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_ssl_handshake_wrapup_free_hs_transform(ssl);
|
2022-02-17 07:38:28 +01:00
|
|
|
|
2022-10-27 07:08:42 +02:00
|
|
|
ssl->state = MBEDTLS_SSL_HANDSHAKE_OVER;
|
2022-02-17 07:38:28 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(3, ("<= handshake wrapup"));
|
2022-02-17 07:38:28 +01:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_ssl_write_finished(mbedtls_ssl_context *ssl)
|
2022-02-17 07:38:28 +01:00
|
|
|
{
|
2023-11-04 00:36:06 +01:00
|
|
|
int ret;
|
|
|
|
unsigned int hash_len;
|
2022-02-17 07:38:28 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(2, ("=> write finished"));
|
2022-02-17 07:38:28 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_ssl_update_out_pointers(ssl, ssl->transform_negotiate);
|
2022-02-17 07:38:28 +01:00
|
|
|
|
2023-02-06 00:34:21 +01:00
|
|
|
ret = ssl->handshake->calc_finished(ssl, ssl->out_msg + 4, ssl->conf->endpoint);
|
|
|
|
if (ret != 0) {
|
|
|
|
MBEDTLS_SSL_DEBUG_RET(1, "calc_finished", ret);
|
|
|
|
}
|
2022-02-17 07:38:28 +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 = 12;
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_SSL_RENEGOTIATION)
|
|
|
|
ssl->verify_data_len = hash_len;
|
2023-01-11 14:50:10 +01:00
|
|
|
memcpy(ssl->own_verify_data, ssl->out_msg + 4, hash_len);
|
2022-02-17 07:38:28 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
ssl->out_msglen = 4 + hash_len;
|
|
|
|
ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
|
|
|
|
ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* In case of session resuming, invert the client and server
|
|
|
|
* ChangeCipherSpec messages order.
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->handshake->resume != 0) {
|
2022-02-17 07:38:28 +01:00
|
|
|
#if defined(MBEDTLS_SSL_CLI_C)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
|
2022-02-17 07:38:28 +01:00
|
|
|
ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2022-02-17 07:38:28 +01:00
|
|
|
#endif
|
|
|
|
#if defined(MBEDTLS_SSL_SRV_C)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
|
2022-02-17 07:38:28 +01:00
|
|
|
ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2022-02-17 07:38:28 +01:00
|
|
|
#endif
|
2023-01-11 14:50:10 +01:00
|
|
|
} else {
|
2022-02-17 07:38:28 +01:00
|
|
|
ssl->state++;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2022-02-17 07:38:28 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Switch to our negotiated transform and session parameters for outbound
|
|
|
|
* data.
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(3, ("switching to new transform spec for outbound data"));
|
2022-02-17 07:38:28 +01:00
|
|
|
|
|
|
|
#if defined(MBEDTLS_SSL_PROTO_DTLS)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
|
2022-02-17 07:38:28 +01:00
|
|
|
unsigned char i;
|
|
|
|
|
|
|
|
/* Remember current epoch settings for resending */
|
|
|
|
ssl->handshake->alt_transform_out = ssl->transform_out;
|
2023-01-11 14:50:10 +01:00
|
|
|
memcpy(ssl->handshake->alt_out_ctr, ssl->cur_out_ctr,
|
|
|
|
sizeof(ssl->handshake->alt_out_ctr));
|
2022-02-17 07:38:28 +01:00
|
|
|
|
|
|
|
/* Set sequence_number to zero */
|
2023-01-11 14:50:10 +01:00
|
|
|
memset(&ssl->cur_out_ctr[2], 0, sizeof(ssl->cur_out_ctr) - 2);
|
2022-02-17 07:38:28 +01:00
|
|
|
|
|
|
|
|
|
|
|
/* Increment epoch */
|
2023-01-11 14:50:10 +01:00
|
|
|
for (i = 2; i > 0; i--) {
|
|
|
|
if (++ssl->cur_out_ctr[i - 1] != 0) {
|
2022-02-17 07:38:28 +01:00
|
|
|
break;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
|
|
|
}
|
2022-02-17 07:38:28 +01:00
|
|
|
|
|
|
|
/* The loop goes to its end iff the counter is wrapping */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (i == 0) {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(1, ("DTLS epoch would wrap"));
|
|
|
|
return MBEDTLS_ERR_SSL_COUNTER_WRAPPING;
|
2022-02-17 07:38:28 +01:00
|
|
|
}
|
2023-01-11 14:50:10 +01:00
|
|
|
} else
|
2022-02-17 07:38:28 +01:00
|
|
|
#endif /* MBEDTLS_SSL_PROTO_DTLS */
|
2023-01-11 14:50:10 +01:00
|
|
|
memset(ssl->cur_out_ctr, 0, sizeof(ssl->cur_out_ctr));
|
2022-02-17 07:38:28 +01:00
|
|
|
|
|
|
|
ssl->transform_out = ssl->transform_negotiate;
|
|
|
|
ssl->session_out = ssl->session_negotiate;
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_SSL_PROTO_DTLS)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
|
|
|
|
mbedtls_ssl_send_flight_completed(ssl);
|
|
|
|
}
|
2022-02-17 07:38:28 +01:00
|
|
|
#endif
|
|
|
|
|
2023-01-11 14:50: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;
|
2022-02-17 07:38:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_SSL_PROTO_DTLS)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
|
|
|
|
(ret = mbedtls_ssl_flight_transmit(ssl)) != 0) {
|
|
|
|
MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_flight_transmit", ret);
|
|
|
|
return ret;
|
2022-02-17 07:38:28 +01:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(2, ("<= write finished"));
|
2022-02-17 07:25:15 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2022-02-17 07:25:15 +01:00
|
|
|
}
|
2022-02-17 07:25:53 +01:00
|
|
|
|
2022-02-17 07:38:28 +01:00
|
|
|
#define SSL_MAX_HASH_LEN 12
|
2022-02-17 07:25:53 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_ssl_parse_finished(mbedtls_ssl_context *ssl)
|
2022-02-17 07:25:53 +01:00
|
|
|
{
|
2022-02-17 07:38:28 +01:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
|
|
|
unsigned int hash_len = 12;
|
|
|
|
unsigned char buf[SSL_MAX_HASH_LEN];
|
2022-02-17 07:25:53 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse finished"));
|
2022-02-17 07:25:53 +01:00
|
|
|
|
2023-02-06 00:34:21 +01:00
|
|
|
ret = ssl->handshake->calc_finished(ssl, buf, ssl->conf->endpoint ^ 1);
|
|
|
|
if (ret != 0) {
|
|
|
|
MBEDTLS_SSL_DEBUG_RET(1, "calc_finished", ret);
|
|
|
|
}
|
2022-02-17 07:25:53 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) {
|
|
|
|
MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
|
2022-02-17 07:38:28 +01:00
|
|
|
goto exit;
|
2022-02-17 07:25:53 +01:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50: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);
|
2022-02-17 07:38:28 +01:00
|
|
|
ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
|
|
|
|
goto exit;
|
2022-02-17 07:25:53 +01:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED) {
|
|
|
|
mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
|
|
|
|
MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
|
2022-02-17 07:38:28 +01:00
|
|
|
ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
|
|
|
|
goto exit;
|
|
|
|
}
|
2022-02-17 07:25:53 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (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);
|
2022-02-17 07:38:28 +01:00
|
|
|
ret = MBEDTLS_ERR_SSL_DECODE_ERROR;
|
|
|
|
goto exit;
|
|
|
|
}
|
2022-02-17 07:25:53 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (mbedtls_ct_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_DECRYPT_ERROR);
|
2022-02-17 07:38:28 +01:00
|
|
|
ret = MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
|
|
|
|
goto exit;
|
|
|
|
}
|
2022-02-17 07:25:53 +01:00
|
|
|
|
2022-02-17 07:38:28 +01:00
|
|
|
#if defined(MBEDTLS_SSL_RENEGOTIATION)
|
|
|
|
ssl->verify_data_len = hash_len;
|
2023-01-11 14:50:10 +01:00
|
|
|
memcpy(ssl->peer_verify_data, buf, hash_len);
|
2022-02-17 07:25:53 +01:00
|
|
|
#endif
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->handshake->resume != 0) {
|
2022-02-17 07:38:28 +01:00
|
|
|
#if defined(MBEDTLS_SSL_CLI_C)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
|
2022-02-17 07:38:28 +01:00
|
|
|
ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2022-02-17 07:25:53 +01:00
|
|
|
#endif
|
2022-02-17 07:38:28 +01:00
|
|
|
#if defined(MBEDTLS_SSL_SRV_C)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
|
2022-02-17 07:38:28 +01:00
|
|
|
ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2022-02-17 07:38:28 +01:00
|
|
|
#endif
|
2023-01-11 14:50:10 +01:00
|
|
|
} else {
|
2022-02-17 07:38:28 +01:00
|
|
|
ssl->state++;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2022-02-17 07:25:53 +01:00
|
|
|
|
2022-02-17 07:38:28 +01:00
|
|
|
#if defined(MBEDTLS_SSL_PROTO_DTLS)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
|
|
|
|
mbedtls_ssl_recv_flight_completed(ssl);
|
|
|
|
}
|
2022-02-17 07:38:28 +01:00
|
|
|
#endif
|
2022-02-17 07:25:53 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse finished"));
|
2022-02-17 07:25:53 +01:00
|
|
|
|
2022-02-17 07:38:28 +01:00
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_platform_zeroize(buf, hash_len);
|
|
|
|
return ret;
|
2022-02-17 07:25:53 +01:00
|
|
|
}
|
|
|
|
|
2022-02-17 07:38:28 +01:00
|
|
|
#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
|
|
|
|
/*
|
|
|
|
* Helper to get TLS 1.2 PRF from ciphersuite
|
|
|
|
* (Duplicates bits of logic from ssl_set_handshake_prfs().)
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
static tls_prf_fn ssl_tls12prf_from_cs(int ciphersuite_id)
|
2022-02-17 07:27:31 +01:00
|
|
|
{
|
2022-02-17 07:38:28 +01:00
|
|
|
const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_ssl_ciphersuite_from_id(ciphersuite_id);
|
2023-03-17 12:50:01 +01:00
|
|
|
#if defined(MBEDTLS_MD_CAN_SHA384)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ciphersuite_info != NULL && ciphersuite_info->mac == MBEDTLS_MD_SHA384) {
|
|
|
|
return tls_prf_sha384;
|
|
|
|
} else
|
2022-09-29 12:31:14 +02:00
|
|
|
#endif
|
2023-03-17 12:50:01 +01:00
|
|
|
#if defined(MBEDTLS_MD_CAN_SHA256)
|
2022-09-29 12:31:14 +02:00
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ciphersuite_info != NULL && ciphersuite_info->mac == MBEDTLS_MD_SHA256) {
|
|
|
|
return tls_prf_sha256;
|
|
|
|
}
|
2022-09-29 12:31:14 +02:00
|
|
|
}
|
2022-02-17 07:38:28 +01:00
|
|
|
#endif
|
2023-03-17 12:50:01 +01:00
|
|
|
#if !defined(MBEDTLS_MD_CAN_SHA384) && \
|
|
|
|
!defined(MBEDTLS_MD_CAN_SHA256)
|
2022-09-29 12:31:14 +02:00
|
|
|
(void) ciphersuite_info;
|
|
|
|
#endif
|
2022-10-17 13:52:51 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return NULL;
|
2022-02-17 07:38:28 +01:00
|
|
|
}
|
|
|
|
#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
|
2022-02-17 07:27:31 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
static mbedtls_tls_prf_types tls_prf_get_type(mbedtls_ssl_tls_prf_cb *tls_prf)
|
2022-02-17 07:38:28 +01:00
|
|
|
{
|
|
|
|
((void) tls_prf);
|
2023-03-17 12:50:01 +01:00
|
|
|
#if defined(MBEDTLS_MD_CAN_SHA384)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (tls_prf == tls_prf_sha384) {
|
|
|
|
return MBEDTLS_SSL_TLS_PRF_SHA384;
|
|
|
|
} else
|
2022-02-17 07:38:28 +01:00
|
|
|
#endif
|
2023-03-17 12:50:01 +01:00
|
|
|
#if defined(MBEDTLS_MD_CAN_SHA256)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (tls_prf == tls_prf_sha256) {
|
|
|
|
return MBEDTLS_SSL_TLS_PRF_SHA256;
|
|
|
|
} else
|
2022-02-17 07:38:28 +01:00
|
|
|
#endif
|
2023-01-11 14:50:10 +01:00
|
|
|
return MBEDTLS_SSL_TLS_PRF_NONE;
|
2022-02-17 07:27:31 +01:00
|
|
|
}
|
|
|
|
|
2022-02-17 07:38:28 +01:00
|
|
|
/*
|
|
|
|
* Populate a transform structure with session keys and all the other
|
|
|
|
* necessary information.
|
|
|
|
*
|
|
|
|
* Parameters:
|
|
|
|
* - [in/out]: transform: structure to populate
|
|
|
|
* [in] must be just initialised with mbedtls_ssl_transform_init()
|
|
|
|
* [out] fully populated, ready for use by mbedtls_ssl_{en,de}crypt_buf()
|
|
|
|
* - [in] ciphersuite
|
|
|
|
* - [in] master
|
|
|
|
* - [in] encrypt_then_mac
|
|
|
|
* - [in] tls_prf: pointer to PRF to use for key derivation
|
|
|
|
* - [in] randbytes: buffer holding ServerHello.random + ClientHello.random
|
2022-03-14 17:34:51 +01:00
|
|
|
* - [in] tls_version: TLS version
|
2022-02-17 07:38:28 +01:00
|
|
|
* - [in] endpoint: client or server
|
|
|
|
* - [in] ssl: used for:
|
|
|
|
* - ssl->conf->{f,p}_export_keys
|
|
|
|
* [in] optionally used for:
|
|
|
|
* - MBEDTLS_DEBUG_C: ssl->conf->{f,p}_dbg
|
|
|
|
*/
|
2022-06-17 10:52:54 +02:00
|
|
|
MBEDTLS_CHECK_RETURN_CRITICAL
|
2023-01-11 14:50:10 +01:00
|
|
|
static int ssl_tls12_populate_transform(mbedtls_ssl_transform *transform,
|
|
|
|
int ciphersuite,
|
|
|
|
const unsigned char master[48],
|
2022-04-05 11:16:53 +02:00
|
|
|
#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
|
2023-01-11 14:50:10 +01:00
|
|
|
int encrypt_then_mac,
|
2022-04-05 11:16:53 +02:00
|
|
|
#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
|
2023-01-11 14:50:10 +01:00
|
|
|
ssl_tls_prf_t tls_prf,
|
|
|
|
const unsigned char randbytes[64],
|
|
|
|
mbedtls_ssl_protocol_version tls_version,
|
|
|
|
unsigned endpoint,
|
|
|
|
const mbedtls_ssl_context *ssl)
|
2022-02-17 07:28:51 +01:00
|
|
|
{
|
2022-02-17 07:38:28 +01:00
|
|
|
int ret = 0;
|
|
|
|
unsigned char keyblk[256];
|
|
|
|
unsigned char *key1;
|
|
|
|
unsigned char *key2;
|
|
|
|
unsigned char *mac_enc;
|
|
|
|
unsigned char *mac_dec;
|
|
|
|
size_t mac_key_len = 0;
|
|
|
|
size_t iv_copy_len;
|
|
|
|
size_t keylen;
|
|
|
|
const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
|
2022-04-01 15:40:25 +02:00
|
|
|
mbedtls_ssl_mode_t ssl_mode;
|
2022-03-08 09:08:22 +01:00
|
|
|
#if !defined(MBEDTLS_USE_PSA_CRYPTO)
|
2022-04-01 17:36:10 +02:00
|
|
|
const mbedtls_cipher_info_t *cipher_info;
|
2022-02-17 07:38:28 +01:00
|
|
|
const mbedtls_md_info_t *md_info;
|
2022-03-08 09:08:22 +01:00
|
|
|
#endif /* !MBEDTLS_USE_PSA_CRYPTO */
|
2022-02-17 07:28:51 +01:00
|
|
|
|
2022-02-17 07:38:28 +01:00
|
|
|
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
|
|
|
psa_key_type_t key_type;
|
|
|
|
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
|
|
|
|
psa_algorithm_t alg;
|
2022-03-08 09:08:22 +01:00
|
|
|
psa_algorithm_t mac_alg = 0;
|
2022-02-17 07:38:28 +01:00
|
|
|
size_t key_bits;
|
|
|
|
psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
|
|
|
|
#endif
|
2022-02-17 07:28:51 +01:00
|
|
|
|
2022-02-17 07:38:28 +01:00
|
|
|
/*
|
|
|
|
* Some data just needs copying into the structure
|
|
|
|
*/
|
2022-04-05 11:16:53 +02:00
|
|
|
#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
|
2022-02-17 07:38:28 +01:00
|
|
|
transform->encrypt_then_mac = encrypt_then_mac;
|
2022-04-05 11:16:53 +02:00
|
|
|
#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
|
2022-03-14 17:34:51 +01:00
|
|
|
transform->tls_version = tls_version;
|
2022-02-17 07:38:28 +01:00
|
|
|
|
|
|
|
#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
|
2023-01-11 14:50:10 +01:00
|
|
|
memcpy(transform->randbytes, randbytes, sizeof(transform->randbytes));
|
2022-02-17 07:38:28 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (tls_version == MBEDTLS_SSL_VERSION_TLS1_3) {
|
2022-02-17 07:38:28 +01:00
|
|
|
/* At the moment, we keep TLS <= 1.2 and TLS 1.3 transform
|
|
|
|
* generation separate. This should never happen. */
|
2023-01-11 14:50:10 +01:00
|
|
|
return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
|
2022-02-17 07:28:51 +01:00
|
|
|
}
|
2022-02-17 07:38:28 +01:00
|
|
|
#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
|
2022-02-17 07:28:51 +01:00
|
|
|
|
|
|
|
/*
|
2022-02-17 07:38:28 +01:00
|
|
|
* Get various info structures
|
2022-02-17 07:28:51 +01:00
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
ciphersuite_info = mbedtls_ssl_ciphersuite_from_id(ciphersuite);
|
|
|
|
if (ciphersuite_info == NULL) {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(1, ("ciphersuite info for %d not found",
|
|
|
|
ciphersuite));
|
|
|
|
return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
|
2022-02-17 07:28:51 +01:00
|
|
|
}
|
|
|
|
|
2022-04-04 11:07:59 +02:00
|
|
|
ssl_mode = mbedtls_ssl_get_mode_from_ciphersuite(
|
2022-04-05 11:16:53 +02:00
|
|
|
#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
|
2023-01-11 14:50:10 +01:00
|
|
|
encrypt_then_mac,
|
2022-04-05 11:16:53 +02:00
|
|
|
#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
|
2023-01-11 14:50:10 +01:00
|
|
|
ciphersuite_info);
|
2022-04-01 15:40:25 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl_mode == MBEDTLS_SSL_MODE_AEAD) {
|
2022-04-01 17:36:10 +02:00
|
|
|
transform->taglen =
|
|
|
|
ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2022-04-01 17:36:10 +02:00
|
|
|
|
|
|
|
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
2023-10-05 14:30:37 +02:00
|
|
|
if ((status = mbedtls_ssl_cipher_to_psa((mbedtls_cipher_type_t) ciphersuite_info->cipher,
|
2023-01-11 14:50:10 +01:00
|
|
|
transform->taglen,
|
|
|
|
&alg,
|
|
|
|
&key_type,
|
|
|
|
&key_bits)) != PSA_SUCCESS) {
|
2022-12-23 17:00:06 +01:00
|
|
|
ret = PSA_TO_MBEDTLS_ERR(status);
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_cipher_to_psa", ret);
|
2022-04-01 17:36:10 +02:00
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
#else
|
2023-07-13 14:12:40 +02:00
|
|
|
cipher_info = mbedtls_cipher_info_from_type((mbedtls_cipher_type_t) ciphersuite_info->cipher);
|
2023-01-11 14:50:10 +01:00
|
|
|
if (cipher_info == NULL) {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(1, ("cipher info for %u not found",
|
|
|
|
ciphersuite_info->cipher));
|
|
|
|
return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
|
2022-02-17 07:28:51 +01:00
|
|
|
}
|
2022-04-01 17:36:10 +02:00
|
|
|
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
2022-02-17 07:30:01 +01:00
|
|
|
|
2022-03-08 09:08:22 +01:00
|
|
|
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
2023-10-05 14:30:37 +02:00
|
|
|
mac_alg = mbedtls_md_psa_alg_from_type((mbedtls_md_type_t) ciphersuite_info->mac);
|
2023-01-11 14:50:10 +01:00
|
|
|
if (mac_alg == 0) {
|
2023-03-28 11:38:08 +02:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(1, ("mbedtls_md_psa_alg_from_type for %u not found",
|
2023-01-11 14:50:10 +01:00
|
|
|
(unsigned) ciphersuite_info->mac));
|
|
|
|
return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
|
2022-03-08 09:08:22 +01:00
|
|
|
}
|
|
|
|
#else
|
2023-07-13 14:12:40 +02:00
|
|
|
md_info = mbedtls_md_info_from_type((mbedtls_md_type_t) ciphersuite_info->mac);
|
2023-01-11 14:50:10 +01:00
|
|
|
if (md_info == NULL) {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(1, ("mbedtls_md info for %u not found",
|
|
|
|
(unsigned) ciphersuite_info->mac));
|
|
|
|
return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
|
2022-02-17 07:38:28 +01:00
|
|
|
}
|
2022-03-08 09:08:22 +01:00
|
|
|
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
2022-02-17 07:30:01 +01:00
|
|
|
|
2022-02-17 07:38:28 +01:00
|
|
|
#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
|
|
|
|
/* Copy own and peer's CID if the use of the CID
|
|
|
|
* extension has been negotiated. */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_ENABLED) {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(3, ("Copy CIDs into SSL transform"));
|
2022-02-17 07:30:01 +01:00
|
|
|
|
2022-02-17 07:38:28 +01:00
|
|
|
transform->in_cid_len = ssl->own_cid_len;
|
2023-01-11 14:50:10 +01:00
|
|
|
memcpy(transform->in_cid, ssl->own_cid, ssl->own_cid_len);
|
|
|
|
MBEDTLS_SSL_DEBUG_BUF(3, "Incoming CID", transform->in_cid,
|
|
|
|
transform->in_cid_len);
|
2022-02-17 07:30:01 +01:00
|
|
|
|
2022-02-17 07:38:28 +01:00
|
|
|
transform->out_cid_len = ssl->handshake->peer_cid_len;
|
2023-01-11 14:50:10 +01:00
|
|
|
memcpy(transform->out_cid, ssl->handshake->peer_cid,
|
|
|
|
ssl->handshake->peer_cid_len);
|
|
|
|
MBEDTLS_SSL_DEBUG_BUF(3, "Outgoing CID", transform->out_cid,
|
|
|
|
transform->out_cid_len);
|
2022-02-17 07:38:28 +01:00
|
|
|
}
|
|
|
|
#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
|
2022-02-17 07:30:01 +01:00
|
|
|
|
|
|
|
/*
|
2022-02-17 07:38:28 +01:00
|
|
|
* Compute key block using the PRF
|
2022-02-17 07:30:01 +01:00
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = tls_prf(master, 48, "key expansion", randbytes, 64, keyblk, 256);
|
|
|
|
if (ret != 0) {
|
|
|
|
MBEDTLS_SSL_DEBUG_RET(1, "prf", ret);
|
|
|
|
return ret;
|
2022-02-17 07:30:01 +01:00
|
|
|
}
|
2022-02-17 07:38:28 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(3, ("ciphersuite = %s",
|
|
|
|
mbedtls_ssl_get_ciphersuite_name(ciphersuite)));
|
|
|
|
MBEDTLS_SSL_DEBUG_BUF(3, "master secret", master, 48);
|
|
|
|
MBEDTLS_SSL_DEBUG_BUF(4, "random bytes", randbytes, 64);
|
|
|
|
MBEDTLS_SSL_DEBUG_BUF(4, "key block", keyblk, 256);
|
2022-02-17 07:30:01 +01:00
|
|
|
|
|
|
|
/*
|
2022-02-17 07:38:28 +01:00
|
|
|
* Determine the appropriate key, IV and MAC length.
|
2022-02-17 07:30:01 +01:00
|
|
|
*/
|
|
|
|
|
2022-04-01 17:36:10 +02:00
|
|
|
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
|
|
|
keylen = PSA_BITS_TO_BYTES(key_bits);
|
|
|
|
#else
|
2023-01-11 14:50:10 +01:00
|
|
|
keylen = mbedtls_cipher_info_get_key_bitlen(cipher_info) / 8;
|
2022-04-01 17:36:10 +02:00
|
|
|
#endif
|
2022-02-17 07:38:28 +01:00
|
|
|
|
2023-10-11 11:54:42 +02:00
|
|
|
#if defined(MBEDTLS_SSL_HAVE_AEAD)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl_mode == MBEDTLS_SSL_MODE_AEAD) {
|
2022-02-17 07:38:28 +01:00
|
|
|
size_t explicit_ivlen;
|
2022-02-17 07:30:01 +01:00
|
|
|
|
2022-02-17 07:38:28 +01:00
|
|
|
transform->maclen = 0;
|
|
|
|
mac_key_len = 0;
|
2022-02-17 07:30:01 +01:00
|
|
|
|
2022-02-17 07:38:28 +01: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)
|
|
|
|
* - 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).
|
|
|
|
*/
|
|
|
|
transform->ivlen = 12;
|
2022-10-06 15:49:08 +02:00
|
|
|
|
|
|
|
int is_chachapoly = 0;
|
2022-04-01 17:36:10 +02:00
|
|
|
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
2023-01-11 14:50:10 +01:00
|
|
|
is_chachapoly = (key_type == PSA_KEY_TYPE_CHACHA20);
|
2022-04-01 17:36:10 +02:00
|
|
|
#else
|
2023-01-11 14:50:10 +01:00
|
|
|
is_chachapoly = (mbedtls_cipher_info_get_mode(cipher_info)
|
|
|
|
== MBEDTLS_MODE_CHACHAPOLY);
|
2022-04-01 17:36:10 +02:00
|
|
|
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
2022-10-06 15:49:08 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (is_chachapoly) {
|
2022-02-17 07:38:28 +01:00
|
|
|
transform->fixed_ivlen = 12;
|
2023-01-11 14:50:10 +01:00
|
|
|
} else {
|
2022-02-17 07:38:28 +01:00
|
|
|
transform->fixed_ivlen = 4;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2022-02-17 07:30:01 +01:00
|
|
|
|
2022-02-17 07:38:28 +01:00
|
|
|
/* Minimum length of encrypted record */
|
|
|
|
explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
|
|
|
|
transform->minlen = explicit_ivlen + transform->taglen;
|
2023-01-11 14:50:10 +01:00
|
|
|
} else
|
2023-10-11 11:54:42 +02:00
|
|
|
#endif /* MBEDTLS_SSL_HAVE_AEAD */
|
2022-02-17 07:38:28 +01:00
|
|
|
#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl_mode == MBEDTLS_SSL_MODE_STREAM ||
|
2022-04-01 15:40:25 +02:00
|
|
|
ssl_mode == MBEDTLS_SSL_MODE_CBC ||
|
2023-01-11 14:50:10 +01:00
|
|
|
ssl_mode == MBEDTLS_SSL_MODE_CBC_ETM) {
|
2022-04-01 17:36:10 +02:00
|
|
|
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
2023-01-11 14:50:10 +01:00
|
|
|
size_t block_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type);
|
2022-04-01 17:36:10 +02:00
|
|
|
#else
|
2023-06-24 12:41:50 +02:00
|
|
|
size_t block_size = mbedtls_cipher_info_get_block_size(cipher_info);
|
2022-04-01 17:36:10 +02:00
|
|
|
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
|
|
|
|
2022-03-08 09:08:22 +01:00
|
|
|
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
|
|
|
/* Get MAC length */
|
|
|
|
mac_key_len = PSA_HASH_LENGTH(mac_alg);
|
|
|
|
#else
|
2022-02-17 07:38:28 +01:00
|
|
|
/* Initialize HMAC contexts */
|
2023-01-11 14:50:10 +01: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) {
|
|
|
|
MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_setup", ret);
|
2022-02-17 07:38:28 +01:00
|
|
|
goto end;
|
|
|
|
}
|
2022-02-17 07:30:01 +01:00
|
|
|
|
2022-02-17 07:38:28 +01:00
|
|
|
/* Get MAC length */
|
2023-01-11 14:50:10 +01:00
|
|
|
mac_key_len = mbedtls_md_get_size(md_info);
|
2022-03-08 09:08:22 +01:00
|
|
|
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
2022-02-17 07:38:28 +01:00
|
|
|
transform->maclen = mac_key_len;
|
2022-02-17 07:30:01 +01:00
|
|
|
|
2022-02-17 07:38:28 +01:00
|
|
|
/* IV length */
|
2022-04-01 17:36:10 +02:00
|
|
|
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
2023-01-11 14:50:10 +01:00
|
|
|
transform->ivlen = PSA_CIPHER_IV_LENGTH(key_type, alg);
|
2022-04-01 17:36:10 +02:00
|
|
|
#else
|
2023-06-24 12:21:25 +02:00
|
|
|
transform->ivlen = mbedtls_cipher_info_get_iv_size(cipher_info);
|
2022-04-01 17:36:10 +02:00
|
|
|
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
2022-02-17 07:38:28 +01:00
|
|
|
|
|
|
|
/* Minimum length */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl_mode == MBEDTLS_SSL_MODE_STREAM) {
|
2022-02-17 07:38:28 +01:00
|
|
|
transform->minlen = transform->maclen;
|
2023-01-11 14:50:10 +01:00
|
|
|
} else {
|
2022-02-17 07:38:28 +01:00
|
|
|
/*
|
|
|
|
* GenericBlockCipher:
|
|
|
|
* 1. if EtM is in use: one block plus MAC
|
|
|
|
* otherwise: * first multiple of blocklen greater than maclen
|
|
|
|
* 2. IV
|
|
|
|
*/
|
|
|
|
#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl_mode == MBEDTLS_SSL_MODE_CBC_ETM) {
|
2022-02-17 07:38:28 +01:00
|
|
|
transform->minlen = transform->maclen
|
2023-01-11 14:50:10 +01:00
|
|
|
+ block_size;
|
|
|
|
} else
|
2022-02-17 07:38:28 +01:00
|
|
|
#endif
|
|
|
|
{
|
|
|
|
transform->minlen = transform->maclen
|
2023-01-11 14:50:10 +01:00
|
|
|
+ block_size
|
|
|
|
- transform->maclen % block_size;
|
2022-02-17 07:38:28 +01:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (tls_version == MBEDTLS_SSL_VERSION_TLS1_2) {
|
2022-02-17 07:38:28 +01:00
|
|
|
transform->minlen += transform->ivlen;
|
2023-01-11 14:50:10 +01:00
|
|
|
} else {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
|
2022-02-17 07:38:28 +01:00
|
|
|
ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
|
|
|
|
goto end;
|
|
|
|
}
|
2022-02-17 07:30:01 +01:00
|
|
|
}
|
2023-01-11 14:50:10 +01:00
|
|
|
} else
|
2022-02-17 07:38:28 +01:00
|
|
|
#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
|
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
|
|
|
|
return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
|
2022-02-17 07:38:28 +01:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +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));
|
2022-02-17 07:38:28 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Finally setup the cipher contexts, IVs and MAC secrets.
|
|
|
|
*/
|
|
|
|
#if defined(MBEDTLS_SSL_CLI_C)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (endpoint == MBEDTLS_SSL_IS_CLIENT) {
|
2022-02-17 07:38:28 +01:00
|
|
|
key1 = keyblk + mac_key_len * 2;
|
|
|
|
key2 = keyblk + mac_key_len * 2 + keylen;
|
|
|
|
|
|
|
|
mac_enc = keyblk;
|
|
|
|
mac_dec = keyblk + mac_key_len;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
iv_copy_len = (transform->fixed_ivlen) ?
|
|
|
|
transform->fixed_ivlen : transform->ivlen;
|
|
|
|
memcpy(transform->iv_enc, key2 + keylen, iv_copy_len);
|
|
|
|
memcpy(transform->iv_dec, key2 + keylen + iv_copy_len,
|
|
|
|
iv_copy_len);
|
|
|
|
} else
|
2022-02-17 07:38:28 +01:00
|
|
|
#endif /* MBEDTLS_SSL_CLI_C */
|
|
|
|
#if defined(MBEDTLS_SSL_SRV_C)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (endpoint == MBEDTLS_SSL_IS_SERVER) {
|
2022-02-17 07:38:28 +01:00
|
|
|
key1 = keyblk + mac_key_len * 2 + keylen;
|
|
|
|
key2 = keyblk + mac_key_len * 2;
|
2022-02-17 07:30:01 +01:00
|
|
|
|
2022-02-17 07:38:28 +01:00
|
|
|
mac_enc = keyblk + mac_key_len;
|
|
|
|
mac_dec = keyblk;
|
2022-02-17 07:30:01 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
iv_copy_len = (transform->fixed_ivlen) ?
|
|
|
|
transform->fixed_ivlen : transform->ivlen;
|
|
|
|
memcpy(transform->iv_dec, key1 + keylen, iv_copy_len);
|
|
|
|
memcpy(transform->iv_enc, key1 + keylen + iv_copy_len,
|
|
|
|
iv_copy_len);
|
|
|
|
} else
|
2022-02-17 07:38:28 +01:00
|
|
|
#endif /* MBEDTLS_SSL_SRV_C */
|
2022-02-17 07:30:01 +01:00
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
|
2022-02-17 07:38:28 +01:00
|
|
|
ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
|
|
|
|
goto end;
|
2022-02-17 07:30:01 +01:00
|
|
|
}
|
|
|
|
|
2023-10-18 13:15:30 +02:00
|
|
|
if (ssl->f_export_keys != NULL) {
|
2023-01-11 14:50:10 +01:00
|
|
|
ssl->f_export_keys(ssl->p_export_keys,
|
|
|
|
MBEDTLS_SSL_KEY_EXPORT_TLS12_MASTER_SECRET,
|
|
|
|
master, 48,
|
|
|
|
randbytes + 32,
|
|
|
|
randbytes,
|
|
|
|
tls_prf_get_type(tls_prf));
|
2022-02-17 07:38:28 +01:00
|
|
|
}
|
2022-02-17 07:30:01 +01:00
|
|
|
|
2022-02-17 07:38:28 +01:00
|
|
|
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
|
|
|
transform->psa_alg = alg;
|
2022-02-17 07:30:51 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (alg != MBEDTLS_SSL_NULL_CIPHER) {
|
|
|
|
psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
|
|
|
|
psa_set_key_algorithm(&attributes, alg);
|
|
|
|
psa_set_key_type(&attributes, key_type);
|
|
|
|
|
|
|
|
if ((status = psa_import_key(&attributes,
|
|
|
|
key1,
|
|
|
|
PSA_BITS_TO_BYTES(key_bits),
|
|
|
|
&transform->psa_key_enc)) != PSA_SUCCESS) {
|
|
|
|
MBEDTLS_SSL_DEBUG_RET(3, "psa_import_key", (int) status);
|
2022-12-23 17:00:06 +01:00
|
|
|
ret = PSA_TO_MBEDTLS_ERR(status);
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_RET(1, "psa_import_key", ret);
|
2022-02-17 07:38:28 +01:00
|
|
|
goto end;
|
|
|
|
}
|
2022-02-17 07:30:51 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
|
2022-02-17 07:30:51 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if ((status = psa_import_key(&attributes,
|
|
|
|
key2,
|
|
|
|
PSA_BITS_TO_BYTES(key_bits),
|
|
|
|
&transform->psa_key_dec)) != PSA_SUCCESS) {
|
2022-12-23 17:00:06 +01:00
|
|
|
ret = PSA_TO_MBEDTLS_ERR(status);
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_RET(1, "psa_import_key", ret);
|
2022-02-17 07:38:28 +01:00
|
|
|
goto end;
|
|
|
|
}
|
2022-02-17 07:30:51 +01:00
|
|
|
}
|
2022-02-17 07:38:28 +01:00
|
|
|
#else
|
2023-01-11 14:50:10 +01:00
|
|
|
if ((ret = mbedtls_cipher_setup(&transform->cipher_ctx_enc,
|
|
|
|
cipher_info)) != 0) {
|
|
|
|
MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setup", ret);
|
2022-02-17 07:38:28 +01:00
|
|
|
goto end;
|
2022-02-17 07:30:51 +01:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if ((ret = mbedtls_cipher_setup(&transform->cipher_ctx_dec,
|
|
|
|
cipher_info)) != 0) {
|
|
|
|
MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setup", ret);
|
2022-02-17 07:38:28 +01:00
|
|
|
goto end;
|
2022-02-17 07:30:51 +01:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if ((ret = mbedtls_cipher_setkey(&transform->cipher_ctx_enc, key1,
|
|
|
|
(int) mbedtls_cipher_info_get_key_bitlen(cipher_info),
|
|
|
|
MBEDTLS_ENCRYPT)) != 0) {
|
|
|
|
MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setkey", ret);
|
2022-02-17 07:38:28 +01:00
|
|
|
goto end;
|
2022-02-17 07:30:51 +01:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if ((ret = mbedtls_cipher_setkey(&transform->cipher_ctx_dec, key2,
|
|
|
|
(int) mbedtls_cipher_info_get_key_bitlen(cipher_info),
|
|
|
|
MBEDTLS_DECRYPT)) != 0) {
|
|
|
|
MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setkey", ret);
|
2022-02-17 07:38:28 +01:00
|
|
|
goto end;
|
2022-02-17 07:30:51 +01:00
|
|
|
}
|
|
|
|
|
2022-02-17 07:38:28 +01:00
|
|
|
#if defined(MBEDTLS_CIPHER_MODE_CBC)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (mbedtls_cipher_info_get_mode(cipher_info) == MBEDTLS_MODE_CBC) {
|
|
|
|
if ((ret = mbedtls_cipher_set_padding_mode(&transform->cipher_ctx_enc,
|
|
|
|
MBEDTLS_PADDING_NONE)) != 0) {
|
|
|
|
MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_set_padding_mode", ret);
|
2022-02-17 07:38:28 +01:00
|
|
|
goto end;
|
|
|
|
}
|
2022-02-17 07:30:51 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if ((ret = mbedtls_cipher_set_padding_mode(&transform->cipher_ctx_dec,
|
|
|
|
MBEDTLS_PADDING_NONE)) != 0) {
|
|
|
|
MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_set_padding_mode", ret);
|
2022-02-17 07:38:28 +01:00
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif /* MBEDTLS_CIPHER_MODE_CBC */
|
|
|
|
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
2022-02-17 07:30:51 +01:00
|
|
|
|
2022-03-17 17:47:28 +01:00
|
|
|
#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
|
|
|
|
/* For HMAC-based ciphersuites, initialize the HMAC transforms.
|
|
|
|
For AEAD-based ciphersuites, there is nothing to do here. */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (mac_key_len != 0) {
|
2022-03-17 17:47:28 +01:00
|
|
|
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
2023-01-11 14:50:10 +01:00
|
|
|
transform->psa_mac_alg = PSA_ALG_HMAC(mac_alg);
|
2022-03-17 17:47:28 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
|
|
|
|
psa_set_key_algorithm(&attributes, PSA_ALG_HMAC(mac_alg));
|
|
|
|
psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC);
|
2022-03-17 17:47:28 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if ((status = psa_import_key(&attributes,
|
|
|
|
mac_enc, mac_key_len,
|
|
|
|
&transform->psa_mac_enc)) != PSA_SUCCESS) {
|
2022-12-23 17:00:06 +01:00
|
|
|
ret = PSA_TO_MBEDTLS_ERR(status);
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_RET(1, "psa_import_mac_key", ret);
|
2022-03-17 17:47:28 +01:00
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if ((transform->psa_alg == MBEDTLS_SSL_NULL_CIPHER) ||
|
|
|
|
((transform->psa_alg == PSA_ALG_CBC_NO_PADDING)
|
2022-08-17 22:17:00 +02:00
|
|
|
#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
|
2023-01-11 14:50:10 +01:00
|
|
|
&& (transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED)
|
2022-08-17 22:17:00 +02:00
|
|
|
#endif
|
2023-01-11 14:50:10 +01:00
|
|
|
)) {
|
2022-03-17 17:47:28 +01:00
|
|
|
/* mbedtls_ct_hmac() requires the key to be exportable */
|
2023-01-11 14:50:10 +01:00
|
|
|
psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_EXPORT |
|
|
|
|
PSA_KEY_USAGE_VERIFY_HASH);
|
|
|
|
} else {
|
|
|
|
psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((status = psa_import_key(&attributes,
|
|
|
|
mac_dec, mac_key_len,
|
|
|
|
&transform->psa_mac_dec)) != PSA_SUCCESS) {
|
2022-12-23 17:00:06 +01:00
|
|
|
ret = PSA_TO_MBEDTLS_ERR(status);
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_RET(1, "psa_import_mac_key", ret);
|
2022-03-17 17:47:28 +01:00
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
#else
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = mbedtls_md_hmac_starts(&transform->md_ctx_enc, mac_enc, mac_key_len);
|
|
|
|
if (ret != 0) {
|
2022-03-17 17:47:28 +01:00
|
|
|
goto end;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
|
|
|
ret = mbedtls_md_hmac_starts(&transform->md_ctx_dec, mac_dec, mac_key_len);
|
|
|
|
if (ret != 0) {
|
2022-03-17 17:47:28 +01:00
|
|
|
goto end;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2022-03-17 17:47:28 +01:00
|
|
|
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
|
|
|
}
|
|
|
|
#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
|
|
|
|
|
|
|
|
((void) mac_dec);
|
|
|
|
((void) mac_enc);
|
|
|
|
|
2022-02-17 07:38:28 +01:00
|
|
|
end:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_platform_zeroize(keyblk, sizeof(keyblk));
|
|
|
|
return ret;
|
2022-02-17 07:30:51 +01:00
|
|
|
}
|
|
|
|
|
2022-11-17 15:10:02 +01:00
|
|
|
#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) && \
|
|
|
|
defined(MBEDTLS_USE_PSA_CRYPTO)
|
2022-11-17 17:14:54 +01:00
|
|
|
int mbedtls_psa_ecjpake_read_round(
|
2023-01-11 14:50:10 +01:00
|
|
|
psa_pake_operation_t *pake_ctx,
|
|
|
|
const unsigned char *buf,
|
|
|
|
size_t len, mbedtls_ecjpake_rounds_t round)
|
2022-11-17 15:10:02 +01:00
|
|
|
{
|
|
|
|
psa_status_t status;
|
|
|
|
size_t input_offset = 0;
|
2022-11-17 18:05:19 +01:00
|
|
|
/*
|
2022-11-17 17:14:54 +01:00
|
|
|
* At round one repeat the KEY_SHARE, ZK_PUBLIC & ZF_PROOF twice
|
|
|
|
* At round two perform a single cycle
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
unsigned int remaining_steps = (round == MBEDTLS_ECJPAKE_ROUND_ONE) ? 2 : 1;
|
2022-11-17 15:10:02 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
for (; remaining_steps > 0; remaining_steps--) {
|
|
|
|
for (psa_pake_step_t step = PSA_PAKE_STEP_KEY_SHARE;
|
2022-11-17 15:10:02 +01:00
|
|
|
step <= PSA_PAKE_STEP_ZK_PROOF;
|
2023-01-11 14:50:10 +01:00
|
|
|
++step) {
|
2022-11-17 15:10:02 +01:00
|
|
|
/* Length is stored at the first byte */
|
|
|
|
size_t length = buf[input_offset];
|
|
|
|
input_offset += 1;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (input_offset + length > len) {
|
2022-11-17 15:10:02 +01:00
|
|
|
return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
|
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
status = psa_pake_input(pake_ctx, step,
|
|
|
|
buf + input_offset, length);
|
|
|
|
if (status != PSA_SUCCESS) {
|
2022-12-23 17:00:06 +01:00
|
|
|
return PSA_TO_MBEDTLS_ERR(status);
|
2022-11-17 15:10:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
input_offset += length;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (input_offset != len) {
|
2022-11-18 12:11:00 +01:00
|
|
|
return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2022-11-17 16:23:34 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2022-11-17 15:10:02 +01:00
|
|
|
}
|
|
|
|
|
2022-11-17 17:14:54 +01:00
|
|
|
int mbedtls_psa_ecjpake_write_round(
|
2023-01-11 14:50:10 +01:00
|
|
|
psa_pake_operation_t *pake_ctx,
|
|
|
|
unsigned char *buf,
|
|
|
|
size_t len, size_t *olen,
|
|
|
|
mbedtls_ecjpake_rounds_t round)
|
2022-11-17 15:10:02 +01:00
|
|
|
{
|
|
|
|
psa_status_t status;
|
|
|
|
size_t output_offset = 0;
|
|
|
|
size_t output_len;
|
2022-11-17 18:05:19 +01:00
|
|
|
/*
|
2022-11-17 17:14:54 +01:00
|
|
|
* At round one repeat the KEY_SHARE, ZK_PUBLIC & ZF_PROOF twice
|
|
|
|
* At round two perform a single cycle
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
unsigned int remaining_steps = (round == MBEDTLS_ECJPAKE_ROUND_ONE) ? 2 : 1;
|
2022-11-17 15:10:02 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
for (; remaining_steps > 0; remaining_steps--) {
|
|
|
|
for (psa_pake_step_t step = PSA_PAKE_STEP_KEY_SHARE;
|
|
|
|
step <= PSA_PAKE_STEP_ZK_PROOF;
|
|
|
|
++step) {
|
2022-11-21 14:17:03 +01:00
|
|
|
/*
|
2022-11-22 11:11:10 +01:00
|
|
|
* For each step, prepend 1 byte with the length of the data as
|
|
|
|
* given by psa_pake_output().
|
2022-11-21 14:17:03 +01:00
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
status = psa_pake_output(pake_ctx, step,
|
|
|
|
buf + output_offset + 1,
|
|
|
|
len - output_offset - 1,
|
|
|
|
&output_len);
|
|
|
|
if (status != PSA_SUCCESS) {
|
2022-12-23 17:00:06 +01:00
|
|
|
return PSA_TO_MBEDTLS_ERR(status);
|
2022-11-17 15:10:02 +01:00
|
|
|
}
|
|
|
|
|
2022-11-22 16:03:43 +01:00
|
|
|
*(buf + output_offset) = (uint8_t) output_len;
|
2022-11-21 14:17:03 +01:00
|
|
|
|
|
|
|
output_offset += output_len + 1;
|
2022-11-17 15:10:02 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
*olen = output_offset;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2022-11-17 15:10:02 +01:00
|
|
|
}
|
|
|
|
#endif //MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED && MBEDTLS_USE_PSA_CRYPTO
|
|
|
|
|
2022-02-17 07:55:16 +01:00
|
|
|
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
2023-01-11 14:50:10 +01:00
|
|
|
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)
|
2022-02-17 07:55:16 +01:00
|
|
|
{
|
|
|
|
psa_status_t status;
|
|
|
|
psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT;
|
2023-03-28 11:38:08 +02:00
|
|
|
psa_algorithm_t hash_alg = mbedtls_md_psa_alg_from_type(md_alg);
|
2022-02-17 07:55:16 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(3, ("Perform PSA-based computation of digest of ServerKeyExchange"));
|
2022-02-17 07:55:16 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if ((status = psa_hash_setup(&hash_operation,
|
|
|
|
hash_alg)) != PSA_SUCCESS) {
|
|
|
|
MBEDTLS_SSL_DEBUG_RET(1, "psa_hash_setup", status);
|
2022-02-17 07:55:16 +01:00
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if ((status = psa_hash_update(&hash_operation, ssl->handshake->randbytes,
|
|
|
|
64)) != PSA_SUCCESS) {
|
|
|
|
MBEDTLS_SSL_DEBUG_RET(1, "psa_hash_update", status);
|
2022-02-17 07:55:16 +01:00
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if ((status = psa_hash_update(&hash_operation,
|
|
|
|
data, data_len)) != PSA_SUCCESS) {
|
|
|
|
MBEDTLS_SSL_DEBUG_RET(1, "psa_hash_update", status);
|
2022-02-17 07:55:16 +01:00
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if ((status = psa_hash_finish(&hash_operation, hash, PSA_HASH_MAX_SIZE,
|
|
|
|
hashlen)) != PSA_SUCCESS) {
|
|
|
|
MBEDTLS_SSL_DEBUG_RET(1, "psa_hash_finish", status);
|
|
|
|
goto exit;
|
2022-02-17 07:55:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
if (status != PSA_SUCCESS) {
|
|
|
|
mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
|
|
|
|
MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
|
|
|
|
switch (status) {
|
2022-02-17 07:55:16 +01:00
|
|
|
case PSA_ERROR_NOT_SUPPORTED:
|
2023-01-11 14:50:10 +01:00
|
|
|
return MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE;
|
2022-02-17 07:55:16 +01:00
|
|
|
case PSA_ERROR_BAD_STATE: /* Intentional fallthrough */
|
|
|
|
case PSA_ERROR_BUFFER_TOO_SMALL:
|
2023-01-11 14:50:10 +01:00
|
|
|
return MBEDTLS_ERR_MD_BAD_INPUT_DATA;
|
2022-02-17 07:55:16 +01:00
|
|
|
case PSA_ERROR_INSUFFICIENT_MEMORY:
|
2023-01-11 14:50:10 +01:00
|
|
|
return MBEDTLS_ERR_MD_ALLOC_FAILED;
|
2022-02-17 07:55:16 +01:00
|
|
|
default:
|
2023-01-11 14:50:10 +01:00
|
|
|
return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
|
2022-02-17 07:55:16 +01:00
|
|
|
}
|
|
|
|
}
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2022-02-17 07:55:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
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)
|
2022-02-17 07:55:16 +01:00
|
|
|
{
|
|
|
|
int ret = 0;
|
|
|
|
mbedtls_md_context_t ctx;
|
2023-01-11 14:50:10 +01:00
|
|
|
const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type(md_alg);
|
|
|
|
*hashlen = mbedtls_md_get_size(md_info);
|
2022-02-17 07:55:16 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(3, ("Perform mbedtls-based computation of digest of ServerKeyExchange"));
|
2022-02-17 07:55:16 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_md_init(&ctx);
|
2022-02-17 07:55:16 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* digitally-signed struct {
|
|
|
|
* opaque client_random[32];
|
|
|
|
* opaque server_random[32];
|
|
|
|
* ServerDHParams params;
|
|
|
|
* };
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
if ((ret = mbedtls_md_setup(&ctx, md_info, 0)) != 0) {
|
|
|
|
MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_setup", ret);
|
2022-02-17 07:55:16 +01:00
|
|
|
goto exit;
|
|
|
|
}
|
2023-01-11 14:50:10 +01:00
|
|
|
if ((ret = mbedtls_md_starts(&ctx)) != 0) {
|
|
|
|
MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_starts", ret);
|
2022-02-17 07:55:16 +01:00
|
|
|
goto exit;
|
|
|
|
}
|
2023-01-11 14:50:10 +01:00
|
|
|
if ((ret = mbedtls_md_update(&ctx, ssl->handshake->randbytes, 64)) != 0) {
|
|
|
|
MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_update", ret);
|
2022-02-17 07:55:16 +01:00
|
|
|
goto exit;
|
|
|
|
}
|
2023-01-11 14:50:10 +01:00
|
|
|
if ((ret = mbedtls_md_update(&ctx, data, data_len)) != 0) {
|
|
|
|
MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_update", ret);
|
2022-02-17 07:55:16 +01:00
|
|
|
goto exit;
|
|
|
|
}
|
2023-01-11 14:50:10 +01:00
|
|
|
if ((ret = mbedtls_md_finish(&ctx, hash)) != 0) {
|
|
|
|
MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_finish", ret);
|
2022-02-17 07:55:16 +01:00
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_md_free(&ctx);
|
2022-02-17 07:55:16 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ret != 0) {
|
|
|
|
mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
|
|
|
|
MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
|
|
|
|
}
|
2022-02-17 07:55:16 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return ret;
|
2022-02-17 07:55:16 +01:00
|
|
|
}
|
|
|
|
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
|
|
|
|
2022-02-17 07:57:06 +01:00
|
|
|
#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
|
|
|
|
|
2022-05-10 12:44:09 +02:00
|
|
|
/* Find the preferred hash for a given signature algorithm. */
|
|
|
|
unsigned int mbedtls_ssl_tls12_get_preferred_hash_for_sig_alg(
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_ssl_context *ssl,
|
|
|
|
unsigned int sig_alg)
|
2022-02-17 07:57:06 +01:00
|
|
|
{
|
2022-04-27 21:17:56 +02:00
|
|
|
unsigned int i;
|
2022-05-10 12:44:09 +02:00
|
|
|
uint16_t *received_sig_algs = ssl->handshake->received_sig_algs;
|
2022-02-17 07:57:06 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (sig_alg == MBEDTLS_SSL_SIG_ANON) {
|
|
|
|
return MBEDTLS_SSL_HASH_NONE;
|
|
|
|
}
|
2022-02-17 07:57:06 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
for (i = 0; received_sig_algs[i] != MBEDTLS_TLS_SIG_NONE; i++) {
|
2022-06-24 18:19:19 +02:00
|
|
|
unsigned int hash_alg_received =
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_TLS12_HASH_ALG_FROM_SIG_AND_HASH_ALG(
|
|
|
|
received_sig_algs[i]);
|
2022-06-24 18:19:19 +02:00
|
|
|
unsigned int sig_alg_received =
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_TLS12_SIG_ALG_FROM_SIG_AND_HASH_ALG(
|
|
|
|
received_sig_algs[i]);
|
2022-06-24 18:19:19 +02:00
|
|
|
|
2023-06-05 12:40:32 +02:00
|
|
|
mbedtls_md_type_t md_alg =
|
|
|
|
mbedtls_ssl_md_alg_from_hash((unsigned char) hash_alg_received);
|
|
|
|
if (md_alg == MBEDTLS_MD_NONE) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (sig_alg == sig_alg_received) {
|
2022-06-24 18:19:19 +02:00
|
|
|
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->handshake->key_cert && ssl->handshake->key_cert->key) {
|
2022-06-30 18:05:05 +02:00
|
|
|
psa_algorithm_t psa_hash_alg =
|
2023-06-05 12:40:32 +02:00
|
|
|
mbedtls_md_psa_alg_from_type(md_alg);
|
2022-06-24 18:19:19 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (sig_alg_received == MBEDTLS_SSL_SIG_ECDSA &&
|
|
|
|
!mbedtls_pk_can_do_ext(ssl->handshake->key_cert->key,
|
|
|
|
PSA_ALG_ECDSA(psa_hash_alg),
|
|
|
|
PSA_KEY_USAGE_SIGN_HASH)) {
|
2022-06-24 18:19:19 +02:00
|
|
|
continue;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2022-06-24 18:19:19 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (sig_alg_received == MBEDTLS_SSL_SIG_RSA &&
|
|
|
|
!mbedtls_pk_can_do_ext(ssl->handshake->key_cert->key,
|
|
|
|
PSA_ALG_RSA_PKCS1V15_SIGN(
|
|
|
|
psa_hash_alg),
|
|
|
|
PSA_KEY_USAGE_SIGN_HASH)) {
|
2022-06-24 18:19:19 +02:00
|
|
|
continue;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2022-06-24 18:19:19 +02:00
|
|
|
}
|
|
|
|
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
2022-06-30 18:05:05 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return hash_alg_received;
|
2022-06-24 18:19:19 +02:00
|
|
|
}
|
2022-02-17 07:57:06 +01:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return MBEDTLS_SSL_HASH_NONE;
|
2022-02-17 07:57:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
|
|
|
|
|
2022-02-17 07:58:27 +01:00
|
|
|
/* Serialization of TLS 1.2 sessions:
|
|
|
|
*
|
|
|
|
* struct {
|
2024-02-09 10:01:30 +01:00
|
|
|
* opaque ticket<0..2^24-1>; // length 0 means no ticket
|
|
|
|
* uint32 ticket_lifetime;
|
|
|
|
* } ClientOnlyData;
|
2022-02-17 07:58:27 +01:00
|
|
|
*
|
2024-02-09 10:01:30 +01:00
|
|
|
* struct {
|
|
|
|
* uint64 start_time;
|
|
|
|
* uint8 session_id_len; // at most 32
|
|
|
|
* opaque session_id[32];
|
|
|
|
* opaque master[48]; // fixed length in the standard
|
|
|
|
* uint32 verify_result;
|
|
|
|
* opaque peer_cert<0..2^24-1>; // length 0 means no peer cert
|
|
|
|
* select (endpoint) {
|
|
|
|
* case client: ClientOnlyData;
|
|
|
|
* case server: uint64 ticket_creation_time;
|
|
|
|
* };
|
|
|
|
* uint8 mfl_code; // up to 255 according to standard
|
|
|
|
* uint8 encrypt_then_mac; // 0 or 1
|
|
|
|
* } serialized_session_tls12;
|
2022-02-17 07:58:27 +01:00
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
static size_t ssl_tls12_session_save(const mbedtls_ssl_session *session,
|
|
|
|
unsigned char *buf,
|
|
|
|
size_t buf_len)
|
2022-02-17 07:58:27 +01:00
|
|
|
{
|
|
|
|
unsigned char *p = buf;
|
|
|
|
size_t used = 0;
|
|
|
|
|
|
|
|
#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 */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Time
|
|
|
|
*/
|
|
|
|
#if defined(MBEDTLS_HAVE_TIME)
|
|
|
|
used += 8;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (used <= buf_len) {
|
2022-02-17 07:58:27 +01:00
|
|
|
start = (uint64_t) session->start;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_PUT_UINT64_BE(start, p, 0);
|
2022-02-17 07:58:27 +01:00
|
|
|
p += 8;
|
|
|
|
}
|
|
|
|
#endif /* MBEDTLS_HAVE_TIME */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Basic mandatory fields
|
|
|
|
*/
|
2024-01-15 10:21:30 +01:00
|
|
|
used += 1 /* id_len */
|
2023-01-11 14:50:10 +01:00
|
|
|
+ sizeof(session->id)
|
|
|
|
+ sizeof(session->master)
|
|
|
|
+ 4; /* verify_result */
|
2022-02-17 07:58:27 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (used <= buf_len) {
|
|
|
|
*p++ = MBEDTLS_BYTE_0(session->id_len);
|
|
|
|
memcpy(p, session->id, 32);
|
2022-02-17 07:58:27 +01:00
|
|
|
p += 32;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
memcpy(p, session->master, 48);
|
2022-02-17 07:58:27 +01:00
|
|
|
p += 48;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_PUT_UINT32_BE(session->verify_result, p, 0);
|
2022-02-17 07:58:27 +01:00
|
|
|
p += 4;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Peer's end-entity certificate
|
|
|
|
*/
|
|
|
|
#if defined(MBEDTLS_X509_CRT_PARSE_C)
|
|
|
|
#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (session->peer_cert == NULL) {
|
2022-02-17 07:58:27 +01:00
|
|
|
cert_len = 0;
|
2023-01-11 14:50:10 +01:00
|
|
|
} else {
|
2022-02-17 07:58:27 +01:00
|
|
|
cert_len = session->peer_cert->raw.len;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2022-02-17 07:58:27 +01:00
|
|
|
|
|
|
|
used += 3 + cert_len;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (used <= buf_len) {
|
|
|
|
*p++ = MBEDTLS_BYTE_2(cert_len);
|
|
|
|
*p++ = MBEDTLS_BYTE_1(cert_len);
|
|
|
|
*p++ = MBEDTLS_BYTE_0(cert_len);
|
2022-02-17 07:58:27 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (session->peer_cert != NULL) {
|
|
|
|
memcpy(p, session->peer_cert->raw.p, cert_len);
|
2022-02-17 07:58:27 +01:00
|
|
|
p += cert_len;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (session->peer_cert_digest != NULL) {
|
2022-02-17 07:58:27 +01:00
|
|
|
used += 1 /* type */ + 1 /* length */ + session->peer_cert_digest_len;
|
2023-01-11 14:50:10 +01:00
|
|
|
if (used <= buf_len) {
|
2022-02-17 07:58:27 +01:00
|
|
|
*p++ = (unsigned char) session->peer_cert_digest_type;
|
|
|
|
*p++ = (unsigned char) session->peer_cert_digest_len;
|
2023-01-11 14:50:10 +01:00
|
|
|
memcpy(p, session->peer_cert_digest,
|
|
|
|
session->peer_cert_digest_len);
|
2022-02-17 07:58:27 +01:00
|
|
|
p += session->peer_cert_digest_len;
|
|
|
|
}
|
2023-01-11 14:50:10 +01:00
|
|
|
} else {
|
2022-02-17 07:58:27 +01:00
|
|
|
used += 2;
|
2023-01-11 14:50:10 +01:00
|
|
|
if (used <= buf_len) {
|
2022-02-17 07:58:27 +01:00
|
|
|
*p++ = (unsigned char) MBEDTLS_MD_NONE;
|
|
|
|
*p++ = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
|
|
|
|
#endif /* MBEDTLS_X509_CRT_PARSE_C */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Session ticket if any, plus associated data
|
|
|
|
*/
|
2023-11-22 09:50:01 +01:00
|
|
|
#if defined(MBEDTLS_SSL_SESSION_TICKETS)
|
|
|
|
#if defined(MBEDTLS_SSL_CLI_C)
|
2023-11-23 12:34:43 +01:00
|
|
|
if (session->endpoint == MBEDTLS_SSL_IS_CLIENT) {
|
|
|
|
used += 3 + session->ticket_len + 4; /* len + ticket + lifetime */
|
2022-02-17 07:58:27 +01:00
|
|
|
|
2023-11-23 12:34:43 +01:00
|
|
|
if (used <= buf_len) {
|
|
|
|
*p++ = MBEDTLS_BYTE_2(session->ticket_len);
|
|
|
|
*p++ = MBEDTLS_BYTE_1(session->ticket_len);
|
|
|
|
*p++ = MBEDTLS_BYTE_0(session->ticket_len);
|
2022-02-17 07:58:27 +01:00
|
|
|
|
2023-11-23 12:34:43 +01:00
|
|
|
if (session->ticket != NULL) {
|
|
|
|
memcpy(p, session->ticket, session->ticket_len);
|
|
|
|
p += session->ticket_len;
|
|
|
|
}
|
2022-02-17 07:58:27 +01:00
|
|
|
|
2023-11-23 12:34:43 +01:00
|
|
|
MBEDTLS_PUT_UINT32_BE(session->ticket_lifetime, p, 0);
|
|
|
|
p += 4;
|
2022-02-17 07:58:27 +01:00
|
|
|
}
|
|
|
|
}
|
2023-11-22 09:50:01 +01:00
|
|
|
#endif /* MBEDTLS_SSL_CLI_C */
|
|
|
|
#if defined(MBEDTLS_HAVE_TIME) && defined(MBEDTLS_SSL_SRV_C)
|
|
|
|
if (session->endpoint == MBEDTLS_SSL_IS_SERVER) {
|
|
|
|
used += 8;
|
2022-02-17 07:58:27 +01:00
|
|
|
|
2023-11-22 09:50:01 +01:00
|
|
|
if (used <= buf_len) {
|
|
|
|
MBEDTLS_PUT_UINT64_BE((uint64_t) session->ticket_creation_time, p, 0);
|
|
|
|
p += 8;
|
|
|
|
}
|
2022-02-17 07:58:27 +01:00
|
|
|
}
|
2023-11-22 09:50:01 +01:00
|
|
|
#endif /* MBEDTLS_HAVE_TIME && MBEDTLS_SSL_SRV_C */
|
|
|
|
#endif /* MBEDTLS_SSL_SESSION_TICKETS */
|
2022-02-17 07:58:27 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Misc extension-related info
|
|
|
|
*/
|
|
|
|
#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
|
|
|
|
used += 1;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (used <= buf_len) {
|
2022-02-17 07:58:27 +01:00
|
|
|
*p++ = session->mfl_code;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2022-02-17 07:58:27 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
|
|
|
|
used += 1;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (used <= buf_len) {
|
|
|
|
*p++ = MBEDTLS_BYTE_0(session->encrypt_then_mac);
|
|
|
|
}
|
2022-02-17 07:58:27 +01:00
|
|
|
#endif
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return used;
|
2022-02-17 07:58:27 +01:00
|
|
|
}
|
|
|
|
|
2022-06-17 10:52:54 +02:00
|
|
|
MBEDTLS_CHECK_RETURN_CRITICAL
|
2023-01-11 14:50:10 +01:00
|
|
|
static int ssl_tls12_session_load(mbedtls_ssl_session *session,
|
|
|
|
const unsigned char *buf,
|
|
|
|
size_t len)
|
2022-02-17 07:58:27 +01:00
|
|
|
{
|
|
|
|
#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 */
|
|
|
|
|
|
|
|
const unsigned char *p = buf;
|
|
|
|
const unsigned char * const end = buf + len;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Time
|
|
|
|
*/
|
|
|
|
#if defined(MBEDTLS_HAVE_TIME)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (8 > (size_t) (end - p)) {
|
|
|
|
return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
|
|
|
|
}
|
|
|
|
|
2023-05-23 18:34:33 +02:00
|
|
|
start = MBEDTLS_GET_UINT64_BE(p, 0);
|
2022-02-17 07:58:27 +01:00
|
|
|
p += 8;
|
|
|
|
|
|
|
|
session->start = (time_t) start;
|
|
|
|
#endif /* MBEDTLS_HAVE_TIME */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Basic mandatory fields
|
|
|
|
*/
|
2024-01-15 10:21:30 +01:00
|
|
|
if (1 + 32 + 48 + 4 > (size_t) (end - p)) {
|
2023-01-11 14:50:10 +01:00
|
|
|
return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
|
|
|
|
}
|
2022-02-17 07:58:27 +01:00
|
|
|
|
|
|
|
session->id_len = *p++;
|
2023-01-11 14:50:10 +01:00
|
|
|
memcpy(session->id, p, 32);
|
2022-02-17 07:58:27 +01:00
|
|
|
p += 32;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
memcpy(session->master, p, 48);
|
2022-02-17 07:58:27 +01:00
|
|
|
p += 48;
|
|
|
|
|
2023-05-23 18:34:33 +02:00
|
|
|
session->verify_result = MBEDTLS_GET_UINT32_BE(p, 0);
|
2022-02-17 07:58:27 +01:00
|
|
|
p += 4;
|
|
|
|
|
|
|
|
/* 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 */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Peer certificate
|
|
|
|
*/
|
|
|
|
#if defined(MBEDTLS_X509_CRT_PARSE_C)
|
|
|
|
#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
|
|
|
|
/* Deserialize CRT from the end of the ticket. */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (3 > (size_t) (end - p)) {
|
|
|
|
return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
|
|
|
|
}
|
2022-02-17 07:58:27 +01:00
|
|
|
|
2023-11-04 00:34:02 +01:00
|
|
|
cert_len = MBEDTLS_GET_UINT24_BE(p, 0);
|
2022-02-17 07:58:27 +01:00
|
|
|
p += 3;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (cert_len != 0) {
|
2022-02-17 07:58:27 +01:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (cert_len > (size_t) (end - p)) {
|
|
|
|
return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
|
|
|
|
}
|
2022-02-17 07:58:27 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
session->peer_cert = mbedtls_calloc(1, sizeof(mbedtls_x509_crt));
|
2022-02-17 07:58:27 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (session->peer_cert == NULL) {
|
|
|
|
return MBEDTLS_ERR_SSL_ALLOC_FAILED;
|
|
|
|
}
|
2022-02-17 07:58:27 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_x509_crt_init(session->peer_cert);
|
2022-02-17 07:58:27 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if ((ret = mbedtls_x509_crt_parse_der(session->peer_cert,
|
|
|
|
p, cert_len)) != 0) {
|
|
|
|
mbedtls_x509_crt_free(session->peer_cert);
|
|
|
|
mbedtls_free(session->peer_cert);
|
2022-02-17 07:58:27 +01:00
|
|
|
session->peer_cert = NULL;
|
2023-01-11 14:50:10 +01:00
|
|
|
return ret;
|
2022-02-17 07:58:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
p += cert_len;
|
|
|
|
}
|
|
|
|
#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
|
|
|
|
/* Deserialize CRT digest from the end of the ticket. */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (2 > (size_t) (end - p)) {
|
|
|
|
return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
|
|
|
|
}
|
2022-02-17 07:58:27 +01:00
|
|
|
|
|
|
|
session->peer_cert_digest_type = (mbedtls_md_type_t) *p++;
|
|
|
|
session->peer_cert_digest_len = (size_t) *p++;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (session->peer_cert_digest_len != 0) {
|
2022-02-17 07:58:27 +01:00
|
|
|
const mbedtls_md_info_t *md_info =
|
2023-01-11 14:50:10 +01:00
|
|
|
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;
|
|
|
|
}
|
2022-02-17 07:58:27 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (session->peer_cert_digest_len > (size_t) (end - p)) {
|
|
|
|
return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
|
|
|
|
}
|
2022-02-17 07:58:27 +01:00
|
|
|
|
|
|
|
session->peer_cert_digest =
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_calloc(1, session->peer_cert_digest_len);
|
|
|
|
if (session->peer_cert_digest == NULL) {
|
|
|
|
return MBEDTLS_ERR_SSL_ALLOC_FAILED;
|
|
|
|
}
|
2022-02-17 07:58:27 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
memcpy(session->peer_cert_digest, p,
|
|
|
|
session->peer_cert_digest_len);
|
2022-02-17 07:58:27 +01:00
|
|
|
p += session->peer_cert_digest_len;
|
|
|
|
}
|
|
|
|
#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
|
|
|
|
#endif /* MBEDTLS_X509_CRT_PARSE_C */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Session ticket and associated data
|
|
|
|
*/
|
2023-11-22 09:50:01 +01:00
|
|
|
#if defined(MBEDTLS_SSL_SESSION_TICKETS)
|
|
|
|
#if defined(MBEDTLS_SSL_CLI_C)
|
2023-11-23 12:34:43 +01:00
|
|
|
if (session->endpoint == MBEDTLS_SSL_IS_CLIENT) {
|
|
|
|
if (3 > (size_t) (end - p)) {
|
2023-01-11 14:50:10 +01:00
|
|
|
return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
|
|
|
|
}
|
2022-02-17 07:58:27 +01:00
|
|
|
|
2023-11-23 12:34:43 +01:00
|
|
|
session->ticket_len = MBEDTLS_GET_UINT24_BE(p, 0);
|
|
|
|
p += 3;
|
2022-02-17 07:58:27 +01:00
|
|
|
|
2023-11-23 12:34:43 +01:00
|
|
|
if (session->ticket_len != 0) {
|
|
|
|
if (session->ticket_len > (size_t) (end - p)) {
|
|
|
|
return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
|
|
|
|
}
|
|
|
|
|
|
|
|
session->ticket = mbedtls_calloc(1, session->ticket_len);
|
|
|
|
if (session->ticket == NULL) {
|
|
|
|
return MBEDTLS_ERR_SSL_ALLOC_FAILED;
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(session->ticket, p, session->ticket_len);
|
|
|
|
p += session->ticket_len;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2022-02-17 07:58:27 +01:00
|
|
|
|
2023-11-23 12:34:43 +01:00
|
|
|
if (4 > (size_t) (end - p)) {
|
|
|
|
return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2022-02-17 07:58:27 +01:00
|
|
|
|
2023-11-23 12:34:43 +01:00
|
|
|
session->ticket_lifetime = MBEDTLS_GET_UINT32_BE(p, 0);
|
|
|
|
p += 4;
|
2022-02-17 07:58:27 +01:00
|
|
|
}
|
2023-11-22 09:50:01 +01:00
|
|
|
#endif /* MBEDTLS_SSL_CLI_C */
|
|
|
|
#if defined(MBEDTLS_HAVE_TIME) && defined(MBEDTLS_SSL_SRV_C)
|
|
|
|
if (session->endpoint == MBEDTLS_SSL_IS_SERVER) {
|
|
|
|
if (8 > (size_t) (end - p)) {
|
|
|
|
return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
|
|
|
|
}
|
|
|
|
session->ticket_creation_time = MBEDTLS_GET_UINT64_BE(p, 0);
|
|
|
|
p += 8;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2023-11-22 09:50:01 +01:00
|
|
|
#endif /* MBEDTLS_HAVE_TIME && MBEDTLS_SSL_SRV_C */
|
|
|
|
#endif /* MBEDTLS_SSL_SESSION_TICKETS */
|
2022-02-17 07:58:27 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Misc extension-related info
|
|
|
|
*/
|
|
|
|
#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (1 > (size_t) (end - p)) {
|
|
|
|
return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
|
|
|
|
}
|
2022-02-17 07:58:27 +01:00
|
|
|
|
|
|
|
session->mfl_code = *p++;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (1 > (size_t) (end - p)) {
|
|
|
|
return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
|
|
|
|
}
|
2022-02-17 07:58:27 +01:00
|
|
|
|
|
|
|
session->encrypt_then_mac = *p++;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Done, should have consumed entire buffer */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (p != end) {
|
|
|
|
return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
|
|
|
|
}
|
2022-02-17 07:58:27 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2022-02-17 07:58:27 +01:00
|
|
|
}
|
2022-02-17 06:44:15 +01:00
|
|
|
#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
|
|
|
|
|
2022-04-20 13:05:24 +02:00
|
|
|
int mbedtls_ssl_validate_ciphersuite(
|
|
|
|
const mbedtls_ssl_context *ssl,
|
|
|
|
const mbedtls_ssl_ciphersuite_t *suite_info,
|
|
|
|
mbedtls_ssl_protocol_version min_tls_version,
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_ssl_protocol_version max_tls_version)
|
2022-04-20 13:05:24 +02:00
|
|
|
{
|
|
|
|
(void) ssl;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (suite_info == NULL) {
|
|
|
|
return -1;
|
|
|
|
}
|
2022-04-20 13:05:24 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if ((suite_info->min_tls_version > max_tls_version) ||
|
|
|
|
(suite_info->max_tls_version < min_tls_version)) {
|
|
|
|
return -1;
|
2022-04-20 13:05:24 +02:00
|
|
|
}
|
|
|
|
|
2022-04-21 11:24:56 +02:00
|
|
|
#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_CLI_C)
|
2022-04-20 13:05:24 +02:00
|
|
|
#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
|
2022-05-31 14:43:23 +02:00
|
|
|
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE &&
|
|
|
|
ssl->handshake->psa_pake_ctx_is_ok != 1)
|
2022-05-31 14:43:23 +02:00
|
|
|
#else
|
2023-01-11 14:50:10 +01:00
|
|
|
if (suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE &&
|
|
|
|
mbedtls_ecjpake_check(&ssl->handshake->ecjpake_ctx) != 0)
|
2022-05-31 14:43:23 +02:00
|
|
|
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
2022-04-20 13:05:24 +02:00
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
return -1;
|
2022-04-20 13:05:24 +02:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Don't suggest PSK-based ciphersuite if no PSK is available. */
|
|
|
|
#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (mbedtls_ssl_ciphersuite_uses_psk(suite_info) &&
|
|
|
|
mbedtls_ssl_conf_has_static_psk(ssl->conf) == 0) {
|
|
|
|
return -1;
|
2022-04-20 13:05:24 +02:00
|
|
|
}
|
|
|
|
#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
|
|
|
|
#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2022-04-20 13:05:24 +02:00
|
|
|
}
|
|
|
|
|
2022-10-05 12:46:29 +02:00
|
|
|
#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
|
2022-04-24 11:07:44 +02:00
|
|
|
/*
|
|
|
|
* Function for writing a signature algorithm extension.
|
|
|
|
*
|
|
|
|
* The `extension_data` field of signature algorithm contains a `SignatureSchemeList`
|
|
|
|
* value (TLS 1.3 RFC8446):
|
|
|
|
* enum {
|
|
|
|
* ....
|
|
|
|
* ecdsa_secp256r1_sha256( 0x0403 ),
|
|
|
|
* ecdsa_secp384r1_sha384( 0x0503 ),
|
|
|
|
* ecdsa_secp521r1_sha512( 0x0603 ),
|
|
|
|
* ....
|
|
|
|
* } SignatureScheme;
|
|
|
|
*
|
|
|
|
* struct {
|
|
|
|
* SignatureScheme supported_signature_algorithms<2..2^16-2>;
|
|
|
|
* } SignatureSchemeList;
|
|
|
|
*
|
|
|
|
* The `extension_data` field of signature algorithm contains a `SignatureAndHashAlgorithm`
|
|
|
|
* value (TLS 1.2 RFC5246):
|
|
|
|
* enum {
|
|
|
|
* none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5),
|
|
|
|
* sha512(6), (255)
|
|
|
|
* } HashAlgorithm;
|
|
|
|
*
|
|
|
|
* enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) }
|
|
|
|
* SignatureAlgorithm;
|
|
|
|
*
|
|
|
|
* struct {
|
|
|
|
* HashAlgorithm hash;
|
|
|
|
* SignatureAlgorithm signature;
|
|
|
|
* } SignatureAndHashAlgorithm;
|
|
|
|
*
|
|
|
|
* SignatureAndHashAlgorithm
|
|
|
|
* supported_signature_algorithms<2..2^16-2>;
|
|
|
|
*
|
|
|
|
* The TLS 1.3 signature algorithm extension was defined to be a compatible
|
|
|
|
* generalization of the TLS 1.2 signature algorithm extension.
|
|
|
|
* `SignatureAndHashAlgorithm` field of TLS 1.2 can be represented by
|
|
|
|
* `SignatureScheme` field of TLS 1.3
|
|
|
|
*
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_ssl_write_sig_alg_ext(mbedtls_ssl_context *ssl, unsigned char *buf,
|
|
|
|
const unsigned char *end, size_t *out_len)
|
2022-04-24 11:07:44 +02:00
|
|
|
{
|
|
|
|
unsigned char *p = buf;
|
|
|
|
unsigned char *supported_sig_alg; /* Start of supported_signature_algorithms */
|
|
|
|
size_t supported_sig_alg_len = 0; /* Length of supported_signature_algorithms */
|
|
|
|
|
|
|
|
*out_len = 0;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(3, ("adding signature_algorithms extension"));
|
2022-04-24 11:07:44 +02:00
|
|
|
|
|
|
|
/* Check if we have space for header and length field:
|
|
|
|
* - extension_type (2 bytes)
|
|
|
|
* - extension_data_length (2 bytes)
|
|
|
|
* - supported_signature_algorithms_length (2 bytes)
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_CHK_BUF_PTR(p, end, 6);
|
2022-04-24 11:07:44 +02:00
|
|
|
p += 6;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Write supported_signature_algorithms
|
|
|
|
*/
|
|
|
|
supported_sig_alg = p;
|
2023-01-11 14:50:10 +01:00
|
|
|
const uint16_t *sig_alg = mbedtls_ssl_get_sig_algs(ssl);
|
|
|
|
if (sig_alg == NULL) {
|
|
|
|
return MBEDTLS_ERR_SSL_BAD_CONFIG;
|
|
|
|
}
|
2022-04-24 11:07:44 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
for (; *sig_alg != MBEDTLS_TLS1_3_SIG_NONE; sig_alg++) {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(3, ("got signature scheme [%x] %s",
|
|
|
|
*sig_alg,
|
|
|
|
mbedtls_ssl_sig_alg_to_str(*sig_alg)));
|
|
|
|
if (!mbedtls_ssl_sig_alg_is_supported(ssl, *sig_alg)) {
|
2022-04-24 11:07:44 +02:00
|
|
|
continue;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
|
|
|
MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
|
|
|
|
MBEDTLS_PUT_UINT16_BE(*sig_alg, p, 0);
|
2022-04-24 11:07:44 +02:00
|
|
|
p += 2;
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(3, ("sent signature scheme [%x] %s",
|
|
|
|
*sig_alg,
|
|
|
|
mbedtls_ssl_sig_alg_to_str(*sig_alg)));
|
2022-04-24 11:07:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Length of supported_signature_algorithms */
|
2023-11-04 13:20:09 +01:00
|
|
|
supported_sig_alg_len = (size_t) (p - supported_sig_alg);
|
2023-01-11 14:50:10 +01:00
|
|
|
if (supported_sig_alg_len == 0) {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(1, ("No signature algorithms defined."));
|
|
|
|
return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
|
2022-04-24 11:07:44 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_SIG_ALG, buf, 0);
|
|
|
|
MBEDTLS_PUT_UINT16_BE(supported_sig_alg_len + 2, buf, 2);
|
|
|
|
MBEDTLS_PUT_UINT16_BE(supported_sig_alg_len, buf, 4);
|
2022-04-24 11:07:44 +02:00
|
|
|
|
2023-11-04 13:20:09 +01:00
|
|
|
*out_len = (size_t) (p - buf);
|
2022-04-24 11:07:44 +02:00
|
|
|
|
|
|
|
#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_SIG_ALG);
|
2022-04-24 11:07:44 +02:00
|
|
|
#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
|
2022-08-29 09:25:36 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2022-04-24 11:07:44 +02:00
|
|
|
}
|
2022-10-05 12:46:29 +02:00
|
|
|
#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
|
2022-04-24 11:07:44 +02:00
|
|
|
|
2022-05-07 11:02:40 +02:00
|
|
|
#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
|
2022-05-17 04:57:00 +02:00
|
|
|
/*
|
|
|
|
* mbedtls_ssl_parse_server_name_ext
|
|
|
|
*
|
|
|
|
* Structure of server_name extension:
|
|
|
|
*
|
|
|
|
* enum {
|
|
|
|
* host_name(0), (255)
|
|
|
|
* } NameType;
|
|
|
|
* opaque HostName<1..2^16-1>;
|
|
|
|
*
|
|
|
|
* struct {
|
|
|
|
* NameType name_type;
|
|
|
|
* select (name_type) {
|
|
|
|
* case host_name: HostName;
|
|
|
|
* } name;
|
|
|
|
* } ServerName;
|
|
|
|
* struct {
|
|
|
|
* ServerName server_name_list<1..2^16-1>
|
|
|
|
* } ServerNameList;
|
|
|
|
*/
|
2022-07-08 18:56:49 +02:00
|
|
|
MBEDTLS_CHECK_RETURN_CRITICAL
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_ssl_parse_server_name_ext(mbedtls_ssl_context *ssl,
|
|
|
|
const unsigned char *buf,
|
|
|
|
const unsigned char *end)
|
2022-05-07 11:02:40 +02:00
|
|
|
{
|
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
|
|
|
const unsigned char *p = buf;
|
2022-05-17 04:57:00 +02:00
|
|
|
size_t server_name_list_len, hostname_len;
|
|
|
|
const unsigned char *server_name_list_end;
|
2022-05-07 11:02:40 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(3, ("parse ServerName extension"));
|
2022-05-07 11:02:40 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
|
|
|
|
server_name_list_len = MBEDTLS_GET_UINT16_BE(p, 0);
|
2022-05-07 11:02:40 +02:00
|
|
|
p += 2;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, server_name_list_len);
|
2022-05-17 04:57:00 +02:00
|
|
|
server_name_list_end = p + server_name_list_len;
|
2023-01-11 14:50:10 +01:00
|
|
|
while (p < server_name_list_end) {
|
|
|
|
MBEDTLS_SSL_CHK_BUF_READ_PTR(p, server_name_list_end, 3);
|
|
|
|
hostname_len = MBEDTLS_GET_UINT16_BE(p, 1);
|
|
|
|
MBEDTLS_SSL_CHK_BUF_READ_PTR(p, server_name_list_end,
|
|
|
|
hostname_len + 3);
|
2022-05-07 11:02:40 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (p[0] == MBEDTLS_TLS_EXT_SERVERNAME_HOSTNAME) {
|
2022-06-15 11:42:45 +02:00
|
|
|
/* sni_name is intended to be used only during the parsing of the
|
|
|
|
* ClientHello message (it is reset to NULL before the end of
|
|
|
|
* the message parsing). Thus it is ok to just point to the
|
|
|
|
* reception buffer and not make a copy of it.
|
|
|
|
*/
|
2022-05-20 08:44:24 +02:00
|
|
|
ssl->handshake->sni_name = p + 3;
|
|
|
|
ssl->handshake->sni_name_len = hostname_len;
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->conf->f_sni == NULL) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
ret = ssl->conf->f_sni(ssl->conf->p_sni,
|
|
|
|
ssl, p + 3, hostname_len);
|
|
|
|
if (ret != 0) {
|
|
|
|
MBEDTLS_SSL_DEBUG_RET(1, "ssl_sni_wrapper", ret);
|
|
|
|
MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_UNRECOGNIZED_NAME,
|
|
|
|
MBEDTLS_ERR_SSL_UNRECOGNIZED_NAME);
|
|
|
|
return MBEDTLS_ERR_SSL_UNRECOGNIZED_NAME;
|
2022-05-07 11:02:40 +02:00
|
|
|
}
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2022-05-07 11:02:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
p += hostname_len + 3;
|
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2022-05-07 11:02:40 +02:00
|
|
|
}
|
|
|
|
#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
|
|
|
|
|
2022-06-17 12:18:48 +02:00
|
|
|
#if defined(MBEDTLS_SSL_ALPN)
|
2022-07-08 18:56:49 +02:00
|
|
|
MBEDTLS_CHECK_RETURN_CRITICAL
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_ssl_parse_alpn_ext(mbedtls_ssl_context *ssl,
|
|
|
|
const unsigned char *buf,
|
|
|
|
const unsigned char *end)
|
2022-06-17 12:18:48 +02:00
|
|
|
{
|
|
|
|
const unsigned char *p = buf;
|
2022-06-23 05:24:12 +02:00
|
|
|
size_t protocol_name_list_len;
|
2022-06-24 04:29:26 +02:00
|
|
|
const unsigned char *protocol_name_list;
|
|
|
|
const unsigned char *protocol_name_list_end;
|
2022-06-23 05:24:12 +02:00
|
|
|
size_t protocol_name_len;
|
2022-06-17 12:18:48 +02:00
|
|
|
|
|
|
|
/* If ALPN not configured, just ignore the extension */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->conf->alpn_list == NULL) {
|
|
|
|
return 0;
|
|
|
|
}
|
2022-06-17 12:18:48 +02:00
|
|
|
|
|
|
|
/*
|
2022-06-23 05:24:12 +02:00
|
|
|
* RFC7301, section 3.1
|
|
|
|
* opaque ProtocolName<1..2^8-1>;
|
2022-06-17 12:18:48 +02:00
|
|
|
*
|
2022-06-23 05:24:12 +02:00
|
|
|
* struct {
|
|
|
|
* ProtocolName protocol_name_list<2..2^16-1>
|
|
|
|
* } ProtocolNameList;
|
2022-06-17 12:18:48 +02:00
|
|
|
*/
|
|
|
|
|
2022-06-23 05:24:12 +02:00
|
|
|
/*
|
2022-06-24 11:04:59 +02:00
|
|
|
* protocol_name_list_len 2 bytes
|
|
|
|
* protocol_name_len 1 bytes
|
|
|
|
* protocol_name >=1 byte
|
2022-06-23 05:24:12 +02:00
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 4);
|
2022-06-17 12:18:48 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
protocol_name_list_len = MBEDTLS_GET_UINT16_BE(p, 0);
|
2022-06-17 12:18:48 +02:00
|
|
|
p += 2;
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, protocol_name_list_len);
|
2022-06-24 04:29:26 +02:00
|
|
|
protocol_name_list = p;
|
|
|
|
protocol_name_list_end = p + protocol_name_list_len;
|
2022-06-17 12:18:48 +02:00
|
|
|
|
|
|
|
/* Validate peer's list (lengths) */
|
2023-01-11 14:50:10 +01:00
|
|
|
while (p < protocol_name_list_end) {
|
2022-06-24 04:29:26 +02:00
|
|
|
protocol_name_len = *p++;
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_CHK_BUF_READ_PTR(p, protocol_name_list_end,
|
|
|
|
protocol_name_len);
|
|
|
|
if (protocol_name_len == 0) {
|
2022-06-24 04:29:26 +02:00
|
|
|
MBEDTLS_SSL_PEND_FATAL_ALERT(
|
|
|
|
MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
|
|
|
|
return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
|
2022-06-24 04:29:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
p += protocol_name_len;
|
2022-06-17 12:18:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Use our order of preference */
|
2023-01-11 14:50:10 +01:00
|
|
|
for (const char **alpn = ssl->conf->alpn_list; *alpn != NULL; alpn++) {
|
|
|
|
size_t const alpn_len = strlen(*alpn);
|
2022-06-24 04:29:26 +02:00
|
|
|
p = protocol_name_list;
|
2023-01-11 14:50:10 +01:00
|
|
|
while (p < protocol_name_list_end) {
|
2022-06-24 04:29:26 +02:00
|
|
|
protocol_name_len = *p++;
|
2023-01-11 14:50:10 +01:00
|
|
|
if (protocol_name_len == alpn_len &&
|
|
|
|
memcmp(p, *alpn, alpn_len) == 0) {
|
2022-06-17 12:18:48 +02:00
|
|
|
ssl->alpn_chosen = *alpn;
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2022-06-17 12:18:48 +02:00
|
|
|
}
|
2022-06-24 04:29:26 +02:00
|
|
|
|
|
|
|
p += protocol_name_len;
|
2022-06-17 12:18:48 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-24 04:29:26 +02:00
|
|
|
/* If we get here, no match was found */
|
2022-06-17 12:18:48 +02:00
|
|
|
MBEDTLS_SSL_PEND_FATAL_ALERT(
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_ALERT_MSG_NO_APPLICATION_PROTOCOL,
|
|
|
|
MBEDTLS_ERR_SSL_NO_APPLICATION_PROTOCOL);
|
|
|
|
return MBEDTLS_ERR_SSL_NO_APPLICATION_PROTOCOL;
|
2022-06-17 12:18:48 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_ssl_write_alpn_ext(mbedtls_ssl_context *ssl,
|
|
|
|
unsigned char *buf,
|
|
|
|
unsigned char *end,
|
|
|
|
size_t *out_len)
|
2022-06-17 12:18:48 +02:00
|
|
|
{
|
|
|
|
unsigned char *p = buf;
|
2022-06-24 04:29:26 +02:00
|
|
|
size_t protocol_name_len;
|
2022-06-23 05:24:12 +02:00
|
|
|
*out_len = 0;
|
2022-06-17 12:18:48 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->alpn_chosen == NULL) {
|
|
|
|
return 0;
|
2022-06-17 12:18:48 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
protocol_name_len = strlen(ssl->alpn_chosen);
|
|
|
|
MBEDTLS_SSL_CHK_BUF_PTR(p, end, 7 + protocol_name_len);
|
2022-06-17 12:18:48 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(3, ("server side, adding alpn extension"));
|
2022-06-17 12:18:48 +02:00
|
|
|
/*
|
|
|
|
* 0 . 1 ext identifier
|
|
|
|
* 2 . 3 ext length
|
|
|
|
* 4 . 5 protocol list length
|
|
|
|
* 6 . 6 protocol name length
|
|
|
|
* 7 . 7+n protocol name
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_ALPN, p, 0);
|
2022-06-17 12:18:48 +02:00
|
|
|
|
2022-06-24 04:29:26 +02:00
|
|
|
*out_len = 7 + protocol_name_len;
|
2022-06-17 12:18:48 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_PUT_UINT16_BE(protocol_name_len + 3, p, 2);
|
|
|
|
MBEDTLS_PUT_UINT16_BE(protocol_name_len + 1, p, 4);
|
2022-06-24 11:04:59 +02:00
|
|
|
/* Note: the length of the chosen protocol has been checked to be less
|
|
|
|
* than 255 bytes in `mbedtls_ssl_conf_alpn_protocols`.
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
p[6] = MBEDTLS_BYTE_0(protocol_name_len);
|
2022-06-17 12:18:48 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
memcpy(p + 7, ssl->alpn_chosen, protocol_name_len);
|
2022-11-08 14:19:34 +01:00
|
|
|
|
|
|
|
#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_ALPN);
|
2022-11-08 14:19:34 +01:00
|
|
|
#endif
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2022-06-17 12:18:48 +02:00
|
|
|
}
|
|
|
|
#endif /* MBEDTLS_SSL_ALPN */
|
|
|
|
|
2022-10-11 08:20:56 +02:00
|
|
|
#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
|
2022-10-12 04:49:52 +02:00
|
|
|
defined(MBEDTLS_SSL_SESSION_TICKETS) && \
|
2022-10-12 08:58:13 +02:00
|
|
|
defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) && \
|
2022-10-12 10:31:11 +02:00
|
|
|
defined(MBEDTLS_SSL_CLI_C)
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_ssl_session_set_hostname(mbedtls_ssl_session *session,
|
|
|
|
const char *hostname)
|
2022-10-11 08:20:56 +02:00
|
|
|
{
|
|
|
|
/* Initialize to suppress unnecessary compiler warning */
|
|
|
|
size_t hostname_len = 0;
|
|
|
|
|
|
|
|
/* Check if new hostname is valid before
|
|
|
|
* making any change to current one */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (hostname != NULL) {
|
|
|
|
hostname_len = strlen(hostname);
|
2022-10-11 08:20:56 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN) {
|
|
|
|
return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
|
|
|
|
}
|
2022-10-11 08:20:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Now it's clear that we will overwrite the old hostname,
|
|
|
|
* so we can free it safely */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (session->hostname != NULL) {
|
2023-07-17 16:17:40 +02:00
|
|
|
mbedtls_zeroize_and_free(session->hostname,
|
2023-01-11 14:50:10 +01:00
|
|
|
strlen(session->hostname));
|
2022-10-11 08:20:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Passing NULL as hostname shall clear the old one */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (hostname == NULL) {
|
2022-10-11 08:20:56 +02:00
|
|
|
session->hostname = NULL;
|
2023-01-11 14:50:10 +01:00
|
|
|
} else {
|
|
|
|
session->hostname = mbedtls_calloc(1, hostname_len + 1);
|
|
|
|
if (session->hostname == NULL) {
|
|
|
|
return MBEDTLS_ERR_SSL_ALLOC_FAILED;
|
|
|
|
}
|
2022-10-11 08:20:56 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
memcpy(session->hostname, hostname, hostname_len);
|
2022-10-11 08:20:56 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2022-10-11 08:20:56 +02:00
|
|
|
}
|
2022-10-12 04:49:52 +02:00
|
|
|
#endif /* MBEDTLS_SSL_PROTO_TLS1_3 &&
|
|
|
|
MBEDTLS_SSL_SESSION_TICKETS &&
|
2022-10-12 08:58:13 +02:00
|
|
|
MBEDTLS_SSL_SERVER_NAME_INDICATION &&
|
2022-10-12 10:31:11 +02:00
|
|
|
MBEDTLS_SSL_CLI_C */
|
2022-10-11 08:20:56 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#endif /* MBEDTLS_SSL_TLS_C */
|