@@ -920,6 +920,55 @@ static int wolfPKCS11_Store_GetMaxSize(int type, int variableSz)
920920
921921/* Functions that handle storing data. */
922922
923+ int wolfPKCS11_Store_Remove (int type , CK_ULONG id1 , CK_ULONG id2 )
924+ {
925+ int ret ;
926+ #ifndef WOLFPKCS11_NO_ENV
927+ const char * str = NULL ;
928+ #endif
929+ #ifdef WOLFPKCS11_TPM_STORE
930+ WP11_TpmStore * tpmStore = & tpmStores [0 ];
931+ word32 nvIndex ;
932+ WOLFTPM2_HANDLE parent ;
933+ #else
934+ void * storage = NULL ;
935+ #endif
936+
937+ #ifdef WOLFPKCS11_DEBUG_STORE
938+ printf ("Store remove: Type %d, id1 %ld, id2 %ld\n" , type , id1 , id2 );
939+ #endif
940+
941+ #ifndef WOLFPKCS11_NO_ENV
942+ str = XGETENV ("WOLFPKCS11_NO_STORE" );
943+ if (str != NULL ) {
944+ return NOT_AVAILABLE_E ;
945+ }
946+ #endif
947+
948+ #ifdef WOLFPKCS11_TPM_STORE
949+ /* Build unique handle */
950+ nvIndex = WOLFPKCS11_TPM_NV_BASE +
951+ ((type & 0x0F ) << 16 ) +
952+ (((word32 )id1 & 0xFF ) << 8 ) +
953+ ((word32 )id2 & 0xFF );
954+
955+ XMEMSET (& parent , 0 , sizeof (parent ));
956+ parent .hndl = WOLFPKCS11_TPM_AUTH_TYPE ;
957+ ret = wolfTPM2_NVDeleteAuth (tpmStore -> dev , & parent , nvIndex );
958+ if (ret != 0 ) {
959+ printf ("Error %d (%s) removing NV handle 0x%x: \n" ,
960+ ret , wolfTPM2_GetRCString (ret ), nvIndex );
961+ }
962+ #else
963+ /* truncate the storage file */
964+ ret = wolfPKCS11_Store_Open (type , id1 , id2 , 0 , & storage );
965+ if (ret == 0 ) {
966+ wolfPKCS11_Store_Close (storage );
967+ }
968+ #endif
969+ return ret ;
970+ }
971+
923972/**
924973 * Opens access to location to read/write token data.
925974 *
@@ -1376,6 +1425,11 @@ static int wp11_storage_open(int type, CK_ULONG id1, CK_ULONG id2,
13761425#endif
13771426}
13781427
1428+ static int wp11_storage_remove (int type , CK_ULONG id1 , CK_ULONG id2 )
1429+ {
1430+ return wolfPKCS11_Store_Remove (type , id1 , id2 );
1431+ }
1432+
13791433/*
13801434 * Closes access to location being read or written.
13811435 * Any dynamic memory associated with the store is freed here.
@@ -2844,11 +2898,10 @@ static int wp11_Object_Store_RsaKey(WP11_Object* object, int tokenId, int objId)
28442898
28452899 if (object -> keyData == NULL ) {
28462900 ret = wp11_Object_Encode_RsaKey (object );
2901+ if (ret != 0 )
2902+ return ret ;
28472903 }
28482904
2849- if (ret != 0 )
2850- return ret ;
2851-
28522905 /* Determine store type - private keys may be encrypted. */
28532906 if (object -> objClass == CKO_PRIVATE_KEY )
28542907 storeType = WOLFPKCS11_STORE_RSAKEY_PRIV ;
@@ -3064,11 +3117,10 @@ static int wp11_Object_Store_EccKey(WP11_Object* object, int tokenId, int objId)
30643117
30653118 if (object -> keyData == NULL ) {
30663119 ret = wp11_Object_Encode_EccKey (object );
3120+ if (ret != 0 )
3121+ return ret ;
30673122 }
30683123
3069- if (ret != 0 )
3070- return ret ;
3071-
30723124 /* Determine store type - private keys may be encrypted. */
30733125 if (object -> objClass == CKO_PRIVATE_KEY )
30743126 storeType = WOLFPKCS11_STORE_ECCKEY_PRIV ;
@@ -4002,12 +4054,10 @@ static int wp11_Object_Encode(WP11_Object* object, int protect)
40024054 */
40034055static void wp11_Object_Unstore (WP11_Object * object , int tokenId , int objId )
40044056{
4005- void * storage = NULL ;
40064057 int storeObjType = -1 ;
40074058
4008- /* Open access to key object. */
4009- wp11_storage_open (WOLFPKCS11_STORE_OBJECT , tokenId , objId , 0 , & storage );
4010- wp11_storage_close (storage );
4059+ /* Remove store and key object */
4060+ wp11_storage_remove (WOLFPKCS11_STORE_OBJECT , tokenId , objId );
40114061
40124062 /* CKK_* and CKC_* values overlap, check for cert separately */
40134063 if (object -> objClass == CKO_CERTIFICATE ) {
@@ -4053,8 +4103,7 @@ static void wp11_Object_Unstore(WP11_Object* object, int tokenId, int objId)
40534103 break ;
40544104 }
40554105 }
4056- wp11_storage_open (storeObjType , tokenId , objId , 0 , & storage );
4057- wp11_storage_close (storage );
4106+ wp11_storage_remove (storeObjType , tokenId , objId );
40584107}
40594108#endif /* !WOLFPKCS11_NO_STORE */
40604109
0 commit comments