2021-08-06 10:29:08 +02:00
|
|
|
/*
|
2021-08-06 11:11:51 +02:00
|
|
|
* TLS 1.3 server-side functions
|
2021-08-06 10:29:08 +02:00
|
|
|
*
|
|
|
|
* Copyright The Mbed TLS Contributors
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); you may
|
|
|
|
* not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
2023-01-11 14:50:10 +01:00
|
|
|
*/
|
2021-08-06 10:29:08 +02:00
|
|
|
|
|
|
|
#include "common.h"
|
|
|
|
|
2022-01-27 08:03:26 +01:00
|
|
|
#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
|
2021-08-06 10:29:08 +02:00
|
|
|
|
2021-09-14 10:03:56 +02:00
|
|
|
#include "mbedtls/debug.h"
|
2022-02-15 11:00:56 +01:00
|
|
|
#include "mbedtls/error.h"
|
|
|
|
#include "mbedtls/platform.h"
|
2022-07-10 08:32:38 +02:00
|
|
|
#include "mbedtls/constant_time.h"
|
2023-06-20 10:14:58 +02:00
|
|
|
#include "mbedtls/oid.h"
|
2023-03-28 12:33:20 +02:00
|
|
|
#include "md_psa.h"
|
2022-03-30 16:02:12 +02:00
|
|
|
|
2022-02-14 13:57:18 +01:00
|
|
|
#include "ssl_misc.h"
|
|
|
|
#include "ssl_tls13_keys.h"
|
|
|
|
#include "ssl_debug_helpers.h"
|
|
|
|
|
2022-08-23 11:58:26 +02:00
|
|
|
|
2022-08-25 04:51:44 +02:00
|
|
|
static const mbedtls_ssl_ciphersuite_t *ssl_tls13_validate_peer_ciphersuite(
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_ssl_context *ssl,
|
|
|
|
unsigned int cipher_suite)
|
2022-08-23 11:58:26 +02:00
|
|
|
{
|
|
|
|
const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
|
2023-01-11 14:50:10 +01:00
|
|
|
if (!mbedtls_ssl_tls13_cipher_suite_is_offered(ssl, cipher_suite)) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2022-08-23 11:58:26 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
ciphersuite_info = mbedtls_ssl_ciphersuite_from_id(cipher_suite);
|
|
|
|
if ((mbedtls_ssl_validate_ciphersuite(ssl, ciphersuite_info,
|
|
|
|
ssl->tls_version,
|
|
|
|
ssl->tls_version) != 0)) {
|
|
|
|
return NULL;
|
2022-08-23 11:58:26 +02:00
|
|
|
}
|
2023-01-11 14:50:10 +01:00
|
|
|
return ciphersuite_info;
|
2022-08-23 11:58:26 +02:00
|
|
|
}
|
|
|
|
|
2022-10-04 16:38:25 +02:00
|
|
|
#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
|
2022-07-08 14:04:51 +02:00
|
|
|
/* From RFC 8446:
|
|
|
|
*
|
|
|
|
* enum { psk_ke(0), psk_dhe_ke(1), (255) } PskKeyExchangeMode;
|
|
|
|
* struct {
|
|
|
|
* PskKeyExchangeMode ke_modes<1..255>;
|
|
|
|
* } PskKeyExchangeModes;
|
|
|
|
*/
|
2022-07-20 14:49:32 +02:00
|
|
|
MBEDTLS_CHECK_RETURN_CRITICAL
|
2023-01-11 14:50:10 +01:00
|
|
|
static int ssl_tls13_parse_key_exchange_modes_ext(mbedtls_ssl_context *ssl,
|
|
|
|
const unsigned char *buf,
|
|
|
|
const unsigned char *end)
|
2022-07-08 14:04:51 +02:00
|
|
|
{
|
2022-07-13 17:06:36 +02:00
|
|
|
const unsigned char *p = buf;
|
2022-07-08 14:04:51 +02:00
|
|
|
size_t ke_modes_len;
|
|
|
|
int ke_modes = 0;
|
|
|
|
|
2022-07-15 08:28:27 +02:00
|
|
|
/* Read ke_modes length (1 Byte) */
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 1);
|
2022-07-13 17:06:36 +02:00
|
|
|
ke_modes_len = *p++;
|
2022-07-08 14:04:51 +02:00
|
|
|
/* Currently, there are only two PSK modes, so even without looking
|
|
|
|
* at the content, something's wrong if the list has more than 2 items. */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ke_modes_len > 2) {
|
|
|
|
MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
|
|
|
|
MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
|
|
|
|
return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
|
2022-07-13 17:06:36 +02:00
|
|
|
}
|
2022-07-08 14:04:51 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, ke_modes_len);
|
2022-07-08 14:04:51 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
while (ke_modes_len-- != 0) {
|
|
|
|
switch (*p++) {
|
|
|
|
case MBEDTLS_SSL_TLS1_3_PSK_MODE_PURE:
|
|
|
|
ke_modes |= MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK;
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(3, ("Found PSK KEX MODE"));
|
|
|
|
break;
|
|
|
|
case MBEDTLS_SSL_TLS1_3_PSK_MODE_ECDHE:
|
|
|
|
ke_modes |= MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL;
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(3, ("Found PSK_EPHEMERAL KEX MODE"));
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
|
|
|
|
MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
|
|
|
|
return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
|
2022-07-08 14:04:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ssl->handshake->tls13_kex_modes = ke_modes;
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2022-07-08 14:04:51 +02:00
|
|
|
}
|
2022-07-10 08:32:38 +02:00
|
|
|
|
2022-08-20 13:21:15 +02:00
|
|
|
#define SSL_TLS1_3_OFFERED_PSK_NOT_MATCH 1
|
|
|
|
#define SSL_TLS1_3_OFFERED_PSK_MATCH 0
|
2022-08-21 13:22:23 +02:00
|
|
|
|
|
|
|
#if defined(MBEDTLS_SSL_SESSION_TICKETS)
|
|
|
|
|
|
|
|
MBEDTLS_CHECK_RETURN_CRITICAL
|
|
|
|
static int ssl_tls13_offered_psks_check_identity_match_ticket(
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_ssl_context *ssl,
|
|
|
|
const unsigned char *identity,
|
|
|
|
size_t identity_len,
|
|
|
|
uint32_t obfuscated_ticket_age,
|
|
|
|
mbedtls_ssl_session *session)
|
2022-08-21 13:22:23 +02:00
|
|
|
{
|
2022-09-06 03:16:35 +02:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2022-08-21 13:22:23 +02:00
|
|
|
unsigned char *ticket_buffer;
|
2022-09-13 08:15:48 +02:00
|
|
|
#if defined(MBEDTLS_HAVE_TIME)
|
|
|
|
mbedtls_time_t now;
|
2022-09-14 08:35:11 +02:00
|
|
|
uint64_t age_in_s;
|
2022-09-14 16:31:39 +02:00
|
|
|
int64_t age_diff_in_ms;
|
2022-09-13 08:15:48 +02:00
|
|
|
#endif
|
2022-08-21 13:22:23 +02:00
|
|
|
|
|
|
|
((void) obfuscated_ticket_age);
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(2, ("=> check_identity_match_ticket"));
|
2022-08-21 13:22:23 +02:00
|
|
|
|
2022-09-06 03:16:35 +02:00
|
|
|
/* Ticket parser is not configured, Skip */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->conf->f_ticket_parse == NULL || identity_len == 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
2022-08-21 13:22:23 +02:00
|
|
|
|
2022-09-13 05:11:48 +02:00
|
|
|
/* We create a copy of the encrypted ticket since the ticket parsing
|
|
|
|
* function is allowed to use its input buffer as an output buffer
|
|
|
|
* (in-place decryption). We do, however, need the original buffer for
|
|
|
|
* computing the PSK binder value.
|
2022-08-21 13:22:23 +02:00
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
ticket_buffer = mbedtls_calloc(1, identity_len);
|
|
|
|
if (ticket_buffer == NULL) {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(1, ("buffer too small"));
|
|
|
|
return MBEDTLS_ERR_SSL_ALLOC_FAILED;
|
|
|
|
}
|
|
|
|
memcpy(ticket_buffer, identity, identity_len);
|
|
|
|
|
|
|
|
if ((ret = ssl->conf->f_ticket_parse(ssl->conf->p_ticket,
|
|
|
|
session,
|
|
|
|
ticket_buffer, identity_len)) != 0) {
|
|
|
|
if (ret == MBEDTLS_ERR_SSL_INVALID_MAC) {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(3, ("ticket is not authentic"));
|
|
|
|
} else if (ret == MBEDTLS_ERR_SSL_SESSION_TICKET_EXPIRED) {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(3, ("ticket is expired"));
|
|
|
|
} else {
|
|
|
|
MBEDTLS_SSL_DEBUG_RET(1, "ticket_parse", ret);
|
|
|
|
}
|
2022-08-21 13:22:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* We delete the temporary buffer */
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_free(ticket_buffer);
|
2022-08-21 13:22:23 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ret != 0) {
|
2022-09-13 08:15:48 +02:00
|
|
|
goto exit;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2022-09-13 08:15:48 +02:00
|
|
|
|
2022-12-05 09:35:12 +01:00
|
|
|
/* RFC 8446 section 4.2.9
|
|
|
|
*
|
|
|
|
* Servers SHOULD NOT send NewSessionTicket with tickets that are not
|
|
|
|
* compatible with the advertised modes; however, if a server does so,
|
|
|
|
* the impact will just be that the client's attempts at resumption fail.
|
|
|
|
*
|
|
|
|
* We regard the ticket with incompatible key exchange modes as not match.
|
|
|
|
*/
|
2023-01-12 05:28:09 +01:00
|
|
|
ret = MBEDTLS_ERR_ERROR_GENERIC_ERROR;
|
2023-01-16 04:28:49 +01:00
|
|
|
MBEDTLS_SSL_PRINT_TICKET_FLAGS(4,
|
2023-01-12 04:54:04 +01:00
|
|
|
session->ticket_flags);
|
2023-01-16 05:28:27 +01:00
|
|
|
if (mbedtls_ssl_tls13_check_kex_modes(
|
|
|
|
ssl,
|
|
|
|
mbedtls_ssl_session_get_ticket_flags(
|
|
|
|
session,
|
|
|
|
MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_ALL))) {
|
2022-12-05 09:35:12 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(3, ("No suitable key exchange mode"));
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
2022-09-13 08:15:48 +02:00
|
|
|
ret = MBEDTLS_ERR_SSL_SESSION_TICKET_EXPIRED;
|
2022-08-21 13:22:23 +02:00
|
|
|
#if defined(MBEDTLS_HAVE_TIME)
|
2023-01-11 14:50:10 +01:00
|
|
|
now = mbedtls_time(NULL);
|
2022-08-21 13:22:23 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (now < session->start) {
|
2022-09-13 08:15:48 +02:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(
|
2023-01-11 14:50:10 +01:00
|
|
|
3, ("Invalid ticket start time ( now=%" MBEDTLS_PRINTF_LONGLONG
|
|
|
|
", start=%" MBEDTLS_PRINTF_LONGLONG " )",
|
|
|
|
(long long) now, (long long) session->start));
|
2022-09-13 08:15:48 +02:00
|
|
|
goto exit;
|
|
|
|
}
|
2022-08-21 13:22:23 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
age_in_s = (uint64_t) (now - session->start);
|
2022-09-13 08:15:48 +02:00
|
|
|
|
|
|
|
/* RFC 8446 section 4.6.1
|
|
|
|
*
|
|
|
|
* Servers MUST NOT use any value greater than 604800 seconds (7 days).
|
|
|
|
*
|
|
|
|
* RFC 8446 section 4.2.11.1
|
|
|
|
*
|
|
|
|
* Clients MUST NOT attempt to use tickets which have ages greater than
|
|
|
|
* the "ticket_lifetime" value which was provided with the ticket.
|
|
|
|
*
|
|
|
|
* For time being, the age MUST be less than 604800 seconds (7 days).
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
if (age_in_s > 604800) {
|
2022-09-13 08:15:48 +02:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(
|
2023-01-11 14:50:10 +01:00
|
|
|
3, ("Ticket age exceeds limitation ticket_age=%lu",
|
|
|
|
(long unsigned int) age_in_s));
|
2022-09-13 08:15:48 +02:00
|
|
|
goto exit;
|
|
|
|
}
|
2022-08-21 13:22:23 +02:00
|
|
|
|
2022-09-13 08:15:48 +02:00
|
|
|
/* RFC 8446 section 4.2.10
|
|
|
|
*
|
|
|
|
* For PSKs provisioned via NewSessionTicket, a server MUST validate that
|
|
|
|
* the ticket age for the selected PSK identity (computed by subtracting
|
|
|
|
* ticket_age_add from PskIdentity.obfuscated_ticket_age modulo 2^32) is
|
|
|
|
* within a small tolerance of the time since the ticket was issued.
|
2022-09-14 16:31:39 +02:00
|
|
|
*
|
2022-09-15 10:15:06 +02:00
|
|
|
* NOTE: When `now == session->start`, `age_diff_in_ms` may be negative
|
|
|
|
* as the age units are different on the server (s) and in the
|
|
|
|
* client (ms) side. Add a -1000 ms tolerance window to take this
|
|
|
|
* into account.
|
2022-09-13 08:15:48 +02:00
|
|
|
*/
|
2022-09-14 16:31:39 +02:00
|
|
|
age_diff_in_ms = age_in_s * 1000;
|
2023-01-11 14:50:10 +01:00
|
|
|
age_diff_in_ms -= (obfuscated_ticket_age - session->ticket_age_add);
|
|
|
|
if (age_diff_in_ms <= -1000 ||
|
|
|
|
age_diff_in_ms > MBEDTLS_SSL_TLS1_3_TICKET_AGE_TOLERANCE) {
|
2022-09-13 08:15:48 +02:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(
|
2023-01-11 14:50:10 +01:00
|
|
|
3, ("Ticket age outside tolerance window ( diff=%d )",
|
|
|
|
(int) age_diff_in_ms));
|
2022-09-13 08:15:48 +02:00
|
|
|
goto exit;
|
2022-08-21 13:22:23 +02:00
|
|
|
}
|
|
|
|
|
2022-09-13 08:15:48 +02:00
|
|
|
ret = 0;
|
|
|
|
|
|
|
|
#endif /* MBEDTLS_HAVE_TIME */
|
|
|
|
|
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ret != 0) {
|
|
|
|
mbedtls_ssl_session_free(session);
|
|
|
|
}
|
2022-09-13 08:15:48 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(2, ("<= check_identity_match_ticket"));
|
|
|
|
return ret;
|
2022-08-21 13:22:23 +02:00
|
|
|
}
|
|
|
|
#endif /* MBEDTLS_SSL_SESSION_TICKETS */
|
|
|
|
|
2022-07-20 14:49:32 +02:00
|
|
|
MBEDTLS_CHECK_RETURN_CRITICAL
|
2022-07-10 08:32:38 +02:00
|
|
|
static int ssl_tls13_offered_psks_check_identity_match(
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_ssl_context *ssl,
|
|
|
|
const unsigned char *identity,
|
|
|
|
size_t identity_len,
|
|
|
|
uint32_t obfuscated_ticket_age,
|
|
|
|
int *psk_type,
|
|
|
|
mbedtls_ssl_session *session)
|
2022-07-10 08:32:38 +02:00
|
|
|
{
|
2023-02-17 11:36:33 +01:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
|
|
|
|
2022-08-21 13:22:23 +02:00
|
|
|
((void) session);
|
|
|
|
((void) obfuscated_ticket_age);
|
2022-08-21 11:27:16 +02:00
|
|
|
*psk_type = MBEDTLS_SSL_TLS1_3_PSK_EXTERNAL;
|
2022-08-21 13:22:23 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_BUF(4, "identity", identity, identity_len);
|
2022-08-21 13:22:23 +02:00
|
|
|
ssl->handshake->resume = 0;
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_SSL_SESSION_TICKETS)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl_tls13_offered_psks_check_identity_match_ticket(
|
2022-09-13 08:15:48 +02:00
|
|
|
ssl, identity, identity_len, obfuscated_ticket_age,
|
2023-01-11 14:50:10 +01:00
|
|
|
session) == SSL_TLS1_3_OFFERED_PSK_MATCH) {
|
2022-08-21 13:22:23 +02:00
|
|
|
ssl->handshake->resume = 1;
|
|
|
|
*psk_type = MBEDTLS_SSL_TLS1_3_PSK_RESUMPTION;
|
2023-02-17 11:36:33 +01:00
|
|
|
ret = mbedtls_ssl_set_hs_psk(ssl,
|
|
|
|
session->resumption_key,
|
|
|
|
session->resumption_key_len);
|
|
|
|
if (ret != 0) {
|
|
|
|
MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_set_hs_psk", ret);
|
|
|
|
return ret;
|
|
|
|
}
|
2023-01-11 14:50:10 +01:00
|
|
|
|
|
|
|
MBEDTLS_SSL_DEBUG_BUF(4, "Ticket-resumed PSK:",
|
|
|
|
session->resumption_key,
|
|
|
|
session->resumption_key_len);
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(4, ("ticket: obfuscated_ticket_age: %u",
|
|
|
|
(unsigned) obfuscated_ticket_age));
|
|
|
|
return SSL_TLS1_3_OFFERED_PSK_MATCH;
|
2022-08-21 13:22:23 +02:00
|
|
|
}
|
|
|
|
#endif /* MBEDTLS_SSL_SESSION_TICKETS */
|
|
|
|
|
2022-07-10 08:32:38 +02:00
|
|
|
/* Check identity with external configured function */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->conf->f_psk != NULL) {
|
|
|
|
if (ssl->conf->f_psk(
|
|
|
|
ssl->conf->p_psk, ssl, identity, identity_len) == 0) {
|
|
|
|
return SSL_TLS1_3_OFFERED_PSK_MATCH;
|
2022-07-10 08:32:38 +02:00
|
|
|
}
|
2023-01-11 14:50:10 +01:00
|
|
|
return SSL_TLS1_3_OFFERED_PSK_NOT_MATCH;
|
2022-07-10 08:32:38 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_BUF(5, "identity", identity, identity_len);
|
2022-07-10 08:32:38 +02:00
|
|
|
/* Check identity with pre-configured psk */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->conf->psk_identity != NULL &&
|
2022-07-22 13:34:48 +02:00
|
|
|
identity_len == ssl->conf->psk_identity_len &&
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_ct_memcmp(ssl->conf->psk_identity,
|
|
|
|
identity, identity_len) == 0) {
|
2023-02-17 11:36:33 +01:00
|
|
|
ret = mbedtls_ssl_set_hs_psk(ssl, ssl->conf->psk, ssl->conf->psk_len);
|
|
|
|
if (ret != 0) {
|
|
|
|
MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_set_hs_psk", ret);
|
|
|
|
return ret;
|
|
|
|
}
|
2023-01-11 14:50:10 +01:00
|
|
|
return SSL_TLS1_3_OFFERED_PSK_MATCH;
|
2022-07-10 08:32:38 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return SSL_TLS1_3_OFFERED_PSK_NOT_MATCH;
|
2022-07-10 08:32:38 +02:00
|
|
|
}
|
|
|
|
|
2022-07-21 09:11:34 +02:00
|
|
|
MBEDTLS_CHECK_RETURN_CRITICAL
|
2023-03-30 04:50:04 +02:00
|
|
|
static int ssl_tls13_offered_psks_check_binder_match(
|
|
|
|
mbedtls_ssl_context *ssl,
|
|
|
|
const unsigned char *binder, size_t binder_len,
|
|
|
|
int psk_type, psa_algorithm_t psk_hash_alg)
|
2022-07-10 08:32:38 +02:00
|
|
|
{
|
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2022-07-21 09:11:34 +02:00
|
|
|
|
2022-07-20 15:31:43 +02:00
|
|
|
unsigned char transcript[PSA_HASH_MAX_SIZE];
|
2022-07-10 08:32:38 +02:00
|
|
|
size_t transcript_len;
|
2022-07-22 13:34:48 +02:00
|
|
|
unsigned char *psk;
|
2022-07-21 09:11:34 +02:00
|
|
|
size_t psk_len;
|
2022-07-20 15:31:43 +02:00
|
|
|
unsigned char server_computed_binder[PSA_HASH_MAX_SIZE];
|
2022-07-10 08:32:38 +02:00
|
|
|
|
|
|
|
/* Get current state of handshake transcript. */
|
2022-08-23 11:52:45 +02:00
|
|
|
ret = mbedtls_ssl_get_handshake_transcript(
|
2023-03-28 11:38:08 +02:00
|
|
|
ssl, mbedtls_md_type_from_psa_alg(psk_hash_alg),
|
2023-01-11 14:50:10 +01:00
|
|
|
transcript, sizeof(transcript), &transcript_len);
|
|
|
|
if (ret != 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = mbedtls_ssl_tls13_export_handshake_psk(ssl, &psk, &psk_len);
|
|
|
|
if (ret != 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = mbedtls_ssl_tls13_create_psk_binder(ssl, psk_hash_alg,
|
|
|
|
psk, psk_len, psk_type,
|
|
|
|
transcript,
|
|
|
|
server_computed_binder);
|
2022-07-21 09:11:34 +02:00
|
|
|
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_free((void *) psk);
|
2022-07-21 09:11:34 +02:00
|
|
|
#endif
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ret != 0) {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(1, ("PSK binder calculation failed."));
|
|
|
|
return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
|
2022-07-10 08:32:38 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_BUF(3, "psk binder ( computed ): ",
|
|
|
|
server_computed_binder, transcript_len);
|
|
|
|
MBEDTLS_SSL_DEBUG_BUF(3, "psk binder ( received ): ", binder, binder_len);
|
2022-07-10 08:32:38 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (mbedtls_ct_memcmp(server_computed_binder, binder, binder_len) == 0) {
|
|
|
|
return SSL_TLS1_3_OFFERED_PSK_MATCH;
|
2022-07-10 08:32:38 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_platform_zeroize(server_computed_binder,
|
|
|
|
sizeof(server_computed_binder));
|
|
|
|
return SSL_TLS1_3_OFFERED_PSK_NOT_MATCH;
|
2022-07-10 08:32:38 +02:00
|
|
|
}
|
2022-07-21 09:11:34 +02:00
|
|
|
|
2022-08-21 11:27:16 +02:00
|
|
|
MBEDTLS_CHECK_RETURN_CRITICAL
|
2022-08-23 11:58:26 +02:00
|
|
|
static int ssl_tls13_select_ciphersuite_for_psk(
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_ssl_context *ssl,
|
|
|
|
const unsigned char *cipher_suites,
|
|
|
|
const unsigned char *cipher_suites_end,
|
|
|
|
uint16_t *selected_ciphersuite,
|
|
|
|
const mbedtls_ssl_ciphersuite_t **selected_ciphersuite_info)
|
2022-08-21 11:27:16 +02:00
|
|
|
{
|
2022-08-23 11:58:26 +02:00
|
|
|
psa_algorithm_t psk_hash_alg = PSA_ALG_SHA_256;
|
2022-08-21 11:27:16 +02:00
|
|
|
|
2022-08-25 05:21:04 +02:00
|
|
|
*selected_ciphersuite = 0;
|
|
|
|
*selected_ciphersuite_info = NULL;
|
|
|
|
|
2022-08-23 11:58:26 +02:00
|
|
|
/* RFC 8446, page 55.
|
|
|
|
*
|
|
|
|
* For externally established PSKs, the Hash algorithm MUST be set when the
|
|
|
|
* PSK is established or default to SHA-256 if no such algorithm is defined.
|
|
|
|
*
|
|
|
|
*/
|
2022-08-21 11:27:16 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Search for a matching ciphersuite
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
for (const unsigned char *p = cipher_suites;
|
|
|
|
p < cipher_suites_end; p += 2) {
|
2022-08-21 11:27:16 +02:00
|
|
|
uint16_t cipher_suite;
|
2022-08-23 11:58:26 +02:00
|
|
|
const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
|
2022-08-21 11:27:16 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
cipher_suite = MBEDTLS_GET_UINT16_BE(p, 0);
|
|
|
|
ciphersuite_info = ssl_tls13_validate_peer_ciphersuite(ssl,
|
|
|
|
cipher_suite);
|
|
|
|
if (ciphersuite_info == NULL) {
|
2022-08-21 11:27:16 +02:00
|
|
|
continue;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2022-08-21 11:27:16 +02:00
|
|
|
|
|
|
|
/* MAC of selected ciphersuite MUST be same with PSK binder if exist.
|
|
|
|
* Otherwise, client should reject.
|
|
|
|
*/
|
2023-10-05 14:30:37 +02:00
|
|
|
if (psk_hash_alg ==
|
|
|
|
mbedtls_md_psa_alg_from_type((mbedtls_md_type_t) ciphersuite_info->mac)) {
|
2022-08-23 11:58:26 +02:00
|
|
|
*selected_ciphersuite = cipher_suite;
|
|
|
|
*selected_ciphersuite_info = ciphersuite_info;
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2022-08-23 11:58:26 +02:00
|
|
|
}
|
|
|
|
}
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(2, ("No matched ciphersuite"));
|
|
|
|
return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
|
2022-08-23 11:58:26 +02:00
|
|
|
}
|
2022-08-21 11:27:16 +02:00
|
|
|
|
2022-08-30 04:42:33 +02:00
|
|
|
#if defined(MBEDTLS_SSL_SESSION_TICKETS)
|
2022-08-23 11:58:26 +02:00
|
|
|
MBEDTLS_CHECK_RETURN_CRITICAL
|
|
|
|
static int ssl_tls13_select_ciphersuite_for_resumption(
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_ssl_context *ssl,
|
|
|
|
const unsigned char *cipher_suites,
|
|
|
|
const unsigned char *cipher_suites_end,
|
|
|
|
mbedtls_ssl_session *session,
|
|
|
|
uint16_t *selected_ciphersuite,
|
|
|
|
const mbedtls_ssl_ciphersuite_t **selected_ciphersuite_info)
|
2022-08-23 11:58:26 +02:00
|
|
|
{
|
2022-08-30 04:42:33 +02:00
|
|
|
|
2022-08-23 11:58:26 +02:00
|
|
|
*selected_ciphersuite = 0;
|
|
|
|
*selected_ciphersuite_info = NULL;
|
2023-01-11 14:50:10 +01:00
|
|
|
for (const unsigned char *p = cipher_suites; p < cipher_suites_end; p += 2) {
|
|
|
|
uint16_t cipher_suite = MBEDTLS_GET_UINT16_BE(p, 0);
|
2022-08-30 04:42:33 +02:00
|
|
|
const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (cipher_suite != session->ciphersuite) {
|
2022-08-30 04:42:33 +02:00
|
|
|
continue;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2022-08-30 04:42:33 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
ciphersuite_info = ssl_tls13_validate_peer_ciphersuite(ssl,
|
|
|
|
cipher_suite);
|
|
|
|
if (ciphersuite_info == NULL) {
|
2022-08-30 04:42:33 +02:00
|
|
|
continue;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2022-08-30 04:42:33 +02:00
|
|
|
|
2022-09-13 05:11:48 +02:00
|
|
|
*selected_ciphersuite = cipher_suite;
|
2022-08-30 04:42:33 +02:00
|
|
|
*selected_ciphersuite_info = ciphersuite_info;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2022-08-30 04:42:33 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
|
2022-08-21 11:27:16 +02:00
|
|
|
}
|
2022-08-23 11:58:26 +02:00
|
|
|
|
2022-08-30 04:42:33 +02:00
|
|
|
MBEDTLS_CHECK_RETURN_CRITICAL
|
2023-01-11 14:50:10 +01:00
|
|
|
static int ssl_tls13_session_copy_ticket(mbedtls_ssl_session *dst,
|
|
|
|
const mbedtls_ssl_session *src)
|
2022-08-30 04:42:33 +02:00
|
|
|
{
|
|
|
|
dst->ticket_age_add = src->ticket_age_add;
|
|
|
|
dst->ticket_flags = src->ticket_flags;
|
|
|
|
dst->resumption_key_len = src->resumption_key_len;
|
2023-01-11 14:50:10 +01:00
|
|
|
if (src->resumption_key_len == 0) {
|
|
|
|
return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
|
|
|
|
}
|
|
|
|
memcpy(dst->resumption_key, src->resumption_key, src->resumption_key_len);
|
2022-09-13 05:11:48 +02:00
|
|
|
|
2022-12-12 09:01:43 +01:00
|
|
|
#if defined(MBEDTLS_SSL_EARLY_DATA)
|
|
|
|
dst->max_early_data_size = src->max_early_data_size;
|
|
|
|
#endif
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2022-08-21 11:27:16 +02:00
|
|
|
}
|
2022-08-30 04:42:33 +02:00
|
|
|
#endif /* MBEDTLS_SSL_SESSION_TICKETS */
|
2022-08-23 11:58:26 +02:00
|
|
|
|
2022-07-10 08:32:38 +02:00
|
|
|
/* Parser for pre_shared_key extension in client hello
|
|
|
|
* 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-20 14:49:32 +02:00
|
|
|
MBEDTLS_CHECK_RETURN_CRITICAL
|
2023-03-30 04:50:04 +02:00
|
|
|
static int ssl_tls13_parse_pre_shared_key_ext(
|
|
|
|
mbedtls_ssl_context *ssl,
|
|
|
|
const unsigned char *pre_shared_key_ext,
|
|
|
|
const unsigned char *pre_shared_key_ext_end,
|
|
|
|
const unsigned char *ciphersuites,
|
|
|
|
const unsigned char *ciphersuites_end)
|
2022-07-10 08:32:38 +02:00
|
|
|
{
|
2023-02-06 00:34:21 +01:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2022-08-23 11:52:45 +02:00
|
|
|
const unsigned char *identities = pre_shared_key_ext;
|
2022-07-22 15:27:34 +02:00
|
|
|
const unsigned char *p_identity_len;
|
|
|
|
size_t identities_len;
|
2022-07-10 08:32:38 +02:00
|
|
|
const unsigned char *identities_end;
|
2022-07-22 15:27:34 +02:00
|
|
|
const unsigned char *binders;
|
|
|
|
const unsigned char *p_binder_len;
|
|
|
|
size_t binders_len;
|
2022-07-10 08:32:38 +02:00
|
|
|
const unsigned char *binders_end;
|
2022-07-21 09:11:34 +02:00
|
|
|
int matched_identity = -1;
|
|
|
|
int identity_id = -1;
|
2022-07-10 08:32:38 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_BUF(3, "pre_shared_key extension",
|
|
|
|
pre_shared_key_ext,
|
|
|
|
pre_shared_key_ext_end - pre_shared_key_ext);
|
2022-07-10 08:32:38 +02:00
|
|
|
|
2022-07-21 09:11:34 +02:00
|
|
|
/* identities_len 2 bytes
|
|
|
|
* identities_data >= 7 bytes
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_CHK_BUF_READ_PTR(identities, pre_shared_key_ext_end, 7 + 2);
|
|
|
|
identities_len = MBEDTLS_GET_UINT16_BE(identities, 0);
|
2022-07-22 15:27:34 +02:00
|
|
|
p_identity_len = identities + 2;
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_CHK_BUF_READ_PTR(p_identity_len, pre_shared_key_ext_end,
|
|
|
|
identities_len);
|
2022-07-22 15:27:34 +02:00
|
|
|
identities_end = p_identity_len + identities_len;
|
2022-07-21 09:11:34 +02:00
|
|
|
|
|
|
|
/* binders_len 2 bytes
|
|
|
|
* binders >= 33 bytes
|
|
|
|
*/
|
2022-07-22 15:27:34 +02:00
|
|
|
binders = identities_end;
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_CHK_BUF_READ_PTR(binders, pre_shared_key_ext_end, 33 + 2);
|
|
|
|
binders_len = MBEDTLS_GET_UINT16_BE(binders, 0);
|
2022-07-22 15:27:34 +02:00
|
|
|
p_binder_len = binders + 2;
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_CHK_BUF_READ_PTR(p_binder_len, pre_shared_key_ext_end, binders_len);
|
2022-07-22 15:27:34 +02:00
|
|
|
binders_end = p_binder_len + binders_len;
|
2022-07-10 08:32:38 +02:00
|
|
|
|
2023-02-06 00:34:21 +01:00
|
|
|
ret = ssl->handshake->update_checksum(ssl, pre_shared_key_ext,
|
|
|
|
identities_end - pre_shared_key_ext);
|
|
|
|
if (0 != ret) {
|
|
|
|
MBEDTLS_SSL_DEBUG_RET(1, ("update_checksum"), ret);
|
|
|
|
return ret;
|
|
|
|
}
|
2022-07-10 08:32:38 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
while (p_identity_len < identities_end && p_binder_len < binders_end) {
|
2022-07-10 08:32:38 +02:00
|
|
|
const unsigned char *identity;
|
2022-07-22 15:27:34 +02:00
|
|
|
size_t identity_len;
|
2022-08-30 04:42:33 +02:00
|
|
|
uint32_t obfuscated_ticket_age;
|
2022-07-21 09:11:34 +02:00
|
|
|
const unsigned char *binder;
|
2022-07-22 15:27:34 +02:00
|
|
|
size_t binder_len;
|
2022-08-21 11:27:16 +02:00
|
|
|
int psk_type;
|
|
|
|
uint16_t cipher_suite;
|
2022-08-23 11:52:45 +02:00
|
|
|
const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
|
2022-08-30 04:42:33 +02:00
|
|
|
#if defined(MBEDTLS_SSL_SESSION_TICKETS)
|
|
|
|
mbedtls_ssl_session session;
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_ssl_session_init(&session);
|
2022-08-30 04:42:33 +02:00
|
|
|
#endif
|
2022-07-21 09:11:34 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_CHK_BUF_READ_PTR(p_identity_len, identities_end, 2 + 1 + 4);
|
|
|
|
identity_len = MBEDTLS_GET_UINT16_BE(p_identity_len, 0);
|
2022-07-22 15:27:34 +02:00
|
|
|
identity = p_identity_len + 2;
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_CHK_BUF_READ_PTR(identity, identities_end, identity_len + 4);
|
|
|
|
obfuscated_ticket_age = MBEDTLS_GET_UINT32_BE(identity, identity_len);
|
2022-07-22 15:27:34 +02:00
|
|
|
p_identity_len += identity_len + 6;
|
2022-07-10 08:32:38 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_CHK_BUF_READ_PTR(p_binder_len, binders_end, 1 + 32);
|
2022-07-22 15:27:34 +02:00
|
|
|
binder_len = *p_binder_len;
|
|
|
|
binder = p_binder_len + 1;
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_CHK_BUF_READ_PTR(binder, binders_end, binder_len);
|
2022-07-22 15:27:34 +02:00
|
|
|
p_binder_len += binder_len + 1;
|
2022-07-10 08:32:38 +02:00
|
|
|
|
2022-07-21 09:11:34 +02:00
|
|
|
identity_id++;
|
2023-01-11 14:50:10 +01:00
|
|
|
if (matched_identity != -1) {
|
2022-07-21 09:11:34 +02:00
|
|
|
continue;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2022-07-10 08:32:38 +02:00
|
|
|
|
2022-07-21 09:11:34 +02:00
|
|
|
ret = ssl_tls13_offered_psks_check_identity_match(
|
2023-01-11 14:50:10 +01:00
|
|
|
ssl, identity, identity_len, obfuscated_ticket_age,
|
|
|
|
&psk_type, &session);
|
|
|
|
if (ret != SSL_TLS1_3_OFFERED_PSK_MATCH) {
|
2022-07-10 08:32:38 +02:00
|
|
|
continue;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2022-07-10 08:32:38 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(4, ("found matched identity"));
|
|
|
|
switch (psk_type) {
|
2022-08-25 05:21:04 +02:00
|
|
|
case MBEDTLS_SSL_TLS1_3_PSK_EXTERNAL:
|
|
|
|
ret = ssl_tls13_select_ciphersuite_for_psk(
|
2023-01-11 14:50:10 +01:00
|
|
|
ssl, ciphersuites, ciphersuites_end,
|
|
|
|
&cipher_suite, &ciphersuite_info);
|
2022-08-25 05:21:04 +02:00
|
|
|
break;
|
|
|
|
case MBEDTLS_SSL_TLS1_3_PSK_RESUMPTION:
|
2022-08-30 04:42:33 +02:00
|
|
|
#if defined(MBEDTLS_SSL_SESSION_TICKETS)
|
2022-08-25 05:21:04 +02:00
|
|
|
ret = ssl_tls13_select_ciphersuite_for_resumption(
|
2023-01-11 14:50:10 +01:00
|
|
|
ssl, ciphersuites, ciphersuites_end, &session,
|
|
|
|
&cipher_suite, &ciphersuite_info);
|
|
|
|
if (ret != 0) {
|
|
|
|
mbedtls_ssl_session_free(&session);
|
|
|
|
}
|
2022-08-30 04:42:33 +02:00
|
|
|
#else
|
|
|
|
ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
|
|
|
|
#endif
|
2022-08-25 05:21:04 +02:00
|
|
|
break;
|
|
|
|
default:
|
2023-01-11 14:50:10 +01:00
|
|
|
return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
|
2022-08-25 05:21:04 +02:00
|
|
|
}
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ret != 0) {
|
2022-08-23 11:58:26 +02:00
|
|
|
/* See below, no cipher_suite available, abort handshake */
|
|
|
|
MBEDTLS_SSL_PEND_FATAL_ALERT(
|
|
|
|
MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR,
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
|
2022-08-23 11:58:26 +02:00
|
|
|
MBEDTLS_SSL_DEBUG_RET(
|
2023-01-11 14:50:10 +01:00
|
|
|
2, "ssl_tls13_select_ciphersuite", ret);
|
|
|
|
return ret;
|
2022-08-21 11:27:16 +02:00
|
|
|
}
|
|
|
|
|
2022-07-21 09:11:34 +02:00
|
|
|
ret = ssl_tls13_offered_psks_check_binder_match(
|
2023-01-11 14:50:10 +01:00
|
|
|
ssl, binder, binder_len, psk_type,
|
2023-10-05 14:30:37 +02:00
|
|
|
mbedtls_md_psa_alg_from_type((mbedtls_md_type_t) ciphersuite_info->mac));
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ret != SSL_TLS1_3_OFFERED_PSK_MATCH) {
|
2022-08-25 04:51:44 +02:00
|
|
|
/* For security reasons, the handshake should be aborted when we
|
|
|
|
* fail to validate a binder value. See RFC 8446 section 4.2.11.2
|
|
|
|
* and appendix E.6. */
|
2022-08-30 04:42:33 +02:00
|
|
|
#if defined(MBEDTLS_SSL_SESSION_TICKETS)
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_ssl_session_free(&session);
|
2022-08-30 04:42:33 +02:00
|
|
|
#endif
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(3, ("Invalid binder."));
|
2023-03-30 04:50:04 +02:00
|
|
|
MBEDTLS_SSL_DEBUG_RET(
|
|
|
|
1, "ssl_tls13_offered_psks_check_binder_match", ret);
|
2022-07-10 08:32:38 +02:00
|
|
|
MBEDTLS_SSL_PEND_FATAL_ALERT(
|
2022-07-20 16:11:00 +02:00
|
|
|
MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR,
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
|
|
|
|
return ret;
|
2022-07-10 08:32:38 +02:00
|
|
|
}
|
|
|
|
|
2022-07-21 09:11:34 +02:00
|
|
|
matched_identity = identity_id;
|
2022-08-21 11:27:16 +02:00
|
|
|
|
|
|
|
/* Update handshake parameters */
|
2022-08-29 14:16:09 +02:00
|
|
|
ssl->handshake->ciphersuite_info = ciphersuite_info;
|
2022-09-13 05:11:48 +02:00
|
|
|
ssl->session_negotiate->ciphersuite = cipher_suite;
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(2, ("overwrite ciphersuite: %04x - %s",
|
|
|
|
cipher_suite, ciphersuite_info->name));
|
2022-08-30 04:42:33 +02:00
|
|
|
#if defined(MBEDTLS_SSL_SESSION_TICKETS)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (psk_type == MBEDTLS_SSL_TLS1_3_PSK_RESUMPTION) {
|
|
|
|
ret = ssl_tls13_session_copy_ticket(ssl->session_negotiate,
|
|
|
|
&session);
|
|
|
|
mbedtls_ssl_session_free(&session);
|
|
|
|
if (ret != 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
2022-08-30 04:42:33 +02:00
|
|
|
}
|
|
|
|
#endif /* MBEDTLS_SSL_SESSION_TICKETS */
|
2022-07-10 08:32:38 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (p_identity_len != identities_end || p_binder_len != binders_end) {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(3, ("pre_shared_key extension decode error"));
|
|
|
|
MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
|
|
|
|
MBEDTLS_ERR_SSL_DECODE_ERROR);
|
|
|
|
return MBEDTLS_ERR_SSL_DECODE_ERROR;
|
2022-07-10 08:32:38 +02:00
|
|
|
}
|
|
|
|
|
2022-07-22 17:05:59 +02:00
|
|
|
/* Update the handshake transcript with the binder list. */
|
2023-03-30 04:50:04 +02:00
|
|
|
ret = ssl->handshake->update_checksum(
|
|
|
|
ssl, identities_end, (size_t) (binders_end - identities_end));
|
2023-02-06 00:34:21 +01:00
|
|
|
if (0 != ret) {
|
|
|
|
MBEDTLS_SSL_DEBUG_RET(1, ("update_checksum"), ret);
|
|
|
|
return ret;
|
|
|
|
}
|
2023-01-11 14:50:10 +01:00
|
|
|
if (matched_identity == -1) {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(3, ("No matched PSK or ticket."));
|
|
|
|
return MBEDTLS_ERR_SSL_UNKNOWN_IDENTITY;
|
2022-07-21 09:11:34 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
ssl->handshake->selected_identity = (uint16_t) matched_identity;
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(3, ("Pre shared key found"));
|
2022-07-21 09:11:34 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2022-07-10 08:32:38 +02:00
|
|
|
}
|
2022-07-11 08:10:03 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* struct {
|
|
|
|
* select ( Handshake.msg_type ) {
|
|
|
|
* ....
|
|
|
|
* case server_hello:
|
|
|
|
* uint16 selected_identity;
|
|
|
|
* }
|
|
|
|
* } PreSharedKeyExtension;
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
static int ssl_tls13_write_server_pre_shared_key_ext(mbedtls_ssl_context *ssl,
|
|
|
|
unsigned char *buf,
|
|
|
|
unsigned char *end,
|
|
|
|
size_t *olen)
|
2022-07-11 08:10:03 +02:00
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
unsigned char *p = (unsigned char *) buf;
|
2022-07-11 08:10:03 +02:00
|
|
|
|
|
|
|
*olen = 0;
|
|
|
|
|
2022-10-06 19:34:28 +02:00
|
|
|
int not_using_psk = 0;
|
2022-07-11 08:10:03 +02:00
|
|
|
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
2023-01-11 14:50:10 +01:00
|
|
|
not_using_psk = (mbedtls_svc_key_id_is_null(ssl->handshake->psk_opaque));
|
2022-07-11 08:10:03 +02:00
|
|
|
#else
|
2023-01-11 14:50:10 +01:00
|
|
|
not_using_psk = (ssl->handshake->psk == NULL);
|
2022-07-11 08:10:03 +02:00
|
|
|
#endif
|
2023-01-11 14:50:10 +01:00
|
|
|
if (not_using_psk) {
|
2022-07-11 08:10:03 +02:00
|
|
|
/* We shouldn't have called this extension writer unless we've
|
|
|
|
* chosen to use a PSK. */
|
2023-01-11 14:50:10 +01:00
|
|
|
return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
|
2022-07-11 08:10:03 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, adding pre_shared_key extension"));
|
|
|
|
MBEDTLS_SSL_CHK_BUF_PTR(p, end, 6);
|
2022-07-11 08:10:03 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_PRE_SHARED_KEY, p, 0);
|
|
|
|
MBEDTLS_PUT_UINT16_BE(2, p, 2);
|
2022-07-11 08:10:03 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_PUT_UINT16_BE(ssl->handshake->selected_identity, p, 4);
|
2022-07-11 08:10:03 +02:00
|
|
|
|
|
|
|
*olen = 6;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(4, ("sent selected_identity: %u",
|
|
|
|
ssl->handshake->selected_identity));
|
2022-07-11 08:10:03 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_PRE_SHARED_KEY);
|
2022-11-08 14:19:34 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2022-07-11 08:10:03 +02:00
|
|
|
}
|
|
|
|
|
2022-10-04 16:38:25 +02:00
|
|
|
#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED */
|
2022-07-08 14:04:51 +02:00
|
|
|
|
2022-02-15 11:04:37 +01:00
|
|
|
/* From RFC 8446:
|
|
|
|
* struct {
|
2022-04-14 09:10:37 +02:00
|
|
|
* ProtocolVersion versions<2..254>;
|
2022-02-15 11:04:37 +01:00
|
|
|
* } 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_parse_supported_versions_ext(mbedtls_ssl_context *ssl,
|
|
|
|
const unsigned char *buf,
|
|
|
|
const unsigned char *end)
|
2022-02-15 11:04:37 +01:00
|
|
|
{
|
|
|
|
const unsigned char *p = buf;
|
2022-04-15 04:52:39 +02:00
|
|
|
size_t versions_len;
|
2022-04-11 11:55:18 +02:00
|
|
|
const unsigned char *versions_end;
|
2022-04-21 05:01:38 +02:00
|
|
|
uint16_t tls_version;
|
2023-03-07 20:46:59 +01:00
|
|
|
int found_supported_version = 0;
|
2022-02-15 11:04:37 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 1);
|
2022-04-11 11:55:18 +02:00
|
|
|
versions_len = p[0];
|
2022-02-15 11:04:37 +01:00
|
|
|
p += 1;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, versions_len);
|
2022-04-11 11:55:18 +02:00
|
|
|
versions_end = p + versions_len;
|
2023-01-11 14:50:10 +01:00
|
|
|
while (p < versions_end) {
|
|
|
|
MBEDTLS_SSL_CHK_BUF_READ_PTR(p, versions_end, 2);
|
|
|
|
tls_version = mbedtls_ssl_read_version(p, ssl->conf->transport);
|
2022-04-19 02:02:38 +02:00
|
|
|
p += 2;
|
2022-02-15 11:04:37 +01:00
|
|
|
|
2023-04-03 16:45:39 +02:00
|
|
|
if (MBEDTLS_SSL_VERSION_TLS1_3 == tls_version) {
|
2023-03-07 20:46:59 +01:00
|
|
|
found_supported_version = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2023-04-03 16:45:39 +02:00
|
|
|
if ((MBEDTLS_SSL_VERSION_TLS1_2 == tls_version) &&
|
|
|
|
mbedtls_ssl_conf_is_tls12_enabled(ssl->conf)) {
|
2023-03-07 20:46:59 +01:00
|
|
|
found_supported_version = 1;
|
2022-02-15 11:04:37 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-07 20:46:59 +01:00
|
|
|
if (!found_supported_version) {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(1, ("No supported version found."));
|
2022-02-15 11:04:37 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION,
|
|
|
|
MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION);
|
|
|
|
return MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION;
|
2022-02-15 11:04:37 +01:00
|
|
|
}
|
|
|
|
|
2023-03-07 20:46:59 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(1, ("Negotiated version: [%04x]",
|
2023-01-11 14:50:10 +01:00
|
|
|
(unsigned int) tls_version));
|
2022-02-15 11:04:37 +01:00
|
|
|
|
2023-03-07 20:46:59 +01:00
|
|
|
return (int) tls_version;
|
2022-02-15 11:04:37 +01:00
|
|
|
}
|
|
|
|
|
2023-06-15 16:48:19 +02:00
|
|
|
#if defined(PSA_WANT_ALG_ECDH) || defined(PSA_WANT_ALG_FFDH)
|
2022-04-22 04:34:40 +02:00
|
|
|
/*
|
2022-02-15 11:04:37 +01:00
|
|
|
*
|
|
|
|
* From RFC 8446:
|
|
|
|
* enum {
|
|
|
|
* ... (0xFFFF)
|
|
|
|
* } NamedGroup;
|
|
|
|
* struct {
|
|
|
|
* NamedGroup named_group_list<2..2^16-1>;
|
|
|
|
* } NamedGroupList;
|
|
|
|
*/
|
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_groups_ext(mbedtls_ssl_context *ssl,
|
|
|
|
const unsigned char *buf,
|
|
|
|
const unsigned char *end)
|
2022-02-15 11:04:37 +01:00
|
|
|
{
|
|
|
|
const unsigned char *p = buf;
|
2022-04-19 09:57:30 +02:00
|
|
|
size_t named_group_list_len;
|
2022-04-15 04:52:39 +02:00
|
|
|
const unsigned char *named_group_list_end;
|
2022-02-15 11:04:37 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_BUF(3, "supported_groups extension", p, end - buf);
|
|
|
|
MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
|
|
|
|
named_group_list_len = MBEDTLS_GET_UINT16_BE(p, 0);
|
2022-02-15 11:04:37 +01:00
|
|
|
p += 2;
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, named_group_list_len);
|
2022-04-15 04:52:39 +02:00
|
|
|
named_group_list_end = p + named_group_list_len;
|
2022-04-21 11:48:09 +02:00
|
|
|
ssl->handshake->hrr_selected_group = 0;
|
2022-02-15 11:04:37 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
while (p < named_group_list_end) {
|
2022-04-20 09:16:41 +02:00
|
|
|
uint16_t named_group;
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_CHK_BUF_READ_PTR(p, named_group_list_end, 2);
|
|
|
|
named_group = MBEDTLS_GET_UINT16_BE(p, 0);
|
2022-04-20 09:16:41 +02:00
|
|
|
p += 2;
|
2022-02-15 11:04:37 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(2,
|
|
|
|
("got named group: %s(%04x)",
|
|
|
|
mbedtls_ssl_named_group_to_str(named_group),
|
|
|
|
named_group));
|
2022-04-19 09:57:30 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (!mbedtls_ssl_named_group_is_offered(ssl, named_group) ||
|
|
|
|
!mbedtls_ssl_named_group_is_supported(named_group) ||
|
|
|
|
ssl->handshake->hrr_selected_group != 0) {
|
2022-04-20 09:16:41 +02:00
|
|
|
continue;
|
2022-02-15 11:04:37 +01:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(2,
|
|
|
|
("add named group %s(%04x) into received list.",
|
|
|
|
mbedtls_ssl_named_group_to_str(named_group),
|
|
|
|
named_group));
|
2022-04-23 10:11:39 +02:00
|
|
|
|
2022-04-21 11:48:09 +02:00
|
|
|
ssl->handshake->hrr_selected_group = named_group;
|
2022-02-15 11:04:37 +01:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2022-02-15 11:04:37 +01:00
|
|
|
|
|
|
|
}
|
2023-06-15 16:48:19 +02:00
|
|
|
#endif /* PSA_WANT_ALG_ECDH || PSA_WANT_ALG_FFDH */
|
2022-02-15 11:04:37 +01:00
|
|
|
|
2022-04-20 09:16:41 +02:00
|
|
|
#define SSL_TLS1_3_PARSE_KEY_SHARES_EXT_NO_MATCH 1
|
|
|
|
|
2023-07-25 11:23:50 +02:00
|
|
|
#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED)
|
2022-02-15 11:04:37 +01:00
|
|
|
/*
|
|
|
|
* ssl_tls13_parse_key_shares_ext() verifies whether the information in the
|
2023-03-30 04:50:04 +02:00
|
|
|
* extension is correct and stores the first acceptable key share and its
|
|
|
|
* associated group.
|
2022-02-15 11:04:37 +01:00
|
|
|
*
|
|
|
|
* Possible return values are:
|
|
|
|
* - 0: Successful processing of the client provided key share extension.
|
2023-03-30 04:50:04 +02:00
|
|
|
* - SSL_TLS1_3_PARSE_KEY_SHARES_EXT_NO_MATCH: The key shares provided by
|
|
|
|
* the client does not match a group supported by the server. A
|
|
|
|
* HelloRetryRequest will be needed.
|
2022-04-22 04:34:40 +02:00
|
|
|
* - A negative value for fatal errors.
|
2022-04-23 10:11:39 +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_key_shares_ext(mbedtls_ssl_context *ssl,
|
|
|
|
const unsigned char *buf,
|
|
|
|
const unsigned char *end)
|
2022-02-15 11:04:37 +01:00
|
|
|
{
|
2022-04-19 02:02:38 +02:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2022-02-15 11:04:37 +01:00
|
|
|
unsigned char const *p = buf;
|
2022-04-15 04:52:39 +02:00
|
|
|
unsigned char const *client_shares_end;
|
2022-04-23 10:11:39 +02:00
|
|
|
size_t client_shares_len;
|
2022-02-15 11:04:37 +01:00
|
|
|
|
|
|
|
/* From RFC 8446:
|
|
|
|
*
|
|
|
|
* struct {
|
|
|
|
* KeyShareEntry client_shares<0..2^16-1>;
|
|
|
|
* } KeyShareClientHello;
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
|
|
|
|
client_shares_len = MBEDTLS_GET_UINT16_BE(p, 0);
|
2022-02-15 11:04:37 +01:00
|
|
|
p += 2;
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, client_shares_len);
|
2022-02-15 11:04:37 +01:00
|
|
|
|
|
|
|
ssl->handshake->offered_group_id = 0;
|
2022-04-15 04:52:39 +02:00
|
|
|
client_shares_end = p + client_shares_len;
|
2022-02-15 11:04:37 +01:00
|
|
|
|
|
|
|
/* We try to find a suitable key share entry and copy it to the
|
|
|
|
* handshake context. Later, we have to find out whether we can do
|
|
|
|
* something with the provided key share or whether we have to
|
2022-04-02 05:34:37 +02:00
|
|
|
* dismiss it and send a HelloRetryRequest message.
|
|
|
|
*/
|
2022-02-15 11:04:37 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
while (p < client_shares_end) {
|
2022-04-10 12:20:43 +02:00
|
|
|
uint16_t group;
|
2022-04-23 10:11:39 +02:00
|
|
|
size_t key_exchange_len;
|
2022-05-05 04:50:38 +02:00
|
|
|
const unsigned char *key_exchange;
|
2022-02-15 11:04:37 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* struct {
|
|
|
|
* NamedGroup group;
|
|
|
|
* opaque key_exchange<1..2^16-1>;
|
|
|
|
* } KeyShareEntry;
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_CHK_BUF_READ_PTR(p, client_shares_end, 4);
|
|
|
|
group = MBEDTLS_GET_UINT16_BE(p, 0);
|
|
|
|
key_exchange_len = MBEDTLS_GET_UINT16_BE(p, 2);
|
2022-04-23 10:11:39 +02:00
|
|
|
p += 4;
|
2022-05-05 04:50:38 +02:00
|
|
|
key_exchange = p;
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_CHK_BUF_READ_PTR(p, client_shares_end, key_exchange_len);
|
2022-05-05 04:50:38 +02:00
|
|
|
p += key_exchange_len;
|
2022-02-15 11:04:37 +01:00
|
|
|
|
|
|
|
/* Continue parsing even if we have already found a match,
|
2022-04-02 05:34:37 +02:00
|
|
|
* for input validation purposes.
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
if (!mbedtls_ssl_named_group_is_offered(ssl, group) ||
|
|
|
|
!mbedtls_ssl_named_group_is_supported(group) ||
|
|
|
|
ssl->handshake->offered_group_id != 0) {
|
2022-04-21 11:24:56 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2022-02-15 11:04:37 +01:00
|
|
|
/*
|
2023-05-18 15:45:53 +02:00
|
|
|
* ECDHE and FFDHE groups are supported
|
2022-02-15 11:04:37 +01:00
|
|
|
*/
|
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-05-18 15:45:53 +02:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(2, ("ECDH/FFDH group: %s (%04x)",
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_ssl_named_group_to_str(group),
|
|
|
|
group));
|
2023-07-04 10:02:38 +02:00
|
|
|
ret = mbedtls_ssl_tls13_read_public_xxdhe_share(
|
2023-01-11 14:50:10 +01:00
|
|
|
ssl, key_exchange - 2, key_exchange_len + 2);
|
|
|
|
if (ret != 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
2022-04-21 11:48:09 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
} else {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(4, ("Unrecognized NamedGroup %u",
|
|
|
|
(unsigned) group));
|
2022-04-10 12:20:43 +02:00
|
|
|
continue;
|
2022-02-15 11:04:37 +01:00
|
|
|
}
|
|
|
|
|
2022-04-10 12:20:43 +02:00
|
|
|
ssl->handshake->offered_group_id = group;
|
2022-02-15 11:04:37 +01:00
|
|
|
}
|
|
|
|
|
2022-04-23 10:11:39 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->handshake->offered_group_id == 0) {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(1, ("no matching key share"));
|
|
|
|
return SSL_TLS1_3_PARSE_KEY_SHARES_EXT_NO_MATCH;
|
2022-02-15 11:04:37 +01:00
|
|
|
}
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2022-02-15 11:04:37 +01:00
|
|
|
}
|
2023-07-25 11:23:50 +02:00
|
|
|
#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED */
|
2022-02-15 11:04:37 +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_client_hello_has_exts(mbedtls_ssl_context *ssl,
|
|
|
|
int exts_mask)
|
2022-02-15 11:04:37 +01:00
|
|
|
{
|
2022-08-29 09:25:36 +02:00
|
|
|
int masked = ssl->handshake->received_extensions & exts_mask;
|
2023-01-11 14:50:10 +01:00
|
|
|
return masked == exts_mask;
|
2022-02-15 11:04:37 +01:00
|
|
|
}
|
|
|
|
|
2022-11-07 07:03:44 +01:00
|
|
|
#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
|
2022-06-17 10:52:54 +02:00
|
|
|
MBEDTLS_CHECK_RETURN_CRITICAL
|
2022-04-19 02:02:38 +02:00
|
|
|
static int ssl_tls13_client_hello_has_exts_for_ephemeral_key_exchange(
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_ssl_context *ssl)
|
2022-02-15 11:04:37 +01:00
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
return ssl_tls13_client_hello_has_exts(
|
|
|
|
ssl,
|
|
|
|
MBEDTLS_SSL_EXT_MASK(SUPPORTED_GROUPS) |
|
|
|
|
MBEDTLS_SSL_EXT_MASK(KEY_SHARE) |
|
|
|
|
MBEDTLS_SSL_EXT_MASK(SIG_ALG));
|
2022-07-11 09:03:24 +02:00
|
|
|
}
|
2022-11-07 07:03:44 +01:00
|
|
|
#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
|
2022-07-11 09:03:24 +02:00
|
|
|
|
2022-11-07 07:03:44 +01:00
|
|
|
#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_ENABLED)
|
2022-07-11 09:03:24 +02:00
|
|
|
MBEDTLS_CHECK_RETURN_CRITICAL
|
|
|
|
static int ssl_tls13_client_hello_has_exts_for_psk_key_exchange(
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_ssl_context *ssl)
|
2022-07-11 09:03:24 +02:00
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
return ssl_tls13_client_hello_has_exts(
|
|
|
|
ssl,
|
|
|
|
MBEDTLS_SSL_EXT_MASK(PRE_SHARED_KEY) |
|
|
|
|
MBEDTLS_SSL_EXT_MASK(PSK_KEY_EXCHANGE_MODES));
|
2022-02-15 11:04:37 +01:00
|
|
|
}
|
2022-11-07 07:03:44 +01:00
|
|
|
#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_ENABLED */
|
2022-02-15 11:04:37 +01:00
|
|
|
|
2022-11-07 07:03:44 +01:00
|
|
|
#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL_ENABLED)
|
2022-07-11 09:03:24 +02:00
|
|
|
MBEDTLS_CHECK_RETURN_CRITICAL
|
|
|
|
static int ssl_tls13_client_hello_has_exts_for_psk_ephemeral_key_exchange(
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_ssl_context *ssl)
|
2022-07-11 09:03:24 +02:00
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
return ssl_tls13_client_hello_has_exts(
|
|
|
|
ssl,
|
|
|
|
MBEDTLS_SSL_EXT_MASK(SUPPORTED_GROUPS) |
|
|
|
|
MBEDTLS_SSL_EXT_MASK(KEY_SHARE) |
|
|
|
|
MBEDTLS_SSL_EXT_MASK(PRE_SHARED_KEY) |
|
|
|
|
MBEDTLS_SSL_EXT_MASK(PSK_KEY_EXCHANGE_MODES));
|
2022-07-11 09:03:24 +02:00
|
|
|
}
|
2022-11-07 07:03:44 +01:00
|
|
|
#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL_ENABLED */
|
2022-07-11 09:03:24 +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_ephemeral_key_exchange(mbedtls_ssl_context *ssl)
|
2022-02-15 11:04:37 +01:00
|
|
|
{
|
2022-11-07 07:03:44 +01:00
|
|
|
#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
|
2023-01-11 14:50:10 +01:00
|
|
|
return mbedtls_ssl_conf_tls13_ephemeral_enabled(ssl) &&
|
|
|
|
ssl_tls13_client_hello_has_exts_for_ephemeral_key_exchange(ssl);
|
2022-11-07 07:03:44 +01:00
|
|
|
#else
|
|
|
|
((void) ssl);
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2022-11-07 07:03:44 +01:00
|
|
|
#endif
|
2022-07-11 09:03:24 +02:00
|
|
|
}
|
2022-02-15 11:04:37 +01:00
|
|
|
|
2022-07-11 09:03:24 +02:00
|
|
|
MBEDTLS_CHECK_RETURN_CRITICAL
|
2023-01-11 14:50:10 +01:00
|
|
|
static int ssl_tls13_check_psk_key_exchange(mbedtls_ssl_context *ssl)
|
2022-07-11 09:03:24 +02:00
|
|
|
{
|
2022-11-07 07:03:44 +01:00
|
|
|
#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_ENABLED)
|
2023-01-11 14:50:10 +01:00
|
|
|
return mbedtls_ssl_conf_tls13_psk_enabled(ssl) &&
|
|
|
|
mbedtls_ssl_tls13_psk_enabled(ssl) &&
|
|
|
|
ssl_tls13_client_hello_has_exts_for_psk_key_exchange(ssl);
|
2022-07-11 09:03:24 +02:00
|
|
|
#else
|
|
|
|
((void) ssl);
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2022-07-11 09:03:24 +02:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
MBEDTLS_CHECK_RETURN_CRITICAL
|
2023-01-11 14:50:10 +01:00
|
|
|
static int ssl_tls13_check_psk_ephemeral_key_exchange(mbedtls_ssl_context *ssl)
|
2022-07-11 09:03:24 +02:00
|
|
|
{
|
2022-11-07 07:03:44 +01:00
|
|
|
#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL_ENABLED)
|
2023-01-11 14:50:10 +01:00
|
|
|
return mbedtls_ssl_conf_tls13_psk_ephemeral_enabled(ssl) &&
|
|
|
|
mbedtls_ssl_tls13_psk_ephemeral_enabled(ssl) &&
|
|
|
|
ssl_tls13_client_hello_has_exts_for_psk_ephemeral_key_exchange(ssl);
|
2022-07-11 09:03:24 +02:00
|
|
|
#else
|
|
|
|
((void) ssl);
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2022-07-11 09:03:24 +02:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
static int ssl_tls13_determine_key_exchange_mode(mbedtls_ssl_context *ssl)
|
2022-07-11 09:03:24 +02:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Determine the key exchange algorithm to use.
|
|
|
|
* There are three types of key exchanges supported in TLS 1.3:
|
|
|
|
* - (EC)DH with ECDSA,
|
|
|
|
* - (EC)DH with PSK,
|
|
|
|
* - plain PSK.
|
|
|
|
*
|
|
|
|
* The PSK-based key exchanges may additionally be used with 0-RTT.
|
|
|
|
*
|
|
|
|
* Our built-in order of preference is
|
2022-07-22 15:49:53 +02:00
|
|
|
* 1 ) (EC)DHE-PSK Mode ( psk_ephemeral )
|
|
|
|
* 2 ) Certificate Mode ( ephemeral )
|
|
|
|
* 3 ) Plain PSK Mode ( psk )
|
2022-07-11 09:03:24 +02:00
|
|
|
*/
|
|
|
|
|
2023-03-30 04:50:04 +02:00
|
|
|
ssl->handshake->key_exchange_mode =
|
|
|
|
MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_NONE;
|
2022-07-11 09:03:24 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl_tls13_check_psk_ephemeral_key_exchange(ssl)) {
|
2022-07-11 09:03:24 +02:00
|
|
|
ssl->handshake->key_exchange_mode =
|
|
|
|
MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL;
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(2, ("key exchange mode: psk_ephemeral"));
|
|
|
|
} else
|
|
|
|
if (ssl_tls13_check_ephemeral_key_exchange(ssl)) {
|
2022-07-11 09:03:24 +02:00
|
|
|
ssl->handshake->key_exchange_mode =
|
|
|
|
MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL;
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(2, ("key exchange mode: ephemeral"));
|
|
|
|
} else
|
|
|
|
if (ssl_tls13_check_psk_key_exchange(ssl)) {
|
2022-07-22 15:49:53 +02:00
|
|
|
ssl->handshake->key_exchange_mode =
|
|
|
|
MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK;
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(2, ("key exchange mode: psk"));
|
|
|
|
} else {
|
2022-07-11 09:03:24 +02:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(
|
2023-01-11 14:50:10 +01:00
|
|
|
1,
|
|
|
|
("ClientHello message misses mandatory extensions."));
|
|
|
|
MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_MISSING_EXTENSION,
|
|
|
|
MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
|
|
|
|
return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
|
2022-07-11 09:03:24 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2022-02-15 11:04:37 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-06-10 15:25:22 +02:00
|
|
|
#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
|
2022-10-04 16:14:26 +02:00
|
|
|
defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
|
2022-09-15 17:34:42 +02:00
|
|
|
|
|
|
|
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
2023-01-11 14:50:10 +01:00
|
|
|
static psa_algorithm_t ssl_tls13_iana_sig_alg_to_psa_alg(uint16_t sig_alg)
|
2022-09-15 17:34:42 +02:00
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
switch (sig_alg) {
|
2022-09-15 17:34:42 +02:00
|
|
|
case MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256:
|
2023-01-11 14:50:10 +01:00
|
|
|
return PSA_ALG_ECDSA(PSA_ALG_SHA_256);
|
2022-09-15 17:34:42 +02:00
|
|
|
case MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384:
|
2023-01-11 14:50:10 +01:00
|
|
|
return PSA_ALG_ECDSA(PSA_ALG_SHA_384);
|
2022-09-15 17:34:42 +02:00
|
|
|
case MBEDTLS_TLS1_3_SIG_ECDSA_SECP521R1_SHA512:
|
2023-01-11 14:50:10 +01:00
|
|
|
return PSA_ALG_ECDSA(PSA_ALG_SHA_512);
|
2022-09-15 17:34:42 +02:00
|
|
|
case MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256:
|
2023-01-11 14:50:10 +01:00
|
|
|
return PSA_ALG_RSA_PSS(PSA_ALG_SHA_256);
|
2022-09-15 17:34:42 +02:00
|
|
|
case MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384:
|
2023-01-11 14:50:10 +01:00
|
|
|
return PSA_ALG_RSA_PSS(PSA_ALG_SHA_384);
|
2022-09-15 17:34:42 +02:00
|
|
|
case MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512:
|
2023-01-11 14:50:10 +01:00
|
|
|
return PSA_ALG_RSA_PSS(PSA_ALG_SHA_512);
|
2022-09-15 17:34:42 +02:00
|
|
|
case MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256:
|
2023-01-11 14:50:10 +01:00
|
|
|
return PSA_ALG_RSA_PKCS1V15_SIGN(PSA_ALG_SHA_256);
|
2022-09-15 17:34:42 +02:00
|
|
|
case MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA384:
|
2023-01-11 14:50:10 +01:00
|
|
|
return PSA_ALG_RSA_PKCS1V15_SIGN(PSA_ALG_SHA_384);
|
2022-09-15 17:34:42 +02:00
|
|
|
case MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA512:
|
2023-01-11 14:50:10 +01:00
|
|
|
return PSA_ALG_RSA_PKCS1V15_SIGN(PSA_ALG_SHA_512);
|
2022-09-15 17:34:42 +02:00
|
|
|
default:
|
2023-01-11 14:50:10 +01:00
|
|
|
return PSA_ALG_NONE;
|
2022-09-15 17:34:42 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
|
|
|
|
2022-06-07 04:04:34 +02:00
|
|
|
/*
|
2022-06-15 05:57:21 +02:00
|
|
|
* Pick best ( private key, certificate chain ) pair based on the signature
|
|
|
|
* algorithms supported by the client.
|
2022-06-07 04:04:34 +02:00
|
|
|
*/
|
2022-07-08 18:56:49 +02:00
|
|
|
MBEDTLS_CHECK_RETURN_CRITICAL
|
2023-01-11 14:50:10 +01:00
|
|
|
static int ssl_tls13_pick_key_cert(mbedtls_ssl_context *ssl)
|
2022-06-07 04:04:34 +02:00
|
|
|
{
|
2022-06-15 05:57:21 +02:00
|
|
|
mbedtls_ssl_key_cert *key_cert, *key_cert_list;
|
2022-06-10 15:25:22 +02:00
|
|
|
const uint16_t *sig_alg = ssl->handshake->received_sig_algs;
|
2022-06-07 04:04:34 +02:00
|
|
|
|
|
|
|
#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->handshake->sni_key_cert != NULL) {
|
2022-06-15 05:57:21 +02:00
|
|
|
key_cert_list = ssl->handshake->sni_key_cert;
|
2023-01-11 14:50:10 +01:00
|
|
|
} else
|
2022-06-07 04:04:34 +02:00
|
|
|
#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
|
2023-01-11 14:50:10 +01:00
|
|
|
key_cert_list = ssl->conf->key_cert;
|
2022-06-07 04:04:34 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (key_cert_list == NULL) {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(3, ("server has no certificate"));
|
|
|
|
return -1;
|
2022-06-07 04:04:34 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
for (; *sig_alg != MBEDTLS_TLS1_3_SIG_NONE; sig_alg++) {
|
|
|
|
if (!mbedtls_ssl_sig_alg_is_offered(ssl, *sig_alg)) {
|
2022-09-15 17:34:42 +02:00
|
|
|
continue;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2022-09-15 17:34:42 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (!mbedtls_ssl_tls13_sig_alg_for_cert_verify_is_supported(*sig_alg)) {
|
2022-09-15 17:34:42 +02:00
|
|
|
continue;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2022-09-15 17:34:42 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
for (key_cert = key_cert_list; key_cert != NULL;
|
|
|
|
key_cert = key_cert->next) {
|
2022-09-15 17:34:42 +02:00
|
|
|
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
|
|
|
psa_algorithm_t psa_alg = PSA_ALG_NONE;
|
|
|
|
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_CRT(3, "certificate (chain) candidate",
|
|
|
|
key_cert->cert);
|
2022-06-10 15:25:22 +02:00
|
|
|
|
|
|
|
/*
|
2023-01-11 14:50:10 +01:00
|
|
|
* This avoids sending the client a cert it'll reject based on
|
|
|
|
* keyUsage or other extensions.
|
|
|
|
*/
|
|
|
|
if (mbedtls_x509_crt_check_key_usage(
|
|
|
|
key_cert->cert, MBEDTLS_X509_KU_DIGITAL_SIGNATURE) != 0 ||
|
2022-06-10 15:25:22 +02:00
|
|
|
mbedtls_x509_crt_check_extended_key_usage(
|
2022-06-15 05:57:21 +02:00
|
|
|
key_cert->cert, MBEDTLS_OID_SERVER_AUTH,
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_OID_SIZE(MBEDTLS_OID_SERVER_AUTH)) != 0) {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(3, ("certificate mismatch: "
|
|
|
|
"(extended) key usage extension"));
|
2022-06-10 15:25:22 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(3,
|
|
|
|
("ssl_tls13_pick_key_cert:"
|
|
|
|
"check signature algorithm %s [%04x]",
|
|
|
|
mbedtls_ssl_sig_alg_to_str(*sig_alg),
|
|
|
|
*sig_alg));
|
2022-09-15 17:34:42 +02:00
|
|
|
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
2023-01-11 14:50:10 +01:00
|
|
|
psa_alg = ssl_tls13_iana_sig_alg_to_psa_alg(*sig_alg);
|
2022-09-15 17:34:42 +02:00
|
|
|
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (mbedtls_ssl_tls13_check_sig_alg_cert_key_match(
|
|
|
|
*sig_alg, &key_cert->cert->pk)
|
2022-09-15 17:34:42 +02:00
|
|
|
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
|
|
|
&& psa_alg != PSA_ALG_NONE &&
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_pk_can_do_ext(&key_cert->cert->pk, psa_alg,
|
|
|
|
PSA_KEY_USAGE_SIGN_HASH) == 1
|
2022-09-15 17:34:42 +02:00
|
|
|
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
2023-01-11 14:50:10 +01:00
|
|
|
) {
|
2022-06-15 05:57:21 +02:00
|
|
|
ssl->handshake->key_cert = key_cert;
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(3,
|
|
|
|
("ssl_tls13_pick_key_cert:"
|
|
|
|
"selected signature algorithm"
|
|
|
|
" %s [%04x]",
|
|
|
|
mbedtls_ssl_sig_alg_to_str(*sig_alg),
|
|
|
|
*sig_alg));
|
2022-06-15 05:57:21 +02:00
|
|
|
MBEDTLS_SSL_DEBUG_CRT(
|
2023-01-11 14:50:10 +01:00
|
|
|
3, "selected certificate (chain)",
|
|
|
|
ssl->handshake->key_cert->cert);
|
|
|
|
return 0;
|
2022-06-10 15:25:22 +02:00
|
|
|
}
|
|
|
|
}
|
2022-06-07 04:04:34 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(2, ("ssl_tls13_pick_key_cert:"
|
|
|
|
"no suitable certificate found"));
|
|
|
|
return -1;
|
2022-06-07 04:04:34 +02:00
|
|
|
}
|
2022-06-10 15:25:22 +02:00
|
|
|
#endif /* MBEDTLS_X509_CRT_PARSE_C &&
|
2022-10-04 16:14:26 +02:00
|
|
|
MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
|
2022-06-07 04:04:34 +02:00
|
|
|
|
2022-04-13 10:21:05 +02:00
|
|
|
/*
|
|
|
|
*
|
|
|
|
* STATE HANDLING: ClientHello
|
|
|
|
*
|
2022-04-19 02:02:38 +02:00
|
|
|
* There are three possible classes of outcomes when parsing the ClientHello:
|
2022-04-13 10:21:05 +02:00
|
|
|
*
|
2022-04-19 02:02:38 +02:00
|
|
|
* 1) The ClientHello was well-formed and matched the server's configuration.
|
2022-04-13 10:21:05 +02:00
|
|
|
*
|
|
|
|
* In this case, the server progresses to sending its ServerHello.
|
|
|
|
*
|
2022-04-19 02:02:38 +02:00
|
|
|
* 2) The ClientHello was well-formed but didn't match the server's
|
|
|
|
* configuration.
|
2022-04-13 10:21:05 +02:00
|
|
|
*
|
|
|
|
* For example, the client might not have offered a key share which
|
|
|
|
* the server supports, or the server might require a cookie.
|
|
|
|
*
|
|
|
|
* In this case, the server sends a HelloRetryRequest.
|
|
|
|
*
|
2022-04-19 02:02:38 +02:00
|
|
|
* 3) The ClientHello was ill-formed
|
2022-04-13 10:21:05 +02:00
|
|
|
*
|
|
|
|
* In this case, we abort the handshake.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2022-04-11 11:55:18 +02:00
|
|
|
/*
|
|
|
|
* Structure of this message:
|
|
|
|
*
|
2022-04-22 04:34:40 +02:00
|
|
|
* uint16 ProtocolVersion;
|
|
|
|
* opaque Random[32];
|
|
|
|
* uint8 CipherSuite[2]; // Cryptographic suite selector
|
2022-04-11 11:55:18 +02:00
|
|
|
*
|
2022-04-22 04:34:40 +02:00
|
|
|
* struct {
|
|
|
|
* ProtocolVersion legacy_version = 0x0303; // TLS v1.2
|
|
|
|
* Random random;
|
|
|
|
* opaque legacy_session_id<0..32>;
|
|
|
|
* CipherSuite cipher_suites<2..2^16-2>;
|
|
|
|
* opaque legacy_compression_methods<1..2^8-1>;
|
|
|
|
* Extension extensions<8..2^16-1>;
|
|
|
|
* } ClientHello;
|
2022-04-11 11:55:18 +02:00
|
|
|
*/
|
2022-04-13 10:21:05 +02:00
|
|
|
|
|
|
|
#define SSL_CLIENT_HELLO_OK 0
|
|
|
|
#define SSL_CLIENT_HELLO_HRR_REQUIRED 1
|
2023-03-07 20:46:59 +01:00
|
|
|
#define SSL_CLIENT_HELLO_TLS1_2 2
|
2022-04-13 10:21:05 +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_client_hello(mbedtls_ssl_context *ssl,
|
|
|
|
const unsigned char *buf,
|
|
|
|
const unsigned char *end)
|
2022-02-15 11:04:37 +01:00
|
|
|
{
|
2022-04-19 02:02:38 +02:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
|
|
|
const unsigned char *p = buf;
|
2023-03-07 09:41:48 +01:00
|
|
|
const unsigned char *random;
|
2022-04-11 11:55:18 +02:00
|
|
|
size_t legacy_session_id_len;
|
2023-03-07 09:51:39 +01:00
|
|
|
const unsigned char *legacy_session_id;
|
2022-04-20 11:43:51 +02:00
|
|
|
size_t cipher_suites_len;
|
2023-03-07 10:07:32 +01:00
|
|
|
const unsigned char *cipher_suites;
|
2022-04-21 11:24:56 +02:00
|
|
|
const unsigned char *cipher_suites_end;
|
2022-04-19 02:02:38 +02:00
|
|
|
size_t extensions_len;
|
2022-02-15 11:04:37 +01:00
|
|
|
const unsigned char *extensions_end;
|
2023-04-03 17:36:31 +02:00
|
|
|
const unsigned char *supported_versions_data;
|
|
|
|
const unsigned char *supported_versions_data_end;
|
2022-10-29 03:08:47 +02:00
|
|
|
mbedtls_ssl_handshake_params *handshake = ssl->handshake;
|
2022-05-05 05:05:22 +02:00
|
|
|
int hrr_required = 0;
|
2023-06-14 17:59:29 +02:00
|
|
|
int no_usable_share_for_key_agreement = 0;
|
2022-02-15 11:04:37 +01:00
|
|
|
|
2022-10-04 16:38:25 +02:00
|
|
|
#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
|
2022-08-23 11:52:45 +02:00
|
|
|
const unsigned char *pre_shared_key_ext = NULL;
|
2022-07-10 08:32:38 +02:00
|
|
|
const unsigned char *pre_shared_key_ext_end = NULL;
|
2022-10-04 16:38:25 +02:00
|
|
|
#endif
|
2022-02-15 11:04:37 +01:00
|
|
|
|
|
|
|
/*
|
2022-04-19 02:02:38 +02:00
|
|
|
* ClientHello layout:
|
2022-02-15 11:04:37 +01:00
|
|
|
* 0 . 1 protocol version
|
2022-04-22 04:34:40 +02:00
|
|
|
* 2 . 33 random bytes
|
2022-04-02 05:34:37 +02:00
|
|
|
* 34 . 34 session id length ( 1 byte )
|
2022-02-15 11:04:37 +01:00
|
|
|
* 35 . 34+x session id
|
|
|
|
* .. . .. ciphersuite list length ( 2 bytes )
|
|
|
|
* .. . .. ciphersuite list
|
|
|
|
* .. . .. compression alg. list length ( 1 byte )
|
|
|
|
* .. . .. compression alg. list
|
|
|
|
* .. . .. extensions length ( 2 bytes, optional )
|
|
|
|
* .. . .. extensions ( optional )
|
|
|
|
*/
|
|
|
|
|
2022-04-19 02:02:38 +02:00
|
|
|
/*
|
2022-05-18 01:30:44 +02:00
|
|
|
* Minimal length ( with everything empty and extensions omitted ) is
|
2022-02-15 11:04:37 +01:00
|
|
|
* 2 + 32 + 1 + 2 + 1 = 38 bytes. Check that first, so that we can
|
|
|
|
* read at least up to session id length without worrying.
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 38);
|
2022-02-15 11:04:37 +01:00
|
|
|
|
|
|
|
/* ...
|
|
|
|
* ProtocolVersion legacy_version = 0x0303; // TLS 1.2
|
|
|
|
* ...
|
|
|
|
* 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);
|
|
|
|
return MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION;
|
2022-02-15 11:04:37 +01:00
|
|
|
}
|
|
|
|
p += 2;
|
|
|
|
|
2022-04-23 10:11:39 +02:00
|
|
|
/* ...
|
|
|
|
* Random random;
|
|
|
|
* ...
|
2022-04-19 02:02:38 +02:00
|
|
|
* with Random defined as:
|
|
|
|
* opaque Random[32];
|
2022-02-15 11:04:37 +01:00
|
|
|
*/
|
2023-03-07 09:41:48 +01:00
|
|
|
random = p;
|
2022-04-20 09:16:41 +02:00
|
|
|
p += MBEDTLS_CLIENT_HELLO_RANDOM_LEN;
|
2022-02-15 11:04:37 +01:00
|
|
|
|
2022-04-23 10:11:39 +02:00
|
|
|
/* ...
|
2022-04-19 02:02:38 +02:00
|
|
|
* opaque legacy_session_id<0..32>;
|
2022-04-23 10:11:39 +02:00
|
|
|
* ...
|
2022-02-15 11:04:37 +01:00
|
|
|
*/
|
2023-03-07 09:51:39 +01:00
|
|
|
legacy_session_id_len = *(p++);
|
|
|
|
legacy_session_id = p;
|
2022-02-15 11:04:37 +01:00
|
|
|
|
2022-04-19 02:02:38 +02:00
|
|
|
/*
|
|
|
|
* Check we have enough data for the legacy session identifier
|
2022-07-26 09:48:20 +02:00
|
|
|
* and the ciphersuite list length.
|
2022-04-19 02:02:38 +02:00
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, legacy_session_id_len + 2);
|
2022-04-11 11:55:18 +02:00
|
|
|
p += legacy_session_id_len;
|
2022-02-15 11:04:37 +01:00
|
|
|
|
2023-03-07 10:07:32 +01:00
|
|
|
/* ...
|
|
|
|
* CipherSuite cipher_suites<2..2^16-2>;
|
|
|
|
* ...
|
|
|
|
* with CipherSuite defined as:
|
|
|
|
* uint8 CipherSuite[2];
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
cipher_suites_len = MBEDTLS_GET_UINT16_BE(p, 0);
|
2022-02-15 11:04:37 +01:00
|
|
|
p += 2;
|
2023-03-07 10:07:32 +01:00
|
|
|
cipher_suites = p;
|
2022-02-15 11:04:37 +01:00
|
|
|
|
2023-02-16 15:32:19 +01:00
|
|
|
/*
|
|
|
|
* The length of the ciphersuite list has to be even.
|
|
|
|
*/
|
|
|
|
if (cipher_suites_len & 1) {
|
|
|
|
MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
|
|
|
|
MBEDTLS_ERR_SSL_DECODE_ERROR);
|
|
|
|
return MBEDTLS_ERR_SSL_DECODE_ERROR;
|
|
|
|
}
|
|
|
|
|
2022-04-19 02:02:38 +02:00
|
|
|
/* Check we have enough data for the ciphersuite list, the legacy
|
|
|
|
* compression methods and the length of the extensions.
|
2022-07-26 09:48:20 +02:00
|
|
|
*
|
|
|
|
* cipher_suites cipher_suites_len bytes
|
|
|
|
* legacy_compression_methods 2 bytes
|
|
|
|
* extensions_len 2 bytes
|
2022-04-19 02:02:38 +02:00
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, cipher_suites_len + 2 + 2);
|
2023-03-07 15:47:47 +01:00
|
|
|
p += cipher_suites_len;
|
|
|
|
cipher_suites_end = p;
|
2022-08-21 11:27:16 +02:00
|
|
|
|
2023-03-07 09:21:40 +01:00
|
|
|
/*
|
2023-03-07 15:47:47 +01:00
|
|
|
* Search for the supported versions extension and parse it to determine
|
|
|
|
* if the client supports TLS 1.3.
|
|
|
|
*/
|
|
|
|
ret = mbedtls_ssl_tls13_is_supported_versions_ext_present_in_exts(
|
|
|
|
ssl, p + 2, end,
|
2023-04-03 17:36:31 +02:00
|
|
|
&supported_versions_data, &supported_versions_data_end);
|
2023-03-07 15:47:47 +01:00
|
|
|
if (ret < 0) {
|
|
|
|
MBEDTLS_SSL_DEBUG_RET(1,
|
|
|
|
("mbedtls_ssl_tls13_is_supported_versions_ext_present_in_exts"), ret);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ret == 0) {
|
2023-03-07 20:46:59 +01:00
|
|
|
return SSL_CLIENT_HELLO_TLS1_2;
|
2023-03-07 15:47:47 +01:00
|
|
|
}
|
|
|
|
|
2023-03-07 20:46:59 +01:00
|
|
|
if (ret == 1) {
|
|
|
|
ret = ssl_tls13_parse_supported_versions_ext(ssl,
|
2023-04-03 17:36:31 +02:00
|
|
|
supported_versions_data,
|
|
|
|
supported_versions_data_end);
|
2023-03-07 20:46:59 +01:00
|
|
|
if (ret < 0) {
|
|
|
|
MBEDTLS_SSL_DEBUG_RET(1,
|
|
|
|
("ssl_tls13_parse_supported_versions_ext"), ret);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2023-04-03 16:37:22 +02:00
|
|
|
/*
|
|
|
|
* The supported versions extension was parsed successfully as the
|
|
|
|
* value returned by ssl_tls13_parse_supported_versions_ext() is
|
|
|
|
* positive. The return value is then equal to
|
|
|
|
* MBEDTLS_SSL_VERSION_TLS1_2 or MBEDTLS_SSL_VERSION_TLS1_3, defining
|
|
|
|
* the TLS version to negotiate.
|
|
|
|
*/
|
2023-03-07 20:46:59 +01:00
|
|
|
if (MBEDTLS_SSL_VERSION_TLS1_2 == ret) {
|
|
|
|
return SSL_CLIENT_HELLO_TLS1_2;
|
|
|
|
}
|
2023-03-07 15:47:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We negotiate TLS 1.3.
|
2023-03-07 09:21:40 +01:00
|
|
|
*/
|
|
|
|
ssl->tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_SSL_SESSION_TICKETS)
|
|
|
|
/* Store minor version for later use with ticket serialization. */
|
|
|
|
ssl->session_negotiate->tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
|
|
|
|
ssl->session_negotiate->endpoint = ssl->conf->endpoint;
|
|
|
|
#endif
|
|
|
|
|
2023-03-07 09:41:48 +01:00
|
|
|
/*
|
2023-04-06 09:57:52 +02:00
|
|
|
* We are negotiating the version 1.3 of the protocol. Do what we have
|
2023-03-07 09:51:39 +01:00
|
|
|
* postponed: copy of the client random bytes, copy of the legacy session
|
2023-03-07 10:07:32 +01:00
|
|
|
* identifier and selection of the TLS 1.3 cipher suite.
|
2023-03-07 09:41:48 +01:00
|
|
|
*/
|
|
|
|
MBEDTLS_SSL_DEBUG_BUF(3, "client hello, random bytes",
|
|
|
|
random, MBEDTLS_CLIENT_HELLO_RANDOM_LEN);
|
|
|
|
memcpy(&handshake->randbytes[0], random, MBEDTLS_CLIENT_HELLO_RANDOM_LEN);
|
|
|
|
|
2023-03-07 09:51:39 +01:00
|
|
|
if (legacy_session_id_len > sizeof(ssl->session_negotiate->id)) {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message"));
|
|
|
|
return MBEDTLS_ERR_SSL_DECODE_ERROR;
|
|
|
|
}
|
|
|
|
ssl->session_negotiate->id_len = legacy_session_id_len;
|
|
|
|
MBEDTLS_SSL_DEBUG_BUF(3, "client hello, session id",
|
|
|
|
legacy_session_id, legacy_session_id_len);
|
|
|
|
memcpy(&ssl->session_negotiate->id[0],
|
|
|
|
legacy_session_id, legacy_session_id_len);
|
|
|
|
|
2022-08-21 11:27:16 +02:00
|
|
|
/*
|
|
|
|
* Search for a matching ciphersuite
|
|
|
|
*/
|
2023-03-07 10:07:32 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_BUF(3, "client hello, list of cipher suites",
|
|
|
|
cipher_suites, cipher_suites_len);
|
2023-04-04 15:10:06 +02:00
|
|
|
for (const unsigned char *cipher_suites_p = cipher_suites;
|
|
|
|
cipher_suites_p < cipher_suites_end; cipher_suites_p += 2) {
|
2022-04-22 04:34:40 +02:00
|
|
|
uint16_t cipher_suite;
|
2023-01-11 14:50:10 +01:00
|
|
|
const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
|
2022-08-21 11:27:16 +02:00
|
|
|
|
2023-02-21 08:53:33 +01:00
|
|
|
/*
|
2023-04-04 15:10:06 +02:00
|
|
|
* "cipher_suites_end - cipher_suites_p is even" is an invariant of the
|
|
|
|
* loop. As cipher_suites_end - cipher_suites_p > 0, we have
|
|
|
|
* cipher_suites_end - cipher_suites_p >= 2 and it is thus safe to read
|
|
|
|
* two bytes.
|
2023-02-21 08:53:33 +01:00
|
|
|
*/
|
2023-04-04 15:10:06 +02:00
|
|
|
cipher_suite = MBEDTLS_GET_UINT16_BE(cipher_suites_p, 0);
|
2022-08-25 04:51:44 +02:00
|
|
|
ciphersuite_info = ssl_tls13_validate_peer_ciphersuite(
|
2023-01-11 14:50:10 +01:00
|
|
|
ssl, cipher_suite);
|
|
|
|
if (ciphersuite_info == NULL) {
|
2022-08-21 11:27:16 +02:00
|
|
|
continue;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2022-08-21 11:27:16 +02:00
|
|
|
|
|
|
|
ssl->session_negotiate->ciphersuite = cipher_suite;
|
2022-10-29 03:08:47 +02:00
|
|
|
handshake->ciphersuite_info = ciphersuite_info;
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(2, ("selected ciphersuite: %04x - %s",
|
|
|
|
cipher_suite,
|
|
|
|
ciphersuite_info->name));
|
2023-02-16 15:35:16 +01:00
|
|
|
break;
|
2022-08-21 11:27:16 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (handshake->ciphersuite_info == NULL) {
|
|
|
|
MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
|
|
|
|
MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
|
|
|
|
return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
|
2022-04-19 11:57:41 +02:00
|
|
|
}
|
2022-04-23 10:11:39 +02:00
|
|
|
|
2022-04-11 11:55:18 +02:00
|
|
|
/* ...
|
2022-04-19 02:02:38 +02:00
|
|
|
* opaque legacy_compression_methods<1..2^8-1>;
|
2022-04-11 11:55:18 +02:00
|
|
|
* ...
|
2022-02-15 11:04:37 +01:00
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
if (p[0] != 1 || p[1] != MBEDTLS_SSL_COMPRESS_NULL) {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(1, ("bad legacy compression method"));
|
|
|
|
MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
|
|
|
|
MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
|
|
|
|
return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
|
2022-02-15 11:04:37 +01:00
|
|
|
}
|
2022-04-19 02:02:38 +02:00
|
|
|
p += 2;
|
2022-02-15 11:04:37 +01:00
|
|
|
|
2022-04-23 10:11:39 +02:00
|
|
|
/* ...
|
2022-04-19 02:02:38 +02:00
|
|
|
* Extension extensions<8..2^16-1>;
|
2022-04-23 10:11:39 +02:00
|
|
|
* ...
|
2022-04-19 02:02:38 +02:00
|
|
|
* with Extension defined as:
|
|
|
|
* struct {
|
|
|
|
* ExtensionType extension_type;
|
|
|
|
* opaque extension_data<0..2^16-1>;
|
|
|
|
* } Extension;
|
2022-02-15 11:04:37 +01:00
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
extensions_len = MBEDTLS_GET_UINT16_BE(p, 0);
|
2022-02-15 11:04:37 +01:00
|
|
|
p += 2;
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, extensions_len);
|
2022-04-22 04:34:40 +02:00
|
|
|
extensions_end = p + extensions_len;
|
2022-02-15 11:04:37 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_BUF(3, "client hello extensions", p, extensions_len);
|
2022-10-31 06:38:40 +01:00
|
|
|
handshake->received_extensions = MBEDTLS_SSL_EXT_MASK_NONE;
|
2022-08-29 09:25:36 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
while (p < extensions_end) {
|
2022-02-15 11:04:37 +01:00
|
|
|
unsigned int extension_type;
|
|
|
|
size_t extension_data_len;
|
|
|
|
const unsigned char *extension_data_end;
|
|
|
|
|
2022-08-29 09:25:36 +02:00
|
|
|
/* RFC 8446, section 4.2.11
|
2022-07-21 06:37:39 +02:00
|
|
|
*
|
|
|
|
* The "pre_shared_key" extension MUST be the last extension in the
|
|
|
|
* ClientHello (this facilitates implementation as described below).
|
|
|
|
* Servers MUST check that it is the last extension and otherwise fail
|
|
|
|
* the handshake with an "illegal_parameter" alert.
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
if (handshake->received_extensions & MBEDTLS_SSL_EXT_MASK(PRE_SHARED_KEY)) {
|
2022-07-21 06:37:39 +02:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(
|
2023-01-11 14:50:10 +01:00
|
|
|
3, ("pre_shared_key is not last extension."));
|
2022-07-21 06:37:39 +02:00
|
|
|
MBEDTLS_SSL_PEND_FATAL_ALERT(
|
|
|
|
MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
|
|
|
|
return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
|
2022-07-21 06:37:39 +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);
|
2022-02-15 11:04:37 +01:00
|
|
|
p += 4;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_CHK_BUF_READ_PTR(p, extensions_end, extension_data_len);
|
2022-02-15 11:04:37 +01:00
|
|
|
extension_data_end = p + extension_data_len;
|
|
|
|
|
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_CLIENT_HELLO, extension_type,
|
|
|
|
MBEDTLS_SSL_TLS1_3_ALLOWED_EXTS_OF_CH);
|
|
|
|
if (ret != 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
2022-08-04 10:29:22 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
switch (extension_type) {
|
2022-05-07 11:02:40 +02:00
|
|
|
#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
|
|
|
|
case MBEDTLS_TLS_EXT_SERVERNAME:
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(3, ("found ServerName extension"));
|
|
|
|
ret = mbedtls_ssl_parse_server_name_ext(ssl, p,
|
|
|
|
extension_data_end);
|
|
|
|
if (ret != 0) {
|
2022-05-07 11:02:40 +02:00
|
|
|
MBEDTLS_SSL_DEBUG_RET(
|
2023-01-11 14:50:10 +01:00
|
|
|
1, "mbedtls_ssl_parse_servername_ext", ret);
|
|
|
|
return ret;
|
2022-05-07 11:02:40 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
|
|
|
|
|
2023-06-15 16:48:19 +02:00
|
|
|
#if defined(PSA_WANT_ALG_ECDH) || defined(PSA_WANT_ALG_FFDH)
|
2022-02-15 11:04:37 +01:00
|
|
|
case MBEDTLS_TLS_EXT_SUPPORTED_GROUPS:
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(3, ("found supported group extension"));
|
2022-02-15 11:04:37 +01:00
|
|
|
|
|
|
|
/* Supported Groups Extension
|
|
|
|
*
|
|
|
|
* When sent by the client, the "supported_groups" extension
|
|
|
|
* indicates the named groups which the client supports,
|
|
|
|
* ordered from most preferred to least preferred.
|
|
|
|
*/
|
2022-04-23 10:11:39 +02:00
|
|
|
ret = ssl_tls13_parse_supported_groups_ext(
|
2023-01-11 14:50:10 +01:00
|
|
|
ssl, p, extension_data_end);
|
|
|
|
if (ret != 0) {
|
2023-03-30 04:50:04 +02:00
|
|
|
MBEDTLS_SSL_DEBUG_RET(
|
2023-04-04 12:15:48 +02:00
|
|
|
1, "ssl_tls13_parse_supported_groups_ext", ret);
|
2023-01-11 14:50:10 +01:00
|
|
|
return ret;
|
2022-02-15 11:04:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
2023-06-15 16:48:19 +02:00
|
|
|
#endif /* PSA_WANT_ALG_ECDH || PSA_WANT_ALG_FFDH*/
|
2022-02-15 11:04:37 +01:00
|
|
|
|
2023-07-25 11:23:50 +02:00
|
|
|
#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED)
|
2022-02-15 11:04:37 +01:00
|
|
|
case MBEDTLS_TLS_EXT_KEY_SHARE:
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(3, ("found key share extension"));
|
2022-02-15 11:04:37 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Key Share Extension
|
|
|
|
*
|
|
|
|
* When sent by the client, the "key_share" extension
|
|
|
|
* contains the endpoint's cryptographic parameters for
|
|
|
|
* ECDHE/DHE key establishment methods.
|
|
|
|
*/
|
2022-04-23 10:11:39 +02:00
|
|
|
ret = ssl_tls13_parse_key_shares_ext(
|
2023-01-11 14:50:10 +01:00
|
|
|
ssl, p, extension_data_end);
|
|
|
|
if (ret == SSL_TLS1_3_PARSE_KEY_SHARES_EXT_NO_MATCH) {
|
2023-06-14 17:59:29 +02:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(2, ("No usable share for key agreement."));
|
|
|
|
no_usable_share_for_key_agreement = 1;
|
2022-02-15 11:04:37 +01:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ret < 0) {
|
2022-04-22 15:59:01 +02:00
|
|
|
MBEDTLS_SSL_DEBUG_RET(
|
2023-01-11 14:50:10 +01:00
|
|
|
1, "ssl_tls13_parse_key_shares_ext", ret);
|
|
|
|
return ret;
|
2022-04-22 15:59:01 +02:00
|
|
|
}
|
2022-02-15 11:04:37 +01:00
|
|
|
|
|
|
|
break;
|
2023-07-25 11:23:50 +02:00
|
|
|
#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED */
|
2022-02-15 11:04:37 +01:00
|
|
|
|
|
|
|
case MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS:
|
2023-03-07 15:47:47 +01:00
|
|
|
/* Already parsed */
|
2022-02-15 11:04:37 +01:00
|
|
|
break;
|
|
|
|
|
2022-10-04 16:38:25 +02:00
|
|
|
#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
|
2022-07-08 14:04:51 +02:00
|
|
|
case MBEDTLS_TLS_EXT_PSK_KEY_EXCHANGE_MODES:
|
2023-03-30 04:50:04 +02:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(
|
|
|
|
3, ("found psk key exchange modes extension"));
|
2022-07-08 14:04:51 +02:00
|
|
|
|
|
|
|
ret = ssl_tls13_parse_key_exchange_modes_ext(
|
2023-01-11 14:50:10 +01:00
|
|
|
ssl, p, extension_data_end);
|
|
|
|
if (ret != 0) {
|
2022-07-08 14:04:51 +02:00
|
|
|
MBEDTLS_SSL_DEBUG_RET(
|
2023-01-11 14:50:10 +01:00
|
|
|
1, "ssl_tls13_parse_key_exchange_modes_ext", ret);
|
|
|
|
return ret;
|
2022-07-08 14:04:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
2022-10-04 16:38:25 +02:00
|
|
|
#endif
|
2022-07-08 14:04:51 +02:00
|
|
|
|
2022-07-10 08:32:38 +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"));
|
|
|
|
if ((handshake->received_extensions &
|
|
|
|
MBEDTLS_SSL_EXT_MASK(PSK_KEY_EXCHANGE_MODES)) == 0) {
|
2022-07-22 17:17:11 +02:00
|
|
|
MBEDTLS_SSL_PEND_FATAL_ALERT(
|
|
|
|
MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
|
|
|
|
return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
|
2022-07-22 17:17:11 +02:00
|
|
|
}
|
2022-10-04 16:38:25 +02:00
|
|
|
#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
|
2022-07-10 08:32:38 +02:00
|
|
|
/* Delay processing of the PSK identity once we have
|
|
|
|
* found out which algorithms to use. We keep a pointer
|
|
|
|
* to the buffer and the size for later processing.
|
|
|
|
*/
|
2022-08-23 11:52:45 +02:00
|
|
|
pre_shared_key_ext = p;
|
2022-07-10 08:32:38 +02:00
|
|
|
pre_shared_key_ext_end = extension_data_end;
|
2022-08-04 10:29:22 +02:00
|
|
|
#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED */
|
2022-07-10 08:32:38 +02:00
|
|
|
break;
|
|
|
|
|
2022-06-17 12:18:48 +02: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-06-17 12:18:48 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = mbedtls_ssl_parse_alpn_ext(ssl, p, extension_data_end);
|
|
|
|
if (ret != 0) {
|
2022-06-17 12:18:48 +02:00
|
|
|
MBEDTLS_SSL_DEBUG_RET(
|
2023-01-11 14:50:10 +01:00
|
|
|
1, ("mbedtls_ssl_parse_alpn_ext"), ret);
|
|
|
|
return ret;
|
2022-06-17 12:18:48 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
#endif /* MBEDTLS_SSL_ALPN */
|
|
|
|
|
2022-10-04 16:14:26 +02:00
|
|
|
#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
|
2022-02-15 11:04:37 +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"));
|
2022-02-15 11:04:37 +01:00
|
|
|
|
2022-04-27 21:17:56 +02:00
|
|
|
ret = mbedtls_ssl_parse_sig_alg_ext(
|
2023-01-11 14:50:10 +01:00
|
|
|
ssl, p, extension_data_end);
|
|
|
|
if (ret != 0) {
|
2023-04-06 04:31:02 +02:00
|
|
|
MBEDTLS_SSL_DEBUG_RET(
|
|
|
|
1, "mbedtls_ssl_parse_sig_alg_ext", ret);
|
2023-01-11 14:50:10 +01:00
|
|
|
return ret;
|
2022-02-15 11:04:37 +01:00
|
|
|
}
|
|
|
|
break;
|
2022-10-04 16:14:26 +02:00
|
|
|
#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
|
2022-02-15 11:04:37 +01:00
|
|
|
|
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-30 04:50:04 +02:00
|
|
|
ret = mbedtls_ssl_tls13_parse_record_size_limit_ext(
|
|
|
|
ssl, p, extension_data_end);
|
2023-02-10 12:45:19 +01:00
|
|
|
|
2023-04-04 12:18:38 +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 */
|
|
|
|
|
2022-02-15 11:04:37 +01:00
|
|
|
default:
|
2022-11-08 14:30:21 +01:00
|
|
|
MBEDTLS_SSL_PRINT_EXT(
|
2022-10-31 06:38:40 +01:00
|
|
|
3, MBEDTLS_SSL_HS_CLIENT_HELLO,
|
2023-01-11 14:50:10 +01:00
|
|
|
extension_type, "( ignored )");
|
2022-08-29 09:25:36 +02:00
|
|
|
break;
|
2022-02-15 11:04:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
p += extension_data_len;
|
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_PRINT_EXTS(3, MBEDTLS_SSL_HS_CLIENT_HELLO,
|
|
|
|
handshake->received_extensions);
|
2022-08-04 10:29:22 +02:00
|
|
|
|
2023-02-06 00:34:21 +01:00
|
|
|
ret = mbedtls_ssl_add_hs_hdr_to_checksum(ssl,
|
|
|
|
MBEDTLS_SSL_HS_CLIENT_HELLO,
|
|
|
|
p - buf);
|
|
|
|
if (0 != ret) {
|
|
|
|
MBEDTLS_SSL_DEBUG_RET(1, ("mbedtls_ssl_add_hs_hdr_to_checksum"), ret);
|
|
|
|
return ret;
|
|
|
|
}
|
2022-07-11 08:10:03 +02:00
|
|
|
|
2022-10-04 16:38:25 +02:00
|
|
|
#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
|
2022-02-15 11:04:37 +01:00
|
|
|
/* Update checksum with either
|
|
|
|
* - The entire content of the CH message, if no PSK extension is present
|
|
|
|
* - The content up to but excluding the PSK extension, if present.
|
|
|
|
*/
|
2022-07-10 08:32:38 +02:00
|
|
|
/* If we've settled on a PSK-based exchange, parse PSK identity ext */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (mbedtls_ssl_tls13_some_psk_enabled(ssl) &&
|
|
|
|
mbedtls_ssl_conf_tls13_some_psk_enabled(ssl) &&
|
|
|
|
(handshake->received_extensions & MBEDTLS_SSL_EXT_MASK(PRE_SHARED_KEY))) {
|
2023-02-06 00:34:21 +01:00
|
|
|
ret = handshake->update_checksum(ssl, buf,
|
|
|
|
pre_shared_key_ext - buf);
|
|
|
|
if (0 != ret) {
|
|
|
|
MBEDTLS_SSL_DEBUG_RET(1, ("update_checksum"), ret);
|
|
|
|
return ret;
|
|
|
|
}
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = ssl_tls13_parse_pre_shared_key_ext(ssl,
|
|
|
|
pre_shared_key_ext,
|
|
|
|
pre_shared_key_ext_end,
|
|
|
|
cipher_suites,
|
|
|
|
cipher_suites_end);
|
|
|
|
if (ret == MBEDTLS_ERR_SSL_UNKNOWN_IDENTITY) {
|
|
|
|
handshake->received_extensions &= ~MBEDTLS_SSL_EXT_MASK(PRE_SHARED_KEY);
|
|
|
|
} else if (ret != 0) {
|
2022-10-31 06:38:40 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_RET(
|
2023-01-11 14:50:10 +01:00
|
|
|
1, "ssl_tls13_parse_pre_shared_key_ext", ret);
|
|
|
|
return ret;
|
2022-07-10 08:32:38 +02:00
|
|
|
}
|
2023-01-11 14:50:10 +01:00
|
|
|
} else
|
2022-10-04 16:38:25 +02:00
|
|
|
#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED */
|
2022-07-10 08:32:38 +02:00
|
|
|
{
|
2023-02-06 00:34:21 +01:00
|
|
|
ret = handshake->update_checksum(ssl, buf, p - buf);
|
|
|
|
if (0 != ret) {
|
|
|
|
MBEDTLS_SSL_DEBUG_RET(1, ("update_checksum"), ret);
|
|
|
|
return ret;
|
|
|
|
}
|
2022-07-10 08:32:38 +02:00
|
|
|
}
|
2022-02-15 11:04:37 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = ssl_tls13_determine_key_exchange_mode(ssl);
|
|
|
|
if (ret < 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
2022-02-15 11:04:37 +01:00
|
|
|
|
2023-06-14 17:59:29 +02:00
|
|
|
if (ssl->handshake->key_exchange_mode !=
|
|
|
|
MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK) {
|
|
|
|
hrr_required = (no_usable_share_for_key_agreement != 0);
|
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_ssl_optimize_checksum(ssl, handshake->ciphersuite_info);
|
2022-08-29 14:16:09 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return hrr_required ? SSL_CLIENT_HELLO_HRR_REQUIRED : SSL_CLIENT_HELLO_OK;
|
2022-06-15 11:42:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Update the handshake state machine */
|
|
|
|
|
2022-07-08 18:56:49 +02:00
|
|
|
MBEDTLS_CHECK_RETURN_CRITICAL
|
2023-01-11 14:50:10 +01:00
|
|
|
static int ssl_tls13_postprocess_client_hello(mbedtls_ssl_context *ssl)
|
2022-06-15 11:42:45 +02:00
|
|
|
{
|
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
|
|
|
|
2022-06-15 05:57:21 +02:00
|
|
|
/*
|
|
|
|
* Server certificate selection
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->conf->f_cert_cb && (ret = ssl->conf->f_cert_cb(ssl)) != 0) {
|
|
|
|
MBEDTLS_SSL_DEBUG_RET(1, "f_cert_cb", ret);
|
|
|
|
return ret;
|
2022-06-15 05:57:21 +02:00
|
|
|
}
|
|
|
|
#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
|
|
|
|
ssl->handshake->sni_name = NULL;
|
|
|
|
ssl->handshake->sni_name_len = 0;
|
|
|
|
#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
|
2022-02-15 11:04:37 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = mbedtls_ssl_tls13_key_schedule_stage_early(ssl);
|
|
|
|
if (ret != 0) {
|
|
|
|
MBEDTLS_SSL_DEBUG_RET(1,
|
|
|
|
"mbedtls_ssl_tls1_3_key_schedule_stage_early", ret);
|
|
|
|
return ret;
|
2022-02-15 11:04:37 +01:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2022-02-15 11:04:37 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-04-13 10:21:05 +02:00
|
|
|
/*
|
|
|
|
* Main entry point from the state machine; orchestrates the otherfunctions.
|
|
|
|
*/
|
|
|
|
|
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_client_hello(mbedtls_ssl_context *ssl)
|
2022-04-13 10:21:05 +02:00
|
|
|
{
|
|
|
|
|
2022-04-20 09:16:41 +02:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2023-01-11 14:50:10 +01:00
|
|
|
unsigned char *buf = NULL;
|
2022-04-13 10:21:05 +02:00
|
|
|
size_t buflen = 0;
|
2022-05-09 09:50:57 +02:00
|
|
|
int parse_client_hello_ret;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse client hello"));
|
2022-04-13 10:21:05 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_tls13_fetch_handshake_msg(
|
|
|
|
ssl, MBEDTLS_SSL_HS_CLIENT_HELLO,
|
|
|
|
&buf, &buflen));
|
2022-04-13 10:21:05 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_PROC_CHK_NEG(ssl_tls13_parse_client_hello(ssl, buf,
|
|
|
|
buf + buflen));
|
2023-03-07 20:46:59 +01:00
|
|
|
parse_client_hello_ret = ret; /* Store positive return value of
|
|
|
|
* parse_client_hello,
|
|
|
|
* as negative error codes are handled
|
2022-05-09 16:20:30 +02:00
|
|
|
* by MBEDTLS_SSL_PROC_CHK_NEG. */
|
2022-05-07 04:50:12 +02:00
|
|
|
|
2023-04-03 16:37:22 +02:00
|
|
|
/*
|
|
|
|
* Version 1.2 of the protocol has been chosen, set the
|
|
|
|
* ssl->keep_current_message flag for the ClientHello to be kept and parsed
|
|
|
|
* as a TLS 1.2 ClientHello. 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.
|
|
|
|
*/
|
2023-03-07 20:46:59 +01:00
|
|
|
if (SSL_CLIENT_HELLO_TLS1_2 == parse_client_hello_ret) {
|
|
|
|
ssl->keep_current_message = 1;
|
|
|
|
ssl->tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_PROC_CHK(ssl_tls13_postprocess_client_hello(ssl));
|
2022-05-07 04:50:12 +02:00
|
|
|
|
2023-03-07 20:46:59 +01:00
|
|
|
if (SSL_CLIENT_HELLO_OK == parse_client_hello_ret) {
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_HELLO);
|
|
|
|
} else {
|
|
|
|
mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HELLO_RETRY_REQUEST);
|
|
|
|
}
|
2022-04-13 10:21:05 +02:00
|
|
|
|
|
|
|
cleanup:
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse client hello"));
|
|
|
|
return ret;
|
2022-04-13 10:21:05 +02:00
|
|
|
}
|
|
|
|
|
2022-03-30 11:15:02 +02:00
|
|
|
/*
|
2022-04-20 15:23:40 +02:00
|
|
|
* Handler for MBEDTLS_SSL_SERVER_HELLO
|
2022-03-30 11:15:02 +02:00
|
|
|
*/
|
2022-06-17 10:52:54 +02:00
|
|
|
MBEDTLS_CHECK_RETURN_CRITICAL
|
2023-01-11 14:50:10 +01:00
|
|
|
static int ssl_tls13_prepare_server_hello(mbedtls_ssl_context *ssl)
|
2022-03-30 11:32:21 +02:00
|
|
|
{
|
2022-04-20 15:37:58 +02:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
|
|
|
unsigned char *server_randbytes =
|
2023-01-11 14:50:10 +01:00
|
|
|
ssl->handshake->randbytes + MBEDTLS_CLIENT_HELLO_RANDOM_LEN;
|
|
|
|
if (ssl->conf->f_rng == NULL) {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(1, ("no RNG provided"));
|
|
|
|
return MBEDTLS_ERR_SSL_NO_RNG;
|
2022-03-30 11:32:21 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if ((ret = ssl->conf->f_rng(ssl->conf->p_rng, server_randbytes,
|
|
|
|
MBEDTLS_SERVER_HELLO_RANDOM_LEN)) != 0) {
|
|
|
|
MBEDTLS_SSL_DEBUG_RET(1, "f_rng", ret);
|
|
|
|
return ret;
|
2022-03-30 11:32:21 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_BUF(3, "server hello, random bytes", server_randbytes,
|
|
|
|
MBEDTLS_SERVER_HELLO_RANDOM_LEN);
|
2022-03-30 11:32:21 +02:00
|
|
|
|
|
|
|
#if defined(MBEDTLS_HAVE_TIME)
|
2023-05-22 21:08:12 +02:00
|
|
|
ssl->session_negotiate->start = mbedtls_time(NULL);
|
2022-03-30 11:32:21 +02:00
|
|
|
#endif /* MBEDTLS_HAVE_TIME */
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return ret;
|
2022-03-30 11:32:21 +02:00
|
|
|
}
|
|
|
|
|
2022-03-30 16:02:12 +02:00
|
|
|
/*
|
2022-04-21 03:23:16 +02:00
|
|
|
* ssl_tls13_write_server_hello_supported_versions_ext ():
|
2022-03-30 16:02:12 +02:00
|
|
|
*
|
|
|
|
* struct {
|
2022-04-06 04:08:34 +02:00
|
|
|
* ProtocolVersion selected_version;
|
2022-03-30 16:02:12 +02:00
|
|
|
* } SupportedVersions;
|
|
|
|
*/
|
2022-06-17 10:52:54 +02:00
|
|
|
MBEDTLS_CHECK_RETURN_CRITICAL
|
2022-04-21 03:23:16 +02:00
|
|
|
static int ssl_tls13_write_server_hello_supported_versions_ext(
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_ssl_context *ssl,
|
|
|
|
unsigned char *buf,
|
|
|
|
unsigned char *end,
|
|
|
|
size_t *out_len)
|
2022-03-30 16:02:12 +02:00
|
|
|
{
|
|
|
|
*out_len = 0;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, write selected version"));
|
2022-03-30 16:02:12 +02:00
|
|
|
|
|
|
|
/* Check if we have space to write the extension:
|
|
|
|
* - extension_type (2 bytes)
|
|
|
|
* - extension_data_length (2 bytes)
|
2022-04-14 14:52:56 +02:00
|
|
|
* - selected_version (2 bytes)
|
2022-03-30 16:02:12 +02:00
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_CHK_BUF_PTR(buf, end, 6);
|
2022-03-30 16:02:12 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS, buf, 0);
|
2022-03-30 16:02:12 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_PUT_UINT16_BE(2, buf, 2);
|
2022-03-30 16:02:12 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_ssl_write_version(buf + 4,
|
|
|
|
ssl->conf->transport,
|
|
|
|
ssl->tls_version);
|
2022-03-30 16:02:12 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(3, ("supported version: [%04x]",
|
|
|
|
ssl->tls_version));
|
2022-03-30 16:02:12 +02:00
|
|
|
|
2022-04-14 14:52:56 +02:00
|
|
|
*out_len = 6;
|
2022-03-30 16:02:12 +02:00
|
|
|
|
2022-11-08 14:19:34 +01:00
|
|
|
mbedtls_ssl_tls13_set_hs_sent_ext_mask(
|
2023-01-11 14:50:10 +01:00
|
|
|
ssl, MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS);
|
2022-11-08 14:19:34 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2022-03-30 16:02:12 +02:00
|
|
|
}
|
|
|
|
|
2022-04-20 16:28:09 +02:00
|
|
|
|
2022-03-30 16:02:12 +02:00
|
|
|
|
|
|
|
/* Generate and export a single key share. For hybrid KEMs, this can
|
|
|
|
* be called multiple times with the different components of the hybrid. */
|
2022-06-17 10:52:54 +02:00
|
|
|
MBEDTLS_CHECK_RETURN_CRITICAL
|
2023-01-11 14:50:10 +01:00
|
|
|
static int ssl_tls13_generate_and_write_key_share(mbedtls_ssl_context *ssl,
|
|
|
|
uint16_t named_group,
|
|
|
|
unsigned char *buf,
|
|
|
|
unsigned char *end,
|
|
|
|
size_t *out_len)
|
2022-03-30 16:02:12 +02:00
|
|
|
{
|
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2022-04-22 16:27:33 +02:00
|
|
|
|
|
|
|
*out_len = 0;
|
|
|
|
|
2023-07-25 11:23:50 +02:00
|
|
|
#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED)
|
2023-05-31 15:21:04 +02:00
|
|
|
if (mbedtls_ssl_tls13_named_group_is_ecdhe(named_group) ||
|
2023-06-29 09:08:43 +02:00
|
|
|
mbedtls_ssl_tls13_named_group_is_ffdh(named_group)) {
|
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, named_group, buf, end, out_len);
|
|
|
|
if (ret != 0) {
|
2022-03-30 16:43:29 +02:00
|
|
|
MBEDTLS_SSL_DEBUG_RET(
|
2023-07-06 11:26:44 +02:00
|
|
|
1, "mbedtls_ssl_tls13_generate_and_write_xxdh_key_exchange",
|
2023-01-11 14:50:10 +01:00
|
|
|
ret);
|
|
|
|
return ret;
|
2022-03-30 16:02:12 +02:00
|
|
|
}
|
2023-01-11 14:50:10 +01:00
|
|
|
} 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 kinds of KEMs */) {
|
|
|
|
} else {
|
2022-04-22 16:27:33 +02:00
|
|
|
((void) ssl);
|
|
|
|
((void) named_group);
|
|
|
|
((void) buf);
|
|
|
|
((void) end);
|
2022-03-30 16:02:12 +02:00
|
|
|
ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
|
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return ret;
|
2022-03-30 16:02:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* ssl_tls13_write_key_share_ext
|
|
|
|
*
|
|
|
|
* Structure of key_share extension in ServerHello:
|
|
|
|
*
|
2022-04-20 15:23:40 +02:00
|
|
|
* struct {
|
|
|
|
* NamedGroup group;
|
|
|
|
* opaque key_exchange<1..2^16-1>;
|
|
|
|
* } KeyShareEntry;
|
|
|
|
* struct {
|
|
|
|
* KeyShareEntry server_share;
|
|
|
|
* } KeyShareServerHello;
|
2022-03-30 16:02:12 +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)
|
2022-03-30 16:02:12 +02:00
|
|
|
{
|
2022-04-22 16:27:33 +02:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2022-03-30 16:02:12 +02:00
|
|
|
unsigned char *p = buf;
|
2022-04-20 15:50:42 +02:00
|
|
|
uint16_t group = ssl->handshake->offered_group_id;
|
2022-03-30 16:02:12 +02:00
|
|
|
unsigned char *server_share = buf + 4;
|
|
|
|
size_t key_exchange_length;
|
|
|
|
|
|
|
|
*out_len = 0;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, adding key share extension"));
|
2022-03-30 16:02:12 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(2, ("server hello, write selected_group: %s (%04x)",
|
|
|
|
mbedtls_ssl_named_group_to_str(group),
|
|
|
|
group));
|
2022-09-06 05:19:31 +02:00
|
|
|
|
2022-03-30 16:02:12 +02:00
|
|
|
/* Check if we have space for header and length fields:
|
|
|
|
* - extension_type (2 bytes)
|
|
|
|
* - extension_data_length (2 bytes)
|
|
|
|
* - group (2 bytes)
|
|
|
|
* - key_exchange_length (2 bytes)
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_CHK_BUF_PTR(p, end, 8);
|
|
|
|
MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_KEY_SHARE, p, 0);
|
|
|
|
MBEDTLS_PUT_UINT16_BE(group, server_share, 0);
|
2022-03-30 16:02:12 +02:00
|
|
|
p += 8;
|
2022-04-20 15:50:42 +02:00
|
|
|
|
2022-03-30 16:02:12 +02:00
|
|
|
/* When we introduce PQC-ECDHE hybrids, we'll want to call this
|
|
|
|
* function multiple times. */
|
2022-04-22 16:27:33 +02:00
|
|
|
ret = ssl_tls13_generate_and_write_key_share(
|
2023-01-11 14:50:10 +01:00
|
|
|
ssl, group, server_share + 4, end, &key_exchange_length);
|
|
|
|
if (ret != 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
2022-03-30 16:02:12 +02:00
|
|
|
p += key_exchange_length;
|
2022-04-23 10:11:39 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_PUT_UINT16_BE(key_exchange_length, server_share + 2, 0);
|
2022-03-30 16:02:12 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_PUT_UINT16_BE(p - server_share, buf, 2);
|
2022-03-30 16:02:12 +02:00
|
|
|
|
2022-04-20 15:50:42 +02:00
|
|
|
*out_len = p - buf;
|
2022-04-22 16:27:33 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_KEY_SHARE);
|
2022-11-08 14:19:34 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2022-03-30 16:02:12 +02:00
|
|
|
}
|
2022-04-20 16:28: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_write_hrr_key_share_ext(mbedtls_ssl_context *ssl,
|
|
|
|
unsigned char *buf,
|
|
|
|
unsigned char *end,
|
|
|
|
size_t *out_len)
|
2022-04-23 09:16:45 +02:00
|
|
|
{
|
|
|
|
uint16_t selected_group = ssl->handshake->hrr_selected_group;
|
|
|
|
/* key_share Extension
|
|
|
|
*
|
|
|
|
* struct {
|
|
|
|
* select (Handshake.msg_type) {
|
|
|
|
* ...
|
|
|
|
* case hello_retry_request:
|
|
|
|
* NamedGroup selected_group;
|
|
|
|
* ...
|
|
|
|
* };
|
|
|
|
* } KeyShare;
|
|
|
|
*/
|
|
|
|
|
|
|
|
*out_len = 0;
|
|
|
|
|
2022-05-05 05:10:08 +02:00
|
|
|
/*
|
|
|
|
* For a pure PSK key exchange, there is no group to agree upon. The purpose
|
|
|
|
* of the HRR is then to transmit a cookie to force the client to demonstrate
|
|
|
|
* reachability at their apparent network address (primarily useful for DTLS).
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
if (!mbedtls_ssl_tls13_key_exchange_mode_with_ephemeral(ssl)) {
|
|
|
|
return 0;
|
|
|
|
}
|
2022-04-23 09:16:45 +02:00
|
|
|
|
|
|
|
/* We should only send the key_share extension if the client's initial
|
|
|
|
* key share was not acceptable. */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->handshake->offered_group_id != 0) {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(4, ("Skip key_share extension in HRR"));
|
|
|
|
return 0;
|
2022-04-23 09:16:45 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (selected_group == 0) {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(1, ("no matching named group found"));
|
|
|
|
return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
|
2022-04-23 09:16:45 +02:00
|
|
|
}
|
|
|
|
|
2022-05-05 05:10:08 +02:00
|
|
|
/* Check if we have enough space:
|
|
|
|
* - extension_type (2 bytes)
|
|
|
|
* - extension_data_length (2 bytes)
|
|
|
|
* - selected_group (2 bytes)
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_CHK_BUF_PTR(buf, end, 6);
|
2022-04-23 09:16:45 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_KEY_SHARE, buf, 0);
|
|
|
|
MBEDTLS_PUT_UINT16_BE(2, buf, 2);
|
|
|
|
MBEDTLS_PUT_UINT16_BE(selected_group, buf, 4);
|
2022-04-23 09:16:45 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(3,
|
|
|
|
("HRR selected_group: %s (%x)",
|
|
|
|
mbedtls_ssl_named_group_to_str(selected_group),
|
|
|
|
selected_group));
|
2022-04-23 09:16:45 +02:00
|
|
|
|
|
|
|
*out_len = 6;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_KEY_SHARE);
|
2022-11-08 14:19:34 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2022-04-23 09:16:45 +02:00
|
|
|
}
|
2022-03-30 16:02:12 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Structure of ServerHello message:
|
|
|
|
*
|
|
|
|
* struct {
|
|
|
|
* ProtocolVersion legacy_version = 0x0303; // TLS v1.2
|
|
|
|
* Random random;
|
|
|
|
* opaque legacy_session_id_echo<0..32>;
|
|
|
|
* CipherSuite cipher_suite;
|
|
|
|
* uint8 legacy_compression_method = 0;
|
|
|
|
* Extension extensions<6..2^16-1>;
|
|
|
|
* } 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_write_server_hello_body(mbedtls_ssl_context *ssl,
|
|
|
|
unsigned char *buf,
|
|
|
|
unsigned char *end,
|
|
|
|
size_t *out_len,
|
|
|
|
int is_hrr)
|
2022-03-30 11:36:13 +02:00
|
|
|
{
|
2022-04-22 16:27:33 +02:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2022-03-30 16:02:12 +02:00
|
|
|
unsigned char *p = buf;
|
2022-04-20 16:28:09 +02:00
|
|
|
unsigned char *p_extensions_len;
|
2022-04-25 13:31:51 +02:00
|
|
|
size_t output_len;
|
2022-03-30 16:02:12 +02:00
|
|
|
|
|
|
|
*out_len = 0;
|
2022-10-31 07:45:01 +01:00
|
|
|
ssl->handshake->sent_extensions = MBEDTLS_SSL_EXT_MASK_NONE;
|
2022-03-30 16:02:12 +02:00
|
|
|
|
2022-04-21 03:31:58 +02:00
|
|
|
/* ...
|
|
|
|
* ProtocolVersion legacy_version = 0x0303; // TLS 1.2
|
|
|
|
* ...
|
|
|
|
* with ProtocolVersion defined as:
|
|
|
|
* uint16 ProtocolVersion;
|
2022-03-30 16:02:12 +02:00
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
|
|
|
|
MBEDTLS_PUT_UINT16_BE(0x0303, p, 0);
|
2022-03-30 16:02:12 +02:00
|
|
|
p += 2;
|
|
|
|
|
2022-04-20 15:23:40 +02:00
|
|
|
/* ...
|
|
|
|
* Random random;
|
|
|
|
* ...
|
2022-04-21 03:31:58 +02:00
|
|
|
* with Random defined as:
|
|
|
|
* opaque Random[MBEDTLS_SERVER_HELLO_RANDOM_LEN];
|
2022-04-20 15:23:40 +02:00
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_CHK_BUF_PTR(p, end, MBEDTLS_SERVER_HELLO_RANDOM_LEN);
|
|
|
|
if (is_hrr) {
|
|
|
|
memcpy(p, mbedtls_ssl_tls13_hello_retry_request_magic,
|
|
|
|
MBEDTLS_SERVER_HELLO_RANDOM_LEN);
|
|
|
|
} else {
|
|
|
|
memcpy(p, &ssl->handshake->randbytes[MBEDTLS_CLIENT_HELLO_RANDOM_LEN],
|
|
|
|
MBEDTLS_SERVER_HELLO_RANDOM_LEN);
|
|
|
|
}
|
|
|
|
MBEDTLS_SSL_DEBUG_BUF(3, "server hello, random bytes",
|
|
|
|
p, MBEDTLS_SERVER_HELLO_RANDOM_LEN);
|
2022-03-30 16:02:12 +02:00
|
|
|
p += MBEDTLS_SERVER_HELLO_RANDOM_LEN;
|
|
|
|
|
2022-04-21 03:31:58 +02:00
|
|
|
/* ...
|
|
|
|
* opaque legacy_session_id_echo<0..32>;
|
|
|
|
* ...
|
2022-03-30 16:02:12 +02:00
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_CHK_BUF_PTR(p, end, 1 + ssl->session_negotiate->id_len);
|
|
|
|
*p++ = (unsigned char) ssl->session_negotiate->id_len;
|
|
|
|
if (ssl->session_negotiate->id_len > 0) {
|
|
|
|
memcpy(p, &ssl->session_negotiate->id[0],
|
|
|
|
ssl->session_negotiate->id_len);
|
2022-03-30 16:02:12 +02:00
|
|
|
p += ssl->session_negotiate->id_len;
|
2022-04-22 16:27:33 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_BUF(3, "session id", ssl->session_negotiate->id,
|
|
|
|
ssl->session_negotiate->id_len);
|
2022-03-30 16:02:12 +02:00
|
|
|
}
|
|
|
|
|
2022-04-21 03:31:58 +02:00
|
|
|
/* ...
|
|
|
|
* CipherSuite cipher_suite;
|
|
|
|
* ...
|
|
|
|
* with CipherSuite defined as:
|
|
|
|
* uint8 CipherSuite[2];
|
2022-03-30 16:02:12 +02:00
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
|
|
|
|
MBEDTLS_PUT_UINT16_BE(ssl->session_negotiate->ciphersuite, p, 0);
|
2022-03-30 16:02:12 +02:00
|
|
|
p += 2;
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(3,
|
|
|
|
("server hello, chosen ciphersuite: %s ( id=%d )",
|
|
|
|
mbedtls_ssl_get_ciphersuite_name(
|
|
|
|
ssl->session_negotiate->ciphersuite),
|
|
|
|
ssl->session_negotiate->ciphersuite));
|
2022-03-30 16:02:12 +02:00
|
|
|
|
2022-04-21 03:31:58 +02:00
|
|
|
/* ...
|
|
|
|
* uint8 legacy_compression_method = 0;
|
|
|
|
* ...
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_CHK_BUF_PTR(p, end, 1);
|
2022-07-25 16:59:25 +02:00
|
|
|
*p++ = MBEDTLS_SSL_COMPRESS_NULL;
|
2022-03-30 16:02:12 +02:00
|
|
|
|
2022-04-21 03:31:58 +02:00
|
|
|
/* ...
|
|
|
|
* Extension extensions<6..2^16-1>;
|
|
|
|
* ...
|
|
|
|
* 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_PTR(p, end, 2);
|
2022-04-20 16:28:09 +02:00
|
|
|
p_extensions_len = p;
|
2022-03-30 16:02:12 +02:00
|
|
|
p += 2;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if ((ret = ssl_tls13_write_server_hello_supported_versions_ext(
|
|
|
|
ssl, p, end, &output_len)) != 0) {
|
2022-04-22 16:27:33 +02:00
|
|
|
MBEDTLS_SSL_DEBUG_RET(
|
2023-01-11 14:50:10 +01:00
|
|
|
1, "ssl_tls13_write_server_hello_supported_versions_ext", ret);
|
|
|
|
return ret;
|
2022-03-30 16:02:12 +02:00
|
|
|
}
|
|
|
|
p += output_len;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (mbedtls_ssl_tls13_key_exchange_mode_with_ephemeral(ssl)) {
|
|
|
|
if (is_hrr) {
|
|
|
|
ret = ssl_tls13_write_hrr_key_share_ext(ssl, p, end, &output_len);
|
|
|
|
} else {
|
|
|
|
ret = ssl_tls13_write_key_share_ext(ssl, p, end, &output_len);
|
|
|
|
}
|
|
|
|
if (ret != 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
2022-03-30 16:02:12 +02:00
|
|
|
p += output_len;
|
|
|
|
}
|
|
|
|
|
2022-10-04 16:38:25 +02:00
|
|
|
#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (!is_hrr && mbedtls_ssl_tls13_key_exchange_mode_with_psk(ssl)) {
|
|
|
|
ret = ssl_tls13_write_server_pre_shared_key_ext(ssl, p, end, &output_len);
|
|
|
|
if (ret != 0) {
|
|
|
|
MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_write_server_pre_shared_key_ext",
|
|
|
|
ret);
|
|
|
|
return ret;
|
2022-07-11 08:10:03 +02:00
|
|
|
}
|
|
|
|
p += output_len;
|
|
|
|
}
|
2022-10-04 16:38:25 +02:00
|
|
|
#endif
|
2022-07-11 08:10:03 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_PUT_UINT16_BE(p - p_extensions_len - 2, p_extensions_len, 0);
|
2022-03-30 16:02:12 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_BUF(4, "server hello extensions",
|
|
|
|
p_extensions_len, p - p_extensions_len);
|
2022-03-30 16:02:12 +02:00
|
|
|
|
2022-04-20 16:28:09 +02:00
|
|
|
*out_len = p - buf;
|
2022-03-30 16:02:12 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_BUF(3, "server hello", buf, *out_len);
|
2022-03-30 16:02:12 +02:00
|
|
|
|
2022-11-08 14:43:46 +01:00
|
|
|
MBEDTLS_SSL_PRINT_EXTS(
|
2022-10-31 06:31:22 +01:00
|
|
|
3, is_hrr ? MBEDTLS_SSL_TLS1_3_HS_HELLO_RETRY_REQUEST :
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_HS_SERVER_HELLO,
|
|
|
|
ssl->handshake->sent_extensions);
|
2022-10-31 06:31:22 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return ret;
|
2022-03-30 11:36:13 +02:00
|
|
|
}
|
|
|
|
|
2022-06-17 10:52:54 +02:00
|
|
|
MBEDTLS_CHECK_RETURN_CRITICAL
|
2023-02-06 11:23:04 +01:00
|
|
|
static int ssl_tls13_finalize_server_hello(mbedtls_ssl_context *ssl)
|
2022-05-05 04:19:22 +02:00
|
|
|
{
|
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
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);
|
|
|
|
return ret;
|
2022-05-05 04:19:22 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return ret;
|
2022-05-05 04:19:22 +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_server_hello(mbedtls_ssl_context *ssl)
|
2022-03-30 11:15:02 +02:00
|
|
|
{
|
2022-04-20 15:37:58 +02:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2022-03-30 11:32:21 +02:00
|
|
|
unsigned char *buf;
|
|
|
|
size_t buf_len, msg_len;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(2, ("=> write server hello"));
|
2022-03-30 11:32:21 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_PROC_CHK(ssl_tls13_prepare_server_hello(ssl));
|
2022-03-30 11:32:21 +02:00
|
|
|
|
2023-03-30 04:50:04 +02:00
|
|
|
MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
|
|
|
|
ssl, MBEDTLS_SSL_HS_SERVER_HELLO, &buf, &buf_len));
|
2022-03-30 11:32:21 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_server_hello_body(ssl, buf,
|
|
|
|
buf + buf_len,
|
|
|
|
&msg_len,
|
|
|
|
0));
|
2022-03-30 11:32:21 +02:00
|
|
|
|
2023-02-06 00:34:21 +01:00
|
|
|
MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
|
2023-02-06 11:48:19 +01:00
|
|
|
ssl, MBEDTLS_SSL_HS_SERVER_HELLO, buf, msg_len));
|
2022-03-30 11:32:21 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(
|
|
|
|
ssl, buf_len, msg_len));
|
2022-04-20 15:37:58 +02:00
|
|
|
|
2023-02-06 11:23:04 +01:00
|
|
|
MBEDTLS_SSL_PROC_CHK(ssl_tls13_finalize_server_hello(ssl));
|
2022-05-05 04:19:22 +02:00
|
|
|
|
2022-05-24 16:04:14 +02:00
|
|
|
#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
|
|
|
|
/* The server sends a dummy change_cipher_spec record immediately
|
|
|
|
* after its first handshake message. This may either be after
|
|
|
|
* a ServerHello or a HelloRetryRequest.
|
|
|
|
*/
|
2022-06-28 11:45:18 +02:00
|
|
|
mbedtls_ssl_handshake_set_state(
|
2023-01-11 14:50:10 +01:00
|
|
|
ssl, MBEDTLS_SSL_SERVER_CCS_AFTER_SERVER_HELLO);
|
2022-05-24 16:04:14 +02:00
|
|
|
#else
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_ENCRYPTED_EXTENSIONS);
|
2022-05-24 16:04:14 +02:00
|
|
|
#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
|
2022-05-05 04:19:22 +02:00
|
|
|
|
2022-03-30 11:32:21 +02:00
|
|
|
cleanup:
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(2, ("<= write server hello"));
|
|
|
|
return ret;
|
2022-03-30 11:15:02 +02:00
|
|
|
}
|
|
|
|
|
2022-05-12 12:08:59 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Handler for MBEDTLS_SSL_HELLO_RETRY_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_prepare_hello_retry_request(mbedtls_ssl_context *ssl)
|
2022-05-12 12:08:59 +02:00
|
|
|
{
|
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->handshake->hello_retry_request_count > 0) {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(1, ("Too many HRRs"));
|
|
|
|
MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
|
|
|
|
MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
|
|
|
|
return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
|
2022-05-12 12:08:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Create stateless transcript hash for HRR
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(4, ("Reset transcript for HRR"));
|
|
|
|
ret = mbedtls_ssl_reset_transcript_for_hrr(ssl);
|
|
|
|
if (ret != 0) {
|
|
|
|
MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_reset_transcript_for_hrr", ret);
|
|
|
|
return ret;
|
2022-05-12 12:08:59 +02:00
|
|
|
}
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_ssl_session_reset_msg_layer(ssl, 0);
|
2022-05-12 12:08:59 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2022-05-12 12:08:59 +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_hello_retry_request(mbedtls_ssl_context *ssl)
|
2022-05-12 12:08:59 +02:00
|
|
|
{
|
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
|
|
|
unsigned char *buf;
|
|
|
|
size_t buf_len, msg_len;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(2, ("=> write hello retry request"));
|
2022-05-12 12:08:59 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_PROC_CHK(ssl_tls13_prepare_hello_retry_request(ssl));
|
2022-05-12 12:08:59 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
|
|
|
|
ssl, MBEDTLS_SSL_HS_SERVER_HELLO,
|
|
|
|
&buf, &buf_len));
|
2022-05-12 12:08:59 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_server_hello_body(ssl, buf,
|
|
|
|
buf + buf_len,
|
|
|
|
&msg_len,
|
|
|
|
1));
|
2023-02-06 00:34:21 +01:00
|
|
|
MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
|
2023-02-06 11:48:19 +01:00
|
|
|
ssl, MBEDTLS_SSL_HS_SERVER_HELLO, buf, msg_len));
|
2022-05-12 12:08:59 +02:00
|
|
|
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(ssl, buf_len,
|
|
|
|
msg_len));
|
2022-05-12 12:08:59 +02:00
|
|
|
|
|
|
|
ssl->handshake->hello_retry_request_count++;
|
|
|
|
|
2022-05-24 16:04:14 +02:00
|
|
|
#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
|
|
|
|
/* The server sends a dummy change_cipher_spec record immediately
|
|
|
|
* after its first handshake message. This may either be after
|
|
|
|
* a ServerHello or a HelloRetryRequest.
|
|
|
|
*/
|
2022-06-28 11:45:18 +02:00
|
|
|
mbedtls_ssl_handshake_set_state(
|
2023-01-11 14:50:10 +01:00
|
|
|
ssl, MBEDTLS_SSL_SERVER_CCS_AFTER_HELLO_RETRY_REQUEST);
|
2022-05-24 16:04:14 +02:00
|
|
|
#else
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_HELLO);
|
2022-05-24 16:04:14 +02:00
|
|
|
#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
|
2022-05-12 12:08:59 +02:00
|
|
|
|
|
|
|
cleanup:
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(2, ("<= write hello retry request"));
|
|
|
|
return ret;
|
2022-05-12 12:08:59 +02:00
|
|
|
}
|
|
|
|
|
2022-04-16 06:37:19 +02:00
|
|
|
/*
|
|
|
|
* Handler for MBEDTLS_SSL_ENCRYPTED_EXTENSIONS
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* struct {
|
|
|
|
* Extension extensions<0..2 ^ 16 - 1>;
|
|
|
|
* } 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_write_encrypted_extensions_body(mbedtls_ssl_context *ssl,
|
|
|
|
unsigned char *buf,
|
|
|
|
unsigned char *end,
|
|
|
|
size_t *out_len)
|
2022-04-16 06:37:19 +02:00
|
|
|
{
|
2022-06-17 12:18:48 +02:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2022-04-16 06:37:19 +02:00
|
|
|
unsigned char *p = buf;
|
|
|
|
size_t extensions_len = 0;
|
2022-05-03 06:12:14 +02:00
|
|
|
unsigned char *p_extensions_len;
|
2022-06-17 12:18:48 +02:00
|
|
|
size_t output_len;
|
2022-05-03 09:46:09 +02:00
|
|
|
|
2022-04-16 06:37:19 +02:00
|
|
|
*out_len = 0;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
|
2022-05-03 06:12:14 +02:00
|
|
|
p_extensions_len = p;
|
2022-04-16 06:37:19 +02:00
|
|
|
p += 2;
|
|
|
|
|
2022-05-03 06:12:14 +02:00
|
|
|
((void) ssl);
|
2022-06-17 12:18:48 +02:00
|
|
|
((void) ret);
|
|
|
|
((void) output_len);
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_SSL_ALPN)
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = mbedtls_ssl_write_alpn_ext(ssl, p, end, &output_len);
|
|
|
|
if (ret != 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
2022-06-24 04:29:26 +02:00
|
|
|
p += output_len;
|
2022-06-17 12:18:48 +02:00
|
|
|
#endif /* MBEDTLS_SSL_ALPN */
|
2022-04-16 06:37:19 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
extensions_len = (p - p_extensions_len) - 2;
|
|
|
|
MBEDTLS_PUT_UINT16_BE(extensions_len, p_extensions_len, 0);
|
2022-05-03 06:12:14 +02:00
|
|
|
|
|
|
|
*out_len = p - buf;
|
2022-04-16 06:37:19 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_BUF(4, "encrypted extensions", buf, *out_len);
|
2022-04-16 06:37:19 +02:00
|
|
|
|
2022-11-08 14:43:46 +01:00
|
|
|
MBEDTLS_SSL_PRINT_EXTS(
|
2023-01-11 14:50:10 +01:00
|
|
|
3, MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS, ssl->handshake->sent_extensions);
|
2022-10-31 06:31:22 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2022-04-16 06:37:19 +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_encrypted_extensions(mbedtls_ssl_context *ssl)
|
2022-04-16 06:37:19 +02:00
|
|
|
{
|
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
|
|
|
unsigned char *buf;
|
2022-05-03 06:14:04 +02:00
|
|
|
size_t buf_len, msg_len;
|
2022-04-16 06:37:19 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_ssl_set_outbound_transform(ssl,
|
|
|
|
ssl->handshake->transform_handshake);
|
2022-06-28 11:34:56 +02:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(
|
2023-01-11 14:50:10 +01:00
|
|
|
3, ("switching to handshake transform for outbound data"));
|
2022-06-28 11:34:56 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(2, ("=> write encrypted extensions"));
|
2022-04-16 06:37:19 +02:00
|
|
|
|
2023-03-30 04:50:04 +02:00
|
|
|
MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
|
|
|
|
ssl, MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS,
|
|
|
|
&buf, &buf_len));
|
2022-04-16 06:37:19 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_encrypted_extensions_body(
|
|
|
|
ssl, buf, buf + buf_len, &msg_len));
|
2022-04-16 06:37:19 +02:00
|
|
|
|
2023-02-06 00:34:21 +01:00
|
|
|
MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
|
2023-03-30 04:50:04 +02:00
|
|
|
ssl, MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS,
|
|
|
|
buf, msg_len));
|
2022-04-16 06:37:19 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(
|
|
|
|
ssl, buf_len, msg_len));
|
2022-04-16 06:37:19 +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-03 06:12:14 +02:00
|
|
|
#else
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_FINISHED);
|
2022-05-03 06:12:14 +02:00
|
|
|
#endif
|
|
|
|
|
2022-04-16 06:37:19 +02:00
|
|
|
cleanup:
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(2, ("<= write encrypted extensions"));
|
|
|
|
return ret;
|
2022-04-16 06:37:19 +02:00
|
|
|
}
|
|
|
|
|
2022-10-04 16:14:26 +02:00
|
|
|
#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
|
2022-05-07 03:25:58 +02:00
|
|
|
#define SSL_CERTIFICATE_REQUEST_SEND_REQUEST 0
|
|
|
|
#define SSL_CERTIFICATE_REQUEST_SKIP 1
|
|
|
|
/* Coordination:
|
|
|
|
* Check whether a CertificateRequest message should be written.
|
|
|
|
* Returns a negative code on failure, or
|
|
|
|
* - SSL_CERTIFICATE_REQUEST_SEND_REQUEST
|
|
|
|
* - SSL_CERTIFICATE_REQUEST_SKIP
|
|
|
|
* indicating if the writing of the CertificateRequest
|
|
|
|
* should be skipped 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)
|
2022-05-07 03:25:58 +02:00
|
|
|
{
|
|
|
|
int authmode;
|
|
|
|
|
2022-05-07 11:02:40 +02:00
|
|
|
#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET) {
|
2022-05-07 11:02:40 +02:00
|
|
|
authmode = ssl->handshake->sni_authmode;
|
2023-01-11 14:50:10 +01:00
|
|
|
} else
|
2022-05-07 11:02:40 +02:00
|
|
|
#endif
|
2022-05-07 03:25:58 +02:00
|
|
|
authmode = ssl->conf->authmode;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (authmode == MBEDTLS_SSL_VERIFY_NONE) {
|
2022-09-13 10:16:31 +02:00
|
|
|
ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_SKIP_VERIFY;
|
2023-01-11 14:50:10 +01:00
|
|
|
return SSL_CERTIFICATE_REQUEST_SKIP;
|
2022-09-13 10:16:31 +02:00
|
|
|
}
|
2022-05-07 03:25:58 +02:00
|
|
|
|
2022-05-13 07:55:41 +02:00
|
|
|
ssl->handshake->certificate_request_sent = 1;
|
2022-05-10 10:12:17 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return SSL_CERTIFICATE_REQUEST_SEND_REQUEST;
|
2022-05-07 03:25:58 +02:00
|
|
|
}
|
|
|
|
|
2022-05-06 09:28:50 +02:00
|
|
|
/*
|
|
|
|
* 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_write_certificate_request_body(mbedtls_ssl_context *ssl,
|
|
|
|
unsigned char *buf,
|
|
|
|
const unsigned char *end,
|
|
|
|
size_t *out_len)
|
2022-05-06 09:28:50 +02:00
|
|
|
{
|
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
|
|
|
unsigned char *p = buf;
|
2022-05-06 11:53:10 +02:00
|
|
|
size_t output_len = 0;
|
2022-05-06 09:28:50 +02:00
|
|
|
unsigned char *p_extensions_len;
|
|
|
|
|
|
|
|
*out_len = 0;
|
|
|
|
|
|
|
|
/* Check if we have enough space:
|
|
|
|
* - certificate_request_context (1 byte)
|
|
|
|
* - extensions length (2 bytes)
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_CHK_BUF_PTR(p, end, 3);
|
2022-05-06 09:28:50 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Write certificate_request_context
|
|
|
|
*/
|
|
|
|
/*
|
|
|
|
* We use a zero length context for the normal handshake
|
|
|
|
* messages. For post-authentication handshake messages
|
|
|
|
* this request context would be set to a non-zero value.
|
|
|
|
*/
|
|
|
|
*p++ = 0x0;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Write extensions
|
|
|
|
*/
|
|
|
|
/* The extensions must contain the signature_algorithms. */
|
|
|
|
p_extensions_len = p;
|
|
|
|
p += 2;
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = mbedtls_ssl_write_sig_alg_ext(ssl, p, end, &output_len);
|
|
|
|
if (ret != 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
2022-05-06 09:28:50 +02:00
|
|
|
|
2022-05-06 11:53:10 +02:00
|
|
|
p += output_len;
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_PUT_UINT16_BE(p - p_extensions_len - 2, p_extensions_len, 0);
|
2022-05-06 09:28:50 +02:00
|
|
|
|
|
|
|
*out_len = p - buf;
|
|
|
|
|
2022-11-08 14:43:46 +01:00
|
|
|
MBEDTLS_SSL_PRINT_EXTS(
|
2023-01-11 14:50:10 +01:00
|
|
|
3, MBEDTLS_SSL_HS_CERTIFICATE_REQUEST, ssl->handshake->sent_extensions);
|
2022-10-31 06:31:22 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2022-05-06 09:28:50 +02:00
|
|
|
}
|
|
|
|
|
2022-06-17 10:52:54 +02:00
|
|
|
MBEDTLS_CHECK_RETURN_CRITICAL
|
2023-01-11 14:50:10 +01:00
|
|
|
static int ssl_tls13_write_certificate_request(mbedtls_ssl_context *ssl)
|
2022-05-06 09:28:50 +02:00
|
|
|
{
|
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(2, ("=> write certificate request"));
|
2022-05-06 09:28:50 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_PROC_CHK_NEG(ssl_tls13_certificate_request_coordinate(ssl));
|
2022-05-06 09:28:50 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ret == SSL_CERTIFICATE_REQUEST_SEND_REQUEST) {
|
2022-05-06 09:28:50 +02:00
|
|
|
unsigned char *buf;
|
|
|
|
size_t buf_len, msg_len;
|
|
|
|
|
2023-03-30 04:50:04 +02:00
|
|
|
MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
|
|
|
|
ssl, MBEDTLS_SSL_HS_CERTIFICATE_REQUEST,
|
|
|
|
&buf, &buf_len));
|
2022-05-06 09:28:50 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_certificate_request_body(
|
|
|
|
ssl, buf, buf + buf_len, &msg_len));
|
2022-05-06 09:28:50 +02:00
|
|
|
|
2023-02-06 00:34:21 +01:00
|
|
|
MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum(
|
2023-03-30 04:50:04 +02:00
|
|
|
ssl, MBEDTLS_SSL_HS_CERTIFICATE_REQUEST,
|
|
|
|
buf, msg_len));
|
2022-05-06 09:28:50 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(
|
|
|
|
ssl, buf_len, msg_len));
|
|
|
|
} else if (ret == SSL_CERTIFICATE_REQUEST_SKIP) {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate request"));
|
2022-05-06 09:28:50 +02:00
|
|
|
ret = 0;
|
2023-01-11 14:50:10 +01:00
|
|
|
} else {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
|
2022-05-06 09:28:50 +02:00
|
|
|
ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_CERTIFICATE);
|
2022-05-06 09:28:50 +02:00
|
|
|
cleanup:
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(2, ("<= write certificate request"));
|
|
|
|
return ret;
|
2022-05-06 09:28:50 +02:00
|
|
|
}
|
|
|
|
|
2022-04-16 07:59:52 +02:00
|
|
|
/*
|
2022-04-16 13:42:57 +02:00
|
|
|
* Handler for MBEDTLS_SSL_SERVER_CERTIFICATE
|
2022-04-16 07:59:52 +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_server_certificate(mbedtls_ssl_context *ssl)
|
2022-04-16 07:59:52 +02:00
|
|
|
{
|
2022-05-10 14:46:40 +02:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2022-06-15 05:57:21 +02:00
|
|
|
|
|
|
|
#if defined(MBEDTLS_X509_CRT_PARSE_C)
|
2023-01-11 14:50:10 +01:00
|
|
|
if ((ssl_tls13_pick_key_cert(ssl) != 0) ||
|
|
|
|
mbedtls_ssl_own_cert(ssl) == NULL) {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(2, ("No certificate available."));
|
|
|
|
MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
|
|
|
|
MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
|
|
|
|
return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
|
2022-05-10 14:46:40 +02:00
|
|
|
}
|
2022-06-15 05:57:21 +02:00
|
|
|
#endif /* MBEDTLS_X509_CRT_PARSE_C */
|
2022-05-10 14:46:40 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = mbedtls_ssl_tls13_write_certificate(ssl);
|
|
|
|
if (ret != 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CERTIFICATE_VERIFY);
|
|
|
|
return 0;
|
2022-04-16 07:59:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2022-04-16 13:42:57 +02:00
|
|
|
* Handler for MBEDTLS_SSL_CERTIFICATE_VERIFY
|
2022-04-16 07:59:52 +02:00
|
|
|
*/
|
2022-06-17 10:52:54 +02:00
|
|
|
MBEDTLS_CHECK_RETURN_CRITICAL
|
2023-01-11 14:50:10 +01:00
|
|
|
static int ssl_tls13_write_certificate_verify(mbedtls_ssl_context *ssl)
|
2022-04-16 07:59:52 +02:00
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
int ret = mbedtls_ssl_tls13_write_certificate_verify(ssl);
|
|
|
|
if (ret != 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_FINISHED);
|
|
|
|
return 0;
|
2022-04-16 07:59:52 +02:00
|
|
|
}
|
2022-10-04 16:14:26 +02:00
|
|
|
#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
|
2022-04-16 07:59:52 +02:00
|
|
|
|
2022-04-16 06:51:26 +02:00
|
|
|
/*
|
2022-05-18 07:59:24 +02:00
|
|
|
* Handler for MBEDTLS_SSL_SERVER_FINISHED
|
2022-04-16 06:51:26 +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_server_finished(mbedtls_ssl_context *ssl)
|
2022-04-16 06:51:26 +02:00
|
|
|
{
|
2022-05-18 07:59:24 +02:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2022-04-16 07:33:27 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = mbedtls_ssl_tls13_write_finished_message(ssl);
|
|
|
|
if (ret != 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
2022-04-16 07:33:27 +02: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-05-13 07:55:41 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(1, ("Switch to handshake keys for inbound traffic"));
|
|
|
|
mbedtls_ssl_set_inbound_transform(ssl, ssl->handshake->transform_handshake);
|
2022-05-31 14:41:53 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->handshake->certificate_request_sent) {
|
|
|
|
mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_CERTIFICATE);
|
|
|
|
} else {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(2, ("skip parse certificate"));
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(2, ("skip parse certificate verify"));
|
|
|
|
mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_FINISHED);
|
2022-06-15 16:26:13 +02:00
|
|
|
}
|
2022-05-31 14:41:53 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2022-04-16 06:51:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2022-05-18 07:59:24 +02:00
|
|
|
* Handler for MBEDTLS_SSL_CLIENT_FINISHED
|
2022-04-16 06:51:26 +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_client_finished(mbedtls_ssl_context *ssl)
|
2022-04-16 06:51:26 +02:00
|
|
|
{
|
2022-05-18 07:59:24 +02:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = mbedtls_ssl_tls13_process_finished_message(ssl);
|
|
|
|
if (ret != 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
2022-04-16 10:52:57 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = mbedtls_ssl_tls13_compute_resumption_master_secret(ssl);
|
|
|
|
if (ret != 0) {
|
2023-03-30 04:50:04 +02:00
|
|
|
MBEDTLS_SSL_DEBUG_RET(
|
|
|
|
1, "mbedtls_ssl_tls13_compute_resumption_master_secret", 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_HANDSHAKE_WRAPUP);
|
|
|
|
return 0;
|
2022-04-16 06:51:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2022-05-18 07:59:24 +02:00
|
|
|
* Handler for MBEDTLS_SSL_HANDSHAKE_WRAPUP
|
2022-04-16 06:51:26 +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_handshake_wrapup(mbedtls_ssl_context *ssl)
|
2022-04-16 06:51:26 +02:00
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(2, ("handshake: done"));
|
2022-04-16 11:13:30 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_ssl_tls13_handshake_wrapup(ssl);
|
2022-07-07 09:29:42 +02:00
|
|
|
|
2023-01-12 09:46:28 +01:00
|
|
|
#if defined(MBEDTLS_SSL_SESSION_TICKETS) && \
|
|
|
|
defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
|
2022-12-13 06:49:59 +01:00
|
|
|
/* TODO: Remove the check of SOME_PSK_ENABLED since SESSION_TICKETS requires
|
|
|
|
* SOME_PSK_ENABLED to be enabled. Here is just to make CI happy. It is
|
|
|
|
* expected to be resolved with issue#6395.
|
|
|
|
*/
|
2022-12-01 09:33:00 +01:00
|
|
|
/* Sent NewSessionTicket message only when client supports PSK */
|
2023-01-12 09:46:28 +01:00
|
|
|
if (mbedtls_ssl_tls13_some_psk_enabled(ssl)) {
|
2023-03-30 04:50:04 +02:00
|
|
|
mbedtls_ssl_handshake_set_state(
|
|
|
|
ssl, MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET);
|
2022-12-01 09:33:00 +01:00
|
|
|
} else
|
2022-07-21 04:37:48 +02:00
|
|
|
#endif
|
2023-01-12 09:46:28 +01:00
|
|
|
{
|
|
|
|
mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HANDSHAKE_OVER);
|
|
|
|
}
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2022-04-16 06:51:26 +02:00
|
|
|
}
|
|
|
|
|
2022-07-07 09:29:42 +02:00
|
|
|
/*
|
2022-10-30 07:51:23 +01:00
|
|
|
* Handler for MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET
|
2022-07-07 09:29:42 +02:00
|
|
|
*/
|
|
|
|
#define SSL_NEW_SESSION_TICKET_SKIP 0
|
|
|
|
#define SSL_NEW_SESSION_TICKET_WRITE 1
|
|
|
|
MBEDTLS_CHECK_RETURN_CRITICAL
|
2023-01-11 14:50:10 +01:00
|
|
|
static int ssl_tls13_write_new_session_ticket_coordinate(mbedtls_ssl_context *ssl)
|
2022-07-07 09:29:42 +02:00
|
|
|
{
|
|
|
|
/* Check whether the use of session tickets is enabled */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->conf->f_ticket_write == NULL) {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(2, ("NewSessionTicket: disabled,"
|
|
|
|
" callback is not set"));
|
|
|
|
return SSL_NEW_SESSION_TICKET_SKIP;
|
2022-09-22 04:46:57 +02:00
|
|
|
}
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->conf->new_session_tickets_count == 0) {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(2, ("NewSessionTicket: disabled,"
|
|
|
|
" configured count is zero"));
|
|
|
|
return SSL_NEW_SESSION_TICKET_SKIP;
|
2022-09-22 04:46:57 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->handshake->new_session_tickets_count == 0) {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(2, ("NewSessionTicket: all tickets have "
|
|
|
|
"been sent."));
|
|
|
|
return SSL_NEW_SESSION_TICKET_SKIP;
|
2022-07-07 09:29:42 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return SSL_NEW_SESSION_TICKET_WRITE;
|
2022-07-07 09:29:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_SSL_SESSION_TICKETS)
|
|
|
|
MBEDTLS_CHECK_RETURN_CRITICAL
|
2023-01-11 14:50:10 +01:00
|
|
|
static int ssl_tls13_prepare_new_session_ticket(mbedtls_ssl_context *ssl,
|
|
|
|
unsigned char *ticket_nonce,
|
|
|
|
size_t ticket_nonce_size)
|
2022-07-07 09:29:42 +02:00
|
|
|
{
|
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
|
|
|
mbedtls_ssl_session *session = ssl->session;
|
|
|
|
mbedtls_ssl_ciphersuite_t *ciphersuite_info;
|
|
|
|
psa_algorithm_t psa_hash_alg;
|
|
|
|
int hash_length;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(2, ("=> prepare NewSessionTicket msg"));
|
2022-07-07 09:29:42 +02:00
|
|
|
|
|
|
|
#if defined(MBEDTLS_HAVE_TIME)
|
2023-01-11 14:50:10 +01:00
|
|
|
session->start = mbedtls_time(NULL);
|
2022-07-07 09:29:42 +02:00
|
|
|
#endif
|
|
|
|
|
2022-11-17 08:22:33 +01:00
|
|
|
/* Set ticket_flags depends on the advertised psk key exchange mode */
|
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-12-06 02:30:29 +01:00
|
|
|
#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
|
2023-01-12 04:54:04 +01:00
|
|
|
mbedtls_ssl_session_set_ticket_flags(
|
2022-12-09 07:39:19 +01:00
|
|
|
session, ssl->handshake->tls13_kex_modes);
|
2022-12-06 02:30:29 +01:00
|
|
|
#endif
|
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
|
|
|
|
2022-07-07 09:29:42 +02:00
|
|
|
/* Generate ticket_age_add */
|
2023-01-11 14:50:10 +01:00
|
|
|
if ((ret = ssl->conf->f_rng(ssl->conf->p_rng,
|
|
|
|
(unsigned char *) &session->ticket_age_add,
|
|
|
|
sizeof(session->ticket_age_add)) != 0)) {
|
|
|
|
MBEDTLS_SSL_DEBUG_RET(1, "generate_ticket_age_add", ret);
|
|
|
|
return ret;
|
2022-07-07 09:29:42 +02:00
|
|
|
}
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(3, ("ticket_age_add: %u",
|
|
|
|
(unsigned int) session->ticket_age_add));
|
2022-07-07 09:29:42 +02:00
|
|
|
|
|
|
|
/* Generate ticket_nonce */
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = ssl->conf->f_rng(ssl->conf->p_rng, ticket_nonce, ticket_nonce_size);
|
|
|
|
if (ret != 0) {
|
|
|
|
MBEDTLS_SSL_DEBUG_RET(1, "generate_ticket_nonce", ret);
|
|
|
|
return ret;
|
2022-07-07 09:29:42 +02:00
|
|
|
}
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_BUF(3, "ticket_nonce:",
|
|
|
|
ticket_nonce, ticket_nonce_size);
|
2022-07-07 09:29:42 +02:00
|
|
|
|
|
|
|
ciphersuite_info =
|
2023-01-11 14:50:10 +01:00
|
|
|
(mbedtls_ssl_ciphersuite_t *) ssl->handshake->ciphersuite_info;
|
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-21 04:37:48 +02:00
|
|
|
}
|
|
|
|
|
2022-07-07 09:29:42 +02:00
|
|
|
/* In this code the psk key length equals the length of the hash */
|
|
|
|
session->resumption_key_len = hash_length;
|
|
|
|
session->ciphersuite = ciphersuite_info->id;
|
|
|
|
|
|
|
|
/* Compute resumption key
|
|
|
|
*
|
|
|
|
* 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_size,
|
|
|
|
session->resumption_key,
|
|
|
|
hash_length);
|
|
|
|
|
|
|
|
if (ret != 0) {
|
|
|
|
MBEDTLS_SSL_DEBUG_RET(2,
|
|
|
|
"Creating the ticket-resumed PSK failed",
|
|
|
|
ret);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
MBEDTLS_SSL_DEBUG_BUF(3, "Ticket-resumed PSK",
|
|
|
|
session->resumption_key,
|
|
|
|
session->resumption_key_len);
|
|
|
|
|
|
|
|
MBEDTLS_SSL_DEBUG_BUF(3, "resumption_master_secret",
|
|
|
|
session->app_secrets.resumption_master_secret,
|
|
|
|
hash_length);
|
|
|
|
|
|
|
|
return 0;
|
2022-07-07 09:29:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* This function creates a NewSessionTicket message in the following format:
|
|
|
|
*
|
|
|
|
* 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;
|
|
|
|
*
|
|
|
|
* The ticket inside the NewSessionTicket message is an encrypted container
|
|
|
|
* carrying the necessary information so that the server is later able to
|
|
|
|
* re-start the communication.
|
|
|
|
*
|
|
|
|
* The following fields are placed inside the ticket by the
|
|
|
|
* f_ticket_write() function:
|
|
|
|
*
|
|
|
|
* - creation time (start)
|
|
|
|
* - flags (flags)
|
|
|
|
* - age add (ticket_age_add)
|
|
|
|
* - key (key)
|
|
|
|
* - key length (key_len)
|
|
|
|
* - ciphersuite (ciphersuite)
|
|
|
|
*/
|
|
|
|
MBEDTLS_CHECK_RETURN_CRITICAL
|
2023-01-11 14:50:10 +01:00
|
|
|
static int ssl_tls13_write_new_session_ticket_body(mbedtls_ssl_context *ssl,
|
|
|
|
unsigned char *buf,
|
|
|
|
unsigned char *end,
|
|
|
|
size_t *out_len,
|
|
|
|
unsigned char *ticket_nonce,
|
|
|
|
size_t ticket_nonce_size)
|
2022-07-07 09:29:42 +02:00
|
|
|
{
|
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
|
|
|
unsigned char *p = buf;
|
|
|
|
mbedtls_ssl_session *session = ssl->session;
|
|
|
|
size_t ticket_len;
|
|
|
|
uint32_t ticket_lifetime;
|
|
|
|
|
|
|
|
*out_len = 0;
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(2, ("=> write NewSessionTicket msg"));
|
2022-07-07 09:29:42 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* ticket_lifetime 4 bytes
|
|
|
|
* ticket_age_add 4 bytes
|
|
|
|
* ticket_nonce 1 + ticket_nonce_size bytes
|
|
|
|
* ticket >=2 bytes
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_CHK_BUF_PTR(p, end, 4 + 4 + 1 + ticket_nonce_size + 2);
|
2022-07-07 09:29:42 +02:00
|
|
|
|
|
|
|
/* Generate ticket and ticket_lifetime */
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = ssl->conf->f_ticket_write(ssl->conf->p_ticket,
|
|
|
|
session,
|
|
|
|
p + 9 + ticket_nonce_size + 2,
|
|
|
|
end,
|
|
|
|
&ticket_len,
|
|
|
|
&ticket_lifetime);
|
|
|
|
if (ret != 0) {
|
|
|
|
MBEDTLS_SSL_DEBUG_RET(1, "write_ticket", ret);
|
|
|
|
return ret;
|
2022-07-07 09:29:42 +02:00
|
|
|
}
|
|
|
|
/* RFC 8446 4.6.1
|
|
|
|
* ticket_lifetime: Indicates the lifetime in seconds as a 32-bit
|
|
|
|
* unsigned integer in network byte order from the time of ticket
|
|
|
|
* issuance. Servers MUST NOT use any value greater than
|
|
|
|
* 604800 seconds (7 days). The value of zero indicates that the
|
|
|
|
* ticket should be discarded immediately. Clients MUST NOT cache
|
|
|
|
* tickets for longer than 7 days, regardless of the ticket_lifetime,
|
|
|
|
* and MAY delete tickets earlier based on local policy. A server
|
|
|
|
* MAY treat a ticket as valid for a shorter period of time than what
|
|
|
|
* is stated in the ticket_lifetime.
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ticket_lifetime > 604800) {
|
2022-10-13 10:18:19 +02:00
|
|
|
ticket_lifetime = 604800;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
|
|
|
MBEDTLS_PUT_UINT32_BE(ticket_lifetime, p, 0);
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(3, ("ticket_lifetime: %u",
|
|
|
|
(unsigned int) ticket_lifetime));
|
2022-07-07 09:29:42 +02:00
|
|
|
|
|
|
|
/* Write ticket_age_add */
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_PUT_UINT32_BE(session->ticket_age_add, p, 4);
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(3, ("ticket_age_add: %u",
|
|
|
|
(unsigned int) session->ticket_age_add));
|
2022-07-07 09:29:42 +02:00
|
|
|
|
|
|
|
/* Write ticket_nonce */
|
2023-01-11 14:50:10 +01:00
|
|
|
p[8] = (unsigned char) ticket_nonce_size;
|
|
|
|
if (ticket_nonce_size > 0) {
|
|
|
|
memcpy(p + 9, ticket_nonce, ticket_nonce_size);
|
2022-07-07 09:29:42 +02:00
|
|
|
}
|
|
|
|
p += 9 + ticket_nonce_size;
|
|
|
|
|
|
|
|
/* Write ticket */
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_PUT_UINT16_BE(ticket_len, p, 0);
|
2022-07-07 09:29:42 +02:00
|
|
|
p += 2;
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_BUF(4, "ticket", p, ticket_len);
|
2022-07-07 09:29:42 +02:00
|
|
|
p += ticket_len;
|
|
|
|
|
|
|
|
/* Ticket Extensions
|
|
|
|
*
|
|
|
|
* Note: We currently don't have any extensions.
|
|
|
|
* Set length to zero.
|
|
|
|
*/
|
2022-10-31 07:37:31 +01:00
|
|
|
ssl->handshake->sent_extensions = MBEDTLS_SSL_EXT_MASK_NONE;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
|
|
|
|
MBEDTLS_PUT_UINT16_BE(0, p, 0);
|
2022-07-07 09:29:42 +02:00
|
|
|
p += 2;
|
|
|
|
|
|
|
|
*out_len = p - buf;
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_BUF(4, "ticket", buf, *out_len);
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(2, ("<= write new session ticket"));
|
2022-07-07 09:29:42 +02:00
|
|
|
|
2022-11-08 14:43:46 +01:00
|
|
|
MBEDTLS_SSL_PRINT_EXTS(
|
2023-01-11 14:50:10 +01:00
|
|
|
3, MBEDTLS_SSL_HS_NEW_SESSION_TICKET, ssl->handshake->sent_extensions);
|
2022-10-31 06:31:22 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2022-04-16 06:51:26 +02:00
|
|
|
}
|
|
|
|
|
2022-07-07 09:29:42 +02:00
|
|
|
/*
|
2022-10-30 07:51:23 +01:00
|
|
|
* Handler for MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET
|
2022-07-07 09:29:42 +02:00
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
static int ssl_tls13_write_new_session_ticket(mbedtls_ssl_context *ssl)
|
2022-07-07 09:29:42 +02:00
|
|
|
{
|
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_PROC_CHK_NEG(ssl_tls13_write_new_session_ticket_coordinate(ssl));
|
2022-07-07 09:29:42 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ret == SSL_NEW_SESSION_TICKET_WRITE) {
|
2022-07-07 09:29:42 +02:00
|
|
|
unsigned char ticket_nonce[MBEDTLS_SSL_TLS1_3_TICKET_NONCE_LENGTH];
|
|
|
|
unsigned char *buf;
|
|
|
|
size_t buf_len, msg_len;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_PROC_CHK(ssl_tls13_prepare_new_session_ticket(
|
|
|
|
ssl, ticket_nonce, sizeof(ticket_nonce)));
|
2022-07-07 09:29:42 +02:00
|
|
|
|
2023-03-30 04:50:04 +02:00
|
|
|
MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
|
|
|
|
ssl, MBEDTLS_SSL_HS_NEW_SESSION_TICKET,
|
|
|
|
&buf, &buf_len));
|
2022-07-07 09:29:42 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_new_session_ticket_body(
|
|
|
|
ssl, buf, buf + buf_len, &msg_len,
|
|
|
|
ticket_nonce, sizeof(ticket_nonce)));
|
2022-07-07 09:29:42 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(
|
|
|
|
ssl, buf_len, msg_len));
|
2022-07-21 04:37:48 +02:00
|
|
|
|
2022-09-22 17:47:43 +02:00
|
|
|
/* Limit session tickets count to one when resumption connection.
|
|
|
|
*
|
|
|
|
* See document of mbedtls_ssl_conf_new_session_tickets.
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->handshake->resume == 1) {
|
2022-09-22 17:47:43 +02:00
|
|
|
ssl->handshake->new_session_tickets_count = 0;
|
2023-01-11 14:50:10 +01:00
|
|
|
} else {
|
2022-09-22 17:47:43 +02:00
|
|
|
ssl->handshake->new_session_tickets_count--;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2022-09-22 05:07:18 +02:00
|
|
|
|
2022-10-30 07:51:23 +01:00
|
|
|
mbedtls_ssl_handshake_set_state(
|
2023-01-11 14:50:10 +01:00
|
|
|
ssl, MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET_FLUSH);
|
|
|
|
} else {
|
|
|
|
mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HANDSHAKE_OVER);
|
2022-07-07 09:29:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
cleanup:
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return ret;
|
2022-07-07 09:29:42 +02:00
|
|
|
}
|
|
|
|
#endif /* MBEDTLS_SSL_SESSION_TICKETS */
|
|
|
|
|
2022-02-15 11:04:37 +01:00
|
|
|
/*
|
2022-04-22 04:34:40 +02:00
|
|
|
* TLS 1.3 State Machine -- server side
|
2022-02-15 11:04:37 +01:00
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_ssl_tls13_handshake_server_step(mbedtls_ssl_context *ssl)
|
2021-08-06 11:11:51 +02:00
|
|
|
{
|
2022-04-20 09:16:41 +02:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2022-02-15 11:04:37 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER || ssl->handshake == NULL) {
|
|
|
|
return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
|
|
|
|
}
|
2022-02-15 11:04:37 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(2, ("tls13 server state: %s(%d)",
|
2023-10-05 14:30:37 +02:00
|
|
|
mbedtls_ssl_states_str((mbedtls_ssl_states) ssl->state),
|
2023-01-11 14:50:10 +01:00
|
|
|
ssl->state));
|
2021-09-27 05:16:17 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
switch (ssl->state) {
|
2022-02-15 11:04:37 +01:00
|
|
|
/* start state */
|
|
|
|
case MBEDTLS_SSL_HELLO_REQUEST:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_HELLO);
|
2022-04-20 09:16:41 +02:00
|
|
|
ret = 0;
|
2022-02-15 11:04:37 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case MBEDTLS_SSL_CLIENT_HELLO:
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = ssl_tls13_process_client_hello(ssl);
|
|
|
|
if (ret != 0) {
|
|
|
|
MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_process_client_hello", ret);
|
|
|
|
}
|
2022-05-09 16:20:30 +02:00
|
|
|
break;
|
2022-02-15 11:04:37 +01:00
|
|
|
|
2022-05-09 16:20:30 +02:00
|
|
|
case MBEDTLS_SSL_HELLO_RETRY_REQUEST:
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = ssl_tls13_write_hello_retry_request(ssl);
|
|
|
|
if (ret != 0) {
|
|
|
|
MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_write_hello_retry_request", ret);
|
|
|
|
return ret;
|
2022-05-09 16:20:30 +02:00
|
|
|
}
|
2022-02-15 11:04:37 +01:00
|
|
|
break;
|
|
|
|
|
2022-03-30 11:15:02 +02:00
|
|
|
case MBEDTLS_SSL_SERVER_HELLO:
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = ssl_tls13_write_server_hello(ssl);
|
2022-03-30 11:15:02 +02:00
|
|
|
break;
|
|
|
|
|
2022-02-15 11:00:56 +01:00
|
|
|
case MBEDTLS_SSL_ENCRYPTED_EXTENSIONS:
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = ssl_tls13_write_encrypted_extensions(ssl);
|
|
|
|
if (ret != 0) {
|
|
|
|
MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_write_encrypted_extensions", ret);
|
|
|
|
return ret;
|
2022-02-15 11:00:56 +01:00
|
|
|
}
|
2022-05-06 15:35:44 +02:00
|
|
|
break;
|
2022-05-05 05:14:19 +02:00
|
|
|
|
2022-10-04 16:14:26 +02:00
|
|
|
#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
|
2022-02-14 13:57:18 +01:00
|
|
|
case MBEDTLS_SSL_CERTIFICATE_REQUEST:
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = ssl_tls13_write_certificate_request(ssl);
|
2022-02-14 13:57:18 +01:00
|
|
|
break;
|
|
|
|
|
2022-04-16 07:59:52 +02:00
|
|
|
case MBEDTLS_SSL_SERVER_CERTIFICATE:
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = ssl_tls13_write_server_certificate(ssl);
|
2022-04-16 07:59:52 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case MBEDTLS_SSL_CERTIFICATE_VERIFY:
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = ssl_tls13_write_certificate_verify(ssl);
|
2022-04-16 07:59:52 +02:00
|
|
|
break;
|
2022-10-04 16:14:26 +02:00
|
|
|
#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
|
2022-04-16 07:59:52 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
/*
|
|
|
|
* Injection of dummy-CCS's for middlebox compatibility
|
|
|
|
*/
|
2022-05-24 16:04:14 +02:00
|
|
|
#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
|
2022-06-28 16:01:49 +02:00
|
|
|
case MBEDTLS_SSL_SERVER_CCS_AFTER_HELLO_RETRY_REQUEST:
|
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-05-24 16:04:14 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case MBEDTLS_SSL_SERVER_CCS_AFTER_SERVER_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_ENCRYPTED_EXTENSIONS);
|
|
|
|
}
|
2022-05-24 16:04:14 +02:00
|
|
|
break;
|
|
|
|
#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
|
|
|
|
|
2022-04-16 06:51:26 +02:00
|
|
|
case MBEDTLS_SSL_SERVER_FINISHED:
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = ssl_tls13_write_server_finished(ssl);
|
2022-04-16 06:51:26 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case MBEDTLS_SSL_CLIENT_FINISHED:
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = ssl_tls13_process_client_finished(ssl);
|
2022-04-16 06:51:26 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case MBEDTLS_SSL_HANDSHAKE_WRAPUP:
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = ssl_tls13_handshake_wrapup(ssl);
|
2022-04-16 06:51:26 +02:00
|
|
|
break;
|
|
|
|
|
2022-10-18 12:17:11 +02:00
|
|
|
#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
|
2022-04-25 09:29:34 +02:00
|
|
|
case MBEDTLS_SSL_CLIENT_CERTIFICATE:
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = mbedtls_ssl_tls13_process_certificate(ssl);
|
|
|
|
if (ret == 0) {
|
|
|
|
if (ssl->session_negotiate->peer_cert != NULL) {
|
2022-06-07 10:30:19 +02:00
|
|
|
mbedtls_ssl_handshake_set_state(
|
2023-01-11 14:50:10 +01:00
|
|
|
ssl, MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY);
|
|
|
|
} else {
|
|
|
|
MBEDTLS_SSL_DEBUG_MSG(2, ("skip parse certificate verify"));
|
2022-06-07 10:30:19 +02:00
|
|
|
mbedtls_ssl_handshake_set_state(
|
2023-01-11 14:50:10 +01:00
|
|
|
ssl, MBEDTLS_SSL_CLIENT_FINISHED);
|
2022-06-15 16:26:13 +02:00
|
|
|
}
|
2022-04-25 09:29:34 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY:
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = mbedtls_ssl_tls13_process_certificate_verify(ssl);
|
|
|
|
if (ret == 0) {
|
2022-04-25 09:29:34 +02:00
|
|
|
mbedtls_ssl_handshake_set_state(
|
2023-01-11 14:50:10 +01:00
|
|
|
ssl, MBEDTLS_SSL_CLIENT_FINISHED);
|
2022-04-25 09:29:34 +02:00
|
|
|
}
|
|
|
|
break;
|
2022-10-18 12:17:11 +02:00
|
|
|
#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */
|
2022-04-25 09:29:34 +02:00
|
|
|
|
2022-07-07 09:29:42 +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_write_new_session_ticket(ssl);
|
|
|
|
if (ret != 0) {
|
|
|
|
MBEDTLS_SSL_DEBUG_RET(1,
|
|
|
|
"ssl_tls13_write_new_session_ticket ",
|
|
|
|
ret);
|
2022-07-07 09:29:42 +02:00
|
|
|
}
|
|
|
|
break;
|
2022-10-30 07:51:23 +01:00
|
|
|
case MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET_FLUSH:
|
2022-07-07 09:29:42 +02:00
|
|
|
/* This state is necessary to do the flush of the New Session
|
2022-10-30 07:51:23 +01:00
|
|
|
* Ticket message written in MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET
|
2022-07-07 09:29:42 +02:00
|
|
|
* as part of ssl_prepare_handshake_step.
|
|
|
|
*/
|
|
|
|
ret = 0;
|
2022-08-09 07:33:50 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ssl->handshake->new_session_tickets_count == 0) {
|
|
|
|
mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HANDSHAKE_OVER);
|
|
|
|
} else {
|
2023-03-30 04:50:04 +02:00
|
|
|
mbedtls_ssl_handshake_set_state(
|
|
|
|
ssl, MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET);
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2022-07-07 09:29:42 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
#endif /* MBEDTLS_SSL_SESSION_TICKETS */
|
|
|
|
|
2022-02-15 11:04:37 +01:00
|
|
|
default:
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_SSL_DEBUG_MSG(1, ("invalid state %d", ssl->state));
|
|
|
|
return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
|
2022-02-15 11:04:37 +01:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return ret;
|
2021-08-06 11:11:51 +02:00
|
|
|
}
|
2021-08-06 10:29:08 +02:00
|
|
|
|
2022-01-27 08:03:26 +01:00
|
|
|
#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_PROTO_TLS1_3 */
|