Consolidate invalid-handle tests

Consolidate the invalid-handle tests from test_suite_psa_crypto and
test_suite_psa_crypto_slot_management. Start with the code in
test_suite_psa_crypto_slot_management and adapt it to test one invalid
handle value per run of the test function.
This commit is contained in:
Gilles Peskine 2019-10-11 11:44:48 +02:00
parent 04129a0d96
commit b8cde4ec03
4 changed files with 60 additions and 67 deletions

View file

@ -22,24 +22,6 @@ persistence_attributes:0x1234:3:0x1235:0x1235:3
PSA key attributes: slot number
slot_number_attribute:
psa_destroy_key(0)
destroy_invalid:0:PSA_SUCCESS
psa_destroy_key(invalid)
destroy_invalid:1:PSA_ERROR_INVALID_HANDLE
psa_destroy_key(huge)
destroy_invalid:-1:PSA_ERROR_INVALID_HANDLE
psa_close_key(0)
close_invalid:0:PSA_SUCCESS
psa_close_key(invalid)
close_invalid:1:PSA_ERROR_INVALID_HANDLE
psa_close_key(huge)
close_invalid:-1:PSA_ERROR_INVALID_HANDLE
PSA import/export raw: 1 bytes
import_export:"2a":PSA_KEY_TYPE_RAW_DATA:PSA_KEY_USAGE_EXPORT:0:8:0:PSA_SUCCESS:1

View file

