Skip to content

Commit 150d577

Browse files
Merge pull request #292 from gilles-peskine-arm/psa-destroy_0
Make psa_close_key(0) and psa_destroy_key(0) succeed
2 parents 3cdb3da + b8cde4e commit 150d577

7 files changed

+77
-39
lines changed

include/psa/crypto.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -459,9 +459,12 @@ psa_status_t psa_open_key(psa_key_id_t id,
459459
* maintain the key handle until after the multipart operation has finished.
460460
*
461461
* \param handle The key handle to close.
462+
* If this is \c 0, do nothing and return \c PSA_SUCCESS.
462463
*
463464
* \retval #PSA_SUCCESS
465+
* \p handle was a valid handle or \c 0. It is now closed.
464466
* \retval #PSA_ERROR_INVALID_HANDLE
467+
* \p handle is not a valid handle nor \c 0.
465468
* \retval #PSA_ERROR_COMMUNICATION_FAILURE
466469
* \retval #PSA_ERROR_CORRUPTION_DETECTED
467470
* \retval #PSA_ERROR_BAD_STATE
@@ -579,13 +582,17 @@ psa_status_t psa_copy_key(psa_key_handle_t source_handle,
579582
* key will cause the multipart operation to fail.
580583
*
581584
* \param handle Handle to the key to erase.
585+
* If this is \c 0, do nothing and return \c PSA_SUCCESS.
582586
*
583587
* \retval #PSA_SUCCESS
584-
* The key material has been erased.
588+
* \p handle was a valid handle and the key material that it
589+
* referred to has been erased.
590+
* Alternatively, \p handle is \c 0.
585591
* \retval #PSA_ERROR_NOT_PERMITTED
586592
* The key cannot be erased because it is
587593
* read-only, either due to a policy or due to physical restrictions.
588594
* \retval #PSA_ERROR_INVALID_HANDLE
595+
* \p handle is not a valid handle nor \c 0.
589596
* \retval #PSA_ERROR_COMMUNICATION_FAILURE
590597
* There was an failure in communication with the cryptoprocessor.
591598
* The key material may still be present in the cryptoprocessor.

library/psa_crypto.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1013,6 +1013,9 @@ psa_status_t psa_destroy_key( psa_key_handle_t handle )
10131013
psa_se_drv_table_entry_t *driver;
10141014
#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
10151015

1016+
if( handle == 0 )
1017+
return( PSA_SUCCESS );
1018+
10161019
status = psa_get_key_slot( handle, &slot );
10171020
if( status != PSA_SUCCESS )
10181021
return( status );

library/psa_crypto_slot_management.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,9 @@ psa_status_t psa_close_key( psa_key_handle_t handle )
255255
psa_status_t status;
256256
psa_key_slot_t *slot;
257257

258+
if( handle == 0 )
259+
return( PSA_SUCCESS );
260+
258261
status = psa_get_key_slot( handle, &slot );
259262
if( status != PSA_SUCCESS )
260263
return( status );

tests/suites/test_suite_psa_crypto.data

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -43,15 +43,6 @@ PSA import/export AES-256
4343
depends_on:MBEDTLS_AES_C
4444
import_export:"0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef":PSA_KEY_TYPE_AES:PSA_KEY_USAGE_EXPORT:PSA_ALG_CTR:256:0:PSA_SUCCESS:1
4545

46-
PSA invalid handle (0)
47-
invalid_handle:0
48-
49-
PSA invalid handle (smallest plausible handle)
50-
invalid_handle:1
51-
52-
PSA invalid handle (largest plausible handle)
53-
invalid_handle:-1
54-
5546
PSA import: bad usage flag
5647
import_with_policy:PSA_KEY_TYPE_RAW_DATA:0x40000000:0:PSA_ERROR_INVALID_ARGUMENT
5748

tests/suites/test_suite_psa_crypto.function

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1103,9 +1103,6 @@ static int test_operations_on_invalid_handle( psa_key_handle_t handle )
11031103
buffer, sizeof( buffer ), &length ),
11041104
PSA_ERROR_INVALID_HANDLE );
11051105

1106-
TEST_EQUAL( psa_close_key( handle ), PSA_ERROR_INVALID_HANDLE );
1107-
TEST_EQUAL( psa_destroy_key( handle ), PSA_ERROR_INVALID_HANDLE );
1108-
11091106
ok = 1;
11101107

11111108
exit:
@@ -1535,17 +1532,6 @@ exit:
15351532
}
15361533
/* END_CASE */
15371534

1538-
/* BEGIN_CASE */
1539-
void invalid_handle( int handle )
1540-
{
1541-
PSA_ASSERT( psa_crypto_init( ) );
1542-
test_operations_on_invalid_handle( handle );
1543-
1544-
exit:
1545-
PSA_DONE( );
1546-
}
1547-
/* END_CASE */
1548-
15491535
/* BEGIN_CASE */
15501536
void import_export_public_key( data_t *data,
15511537
int type_arg,

tests/suites/test_suite_psa_crypto_slot_management.data

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,8 +148,17 @@ Copy persistent to same
148148
depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C
149149
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"
150150

151-
Close/destroy invalid handle
152-
invalid_handle:
151+
invalid handle: 0
152+
invalid_handle:INVALID_HANDLE_0:PSA_SUCCESS:PSA_ERROR_INVALID_HANDLE
153+
154+
invalid handle: never opened
155+
invalid_handle:INVALID_HANDLE_UNOPENED:PSA_ERROR_INVALID_HANDLE:PSA_ERROR_INVALID_HANDLE
156+
157+
invalid handle: already closed
158+
invalid_handle:INVALID_HANDLE_CLOSED:PSA_ERROR_INVALID_HANDLE:PSA_ERROR_INVALID_HANDLE
159+
160+
invalid handle: huge
161+
invalid_handle:INVALID_HANDLE_HUGE:PSA_ERROR_INVALID_HANDLE:PSA_ERROR_INVALID_HANDLE
153162

154163
Open many transient handles
155164
many_transient_handles:42

tests/suites/test_suite_psa_crypto_slot_management.function

Lines changed: 52 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,14 @@ typedef enum
2020
CLOSE_AFTER,
2121
} reopen_policy_t;
2222

