Skip to content

Commit dd77544

Browse files
committed
Finish fixes to resolve stray object store files.
1 parent 849f288 commit dd77544

3 files changed

Lines changed: 40 additions & 14 deletions

File tree

src/crypto.c

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -884,7 +884,7 @@ static CK_RV AddRSAPrivateKeyObject(WP11_Session* session,
884884
err_out:
885885
if (rv != CKR_OK) {
886886
if (*phKey != CK_INVALID_HANDLE) {
887-
WP11_Session_RemoveObject(session, privKeyObject);
887+
rv = WP11_Session_RemoveObject(session, privKeyObject);
888888
*phKey = CK_INVALID_HANDLE;
889889
}
890890
if (privKeyObject != NULL) {
@@ -1050,7 +1050,7 @@ CK_RV C_CreateObject(CK_SESSION_HANDLE hSession, CK_ATTRIBUTE_PTR pTemplate,
10501050
}
10511051
rv = AddObject(session, object, pTemplate, ulCount, phObject);
10521052
if (rv != CKR_OK) {
1053-
WP11_Session_RemoveObject(session, object);
1053+
rv = WP11_Session_RemoveObject(session, object);
10541054
WP11_Object_Free(object);
10551055
}
10561056

@@ -1232,10 +1232,9 @@ CK_RV C_DestroyObject(CK_SESSION_HANDLE hSession,
12321232
return rv;
12331233
}
12341234

1235-
WP11_Session_RemoveObject(session, obj);
1235+
rv = WP11_Session_RemoveObject(session, obj);
12361236
WP11_Object_Free(obj);
12371237

1238-
rv = CKR_OK;
12391238
WOLFPKCS11_LEAVE("C_DestroyObject", rv);
12401239
return rv;
12411240
}
@@ -6731,7 +6730,7 @@ CK_RV C_UnwrapKey(CK_SESSION_HANDLE hSession,
67316730
}
67326731
if (rv != CKR_OK) {
67336732
if (*phKey != CK_INVALID_HANDLE) {
6734-
WP11_Session_RemoveObject(session, keyObj);
6733+
rv = WP11_Session_RemoveObject(session, keyObj);
67356734
*phKey = CK_INVALID_HANDLE;
67366735
}
67376736
if (keyObj != NULL) {

src/internal.c

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -763,7 +763,8 @@ static void wp11_Session_Final(WP11_Session* session)
763763
if (session->inUse) {
764764
/* Free objects in session. */
765765
while ((obj = session->object) != NULL) {
766-
WP11_Session_RemoveObject(session, obj);
766+
/* ignore return value, logged in function */
767+
(void)WP11_Session_RemoveObject(session, obj);
767768
WP11_Object_Free(obj);
768769
}
769770
session->inUse = 0;
@@ -4260,7 +4261,7 @@ static int wp11_Object_Unstore(WP11_Object* object, int tokenId, int objId)
42604261
return BAD_FUNC_ARG;
42614262
}
42624263

4263-
/* Remove store and key object */
4264+
/* Remove store object */
42644265
ret = wp11_storage_remove(WOLFPKCS11_STORE_OBJECT, tokenId, objId);
42654266
if (ret != 0) {
42664267
return ret;
@@ -4310,6 +4311,7 @@ static int wp11_Object_Unstore(WP11_Object* object, int tokenId, int objId)
43104311
break;
43114312
}
43124313
}
4314+
/* remove actual object by type */
43134315
return wp11_storage_remove(storeObjType, tokenId, objId);
43144316
}
43154317
#endif /* !WOLFPKCS11_NO_STORE */
@@ -6521,12 +6523,16 @@ int WP11_Session_AddObject(WP11_Session* session, int onToken,
65216523
*
65226524
* @param session [in] Session object.
65236525
* @param object [in] Key Object object.
6526+
* @return -ve on failure.
6527+
* 0 on success.
65246528
*/
6525-
void WP11_Session_RemoveObject(WP11_Session* session, WP11_Object* object)
6529+
int WP11_Session_RemoveObject(WP11_Session* session, WP11_Object* object)
65266530
{
6531+
int ret = 0;
65276532
WP11_Object** curr;
65286533
WP11_Token* token;
65296534
int id;
6535+
65306536
#ifdef WOLFPKCS11_NSS
65316537
if (object->session && (session != object->session))
65326538
session = object->session;
@@ -6549,34 +6555,55 @@ void WP11_Session_RemoveObject(WP11_Session* session, WP11_Object* object)
65496555
WP11_Lock_LockRW(&session->slot->token.lock);
65506556
}
65516557

6558+
/* walk list to get id for object to remove */
65526559
while (*curr != NULL) {
65536560
if (*curr == object) {
65546561
*curr = object->next;
65556562
break;
65566563
}
6564+
6565+
#ifndef WOLFPKCS11_NO_STORE
6566+
if (object->onToken) {
6567+
/* remove any id's with higher value */
6568+
ret = wp11_Object_Unstore(*curr, (int)session->slotId, id);
6569+
#ifdef DEBUG_WOLFPKCS11
6570+
if (ret != 0) {
6571+
printf("Failed to unstore slot %d, id: %d, ret: %d, continuing...\n",
6572+
(int)session->slotId, id, ret);
6573+
}
6574+
#endif
6575+
}
6576+
#endif
6577+
65576578
curr = &(*curr)->next;
65586579
/* Id of next object as it isn't the one being removed. */
65596580
id--;
65606581
}
6582+
65616583
if (object->onToken) {
65626584
#ifndef WOLFPKCS11_NO_STORE
6563-
int ret = wp11_Object_Unstore(object, (int)session->slotId, id);
6564-
if (ret == 0) {
6565-
ret = wp11_Slot_Store(session->slot, (int)session->slotId);
6585+
ret = wp11_Object_Unstore(object, (int)session->slotId, id);
6586+
#ifdef DEBUG_WOLFPKCS11
6587+
if (ret != 0) {
6588+
printf("Failed to unstore slot %d, id: %d, ret: %d\n",
6589+
(int)session->slotId, id, ret);
65666590
}
6591+
#endif
6592+
/* re-store the token */
6593+
ret = wp11_Slot_Store(session->slot, (int)session->slotId);
65676594
#ifdef DEBUG_WOLFPKCS11
65686595
if (ret != 0) {
6569-
printf("Failed to unstore slot %d, ret: %d\n",
6596+
printf("Failed to store slot %d, ret: %d\n",
65706597
(int)session->slotId, ret);
65716598
}
65726599
#endif
6573-
(void)ret; /* store failure cannot be returned, so log and ignore */
65746600
#endif
65756601
WP11_Lock_UnlockRW(object->lock);
65766602
}
65776603
else {
65786604
WP11_Lock_UnlockRW(&session->slot->token.lock);
65796605
}
6606+
return ret;
65806607
}
65816608

65826609
/**

wolfpkcs11/internal.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,7 @@ WP11_LOCAL int WP11_Session_SetCtsParams(WP11_Session* session, unsigned char* i
358358
int enc, WP11_Object* object);
359359
WP11_LOCAL int WP11_Session_AddObject(WP11_Session* session, int onToken,
360360
WP11_Object* object);
361-
WP11_LOCAL void WP11_Session_RemoveObject(WP11_Session* session, WP11_Object* object);
361+
WP11_LOCAL int WP11_Session_RemoveObject(WP11_Session* session, WP11_Object* object);
362362
WP11_LOCAL void WP11_Session_GetObject(WP11_Session* session, WP11_Object** object);
363363
WP11_LOCAL void WP11_Session_SetObject(WP11_Session* session, WP11_Object* object);
364364

0 commit comments

Comments
 (0)