2021-08-18 10:38:40 +02:00
|
|
|
/*
|
|
|
|
* TLS 1.3 functionality shared between client and server
|
|
|
|
*
|
|
|
|
* Copyright The Mbed TLS Contributors
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); you may
|
|
|
|
* not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "common.h"
|
|
|
|
|
2022-01-27 08:03:26 +01:00
|
|
|
#if defined(MBEDTLS_SSL_TLS_C) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
|
2021-08-18 10:38:40 +02:00
|
|
|
|
2021-09-12 14:16:03 +02:00
|
|
|
#include <string.h>
|
|
|
|
|
2021-08-18 10:46:28 +02:00
|
|
|
#include "mbedtls/error.h"
|
2021-09-01 09:59:36 +02:00
|
|
|
#include "mbedtls/debug.h"
|
2021-09-12 14:16:03 +02:00
|
|
|
#include "mbedtls/oid.h"
|
|
|
|
#include "mbedtls/platform.h"
|
2021-11-24 11:17:36 +01:00
|
|
|
#include "mbedtls/constant_time.h"
|
2022-12-01 13:30:41 +01:00
|
|
|
#include "psa/crypto.h"
|
|
|
|
#include "mbedtls/psa_util.h"
|
2021-08-18 10:46:28 +02:00
|
|
|
|
2021-08-18 10:38:40 +02:00
|
|
|
#include "ssl_misc.h"
|
2022-06-10 17:21:51 +02:00
|
|
|
#include "ssl_tls13_invasive.h"
|
2021-09-12 14:16:03 +02:00
|
|
|
#include "ssl_tls13_keys.h"
|
2022-02-25 06:37:36 +01:00
|
|
|
#include "ssl_debug_helpers.h"
|
2021-08-18 10:38:40 +02:00
|
|
|
|
2022-12-23 17:00:06 +01:00
|
|
|
#include "psa/crypto.h"
|
|
|
|
#include "mbedtls/psa_util.h"
|
|
|
|
|
|
|
|
#define PSA_TO_MBEDTLS_ERR(status) PSA_TO_MBEDTLS_ERR_LIST(status, \
|
|
|
|
psa_to_ssl_errors, \
|
|
|
|
psa_generic_status_to_mbedtls)
|
|
|
|
|
2022-04-25 13:31:51 +02:00
|
|
|
const uint8_t mbedtls_ssl_tls13_hello_retry_request_magic[
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SERVER_HELLO_RANDOM_LEN] =
|
|
|
|
{ 0xCF, 0x21, 0xAD, 0x74, 0xE5, 0x9A, 0x61, 0x11,
|
|
|
|
0xBE, 0x1D, 0x8C, 0x02, 0x1E, 0x65, 0xB8, 0x91,
|
|
|
|
0xC2, 0xA2, 0x11, 0x16, 0x7A, 0xBB, 0x8C, 0x5E,
|
|
|
|
0x07, 0x9E, 0x09, 0xE2, 0xC8, 0xA8, 0x33, 0x9C };
|
|
|
|
|
|
|
|
int mbedtls_ssl_tls13_fetch_handshake_msg(mbedtls_ssl_context *ssl,
|
|
|
|
unsigned hs_type,
|
|
|
|
unsigned char **buf,
|
|
|
|
size_t *buf_len)
|
2021-09-24 09:51:16 +02:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if ((ret = mbedtls_ssl_read_record(ssl, 0)) != 0) {
|
|
|
|
MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
|
2021-09-24 09:51:16 +02:00
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE ||
|
|
|
|
ssl->in_msg[0] != hs_type) {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(1, ("Receive unexpected handshake message."));
|
|
|
|
MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE,
|
|
|
|
MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE);
|
2021-09-24 09:51:16 +02:00
|
|
|
ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
2021-09-29 10:46:37 +02:00
|
|
|
/*
|
|
|
|
* Jump handshake header (4 bytes, see Section 4 of RFC 8446).
|
|
|
|
* ...
|
|
|
|
* HandshakeType msg_type;
|
|
|
|
* uint24 length;
|
|
|
|
* ...
|
|
|
|
*/
|
2021-11-18 08:29:56 +01:00
|
|
|
*buf = ssl->in_msg + 4;
|
|
|
|
*buf_len = ssl->in_hslen - 4;
|
2021-09-24 09:51:16 +02:00
|
|
|
|
|
|
|
cleanup:
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return ret;
|
2021-09-24 09:51:16 +02:00
|
|
|
}
|
|
|
|
|
2023-02-08 17:38:29 +01:00
|
|
|
int mbedtls_ssl_tls13_is_supported_versions_ext_present_in_exts(
|
|
|
|
mbedtls_ssl_context *ssl,
|
|
|
|
const unsigned char *buf, const unsigned char *end,
|
2023-04-03 17:36:31 +02:00
|
|
|
const unsigned char **supported_versions_data,
|
|
|
|
const unsigned char **supported_versions_data_end)
|
2023-02-08 17:38:29 +01:00
|
|
|
{
|
|
|
|
const unsigned char *p = buf;
|
|
|
|
size_t extensions_len;
|
|
|
|
const unsigned char *extensions_end;
|
|
|
|
|
2023-04-03 17:36:31 +02:00
|
|
|
*supported_versions_data = NULL;
|
|
|
|
*supported_versions_data_end = NULL;
|
2023-02-08 17:38:29 +01:00
|
|
|
|
|
|
|
/* Case of no extension */
|
|
|
|
if (p == end) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ...
|
|
|
|
* Extension extensions<x..2^16-1>;
|
|
|
|
* ...
|
|
|
|
* struct {
|
|
|
|
* ExtensionType extension_type; (2 bytes)
|
|
|
|
* opaque extension_data<0..2^16-1>;
|
|
|
|
* } Extension;
|
|
|
|
*/
|
|
|
|
MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
|
|
|
|
extensions_len = MBEDTLS_GET_UINT16_BE(p, 0);
|
|
|
|
p += 2;
|
|
|
|
|
|
|
|
/* Check extensions do not go beyond the buffer of data. */
|
|
|
|
MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, extensions_len);
|
|
|
|
extensions_end = p + extensions_len;
|
|
|
|
|
|
|
|
while (p < extensions_end) {
|
|
|
|
unsigned int extension_type;
|
|
|
|
size_t extension_data_len;
|
|
|
|
|
|
|
|
MBEDTLS_SSL_CHK_BUF_READ_PTR(p, extensions_end, 4);
|
|
|
|
extension_type = MBEDTLS_GET_UINT16_BE(p, 0);
|
|
|
|
extension_data_len = MBEDTLS_GET_UINT16_BE(p, 2);
|
|
|
|
p += 4;
|
|
|
|
MBEDTLS_SSL_CHK_BUF_READ_PTR(p, extensions_end, extension_data_len);
|
|
|
|
|
|
|
|
if (extension_type == MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS) {
|
2023-04-03 17:36:31 +02:00
|
|
|
*supported_versions_data = p;
|
|
|
|
*supported_versions_data_end = p + extension_data_len;
|
2023-02-08 17:38:29 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
p += extension_data_len;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-10-04 16:14:26 +02:00
|
|
|
#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
|
2021-10-28 07:41:59 +02:00
|
|
|
/*
|
|
|
|
* STATE HANDLING: Read CertificateVerify
|
|
|
|
*/
|
2021-10-29 05:09:06 +02:00
|
|
|
/* Macro to express the maximum length of the verify structure.
|
2021-10-28 07:41:59 +02:00
|
|
|
*
|
|
|
|
* The structure is computed per TLS 1.3 specification as:
|
|
|
|
* - 64 bytes of octet 32,
|
|
|
|
* - 33 bytes for the context string
|
|
|
|
* (which is either "TLS 1.3, client CertificateVerify"
|
|
|
|
* or "TLS 1.3, server CertificateVerify"),
|
2021-10-29 05:09:06 +02:00
|
|
|
* - 1 byte for the octet 0x0, which serves as a separator,
|
2021-10-28 07:41:59 +02:00
|
|
|
* - 32 or 48 bytes for the Transcript-Hash(Handshake Context, Certificate)
|
|
|
|
* (depending on the size of the transcript_hash)
|
|
|
|
*
|
|
|
|
* This results in a total size of
|
|
|
|
* - 130 bytes for a SHA256-based transcript hash, or
|
|
|
|
* (64 + 33 + 1 + 32 bytes)
|
|
|
|
* - 146 bytes for a SHA384-based transcript hash.
|
|
|
|
* (64 + 33 + 1 + 48 bytes)
|
|
|
|
*
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
#define SSL_VERIFY_STRUCT_MAX_SIZE (64 + \
|
|
|
|
33 + \
|
|
|
|
1 + \
|
|
|
|
MBEDTLS_TLS1_3_MD_MAX_SIZE \
|
|
|
|
)
|
2021-10-28 07:41:59 +02:00
|
|
|
|
2021-09-12 14:16:03 +02:00
|
|
|
/*
|
|
|
|
* The ssl_tls13_create_verify_structure() creates the verify structure.
|
|
|
|
* As input, it requires the transcript hash.
|
|
|
|
*
|
|
|
|
* The caller has to ensure that the buffer has size at least
|
|
|
|
* SSL_VERIFY_STRUCT_MAX_SIZE bytes.
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
static void ssl_tls13_create_verify_structure(const unsigned char *transcript_hash,
|
|
|
|
size_t transcript_hash_len,
|
|
|
|
unsigned char *verify_buffer,
|
|
|
|
size_t *verify_buffer_len,
|
|
|
|
int from)
|
2021-09-12 14:16:03 +02:00
|
|
|
{
|
2021-10-25 06:42:58 +02:00
|
|
|
size_t idx;
|
2021-09-12 14:16:03 +02:00
|
|
|
|
|
|
|
/* RFC 8446, Section 4.4.3:
|
|
|
|
*
|
|
|
|
* The digital signature [in the CertificateVerify message] is then
|
|
|
|
* computed over the concatenation of:
|
|
|
|
* - A string that consists of octet 32 (0x20) repeated 64 times
|
|
|
|
* - The context string
|
|
|
|
* - A single 0 byte which serves as the separator
|
|
|
|
* - The content to be signed
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
memset(verify_buffer, 0x20, 64);
|
2021-10-28 07:41:59 +02:00
|
|
|
idx = 64;
|
2021-09-12 14:16:03 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (from == MBEDTLS_SSL_IS_CLIENT) {
|
|
|
|
memcpy(verify_buffer + idx, MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(client_cv));
|
|
|
|
idx += MBEDTLS_SSL_TLS1_3_LBL_LEN(client_cv);
|
|
|
|
} else { /* from == MBEDTLS_SSL_IS_SERVER */
|
|
|
|
memcpy(verify_buffer + idx, MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(server_cv));
|
|
|
|
idx += MBEDTLS_SSL_TLS1_3_LBL_LEN(server_cv);
|
2021-09-12 14:16:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
verify_buffer[idx++] = 0x0;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
memcpy(verify_buffer + idx, transcript_hash, transcript_hash_len);
|
2021-09-12 14:16:03 +02:00
|
|
|
idx += transcript_hash_len;
|
|
|
|
|
|
|
|
*verify_buffer_len = idx;
|
|
|
|
}
|
|
|
|
|
2022-06-17 10:52:54 +02:00
|
|
|
MBEDTLS_CHECK_RETURN_CRITICAL
|
2023-01-11 14:50:10 +01:00
|
|
|
static int ssl_tls13_parse_certificate_verify(mbedtls_ssl_context *ssl,
|
|
|
|
const unsigned char *buf,
|
|
|
|
const unsigned char *end,
|
|
|
|
const unsigned char *verify_buffer,
|
|
|
|
size_t verify_buffer_len)
|
2021-09-12 14:16:03 +02:00
|
|
|
{
|
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2022-04-26 15:03:11 +02:00
|
|
|
psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
|
2021-09-12 14:16:03 +02:00
|
|
|
const unsigned char *p = buf;
|
|
|
|
uint16_t algorithm;
|
|
|
|
size_t signature_len;
|
|
|
|
mbedtls_pk_type_t sig_alg;
|
|
|
|
mbedtls_md_type_t md_alg;
|
2022-04-26 15:03:11 +02:00
|
|
|
psa_algorithm_t hash_alg = PSA_ALG_NONE;
|
|
|
|
unsigned char verify_hash[PSA_HASH_MAX_SIZE];
|
2021-09-12 14:16:03 +02:00
|
|
|
size_t verify_hash_len;
|
|
|
|
|
2021-12-02 07:36:27 +01:00
|
|
|
void const *options = NULL;
|
2021-11-03 09:51:56 +01:00
|
|
|
#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
|
2021-12-02 07:36:27 +01:00
|
|
|
mbedtls_pk_rsassa_pss_options rsassa_pss_options;
|
2021-11-03 09:51:56 +01:00
|
|
|
#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
|
|
|
|
|
2021-09-12 14:16:03 +02:00
|
|
|
/*
|
|
|
|
* struct {
|
|
|
|
* SignatureScheme algorithm;
|
|
|
|
* opaque signature<0..2^16-1>;
|
|
|
|
* } CertificateVerify;
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
|
|
|
|
algorithm = MBEDTLS_GET_UINT16_BE(p, 0);
|
2021-09-12 14:16:03 +02:00
|
|
|
p += 2;
|
|
|
|
|
|
|
|
/* RFC 8446 section 4.4.3
|
|
|
|
*
|
2023-03-29 10:24:12 +02:00
|
|
|
* If the CertificateVerify message is sent by a server, the signature
|
|
|
|
* algorithm MUST be one offered in the client's "signature_algorithms"
|
|
|
|
* extension unless no valid certificate chain can be produced without
|
|
|
|
* unsupported algorithms
|
2021-09-12 14:16:03 +02:00
|
|
|
*
|
|
|
|
* RFC 8446 section 4.4.2.2
|
|
|
|
*
|
|
|
|
* If the client cannot construct an acceptable chain using the provided
|
2023-03-29 10:24:12 +02:00
|
|
|
* certificates and decides to abort the handshake, then it MUST abort the
|
|
|
|
* handshake with an appropriate certificate-related alert
|
|
|
|
* (by default, "unsupported_certificate").
|
2021-09-12 14:16:03 +02:00
|
|
|
*
|
2021-10-29 14:12:51 +02:00
|
|
|
* Check if algorithm is an offered signature algorithm.
|
2021-09-12 14:16:03 +02:00
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
if (!mbedtls_ssl_sig_alg_is_offered(ssl, algorithm)) {
|
2021-10-14 09:59:37 +02:00
|
|
|
/* algorithm not in offered signature algorithms list */
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(1, ("Received signature algorithm(%04x) is not "
|
|
|
|
"offered.",
|
|
|
|
(unsigned int) algorithm));
|
2021-10-29 14:12:51 +02:00
|
|
|
goto error;
|
2021-09-12 14:16:03 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (mbedtls_ssl_get_pk_type_and_md_alg_from_sig_alg(
|
|
|
|
algorithm, &sig_alg, &md_alg) != 0) {
|
2022-03-23 06:34:04 +01:00
|
|
|
goto error;
|
2021-09-12 14:16:03 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
hash_alg = mbedtls_hash_info_psa_from_md(md_alg);
|
|
|
|
if (hash_alg == 0) {
|
2022-04-26 15:03:11 +02:00
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(3, ("Certificate Verify: Signature algorithm ( %04x )",
|
|
|
|
(unsigned int) algorithm));
|
2021-09-12 14:16:03 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Check the certificate's key type matches the signature alg
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
if (!mbedtls_pk_can_do(&ssl->session_negotiate->peer_cert->pk, sig_alg)) {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(1, ("signature algorithm doesn't match cert key"));
|
2021-10-29 14:12:51 +02:00
|
|
|
goto error;
|
2021-09-12 14:16:03 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
|
|
|
|
signature_len = MBEDTLS_GET_UINT16_BE(p, 0);
|
2021-09-12 14:16:03 +02:00
|
|
|
p += 2;
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, signature_len);
|
|
|
|
|
|
|
|
status = psa_hash_compute(hash_alg,
|
|
|
|
verify_buffer,
|
|
|
|
verify_buffer_len,
|
|
|
|
verify_hash,
|
|
|
|
sizeof(verify_hash),
|
|
|
|
&verify_hash_len);
|
|
|
|
if (status != PSA_SUCCESS) {
|
|
|
|
MBEDTLS_SSL_DEBUG_RET(1, "hash computation PSA error", status);
|
2021-10-29 14:12:51 +02:00
|
|
|
goto error;
|
2021-10-25 08:01:13 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_BUF(3, "verify hash", verify_hash, verify_hash_len);
|
2021-11-03 09:51:56 +01:00
|
|
|
#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (sig_alg == MBEDTLS_PK_RSASSA_PSS) {
|
2021-12-02 07:36:27 +01:00
|
|
|
rsassa_pss_options.mgf1_hash_id = md_alg;
|
2022-06-27 11:53:13 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
rsassa_pss_options.expected_salt_len = PSA_HASH_LENGTH(hash_alg);
|
|
|
|
options = (const void *) &rsassa_pss_options;
|
2021-11-03 09:51:56 +01:00
|
|
|
}
|
|
|
|
#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
|
2021-09-12 14:16:03 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if ((ret = mbedtls_pk_verify_ext(sig_alg, options,
|
|
|
|
&ssl->session_negotiate->peer_cert->pk,
|
|
|
|
md_alg, verify_hash, verify_hash_len,
|
|
|
|
p, signature_len)) == 0) {
|
|
|
|
return 0;
|
2021-09-12 14:16:03 +02:00
|
|
|
}
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_pk_verify_ext", ret);
|
2021-10-29 14:12:51 +02:00
|
|
|
|
|
|
|
error:
|
|
|
|
/* RFC 8446 section 4.4.3
|
|
|
|
*
|
|
|
|
* If the verification fails, the receiver MUST terminate the handshake
|
|
|
|
* with a "decrypt_error" alert.
|
2023-01-11 14:50:10 +01:00
|
|
|
*/
|
|
|
|
MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR,
|
|
|
|
MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
|
|
|
|
return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
|
2021-09-12 14:16:03 +02:00
|
|
|
|
|
|
|
}
|
2022-10-04 16:14:26 +02:00
|
|
|
#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
|
2021-09-12 14:16:03 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_ssl_tls13_process_certificate_verify(mbedtls_ssl_context *ssl)
|
2021-09-12 14:16:03 +02:00
|
|
|
{
|
2021-10-25 09:06:49 +02:00
|
|
|
|
2022-10-04 16:14:26 +02:00
|
|
|
#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
|
2021-09-12 14:16:03 +02:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2021-10-25 09:06:49 +02:00
|
|
|
unsigned char verify_buffer[SSL_VERIFY_STRUCT_MAX_SIZE];
|
|
|
|
size_t verify_buffer_len;
|
|
|
|
unsigned char transcript[MBEDTLS_TLS1_3_MD_MAX_SIZE];
|
|
|
|
size_t transcript_len;
|
|
|
|
unsigned char *buf;
|
|
|
|
size_t buf_len;
|
2021-09-12 14:16:03 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse certificate verify"));
|
2021-10-25 09:06:49 +02:00
|
|
|
|
|
|
|
MBEDTLS_SSL_PROC_CHK(
|
2023-03-29 10:24:12 +02:00
|
|
|
mbedtls_ssl_tls13_fetch_handshake_msg(
|
|
|
|
ssl, MBEDTLS_SSL_HS_CERTIFICATE_VERIFY, &buf, &buf_len));
|
2021-10-25 09:06:49 +02:00
|
|
|
|
|
|
|
/* Need to calculate the hash of the transcript first
|
2021-10-28 07:41:59 +02:00
|
|
|
* before reading the message since otherwise it gets
|
|
|
|
* included in the transcript
|
|
|
|
*/
|
2023-03-29 10:24:12 +02:00
|
|
|
ret = mbedtls_ssl_get_handshake_transcript(
|
|
|
|
ssl,
|
|
|
|
ssl->handshake->ciphersuite_info->mac,
|
|
|
|
transcript, sizeof(transcript),
|
|
|
|
&transcript_len);
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ret != 0) {
|
2021-10-25 09:06:49 +02:00
|
|
|
MBEDTLS_SSL_PEND_FATAL_ALERT(
|
|
|
|
MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR,
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_ERR_SSL_INTERNAL_ERROR);
|
|
|
|
return ret;
|
2021-09-12 14:16:03 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_BUF(3, "handshake hash", transcript, transcript_len);
|
2021-10-25 09:06:49 +02:00
|
|
|
|
|
|
|
/* Create verify structure */
|
2023-01-11 14:50:10 +01:00
|
|
|
ssl_tls13_create_verify_structure(transcript,
|
|
|
|
transcript_len,
|
|
|
|
verify_buffer,
|
|
|
|
&verify_buffer_len,
|
|
|
|
(ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) ?
|
|
|
|
MBEDTLS_SSL_IS_SERVER :
|
|
|
|
MBEDTLS_SSL_IS_CLIENT);
|
2021-10-25 09:06:49 +02:00
|
|
|
|
|
|
|
/* Process the message contents */
|
2023-03-29 10:24:12 +02:00
|
|
|
MBEDTLS_SSL_PROC_CHK(ssl_tls13_parse_certificate_verify(
|
|
|
|
ssl, buf, buf + buf_len,
|
|
|
|
verify_buffer, verify_buffer_len));
|
2021-10-25 09:06:49 +02:00
|
|
|
|
2023-03-29 10:24:12 +02:00
|
|
|
MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
|
|
|
|
ssl, MBEDTLS_SSL_HS_CERTIFICATE_VERIFY,
|
|
|
|
buf, buf_len));
|
2021-09-12 14:16:03 +02:00
|
|
|
|
|
|
|
cleanup:
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse certificate verify"));
|
|
|
|
MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_tls13_process_certificate_verify", ret);
|
|
|
|
return ret;
|
2021-10-25 09:06:49 +02:00
|
|
|
#else
|
|
|
|
((void) ssl);
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
|
|
|
|
return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
|
2022-10-04 16:14:26 +02:00
|
|
|
#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
|
2021-09-12 14:16:03 +02:00
|
|
|
}
|
2021-08-24 09:59:48 +02:00
|
|
|
|
2021-09-29 11:12:03 +02:00
|
|
|
/*
|
|
|
|
*
|
2022-04-25 09:29:34 +02:00
|
|
|
* STATE HANDLING: Incoming Certificate.
|
2021-09-29 11:12:03 +02:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2022-10-04 17:15:35 +02:00
|
|
|
#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
|
2021-09-29 11:12:03 +02:00
|
|
|
#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
|
|
|
|
/*
|
|
|
|
* Structure of Certificate message:
|
|
|
|
*
|
|
|
|
* enum {
|
|
|
|
* X509(0),
|
|
|
|
* RawPublicKey(2),
|
|
|
|
* (255)
|
|
|
|
* } CertificateType;
|
|
|
|
*
|
|
|
|
* struct {
|
|
|
|
* select (certificate_type) {
|
|
|
|
* case RawPublicKey:
|
|
|
|
* * From RFC 7250 ASN.1_subjectPublicKeyInfo *
|
|
|
|
* opaque ASN1_subjectPublicKeyInfo<1..2^24-1>;
|
|
|
|
* case X509:
|
|
|
|
* opaque cert_data<1..2^24-1>;
|
|
|
|
* };
|
|
|
|
* Extension extensions<0..2^16-1>;
|
|
|
|
* } CertificateEntry;
|
|
|
|
*
|
|
|
|
* struct {
|
|
|
|
* opaque certificate_request_context<0..2^8-1>;
|
|
|
|
* CertificateEntry certificate_list<0..2^24-1>;
|
|
|
|
* } Certificate;
|
|
|
|
*
|
|
|
|
*/
|
2021-10-26 09:16:45 +02:00
|
|
|
|
|
|
|
/* Parse certificate chain send by the server. */
|
2022-06-17 10:52:54 +02:00
|
|
|
MBEDTLS_CHECK_RETURN_CRITICAL
|
2022-06-10 17:21:51 +02:00
|
|
|
MBEDTLS_STATIC_TESTABLE
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_ssl_tls13_parse_certificate(mbedtls_ssl_context *ssl,
|
|
|
|
const unsigned char *buf,
|
|
|
|
const unsigned char *end)
|
2021-09-29 11:12:03 +02:00
|
|
|
{
|
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
|
|
|
size_t certificate_request_context_len = 0;
|
|
|
|
size_t certificate_list_len = 0;
|
|
|
|
const unsigned char *p = buf;
|
|
|
|
const unsigned char *certificate_list_end;
|
2022-10-29 03:08:47 +02:00
|
|
|
mbedtls_ssl_handshake_params *handshake = ssl->handshake;
|
2021-09-29 11:12:03 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 4);
|
2021-09-29 11:12:03 +02:00
|
|
|
certificate_request_context_len = p[0];
|
2023-01-11 14:50:10 +01:00
|
|
|
certificate_list_len = MBEDTLS_GET_UINT24_BE(p, 1);
|
2022-05-15 06:26:57 +02:00
|
|
|
p += 4;
|
2021-09-29 11:12:03 +02:00
|
|
|
|
|
|
|
/* In theory, the certificate list 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 ((certificate_request_context_len != 0) ||
|
|
|
|
(certificate_list_len >= 0x10000)) {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
|
|
|
|
MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
|
|
|
|
MBEDTLS_ERR_SSL_DECODE_ERROR);
|
|
|
|
return MBEDTLS_ERR_SSL_DECODE_ERROR;
|
2021-09-29 11:12:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* In case we tried to reuse a session but it failed */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->session_negotiate->peer_cert != NULL) {
|
|
|
|
mbedtls_x509_crt_free(ssl->session_negotiate->peer_cert);
|
|
|
|
mbedtls_free(ssl->session_negotiate->peer_cert);
|
2021-09-29 11:12:03 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (certificate_list_len == 0) {
|
2022-05-13 07:55:41 +02:00
|
|
|
ssl->session_negotiate->peer_cert = NULL;
|
|
|
|
ret = 0;
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if ((ssl->session_negotiate->peer_cert =
|
|
|
|
mbedtls_calloc(1, sizeof(mbedtls_x509_crt))) == NULL) {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(1, ("alloc( %" MBEDTLS_PRINTF_SIZET " bytes ) failed",
|
|
|
|
sizeof(mbedtls_x509_crt)));
|
|
|
|
MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR,
|
|
|
|
MBEDTLS_ERR_SSL_ALLOC_FAILED);
|
|
|
|
return MBEDTLS_ERR_SSL_ALLOC_FAILED;
|
2021-09-29 11:12:03 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_x509_crt_init(ssl->session_negotiate->peer_cert);
|
2021-09-29 11:12:03 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, certificate_list_len);
|
2021-09-29 11:12:03 +02:00
|
|
|
certificate_list_end = p + certificate_list_len;
|
2023-01-11 14:50:10 +01:00
|
|
|
while (p < certificate_list_end) {
|
2021-09-29 11:12:03 +02:00
|
|
|
size_t cert_data_len, extensions_len;
|
2022-08-04 11:28:15 +02:00
|
|
|
const unsigned char *extensions_end;
|
2021-09-29 11:12:03 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_CHK_BUF_READ_PTR(p, certificate_list_end, 3);
|
|
|
|
cert_data_len = MBEDTLS_GET_UINT24_BE(p, 0);
|
2021-09-29 11:12:03 +02:00
|
|
|
p += 3;
|
|
|
|
|
|
|
|
/* In theory, the CRT can be up to 2^24 Bytes, but we don't support
|
|
|
|
* anything beyond 2^16 = 64K. Otherwise as in the TLS 1.2 code,
|
|
|
|
* check that we have a minimum of 128 bytes of data, this is not
|
|
|
|
* clear why we need that though.
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
if ((cert_data_len < 128) || (cert_data_len >= 0x10000)) {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(1, ("bad Certificate message"));
|
|
|
|
MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
|
|
|
|
MBEDTLS_ERR_SSL_DECODE_ERROR);
|
|
|
|
return MBEDTLS_ERR_SSL_DECODE_ERROR;
|
2021-09-29 11:12:03 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_CHK_BUF_READ_PTR(p, certificate_list_end, cert_data_len);
|
|
|
|
ret = mbedtls_x509_crt_parse_der(ssl->session_negotiate->peer_cert,
|
|
|
|
p, cert_data_len);
|
2021-09-29 11:12:03 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
switch (ret) {
|
2021-09-29 11:12:03 +02:00
|
|
|
case 0: /*ok*/
|
|
|
|
break;
|
|
|
|
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;
|
|
|
|
|
|
|
|
case MBEDTLS_ERR_X509_ALLOC_FAILED:
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR,
|
|
|
|
MBEDTLS_ERR_X509_ALLOC_FAILED);
|
|
|
|
MBEDTLS_SSL_DEBUG_RET(1, " mbedtls_x509_crt_parse_der", ret);
|
|
|
|
return ret;
|
2021-09-29 11:12:03 +02:00
|
|
|
|
|
|
|
case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT,
|
|
|
|
MBEDTLS_ERR_X509_UNKNOWN_VERSION);
|
|
|
|
MBEDTLS_SSL_DEBUG_RET(1, " mbedtls_x509_crt_parse_der", ret);
|
|
|
|
return ret;
|
2021-09-29 11:12:03 +02:00
|
|
|
|
|
|
|
default:
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_BAD_CERT,
|
|
|
|
ret);
|
|
|
|
MBEDTLS_SSL_DEBUG_RET(1, " mbedtls_x509_crt_parse_der", ret);
|
|
|
|
return ret;
|
2021-09-29 11:12:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
p += cert_data_len;
|
|
|
|
|
|
|
|
/* Certificate extensions length */
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_CHK_BUF_READ_PTR(p, certificate_list_end, 2);
|
|
|
|
extensions_len = MBEDTLS_GET_UINT16_BE(p, 0);
|
2021-09-29 11:12:03 +02:00
|
|
|
p += 2;
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_CHK_BUF_READ_PTR(p, certificate_list_end, extensions_len);
|
2022-08-04 11:28:15 +02:00
|
|
|
|
|
|
|
extensions_end = p + extensions_len;
|
2022-10-31 07:15:48 +01:00
|
|
|
handshake->received_extensions = MBEDTLS_SSL_EXT_MASK_NONE;
|
2022-08-04 11:28:15 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
while (p < extensions_end) {
|
2022-08-04 11:28:15 +02:00
|
|
|
unsigned int extension_type;
|
|
|
|
size_t extension_data_len;
|
|
|
|
|
|
|
|
/*
|
2023-01-11 14:50:10 +01:00
|
|
|
* struct {
|
|
|
|
* ExtensionType extension_type; (2 bytes)
|
|
|
|
* opaque extension_data<0..2^16-1>;
|
|
|
|
* } Extension;
|
|
|
|
*/
|
|
|
|
MBEDTLS_SSL_CHK_BUF_READ_PTR(p, extensions_end, 4);
|
|
|
|
extension_type = MBEDTLS_GET_UINT16_BE(p, 0);
|
|
|
|
extension_data_len = MBEDTLS_GET_UINT16_BE(p, 2);
|
2022-08-04 11:28:15 +02:00
|
|
|
p += 4;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_CHK_BUF_READ_PTR(p, extensions_end, extension_data_len);
|
2022-08-04 11:28:15 +02:00
|
|
|
|
2022-10-29 03:08:47 +02:00
|
|
|
ret = mbedtls_ssl_tls13_check_received_extension(
|
2023-01-11 14:50:10 +01:00
|
|
|
ssl, MBEDTLS_SSL_HS_CERTIFICATE, extension_type,
|
|
|
|
MBEDTLS_SSL_TLS1_3_ALLOWED_EXTS_OF_CT);
|
|
|
|
if (ret != 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
2022-08-29 09:25:36 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
switch (extension_type) {
|
2022-08-04 11:28:15 +02:00
|
|
|
default:
|
2022-11-08 14:30:21 +01:00
|
|
|
MBEDTLS_SSL_PRINT_EXT(
|
2022-10-31 07:15:48 +01:00
|
|
|
3, MBEDTLS_SSL_HS_CERTIFICATE,
|
2023-01-11 14:50:10 +01:00
|
|
|
extension_type, "( ignored )");
|
2022-08-04 11:28:15 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
p += extension_data_len;
|
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_PRINT_EXTS(3, MBEDTLS_SSL_HS_CERTIFICATE,
|
|
|
|
handshake->received_extensions);
|
2021-09-29 11:12:03 +02:00
|
|
|
}
|
|
|
|
|
2022-05-15 06:26:57 +02:00
|
|
|
exit:
|
2021-09-29 11:12:03 +02:00
|
|
|
/* Check that all the message is consumed. */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (p != end) {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(1, ("bad Certificate message"));
|
|
|
|
MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
|
|
|
|
MBEDTLS_ERR_SSL_DECODE_ERROR);
|
|
|
|
return MBEDTLS_ERR_SSL_DECODE_ERROR;
|
2021-09-29 11:12:03 +02:00
|
|
|
}
|
|
|
|
|
2023-03-29 10:24:12 +02:00
|
|
|
MBEDTLS_SSL_DEBUG_CRT(3, "peer certificate",
|
|
|
|
ssl->session_negotiate->peer_cert);
|
2021-09-29 11:12:03 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return ret;
|
2021-09-29 11:12:03 +02:00
|
|
|
}
|
|
|
|
#else
|
2022-06-17 10:52:54 +02:00
|
|
|
MBEDTLS_CHECK_RETURN_CRITICAL
|
2022-06-10 17:21:51 +02:00
|
|
|
MBEDTLS_STATIC_TESTABLE
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_ssl_tls13_parse_certificate(mbedtls_ssl_context *ssl,
|
|
|
|
const unsigned char *buf,
|
|
|
|
const unsigned char *end)
|
2021-09-29 11:12:03 +02:00
|
|
|
{
|
|
|
|
((void) ssl);
|
|
|
|
((void) buf);
|
|
|
|
((void) end);
|
2023-01-11 14:50:10 +01:00
|
|
|
return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
|
2021-09-29 11:12:03 +02:00
|
|
|
}
|
|
|
|
#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
|
2022-10-04 17:15:35 +02:00
|
|
|
#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
|
2021-09-29 11:12:03 +02:00
|
|
|
|
2022-10-04 17:15:35 +02:00
|
|
|
#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
|
2021-09-29 11:12:03 +02:00
|
|
|
#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
|
2021-10-26 09:16:45 +02:00
|
|
|
/* Validate certificate chain sent by the server. */
|
2022-06-17 10:52:54 +02:00
|
|
|
MBEDTLS_CHECK_RETURN_CRITICAL
|
2023-01-11 14:50:10 +01:00
|
|
|
static int ssl_tls13_validate_certificate(mbedtls_ssl_context *ssl)
|
2021-09-29 11:12:03 +02:00
|
|
|
{
|
|
|
|
int ret = 0;
|
2022-05-17 03:50:15 +02:00
|
|
|
int authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
|
2021-09-29 11:12:03 +02:00
|
|
|
mbedtls_x509_crt *ca_chain;
|
|
|
|
mbedtls_x509_crl *ca_crl;
|
2022-06-16 19:31:06 +02:00
|
|
|
const char *ext_oid;
|
|
|
|
size_t ext_len;
|
2021-10-28 08:50:17 +02:00
|
|
|
uint32_t verify_result = 0;
|
2021-09-29 11:12:03 +02:00
|
|
|
|
2022-04-25 09:29:34 +02:00
|
|
|
/* If SNI was used, overwrite authentication mode
|
|
|
|
* from the configuration. */
|
2022-05-17 03:50:15 +02:00
|
|
|
#if defined(MBEDTLS_SSL_SRV_C)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
|
2022-05-30 10:10:53 +02:00
|
|
|
#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET) {
|
2022-05-30 10:10:53 +02:00
|
|
|
authmode = ssl->handshake->sni_authmode;
|
2023-01-11 14:50:10 +01:00
|
|
|
} else
|
2022-05-30 10:10:53 +02:00
|
|
|
#endif
|
2023-01-11 14:50:10 +01:00
|
|
|
authmode = ssl->conf->authmode;
|
2022-05-30 10:10:53 +02:00
|
|
|
}
|
2022-04-25 09:29:34 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/*
|
2022-05-17 03:50:15 +02:00
|
|
|
* If the peer hasn't sent a certificate ( i.e. it sent
|
2022-04-25 09:29:34 +02:00
|
|
|
* an empty certificate chain ), this is reflected in the peer CRT
|
|
|
|
* structure being unset.
|
|
|
|
* Check for that and handle it depending on the
|
2022-05-17 03:50:15 +02:00
|
|
|
* authentication mode.
|
2022-04-25 09:29:34 +02:00
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->session_negotiate->peer_cert == NULL) {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(1, ("peer has no certificate"));
|
2022-05-17 03:50:15 +02:00
|
|
|
|
2022-05-15 06:26:57 +02:00
|
|
|
#if defined(MBEDTLS_SSL_SRV_C)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
|
2022-05-15 06:26:57 +02:00
|
|
|
/* The client was asked for a certificate but didn't send
|
|
|
|
* one. The client should know what's going on, so we
|
|
|
|
* don't send an alert.
|
|
|
|
*/
|
|
|
|
ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
|
2023-01-11 14:50:10 +01:00
|
|
|
if (authmode == MBEDTLS_SSL_VERIFY_OPTIONAL) {
|
|
|
|
return 0;
|
|
|
|
} else {
|
2023-03-29 10:24:12 +02:00
|
|
|
MBEDTLS_SSL_PEND_FATAL_ALERT(
|
|
|
|
MBEDTLS_SSL_ALERT_MSG_NO_CERT,
|
|
|
|
MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE);
|
2023-01-11 14:50:10 +01:00
|
|
|
return MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
|
2022-05-17 03:50:15 +02:00
|
|
|
}
|
2022-05-15 06:26:57 +02:00
|
|
|
}
|
2022-04-25 09:29:34 +02:00
|
|
|
#endif /* MBEDTLS_SSL_SRV_C */
|
|
|
|
|
2022-05-13 07:55:41 +02: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_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_NO_CERT,
|
|
|
|
MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE);
|
|
|
|
return MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE;
|
2022-05-15 06:26:57 +02:00
|
|
|
}
|
2022-05-13 07:55:41 +02:00
|
|
|
#endif /* MBEDTLS_SSL_CLI_C */
|
2022-05-15 06:26:57 +02:00
|
|
|
}
|
2022-04-25 09:29:34 +02:00
|
|
|
|
2021-09-29 11:12:03 +02:00
|
|
|
#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->handshake->sni_ca_chain != NULL) {
|
2021-09-29 11:12:03 +02:00
|
|
|
ca_chain = ssl->handshake->sni_ca_chain;
|
|
|
|
ca_crl = ssl->handshake->sni_ca_crl;
|
2023-01-11 14:50:10 +01:00
|
|
|
} else
|
2021-09-29 11:12:03 +02:00
|
|
|
#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
|
|
|
|
{
|
|
|
|
ca_chain = ssl->conf->ca_chain;
|
|
|
|
ca_crl = ssl->conf->ca_crl;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Main check: verify certificate
|
|
|
|
*/
|
|
|
|
ret = mbedtls_x509_crt_verify_with_profile(
|
|
|
|
ssl->session_negotiate->peer_cert,
|
|
|
|
ca_chain, ca_crl,
|
|
|
|
ssl->conf->cert_profile,
|
|
|
|
ssl->hostname,
|
2021-10-28 08:50:17 +02:00
|
|
|
&verify_result,
|
2023-01-11 14:50:10 +01:00
|
|
|
ssl->conf->f_vrfy, ssl->conf->p_vrfy);
|
2021-09-29 11:12:03 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ret != 0) {
|
|
|
|
MBEDTLS_SSL_DEBUG_RET(1, "x509_verify_cert", ret);
|
2021-09-29 11:12:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Secondary checks: always done, but change 'ret' only if it was 0
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
|
2022-06-16 19:31:06 +02: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-06-16 19:31:06 +02: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-06-16 19:31:06 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if ((mbedtls_x509_crt_check_key_usage(
|
|
|
|
ssl->session_negotiate->peer_cert,
|
|
|
|
MBEDTLS_X509_KU_DIGITAL_SIGNATURE) != 0) ||
|
|
|
|
(mbedtls_x509_crt_check_extended_key_usage(
|
|
|
|
ssl->session_negotiate->peer_cert,
|
|
|
|
ext_oid, ext_len) != 0)) {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate (usage extensions)"));
|
|
|
|
if (ret == 0) {
|
2021-09-29 11:12:03 +02:00
|
|
|
ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2021-09-29 11:12:03 +02:00
|
|
|
}
|
|
|
|
|
2022-04-25 09:29:34 +02: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
|
2022-06-10 17:21:51 +02:00
|
|
|
* mbedtls_ssl_tls13_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-04-25 09:29:34 +02:00
|
|
|
ret = 0;
|
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ca_chain == NULL && authmode == MBEDTLS_SSL_VERIFY_REQUIRED) {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(1, ("got no CA chain"));
|
2021-09-29 11:12:03 +02:00
|
|
|
ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
|
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ret != 0) {
|
2021-09-29 11:12:03 +02: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 (verify_result & MBEDTLS_X509_BADCERT_OTHER) {
|
2023-03-29 10:24:12 +02:00
|
|
|
MBEDTLS_SSL_PEND_FATAL_ALERT(
|
|
|
|
MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED, ret);
|
2023-01-11 14:50:10 +01:00
|
|
|
} else if (verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH) {
|
|
|
|
MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_BAD_CERT, ret);
|
|
|
|
} else if (verify_result & (MBEDTLS_X509_BADCERT_KEY_USAGE |
|
|
|
|
MBEDTLS_X509_BADCERT_EXT_KEY_USAGE |
|
|
|
|
MBEDTLS_X509_BADCERT_NS_CERT_TYPE |
|
|
|
|
MBEDTLS_X509_BADCERT_BAD_PK |
|
|
|
|
MBEDTLS_X509_BADCERT_BAD_KEY)) {
|
2023-03-29 10:24:12 +02:00
|
|
|
MBEDTLS_SSL_PEND_FATAL_ALERT(
|
|
|
|
MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT, ret);
|
2023-01-11 14:50:10 +01:00
|
|
|
} else if (verify_result & MBEDTLS_X509_BADCERT_EXPIRED) {
|
2023-03-29 10:24:12 +02:00
|
|
|
MBEDTLS_SSL_PEND_FATAL_ALERT(
|
|
|
|
MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED, ret);
|
2023-01-11 14:50:10 +01:00
|
|
|
} else if (verify_result & MBEDTLS_X509_BADCERT_REVOKED) {
|
2023-03-29 10:24:12 +02:00
|
|
|
MBEDTLS_SSL_PEND_FATAL_ALERT(
|
|
|
|
MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED, ret);
|
2023-01-11 14:50:10 +01:00
|
|
|
} else if (verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED) {
|
|
|
|
MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA, ret);
|
|
|
|
} else {
|
2023-03-29 10:24:12 +02:00
|
|
|
MBEDTLS_SSL_PEND_FATAL_ALERT(
|
|
|
|
MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN, ret);
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2021-09-29 11:12:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_DEBUG_C)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (verify_result != 0) {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(3, ("! Certificate verification flags %08x",
|
|
|
|
(unsigned int) verify_result));
|
|
|
|
} else {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(3, ("Certificate verification flags clear"));
|
2021-09-29 11:12:03 +02:00
|
|
|
}
|
|
|
|
#endif /* MBEDTLS_DEBUG_C */
|
|
|
|
|
2021-10-28 08:50:17 +02:00
|
|
|
ssl->session_negotiate->verify_result = verify_result;
|
2023-01-11 14:50:10 +01:00
|
|
|
return ret;
|
2021-09-29 11:12:03 +02: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_tls13_validate_certificate(mbedtls_ssl_context *ssl)
|
2021-09-29 11:12:03 +02:00
|
|
|
{
|
|
|
|
((void) ssl);
|
2023-01-11 14:50:10 +01:00
|
|
|
return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
|
2021-09-29 11:12:03 +02:00
|
|
|
}
|
|
|
|
#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
|
2022-10-04 17:15:35 +02:00
|
|
|
#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
|
2021-09-29 11:12:03 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_ssl_tls13_process_certificate(mbedtls_ssl_context *ssl)
|
2021-09-29 11:12:03 +02:00
|
|
|
{
|
2021-10-26 09:16:45 +02:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse certificate"));
|
2021-10-26 09:16:45 +02:00
|
|
|
|
2022-10-04 17:15:35 +02:00
|
|
|
#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
|
2022-05-13 07:55:41 +02:00
|
|
|
unsigned char *buf;
|
|
|
|
size_t buf_len;
|
2021-10-26 09:16:45 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_tls13_fetch_handshake_msg(
|
|
|
|
ssl, MBEDTLS_SSL_HS_CERTIFICATE,
|
|
|
|
&buf, &buf_len));
|
2021-10-26 09:16:45 +02:00
|
|
|
|
2022-05-13 07:55:41 +02:00
|
|
|
/* Parse the certificate chain sent by the peer. */
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_tls13_parse_certificate(ssl, buf,
|
|
|
|
buf + buf_len));
|
2022-05-13 07:55:41 +02:00
|
|
|
/* Validate the certificate chain and set the verification results. */
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_PROC_CHK(ssl_tls13_validate_certificate(ssl));
|
2022-04-25 09:29:34 +02:00
|
|
|
|
2023-03-29 10:24:12 +02:00
|
|
|
MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
|
|
|
|
ssl, MBEDTLS_SSL_HS_CERTIFICATE, buf, buf_len));
|
2021-10-26 09:16:45 +02:00
|
|
|
|
|
|
|
cleanup:
|
2022-10-04 17:15:35 +02:00
|
|
|
#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
|
2021-10-26 09:16:45 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse certificate"));
|
|
|
|
return ret;
|
2021-09-29 11:12:03 +02:00
|
|
|
}
|
2022-10-04 16:14:26 +02:00
|
|
|
#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
|
2022-01-30 10:54:19 +01:00
|
|
|
/*
|
|
|
|
* enum {
|
|
|
|
* X509(0),
|
|
|
|
* RawPublicKey(2),
|
|
|
|
* (255)
|
|
|
|
* } CertificateType;
|
|
|
|
*
|
|
|
|
* struct {
|
|
|
|
* select (certificate_type) {
|
|
|
|
* case RawPublicKey:
|
|
|
|
* // From RFC 7250 ASN.1_subjectPublicKeyInfo
|
|
|
|
* opaque ASN1_subjectPublicKeyInfo<1..2^24-1>;
|
|
|
|
*
|
|
|
|
* case X509:
|
|
|
|
* opaque cert_data<1..2^24-1>;
|
|
|
|
* };
|
|
|
|
* Extension extensions<0..2^16-1>;
|
|
|
|
* } CertificateEntry;
|
|
|
|
*
|
|
|
|
* struct {
|
|
|
|
* opaque certificate_request_context<0..2^8-1>;
|
|
|
|
* CertificateEntry certificate_list<0..2^24-1>;
|
|
|
|
* } Certificate;
|
|
|
|
*/
|
2022-06-17 10:52:54 +02:00
|
|
|
MBEDTLS_CHECK_RETURN_CRITICAL
|
2023-01-11 14:50:10 +01:00
|
|
|
static int ssl_tls13_write_certificate_body(mbedtls_ssl_context *ssl,
|
|
|
|
unsigned char *buf,
|
|
|
|
unsigned char *end,
|
|
|
|
size_t *out_len)
|
2022-01-28 09:16:08 +01:00
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
const mbedtls_x509_crt *crt = mbedtls_ssl_own_cert(ssl);
|
2022-02-15 04:05:59 +01:00
|
|
|
unsigned char *p = buf;
|
2022-02-18 05:10:03 +01:00
|
|
|
unsigned char *certificate_request_context =
|
2023-01-11 14:50:10 +01:00
|
|
|
ssl->handshake->certificate_request_context;
|
2022-02-18 05:10:03 +01:00
|
|
|
unsigned char certificate_request_context_len =
|
2023-01-11 14:50:10 +01:00
|
|
|
ssl->handshake->certificate_request_context_len;
|
2022-02-18 05:10:03 +01:00
|
|
|
unsigned char *p_certificate_list_len;
|
2022-01-28 09:16:08 +01:00
|
|
|
|
|
|
|
|
2022-02-16 04:21:37 +01:00
|
|
|
/* ...
|
|
|
|
* opaque certificate_request_context<0..2^8-1>;
|
|
|
|
* ...
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_CHK_BUF_PTR(p, end, certificate_request_context_len + 1);
|
2022-02-18 05:10:03 +01:00
|
|
|
*p++ = certificate_request_context_len;
|
2023-01-11 14:50:10 +01:00
|
|
|
if (certificate_request_context_len > 0) {
|
|
|
|
memcpy(p, certificate_request_context, certificate_request_context_len);
|
2022-02-18 05:10:03 +01:00
|
|
|
p += certificate_request_context_len;
|
2022-02-15 07:00:57 +01:00
|
|
|
}
|
|
|
|
|
2022-02-16 04:21:37 +01:00
|
|
|
/* ...
|
|
|
|
* CertificateEntry certificate_list<0..2^24-1>;
|
|
|
|
* ...
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_CHK_BUF_PTR(p, end, 3);
|
2022-02-18 05:10:03 +01:00
|
|
|
p_certificate_list_len = p;
|
2022-02-15 04:05:59 +01:00
|
|
|
p += 3;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_CRT(3, "own certificate", crt);
|
2022-01-28 09:16:08 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
while (crt != NULL) {
|
2022-01-30 10:54:19 +01:00
|
|
|
size_t cert_data_len = crt->raw.len;
|
2022-01-28 09:16:08 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_CHK_BUF_PTR(p, end, cert_data_len + 3 + 2);
|
|
|
|
MBEDTLS_PUT_UINT24_BE(cert_data_len, p, 0);
|
2022-01-30 10:54:19 +01:00
|
|
|
p += 3;
|
2022-01-28 09:16:08 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
memcpy(p, crt->raw.p, cert_data_len);
|
2022-01-30 10:54:19 +01:00
|
|
|
p += cert_data_len;
|
|
|
|
crt = crt->next;
|
2022-01-28 09:16:08 +01:00
|
|
|
|
|
|
|
/* Currently, we don't have any certificate extensions defined.
|
|
|
|
* Hence, we are sending an empty extension with length zero.
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_PUT_UINT16_BE(0, p, 0);
|
2022-01-30 10:54:19 +01:00
|
|
|
p += 2;
|
2022-01-28 09:16:08 +01:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_PUT_UINT24_BE(p - p_certificate_list_len - 3,
|
|
|
|
p_certificate_list_len, 0);
|
2022-01-30 10:54:19 +01:00
|
|
|
|
2022-02-15 04:05:59 +01:00
|
|
|
*out_len = p - buf;
|
2022-01-28 09:16:08 +01:00
|
|
|
|
2022-11-08 14:43:46 +01:00
|
|
|
MBEDTLS_SSL_PRINT_EXTS(
|
2023-01-11 14:50:10 +01:00
|
|
|
3, MBEDTLS_SSL_HS_CERTIFICATE, ssl->handshake->sent_extensions);
|
2022-10-31 06:31:22 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2022-01-28 09:16:08 +01:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_ssl_tls13_write_certificate(mbedtls_ssl_context *ssl)
|
2022-01-28 09:16:08 +01:00
|
|
|
{
|
|
|
|
int ret;
|
2022-03-09 07:00:13 +01:00
|
|
|
unsigned char *buf;
|
|
|
|
size_t buf_len, msg_len;
|
2022-01-28 09:16:08 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(2, ("=> write certificate"));
|
2022-01-28 09:16:08 +01:00
|
|
|
|
2023-03-29 10:24:12 +02:00
|
|
|
MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
|
|
|
|
ssl, MBEDTLS_SSL_HS_CERTIFICATE, &buf, &buf_len));
|
2022-01-28 09:16:08 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_certificate_body(ssl,
|
|
|
|
buf,
|
|
|
|
buf + buf_len,
|
|
|
|
&msg_len));
|
2022-01-28 09:16:08 +01:00
|
|
|
|
2023-03-29 10:24:12 +02:00
|
|
|
MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
|
|
|
|
ssl, MBEDTLS_SSL_HS_CERTIFICATE, buf, msg_len));
|
2022-01-28 09:16:08 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(
|
|
|
|
ssl, buf_len, msg_len));
|
2022-01-28 09:16:08 +01:00
|
|
|
cleanup:
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(2, ("<= write certificate"));
|
|
|
|
return ret;
|
2022-01-28 09:16:08 +01:00
|
|
|
}
|
|
|
|
|
2022-02-15 04:05:59 +01:00
|
|
|
/*
|
|
|
|
* STATE HANDLING: Output Certificate Verify
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_ssl_tls13_check_sig_alg_cert_key_match(uint16_t sig_alg,
|
|
|
|
mbedtls_pk_context *key)
|
2022-01-29 03:01:04 +01:00
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_pk_type_t pk_type = mbedtls_ssl_sig_from_pk(key);
|
|
|
|
size_t key_size = mbedtls_pk_get_bitlen(key);
|
2022-02-23 05:23:05 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
switch (pk_type) {
|
2022-02-17 11:30:13 +01:00
|
|
|
case MBEDTLS_SSL_SIG_ECDSA:
|
2023-01-11 14:50:10 +01:00
|
|
|
switch (key_size) {
|
2022-02-17 11:30:13 +01:00
|
|
|
case 256:
|
2023-01-11 14:50:10 +01:00
|
|
|
return
|
|
|
|
sig_alg == MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256;
|
2022-06-20 14:42:00 +02:00
|
|
|
|
2022-02-17 11:30:13 +01:00
|
|
|
case 384:
|
2023-01-11 14:50:10 +01:00
|
|
|
return
|
|
|
|
sig_alg == MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384;
|
2022-06-20 14:42:00 +02:00
|
|
|
|
2022-02-17 11:30:13 +01:00
|
|
|
case 521:
|
2023-01-11 14:50:10 +01:00
|
|
|
return
|
|
|
|
sig_alg == MBEDTLS_TLS1_3_SIG_ECDSA_SECP521R1_SHA512;
|
2022-02-17 11:30:13 +01:00
|
|
|
default:
|
2022-02-22 03:28:13 +01:00
|
|
|
break;
|
2022-02-17 11:30:13 +01:00
|
|
|
}
|
2022-02-15 04:05:59 +01:00
|
|
|
break;
|
2022-02-17 11:30:13 +01:00
|
|
|
|
2022-02-23 03:40:19 +01:00
|
|
|
case MBEDTLS_SSL_SIG_RSA:
|
2023-01-11 14:50:10 +01:00
|
|
|
switch (sig_alg) {
|
2022-09-16 11:19:27 +02:00
|
|
|
case MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256: /* Intentional fallthrough */
|
|
|
|
case MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384: /* Intentional fallthrough */
|
2022-06-20 14:42:00 +02:00
|
|
|
case MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512:
|
2023-01-11 14:50:10 +01:00
|
|
|
return 1;
|
2022-06-27 16:13:03 +02:00
|
|
|
|
2022-06-20 14:42:00 +02:00
|
|
|
default:
|
|
|
|
break;
|
2022-03-22 16:00:13 +01:00
|
|
|
}
|
2022-02-23 03:40:19 +01:00
|
|
|
break;
|
2022-06-20 14:42:00 +02:00
|
|
|
|
2022-02-15 04:05:59 +01:00
|
|
|
default:
|
2022-02-25 06:37:36 +01:00
|
|
|
break;
|
2022-01-29 03:01:04 +01:00
|
|
|
}
|
2022-06-20 14:42:00 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2022-06-20 14:42:00 +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_tls13_write_certificate_verify_body(mbedtls_ssl_context *ssl,
|
|
|
|
unsigned char *buf,
|
|
|
|
unsigned char *end,
|
|
|
|
size_t *out_len)
|
2022-01-29 03:01:04 +01:00
|
|
|
{
|
2022-09-16 13:44:49 +02:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2022-02-15 04:05:59 +01:00
|
|
|
unsigned char *p = buf;
|
|
|
|
mbedtls_pk_context *own_key;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
unsigned char handshake_hash[MBEDTLS_TLS1_3_MD_MAX_SIZE];
|
2022-02-15 04:05:59 +01:00
|
|
|
size_t handshake_hash_len;
|
2023-01-11 14:50:10 +01:00
|
|
|
unsigned char verify_buffer[SSL_VERIFY_STRUCT_MAX_SIZE];
|
2022-01-29 03:01:04 +01:00
|
|
|
size_t verify_buffer_len;
|
2022-09-16 13:44:49 +02:00
|
|
|
|
|
|
|
uint16_t *sig_alg = ssl->handshake->received_sig_algs;
|
2022-02-15 04:05:59 +01:00
|
|
|
size_t signature_len = 0;
|
2022-01-29 03:01:04 +01:00
|
|
|
|
2022-02-23 05:23:05 +01:00
|
|
|
*out_len = 0;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
own_key = mbedtls_ssl_own_key(ssl);
|
|
|
|
if (own_key == NULL) {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
|
|
|
|
return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
|
2022-01-29 03:01:04 +01:00
|
|
|
}
|
|
|
|
|
2023-03-29 10:24:12 +02:00
|
|
|
ret = mbedtls_ssl_get_handshake_transcript(
|
|
|
|
ssl, ssl->handshake->ciphersuite_info->mac,
|
|
|
|
handshake_hash, sizeof(handshake_hash), &handshake_hash_len);
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ret != 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
2022-01-29 03:01:04 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_BUF(3, "handshake hash",
|
|
|
|
handshake_hash,
|
|
|
|
handshake_hash_len);
|
2022-01-29 03:01:04 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
ssl_tls13_create_verify_structure(handshake_hash, handshake_hash_len,
|
|
|
|
verify_buffer, &verify_buffer_len,
|
|
|
|
ssl->conf->endpoint);
|
2022-01-29 03:01:04 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* struct {
|
|
|
|
* SignatureScheme algorithm;
|
|
|
|
* opaque signature<0..2^16-1>;
|
|
|
|
* } CertificateVerify;
|
|
|
|
*/
|
2022-09-16 13:44:49 +02:00
|
|
|
/* Check there is space for the algorithm identifier (2 bytes) and the
|
|
|
|
* signature length (2 bytes).
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_CHK_BUF_PTR(p, end, 4);
|
2022-09-16 13:44:49 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
for (; *sig_alg != MBEDTLS_TLS1_3_SIG_NONE; sig_alg++) {
|
2022-09-16 13:44:49 +02:00
|
|
|
psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
|
|
|
|
mbedtls_pk_type_t pk_type = MBEDTLS_PK_NONE;
|
|
|
|
mbedtls_md_type_t md_alg = MBEDTLS_MD_NONE;
|
|
|
|
psa_algorithm_t psa_algorithm = PSA_ALG_NONE;
|
|
|
|
unsigned char verify_hash[PSA_HASH_MAX_SIZE];
|
|
|
|
size_t verify_hash_len;
|
2022-02-25 06:37:36 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (!mbedtls_ssl_sig_alg_is_offered(ssl, *sig_alg)) {
|
2022-09-16 13:44:49 +02:00
|
|
|
continue;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2022-02-25 06:37:36 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (!mbedtls_ssl_tls13_sig_alg_for_cert_verify_is_supported(*sig_alg)) {
|
2022-09-16 13:44:49 +02:00
|
|
|
continue;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2022-01-29 03:01:04 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (!mbedtls_ssl_tls13_check_sig_alg_cert_key_match(*sig_alg, own_key)) {
|
2022-09-16 13:44:49 +02:00
|
|
|
continue;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2022-06-19 10:52:27 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (mbedtls_ssl_get_pk_type_and_md_alg_from_sig_alg(
|
|
|
|
*sig_alg, &pk_type, &md_alg) != 0) {
|
|
|
|
return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
|
2022-09-16 13:44:49 +02:00
|
|
|
}
|
2022-03-23 06:34:04 +01:00
|
|
|
|
2022-09-16 13:44:49 +02:00
|
|
|
/* Hash verify buffer with indicated hash function */
|
2023-01-11 14:50:10 +01:00
|
|
|
psa_algorithm = mbedtls_hash_info_psa_from_md(md_alg);
|
|
|
|
status = psa_hash_compute(psa_algorithm,
|
|
|
|
verify_buffer,
|
|
|
|
verify_buffer_len,
|
|
|
|
verify_hash, sizeof(verify_hash),
|
|
|
|
&verify_hash_len);
|
|
|
|
if (status != PSA_SUCCESS) {
|
2022-12-23 17:00:06 +01:00
|
|
|
return PSA_TO_MBEDTLS_ERR(status);
|
2022-09-16 13:44:49 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_BUF(3, "verify hash", verify_hash, verify_hash_len);
|
|
|
|
|
|
|
|
if ((ret = mbedtls_pk_sign_ext(pk_type, own_key,
|
|
|
|
md_alg, verify_hash, verify_hash_len,
|
|
|
|
p + 4, (size_t) (end - (p + 4)), &signature_len,
|
|
|
|
ssl->conf->f_rng, ssl->conf->p_rng)) != 0) {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(2, ("CertificateVerify signature failed with %s",
|
|
|
|
mbedtls_ssl_sig_alg_to_str(*sig_alg)));
|
|
|
|
MBEDTLS_SSL_DEBUG_RET(2, "mbedtls_pk_sign_ext", ret);
|
|
|
|
|
|
|
|
/* The signature failed. This is possible if the private key
|
|
|
|
* was not suitable for the signature operation as purposely we
|
|
|
|
* did not check its suitability completely. Let's try with
|
|
|
|
* another signature algorithm.
|
|
|
|
*/
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(2, ("CertificateVerify signature with %s",
|
|
|
|
mbedtls_ssl_sig_alg_to_str(*sig_alg)));
|
2022-09-16 13:44:49 +02:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (*sig_alg == MBEDTLS_TLS1_3_SIG_NONE) {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(1, ("no suitable signature algorithm"));
|
|
|
|
MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
|
|
|
|
MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
|
|
|
|
return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
|
2022-01-29 03:01:04 +01:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_PUT_UINT16_BE(*sig_alg, p, 0);
|
|
|
|
MBEDTLS_PUT_UINT16_BE(signature_len, p, 2);
|
2022-02-15 04:05:59 +01:00
|
|
|
|
2022-09-16 13:44:49 +02:00
|
|
|
*out_len = 4 + signature_len;
|
2022-01-29 03:01:04 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2022-01-29 03:01:04 +01:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_ssl_tls13_write_certificate_verify(mbedtls_ssl_context *ssl)
|
2022-01-29 03:01:04 +01:00
|
|
|
{
|
|
|
|
int ret = 0;
|
2022-02-15 07:22:05 +01:00
|
|
|
unsigned char *buf;
|
|
|
|
size_t buf_len, msg_len;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(2, ("=> write certificate verify"));
|
2022-01-29 03:01:04 +01:00
|
|
|
|
2023-03-29 10:24:12 +02:00
|
|
|
MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
|
|
|
|
ssl, MBEDTLS_SSL_HS_CERTIFICATE_VERIFY,
|
|
|
|
&buf, &buf_len));
|
2022-01-29 03:01:04 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_certificate_verify_body(
|
|
|
|
ssl, buf, buf + buf_len, &msg_len));
|
2022-01-29 03:01:04 +01:00
|
|
|
|
2023-03-29 10:24:12 +02:00
|
|
|
MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
|
|
|
|
ssl, MBEDTLS_SSL_HS_CERTIFICATE_VERIFY,
|
|
|
|
buf, msg_len));
|
2022-01-29 03:01:04 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(
|
|
|
|
ssl, buf_len, msg_len));
|
2022-01-29 03:01:04 +01:00
|
|
|
|
|
|
|
cleanup:
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(2, ("<= write certificate verify"));
|
|
|
|
return ret;
|
2022-01-29 03:01:04 +01:00
|
|
|
}
|
|
|
|
|
2022-10-04 16:14:26 +02:00
|
|
|
#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
|
2022-01-29 15:12:42 +01:00
|
|
|
|
2021-09-18 08:20:25 +02:00
|
|
|
/*
|
|
|
|
*
|
2021-11-09 12:55:10 +01:00
|
|
|
* STATE HANDLING: Incoming Finished message.
|
2021-09-18 08:20:25 +02:00
|
|
|
*/
|
|
|
|
/*
|
|
|
|
* Implementation
|
|
|
|
*/
|
|
|
|
|
2022-06-17 10:52:54 +02:00
|
|
|
MBEDTLS_CHECK_RETURN_CRITICAL
|
2023-01-11 14:50:10 +01:00
|
|
|
static int ssl_tls13_preprocess_finished_message(mbedtls_ssl_context *ssl)
|
2021-09-18 08:20:25 +02:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
2023-03-29 10:24:12 +02:00
|
|
|
ret = mbedtls_ssl_tls13_calculate_verify_data(
|
|
|
|
ssl,
|
|
|
|
ssl->handshake->state_local.finished_in.digest,
|
|
|
|
sizeof(ssl->handshake->state_local.finished_in.digest),
|
|
|
|
&ssl->handshake->state_local.finished_in.digest_len,
|
|
|
|
ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ?
|
|
|
|
MBEDTLS_SSL_IS_SERVER : MBEDTLS_SSL_IS_CLIENT);
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ret != 0) {
|
|
|
|
MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_tls13_calculate_verify_data", ret);
|
|
|
|
return ret;
|
2021-09-18 08:20:25 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2021-09-18 08:20:25 +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_tls13_parse_finished_message(mbedtls_ssl_context *ssl,
|
|
|
|
const unsigned char *buf,
|
|
|
|
const unsigned char *end)
|
2021-09-18 08:20:25 +02:00
|
|
|
{
|
2021-11-11 04:37:45 +01:00
|
|
|
/*
|
|
|
|
* struct {
|
2021-11-11 07:13:22 +01:00
|
|
|
* opaque verify_data[Hash.length];
|
2021-11-11 04:37:45 +01:00
|
|
|
* } Finished;
|
|
|
|
*/
|
|
|
|
const unsigned char *expected_verify_data =
|
|
|
|
ssl->handshake->state_local.finished_in.digest;
|
|
|
|
size_t expected_verify_data_len =
|
|
|
|
ssl->handshake->state_local.finished_in.digest_len;
|
2021-09-18 08:20:25 +02:00
|
|
|
/* Structural validation */
|
2023-01-11 14:50:10 +01:00
|
|
|
if ((size_t) (end - buf) != expected_verify_data_len) {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(1, ("bad finished message"));
|
2021-09-18 08:20:25 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
|
|
|
|
MBEDTLS_ERR_SSL_DECODE_ERROR);
|
|
|
|
return MBEDTLS_ERR_SSL_DECODE_ERROR;
|
2021-09-18 08:20:25 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_BUF(4, "verify_data (self-computed):",
|
|
|
|
expected_verify_data,
|
|
|
|
expected_verify_data_len);
|
|
|
|
MBEDTLS_SSL_DEBUG_BUF(4, "verify_data (received message):", buf,
|
|
|
|
expected_verify_data_len);
|
2021-09-18 08:20:25 +02:00
|
|
|
|
|
|
|
/* Semantic validation */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (mbedtls_ct_memcmp(buf,
|
|
|
|
expected_verify_data,
|
|
|
|
expected_verify_data_len) != 0) {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(1, ("bad finished message"));
|
|
|
|
|
|
|
|
MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR,
|
|
|
|
MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
|
|
|
|
return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
|
2021-09-18 08:20:25 +02:00
|
|
|
}
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2021-09-18 08:20:25 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_ssl_tls13_process_finished_message(mbedtls_ssl_context *ssl)
|
2021-11-09 12:55:10 +01:00
|
|
|
{
|
2021-11-11 04:37:45 +01:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2021-11-09 12:55:10 +01:00
|
|
|
unsigned char *buf;
|
2021-11-18 08:29:56 +01:00
|
|
|
size_t buf_len;
|
2021-11-09 12:55:10 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse finished message"));
|
2021-11-09 12:55:10 +01:00
|
|
|
|
2023-03-29 10:24:12 +02:00
|
|
|
MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_tls13_fetch_handshake_msg(
|
|
|
|
ssl, MBEDTLS_SSL_HS_FINISHED, &buf, &buf_len));
|
2022-05-16 10:54:46 +02:00
|
|
|
|
|
|
|
/* Preprocessing step: Compute handshake digest */
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_PROC_CHK(ssl_tls13_preprocess_finished_message(ssl));
|
2022-05-16 10:54:46 +02:00
|
|
|
|
2023-03-29 10:24:12 +02:00
|
|
|
MBEDTLS_SSL_PROC_CHK(ssl_tls13_parse_finished_message(
|
|
|
|
ssl, buf, buf + buf_len));
|
2022-05-16 10:54:46 +02:00
|
|
|
|
2023-03-29 10:24:12 +02:00
|
|
|
MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
|
|
|
|
ssl, MBEDTLS_SSL_HS_FINISHED, buf, buf_len));
|
2021-11-09 12:55:10 +01:00
|
|
|
|
|
|
|
cleanup:
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse finished message"));
|
|
|
|
return ret;
|
2021-11-09 12:55:10 +01:00
|
|
|
}
|
|
|
|
|
2021-09-22 09:40:30 +02:00
|
|
|
/*
|
|
|
|
*
|
2021-11-09 13:30:09 +01:00
|
|
|
* STATE HANDLING: Write and send Finished message.
|
2021-09-22 09:40:30 +02:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
/*
|
2021-11-11 09:16:19 +01:00
|
|
|
* Implement
|
2021-09-22 09:40:30 +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_tls13_prepare_finished_message(mbedtls_ssl_context *ssl)
|
2021-09-22 09:40:30 +02:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Compute transcript of handshake up to now. */
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = mbedtls_ssl_tls13_calculate_verify_data(ssl,
|
|
|
|
ssl->handshake->state_local.finished_out.digest,
|
|
|
|
sizeof(ssl->handshake->state_local.finished_out.
|
|
|
|
digest),
|
|
|
|
&ssl->handshake->state_local.finished_out.digest_len,
|
|
|
|
ssl->conf->endpoint);
|
|
|
|
|
|
|
|
if (ret != 0) {
|
|
|
|
MBEDTLS_SSL_DEBUG_RET(1, "calculate_verify_data failed", ret);
|
|
|
|
return ret;
|
2021-09-22 09:40:30 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2021-09-22 09:40:30 +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_tls13_write_finished_message_body(mbedtls_ssl_context *ssl,
|
|
|
|
unsigned char *buf,
|
|
|
|
unsigned char *end,
|
|
|
|
size_t *out_len)
|
2021-09-22 09:40:30 +02:00
|
|
|
{
|
2021-11-10 08:33:09 +01:00
|
|
|
size_t verify_data_len = ssl->handshake->state_local.finished_out.digest_len;
|
2021-11-15 04:33:57 +01:00
|
|
|
/*
|
|
|
|
* struct {
|
|
|
|
* opaque verify_data[Hash.length];
|
|
|
|
* } Finished;
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_CHK_BUF_PTR(buf, end, verify_data_len);
|
2021-09-22 09:40:30 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
memcpy(buf, ssl->handshake->state_local.finished_out.digest,
|
|
|
|
verify_data_len);
|
2021-09-22 09:40:30 +02:00
|
|
|
|
2021-12-02 07:36:27 +01:00
|
|
|
*out_len = verify_data_len;
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2021-09-22 09:40:30 +02:00
|
|
|
}
|
2021-11-09 12:55:10 +01:00
|
|
|
|
2021-11-11 09:16:19 +01:00
|
|
|
/* Main entry point: orchestrates the other functions */
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_ssl_tls13_write_finished_message(mbedtls_ssl_context *ssl)
|
2021-11-11 09:16:19 +01:00
|
|
|
{
|
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
|
|
|
unsigned char *buf;
|
|
|
|
size_t buf_len, msg_len;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(2, ("=> write finished message"));
|
2021-11-11 09:16:19 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_PROC_CHK(ssl_tls13_prepare_finished_message(ssl));
|
2021-11-15 07:01:26 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(ssl,
|
|
|
|
MBEDTLS_SSL_HS_FINISHED, &buf, &buf_len));
|
2021-11-11 09:16:19 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_finished_message_body(
|
|
|
|
ssl, buf, buf + buf_len, &msg_len));
|
2021-11-11 09:16:19 +01:00
|
|
|
|
2023-02-06 00:34:21 +01:00
|
|
|
MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(ssl,
|
2023-02-06 11:48:19 +01:00
|
|
|
MBEDTLS_SSL_HS_FINISHED, buf, msg_len));
|
2021-11-11 09:16:19 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(
|
|
|
|
ssl, buf_len, msg_len));
|
2021-11-11 09:16:19 +01:00
|
|
|
cleanup:
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(2, ("<= write finished message"));
|
|
|
|
return ret;
|
2021-11-11 09:16:19 +01:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
void mbedtls_ssl_tls13_handshake_wrapup(mbedtls_ssl_context *ssl)
|
2021-10-30 15:44:47 +02:00
|
|
|
{
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(3, ("=> handshake wrapup"));
|
2021-10-30 15:44:47 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(1, ("Switch to application keys for inbound traffic"));
|
|
|
|
mbedtls_ssl_set_inbound_transform(ssl, ssl->transform_application);
|
2022-05-18 08:48:56 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(1, ("Switch to application keys for outbound traffic"));
|
|
|
|
mbedtls_ssl_set_outbound_transform(ssl, ssl->transform_application);
|
2022-05-18 08:48:56 +02:00
|
|
|
|
2021-10-30 15:44:47 +02:00
|
|
|
/*
|
2021-11-15 06:54:06 +01:00
|
|
|
* Free the previous session and switch to the current one.
|
2021-10-30 15:44:47 +02:00
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->session) {
|
|
|
|
mbedtls_ssl_session_free(ssl->session);
|
|
|
|
mbedtls_free(ssl->session);
|
2021-10-30 15:44:47 +02:00
|
|
|
}
|
|
|
|
ssl->session = ssl->session_negotiate;
|
|
|
|
ssl->session_negotiate = NULL;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(3, ("<= handshake wrapup"));
|
2021-10-30 15:44:47 +02:00
|
|
|
}
|
|
|
|
|
2021-11-24 16:25:31 +01:00
|
|
|
/*
|
|
|
|
*
|
|
|
|
* STATE HANDLING: Write ChangeCipherSpec
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
|
2022-06-17 10:52:54 +02:00
|
|
|
MBEDTLS_CHECK_RETURN_CRITICAL
|
2023-01-11 14:50:10 +01:00
|
|
|
static int ssl_tls13_write_change_cipher_spec_body(mbedtls_ssl_context *ssl,
|
|
|
|
unsigned char *buf,
|
|
|
|
unsigned char *end,
|
|
|
|
size_t *olen)
|
2021-11-24 16:25:31 +01:00
|
|
|
{
|
|
|
|
((void) ssl);
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_CHK_BUF_PTR(buf, end, 1);
|
2021-11-24 16:25:31 +01:00
|
|
|
buf[0] = 1;
|
|
|
|
*olen = 1;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2021-11-24 16:25:31 +01:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_ssl_tls13_write_change_cipher_spec(mbedtls_ssl_context *ssl)
|
2021-11-24 16:25:31 +01:00
|
|
|
{
|
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(2, ("=> write change cipher spec"));
|
2021-11-24 16:25:31 +01:00
|
|
|
|
|
|
|
/* Write CCS message */
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_change_cipher_spec_body(
|
|
|
|
ssl, ssl->out_msg,
|
|
|
|
ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN,
|
|
|
|
&ssl->out_msglen));
|
2021-11-24 16:25:31 +01:00
|
|
|
|
|
|
|
ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
|
|
|
|
|
|
|
|
/* Dispatch message */
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_write_record(ssl, 0));
|
2021-11-24 16:25:31 +01:00
|
|
|
|
|
|
|
cleanup:
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(2, ("<= write change cipher spec"));
|
|
|
|
return ret;
|
2021-11-24 16:25:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
|
|
|
|
|
2022-11-02 08:52:47 +01:00
|
|
|
/* Early Data Indication Extension
|
2022-10-24 13:12:51 +02:00
|
|
|
*
|
|
|
|
* struct {
|
|
|
|
* select ( Handshake.msg_type ) {
|
2022-11-02 08:52:47 +01:00
|
|
|
* ...
|
2022-10-24 13:12:51 +02:00
|
|
|
* case client_hello: Empty;
|
|
|
|
* case encrypted_extensions: Empty;
|
|
|
|
* };
|
|
|
|
* } EarlyDataIndication;
|
|
|
|
*/
|
|
|
|
#if defined(MBEDTLS_SSL_EARLY_DATA)
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_ssl_tls13_write_early_data_ext(mbedtls_ssl_context *ssl,
|
|
|
|
unsigned char *buf,
|
|
|
|
const unsigned char *end,
|
|
|
|
size_t *out_len)
|
2022-10-24 13:12:51 +02:00
|
|
|
{
|
|
|
|
unsigned char *p = buf;
|
|
|
|
*out_len = 0;
|
|
|
|
((void) ssl);
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_CHK_BUF_PTR(p, end, 4);
|
2022-10-24 13:12:51 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_EARLY_DATA, p, 0);
|
|
|
|
MBEDTLS_PUT_UINT16_BE(0, p, 2);
|
2022-10-24 13:12:51 +02:00
|
|
|
|
|
|
|
*out_len = 4;
|
2022-11-15 11:33:53 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_EARLY_DATA);
|
2022-11-15 11:33:53 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2022-10-24 13:12:51 +02:00
|
|
|
}
|
|
|
|
#endif /* MBEDTLS_SSL_EARLY_DATA */
|
|
|
|
|
2022-01-19 07:56:30 +01:00
|
|
|
/* Reset SSL context and update hash for handling HRR.
|
|
|
|
*
|
|
|
|
* Replace Transcript-Hash(X) by
|
|
|
|
* Transcript-Hash( message_hash ||
|
|
|
|
* 00 00 Hash.length ||
|
|
|
|
* X )
|
|
|
|
* A few states of the handshake are preserved, including:
|
|
|
|
* - session ID
|
|
|
|
* - session ticket
|
|
|
|
* - negotiated ciphersuite
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_ssl_reset_transcript_for_hrr(mbedtls_ssl_context *ssl)
|
2022-01-19 07:56:30 +01:00
|
|
|
{
|
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2022-09-14 12:50:51 +02:00
|
|
|
unsigned char hash_transcript[PSA_HASH_MAX_SIZE + 4];
|
2022-01-24 09:56:23 +01:00
|
|
|
size_t hash_len;
|
2023-02-07 04:17:45 +01:00
|
|
|
const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
|
|
|
|
ssl->handshake->ciphersuite_info;
|
2022-01-19 07:56:30 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(3, ("Reset SSL session for HRR"));
|
2022-01-19 07:56:30 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = mbedtls_ssl_get_handshake_transcript(ssl, ciphersuite_info->mac,
|
|
|
|
hash_transcript + 4,
|
|
|
|
PSA_HASH_MAX_SIZE,
|
|
|
|
&hash_len);
|
|
|
|
if (ret != 0) {
|
2023-02-21 09:31:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_get_handshake_transcript", ret);
|
2023-01-11 14:50:10 +01:00
|
|
|
return ret;
|
2022-01-24 09:56:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
hash_transcript[0] = MBEDTLS_SSL_HS_MESSAGE_HASH;
|
|
|
|
hash_transcript[1] = 0;
|
|
|
|
hash_transcript[2] = 0;
|
|
|
|
hash_transcript[3] = (unsigned char) hash_len;
|
|
|
|
|
|
|
|
hash_len += 4;
|
|
|
|
|
2023-02-21 09:31:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_BUF(4, "Truncated handshake transcript",
|
|
|
|
hash_transcript, hash_len);
|
|
|
|
|
2023-02-05 10:26:49 +01:00
|
|
|
/* Reset running hash and replace it with a hash of the transcript */
|
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;
|
|
|
|
}
|
|
|
|
ret = ssl->handshake->update_checksum(ssl, hash_transcript, hash_len);
|
|
|
|
if (ret != 0) {
|
|
|
|
MBEDTLS_SSL_DEBUG_RET(1, "update_checksum", ret);
|
|
|
|
return ret;
|
|
|
|
}
|
2022-02-14 16:39:52 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return ret;
|
2022-01-19 07:56:30 +01:00
|
|
|
}
|
|
|
|
|
2023-03-20 15:22:47 +01:00
|
|
|
#if defined(PSA_WANT_ALG_ECDH)
|
2022-02-15 11:04:37 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_ssl_tls13_read_public_ecdhe_share(mbedtls_ssl_context *ssl,
|
|
|
|
const unsigned char *buf,
|
|
|
|
size_t buf_len)
|
2022-02-15 11:04:37 +01:00
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
uint8_t *p = (uint8_t *) buf;
|
2022-04-14 09:10:37 +02:00
|
|
|
const uint8_t *end = buf + buf_len;
|
2022-04-10 12:20:43 +02:00
|
|
|
mbedtls_ssl_handshake_params *handshake = ssl->handshake;
|
2022-02-15 11:04:37 +01:00
|
|
|
|
2022-04-10 12:20:43 +02:00
|
|
|
/* Get size of the TLS opaque key_exchange field of the KeyShareEntry struct. */
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
|
|
|
|
uint16_t peerkey_len = MBEDTLS_GET_UINT16_BE(p, 0);
|
2022-04-10 12:20:43 +02:00
|
|
|
p += 2;
|
2022-02-23 04:15:27 +01:00
|
|
|
|
2022-04-10 12:20:43 +02:00
|
|
|
/* Check if key size is consistent with given buffer length. */
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, peerkey_len);
|
2022-02-23 04:15:27 +01:00
|
|
|
|
2022-04-10 12:20:43 +02:00
|
|
|
/* Store peer's ECDH public key. */
|
2023-01-11 14:50:10 +01:00
|
|
|
memcpy(handshake->ecdh_psa_peerkey, p, peerkey_len);
|
2022-04-10 12:20:43 +02:00
|
|
|
handshake->ecdh_psa_peerkey_len = peerkey_len;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2022-02-15 11:04:37 +01:00
|
|
|
}
|
2022-03-30 16:43:29 +02:00
|
|
|
|
|
|
|
int mbedtls_ssl_tls13_generate_and_write_ecdh_key_exchange(
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_ssl_context *ssl,
|
|
|
|
uint16_t named_group,
|
|
|
|
unsigned char *buf,
|
|
|
|
unsigned char *end,
|
|
|
|
size_t *out_len)
|
2022-03-30 16:43:29 +02:00
|
|
|
{
|
|
|
|
psa_status_t status = PSA_ERROR_GENERIC_ERROR;
|
|
|
|
int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
|
|
|
|
psa_key_attributes_t key_attributes;
|
|
|
|
size_t own_pubkey_len;
|
|
|
|
mbedtls_ssl_handshake_params *handshake = ssl->handshake;
|
2023-01-04 16:08:04 +01:00
|
|
|
psa_ecc_family_t ec_psa_family = 0;
|
|
|
|
size_t ec_bits = 0;
|
2022-03-30 16:43:29 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(1, ("Perform PSA-based ECDH computation."));
|
2022-03-30 16:43:29 +02:00
|
|
|
|
2023-01-04 16:08:04 +01:00
|
|
|
/* Convert EC's TLS ID to PSA key type. */
|
2023-03-29 10:24:12 +02:00
|
|
|
if (mbedtls_ssl_get_psa_curve_info_from_tls_id(
|
|
|
|
named_group, &ec_psa_family, &ec_bits) == PSA_ERROR_NOT_SUPPORTED) {
|
2023-01-11 14:50:10 +01:00
|
|
|
return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
|
2023-01-04 16:08:04 +01:00
|
|
|
}
|
2023-01-11 14:50:10 +01:00
|
|
|
handshake->ecdh_psa_type = PSA_KEY_TYPE_ECC_KEY_PAIR(ec_psa_family);
|
2023-01-04 16:08:04 +01:00
|
|
|
ssl->handshake->ecdh_bits = ec_bits;
|
2022-03-30 16:43:29 +02:00
|
|
|
|
|
|
|
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, PSA_ALG_ECDH);
|
|
|
|
psa_set_key_type(&key_attributes, handshake->ecdh_psa_type);
|
|
|
|
psa_set_key_bits(&key_attributes, handshake->ecdh_bits);
|
2022-03-30 16:43:29 +02:00
|
|
|
|
|
|
|
/* Generate ECDH private key. */
|
2023-01-11 14:50:10 +01:00
|
|
|
status = psa_generate_key(&key_attributes,
|
|
|
|
&handshake->ecdh_psa_privkey);
|
|
|
|
if (status != 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_generate_key", ret);
|
|
|
|
return ret;
|
2022-03-30 16:43:29 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Export the public part of the ECDH private key from PSA. */
|
2023-01-11 14:50:10 +01:00
|
|
|
status = psa_export_public_key(handshake->ecdh_psa_privkey,
|
|
|
|
buf, (size_t) (end - buf),
|
|
|
|
&own_pubkey_len);
|
|
|
|
if (status != 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_export_public_key", ret);
|
|
|
|
return ret;
|
2022-03-30 16:43:29 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
*out_len = own_pubkey_len;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2022-03-30 16:43:29 +02:00
|
|
|
}
|
2023-03-20 15:22:47 +01:00
|
|
|
#endif /* PSA_WANT_ALG_ECDH */
|
2022-02-15 11:04:37 +01:00
|
|
|
|
2022-08-29 09:25:36 +02:00
|
|
|
/* RFC 8446 section 4.2
|
|
|
|
*
|
|
|
|
* If an implementation receives an extension which it recognizes and which is
|
|
|
|
* not specified for the message in which it appears, it MUST abort the handshake
|
|
|
|
* with an "illegal_parameter" alert.
|
|
|
|
*
|
|
|
|
*/
|
2022-10-29 03:08:47 +02:00
|
|
|
int mbedtls_ssl_tls13_check_received_extension(
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_ssl_context *ssl,
|
|
|
|
int hs_msg_type,
|
|
|
|
unsigned int received_extension_type,
|
|
|
|
uint32_t hs_msg_allowed_extensions_mask)
|
2022-08-29 09:25:36 +02:00
|
|
|
{
|
2022-10-31 06:20:57 +01:00
|
|
|
uint32_t extension_mask = mbedtls_ssl_get_extension_mask(
|
2023-01-11 14:50:10 +01:00
|
|
|
received_extension_type);
|
2022-08-29 09:25:36 +02:00
|
|
|
|
2022-11-08 14:30:21 +01:00
|
|
|
MBEDTLS_SSL_PRINT_EXT(
|
2023-01-11 14:50:10 +01:00
|
|
|
3, hs_msg_type, received_extension_type, "received");
|
2022-08-29 09:25:36 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if ((extension_mask & hs_msg_allowed_extensions_mask) == 0) {
|
2022-11-08 14:30:21 +01:00
|
|
|
MBEDTLS_SSL_PRINT_EXT(
|
2023-01-11 14:50:10 +01:00
|
|
|
3, hs_msg_type, received_extension_type, "is illegal");
|
2022-08-29 09:25:36 +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-08-29 09:25:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ssl->handshake->received_extensions |= extension_mask;
|
2022-10-29 03:08:47 +02:00
|
|
|
/*
|
|
|
|
* If it is a message containing extension responses, check that we
|
|
|
|
* previously sent the extension.
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
switch (hs_msg_type) {
|
2022-08-29 09:25:36 +02:00
|
|
|
case MBEDTLS_SSL_HS_SERVER_HELLO:
|
2022-10-31 06:20:57 +01:00
|
|
|
case MBEDTLS_SSL_TLS1_3_HS_HELLO_RETRY_REQUEST:
|
2022-08-29 09:25:36 +02:00
|
|
|
case MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS:
|
|
|
|
case MBEDTLS_SSL_HS_CERTIFICATE:
|
2022-10-29 03:08:47 +02:00
|
|
|
/* Check if the received extension is sent by peer message.*/
|
2023-01-11 14:50:10 +01:00
|
|
|
if ((ssl->handshake->sent_extensions & extension_mask) != 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
2022-08-29 09:25:36 +02:00
|
|
|
break;
|
|
|
|
default:
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2022-08-29 09:25:36 +02:00
|
|
|
}
|
|
|
|
|
2022-11-08 14:30:21 +01:00
|
|
|
MBEDTLS_SSL_PRINT_EXT(
|
2023-01-11 14:50:10 +01:00
|
|
|
3, hs_msg_type, received_extension_type, "is unsupported");
|
2022-08-29 09:25:36 +02:00
|
|
|
MBEDTLS_SSL_PEND_FATAL_ALERT(
|
|
|
|
MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT,
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION);
|
|
|
|
return MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION;
|
2022-08-29 09:25:36 +02:00
|
|
|
}
|
|
|
|
|
2023-02-10 12:45:19 +01:00
|
|
|
#if defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT)
|
2023-03-15 14:15:11 +01:00
|
|
|
/* RFC 8449, section 4:
|
|
|
|
*
|
2023-02-10 12:45:19 +01:00
|
|
|
* The ExtensionData of the "record_size_limit" extension is
|
|
|
|
* RecordSizeLimit:
|
|
|
|
* uint16 RecordSizeLimit;
|
|
|
|
*/
|
|
|
|
MBEDTLS_CHECK_RETURN_CRITICAL
|
|
|
|
int mbedtls_ssl_tls13_parse_record_size_limit_ext(mbedtls_ssl_context *ssl,
|
|
|
|
const unsigned char *buf,
|
|
|
|
const unsigned char *end)
|
|
|
|
{
|
2023-03-15 14:15:11 +01:00
|
|
|
const unsigned char *p = buf;
|
|
|
|
uint16_t record_size_limit;
|
2023-03-15 11:04:45 +01:00
|
|
|
const size_t extension_data_len = end - buf;
|
2023-03-15 14:15:11 +01:00
|
|
|
|
2023-03-29 10:24:12 +02:00
|
|
|
if (extension_data_len !=
|
|
|
|
MBEDTLS_SSL_RECORD_SIZE_LIMIT_EXTENSION_DATA_LENGTH) {
|
2023-02-10 12:45:19 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(2,
|
2023-03-15 14:15:11 +01:00
|
|
|
("record_size_limit extension has invalid length: %"
|
|
|
|
MBEDTLS_PRINTF_SIZET " Bytes",
|
2023-02-10 12:45:19 +01:00
|
|
|
extension_data_len));
|
|
|
|
|
|
|
|
MBEDTLS_SSL_PEND_FATAL_ALERT(
|
|
|
|
MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
|
|
|
|
MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
|
|
|
|
return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
|
|
|
|
}
|
|
|
|
|
|
|
|
MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
|
|
|
|
record_size_limit = MBEDTLS_GET_UINT16_BE(p, 0);
|
|
|
|
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(2, ("RecordSizeLimit: %u Bytes", record_size_limit));
|
|
|
|
|
2023-03-15 14:15:11 +01:00
|
|
|
/* RFC 8449, section 4
|
2023-02-10 12:45:19 +01:00
|
|
|
*
|
|
|
|
* Endpoints MUST NOT send a "record_size_limit" extension with a value
|
|
|
|
* smaller than 64. An endpoint MUST treat receipt of a smaller value
|
|
|
|
* as a fatal error and generate an "illegal_parameter" alert.
|
|
|
|
*/
|
2023-03-15 11:04:45 +01:00
|
|
|
if (record_size_limit < MBEDTLS_SSL_RECORD_SIZE_LIMIT_MIN) {
|
2023-02-10 12:45:19 +01:00
|
|
|
MBEDTLS_SSL_PEND_FATAL_ALERT(
|
|
|
|
MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
|
|
|
|
MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
|
|
|
|
return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
|
|
|
|
}
|
|
|
|
|
2023-03-29 10:24:12 +02:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(
|
|
|
|
2, ("record_size_limit extension is still in development. Aborting handshake."));
|
2023-02-10 12:45:19 +01:00
|
|
|
|
|
|
|
MBEDTLS_SSL_PEND_FATAL_ALERT(
|
|
|
|
MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT,
|
|
|
|
MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION);
|
|
|
|
return MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION;
|
|
|
|
}
|
|
|
|
#endif /* MBEDTLS_SSL_RECORD_SIZE_LIMIT */
|
|
|
|
|
2022-01-27 08:03:26 +01:00
|
|
|
#endif /* MBEDTLS_SSL_TLS_C && MBEDTLS_SSL_PROTO_TLS1_3 */
|