2021-08-06 10:29:08 +02:00
|
|
|
/*
|
|
|
|
* TLS 1.3 client-side functions
|
|
|
|
*
|
|
|
|
* 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
|
2021-08-06 10:29:08 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "common.h"
|
|
|
|
|
2022-01-28 03:24:45 +01:00
|
|
|
#if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
|
2021-08-06 10:29:08 +02:00
|
|
|
|
2021-08-24 09:59:48 +02:00
|
|
|
#include <string.h>
|
|
|
|
|
2021-09-01 11:48:49 +02:00
|
|
|
#include "mbedtls/debug.h"
|
|
|
|
#include "mbedtls/error.h"
|
2021-09-10 04:08:31 +02:00
|
|
|
#include "mbedtls/platform.h"
|
2021-08-17 04:44:40 +02:00
|
|
|
|
2021-09-14 13:30:36 +02:00
|
|
|
#include "ssl_misc.h"
|
2022-02-18 17:24:56 +01:00
|
|
|
#include "ssl_client.h"
|
2021-09-10 04:08:31 +02:00
|
|
|
#include "ssl_tls13_keys.h"
|
2022-08-04 10:55:10 +02:00
|
|
|
#include "ssl_debug_helpers.h"
|
2023-03-28 12:33:20 +02:00
|
|
|
#include "md_psa.h"
|
2021-09-14 13:30:36 +02:00
|
|
|
|
2023-07-25 11:23:50 +02:00
|
|
|
#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED)
|
2023-05-30 11:45:00 +02:00
|
|
|
/* Define a local translating function 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)
|
2023-05-30 21:16:34 +02:00
|
|
|
#endif
|
|
|
|
|
2021-08-27 10:59:09 +02:00
|
|
|
/* Write extensions */
|
2021-08-18 10:38:40 +02:00
|
|
|
|
2021-08-27 10:59:09 +02:00
|
|
|
/*
|
|
|
|
* ssl_tls13_write_supported_versions_ext():
|
|
|
|
*
|
|
|
|
* struct {
|
|
|
|
* ProtocolVersion versions<2..254>;
|
|
|
|
* } SupportedVersions;
|
|
|
|
*/
|
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_supported_versions_ext(mbedtls_ssl_context *ssl,
|
|
|
|
unsigned char *buf,
|
|
|
|
unsigned char *end,
|
|
|
|
size_t *out_len)
|
2021-08-17 04:44:40 +02:00
|
|
|
{
|
2021-08-27 10:59:09 +02:00
|
|
|
unsigned char *p = buf;
|
2023-01-11 14:50:10 +01:00
|
|
|
unsigned char versions_len = (ssl->handshake->min_tls_version <=
|
|
|
|
MBEDTLS_SSL_VERSION_TLS1_2) ? 4 : 2;
|
2021-08-18 10:38:40 +02:00
|
|
|
|
2021-12-02 07:36:27 +01:00
|
|
|
*out_len = 0;
|
2021-08-18 10:38:40 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(3, ("client hello, adding supported versions extension"));
|
2021-08-17 04:44:40 +02:00
|
|
|
|
2021-09-15 12:41:02 +02:00
|
|
|
/* Check if we have space to write the extension:
|
2021-09-08 10:41:02 +02:00
|
|
|
* - extension_type (2 bytes)
|
|
|
|
* - extension_data_length (2 bytes)
|
|
|
|
* - versions_length (1 byte )
|
2022-03-30 17:20:47 +02:00
|
|
|
* - versions (2 or 4 bytes)
|
2021-08-31 06:51:25 +02:00
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_CHK_BUF_PTR(p, end, 5 + versions_len);
|
2021-08-06 10:29:08 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS, p, 0);
|
|
|
|
MBEDTLS_PUT_UINT16_BE(versions_len + 1, p, 2);
|
2021-08-30 12:32:07 +02:00
|
|
|
p += 4;
|
2021-08-18 10:46:28 +02:00
|
|
|
|
2021-09-01 06:57:29 +02:00
|
|
|
/* Length of versions */
|
2022-02-10 14:35:27 +01:00
|
|
|
*p++ = versions_len;
|
2021-08-27 09:14:01 +02:00
|
|
|
|
2021-09-02 06:59:12 +02:00
|
|
|
/* Write values of supported versions.
|
|
|
|
* They are defined by the configuration.
|
2022-02-10 14:35:27 +01:00
|
|
|
* Currently, we advertise only TLS 1.3 or both TLS 1.3 and TLS 1.2.
|
2021-08-27 10:59:09 +02:00
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_ssl_write_version(p, MBEDTLS_SSL_TRANSPORT_STREAM,
|
|
|
|
MBEDTLS_SSL_VERSION_TLS1_3);
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(3, ("supported version: [3:4]"));
|
2021-08-18 10:46:28 +02:00
|
|
|
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->handshake->min_tls_version <= MBEDTLS_SSL_VERSION_TLS1_2) {
|
|
|
|
mbedtls_ssl_write_version(p + 2, MBEDTLS_SSL_TRANSPORT_STREAM,
|
|
|
|
MBEDTLS_SSL_VERSION_TLS1_2);
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(3, ("supported version: [3:3]"));
|
2022-02-10 14:35:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
*out_len = 5 + versions_len;
|
2022-10-31 06:31:22 +01:00
|
|
|
|
2022-10-29 03:08:47 +02:00
|
|
|
mbedtls_ssl_tls13_set_hs_sent_ext_mask(
|
2023-01-11 14:50:10 +01:00
|
|
|
ssl, MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS);
|
2022-10-31 06:31:22 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2021-08-18 10:38:40 +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_supported_versions_ext(mbedtls_ssl_context *ssl,
|
|
|
|
const unsigned char *buf,
|
|
|
|
const unsigned char *end)
|
2021-09-10 04:08:31 +02:00
|
|
|
{
|
|
|
|
((void) ssl);
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_CHK_BUF_READ_PTR(buf, end, 2);
|
|
|
|
if (mbedtls_ssl_read_version(buf, ssl->conf->transport) !=
|
|
|
|
MBEDTLS_SSL_VERSION_TLS1_3) {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(1, ("unexpected version"));
|
2021-10-11 15:45:31 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
|
|
|
|
MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
|
|
|
|
return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
|
2021-09-10 04:08:31 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (&buf[2] != end) {
|
2023-04-03 11:07:03 +02:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(
|
|
|
|
1, ("supported_versions ext data length incorrect"));
|
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;
|
2022-03-30 20:04:10 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2021-09-10 04:08:31 +02:00
|
|
|
}
|
|
|
|
|
2022-01-21 16:34:27 +01:00
|
|
|
#if defined(MBEDTLS_SSL_ALPN)
|
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_alpn_ext(mbedtls_ssl_context *ssl,
|
|
|
|
const unsigned char *buf, size_t len)
|
2022-01-21 16:34:27 +01:00
|
|
|
{
|
|
|
|
const unsigned char *p = buf;
|
|
|
|
const unsigned char *end = buf + len;
|
2022-05-31 16:04:11 +02:00
|
|
|
size_t protocol_name_list_len, protocol_name_len;
|
|
|
|
const unsigned char *protocol_name_list_end;
|
2022-01-21 16:34:27 +01:00
|
|
|
|
|
|
|
/* If we didn't send it, the server shouldn't send it */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->conf->alpn_list == NULL) {
|
|
|
|
return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
|
|
|
|
}
|
2022-01-21 16:34:27 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* opaque ProtocolName<1..2^8-1>;
|
|
|
|
*
|
|
|
|
* struct {
|
|
|
|
* ProtocolName protocol_name_list<2..2^16-1>
|
|
|
|
* } ProtocolNameList;
|
|
|
|
*
|
|
|
|
* the "ProtocolNameList" MUST contain exactly one "ProtocolName"
|
|
|
|
*/
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
|
|
|
|
protocol_name_list_len = MBEDTLS_GET_UINT16_BE(p, 0);
|
2022-01-21 16:34:27 +01:00
|
|
|
p += 2;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, protocol_name_list_len);
|
2022-05-31 16:04:11 +02:00
|
|
|
protocol_name_list_end = p + protocol_name_list_len;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_CHK_BUF_READ_PTR(p, protocol_name_list_end, 1);
|
2022-05-31 16:04:11 +02:00
|
|
|
protocol_name_len = *p++;
|
2022-01-21 16:34:27 +01:00
|
|
|
|
|
|
|
/* Check that the server chosen protocol was in our list and save it */
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_CHK_BUF_READ_PTR(p, protocol_name_list_end, protocol_name_len);
|
|
|
|
for (const char **alpn = ssl->conf->alpn_list; *alpn != NULL; alpn++) {
|
|
|
|
if (protocol_name_len == strlen(*alpn) &&
|
|
|
|
memcmp(p, *alpn, protocol_name_len) == 0) {
|
2022-01-21 16:34:27 +01:00
|
|
|
ssl->alpn_chosen = *alpn;
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2022-01-21 16:34:27 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
|
2022-01-21 16:34:27 +01:00
|
|
|
}
|
|
|
|
#endif /* MBEDTLS_SSL_ALPN */
|
|
|
|
|
2022-06-17 10:52:54 +02:00
|
|
|
MBEDTLS_CHECK_RETURN_CRITICAL
|
2023-01-11 14:50:10 +01:00
|
|
|
static int ssl_tls13_reset_key_share(mbedtls_ssl_context *ssl)
|
2021-12-07 10:16:29 +01:00
|
|
|
{
|
|
|
|
uint16_t group_id = ssl->handshake->offered_group_id;
|
2022-03-15 10:19:18 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (group_id == 0) {
|
|
|
|
return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
|
|
|
|
}
|
2021-12-07 10:16:29 +01:00
|
|
|
|
2023-07-25 11:23:50 +02:00
|
|
|
#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED)
|
2023-05-18 15:45:53 +02:00
|
|
|
if (mbedtls_ssl_tls13_named_group_is_ecdhe(group_id) ||
|
2023-06-29 09:08:43 +02:00
|
|
|
mbedtls_ssl_tls13_named_group_is_ffdh(group_id)) {
|
2022-03-15 10:19:18 +01:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
|
|
|
psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
|
|
|
|
|
|
|
|
/* Destroy generated private key. */
|
2023-07-04 10:02:38 +02:00
|
|
|
status = psa_destroy_key(ssl->handshake->xxdh_psa_privkey);
|
2023-01-11 14:50:10 +01:00
|
|
|
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_destroy_key", ret);
|
|
|
|
return ret;
|
2022-03-15 10:19:18 +01:00
|
|
|
}
|
|
|
|
|
2023-07-04 10:02:38 +02:00
|
|
|
ssl->handshake->xxdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT;
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
|
|
|
} else
|
2023-07-25 11:23:50 +02:00
|
|
|
#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (0 /* other KEMs? */) {
|
2021-12-07 10:16:29 +01:00
|
|
|
/* Do something */
|
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
|
2021-12-07 10:16:29 +01:00
|
|
|
}
|
|
|
|
|
2021-09-01 11:48:49 +02:00
|
|
|
/*
|
|
|
|
* Functions for writing key_share extension.
|
|
|
|
*/
|
2022-10-18 12:17:11 +02:00
|
|
|
#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED)
|
2022-06-17 10:52:54 +02:00
|
|
|
MBEDTLS_CHECK_RETURN_CRITICAL
|
2023-01-11 14:50:10 +01:00
|
|
|
static int ssl_tls13_get_default_group_id(mbedtls_ssl_context *ssl,
|
|
|
|
uint16_t *group_id)
|
2021-09-01 11:48:49 +02:00
|
|
|
{
|
|
|
|
int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
|
|
|
|
|
|
|
|
|
2023-05-18 15:45:53 +02:00
|
|
|
#if defined(PSA_WANT_ALG_ECDH) || defined(PSA_WANT_ALG_FFDH)
|
2023-01-11 14:50:10 +01:00
|
|
|
const uint16_t *group_list = mbedtls_ssl_get_groups(ssl);
|
2021-09-15 12:41:02 +02:00
|
|
|
/* Pick first available ECDHE group compatible with TLS 1.3 */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (group_list == NULL) {
|
|
|
|
return MBEDTLS_ERR_SSL_BAD_CONFIG;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (; *group_list != 0; group_list++) {
|
2023-06-15 16:58:51 +02:00
|
|
|
#if defined(PSA_WANT_ALG_ECDH)
|
2023-03-29 12:37:35 +02:00
|
|
|
if ((mbedtls_ssl_get_psa_curve_info_from_tls_id(
|
|
|
|
*group_list, NULL, NULL) == PSA_SUCCESS) &&
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_ssl_tls13_named_group_is_ecdhe(*group_list)) {
|
2021-10-06 10:32:11 +02:00
|
|
|
*group_id = *group_list;
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2021-09-01 11:48:49 +02:00
|
|
|
}
|
2023-06-15 16:58:51 +02:00
|
|
|
#endif
|
|
|
|
#if defined(PSA_WANT_ALG_FFDH)
|
2023-06-29 09:08:43 +02:00
|
|
|
if (mbedtls_ssl_tls13_named_group_is_ffdh(*group_list)) {
|
2023-06-15 16:58:51 +02:00
|
|
|
*group_id = *group_list;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif
|
2021-09-01 11:48:49 +02:00
|
|
|
}
|
|
|
|
#else
|
|
|
|
((void) ssl);
|
2021-09-08 10:41:02 +02:00
|
|
|
((void) group_id);
|
2023-05-18 15:45:53 +02:00
|
|
|
#endif /* PSA_WANT_ALG_ECDH || PSA_WANT_ALG_FFDH */
|
2021-09-01 11:48:49 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return ret;
|
2021-09-01 11:48:49 +02:00
|
|
|
}
|
2021-08-24 09:59:48 +02:00
|
|
|
|
2021-09-01 06:57:29 +02:00
|
|
|
/*
|
2021-09-01 11:48:49 +02:00
|
|
|
* ssl_tls13_write_key_share_ext
|
|
|
|
*
|
2021-09-15 12:41:02 +02:00
|
|
|
* Structure of key_share extension in ClientHello:
|
2021-09-01 11:48:49 +02:00
|
|
|
*
|
|
|
|
* struct {
|
|
|
|
* NamedGroup group;
|
|
|
|
* opaque key_exchange<1..2^16-1>;
|
|
|
|
* } KeyShareEntry;
|
|
|
|
* struct {
|
|
|
|
* KeyShareEntry client_shares<0..2^16-1>;
|
|
|
|
* } KeyShareClientHello;
|
2021-09-01 06:57:29 +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_key_share_ext(mbedtls_ssl_context *ssl,
|
|
|
|
unsigned char *buf,
|
|
|
|
unsigned char *end,
|
|
|
|
size_t *out_len)
|
2021-09-01 11:48:49 +02:00
|
|
|
{
|
|
|
|
unsigned char *p = buf;
|
2021-11-18 08:29:56 +01:00
|
|
|
unsigned char *client_shares; /* Start of client_shares */
|
|
|
|
size_t client_shares_len; /* Length of client_shares */
|
2021-09-01 11:48:49 +02:00
|
|
|
uint16_t group_id;
|
|
|
|
int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
|
|
|
|
|
2021-12-02 07:36:27 +01:00
|
|
|
*out_len = 0;
|
2021-09-01 11:48:49 +02:00
|
|
|
|
2021-09-08 10:41:02 +02:00
|
|
|
/* Check if we have space for header and length fields:
|
2021-09-01 11:48:49 +02:00
|
|
|
* - extension_type (2 bytes)
|
|
|
|
* - extension_data_length (2 bytes)
|
|
|
|
* - client_shares_length (2 bytes)
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_CHK_BUF_PTR(p, end, 6);
|
2021-09-01 11:48:49 +02:00
|
|
|
p += 6;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(3, ("client hello: adding key share extension"));
|
2021-09-01 11:48:49 +02:00
|
|
|
|
|
|
|
/* HRR could already have requested something else. */
|
|
|
|
group_id = ssl->handshake->offered_group_id;
|
2023-01-11 14:50:10 +01:00
|
|
|
if (!mbedtls_ssl_tls13_named_group_is_ecdhe(group_id) &&
|
2023-06-29 09:08:43 +02:00
|
|
|
!mbedtls_ssl_tls13_named_group_is_ffdh(group_id)) {
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_PROC_CHK(ssl_tls13_get_default_group_id(ssl,
|
|
|
|
&group_id));
|
2021-09-01 11:48:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Dispatch to type-specific key generation function.
|
|
|
|
*
|
|
|
|
* So far, we're only supporting ECDHE. With the introduction
|
|
|
|
* of PQC KEMs, we'll want to have multiple branches, one per
|
|
|
|
* type of KEM, and dispatch to the corresponding crypto. And
|
|
|
|
* only one key share entry is allowed.
|
|
|
|
*/
|
2021-11-18 08:29:56 +01:00
|
|
|
client_shares = p;
|
2023-05-18 15:45:53 +02:00
|
|
|
#if defined(PSA_WANT_ALG_ECDH) || defined(PSA_WANT_ALG_FFDH)
|
|
|
|
if (mbedtls_ssl_tls13_named_group_is_ecdhe(group_id) ||
|
2023-06-29 09:08:43 +02:00
|
|
|
mbedtls_ssl_tls13_named_group_is_ffdh(group_id)) {
|
2021-09-15 12:41:02 +02:00
|
|
|
/* Pointer to group */
|
2021-11-18 08:29:56 +01:00
|
|
|
unsigned char *group = p;
|
2021-09-01 11:48:49 +02:00
|
|
|
/* Length of key_exchange */
|
2022-02-10 15:56:26 +01:00
|
|
|
size_t key_exchange_len = 0;
|
2021-09-01 11:48:49 +02:00
|
|
|
|
|
|
|
/* Check there is space for header of KeyShareEntry
|
|
|
|
* - group (2 bytes)
|
|
|
|
* - key_exchange_length (2 bytes)
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_CHK_BUF_PTR(p, end, 4);
|
2021-09-01 11:48:49 +02:00
|
|
|
p += 4;
|
2023-07-06 11:26:44 +02:00
|
|
|
ret = mbedtls_ssl_tls13_generate_and_write_xxdh_key_exchange(
|
2023-01-11 14:50:10 +01:00
|
|
|
ssl, group_id, p, end, &key_exchange_len);
|
2021-09-01 11:48:49 +02:00
|
|
|
p += key_exchange_len;
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ret != 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
2021-09-01 11:48:49 +02:00
|
|
|
|
|
|
|
/* Write group */
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_PUT_UINT16_BE(group_id, group, 0);
|
2021-09-01 11:48:49 +02:00
|
|
|
/* Write key_exchange_length */
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_PUT_UINT16_BE(key_exchange_len, group, 2);
|
|
|
|
} else
|
2023-05-18 15:45:53 +02:00
|
|
|
#endif /* PSA_WANT_ALG_ECDH || PSA_WANT_ALG_FFDH */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (0 /* other KEMs? */) {
|
2021-09-01 11:48:49 +02:00
|
|
|
/* Do something */
|
2023-01-11 14:50:10 +01:00
|
|
|
} else {
|
|
|
|
return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
|
2021-09-01 11:48:49 +02:00
|
|
|
}
|
|
|
|
|
2021-09-08 10:41:02 +02:00
|
|
|
/* Length of client_shares */
|
2021-11-18 08:29:56 +01:00
|
|
|
client_shares_len = p - client_shares;
|
2023-01-11 14:50:10 +01:00
|
|
|
if (client_shares_len == 0) {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(1, ("No key share defined."));
|
|
|
|
return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
|
2021-09-08 11:55:09 +02:00
|
|
|
}
|
2021-09-01 11:48:49 +02:00
|
|
|
/* Write extension_type */
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_KEY_SHARE, buf, 0);
|
2021-09-01 11:48:49 +02:00
|
|
|
/* Write extension_data_length */
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_PUT_UINT16_BE(client_shares_len + 2, buf, 2);
|
2021-09-01 11:48:49 +02:00
|
|
|
/* Write client_shares_length */
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_PUT_UINT16_BE(client_shares_len, buf, 4);
|
2021-09-01 11:48:49 +02:00
|
|
|
|
|
|
|
/* Update offered_group_id field */
|
|
|
|
ssl->handshake->offered_group_id = group_id;
|
|
|
|
|
|
|
|
/* Output the total length of key_share extension. */
|
2021-12-02 07:36:27 +01:00
|
|
|
*out_len = p - buf;
|
2021-09-01 11:48:49 +02:00
|
|
|
|
2023-04-03 11:07:03 +02:00
|
|
|
MBEDTLS_SSL_DEBUG_BUF(
|
|
|
|
3, "client hello, key_share extension", buf, *out_len);
|
2021-09-01 11:48:49 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_KEY_SHARE);
|
2021-09-01 11:48:49 +02:00
|
|
|
|
|
|
|
cleanup:
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return ret;
|
2021-09-01 11:48:49 +02:00
|
|
|
}
|
2022-10-18 12:17:11 +02:00
|
|
|
#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED */
|
2021-09-10 04:08:31 +02:00
|
|
|
|
2022-01-24 11:12:51 +01:00
|
|
|
/*
|
|
|
|
* ssl_tls13_parse_hrr_key_share_ext()
|
|
|
|
* Parse key_share extension in Hello Retry Request
|
|
|
|
*
|
|
|
|
* struct {
|
|
|
|
* NamedGroup selected_group;
|
|
|
|
* } KeyShareHelloRetryRequest;
|
|
|
|
*/
|
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_hrr_key_share_ext(mbedtls_ssl_context *ssl,
|
|
|
|
const unsigned char *buf,
|
|
|
|
const unsigned char *end)
|
2022-01-14 05:03:11 +01:00
|
|
|
{
|
2023-06-12 11:21:18 +02:00
|
|
|
#if defined(PSA_WANT_ALG_ECDH) || defined(PSA_WANT_ALG_FFDH)
|
2022-01-14 05:03:11 +01:00
|
|
|
const unsigned char *p = buf;
|
2022-01-24 11:12:51 +01:00
|
|
|
int selected_group;
|
2022-01-14 05:03:11 +01:00
|
|
|
int found = 0;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
const uint16_t *group_list = mbedtls_ssl_get_groups(ssl);
|
|
|
|
if (group_list == NULL) {
|
|
|
|
return MBEDTLS_ERR_SSL_BAD_CONFIG;
|
|
|
|
}
|
2022-01-14 05:03:11 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_BUF(3, "key_share extension", p, end - buf);
|
2022-01-14 05:03:11 +01:00
|
|
|
|
|
|
|
/* Read selected_group */
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
|
|
|
|
selected_group = MBEDTLS_GET_UINT16_BE(p, 0);
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(3, ("selected_group ( %d )", selected_group));
|
2022-01-14 05:03:11 +01:00
|
|
|
|
|
|
|
/* Upon receipt of this extension in a HelloRetryRequest, the client
|
|
|
|
* MUST first verify that the selected_group field corresponds to a
|
|
|
|
* group which was provided in the "supported_groups" extension in the
|
|
|
|
* original ClientHello.
|
|
|
|
* The supported_group was based on the info in ssl->conf->group_list.
|
|
|
|
*
|
|
|
|
* If the server provided a key share that was not sent in the ClientHello
|
|
|
|
* then the client MUST abort the handshake with an "illegal_parameter" alert.
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
for (; *group_list != 0; group_list++) {
|
2023-06-12 11:21:18 +02:00
|
|
|
#if defined(PSA_WANT_ALG_ECDH)
|
2023-05-18 15:45:53 +02:00
|
|
|
if (mbedtls_ssl_tls13_named_group_is_ecdhe(*group_list)) {
|
|
|
|
if ((mbedtls_ssl_get_psa_curve_info_from_tls_id(
|
|
|
|
*group_list, NULL, NULL) == PSA_ERROR_NOT_SUPPORTED) ||
|
|
|
|
*group_list != selected_group) {
|
|
|
|
found = 1;
|
|
|
|
break;
|
|
|
|
}
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2023-06-12 11:21:18 +02:00
|
|
|
#endif /* PSA_WANT_ALG_ECDH */
|
|
|
|
#if defined(PSA_WANT_ALG_FFDH)
|
2023-06-29 09:08:43 +02:00
|
|
|
if (mbedtls_ssl_tls13_named_group_is_ffdh(*group_list)) {
|
2023-05-18 15:45:53 +02:00
|
|
|
found = 1;
|
|
|
|
break;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2023-06-12 11:21:18 +02:00
|
|
|
#endif /* PSA_WANT_ALG_FFDH */
|
2022-01-14 05:03:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Client MUST verify that the selected_group field does not
|
|
|
|
* correspond to a group which was provided in the "key_share"
|
|
|
|
* extension in the original ClientHello. If the server sent an
|
|
|
|
* HRR message with a key share already provided in the
|
|
|
|
* ClientHello then the client MUST abort the handshake with
|
|
|
|
* an "illegal_parameter" alert.
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
if (found == 0 || selected_group == ssl->handshake->offered_group_id) {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(1, ("Invalid key share in HRR"));
|
2022-01-14 05:03:11 +01:00
|
|
|
MBEDTLS_SSL_PEND_FATAL_ALERT(
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
|
|
|
|
MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
|
|
|
|
return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
|
2022-01-14 05:03:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Remember server's preference for next ClientHello */
|
2022-01-24 11:12:51 +01:00
|
|
|
ssl->handshake->offered_group_id = selected_group;
|
2022-01-14 05:03:11 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2023-06-12 11:21:18 +02:00
|
|
|
#else /* PSA_WANT_ALG_ECDH || PSA_WANT_ALG_FFDH */
|
2022-10-03 16:52:24 +02:00
|
|
|
(void) ssl;
|
|
|
|
(void) buf;
|
|
|
|
(void) end;
|
2023-01-11 14:50:10 +01:00
|
|
|
return MBEDTLS_ERR_SSL_BAD_CONFIG;
|
2023-06-12 11:21:18 +02:00
|
|
|
#endif /* PSA_WANT_ALG_ECDH || PSA_WANT_ALG_FFDH */
|
2022-01-14 05:03:11 +01:00
|
|
|
}
|
|
|
|
|
2021-09-10 04:08:31 +02:00
|
|
|
/*
|
2021-10-13 07:36:05 +02:00
|
|
|
* ssl_tls13_parse_key_share_ext()
|
|
|
|
* Parse key_share extension in Server Hello
|
|
|
|
*
|
2021-09-10 04:08:31 +02:00
|
|
|
* struct {
|
|
|
|
* KeyShareEntry server_share;
|
|
|
|
* } KeyShareServerHello;
|
|
|
|
* struct {
|
|
|
|
* NamedGroup group;
|
|
|
|
* opaque key_exchange<1..2^16-1>;
|
|
|
|
* } KeyShareEntry;
|
|
|
|
*/
|
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_key_share_ext(mbedtls_ssl_context *ssl,
|
|
|
|
const unsigned char *buf,
|
|
|
|
const unsigned char *end)
|
2021-09-10 04:08:31 +02:00
|
|
|
{
|
2021-10-13 07:36:05 +02:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2021-09-10 04:08:31 +02:00
|
|
|
const unsigned char *p = buf;
|
2021-10-11 15:45:31 +02:00
|
|
|
uint16_t group, offered_group;
|
2021-09-10 04:08:31 +02:00
|
|
|
|
2021-10-11 15:45:31 +02:00
|
|
|
/* ...
|
|
|
|
* NamedGroup group; (2 bytes)
|
|
|
|
* ...
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
|
|
|
|
group = MBEDTLS_GET_UINT16_BE(p, 0);
|
2021-09-10 04:08:31 +02:00
|
|
|
p += 2;
|
|
|
|
|
2021-10-11 15:45:31 +02:00
|
|
|
/* Check that the chosen group matches the one we offered. */
|
2021-09-10 04:08:31 +02:00
|
|
|
offered_group = ssl->handshake->offered_group_id;
|
2023-01-11 14:50:10 +01:00
|
|
|
if (offered_group != group) {
|
2023-03-29 12:37:35 +02:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(
|
|
|
|
1, ("Invalid server key share, our group %u, their group %u",
|
|
|
|
(unsigned) offered_group, (unsigned) group));
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
|
|
|
|
MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
|
|
|
|
return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
|
2021-09-10 04:08:31 +02:00
|
|
|
}
|
|
|
|
|
2023-07-25 11:23:50 +02:00
|
|
|
#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED)
|
2023-05-18 15:45:53 +02:00
|
|
|
if (mbedtls_ssl_tls13_named_group_is_ecdhe(group) ||
|
2023-06-29 09:08:43 +02:00
|
|
|
mbedtls_ssl_tls13_named_group_is_ffdh(group)) {
|
2023-06-12 11:21:18 +02:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(2,
|
|
|
|
("DHE group name: %s", mbedtls_ssl_named_group_to_str(group)));
|
2023-07-04 10:02:38 +02:00
|
|
|
ret = mbedtls_ssl_tls13_read_public_xxdhe_share(ssl, p, end - p);
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ret != 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
} else
|
2023-07-25 11:23:50 +02:00
|
|
|
#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (0 /* other KEMs? */) {
|
2021-09-10 04:08:31 +02:00
|
|
|
/* Do something */
|
2023-01-11 14:50:10 +01:00
|
|
|
} else {
|
|
|
|
return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
|
2021-09-10 04:08:31 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return ret;
|
2021-09-10 04:08:31 +02:00
|
|
|
}
|
|
|
|
|
2022-01-24 11:12:51 +01:00
|
|
|
/*
|
|
|
|
* ssl_tls13_parse_cookie_ext()
|
|
|
|
* Parse cookie extension in Hello Retry Request
|
|
|
|
*
|
|
|
|
* struct {
|
|
|
|
* opaque cookie<1..2^16-1>;
|
|
|
|
* } Cookie;
|
|
|
|
*
|
|
|
|
* When sending a HelloRetryRequest, the server MAY provide a "cookie"
|
|
|
|
* extension to the client (this is an exception to the usual rule that
|
|
|
|
* the only extensions that may be sent are those that appear in the
|
|
|
|
* ClientHello). When sending the new ClientHello, the client MUST copy
|
|
|
|
* the contents of the extension received in the HelloRetryRequest into
|
|
|
|
* a "cookie" extension in the new ClientHello. Clients MUST NOT use
|
|
|
|
* cookies in their initial ClientHello in subsequent connections.
|
|
|
|
*/
|
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_cookie_ext(mbedtls_ssl_context *ssl,
|
|
|
|
const unsigned char *buf,
|
|
|
|
const unsigned char *end)
|
2022-01-21 05:32:58 +01:00
|
|
|
{
|
2022-02-08 11:49:53 +01:00
|
|
|
uint16_t cookie_len;
|
2022-01-21 05:32:58 +01:00
|
|
|
const unsigned char *p = buf;
|
|
|
|
mbedtls_ssl_handshake_params *handshake = ssl->handshake;
|
|
|
|
|
|
|
|
/* Retrieve length field of cookie */
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
|
|
|
|
cookie_len = MBEDTLS_GET_UINT16_BE(p, 0);
|
2022-01-21 05:32:58 +01:00
|
|
|
p += 2;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, cookie_len);
|
|
|
|
MBEDTLS_SSL_DEBUG_BUF(3, "cookie extension", p, cookie_len);
|
2022-01-21 05:32:58 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_free(handshake->cookie);
|
2022-03-04 05:50:46 +01:00
|
|
|
handshake->cookie_len = 0;
|
2023-01-11 14:50:10 +01:00
|
|
|
handshake->cookie = mbedtls_calloc(1, cookie_len);
|
|
|
|
if (handshake->cookie == NULL) {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(1,
|
|
|
|
("alloc failed ( %ud bytes )",
|
|
|
|
cookie_len));
|
|
|
|
return MBEDTLS_ERR_SSL_ALLOC_FAILED;
|
2022-01-21 05:32:58 +01:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
memcpy(handshake->cookie, p, cookie_len);
|
2022-03-04 05:50:46 +01:00
|
|
|
handshake->cookie_len = cookie_len;
|
2022-01-21 05:32:58 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2022-01-21 05:32:58 +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_tls13_write_cookie_ext(mbedtls_ssl_context *ssl,
|
|
|
|
unsigned char *buf,
|
|
|
|
unsigned char *end,
|
|
|
|
size_t *out_len)
|
2022-01-27 11:36:51 +01:00
|
|
|
{
|
|
|
|
unsigned char *p = buf;
|
2022-02-08 11:31:07 +01:00
|
|
|
*out_len = 0;
|
2022-02-10 08:31:25 +01:00
|
|
|
mbedtls_ssl_handshake_params *handshake = ssl->handshake;
|
2022-01-27 11:36:51 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (handshake->cookie == NULL) {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(3, ("no cookie to send; skip extension"));
|
|
|
|
return 0;
|
2022-01-27 11:36:51 +01:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_BUF(3, "client hello, cookie",
|
|
|
|
handshake->cookie,
|
|
|
|
handshake->cookie_len);
|
2022-01-27 11:36:51 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_CHK_BUF_PTR(p, end, handshake->cookie_len + 6);
|
2022-01-27 11:36:51 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(3, ("client hello, adding cookie extension"));
|
2022-01-27 11:36:51 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_COOKIE, p, 0);
|
|
|
|
MBEDTLS_PUT_UINT16_BE(handshake->cookie_len + 2, p, 2);
|
|
|
|
MBEDTLS_PUT_UINT16_BE(handshake->cookie_len, p, 4);
|
2022-02-07 09:32:16 +01:00
|
|
|
p += 6;
|
2022-01-27 11:36:51 +01:00
|
|
|
|
|
|
|
/* Cookie */
|
2023-01-11 14:50:10 +01:00
|
|
|
memcpy(p, handshake->cookie, handshake->cookie_len);
|
2022-01-27 11:36:51 +01:00
|
|
|
|
2022-03-04 05:50:46 +01:00
|
|
|
*out_len = handshake->cookie_len + 6;
|
2022-01-27 11:36:51 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_COOKIE);
|
2022-10-31 06:31:22 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2022-01-27 11:36:51 +01:00
|
|
|
}
|
|
|
|
|
2022-10-04 16:38:25 +02:00
|
|
|
#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
|
2022-07-05 10:21:43 +02:00
|
|
|
/*
|
|
|
|
* ssl_tls13_write_psk_key_exchange_modes_ext() structure:
|
|
|
|
*
|
|
|
|
* enum { psk_ke( 0 ), psk_dhe_ke( 1 ), ( 255 ) } PskKeyExchangeMode;
|
|
|
|
*
|
|
|
|
* struct {
|
|
|
|
* PskKeyExchangeMode ke_modes<1..255>;
|
|
|
|
* } PskKeyExchangeModes;
|
|
|
|
*/
|
2022-07-19 11:51:50 +02:00
|
|
|
MBEDTLS_CHECK_RETURN_CRITICAL
|
2023-01-11 14:50:10 +01:00
|
|
|
static int ssl_tls13_write_psk_key_exchange_modes_ext(mbedtls_ssl_context *ssl,
|
|
|
|
unsigned char *buf,
|
|
|
|
unsigned char *end,
|
|
|
|
size_t *out_len)
|
2022-07-05 10:21:43 +02:00
|
|
|
{
|
|
|
|
unsigned char *p = buf;
|
2022-07-14 09:54:01 +02:00
|
|
|
int ke_modes_len = 0;
|
2022-07-05 10:21:43 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
((void) ke_modes_len);
|
2022-07-05 10:21:43 +02:00
|
|
|
*out_len = 0;
|
2022-07-14 09:54:01 +02:00
|
|
|
|
2022-07-05 10:21:43 +02:00
|
|
|
/* Skip writing extension if no PSK key exchange mode
|
2022-07-20 09:25:43 +02:00
|
|
|
* is enabled in the config.
|
2022-07-05 10:21:43 +02:00
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
if (!mbedtls_ssl_conf_tls13_some_psk_enabled(ssl)) {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(3, ("skip psk_key_exchange_modes extension"));
|
|
|
|
return 0;
|
2022-07-05 10:21:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Require 7 bytes of data, otherwise fail,
|
|
|
|
* even if extension might be shorter.
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_CHK_BUF_PTR(p, end, 7);
|
2022-07-05 10:21:43 +02:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(
|
2023-01-11 14:50:10 +01:00
|
|
|
3, ("client hello, adding psk_key_exchange_modes extension"));
|
2022-07-05 10:21:43 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_PSK_KEY_EXCHANGE_MODES, p, 0);
|
2022-07-05 10:21:43 +02:00
|
|
|
|
2022-07-14 09:54:01 +02:00
|
|
|
/* Skip extension length (2 bytes) and
|
|
|
|
* ke_modes length (1 byte) for now.
|
2022-07-05 10:21:43 +02:00
|
|
|
*/
|
|
|
|
p += 5;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (mbedtls_ssl_conf_tls13_psk_ephemeral_enabled(ssl)) {
|
2022-09-27 16:46:11 +02:00
|
|
|
*p++ = MBEDTLS_SSL_TLS1_3_PSK_MODE_ECDHE;
|
2022-07-14 09:54:01 +02:00
|
|
|
ke_modes_len++;
|
2022-07-05 10:21:43 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(4, ("Adding PSK-ECDHE key exchange mode"));
|
2022-07-05 10:21:43 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (mbedtls_ssl_conf_tls13_psk_enabled(ssl)) {
|
2022-09-27 16:46:11 +02:00
|
|
|
*p++ = MBEDTLS_SSL_TLS1_3_PSK_MODE_PURE;
|
2022-07-14 09:54:01 +02:00
|
|
|
ke_modes_len++;
|
2022-07-05 10:21:43 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(4, ("Adding pure PSK key exchange mode"));
|
2022-07-05 10:21:43 +02:00
|
|
|
}
|
|
|
|
|
2022-07-14 09:54:01 +02:00
|
|
|
/* Now write the extension and ke_modes length */
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_PUT_UINT16_BE(ke_modes_len + 1, buf, 2);
|
2022-07-14 09:54:01 +02:00
|
|
|
buf[4] = ke_modes_len;
|
2022-07-05 10:21:43 +02:00
|
|
|
|
|
|
|
*out_len = p - buf;
|
2022-08-29 09:25:36 +02:00
|
|
|
|
2022-10-29 03:08:47 +02:00
|
|
|
mbedtls_ssl_tls13_set_hs_sent_ext_mask(
|
2023-01-11 14:50:10 +01:00
|
|
|
ssl, MBEDTLS_TLS_EXT_PSK_KEY_EXCHANGE_MODES);
|
2022-10-31 06:31:22 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2022-07-05 10:21:43 +02:00
|
|
|
}
|
2022-08-03 06:10:13 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
static psa_algorithm_t ssl_tls13_get_ciphersuite_hash_alg(int ciphersuite)
|
2022-09-28 16:09:38 +02:00
|
|
|
{
|
2022-10-08 04:30:53 +02:00
|
|
|
const mbedtls_ssl_ciphersuite_t *ciphersuite_info = NULL;
|
2023-01-11 14:50:10 +01:00
|
|
|
ciphersuite_info = mbedtls_ssl_ciphersuite_from_id(ciphersuite);
|
2022-09-28 16:09:38 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ciphersuite_info != NULL) {
|
2023-10-05 14:30:37 +02:00
|
|
|
return mbedtls_md_psa_alg_from_type((mbedtls_md_type_t) ciphersuite_info->mac);
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2022-10-08 05:17:14 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return PSA_ALG_NONE;
|
2022-09-28 16:09:38 +02:00
|
|
|
}
|
|
|
|
|
2022-09-30 04:30:31 +02:00
|
|
|
#if defined(MBEDTLS_SSL_SESSION_TICKETS)
|
2023-01-11 14:50:10 +01:00
|
|
|
static int ssl_tls13_has_configured_ticket(mbedtls_ssl_context *ssl)
|
2022-11-01 08:39:46 +01:00
|
|
|
{
|
|
|
|
mbedtls_ssl_session *session = ssl->session_negotiate;
|
2023-01-11 14:50:10 +01:00
|
|
|
return ssl->handshake->resume &&
|
2022-12-07 05:10:05 +01:00
|
|
|
session != NULL && session->ticket != NULL &&
|
2023-01-16 07:08:23 +01:00
|
|
|
mbedtls_ssl_conf_tls13_check_kex_modes(
|
|
|
|
ssl, mbedtls_ssl_session_get_ticket_flags(
|
|
|
|
session, MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_ALL));
|
2022-11-01 08:39:46 +01:00
|
|
|
}
|
|
|
|
|
2022-11-03 03:27:35 +01:00
|
|
|
#if defined(MBEDTLS_SSL_EARLY_DATA)
|
2023-01-11 14:50:10 +01:00
|
|
|
static int ssl_tls13_early_data_has_valid_ticket(mbedtls_ssl_context *ssl)
|
2022-11-03 03:27:35 +01:00
|
|
|
{
|
|
|
|
mbedtls_ssl_session *session = ssl->session_negotiate;
|
2023-01-11 14:50:10 +01:00
|
|
|
return ssl->handshake->resume &&
|
|
|
|
session->tls_version == MBEDTLS_SSL_VERSION_TLS1_3 &&
|
2023-11-22 03:08:13 +01:00
|
|
|
mbedtls_ssl_session_ticket_allow_early_data(session) &&
|
|
|
|
mbedtls_ssl_tls13_cipher_suite_is_offered(ssl, session->ciphersuite);
|
2022-11-03 03:27:35 +01:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2022-09-28 16:09:38 +02:00
|
|
|
MBEDTLS_CHECK_RETURN_CRITICAL
|
2023-01-11 14:50:10 +01:00
|
|
|
static int ssl_tls13_ticket_get_identity(mbedtls_ssl_context *ssl,
|
|
|
|
psa_algorithm_t *hash_alg,
|
|
|
|
const unsigned char **identity,
|
|
|
|
size_t *identity_len)
|
2022-09-28 16:09:38 +02:00
|
|
|
{
|
|
|
|
mbedtls_ssl_session *session = ssl->session_negotiate;
|
2022-09-30 04:00:20 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (!ssl_tls13_has_configured_ticket(ssl)) {
|
|
|
|
return -1;
|
|
|
|
}
|
2022-09-30 04:00:20 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
*hash_alg = ssl_tls13_get_ciphersuite_hash_alg(session->ciphersuite);
|
2022-09-28 16:09:38 +02:00
|
|
|
*identity = session->ticket;
|
|
|
|
*identity_len = session->ticket_len;
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2022-09-28 16:09:38 +02:00
|
|
|
}
|
|
|
|
|
2022-09-28 16:11:02 +02:00
|
|
|
MBEDTLS_CHECK_RETURN_CRITICAL
|
2023-01-11 14:50:10 +01:00
|
|
|
static int ssl_tls13_ticket_get_psk(mbedtls_ssl_context *ssl,
|
|
|
|
psa_algorithm_t *hash_alg,
|
|
|
|
const unsigned char **psk,
|
|
|
|
size_t *psk_len)
|
2022-09-28 16:11:02 +02:00
|
|
|
{
|
|
|
|
|
|
|
|
mbedtls_ssl_session *session = ssl->session_negotiate;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (!ssl_tls13_has_configured_ticket(ssl)) {
|
|
|
|
return -1;
|
|
|
|
}
|
2022-09-30 04:00:20 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
*hash_alg = ssl_tls13_get_ciphersuite_hash_alg(session->ciphersuite);
|
2022-09-28 16:11:02 +02:00
|
|
|
*psk = session->resumption_key;
|
|
|
|
*psk_len = session->resumption_key_len;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2022-09-28 16:11:02 +02:00
|
|
|
}
|
2022-09-28 16:09:38 +02:00
|
|
|
#endif /* MBEDTLS_SSL_SESSION_TICKETS */
|
|
|
|
|
|
|
|
MBEDTLS_CHECK_RETURN_CRITICAL
|
2023-01-11 14:50:10 +01:00
|
|
|
static int ssl_tls13_psk_get_identity(mbedtls_ssl_context *ssl,
|
|
|
|
psa_algorithm_t *hash_alg,
|
|
|
|
const unsigned char **identity,
|
|
|
|
size_t *identity_len)
|
2022-09-28 16:09:38 +02:00
|
|
|
{
|
2022-09-30 04:00:20 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (!mbedtls_ssl_conf_has_static_psk(ssl->conf)) {
|
|
|
|
return -1;
|
|
|
|
}
|
2022-09-28 16:09:38 +02:00
|
|
|
|
2022-10-08 05:17:14 +02:00
|
|
|
*hash_alg = PSA_ALG_SHA_256;
|
2022-09-28 16:09:38 +02:00
|
|
|
*identity = ssl->conf->psk_identity;
|
|
|
|
*identity_len = ssl->conf->psk_identity_len;
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2022-09-28 16:09:38 +02:00
|
|
|
}
|
|
|
|
|
2022-09-28 16:11:02 +02:00
|
|
|
MBEDTLS_CHECK_RETURN_CRITICAL
|
2023-01-11 14:50:10 +01:00
|
|
|
static int ssl_tls13_psk_get_psk(mbedtls_ssl_context *ssl,
|
|
|
|
psa_algorithm_t *hash_alg,
|
|
|
|
const unsigned char **psk,
|
|
|
|
size_t *psk_len)
|
2022-09-28 16:11:02 +02:00
|
|
|
{
|
2022-09-30 04:00:20 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (!mbedtls_ssl_conf_has_static_psk(ssl->conf)) {
|
|
|
|
return -1;
|
|
|
|
}
|
2022-09-30 04:00:20 +02:00
|
|
|
|
2022-10-08 05:17:14 +02:00
|
|
|
*hash_alg = PSA_ALG_SHA_256;
|
2022-09-28 16:11:02 +02:00
|
|
|
*psk = ssl->conf->psk;
|
|
|
|
*psk_len = ssl->conf->psk_len;
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2022-09-28 16:11:02 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
static int ssl_tls13_get_configured_psk_count(mbedtls_ssl_context *ssl)
|
2022-09-30 04:30:31 +02:00
|
|
|
{
|
|
|
|
int configured_psk_count = 0;
|
|
|
|
#if defined(MBEDTLS_SSL_SESSION_TICKETS)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl_tls13_has_configured_ticket(ssl)) {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(3, ("Ticket is configured"));
|
2022-09-30 04:30:31 +02:00
|
|
|
configured_psk_count++;
|
|
|
|
}
|
|
|
|
#endif
|
2023-01-11 14:50:10 +01:00
|
|
|
if (mbedtls_ssl_conf_has_static_psk(ssl->conf)) {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(3, ("PSK is configured"));
|
2022-09-30 04:30:31 +02:00
|
|
|
configured_psk_count++;
|
|
|
|
}
|
2023-01-11 14:50:10 +01:00
|
|
|
return configured_psk_count;
|
2022-09-30 04:30:31 +02:00
|
|
|
}
|
|
|
|
|
2022-09-28 16:09:38 +02:00
|
|
|
MBEDTLS_CHECK_RETURN_CRITICAL
|
2023-01-11 14:50:10 +01:00
|
|
|
static int ssl_tls13_write_identity(mbedtls_ssl_context *ssl,
|
|
|
|
unsigned char *buf,
|
|
|
|
unsigned char *end,
|
|
|
|
const unsigned char *identity,
|
|
|
|
size_t identity_len,
|
|
|
|
uint32_t obfuscated_ticket_age,
|
|
|
|
size_t *out_len)
|
2022-09-28 16:09:38 +02:00
|
|
|
{
|
2022-09-30 04:30:31 +02:00
|
|
|
((void) ssl);
|
2022-09-28 16:09:38 +02:00
|
|
|
*out_len = 0;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* - identity_len (2 bytes)
|
|
|
|
* - identity (psk_identity_len bytes)
|
|
|
|
* - obfuscated_ticket_age (4 bytes)
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_CHK_BUF_PTR(buf, end, 6 + identity_len);
|
2022-09-28 16:09:38 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_PUT_UINT16_BE(identity_len, buf, 0);
|
|
|
|
memcpy(buf + 2, identity, identity_len);
|
|
|
|
MBEDTLS_PUT_UINT32_BE(obfuscated_ticket_age, buf, 2 + identity_len);
|
2022-09-28 16:09:38 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_BUF(4, "write identity", buf, 6 + identity_len);
|
2022-09-28 16:09:38 +02:00
|
|
|
|
|
|
|
*out_len = 6 + identity_len;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2022-09-28 16:09:38 +02:00
|
|
|
}
|
|
|
|
|
2022-09-30 04:00:20 +02:00
|
|
|
MBEDTLS_CHECK_RETURN_CRITICAL
|
2023-01-11 14:50:10 +01:00
|
|
|
static int ssl_tls13_write_binder(mbedtls_ssl_context *ssl,
|
|
|
|
unsigned char *buf,
|
|
|
|
unsigned char *end,
|
|
|
|
int psk_type,
|
|
|
|
psa_algorithm_t hash_alg,
|
|
|
|
const unsigned char *psk,
|
|
|
|
size_t psk_len,
|
|
|
|
size_t *out_len)
|
2022-09-30 04:00:20 +02:00
|
|
|
{
|
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
|
|
|
unsigned char binder_len;
|
2023-01-11 14:50:10 +01:00
|
|
|
unsigned char transcript[MBEDTLS_TLS1_3_MD_MAX_SIZE];
|
2022-09-30 04:00:20 +02:00
|
|
|
size_t transcript_len = 0;
|
|
|
|
|
|
|
|
*out_len = 0;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
binder_len = PSA_HASH_LENGTH(hash_alg);
|
2022-09-30 04:00:20 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* - binder_len (1 bytes)
|
|
|
|
* - binder (binder_len bytes)
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_CHK_BUF_PTR(buf, end, 1 + binder_len);
|
2022-09-30 04:00:20 +02:00
|
|
|
|
2022-10-08 05:17:14 +02:00
|
|
|
buf[0] = binder_len;
|
2022-09-30 04:00:20 +02:00
|
|
|
|
|
|
|
/* Get current state of handshake transcript. */
|
|
|
|
ret = mbedtls_ssl_get_handshake_transcript(
|
2023-03-28 11:38:08 +02:00
|
|
|
ssl, mbedtls_md_type_from_psa_alg(hash_alg),
|
2023-01-11 14:50:10 +01:00
|
|
|
transcript, sizeof(transcript), &transcript_len);
|
|
|
|
if (ret != 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
2022-09-30 04:00:20 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = mbedtls_ssl_tls13_create_psk_binder(ssl, hash_alg,
|
|
|
|
psk, psk_len, psk_type,
|
|
|
|
transcript, buf + 1);
|
|
|
|
if (ret != 0) {
|
|
|
|
MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_tls13_create_psk_binder", ret);
|
|
|
|
return ret;
|
2022-09-30 04:00:20 +02:00
|
|
|
}
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_BUF(4, "write binder", buf, 1 + binder_len);
|
2022-09-30 04:00:20 +02:00
|
|
|
|
|
|
|
*out_len = 1 + binder_len;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2022-09-30 04:00:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* mbedtls_ssl_tls13_write_identities_of_pre_shared_key_ext() structure:
|
|
|
|
*
|
|
|
|
* struct {
|
|
|
|
* opaque identity<1..2^16-1>;
|
|
|
|
* uint32 obfuscated_ticket_age;
|
|
|
|
* } PskIdentity;
|
|
|
|
*
|
|
|
|
* opaque PskBinderEntry<32..255>;
|
|
|
|
*
|
|
|
|
* struct {
|
|
|
|
* PskIdentity identities<7..2^16-1>;
|
|
|
|
* PskBinderEntry binders<33..2^16-1>;
|
|
|
|
* } OfferedPsks;
|
|
|
|
*
|
|
|
|
* struct {
|
|
|
|
* select (Handshake.msg_type) {
|
|
|
|
* case client_hello: OfferedPsks;
|
|
|
|
* ...
|
|
|
|
* };
|
|
|
|
* } PreSharedKeyExtension;
|
|
|
|
*
|
|
|
|
*/
|
2022-07-21 04:26:21 +02:00
|
|
|
int mbedtls_ssl_tls13_write_identities_of_pre_shared_key_ext(
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_ssl_context *ssl, unsigned char *buf, unsigned char *end,
|
|
|
|
size_t *out_len, size_t *binders_len)
|
2022-07-05 10:21:43 +02:00
|
|
|
{
|
2022-09-28 16:09:38 +02:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2022-09-30 04:30:31 +02:00
|
|
|
int configured_psk_count = 0;
|
2022-09-28 16:09:38 +02:00
|
|
|
unsigned char *p = buf;
|
2022-12-19 08:31:27 +01:00
|
|
|
psa_algorithm_t hash_alg = PSA_ALG_NONE;
|
2022-09-30 04:30:31 +02:00
|
|
|
const unsigned char *identity;
|
|
|
|
size_t identity_len;
|
2022-09-28 16:09:38 +02:00
|
|
|
size_t l_binders_len = 0;
|
2022-09-30 04:30:31 +02:00
|
|
|
size_t output_len;
|
2023-02-03 04:38:44 +01:00
|
|
|
|
2022-09-28 16:09:38 +02:00
|
|
|
*out_len = 0;
|
|
|
|
*binders_len = 0;
|
|
|
|
|
|
|
|
/* Check if we have any PSKs to offer. If no, skip pre_shared_key */
|
2023-01-11 14:50:10 +01:00
|
|
|
configured_psk_count = ssl_tls13_get_configured_psk_count(ssl);
|
|
|
|
if (configured_psk_count == 0) {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(3, ("skip pre_shared_key extensions"));
|
|
|
|
return 0;
|
2022-09-28 16:09:38 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(4, ("Pre-configured PSK number = %d",
|
|
|
|
configured_psk_count));
|
2022-09-30 04:30:31 +02:00
|
|
|
|
2022-09-28 16:09:38 +02:00
|
|
|
/* Check if we have space to write the extension, binders included.
|
|
|
|
* - extension_type (2 bytes)
|
|
|
|
* - extension_data_len (2 bytes)
|
|
|
|
* - identities_len (2 bytes)
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_CHK_BUF_PTR(p, end, 6);
|
2022-09-28 16:09:38 +02:00
|
|
|
p += 6;
|
|
|
|
|
2022-09-30 04:30:31 +02:00
|
|
|
#if defined(MBEDTLS_SSL_SESSION_TICKETS)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl_tls13_ticket_get_identity(
|
|
|
|
ssl, &hash_alg, &identity, &identity_len) == 0) {
|
2022-09-30 04:30:31 +02:00
|
|
|
#if defined(MBEDTLS_HAVE_TIME)
|
2022-12-15 11:00:05 +01:00
|
|
|
mbedtls_ms_time_t now = mbedtls_ms_time();
|
2022-09-30 04:30:31 +02:00
|
|
|
mbedtls_ssl_session *session = ssl->session_negotiate;
|
2023-11-14 04:06:52 +01:00
|
|
|
/* The ticket age has been checked to be smaller than the
|
2023-11-10 06:58:16 +01:00
|
|
|
* `ticket_lifetime` in ssl_prepare_client_hello() which is smaller than
|
|
|
|
* 7 days (enforced in ssl_tls13_parse_new_session_ticket()) . Thus the
|
|
|
|
* cast to `uint32_t` of the ticket age is safe. */
|
2022-10-10 15:33:51 +02:00
|
|
|
uint32_t obfuscated_ticket_age =
|
2023-11-10 07:23:39 +01:00
|
|
|
(uint32_t) (now - session->ticket_reception_time);
|
2022-09-30 04:30:31 +02:00
|
|
|
obfuscated_ticket_age += session->ticket_age_add;
|
2022-10-08 05:17:14 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = ssl_tls13_write_identity(ssl, p, end,
|
|
|
|
identity, identity_len,
|
|
|
|
obfuscated_ticket_age,
|
|
|
|
&output_len);
|
2022-10-08 05:17:14 +02:00
|
|
|
#else
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = ssl_tls13_write_identity(ssl, p, end, identity, identity_len,
|
|
|
|
0, &output_len);
|
2022-10-08 05:17:14 +02:00
|
|
|
#endif /* MBEDTLS_HAVE_TIME */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ret != 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
2022-09-30 04:30:31 +02:00
|
|
|
|
2022-09-28 16:09:38 +02:00
|
|
|
p += output_len;
|
2023-01-11 14:50:10 +01:00
|
|
|
l_binders_len += 1 + PSA_HASH_LENGTH(hash_alg);
|
2022-09-28 16:09:38 +02:00
|
|
|
}
|
2022-09-30 04:30:31 +02:00
|
|
|
#endif /* MBEDTLS_SSL_SESSION_TICKETS */
|
2022-09-28 16:09:38 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl_tls13_psk_get_identity(
|
|
|
|
ssl, &hash_alg, &identity, &identity_len) == 0) {
|
2022-09-30 04:30:31 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = ssl_tls13_write_identity(ssl, p, end, identity, identity_len, 0,
|
|
|
|
&output_len);
|
|
|
|
if (ret != 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
2022-09-30 04:30:31 +02:00
|
|
|
|
2022-09-28 16:09:38 +02:00
|
|
|
p += output_len;
|
2023-01-11 14:50:10 +01:00
|
|
|
l_binders_len += 1 + PSA_HASH_LENGTH(hash_alg);
|
2022-09-28 16:09:38 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(3,
|
|
|
|
("client hello, adding pre_shared_key extension, "
|
|
|
|
"omitting PSK binder list"));
|
2022-10-08 05:17:14 +02:00
|
|
|
|
|
|
|
/* Take into account the two bytes for the length of the binders. */
|
|
|
|
l_binders_len += 2;
|
2022-10-10 15:33:51 +02:00
|
|
|
/* Check if there is enough space for binders */
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_CHK_BUF_PTR(p, end, l_binders_len);
|
2022-10-08 05:17:14 +02:00
|
|
|
|
2022-09-28 16:09:38 +02:00
|
|
|
/*
|
|
|
|
* - extension_type (2 bytes)
|
|
|
|
* - extension_data_len (2 bytes)
|
|
|
|
* - identities_len (2 bytes)
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_PRE_SHARED_KEY, buf, 0);
|
|
|
|
MBEDTLS_PUT_UINT16_BE(p - buf - 4 + l_binders_len, buf, 2);
|
|
|
|
MBEDTLS_PUT_UINT16_BE(p - buf - 6, buf, 4);
|
2022-09-28 16:09:38 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
*out_len = (p - buf) + l_binders_len;
|
2022-10-08 05:17:14 +02:00
|
|
|
*binders_len = l_binders_len;
|
2022-09-28 16:09:38 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_BUF(3, "pre_shared_key identities", buf, p - buf);
|
2022-09-28 16:09:38 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2022-07-05 10:21:43 +02:00
|
|
|
}
|
|
|
|
|
2022-07-19 11:51:50 +02:00
|
|
|
int mbedtls_ssl_tls13_write_binders_of_pre_shared_key_ext(
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_ssl_context *ssl, unsigned char *buf, unsigned char *end)
|
2022-07-05 10:21:43 +02:00
|
|
|
{
|
2022-09-28 16:11:02 +02:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
|
|
|
unsigned char *p = buf;
|
2022-09-30 05:08:57 +02:00
|
|
|
psa_algorithm_t hash_alg = PSA_ALG_NONE;
|
|
|
|
const unsigned char *psk;
|
|
|
|
size_t psk_len;
|
|
|
|
size_t output_len;
|
2022-09-28 16:11:02 +02:00
|
|
|
|
|
|
|
/* Check if we have space to write binders_len.
|
|
|
|
* - binders_len (2 bytes)
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
|
2022-09-28 16:11:02 +02:00
|
|
|
p += 2;
|
|
|
|
|
2022-09-30 05:08:57 +02:00
|
|
|
#if defined(MBEDTLS_SSL_SESSION_TICKETS)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl_tls13_ticket_get_psk(ssl, &hash_alg, &psk, &psk_len) == 0) {
|
|
|
|
|
|
|
|
ret = ssl_tls13_write_binder(ssl, p, end,
|
|
|
|
MBEDTLS_SSL_TLS1_3_PSK_RESUMPTION,
|
|
|
|
hash_alg, psk, psk_len,
|
|
|
|
&output_len);
|
|
|
|
if (ret != 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
2022-09-28 16:11:02 +02:00
|
|
|
p += output_len;
|
|
|
|
}
|
2022-09-30 05:08:57 +02:00
|
|
|
#endif /* MBEDTLS_SSL_SESSION_TICKETS */
|
2022-09-28 16:11:02 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl_tls13_psk_get_psk(ssl, &hash_alg, &psk, &psk_len) == 0) {
|
2022-09-30 05:08:57 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = ssl_tls13_write_binder(ssl, p, end,
|
|
|
|
MBEDTLS_SSL_TLS1_3_PSK_EXTERNAL,
|
|
|
|
hash_alg, psk, psk_len,
|
|
|
|
&output_len);
|
|
|
|
if (ret != 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
2022-09-28 16:11:02 +02:00
|
|
|
p += output_len;
|
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(3, ("client hello, adding PSK binder list."));
|
2022-09-28 16:11:02 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* - binders_len (2 bytes)
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_PUT_UINT16_BE(p - buf - 2, buf, 0);
|
2022-09-28 16:11:02 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_BUF(3, "pre_shared_key binders", buf, p - buf);
|
2022-07-05 10:21:43 +02:00
|
|
|
|
2022-10-29 03:08:47 +02:00
|
|
|
mbedtls_ssl_tls13_set_hs_sent_ext_mask(
|
2023-01-11 14:50:10 +01:00
|
|
|
ssl, MBEDTLS_TLS_EXT_PRE_SHARED_KEY);
|
2022-08-29 09:25:36 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2022-08-12 11:26:40 +02:00
|
|
|
}
|
2022-07-05 10:21:43 +02:00
|
|
|
|
2022-08-12 11:26:40 +02:00
|
|
|
/*
|
|
|
|
* struct {
|
|
|
|
* opaque identity<1..2^16-1>;
|
|
|
|
* uint32 obfuscated_ticket_age;
|
|
|
|
* } PskIdentity;
|
|
|
|
*
|
|
|
|
* opaque PskBinderEntry<32..255>;
|
|
|
|
*
|
|
|
|
* struct {
|
|
|
|
*
|
|
|
|
* select (Handshake.msg_type) {
|
|
|
|
* ...
|
|
|
|
* case server_hello: uint16 selected_identity;
|
|
|
|
* };
|
|
|
|
*
|
|
|
|
* } PreSharedKeyExtension;
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
MBEDTLS_CHECK_RETURN_CRITICAL
|
2023-01-11 14:50:10 +01:00
|
|
|
static int ssl_tls13_parse_server_pre_shared_key_ext(mbedtls_ssl_context *ssl,
|
|
|
|
const unsigned char *buf,
|
|
|
|
const unsigned char *end)
|
2022-08-12 11:26:40 +02:00
|
|
|
{
|
2022-10-08 05:17:14 +02:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2022-09-28 16:12:07 +02:00
|
|
|
int selected_identity;
|
|
|
|
const unsigned char *psk;
|
|
|
|
size_t psk_len;
|
2022-10-08 05:17:14 +02:00
|
|
|
psa_algorithm_t hash_alg;
|
2022-09-28 16:12:07 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_CHK_BUF_READ_PTR(buf, end, 2);
|
|
|
|
selected_identity = MBEDTLS_GET_UINT16_BE(buf, 0);
|
2023-01-04 04:15:09 +01:00
|
|
|
ssl->handshake->selected_identity = (uint16_t) selected_identity;
|
2022-10-08 05:17:14 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(3, ("selected_identity = %d", selected_identity));
|
2022-10-08 05:17:14 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (selected_identity >= ssl_tls13_get_configured_psk_count(ssl)) {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(1, ("Invalid PSK identity."));
|
2022-09-28 16:12:07 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
|
|
|
|
MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
|
|
|
|
return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
|
2022-09-30 06:22:01 +02:00
|
|
|
}
|
2022-10-08 05:17:14 +02:00
|
|
|
|
2022-09-30 06:22:01 +02:00
|
|
|
#if defined(MBEDTLS_SSL_SESSION_TICKETS)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (selected_identity == 0 && ssl_tls13_has_configured_ticket(ssl)) {
|
|
|
|
ret = ssl_tls13_ticket_get_psk(ssl, &hash_alg, &psk, &psk_len);
|
|
|
|
} else
|
2022-09-30 06:22:01 +02:00
|
|
|
#endif
|
2023-01-11 14:50:10 +01:00
|
|
|
if (mbedtls_ssl_conf_has_static_psk(ssl->conf)) {
|
|
|
|
ret = ssl_tls13_psk_get_psk(ssl, &hash_alg, &psk, &psk_len);
|
|
|
|
} else {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
|
|
|
|
return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
|
2022-09-28 16:12:07 +02:00
|
|
|
}
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ret != 0) {
|
|
|
|
return ret;
|
2022-09-28 16:12:07 +02:00
|
|
|
}
|
|
|
|
|
2023-10-05 14:30:37 +02:00
|
|
|
if (mbedtls_md_psa_alg_from_type((mbedtls_md_type_t) ssl->handshake->ciphersuite_info->mac)
|
2023-02-07 03:08:56 +01:00
|
|
|
!= hash_alg) {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(
|
|
|
|
1, ("Invalid ciphersuite for external psk."));
|
|
|
|
|
|
|
|
MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
|
|
|
|
MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
|
|
|
|
return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
|
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = mbedtls_ssl_set_hs_psk(ssl, psk, psk_len);
|
|
|
|
if (ret != 0) {
|
|
|
|
MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_set_hs_psk", ret);
|
|
|
|
return ret;
|
2022-09-28 16:12:07 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2022-07-05 10:21:43 +02:00
|
|
|
}
|
2022-10-04 16:38:25 +02:00
|
|
|
#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED */
|
2022-07-05 10:21:43 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_ssl_tls13_write_client_hello_exts(mbedtls_ssl_context *ssl,
|
|
|
|
unsigned char *buf,
|
|
|
|
unsigned char *end,
|
|
|
|
size_t *out_len)
|
2022-02-18 12:06:07 +01:00
|
|
|
{
|
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
|
|
|
unsigned char *p = buf;
|
|
|
|
size_t ext_len;
|
|
|
|
|
|
|
|
*out_len = 0;
|
|
|
|
|
|
|
|
/* Write supported_versions extension
|
|
|
|
*
|
|
|
|
* Supported Versions Extension is mandatory with TLS 1.3.
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = ssl_tls13_write_supported_versions_ext(ssl, p, end, &ext_len);
|
|
|
|
if (ret != 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
2022-02-18 12:06:07 +01:00
|
|
|
p += ext_len;
|
|
|
|
|
|
|
|
/* Echo the cookie if the server provided one in its preceding
|
|
|
|
* HelloRetryRequest message.
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = ssl_tls13_write_cookie_ext(ssl, p, end, &ext_len);
|
|
|
|
if (ret != 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
2022-02-18 12:06:07 +01:00
|
|
|
p += ext_len;
|
|
|
|
|
2022-10-18 12:17:11 +02:00
|
|
|
#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (mbedtls_ssl_conf_tls13_some_ephemeral_enabled(ssl)) {
|
|
|
|
ret = ssl_tls13_write_key_share_ext(ssl, p, end, &ext_len);
|
|
|
|
if (ret != 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
2022-02-18 12:06:07 +01:00
|
|
|
p += ext_len;
|
|
|
|
}
|
2022-10-18 12:17:11 +02:00
|
|
|
#endif
|
2022-02-18 12:06:07 +01:00
|
|
|
|
2022-10-24 13:12:51 +02:00
|
|
|
#if defined(MBEDTLS_SSL_EARLY_DATA)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (mbedtls_ssl_conf_tls13_some_psk_enabled(ssl) &&
|
|
|
|
ssl_tls13_early_data_has_valid_ticket(ssl) &&
|
|
|
|
ssl->conf->early_data_enabled == MBEDTLS_SSL_EARLY_DATA_ENABLED) {
|
2023-11-23 11:06:06 +01:00
|
|
|
|
|
|
|
ret = mbedtls_ssl_tls13_write_early_data_ext(
|
2023-12-05 03:40:49 +01:00
|
|
|
ssl, 0, p, end, &ext_len);
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ret != 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
2022-10-24 13:12:51 +02:00
|
|
|
p += ext_len;
|
|
|
|
|
2022-10-26 18:49:09 +02:00
|
|
|
/* Initializes the status to `rejected`. It will be updated to
|
|
|
|
* `accepted` if the EncryptedExtension message contain an early data
|
|
|
|
* indication extension.
|
2022-11-09 02:59:33 +01:00
|
|
|
*/
|
2022-10-26 18:49:09 +02:00
|
|
|
ssl->early_data_status = MBEDTLS_SSL_EARLY_DATA_STATUS_REJECTED;
|
2023-01-11 14:50:10 +01:00
|
|
|
} else {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write early_data extension"));
|
2022-11-08 08:02:27 +01:00
|
|
|
ssl->early_data_status = MBEDTLS_SSL_EARLY_DATA_STATUS_NOT_SENT;
|
2022-10-24 13:12:51 +02:00
|
|
|
}
|
|
|
|
#endif /* MBEDTLS_SSL_EARLY_DATA */
|
|
|
|
|
2022-10-04 16:38:25 +02:00
|
|
|
#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
|
2022-07-05 10:21:43 +02:00
|
|
|
/* For PSK-based key exchange we need the pre_shared_key extension
|
|
|
|
* and the psk_key_exchange_modes extension.
|
|
|
|
*
|
|
|
|
* The pre_shared_key extension MUST be the last extension in the
|
|
|
|
* ClientHello. Servers MUST check that it is the last extension and
|
|
|
|
* otherwise fail the handshake with an "illegal_parameter" alert.
|
|
|
|
*
|
|
|
|
* Add the psk_key_exchange_modes extension.
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = ssl_tls13_write_psk_key_exchange_modes_ext(ssl, p, end, &ext_len);
|
|
|
|
if (ret != 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
2022-07-05 10:21:43 +02:00
|
|
|
p += ext_len;
|
2022-10-04 16:38:25 +02:00
|
|
|
#endif
|
2022-07-05 10:21:43 +02:00
|
|
|
|
2022-02-18 12:06:07 +01:00
|
|
|
*out_len = p - buf;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2022-02-18 12:06:07 +01:00
|
|
|
}
|
|
|
|
|
2023-02-06 11:23:04 +01:00
|
|
|
int mbedtls_ssl_tls13_finalize_client_hello(mbedtls_ssl_context *ssl)
|
2023-01-03 11:29:41 +01:00
|
|
|
{
|
2023-01-06 06:54:59 +01:00
|
|
|
((void) ssl);
|
2023-01-28 11:35:29 +01:00
|
|
|
|
2023-01-03 11:29:41 +01:00
|
|
|
#if defined(MBEDTLS_SSL_EARLY_DATA)
|
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
|
|
|
psa_algorithm_t hash_alg = PSA_ALG_NONE;
|
|
|
|
const unsigned char *psk;
|
|
|
|
size_t psk_len;
|
|
|
|
const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
|
2023-01-04 11:47:05 +01:00
|
|
|
|
2023-01-03 11:29:41 +01:00
|
|
|
if (ssl->early_data_status == MBEDTLS_SSL_EARLY_DATA_STATUS_REJECTED) {
|
2023-02-03 07:04:43 +01:00
|
|
|
#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
|
|
|
|
mbedtls_ssl_handshake_set_state(
|
|
|
|
ssl, MBEDTLS_SSL_CLIENT_CCS_AFTER_CLIENT_HELLO);
|
|
|
|
#endif
|
2023-01-03 11:29:41 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(
|
|
|
|
1, ("Set hs psk for early data when writing the first psk"));
|
|
|
|
|
|
|
|
ret = ssl_tls13_ticket_get_psk(ssl, &hash_alg, &psk, &psk_len);
|
|
|
|
if (ret != 0) {
|
|
|
|
MBEDTLS_SSL_DEBUG_RET(
|
|
|
|
1, "ssl_tls13_ticket_get_psk", ret);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = mbedtls_ssl_set_hs_psk(ssl, psk, psk_len);
|
|
|
|
if (ret != 0) {
|
|
|
|
MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_set_hs_psk", ret);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2023-02-07 03:32:23 +01:00
|
|
|
/*
|
|
|
|
* Early data are going to be encrypted using the ciphersuite
|
|
|
|
* associated with the pre-shared key used for the handshake.
|
|
|
|
* Note that if the server rejects early data, the handshake
|
|
|
|
* based on the pre-shared key may complete successfully
|
|
|
|
* with a selected ciphersuite different from the ciphersuite
|
|
|
|
* associated with the pre-shared key. Only the hashes of the
|
|
|
|
* two ciphersuites have to be the same. In that case, the
|
|
|
|
* encrypted handshake data and application data are
|
|
|
|
* encrypted using a different ciphersuite than the one used for
|
|
|
|
* the rejected early data.
|
|
|
|
*/
|
2023-01-03 11:29:41 +01:00
|
|
|
ciphersuite_info = mbedtls_ssl_ciphersuite_from_id(
|
|
|
|
ssl->session_negotiate->ciphersuite);
|
|
|
|
ssl->handshake->ciphersuite_info = ciphersuite_info;
|
2023-02-07 11:49:50 +01:00
|
|
|
|
2023-03-07 12:39:52 +01:00
|
|
|
/* Enable psk and psk_ephemeral to make stage early happy */
|
2023-01-03 11:29:41 +01:00
|
|
|
ssl->handshake->key_exchange_mode =
|
|
|
|
MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_ALL;
|
|
|
|
|
|
|
|
/* Start the TLS 1.3 key schedule:
|
2023-02-07 11:49:50 +01:00
|
|
|
* Set the PSK and derive early secret.
|
2023-01-03 11:29:41 +01:00
|
|
|
*/
|
|
|
|
ret = mbedtls_ssl_tls13_key_schedule_stage_early(ssl);
|
|
|
|
if (ret != 0) {
|
2023-02-08 07:04:50 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_RET(
|
|
|
|
1, "mbedtls_ssl_tls13_key_schedule_stage_early", ret);
|
2023-01-03 11:29:41 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Derive early data key material */
|
|
|
|
ret = mbedtls_ssl_tls13_compute_early_transform(ssl);
|
|
|
|
if (ret != 0) {
|
2023-02-08 07:04:50 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_RET(
|
|
|
|
1, "mbedtls_ssl_tls13_compute_early_transform", ret);
|
2023-01-03 11:29:41 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
#endif /* MBEDTLS_SSL_EARLY_DATA */
|
|
|
|
return 0;
|
|
|
|
}
|
2021-09-14 10:03:56 +02:00
|
|
|
/*
|
2021-10-11 15:45:31 +02:00
|
|
|
* Functions for parsing and processing Server Hello
|
2021-09-10 04:08:31 +02:00
|
|
|
*/
|
2022-04-05 11:04:20 +02:00
|
|
|
|
2022-03-30 09:57:11 +02:00
|
|
|
/**
|
|
|
|
* \brief Detect if the ServerHello contains a supported_versions extension
|
|
|
|
* or not.
|
|
|
|
*
|
|
|
|
* \param[in] ssl SSL context
|
|
|
|
* \param[in] buf Buffer containing the ServerHello message
|
|
|
|
* \param[in] end End of the buffer containing the ServerHello message
|
|
|
|
*
|
|
|
|
* \return 0 if the ServerHello does not contain a supported_versions extension
|
|
|
|
* \return 1 if the ServerHello contains a supported_versions extension
|
|
|
|
* \return A negative value if an error occurred while parsing the ServerHello.
|
|
|
|
*/
|
2022-06-17 10:52:54 +02:00
|
|
|
MBEDTLS_CHECK_RETURN_CRITICAL
|
2022-02-10 16:45:15 +01:00
|
|
|
static int ssl_tls13_is_supported_versions_ext_present(
|
|
|
|
mbedtls_ssl_context *ssl,
|
|
|
|
const unsigned char *buf,
|
2023-01-11 14:50:10 +01:00
|
|
|
const unsigned char *end)
|
2022-02-10 16:45:15 +01:00
|
|
|
{
|
|
|
|
const unsigned char *p = buf;
|
|
|
|
size_t legacy_session_id_echo_len;
|
2023-04-03 17:36:31 +02:00
|
|
|
const unsigned char *supported_versions_data;
|
|
|
|
const unsigned char *supported_versions_data_end;
|
2022-02-10 16:45:15 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Check there is enough data to access the legacy_session_id_echo vector
|
2022-03-30 09:57:11 +02:00
|
|
|
* length:
|
|
|
|
* - legacy_version 2 bytes
|
|
|
|
* - random MBEDTLS_SERVER_HELLO_RANDOM_LEN bytes
|
|
|
|
* - legacy_session_id_echo length 1 byte
|
2022-02-10 16:45:15 +01:00
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, MBEDTLS_SERVER_HELLO_RANDOM_LEN + 3);
|
2022-02-10 16:45:15 +01:00
|
|
|
p += MBEDTLS_SERVER_HELLO_RANDOM_LEN + 2;
|
|
|
|
legacy_session_id_echo_len = *p;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Jump to the extensions, jumping over:
|
|
|
|
* - legacy_session_id_echo (legacy_session_id_echo_len + 1) bytes
|
|
|
|
* - cipher_suite 2 bytes
|
|
|
|
* - legacy_compression_method 1 byte
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, legacy_session_id_echo_len + 4);
|
|
|
|
p += legacy_session_id_echo_len + 4;
|
2022-02-10 16:45:15 +01:00
|
|
|
|
2023-02-08 17:38:29 +01:00
|
|
|
return mbedtls_ssl_tls13_is_supported_versions_ext_present_in_exts(
|
|
|
|
ssl, p, end,
|
2023-04-03 17:36:31 +02:00
|
|
|
&supported_versions_data, &supported_versions_data_end);
|
2022-02-10 16:45:15 +01:00
|
|
|
}
|
|
|
|
|
2022-04-05 11:04:20 +02:00
|
|
|
/* Returns a negative value on failure, and otherwise
|
|
|
|
* - 1 if the last eight bytes of the ServerHello random bytes indicate that
|
|
|
|
* the server is TLS 1.3 capable but negotiating TLS 1.2 or below.
|
|
|
|
* - 0 otherwise
|
|
|
|
*/
|
2022-06-17 10:52:54 +02:00
|
|
|
MBEDTLS_CHECK_RETURN_CRITICAL
|
2023-01-11 14:50:10 +01:00
|
|
|
static int ssl_tls13_is_downgrade_negotiation(mbedtls_ssl_context *ssl,
|
|
|
|
const unsigned char *buf,
|
|
|
|
const unsigned char *end)
|
2022-04-05 11:04:20 +02:00
|
|
|
{
|
|
|
|
/* First seven bytes of the magic downgrade strings, see RFC 8446 4.1.3 */
|
|
|
|
static const unsigned char magic_downgrade_string[] =
|
2023-01-11 14:50:10 +01:00
|
|
|
{ 0x44, 0x4F, 0x57, 0x4E, 0x47, 0x52, 0x44 };
|
2022-04-05 11:04:20 +02:00
|
|
|
const unsigned char *last_eight_bytes_of_random;
|
|
|
|
unsigned char last_byte_of_random;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_CHK_BUF_READ_PTR(buf, end, MBEDTLS_SERVER_HELLO_RANDOM_LEN + 2);
|
2022-04-05 11:04:20 +02:00
|
|
|
last_eight_bytes_of_random = buf + 2 + MBEDTLS_SERVER_HELLO_RANDOM_LEN - 8;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (memcmp(last_eight_bytes_of_random,
|
|
|
|
magic_downgrade_string,
|
|
|
|
sizeof(magic_downgrade_string)) == 0) {
|
2022-04-05 11:04:20 +02:00
|
|
|
last_byte_of_random = last_eight_bytes_of_random[7];
|
2023-01-11 14:50:10 +01:00
|
|
|
return last_byte_of_random == 0 ||
|
|
|
|
last_byte_of_random == 1;
|
2022-04-05 11:04:20 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2022-04-05 11:04:20 +02:00
|
|
|
}
|
|
|
|
|
2021-10-15 12:46:14 +02:00
|
|
|
/* Returns a negative value on failure, and otherwise
|
2022-05-31 12:04:31 +02:00
|
|
|
* - SSL_SERVER_HELLO or
|
|
|
|
* - SSL_SERVER_HELLO_HRR
|
2021-12-10 11:33:56 +01:00
|
|
|
* to indicate which message is expected and to be parsed next.
|
|
|
|
*/
|
2022-05-31 12:04:31 +02:00
|
|
|
#define SSL_SERVER_HELLO 0
|
|
|
|
#define SSL_SERVER_HELLO_HRR 1
|
2022-06-17 10:52:54 +02:00
|
|
|
MBEDTLS_CHECK_RETURN_CRITICAL
|
2023-01-11 14:50:10 +01:00
|
|
|
static int ssl_server_hello_is_hrr(mbedtls_ssl_context *ssl,
|
|
|
|
const unsigned char *buf,
|
|
|
|
const unsigned char *end)
|
2021-09-10 04:08:31 +02:00
|
|
|
{
|
|
|
|
|
|
|
|
/* Check whether this message is a HelloRetryRequest ( HRR ) message.
|
|
|
|
*
|
2021-10-11 15:45:31 +02:00
|
|
|
* Server Hello and HRR are only distinguished by Random set to the
|
2021-09-10 04:08:31 +02:00
|
|
|
* special value of the SHA-256 of "HelloRetryRequest".
|
|
|
|
*
|
|
|
|
* struct {
|
|
|
|
* ProtocolVersion legacy_version = 0x0303;
|
|
|
|
* Random random;
|
|
|
|
* opaque legacy_session_id_echo<0..32>;
|
|
|
|
* CipherSuite cipher_suite;
|
|
|
|
* uint8 legacy_compression_method = 0;
|
2021-10-13 07:36:05 +02:00
|
|
|
* Extension extensions<6..2^16-1>;
|
2021-09-10 04:08:31 +02:00
|
|
|
* } ServerHello;
|
|
|
|
*
|
|
|
|
*/
|
2023-03-29 12:37:35 +02:00
|
|
|
MBEDTLS_SSL_CHK_BUF_READ_PTR(
|
|
|
|
buf, end, 2 + sizeof(mbedtls_ssl_tls13_hello_retry_request_magic));
|
2021-10-11 15:45:31 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (memcmp(buf + 2, mbedtls_ssl_tls13_hello_retry_request_magic,
|
|
|
|
sizeof(mbedtls_ssl_tls13_hello_retry_request_magic)) == 0) {
|
|
|
|
return SSL_SERVER_HELLO_HRR;
|
2021-09-10 04:08:31 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return SSL_SERVER_HELLO;
|
2021-09-10 04:08:31 +02:00
|
|
|
}
|
|
|
|
|
2022-05-31 12:04:31 +02:00
|
|
|
/*
|
2021-10-13 16:01:04 +02:00
|
|
|
* Returns a negative value on failure, and otherwise
|
2022-05-31 12:04:31 +02:00
|
|
|
* - SSL_SERVER_HELLO or
|
|
|
|
* - SSL_SERVER_HELLO_HRR or
|
|
|
|
* - SSL_SERVER_HELLO_TLS1_2
|
2021-10-13 16:01:04 +02:00
|
|
|
*/
|
2022-05-31 12:04:31 +02:00
|
|
|
#define SSL_SERVER_HELLO_TLS1_2 2
|
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_server_hello(mbedtls_ssl_context *ssl,
|
|
|
|
const unsigned char *buf,
|
|
|
|
const unsigned char *end)
|
2021-09-10 04:08:31 +02:00
|
|
|
{
|
2021-10-11 15:45:31 +02:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2022-05-31 12:11:39 +02:00
|
|
|
mbedtls_ssl_handshake_params *handshake = ssl->handshake;
|
2021-09-10 04:08:31 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_PROC_CHK_NEG(ssl_tls13_is_supported_versions_ext_present(
|
|
|
|
ssl, buf, end));
|
2022-07-13 08:30:29 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ret == 0) {
|
2022-04-05 11:04:20 +02:00
|
|
|
MBEDTLS_SSL_PROC_CHK_NEG(
|
2023-01-11 14:50:10 +01:00
|
|
|
ssl_tls13_is_downgrade_negotiation(ssl, buf, end));
|
2022-04-05 11:04:20 +02:00
|
|
|
|
|
|
|
/* If the server is negotiating TLS 1.2 or below and:
|
|
|
|
* . we did not propose TLS 1.2 or
|
|
|
|
* . the server responded it is TLS 1.3 capable but negotiating a lower
|
|
|
|
* version of the protocol and thus we are under downgrade attack
|
|
|
|
* abort the handshake with an "illegal parameter" alert.
|
2022-02-10 16:45:15 +01:00
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
if (handshake->min_tls_version > MBEDTLS_SSL_VERSION_TLS1_2 || ret) {
|
|
|
|
MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
|
|
|
|
MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
|
|
|
|
return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
|
2022-02-10 16:45:15 +01:00
|
|
|
}
|
|
|
|
|
2023-04-03 16:37:22 +02:00
|
|
|
/*
|
|
|
|
* Version 1.2 of the protocol has been negotiated, set the
|
|
|
|
* ssl->keep_current_message flag for the ServerHello to be kept and
|
|
|
|
* parsed as a TLS 1.2 ServerHello. We also change ssl->tls_version to
|
|
|
|
* MBEDTLS_SSL_VERSION_TLS1_2 thus from now on mbedtls_ssl_handshake_step()
|
|
|
|
* will dispatch to the TLS 1.2 state machine.
|
|
|
|
*/
|
2022-02-10 16:45:15 +01:00
|
|
|
ssl->keep_current_message = 1;
|
2022-03-15 00:04:24 +01:00
|
|
|
ssl->tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
|
2023-03-29 12:37:35 +02:00
|
|
|
MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
|
|
|
|
ssl, MBEDTLS_SSL_HS_SERVER_HELLO,
|
|
|
|
buf, (size_t) (end - buf)));
|
2023-01-11 14:50:10 +01:00
|
|
|
|
|
|
|
if (mbedtls_ssl_conf_tls13_some_ephemeral_enabled(ssl)) {
|
|
|
|
ret = ssl_tls13_reset_key_share(ssl);
|
|
|
|
if (ret != 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
2022-02-10 16:45:15 +01:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return SSL_SERVER_HELLO_TLS1_2;
|
2022-02-10 16:45:15 +01:00
|
|
|
}
|
|
|
|
|
2022-07-13 08:30:29 +02:00
|
|
|
#if defined(MBEDTLS_SSL_SESSION_TICKETS)
|
|
|
|
ssl->session_negotiate->endpoint = ssl->conf->endpoint;
|
|
|
|
ssl->session_negotiate->tls_version = ssl->tls_version;
|
|
|
|
#endif /* MBEDTLS_SSL_SESSION_TICKETS */
|
|
|
|
|
2022-10-31 07:45:01 +01:00
|
|
|
handshake->received_extensions = MBEDTLS_SSL_EXT_MASK_NONE;
|
2022-05-31 12:11:39 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = ssl_server_hello_is_hrr(ssl, buf, end);
|
|
|
|
switch (ret) {
|
2022-05-31 12:04:31 +02:00
|
|
|
case SSL_SERVER_HELLO:
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(2, ("received ServerHello message"));
|
2021-10-13 16:01:04 +02:00
|
|
|
break;
|
2022-05-31 12:04:31 +02:00
|
|
|
case SSL_SERVER_HELLO_HRR:
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(2, ("received HelloRetryRequest message"));
|
2023-03-29 12:37:35 +02:00
|
|
|
/* If a client receives a second HelloRetryRequest in the same
|
|
|
|
* connection (i.e., where the ClientHello was itself in response
|
|
|
|
* to a HelloRetryRequest), it MUST abort the handshake with an
|
|
|
|
* "unexpected_message" alert.
|
2023-01-11 14:50:10 +01:00
|
|
|
*/
|
|
|
|
if (handshake->hello_retry_request_count > 0) {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(1, ("Multiple HRRs received"));
|
2023-03-29 12:37:35 +02:00
|
|
|
MBEDTLS_SSL_PEND_FATAL_ALERT(
|
|
|
|
MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE,
|
|
|
|
MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE);
|
2023-01-11 14:50:10 +01:00
|
|
|
return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
|
2022-01-14 08:35:47 +01:00
|
|
|
}
|
2022-01-21 03:53:13 +01:00
|
|
|
/*
|
|
|
|
* Clients must abort the handshake with an "illegal_parameter"
|
|
|
|
* alert if the HelloRetryRequest would not result in any change
|
|
|
|
* in the ClientHello.
|
|
|
|
* In a PSK only key exchange that what we expect.
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
if (!mbedtls_ssl_conf_tls13_some_ephemeral_enabled(ssl)) {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(1,
|
|
|
|
("Unexpected HRR in pure PSK key exchange."));
|
2022-01-21 03:53:13 +01:00
|
|
|
MBEDTLS_SSL_PEND_FATAL_ALERT(
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
|
|
|
|
MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
|
|
|
|
return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
|
2022-01-21 03:53:13 +01:00
|
|
|
}
|
2022-01-14 08:35:47 +01:00
|
|
|
|
2022-05-31 12:11:39 +02:00
|
|
|
handshake->hello_retry_request_count++;
|
2022-01-14 08:35:47 +01:00
|
|
|
|
2021-10-13 16:01:04 +02:00
|
|
|
break;
|
2021-09-10 04:08:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
cleanup:
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return ret;
|
2021-09-10 04:08: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_tls13_check_server_hello_session_id_echo(mbedtls_ssl_context *ssl,
|
|
|
|
const unsigned char **buf,
|
|
|
|
const unsigned char *end)
|
2021-09-10 04:08:31 +02:00
|
|
|
{
|
|
|
|
const unsigned char *p = *buf;
|
2021-10-11 15:45:31 +02:00
|
|
|
size_t legacy_session_id_echo_len;
|
2021-09-10 04:08:31 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 1);
|
|
|
|
legacy_session_id_echo_len = *p++;
|
2021-09-10 04:08:31 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, legacy_session_id_echo_len);
|
2021-09-10 04:08:31 +02:00
|
|
|
|
|
|
|
/* legacy_session_id_echo */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->session_negotiate->id_len != legacy_session_id_echo_len ||
|
|
|
|
memcmp(ssl->session_negotiate->id, p, legacy_session_id_echo_len) != 0) {
|
|
|
|
MBEDTLS_SSL_DEBUG_BUF(3, "Expected Session ID",
|
|
|
|
ssl->session_negotiate->id,
|
|
|
|
ssl->session_negotiate->id_len);
|
|
|
|
MBEDTLS_SSL_DEBUG_BUF(3, "Received Session ID", p,
|
|
|
|
legacy_session_id_echo_len);
|
2021-10-11 15:45:31 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
|
|
|
|
MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
|
2021-10-11 15:45:31 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
|
2021-09-10 04:08:31 +02:00
|
|
|
}
|
|
|
|
|
2021-10-11 15:45:31 +02:00
|
|
|
p += legacy_session_id_echo_len;
|
2021-09-10 04:08:31 +02:00
|
|
|
*buf = p;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_BUF(3, "Session ID", ssl->session_negotiate->id,
|
|
|
|
ssl->session_negotiate->id_len);
|
|
|
|
return 0;
|
2021-09-10 04:08:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Parse ServerHello message and configure context
|
|
|
|
*
|
|
|
|
* struct {
|
|
|
|
* ProtocolVersion legacy_version = 0x0303; // TLS 1.2
|
|
|
|
* Random random;
|
|
|
|
* opaque legacy_session_id_echo<0..32>;
|
|
|
|
* CipherSuite cipher_suite;
|
|
|
|
* uint8 legacy_compression_method = 0;
|
2021-10-13 07:36:05 +02:00
|
|
|
* Extension extensions<6..2^16-1>;
|
2021-09-10 04:08:31 +02:00
|
|
|
* } ServerHello;
|
|
|
|
*/
|
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_server_hello(mbedtls_ssl_context *ssl,
|
|
|
|
const unsigned char *buf,
|
|
|
|
const unsigned char *end,
|
|
|
|
int is_hrr)
|
2021-09-10 04:08:31 +02:00
|
|
|
{
|
2021-10-13 07:36:05 +02:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2021-09-10 04:08:31 +02:00
|
|
|
const unsigned char *p = buf;
|
2022-01-20 12:14:50 +01:00
|
|
|
mbedtls_ssl_handshake_params *handshake = ssl->handshake;
|
2021-10-13 07:36:05 +02:00
|
|
|
size_t extensions_len;
|
|
|
|
const unsigned char *extensions_end;
|
2021-09-10 04:08:31 +02:00
|
|
|
uint16_t cipher_suite;
|
2021-09-19 12:05:08 +02:00
|
|
|
const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
|
2022-01-26 04:29:10 +01:00
|
|
|
int fatal_alert = 0;
|
2022-08-29 09:25:36 +02:00
|
|
|
uint32_t allowed_extensions_mask;
|
2022-10-31 07:45:01 +01:00
|
|
|
int hs_msg_type = is_hrr ? MBEDTLS_SSL_TLS1_3_HS_HELLO_RETRY_REQUEST :
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_HS_SERVER_HELLO;
|
2021-09-10 04:08:31 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Check there is space for minimal fields
|
|
|
|
*
|
|
|
|
* - legacy_version ( 2 bytes)
|
2021-10-26 04:44:32 +02:00
|
|
|
* - random (MBEDTLS_SERVER_HELLO_RANDOM_LEN bytes)
|
2021-09-10 04:08:31 +02:00
|
|
|
* - legacy_session_id_echo ( 1 byte ), minimum size
|
|
|
|
* - cipher_suite ( 2 bytes)
|
|
|
|
* - legacy_compression_method ( 1 byte )
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, MBEDTLS_SERVER_HELLO_RANDOM_LEN + 6);
|
2021-09-10 04:08:31 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_BUF(4, "server hello", p, end - p);
|
|
|
|
MBEDTLS_SSL_DEBUG_BUF(3, "server hello, version", p, 2);
|
2021-09-10 04:08:31 +02:00
|
|
|
|
2021-10-11 15:45:31 +02:00
|
|
|
/* ...
|
2021-10-13 07:36:05 +02:00
|
|
|
* ProtocolVersion legacy_version = 0x0303; // TLS 1.2
|
2021-10-11 15:45:31 +02:00
|
|
|
* ...
|
|
|
|
* with ProtocolVersion defined as:
|
|
|
|
* uint16 ProtocolVersion;
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
if (mbedtls_ssl_read_version(p, ssl->conf->transport) !=
|
|
|
|
MBEDTLS_SSL_VERSION_TLS1_2) {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(1, ("Unsupported version of TLS."));
|
|
|
|
MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION,
|
|
|
|
MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION);
|
2022-01-24 11:12:51 +01:00
|
|
|
ret = MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION;
|
|
|
|
goto cleanup;
|
2021-09-10 04:08:31 +02:00
|
|
|
}
|
|
|
|
p += 2;
|
|
|
|
|
2021-10-26 04:44:32 +02:00
|
|
|
/* ...
|
2021-10-11 15:45:31 +02:00
|
|
|
* Random random;
|
|
|
|
* ...
|
|
|
|
* with Random defined as:
|
2021-10-26 04:44:32 +02:00
|
|
|
* opaque Random[MBEDTLS_SERVER_HELLO_RANDOM_LEN];
|
2021-10-11 15:45:31 +02:00
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
if (!is_hrr) {
|
|
|
|
memcpy(&handshake->randbytes[MBEDTLS_CLIENT_HELLO_RANDOM_LEN], p,
|
|
|
|
MBEDTLS_SERVER_HELLO_RANDOM_LEN);
|
|
|
|
MBEDTLS_SSL_DEBUG_BUF(3, "server hello, random bytes",
|
|
|
|
p, MBEDTLS_SERVER_HELLO_RANDOM_LEN);
|
2022-01-18 11:47:33 +01:00
|
|
|
}
|
2021-10-26 04:44:32 +02:00
|
|
|
p += MBEDTLS_SERVER_HELLO_RANDOM_LEN;
|
2021-09-10 04:08:31 +02:00
|
|
|
|
2021-10-11 15:45:31 +02:00
|
|
|
/* ...
|
|
|
|
* opaque legacy_session_id_echo<0..32>;
|
|
|
|
* ...
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl_tls13_check_server_hello_session_id_echo(ssl, &p, end) != 0) {
|
2022-01-26 10:49:29 +01:00
|
|
|
fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
|
2022-01-24 11:12:51 +01:00
|
|
|
goto cleanup;
|
2021-09-10 04:08:31 +02:00
|
|
|
}
|
|
|
|
|
2021-10-11 15:45:31 +02:00
|
|
|
/* ...
|
|
|
|
* CipherSuite cipher_suite;
|
|
|
|
* ...
|
|
|
|
* with CipherSuite defined as:
|
|
|
|
* uint8 CipherSuite[2];
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
|
|
|
|
cipher_suite = MBEDTLS_GET_UINT16_BE(p, 0);
|
2021-09-10 04:08:31 +02:00
|
|
|
p += 2;
|
|
|
|
|
2021-10-11 15:45:31 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
ciphersuite_info = mbedtls_ssl_ciphersuite_from_id(cipher_suite);
|
2021-10-11 15:45:31 +02:00
|
|
|
/*
|
2022-03-30 22:09:48 +02:00
|
|
|
* Check whether this ciphersuite is valid and offered.
|
2021-10-11 15:45:31 +02:00
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
if ((mbedtls_ssl_validate_ciphersuite(ssl, ciphersuite_info,
|
|
|
|
ssl->tls_version,
|
|
|
|
ssl->tls_version) != 0) ||
|
|
|
|
!mbedtls_ssl_tls13_cipher_suite_is_offered(ssl, cipher_suite)) {
|
2022-01-26 10:49:29 +01:00
|
|
|
fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
|
2021-09-10 04:08:31 +02:00
|
|
|
}
|
2022-01-18 11:47:33 +01:00
|
|
|
/*
|
2022-01-20 12:14:50 +01:00
|
|
|
* If we received an HRR before and that the proposed selected
|
|
|
|
* ciphersuite in this server hello is not the same as the one
|
|
|
|
* proposed in the HRR, we abort the handshake and send an
|
|
|
|
* "illegal_parameter" alert.
|
2022-01-18 11:47:33 +01:00
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
else if ((!is_hrr) && (handshake->hello_retry_request_count > 0) &&
|
|
|
|
(cipher_suite != ssl->session_negotiate->ciphersuite)) {
|
2022-01-26 10:49:29 +01:00
|
|
|
fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
|
2022-01-20 12:14:50 +01:00
|
|
|
}
|
2022-01-18 11:47:33 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (fatal_alert == MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER) {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(1, ("invalid ciphersuite(%04x) parameter",
|
|
|
|
cipher_suite));
|
2022-01-24 11:12:51 +01:00
|
|
|
goto cleanup;
|
2022-01-18 11:47:33 +01:00
|
|
|
}
|
2021-09-10 04:08:31 +02:00
|
|
|
|
2021-10-11 15:45:31 +02:00
|
|
|
/* Configure ciphersuites */
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_ssl_optimize_checksum(ssl, ciphersuite_info);
|
2021-09-10 04:08:31 +02:00
|
|
|
|
2022-01-20 12:14:50 +01:00
|
|
|
handshake->ciphersuite_info = ciphersuite_info;
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, chosen ciphersuite: ( %04x ) - %s",
|
|
|
|
cipher_suite, ciphersuite_info->name));
|
2021-09-10 04:08:31 +02:00
|
|
|
|
|
|
|
#if defined(MBEDTLS_HAVE_TIME)
|
2023-05-22 21:08:12 +02:00
|
|
|
ssl->session_negotiate->start = mbedtls_time(NULL);
|
2021-09-10 04:08:31 +02:00
|
|
|
#endif /* MBEDTLS_HAVE_TIME */
|
|
|
|
|
2021-10-11 15:45:31 +02:00
|
|
|
/* ...
|
|
|
|
* uint8 legacy_compression_method = 0;
|
|
|
|
* ...
|
2021-09-10 04:08:31 +02:00
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 1);
|
|
|
|
if (p[0] != MBEDTLS_SSL_COMPRESS_NULL) {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(1, ("bad legacy compression method"));
|
2022-01-26 10:49:29 +01:00
|
|
|
fatal_alert = MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER;
|
2022-01-24 11:12:51 +01:00
|
|
|
goto cleanup;
|
2021-09-10 04:08:31 +02:00
|
|
|
}
|
|
|
|
p++;
|
|
|
|
|
2021-10-13 07:36:05 +02:00
|
|
|
/* ...
|
|
|
|
* Extension extensions<6..2^16-1>;
|
|
|
|
* ...
|
2021-10-11 15:45:31 +02:00
|
|
|
* struct {
|
|
|
|
* ExtensionType extension_type; (2 bytes)
|
|
|
|
* opaque extension_data<0..2^16-1>;
|
|
|
|
* } Extension;
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
|
|
|
|
extensions_len = MBEDTLS_GET_UINT16_BE(p, 0);
|
2021-09-10 04:08:31 +02:00
|
|
|
p += 2;
|
2021-09-19 12:05:08 +02:00
|
|
|
|
2021-10-11 15:45:31 +02:00
|
|
|
/* Check extensions do not go beyond the buffer of data. */
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, extensions_len);
|
2021-10-11 15:45:31 +02:00
|
|
|
extensions_end = p + extensions_len;
|
2021-09-10 04:08:31 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_BUF(3, "server hello extensions", p, extensions_len);
|
2021-09-10 04:08:31 +02:00
|
|
|
|
2022-10-31 07:45:01 +01:00
|
|
|
handshake->received_extensions = MBEDTLS_SSL_EXT_MASK_NONE;
|
2022-08-29 09:25:36 +02:00
|
|
|
allowed_extensions_mask = is_hrr ?
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_TLS1_3_ALLOWED_EXTS_OF_HRR :
|
|
|
|
MBEDTLS_SSL_TLS1_3_ALLOWED_EXTS_OF_SH;
|
2022-08-04 11:42:49 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
while (p < extensions_end) {
|
2021-09-10 04:08:31 +02:00
|
|
|
unsigned int extension_type;
|
|
|
|
size_t extension_data_len;
|
2022-01-21 05:32:58 +01:00
|
|
|
const unsigned char *extension_data_end;
|
2021-09-10 04:08:31 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
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);
|
2021-09-10 04:08:31 +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-01-21 05:32:58 +01:00
|
|
|
extension_data_end = p + extension_data_len;
|
2021-09-10 04:08:31 +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, hs_msg_type, extension_type, allowed_extensions_mask);
|
|
|
|
if (ret != 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
2022-08-04 11:42:49 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
switch (extension_type) {
|
2022-01-14 05:03:11 +01:00
|
|
|
case MBEDTLS_TLS_EXT_COOKIE:
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = ssl_tls13_parse_cookie_ext(ssl,
|
|
|
|
p, extension_data_end);
|
|
|
|
if (ret != 0) {
|
|
|
|
MBEDTLS_SSL_DEBUG_RET(1,
|
|
|
|
"ssl_tls13_parse_cookie_ext",
|
|
|
|
ret);
|
2022-01-24 11:12:51 +01:00
|
|
|
goto cleanup;
|
2022-01-14 05:03:11 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2021-09-10 04:08:31 +02:00
|
|
|
case MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS:
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = ssl_tls13_parse_supported_versions_ext(ssl,
|
|
|
|
p,
|
|
|
|
extension_data_end);
|
|
|
|
if (ret != 0) {
|
2022-01-24 11:12:51 +01:00
|
|
|
goto cleanup;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2021-09-10 04:08:31 +02:00
|
|
|
break;
|
|
|
|
|
2022-10-04 16:38:25 +02:00
|
|
|
#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
|
2021-09-10 04:08:31 +02:00
|
|
|
case MBEDTLS_TLS_EXT_PRE_SHARED_KEY:
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(3, ("found pre_shared_key extension"));
|
2021-10-11 15:45:31 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if ((ret = ssl_tls13_parse_server_pre_shared_key_ext(
|
|
|
|
ssl, p, extension_data_end)) != 0) {
|
2022-07-05 10:21:43 +02:00
|
|
|
MBEDTLS_SSL_DEBUG_RET(
|
2023-01-11 14:50:10 +01:00
|
|
|
1, ("ssl_tls13_parse_server_pre_shared_key_ext"), ret);
|
|
|
|
return ret;
|
2022-07-05 10:21:43 +02:00
|
|
|
}
|
|
|
|
break;
|
2022-10-04 16:38:25 +02:00
|
|
|
#endif
|
2021-09-10 04:08:31 +02:00
|
|
|
|
|
|
|
case MBEDTLS_TLS_EXT_KEY_SHARE:
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(3, ("found key_shares extension"));
|
|
|
|
if (!mbedtls_ssl_conf_tls13_some_ephemeral_enabled(ssl)) {
|
2022-01-26 10:49:29 +01:00
|
|
|
fatal_alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT;
|
2022-01-24 11:12:51 +01:00
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (is_hrr) {
|
|
|
|
ret = ssl_tls13_parse_hrr_key_share_ext(ssl,
|
|
|
|
p, extension_data_end);
|
|
|
|
} else {
|
|
|
|
ret = ssl_tls13_parse_key_share_ext(ssl,
|
|
|
|
p, extension_data_end);
|
|
|
|
}
|
|
|
|
if (ret != 0) {
|
|
|
|
MBEDTLS_SSL_DEBUG_RET(1,
|
|
|
|
"ssl_tls13_parse_key_share_ext",
|
|
|
|
ret);
|
2022-01-24 11:12:51 +01:00
|
|
|
goto cleanup;
|
2021-09-10 04:08:31 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2022-08-29 07:42:01 +02:00
|
|
|
ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
|
|
|
|
goto cleanup;
|
2021-09-10 04:08:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
p += extension_data_len;
|
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_PRINT_EXTS(3, hs_msg_type, handshake->received_extensions);
|
2022-08-04 11:42:49 +02:00
|
|
|
|
2022-01-24 11:12:51 +01:00
|
|
|
cleanup:
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (fatal_alert == MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT) {
|
|
|
|
MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT,
|
|
|
|
MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION);
|
2022-01-24 11:12:51 +01:00
|
|
|
ret = MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION;
|
2023-01-11 14:50:10 +01:00
|
|
|
} else if (fatal_alert == MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER) {
|
|
|
|
MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
|
|
|
|
MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
|
2022-01-24 11:12:51 +01:00
|
|
|
ret = MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
|
2022-01-18 11:47:33 +01:00
|
|
|
}
|
2023-01-11 14:50:10 +01:00
|
|
|
return ret;
|
2021-09-10 04:08:31 +02:00
|
|
|
}
|
|
|
|
|
2022-09-26 13:59:32 +02:00
|
|
|
#if defined(MBEDTLS_DEBUG_C)
|
|
|
|
static const char *ssl_tls13_get_kex_mode_str(int mode)
|
2022-09-26 10:23:45 +02:00
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
switch (mode) {
|
2022-09-26 10:23:45 +02:00
|
|
|
case MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK:
|
|
|
|
return "psk";
|
|
|
|
case MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL:
|
|
|
|
return "ephemeral";
|
|
|
|
case MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL:
|
|
|
|
return "psk_ephemeral";
|
|
|
|
default:
|
|
|
|
return "unknown mode";
|
|
|
|
}
|
|
|
|
}
|
2022-09-26 13:59:32 +02:00
|
|
|
#endif /* MBEDTLS_DEBUG_C */
|
2022-09-26 10:23:45 +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_postprocess_server_hello(mbedtls_ssl_context *ssl)
|
2021-09-10 04:08:31 +02:00
|
|
|
{
|
2021-10-13 07:36:05 +02:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
|
|
|
mbedtls_ssl_handshake_params *handshake = ssl->handshake;
|
|
|
|
|
|
|
|
/* Determine the key exchange mode:
|
|
|
|
* 1) If both the pre_shared_key and key_share extensions were received
|
|
|
|
* then the key exchange mode is PSK with EPHEMERAL.
|
|
|
|
* 2) If only the pre_shared_key extension was received then the key
|
|
|
|
* exchange mode is PSK-only.
|
|
|
|
* 3) If only the key_share extension was received then the key
|
|
|
|
* exchange mode is EPHEMERAL-only.
|
2021-09-05 13:41:30 +02:00
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
switch (handshake->received_extensions &
|
2023-03-29 12:37:35 +02:00
|
|
|
(MBEDTLS_SSL_EXT_MASK(PRE_SHARED_KEY) |
|
|
|
|
MBEDTLS_SSL_EXT_MASK(KEY_SHARE))) {
|
2021-10-13 16:01:04 +02:00
|
|
|
/* Only the pre_shared_key extension was received */
|
2023-01-11 14:50:10 +01:00
|
|
|
case MBEDTLS_SSL_EXT_MASK(PRE_SHARED_KEY):
|
2023-03-29 12:37:35 +02:00
|
|
|
handshake->key_exchange_mode =
|
|
|
|
MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK;
|
2021-10-13 16:01:04 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
/* Only the key_share extension was received */
|
2023-01-11 14:50:10 +01:00
|
|
|
case MBEDTLS_SSL_EXT_MASK(KEY_SHARE):
|
2023-03-29 12:37:35 +02:00
|
|
|
handshake->key_exchange_mode =
|
|
|
|
MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL;
|
2021-10-13 16:01:04 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
/* Both the pre_shared_key and key_share extensions were received */
|
2023-03-29 12:37:35 +02:00
|
|
|
case (MBEDTLS_SSL_EXT_MASK(PRE_SHARED_KEY) |
|
|
|
|
MBEDTLS_SSL_EXT_MASK(KEY_SHARE)):
|
|
|
|
handshake->key_exchange_mode =
|
|
|
|
MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL;
|
2021-10-13 16:01:04 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
/* Neither pre_shared_key nor key_share extension was received */
|
|
|
|
default:
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(1, ("Unknown key exchange."));
|
2021-10-13 16:01:04 +02:00
|
|
|
ret = MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
|
|
|
|
goto cleanup;
|
2021-09-05 13:41:30 +02:00
|
|
|
}
|
|
|
|
|
2023-02-08 07:04:50 +01:00
|
|
|
if (!mbedtls_ssl_conf_tls13_check_kex_modes(
|
|
|
|
ssl, handshake->key_exchange_mode)) {
|
2022-09-26 06:01:06 +02:00
|
|
|
ret = MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
|
2023-03-29 12:37:35 +02:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(
|
|
|
|
2, ("Key exchange mode(%s) is not supported.",
|
|
|
|
ssl_tls13_get_kex_mode_str(handshake->key_exchange_mode)));
|
2022-09-26 06:01:06 +02:00
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
2023-03-29 12:37:35 +02:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(
|
|
|
|
3, ("Selected key exchange mode: %s",
|
|
|
|
ssl_tls13_get_kex_mode_str(handshake->key_exchange_mode)));
|
2022-09-26 10:23:45 +02:00
|
|
|
|
2023-02-02 07:05:48 +01:00
|
|
|
/* Start the TLS 1.3 key scheduling if not already done.
|
2021-09-05 13:41:30 +02:00
|
|
|
*
|
2023-02-02 07:05:48 +01:00
|
|
|
* If we proposed early data then we have already derived an
|
|
|
|
* early secret using the selected PSK and its associated hash.
|
|
|
|
* It means that if the negotiated key exchange mode is psk or
|
|
|
|
* psk_ephemeral, we have already correctly computed the
|
|
|
|
* early secret and thus we do not do it again. In all other
|
|
|
|
* cases we compute it here.
|
2023-01-04 09:43:46 +01:00
|
|
|
*/
|
2023-01-06 06:54:59 +01:00
|
|
|
#if defined(MBEDTLS_SSL_EARLY_DATA)
|
2023-02-07 03:19:42 +01:00
|
|
|
if (ssl->early_data_status == MBEDTLS_SSL_EARLY_DATA_STATUS_NOT_SENT ||
|
|
|
|
handshake->key_exchange_mode ==
|
|
|
|
MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL)
|
2023-01-06 06:54:59 +01:00
|
|
|
#endif
|
|
|
|
{
|
2023-01-04 09:43:46 +01:00
|
|
|
ret = mbedtls_ssl_tls13_key_schedule_stage_early(ssl);
|
|
|
|
if (ret != 0) {
|
|
|
|
MBEDTLS_SSL_DEBUG_RET(
|
|
|
|
1, "mbedtls_ssl_tls13_key_schedule_stage_early", ret);
|
|
|
|
goto cleanup;
|
|
|
|
}
|
2021-09-05 13:41:30 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = mbedtls_ssl_tls13_compute_handshake_transform(ssl);
|
|
|
|
if (ret != 0) {
|
|
|
|
MBEDTLS_SSL_DEBUG_RET(1,
|
|
|
|
"mbedtls_ssl_tls13_compute_handshake_transform",
|
|
|
|
ret);
|
2021-10-11 15:45:31 +02:00
|
|
|
goto cleanup;
|
2021-09-05 13:41:30 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_ssl_set_inbound_transform(ssl, handshake->transform_handshake);
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(1, ("Switch to handshake keys for inbound traffic"));
|
2021-09-05 13:41:30 +02:00
|
|
|
ssl->session_in = ssl->session_negotiate;
|
|
|
|
|
2021-10-11 15:45:31 +02:00
|
|
|
cleanup:
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ret != 0) {
|
2021-10-11 15:45:31 +02:00
|
|
|
MBEDTLS_SSL_PEND_FATAL_ALERT(
|
|
|
|
MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
|
2021-10-11 15:45:31 +02:00
|
|
|
}
|
2022-05-05 04:19:22 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return ret;
|
2021-09-10 04:08: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_tls13_postprocess_hrr(mbedtls_ssl_context *ssl)
|
2021-12-07 10:16:29 +01:00
|
|
|
{
|
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_ssl_session_reset_msg_layer(ssl, 0);
|
2021-12-07 10:16:29 +01:00
|
|
|
|
2022-01-19 07:56:30 +01:00
|
|
|
/*
|
2022-01-20 12:14:50 +01:00
|
|
|
* We are going to re-generate a shared secret corresponding to the group
|
|
|
|
* selected by the server, which is different from the group for which we
|
|
|
|
* generated a shared secret in the first client hello.
|
|
|
|
* Thus, reset the shared secret.
|
2021-12-10 11:33:56 +01:00
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = ssl_tls13_reset_key_share(ssl);
|
|
|
|
if (ret != 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
2021-12-07 10:16:29 +01:00
|
|
|
|
2023-02-06 12:06:16 +01:00
|
|
|
ssl->session_negotiate->ciphersuite = ssl->handshake->ciphersuite_info->id;
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2021-12-07 10:16:29 +01:00
|
|
|
}
|
|
|
|
|
2021-09-10 04:08:31 +02:00
|
|
|
/*
|
2021-10-11 15:45:31 +02:00
|
|
|
* Wait and parse ServerHello handshake message.
|
2021-09-14 10:03:56 +02:00
|
|
|
* Handler for MBEDTLS_SSL_SERVER_HELLO
|
|
|
|
*/
|
2022-06-17 10:52:54 +02:00
|
|
|
MBEDTLS_CHECK_RETURN_CRITICAL
|
2023-01-11 14:50:10 +01:00
|
|
|
static int ssl_tls13_process_server_hello(mbedtls_ssl_context *ssl)
|
2021-09-14 10:03:56 +02:00
|
|
|
{
|
2021-10-11 15:45:31 +02:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2021-12-07 10:16:29 +01:00
|
|
|
unsigned char *buf = NULL;
|
|
|
|
size_t buf_len = 0;
|
2022-01-19 07:56:30 +01:00
|
|
|
int is_hrr = 0;
|
2021-09-10 04:08:31 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(2, ("=> %s", __func__));
|
2021-09-10 04:08:31 +02:00
|
|
|
|
2023-03-29 12:37:35 +02:00
|
|
|
MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_tls13_fetch_handshake_msg(
|
|
|
|
ssl, MBEDTLS_SSL_HS_SERVER_HELLO, &buf, &buf_len));
|
2021-09-10 04:08:31 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = ssl_tls13_preprocess_server_hello(ssl, buf, buf + buf_len);
|
|
|
|
if (ret < 0) {
|
2022-01-14 08:35:47 +01:00
|
|
|
goto cleanup;
|
2023-01-11 14:50:10 +01:00
|
|
|
} else {
|
|
|
|
is_hrr = (ret == SSL_SERVER_HELLO_HRR);
|
|
|
|
}
|
2022-01-24 11:12:51 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ret == SSL_SERVER_HELLO_TLS1_2) {
|
2022-02-10 16:45:15 +01:00
|
|
|
ret = 0;
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_PROC_CHK(ssl_tls13_parse_server_hello(ssl, buf,
|
|
|
|
buf + buf_len,
|
|
|
|
is_hrr));
|
|
|
|
if (is_hrr) {
|
|
|
|
MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_reset_transcript_for_hrr(ssl));
|
|
|
|
}
|
2021-09-10 04:08:31 +02:00
|
|
|
|
2023-03-29 12:37:35 +02:00
|
|
|
MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
|
|
|
|
ssl, MBEDTLS_SSL_HS_SERVER_HELLO, buf, buf_len));
|
2021-09-10 04:08:31 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (is_hrr) {
|
|
|
|
MBEDTLS_SSL_PROC_CHK(ssl_tls13_postprocess_hrr(ssl));
|
2022-05-31 14:49:55 +02:00
|
|
|
#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
|
2023-01-11 14:50:10 +01:00
|
|
|
/* If not offering early data, the client sends a dummy CCS record
|
|
|
|
* immediately before its second flight. This may either be before
|
|
|
|
* its second ClientHello or before its encrypted handshake flight.
|
|
|
|
*/
|
2023-03-29 12:37:35 +02:00
|
|
|
mbedtls_ssl_handshake_set_state(
|
|
|
|
ssl, MBEDTLS_SSL_CLIENT_CCS_BEFORE_2ND_CLIENT_HELLO);
|
2022-05-31 14:49:55 +02:00
|
|
|
#else
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_HELLO);
|
2022-05-31 14:49:55 +02:00
|
|
|
#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
|
2023-01-11 14:50:10 +01:00
|
|
|
} else {
|
|
|
|
MBEDTLS_SSL_PROC_CHK(ssl_tls13_postprocess_server_hello(ssl));
|
|
|
|
mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_ENCRYPTED_EXTENSIONS);
|
2022-05-31 14:49:55 +02:00
|
|
|
}
|
2021-09-10 04:08:31 +02:00
|
|
|
|
|
|
|
cleanup:
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(2, ("<= %s ( %s )", __func__,
|
|
|
|
is_hrr ? "HelloRetryRequest" : "ServerHello"));
|
|
|
|
return ret;
|
2021-09-14 10:03:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2021-09-13 09:30:09 +02:00
|
|
|
*
|
2022-05-30 16:05:38 +02:00
|
|
|
* Handler for MBEDTLS_SSL_ENCRYPTED_EXTENSIONS
|
2021-09-13 09:30:09 +02:00
|
|
|
*
|
|
|
|
* The EncryptedExtensions message contains any extensions which
|
|
|
|
* should be protected, i.e., any which are not needed to establish
|
|
|
|
* the cryptographic context.
|
|
|
|
*/
|
|
|
|
|
2021-10-09 12:12:11 +02:00
|
|
|
/* Parse EncryptedExtensions message
|
|
|
|
* struct {
|
2021-10-11 12:05:54 +02:00
|
|
|
* Extension extensions<0..2^16-1>;
|
2021-10-09 12:12:11 +02:00
|
|
|
* } EncryptedExtensions;
|
|
|
|
*/
|
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_encrypted_extensions(mbedtls_ssl_context *ssl,
|
|
|
|
const unsigned char *buf,
|
|
|
|
const unsigned char *end)
|
2021-09-13 09:30:09 +02:00
|
|
|
{
|
|
|
|
int ret = 0;
|
2021-10-09 12:12:11 +02:00
|
|
|
size_t extensions_len;
|
2021-10-08 10:05:53 +02:00
|
|
|
const unsigned char *p = buf;
|
2021-10-13 07:56:18 +02:00
|
|
|
const unsigned char *extensions_end;
|
2022-10-29 03:08:47 +02:00
|
|
|
mbedtls_ssl_handshake_params *handshake = ssl->handshake;
|
2021-09-13 09:30:09 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
|
|
|
|
extensions_len = MBEDTLS_GET_UINT16_BE(p, 0);
|
2021-10-09 12:12:11 +02:00
|
|
|
p += 2;
|
2021-09-13 09:30:09 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, extensions_len);
|
2022-05-31 16:24:05 +02:00
|
|
|
extensions_end = p + extensions_len;
|
2021-09-13 09:30:09 +02:00
|
|
|
|
2023-02-23 12:08:09 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_BUF(3, "encrypted extensions", p, extensions_len);
|
|
|
|
|
2022-10-31 06:46:16 +01:00
|
|
|
handshake->received_extensions = MBEDTLS_SSL_EXT_MASK_NONE;
|
2022-08-04 10:55:10 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
while (p < extensions_end) {
|
2021-10-09 12:12:11 +02:00
|
|
|
unsigned int extension_type;
|
|
|
|
size_t extension_data_len;
|
2021-09-13 09:30:09 +02:00
|
|
|
|
2021-10-09 12:12:11 +02:00
|
|
|
/*
|
|
|
|
* struct {
|
2021-10-11 12:05:54 +02:00
|
|
|
* ExtensionType extension_type; (2 bytes)
|
|
|
|
* opaque extension_data<0..2^16-1>;
|
2021-10-09 12:12:11 +02:00
|
|
|
* } Extension;
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
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);
|
2021-10-09 12:12:11 +02:00
|
|
|
p += 4;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_CHK_BUF_READ_PTR(p, extensions_end, extension_data_len);
|
2021-09-13 09:30:09 +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_ENCRYPTED_EXTENSIONS, extension_type,
|
|
|
|
MBEDTLS_SSL_TLS1_3_ALLOWED_EXTS_OF_EE);
|
|
|
|
if (ret != 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
2022-08-04 10:55:10 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
switch (extension_type) {
|
2022-01-21 16:34:27 +01:00
|
|
|
#if defined(MBEDTLS_SSL_ALPN)
|
|
|
|
case MBEDTLS_TLS_EXT_ALPN:
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(3, ("found alpn extension"));
|
2022-01-21 16:34:27 +01:00
|
|
|
|
2023-03-29 12:37:35 +02:00
|
|
|
if ((ret = ssl_tls13_parse_alpn_ext(
|
|
|
|
ssl, p, (size_t) extension_data_len)) != 0) {
|
2023-01-11 14:50:10 +01:00
|
|
|
return ret;
|
2022-01-21 16:34:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
#endif /* MBEDTLS_SSL_ALPN */
|
2022-10-27 12:21:05 +02:00
|
|
|
|
|
|
|
#if defined(MBEDTLS_SSL_EARLY_DATA)
|
|
|
|
case MBEDTLS_TLS_EXT_EARLY_DATA:
|
2022-11-22 11:05:19 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (extension_data_len != 0) {
|
2022-11-22 11:05:19 +01:00
|
|
|
/* The message must be empty. */
|
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;
|
2022-11-22 11:05:19 +01:00
|
|
|
}
|
|
|
|
|
2022-10-27 12:21:05 +02:00
|
|
|
break;
|
|
|
|
#endif /* MBEDTLS_SSL_EARLY_DATA */
|
|
|
|
|
2023-02-10 12:45:19 +01:00
|
|
|
#if defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT)
|
|
|
|
case MBEDTLS_TLS_EXT_RECORD_SIZE_LIMIT:
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(3, ("found record_size_limit extension"));
|
|
|
|
|
2023-03-29 12:37:35 +02:00
|
|
|
ret = mbedtls_ssl_tls13_parse_record_size_limit_ext(
|
|
|
|
ssl, p, p + extension_data_len);
|
2023-02-10 12:45:19 +01:00
|
|
|
|
2023-03-29 12:37:35 +02:00
|
|
|
/* TODO: Return unconditionally here until we handle the record
|
|
|
|
* size limit correctly. Once handled correctly, only return in
|
|
|
|
* case of errors. */
|
2023-02-10 12:45:19 +01:00
|
|
|
return ret;
|
|
|
|
|
|
|
|
break;
|
|
|
|
#endif /* MBEDTLS_SSL_RECORD_SIZE_LIMIT */
|
|
|
|
|
2021-10-09 12:12:11 +02:00
|
|
|
default:
|
2022-11-08 14:30:21 +01:00
|
|
|
MBEDTLS_SSL_PRINT_EXT(
|
2022-10-31 06:46:16 +01:00
|
|
|
3, MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS,
|
2023-01-11 14:50:10 +01:00
|
|
|
extension_type, "( ignored )");
|
2022-08-04 10:55:10 +02:00
|
|
|
break;
|
2021-10-09 12:12:11 +02:00
|
|
|
}
|
2021-09-13 09:30:09 +02:00
|
|
|
|
2021-10-09 12:12:11 +02:00
|
|
|
p += extension_data_len;
|
2021-10-11 12:05:54 +02:00
|
|
|
}
|
2021-09-13 09:30:09 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_PRINT_EXTS(3, MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS,
|
|
|
|
handshake->received_extensions);
|
2022-08-04 10:55:10 +02:00
|
|
|
|
2021-10-11 12:05:54 +02:00
|
|
|
/* Check that we consumed all the message. */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (p != end) {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(1, ("EncryptedExtension lengths misaligned"));
|
|
|
|
MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
|
|
|
|
MBEDTLS_ERR_SSL_DECODE_ERROR);
|
|
|
|
return MBEDTLS_ERR_SSL_DECODE_ERROR;
|
2021-09-13 09:30:09 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return ret;
|
2021-09-13 09:30:09 +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_process_encrypted_extensions(mbedtls_ssl_context *ssl)
|
2022-05-30 16:05:38 +02:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
unsigned char *buf;
|
|
|
|
size_t buf_len;
|
2023-10-31 09:40:01 +01:00
|
|
|
mbedtls_ssl_handshake_params *handshake = ssl->handshake;
|
2022-05-30 16:05:38 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse encrypted extensions"));
|
2022-05-30 16:05:38 +02:00
|
|
|
|
2023-03-29 12:37:35 +02:00
|
|
|
MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_tls13_fetch_handshake_msg(
|
|
|
|
ssl, MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS,
|
|
|
|
&buf, &buf_len));
|
2022-05-30 16:05:38 +02:00
|
|
|
|
|
|
|
/* Process the message contents */
|
|
|
|
MBEDTLS_SSL_PROC_CHK(
|
2023-01-11 14:50:10 +01:00
|
|
|
ssl_tls13_parse_encrypted_extensions(ssl, buf, buf + buf_len));
|
2022-05-30 16:05:38 +02:00
|
|
|
|
2022-11-23 09:12:26 +01:00
|
|
|
#if defined(MBEDTLS_SSL_EARLY_DATA)
|
2023-10-31 09:40:01 +01:00
|
|
|
if (handshake->received_extensions & MBEDTLS_SSL_EXT_MASK(EARLY_DATA)) {
|
|
|
|
/* RFC8446 4.2.11
|
|
|
|
* If the server supplies an "early_data" extension, the
|
|
|
|
* client MUST verify that the server's selected_identity
|
|
|
|
* is 0. If any other value is returned, the client MUST
|
|
|
|
* abort the handshake with an "illegal_parameter" alert.
|
|
|
|
*
|
|
|
|
* RFC 8446 4.2.10
|
|
|
|
* In order to accept early data, the server MUST have accepted a PSK
|
|
|
|
* cipher suite and selected the first key offered in the client's
|
|
|
|
* "pre_shared_key" extension. In addition, it MUST verify that the
|
|
|
|
* following values are the same as those associated with the
|
|
|
|
* selected PSK:
|
|
|
|
* - The TLS version number
|
|
|
|
* - The selected cipher suite
|
|
|
|
* - The selected ALPN [RFC7301] protocol, if any
|
|
|
|
*
|
2023-12-01 10:40:19 +01:00
|
|
|
* The server has sent an early data extension in its Encrypted
|
|
|
|
* Extension message thus accepted to receive early data. We
|
|
|
|
* check here that the additional constraints on the handshake
|
|
|
|
* parameters, when early data are exchanged, are met,
|
|
|
|
* namely:
|
2023-12-01 15:33:59 +01:00
|
|
|
* - a PSK has been selected for the handshake
|
2023-12-01 10:40:19 +01:00
|
|
|
* - the selected PSK for the handshake was the first one proposed
|
|
|
|
* by the client.
|
|
|
|
* - the selected ciphersuite for the handshake is the ciphersuite
|
|
|
|
* associated with the selected PSK.
|
2023-10-31 09:40:01 +01:00
|
|
|
*/
|
2023-12-01 15:33:59 +01:00
|
|
|
if ((!mbedtls_ssl_tls13_key_exchange_mode_with_psk(ssl)) ||
|
|
|
|
handshake->selected_identity != 0 ||
|
2023-10-31 09:40:01 +01:00
|
|
|
handshake->ciphersuite_info->id !=
|
|
|
|
ssl->session_negotiate->ciphersuite) {
|
|
|
|
|
|
|
|
MBEDTLS_SSL_PEND_FATAL_ALERT(
|
|
|
|
MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
|
|
|
|
MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
|
|
|
|
return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
|
|
|
|
}
|
|
|
|
|
2022-11-23 09:12:26 +01:00
|
|
|
ssl->early_data_status = MBEDTLS_SSL_EARLY_DATA_STATUS_ACCEPTED;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2023-11-30 07:06:14 +01:00
|
|
|
/*
|
2023-12-01 10:46:06 +01:00
|
|
|
* In case the client has proposed a PSK associated with a ticket,
|
|
|
|
* `ssl->session_negotiate->ciphersuite` still contains at this point the
|
|
|
|
* identifier of the ciphersuite associated with the ticket. This is that
|
|
|
|
* way because, if an exchange of early data is agreed upon, we need
|
|
|
|
* it to check that the ciphersuite selected for the handshake is the
|
|
|
|
* ticket ciphersuite (see above). This information is not needed
|
|
|
|
* anymore thus we can now set it to the identifier of the ciphersuite
|
|
|
|
* used in this session under negotiation.
|
2023-11-30 07:06:14 +01:00
|
|
|
*/
|
|
|
|
ssl->session_negotiate->ciphersuite = handshake->ciphersuite_info->id;
|
|
|
|
|
2023-03-29 12:37:35 +02:00
|
|
|
MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
|
|
|
|
ssl, MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS,
|
|
|
|
buf, buf_len));
|
2022-05-30 16:05:38 +02:00
|
|
|
|
2022-10-04 16:14:26 +02:00
|
|
|
#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (mbedtls_ssl_tls13_key_exchange_mode_with_psk(ssl)) {
|
|
|
|
mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_FINISHED);
|
|
|
|
} else {
|
|
|
|
mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CERTIFICATE_REQUEST);
|
|
|
|
}
|
2022-05-31 14:49:55 +02:00
|
|
|
#else
|
|
|
|
((void) ssl);
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_FINISHED);
|
2022-05-31 14:49:55 +02:00
|
|
|
#endif
|
2022-05-30 16:05:38 +02:00
|
|
|
|
|
|
|
cleanup:
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse encrypted extensions"));
|
|
|
|
return ret;
|
2022-05-30 16:05:38 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-10-28 08:04:06 +02:00
|
|
|
/*
|
|
|
|
* Handler for MBEDTLS_SSL_END_OF_EARLY_DATA
|
|
|
|
*
|
2022-12-19 03:43:33 +01:00
|
|
|
* RFC 8446 section 4.5
|
2022-10-28 08:04:06 +02:00
|
|
|
*
|
|
|
|
* struct {} EndOfEarlyData;
|
2022-12-19 03:43:33 +01:00
|
|
|
*
|
|
|
|
* If the server sent an "early_data" extension in EncryptedExtensions, the
|
|
|
|
* client MUST send an EndOfEarlyData message after receiving the server
|
|
|
|
* Finished. Otherwise, the client MUST NOT send an EndOfEarlyData message.
|
2022-10-28 08:04:06 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
MBEDTLS_CHECK_RETURN_CRITICAL
|
|
|
|
static int ssl_tls13_write_end_of_early_data(mbedtls_ssl_context *ssl)
|
|
|
|
{
|
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2022-12-19 07:34:44 +01:00
|
|
|
unsigned char *buf = NULL;
|
|
|
|
size_t buf_len;
|
2022-12-19 07:40:47 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(2, ("=> write EndOfEarlyData"));
|
2022-10-28 08:04:06 +02:00
|
|
|
|
2022-12-19 07:34:44 +01:00
|
|
|
MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
|
2023-02-08 07:04:50 +01:00
|
|
|
ssl, MBEDTLS_SSL_HS_END_OF_EARLY_DATA,
|
|
|
|
&buf, &buf_len));
|
2022-10-28 08:04:06 +02:00
|
|
|
|
2023-02-21 15:45:15 +01:00
|
|
|
MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_hdr_to_checksum(
|
|
|
|
ssl, MBEDTLS_SSL_HS_END_OF_EARLY_DATA, 0));
|
2022-10-28 08:04:06 +02:00
|
|
|
|
2022-12-19 07:34:44 +01:00
|
|
|
MBEDTLS_SSL_PROC_CHK(
|
|
|
|
mbedtls_ssl_finish_handshake_msg(ssl, buf_len, 0));
|
2022-12-15 15:55:35 +01:00
|
|
|
|
2023-01-03 04:39:50 +01:00
|
|
|
mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_CERTIFICATE);
|
2022-10-28 08:04:06 +02:00
|
|
|
|
|
|
|
cleanup:
|
|
|
|
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(2, ("<= write EndOfEarlyData"));
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2022-10-04 16:14:26 +02:00
|
|
|
#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
|
2021-10-29 04:08:19 +02:00
|
|
|
/*
|
2022-01-16 13:14:45 +01:00
|
|
|
* STATE HANDLING: CertificateRequest
|
|
|
|
*
|
2021-10-29 04:08:19 +02:00
|
|
|
*/
|
2022-01-20 09:25:00 +01:00
|
|
|
#define SSL_CERTIFICATE_REQUEST_EXPECT_REQUEST 0
|
|
|
|
#define SSL_CERTIFICATE_REQUEST_SKIP 1
|
2022-01-16 13:14:45 +01:00
|
|
|
/* Coordination:
|
|
|
|
* Deals with the ambiguity of not knowing if a CertificateRequest
|
|
|
|
* will be sent. Returns a negative code on failure, or
|
|
|
|
* - SSL_CERTIFICATE_REQUEST_EXPECT_REQUEST
|
|
|
|
* - SSL_CERTIFICATE_REQUEST_SKIP
|
|
|
|
* indicating if a Certificate Request is expected or not.
|
|
|
|
*/
|
2022-06-17 10:52:54 +02:00
|
|
|
MBEDTLS_CHECK_RETURN_CRITICAL
|
2023-01-11 14:50:10 +01:00
|
|
|
static int ssl_tls13_certificate_request_coordinate(mbedtls_ssl_context *ssl)
|
2021-10-29 04:08:19 +02:00
|
|
|
{
|
2022-01-18 06:47:05 +01:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2021-10-29 04:08:19 +02:00
|
|
|
|
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);
|
|
|
|
return ret;
|
2021-10-29 04:08:19 +02:00
|
|
|
}
|
2022-01-16 13:14:45 +01:00
|
|
|
ssl->keep_current_message = 1;
|
2021-10-29 04:08:19 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if ((ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE) &&
|
|
|
|
(ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE_REQUEST)) {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(3, ("got a certificate request"));
|
|
|
|
return SSL_CERTIFICATE_REQUEST_EXPECT_REQUEST;
|
2021-10-29 04:08:19 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(3, ("got no certificate request"));
|
2022-06-15 16:26:13 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return SSL_CERTIFICATE_REQUEST_SKIP;
|
2022-01-16 13:14:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* ssl_tls13_parse_certificate_request()
|
|
|
|
* Parse certificate request
|
|
|
|
* struct {
|
|
|
|
* opaque certificate_request_context<0..2^8-1>;
|
|
|
|
* Extension extensions<2..2^16-1>;
|
|
|
|
* } CertificateRequest;
|
|
|
|
*/
|
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_request(mbedtls_ssl_context *ssl,
|
|
|
|
const unsigned char *buf,
|
|
|
|
const unsigned char *end)
|
2022-01-16 13:14:45 +01:00
|
|
|
{
|
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
|
|
|
const unsigned char *p = buf;
|
|
|
|
size_t certificate_request_context_len = 0;
|
2022-01-26 10:21:54 +01:00
|
|
|
size_t extensions_len = 0;
|
|
|
|
const unsigned char *extensions_end;
|
2022-10-29 03:08:47 +02:00
|
|
|
mbedtls_ssl_handshake_params *handshake = ssl->handshake;
|
2022-01-16 13:14:45 +01:00
|
|
|
|
2022-01-26 10:21:54 +01:00
|
|
|
/* ...
|
|
|
|
* opaque certificate_request_context<0..2^8-1>
|
|
|
|
* ...
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 1);
|
2022-01-16 13:14:45 +01:00
|
|
|
certificate_request_context_len = (size_t) p[0];
|
2022-01-20 09:25:00 +01:00
|
|
|
p += 1;
|
2022-01-16 13:14:45 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (certificate_request_context_len > 0) {
|
|
|
|
MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, certificate_request_context_len);
|
|
|
|
MBEDTLS_SSL_DEBUG_BUF(3, "Certificate Request Context",
|
|
|
|
p, certificate_request_context_len);
|
2022-01-16 13:14:45 +01:00
|
|
|
|
2022-01-20 09:25:00 +01:00
|
|
|
handshake->certificate_request_context =
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_calloc(1, certificate_request_context_len);
|
|
|
|
if (handshake->certificate_request_context == NULL) {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(1, ("buffer too small"));
|
|
|
|
return MBEDTLS_ERR_SSL_ALLOC_FAILED;
|
2022-01-16 13:14:45 +01:00
|
|
|
}
|
2023-01-11 14:50:10 +01:00
|
|
|
memcpy(handshake->certificate_request_context, p,
|
|
|
|
certificate_request_context_len);
|
2022-01-16 13:14:45 +01:00
|
|
|
p += certificate_request_context_len;
|
|
|
|
}
|
|
|
|
|
2022-01-26 10:21:54 +01:00
|
|
|
/* ...
|
|
|
|
* Extension extensions<2..2^16-1>;
|
|
|
|
* ...
|
2022-01-16 13:14:45 +01:00
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
|
|
|
|
extensions_len = MBEDTLS_GET_UINT16_BE(p, 0);
|
2022-01-16 13:14:45 +01:00
|
|
|
p += 2;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, extensions_len);
|
2022-01-16 13:14:45 +01:00
|
|
|
extensions_end = p + extensions_len;
|
|
|
|
|
2022-10-31 07:13:25 +01:00
|
|
|
handshake->received_extensions = MBEDTLS_SSL_EXT_MASK_NONE;
|
2022-08-04 11:01:21 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
while (p < extensions_end) {
|
2022-01-16 13:14:45 +01:00
|
|
|
unsigned int extension_type;
|
|
|
|
size_t extension_data_len;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
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-01-16 13:14:45 +01:00
|
|
|
p += 4;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_CHK_BUF_READ_PTR(p, extensions_end, extension_data_len);
|
2021-10-29 04:08:19 +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_REQUEST, extension_type,
|
|
|
|
MBEDTLS_SSL_TLS1_3_ALLOWED_EXTS_OF_CR);
|
|
|
|
if (ret != 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
2022-08-04 11:01:21 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
switch (extension_type) {
|
2022-01-16 13:14:45 +01:00
|
|
|
case MBEDTLS_TLS_EXT_SIG_ALG:
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(3,
|
|
|
|
("found signature algorithms extension"));
|
|
|
|
ret = mbedtls_ssl_parse_sig_alg_ext(ssl, p,
|
|
|
|
p + extension_data_len);
|
|
|
|
if (ret != 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
2022-08-04 11:01:21 +02:00
|
|
|
|
2022-01-16 13:14:45 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2022-11-08 14:30:21 +01:00
|
|
|
MBEDTLS_SSL_PRINT_EXT(
|
2022-10-31 07:13:25 +01:00
|
|
|
3, MBEDTLS_SSL_HS_CERTIFICATE_REQUEST,
|
2023-01-11 14:50:10 +01:00
|
|
|
extension_type, "( ignored )");
|
2022-01-26 10:21:54 +01:00
|
|
|
break;
|
2022-01-16 13:14:45 +01:00
|
|
|
}
|
2022-08-04 11:01:21 +02:00
|
|
|
|
2022-01-16 13:14:45 +01:00
|
|
|
p += extension_data_len;
|
|
|
|
}
|
2022-08-04 11:01:21 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_PRINT_EXTS(3, MBEDTLS_SSL_HS_CERTIFICATE_REQUEST,
|
|
|
|
handshake->received_extensions);
|
2022-08-04 11:01:21 +02:00
|
|
|
|
2022-01-20 09:25:00 +01:00
|
|
|
/* Check that we consumed all the message. */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (p != end) {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(1,
|
|
|
|
("CertificateRequest misaligned"));
|
2022-02-08 10:59:23 +01:00
|
|
|
goto decode_error;
|
2022-01-20 09:25:00 +01:00
|
|
|
}
|
2022-08-04 11:01:21 +02:00
|
|
|
|
2022-08-29 09:25:36 +02:00
|
|
|
/* RFC 8446 section 4.3.2
|
2022-08-04 11:01:21 +02:00
|
|
|
*
|
|
|
|
* The "signature_algorithms" extension MUST be specified
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
if ((handshake->received_extensions & MBEDTLS_SSL_EXT_MASK(SIG_ALG)) == 0) {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(3,
|
|
|
|
("no signature algorithms extension found"));
|
2022-02-08 10:59:23 +01:00
|
|
|
goto decode_error;
|
2022-01-20 09:25:00 +01:00
|
|
|
}
|
2022-01-16 13:14:45 +01:00
|
|
|
|
2022-01-29 03:26:51 +01:00
|
|
|
ssl->handshake->client_auth = 1;
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2022-02-08 08:28:04 +01:00
|
|
|
|
2022-02-08 10:59:23 +01:00
|
|
|
decode_error:
|
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;
|
2022-01-16 13:14:45 +01:00
|
|
|
}
|
|
|
|
|
2022-01-18 06:47:05 +01:00
|
|
|
/*
|
|
|
|
* Handler for MBEDTLS_SSL_CERTIFICATE_REQUEST
|
|
|
|
*/
|
2022-06-17 10:52:54 +02:00
|
|
|
MBEDTLS_CHECK_RETURN_CRITICAL
|
2023-01-11 14:50:10 +01:00
|
|
|
static int ssl_tls13_process_certificate_request(mbedtls_ssl_context *ssl)
|
2022-01-16 13:14:45 +01:00
|
|
|
{
|
2022-01-18 06:47:05 +01:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2022-01-16 13:14:45 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse certificate request"));
|
2022-01-16 13:14:45 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_PROC_CHK_NEG(ssl_tls13_certificate_request_coordinate(ssl));
|
2022-01-16 13:14:45 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ret == SSL_CERTIFICATE_REQUEST_EXPECT_REQUEST) {
|
2022-01-16 13:14:45 +01:00
|
|
|
unsigned char *buf;
|
|
|
|
size_t buf_len;
|
|
|
|
|
2023-03-29 12:37:35 +02:00
|
|
|
MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_tls13_fetch_handshake_msg(
|
|
|
|
ssl, MBEDTLS_SSL_HS_CERTIFICATE_REQUEST,
|
|
|
|
&buf, &buf_len));
|
2022-01-16 13:14:45 +01:00
|
|
|
|
2023-03-29 12:37:35 +02:00
|
|
|
MBEDTLS_SSL_PROC_CHK(ssl_tls13_parse_certificate_request(
|
|
|
|
ssl, buf, buf + buf_len));
|
2022-01-16 13:14:45 +01:00
|
|
|
|
2023-03-29 12:37:35 +02:00
|
|
|
MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
|
|
|
|
ssl, MBEDTLS_SSL_HS_CERTIFICATE_REQUEST,
|
|
|
|
buf, buf_len));
|
2023-01-11 14:50:10 +01:00
|
|
|
} else if (ret == SSL_CERTIFICATE_REQUEST_SKIP) {
|
2022-01-16 15:54:35 +01:00
|
|
|
ret = 0;
|
2023-01-11 14:50:10 +01:00
|
|
|
} else {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
|
2022-02-08 08:28:04 +01:00
|
|
|
ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
|
|
|
|
goto cleanup;
|
2022-01-16 13:14:45 +01:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_CERTIFICATE);
|
2022-01-18 06:47:05 +01:00
|
|
|
|
2022-01-16 13:14:45 +01:00
|
|
|
cleanup:
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse certificate request"));
|
|
|
|
return ret;
|
2021-10-29 04:08:19 +02:00
|
|
|
}
|
|
|
|
|
2021-09-14 10:03:56 +02:00
|
|
|
/*
|
|
|
|
* Handler for MBEDTLS_SSL_SERVER_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_process_server_certificate(mbedtls_ssl_context *ssl)
|
2021-09-14 10:03:56 +02:00
|
|
|
{
|
2021-09-29 11:12:03 +02:00
|
|
|
int ret;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = mbedtls_ssl_tls13_process_certificate(ssl);
|
|
|
|
if (ret != 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
2021-09-29 11:12:03 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CERTIFICATE_VERIFY);
|
|
|
|
return 0;
|
2021-09-14 10:03:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Handler for MBEDTLS_SSL_CERTIFICATE_VERIFY
|
|
|
|
*/
|
2022-06-17 10:52:54 +02:00
|
|
|
MBEDTLS_CHECK_RETURN_CRITICAL
|
2023-01-11 14:50:10 +01:00
|
|
|
static int ssl_tls13_process_certificate_verify(mbedtls_ssl_context *ssl)
|
2021-09-14 10:03:56 +02:00
|
|
|
{
|
2021-09-12 14:16:03 +02:00
|
|
|
int ret;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = mbedtls_ssl_tls13_process_certificate_verify(ssl);
|
|
|
|
if (ret != 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
2021-09-12 14:16:03 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_FINISHED);
|
|
|
|
return 0;
|
2021-09-14 10:03:56 +02:00
|
|
|
}
|
2022-10-04 16:14:26 +02:00
|
|
|
#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
|
2021-12-06 09:06:46 +01:00
|
|
|
|
2021-09-14 10:03:56 +02:00
|
|
|
/*
|
|
|
|
* Handler for MBEDTLS_SSL_SERVER_FINISHED
|
|
|
|
*/
|
2022-06-17 10:52:54 +02:00
|
|
|
MBEDTLS_CHECK_RETURN_CRITICAL
|
2023-01-11 14:50:10 +01:00
|
|
|
static int ssl_tls13_process_server_finished(mbedtls_ssl_context *ssl)
|
2021-09-14 10:03:56 +02:00
|
|
|
{
|
2021-11-03 07:40:11 +01:00
|
|
|
int ret;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = mbedtls_ssl_tls13_process_finished_message(ssl);
|
|
|
|
if (ret != 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
2021-11-03 07:40:11 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = mbedtls_ssl_tls13_compute_application_transform(ssl);
|
|
|
|
if (ret != 0) {
|
2022-05-19 09:33:10 +02:00
|
|
|
MBEDTLS_SSL_PEND_FATAL_ALERT(
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
|
|
|
|
MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
|
|
|
|
return ret;
|
2022-05-19 09:33:10 +02:00
|
|
|
}
|
|
|
|
|
2022-12-19 07:16:42 +01:00
|
|
|
#if defined(MBEDTLS_SSL_EARLY_DATA)
|
|
|
|
if (ssl->early_data_status == MBEDTLS_SSL_EARLY_DATA_STATUS_ACCEPTED) {
|
|
|
|
mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_END_OF_EARLY_DATA);
|
2023-02-02 06:56:30 +01:00
|
|
|
} else if (ssl->early_data_status == MBEDTLS_SSL_EARLY_DATA_STATUS_REJECTED) {
|
|
|
|
mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_CERTIFICATE);
|
2022-12-19 07:16:42 +01:00
|
|
|
} else
|
|
|
|
#endif /* MBEDTLS_SSL_EARLY_DATA */
|
|
|
|
{
|
|
|
|
#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
|
|
|
|
mbedtls_ssl_handshake_set_state(
|
|
|
|
ssl, MBEDTLS_SSL_CLIENT_CCS_AFTER_SERVER_FINISHED);
|
|
|
|
#else
|
|
|
|
mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_CERTIFICATE);
|
|
|
|
#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
|
|
|
|
}
|
2021-11-24 16:25:31 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2021-09-14 10:03:56 +02:00
|
|
|
}
|
|
|
|
|
2022-01-26 08:41:22 +01:00
|
|
|
/*
|
|
|
|
* Handler for MBEDTLS_SSL_CLIENT_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_client_certificate(mbedtls_ssl_context *ssl)
|
2022-01-26 08:41:22 +01:00
|
|
|
{
|
2022-03-09 07:44:27 +01:00
|
|
|
int non_empty_certificate_msg = 0;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(1,
|
|
|
|
("Switch to handshake traffic keys for outbound traffic"));
|
|
|
|
mbedtls_ssl_set_outbound_transform(ssl, ssl->handshake->transform_handshake);
|
2022-01-28 09:16:08 +01:00
|
|
|
|
2022-10-04 16:14:26 +02:00
|
|
|
#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->handshake->client_auth) {
|
|
|
|
int ret = mbedtls_ssl_tls13_write_certificate(ssl);
|
|
|
|
if (ret != 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
2022-03-09 07:00:13 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (mbedtls_ssl_own_cert(ssl) != NULL) {
|
2022-03-09 07:44:27 +01:00
|
|
|
non_empty_certificate_msg = 1;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(2, ("skip write certificate"));
|
2022-03-09 07:44:27 +01:00
|
|
|
}
|
2022-03-08 18:38:54 +01:00
|
|
|
#endif
|
2022-03-09 07:00:13 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (non_empty_certificate_msg) {
|
|
|
|
mbedtls_ssl_handshake_set_state(ssl,
|
|
|
|
MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY);
|
|
|
|
} else {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(2, ("skip write certificate verify"));
|
|
|
|
mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_FINISHED);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2022-01-26 08:41:22 +01:00
|
|
|
}
|
|
|
|
|
2022-10-04 16:14:26 +02:00
|
|
|
#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
|
2022-01-26 08:41:22 +01:00
|
|
|
/*
|
|
|
|
* Handler for MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY
|
|
|
|
*/
|
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_client_certificate_verify(mbedtls_ssl_context *ssl)
|
2022-01-26 08:41:22 +01:00
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
int ret = mbedtls_ssl_tls13_write_certificate_verify(ssl);
|
2022-03-09 07:59:25 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ret == 0) {
|
|
|
|
mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_FINISHED);
|
|
|
|
}
|
2022-03-09 07:59:25 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return ret;
|
2022-01-26 08:41:22 +01:00
|
|
|
}
|
2022-10-04 16:14:26 +02:00
|
|
|
#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
|
2022-01-26 08:41:22 +01:00
|
|
|
|
2021-09-14 10:03:56 +02:00
|
|
|
/*
|
|
|
|
* Handler for MBEDTLS_SSL_CLIENT_FINISHED
|
|
|
|
*/
|
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_client_finished(mbedtls_ssl_context *ssl)
|
2021-09-14 10:03:56 +02:00
|
|
|
{
|
2021-11-15 04:33:57 +01:00
|
|
|
int ret;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = mbedtls_ssl_tls13_write_finished_message(ssl);
|
|
|
|
if (ret != 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
2021-11-15 04:33:57 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = mbedtls_ssl_tls13_compute_resumption_master_secret(ssl);
|
|
|
|
if (ret != 0) {
|
2023-03-29 12:37:35 +02:00
|
|
|
MBEDTLS_SSL_DEBUG_RET(
|
|
|
|
1, "mbedtls_ssl_tls13_compute_resumption_master_secret ", ret);
|
2023-01-11 14:50:10 +01:00
|
|
|
return ret;
|
2022-05-19 09:33:10 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_FLUSH_BUFFERS);
|
|
|
|
return 0;
|
2021-09-14 10:03:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Handler for MBEDTLS_SSL_FLUSH_BUFFERS
|
|
|
|
*/
|
2022-06-17 10:52:54 +02:00
|
|
|
MBEDTLS_CHECK_RETURN_CRITICAL
|
2023-01-11 14:50:10 +01:00
|
|
|
static int ssl_tls13_flush_buffers(mbedtls_ssl_context *ssl)
|
2021-09-14 10:03:56 +02:00
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(2, ("handshake: done"));
|
|
|
|
mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HANDSHAKE_WRAPUP);
|
|
|
|
return 0;
|
2021-09-14 10:03:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Handler for MBEDTLS_SSL_HANDSHAKE_WRAPUP
|
|
|
|
*/
|
2022-06-17 10:52:54 +02:00
|
|
|
MBEDTLS_CHECK_RETURN_CRITICAL
|
2023-01-11 14:50:10 +01:00
|
|
|
static int ssl_tls13_handshake_wrapup(mbedtls_ssl_context *ssl)
|
2021-09-14 10:03:56 +02:00
|
|
|
{
|
2021-10-30 15:44:47 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_ssl_tls13_handshake_wrapup(ssl);
|
2021-10-30 15:44:47 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HANDSHAKE_OVER);
|
|
|
|
return 0;
|
2021-09-14 10:03:56 +02:00
|
|
|
}
|
|
|
|
|
2022-07-07 13:32:32 +02:00
|
|
|
#if defined(MBEDTLS_SSL_SESSION_TICKETS)
|
|
|
|
|
2023-11-14 10:20:16 +01:00
|
|
|
#if defined(MBEDTLS_SSL_EARLY_DATA)
|
|
|
|
/* From RFC 8446 section 4.2.10
|
|
|
|
*
|
|
|
|
* struct {
|
|
|
|
* select (Handshake.msg_type) {
|
|
|
|
* case new_session_ticket: uint32 max_early_data_size;
|
|
|
|
* ...
|
|
|
|
* };
|
|
|
|
* } EarlyDataIndication;
|
|
|
|
*/
|
|
|
|
MBEDTLS_CHECK_RETURN_CRITICAL
|
2023-11-30 09:49:49 +01:00
|
|
|
static int ssl_tls13_parse_new_session_ticket_early_data_ext(
|
|
|
|
mbedtls_ssl_context *ssl,
|
|
|
|
const unsigned char *buf,
|
|
|
|
const unsigned char *end)
|
2023-11-14 10:20:16 +01:00
|
|
|
{
|
2023-11-23 09:35:54 +01:00
|
|
|
mbedtls_ssl_session *session = ssl->session;
|
|
|
|
|
2023-11-14 10:20:16 +01:00
|
|
|
MBEDTLS_SSL_CHK_BUF_READ_PTR(buf, end, 4);
|
|
|
|
|
2023-11-23 09:35:54 +01:00
|
|
|
session->max_early_data_size = MBEDTLS_GET_UINT32_BE(buf, 0);
|
|
|
|
mbedtls_ssl_session_set_ticket_flags(
|
|
|
|
session, MBEDTLS_SSL_TLS1_3_TICKET_ALLOW_EARLY_DATA);
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(
|
|
|
|
3, ("received max_early_data_size: %u",
|
|
|
|
(unsigned int) session->max_early_data_size));
|
2023-11-14 10:20:16 +01:00
|
|
|
|
2023-11-23 09:35:54 +01:00
|
|
|
return 0;
|
2023-11-14 10:20:16 +01:00
|
|
|
}
|
|
|
|
#endif /* MBEDTLS_SSL_EARLY_DATA */
|
|
|
|
|
2022-07-13 05:22:55 +02:00
|
|
|
MBEDTLS_CHECK_RETURN_CRITICAL
|
2023-01-11 14:50:10 +01:00
|
|
|
static int ssl_tls13_parse_new_session_ticket_exts(mbedtls_ssl_context *ssl,
|
|
|
|
const unsigned char *buf,
|
|
|
|
const unsigned char *end)
|
2022-07-07 13:32:32 +02:00
|
|
|
{
|
2022-10-29 03:08:47 +02:00
|
|
|
mbedtls_ssl_handshake_params *handshake = ssl->handshake;
|
2022-07-12 07:47:21 +02:00
|
|
|
const unsigned char *p = buf;
|
2022-07-07 13:32:32 +02:00
|
|
|
|
|
|
|
|
2022-10-31 07:37:31 +01:00
|
|
|
handshake->received_extensions = MBEDTLS_SSL_EXT_MASK_NONE;
|
2022-08-04 11:53:25 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
while (p < end) {
|
2022-07-07 13:32:32 +02:00
|
|
|
unsigned int extension_type;
|
|
|
|
size_t extension_data_len;
|
2022-08-29 09:25:36 +02:00
|
|
|
int ret;
|
2022-07-07 13:32:32 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 4);
|
|
|
|
extension_type = MBEDTLS_GET_UINT16_BE(p, 0);
|
|
|
|
extension_data_len = MBEDTLS_GET_UINT16_BE(p, 2);
|
2022-07-07 13:32:32 +02:00
|
|
|
p += 4;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, extension_data_len);
|
2022-07-07 13:32:32 +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_NEW_SESSION_TICKET, extension_type,
|
|
|
|
MBEDTLS_SSL_TLS1_3_ALLOWED_EXTS_OF_NST);
|
|
|
|
if (ret != 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
2022-08-04 11:53:25 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
switch (extension_type) {
|
2022-11-16 09:43:50 +01:00
|
|
|
#if defined(MBEDTLS_SSL_EARLY_DATA)
|
2022-11-08 08:02:27 +01:00
|
|
|
case MBEDTLS_TLS_EXT_EARLY_DATA:
|
2023-11-30 09:49:49 +01:00
|
|
|
ret = ssl_tls13_parse_new_session_ticket_early_data_ext(
|
2023-11-14 10:20:16 +01:00
|
|
|
ssl, p, p + extension_data_len);
|
|
|
|
if (ret != 0) {
|
|
|
|
MBEDTLS_SSL_DEBUG_RET(
|
2023-11-30 09:49:49 +01:00
|
|
|
1, "ssl_tls13_parse_new_session_ticket_early_data_ext",
|
|
|
|
ret);
|
2022-11-09 08:55:48 +01:00
|
|
|
}
|
2022-11-08 08:02:27 +01:00
|
|
|
break;
|
2022-11-16 09:43:50 +01:00
|
|
|
#endif /* MBEDTLS_SSL_EARLY_DATA */
|
2022-11-08 08:02:27 +01:00
|
|
|
|
2022-07-07 13:32:32 +02:00
|
|
|
default:
|
2022-11-08 14:30:21 +01:00
|
|
|
MBEDTLS_SSL_PRINT_EXT(
|
2022-10-31 07:37:31 +01:00
|
|
|
3, MBEDTLS_SSL_HS_NEW_SESSION_TICKET,
|
2023-01-11 14:50:10 +01:00
|
|
|
extension_type, "( ignored )");
|
2022-07-07 13:32:32 +02:00
|
|
|
break;
|
|
|
|
}
|
2022-08-04 11:53:25 +02:00
|
|
|
|
2022-07-07 13:32:32 +02:00
|
|
|
p += extension_data_len;
|
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_PRINT_EXTS(3, MBEDTLS_SSL_HS_NEW_SESSION_TICKET,
|
|
|
|
handshake->received_extensions);
|
2022-08-04 11:53:25 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2022-07-07 13:32:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2022-07-20 04:36:12 +02:00
|
|
|
* From RFC8446, page 74
|
|
|
|
*
|
2022-07-07 13:32:32 +02:00
|
|
|
* struct {
|
|
|
|
* uint32 ticket_lifetime;
|
|
|
|
* uint32 ticket_age_add;
|
|
|
|
* opaque ticket_nonce<0..255>;
|
|
|
|
* opaque ticket<1..2^16-1>;
|
|
|
|
* Extension extensions<0..2^16-2>;
|
|
|
|
* } NewSessionTicket;
|
|
|
|
*
|
|
|
|
*/
|
2022-07-13 05:22:55 +02:00
|
|
|
MBEDTLS_CHECK_RETURN_CRITICAL
|
2023-01-11 14:50:10 +01:00
|
|
|
static int ssl_tls13_parse_new_session_ticket(mbedtls_ssl_context *ssl,
|
|
|
|
unsigned char *buf,
|
|
|
|
unsigned char *end,
|
|
|
|
unsigned char **ticket_nonce,
|
|
|
|
size_t *ticket_nonce_len)
|
2022-07-07 13:32:32 +02:00
|
|
|
{
|
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
|
|
|
unsigned char *p = buf;
|
|
|
|
mbedtls_ssl_session *session = ssl->session;
|
|
|
|
size_t ticket_len;
|
|
|
|
unsigned char *ticket;
|
|
|
|
size_t extensions_len;
|
|
|
|
|
2022-07-12 08:09:38 +02:00
|
|
|
*ticket_nonce = NULL;
|
|
|
|
*ticket_nonce_len = 0;
|
2022-07-07 13:32:32 +02:00
|
|
|
/*
|
|
|
|
* ticket_lifetime 4 bytes
|
|
|
|
* ticket_age_add 4 bytes
|
2022-07-20 04:36:12 +02:00
|
|
|
* ticket_nonce_len 1 byte
|
2022-07-07 13:32:32 +02:00
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 9);
|
2022-07-07 13:32:32 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
session->ticket_lifetime = MBEDTLS_GET_UINT32_BE(p, 0);
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(3,
|
|
|
|
("ticket_lifetime: %u",
|
|
|
|
(unsigned int) session->ticket_lifetime));
|
2023-11-10 06:58:16 +01:00
|
|
|
if (session->ticket_lifetime >
|
|
|
|
MBEDTLS_SSL_TLS1_3_MAX_ALLOWED_TICKET_LIFETIME) {
|
2023-11-10 06:58:16 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(3, ("ticket_lifetime exceeds 7 days."));
|
2023-11-14 04:06:52 +01:00
|
|
|
return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
|
2023-11-10 06:58:16 +01:00
|
|
|
}
|
2022-07-07 13:32:32 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
session->ticket_age_add = MBEDTLS_GET_UINT32_BE(p, 4);
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(3,
|
|
|
|
("ticket_age_add: %u",
|
|
|
|
(unsigned int) session->ticket_age_add));
|
2022-07-07 13:32:32 +02:00
|
|
|
|
2022-07-12 08:09:38 +02:00
|
|
|
*ticket_nonce_len = p[8];
|
|
|
|
p += 9;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, *ticket_nonce_len);
|
2022-07-12 08:09:38 +02:00
|
|
|
*ticket_nonce = p;
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_BUF(3, "ticket_nonce:", *ticket_nonce, *ticket_nonce_len);
|
2022-07-12 08:09:38 +02:00
|
|
|
p += *ticket_nonce_len;
|
2022-07-07 13:32:32 +02:00
|
|
|
|
|
|
|
/* Ticket */
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
|
|
|
|
ticket_len = MBEDTLS_GET_UINT16_BE(p, 0);
|
2022-07-07 13:32:32 +02:00
|
|
|
p += 2;
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, ticket_len);
|
|
|
|
MBEDTLS_SSL_DEBUG_BUF(3, "received ticket", p, ticket_len);
|
2022-07-07 13:32:32 +02:00
|
|
|
|
|
|
|
/* Check if we previously received a ticket already. */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (session->ticket != NULL || session->ticket_len > 0) {
|
|
|
|
mbedtls_free(session->ticket);
|
2022-07-07 13:32:32 +02:00
|
|
|
session->ticket = NULL;
|
|
|
|
session->ticket_len = 0;
|
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if ((ticket = mbedtls_calloc(1, ticket_len)) == NULL) {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(1, ("ticket alloc failed"));
|
|
|
|
return MBEDTLS_ERR_SSL_ALLOC_FAILED;
|
2022-07-07 13:32:32 +02:00
|
|
|
}
|
2023-01-11 14:50:10 +01:00
|
|
|
memcpy(ticket, p, ticket_len);
|
2022-07-07 13:32:32 +02:00
|
|
|
p += ticket_len;
|
|
|
|
session->ticket = ticket;
|
|
|
|
session->ticket_len = ticket_len;
|
|
|
|
|
2022-11-17 08:22:33 +01:00
|
|
|
/* Clear all flags in ticket_flags */
|
2023-01-12 04:54:04 +01:00
|
|
|
mbedtls_ssl_session_clear_ticket_flags(
|
2022-12-09 07:39:19 +01:00
|
|
|
session, MBEDTLS_SSL_TLS1_3_TICKET_FLAGS_MASK);
|
2022-11-17 08:22:33 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
|
|
|
|
extensions_len = MBEDTLS_GET_UINT16_BE(p, 0);
|
2022-07-07 13:32:32 +02:00
|
|
|
p += 2;
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, extensions_len);
|
2022-07-07 13:32:32 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_BUF(3, "ticket extension", p, extensions_len);
|
2022-07-07 13:32:32 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = ssl_tls13_parse_new_session_ticket_exts(ssl, p, p + extensions_len);
|
|
|
|
if (ret != 0) {
|
|
|
|
MBEDTLS_SSL_DEBUG_RET(1,
|
|
|
|
"ssl_tls13_parse_new_session_ticket_exts",
|
|
|
|
ret);
|
|
|
|
return ret;
|
2022-07-07 13:32:32 +02:00
|
|
|
}
|
2022-07-12 08:09:38 +02:00
|
|
|
|
2022-08-03 06:10:13 +02:00
|
|
|
/* session has been updated, allow export */
|
|
|
|
session->exported = 0;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2022-07-12 08:09:38 +02:00
|
|
|
}
|
|
|
|
|
2022-07-13 05:22:55 +02:00
|
|
|
MBEDTLS_CHECK_RETURN_CRITICAL
|
2023-01-11 14:50:10 +01:00
|
|
|
static int ssl_tls13_postprocess_new_session_ticket(mbedtls_ssl_context *ssl,
|
|
|
|
unsigned char *ticket_nonce,
|
|
|
|
size_t ticket_nonce_len)
|
2022-07-12 08:09:38 +02:00
|
|
|
{
|
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
|
|
|
mbedtls_ssl_session *session = ssl->session;
|
|
|
|
const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
|
|
|
|
psa_algorithm_t psa_hash_alg;
|
|
|
|
int hash_length;
|
|
|
|
|
2022-07-13 05:16:51 +02:00
|
|
|
#if defined(MBEDTLS_HAVE_TIME)
|
|
|
|
/* Store ticket creation time */
|
2023-11-10 07:23:39 +01:00
|
|
|
session->ticket_reception_time = mbedtls_ms_time();
|
2022-07-13 05:16:51 +02:00
|
|
|
#endif
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
ciphersuite_info = mbedtls_ssl_ciphersuite_from_id(session->ciphersuite);
|
|
|
|
if (ciphersuite_info == NULL) {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
|
|
|
|
return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
|
2022-07-07 13:32:32 +02:00
|
|
|
}
|
|
|
|
|
2023-10-05 14:30:37 +02:00
|
|
|
psa_hash_alg = mbedtls_md_psa_alg_from_type((mbedtls_md_type_t) ciphersuite_info->mac);
|
2023-01-11 14:50:10 +01:00
|
|
|
hash_length = PSA_HASH_LENGTH(psa_hash_alg);
|
|
|
|
if (hash_length == -1 ||
|
|
|
|
(size_t) hash_length > sizeof(session->resumption_key)) {
|
|
|
|
return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
|
2022-07-20 11:34:14 +02:00
|
|
|
}
|
|
|
|
|
2022-07-07 13:32:32 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_BUF(3, "resumption_master_secret",
|
|
|
|
session->app_secrets.resumption_master_secret,
|
|
|
|
hash_length);
|
2022-07-07 13:32:32 +02:00
|
|
|
|
2022-07-13 05:16:51 +02:00
|
|
|
/* Compute resumption key
|
2022-07-07 13:32:32 +02:00
|
|
|
*
|
|
|
|
* HKDF-Expand-Label( resumption_master_secret,
|
|
|
|
* "resumption", ticket_nonce, Hash.length )
|
|
|
|
*/
|
|
|
|
ret = mbedtls_ssl_tls13_hkdf_expand_label(
|
2023-01-11 14:50:10 +01:00
|
|
|
psa_hash_alg,
|
|
|
|
session->app_secrets.resumption_master_secret,
|
|
|
|
hash_length,
|
|
|
|
MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(resumption),
|
|
|
|
ticket_nonce,
|
|
|
|
ticket_nonce_len,
|
|
|
|
session->resumption_key,
|
|
|
|
hash_length);
|
|
|
|
|
|
|
|
if (ret != 0) {
|
|
|
|
MBEDTLS_SSL_DEBUG_RET(2,
|
|
|
|
"Creating the ticket-resumed PSK failed",
|
|
|
|
ret);
|
|
|
|
return ret;
|
2022-07-07 13:32:32 +02:00
|
|
|
}
|
|
|
|
|
2022-07-20 05:02:48 +02:00
|
|
|
session->resumption_key_len = hash_length;
|
2022-07-07 13:32:32 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_BUF(3, "Ticket-resumed PSK",
|
|
|
|
session->resumption_key,
|
|
|
|
session->resumption_key_len);
|
2022-07-07 13:32:32 +02:00
|
|
|
|
2022-11-17 08:22:33 +01:00
|
|
|
/* Set ticket_flags depends on the selected key exchange modes */
|
2023-01-12 04:54:04 +01:00
|
|
|
mbedtls_ssl_session_set_ticket_flags(
|
2022-12-09 07:39:19 +01:00
|
|
|
session, ssl->conf->tls13_kex_modes);
|
2023-01-16 04:28:49 +01:00
|
|
|
MBEDTLS_SSL_PRINT_TICKET_FLAGS(4, session->ticket_flags);
|
2022-11-17 08:22:33 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2022-07-07 13:32:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2022-10-30 07:51:23 +01:00
|
|
|
* Handler for MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET
|
2022-07-07 13:32:32 +02:00
|
|
|
*/
|
2022-07-13 05:22:55 +02:00
|
|
|
MBEDTLS_CHECK_RETURN_CRITICAL
|
2023-01-11 14:50:10 +01:00
|
|
|
static int ssl_tls13_process_new_session_ticket(mbedtls_ssl_context *ssl)
|
2022-07-07 13:32:32 +02:00
|
|
|
{
|
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
|
|
|
unsigned char *buf;
|
|
|
|
size_t buf_len;
|
2022-07-12 08:09:38 +02:00
|
|
|
unsigned char *ticket_nonce;
|
|
|
|
size_t ticket_nonce_len;
|
2022-07-07 13:32:32 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse new session ticket"));
|
2022-07-07 13:32:32 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_tls13_fetch_handshake_msg(
|
|
|
|
ssl, MBEDTLS_SSL_HS_NEW_SESSION_TICKET,
|
|
|
|
&buf, &buf_len));
|
2022-07-07 13:32:32 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_PROC_CHK(ssl_tls13_parse_new_session_ticket(
|
|
|
|
ssl, buf, buf + buf_len,
|
|
|
|
&ticket_nonce, &ticket_nonce_len));
|
2022-07-07 13:32:32 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_PROC_CHK(ssl_tls13_postprocess_new_session_ticket(
|
|
|
|
ssl, ticket_nonce, ticket_nonce_len));
|
2022-07-07 13:32:32 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HANDSHAKE_OVER);
|
2022-07-13 05:16:51 +02:00
|
|
|
|
2022-07-07 13:32:32 +02:00
|
|
|
cleanup:
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse new session ticket"));
|
|
|
|
return ret;
|
2022-07-07 13:32:32 +02:00
|
|
|
}
|
|
|
|
#endif /* MBEDTLS_SSL_SESSION_TICKETS */
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_ssl_tls13_handshake_client_step(mbedtls_ssl_context *ssl)
|
2021-08-24 09:59:48 +02:00
|
|
|
{
|
2021-08-27 10:59:09 +02:00
|
|
|
int ret = 0;
|
2021-08-18 10:46:28 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
switch (ssl->state) {
|
2021-08-27 10:59:09 +02:00
|
|
|
case MBEDTLS_SSL_HELLO_REQUEST:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_HELLO);
|
2022-12-01 12:43:12 +01:00
|
|
|
break;
|
|
|
|
|
2021-08-27 10:59:09 +02:00
|
|
|
case MBEDTLS_SSL_CLIENT_HELLO:
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = mbedtls_ssl_write_client_hello(ssl);
|
2021-08-27 10:59:09 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case MBEDTLS_SSL_SERVER_HELLO:
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = ssl_tls13_process_server_hello(ssl);
|
2021-09-14 10:03:56 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case MBEDTLS_SSL_ENCRYPTED_EXTENSIONS:
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = ssl_tls13_process_encrypted_extensions(ssl);
|
2021-09-14 10:03:56 +02:00
|
|
|
break;
|
|
|
|
|
2022-10-04 16:14:26 +02:00
|
|
|
#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
|
2021-10-29 04:08:19 +02:00
|
|
|
case MBEDTLS_SSL_CERTIFICATE_REQUEST:
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = ssl_tls13_process_certificate_request(ssl);
|
2021-10-29 04:08:19 +02:00
|
|
|
break;
|
|
|
|
|
2021-09-14 10:03:56 +02:00
|
|
|
case MBEDTLS_SSL_SERVER_CERTIFICATE:
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = ssl_tls13_process_server_certificate(ssl);
|
2021-09-14 10:03:56 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case MBEDTLS_SSL_CERTIFICATE_VERIFY:
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = ssl_tls13_process_certificate_verify(ssl);
|
2021-09-14 10:03:56 +02:00
|
|
|
break;
|
2022-10-04 16:14:26 +02:00
|
|
|
#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
|
2021-09-14 10:03:56 +02:00
|
|
|
|
|
|
|
case MBEDTLS_SSL_SERVER_FINISHED:
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = ssl_tls13_process_server_finished(ssl);
|
2021-09-14 10:03:56 +02:00
|
|
|
break;
|
|
|
|
|
2022-10-28 08:04:06 +02:00
|
|
|
case MBEDTLS_SSL_END_OF_EARLY_DATA:
|
|
|
|
ret = ssl_tls13_write_end_of_early_data(ssl);
|
|
|
|
break;
|
|
|
|
|
2022-01-26 08:41:22 +01:00
|
|
|
case MBEDTLS_SSL_CLIENT_CERTIFICATE:
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = ssl_tls13_write_client_certificate(ssl);
|
2022-01-26 08:41:22 +01:00
|
|
|
break;
|
|
|
|
|
2022-10-04 16:14:26 +02:00
|
|
|
#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
|
2022-01-26 08:41:22 +01:00
|
|
|
case MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY:
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = ssl_tls13_write_client_certificate_verify(ssl);
|
2022-01-26 08:41:22 +01:00
|
|
|
break;
|
2022-10-04 16:14:26 +02:00
|
|
|
#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
|
2022-01-26 08:41:22 +01:00
|
|
|
|
2021-09-14 10:03:56 +02:00
|
|
|
case MBEDTLS_SSL_CLIENT_FINISHED:
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = ssl_tls13_write_client_finished(ssl);
|
2021-09-14 10:03:56 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case MBEDTLS_SSL_FLUSH_BUFFERS:
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = ssl_tls13_flush_buffers(ssl);
|
2021-09-14 10:03:56 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case MBEDTLS_SSL_HANDSHAKE_WRAPUP:
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = ssl_tls13_handshake_wrapup(ssl);
|
2021-08-27 10:59:09 +02:00
|
|
|
break;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
/*
|
|
|
|
* Injection of dummy-CCS's for middlebox compatibility
|
|
|
|
*/
|
2021-11-24 16:25:31 +01:00
|
|
|
#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
|
2021-12-22 03:39:32 +01:00
|
|
|
case MBEDTLS_SSL_CLIENT_CCS_BEFORE_2ND_CLIENT_HELLO:
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = mbedtls_ssl_tls13_write_change_cipher_spec(ssl);
|
|
|
|
if (ret == 0) {
|
|
|
|
mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_HELLO);
|
|
|
|
}
|
2022-02-02 16:02:47 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case MBEDTLS_SSL_CLIENT_CCS_AFTER_SERVER_FINISHED:
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = mbedtls_ssl_tls13_write_change_cipher_spec(ssl);
|
|
|
|
if (ret == 0) {
|
2023-03-29 12:37:35 +02:00
|
|
|
mbedtls_ssl_handshake_set_state(
|
|
|
|
ssl, MBEDTLS_SSL_CLIENT_CERTIFICATE);
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2021-11-24 16:25:31 +01:00
|
|
|
break;
|
2023-02-02 03:46:26 +01:00
|
|
|
|
2023-01-04 11:47:05 +01:00
|
|
|
case MBEDTLS_SSL_CLIENT_CCS_AFTER_CLIENT_HELLO:
|
|
|
|
ret = mbedtls_ssl_tls13_write_change_cipher_spec(ssl);
|
|
|
|
if (ret == 0) {
|
|
|
|
mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_HELLO);
|
|
|
|
|
2023-01-10 07:32:12 +01:00
|
|
|
#if defined(MBEDTLS_SSL_EARLY_DATA)
|
2023-01-04 11:47:05 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(
|
|
|
|
1, ("Switch to early data keys for outbound traffic"));
|
|
|
|
mbedtls_ssl_set_outbound_transform(
|
|
|
|
ssl, ssl->handshake->transform_earlydata);
|
2023-01-10 07:32:12 +01:00
|
|
|
#endif
|
2023-01-04 11:47:05 +01:00
|
|
|
}
|
|
|
|
break;
|
2021-11-24 16:25:31 +01:00
|
|
|
#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
|
|
|
|
|
2022-07-07 13:32:32 +02:00
|
|
|
#if defined(MBEDTLS_SSL_SESSION_TICKETS)
|
2022-10-30 07:51:23 +01:00
|
|
|
case MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET:
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = ssl_tls13_process_new_session_ticket(ssl);
|
|
|
|
if (ret != 0) {
|
2022-07-07 13:32:32 +02:00
|
|
|
break;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2022-07-07 13:32:32 +02:00
|
|
|
ret = MBEDTLS_ERR_SSL_RECEIVED_NEW_SESSION_TICKET;
|
|
|
|
break;
|
|
|
|
#endif /* MBEDTLS_SSL_SESSION_TICKETS */
|
|
|
|
|
2021-08-27 10:59:09 +02:00
|
|
|
default:
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(1, ("invalid state %d", ssl->state));
|
|
|
|
return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
|
2021-08-27 10:59:09 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return ret;
|
2021-08-27 10:59:09 +02:00
|
|
|
}
|
2021-08-18 10:38:40 +02:00
|
|
|
|
2022-01-27 08:03:26 +01:00
|
|
|
#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_PROTO_TLS1_3 */
|