23+
typedef enum
24+
{
25+
INVALID_HANDLE_0,
26+
INVALID_HANDLE_UNOPENED,
27+
INVALID_HANDLE_CLOSED,
28+
INVALID_HANDLE_HUGE,
29+
} invalid_handle_construction_t;
30+
2331
/* All test functions that create persistent keys must call
2432
* `TEST_USES_KEY_ID( key_id )` before creating a persistent key with this
2533
* identifier, and must call psa_purge_key_storage() in their cleanup
@@ -625,9 +633,13 @@ exit:
625633
/* END_CASE */
626634

627635
/* BEGIN_CASE */
628-
void invalid_handle( )
636+
void invalid_handle( int handle_construction,
637+
int close_status_arg, int usage_status_arg )
629638
{
630-
psa_key_handle_t handle1 = 0;
639+
psa_key_handle_t valid_handle = 0;
640+
psa_key_handle_t invalid_handle = 0;
641+
psa_status_t close_status = close_status_arg;
642+
psa_status_t usage_status = usage_status_arg;
631643
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
632644
uint8_t material[1] = "a";
633645

@@ -639,23 +651,50 @@ void invalid_handle( )
639651
psa_set_key_algorithm( &attributes, 0 );
640652
PSA_ASSERT( psa_import_key( &attributes,
641653
material, sizeof( material ),
642-
&handle1 ) );
643-
TEST_ASSERT( handle1 != 0 );
654+
&valid_handle ) );
655+
TEST_ASSERT( valid_handle != 0 );
644656

645-
/* Attempt to close and destroy some invalid handles. */
646-
TEST_EQUAL( psa_close_key( 0 ), PSA_ERROR_INVALID_HANDLE );
647-
TEST_EQUAL( psa_close_key( handle1 - 1 ), PSA_ERROR_INVALID_HANDLE );
648-
TEST_EQUAL( psa_close_key( handle1 + 1 ), PSA_ERROR_INVALID_HANDLE );
649-
TEST_EQUAL( psa_destroy_key( 0 ), PSA_ERROR_INVALID_HANDLE );
650-
TEST_EQUAL( psa_destroy_key( handle1 - 1 ), PSA_ERROR_INVALID_HANDLE );
651-
TEST_EQUAL( psa_destroy_key( handle1 + 1 ), PSA_ERROR_INVALID_HANDLE );
657+
/* Construct an invalid handle as specified in the test case data. */
658+
switch( handle_construction )
659+
{
660+
case INVALID_HANDLE_0:
661+
invalid_handle = 0;
662+
break;
663+
case INVALID_HANDLE_UNOPENED:
664+
/* We can't easily construct a handle that's never been opened
665+
* without knowing how the implementation constructs handle
666+
* values. The current test code assumes that valid handles
667+
* are in a range between 1 and some maximum. */
668+
if( valid_handle == 1 )
669+
invalid_handle = 2;
670+
else
671+
invalid_handle = valid_handle - 1;
672+
break;
673+
case INVALID_HANDLE_CLOSED:
674+
PSA_ASSERT( psa_import_key( &attributes,
675+
material, sizeof( material ),
676+
&invalid_handle ) );
677+
PSA_ASSERT( psa_destroy_key( invalid_handle ) );
678+
break;
679+
case INVALID_HANDLE_HUGE:
680+
invalid_handle = (psa_key_handle_t) ( -1 );
681+
break;
682+
default:
683+
TEST_ASSERT( ! "unknown handle construction" );
684+
}
685+
686+
/* Attempt to use the invalid handle. */
687+
TEST_EQUAL( psa_get_key_attributes( invalid_handle, &attributes ),
688+
usage_status );
689+
TEST_EQUAL( psa_close_key( invalid_handle ), close_status );
690+
TEST_EQUAL( psa_destroy_key( invalid_handle ), close_status );
652691

653692
/* After all this, check that the original handle is intact. */
654-
PSA_ASSERT( psa_get_key_attributes( handle1, &attributes ) );
693+
PSA_ASSERT( psa_get_key_attributes( valid_handle, &attributes ) );
655694
TEST_EQUAL( psa_get_key_type( &attributes ), PSA_KEY_TYPE_RAW_DATA );
656695
TEST_EQUAL( psa_get_key_bits( &attributes ),
657696
PSA_BYTES_TO_BITS( sizeof( material ) ) );
658-
PSA_ASSERT( psa_close_key( handle1 ) );
697+
PSA_ASSERT( psa_close_key( valid_handle ) );
659698

660699
exit:
661700
PSA_DONE( );

0 commit comments

Comments
 (0)