1a8e1175bSopenharmony_ci/* 2a8e1175bSopenharmony_ci * TLS 1.3 server-side functions 3a8e1175bSopenharmony_ci * 4a8e1175bSopenharmony_ci * Copyright The Mbed TLS Contributors 5a8e1175bSopenharmony_ci * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later 6a8e1175bSopenharmony_ci */ 7a8e1175bSopenharmony_ci 8a8e1175bSopenharmony_ci#include "common.h" 9a8e1175bSopenharmony_ci 10a8e1175bSopenharmony_ci#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_PROTO_TLS1_3) 11a8e1175bSopenharmony_ci 12a8e1175bSopenharmony_ci#include "debug_internal.h" 13a8e1175bSopenharmony_ci#include "mbedtls/error.h" 14a8e1175bSopenharmony_ci#include "mbedtls/platform.h" 15a8e1175bSopenharmony_ci#include "mbedtls/constant_time.h" 16a8e1175bSopenharmony_ci#include "mbedtls/oid.h" 17a8e1175bSopenharmony_ci#include "mbedtls/psa_util.h" 18a8e1175bSopenharmony_ci 19a8e1175bSopenharmony_ci#include "ssl_misc.h" 20a8e1175bSopenharmony_ci#include "ssl_tls13_keys.h" 21a8e1175bSopenharmony_ci#include "ssl_debug_helpers.h" 22a8e1175bSopenharmony_ci 23a8e1175bSopenharmony_ci 24a8e1175bSopenharmony_cistatic const mbedtls_ssl_ciphersuite_t *ssl_tls13_validate_peer_ciphersuite( 25a8e1175bSopenharmony_ci mbedtls_ssl_context *ssl, 26a8e1175bSopenharmony_ci unsigned int cipher_suite) 27a8e1175bSopenharmony_ci{ 28a8e1175bSopenharmony_ci const mbedtls_ssl_ciphersuite_t *ciphersuite_info; 29a8e1175bSopenharmony_ci if (!mbedtls_ssl_tls13_cipher_suite_is_offered(ssl, cipher_suite)) { 30a8e1175bSopenharmony_ci return NULL; 31a8e1175bSopenharmony_ci } 32a8e1175bSopenharmony_ci 33a8e1175bSopenharmony_ci ciphersuite_info = mbedtls_ssl_ciphersuite_from_id(cipher_suite); 34a8e1175bSopenharmony_ci if ((mbedtls_ssl_validate_ciphersuite(ssl, ciphersuite_info, 35a8e1175bSopenharmony_ci ssl->tls_version, 36a8e1175bSopenharmony_ci ssl->tls_version) != 0)) { 37a8e1175bSopenharmony_ci return NULL; 38a8e1175bSopenharmony_ci } 39a8e1175bSopenharmony_ci return ciphersuite_info; 40a8e1175bSopenharmony_ci} 41a8e1175bSopenharmony_ci 42a8e1175bSopenharmony_cistatic void ssl_tls13_select_ciphersuite( 43a8e1175bSopenharmony_ci mbedtls_ssl_context *ssl, 44a8e1175bSopenharmony_ci const unsigned char *cipher_suites, 45a8e1175bSopenharmony_ci const unsigned char *cipher_suites_end, 46a8e1175bSopenharmony_ci int psk_ciphersuite_id, 47a8e1175bSopenharmony_ci psa_algorithm_t psk_hash_alg, 48a8e1175bSopenharmony_ci const mbedtls_ssl_ciphersuite_t **selected_ciphersuite_info) 49a8e1175bSopenharmony_ci{ 50a8e1175bSopenharmony_ci *selected_ciphersuite_info = NULL; 51a8e1175bSopenharmony_ci 52a8e1175bSopenharmony_ci /* 53a8e1175bSopenharmony_ci * In a compliant ClientHello the byte-length of the list of ciphersuites 54a8e1175bSopenharmony_ci * is even and this function relies on this fact. This should have been 55a8e1175bSopenharmony_ci * checked in the main ClientHello parsing function. Double check here. 56a8e1175bSopenharmony_ci */ 57a8e1175bSopenharmony_ci if ((cipher_suites_end - cipher_suites) & 1) { 58a8e1175bSopenharmony_ci return; 59a8e1175bSopenharmony_ci } 60a8e1175bSopenharmony_ci 61a8e1175bSopenharmony_ci for (const unsigned char *p = cipher_suites; 62a8e1175bSopenharmony_ci p < cipher_suites_end; p += 2) { 63a8e1175bSopenharmony_ci /* 64a8e1175bSopenharmony_ci * "cipher_suites_end - p is even" is an invariant of the loop. As 65a8e1175bSopenharmony_ci * cipher_suites_end - p > 0, we have cipher_suites_end - p >= 2 and it 66a8e1175bSopenharmony_ci * is thus safe to read two bytes. 67a8e1175bSopenharmony_ci */ 68a8e1175bSopenharmony_ci uint16_t id = MBEDTLS_GET_UINT16_BE(p, 0); 69a8e1175bSopenharmony_ci 70a8e1175bSopenharmony_ci const mbedtls_ssl_ciphersuite_t *info = 71a8e1175bSopenharmony_ci ssl_tls13_validate_peer_ciphersuite(ssl, id); 72a8e1175bSopenharmony_ci if (info == NULL) { 73a8e1175bSopenharmony_ci continue; 74a8e1175bSopenharmony_ci } 75a8e1175bSopenharmony_ci 76a8e1175bSopenharmony_ci /* 77a8e1175bSopenharmony_ci * If a valid PSK ciphersuite identifier has been passed in, we want 78a8e1175bSopenharmony_ci * an exact match. 79a8e1175bSopenharmony_ci */ 80a8e1175bSopenharmony_ci if (psk_ciphersuite_id != 0) { 81a8e1175bSopenharmony_ci if (id != psk_ciphersuite_id) { 82a8e1175bSopenharmony_ci continue; 83a8e1175bSopenharmony_ci } 84a8e1175bSopenharmony_ci } else if (psk_hash_alg != PSA_ALG_NONE) { 85a8e1175bSopenharmony_ci if (mbedtls_md_psa_alg_from_type((mbedtls_md_type_t) info->mac) != 86a8e1175bSopenharmony_ci psk_hash_alg) { 87a8e1175bSopenharmony_ci continue; 88a8e1175bSopenharmony_ci } 89a8e1175bSopenharmony_ci } 90a8e1175bSopenharmony_ci 91a8e1175bSopenharmony_ci *selected_ciphersuite_info = info; 92a8e1175bSopenharmony_ci return; 93a8e1175bSopenharmony_ci } 94a8e1175bSopenharmony_ci 95a8e1175bSopenharmony_ci MBEDTLS_SSL_DEBUG_MSG(2, ("No matched ciphersuite, psk_ciphersuite_id=%x, psk_hash_alg=%x", 96a8e1175bSopenharmony_ci (unsigned) psk_ciphersuite_id, psk_hash_alg)); 97a8e1175bSopenharmony_ci} 98a8e1175bSopenharmony_ci 99a8e1175bSopenharmony_ci#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED) 100a8e1175bSopenharmony_ci/* From RFC 8446: 101a8e1175bSopenharmony_ci * 102a8e1175bSopenharmony_ci * enum { psk_ke(0), psk_dhe_ke(1), (255) } PskKeyExchangeMode; 103a8e1175bSopenharmony_ci * struct { 104a8e1175bSopenharmony_ci * PskKeyExchangeMode ke_modes<1..255>; 105a8e1175bSopenharmony_ci * } PskKeyExchangeModes; 106a8e1175bSopenharmony_ci */ 107a8e1175bSopenharmony_ciMBEDTLS_CHECK_RETURN_CRITICAL 108a8e1175bSopenharmony_cistatic int ssl_tls13_parse_key_exchange_modes_ext(mbedtls_ssl_context *ssl, 109a8e1175bSopenharmony_ci const unsigned char *buf, 110a8e1175bSopenharmony_ci const unsigned char *end) 111a8e1175bSopenharmony_ci{ 112a8e1175bSopenharmony_ci const unsigned char *p = buf; 113a8e1175bSopenharmony_ci size_t ke_modes_len; 114a8e1175bSopenharmony_ci int ke_modes = 0; 115a8e1175bSopenharmony_ci 116a8e1175bSopenharmony_ci /* Read ke_modes length (1 Byte) */ 117a8e1175bSopenharmony_ci MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 1); 118a8e1175bSopenharmony_ci ke_modes_len = *p++; 119a8e1175bSopenharmony_ci /* Currently, there are only two PSK modes, so even without looking 120a8e1175bSopenharmony_ci * at the content, something's wrong if the list has more than 2 items. */ 121a8e1175bSopenharmony_ci if (ke_modes_len > 2) { 122a8e1175bSopenharmony_ci MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER, 123a8e1175bSopenharmony_ci MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER); 124a8e1175bSopenharmony_ci return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE; 125a8e1175bSopenharmony_ci } 126a8e1175bSopenharmony_ci 127a8e1175bSopenharmony_ci MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, ke_modes_len); 128a8e1175bSopenharmony_ci 129a8e1175bSopenharmony_ci while (ke_modes_len-- != 0) { 130a8e1175bSopenharmony_ci switch (*p++) { 131a8e1175bSopenharmony_ci case MBEDTLS_SSL_TLS1_3_PSK_MODE_PURE: 132a8e1175bSopenharmony_ci ke_modes |= MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK; 133a8e1175bSopenharmony_ci MBEDTLS_SSL_DEBUG_MSG(3, ("Found PSK KEX MODE")); 134a8e1175bSopenharmony_ci break; 135a8e1175bSopenharmony_ci case MBEDTLS_SSL_TLS1_3_PSK_MODE_ECDHE: 136a8e1175bSopenharmony_ci ke_modes |= MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL; 137a8e1175bSopenharmony_ci MBEDTLS_SSL_DEBUG_MSG(3, ("Found PSK_EPHEMERAL KEX MODE")); 138a8e1175bSopenharmony_ci break; 139a8e1175bSopenharmony_ci default: 140a8e1175bSopenharmony_ci MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER, 141a8e1175bSopenharmony_ci MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER); 142a8e1175bSopenharmony_ci return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER; 143a8e1175bSopenharmony_ci } 144a8e1175bSopenharmony_ci } 145a8e1175bSopenharmony_ci 146a8e1175bSopenharmony_ci ssl->handshake->tls13_kex_modes = ke_modes; 147a8e1175bSopenharmony_ci return 0; 148a8e1175bSopenharmony_ci} 149a8e1175bSopenharmony_ci 150a8e1175bSopenharmony_ci/* 151a8e1175bSopenharmony_ci * Non-error return values of 152a8e1175bSopenharmony_ci * ssl_tls13_offered_psks_check_identity_match_ticket() and 153a8e1175bSopenharmony_ci * ssl_tls13_offered_psks_check_identity_match(). They are positive to 154a8e1175bSopenharmony_ci * not collide with error codes that are negative. Zero 155a8e1175bSopenharmony_ci * (SSL_TLS1_3_PSK_IDENTITY_MATCH) in case of success as it may be propagated 156a8e1175bSopenharmony_ci * up by the callers of this function as a generic success condition. 157a8e1175bSopenharmony_ci * 158a8e1175bSopenharmony_ci * The return value SSL_TLS1_3_PSK_IDENTITY_MATCH_BUT_PSK_NOT_USABLE means 159a8e1175bSopenharmony_ci * that the pre-shared-key identity matches that of a ticket or an externally- 160a8e1175bSopenharmony_ci * provisioned pre-shared-key. We have thus been able to retrieve the 161a8e1175bSopenharmony_ci * attributes of the pre-shared-key but at least one of them does not meet 162a8e1175bSopenharmony_ci * some criteria and the pre-shared-key cannot be used. For example, a ticket 163a8e1175bSopenharmony_ci * is expired or its version is not TLS 1.3. Note eventually that the return 164a8e1175bSopenharmony_ci * value SSL_TLS1_3_PSK_IDENTITY_MATCH_BUT_PSK_NOT_USABLE does not have 165a8e1175bSopenharmony_ci * anything to do with binder check. A binder check is done only when a 166a8e1175bSopenharmony_ci * suitable pre-shared-key has been selected and only for that selected 167a8e1175bSopenharmony_ci * pre-shared-key: if the binder check fails, we fail the handshake and we do 168a8e1175bSopenharmony_ci * not try to find another pre-shared-key for which the binder check would 169a8e1175bSopenharmony_ci * succeed as recommended by the specification. 170a8e1175bSopenharmony_ci */ 171a8e1175bSopenharmony_ci#define SSL_TLS1_3_PSK_IDENTITY_DOES_NOT_MATCH 2 172a8e1175bSopenharmony_ci#define SSL_TLS1_3_PSK_IDENTITY_MATCH_BUT_PSK_NOT_USABLE 1 173a8e1175bSopenharmony_ci#define SSL_TLS1_3_PSK_IDENTITY_MATCH 0 174a8e1175bSopenharmony_ci 175a8e1175bSopenharmony_ci#if defined(MBEDTLS_SSL_SESSION_TICKETS) 176a8e1175bSopenharmony_ciMBEDTLS_CHECK_RETURN_CRITICAL 177a8e1175bSopenharmony_cistatic int ssl_tls13_key_exchange_is_psk_available(mbedtls_ssl_context *ssl); 178a8e1175bSopenharmony_ciMBEDTLS_CHECK_RETURN_CRITICAL 179a8e1175bSopenharmony_cistatic int ssl_tls13_key_exchange_is_psk_ephemeral_available(mbedtls_ssl_context *ssl); 180a8e1175bSopenharmony_ci 181a8e1175bSopenharmony_ciMBEDTLS_CHECK_RETURN_CRITICAL 182a8e1175bSopenharmony_cistatic int ssl_tls13_offered_psks_check_identity_match_ticket( 183a8e1175bSopenharmony_ci mbedtls_ssl_context *ssl, 184a8e1175bSopenharmony_ci const unsigned char *identity, 185a8e1175bSopenharmony_ci size_t identity_len, 186a8e1175bSopenharmony_ci uint32_t obfuscated_ticket_age, 187a8e1175bSopenharmony_ci mbedtls_ssl_session *session) 188a8e1175bSopenharmony_ci{ 189a8e1175bSopenharmony_ci int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; 190a8e1175bSopenharmony_ci unsigned char *ticket_buffer; 191a8e1175bSopenharmony_ci#if defined(MBEDTLS_HAVE_TIME) 192a8e1175bSopenharmony_ci mbedtls_ms_time_t now; 193a8e1175bSopenharmony_ci mbedtls_ms_time_t server_age; 194a8e1175bSopenharmony_ci uint32_t client_age; 195a8e1175bSopenharmony_ci mbedtls_ms_time_t age_diff; 196a8e1175bSopenharmony_ci#endif 197a8e1175bSopenharmony_ci 198a8e1175bSopenharmony_ci ((void) obfuscated_ticket_age); 199a8e1175bSopenharmony_ci 200a8e1175bSopenharmony_ci MBEDTLS_SSL_DEBUG_MSG(2, ("=> check_identity_match_ticket")); 201a8e1175bSopenharmony_ci 202a8e1175bSopenharmony_ci /* Ticket parser is not configured, Skip */ 203a8e1175bSopenharmony_ci if (ssl->conf->f_ticket_parse == NULL || identity_len == 0) { 204a8e1175bSopenharmony_ci return SSL_TLS1_3_PSK_IDENTITY_DOES_NOT_MATCH; 205a8e1175bSopenharmony_ci } 206a8e1175bSopenharmony_ci 207a8e1175bSopenharmony_ci /* We create a copy of the encrypted ticket since the ticket parsing 208a8e1175bSopenharmony_ci * function is allowed to use its input buffer as an output buffer 209a8e1175bSopenharmony_ci * (in-place decryption). We do, however, need the original buffer for 210a8e1175bSopenharmony_ci * computing the PSK binder value. 211a8e1175bSopenharmony_ci */ 212a8e1175bSopenharmony_ci ticket_buffer = mbedtls_calloc(1, identity_len); 213a8e1175bSopenharmony_ci if (ticket_buffer == NULL) { 214a8e1175bSopenharmony_ci return MBEDTLS_ERR_SSL_ALLOC_FAILED; 215a8e1175bSopenharmony_ci } 216a8e1175bSopenharmony_ci memcpy(ticket_buffer, identity, identity_len); 217a8e1175bSopenharmony_ci 218a8e1175bSopenharmony_ci ret = ssl->conf->f_ticket_parse(ssl->conf->p_ticket, 219a8e1175bSopenharmony_ci session, 220a8e1175bSopenharmony_ci ticket_buffer, identity_len); 221a8e1175bSopenharmony_ci switch (ret) { 222a8e1175bSopenharmony_ci case 0: 223a8e1175bSopenharmony_ci ret = SSL_TLS1_3_PSK_IDENTITY_MATCH; 224a8e1175bSopenharmony_ci break; 225a8e1175bSopenharmony_ci 226a8e1175bSopenharmony_ci case MBEDTLS_ERR_SSL_SESSION_TICKET_EXPIRED: 227a8e1175bSopenharmony_ci MBEDTLS_SSL_DEBUG_MSG(3, ("ticket is expired")); 228a8e1175bSopenharmony_ci ret = SSL_TLS1_3_PSK_IDENTITY_MATCH_BUT_PSK_NOT_USABLE; 229a8e1175bSopenharmony_ci break; 230a8e1175bSopenharmony_ci 231a8e1175bSopenharmony_ci case MBEDTLS_ERR_SSL_INVALID_MAC: 232a8e1175bSopenharmony_ci MBEDTLS_SSL_DEBUG_MSG(3, ("ticket is not authentic")); 233a8e1175bSopenharmony_ci ret = SSL_TLS1_3_PSK_IDENTITY_DOES_NOT_MATCH; 234a8e1175bSopenharmony_ci break; 235a8e1175bSopenharmony_ci 236a8e1175bSopenharmony_ci default: 237a8e1175bSopenharmony_ci MBEDTLS_SSL_DEBUG_RET(1, "ticket_parse", ret); 238a8e1175bSopenharmony_ci ret = SSL_TLS1_3_PSK_IDENTITY_DOES_NOT_MATCH; 239a8e1175bSopenharmony_ci } 240a8e1175bSopenharmony_ci 241a8e1175bSopenharmony_ci /* We delete the temporary buffer */ 242a8e1175bSopenharmony_ci mbedtls_free(ticket_buffer); 243a8e1175bSopenharmony_ci 244a8e1175bSopenharmony_ci if (ret != SSL_TLS1_3_PSK_IDENTITY_MATCH) { 245a8e1175bSopenharmony_ci goto exit; 246a8e1175bSopenharmony_ci } 247a8e1175bSopenharmony_ci 248a8e1175bSopenharmony_ci /* 249a8e1175bSopenharmony_ci * The identity matches that of a ticket. Now check that it has suitable 250a8e1175bSopenharmony_ci * attributes and bet it will not be the case. 251a8e1175bSopenharmony_ci */ 252a8e1175bSopenharmony_ci ret = SSL_TLS1_3_PSK_IDENTITY_MATCH_BUT_PSK_NOT_USABLE; 253a8e1175bSopenharmony_ci 254a8e1175bSopenharmony_ci if (session->tls_version != MBEDTLS_SSL_VERSION_TLS1_3) { 255a8e1175bSopenharmony_ci MBEDTLS_SSL_DEBUG_MSG(3, ("Ticket TLS version is not 1.3.")); 256a8e1175bSopenharmony_ci goto exit; 257a8e1175bSopenharmony_ci } 258a8e1175bSopenharmony_ci 259a8e1175bSopenharmony_ci#if defined(MBEDTLS_HAVE_TIME) 260a8e1175bSopenharmony_ci now = mbedtls_ms_time(); 261a8e1175bSopenharmony_ci 262a8e1175bSopenharmony_ci if (now < session->ticket_creation_time) { 263a8e1175bSopenharmony_ci MBEDTLS_SSL_DEBUG_MSG( 264a8e1175bSopenharmony_ci 3, ("Invalid ticket creation time ( now = %" MBEDTLS_PRINTF_MS_TIME 265a8e1175bSopenharmony_ci ", creation_time = %" MBEDTLS_PRINTF_MS_TIME " )", 266a8e1175bSopenharmony_ci now, session->ticket_creation_time)); 267a8e1175bSopenharmony_ci goto exit; 268a8e1175bSopenharmony_ci } 269a8e1175bSopenharmony_ci 270a8e1175bSopenharmony_ci server_age = now - session->ticket_creation_time; 271a8e1175bSopenharmony_ci 272a8e1175bSopenharmony_ci /* RFC 8446 section 4.6.1 273a8e1175bSopenharmony_ci * 274a8e1175bSopenharmony_ci * Servers MUST NOT use any value greater than 604800 seconds (7 days). 275a8e1175bSopenharmony_ci * 276a8e1175bSopenharmony_ci * RFC 8446 section 4.2.11.1 277a8e1175bSopenharmony_ci * 278a8e1175bSopenharmony_ci * Clients MUST NOT attempt to use tickets which have ages greater than 279a8e1175bSopenharmony_ci * the "ticket_lifetime" value which was provided with the ticket. 280a8e1175bSopenharmony_ci * 281a8e1175bSopenharmony_ci */ 282a8e1175bSopenharmony_ci if (server_age > MBEDTLS_SSL_TLS1_3_MAX_ALLOWED_TICKET_LIFETIME * 1000) { 283a8e1175bSopenharmony_ci MBEDTLS_SSL_DEBUG_MSG( 284a8e1175bSopenharmony_ci 3, ("Ticket age exceeds limitation ticket_age = %" MBEDTLS_PRINTF_MS_TIME, 285a8e1175bSopenharmony_ci server_age)); 286a8e1175bSopenharmony_ci goto exit; 287a8e1175bSopenharmony_ci } 288a8e1175bSopenharmony_ci 289a8e1175bSopenharmony_ci /* RFC 8446 section 4.2.10 290a8e1175bSopenharmony_ci * 291a8e1175bSopenharmony_ci * For PSKs provisioned via NewSessionTicket, a server MUST validate that 292a8e1175bSopenharmony_ci * the ticket age for the selected PSK identity (computed by subtracting 293a8e1175bSopenharmony_ci * ticket_age_add from PskIdentity.obfuscated_ticket_age modulo 2^32) is 294a8e1175bSopenharmony_ci * within a small tolerance of the time since the ticket was issued. 295a8e1175bSopenharmony_ci * 296a8e1175bSopenharmony_ci * NOTE: The typical accuracy of an RTC crystal is ��100 to ��20 parts per 297a8e1175bSopenharmony_ci * million (360 to 72 milliseconds per hour). Default tolerance 298a8e1175bSopenharmony_ci * window is 6s, thus in the worst case clients and servers must 299a8e1175bSopenharmony_ci * sync up their system time every 6000/360/2~=8 hours. 300a8e1175bSopenharmony_ci */ 301a8e1175bSopenharmony_ci client_age = obfuscated_ticket_age - session->ticket_age_add; 302a8e1175bSopenharmony_ci age_diff = server_age - (mbedtls_ms_time_t) client_age; 303a8e1175bSopenharmony_ci if (age_diff < -MBEDTLS_SSL_TLS1_3_TICKET_AGE_TOLERANCE || 304a8e1175bSopenharmony_ci age_diff > MBEDTLS_SSL_TLS1_3_TICKET_AGE_TOLERANCE) { 305a8e1175bSopenharmony_ci MBEDTLS_SSL_DEBUG_MSG( 306a8e1175bSopenharmony_ci 3, ("Ticket age outside tolerance window ( diff = %" 307a8e1175bSopenharmony_ci MBEDTLS_PRINTF_MS_TIME ")", 308a8e1175bSopenharmony_ci age_diff)); 309a8e1175bSopenharmony_ci goto exit; 310a8e1175bSopenharmony_ci } 311a8e1175bSopenharmony_ci#endif /* MBEDTLS_HAVE_TIME */ 312a8e1175bSopenharmony_ci 313a8e1175bSopenharmony_ci /* 314a8e1175bSopenharmony_ci * All good, we have found a suitable ticket. 315a8e1175bSopenharmony_ci */ 316a8e1175bSopenharmony_ci ret = SSL_TLS1_3_PSK_IDENTITY_MATCH; 317a8e1175bSopenharmony_ci 318a8e1175bSopenharmony_ciexit: 319a8e1175bSopenharmony_ci if (ret != SSL_TLS1_3_PSK_IDENTITY_MATCH) { 320a8e1175bSopenharmony_ci mbedtls_ssl_session_free(session); 321a8e1175bSopenharmony_ci } 322a8e1175bSopenharmony_ci 323a8e1175bSopenharmony_ci MBEDTLS_SSL_DEBUG_MSG(2, ("<= check_identity_match_ticket")); 324a8e1175bSopenharmony_ci return ret; 325a8e1175bSopenharmony_ci} 326a8e1175bSopenharmony_ci#endif /* MBEDTLS_SSL_SESSION_TICKETS */ 327a8e1175bSopenharmony_ci 328a8e1175bSopenharmony_ciMBEDTLS_CHECK_RETURN_CRITICAL 329a8e1175bSopenharmony_cistatic int ssl_tls13_offered_psks_check_identity_match( 330a8e1175bSopenharmony_ci mbedtls_ssl_context *ssl, 331a8e1175bSopenharmony_ci const unsigned char *identity, 332a8e1175bSopenharmony_ci size_t identity_len, 333a8e1175bSopenharmony_ci uint32_t obfuscated_ticket_age, 334a8e1175bSopenharmony_ci int *psk_type, 335a8e1175bSopenharmony_ci mbedtls_ssl_session *session) 336a8e1175bSopenharmony_ci{ 337a8e1175bSopenharmony_ci int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; 338a8e1175bSopenharmony_ci 339a8e1175bSopenharmony_ci ((void) session); 340a8e1175bSopenharmony_ci ((void) obfuscated_ticket_age); 341a8e1175bSopenharmony_ci *psk_type = MBEDTLS_SSL_TLS1_3_PSK_EXTERNAL; 342a8e1175bSopenharmony_ci 343a8e1175bSopenharmony_ci MBEDTLS_SSL_DEBUG_BUF(4, "identity", identity, identity_len); 344a8e1175bSopenharmony_ci 345a8e1175bSopenharmony_ci#if defined(MBEDTLS_SSL_SESSION_TICKETS) 346a8e1175bSopenharmony_ci ret = ssl_tls13_offered_psks_check_identity_match_ticket( 347a8e1175bSopenharmony_ci ssl, identity, identity_len, obfuscated_ticket_age, session); 348a8e1175bSopenharmony_ci if (ret == SSL_TLS1_3_PSK_IDENTITY_MATCH) { 349a8e1175bSopenharmony_ci *psk_type = MBEDTLS_SSL_TLS1_3_PSK_RESUMPTION; 350a8e1175bSopenharmony_ci ret = mbedtls_ssl_set_hs_psk(ssl, 351a8e1175bSopenharmony_ci session->resumption_key, 352a8e1175bSopenharmony_ci session->resumption_key_len); 353a8e1175bSopenharmony_ci if (ret != 0) { 354a8e1175bSopenharmony_ci MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_set_hs_psk", ret); 355a8e1175bSopenharmony_ci return ret; 356a8e1175bSopenharmony_ci } 357a8e1175bSopenharmony_ci 358a8e1175bSopenharmony_ci MBEDTLS_SSL_DEBUG_BUF(4, "Ticket-resumed PSK:", 359a8e1175bSopenharmony_ci session->resumption_key, 360a8e1175bSopenharmony_ci session->resumption_key_len); 361a8e1175bSopenharmony_ci MBEDTLS_SSL_DEBUG_MSG(4, ("ticket: obfuscated_ticket_age: %u", 362a8e1175bSopenharmony_ci (unsigned) obfuscated_ticket_age)); 363a8e1175bSopenharmony_ci return SSL_TLS1_3_PSK_IDENTITY_MATCH; 364a8e1175bSopenharmony_ci } else if (ret == SSL_TLS1_3_PSK_IDENTITY_MATCH_BUT_PSK_NOT_USABLE) { 365a8e1175bSopenharmony_ci return SSL_TLS1_3_PSK_IDENTITY_MATCH_BUT_PSK_NOT_USABLE; 366a8e1175bSopenharmony_ci } 367a8e1175bSopenharmony_ci#endif /* MBEDTLS_SSL_SESSION_TICKETS */ 368a8e1175bSopenharmony_ci 369a8e1175bSopenharmony_ci /* Check identity with external configured function */ 370a8e1175bSopenharmony_ci if (ssl->conf->f_psk != NULL) { 371a8e1175bSopenharmony_ci if (ssl->conf->f_psk( 372a8e1175bSopenharmony_ci ssl->conf->p_psk, ssl, identity, identity_len) == 0) { 373a8e1175bSopenharmony_ci return SSL_TLS1_3_PSK_IDENTITY_MATCH; 374a8e1175bSopenharmony_ci } 375a8e1175bSopenharmony_ci return SSL_TLS1_3_PSK_IDENTITY_DOES_NOT_MATCH; 376a8e1175bSopenharmony_ci } 377a8e1175bSopenharmony_ci 378a8e1175bSopenharmony_ci MBEDTLS_SSL_DEBUG_BUF(5, "identity", identity, identity_len); 379a8e1175bSopenharmony_ci /* Check identity with pre-configured psk */ 380a8e1175bSopenharmony_ci if (ssl->conf->psk_identity != NULL && 381a8e1175bSopenharmony_ci identity_len == ssl->conf->psk_identity_len && 382a8e1175bSopenharmony_ci mbedtls_ct_memcmp(ssl->conf->psk_identity, 383a8e1175bSopenharmony_ci identity, identity_len) == 0) { 384a8e1175bSopenharmony_ci ret = mbedtls_ssl_set_hs_psk(ssl, ssl->conf->psk, ssl->conf->psk_len); 385a8e1175bSopenharmony_ci if (ret != 0) { 386a8e1175bSopenharmony_ci MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_set_hs_psk", ret); 387a8e1175bSopenharmony_ci return ret; 388a8e1175bSopenharmony_ci } 389a8e1175bSopenharmony_ci return SSL_TLS1_3_PSK_IDENTITY_MATCH; 390a8e1175bSopenharmony_ci } 391a8e1175bSopenharmony_ci 392a8e1175bSopenharmony_ci return SSL_TLS1_3_PSK_IDENTITY_DOES_NOT_MATCH; 393a8e1175bSopenharmony_ci} 394a8e1175bSopenharmony_ci 395a8e1175bSopenharmony_ci/* 396a8e1175bSopenharmony_ci * Non-error return values of ssl_tls13_offered_psks_check_binder_match(). 397a8e1175bSopenharmony_ci * They are positive to not collide with error codes that are negative. Zero 398a8e1175bSopenharmony_ci * (SSL_TLS1_3_BINDER_MATCH) in case of success as it may be propagated up 399a8e1175bSopenharmony_ci * by the callers of this function as a generic success condition. 400a8e1175bSopenharmony_ci */ 401a8e1175bSopenharmony_ci#define SSL_TLS1_3_BINDER_DOES_NOT_MATCH 1 402a8e1175bSopenharmony_ci#define SSL_TLS1_3_BINDER_MATCH 0 403a8e1175bSopenharmony_ciMBEDTLS_CHECK_RETURN_CRITICAL 404a8e1175bSopenharmony_cistatic int ssl_tls13_offered_psks_check_binder_match( 405a8e1175bSopenharmony_ci mbedtls_ssl_context *ssl, 406a8e1175bSopenharmony_ci const unsigned char *binder, size_t binder_len, 407a8e1175bSopenharmony_ci int psk_type, psa_algorithm_t psk_hash_alg) 408a8e1175bSopenharmony_ci{ 409a8e1175bSopenharmony_ci int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; 410a8e1175bSopenharmony_ci 411a8e1175bSopenharmony_ci unsigned char transcript[PSA_HASH_MAX_SIZE]; 412a8e1175bSopenharmony_ci size_t transcript_len; 413a8e1175bSopenharmony_ci unsigned char *psk; 414a8e1175bSopenharmony_ci size_t psk_len; 415a8e1175bSopenharmony_ci unsigned char server_computed_binder[PSA_HASH_MAX_SIZE]; 416a8e1175bSopenharmony_ci 417a8e1175bSopenharmony_ci if (binder_len != PSA_HASH_LENGTH(psk_hash_alg)) { 418a8e1175bSopenharmony_ci return SSL_TLS1_3_BINDER_DOES_NOT_MATCH; 419a8e1175bSopenharmony_ci } 420a8e1175bSopenharmony_ci 421a8e1175bSopenharmony_ci /* Get current state of handshake transcript. */ 422a8e1175bSopenharmony_ci ret = mbedtls_ssl_get_handshake_transcript( 423a8e1175bSopenharmony_ci ssl, mbedtls_md_type_from_psa_alg(psk_hash_alg), 424a8e1175bSopenharmony_ci transcript, sizeof(transcript), &transcript_len); 425a8e1175bSopenharmony_ci if (ret != 0) { 426a8e1175bSopenharmony_ci return ret; 427a8e1175bSopenharmony_ci } 428a8e1175bSopenharmony_ci 429a8e1175bSopenharmony_ci ret = mbedtls_ssl_tls13_export_handshake_psk(ssl, &psk, &psk_len); 430a8e1175bSopenharmony_ci if (ret != 0) { 431a8e1175bSopenharmony_ci return ret; 432a8e1175bSopenharmony_ci } 433a8e1175bSopenharmony_ci 434a8e1175bSopenharmony_ci ret = mbedtls_ssl_tls13_create_psk_binder(ssl, psk_hash_alg, 435a8e1175bSopenharmony_ci psk, psk_len, psk_type, 436a8e1175bSopenharmony_ci transcript, 437a8e1175bSopenharmony_ci server_computed_binder); 438a8e1175bSopenharmony_ci#if defined(MBEDTLS_USE_PSA_CRYPTO) 439a8e1175bSopenharmony_ci mbedtls_free((void *) psk); 440a8e1175bSopenharmony_ci#endif 441a8e1175bSopenharmony_ci if (ret != 0) { 442a8e1175bSopenharmony_ci MBEDTLS_SSL_DEBUG_MSG(1, ("PSK binder calculation failed.")); 443a8e1175bSopenharmony_ci return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE; 444a8e1175bSopenharmony_ci } 445a8e1175bSopenharmony_ci 446a8e1175bSopenharmony_ci MBEDTLS_SSL_DEBUG_BUF(3, "psk binder ( computed ): ", 447a8e1175bSopenharmony_ci server_computed_binder, transcript_len); 448a8e1175bSopenharmony_ci MBEDTLS_SSL_DEBUG_BUF(3, "psk binder ( received ): ", binder, binder_len); 449a8e1175bSopenharmony_ci 450a8e1175bSopenharmony_ci if (mbedtls_ct_memcmp(server_computed_binder, 451a8e1175bSopenharmony_ci binder, 452a8e1175bSopenharmony_ci PSA_HASH_LENGTH(psk_hash_alg)) == 0) { 453a8e1175bSopenharmony_ci return SSL_TLS1_3_BINDER_MATCH; 454a8e1175bSopenharmony_ci } 455a8e1175bSopenharmony_ci 456a8e1175bSopenharmony_ci mbedtls_platform_zeroize(server_computed_binder, 457a8e1175bSopenharmony_ci sizeof(server_computed_binder)); 458a8e1175bSopenharmony_ci return SSL_TLS1_3_BINDER_DOES_NOT_MATCH; 459a8e1175bSopenharmony_ci} 460a8e1175bSopenharmony_ci 461a8e1175bSopenharmony_ci#if defined(MBEDTLS_SSL_SESSION_TICKETS) 462a8e1175bSopenharmony_ciMBEDTLS_CHECK_RETURN_CRITICAL 463a8e1175bSopenharmony_cistatic int ssl_tls13_session_copy_ticket(mbedtls_ssl_session *dst, 464a8e1175bSopenharmony_ci const mbedtls_ssl_session *src) 465a8e1175bSopenharmony_ci{ 466a8e1175bSopenharmony_ci dst->ticket_age_add = src->ticket_age_add; 467a8e1175bSopenharmony_ci dst->ticket_flags = src->ticket_flags; 468a8e1175bSopenharmony_ci dst->resumption_key_len = src->resumption_key_len; 469a8e1175bSopenharmony_ci if (src->resumption_key_len == 0) { 470a8e1175bSopenharmony_ci return MBEDTLS_ERR_SSL_INTERNAL_ERROR; 471a8e1175bSopenharmony_ci } 472a8e1175bSopenharmony_ci memcpy(dst->resumption_key, src->resumption_key, src->resumption_key_len); 473a8e1175bSopenharmony_ci 474a8e1175bSopenharmony_ci#if defined(MBEDTLS_SSL_EARLY_DATA) 475a8e1175bSopenharmony_ci dst->max_early_data_size = src->max_early_data_size; 476a8e1175bSopenharmony_ci 477a8e1175bSopenharmony_ci#if defined(MBEDTLS_SSL_ALPN) 478a8e1175bSopenharmony_ci int ret = mbedtls_ssl_session_set_ticket_alpn(dst, src->ticket_alpn); 479a8e1175bSopenharmony_ci if (ret != 0) { 480a8e1175bSopenharmony_ci return ret; 481a8e1175bSopenharmony_ci } 482a8e1175bSopenharmony_ci#endif /* MBEDTLS_SSL_ALPN */ 483a8e1175bSopenharmony_ci#endif /* MBEDTLS_SSL_EARLY_DATA*/ 484a8e1175bSopenharmony_ci 485a8e1175bSopenharmony_ci return 0; 486a8e1175bSopenharmony_ci} 487a8e1175bSopenharmony_ci#endif /* MBEDTLS_SSL_SESSION_TICKETS */ 488a8e1175bSopenharmony_ci 489a8e1175bSopenharmony_cistruct psk_attributes { 490a8e1175bSopenharmony_ci int type; 491a8e1175bSopenharmony_ci int key_exchange_mode; 492a8e1175bSopenharmony_ci const mbedtls_ssl_ciphersuite_t *ciphersuite_info; 493a8e1175bSopenharmony_ci}; 494a8e1175bSopenharmony_ci#define PSK_ATTRIBUTES_INIT { 0, 0, NULL } 495a8e1175bSopenharmony_ci 496a8e1175bSopenharmony_ci/* Parser for pre_shared_key extension in client hello 497a8e1175bSopenharmony_ci * struct { 498a8e1175bSopenharmony_ci * opaque identity<1..2^16-1>; 499a8e1175bSopenharmony_ci * uint32 obfuscated_ticket_age; 500a8e1175bSopenharmony_ci * } PskIdentity; 501a8e1175bSopenharmony_ci * 502a8e1175bSopenharmony_ci * opaque PskBinderEntry<32..255>; 503a8e1175bSopenharmony_ci * 504a8e1175bSopenharmony_ci * struct { 505a8e1175bSopenharmony_ci * PskIdentity identities<7..2^16-1>; 506a8e1175bSopenharmony_ci * PskBinderEntry binders<33..2^16-1>; 507a8e1175bSopenharmony_ci * } OfferedPsks; 508a8e1175bSopenharmony_ci * 509a8e1175bSopenharmony_ci * struct { 510a8e1175bSopenharmony_ci * select (Handshake.msg_type) { 511a8e1175bSopenharmony_ci * case client_hello: OfferedPsks; 512a8e1175bSopenharmony_ci * .... 513a8e1175bSopenharmony_ci * }; 514a8e1175bSopenharmony_ci * } PreSharedKeyExtension; 515a8e1175bSopenharmony_ci */ 516a8e1175bSopenharmony_ciMBEDTLS_CHECK_RETURN_CRITICAL 517a8e1175bSopenharmony_cistatic int ssl_tls13_parse_pre_shared_key_ext( 518a8e1175bSopenharmony_ci mbedtls_ssl_context *ssl, 519a8e1175bSopenharmony_ci const unsigned char *pre_shared_key_ext, 520a8e1175bSopenharmony_ci const unsigned char *pre_shared_key_ext_end, 521a8e1175bSopenharmony_ci const unsigned char *ciphersuites, 522a8e1175bSopenharmony_ci const unsigned char *ciphersuites_end, 523a8e1175bSopenharmony_ci struct psk_attributes *psk) 524a8e1175bSopenharmony_ci{ 525a8e1175bSopenharmony_ci int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; 526a8e1175bSopenharmony_ci const unsigned char *identities = pre_shared_key_ext; 527a8e1175bSopenharmony_ci const unsigned char *p_identity_len; 528a8e1175bSopenharmony_ci size_t identities_len; 529a8e1175bSopenharmony_ci const unsigned char *identities_end; 530a8e1175bSopenharmony_ci const unsigned char *binders; 531a8e1175bSopenharmony_ci const unsigned char *p_binder_len; 532a8e1175bSopenharmony_ci size_t binders_len; 533a8e1175bSopenharmony_ci const unsigned char *binders_end; 534a8e1175bSopenharmony_ci int matched_identity = -1; 535a8e1175bSopenharmony_ci int identity_id = -1; 536a8e1175bSopenharmony_ci 537a8e1175bSopenharmony_ci MBEDTLS_SSL_DEBUG_BUF(3, "pre_shared_key extension", 538a8e1175bSopenharmony_ci pre_shared_key_ext, 539a8e1175bSopenharmony_ci pre_shared_key_ext_end - pre_shared_key_ext); 540a8e1175bSopenharmony_ci 541a8e1175bSopenharmony_ci /* identities_len 2 bytes 542a8e1175bSopenharmony_ci * identities_data >= 7 bytes 543a8e1175bSopenharmony_ci */ 544a8e1175bSopenharmony_ci MBEDTLS_SSL_CHK_BUF_READ_PTR(identities, pre_shared_key_ext_end, 7 + 2); 545a8e1175bSopenharmony_ci identities_len = MBEDTLS_GET_UINT16_BE(identities, 0); 546a8e1175bSopenharmony_ci p_identity_len = identities + 2; 547a8e1175bSopenharmony_ci MBEDTLS_SSL_CHK_BUF_READ_PTR(p_identity_len, pre_shared_key_ext_end, 548a8e1175bSopenharmony_ci identities_len); 549a8e1175bSopenharmony_ci identities_end = p_identity_len + identities_len; 550a8e1175bSopenharmony_ci 551a8e1175bSopenharmony_ci /* binders_len 2 bytes 552a8e1175bSopenharmony_ci * binders >= 33 bytes 553a8e1175bSopenharmony_ci */ 554a8e1175bSopenharmony_ci binders = identities_end; 555a8e1175bSopenharmony_ci MBEDTLS_SSL_CHK_BUF_READ_PTR(binders, pre_shared_key_ext_end, 33 + 2); 556a8e1175bSopenharmony_ci binders_len = MBEDTLS_GET_UINT16_BE(binders, 0); 557a8e1175bSopenharmony_ci p_binder_len = binders + 2; 558a8e1175bSopenharmony_ci MBEDTLS_SSL_CHK_BUF_READ_PTR(p_binder_len, pre_shared_key_ext_end, binders_len); 559a8e1175bSopenharmony_ci binders_end = p_binder_len + binders_len; 560a8e1175bSopenharmony_ci 561a8e1175bSopenharmony_ci ret = ssl->handshake->update_checksum(ssl, pre_shared_key_ext, 562a8e1175bSopenharmony_ci identities_end - pre_shared_key_ext); 563a8e1175bSopenharmony_ci if (0 != ret) { 564a8e1175bSopenharmony_ci MBEDTLS_SSL_DEBUG_RET(1, ("update_checksum"), ret); 565a8e1175bSopenharmony_ci return ret; 566a8e1175bSopenharmony_ci } 567a8e1175bSopenharmony_ci 568a8e1175bSopenharmony_ci while (p_identity_len < identities_end && p_binder_len < binders_end) { 569a8e1175bSopenharmony_ci const unsigned char *identity; 570a8e1175bSopenharmony_ci size_t identity_len; 571a8e1175bSopenharmony_ci uint32_t obfuscated_ticket_age; 572a8e1175bSopenharmony_ci const unsigned char *binder; 573a8e1175bSopenharmony_ci size_t binder_len; 574a8e1175bSopenharmony_ci int psk_ciphersuite_id; 575a8e1175bSopenharmony_ci psa_algorithm_t psk_hash_alg; 576a8e1175bSopenharmony_ci int allowed_key_exchange_modes; 577a8e1175bSopenharmony_ci 578a8e1175bSopenharmony_ci#if defined(MBEDTLS_SSL_SESSION_TICKETS) 579a8e1175bSopenharmony_ci mbedtls_ssl_session session; 580a8e1175bSopenharmony_ci mbedtls_ssl_session_init(&session); 581a8e1175bSopenharmony_ci#endif 582a8e1175bSopenharmony_ci 583a8e1175bSopenharmony_ci MBEDTLS_SSL_CHK_BUF_READ_PTR(p_identity_len, identities_end, 2 + 1 + 4); 584a8e1175bSopenharmony_ci identity_len = MBEDTLS_GET_UINT16_BE(p_identity_len, 0); 585a8e1175bSopenharmony_ci identity = p_identity_len + 2; 586a8e1175bSopenharmony_ci MBEDTLS_SSL_CHK_BUF_READ_PTR(identity, identities_end, identity_len + 4); 587a8e1175bSopenharmony_ci obfuscated_ticket_age = MBEDTLS_GET_UINT32_BE(identity, identity_len); 588a8e1175bSopenharmony_ci p_identity_len += identity_len + 6; 589a8e1175bSopenharmony_ci 590a8e1175bSopenharmony_ci MBEDTLS_SSL_CHK_BUF_READ_PTR(p_binder_len, binders_end, 1 + 32); 591a8e1175bSopenharmony_ci binder_len = *p_binder_len; 592a8e1175bSopenharmony_ci binder = p_binder_len + 1; 593a8e1175bSopenharmony_ci MBEDTLS_SSL_CHK_BUF_READ_PTR(binder, binders_end, binder_len); 594a8e1175bSopenharmony_ci p_binder_len += binder_len + 1; 595a8e1175bSopenharmony_ci 596a8e1175bSopenharmony_ci identity_id++; 597a8e1175bSopenharmony_ci if (matched_identity != -1) { 598a8e1175bSopenharmony_ci continue; 599a8e1175bSopenharmony_ci } 600a8e1175bSopenharmony_ci 601a8e1175bSopenharmony_ci ret = ssl_tls13_offered_psks_check_identity_match( 602a8e1175bSopenharmony_ci ssl, identity, identity_len, obfuscated_ticket_age, 603a8e1175bSopenharmony_ci &psk->type, &session); 604a8e1175bSopenharmony_ci if (ret != SSL_TLS1_3_PSK_IDENTITY_MATCH) { 605a8e1175bSopenharmony_ci continue; 606a8e1175bSopenharmony_ci } 607a8e1175bSopenharmony_ci 608a8e1175bSopenharmony_ci MBEDTLS_SSL_DEBUG_MSG(4, ("found matched identity")); 609a8e1175bSopenharmony_ci 610a8e1175bSopenharmony_ci switch (psk->type) { 611a8e1175bSopenharmony_ci case MBEDTLS_SSL_TLS1_3_PSK_EXTERNAL: 612a8e1175bSopenharmony_ci psk_ciphersuite_id = 0; 613a8e1175bSopenharmony_ci psk_hash_alg = PSA_ALG_SHA_256; 614a8e1175bSopenharmony_ci allowed_key_exchange_modes = 615a8e1175bSopenharmony_ci MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_ALL; 616a8e1175bSopenharmony_ci break; 617a8e1175bSopenharmony_ci#if defined(MBEDTLS_SSL_SESSION_TICKETS) 618a8e1175bSopenharmony_ci case MBEDTLS_SSL_TLS1_3_PSK_RESUMPTION: 619a8e1175bSopenharmony_ci psk_ciphersuite_id = session.ciphersuite; 620a8e1175bSopenharmony_ci psk_hash_alg = PSA_ALG_NONE; 621a8e1175bSopenharmony_ci ssl->session_negotiate->ticket_flags = session.ticket_flags; 622a8e1175bSopenharmony_ci allowed_key_exchange_modes = 623a8e1175bSopenharmony_ci session.ticket_flags & 624a8e1175bSopenharmony_ci MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_ALL; 625a8e1175bSopenharmony_ci break; 626a8e1175bSopenharmony_ci#endif 627a8e1175bSopenharmony_ci default: 628a8e1175bSopenharmony_ci return MBEDTLS_ERR_SSL_INTERNAL_ERROR; 629a8e1175bSopenharmony_ci } 630a8e1175bSopenharmony_ci 631a8e1175bSopenharmony_ci psk->key_exchange_mode = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_NONE; 632a8e1175bSopenharmony_ci 633a8e1175bSopenharmony_ci if ((allowed_key_exchange_modes & 634a8e1175bSopenharmony_ci MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL) && 635a8e1175bSopenharmony_ci ssl_tls13_key_exchange_is_psk_ephemeral_available(ssl)) { 636a8e1175bSopenharmony_ci psk->key_exchange_mode = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL; 637a8e1175bSopenharmony_ci } else if ((allowed_key_exchange_modes & 638a8e1175bSopenharmony_ci MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK) && 639a8e1175bSopenharmony_ci ssl_tls13_key_exchange_is_psk_available(ssl)) { 640a8e1175bSopenharmony_ci psk->key_exchange_mode = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK; 641a8e1175bSopenharmony_ci } 642a8e1175bSopenharmony_ci 643a8e1175bSopenharmony_ci if (psk->key_exchange_mode == MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_NONE) { 644a8e1175bSopenharmony_ci MBEDTLS_SSL_DEBUG_MSG(3, ("No suitable PSK key exchange mode")); 645a8e1175bSopenharmony_ci continue; 646a8e1175bSopenharmony_ci } 647a8e1175bSopenharmony_ci 648a8e1175bSopenharmony_ci ssl_tls13_select_ciphersuite(ssl, ciphersuites, ciphersuites_end, 649a8e1175bSopenharmony_ci psk_ciphersuite_id, psk_hash_alg, 650a8e1175bSopenharmony_ci &psk->ciphersuite_info); 651a8e1175bSopenharmony_ci 652a8e1175bSopenharmony_ci if (psk->ciphersuite_info == NULL) { 653a8e1175bSopenharmony_ci#if defined(MBEDTLS_SSL_SESSION_TICKETS) 654a8e1175bSopenharmony_ci mbedtls_ssl_session_free(&session); 655a8e1175bSopenharmony_ci#endif 656a8e1175bSopenharmony_ci /* 657a8e1175bSopenharmony_ci * We consider finding a ciphersuite suitable for the PSK as part 658a8e1175bSopenharmony_ci * of the validation of its binder. Thus if we do not find one, we 659a8e1175bSopenharmony_ci * abort the handshake with a decrypt_error alert. 660a8e1175bSopenharmony_ci */ 661a8e1175bSopenharmony_ci MBEDTLS_SSL_PEND_FATAL_ALERT( 662a8e1175bSopenharmony_ci MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR, 663a8e1175bSopenharmony_ci MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE); 664a8e1175bSopenharmony_ci return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE; 665a8e1175bSopenharmony_ci } 666a8e1175bSopenharmony_ci 667a8e1175bSopenharmony_ci ret = ssl_tls13_offered_psks_check_binder_match( 668a8e1175bSopenharmony_ci ssl, binder, binder_len, psk->type, 669a8e1175bSopenharmony_ci mbedtls_md_psa_alg_from_type((mbedtls_md_type_t) psk->ciphersuite_info->mac)); 670a8e1175bSopenharmony_ci if (ret != SSL_TLS1_3_BINDER_MATCH) { 671a8e1175bSopenharmony_ci /* For security reasons, the handshake should be aborted when we 672a8e1175bSopenharmony_ci * fail to validate a binder value. See RFC 8446 section 4.2.11.2 673a8e1175bSopenharmony_ci * and appendix E.6. */ 674a8e1175bSopenharmony_ci#if defined(MBEDTLS_SSL_SESSION_TICKETS) 675a8e1175bSopenharmony_ci mbedtls_ssl_session_free(&session); 676a8e1175bSopenharmony_ci#endif 677a8e1175bSopenharmony_ci MBEDTLS_SSL_DEBUG_MSG(3, ("Invalid binder.")); 678a8e1175bSopenharmony_ci MBEDTLS_SSL_DEBUG_RET( 679a8e1175bSopenharmony_ci 1, "ssl_tls13_offered_psks_check_binder_match", ret); 680a8e1175bSopenharmony_ci MBEDTLS_SSL_PEND_FATAL_ALERT( 681a8e1175bSopenharmony_ci MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR, 682a8e1175bSopenharmony_ci MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE); 683a8e1175bSopenharmony_ci return ret; 684a8e1175bSopenharmony_ci } 685a8e1175bSopenharmony_ci 686a8e1175bSopenharmony_ci matched_identity = identity_id; 687a8e1175bSopenharmony_ci 688a8e1175bSopenharmony_ci#if defined(MBEDTLS_SSL_SESSION_TICKETS) 689a8e1175bSopenharmony_ci if (psk->type == MBEDTLS_SSL_TLS1_3_PSK_RESUMPTION) { 690a8e1175bSopenharmony_ci ret = ssl_tls13_session_copy_ticket(ssl->session_negotiate, 691a8e1175bSopenharmony_ci &session); 692a8e1175bSopenharmony_ci mbedtls_ssl_session_free(&session); 693a8e1175bSopenharmony_ci if (ret != 0) { 694a8e1175bSopenharmony_ci return ret; 695a8e1175bSopenharmony_ci } 696a8e1175bSopenharmony_ci } 697a8e1175bSopenharmony_ci#endif /* MBEDTLS_SSL_SESSION_TICKETS */ 698a8e1175bSopenharmony_ci } 699a8e1175bSopenharmony_ci 700a8e1175bSopenharmony_ci if (p_identity_len != identities_end || p_binder_len != binders_end) { 701a8e1175bSopenharmony_ci MBEDTLS_SSL_DEBUG_MSG(3, ("pre_shared_key extension decode error")); 702a8e1175bSopenharmony_ci MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR, 703a8e1175bSopenharmony_ci MBEDTLS_ERR_SSL_DECODE_ERROR); 704a8e1175bSopenharmony_ci return MBEDTLS_ERR_SSL_DECODE_ERROR; 705a8e1175bSopenharmony_ci } 706a8e1175bSopenharmony_ci 707a8e1175bSopenharmony_ci /* Update the handshake transcript with the binder list. */ 708a8e1175bSopenharmony_ci ret = ssl->handshake->update_checksum( 709a8e1175bSopenharmony_ci ssl, identities_end, (size_t) (binders_end - identities_end)); 710a8e1175bSopenharmony_ci if (0 != ret) { 711a8e1175bSopenharmony_ci MBEDTLS_SSL_DEBUG_RET(1, ("update_checksum"), ret); 712a8e1175bSopenharmony_ci return ret; 713a8e1175bSopenharmony_ci } 714a8e1175bSopenharmony_ci if (matched_identity == -1) { 715a8e1175bSopenharmony_ci MBEDTLS_SSL_DEBUG_MSG(3, ("No usable PSK or ticket.")); 716a8e1175bSopenharmony_ci return MBEDTLS_ERR_SSL_UNKNOWN_IDENTITY; 717a8e1175bSopenharmony_ci } 718a8e1175bSopenharmony_ci 719a8e1175bSopenharmony_ci ssl->handshake->selected_identity = (uint16_t) matched_identity; 720a8e1175bSopenharmony_ci MBEDTLS_SSL_DEBUG_MSG(3, ("Pre shared key found")); 721a8e1175bSopenharmony_ci 722a8e1175bSopenharmony_ci return 0; 723a8e1175bSopenharmony_ci} 724a8e1175bSopenharmony_ci 725a8e1175bSopenharmony_ci/* 726a8e1175bSopenharmony_ci * struct { 727a8e1175bSopenharmony_ci * select ( Handshake.msg_type ) { 728a8e1175bSopenharmony_ci * .... 729a8e1175bSopenharmony_ci * case server_hello: 730a8e1175bSopenharmony_ci * uint16 selected_identity; 731a8e1175bSopenharmony_ci * } 732a8e1175bSopenharmony_ci * } PreSharedKeyExtension; 733a8e1175bSopenharmony_ci */ 734a8e1175bSopenharmony_cistatic int ssl_tls13_write_server_pre_shared_key_ext(mbedtls_ssl_context *ssl, 735a8e1175bSopenharmony_ci unsigned char *buf, 736a8e1175bSopenharmony_ci unsigned char *end, 737a8e1175bSopenharmony_ci size_t *olen) 738a8e1175bSopenharmony_ci{ 739a8e1175bSopenharmony_ci unsigned char *p = (unsigned char *) buf; 740a8e1175bSopenharmony_ci 741a8e1175bSopenharmony_ci *olen = 0; 742a8e1175bSopenharmony_ci 743a8e1175bSopenharmony_ci int not_using_psk = 0; 744a8e1175bSopenharmony_ci#if defined(MBEDTLS_USE_PSA_CRYPTO) 745a8e1175bSopenharmony_ci not_using_psk = (mbedtls_svc_key_id_is_null(ssl->handshake->psk_opaque)); 746a8e1175bSopenharmony_ci#else 747a8e1175bSopenharmony_ci not_using_psk = (ssl->handshake->psk == NULL); 748a8e1175bSopenharmony_ci#endif 749a8e1175bSopenharmony_ci if (not_using_psk) { 750a8e1175bSopenharmony_ci /* We shouldn't have called this extension writer unless we've 751a8e1175bSopenharmony_ci * chosen to use a PSK. */ 752a8e1175bSopenharmony_ci return MBEDTLS_ERR_SSL_INTERNAL_ERROR; 753a8e1175bSopenharmony_ci } 754a8e1175bSopenharmony_ci 755a8e1175bSopenharmony_ci MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, adding pre_shared_key extension")); 756a8e1175bSopenharmony_ci MBEDTLS_SSL_CHK_BUF_PTR(p, end, 6); 757a8e1175bSopenharmony_ci 758a8e1175bSopenharmony_ci MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_PRE_SHARED_KEY, p, 0); 759a8e1175bSopenharmony_ci MBEDTLS_PUT_UINT16_BE(2, p, 2); 760a8e1175bSopenharmony_ci 761a8e1175bSopenharmony_ci MBEDTLS_PUT_UINT16_BE(ssl->handshake->selected_identity, p, 4); 762a8e1175bSopenharmony_ci 763a8e1175bSopenharmony_ci *olen = 6; 764a8e1175bSopenharmony_ci 765a8e1175bSopenharmony_ci MBEDTLS_SSL_DEBUG_MSG(4, ("sent selected_identity: %u", 766a8e1175bSopenharmony_ci ssl->handshake->selected_identity)); 767a8e1175bSopenharmony_ci 768a8e1175bSopenharmony_ci mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_PRE_SHARED_KEY); 769a8e1175bSopenharmony_ci 770a8e1175bSopenharmony_ci return 0; 771a8e1175bSopenharmony_ci} 772a8e1175bSopenharmony_ci 773a8e1175bSopenharmony_ci#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED */ 774a8e1175bSopenharmony_ci 775a8e1175bSopenharmony_ci/* From RFC 8446: 776a8e1175bSopenharmony_ci * struct { 777a8e1175bSopenharmony_ci * ProtocolVersion versions<2..254>; 778a8e1175bSopenharmony_ci * } SupportedVersions; 779a8e1175bSopenharmony_ci */ 780a8e1175bSopenharmony_ciMBEDTLS_CHECK_RETURN_CRITICAL 781a8e1175bSopenharmony_cistatic int ssl_tls13_parse_supported_versions_ext(mbedtls_ssl_context *ssl, 782a8e1175bSopenharmony_ci const unsigned char *buf, 783a8e1175bSopenharmony_ci const unsigned char *end) 784a8e1175bSopenharmony_ci{ 785a8e1175bSopenharmony_ci const unsigned char *p = buf; 786a8e1175bSopenharmony_ci size_t versions_len; 787a8e1175bSopenharmony_ci const unsigned char *versions_end; 788a8e1175bSopenharmony_ci uint16_t tls_version; 789a8e1175bSopenharmony_ci int found_supported_version = 0; 790a8e1175bSopenharmony_ci 791a8e1175bSopenharmony_ci MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 1); 792a8e1175bSopenharmony_ci versions_len = p[0]; 793a8e1175bSopenharmony_ci p += 1; 794a8e1175bSopenharmony_ci 795a8e1175bSopenharmony_ci MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, versions_len); 796a8e1175bSopenharmony_ci versions_end = p + versions_len; 797a8e1175bSopenharmony_ci while (p < versions_end) { 798a8e1175bSopenharmony_ci MBEDTLS_SSL_CHK_BUF_READ_PTR(p, versions_end, 2); 799a8e1175bSopenharmony_ci tls_version = mbedtls_ssl_read_version(p, ssl->conf->transport); 800a8e1175bSopenharmony_ci p += 2; 801a8e1175bSopenharmony_ci 802a8e1175bSopenharmony_ci if (MBEDTLS_SSL_VERSION_TLS1_3 == tls_version) { 803a8e1175bSopenharmony_ci found_supported_version = 1; 804a8e1175bSopenharmony_ci break; 805a8e1175bSopenharmony_ci } 806a8e1175bSopenharmony_ci 807a8e1175bSopenharmony_ci if ((MBEDTLS_SSL_VERSION_TLS1_2 == tls_version) && 808a8e1175bSopenharmony_ci mbedtls_ssl_conf_is_tls12_enabled(ssl->conf)) { 809a8e1175bSopenharmony_ci found_supported_version = 1; 810a8e1175bSopenharmony_ci break; 811a8e1175bSopenharmony_ci } 812a8e1175bSopenharmony_ci } 813a8e1175bSopenharmony_ci 814a8e1175bSopenharmony_ci if (!found_supported_version) { 815a8e1175bSopenharmony_ci MBEDTLS_SSL_DEBUG_MSG(1, ("No supported version found.")); 816a8e1175bSopenharmony_ci 817a8e1175bSopenharmony_ci MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION, 818a8e1175bSopenharmony_ci MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION); 819a8e1175bSopenharmony_ci return MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION; 820a8e1175bSopenharmony_ci } 821a8e1175bSopenharmony_ci 822a8e1175bSopenharmony_ci MBEDTLS_SSL_DEBUG_MSG(1, ("Negotiated version: [%04x]", 823a8e1175bSopenharmony_ci (unsigned int) tls_version)); 824a8e1175bSopenharmony_ci 825a8e1175bSopenharmony_ci return (int) tls_version; 826a8e1175bSopenharmony_ci} 827a8e1175bSopenharmony_ci 828a8e1175bSopenharmony_ci#if defined(PSA_WANT_ALG_ECDH) || defined(PSA_WANT_ALG_FFDH) 829a8e1175bSopenharmony_ci/* 830a8e1175bSopenharmony_ci * 831a8e1175bSopenharmony_ci * From RFC 8446: 832a8e1175bSopenharmony_ci * enum { 833a8e1175bSopenharmony_ci * ... (0xFFFF) 834a8e1175bSopenharmony_ci * } NamedGroup; 835a8e1175bSopenharmony_ci * struct { 836a8e1175bSopenharmony_ci * NamedGroup named_group_list<2..2^16-1>; 837a8e1175bSopenharmony_ci * } NamedGroupList; 838a8e1175bSopenharmony_ci */ 839a8e1175bSopenharmony_ciMBEDTLS_CHECK_RETURN_CRITICAL 840a8e1175bSopenharmony_cistatic int ssl_tls13_parse_supported_groups_ext(mbedtls_ssl_context *ssl, 841a8e1175bSopenharmony_ci const unsigned char *buf, 842a8e1175bSopenharmony_ci const unsigned char *end) 843a8e1175bSopenharmony_ci{ 844a8e1175bSopenharmony_ci const unsigned char *p = buf; 845a8e1175bSopenharmony_ci size_t named_group_list_len; 846a8e1175bSopenharmony_ci const unsigned char *named_group_list_end; 847a8e1175bSopenharmony_ci 848a8e1175bSopenharmony_ci MBEDTLS_SSL_DEBUG_BUF(3, "supported_groups extension", p, end - buf); 849a8e1175bSopenharmony_ci MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2); 850a8e1175bSopenharmony_ci named_group_list_len = MBEDTLS_GET_UINT16_BE(p, 0); 851a8e1175bSopenharmony_ci p += 2; 852a8e1175bSopenharmony_ci MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, named_group_list_len); 853a8e1175bSopenharmony_ci named_group_list_end = p + named_group_list_len; 854a8e1175bSopenharmony_ci ssl->handshake->hrr_selected_group = 0; 855a8e1175bSopenharmony_ci 856a8e1175bSopenharmony_ci while (p < named_group_list_end) { 857a8e1175bSopenharmony_ci uint16_t named_group; 858a8e1175bSopenharmony_ci MBEDTLS_SSL_CHK_BUF_READ_PTR(p, named_group_list_end, 2); 859a8e1175bSopenharmony_ci named_group = MBEDTLS_GET_UINT16_BE(p, 0); 860a8e1175bSopenharmony_ci p += 2; 861a8e1175bSopenharmony_ci 862a8e1175bSopenharmony_ci MBEDTLS_SSL_DEBUG_MSG(2, 863a8e1175bSopenharmony_ci ("got named group: %s(%04x)", 864a8e1175bSopenharmony_ci mbedtls_ssl_named_group_to_str(named_group), 865a8e1175bSopenharmony_ci named_group)); 866a8e1175bSopenharmony_ci 867a8e1175bSopenharmony_ci if (!mbedtls_ssl_named_group_is_offered(ssl, named_group) || 868a8e1175bSopenharmony_ci !mbedtls_ssl_named_group_is_supported(named_group) || 869a8e1175bSopenharmony_ci ssl->handshake->hrr_selected_group != 0) { 870a8e1175bSopenharmony_ci continue; 871a8e1175bSopenharmony_ci } 872a8e1175bSopenharmony_ci 873a8e1175bSopenharmony_ci MBEDTLS_SSL_DEBUG_MSG(2, 874a8e1175bSopenharmony_ci ("add named group %s(%04x) into received list.", 875a8e1175bSopenharmony_ci mbedtls_ssl_named_group_to_str(named_group), 876a8e1175bSopenharmony_ci named_group)); 877a8e1175bSopenharmony_ci 878a8e1175bSopenharmony_ci ssl->handshake->hrr_selected_group = named_group; 879a8e1175bSopenharmony_ci } 880a8e1175bSopenharmony_ci 881a8e1175bSopenharmony_ci return 0; 882a8e1175bSopenharmony_ci 883a8e1175bSopenharmony_ci} 884a8e1175bSopenharmony_ci#endif /* PSA_WANT_ALG_ECDH || PSA_WANT_ALG_FFDH */ 885a8e1175bSopenharmony_ci 886a8e1175bSopenharmony_ci#define SSL_TLS1_3_PARSE_KEY_SHARES_EXT_NO_MATCH 1 887a8e1175bSopenharmony_ci 888a8e1175bSopenharmony_ci#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED) 889a8e1175bSopenharmony_ci/* 890a8e1175bSopenharmony_ci * ssl_tls13_parse_key_shares_ext() verifies whether the information in the 891a8e1175bSopenharmony_ci * extension is correct and stores the first acceptable key share and its 892a8e1175bSopenharmony_ci * associated group. 893a8e1175bSopenharmony_ci * 894a8e1175bSopenharmony_ci * Possible return values are: 895a8e1175bSopenharmony_ci * - 0: Successful processing of the client provided key share extension. 896a8e1175bSopenharmony_ci * - SSL_TLS1_3_PARSE_KEY_SHARES_EXT_NO_MATCH: The key shares provided by 897a8e1175bSopenharmony_ci * the client does not match a group supported by the server. A 898a8e1175bSopenharmony_ci * HelloRetryRequest will be needed. 899a8e1175bSopenharmony_ci * - A negative value for fatal errors. 900a8e1175bSopenharmony_ci */ 901a8e1175bSopenharmony_ciMBEDTLS_CHECK_RETURN_CRITICAL 902a8e1175bSopenharmony_cistatic int ssl_tls13_parse_key_shares_ext(mbedtls_ssl_context *ssl, 903a8e1175bSopenharmony_ci const unsigned char *buf, 904a8e1175bSopenharmony_ci const unsigned char *end) 905a8e1175bSopenharmony_ci{ 906a8e1175bSopenharmony_ci int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; 907a8e1175bSopenharmony_ci unsigned char const *p = buf; 908a8e1175bSopenharmony_ci unsigned char const *client_shares_end; 909a8e1175bSopenharmony_ci size_t client_shares_len; 910a8e1175bSopenharmony_ci 911a8e1175bSopenharmony_ci /* From RFC 8446: 912a8e1175bSopenharmony_ci * 913a8e1175bSopenharmony_ci * struct { 914a8e1175bSopenharmony_ci * KeyShareEntry client_shares<0..2^16-1>; 915a8e1175bSopenharmony_ci * } KeyShareClientHello; 916a8e1175bSopenharmony_ci * 917a8e1175bSopenharmony_ci */ 918a8e1175bSopenharmony_ci 919a8e1175bSopenharmony_ci MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2); 920a8e1175bSopenharmony_ci client_shares_len = MBEDTLS_GET_UINT16_BE(p, 0); 921a8e1175bSopenharmony_ci p += 2; 922a8e1175bSopenharmony_ci MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, client_shares_len); 923a8e1175bSopenharmony_ci 924a8e1175bSopenharmony_ci ssl->handshake->offered_group_id = 0; 925a8e1175bSopenharmony_ci client_shares_end = p + client_shares_len; 926a8e1175bSopenharmony_ci 927a8e1175bSopenharmony_ci /* We try to find a suitable key share entry and copy it to the 928a8e1175bSopenharmony_ci * handshake context. Later, we have to find out whether we can do 929a8e1175bSopenharmony_ci * something with the provided key share or whether we have to 930a8e1175bSopenharmony_ci * dismiss it and send a HelloRetryRequest message. 931a8e1175bSopenharmony_ci */ 932a8e1175bSopenharmony_ci 933a8e1175bSopenharmony_ci while (p < client_shares_end) { 934a8e1175bSopenharmony_ci uint16_t group; 935a8e1175bSopenharmony_ci size_t key_exchange_len; 936a8e1175bSopenharmony_ci const unsigned char *key_exchange; 937a8e1175bSopenharmony_ci 938a8e1175bSopenharmony_ci /* 939a8e1175bSopenharmony_ci * struct { 940a8e1175bSopenharmony_ci * NamedGroup group; 941a8e1175bSopenharmony_ci * opaque key_exchange<1..2^16-1>; 942a8e1175bSopenharmony_ci * } KeyShareEntry; 943a8e1175bSopenharmony_ci */ 944a8e1175bSopenharmony_ci MBEDTLS_SSL_CHK_BUF_READ_PTR(p, client_shares_end, 4); 945a8e1175bSopenharmony_ci group = MBEDTLS_GET_UINT16_BE(p, 0); 946a8e1175bSopenharmony_ci key_exchange_len = MBEDTLS_GET_UINT16_BE(p, 2); 947a8e1175bSopenharmony_ci p += 4; 948a8e1175bSopenharmony_ci key_exchange = p; 949a8e1175bSopenharmony_ci MBEDTLS_SSL_CHK_BUF_READ_PTR(p, client_shares_end, key_exchange_len); 950a8e1175bSopenharmony_ci p += key_exchange_len; 951a8e1175bSopenharmony_ci 952a8e1175bSopenharmony_ci /* Continue parsing even if we have already found a match, 953a8e1175bSopenharmony_ci * for input validation purposes. 954a8e1175bSopenharmony_ci */ 955a8e1175bSopenharmony_ci if (!mbedtls_ssl_named_group_is_offered(ssl, group) || 956a8e1175bSopenharmony_ci !mbedtls_ssl_named_group_is_supported(group) || 957a8e1175bSopenharmony_ci ssl->handshake->offered_group_id != 0) { 958a8e1175bSopenharmony_ci continue; 959a8e1175bSopenharmony_ci } 960a8e1175bSopenharmony_ci 961a8e1175bSopenharmony_ci /* 962a8e1175bSopenharmony_ci * ECDHE and FFDHE groups are supported 963a8e1175bSopenharmony_ci */ 964a8e1175bSopenharmony_ci if (mbedtls_ssl_tls13_named_group_is_ecdhe(group) || 965a8e1175bSopenharmony_ci mbedtls_ssl_tls13_named_group_is_ffdh(group)) { 966a8e1175bSopenharmony_ci MBEDTLS_SSL_DEBUG_MSG(2, ("ECDH/FFDH group: %s (%04x)", 967a8e1175bSopenharmony_ci mbedtls_ssl_named_group_to_str(group), 968a8e1175bSopenharmony_ci group)); 969a8e1175bSopenharmony_ci ret = mbedtls_ssl_tls13_read_public_xxdhe_share( 970a8e1175bSopenharmony_ci ssl, key_exchange - 2, key_exchange_len + 2); 971a8e1175bSopenharmony_ci if (ret != 0) { 972a8e1175bSopenharmony_ci return ret; 973a8e1175bSopenharmony_ci } 974a8e1175bSopenharmony_ci 975a8e1175bSopenharmony_ci } else { 976a8e1175bSopenharmony_ci MBEDTLS_SSL_DEBUG_MSG(4, ("Unrecognized NamedGroup %u", 977a8e1175bSopenharmony_ci (unsigned) group)); 978a8e1175bSopenharmony_ci continue; 979a8e1175bSopenharmony_ci } 980a8e1175bSopenharmony_ci 981a8e1175bSopenharmony_ci ssl->handshake->offered_group_id = group; 982a8e1175bSopenharmony_ci } 983a8e1175bSopenharmony_ci 984a8e1175bSopenharmony_ci 985a8e1175bSopenharmony_ci if (ssl->handshake->offered_group_id == 0) { 986a8e1175bSopenharmony_ci MBEDTLS_SSL_DEBUG_MSG(1, ("no matching key share")); 987a8e1175bSopenharmony_ci return SSL_TLS1_3_PARSE_KEY_SHARES_EXT_NO_MATCH; 988a8e1175bSopenharmony_ci } 989a8e1175bSopenharmony_ci return 0; 990a8e1175bSopenharmony_ci} 991a8e1175bSopenharmony_ci#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED */ 992a8e1175bSopenharmony_ci 993a8e1175bSopenharmony_ciMBEDTLS_CHECK_RETURN_CRITICAL 994a8e1175bSopenharmony_cistatic int ssl_tls13_client_hello_has_exts(mbedtls_ssl_context *ssl, 995a8e1175bSopenharmony_ci int exts_mask) 996a8e1175bSopenharmony_ci{ 997a8e1175bSopenharmony_ci int masked = ssl->handshake->received_extensions & exts_mask; 998a8e1175bSopenharmony_ci return masked == exts_mask; 999a8e1175bSopenharmony_ci} 1000a8e1175bSopenharmony_ci 1001a8e1175bSopenharmony_ci#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED) 1002a8e1175bSopenharmony_ciMBEDTLS_CHECK_RETURN_CRITICAL 1003a8e1175bSopenharmony_cistatic int ssl_tls13_client_hello_has_exts_for_ephemeral_key_exchange( 1004a8e1175bSopenharmony_ci mbedtls_ssl_context *ssl) 1005a8e1175bSopenharmony_ci{ 1006a8e1175bSopenharmony_ci return ssl_tls13_client_hello_has_exts( 1007a8e1175bSopenharmony_ci ssl, 1008a8e1175bSopenharmony_ci MBEDTLS_SSL_EXT_MASK(SUPPORTED_GROUPS) | 1009a8e1175bSopenharmony_ci MBEDTLS_SSL_EXT_MASK(KEY_SHARE) | 1010a8e1175bSopenharmony_ci MBEDTLS_SSL_EXT_MASK(SIG_ALG)); 1011a8e1175bSopenharmony_ci} 1012a8e1175bSopenharmony_ci#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */ 1013a8e1175bSopenharmony_ci 1014a8e1175bSopenharmony_ci#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_ENABLED) 1015a8e1175bSopenharmony_ciMBEDTLS_CHECK_RETURN_CRITICAL 1016a8e1175bSopenharmony_cistatic int ssl_tls13_client_hello_has_exts_for_psk_key_exchange( 1017a8e1175bSopenharmony_ci mbedtls_ssl_context *ssl) 1018a8e1175bSopenharmony_ci{ 1019a8e1175bSopenharmony_ci return ssl_tls13_client_hello_has_exts( 1020a8e1175bSopenharmony_ci ssl, 1021a8e1175bSopenharmony_ci MBEDTLS_SSL_EXT_MASK(PRE_SHARED_KEY) | 1022a8e1175bSopenharmony_ci MBEDTLS_SSL_EXT_MASK(PSK_KEY_EXCHANGE_MODES)); 1023a8e1175bSopenharmony_ci} 1024a8e1175bSopenharmony_ci#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_ENABLED */ 1025a8e1175bSopenharmony_ci 1026a8e1175bSopenharmony_ci#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL_ENABLED) 1027a8e1175bSopenharmony_ciMBEDTLS_CHECK_RETURN_CRITICAL 1028a8e1175bSopenharmony_cistatic int ssl_tls13_client_hello_has_exts_for_psk_ephemeral_key_exchange( 1029a8e1175bSopenharmony_ci mbedtls_ssl_context *ssl) 1030a8e1175bSopenharmony_ci{ 1031a8e1175bSopenharmony_ci return ssl_tls13_client_hello_has_exts( 1032a8e1175bSopenharmony_ci ssl, 1033a8e1175bSopenharmony_ci MBEDTLS_SSL_EXT_MASK(SUPPORTED_GROUPS) | 1034a8e1175bSopenharmony_ci MBEDTLS_SSL_EXT_MASK(KEY_SHARE) | 1035a8e1175bSopenharmony_ci MBEDTLS_SSL_EXT_MASK(PRE_SHARED_KEY) | 1036a8e1175bSopenharmony_ci MBEDTLS_SSL_EXT_MASK(PSK_KEY_EXCHANGE_MODES)); 1037a8e1175bSopenharmony_ci} 1038a8e1175bSopenharmony_ci#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL_ENABLED */ 1039a8e1175bSopenharmony_ci 1040a8e1175bSopenharmony_ci#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED) 1041a8e1175bSopenharmony_ciMBEDTLS_CHECK_RETURN_CRITICAL 1042a8e1175bSopenharmony_cistatic int ssl_tls13_key_exchange_is_psk_available(mbedtls_ssl_context *ssl) 1043a8e1175bSopenharmony_ci{ 1044a8e1175bSopenharmony_ci#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_ENABLED) 1045a8e1175bSopenharmony_ci return mbedtls_ssl_conf_tls13_is_psk_enabled(ssl) && 1046a8e1175bSopenharmony_ci mbedtls_ssl_tls13_is_psk_supported(ssl) && 1047a8e1175bSopenharmony_ci ssl_tls13_client_hello_has_exts_for_psk_key_exchange(ssl); 1048a8e1175bSopenharmony_ci#else 1049a8e1175bSopenharmony_ci ((void) ssl); 1050a8e1175bSopenharmony_ci return 0; 1051a8e1175bSopenharmony_ci#endif 1052a8e1175bSopenharmony_ci} 1053a8e1175bSopenharmony_ci 1054a8e1175bSopenharmony_ciMBEDTLS_CHECK_RETURN_CRITICAL 1055a8e1175bSopenharmony_cistatic int ssl_tls13_key_exchange_is_psk_ephemeral_available(mbedtls_ssl_context *ssl) 1056a8e1175bSopenharmony_ci{ 1057a8e1175bSopenharmony_ci#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL_ENABLED) 1058a8e1175bSopenharmony_ci return mbedtls_ssl_conf_tls13_is_psk_ephemeral_enabled(ssl) && 1059a8e1175bSopenharmony_ci mbedtls_ssl_tls13_is_psk_ephemeral_supported(ssl) && 1060a8e1175bSopenharmony_ci ssl_tls13_client_hello_has_exts_for_psk_ephemeral_key_exchange(ssl); 1061a8e1175bSopenharmony_ci#else 1062a8e1175bSopenharmony_ci ((void) ssl); 1063a8e1175bSopenharmony_ci return 0; 1064a8e1175bSopenharmony_ci#endif 1065a8e1175bSopenharmony_ci} 1066a8e1175bSopenharmony_ci#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED */ 1067a8e1175bSopenharmony_ci 1068a8e1175bSopenharmony_ciMBEDTLS_CHECK_RETURN_CRITICAL 1069a8e1175bSopenharmony_cistatic int ssl_tls13_key_exchange_is_ephemeral_available(mbedtls_ssl_context *ssl) 1070a8e1175bSopenharmony_ci{ 1071a8e1175bSopenharmony_ci#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED) 1072a8e1175bSopenharmony_ci return mbedtls_ssl_conf_tls13_is_ephemeral_enabled(ssl) && 1073a8e1175bSopenharmony_ci ssl_tls13_client_hello_has_exts_for_ephemeral_key_exchange(ssl); 1074a8e1175bSopenharmony_ci#else 1075a8e1175bSopenharmony_ci ((void) ssl); 1076a8e1175bSopenharmony_ci return 0; 1077a8e1175bSopenharmony_ci#endif 1078a8e1175bSopenharmony_ci} 1079a8e1175bSopenharmony_ci 1080a8e1175bSopenharmony_ci#if defined(MBEDTLS_X509_CRT_PARSE_C) && \ 1081a8e1175bSopenharmony_ci defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED) 1082a8e1175bSopenharmony_ci 1083a8e1175bSopenharmony_ci#if defined(MBEDTLS_USE_PSA_CRYPTO) 1084a8e1175bSopenharmony_cistatic psa_algorithm_t ssl_tls13_iana_sig_alg_to_psa_alg(uint16_t sig_alg) 1085a8e1175bSopenharmony_ci{ 1086a8e1175bSopenharmony_ci switch (sig_alg) { 1087a8e1175bSopenharmony_ci case MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256: 1088a8e1175bSopenharmony_ci return PSA_ALG_ECDSA(PSA_ALG_SHA_256); 1089a8e1175bSopenharmony_ci case MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384: 1090a8e1175bSopenharmony_ci return PSA_ALG_ECDSA(PSA_ALG_SHA_384); 1091a8e1175bSopenharmony_ci case MBEDTLS_TLS1_3_SIG_ECDSA_SECP521R1_SHA512: 1092a8e1175bSopenharmony_ci return PSA_ALG_ECDSA(PSA_ALG_SHA_512); 1093a8e1175bSopenharmony_ci case MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256: 1094a8e1175bSopenharmony_ci return PSA_ALG_RSA_PSS(PSA_ALG_SHA_256); 1095a8e1175bSopenharmony_ci case MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384: 1096a8e1175bSopenharmony_ci return PSA_ALG_RSA_PSS(PSA_ALG_SHA_384); 1097a8e1175bSopenharmony_ci case MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512: 1098a8e1175bSopenharmony_ci return PSA_ALG_RSA_PSS(PSA_ALG_SHA_512); 1099a8e1175bSopenharmony_ci case MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256: 1100a8e1175bSopenharmony_ci return PSA_ALG_RSA_PKCS1V15_SIGN(PSA_ALG_SHA_256); 1101a8e1175bSopenharmony_ci case MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA384: 1102a8e1175bSopenharmony_ci return PSA_ALG_RSA_PKCS1V15_SIGN(PSA_ALG_SHA_384); 1103a8e1175bSopenharmony_ci case MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA512: 1104a8e1175bSopenharmony_ci return PSA_ALG_RSA_PKCS1V15_SIGN(PSA_ALG_SHA_512); 1105a8e1175bSopenharmony_ci default: 1106a8e1175bSopenharmony_ci return PSA_ALG_NONE; 1107a8e1175bSopenharmony_ci } 1108a8e1175bSopenharmony_ci} 1109a8e1175bSopenharmony_ci#endif /* MBEDTLS_USE_PSA_CRYPTO */ 1110a8e1175bSopenharmony_ci 1111a8e1175bSopenharmony_ci/* 1112a8e1175bSopenharmony_ci * Pick best ( private key, certificate chain ) pair based on the signature 1113a8e1175bSopenharmony_ci * algorithms supported by the client. 1114a8e1175bSopenharmony_ci */ 1115a8e1175bSopenharmony_ciMBEDTLS_CHECK_RETURN_CRITICAL 1116a8e1175bSopenharmony_cistatic int ssl_tls13_pick_key_cert(mbedtls_ssl_context *ssl) 1117a8e1175bSopenharmony_ci{ 1118a8e1175bSopenharmony_ci mbedtls_ssl_key_cert *key_cert, *key_cert_list; 1119a8e1175bSopenharmony_ci const uint16_t *sig_alg = ssl->handshake->received_sig_algs; 1120a8e1175bSopenharmony_ci 1121a8e1175bSopenharmony_ci#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) 1122a8e1175bSopenharmony_ci if (ssl->handshake->sni_key_cert != NULL) { 1123a8e1175bSopenharmony_ci key_cert_list = ssl->handshake->sni_key_cert; 1124a8e1175bSopenharmony_ci } else 1125a8e1175bSopenharmony_ci#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */ 1126a8e1175bSopenharmony_ci key_cert_list = ssl->conf->key_cert; 1127a8e1175bSopenharmony_ci 1128a8e1175bSopenharmony_ci if (key_cert_list == NULL) { 1129a8e1175bSopenharmony_ci MBEDTLS_SSL_DEBUG_MSG(3, ("server has no certificate")); 1130a8e1175bSopenharmony_ci return -1; 1131a8e1175bSopenharmony_ci } 1132a8e1175bSopenharmony_ci 1133a8e1175bSopenharmony_ci for (; *sig_alg != MBEDTLS_TLS1_3_SIG_NONE; sig_alg++) { 1134a8e1175bSopenharmony_ci if (!mbedtls_ssl_sig_alg_is_offered(ssl, *sig_alg)) { 1135a8e1175bSopenharmony_ci continue; 1136a8e1175bSopenharmony_ci } 1137a8e1175bSopenharmony_ci 1138a8e1175bSopenharmony_ci if (!mbedtls_ssl_tls13_sig_alg_for_cert_verify_is_supported(*sig_alg)) { 1139a8e1175bSopenharmony_ci continue; 1140a8e1175bSopenharmony_ci } 1141a8e1175bSopenharmony_ci 1142a8e1175bSopenharmony_ci for (key_cert = key_cert_list; key_cert != NULL; 1143a8e1175bSopenharmony_ci key_cert = key_cert->next) { 1144a8e1175bSopenharmony_ci#if defined(MBEDTLS_USE_PSA_CRYPTO) 1145a8e1175bSopenharmony_ci psa_algorithm_t psa_alg = PSA_ALG_NONE; 1146a8e1175bSopenharmony_ci#endif /* MBEDTLS_USE_PSA_CRYPTO */ 1147a8e1175bSopenharmony_ci 1148a8e1175bSopenharmony_ci MBEDTLS_SSL_DEBUG_CRT(3, "certificate (chain) candidate", 1149a8e1175bSopenharmony_ci key_cert->cert); 1150a8e1175bSopenharmony_ci 1151a8e1175bSopenharmony_ci /* 1152a8e1175bSopenharmony_ci * This avoids sending the client a cert it'll reject based on 1153a8e1175bSopenharmony_ci * keyUsage or other extensions. 1154a8e1175bSopenharmony_ci */ 1155a8e1175bSopenharmony_ci if (mbedtls_x509_crt_check_key_usage( 1156a8e1175bSopenharmony_ci key_cert->cert, MBEDTLS_X509_KU_DIGITAL_SIGNATURE) != 0 || 1157a8e1175bSopenharmony_ci mbedtls_x509_crt_check_extended_key_usage( 1158a8e1175bSopenharmony_ci key_cert->cert, MBEDTLS_OID_SERVER_AUTH, 1159a8e1175bSopenharmony_ci MBEDTLS_OID_SIZE(MBEDTLS_OID_SERVER_AUTH)) != 0) { 1160a8e1175bSopenharmony_ci MBEDTLS_SSL_DEBUG_MSG(3, ("certificate mismatch: " 1161a8e1175bSopenharmony_ci "(extended) key usage extension")); 1162a8e1175bSopenharmony_ci continue; 1163a8e1175bSopenharmony_ci } 1164a8e1175bSopenharmony_ci 1165a8e1175bSopenharmony_ci MBEDTLS_SSL_DEBUG_MSG(3, 1166a8e1175bSopenharmony_ci ("ssl_tls13_pick_key_cert:" 1167a8e1175bSopenharmony_ci "check signature algorithm %s [%04x]", 1168a8e1175bSopenharmony_ci mbedtls_ssl_sig_alg_to_str(*sig_alg), 1169a8e1175bSopenharmony_ci *sig_alg)); 1170a8e1175bSopenharmony_ci#if defined(MBEDTLS_USE_PSA_CRYPTO) 1171a8e1175bSopenharmony_ci psa_alg = ssl_tls13_iana_sig_alg_to_psa_alg(*sig_alg); 1172a8e1175bSopenharmony_ci#endif /* MBEDTLS_USE_PSA_CRYPTO */ 1173a8e1175bSopenharmony_ci 1174a8e1175bSopenharmony_ci if (mbedtls_ssl_tls13_check_sig_alg_cert_key_match( 1175a8e1175bSopenharmony_ci *sig_alg, &key_cert->cert->pk) 1176a8e1175bSopenharmony_ci#if defined(MBEDTLS_USE_PSA_CRYPTO) 1177a8e1175bSopenharmony_ci && psa_alg != PSA_ALG_NONE && 1178a8e1175bSopenharmony_ci mbedtls_pk_can_do_ext(&key_cert->cert->pk, psa_alg, 1179a8e1175bSopenharmony_ci PSA_KEY_USAGE_SIGN_HASH) == 1 1180a8e1175bSopenharmony_ci#endif /* MBEDTLS_USE_PSA_CRYPTO */ 1181a8e1175bSopenharmony_ci ) { 1182a8e1175bSopenharmony_ci ssl->handshake->key_cert = key_cert; 1183a8e1175bSopenharmony_ci MBEDTLS_SSL_DEBUG_MSG(3, 1184a8e1175bSopenharmony_ci ("ssl_tls13_pick_key_cert:" 1185a8e1175bSopenharmony_ci "selected signature algorithm" 1186a8e1175bSopenharmony_ci " %s [%04x]", 1187a8e1175bSopenharmony_ci mbedtls_ssl_sig_alg_to_str(*sig_alg), 1188a8e1175bSopenharmony_ci *sig_alg)); 1189a8e1175bSopenharmony_ci MBEDTLS_SSL_DEBUG_CRT( 1190a8e1175bSopenharmony_ci 3, "selected certificate (chain)", 1191a8e1175bSopenharmony_ci ssl->handshake->key_cert->cert); 1192a8e1175bSopenharmony_ci return 0; 1193a8e1175bSopenharmony_ci } 1194a8e1175bSopenharmony_ci } 1195a8e1175bSopenharmony_ci } 1196a8e1175bSopenharmony_ci 1197a8e1175bSopenharmony_ci MBEDTLS_SSL_DEBUG_MSG(2, ("ssl_tls13_pick_key_cert:" 1198a8e1175bSopenharmony_ci "no suitable certificate found")); 1199a8e1175bSopenharmony_ci return -1; 1200a8e1175bSopenharmony_ci} 1201a8e1175bSopenharmony_ci#endif /* MBEDTLS_X509_CRT_PARSE_C && 1202a8e1175bSopenharmony_ci MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */ 1203a8e1175bSopenharmony_ci 1204a8e1175bSopenharmony_ci/* 1205a8e1175bSopenharmony_ci * 1206a8e1175bSopenharmony_ci * STATE HANDLING: ClientHello 1207a8e1175bSopenharmony_ci * 1208a8e1175bSopenharmony_ci * There are three possible classes of outcomes when parsing the ClientHello: 1209a8e1175bSopenharmony_ci * 1210a8e1175bSopenharmony_ci * 1) The ClientHello was well-formed and matched the server's configuration. 1211a8e1175bSopenharmony_ci * 1212a8e1175bSopenharmony_ci * In this case, the server progresses to sending its ServerHello. 1213a8e1175bSopenharmony_ci * 1214a8e1175bSopenharmony_ci * 2) The ClientHello was well-formed but didn't match the server's 1215a8e1175bSopenharmony_ci * configuration. 1216a8e1175bSopenharmony_ci * 1217a8e1175bSopenharmony_ci * For example, the client might not have offered a key share which 1218a8e1175bSopenharmony_ci * the server supports, or the server might require a cookie. 1219a8e1175bSopenharmony_ci * 1220a8e1175bSopenharmony_ci * In this case, the server sends a HelloRetryRequest. 1221a8e1175bSopenharmony_ci * 1222a8e1175bSopenharmony_ci * 3) The ClientHello was ill-formed 1223a8e1175bSopenharmony_ci * 1224a8e1175bSopenharmony_ci * In this case, we abort the handshake. 1225a8e1175bSopenharmony_ci * 1226a8e1175bSopenharmony_ci */ 1227a8e1175bSopenharmony_ci 1228a8e1175bSopenharmony_ci/* 1229a8e1175bSopenharmony_ci * Structure of this message: 1230a8e1175bSopenharmony_ci * 1231a8e1175bSopenharmony_ci * uint16 ProtocolVersion; 1232a8e1175bSopenharmony_ci * opaque Random[32]; 1233a8e1175bSopenharmony_ci * uint8 CipherSuite[2]; // Cryptographic suite selector 1234a8e1175bSopenharmony_ci * 1235a8e1175bSopenharmony_ci * struct { 1236a8e1175bSopenharmony_ci * ProtocolVersion legacy_version = 0x0303; // TLS v1.2 1237a8e1175bSopenharmony_ci * Random random; 1238a8e1175bSopenharmony_ci * opaque legacy_session_id<0..32>; 1239a8e1175bSopenharmony_ci * CipherSuite cipher_suites<2..2^16-2>; 1240a8e1175bSopenharmony_ci * opaque legacy_compression_methods<1..2^8-1>; 1241a8e1175bSopenharmony_ci * Extension extensions<8..2^16-1>; 1242a8e1175bSopenharmony_ci * } ClientHello; 1243a8e1175bSopenharmony_ci */ 1244a8e1175bSopenharmony_ci 1245a8e1175bSopenharmony_ci#define SSL_CLIENT_HELLO_OK 0 1246a8e1175bSopenharmony_ci#define SSL_CLIENT_HELLO_HRR_REQUIRED 1 1247a8e1175bSopenharmony_ci#define SSL_CLIENT_HELLO_TLS1_2 2 1248a8e1175bSopenharmony_ci 1249a8e1175bSopenharmony_ciMBEDTLS_CHECK_RETURN_CRITICAL 1250a8e1175bSopenharmony_cistatic int ssl_tls13_parse_client_hello(mbedtls_ssl_context *ssl, 1251a8e1175bSopenharmony_ci const unsigned char *buf, 1252a8e1175bSopenharmony_ci const unsigned char *end) 1253a8e1175bSopenharmony_ci{ 1254a8e1175bSopenharmony_ci int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; 1255a8e1175bSopenharmony_ci const unsigned char *p = buf; 1256a8e1175bSopenharmony_ci const unsigned char *random; 1257a8e1175bSopenharmony_ci size_t legacy_session_id_len; 1258a8e1175bSopenharmony_ci const unsigned char *legacy_session_id; 1259a8e1175bSopenharmony_ci size_t cipher_suites_len; 1260a8e1175bSopenharmony_ci const unsigned char *cipher_suites; 1261a8e1175bSopenharmony_ci const unsigned char *cipher_suites_end; 1262a8e1175bSopenharmony_ci size_t extensions_len; 1263a8e1175bSopenharmony_ci const unsigned char *extensions_end; 1264a8e1175bSopenharmony_ci const unsigned char *supported_versions_data; 1265a8e1175bSopenharmony_ci const unsigned char *supported_versions_data_end; 1266a8e1175bSopenharmony_ci mbedtls_ssl_handshake_params *handshake = ssl->handshake; 1267a8e1175bSopenharmony_ci int hrr_required = 0; 1268a8e1175bSopenharmony_ci int no_usable_share_for_key_agreement = 0; 1269a8e1175bSopenharmony_ci 1270a8e1175bSopenharmony_ci#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED) 1271a8e1175bSopenharmony_ci int got_psk = 0; 1272a8e1175bSopenharmony_ci struct psk_attributes psk = PSK_ATTRIBUTES_INIT; 1273a8e1175bSopenharmony_ci const unsigned char *pre_shared_key_ext = NULL; 1274a8e1175bSopenharmony_ci const unsigned char *pre_shared_key_ext_end = NULL; 1275a8e1175bSopenharmony_ci#endif 1276a8e1175bSopenharmony_ci 1277a8e1175bSopenharmony_ci /* 1278a8e1175bSopenharmony_ci * ClientHello layout: 1279a8e1175bSopenharmony_ci * 0 . 1 protocol version 1280a8e1175bSopenharmony_ci * 2 . 33 random bytes 1281a8e1175bSopenharmony_ci * 34 . 34 session id length ( 1 byte ) 1282a8e1175bSopenharmony_ci * 35 . 34+x session id 1283a8e1175bSopenharmony_ci * .. . .. ciphersuite list length ( 2 bytes ) 1284a8e1175bSopenharmony_ci * .. . .. ciphersuite list 1285a8e1175bSopenharmony_ci * .. . .. compression alg. list length ( 1 byte ) 1286a8e1175bSopenharmony_ci * .. . .. compression alg. list 1287a8e1175bSopenharmony_ci * .. . .. extensions length ( 2 bytes, optional ) 1288a8e1175bSopenharmony_ci * .. . .. extensions ( optional ) 1289a8e1175bSopenharmony_ci */ 1290a8e1175bSopenharmony_ci 1291a8e1175bSopenharmony_ci /* 1292a8e1175bSopenharmony_ci * Minimal length ( with everything empty and extensions omitted ) is 1293a8e1175bSopenharmony_ci * 2 + 32 + 1 + 2 + 1 = 38 bytes. Check that first, so that we can 1294a8e1175bSopenharmony_ci * read at least up to session id length without worrying. 1295a8e1175bSopenharmony_ci */ 1296a8e1175bSopenharmony_ci MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 38); 1297a8e1175bSopenharmony_ci 1298a8e1175bSopenharmony_ci /* ... 1299a8e1175bSopenharmony_ci * ProtocolVersion legacy_version = 0x0303; // TLS 1.2 1300a8e1175bSopenharmony_ci * ... 1301a8e1175bSopenharmony_ci * with ProtocolVersion defined as: 1302a8e1175bSopenharmony_ci * uint16 ProtocolVersion; 1303a8e1175bSopenharmony_ci */ 1304a8e1175bSopenharmony_ci if (mbedtls_ssl_read_version(p, ssl->conf->transport) != 1305a8e1175bSopenharmony_ci MBEDTLS_SSL_VERSION_TLS1_2) { 1306a8e1175bSopenharmony_ci MBEDTLS_SSL_DEBUG_MSG(1, ("Unsupported version of TLS.")); 1307a8e1175bSopenharmony_ci MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION, 1308a8e1175bSopenharmony_ci MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION); 1309a8e1175bSopenharmony_ci return MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION; 1310a8e1175bSopenharmony_ci } 1311a8e1175bSopenharmony_ci p += 2; 1312a8e1175bSopenharmony_ci 1313a8e1175bSopenharmony_ci /* ... 1314a8e1175bSopenharmony_ci * Random random; 1315a8e1175bSopenharmony_ci * ... 1316a8e1175bSopenharmony_ci * with Random defined as: 1317a8e1175bSopenharmony_ci * opaque Random[32]; 1318a8e1175bSopenharmony_ci */ 1319a8e1175bSopenharmony_ci random = p; 1320a8e1175bSopenharmony_ci p += MBEDTLS_CLIENT_HELLO_RANDOM_LEN; 1321a8e1175bSopenharmony_ci 1322a8e1175bSopenharmony_ci /* ... 1323a8e1175bSopenharmony_ci * opaque legacy_session_id<0..32>; 1324a8e1175bSopenharmony_ci * ... 1325a8e1175bSopenharmony_ci */ 1326a8e1175bSopenharmony_ci legacy_session_id_len = *(p++); 1327a8e1175bSopenharmony_ci legacy_session_id = p; 1328a8e1175bSopenharmony_ci 1329a8e1175bSopenharmony_ci /* 1330a8e1175bSopenharmony_ci * Check we have enough data for the legacy session identifier 1331a8e1175bSopenharmony_ci * and the ciphersuite list length. 1332a8e1175bSopenharmony_ci */ 1333a8e1175bSopenharmony_ci MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, legacy_session_id_len + 2); 1334a8e1175bSopenharmony_ci p += legacy_session_id_len; 1335a8e1175bSopenharmony_ci 1336a8e1175bSopenharmony_ci /* ... 1337a8e1175bSopenharmony_ci * CipherSuite cipher_suites<2..2^16-2>; 1338a8e1175bSopenharmony_ci * ... 1339a8e1175bSopenharmony_ci * with CipherSuite defined as: 1340a8e1175bSopenharmony_ci * uint8 CipherSuite[2]; 1341a8e1175bSopenharmony_ci */ 1342a8e1175bSopenharmony_ci cipher_suites_len = MBEDTLS_GET_UINT16_BE(p, 0); 1343a8e1175bSopenharmony_ci p += 2; 1344a8e1175bSopenharmony_ci cipher_suites = p; 1345a8e1175bSopenharmony_ci 1346a8e1175bSopenharmony_ci /* 1347a8e1175bSopenharmony_ci * The length of the ciphersuite list has to be even. 1348a8e1175bSopenharmony_ci */ 1349a8e1175bSopenharmony_ci if (cipher_suites_len & 1) { 1350a8e1175bSopenharmony_ci MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR, 1351a8e1175bSopenharmony_ci MBEDTLS_ERR_SSL_DECODE_ERROR); 1352a8e1175bSopenharmony_ci return MBEDTLS_ERR_SSL_DECODE_ERROR; 1353a8e1175bSopenharmony_ci } 1354a8e1175bSopenharmony_ci 1355a8e1175bSopenharmony_ci /* Check we have enough data for the ciphersuite list, the legacy 1356a8e1175bSopenharmony_ci * compression methods and the length of the extensions. 1357a8e1175bSopenharmony_ci * 1358a8e1175bSopenharmony_ci * cipher_suites cipher_suites_len bytes 1359a8e1175bSopenharmony_ci * legacy_compression_methods 2 bytes 1360a8e1175bSopenharmony_ci * extensions_len 2 bytes 1361a8e1175bSopenharmony_ci */ 1362a8e1175bSopenharmony_ci MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, cipher_suites_len + 2 + 2); 1363a8e1175bSopenharmony_ci p += cipher_suites_len; 1364a8e1175bSopenharmony_ci cipher_suites_end = p; 1365a8e1175bSopenharmony_ci 1366a8e1175bSopenharmony_ci /* 1367a8e1175bSopenharmony_ci * Search for the supported versions extension and parse it to determine 1368a8e1175bSopenharmony_ci * if the client supports TLS 1.3. 1369a8e1175bSopenharmony_ci */ 1370a8e1175bSopenharmony_ci ret = mbedtls_ssl_tls13_is_supported_versions_ext_present_in_exts( 1371a8e1175bSopenharmony_ci ssl, p + 2, end, 1372a8e1175bSopenharmony_ci &supported_versions_data, &supported_versions_data_end); 1373a8e1175bSopenharmony_ci if (ret < 0) { 1374a8e1175bSopenharmony_ci MBEDTLS_SSL_DEBUG_RET(1, 1375a8e1175bSopenharmony_ci ("mbedtls_ssl_tls13_is_supported_versions_ext_present_in_exts"), ret); 1376a8e1175bSopenharmony_ci return ret; 1377a8e1175bSopenharmony_ci } 1378a8e1175bSopenharmony_ci 1379a8e1175bSopenharmony_ci if (ret == 0) { 1380a8e1175bSopenharmony_ci return SSL_CLIENT_HELLO_TLS1_2; 1381a8e1175bSopenharmony_ci } 1382a8e1175bSopenharmony_ci 1383a8e1175bSopenharmony_ci if (ret == 1) { 1384a8e1175bSopenharmony_ci ret = ssl_tls13_parse_supported_versions_ext(ssl, 1385a8e1175bSopenharmony_ci supported_versions_data, 1386a8e1175bSopenharmony_ci supported_versions_data_end); 1387a8e1175bSopenharmony_ci if (ret < 0) { 1388a8e1175bSopenharmony_ci MBEDTLS_SSL_DEBUG_RET(1, 1389a8e1175bSopenharmony_ci ("ssl_tls13_parse_supported_versions_ext"), ret); 1390a8e1175bSopenharmony_ci return ret; 1391a8e1175bSopenharmony_ci } 1392a8e1175bSopenharmony_ci 1393a8e1175bSopenharmony_ci /* 1394a8e1175bSopenharmony_ci * The supported versions extension was parsed successfully as the 1395a8e1175bSopenharmony_ci * value returned by ssl_tls13_parse_supported_versions_ext() is 1396a8e1175bSopenharmony_ci * positive. The return value is then equal to 1397a8e1175bSopenharmony_ci * MBEDTLS_SSL_VERSION_TLS1_2 or MBEDTLS_SSL_VERSION_TLS1_3, defining 1398a8e1175bSopenharmony_ci * the TLS version to negotiate. 1399a8e1175bSopenharmony_ci */ 1400a8e1175bSopenharmony_ci if (MBEDTLS_SSL_VERSION_TLS1_2 == ret) { 1401a8e1175bSopenharmony_ci return SSL_CLIENT_HELLO_TLS1_2; 1402a8e1175bSopenharmony_ci } 1403a8e1175bSopenharmony_ci } 1404a8e1175bSopenharmony_ci 1405a8e1175bSopenharmony_ci /* 1406a8e1175bSopenharmony_ci * We negotiate TLS 1.3. 1407a8e1175bSopenharmony_ci */ 1408a8e1175bSopenharmony_ci ssl->tls_version = MBEDTLS_SSL_VERSION_TLS1_3; 1409a8e1175bSopenharmony_ci ssl->session_negotiate->tls_version = MBEDTLS_SSL_VERSION_TLS1_3; 1410a8e1175bSopenharmony_ci ssl->session_negotiate->endpoint = ssl->conf->endpoint; 1411a8e1175bSopenharmony_ci 1412a8e1175bSopenharmony_ci /* 1413a8e1175bSopenharmony_ci * We are negotiating the version 1.3 of the protocol. Do what we have 1414a8e1175bSopenharmony_ci * postponed: copy of the client random bytes, copy of the legacy session 1415a8e1175bSopenharmony_ci * identifier and selection of the TLS 1.3 cipher suite. 1416a8e1175bSopenharmony_ci */ 1417a8e1175bSopenharmony_ci MBEDTLS_SSL_DEBUG_BUF(3, "client hello, random bytes", 1418a8e1175bSopenharmony_ci random, MBEDTLS_CLIENT_HELLO_RANDOM_LEN); 1419a8e1175bSopenharmony_ci memcpy(&handshake->randbytes[0], random, MBEDTLS_CLIENT_HELLO_RANDOM_LEN); 1420a8e1175bSopenharmony_ci 1421a8e1175bSopenharmony_ci if (legacy_session_id_len > sizeof(ssl->session_negotiate->id)) { 1422a8e1175bSopenharmony_ci MBEDTLS_SSL_DEBUG_MSG(1, ("bad client hello message")); 1423a8e1175bSopenharmony_ci return MBEDTLS_ERR_SSL_DECODE_ERROR; 1424a8e1175bSopenharmony_ci } 1425a8e1175bSopenharmony_ci ssl->session_negotiate->id_len = legacy_session_id_len; 1426a8e1175bSopenharmony_ci MBEDTLS_SSL_DEBUG_BUF(3, "client hello, session id", 1427a8e1175bSopenharmony_ci legacy_session_id, legacy_session_id_len); 1428a8e1175bSopenharmony_ci memcpy(&ssl->session_negotiate->id[0], 1429a8e1175bSopenharmony_ci legacy_session_id, legacy_session_id_len); 1430a8e1175bSopenharmony_ci 1431a8e1175bSopenharmony_ci /* 1432a8e1175bSopenharmony_ci * Search for a matching ciphersuite 1433a8e1175bSopenharmony_ci */ 1434a8e1175bSopenharmony_ci MBEDTLS_SSL_DEBUG_BUF(3, "client hello, list of cipher suites", 1435a8e1175bSopenharmony_ci cipher_suites, cipher_suites_len); 1436a8e1175bSopenharmony_ci 1437a8e1175bSopenharmony_ci ssl_tls13_select_ciphersuite(ssl, cipher_suites, cipher_suites_end, 1438a8e1175bSopenharmony_ci 0, PSA_ALG_NONE, &handshake->ciphersuite_info); 1439a8e1175bSopenharmony_ci 1440a8e1175bSopenharmony_ci if (handshake->ciphersuite_info == NULL) { 1441a8e1175bSopenharmony_ci MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE, 1442a8e1175bSopenharmony_ci MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE); 1443a8e1175bSopenharmony_ci return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE; 1444a8e1175bSopenharmony_ci } 1445a8e1175bSopenharmony_ci ssl->session_negotiate->ciphersuite = handshake->ciphersuite_info->id; 1446a8e1175bSopenharmony_ci 1447a8e1175bSopenharmony_ci MBEDTLS_SSL_DEBUG_MSG(2, ("selected ciphersuite: %04x - %s", 1448a8e1175bSopenharmony_ci ((unsigned) handshake->ciphersuite_info->id), 1449a8e1175bSopenharmony_ci handshake->ciphersuite_info->name)); 1450a8e1175bSopenharmony_ci 1451a8e1175bSopenharmony_ci /* ... 1452a8e1175bSopenharmony_ci * opaque legacy_compression_methods<1..2^8-1>; 1453a8e1175bSopenharmony_ci * ... 1454a8e1175bSopenharmony_ci */ 1455a8e1175bSopenharmony_ci if (p[0] != 1 || p[1] != MBEDTLS_SSL_COMPRESS_NULL) { 1456a8e1175bSopenharmony_ci MBEDTLS_SSL_DEBUG_MSG(1, ("bad legacy compression method")); 1457a8e1175bSopenharmony_ci MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER, 1458a8e1175bSopenharmony_ci MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER); 1459a8e1175bSopenharmony_ci return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER; 1460a8e1175bSopenharmony_ci } 1461a8e1175bSopenharmony_ci p += 2; 1462a8e1175bSopenharmony_ci 1463a8e1175bSopenharmony_ci /* ... 1464a8e1175bSopenharmony_ci * Extension extensions<8..2^16-1>; 1465a8e1175bSopenharmony_ci * ... 1466a8e1175bSopenharmony_ci * with Extension defined as: 1467a8e1175bSopenharmony_ci * struct { 1468a8e1175bSopenharmony_ci * ExtensionType extension_type; 1469a8e1175bSopenharmony_ci * opaque extension_data<0..2^16-1>; 1470a8e1175bSopenharmony_ci * } Extension; 1471a8e1175bSopenharmony_ci */ 1472a8e1175bSopenharmony_ci extensions_len = MBEDTLS_GET_UINT16_BE(p, 0); 1473a8e1175bSopenharmony_ci p += 2; 1474a8e1175bSopenharmony_ci MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, extensions_len); 1475a8e1175bSopenharmony_ci extensions_end = p + extensions_len; 1476a8e1175bSopenharmony_ci 1477a8e1175bSopenharmony_ci MBEDTLS_SSL_DEBUG_BUF(3, "client hello extensions", p, extensions_len); 1478a8e1175bSopenharmony_ci handshake->received_extensions = MBEDTLS_SSL_EXT_MASK_NONE; 1479a8e1175bSopenharmony_ci 1480a8e1175bSopenharmony_ci while (p < extensions_end) { 1481a8e1175bSopenharmony_ci unsigned int extension_type; 1482a8e1175bSopenharmony_ci size_t extension_data_len; 1483a8e1175bSopenharmony_ci const unsigned char *extension_data_end; 1484a8e1175bSopenharmony_ci uint32_t allowed_exts = MBEDTLS_SSL_TLS1_3_ALLOWED_EXTS_OF_CH; 1485a8e1175bSopenharmony_ci 1486a8e1175bSopenharmony_ci if (ssl->handshake->hello_retry_request_flag) { 1487a8e1175bSopenharmony_ci /* Do not accept early data extension in 2nd ClientHello */ 1488a8e1175bSopenharmony_ci allowed_exts &= ~MBEDTLS_SSL_EXT_MASK(EARLY_DATA); 1489a8e1175bSopenharmony_ci } 1490a8e1175bSopenharmony_ci 1491a8e1175bSopenharmony_ci /* RFC 8446, section 4.2.11 1492a8e1175bSopenharmony_ci * 1493a8e1175bSopenharmony_ci * The "pre_shared_key" extension MUST be the last extension in the 1494a8e1175bSopenharmony_ci * ClientHello (this facilitates implementation as described below). 1495a8e1175bSopenharmony_ci * Servers MUST check that it is the last extension and otherwise fail 1496a8e1175bSopenharmony_ci * the handshake with an "illegal_parameter" alert. 1497a8e1175bSopenharmony_ci */ 1498a8e1175bSopenharmony_ci if (handshake->received_extensions & MBEDTLS_SSL_EXT_MASK(PRE_SHARED_KEY)) { 1499a8e1175bSopenharmony_ci MBEDTLS_SSL_DEBUG_MSG( 1500a8e1175bSopenharmony_ci 3, ("pre_shared_key is not last extension.")); 1501a8e1175bSopenharmony_ci MBEDTLS_SSL_PEND_FATAL_ALERT( 1502a8e1175bSopenharmony_ci MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER, 1503a8e1175bSopenharmony_ci MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER); 1504a8e1175bSopenharmony_ci return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER; 1505a8e1175bSopenharmony_ci } 1506a8e1175bSopenharmony_ci 1507a8e1175bSopenharmony_ci MBEDTLS_SSL_CHK_BUF_READ_PTR(p, extensions_end, 4); 1508a8e1175bSopenharmony_ci extension_type = MBEDTLS_GET_UINT16_BE(p, 0); 1509a8e1175bSopenharmony_ci extension_data_len = MBEDTLS_GET_UINT16_BE(p, 2); 1510a8e1175bSopenharmony_ci p += 4; 1511a8e1175bSopenharmony_ci 1512a8e1175bSopenharmony_ci MBEDTLS_SSL_CHK_BUF_READ_PTR(p, extensions_end, extension_data_len); 1513a8e1175bSopenharmony_ci extension_data_end = p + extension_data_len; 1514a8e1175bSopenharmony_ci 1515a8e1175bSopenharmony_ci ret = mbedtls_ssl_tls13_check_received_extension( 1516a8e1175bSopenharmony_ci ssl, MBEDTLS_SSL_HS_CLIENT_HELLO, extension_type, 1517a8e1175bSopenharmony_ci allowed_exts); 1518a8e1175bSopenharmony_ci if (ret != 0) { 1519a8e1175bSopenharmony_ci return ret; 1520a8e1175bSopenharmony_ci } 1521a8e1175bSopenharmony_ci 1522a8e1175bSopenharmony_ci switch (extension_type) { 1523a8e1175bSopenharmony_ci#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) 1524a8e1175bSopenharmony_ci case MBEDTLS_TLS_EXT_SERVERNAME: 1525a8e1175bSopenharmony_ci MBEDTLS_SSL_DEBUG_MSG(3, ("found ServerName extension")); 1526a8e1175bSopenharmony_ci ret = mbedtls_ssl_parse_server_name_ext(ssl, p, 1527a8e1175bSopenharmony_ci extension_data_end); 1528a8e1175bSopenharmony_ci if (ret != 0) { 1529a8e1175bSopenharmony_ci MBEDTLS_SSL_DEBUG_RET( 1530a8e1175bSopenharmony_ci 1, "mbedtls_ssl_parse_servername_ext", ret); 1531a8e1175bSopenharmony_ci return ret; 1532a8e1175bSopenharmony_ci } 1533a8e1175bSopenharmony_ci break; 1534a8e1175bSopenharmony_ci#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */ 1535a8e1175bSopenharmony_ci 1536a8e1175bSopenharmony_ci#if defined(PSA_WANT_ALG_ECDH) || defined(PSA_WANT_ALG_FFDH) 1537a8e1175bSopenharmony_ci case MBEDTLS_TLS_EXT_SUPPORTED_GROUPS: 1538a8e1175bSopenharmony_ci MBEDTLS_SSL_DEBUG_MSG(3, ("found supported group extension")); 1539a8e1175bSopenharmony_ci 1540a8e1175bSopenharmony_ci /* Supported Groups Extension 1541a8e1175bSopenharmony_ci * 1542a8e1175bSopenharmony_ci * When sent by the client, the "supported_groups" extension 1543a8e1175bSopenharmony_ci * indicates the named groups which the client supports, 1544a8e1175bSopenharmony_ci * ordered from most preferred to least preferred. 1545a8e1175bSopenharmony_ci */ 1546a8e1175bSopenharmony_ci ret = ssl_tls13_parse_supported_groups_ext( 1547a8e1175bSopenharmony_ci ssl, p, extension_data_end); 1548a8e1175bSopenharmony_ci if (ret != 0) { 1549a8e1175bSopenharmony_ci MBEDTLS_SSL_DEBUG_RET( 1550a8e1175bSopenharmony_ci 1, "ssl_tls13_parse_supported_groups_ext", ret); 1551a8e1175bSopenharmony_ci return ret; 1552a8e1175bSopenharmony_ci } 1553a8e1175bSopenharmony_ci 1554a8e1175bSopenharmony_ci break; 1555a8e1175bSopenharmony_ci#endif /* PSA_WANT_ALG_ECDH || PSA_WANT_ALG_FFDH*/ 1556a8e1175bSopenharmony_ci 1557a8e1175bSopenharmony_ci#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED) 1558a8e1175bSopenharmony_ci case MBEDTLS_TLS_EXT_KEY_SHARE: 1559a8e1175bSopenharmony_ci MBEDTLS_SSL_DEBUG_MSG(3, ("found key share extension")); 1560a8e1175bSopenharmony_ci 1561a8e1175bSopenharmony_ci /* 1562a8e1175bSopenharmony_ci * Key Share Extension 1563a8e1175bSopenharmony_ci * 1564a8e1175bSopenharmony_ci * When sent by the client, the "key_share" extension 1565a8e1175bSopenharmony_ci * contains the endpoint's cryptographic parameters for 1566a8e1175bSopenharmony_ci * ECDHE/DHE key establishment methods. 1567a8e1175bSopenharmony_ci */ 1568a8e1175bSopenharmony_ci ret = ssl_tls13_parse_key_shares_ext( 1569a8e1175bSopenharmony_ci ssl, p, extension_data_end); 1570a8e1175bSopenharmony_ci if (ret == SSL_TLS1_3_PARSE_KEY_SHARES_EXT_NO_MATCH) { 1571a8e1175bSopenharmony_ci MBEDTLS_SSL_DEBUG_MSG(2, ("No usable share for key agreement.")); 1572a8e1175bSopenharmony_ci no_usable_share_for_key_agreement = 1; 1573a8e1175bSopenharmony_ci } 1574a8e1175bSopenharmony_ci 1575a8e1175bSopenharmony_ci if (ret < 0) { 1576a8e1175bSopenharmony_ci MBEDTLS_SSL_DEBUG_RET( 1577a8e1175bSopenharmony_ci 1, "ssl_tls13_parse_key_shares_ext", ret); 1578a8e1175bSopenharmony_ci return ret; 1579a8e1175bSopenharmony_ci } 1580a8e1175bSopenharmony_ci 1581a8e1175bSopenharmony_ci break; 1582a8e1175bSopenharmony_ci#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED */ 1583a8e1175bSopenharmony_ci 1584a8e1175bSopenharmony_ci case MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS: 1585a8e1175bSopenharmony_ci /* Already parsed */ 1586a8e1175bSopenharmony_ci break; 1587a8e1175bSopenharmony_ci 1588a8e1175bSopenharmony_ci#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED) 1589a8e1175bSopenharmony_ci case MBEDTLS_TLS_EXT_PSK_KEY_EXCHANGE_MODES: 1590a8e1175bSopenharmony_ci MBEDTLS_SSL_DEBUG_MSG( 1591a8e1175bSopenharmony_ci 3, ("found psk key exchange modes extension")); 1592a8e1175bSopenharmony_ci 1593a8e1175bSopenharmony_ci ret = ssl_tls13_parse_key_exchange_modes_ext( 1594a8e1175bSopenharmony_ci ssl, p, extension_data_end); 1595a8e1175bSopenharmony_ci if (ret != 0) { 1596a8e1175bSopenharmony_ci MBEDTLS_SSL_DEBUG_RET( 1597a8e1175bSopenharmony_ci 1, "ssl_tls13_parse_key_exchange_modes_ext", ret); 1598a8e1175bSopenharmony_ci return ret; 1599a8e1175bSopenharmony_ci } 1600a8e1175bSopenharmony_ci 1601a8e1175bSopenharmony_ci break; 1602a8e1175bSopenharmony_ci#endif 1603a8e1175bSopenharmony_ci 1604a8e1175bSopenharmony_ci case MBEDTLS_TLS_EXT_PRE_SHARED_KEY: 1605a8e1175bSopenharmony_ci MBEDTLS_SSL_DEBUG_MSG(3, ("found pre_shared_key extension")); 1606a8e1175bSopenharmony_ci if ((handshake->received_extensions & 1607a8e1175bSopenharmony_ci MBEDTLS_SSL_EXT_MASK(PSK_KEY_EXCHANGE_MODES)) == 0) { 1608a8e1175bSopenharmony_ci MBEDTLS_SSL_PEND_FATAL_ALERT( 1609a8e1175bSopenharmony_ci MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER, 1610a8e1175bSopenharmony_ci MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER); 1611a8e1175bSopenharmony_ci return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER; 1612a8e1175bSopenharmony_ci } 1613a8e1175bSopenharmony_ci#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED) 1614a8e1175bSopenharmony_ci /* Delay processing of the PSK identity once we have 1615a8e1175bSopenharmony_ci * found out which algorithms to use. We keep a pointer 1616a8e1175bSopenharmony_ci * to the buffer and the size for later processing. 1617a8e1175bSopenharmony_ci */ 1618a8e1175bSopenharmony_ci pre_shared_key_ext = p; 1619a8e1175bSopenharmony_ci pre_shared_key_ext_end = extension_data_end; 1620a8e1175bSopenharmony_ci#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED */ 1621a8e1175bSopenharmony_ci break; 1622a8e1175bSopenharmony_ci 1623a8e1175bSopenharmony_ci#if defined(MBEDTLS_SSL_ALPN) 1624a8e1175bSopenharmony_ci case MBEDTLS_TLS_EXT_ALPN: 1625a8e1175bSopenharmony_ci MBEDTLS_SSL_DEBUG_MSG(3, ("found alpn extension")); 1626a8e1175bSopenharmony_ci 1627a8e1175bSopenharmony_ci ret = mbedtls_ssl_parse_alpn_ext(ssl, p, extension_data_end); 1628a8e1175bSopenharmony_ci if (ret != 0) { 1629a8e1175bSopenharmony_ci MBEDTLS_SSL_DEBUG_RET( 1630a8e1175bSopenharmony_ci 1, ("mbedtls_ssl_parse_alpn_ext"), ret); 1631a8e1175bSopenharmony_ci return ret; 1632a8e1175bSopenharmony_ci } 1633a8e1175bSopenharmony_ci break; 1634a8e1175bSopenharmony_ci#endif /* MBEDTLS_SSL_ALPN */ 1635a8e1175bSopenharmony_ci 1636a8e1175bSopenharmony_ci#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED) 1637a8e1175bSopenharmony_ci case MBEDTLS_TLS_EXT_SIG_ALG: 1638a8e1175bSopenharmony_ci MBEDTLS_SSL_DEBUG_MSG(3, ("found signature_algorithms extension")); 1639a8e1175bSopenharmony_ci 1640a8e1175bSopenharmony_ci ret = mbedtls_ssl_parse_sig_alg_ext( 1641a8e1175bSopenharmony_ci ssl, p, extension_data_end); 1642a8e1175bSopenharmony_ci if (ret != 0) { 1643a8e1175bSopenharmony_ci MBEDTLS_SSL_DEBUG_RET( 1644a8e1175bSopenharmony_ci 1, "mbedtls_ssl_parse_sig_alg_ext", ret); 1645a8e1175bSopenharmony_ci return ret; 1646a8e1175bSopenharmony_ci } 1647a8e1175bSopenharmony_ci break; 1648a8e1175bSopenharmony_ci#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */ 1649a8e1175bSopenharmony_ci 1650a8e1175bSopenharmony_ci#if defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT) 1651a8e1175bSopenharmony_ci case MBEDTLS_TLS_EXT_RECORD_SIZE_LIMIT: 1652a8e1175bSopenharmony_ci MBEDTLS_SSL_DEBUG_MSG(3, ("found record_size_limit extension")); 1653a8e1175bSopenharmony_ci 1654a8e1175bSopenharmony_ci ret = mbedtls_ssl_tls13_parse_record_size_limit_ext( 1655a8e1175bSopenharmony_ci ssl, p, extension_data_end); 1656a8e1175bSopenharmony_ci if (ret != 0) { 1657a8e1175bSopenharmony_ci MBEDTLS_SSL_DEBUG_RET( 1658a8e1175bSopenharmony_ci 1, ("mbedtls_ssl_tls13_parse_record_size_limit_ext"), ret); 1659a8e1175bSopenharmony_ci return ret; 1660a8e1175bSopenharmony_ci } 1661a8e1175bSopenharmony_ci break; 1662a8e1175bSopenharmony_ci#endif /* MBEDTLS_SSL_RECORD_SIZE_LIMIT */ 1663a8e1175bSopenharmony_ci 1664a8e1175bSopenharmony_ci default: 1665a8e1175bSopenharmony_ci MBEDTLS_SSL_PRINT_EXT( 1666a8e1175bSopenharmony_ci 3, MBEDTLS_SSL_HS_CLIENT_HELLO, 1667a8e1175bSopenharmony_ci extension_type, "( ignored )"); 1668a8e1175bSopenharmony_ci break; 1669a8e1175bSopenharmony_ci } 1670a8e1175bSopenharmony_ci 1671a8e1175bSopenharmony_ci p += extension_data_len; 1672a8e1175bSopenharmony_ci } 1673a8e1175bSopenharmony_ci 1674a8e1175bSopenharmony_ci MBEDTLS_SSL_PRINT_EXTS(3, MBEDTLS_SSL_HS_CLIENT_HELLO, 1675a8e1175bSopenharmony_ci handshake->received_extensions); 1676a8e1175bSopenharmony_ci 1677a8e1175bSopenharmony_ci ret = mbedtls_ssl_add_hs_hdr_to_checksum(ssl, 1678a8e1175bSopenharmony_ci MBEDTLS_SSL_HS_CLIENT_HELLO, 1679a8e1175bSopenharmony_ci p - buf); 1680a8e1175bSopenharmony_ci if (0 != ret) { 1681a8e1175bSopenharmony_ci MBEDTLS_SSL_DEBUG_RET(1, ("mbedtls_ssl_add_hs_hdr_to_checksum"), ret); 1682a8e1175bSopenharmony_ci return ret; 1683a8e1175bSopenharmony_ci } 1684a8e1175bSopenharmony_ci 1685a8e1175bSopenharmony_ci#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED) 1686a8e1175bSopenharmony_ci /* Update checksum with either 1687a8e1175bSopenharmony_ci * - The entire content of the CH message, if no PSK extension is present 1688a8e1175bSopenharmony_ci * - The content up to but excluding the PSK extension, if present. 1689a8e1175bSopenharmony_ci * Always parse the pre-shared-key extension when present in the 1690a8e1175bSopenharmony_ci * ClientHello even if some pre-requisites for PSK key exchange modes are 1691a8e1175bSopenharmony_ci * not met. That way we always validate the syntax of the extension. 1692a8e1175bSopenharmony_ci */ 1693a8e1175bSopenharmony_ci if (handshake->received_extensions & MBEDTLS_SSL_EXT_MASK(PRE_SHARED_KEY)) { 1694a8e1175bSopenharmony_ci ret = handshake->update_checksum(ssl, buf, 1695a8e1175bSopenharmony_ci pre_shared_key_ext - buf); 1696a8e1175bSopenharmony_ci if (0 != ret) { 1697a8e1175bSopenharmony_ci MBEDTLS_SSL_DEBUG_RET(1, ("update_checksum"), ret); 1698a8e1175bSopenharmony_ci return ret; 1699a8e1175bSopenharmony_ci } 1700a8e1175bSopenharmony_ci ret = ssl_tls13_parse_pre_shared_key_ext(ssl, 1701a8e1175bSopenharmony_ci pre_shared_key_ext, 1702a8e1175bSopenharmony_ci pre_shared_key_ext_end, 1703a8e1175bSopenharmony_ci cipher_suites, 1704a8e1175bSopenharmony_ci cipher_suites_end, 1705a8e1175bSopenharmony_ci &psk); 1706a8e1175bSopenharmony_ci if (ret == 0) { 1707a8e1175bSopenharmony_ci got_psk = 1; 1708a8e1175bSopenharmony_ci } else if (ret != MBEDTLS_ERR_SSL_UNKNOWN_IDENTITY) { 1709a8e1175bSopenharmony_ci MBEDTLS_SSL_DEBUG_RET( 1710a8e1175bSopenharmony_ci 1, "ssl_tls13_parse_pre_shared_key_ext", ret); 1711a8e1175bSopenharmony_ci return ret; 1712a8e1175bSopenharmony_ci } 1713a8e1175bSopenharmony_ci } else 1714a8e1175bSopenharmony_ci#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED */ 1715a8e1175bSopenharmony_ci { 1716a8e1175bSopenharmony_ci ret = handshake->update_checksum(ssl, buf, p - buf); 1717a8e1175bSopenharmony_ci if (0 != ret) { 1718a8e1175bSopenharmony_ci MBEDTLS_SSL_DEBUG_RET(1, ("update_checksum"), ret); 1719a8e1175bSopenharmony_ci return ret; 1720a8e1175bSopenharmony_ci } 1721a8e1175bSopenharmony_ci } 1722a8e1175bSopenharmony_ci 1723a8e1175bSopenharmony_ci /* 1724a8e1175bSopenharmony_ci * Determine the key exchange algorithm to use. 1725a8e1175bSopenharmony_ci * There are three types of key exchanges supported in TLS 1.3: 1726a8e1175bSopenharmony_ci * - (EC)DH with ECDSA, 1727a8e1175bSopenharmony_ci * - (EC)DH with PSK, 1728a8e1175bSopenharmony_ci * - plain PSK. 1729a8e1175bSopenharmony_ci * 1730a8e1175bSopenharmony_ci * The PSK-based key exchanges may additionally be used with 0-RTT. 1731a8e1175bSopenharmony_ci * 1732a8e1175bSopenharmony_ci * Our built-in order of preference is 1733a8e1175bSopenharmony_ci * 1 ) (EC)DHE-PSK Mode ( psk_ephemeral ) 1734a8e1175bSopenharmony_ci * 2 ) Certificate Mode ( ephemeral ) 1735a8e1175bSopenharmony_ci * 3 ) Plain PSK Mode ( psk ) 1736a8e1175bSopenharmony_ci */ 1737a8e1175bSopenharmony_ci#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED) 1738a8e1175bSopenharmony_ci if (got_psk && (psk.key_exchange_mode == 1739a8e1175bSopenharmony_ci MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL)) { 1740a8e1175bSopenharmony_ci handshake->key_exchange_mode = 1741a8e1175bSopenharmony_ci MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL; 1742a8e1175bSopenharmony_ci MBEDTLS_SSL_DEBUG_MSG(2, ("key exchange mode: psk_ephemeral")); 1743a8e1175bSopenharmony_ci 1744a8e1175bSopenharmony_ci } else 1745a8e1175bSopenharmony_ci#endif 1746a8e1175bSopenharmony_ci if (ssl_tls13_key_exchange_is_ephemeral_available(ssl)) { 1747a8e1175bSopenharmony_ci handshake->key_exchange_mode = 1748a8e1175bSopenharmony_ci MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL; 1749a8e1175bSopenharmony_ci MBEDTLS_SSL_DEBUG_MSG(2, ("key exchange mode: ephemeral")); 1750a8e1175bSopenharmony_ci 1751a8e1175bSopenharmony_ci } 1752a8e1175bSopenharmony_ci#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED) 1753a8e1175bSopenharmony_ci else if (got_psk && (psk.key_exchange_mode == 1754a8e1175bSopenharmony_ci MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK)) { 1755a8e1175bSopenharmony_ci handshake->key_exchange_mode = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK; 1756a8e1175bSopenharmony_ci MBEDTLS_SSL_DEBUG_MSG(2, ("key exchange mode: psk")); 1757a8e1175bSopenharmony_ci } 1758a8e1175bSopenharmony_ci#endif 1759a8e1175bSopenharmony_ci else { 1760a8e1175bSopenharmony_ci MBEDTLS_SSL_DEBUG_MSG( 1761a8e1175bSopenharmony_ci 1, 1762a8e1175bSopenharmony_ci ("ClientHello message misses mandatory extensions.")); 1763a8e1175bSopenharmony_ci MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_MISSING_EXTENSION, 1764a8e1175bSopenharmony_ci MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER); 1765a8e1175bSopenharmony_ci return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER; 1766a8e1175bSopenharmony_ci } 1767a8e1175bSopenharmony_ci 1768a8e1175bSopenharmony_ci#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED) 1769a8e1175bSopenharmony_ci if (handshake->key_exchange_mode & 1770a8e1175bSopenharmony_ci MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_ALL) { 1771a8e1175bSopenharmony_ci handshake->ciphersuite_info = psk.ciphersuite_info; 1772a8e1175bSopenharmony_ci ssl->session_negotiate->ciphersuite = psk.ciphersuite_info->id; 1773a8e1175bSopenharmony_ci 1774a8e1175bSopenharmony_ci MBEDTLS_SSL_DEBUG_MSG(2, ("Select PSK ciphersuite: %04x - %s", 1775a8e1175bSopenharmony_ci ((unsigned) psk.ciphersuite_info->id), 1776a8e1175bSopenharmony_ci psk.ciphersuite_info->name)); 1777a8e1175bSopenharmony_ci 1778a8e1175bSopenharmony_ci if (psk.type == MBEDTLS_SSL_TLS1_3_PSK_RESUMPTION) { 1779a8e1175bSopenharmony_ci handshake->resume = 1; 1780a8e1175bSopenharmony_ci } 1781a8e1175bSopenharmony_ci } 1782a8e1175bSopenharmony_ci#endif 1783a8e1175bSopenharmony_ci 1784a8e1175bSopenharmony_ci if (handshake->key_exchange_mode != 1785a8e1175bSopenharmony_ci MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK) { 1786a8e1175bSopenharmony_ci hrr_required = (no_usable_share_for_key_agreement != 0); 1787a8e1175bSopenharmony_ci } 1788a8e1175bSopenharmony_ci 1789a8e1175bSopenharmony_ci mbedtls_ssl_optimize_checksum(ssl, handshake->ciphersuite_info); 1790a8e1175bSopenharmony_ci 1791a8e1175bSopenharmony_ci return hrr_required ? SSL_CLIENT_HELLO_HRR_REQUIRED : SSL_CLIENT_HELLO_OK; 1792a8e1175bSopenharmony_ci} 1793a8e1175bSopenharmony_ci 1794a8e1175bSopenharmony_ci#if defined(MBEDTLS_SSL_EARLY_DATA) 1795a8e1175bSopenharmony_cistatic int ssl_tls13_check_early_data_requirements(mbedtls_ssl_context *ssl) 1796a8e1175bSopenharmony_ci{ 1797a8e1175bSopenharmony_ci mbedtls_ssl_handshake_params *handshake = ssl->handshake; 1798a8e1175bSopenharmony_ci 1799a8e1175bSopenharmony_ci if (ssl->conf->early_data_enabled == MBEDTLS_SSL_EARLY_DATA_DISABLED) { 1800a8e1175bSopenharmony_ci MBEDTLS_SSL_DEBUG_MSG( 1801a8e1175bSopenharmony_ci 1, 1802a8e1175bSopenharmony_ci ("EarlyData: rejected, feature disabled in server configuration.")); 1803a8e1175bSopenharmony_ci return -1; 1804a8e1175bSopenharmony_ci } 1805a8e1175bSopenharmony_ci 1806a8e1175bSopenharmony_ci if (!handshake->resume) { 1807a8e1175bSopenharmony_ci /* We currently support early data only in the case of PSKs established 1808a8e1175bSopenharmony_ci via a NewSessionTicket message thus in the case of a session 1809a8e1175bSopenharmony_ci resumption. */ 1810a8e1175bSopenharmony_ci MBEDTLS_SSL_DEBUG_MSG( 1811a8e1175bSopenharmony_ci 1, ("EarlyData: rejected, not a session resumption.")); 1812a8e1175bSopenharmony_ci return -1; 1813a8e1175bSopenharmony_ci } 1814a8e1175bSopenharmony_ci 1815a8e1175bSopenharmony_ci /* RFC 8446 4.2.10 1816a8e1175bSopenharmony_ci * 1817a8e1175bSopenharmony_ci * In order to accept early data, the server MUST have accepted a PSK cipher 1818a8e1175bSopenharmony_ci * suite and selected the first key offered in the client's "pre_shared_key" 1819a8e1175bSopenharmony_ci * extension. In addition, it MUST verify that the following values are the 1820a8e1175bSopenharmony_ci * same as those associated with the selected PSK: 1821a8e1175bSopenharmony_ci * - The TLS version number 1822a8e1175bSopenharmony_ci * - The selected cipher suite 1823a8e1175bSopenharmony_ci * - The selected ALPN [RFC7301] protocol, if any 1824a8e1175bSopenharmony_ci * 1825a8e1175bSopenharmony_ci * NOTE: 1826a8e1175bSopenharmony_ci * - The TLS version number is checked in 1827a8e1175bSopenharmony_ci * ssl_tls13_offered_psks_check_identity_match_ticket(). 1828a8e1175bSopenharmony_ci */ 1829a8e1175bSopenharmony_ci 1830a8e1175bSopenharmony_ci if (handshake->selected_identity != 0) { 1831a8e1175bSopenharmony_ci MBEDTLS_SSL_DEBUG_MSG( 1832a8e1175bSopenharmony_ci 1, ("EarlyData: rejected, the selected key in " 1833a8e1175bSopenharmony_ci "`pre_shared_key` is not the first one.")); 1834a8e1175bSopenharmony_ci return -1; 1835a8e1175bSopenharmony_ci } 1836a8e1175bSopenharmony_ci 1837a8e1175bSopenharmony_ci if (handshake->ciphersuite_info->id != 1838a8e1175bSopenharmony_ci ssl->session_negotiate->ciphersuite) { 1839a8e1175bSopenharmony_ci MBEDTLS_SSL_DEBUG_MSG( 1840a8e1175bSopenharmony_ci 1, ("EarlyData: rejected, the selected ciphersuite is not the one " 1841a8e1175bSopenharmony_ci "of the selected pre-shared key.")); 1842a8e1175bSopenharmony_ci return -1; 1843a8e1175bSopenharmony_ci 1844a8e1175bSopenharmony_ci } 1845a8e1175bSopenharmony_ci 1846a8e1175bSopenharmony_ci if (!mbedtls_ssl_tls13_session_ticket_allow_early_data(ssl->session_negotiate)) { 1847a8e1175bSopenharmony_ci MBEDTLS_SSL_DEBUG_MSG( 1848a8e1175bSopenharmony_ci 1, 1849a8e1175bSopenharmony_ci ("EarlyData: rejected, early_data not allowed in ticket " 1850a8e1175bSopenharmony_ci "permission bits.")); 1851a8e1175bSopenharmony_ci return -1; 1852a8e1175bSopenharmony_ci } 1853a8e1175bSopenharmony_ci 1854a8e1175bSopenharmony_ci#if defined(MBEDTLS_SSL_ALPN) 1855a8e1175bSopenharmony_ci const char *alpn = mbedtls_ssl_get_alpn_protocol(ssl); 1856a8e1175bSopenharmony_ci size_t alpn_len; 1857a8e1175bSopenharmony_ci 1858a8e1175bSopenharmony_ci if (alpn == NULL && ssl->session_negotiate->ticket_alpn == NULL) { 1859a8e1175bSopenharmony_ci return 0; 1860a8e1175bSopenharmony_ci } 1861a8e1175bSopenharmony_ci 1862a8e1175bSopenharmony_ci if (alpn != NULL) { 1863a8e1175bSopenharmony_ci alpn_len = strlen(alpn); 1864a8e1175bSopenharmony_ci } 1865a8e1175bSopenharmony_ci 1866a8e1175bSopenharmony_ci if (alpn == NULL || 1867a8e1175bSopenharmony_ci ssl->session_negotiate->ticket_alpn == NULL || 1868a8e1175bSopenharmony_ci alpn_len != strlen(ssl->session_negotiate->ticket_alpn) || 1869a8e1175bSopenharmony_ci (memcmp(alpn, ssl->session_negotiate->ticket_alpn, alpn_len) != 0)) { 1870a8e1175bSopenharmony_ci MBEDTLS_SSL_DEBUG_MSG(1, ("EarlyData: rejected, the selected ALPN is different " 1871a8e1175bSopenharmony_ci "from the one associated with the pre-shared key.")); 1872a8e1175bSopenharmony_ci return -1; 1873a8e1175bSopenharmony_ci } 1874a8e1175bSopenharmony_ci#endif 1875a8e1175bSopenharmony_ci 1876a8e1175bSopenharmony_ci return 0; 1877a8e1175bSopenharmony_ci} 1878a8e1175bSopenharmony_ci#endif /* MBEDTLS_SSL_EARLY_DATA */ 1879a8e1175bSopenharmony_ci 1880a8e1175bSopenharmony_ci/* Update the handshake state machine */ 1881a8e1175bSopenharmony_ci 1882a8e1175bSopenharmony_ciMBEDTLS_CHECK_RETURN_CRITICAL 1883a8e1175bSopenharmony_cistatic int ssl_tls13_postprocess_client_hello(mbedtls_ssl_context *ssl, 1884a8e1175bSopenharmony_ci int hrr_required) 1885a8e1175bSopenharmony_ci{ 1886a8e1175bSopenharmony_ci int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; 1887a8e1175bSopenharmony_ci 1888a8e1175bSopenharmony_ci /* 1889a8e1175bSopenharmony_ci * Server certificate selection 1890a8e1175bSopenharmony_ci */ 1891a8e1175bSopenharmony_ci if (ssl->conf->f_cert_cb && (ret = ssl->conf->f_cert_cb(ssl)) != 0) { 1892a8e1175bSopenharmony_ci MBEDTLS_SSL_DEBUG_RET(1, "f_cert_cb", ret); 1893a8e1175bSopenharmony_ci return ret; 1894a8e1175bSopenharmony_ci } 1895a8e1175bSopenharmony_ci#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) 1896a8e1175bSopenharmony_ci ssl->handshake->sni_name = NULL; 1897a8e1175bSopenharmony_ci ssl->handshake->sni_name_len = 0; 1898a8e1175bSopenharmony_ci#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */ 1899a8e1175bSopenharmony_ci 1900a8e1175bSopenharmony_ci ret = mbedtls_ssl_tls13_key_schedule_stage_early(ssl); 1901a8e1175bSopenharmony_ci if (ret != 0) { 1902a8e1175bSopenharmony_ci MBEDTLS_SSL_DEBUG_RET(1, 1903a8e1175bSopenharmony_ci "mbedtls_ssl_tls1_3_key_schedule_stage_early", ret); 1904a8e1175bSopenharmony_ci return ret; 1905a8e1175bSopenharmony_ci } 1906a8e1175bSopenharmony_ci 1907a8e1175bSopenharmony_ci#if defined(MBEDTLS_SSL_EARLY_DATA) 1908a8e1175bSopenharmony_ci if (ssl->handshake->received_extensions & MBEDTLS_SSL_EXT_MASK(EARLY_DATA)) { 1909a8e1175bSopenharmony_ci ssl->handshake->early_data_accepted = 1910a8e1175bSopenharmony_ci (!hrr_required) && (ssl_tls13_check_early_data_requirements(ssl) == 0); 1911a8e1175bSopenharmony_ci 1912a8e1175bSopenharmony_ci if (ssl->handshake->early_data_accepted) { 1913a8e1175bSopenharmony_ci ret = mbedtls_ssl_tls13_compute_early_transform(ssl); 1914a8e1175bSopenharmony_ci if (ret != 0) { 1915a8e1175bSopenharmony_ci MBEDTLS_SSL_DEBUG_RET( 1916a8e1175bSopenharmony_ci 1, "mbedtls_ssl_tls13_compute_early_transform", ret); 1917a8e1175bSopenharmony_ci return ret; 1918a8e1175bSopenharmony_ci } 1919a8e1175bSopenharmony_ci } else { 1920a8e1175bSopenharmony_ci ssl->discard_early_data_record = 1921a8e1175bSopenharmony_ci hrr_required ? 1922a8e1175bSopenharmony_ci MBEDTLS_SSL_EARLY_DATA_DISCARD : 1923a8e1175bSopenharmony_ci MBEDTLS_SSL_EARLY_DATA_TRY_TO_DEPROTECT_AND_DISCARD; 1924a8e1175bSopenharmony_ci } 1925a8e1175bSopenharmony_ci } 1926a8e1175bSopenharmony_ci#else 1927a8e1175bSopenharmony_ci ((void) hrr_required); 1928a8e1175bSopenharmony_ci#endif /* MBEDTLS_SSL_EARLY_DATA */ 1929a8e1175bSopenharmony_ci 1930a8e1175bSopenharmony_ci return 0; 1931a8e1175bSopenharmony_ci} 1932a8e1175bSopenharmony_ci 1933a8e1175bSopenharmony_ci/* 1934a8e1175bSopenharmony_ci * Main entry point from the state machine; orchestrates the otherfunctions. 1935a8e1175bSopenharmony_ci */ 1936a8e1175bSopenharmony_ci 1937a8e1175bSopenharmony_ciMBEDTLS_CHECK_RETURN_CRITICAL 1938a8e1175bSopenharmony_cistatic int ssl_tls13_process_client_hello(mbedtls_ssl_context *ssl) 1939a8e1175bSopenharmony_ci{ 1940a8e1175bSopenharmony_ci 1941a8e1175bSopenharmony_ci int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; 1942a8e1175bSopenharmony_ci unsigned char *buf = NULL; 1943a8e1175bSopenharmony_ci size_t buflen = 0; 1944a8e1175bSopenharmony_ci int parse_client_hello_ret; 1945a8e1175bSopenharmony_ci 1946a8e1175bSopenharmony_ci MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse client hello")); 1947a8e1175bSopenharmony_ci 1948a8e1175bSopenharmony_ci MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_tls13_fetch_handshake_msg( 1949a8e1175bSopenharmony_ci ssl, MBEDTLS_SSL_HS_CLIENT_HELLO, 1950a8e1175bSopenharmony_ci &buf, &buflen)); 1951a8e1175bSopenharmony_ci 1952a8e1175bSopenharmony_ci MBEDTLS_SSL_PROC_CHK_NEG(ssl_tls13_parse_client_hello(ssl, buf, 1953a8e1175bSopenharmony_ci buf + buflen)); 1954a8e1175bSopenharmony_ci parse_client_hello_ret = ret; /* Store positive return value of 1955a8e1175bSopenharmony_ci * parse_client_hello, 1956a8e1175bSopenharmony_ci * as negative error codes are handled 1957a8e1175bSopenharmony_ci * by MBEDTLS_SSL_PROC_CHK_NEG. */ 1958a8e1175bSopenharmony_ci 1959a8e1175bSopenharmony_ci /* 1960a8e1175bSopenharmony_ci * Version 1.2 of the protocol has to be used for the handshake. 1961a8e1175bSopenharmony_ci * If TLS 1.2 is not supported, abort the handshake. Otherwise, set the 1962a8e1175bSopenharmony_ci * ssl->keep_current_message flag for the ClientHello to be kept and parsed 1963a8e1175bSopenharmony_ci * as a TLS 1.2 ClientHello. We also change ssl->tls_version to 1964a8e1175bSopenharmony_ci * MBEDTLS_SSL_VERSION_TLS1_2 thus from now on mbedtls_ssl_handshake_step() 1965a8e1175bSopenharmony_ci * will dispatch to the TLS 1.2 state machine. 1966a8e1175bSopenharmony_ci */ 1967a8e1175bSopenharmony_ci if (SSL_CLIENT_HELLO_TLS1_2 == parse_client_hello_ret) { 1968a8e1175bSopenharmony_ci /* Check if server supports TLS 1.2 */ 1969a8e1175bSopenharmony_ci if (!mbedtls_ssl_conf_is_tls12_enabled(ssl->conf)) { 1970a8e1175bSopenharmony_ci MBEDTLS_SSL_DEBUG_MSG( 1971a8e1175bSopenharmony_ci 1, ("TLS 1.2 not supported.")); 1972a8e1175bSopenharmony_ci MBEDTLS_SSL_PEND_FATAL_ALERT( 1973a8e1175bSopenharmony_ci MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION, 1974a8e1175bSopenharmony_ci MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION); 1975a8e1175bSopenharmony_ci return MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION; 1976a8e1175bSopenharmony_ci } 1977a8e1175bSopenharmony_ci ssl->keep_current_message = 1; 1978a8e1175bSopenharmony_ci ssl->tls_version = MBEDTLS_SSL_VERSION_TLS1_2; 1979a8e1175bSopenharmony_ci return 0; 1980a8e1175bSopenharmony_ci } 1981a8e1175bSopenharmony_ci 1982a8e1175bSopenharmony_ci MBEDTLS_SSL_PROC_CHK( 1983a8e1175bSopenharmony_ci ssl_tls13_postprocess_client_hello(ssl, parse_client_hello_ret == 1984a8e1175bSopenharmony_ci SSL_CLIENT_HELLO_HRR_REQUIRED)); 1985a8e1175bSopenharmony_ci 1986a8e1175bSopenharmony_ci if (SSL_CLIENT_HELLO_OK == parse_client_hello_ret) { 1987a8e1175bSopenharmony_ci mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_HELLO); 1988a8e1175bSopenharmony_ci } else { 1989a8e1175bSopenharmony_ci mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HELLO_RETRY_REQUEST); 1990a8e1175bSopenharmony_ci } 1991a8e1175bSopenharmony_ci 1992a8e1175bSopenharmony_cicleanup: 1993a8e1175bSopenharmony_ci 1994a8e1175bSopenharmony_ci MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse client hello")); 1995a8e1175bSopenharmony_ci return ret; 1996a8e1175bSopenharmony_ci} 1997a8e1175bSopenharmony_ci 1998a8e1175bSopenharmony_ci/* 1999a8e1175bSopenharmony_ci * Handler for MBEDTLS_SSL_SERVER_HELLO 2000a8e1175bSopenharmony_ci */ 2001a8e1175bSopenharmony_ciMBEDTLS_CHECK_RETURN_CRITICAL 2002a8e1175bSopenharmony_cistatic int ssl_tls13_prepare_server_hello(mbedtls_ssl_context *ssl) 2003a8e1175bSopenharmony_ci{ 2004a8e1175bSopenharmony_ci int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; 2005a8e1175bSopenharmony_ci unsigned char *server_randbytes = 2006a8e1175bSopenharmony_ci ssl->handshake->randbytes + MBEDTLS_CLIENT_HELLO_RANDOM_LEN; 2007a8e1175bSopenharmony_ci 2008a8e1175bSopenharmony_ci if ((ret = ssl->conf->f_rng(ssl->conf->p_rng, server_randbytes, 2009a8e1175bSopenharmony_ci MBEDTLS_SERVER_HELLO_RANDOM_LEN)) != 0) { 2010a8e1175bSopenharmony_ci MBEDTLS_SSL_DEBUG_RET(1, "f_rng", ret); 2011a8e1175bSopenharmony_ci return ret; 2012a8e1175bSopenharmony_ci } 2013a8e1175bSopenharmony_ci 2014a8e1175bSopenharmony_ci MBEDTLS_SSL_DEBUG_BUF(3, "server hello, random bytes", server_randbytes, 2015a8e1175bSopenharmony_ci MBEDTLS_SERVER_HELLO_RANDOM_LEN); 2016a8e1175bSopenharmony_ci 2017a8e1175bSopenharmony_ci#if defined(MBEDTLS_HAVE_TIME) 2018a8e1175bSopenharmony_ci ssl->session_negotiate->start = mbedtls_time(NULL); 2019a8e1175bSopenharmony_ci#endif /* MBEDTLS_HAVE_TIME */ 2020a8e1175bSopenharmony_ci 2021a8e1175bSopenharmony_ci return ret; 2022a8e1175bSopenharmony_ci} 2023a8e1175bSopenharmony_ci 2024a8e1175bSopenharmony_ci/* 2025a8e1175bSopenharmony_ci * ssl_tls13_write_server_hello_supported_versions_ext (): 2026a8e1175bSopenharmony_ci * 2027a8e1175bSopenharmony_ci * struct { 2028a8e1175bSopenharmony_ci * ProtocolVersion selected_version; 2029a8e1175bSopenharmony_ci * } SupportedVersions; 2030a8e1175bSopenharmony_ci */ 2031a8e1175bSopenharmony_ciMBEDTLS_CHECK_RETURN_CRITICAL 2032a8e1175bSopenharmony_cistatic int ssl_tls13_write_server_hello_supported_versions_ext( 2033a8e1175bSopenharmony_ci mbedtls_ssl_context *ssl, 2034a8e1175bSopenharmony_ci unsigned char *buf, 2035a8e1175bSopenharmony_ci unsigned char *end, 2036a8e1175bSopenharmony_ci size_t *out_len) 2037a8e1175bSopenharmony_ci{ 2038a8e1175bSopenharmony_ci *out_len = 0; 2039a8e1175bSopenharmony_ci 2040a8e1175bSopenharmony_ci MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, write selected version")); 2041a8e1175bSopenharmony_ci 2042a8e1175bSopenharmony_ci /* Check if we have space to write the extension: 2043a8e1175bSopenharmony_ci * - extension_type (2 bytes) 2044a8e1175bSopenharmony_ci * - extension_data_length (2 bytes) 2045a8e1175bSopenharmony_ci * - selected_version (2 bytes) 2046a8e1175bSopenharmony_ci */ 2047a8e1175bSopenharmony_ci MBEDTLS_SSL_CHK_BUF_PTR(buf, end, 6); 2048a8e1175bSopenharmony_ci 2049a8e1175bSopenharmony_ci MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS, buf, 0); 2050a8e1175bSopenharmony_ci 2051a8e1175bSopenharmony_ci MBEDTLS_PUT_UINT16_BE(2, buf, 2); 2052a8e1175bSopenharmony_ci 2053a8e1175bSopenharmony_ci mbedtls_ssl_write_version(buf + 4, 2054a8e1175bSopenharmony_ci ssl->conf->transport, 2055a8e1175bSopenharmony_ci ssl->tls_version); 2056a8e1175bSopenharmony_ci 2057a8e1175bSopenharmony_ci MBEDTLS_SSL_DEBUG_MSG(3, ("supported version: [%04x]", 2058a8e1175bSopenharmony_ci ssl->tls_version)); 2059a8e1175bSopenharmony_ci 2060a8e1175bSopenharmony_ci *out_len = 6; 2061a8e1175bSopenharmony_ci 2062a8e1175bSopenharmony_ci mbedtls_ssl_tls13_set_hs_sent_ext_mask( 2063a8e1175bSopenharmony_ci ssl, MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS); 2064a8e1175bSopenharmony_ci 2065a8e1175bSopenharmony_ci return 0; 2066a8e1175bSopenharmony_ci} 2067a8e1175bSopenharmony_ci 2068a8e1175bSopenharmony_ci 2069a8e1175bSopenharmony_ci 2070a8e1175bSopenharmony_ci/* Generate and export a single key share. For hybrid KEMs, this can 2071a8e1175bSopenharmony_ci * be called multiple times with the different components of the hybrid. */ 2072a8e1175bSopenharmony_ciMBEDTLS_CHECK_RETURN_CRITICAL 2073a8e1175bSopenharmony_cistatic int ssl_tls13_generate_and_write_key_share(mbedtls_ssl_context *ssl, 2074a8e1175bSopenharmony_ci uint16_t named_group, 2075a8e1175bSopenharmony_ci unsigned char *buf, 2076a8e1175bSopenharmony_ci unsigned char *end, 2077a8e1175bSopenharmony_ci size_t *out_len) 2078a8e1175bSopenharmony_ci{ 2079a8e1175bSopenharmony_ci int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; 2080a8e1175bSopenharmony_ci 2081a8e1175bSopenharmony_ci *out_len = 0; 2082a8e1175bSopenharmony_ci 2083a8e1175bSopenharmony_ci#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED) 2084a8e1175bSopenharmony_ci if (mbedtls_ssl_tls13_named_group_is_ecdhe(named_group) || 2085a8e1175bSopenharmony_ci mbedtls_ssl_tls13_named_group_is_ffdh(named_group)) { 2086a8e1175bSopenharmony_ci ret = mbedtls_ssl_tls13_generate_and_write_xxdh_key_exchange( 2087a8e1175bSopenharmony_ci ssl, named_group, buf, end, out_len); 2088a8e1175bSopenharmony_ci if (ret != 0) { 2089a8e1175bSopenharmony_ci MBEDTLS_SSL_DEBUG_RET( 2090a8e1175bSopenharmony_ci 1, "mbedtls_ssl_tls13_generate_and_write_xxdh_key_exchange", 2091a8e1175bSopenharmony_ci ret); 2092a8e1175bSopenharmony_ci return ret; 2093a8e1175bSopenharmony_ci } 2094a8e1175bSopenharmony_ci } else 2095a8e1175bSopenharmony_ci#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED */ 2096a8e1175bSopenharmony_ci if (0 /* Other kinds of KEMs */) { 2097a8e1175bSopenharmony_ci } else { 2098a8e1175bSopenharmony_ci ((void) ssl); 2099a8e1175bSopenharmony_ci ((void) named_group); 2100a8e1175bSopenharmony_ci ((void) buf); 2101a8e1175bSopenharmony_ci ((void) end); 2102a8e1175bSopenharmony_ci ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR; 2103a8e1175bSopenharmony_ci } 2104a8e1175bSopenharmony_ci 2105a8e1175bSopenharmony_ci return ret; 2106a8e1175bSopenharmony_ci} 2107a8e1175bSopenharmony_ci 2108a8e1175bSopenharmony_ci/* 2109a8e1175bSopenharmony_ci * ssl_tls13_write_key_share_ext 2110a8e1175bSopenharmony_ci * 2111a8e1175bSopenharmony_ci * Structure of key_share extension in ServerHello: 2112a8e1175bSopenharmony_ci * 2113a8e1175bSopenharmony_ci * struct { 2114a8e1175bSopenharmony_ci * NamedGroup group; 2115a8e1175bSopenharmony_ci * opaque key_exchange<1..2^16-1>; 2116a8e1175bSopenharmony_ci * } KeyShareEntry; 2117a8e1175bSopenharmony_ci * struct { 2118a8e1175bSopenharmony_ci * KeyShareEntry server_share; 2119a8e1175bSopenharmony_ci * } KeyShareServerHello; 2120a8e1175bSopenharmony_ci */ 2121a8e1175bSopenharmony_ciMBEDTLS_CHECK_RETURN_CRITICAL 2122a8e1175bSopenharmony_cistatic int ssl_tls13_write_key_share_ext(mbedtls_ssl_context *ssl, 2123a8e1175bSopenharmony_ci unsigned char *buf, 2124a8e1175bSopenharmony_ci unsigned char *end, 2125a8e1175bSopenharmony_ci size_t *out_len) 2126a8e1175bSopenharmony_ci{ 2127a8e1175bSopenharmony_ci int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; 2128a8e1175bSopenharmony_ci unsigned char *p = buf; 2129a8e1175bSopenharmony_ci uint16_t group = ssl->handshake->offered_group_id; 2130a8e1175bSopenharmony_ci unsigned char *server_share = buf + 4; 2131a8e1175bSopenharmony_ci size_t key_exchange_length; 2132a8e1175bSopenharmony_ci 2133a8e1175bSopenharmony_ci *out_len = 0; 2134a8e1175bSopenharmony_ci 2135a8e1175bSopenharmony_ci MBEDTLS_SSL_DEBUG_MSG(3, ("server hello, adding key share extension")); 2136a8e1175bSopenharmony_ci 2137a8e1175bSopenharmony_ci MBEDTLS_SSL_DEBUG_MSG(2, ("server hello, write selected_group: %s (%04x)", 2138a8e1175bSopenharmony_ci mbedtls_ssl_named_group_to_str(group), 2139a8e1175bSopenharmony_ci group)); 2140a8e1175bSopenharmony_ci 2141a8e1175bSopenharmony_ci /* Check if we have space for header and length fields: 2142a8e1175bSopenharmony_ci * - extension_type (2 bytes) 2143a8e1175bSopenharmony_ci * - extension_data_length (2 bytes) 2144a8e1175bSopenharmony_ci * - group (2 bytes) 2145a8e1175bSopenharmony_ci * - key_exchange_length (2 bytes) 2146a8e1175bSopenharmony_ci */ 2147a8e1175bSopenharmony_ci MBEDTLS_SSL_CHK_BUF_PTR(p, end, 8); 2148a8e1175bSopenharmony_ci MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_KEY_SHARE, p, 0); 2149a8e1175bSopenharmony_ci MBEDTLS_PUT_UINT16_BE(group, server_share, 0); 2150a8e1175bSopenharmony_ci p += 8; 2151a8e1175bSopenharmony_ci 2152a8e1175bSopenharmony_ci /* When we introduce PQC-ECDHE hybrids, we'll want to call this 2153a8e1175bSopenharmony_ci * function multiple times. */ 2154a8e1175bSopenharmony_ci ret = ssl_tls13_generate_and_write_key_share( 2155a8e1175bSopenharmony_ci ssl, group, server_share + 4, end, &key_exchange_length); 2156a8e1175bSopenharmony_ci if (ret != 0) { 2157a8e1175bSopenharmony_ci return ret; 2158a8e1175bSopenharmony_ci } 2159a8e1175bSopenharmony_ci p += key_exchange_length; 2160a8e1175bSopenharmony_ci 2161a8e1175bSopenharmony_ci MBEDTLS_PUT_UINT16_BE(key_exchange_length, server_share + 2, 0); 2162a8e1175bSopenharmony_ci 2163a8e1175bSopenharmony_ci MBEDTLS_PUT_UINT16_BE(p - server_share, buf, 2); 2164a8e1175bSopenharmony_ci 2165a8e1175bSopenharmony_ci *out_len = p - buf; 2166a8e1175bSopenharmony_ci 2167a8e1175bSopenharmony_ci mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_KEY_SHARE); 2168a8e1175bSopenharmony_ci 2169a8e1175bSopenharmony_ci return 0; 2170a8e1175bSopenharmony_ci} 2171a8e1175bSopenharmony_ci 2172a8e1175bSopenharmony_ciMBEDTLS_CHECK_RETURN_CRITICAL 2173a8e1175bSopenharmony_cistatic int ssl_tls13_write_hrr_key_share_ext(mbedtls_ssl_context *ssl, 2174a8e1175bSopenharmony_ci unsigned char *buf, 2175a8e1175bSopenharmony_ci unsigned char *end, 2176a8e1175bSopenharmony_ci size_t *out_len) 2177a8e1175bSopenharmony_ci{ 2178a8e1175bSopenharmony_ci uint16_t selected_group = ssl->handshake->hrr_selected_group; 2179a8e1175bSopenharmony_ci /* key_share Extension 2180a8e1175bSopenharmony_ci * 2181a8e1175bSopenharmony_ci * struct { 2182a8e1175bSopenharmony_ci * select (Handshake.msg_type) { 2183a8e1175bSopenharmony_ci * ... 2184a8e1175bSopenharmony_ci * case hello_retry_request: 2185a8e1175bSopenharmony_ci * NamedGroup selected_group; 2186a8e1175bSopenharmony_ci * ... 2187a8e1175bSopenharmony_ci * }; 2188a8e1175bSopenharmony_ci * } KeyShare; 2189a8e1175bSopenharmony_ci */ 2190a8e1175bSopenharmony_ci 2191a8e1175bSopenharmony_ci *out_len = 0; 2192a8e1175bSopenharmony_ci 2193a8e1175bSopenharmony_ci /* 2194a8e1175bSopenharmony_ci * For a pure PSK key exchange, there is no group to agree upon. The purpose 2195a8e1175bSopenharmony_ci * of the HRR is then to transmit a cookie to force the client to demonstrate 2196a8e1175bSopenharmony_ci * reachability at their apparent network address (primarily useful for DTLS). 2197a8e1175bSopenharmony_ci */ 2198a8e1175bSopenharmony_ci if (!mbedtls_ssl_tls13_key_exchange_mode_with_ephemeral(ssl)) { 2199a8e1175bSopenharmony_ci return 0; 2200a8e1175bSopenharmony_ci } 2201a8e1175bSopenharmony_ci 2202a8e1175bSopenharmony_ci /* We should only send the key_share extension if the client's initial 2203a8e1175bSopenharmony_ci * key share was not acceptable. */ 2204a8e1175bSopenharmony_ci if (ssl->handshake->offered_group_id != 0) { 2205a8e1175bSopenharmony_ci MBEDTLS_SSL_DEBUG_MSG(4, ("Skip key_share extension in HRR")); 2206a8e1175bSopenharmony_ci return 0; 2207a8e1175bSopenharmony_ci } 2208a8e1175bSopenharmony_ci 2209a8e1175bSopenharmony_ci if (selected_group == 0) { 2210a8e1175bSopenharmony_ci MBEDTLS_SSL_DEBUG_MSG(1, ("no matching named group found")); 2211a8e1175bSopenharmony_ci return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE; 2212a8e1175bSopenharmony_ci } 2213a8e1175bSopenharmony_ci 2214a8e1175bSopenharmony_ci /* Check if we have enough space: 2215a8e1175bSopenharmony_ci * - extension_type (2 bytes) 2216a8e1175bSopenharmony_ci * - extension_data_length (2 bytes) 2217a8e1175bSopenharmony_ci * - selected_group (2 bytes) 2218a8e1175bSopenharmony_ci */ 2219a8e1175bSopenharmony_ci MBEDTLS_SSL_CHK_BUF_PTR(buf, end, 6); 2220a8e1175bSopenharmony_ci 2221a8e1175bSopenharmony_ci MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_KEY_SHARE, buf, 0); 2222a8e1175bSopenharmony_ci MBEDTLS_PUT_UINT16_BE(2, buf, 2); 2223a8e1175bSopenharmony_ci MBEDTLS_PUT_UINT16_BE(selected_group, buf, 4); 2224a8e1175bSopenharmony_ci 2225a8e1175bSopenharmony_ci MBEDTLS_SSL_DEBUG_MSG(3, 2226a8e1175bSopenharmony_ci ("HRR selected_group: %s (%x)", 2227a8e1175bSopenharmony_ci mbedtls_ssl_named_group_to_str(selected_group), 2228a8e1175bSopenharmony_ci selected_group)); 2229a8e1175bSopenharmony_ci 2230a8e1175bSopenharmony_ci *out_len = 6; 2231a8e1175bSopenharmony_ci 2232a8e1175bSopenharmony_ci mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_KEY_SHARE); 2233a8e1175bSopenharmony_ci 2234a8e1175bSopenharmony_ci return 0; 2235a8e1175bSopenharmony_ci} 2236a8e1175bSopenharmony_ci 2237a8e1175bSopenharmony_ci/* 2238a8e1175bSopenharmony_ci * Structure of ServerHello message: 2239a8e1175bSopenharmony_ci * 2240a8e1175bSopenharmony_ci * struct { 2241a8e1175bSopenharmony_ci * ProtocolVersion legacy_version = 0x0303; // TLS v1.2 2242a8e1175bSopenharmony_ci * Random random; 2243a8e1175bSopenharmony_ci * opaque legacy_session_id_echo<0..32>; 2244a8e1175bSopenharmony_ci * CipherSuite cipher_suite; 2245a8e1175bSopenharmony_ci * uint8 legacy_compression_method = 0; 2246a8e1175bSopenharmony_ci * Extension extensions<6..2^16-1>; 2247a8e1175bSopenharmony_ci * } ServerHello; 2248a8e1175bSopenharmony_ci */ 2249a8e1175bSopenharmony_ciMBEDTLS_CHECK_RETURN_CRITICAL 2250a8e1175bSopenharmony_cistatic int ssl_tls13_write_server_hello_body(mbedtls_ssl_context *ssl, 2251a8e1175bSopenharmony_ci unsigned char *buf, 2252a8e1175bSopenharmony_ci unsigned char *end, 2253a8e1175bSopenharmony_ci size_t *out_len, 2254a8e1175bSopenharmony_ci int is_hrr) 2255a8e1175bSopenharmony_ci{ 2256a8e1175bSopenharmony_ci int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; 2257a8e1175bSopenharmony_ci unsigned char *p = buf; 2258a8e1175bSopenharmony_ci unsigned char *p_extensions_len; 2259a8e1175bSopenharmony_ci size_t output_len; 2260a8e1175bSopenharmony_ci 2261a8e1175bSopenharmony_ci *out_len = 0; 2262a8e1175bSopenharmony_ci ssl->handshake->sent_extensions = MBEDTLS_SSL_EXT_MASK_NONE; 2263a8e1175bSopenharmony_ci 2264a8e1175bSopenharmony_ci /* ... 2265a8e1175bSopenharmony_ci * ProtocolVersion legacy_version = 0x0303; // TLS 1.2 2266a8e1175bSopenharmony_ci * ... 2267a8e1175bSopenharmony_ci * with ProtocolVersion defined as: 2268a8e1175bSopenharmony_ci * uint16 ProtocolVersion; 2269a8e1175bSopenharmony_ci */ 2270a8e1175bSopenharmony_ci MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2); 2271a8e1175bSopenharmony_ci MBEDTLS_PUT_UINT16_BE(0x0303, p, 0); 2272a8e1175bSopenharmony_ci p += 2; 2273a8e1175bSopenharmony_ci 2274a8e1175bSopenharmony_ci /* ... 2275a8e1175bSopenharmony_ci * Random random; 2276a8e1175bSopenharmony_ci * ... 2277a8e1175bSopenharmony_ci * with Random defined as: 2278a8e1175bSopenharmony_ci * opaque Random[MBEDTLS_SERVER_HELLO_RANDOM_LEN]; 2279a8e1175bSopenharmony_ci */ 2280a8e1175bSopenharmony_ci MBEDTLS_SSL_CHK_BUF_PTR(p, end, MBEDTLS_SERVER_HELLO_RANDOM_LEN); 2281a8e1175bSopenharmony_ci if (is_hrr) { 2282a8e1175bSopenharmony_ci memcpy(p, mbedtls_ssl_tls13_hello_retry_request_magic, 2283a8e1175bSopenharmony_ci MBEDTLS_SERVER_HELLO_RANDOM_LEN); 2284a8e1175bSopenharmony_ci } else { 2285a8e1175bSopenharmony_ci memcpy(p, &ssl->handshake->randbytes[MBEDTLS_CLIENT_HELLO_RANDOM_LEN], 2286a8e1175bSopenharmony_ci MBEDTLS_SERVER_HELLO_RANDOM_LEN); 2287a8e1175bSopenharmony_ci } 2288a8e1175bSopenharmony_ci MBEDTLS_SSL_DEBUG_BUF(3, "server hello, random bytes", 2289a8e1175bSopenharmony_ci p, MBEDTLS_SERVER_HELLO_RANDOM_LEN); 2290a8e1175bSopenharmony_ci p += MBEDTLS_SERVER_HELLO_RANDOM_LEN; 2291a8e1175bSopenharmony_ci 2292a8e1175bSopenharmony_ci /* ... 2293a8e1175bSopenharmony_ci * opaque legacy_session_id_echo<0..32>; 2294a8e1175bSopenharmony_ci * ... 2295a8e1175bSopenharmony_ci */ 2296a8e1175bSopenharmony_ci MBEDTLS_SSL_CHK_BUF_PTR(p, end, 1 + ssl->session_negotiate->id_len); 2297a8e1175bSopenharmony_ci *p++ = (unsigned char) ssl->session_negotiate->id_len; 2298a8e1175bSopenharmony_ci if (ssl->session_negotiate->id_len > 0) { 2299a8e1175bSopenharmony_ci memcpy(p, &ssl->session_negotiate->id[0], 2300a8e1175bSopenharmony_ci ssl->session_negotiate->id_len); 2301a8e1175bSopenharmony_ci p += ssl->session_negotiate->id_len; 2302a8e1175bSopenharmony_ci 2303a8e1175bSopenharmony_ci MBEDTLS_SSL_DEBUG_BUF(3, "session id", ssl->session_negotiate->id, 2304a8e1175bSopenharmony_ci ssl->session_negotiate->id_len); 2305a8e1175bSopenharmony_ci } 2306a8e1175bSopenharmony_ci 2307a8e1175bSopenharmony_ci /* ... 2308a8e1175bSopenharmony_ci * CipherSuite cipher_suite; 2309a8e1175bSopenharmony_ci * ... 2310a8e1175bSopenharmony_ci * with CipherSuite defined as: 2311a8e1175bSopenharmony_ci * uint8 CipherSuite[2]; 2312a8e1175bSopenharmony_ci */ 2313a8e1175bSopenharmony_ci MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2); 2314a8e1175bSopenharmony_ci MBEDTLS_PUT_UINT16_BE(ssl->session_negotiate->ciphersuite, p, 0); 2315a8e1175bSopenharmony_ci p += 2; 2316a8e1175bSopenharmony_ci MBEDTLS_SSL_DEBUG_MSG(3, 2317a8e1175bSopenharmony_ci ("server hello, chosen ciphersuite: %s ( id=%d )", 2318a8e1175bSopenharmony_ci mbedtls_ssl_get_ciphersuite_name( 2319a8e1175bSopenharmony_ci ssl->session_negotiate->ciphersuite), 2320a8e1175bSopenharmony_ci ssl->session_negotiate->ciphersuite)); 2321a8e1175bSopenharmony_ci 2322a8e1175bSopenharmony_ci /* ... 2323a8e1175bSopenharmony_ci * uint8 legacy_compression_method = 0; 2324a8e1175bSopenharmony_ci * ... 2325a8e1175bSopenharmony_ci */ 2326a8e1175bSopenharmony_ci MBEDTLS_SSL_CHK_BUF_PTR(p, end, 1); 2327a8e1175bSopenharmony_ci *p++ = MBEDTLS_SSL_COMPRESS_NULL; 2328a8e1175bSopenharmony_ci 2329a8e1175bSopenharmony_ci /* ... 2330a8e1175bSopenharmony_ci * Extension extensions<6..2^16-1>; 2331a8e1175bSopenharmony_ci * ... 2332a8e1175bSopenharmony_ci * struct { 2333a8e1175bSopenharmony_ci * ExtensionType extension_type; (2 bytes) 2334a8e1175bSopenharmony_ci * opaque extension_data<0..2^16-1>; 2335a8e1175bSopenharmony_ci * } Extension; 2336a8e1175bSopenharmony_ci */ 2337a8e1175bSopenharmony_ci MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2); 2338a8e1175bSopenharmony_ci p_extensions_len = p; 2339a8e1175bSopenharmony_ci p += 2; 2340a8e1175bSopenharmony_ci 2341a8e1175bSopenharmony_ci if ((ret = ssl_tls13_write_server_hello_supported_versions_ext( 2342a8e1175bSopenharmony_ci ssl, p, end, &output_len)) != 0) { 2343a8e1175bSopenharmony_ci MBEDTLS_SSL_DEBUG_RET( 2344a8e1175bSopenharmony_ci 1, "ssl_tls13_write_server_hello_supported_versions_ext", ret); 2345a8e1175bSopenharmony_ci return ret; 2346a8e1175bSopenharmony_ci } 2347a8e1175bSopenharmony_ci p += output_len; 2348a8e1175bSopenharmony_ci 2349a8e1175bSopenharmony_ci if (mbedtls_ssl_tls13_key_exchange_mode_with_ephemeral(ssl)) { 2350a8e1175bSopenharmony_ci if (is_hrr) { 2351a8e1175bSopenharmony_ci ret = ssl_tls13_write_hrr_key_share_ext(ssl, p, end, &output_len); 2352a8e1175bSopenharmony_ci } else { 2353a8e1175bSopenharmony_ci ret = ssl_tls13_write_key_share_ext(ssl, p, end, &output_len); 2354a8e1175bSopenharmony_ci } 2355a8e1175bSopenharmony_ci if (ret != 0) { 2356a8e1175bSopenharmony_ci return ret; 2357a8e1175bSopenharmony_ci } 2358a8e1175bSopenharmony_ci p += output_len; 2359a8e1175bSopenharmony_ci } 2360a8e1175bSopenharmony_ci 2361a8e1175bSopenharmony_ci#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED) 2362a8e1175bSopenharmony_ci if (!is_hrr && mbedtls_ssl_tls13_key_exchange_mode_with_psk(ssl)) { 2363a8e1175bSopenharmony_ci ret = ssl_tls13_write_server_pre_shared_key_ext(ssl, p, end, &output_len); 2364a8e1175bSopenharmony_ci if (ret != 0) { 2365a8e1175bSopenharmony_ci MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_write_server_pre_shared_key_ext", 2366a8e1175bSopenharmony_ci ret); 2367a8e1175bSopenharmony_ci return ret; 2368a8e1175bSopenharmony_ci } 2369a8e1175bSopenharmony_ci p += output_len; 2370a8e1175bSopenharmony_ci } 2371a8e1175bSopenharmony_ci#endif 2372a8e1175bSopenharmony_ci 2373a8e1175bSopenharmony_ci MBEDTLS_PUT_UINT16_BE(p - p_extensions_len - 2, p_extensions_len, 0); 2374a8e1175bSopenharmony_ci 2375a8e1175bSopenharmony_ci MBEDTLS_SSL_DEBUG_BUF(4, "server hello extensions", 2376a8e1175bSopenharmony_ci p_extensions_len, p - p_extensions_len); 2377a8e1175bSopenharmony_ci 2378a8e1175bSopenharmony_ci *out_len = p - buf; 2379a8e1175bSopenharmony_ci 2380a8e1175bSopenharmony_ci MBEDTLS_SSL_DEBUG_BUF(3, "server hello", buf, *out_len); 2381a8e1175bSopenharmony_ci 2382a8e1175bSopenharmony_ci MBEDTLS_SSL_PRINT_EXTS( 2383a8e1175bSopenharmony_ci 3, is_hrr ? MBEDTLS_SSL_TLS1_3_HS_HELLO_RETRY_REQUEST : 2384a8e1175bSopenharmony_ci MBEDTLS_SSL_HS_SERVER_HELLO, 2385a8e1175bSopenharmony_ci ssl->handshake->sent_extensions); 2386a8e1175bSopenharmony_ci 2387a8e1175bSopenharmony_ci return ret; 2388a8e1175bSopenharmony_ci} 2389a8e1175bSopenharmony_ci 2390a8e1175bSopenharmony_ciMBEDTLS_CHECK_RETURN_CRITICAL 2391a8e1175bSopenharmony_cistatic int ssl_tls13_finalize_server_hello(mbedtls_ssl_context *ssl) 2392a8e1175bSopenharmony_ci{ 2393a8e1175bSopenharmony_ci int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; 2394a8e1175bSopenharmony_ci ret = mbedtls_ssl_tls13_compute_handshake_transform(ssl); 2395a8e1175bSopenharmony_ci if (ret != 0) { 2396a8e1175bSopenharmony_ci MBEDTLS_SSL_DEBUG_RET(1, 2397a8e1175bSopenharmony_ci "mbedtls_ssl_tls13_compute_handshake_transform", 2398a8e1175bSopenharmony_ci ret); 2399a8e1175bSopenharmony_ci return ret; 2400a8e1175bSopenharmony_ci } 2401a8e1175bSopenharmony_ci 2402a8e1175bSopenharmony_ci return ret; 2403a8e1175bSopenharmony_ci} 2404a8e1175bSopenharmony_ci 2405a8e1175bSopenharmony_ciMBEDTLS_CHECK_RETURN_CRITICAL 2406a8e1175bSopenharmony_cistatic int ssl_tls13_write_server_hello(mbedtls_ssl_context *ssl) 2407a8e1175bSopenharmony_ci{ 2408a8e1175bSopenharmony_ci int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; 2409a8e1175bSopenharmony_ci unsigned char *buf; 2410a8e1175bSopenharmony_ci size_t buf_len, msg_len; 2411a8e1175bSopenharmony_ci 2412a8e1175bSopenharmony_ci MBEDTLS_SSL_DEBUG_MSG(2, ("=> write server hello")); 2413a8e1175bSopenharmony_ci 2414a8e1175bSopenharmony_ci MBEDTLS_SSL_PROC_CHK(ssl_tls13_prepare_server_hello(ssl)); 2415a8e1175bSopenharmony_ci 2416a8e1175bSopenharmony_ci MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg( 2417a8e1175bSopenharmony_ci ssl, MBEDTLS_SSL_HS_SERVER_HELLO, &buf, &buf_len)); 2418a8e1175bSopenharmony_ci 2419a8e1175bSopenharmony_ci MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_server_hello_body(ssl, buf, 2420a8e1175bSopenharmony_ci buf + buf_len, 2421a8e1175bSopenharmony_ci &msg_len, 2422a8e1175bSopenharmony_ci 0)); 2423a8e1175bSopenharmony_ci 2424a8e1175bSopenharmony_ci MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum( 2425a8e1175bSopenharmony_ci ssl, MBEDTLS_SSL_HS_SERVER_HELLO, buf, msg_len)); 2426a8e1175bSopenharmony_ci 2427a8e1175bSopenharmony_ci MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg( 2428a8e1175bSopenharmony_ci ssl, buf_len, msg_len)); 2429a8e1175bSopenharmony_ci 2430a8e1175bSopenharmony_ci MBEDTLS_SSL_PROC_CHK(ssl_tls13_finalize_server_hello(ssl)); 2431a8e1175bSopenharmony_ci 2432a8e1175bSopenharmony_ci#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE) 2433a8e1175bSopenharmony_ci /* The server sends a dummy change_cipher_spec record immediately 2434a8e1175bSopenharmony_ci * after its first handshake message. This may either be after 2435a8e1175bSopenharmony_ci * a ServerHello or a HelloRetryRequest. 2436a8e1175bSopenharmony_ci */ 2437a8e1175bSopenharmony_ci mbedtls_ssl_handshake_set_state( 2438a8e1175bSopenharmony_ci ssl, MBEDTLS_SSL_SERVER_CCS_AFTER_SERVER_HELLO); 2439a8e1175bSopenharmony_ci#else 2440a8e1175bSopenharmony_ci mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_ENCRYPTED_EXTENSIONS); 2441a8e1175bSopenharmony_ci#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */ 2442a8e1175bSopenharmony_ci 2443a8e1175bSopenharmony_cicleanup: 2444a8e1175bSopenharmony_ci 2445a8e1175bSopenharmony_ci MBEDTLS_SSL_DEBUG_MSG(2, ("<= write server hello")); 2446a8e1175bSopenharmony_ci return ret; 2447a8e1175bSopenharmony_ci} 2448a8e1175bSopenharmony_ci 2449a8e1175bSopenharmony_ci 2450a8e1175bSopenharmony_ci/* 2451a8e1175bSopenharmony_ci * Handler for MBEDTLS_SSL_HELLO_RETRY_REQUEST 2452a8e1175bSopenharmony_ci */ 2453a8e1175bSopenharmony_ciMBEDTLS_CHECK_RETURN_CRITICAL 2454a8e1175bSopenharmony_cistatic int ssl_tls13_prepare_hello_retry_request(mbedtls_ssl_context *ssl) 2455a8e1175bSopenharmony_ci{ 2456a8e1175bSopenharmony_ci int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; 2457a8e1175bSopenharmony_ci if (ssl->handshake->hello_retry_request_flag) { 2458a8e1175bSopenharmony_ci MBEDTLS_SSL_DEBUG_MSG(1, ("Too many HRRs")); 2459a8e1175bSopenharmony_ci MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE, 2460a8e1175bSopenharmony_ci MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE); 2461a8e1175bSopenharmony_ci return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE; 2462a8e1175bSopenharmony_ci } 2463a8e1175bSopenharmony_ci 2464a8e1175bSopenharmony_ci /* 2465a8e1175bSopenharmony_ci * Create stateless transcript hash for HRR 2466a8e1175bSopenharmony_ci */ 2467a8e1175bSopenharmony_ci MBEDTLS_SSL_DEBUG_MSG(4, ("Reset transcript for HRR")); 2468a8e1175bSopenharmony_ci ret = mbedtls_ssl_reset_transcript_for_hrr(ssl); 2469a8e1175bSopenharmony_ci if (ret != 0) { 2470a8e1175bSopenharmony_ci MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_reset_transcript_for_hrr", ret); 2471a8e1175bSopenharmony_ci return ret; 2472a8e1175bSopenharmony_ci } 2473a8e1175bSopenharmony_ci mbedtls_ssl_session_reset_msg_layer(ssl, 0); 2474a8e1175bSopenharmony_ci 2475a8e1175bSopenharmony_ci return 0; 2476a8e1175bSopenharmony_ci} 2477a8e1175bSopenharmony_ci 2478a8e1175bSopenharmony_ciMBEDTLS_CHECK_RETURN_CRITICAL 2479a8e1175bSopenharmony_cistatic int ssl_tls13_write_hello_retry_request(mbedtls_ssl_context *ssl) 2480a8e1175bSopenharmony_ci{ 2481a8e1175bSopenharmony_ci int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; 2482a8e1175bSopenharmony_ci unsigned char *buf; 2483a8e1175bSopenharmony_ci size_t buf_len, msg_len; 2484a8e1175bSopenharmony_ci 2485a8e1175bSopenharmony_ci MBEDTLS_SSL_DEBUG_MSG(2, ("=> write hello retry request")); 2486a8e1175bSopenharmony_ci 2487a8e1175bSopenharmony_ci MBEDTLS_SSL_PROC_CHK(ssl_tls13_prepare_hello_retry_request(ssl)); 2488a8e1175bSopenharmony_ci 2489a8e1175bSopenharmony_ci MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg( 2490a8e1175bSopenharmony_ci ssl, MBEDTLS_SSL_HS_SERVER_HELLO, 2491a8e1175bSopenharmony_ci &buf, &buf_len)); 2492a8e1175bSopenharmony_ci 2493a8e1175bSopenharmony_ci MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_server_hello_body(ssl, buf, 2494a8e1175bSopenharmony_ci buf + buf_len, 2495a8e1175bSopenharmony_ci &msg_len, 2496a8e1175bSopenharmony_ci 1)); 2497a8e1175bSopenharmony_ci MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum( 2498a8e1175bSopenharmony_ci ssl, MBEDTLS_SSL_HS_SERVER_HELLO, buf, msg_len)); 2499a8e1175bSopenharmony_ci 2500a8e1175bSopenharmony_ci 2501a8e1175bSopenharmony_ci MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(ssl, buf_len, 2502a8e1175bSopenharmony_ci msg_len)); 2503a8e1175bSopenharmony_ci 2504a8e1175bSopenharmony_ci ssl->handshake->hello_retry_request_flag = 1; 2505a8e1175bSopenharmony_ci 2506a8e1175bSopenharmony_ci#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE) 2507a8e1175bSopenharmony_ci /* The server sends a dummy change_cipher_spec record immediately 2508a8e1175bSopenharmony_ci * after its first handshake message. This may either be after 2509a8e1175bSopenharmony_ci * a ServerHello or a HelloRetryRequest. 2510a8e1175bSopenharmony_ci */ 2511a8e1175bSopenharmony_ci mbedtls_ssl_handshake_set_state( 2512a8e1175bSopenharmony_ci ssl, MBEDTLS_SSL_SERVER_CCS_AFTER_HELLO_RETRY_REQUEST); 2513a8e1175bSopenharmony_ci#else 2514a8e1175bSopenharmony_ci mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_HELLO); 2515a8e1175bSopenharmony_ci#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */ 2516a8e1175bSopenharmony_ci 2517a8e1175bSopenharmony_cicleanup: 2518a8e1175bSopenharmony_ci MBEDTLS_SSL_DEBUG_MSG(2, ("<= write hello retry request")); 2519a8e1175bSopenharmony_ci return ret; 2520a8e1175bSopenharmony_ci} 2521a8e1175bSopenharmony_ci 2522a8e1175bSopenharmony_ci/* 2523a8e1175bSopenharmony_ci * Handler for MBEDTLS_SSL_ENCRYPTED_EXTENSIONS 2524a8e1175bSopenharmony_ci */ 2525a8e1175bSopenharmony_ci 2526a8e1175bSopenharmony_ci/* 2527a8e1175bSopenharmony_ci * struct { 2528a8e1175bSopenharmony_ci * Extension extensions<0..2 ^ 16 - 1>; 2529a8e1175bSopenharmony_ci * } EncryptedExtensions; 2530a8e1175bSopenharmony_ci * 2531a8e1175bSopenharmony_ci */ 2532a8e1175bSopenharmony_ciMBEDTLS_CHECK_RETURN_CRITICAL 2533a8e1175bSopenharmony_cistatic int ssl_tls13_write_encrypted_extensions_body(mbedtls_ssl_context *ssl, 2534a8e1175bSopenharmony_ci unsigned char *buf, 2535a8e1175bSopenharmony_ci unsigned char *end, 2536a8e1175bSopenharmony_ci size_t *out_len) 2537a8e1175bSopenharmony_ci{ 2538a8e1175bSopenharmony_ci int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; 2539a8e1175bSopenharmony_ci unsigned char *p = buf; 2540a8e1175bSopenharmony_ci size_t extensions_len = 0; 2541a8e1175bSopenharmony_ci unsigned char *p_extensions_len; 2542a8e1175bSopenharmony_ci size_t output_len; 2543a8e1175bSopenharmony_ci 2544a8e1175bSopenharmony_ci *out_len = 0; 2545a8e1175bSopenharmony_ci 2546a8e1175bSopenharmony_ci MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2); 2547a8e1175bSopenharmony_ci p_extensions_len = p; 2548a8e1175bSopenharmony_ci p += 2; 2549a8e1175bSopenharmony_ci 2550a8e1175bSopenharmony_ci ((void) ssl); 2551a8e1175bSopenharmony_ci ((void) ret); 2552a8e1175bSopenharmony_ci ((void) output_len); 2553a8e1175bSopenharmony_ci 2554a8e1175bSopenharmony_ci#if defined(MBEDTLS_SSL_ALPN) 2555a8e1175bSopenharmony_ci ret = mbedtls_ssl_write_alpn_ext(ssl, p, end, &output_len); 2556a8e1175bSopenharmony_ci if (ret != 0) { 2557a8e1175bSopenharmony_ci return ret; 2558a8e1175bSopenharmony_ci } 2559a8e1175bSopenharmony_ci p += output_len; 2560a8e1175bSopenharmony_ci#endif /* MBEDTLS_SSL_ALPN */ 2561a8e1175bSopenharmony_ci 2562a8e1175bSopenharmony_ci#if defined(MBEDTLS_SSL_EARLY_DATA) 2563a8e1175bSopenharmony_ci if (ssl->handshake->early_data_accepted) { 2564a8e1175bSopenharmony_ci ret = mbedtls_ssl_tls13_write_early_data_ext( 2565a8e1175bSopenharmony_ci ssl, 0, p, end, &output_len); 2566a8e1175bSopenharmony_ci if (ret != 0) { 2567a8e1175bSopenharmony_ci return ret; 2568a8e1175bSopenharmony_ci } 2569a8e1175bSopenharmony_ci p += output_len; 2570a8e1175bSopenharmony_ci } 2571a8e1175bSopenharmony_ci#endif /* MBEDTLS_SSL_EARLY_DATA */ 2572a8e1175bSopenharmony_ci 2573a8e1175bSopenharmony_ci#if defined(MBEDTLS_SSL_RECORD_SIZE_LIMIT) 2574a8e1175bSopenharmony_ci if (ssl->handshake->received_extensions & MBEDTLS_SSL_EXT_MASK(RECORD_SIZE_LIMIT)) { 2575a8e1175bSopenharmony_ci ret = mbedtls_ssl_tls13_write_record_size_limit_ext( 2576a8e1175bSopenharmony_ci ssl, p, end, &output_len); 2577a8e1175bSopenharmony_ci if (ret != 0) { 2578a8e1175bSopenharmony_ci return ret; 2579a8e1175bSopenharmony_ci } 2580a8e1175bSopenharmony_ci p += output_len; 2581a8e1175bSopenharmony_ci } 2582a8e1175bSopenharmony_ci#endif 2583a8e1175bSopenharmony_ci 2584a8e1175bSopenharmony_ci extensions_len = (p - p_extensions_len) - 2; 2585a8e1175bSopenharmony_ci MBEDTLS_PUT_UINT16_BE(extensions_len, p_extensions_len, 0); 2586a8e1175bSopenharmony_ci 2587a8e1175bSopenharmony_ci *out_len = p - buf; 2588a8e1175bSopenharmony_ci 2589a8e1175bSopenharmony_ci MBEDTLS_SSL_DEBUG_BUF(4, "encrypted extensions", buf, *out_len); 2590a8e1175bSopenharmony_ci 2591a8e1175bSopenharmony_ci MBEDTLS_SSL_PRINT_EXTS( 2592a8e1175bSopenharmony_ci 3, MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS, ssl->handshake->sent_extensions); 2593a8e1175bSopenharmony_ci 2594a8e1175bSopenharmony_ci return 0; 2595a8e1175bSopenharmony_ci} 2596a8e1175bSopenharmony_ci 2597a8e1175bSopenharmony_ciMBEDTLS_CHECK_RETURN_CRITICAL 2598a8e1175bSopenharmony_cistatic int ssl_tls13_write_encrypted_extensions(mbedtls_ssl_context *ssl) 2599a8e1175bSopenharmony_ci{ 2600a8e1175bSopenharmony_ci int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; 2601a8e1175bSopenharmony_ci unsigned char *buf; 2602a8e1175bSopenharmony_ci size_t buf_len, msg_len; 2603a8e1175bSopenharmony_ci 2604a8e1175bSopenharmony_ci mbedtls_ssl_set_outbound_transform(ssl, 2605a8e1175bSopenharmony_ci ssl->handshake->transform_handshake); 2606a8e1175bSopenharmony_ci MBEDTLS_SSL_DEBUG_MSG( 2607a8e1175bSopenharmony_ci 3, ("switching to handshake transform for outbound data")); 2608a8e1175bSopenharmony_ci 2609a8e1175bSopenharmony_ci MBEDTLS_SSL_DEBUG_MSG(2, ("=> write encrypted extensions")); 2610a8e1175bSopenharmony_ci 2611a8e1175bSopenharmony_ci MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg( 2612a8e1175bSopenharmony_ci ssl, MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS, 2613a8e1175bSopenharmony_ci &buf, &buf_len)); 2614a8e1175bSopenharmony_ci 2615a8e1175bSopenharmony_ci MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_encrypted_extensions_body( 2616a8e1175bSopenharmony_ci ssl, buf, buf + buf_len, &msg_len)); 2617a8e1175bSopenharmony_ci 2618a8e1175bSopenharmony_ci MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum( 2619a8e1175bSopenharmony_ci ssl, MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS, 2620a8e1175bSopenharmony_ci buf, msg_len)); 2621a8e1175bSopenharmony_ci 2622a8e1175bSopenharmony_ci MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg( 2623a8e1175bSopenharmony_ci ssl, buf_len, msg_len)); 2624a8e1175bSopenharmony_ci 2625a8e1175bSopenharmony_ci#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED) 2626a8e1175bSopenharmony_ci if (mbedtls_ssl_tls13_key_exchange_mode_with_psk(ssl)) { 2627a8e1175bSopenharmony_ci mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_FINISHED); 2628a8e1175bSopenharmony_ci } else { 2629a8e1175bSopenharmony_ci mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CERTIFICATE_REQUEST); 2630a8e1175bSopenharmony_ci } 2631a8e1175bSopenharmony_ci#else 2632a8e1175bSopenharmony_ci mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_FINISHED); 2633a8e1175bSopenharmony_ci#endif 2634a8e1175bSopenharmony_ci 2635a8e1175bSopenharmony_cicleanup: 2636a8e1175bSopenharmony_ci 2637a8e1175bSopenharmony_ci MBEDTLS_SSL_DEBUG_MSG(2, ("<= write encrypted extensions")); 2638a8e1175bSopenharmony_ci return ret; 2639a8e1175bSopenharmony_ci} 2640a8e1175bSopenharmony_ci 2641a8e1175bSopenharmony_ci#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED) 2642a8e1175bSopenharmony_ci#define SSL_CERTIFICATE_REQUEST_SEND_REQUEST 0 2643a8e1175bSopenharmony_ci#define SSL_CERTIFICATE_REQUEST_SKIP 1 2644a8e1175bSopenharmony_ci/* Coordination: 2645a8e1175bSopenharmony_ci * Check whether a CertificateRequest message should be written. 2646a8e1175bSopenharmony_ci * Returns a negative code on failure, or 2647a8e1175bSopenharmony_ci * - SSL_CERTIFICATE_REQUEST_SEND_REQUEST 2648a8e1175bSopenharmony_ci * - SSL_CERTIFICATE_REQUEST_SKIP 2649a8e1175bSopenharmony_ci * indicating if the writing of the CertificateRequest 2650a8e1175bSopenharmony_ci * should be skipped or not. 2651a8e1175bSopenharmony_ci */ 2652a8e1175bSopenharmony_ciMBEDTLS_CHECK_RETURN_CRITICAL 2653a8e1175bSopenharmony_cistatic int ssl_tls13_certificate_request_coordinate(mbedtls_ssl_context *ssl) 2654a8e1175bSopenharmony_ci{ 2655a8e1175bSopenharmony_ci int authmode; 2656a8e1175bSopenharmony_ci 2657a8e1175bSopenharmony_ci#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) 2658a8e1175bSopenharmony_ci if (ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET) { 2659a8e1175bSopenharmony_ci authmode = ssl->handshake->sni_authmode; 2660a8e1175bSopenharmony_ci } else 2661a8e1175bSopenharmony_ci#endif 2662a8e1175bSopenharmony_ci authmode = ssl->conf->authmode; 2663a8e1175bSopenharmony_ci 2664a8e1175bSopenharmony_ci if (authmode == MBEDTLS_SSL_VERIFY_NONE) { 2665a8e1175bSopenharmony_ci ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_SKIP_VERIFY; 2666a8e1175bSopenharmony_ci return SSL_CERTIFICATE_REQUEST_SKIP; 2667a8e1175bSopenharmony_ci } 2668a8e1175bSopenharmony_ci 2669a8e1175bSopenharmony_ci ssl->handshake->certificate_request_sent = 1; 2670a8e1175bSopenharmony_ci 2671a8e1175bSopenharmony_ci return SSL_CERTIFICATE_REQUEST_SEND_REQUEST; 2672a8e1175bSopenharmony_ci} 2673a8e1175bSopenharmony_ci 2674a8e1175bSopenharmony_ci/* 2675a8e1175bSopenharmony_ci * struct { 2676a8e1175bSopenharmony_ci * opaque certificate_request_context<0..2^8-1>; 2677a8e1175bSopenharmony_ci * Extension extensions<2..2^16-1>; 2678a8e1175bSopenharmony_ci * } CertificateRequest; 2679a8e1175bSopenharmony_ci * 2680a8e1175bSopenharmony_ci */ 2681a8e1175bSopenharmony_ciMBEDTLS_CHECK_RETURN_CRITICAL 2682a8e1175bSopenharmony_cistatic int ssl_tls13_write_certificate_request_body(mbedtls_ssl_context *ssl, 2683a8e1175bSopenharmony_ci unsigned char *buf, 2684a8e1175bSopenharmony_ci const unsigned char *end, 2685a8e1175bSopenharmony_ci size_t *out_len) 2686a8e1175bSopenharmony_ci{ 2687a8e1175bSopenharmony_ci int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; 2688a8e1175bSopenharmony_ci unsigned char *p = buf; 2689a8e1175bSopenharmony_ci size_t output_len = 0; 2690a8e1175bSopenharmony_ci unsigned char *p_extensions_len; 2691a8e1175bSopenharmony_ci 2692a8e1175bSopenharmony_ci *out_len = 0; 2693a8e1175bSopenharmony_ci 2694a8e1175bSopenharmony_ci /* Check if we have enough space: 2695a8e1175bSopenharmony_ci * - certificate_request_context (1 byte) 2696a8e1175bSopenharmony_ci * - extensions length (2 bytes) 2697a8e1175bSopenharmony_ci */ 2698a8e1175bSopenharmony_ci MBEDTLS_SSL_CHK_BUF_PTR(p, end, 3); 2699a8e1175bSopenharmony_ci 2700a8e1175bSopenharmony_ci /* 2701a8e1175bSopenharmony_ci * Write certificate_request_context 2702a8e1175bSopenharmony_ci */ 2703a8e1175bSopenharmony_ci /* 2704a8e1175bSopenharmony_ci * We use a zero length context for the normal handshake 2705a8e1175bSopenharmony_ci * messages. For post-authentication handshake messages 2706a8e1175bSopenharmony_ci * this request context would be set to a non-zero value. 2707a8e1175bSopenharmony_ci */ 2708a8e1175bSopenharmony_ci *p++ = 0x0; 2709a8e1175bSopenharmony_ci 2710a8e1175bSopenharmony_ci /* 2711a8e1175bSopenharmony_ci * Write extensions 2712a8e1175bSopenharmony_ci */ 2713a8e1175bSopenharmony_ci /* The extensions must contain the signature_algorithms. */ 2714a8e1175bSopenharmony_ci p_extensions_len = p; 2715a8e1175bSopenharmony_ci p += 2; 2716a8e1175bSopenharmony_ci ret = mbedtls_ssl_write_sig_alg_ext(ssl, p, end, &output_len); 2717a8e1175bSopenharmony_ci if (ret != 0) { 2718a8e1175bSopenharmony_ci return ret; 2719a8e1175bSopenharmony_ci } 2720a8e1175bSopenharmony_ci 2721a8e1175bSopenharmony_ci p += output_len; 2722a8e1175bSopenharmony_ci MBEDTLS_PUT_UINT16_BE(p - p_extensions_len - 2, p_extensions_len, 0); 2723a8e1175bSopenharmony_ci 2724a8e1175bSopenharmony_ci *out_len = p - buf; 2725a8e1175bSopenharmony_ci 2726a8e1175bSopenharmony_ci MBEDTLS_SSL_PRINT_EXTS( 2727a8e1175bSopenharmony_ci 3, MBEDTLS_SSL_HS_CERTIFICATE_REQUEST, ssl->handshake->sent_extensions); 2728a8e1175bSopenharmony_ci 2729a8e1175bSopenharmony_ci return 0; 2730a8e1175bSopenharmony_ci} 2731a8e1175bSopenharmony_ci 2732a8e1175bSopenharmony_ciMBEDTLS_CHECK_RETURN_CRITICAL 2733a8e1175bSopenharmony_cistatic int ssl_tls13_write_certificate_request(mbedtls_ssl_context *ssl) 2734a8e1175bSopenharmony_ci{ 2735a8e1175bSopenharmony_ci int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; 2736a8e1175bSopenharmony_ci 2737a8e1175bSopenharmony_ci MBEDTLS_SSL_DEBUG_MSG(2, ("=> write certificate request")); 2738a8e1175bSopenharmony_ci 2739a8e1175bSopenharmony_ci MBEDTLS_SSL_PROC_CHK_NEG(ssl_tls13_certificate_request_coordinate(ssl)); 2740a8e1175bSopenharmony_ci 2741a8e1175bSopenharmony_ci if (ret == SSL_CERTIFICATE_REQUEST_SEND_REQUEST) { 2742a8e1175bSopenharmony_ci unsigned char *buf; 2743a8e1175bSopenharmony_ci size_t buf_len, msg_len; 2744a8e1175bSopenharmony_ci 2745a8e1175bSopenharmony_ci MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg( 2746a8e1175bSopenharmony_ci ssl, MBEDTLS_SSL_HS_CERTIFICATE_REQUEST, 2747a8e1175bSopenharmony_ci &buf, &buf_len)); 2748a8e1175bSopenharmony_ci 2749a8e1175bSopenharmony_ci MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_certificate_request_body( 2750a8e1175bSopenharmony_ci ssl, buf, buf + buf_len, &msg_len)); 2751a8e1175bSopenharmony_ci 2752a8e1175bSopenharmony_ci MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum( 2753a8e1175bSopenharmony_ci ssl, MBEDTLS_SSL_HS_CERTIFICATE_REQUEST, 2754a8e1175bSopenharmony_ci buf, msg_len)); 2755a8e1175bSopenharmony_ci 2756a8e1175bSopenharmony_ci MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg( 2757a8e1175bSopenharmony_ci ssl, buf_len, msg_len)); 2758a8e1175bSopenharmony_ci } else if (ret == SSL_CERTIFICATE_REQUEST_SKIP) { 2759a8e1175bSopenharmony_ci MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate request")); 2760a8e1175bSopenharmony_ci ret = 0; 2761a8e1175bSopenharmony_ci } else { 2762a8e1175bSopenharmony_ci MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen")); 2763a8e1175bSopenharmony_ci ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR; 2764a8e1175bSopenharmony_ci goto cleanup; 2765a8e1175bSopenharmony_ci } 2766a8e1175bSopenharmony_ci 2767a8e1175bSopenharmony_ci mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_CERTIFICATE); 2768a8e1175bSopenharmony_cicleanup: 2769a8e1175bSopenharmony_ci 2770a8e1175bSopenharmony_ci MBEDTLS_SSL_DEBUG_MSG(2, ("<= write certificate request")); 2771a8e1175bSopenharmony_ci return ret; 2772a8e1175bSopenharmony_ci} 2773a8e1175bSopenharmony_ci 2774a8e1175bSopenharmony_ci/* 2775a8e1175bSopenharmony_ci * Handler for MBEDTLS_SSL_SERVER_CERTIFICATE 2776a8e1175bSopenharmony_ci */ 2777a8e1175bSopenharmony_ciMBEDTLS_CHECK_RETURN_CRITICAL 2778a8e1175bSopenharmony_cistatic int ssl_tls13_write_server_certificate(mbedtls_ssl_context *ssl) 2779a8e1175bSopenharmony_ci{ 2780a8e1175bSopenharmony_ci int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; 2781a8e1175bSopenharmony_ci 2782a8e1175bSopenharmony_ci#if defined(MBEDTLS_X509_CRT_PARSE_C) 2783a8e1175bSopenharmony_ci if ((ssl_tls13_pick_key_cert(ssl) != 0) || 2784a8e1175bSopenharmony_ci mbedtls_ssl_own_cert(ssl) == NULL) { 2785a8e1175bSopenharmony_ci MBEDTLS_SSL_DEBUG_MSG(2, ("No certificate available.")); 2786a8e1175bSopenharmony_ci MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE, 2787a8e1175bSopenharmony_ci MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE); 2788a8e1175bSopenharmony_ci return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE; 2789a8e1175bSopenharmony_ci } 2790a8e1175bSopenharmony_ci#endif /* MBEDTLS_X509_CRT_PARSE_C */ 2791a8e1175bSopenharmony_ci 2792a8e1175bSopenharmony_ci ret = mbedtls_ssl_tls13_write_certificate(ssl); 2793a8e1175bSopenharmony_ci if (ret != 0) { 2794a8e1175bSopenharmony_ci return ret; 2795a8e1175bSopenharmony_ci } 2796a8e1175bSopenharmony_ci mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CERTIFICATE_VERIFY); 2797a8e1175bSopenharmony_ci return 0; 2798a8e1175bSopenharmony_ci} 2799a8e1175bSopenharmony_ci 2800a8e1175bSopenharmony_ci/* 2801a8e1175bSopenharmony_ci * Handler for MBEDTLS_SSL_CERTIFICATE_VERIFY 2802a8e1175bSopenharmony_ci */ 2803a8e1175bSopenharmony_ciMBEDTLS_CHECK_RETURN_CRITICAL 2804a8e1175bSopenharmony_cistatic int ssl_tls13_write_certificate_verify(mbedtls_ssl_context *ssl) 2805a8e1175bSopenharmony_ci{ 2806a8e1175bSopenharmony_ci int ret = mbedtls_ssl_tls13_write_certificate_verify(ssl); 2807a8e1175bSopenharmony_ci if (ret != 0) { 2808a8e1175bSopenharmony_ci return ret; 2809a8e1175bSopenharmony_ci } 2810a8e1175bSopenharmony_ci mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_FINISHED); 2811a8e1175bSopenharmony_ci return 0; 2812a8e1175bSopenharmony_ci} 2813a8e1175bSopenharmony_ci#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */ 2814a8e1175bSopenharmony_ci 2815a8e1175bSopenharmony_ci/* 2816a8e1175bSopenharmony_ci * RFC 8446 section A.2 2817a8e1175bSopenharmony_ci * 2818a8e1175bSopenharmony_ci * | Send ServerHello 2819a8e1175bSopenharmony_ci * | K_send = handshake 2820a8e1175bSopenharmony_ci * | Send EncryptedExtensions 2821a8e1175bSopenharmony_ci * | [Send CertificateRequest] 2822a8e1175bSopenharmony_ci * Can send | [Send Certificate + CertificateVerify] 2823a8e1175bSopenharmony_ci * app data | Send Finished 2824a8e1175bSopenharmony_ci * after --> | K_send = application 2825a8e1175bSopenharmony_ci * here +--------+--------+ 2826a8e1175bSopenharmony_ci * No 0-RTT | | 0-RTT 2827a8e1175bSopenharmony_ci * | | 2828a8e1175bSopenharmony_ci * K_recv = handshake | | K_recv = early data 2829a8e1175bSopenharmony_ci * [Skip decrypt errors] | +------> WAIT_EOED -+ 2830a8e1175bSopenharmony_ci * | | Recv | | Recv EndOfEarlyData 2831a8e1175bSopenharmony_ci * | | early data | | K_recv = handshake 2832a8e1175bSopenharmony_ci * | +------------+ | 2833a8e1175bSopenharmony_ci * | | 2834a8e1175bSopenharmony_ci * +> WAIT_FLIGHT2 <--------+ 2835a8e1175bSopenharmony_ci * | 2836a8e1175bSopenharmony_ci * +--------+--------+ 2837a8e1175bSopenharmony_ci * No auth | | Client auth 2838a8e1175bSopenharmony_ci * | | 2839a8e1175bSopenharmony_ci * | v 2840a8e1175bSopenharmony_ci * | WAIT_CERT 2841a8e1175bSopenharmony_ci * | Recv | | Recv Certificate 2842a8e1175bSopenharmony_ci * | empty | v 2843a8e1175bSopenharmony_ci * | Certificate | WAIT_CV 2844a8e1175bSopenharmony_ci * | | | Recv 2845a8e1175bSopenharmony_ci * | v | CertificateVerify 2846a8e1175bSopenharmony_ci * +-> WAIT_FINISHED <---+ 2847a8e1175bSopenharmony_ci * | Recv Finished 2848a8e1175bSopenharmony_ci * 2849a8e1175bSopenharmony_ci * 2850a8e1175bSopenharmony_ci * The following function handles the state changes after WAIT_FLIGHT2 in the 2851a8e1175bSopenharmony_ci * above diagram. We are not going to receive early data related messages 2852a8e1175bSopenharmony_ci * anymore, prepare to receive the first handshake message of the client 2853a8e1175bSopenharmony_ci * second flight. 2854a8e1175bSopenharmony_ci */ 2855a8e1175bSopenharmony_cistatic void ssl_tls13_prepare_for_handshake_second_flight( 2856a8e1175bSopenharmony_ci mbedtls_ssl_context *ssl) 2857a8e1175bSopenharmony_ci{ 2858a8e1175bSopenharmony_ci if (ssl->handshake->certificate_request_sent) { 2859a8e1175bSopenharmony_ci mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_CERTIFICATE); 2860a8e1175bSopenharmony_ci } else { 2861a8e1175bSopenharmony_ci MBEDTLS_SSL_DEBUG_MSG(2, ("skip parse certificate")); 2862a8e1175bSopenharmony_ci MBEDTLS_SSL_DEBUG_MSG(2, ("skip parse certificate verify")); 2863a8e1175bSopenharmony_ci 2864a8e1175bSopenharmony_ci mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_FINISHED); 2865a8e1175bSopenharmony_ci } 2866a8e1175bSopenharmony_ci} 2867a8e1175bSopenharmony_ci 2868a8e1175bSopenharmony_ci/* 2869a8e1175bSopenharmony_ci * Handler for MBEDTLS_SSL_SERVER_FINISHED 2870a8e1175bSopenharmony_ci */ 2871a8e1175bSopenharmony_ciMBEDTLS_CHECK_RETURN_CRITICAL 2872a8e1175bSopenharmony_cistatic int ssl_tls13_write_server_finished(mbedtls_ssl_context *ssl) 2873a8e1175bSopenharmony_ci{ 2874a8e1175bSopenharmony_ci int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; 2875a8e1175bSopenharmony_ci 2876a8e1175bSopenharmony_ci ret = mbedtls_ssl_tls13_write_finished_message(ssl); 2877a8e1175bSopenharmony_ci if (ret != 0) { 2878a8e1175bSopenharmony_ci return ret; 2879a8e1175bSopenharmony_ci } 2880a8e1175bSopenharmony_ci 2881a8e1175bSopenharmony_ci ret = mbedtls_ssl_tls13_compute_application_transform(ssl); 2882a8e1175bSopenharmony_ci if (ret != 0) { 2883a8e1175bSopenharmony_ci MBEDTLS_SSL_PEND_FATAL_ALERT( 2884a8e1175bSopenharmony_ci MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE, 2885a8e1175bSopenharmony_ci MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE); 2886a8e1175bSopenharmony_ci return ret; 2887a8e1175bSopenharmony_ci } 2888a8e1175bSopenharmony_ci 2889a8e1175bSopenharmony_ci#if defined(MBEDTLS_SSL_EARLY_DATA) 2890a8e1175bSopenharmony_ci if (ssl->handshake->early_data_accepted) { 2891a8e1175bSopenharmony_ci /* See RFC 8446 section A.2 for more information */ 2892a8e1175bSopenharmony_ci MBEDTLS_SSL_DEBUG_MSG( 2893a8e1175bSopenharmony_ci 1, ("Switch to early keys for inbound traffic. " 2894a8e1175bSopenharmony_ci "( K_recv = early data )")); 2895a8e1175bSopenharmony_ci mbedtls_ssl_set_inbound_transform( 2896a8e1175bSopenharmony_ci ssl, ssl->handshake->transform_earlydata); 2897a8e1175bSopenharmony_ci mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_END_OF_EARLY_DATA); 2898a8e1175bSopenharmony_ci return 0; 2899a8e1175bSopenharmony_ci } 2900a8e1175bSopenharmony_ci#endif /* MBEDTLS_SSL_EARLY_DATA */ 2901a8e1175bSopenharmony_ci MBEDTLS_SSL_DEBUG_MSG( 2902a8e1175bSopenharmony_ci 1, ("Switch to handshake keys for inbound traffic " 2903a8e1175bSopenharmony_ci "( K_recv = handshake )")); 2904a8e1175bSopenharmony_ci mbedtls_ssl_set_inbound_transform(ssl, ssl->handshake->transform_handshake); 2905a8e1175bSopenharmony_ci 2906a8e1175bSopenharmony_ci ssl_tls13_prepare_for_handshake_second_flight(ssl); 2907a8e1175bSopenharmony_ci 2908a8e1175bSopenharmony_ci return 0; 2909a8e1175bSopenharmony_ci} 2910a8e1175bSopenharmony_ci 2911a8e1175bSopenharmony_ci#if defined(MBEDTLS_SSL_EARLY_DATA) 2912a8e1175bSopenharmony_ci/* 2913a8e1175bSopenharmony_ci * Handler for MBEDTLS_SSL_END_OF_EARLY_DATA 2914a8e1175bSopenharmony_ci */ 2915a8e1175bSopenharmony_ci#define SSL_GOT_END_OF_EARLY_DATA 0 2916a8e1175bSopenharmony_ci#define SSL_GOT_EARLY_DATA 1 2917a8e1175bSopenharmony_ci/* Coordination: 2918a8e1175bSopenharmony_ci * Deals with the ambiguity of not knowing if the next message is an 2919a8e1175bSopenharmony_ci * EndOfEarlyData message or an application message containing early data. 2920a8e1175bSopenharmony_ci * Returns a negative code on failure, or 2921a8e1175bSopenharmony_ci * - SSL_GOT_END_OF_EARLY_DATA 2922a8e1175bSopenharmony_ci * - SSL_GOT_EARLY_DATA 2923a8e1175bSopenharmony_ci * indicating which message is received. 2924a8e1175bSopenharmony_ci */ 2925a8e1175bSopenharmony_ciMBEDTLS_CHECK_RETURN_CRITICAL 2926a8e1175bSopenharmony_cistatic int ssl_tls13_end_of_early_data_coordinate(mbedtls_ssl_context *ssl) 2927a8e1175bSopenharmony_ci{ 2928a8e1175bSopenharmony_ci int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; 2929a8e1175bSopenharmony_ci 2930a8e1175bSopenharmony_ci if ((ret = mbedtls_ssl_read_record(ssl, 0)) != 0) { 2931a8e1175bSopenharmony_ci MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret); 2932a8e1175bSopenharmony_ci return ret; 2933a8e1175bSopenharmony_ci } 2934a8e1175bSopenharmony_ci ssl->keep_current_message = 1; 2935a8e1175bSopenharmony_ci 2936a8e1175bSopenharmony_ci if (ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE && 2937a8e1175bSopenharmony_ci ssl->in_msg[0] == MBEDTLS_SSL_HS_END_OF_EARLY_DATA) { 2938a8e1175bSopenharmony_ci MBEDTLS_SSL_DEBUG_MSG(3, ("Received an end_of_early_data message.")); 2939a8e1175bSopenharmony_ci return SSL_GOT_END_OF_EARLY_DATA; 2940a8e1175bSopenharmony_ci } 2941a8e1175bSopenharmony_ci 2942a8e1175bSopenharmony_ci if (ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA) { 2943a8e1175bSopenharmony_ci if (ssl->in_offt == NULL) { 2944a8e1175bSopenharmony_ci MBEDTLS_SSL_DEBUG_MSG(3, ("Received early data")); 2945a8e1175bSopenharmony_ci /* Set the reading pointer */ 2946a8e1175bSopenharmony_ci ssl->in_offt = ssl->in_msg; 2947a8e1175bSopenharmony_ci ret = mbedtls_ssl_tls13_check_early_data_len(ssl, ssl->in_msglen); 2948a8e1175bSopenharmony_ci if (ret != 0) { 2949a8e1175bSopenharmony_ci return ret; 2950a8e1175bSopenharmony_ci } 2951a8e1175bSopenharmony_ci } 2952a8e1175bSopenharmony_ci return SSL_GOT_EARLY_DATA; 2953a8e1175bSopenharmony_ci } 2954a8e1175bSopenharmony_ci 2955a8e1175bSopenharmony_ci MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE, 2956a8e1175bSopenharmony_ci MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE); 2957a8e1175bSopenharmony_ci return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE; 2958a8e1175bSopenharmony_ci} 2959a8e1175bSopenharmony_ci 2960a8e1175bSopenharmony_ciMBEDTLS_CHECK_RETURN_CRITICAL 2961a8e1175bSopenharmony_cistatic int ssl_tls13_parse_end_of_early_data(mbedtls_ssl_context *ssl, 2962a8e1175bSopenharmony_ci const unsigned char *buf, 2963a8e1175bSopenharmony_ci const unsigned char *end) 2964a8e1175bSopenharmony_ci{ 2965a8e1175bSopenharmony_ci /* RFC 8446 section 4.5 2966a8e1175bSopenharmony_ci * 2967a8e1175bSopenharmony_ci * struct {} EndOfEarlyData; 2968a8e1175bSopenharmony_ci */ 2969a8e1175bSopenharmony_ci if (buf != end) { 2970a8e1175bSopenharmony_ci MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR, 2971a8e1175bSopenharmony_ci MBEDTLS_ERR_SSL_DECODE_ERROR); 2972a8e1175bSopenharmony_ci return MBEDTLS_ERR_SSL_DECODE_ERROR; 2973a8e1175bSopenharmony_ci } 2974a8e1175bSopenharmony_ci return 0; 2975a8e1175bSopenharmony_ci} 2976a8e1175bSopenharmony_ci 2977a8e1175bSopenharmony_ci/* 2978a8e1175bSopenharmony_ci * RFC 8446 section A.2 2979a8e1175bSopenharmony_ci * 2980a8e1175bSopenharmony_ci * | Send ServerHello 2981a8e1175bSopenharmony_ci * | K_send = handshake 2982a8e1175bSopenharmony_ci * | Send EncryptedExtensions 2983a8e1175bSopenharmony_ci * | [Send CertificateRequest] 2984a8e1175bSopenharmony_ci * Can send | [Send Certificate + CertificateVerify] 2985a8e1175bSopenharmony_ci * app data | Send Finished 2986a8e1175bSopenharmony_ci * after --> | K_send = application 2987a8e1175bSopenharmony_ci * here +--------+--------+ 2988a8e1175bSopenharmony_ci * No 0-RTT | | 0-RTT 2989a8e1175bSopenharmony_ci * | | 2990a8e1175bSopenharmony_ci * K_recv = handshake | | K_recv = early data 2991a8e1175bSopenharmony_ci * [Skip decrypt errors] | +------> WAIT_EOED -+ 2992a8e1175bSopenharmony_ci * | | Recv | | Recv EndOfEarlyData 2993a8e1175bSopenharmony_ci * | | early data | | K_recv = handshake 2994a8e1175bSopenharmony_ci * | +------------+ | 2995a8e1175bSopenharmony_ci * | | 2996a8e1175bSopenharmony_ci * +> WAIT_FLIGHT2 <--------+ 2997a8e1175bSopenharmony_ci * | 2998a8e1175bSopenharmony_ci * +--------+--------+ 2999a8e1175bSopenharmony_ci * No auth | | Client auth 3000a8e1175bSopenharmony_ci * | | 3001a8e1175bSopenharmony_ci * | v 3002a8e1175bSopenharmony_ci * | WAIT_CERT 3003a8e1175bSopenharmony_ci * | Recv | | Recv Certificate 3004a8e1175bSopenharmony_ci * | empty | v 3005a8e1175bSopenharmony_ci * | Certificate | WAIT_CV 3006a8e1175bSopenharmony_ci * | | | Recv 3007a8e1175bSopenharmony_ci * | v | CertificateVerify 3008a8e1175bSopenharmony_ci * +-> WAIT_FINISHED <---+ 3009a8e1175bSopenharmony_ci * | Recv Finished 3010a8e1175bSopenharmony_ci * 3011a8e1175bSopenharmony_ci * The function handles actions and state changes from 0-RTT to WAIT_FLIGHT2 in 3012a8e1175bSopenharmony_ci * the above diagram. 3013a8e1175bSopenharmony_ci */ 3014a8e1175bSopenharmony_ciMBEDTLS_CHECK_RETURN_CRITICAL 3015a8e1175bSopenharmony_cistatic int ssl_tls13_process_end_of_early_data(mbedtls_ssl_context *ssl) 3016a8e1175bSopenharmony_ci{ 3017a8e1175bSopenharmony_ci int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; 3018a8e1175bSopenharmony_ci 3019a8e1175bSopenharmony_ci MBEDTLS_SSL_DEBUG_MSG(2, ("=> ssl_tls13_process_end_of_early_data")); 3020a8e1175bSopenharmony_ci 3021a8e1175bSopenharmony_ci MBEDTLS_SSL_PROC_CHK_NEG(ssl_tls13_end_of_early_data_coordinate(ssl)); 3022a8e1175bSopenharmony_ci 3023a8e1175bSopenharmony_ci if (ret == SSL_GOT_END_OF_EARLY_DATA) { 3024a8e1175bSopenharmony_ci unsigned char *buf; 3025a8e1175bSopenharmony_ci size_t buf_len; 3026a8e1175bSopenharmony_ci 3027a8e1175bSopenharmony_ci MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_tls13_fetch_handshake_msg( 3028a8e1175bSopenharmony_ci ssl, MBEDTLS_SSL_HS_END_OF_EARLY_DATA, 3029a8e1175bSopenharmony_ci &buf, &buf_len)); 3030a8e1175bSopenharmony_ci 3031a8e1175bSopenharmony_ci MBEDTLS_SSL_PROC_CHK(ssl_tls13_parse_end_of_early_data( 3032a8e1175bSopenharmony_ci ssl, buf, buf + buf_len)); 3033a8e1175bSopenharmony_ci 3034a8e1175bSopenharmony_ci MBEDTLS_SSL_DEBUG_MSG( 3035a8e1175bSopenharmony_ci 1, ("Switch to handshake keys for inbound traffic" 3036a8e1175bSopenharmony_ci "( K_recv = handshake )")); 3037a8e1175bSopenharmony_ci mbedtls_ssl_set_inbound_transform( 3038a8e1175bSopenharmony_ci ssl, ssl->handshake->transform_handshake); 3039a8e1175bSopenharmony_ci 3040a8e1175bSopenharmony_ci MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_add_hs_msg_to_checksum( 3041a8e1175bSopenharmony_ci ssl, MBEDTLS_SSL_HS_END_OF_EARLY_DATA, 3042a8e1175bSopenharmony_ci buf, buf_len)); 3043a8e1175bSopenharmony_ci 3044a8e1175bSopenharmony_ci ssl_tls13_prepare_for_handshake_second_flight(ssl); 3045a8e1175bSopenharmony_ci 3046a8e1175bSopenharmony_ci } else if (ret == SSL_GOT_EARLY_DATA) { 3047a8e1175bSopenharmony_ci ret = MBEDTLS_ERR_SSL_RECEIVED_EARLY_DATA; 3048a8e1175bSopenharmony_ci goto cleanup; 3049a8e1175bSopenharmony_ci } else { 3050a8e1175bSopenharmony_ci MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen")); 3051a8e1175bSopenharmony_ci ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR; 3052a8e1175bSopenharmony_ci goto cleanup; 3053a8e1175bSopenharmony_ci } 3054a8e1175bSopenharmony_ci 3055a8e1175bSopenharmony_cicleanup: 3056a8e1175bSopenharmony_ci MBEDTLS_SSL_DEBUG_MSG(2, ("<= ssl_tls13_process_end_of_early_data")); 3057a8e1175bSopenharmony_ci return ret; 3058a8e1175bSopenharmony_ci} 3059a8e1175bSopenharmony_ci#endif /* MBEDTLS_SSL_EARLY_DATA */ 3060a8e1175bSopenharmony_ci 3061a8e1175bSopenharmony_ci/* 3062a8e1175bSopenharmony_ci * Handler for MBEDTLS_SSL_CLIENT_FINISHED 3063a8e1175bSopenharmony_ci */ 3064a8e1175bSopenharmony_ciMBEDTLS_CHECK_RETURN_CRITICAL 3065a8e1175bSopenharmony_cistatic int ssl_tls13_process_client_finished(mbedtls_ssl_context *ssl) 3066a8e1175bSopenharmony_ci{ 3067a8e1175bSopenharmony_ci int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; 3068a8e1175bSopenharmony_ci 3069a8e1175bSopenharmony_ci ret = mbedtls_ssl_tls13_process_finished_message(ssl); 3070a8e1175bSopenharmony_ci if (ret != 0) { 3071a8e1175bSopenharmony_ci return ret; 3072a8e1175bSopenharmony_ci } 3073a8e1175bSopenharmony_ci 3074a8e1175bSopenharmony_ci ret = mbedtls_ssl_tls13_compute_resumption_master_secret(ssl); 3075a8e1175bSopenharmony_ci if (ret != 0) { 3076a8e1175bSopenharmony_ci MBEDTLS_SSL_DEBUG_RET( 3077a8e1175bSopenharmony_ci 1, "mbedtls_ssl_tls13_compute_resumption_master_secret", ret); 3078a8e1175bSopenharmony_ci } 3079a8e1175bSopenharmony_ci 3080a8e1175bSopenharmony_ci mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HANDSHAKE_WRAPUP); 3081a8e1175bSopenharmony_ci return 0; 3082a8e1175bSopenharmony_ci} 3083a8e1175bSopenharmony_ci 3084a8e1175bSopenharmony_ci/* 3085a8e1175bSopenharmony_ci * Handler for MBEDTLS_SSL_HANDSHAKE_WRAPUP 3086a8e1175bSopenharmony_ci */ 3087a8e1175bSopenharmony_ciMBEDTLS_CHECK_RETURN_CRITICAL 3088a8e1175bSopenharmony_cistatic int ssl_tls13_handshake_wrapup(mbedtls_ssl_context *ssl) 3089a8e1175bSopenharmony_ci{ 3090a8e1175bSopenharmony_ci MBEDTLS_SSL_DEBUG_MSG(2, ("handshake: done")); 3091a8e1175bSopenharmony_ci 3092a8e1175bSopenharmony_ci mbedtls_ssl_tls13_handshake_wrapup(ssl); 3093a8e1175bSopenharmony_ci 3094a8e1175bSopenharmony_ci#if defined(MBEDTLS_SSL_SESSION_TICKETS) && \ 3095a8e1175bSopenharmony_ci defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED) 3096a8e1175bSopenharmony_ci/* TODO: Remove the check of SOME_PSK_ENABLED since SESSION_TICKETS requires 3097a8e1175bSopenharmony_ci * SOME_PSK_ENABLED to be enabled. Here is just to make CI happy. It is 3098a8e1175bSopenharmony_ci * expected to be resolved with issue#6395. 3099a8e1175bSopenharmony_ci */ 3100a8e1175bSopenharmony_ci /* Sent NewSessionTicket message only when client supports PSK */ 3101a8e1175bSopenharmony_ci if (mbedtls_ssl_tls13_is_some_psk_supported(ssl)) { 3102a8e1175bSopenharmony_ci mbedtls_ssl_handshake_set_state( 3103a8e1175bSopenharmony_ci ssl, MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET); 3104a8e1175bSopenharmony_ci } else 3105a8e1175bSopenharmony_ci#endif 3106a8e1175bSopenharmony_ci { 3107a8e1175bSopenharmony_ci mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HANDSHAKE_OVER); 3108a8e1175bSopenharmony_ci } 3109a8e1175bSopenharmony_ci return 0; 3110a8e1175bSopenharmony_ci} 3111a8e1175bSopenharmony_ci 3112a8e1175bSopenharmony_ci/* 3113a8e1175bSopenharmony_ci * Handler for MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET 3114a8e1175bSopenharmony_ci */ 3115a8e1175bSopenharmony_ci#define SSL_NEW_SESSION_TICKET_SKIP 0 3116a8e1175bSopenharmony_ci#define SSL_NEW_SESSION_TICKET_WRITE 1 3117a8e1175bSopenharmony_ciMBEDTLS_CHECK_RETURN_CRITICAL 3118a8e1175bSopenharmony_cistatic int ssl_tls13_write_new_session_ticket_coordinate(mbedtls_ssl_context *ssl) 3119a8e1175bSopenharmony_ci{ 3120a8e1175bSopenharmony_ci /* Check whether the use of session tickets is enabled */ 3121a8e1175bSopenharmony_ci if (ssl->conf->f_ticket_write == NULL) { 3122a8e1175bSopenharmony_ci MBEDTLS_SSL_DEBUG_MSG(2, ("NewSessionTicket: disabled," 3123a8e1175bSopenharmony_ci " callback is not set")); 3124a8e1175bSopenharmony_ci return SSL_NEW_SESSION_TICKET_SKIP; 3125a8e1175bSopenharmony_ci } 3126a8e1175bSopenharmony_ci if (ssl->conf->new_session_tickets_count == 0) { 3127a8e1175bSopenharmony_ci MBEDTLS_SSL_DEBUG_MSG(2, ("NewSessionTicket: disabled," 3128a8e1175bSopenharmony_ci " configured count is zero")); 3129a8e1175bSopenharmony_ci return SSL_NEW_SESSION_TICKET_SKIP; 3130a8e1175bSopenharmony_ci } 3131a8e1175bSopenharmony_ci 3132a8e1175bSopenharmony_ci if (ssl->handshake->new_session_tickets_count == 0) { 3133a8e1175bSopenharmony_ci MBEDTLS_SSL_DEBUG_MSG(2, ("NewSessionTicket: all tickets have " 3134a8e1175bSopenharmony_ci "been sent.")); 3135a8e1175bSopenharmony_ci return SSL_NEW_SESSION_TICKET_SKIP; 3136a8e1175bSopenharmony_ci } 3137a8e1175bSopenharmony_ci 3138a8e1175bSopenharmony_ci return SSL_NEW_SESSION_TICKET_WRITE; 3139a8e1175bSopenharmony_ci} 3140a8e1175bSopenharmony_ci 3141a8e1175bSopenharmony_ci#if defined(MBEDTLS_SSL_SESSION_TICKETS) 3142a8e1175bSopenharmony_ciMBEDTLS_CHECK_RETURN_CRITICAL 3143a8e1175bSopenharmony_cistatic int ssl_tls13_prepare_new_session_ticket(mbedtls_ssl_context *ssl, 3144a8e1175bSopenharmony_ci unsigned char *ticket_nonce, 3145a8e1175bSopenharmony_ci size_t ticket_nonce_size) 3146a8e1175bSopenharmony_ci{ 3147a8e1175bSopenharmony_ci int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; 3148a8e1175bSopenharmony_ci mbedtls_ssl_session *session = ssl->session; 3149a8e1175bSopenharmony_ci mbedtls_ssl_ciphersuite_t *ciphersuite_info; 3150a8e1175bSopenharmony_ci psa_algorithm_t psa_hash_alg; 3151a8e1175bSopenharmony_ci int hash_length; 3152a8e1175bSopenharmony_ci 3153a8e1175bSopenharmony_ci MBEDTLS_SSL_DEBUG_MSG(2, ("=> prepare NewSessionTicket msg")); 3154a8e1175bSopenharmony_ci 3155a8e1175bSopenharmony_ci /* Set ticket_flags depends on the advertised psk key exchange mode */ 3156a8e1175bSopenharmony_ci mbedtls_ssl_tls13_session_clear_ticket_flags( 3157a8e1175bSopenharmony_ci session, MBEDTLS_SSL_TLS1_3_TICKET_FLAGS_MASK); 3158a8e1175bSopenharmony_ci#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED) 3159a8e1175bSopenharmony_ci mbedtls_ssl_tls13_session_set_ticket_flags( 3160a8e1175bSopenharmony_ci session, ssl->handshake->tls13_kex_modes); 3161a8e1175bSopenharmony_ci#endif 3162a8e1175bSopenharmony_ci 3163a8e1175bSopenharmony_ci#if defined(MBEDTLS_SSL_EARLY_DATA) 3164a8e1175bSopenharmony_ci if (ssl->conf->early_data_enabled == MBEDTLS_SSL_EARLY_DATA_ENABLED && 3165a8e1175bSopenharmony_ci ssl->conf->max_early_data_size > 0) { 3166a8e1175bSopenharmony_ci mbedtls_ssl_tls13_session_set_ticket_flags( 3167a8e1175bSopenharmony_ci session, MBEDTLS_SSL_TLS1_3_TICKET_ALLOW_EARLY_DATA); 3168a8e1175bSopenharmony_ci session->max_early_data_size = ssl->conf->max_early_data_size; 3169a8e1175bSopenharmony_ci } 3170a8e1175bSopenharmony_ci#endif /* MBEDTLS_SSL_EARLY_DATA */ 3171a8e1175bSopenharmony_ci 3172a8e1175bSopenharmony_ci MBEDTLS_SSL_PRINT_TICKET_FLAGS(4, session->ticket_flags); 3173a8e1175bSopenharmony_ci 3174a8e1175bSopenharmony_ci#if defined(MBEDTLS_SSL_EARLY_DATA) && defined(MBEDTLS_SSL_ALPN) 3175a8e1175bSopenharmony_ci if (session->ticket_alpn == NULL) { 3176a8e1175bSopenharmony_ci ret = mbedtls_ssl_session_set_ticket_alpn(session, ssl->alpn_chosen); 3177a8e1175bSopenharmony_ci if (ret != 0) { 3178a8e1175bSopenharmony_ci return ret; 3179a8e1175bSopenharmony_ci } 3180a8e1175bSopenharmony_ci } 3181a8e1175bSopenharmony_ci#endif 3182a8e1175bSopenharmony_ci 3183a8e1175bSopenharmony_ci /* Generate ticket_age_add */ 3184a8e1175bSopenharmony_ci if ((ret = ssl->conf->f_rng(ssl->conf->p_rng, 3185a8e1175bSopenharmony_ci (unsigned char *) &session->ticket_age_add, 3186a8e1175bSopenharmony_ci sizeof(session->ticket_age_add)) != 0)) { 3187a8e1175bSopenharmony_ci MBEDTLS_SSL_DEBUG_RET(1, "generate_ticket_age_add", ret); 3188a8e1175bSopenharmony_ci return ret; 3189a8e1175bSopenharmony_ci } 3190a8e1175bSopenharmony_ci MBEDTLS_SSL_DEBUG_MSG(3, ("ticket_age_add: %u", 3191a8e1175bSopenharmony_ci (unsigned int) session->ticket_age_add)); 3192a8e1175bSopenharmony_ci 3193a8e1175bSopenharmony_ci /* Generate ticket_nonce */ 3194a8e1175bSopenharmony_ci ret = ssl->conf->f_rng(ssl->conf->p_rng, ticket_nonce, ticket_nonce_size); 3195a8e1175bSopenharmony_ci if (ret != 0) { 3196a8e1175bSopenharmony_ci MBEDTLS_SSL_DEBUG_RET(1, "generate_ticket_nonce", ret); 3197a8e1175bSopenharmony_ci return ret; 3198a8e1175bSopenharmony_ci } 3199a8e1175bSopenharmony_ci MBEDTLS_SSL_DEBUG_BUF(3, "ticket_nonce:", 3200a8e1175bSopenharmony_ci ticket_nonce, ticket_nonce_size); 3201a8e1175bSopenharmony_ci 3202a8e1175bSopenharmony_ci ciphersuite_info = 3203a8e1175bSopenharmony_ci (mbedtls_ssl_ciphersuite_t *) ssl->handshake->ciphersuite_info; 3204a8e1175bSopenharmony_ci psa_hash_alg = mbedtls_md_psa_alg_from_type((mbedtls_md_type_t) ciphersuite_info->mac); 3205a8e1175bSopenharmony_ci hash_length = PSA_HASH_LENGTH(psa_hash_alg); 3206a8e1175bSopenharmony_ci if (hash_length == -1 || 3207a8e1175bSopenharmony_ci (size_t) hash_length > sizeof(session->resumption_key)) { 3208a8e1175bSopenharmony_ci return MBEDTLS_ERR_SSL_INTERNAL_ERROR; 3209a8e1175bSopenharmony_ci } 3210a8e1175bSopenharmony_ci 3211a8e1175bSopenharmony_ci /* In this code the psk key length equals the length of the hash */ 3212a8e1175bSopenharmony_ci session->resumption_key_len = hash_length; 3213a8e1175bSopenharmony_ci session->ciphersuite = ciphersuite_info->id; 3214a8e1175bSopenharmony_ci 3215a8e1175bSopenharmony_ci /* Compute resumption key 3216a8e1175bSopenharmony_ci * 3217a8e1175bSopenharmony_ci * HKDF-Expand-Label( resumption_master_secret, 3218a8e1175bSopenharmony_ci * "resumption", ticket_nonce, Hash.length ) 3219a8e1175bSopenharmony_ci */ 3220a8e1175bSopenharmony_ci ret = mbedtls_ssl_tls13_hkdf_expand_label( 3221a8e1175bSopenharmony_ci psa_hash_alg, 3222a8e1175bSopenharmony_ci session->app_secrets.resumption_master_secret, 3223a8e1175bSopenharmony_ci hash_length, 3224a8e1175bSopenharmony_ci MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN(resumption), 3225a8e1175bSopenharmony_ci ticket_nonce, 3226a8e1175bSopenharmony_ci ticket_nonce_size, 3227a8e1175bSopenharmony_ci session->resumption_key, 3228a8e1175bSopenharmony_ci hash_length); 3229a8e1175bSopenharmony_ci 3230a8e1175bSopenharmony_ci if (ret != 0) { 3231a8e1175bSopenharmony_ci MBEDTLS_SSL_DEBUG_RET(2, 3232a8e1175bSopenharmony_ci "Creating the ticket-resumed PSK failed", 3233a8e1175bSopenharmony_ci ret); 3234a8e1175bSopenharmony_ci return ret; 3235a8e1175bSopenharmony_ci } 3236a8e1175bSopenharmony_ci MBEDTLS_SSL_DEBUG_BUF(3, "Ticket-resumed PSK", 3237a8e1175bSopenharmony_ci session->resumption_key, 3238a8e1175bSopenharmony_ci session->resumption_key_len); 3239a8e1175bSopenharmony_ci 3240a8e1175bSopenharmony_ci MBEDTLS_SSL_DEBUG_BUF(3, "resumption_master_secret", 3241a8e1175bSopenharmony_ci session->app_secrets.resumption_master_secret, 3242a8e1175bSopenharmony_ci hash_length); 3243a8e1175bSopenharmony_ci 3244a8e1175bSopenharmony_ci return 0; 3245a8e1175bSopenharmony_ci} 3246a8e1175bSopenharmony_ci 3247a8e1175bSopenharmony_ci/* This function creates a NewSessionTicket message in the following format: 3248a8e1175bSopenharmony_ci * 3249a8e1175bSopenharmony_ci * struct { 3250a8e1175bSopenharmony_ci * uint32 ticket_lifetime; 3251a8e1175bSopenharmony_ci * uint32 ticket_age_add; 3252a8e1175bSopenharmony_ci * opaque ticket_nonce<0..255>; 3253a8e1175bSopenharmony_ci * opaque ticket<1..2^16-1>; 3254a8e1175bSopenharmony_ci * Extension extensions<0..2^16-2>; 3255a8e1175bSopenharmony_ci * } NewSessionTicket; 3256a8e1175bSopenharmony_ci * 3257a8e1175bSopenharmony_ci * The ticket inside the NewSessionTicket message is an encrypted container 3258a8e1175bSopenharmony_ci * carrying the necessary information so that the server is later able to 3259a8e1175bSopenharmony_ci * re-start the communication. 3260a8e1175bSopenharmony_ci * 3261a8e1175bSopenharmony_ci * The following fields are placed inside the ticket by the 3262a8e1175bSopenharmony_ci * f_ticket_write() function: 3263a8e1175bSopenharmony_ci * 3264a8e1175bSopenharmony_ci * - creation time (ticket_creation_time) 3265a8e1175bSopenharmony_ci * - flags (ticket_flags) 3266a8e1175bSopenharmony_ci * - age add (ticket_age_add) 3267a8e1175bSopenharmony_ci * - key (resumption_key) 3268a8e1175bSopenharmony_ci * - key length (resumption_key_len) 3269a8e1175bSopenharmony_ci * - ciphersuite (ciphersuite) 3270a8e1175bSopenharmony_ci * - max_early_data_size (max_early_data_size) 3271a8e1175bSopenharmony_ci */ 3272a8e1175bSopenharmony_ciMBEDTLS_CHECK_RETURN_CRITICAL 3273a8e1175bSopenharmony_cistatic int ssl_tls13_write_new_session_ticket_body(mbedtls_ssl_context *ssl, 3274a8e1175bSopenharmony_ci unsigned char *buf, 3275a8e1175bSopenharmony_ci unsigned char *end, 3276a8e1175bSopenharmony_ci size_t *out_len, 3277a8e1175bSopenharmony_ci unsigned char *ticket_nonce, 3278a8e1175bSopenharmony_ci size_t ticket_nonce_size) 3279a8e1175bSopenharmony_ci{ 3280a8e1175bSopenharmony_ci int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; 3281a8e1175bSopenharmony_ci unsigned char *p = buf; 3282a8e1175bSopenharmony_ci mbedtls_ssl_session *session = ssl->session; 3283a8e1175bSopenharmony_ci size_t ticket_len; 3284a8e1175bSopenharmony_ci uint32_t ticket_lifetime; 3285a8e1175bSopenharmony_ci unsigned char *p_extensions_len; 3286a8e1175bSopenharmony_ci 3287a8e1175bSopenharmony_ci *out_len = 0; 3288a8e1175bSopenharmony_ci MBEDTLS_SSL_DEBUG_MSG(2, ("=> write NewSessionTicket msg")); 3289a8e1175bSopenharmony_ci 3290a8e1175bSopenharmony_ci /* 3291a8e1175bSopenharmony_ci * ticket_lifetime 4 bytes 3292a8e1175bSopenharmony_ci * ticket_age_add 4 bytes 3293a8e1175bSopenharmony_ci * ticket_nonce 1 + ticket_nonce_size bytes 3294a8e1175bSopenharmony_ci * ticket >=2 bytes 3295a8e1175bSopenharmony_ci */ 3296a8e1175bSopenharmony_ci MBEDTLS_SSL_CHK_BUF_PTR(p, end, 4 + 4 + 1 + ticket_nonce_size + 2); 3297a8e1175bSopenharmony_ci 3298a8e1175bSopenharmony_ci /* Generate ticket and ticket_lifetime */ 3299a8e1175bSopenharmony_ci#if defined(MBEDTLS_HAVE_TIME) 3300a8e1175bSopenharmony_ci session->ticket_creation_time = mbedtls_ms_time(); 3301a8e1175bSopenharmony_ci#endif 3302a8e1175bSopenharmony_ci ret = ssl->conf->f_ticket_write(ssl->conf->p_ticket, 3303a8e1175bSopenharmony_ci session, 3304a8e1175bSopenharmony_ci p + 9 + ticket_nonce_size + 2, 3305a8e1175bSopenharmony_ci end, 3306a8e1175bSopenharmony_ci &ticket_len, 3307a8e1175bSopenharmony_ci &ticket_lifetime); 3308a8e1175bSopenharmony_ci if (ret != 0) { 3309a8e1175bSopenharmony_ci MBEDTLS_SSL_DEBUG_RET(1, "write_ticket", ret); 3310a8e1175bSopenharmony_ci return ret; 3311a8e1175bSopenharmony_ci } 3312a8e1175bSopenharmony_ci 3313a8e1175bSopenharmony_ci /* RFC 8446 section 4.6.1 3314a8e1175bSopenharmony_ci * 3315a8e1175bSopenharmony_ci * ticket_lifetime: Indicates the lifetime in seconds as a 32-bit 3316a8e1175bSopenharmony_ci * unsigned integer in network byte order from the time of ticket 3317a8e1175bSopenharmony_ci * issuance. Servers MUST NOT use any value greater than 3318a8e1175bSopenharmony_ci * 604800 seconds (7 days) ... 3319a8e1175bSopenharmony_ci */ 3320a8e1175bSopenharmony_ci if (ticket_lifetime > MBEDTLS_SSL_TLS1_3_MAX_ALLOWED_TICKET_LIFETIME) { 3321a8e1175bSopenharmony_ci MBEDTLS_SSL_DEBUG_MSG( 3322a8e1175bSopenharmony_ci 1, ("Ticket lifetime (%u) is greater than 7 days.", 3323a8e1175bSopenharmony_ci (unsigned int) ticket_lifetime)); 3324a8e1175bSopenharmony_ci return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; 3325a8e1175bSopenharmony_ci } 3326a8e1175bSopenharmony_ci 3327a8e1175bSopenharmony_ci MBEDTLS_PUT_UINT32_BE(ticket_lifetime, p, 0); 3328a8e1175bSopenharmony_ci MBEDTLS_SSL_DEBUG_MSG(3, ("ticket_lifetime: %u", 3329a8e1175bSopenharmony_ci (unsigned int) ticket_lifetime)); 3330a8e1175bSopenharmony_ci 3331a8e1175bSopenharmony_ci /* Write ticket_age_add */ 3332a8e1175bSopenharmony_ci MBEDTLS_PUT_UINT32_BE(session->ticket_age_add, p, 4); 3333a8e1175bSopenharmony_ci MBEDTLS_SSL_DEBUG_MSG(3, ("ticket_age_add: %u", 3334a8e1175bSopenharmony_ci (unsigned int) session->ticket_age_add)); 3335a8e1175bSopenharmony_ci 3336a8e1175bSopenharmony_ci /* Write ticket_nonce */ 3337a8e1175bSopenharmony_ci p[8] = (unsigned char) ticket_nonce_size; 3338a8e1175bSopenharmony_ci if (ticket_nonce_size > 0) { 3339a8e1175bSopenharmony_ci memcpy(p + 9, ticket_nonce, ticket_nonce_size); 3340a8e1175bSopenharmony_ci } 3341a8e1175bSopenharmony_ci p += 9 + ticket_nonce_size; 3342a8e1175bSopenharmony_ci 3343a8e1175bSopenharmony_ci /* Write ticket */ 3344a8e1175bSopenharmony_ci MBEDTLS_PUT_UINT16_BE(ticket_len, p, 0); 3345a8e1175bSopenharmony_ci p += 2; 3346a8e1175bSopenharmony_ci MBEDTLS_SSL_DEBUG_BUF(4, "ticket", p, ticket_len); 3347a8e1175bSopenharmony_ci p += ticket_len; 3348a8e1175bSopenharmony_ci 3349a8e1175bSopenharmony_ci /* Ticket Extensions 3350a8e1175bSopenharmony_ci * 3351a8e1175bSopenharmony_ci * Extension extensions<0..2^16-2>; 3352a8e1175bSopenharmony_ci */ 3353a8e1175bSopenharmony_ci ssl->handshake->sent_extensions = MBEDTLS_SSL_EXT_MASK_NONE; 3354a8e1175bSopenharmony_ci 3355a8e1175bSopenharmony_ci MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2); 3356a8e1175bSopenharmony_ci p_extensions_len = p; 3357a8e1175bSopenharmony_ci p += 2; 3358a8e1175bSopenharmony_ci 3359a8e1175bSopenharmony_ci#if defined(MBEDTLS_SSL_EARLY_DATA) 3360a8e1175bSopenharmony_ci if (mbedtls_ssl_tls13_session_ticket_allow_early_data(session)) { 3361a8e1175bSopenharmony_ci size_t output_len; 3362a8e1175bSopenharmony_ci 3363a8e1175bSopenharmony_ci if ((ret = mbedtls_ssl_tls13_write_early_data_ext( 3364a8e1175bSopenharmony_ci ssl, 1, p, end, &output_len)) != 0) { 3365a8e1175bSopenharmony_ci MBEDTLS_SSL_DEBUG_RET( 3366a8e1175bSopenharmony_ci 1, "mbedtls_ssl_tls13_write_early_data_ext", ret); 3367a8e1175bSopenharmony_ci return ret; 3368a8e1175bSopenharmony_ci } 3369a8e1175bSopenharmony_ci p += output_len; 3370a8e1175bSopenharmony_ci } else { 3371a8e1175bSopenharmony_ci MBEDTLS_SSL_DEBUG_MSG( 3372a8e1175bSopenharmony_ci 4, ("early_data not allowed, " 3373a8e1175bSopenharmony_ci "skip early_data extension in NewSessionTicket")); 3374a8e1175bSopenharmony_ci } 3375a8e1175bSopenharmony_ci 3376a8e1175bSopenharmony_ci#endif /* MBEDTLS_SSL_EARLY_DATA */ 3377a8e1175bSopenharmony_ci 3378a8e1175bSopenharmony_ci MBEDTLS_PUT_UINT16_BE(p - p_extensions_len - 2, p_extensions_len, 0); 3379a8e1175bSopenharmony_ci 3380a8e1175bSopenharmony_ci *out_len = p - buf; 3381a8e1175bSopenharmony_ci MBEDTLS_SSL_DEBUG_BUF(4, "ticket", buf, *out_len); 3382a8e1175bSopenharmony_ci MBEDTLS_SSL_DEBUG_MSG(2, ("<= write new session ticket")); 3383a8e1175bSopenharmony_ci 3384a8e1175bSopenharmony_ci MBEDTLS_SSL_PRINT_EXTS( 3385a8e1175bSopenharmony_ci 3, MBEDTLS_SSL_HS_NEW_SESSION_TICKET, ssl->handshake->sent_extensions); 3386a8e1175bSopenharmony_ci 3387a8e1175bSopenharmony_ci return 0; 3388a8e1175bSopenharmony_ci} 3389a8e1175bSopenharmony_ci 3390a8e1175bSopenharmony_ci/* 3391a8e1175bSopenharmony_ci * Handler for MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET 3392a8e1175bSopenharmony_ci */ 3393a8e1175bSopenharmony_cistatic int ssl_tls13_write_new_session_ticket(mbedtls_ssl_context *ssl) 3394a8e1175bSopenharmony_ci{ 3395a8e1175bSopenharmony_ci int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; 3396a8e1175bSopenharmony_ci 3397a8e1175bSopenharmony_ci MBEDTLS_SSL_PROC_CHK_NEG(ssl_tls13_write_new_session_ticket_coordinate(ssl)); 3398a8e1175bSopenharmony_ci 3399a8e1175bSopenharmony_ci if (ret == SSL_NEW_SESSION_TICKET_WRITE) { 3400a8e1175bSopenharmony_ci unsigned char ticket_nonce[MBEDTLS_SSL_TLS1_3_TICKET_NONCE_LENGTH]; 3401a8e1175bSopenharmony_ci unsigned char *buf; 3402a8e1175bSopenharmony_ci size_t buf_len, msg_len; 3403a8e1175bSopenharmony_ci 3404a8e1175bSopenharmony_ci MBEDTLS_SSL_PROC_CHK(ssl_tls13_prepare_new_session_ticket( 3405a8e1175bSopenharmony_ci ssl, ticket_nonce, sizeof(ticket_nonce))); 3406a8e1175bSopenharmony_ci 3407a8e1175bSopenharmony_ci MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg( 3408a8e1175bSopenharmony_ci ssl, MBEDTLS_SSL_HS_NEW_SESSION_TICKET, 3409a8e1175bSopenharmony_ci &buf, &buf_len)); 3410a8e1175bSopenharmony_ci 3411a8e1175bSopenharmony_ci MBEDTLS_SSL_PROC_CHK(ssl_tls13_write_new_session_ticket_body( 3412a8e1175bSopenharmony_ci ssl, buf, buf + buf_len, &msg_len, 3413a8e1175bSopenharmony_ci ticket_nonce, sizeof(ticket_nonce))); 3414a8e1175bSopenharmony_ci 3415a8e1175bSopenharmony_ci MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg( 3416a8e1175bSopenharmony_ci ssl, buf_len, msg_len)); 3417a8e1175bSopenharmony_ci 3418a8e1175bSopenharmony_ci /* Limit session tickets count to one when resumption connection. 3419a8e1175bSopenharmony_ci * 3420a8e1175bSopenharmony_ci * See document of mbedtls_ssl_conf_new_session_tickets. 3421a8e1175bSopenharmony_ci */ 3422a8e1175bSopenharmony_ci if (ssl->handshake->resume == 1) { 3423a8e1175bSopenharmony_ci ssl->handshake->new_session_tickets_count = 0; 3424a8e1175bSopenharmony_ci } else { 3425a8e1175bSopenharmony_ci ssl->handshake->new_session_tickets_count--; 3426a8e1175bSopenharmony_ci } 3427a8e1175bSopenharmony_ci 3428a8e1175bSopenharmony_ci mbedtls_ssl_handshake_set_state( 3429a8e1175bSopenharmony_ci ssl, MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET_FLUSH); 3430a8e1175bSopenharmony_ci } else { 3431a8e1175bSopenharmony_ci mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HANDSHAKE_OVER); 3432a8e1175bSopenharmony_ci } 3433a8e1175bSopenharmony_ci 3434a8e1175bSopenharmony_cicleanup: 3435a8e1175bSopenharmony_ci 3436a8e1175bSopenharmony_ci return ret; 3437a8e1175bSopenharmony_ci} 3438a8e1175bSopenharmony_ci#endif /* MBEDTLS_SSL_SESSION_TICKETS */ 3439a8e1175bSopenharmony_ci 3440a8e1175bSopenharmony_ci/* 3441a8e1175bSopenharmony_ci * TLS 1.3 State Machine -- server side 3442a8e1175bSopenharmony_ci */ 3443a8e1175bSopenharmony_ciint mbedtls_ssl_tls13_handshake_server_step(mbedtls_ssl_context *ssl) 3444a8e1175bSopenharmony_ci{ 3445a8e1175bSopenharmony_ci int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; 3446a8e1175bSopenharmony_ci 3447a8e1175bSopenharmony_ci if (ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER || ssl->handshake == NULL) { 3448a8e1175bSopenharmony_ci return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; 3449a8e1175bSopenharmony_ci } 3450a8e1175bSopenharmony_ci 3451a8e1175bSopenharmony_ci MBEDTLS_SSL_DEBUG_MSG(2, ("tls13 server state: %s(%d)", 3452a8e1175bSopenharmony_ci mbedtls_ssl_states_str((mbedtls_ssl_states) ssl->state), 3453a8e1175bSopenharmony_ci ssl->state)); 3454a8e1175bSopenharmony_ci 3455a8e1175bSopenharmony_ci switch (ssl->state) { 3456a8e1175bSopenharmony_ci /* start state */ 3457a8e1175bSopenharmony_ci case MBEDTLS_SSL_HELLO_REQUEST: 3458a8e1175bSopenharmony_ci mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_HELLO); 3459a8e1175bSopenharmony_ci ret = 0; 3460a8e1175bSopenharmony_ci break; 3461a8e1175bSopenharmony_ci 3462a8e1175bSopenharmony_ci case MBEDTLS_SSL_CLIENT_HELLO: 3463a8e1175bSopenharmony_ci ret = ssl_tls13_process_client_hello(ssl); 3464a8e1175bSopenharmony_ci if (ret != 0) { 3465a8e1175bSopenharmony_ci MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_process_client_hello", ret); 3466a8e1175bSopenharmony_ci } 3467a8e1175bSopenharmony_ci break; 3468a8e1175bSopenharmony_ci 3469a8e1175bSopenharmony_ci case MBEDTLS_SSL_HELLO_RETRY_REQUEST: 3470a8e1175bSopenharmony_ci ret = ssl_tls13_write_hello_retry_request(ssl); 3471a8e1175bSopenharmony_ci if (ret != 0) { 3472a8e1175bSopenharmony_ci MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_write_hello_retry_request", ret); 3473a8e1175bSopenharmony_ci return ret; 3474a8e1175bSopenharmony_ci } 3475a8e1175bSopenharmony_ci break; 3476a8e1175bSopenharmony_ci 3477a8e1175bSopenharmony_ci case MBEDTLS_SSL_SERVER_HELLO: 3478a8e1175bSopenharmony_ci ret = ssl_tls13_write_server_hello(ssl); 3479a8e1175bSopenharmony_ci break; 3480a8e1175bSopenharmony_ci 3481a8e1175bSopenharmony_ci case MBEDTLS_SSL_ENCRYPTED_EXTENSIONS: 3482a8e1175bSopenharmony_ci ret = ssl_tls13_write_encrypted_extensions(ssl); 3483a8e1175bSopenharmony_ci if (ret != 0) { 3484a8e1175bSopenharmony_ci MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls13_write_encrypted_extensions", ret); 3485a8e1175bSopenharmony_ci return ret; 3486a8e1175bSopenharmony_ci } 3487a8e1175bSopenharmony_ci break; 3488a8e1175bSopenharmony_ci 3489a8e1175bSopenharmony_ci#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED) 3490a8e1175bSopenharmony_ci case MBEDTLS_SSL_CERTIFICATE_REQUEST: 3491a8e1175bSopenharmony_ci ret = ssl_tls13_write_certificate_request(ssl); 3492a8e1175bSopenharmony_ci break; 3493a8e1175bSopenharmony_ci 3494a8e1175bSopenharmony_ci case MBEDTLS_SSL_SERVER_CERTIFICATE: 3495a8e1175bSopenharmony_ci ret = ssl_tls13_write_server_certificate(ssl); 3496a8e1175bSopenharmony_ci break; 3497a8e1175bSopenharmony_ci 3498a8e1175bSopenharmony_ci case MBEDTLS_SSL_CERTIFICATE_VERIFY: 3499a8e1175bSopenharmony_ci ret = ssl_tls13_write_certificate_verify(ssl); 3500a8e1175bSopenharmony_ci break; 3501a8e1175bSopenharmony_ci#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */ 3502a8e1175bSopenharmony_ci 3503a8e1175bSopenharmony_ci /* 3504a8e1175bSopenharmony_ci * Injection of dummy-CCS's for middlebox compatibility 3505a8e1175bSopenharmony_ci */ 3506a8e1175bSopenharmony_ci#if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE) 3507a8e1175bSopenharmony_ci case MBEDTLS_SSL_SERVER_CCS_AFTER_HELLO_RETRY_REQUEST: 3508a8e1175bSopenharmony_ci ret = mbedtls_ssl_tls13_write_change_cipher_spec(ssl); 3509a8e1175bSopenharmony_ci if (ret == 0) { 3510a8e1175bSopenharmony_ci mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_CLIENT_HELLO); 3511a8e1175bSopenharmony_ci } 3512a8e1175bSopenharmony_ci break; 3513a8e1175bSopenharmony_ci 3514a8e1175bSopenharmony_ci case MBEDTLS_SSL_SERVER_CCS_AFTER_SERVER_HELLO: 3515a8e1175bSopenharmony_ci ret = mbedtls_ssl_tls13_write_change_cipher_spec(ssl); 3516a8e1175bSopenharmony_ci if (ret != 0) { 3517a8e1175bSopenharmony_ci break; 3518a8e1175bSopenharmony_ci } 3519a8e1175bSopenharmony_ci mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_ENCRYPTED_EXTENSIONS); 3520a8e1175bSopenharmony_ci break; 3521a8e1175bSopenharmony_ci#endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */ 3522a8e1175bSopenharmony_ci 3523a8e1175bSopenharmony_ci case MBEDTLS_SSL_SERVER_FINISHED: 3524a8e1175bSopenharmony_ci ret = ssl_tls13_write_server_finished(ssl); 3525a8e1175bSopenharmony_ci break; 3526a8e1175bSopenharmony_ci 3527a8e1175bSopenharmony_ci#if defined(MBEDTLS_SSL_EARLY_DATA) 3528a8e1175bSopenharmony_ci case MBEDTLS_SSL_END_OF_EARLY_DATA: 3529a8e1175bSopenharmony_ci ret = ssl_tls13_process_end_of_early_data(ssl); 3530a8e1175bSopenharmony_ci break; 3531a8e1175bSopenharmony_ci#endif /* MBEDTLS_SSL_EARLY_DATA */ 3532a8e1175bSopenharmony_ci 3533a8e1175bSopenharmony_ci case MBEDTLS_SSL_CLIENT_FINISHED: 3534a8e1175bSopenharmony_ci ret = ssl_tls13_process_client_finished(ssl); 3535a8e1175bSopenharmony_ci break; 3536a8e1175bSopenharmony_ci 3537a8e1175bSopenharmony_ci case MBEDTLS_SSL_HANDSHAKE_WRAPUP: 3538a8e1175bSopenharmony_ci ret = ssl_tls13_handshake_wrapup(ssl); 3539a8e1175bSopenharmony_ci break; 3540a8e1175bSopenharmony_ci 3541a8e1175bSopenharmony_ci#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED) 3542a8e1175bSopenharmony_ci case MBEDTLS_SSL_CLIENT_CERTIFICATE: 3543a8e1175bSopenharmony_ci ret = mbedtls_ssl_tls13_process_certificate(ssl); 3544a8e1175bSopenharmony_ci if (ret == 0) { 3545a8e1175bSopenharmony_ci if (ssl->session_negotiate->peer_cert != NULL) { 3546a8e1175bSopenharmony_ci mbedtls_ssl_handshake_set_state( 3547a8e1175bSopenharmony_ci ssl, MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY); 3548a8e1175bSopenharmony_ci } else { 3549a8e1175bSopenharmony_ci MBEDTLS_SSL_DEBUG_MSG(2, ("skip parse certificate verify")); 3550a8e1175bSopenharmony_ci mbedtls_ssl_handshake_set_state( 3551a8e1175bSopenharmony_ci ssl, MBEDTLS_SSL_CLIENT_FINISHED); 3552a8e1175bSopenharmony_ci } 3553a8e1175bSopenharmony_ci } 3554a8e1175bSopenharmony_ci break; 3555a8e1175bSopenharmony_ci 3556a8e1175bSopenharmony_ci case MBEDTLS_SSL_CLIENT_CERTIFICATE_VERIFY: 3557a8e1175bSopenharmony_ci ret = mbedtls_ssl_tls13_process_certificate_verify(ssl); 3558a8e1175bSopenharmony_ci if (ret == 0) { 3559a8e1175bSopenharmony_ci mbedtls_ssl_handshake_set_state( 3560a8e1175bSopenharmony_ci ssl, MBEDTLS_SSL_CLIENT_FINISHED); 3561a8e1175bSopenharmony_ci } 3562a8e1175bSopenharmony_ci break; 3563a8e1175bSopenharmony_ci#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED */ 3564a8e1175bSopenharmony_ci 3565a8e1175bSopenharmony_ci#if defined(MBEDTLS_SSL_SESSION_TICKETS) 3566a8e1175bSopenharmony_ci case MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET: 3567a8e1175bSopenharmony_ci ret = ssl_tls13_write_new_session_ticket(ssl); 3568a8e1175bSopenharmony_ci if (ret != 0) { 3569a8e1175bSopenharmony_ci MBEDTLS_SSL_DEBUG_RET(1, 3570a8e1175bSopenharmony_ci "ssl_tls13_write_new_session_ticket ", 3571a8e1175bSopenharmony_ci ret); 3572a8e1175bSopenharmony_ci } 3573a8e1175bSopenharmony_ci break; 3574a8e1175bSopenharmony_ci case MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET_FLUSH: 3575a8e1175bSopenharmony_ci /* This state is necessary to do the flush of the New Session 3576a8e1175bSopenharmony_ci * Ticket message written in MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET 3577a8e1175bSopenharmony_ci * as part of ssl_prepare_handshake_step. 3578a8e1175bSopenharmony_ci */ 3579a8e1175bSopenharmony_ci ret = 0; 3580a8e1175bSopenharmony_ci 3581a8e1175bSopenharmony_ci if (ssl->handshake->new_session_tickets_count == 0) { 3582a8e1175bSopenharmony_ci mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_HANDSHAKE_OVER); 3583a8e1175bSopenharmony_ci } else { 3584a8e1175bSopenharmony_ci mbedtls_ssl_handshake_set_state( 3585a8e1175bSopenharmony_ci ssl, MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET); 3586a8e1175bSopenharmony_ci } 3587a8e1175bSopenharmony_ci break; 3588a8e1175bSopenharmony_ci 3589a8e1175bSopenharmony_ci#endif /* MBEDTLS_SSL_SESSION_TICKETS */ 3590a8e1175bSopenharmony_ci 3591a8e1175bSopenharmony_ci default: 3592a8e1175bSopenharmony_ci MBEDTLS_SSL_DEBUG_MSG(1, ("invalid state %d", ssl->state)); 3593a8e1175bSopenharmony_ci return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; 3594a8e1175bSopenharmony_ci } 3595a8e1175bSopenharmony_ci 3596a8e1175bSopenharmony_ci return ret; 3597a8e1175bSopenharmony_ci} 3598a8e1175bSopenharmony_ci 3599a8e1175bSopenharmony_ci#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_PROTO_TLS1_3 */ 3600