mirror of
https://github.com/Mbed-TLS/mbedtls.git
synced 2025-12-24 17:41:01 +03:00
Merge pull request #5791 from superna9999/5788-unify-non-opaque-and-opaque-psks
Unify non-opaque and opaque PSKs
This commit is contained in:
@@ -1528,7 +1528,6 @@ static int ssl_conf_psk_is_configured( mbedtls_ssl_config const *conf )
|
||||
if( !mbedtls_svc_key_id_is_null( conf->psk_opaque ) )
|
||||
return( 1 );
|
||||
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
||||
|
||||
if( conf->psk != NULL )
|
||||
return( 1 );
|
||||
|
||||
@@ -1545,11 +1544,6 @@ static void ssl_conf_remove_psk( mbedtls_ssl_config *conf )
|
||||
* user's responsibility. */
|
||||
conf->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
|
||||
}
|
||||
/* This and the following branch should never
|
||||
* be taken simultaenously as we maintain the
|
||||
* invariant that raw and opaque PSKs are never
|
||||
* configured simultaneously. As a safeguard,
|
||||
* though, `else` is omitted here. */
|
||||
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
||||
if( conf->psk != NULL )
|
||||
{
|
||||
@@ -1631,10 +1625,16 @@ static void ssl_remove_psk( mbedtls_ssl_context *ssl )
|
||||
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
||||
if( ! mbedtls_svc_key_id_is_null( ssl->handshake->psk_opaque ) )
|
||||
{
|
||||
/* The maintenance of the external PSK key slot is the
|
||||
* user's responsibility. */
|
||||
if( ssl->handshake->psk_opaque_is_internal )
|
||||
{
|
||||
psa_destroy_key( ssl->handshake->psk_opaque );
|
||||
ssl->handshake->psk_opaque_is_internal = 0;
|
||||
}
|
||||
ssl->handshake->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
|
||||
}
|
||||
else
|
||||
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
||||
#else
|
||||
if( ssl->handshake->psk != NULL )
|
||||
{
|
||||
mbedtls_platform_zeroize( ssl->handshake->psk,
|
||||
@@ -1642,11 +1642,19 @@ static void ssl_remove_psk( mbedtls_ssl_context *ssl )
|
||||
mbedtls_free( ssl->handshake->psk );
|
||||
ssl->handshake->psk_len = 0;
|
||||
}
|
||||
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
||||
}
|
||||
|
||||
int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
|
||||
const unsigned char *psk, size_t psk_len )
|
||||
{
|
||||
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
||||
psa_key_attributes_t key_attributes = psa_key_attributes_init();
|
||||
psa_status_t status;
|
||||
psa_algorithm_t alg;
|
||||
mbedtls_svc_key_id_t key;
|
||||
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
||||
|
||||
if( psk == NULL || ssl->handshake == NULL )
|
||||
return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
|
||||
|
||||
@@ -1655,6 +1663,24 @@ int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
|
||||
|
||||
ssl_remove_psk( ssl );
|
||||
|
||||
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
||||
if( ssl->handshake->ciphersuite_info->mac == MBEDTLS_MD_SHA384)
|
||||
alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384);
|
||||
else
|
||||
alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256);
|
||||
|
||||
psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_DERIVE );
|
||||
psa_set_key_algorithm( &key_attributes, alg );
|
||||
psa_set_key_type( &key_attributes, PSA_KEY_TYPE_DERIVE );
|
||||
|
||||
status = psa_import_key( &key_attributes, psk, psk_len, &key );
|
||||
if( status != PSA_SUCCESS )
|
||||
return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
|
||||
|
||||
/* Allow calling psa_destroy_key() on psk remove */
|
||||
ssl->handshake->psk_opaque_is_internal = 1;
|
||||
return mbedtls_ssl_set_hs_psk_opaque( ssl, key );
|
||||
#else
|
||||
if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
|
||||
return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
|
||||
|
||||
@@ -1662,6 +1688,7 @@ int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
|
||||
memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
|
||||
|
||||
return( 0 );
|
||||
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
||||
}
|
||||
|
||||
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
||||
@@ -3231,11 +3258,25 @@ void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
|
||||
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
||||
if( ! mbedtls_svc_key_id_is_null( ssl->handshake->psk_opaque ) )
|
||||
{
|
||||
/* The maintenance of the external PSK key slot is the
|
||||
* user's responsibility. */
|
||||
if( ssl->handshake->psk_opaque_is_internal )
|
||||
{
|
||||
psa_destroy_key( ssl->handshake->psk_opaque );
|
||||
ssl->handshake->psk_opaque_is_internal = 0;
|
||||
}
|
||||
ssl->handshake->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
|
||||
}
|
||||
#else
|
||||
if( handshake->psk != NULL )
|
||||
{
|
||||
mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
|
||||
mbedtls_free( handshake->psk );
|
||||
}
|
||||
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
|
||||
@@ -4424,6 +4465,12 @@ void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
|
||||
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
||||
if( ! mbedtls_svc_key_id_is_null( conf->psk_opaque ) )
|
||||
{
|
||||
conf->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
|
||||
}
|
||||
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
||||
if( conf->psk != NULL )
|
||||
{
|
||||
mbedtls_platform_zeroize( conf->psk, conf->psk_len );
|
||||
@@ -4812,6 +4859,7 @@ int mbedtls_ssl_get_handshake_transcript( mbedtls_ssl_context *ssl,
|
||||
static psa_status_t setup_psa_key_derivation( psa_key_derivation_operation_t* derivation,
|
||||
mbedtls_svc_key_id_t key,
|
||||
psa_algorithm_t alg,
|
||||
const unsigned char* raw_psk, size_t raw_psk_length,
|
||||
const unsigned char* seed, size_t seed_length,
|
||||
const unsigned char* label, size_t label_length,
|
||||
const unsigned char* other_secret,
|
||||
@@ -4845,7 +4893,7 @@ static psa_status_t setup_psa_key_derivation( psa_key_derivation_operation_t* de
|
||||
{
|
||||
status = psa_key_derivation_input_bytes(
|
||||
derivation, PSA_KEY_DERIVATION_INPUT_SECRET,
|
||||
NULL, 0 );
|
||||
raw_psk, raw_psk_length );
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -4911,6 +4959,7 @@ static int tls_prf_generic( mbedtls_md_type_t md_type,
|
||||
|
||||
status = setup_psa_key_derivation( &derivation,
|
||||
master_key, alg,
|
||||
NULL, 0,
|
||||
random, rlen,
|
||||
(unsigned char const *) label,
|
||||
(size_t) strlen( label ),
|
||||
@@ -5100,30 +5149,6 @@ static int ssl_set_handshake_prfs( mbedtls_ssl_handshake_params *handshake,
|
||||
return( 0 );
|
||||
}
|
||||
|
||||
|
||||
|
||||
#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
|
||||
defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED )
|
||||
static int ssl_use_opaque_psk( mbedtls_ssl_context const *ssl )
|
||||
{
|
||||
if( ssl->conf->f_psk != NULL )
|
||||
{
|
||||
/* If we've used a callback to select the PSK,
|
||||
* the static configuration is irrelevant. */
|
||||
if( ! mbedtls_svc_key_id_is_null( ssl->handshake->psk_opaque ) )
|
||||
return( 1 );
|
||||
|
||||
return( 0 );
|
||||
}
|
||||
|
||||
if( ! mbedtls_svc_key_id_is_null( ssl->conf->psk_opaque ) )
|
||||
return( 1 );
|
||||
|
||||
return( 0 );
|
||||
}
|
||||
#endif /* MBEDTLS_USE_PSA_CRYPTO &&
|
||||
MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
|
||||
|
||||
/*
|
||||
* Compute master secret if needed
|
||||
*
|
||||
@@ -5196,8 +5221,7 @@ static int ssl_compute_master( mbedtls_ssl_handshake_params *handshake,
|
||||
|
||||
#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
|
||||
defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
|
||||
if( mbedtls_ssl_ciphersuite_uses_psk( handshake->ciphersuite_info ) == 1 &&
|
||||
ssl_use_opaque_psk( ssl ) == 1 )
|
||||
if( mbedtls_ssl_ciphersuite_uses_psk( handshake->ciphersuite_info ) == 1 )
|
||||
{
|
||||
/* Perform PSK-to-MS expansion in a single step. */
|
||||
psa_status_t status;
|
||||
@@ -5240,6 +5264,7 @@ static int ssl_compute_master( mbedtls_ssl_handshake_params *handshake,
|
||||
}
|
||||
|
||||
status = setup_psa_key_derivation( &derivation, psk, alg,
|
||||
ssl->conf->psk, ssl->conf->psk_len,
|
||||
seed, seed_len,
|
||||
(unsigned char const *) lbl,
|
||||
(size_t) strlen( lbl ),
|
||||
@@ -5472,7 +5497,8 @@ void ssl_calc_verify_tls_sha384( const mbedtls_ssl_context *ssl,
|
||||
}
|
||||
#endif /* MBEDTLS_SHA384_C */
|
||||
|
||||
#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
|
||||
#if !defined(MBEDTLS_USE_PSA_CRYPTO) && \
|
||||
defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
|
||||
int mbedtls_ssl_psk_derive_premaster( mbedtls_ssl_context *ssl, mbedtls_key_exchange_type_t key_ex )
|
||||
{
|
||||
unsigned char *p = ssl->handshake->premaster;
|
||||
@@ -5481,11 +5507,6 @@ int mbedtls_ssl_psk_derive_premaster( mbedtls_ssl_context *ssl, mbedtls_key_exch
|
||||
size_t psk_len = 0;
|
||||
int psk_ret = mbedtls_ssl_get_psk( ssl, &psk, &psk_len );
|
||||
|
||||
#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
|
||||
defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
|
||||
(void) key_ex;
|
||||
#endif /* MBEDTLS_USE_PSA_CRYPTO && MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
|
||||
|
||||
if( psk_ret == MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED )
|
||||
{
|
||||
/*
|
||||
@@ -5560,19 +5581,10 @@ int mbedtls_ssl_psk_derive_premaster( mbedtls_ssl_context *ssl, mbedtls_key_exch
|
||||
p += 2 + len;
|
||||
|
||||
MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
|
||||
|
||||
/* For opaque PSK fill premaster with the the shared secret without PSK. */
|
||||
if( psk_ret == MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED )
|
||||
{
|
||||
MBEDTLS_SSL_DEBUG_MSG( 1,
|
||||
( "skip PMS generation for opaque DHE-PSK" ) );
|
||||
return( 0 );
|
||||
}
|
||||
}
|
||||
else
|
||||
#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
|
||||
#if !defined(MBEDTLS_USE_PSA_CRYPTO) && \
|
||||
defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
|
||||
#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
|
||||
if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
|
||||
{
|
||||
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
||||
@@ -5593,7 +5605,7 @@ int mbedtls_ssl_psk_derive_premaster( mbedtls_ssl_context *ssl, mbedtls_key_exch
|
||||
MBEDTLS_DEBUG_ECDH_Z );
|
||||
}
|
||||
else
|
||||
#endif /* !MBEDTLS_USE_PSA_CRYPTO && MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
|
||||
#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
|
||||
{
|
||||
MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
|
||||
return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
|
||||
@@ -5616,7 +5628,7 @@ int mbedtls_ssl_psk_derive_premaster( mbedtls_ssl_context *ssl, mbedtls_key_exch
|
||||
|
||||
return( 0 );
|
||||
}
|
||||
#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
|
||||
#endif /* !MBEDTLS_USE_PSA_CRYPTO && MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
|
||||
|
||||
#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
|
||||
static int ssl_write_hello_request( mbedtls_ssl_context *ssl );
|
||||
|
||||
Reference in New Issue
Block a user