@@ -182,6 +182,29 @@ static psa_status_t psa_load_persistent_key_into_slot( psa_key_slot_t *p_slot )
182
182
psa_free_persistent_key_data ( key_data , key_data_length );
183
183
return ( status );
184
184
}
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
+ }
185
208
#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
186
209
187
210
/** 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,
209
232
psa_key_slot_t * slot ;
210
233
psa_status_t status ;
211
234
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 ) )
219
236
return ( PSA_ERROR_INVALID_ARGUMENT );
220
237
221
238
status = psa_get_key_slot ( handle , & slot );
0 commit comments