Skip to content

Commit

Permalink
Merge pull request Mbed-TLS#6194 from xkqian/tls13_add_psk_client_cases
Browse files Browse the repository at this point in the history
TLS 1.3: Add PSK client cases
  • Loading branch information
ronald-cron-arm authored Sep 28, 2022
2 parents e3358e1 + a70bd91 commit 77c691f
Show file tree
Hide file tree
Showing 4 changed files with 1,463 additions and 50 deletions.
35 changes: 31 additions & 4 deletions library/ssl_tls13_client.c
Original file line number Diff line number Diff line change
Expand Up @@ -1327,11 +1327,11 @@ static int ssl_tls13_parse_server_pre_shared_key_ext( mbedtls_ssl_context *ssl,
int ret = 0;
size_t selected_identity;

int psk_type;
const unsigned char *psk;
size_t psk_len;
const unsigned char *psk_identity;
size_t psk_identity_len;
int psk_type;

/* Check which PSK we've offered.
*
Expand Down Expand Up @@ -1667,6 +1667,23 @@ static int ssl_tls13_parse_server_hello( mbedtls_ssl_context *ssl,
return( ret );
}

#if defined(MBEDTLS_DEBUG_C)
static const char *ssl_tls13_get_kex_mode_str(int mode)
{
switch( mode )
{
case MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK:
return "psk";
case MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL:
return "ephemeral";
case MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL:
return "psk_ephemeral";
default:
return "unknown mode";
}
}
#endif /* MBEDTLS_DEBUG_C */

MBEDTLS_CHECK_RETURN_CRITICAL
static int ssl_tls13_postprocess_server_hello( mbedtls_ssl_context *ssl )
{
Expand All @@ -1687,19 +1704,16 @@ static int ssl_tls13_postprocess_server_hello( mbedtls_ssl_context *ssl )
/* Only the pre_shared_key extension was received */
case MBEDTLS_SSL_EXT_PRE_SHARED_KEY:
handshake->key_exchange_mode = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK;
MBEDTLS_SSL_DEBUG_MSG( 2, ( "key exchange mode: psk" ) );
break;

/* Only the key_share extension was received */
case MBEDTLS_SSL_EXT_KEY_SHARE:
handshake->key_exchange_mode = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL;
MBEDTLS_SSL_DEBUG_MSG( 2, ( "key exchange mode: ephemeral" ) );
break;

/* Both the pre_shared_key and key_share extensions were received */
case ( MBEDTLS_SSL_EXT_PRE_SHARED_KEY | MBEDTLS_SSL_EXT_KEY_SHARE ):
handshake->key_exchange_mode = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_PSK_EPHEMERAL;
MBEDTLS_SSL_DEBUG_MSG( 2, ( "key exchange mode: psk_ephemeral" ) );
break;

/* Neither pre_shared_key nor key_share extension was received */
Expand All @@ -1709,6 +1723,19 @@ static int ssl_tls13_postprocess_server_hello( mbedtls_ssl_context *ssl )
goto cleanup;
}

if( !mbedtls_ssl_conf_tls13_check_kex_modes( ssl, handshake->key_exchange_mode ) )
{
ret = MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
MBEDTLS_SSL_DEBUG_MSG( 2,
( "Key exchange mode(%s) is not supported.",
ssl_tls13_get_kex_mode_str( handshake->key_exchange_mode ) ) );
goto cleanup;
}

MBEDTLS_SSL_DEBUG_MSG( 3,
( "Selected key exchange mode: %s",
ssl_tls13_get_kex_mode_str( handshake->key_exchange_mode ) ) );

/* Start the TLS 1.3 key schedule: Set the PSK and derive early secret.
*
* TODO: We don't have to do this in case we offered 0-RTT and the
Expand Down
1 change: 1 addition & 0 deletions tests/data_files/simplepass.psk
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0a0b0c:010203
Loading

0 comments on commit 77c691f

Please sign in to comment.