Skip to content

Commit cebb8f9

Browse files
Move key id validity check into its own function
1 parent 9654e11 commit cebb8f9

File tree

1 file changed

+24
-7
lines changed

1 file changed

+24
-7
lines changed

library/psa_crypto_slot_management.c

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,29 @@ static psa_status_t psa_load_persistent_key_into_slot( psa_key_slot_t *p_slot )
182182
psa_free_persistent_key_data( key_data, key_data_length );
183183
return( status );
184184
}
185+
186+
/** Check whether a key identifier is acceptable.
187+
*
188+
* For backward compatibility, key identifiers that were valid in a
189+
* past released version must remain valid, unless a migration path
190+
* is provided.
191+
*
192+
* \param key_id The key identifier to check.
193+
*
194+
* \return 1 if \p key_id is acceptable, otherwise 0.
195+
*/
196+
static int psa_is_key_id_valid( psa_key_id_t key_id )
197+
{
198+
/* Reject id=0 because by general library conventions, 0 is an invalid
199+
* value wherever possible. */
200+
if( key_id == 0 )
201+
return( 0 );
202+
/* Reject high values because the file names are reserved for the
203+
* library's internal use. */
204+
if( key_id >= PSA_MAX_PERSISTENT_KEY_IDENTIFIER )
205+
return( 0 );
206+
return( 1 );
207+
}
185208
#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
186209

187210
/** Declare a slot as persistent and load it from storage.
@@ -209,13 +232,7 @@ static psa_status_t psa_internal_make_key_persistent( psa_key_handle_t handle,
209232
psa_key_slot_t *slot;
210233
psa_status_t status;
211234

212-
/* Reject id=0 because by general library conventions, 0 is an invalid
213-
* value wherever possible. */
214-
if( id == 0 )
215-
return( PSA_ERROR_INVALID_ARGUMENT );
216-
/* Reject high values because the file names are reserved for the
217-
* library's internal use. */
218-
if( id >= PSA_MAX_PERSISTENT_KEY_IDENTIFIER )
235+
if( ! psa_is_key_id_valid( id ) )
219236
return( PSA_ERROR_INVALID_ARGUMENT );
220237

221238
status = psa_get_key_slot( handle, &slot );

0 commit comments

Comments
 (0)