@ -1268,34 +1268,6 @@ void slot_number_attribute( )
}
/* END_CASE */
/* BEGIN_CASE */
void destroy_invalid( int handle_arg, int expected_status_arg )
{
psa_key_handle_t handle = handle_arg;
psa_status_t expected_status = expected_status_arg;
PSA_ASSERT( psa_crypto_init( ) );
TEST_EQUAL( psa_destroy_key( handle ), expected_status );
exit:
PSA_DONE( );
}
/* END_CASE */
/* BEGIN_CASE */
void close_invalid( int handle_arg, int expected_status_arg )
{
psa_key_handle_t handle = handle_arg;
psa_status_t expected_status = expected_status_arg;
PSA_ASSERT( psa_crypto_init( ) );
TEST_EQUAL( psa_close_key( handle ), expected_status );
exit:
PSA_DONE( );
}
/* END_CASE */
/* BEGIN_CASE */
void import_with_policy( int type_arg,
int usage_arg, int alg_arg,

View file

@ -148,8 +148,17 @@ Copy persistent to same
depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C
copy_to_occupied:PSA_KEY_LIFETIME_PERSISTENT:1:PSA_KEY_USAGE_EXPORT | PSA_KEY_USAGE_COPY:PSA_ALG_CTR:PSA_KEY_TYPE_AES:"404142434445464748494a4b4c4d4e4f":PSA_KEY_LIFETIME_PERSISTENT:1:PSA_KEY_USAGE_EXPORT:PSA_ALG_CTR:PSA_KEY_TYPE_AES:"404142434445464748494a4b4c4d4e4f"
Close/destroy invalid handle
invalid_handle:
invalid handle: 0
invalid_handle:INVALID_HANDLE_0:PSA_SUCCESS:PSA_ERROR_INVALID_HANDLE
invalid handle: never opened
invalid_handle:INVALID_HANDLE_UNOPENED:PSA_ERROR_INVALID_HANDLE:PSA_ERROR_INVALID_HANDLE
invalid handle: already closed
invalid_handle:INVALID_HANDLE_CLOSED:PSA_ERROR_INVALID_HANDLE:PSA_ERROR_INVALID_HANDLE
invalid handle: huge
invalid_handle:INVALID_HANDLE_HUGE:PSA_ERROR_INVALID_HANDLE:PSA_ERROR_INVALID_HANDLE
Open many transient handles
many_transient_handles:42

View file

@ -20,6 +20,14 @@ typedef enum
CLOSE_AFTER,
} reopen_policy_t;
typedef enum
{
INVALID_HANDLE_0,
INVALID_HANDLE_UNOPENED,
INVALID_HANDLE_CLOSED,
INVALID_HANDLE_HUGE,
} invalid_handle_construction_t;
/* All test functions that create persistent keys must call
* `TEST_USES_KEY_ID( key_id )` before creating a persistent key with this
* identifier, and must call psa_purge_key_storage() in their cleanup
@ -625,9 +633,13 @@ exit:
/* END_CASE */
/* BEGIN_CASE */
void invalid_handle( )
void invalid_handle( int handle_construction,
int close_status_arg, int usage_status_arg )
{
psa_key_handle_t handle1 = 0;
psa_key_handle_t valid_handle = 0;
psa_key_handle_t invalid_handle = 0;
psa_status_t close_status = close_status_arg;
psa_status_t usage_status = usage_status_arg;
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
uint8_t material[1] = "a";
@ -639,32 +651,50 @@ void invalid_handle( )
psa_set_key_algorithm( &attributes, 0 );
PSA_ASSERT( psa_import_key( &attributes,
material, sizeof( material ),
&handle1 ) );
TEST_ASSERT( handle1 != 0 );
&valid_handle ) );
TEST_ASSERT( valid_handle != 0 );
/* Attempt to close and destroy some invalid handles. */
if( handle1 - 1 != 0 )
/* Construct an invalid handle as specified in the test case data. */
switch( handle_construction )
{
TEST_EQUAL( psa_close_key( handle1 - 1 ), PSA_ERROR_INVALID_HANDLE );
TEST_EQUAL( psa_destroy_key( handle1 - 1 ), PSA_ERROR_INVALID_HANDLE );
}
if( handle1 + 1 != 0 )
{
TEST_EQUAL( psa_close_key( handle1 + 1 ), PSA_ERROR_INVALID_HANDLE );
TEST_EQUAL( psa_destroy_key( handle1 + 1 ), PSA_ERROR_INVALID_HANDLE );
case INVALID_HANDLE_0:
invalid_handle = 0;
break;
case INVALID_HANDLE_UNOPENED:
/* We can't easily construct a handle that's never been opened
* without knowing how the implementation constructs handle
* values. The current test code assumes that valid handles
* are in a range between 1 and some maximum. */
if( valid_handle == 1 )
invalid_handle = 2;
else
invalid_handle = valid_handle - 1;
break;
case INVALID_HANDLE_CLOSED:
PSA_ASSERT( psa_import_key( &attributes,
material, sizeof( material ),
&invalid_handle ) );
PSA_ASSERT( psa_destroy_key( invalid_handle ) );
break;
case INVALID_HANDLE_HUGE:
invalid_handle = (psa_key_handle_t) ( -1 );
break;
default:
TEST_ASSERT( ! "unknown handle construction" );
}
/* 0 is special: it isn't a valid handle, but close/destroy
* succeeds on it. */
TEST_EQUAL( psa_close_key( 0 ), PSA_SUCCESS );
TEST_EQUAL( psa_destroy_key( 0 ), PSA_SUCCESS );
/* Attempt to use the invalid handle. */
TEST_EQUAL( psa_get_key_attributes( invalid_handle, &attributes ),
usage_status );
TEST_EQUAL( psa_close_key( invalid_handle ), close_status );
TEST_EQUAL( psa_destroy_key( invalid_handle ), close_status );
/* After all this, check that the original handle is intact. */
PSA_ASSERT( psa_get_key_attributes( handle1, &attributes ) );
PSA_ASSERT( psa_get_key_attributes( valid_handle, &attributes ) );
TEST_EQUAL( psa_get_key_type( &attributes ), PSA_KEY_TYPE_RAW_DATA );
TEST_EQUAL( psa_get_key_bits( &attributes ),
PSA_BYTES_TO_BITS( sizeof( material ) ) );
PSA_ASSERT( psa_close_key( handle1 ) );
PSA_ASSERT( psa_close_key( valid_handle ) );
exit:
PSA_DONE( );