@@ -20,6 +20,14 @@ typedef enum
20
20
CLOSE_AFTER,
21
21
} reopen_policy_t;
22
22
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
+
23
31
/* All test functions that create persistent keys must call
24
32
* `TEST_USES_KEY_ID( key_id )` before creating a persistent key with this
25
33
* identifier, and must call psa_purge_key_storage() in their cleanup
@@ -625,9 +633,13 @@ exit:
625
633
/* END_CASE */
626
634
627
635
/* BEGIN_CASE */
628
- void invalid_handle( )
636
+ void invalid_handle( int handle_construction,
637
+ int close_status_arg, int usage_status_arg )
629
638
{
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;
631
643
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
632
644
uint8_t material[1] = "a";
633
645
@@ -639,32 +651,50 @@ void invalid_handle( )
639
651
psa_set_key_algorithm( &attributes, 0 );
640
652
PSA_ASSERT( psa_import_key( &attributes,
641
653
material, sizeof( material ),
642
- &handle1 ) );
643
- TEST_ASSERT( handle1 != 0 );
654
+ &valid_handle ) );
655
+ TEST_ASSERT( valid_handle != 0 );
644
656
645
- /* Attempt to close and destroy some invalid handles. */
646
- if( handle1 - 1 != 0 )
647
- {
648
- TEST_EQUAL( psa_close_key( handle1 - 1 ), PSA_ERROR_INVALID_HANDLE );
649
- TEST_EQUAL( psa_destroy_key( handle1 - 1 ), PSA_ERROR_INVALID_HANDLE );
650
- }
651
- if( handle1 + 1 != 0 )
657
+ /* Construct an invalid handle as specified in the test case data. */
658
+ switch( handle_construction )
652
659
{
653
- TEST_EQUAL( psa_close_key( handle1 + 1 ), PSA_ERROR_INVALID_HANDLE );
654
- TEST_EQUAL( psa_destroy_key( handle1 + 1 ), PSA_ERROR_INVALID_HANDLE );
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" );
655
684
}
656
685
657
- /* 0 is special: it isn't a valid handle, but close/destroy
658
- * succeeds on it. */
659
- TEST_EQUAL( psa_close_key( 0 ), PSA_SUCCESS );
660
- TEST_EQUAL( psa_destroy_key( 0 ), PSA_SUCCESS );
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 );
661
691
662
692
/* After all this, check that the original handle is intact. */
663
- PSA_ASSERT( psa_get_key_attributes( handle1 , &attributes ) );
693
+ PSA_ASSERT( psa_get_key_attributes( valid_handle , &attributes ) );
664
694
TEST_EQUAL( psa_get_key_type( &attributes ), PSA_KEY_TYPE_RAW_DATA );
665
695
TEST_EQUAL( psa_get_key_bits( &attributes ),
666
696
PSA_BYTES_TO_BITS( sizeof( material ) ) );
667
- PSA_ASSERT( psa_close_key( handle1 ) );
697
+ PSA_ASSERT( psa_close_key( valid_handle ) );
668
698
669
699
exit:
670
700
PSA_DONE( );
0 commit comments