diff --git a/library/ssl_misc.h b/library/ssl_misc.h index 6329e0d5e5..e13b3d9dad 100644 --- a/library/ssl_misc.h +++ b/library/ssl_misc.h @@ -632,6 +632,7 @@ struct mbedtls_ssl_handshake_params psa_key_type_t ecdh_psa_type; uint16_t ecdh_bits; mbedtls_svc_key_id_t ecdh_psa_privkey; + uint8_t ecdh_psa_shared_key; unsigned char ecdh_psa_peerkey[MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH]; size_t ecdh_psa_peerkey_len; #endif /* MBEDTLS_USE_PSA_CRYPTO || MBEDTLS_SSL_PROTO_TLS1_3 */ diff --git a/library/ssl_tls.c b/library/ssl_tls.c index 86445de247..cc7d7e143c 100644 --- a/library/ssl_tls.c +++ b/library/ssl_tls.c @@ -3146,7 +3146,8 @@ void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl ) #if defined(MBEDTLS_ECDH_C) && \ ( defined(MBEDTLS_USE_PSA_CRYPTO) || defined(MBEDTLS_SSL_PROTO_TLS1_3) ) - psa_destroy_key( handshake->ecdh_psa_privkey ); + if( handshake->ecdh_psa_shared_key == 0 ) + psa_destroy_key( handshake->ecdh_psa_privkey ); #endif /* MBEDTLS_ECDH_C && MBEDTLS_USE_PSA_CRYPTO */ #if defined(MBEDTLS_SSL_PROTO_TLS1_3) diff --git a/library/ssl_tls12_server.c b/library/ssl_tls12_server.c index 422f5cf697..a9d37c1a48 100644 --- a/library/ssl_tls12_server.c +++ b/library/ssl_tls12_server.c @@ -3946,18 +3946,22 @@ static int ssl_parse_client_key_exchange( mbedtls_ssl_context *ssl ) { ret = psa_ssl_status_to_mbedtls( status ); MBEDTLS_SSL_DEBUG_RET( 1, "psa_raw_key_agreement", ret ); - (void) psa_destroy_key( handshake->ecdh_psa_privkey ); + if( handshake->ecdh_psa_shared_key == 0 ) + (void) psa_destroy_key( handshake->ecdh_psa_privkey ); handshake->ecdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT; return( ret ); } - status = psa_destroy_key( handshake->ecdh_psa_privkey ); - - if( status != PSA_SUCCESS ) + if( handshake->ecdh_psa_shared_key == 0 ) { - ret = psa_ssl_status_to_mbedtls( status ); - MBEDTLS_SSL_DEBUG_RET( 1, "psa_destroy_key", ret ); - return( ret ); + status = psa_destroy_key( handshake->ecdh_psa_privkey ); + + if( status != PSA_SUCCESS ) + { + ret = psa_ssl_status_to_mbedtls( status ); + MBEDTLS_SSL_DEBUG_RET( 1, "psa_destroy_key", ret ); + return( ret ); + } } handshake->ecdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT; }