Skip to content

Commit cec4a94

Browse files
Add debug instrumentation and hoist migration logic to run before object loading
- Add DEBUG_WOLFPKCS11 prints at wp11_Token_Load entry and after storage open - Add debug print before migration block showing ret and key fields - Add debug print in NOT_AVAILABLE_E path - Add debug print after object loading showing final ret value - CRITICAL FIX: Hoist migration logic to run BEFORE object loading loop so it works even if object loading fails due to corruption - Migration now runs right after storage close and before object load - This ensures state and tokenFlags are set correctly even when corrupted objects cause wp11_Object_Load to fail The previous implementation only ran migration inside 'if (ret == 0)' after object loading, which meant it never ran when object loading failed. This caused token fields to remain zero, leading to CKR_USER_PIN_NOT_INITIALIZED errors at C_Login time.
1 parent da7dbee commit cec4a94

File tree

1 file changed

+30
-11
lines changed

1 file changed

+30
-11
lines changed

src/internal.c

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5194,9 +5194,18 @@ static int wp11_Token_Load(WP11_Slot* slot, int tokenId, WP11_Token* token)
51945194
int objCnt = 0;
51955195
word32 len;
51965196

5197+
#ifdef DEBUG_WOLFPKCS11
5198+
printf("wp11_Token_Load: ENTRY tokenId=%d\n", tokenId);
5199+
#endif
5200+
51975201
/* Open access to token object. */
51985202
ret = wp11_storage_open_readonly(WOLFPKCS11_STORE_TOKEN, tokenId, 0,
51995203
&storage);
5204+
5205+
#ifdef DEBUG_WOLFPKCS11
5206+
printf("wp11_Token_Load: after storage open, ret=%d\n", ret);
5207+
#endif
5208+
52005209
if (ret == 0) {
52015210
/* Read label for token. (32) */
52025211
ret = wp11_storage_read_string(storage, token->label,
@@ -5304,13 +5313,9 @@ static int wp11_Token_Load(WP11_Slot* slot, int tokenId, WP11_Token* token)
53045313

53055314
wp11_storage_close(storage);
53065315

5307-
object = token->object;
5308-
for (i = token->objCnt - 1; (ret == 0) && (i >= 0); i--) {
5309-
/* Load the objects. */
5310-
ret = wp11_Object_Load(object, tokenId, i);
5311-
object = object->next;
5312-
}
5313-
5316+
/* Migration logic for old versions that didn't persist state field
5317+
* or tokenFlags properly. Run this BEFORE object loading so it works
5318+
* even if object loading fails due to corruption. */
53145319
if (ret == 0) {
53155320
int needMigration = 0;
53165321

@@ -5322,9 +5327,8 @@ static int wp11_Token_Load(WP11_Slot* slot, int tokenId, WP11_Token* token)
53225327
token->soPinLen, token->objCnt, token->nextObjId);
53235328
#endif
53245329

5325-
/* Migration logic for old versions that didn't persist state field
5326-
* or tokenFlags properly. Detect if token is initialized but state
5327-
* field is not set, or if PIN flags are missing. */
5330+
/* Detect if token is initialized but state field is not set, or if
5331+
* PIN flags are missing. */
53285332
if (token->state != WP11_TOKEN_STATE_INITIALIZED) {
53295333
int hasUserPin = (token->userPinLen > 0) ||
53305334
(token->tokenFlags & WP11_TOKEN_FLAG_USER_PIN_SET);
@@ -5369,7 +5373,7 @@ static int wp11_Token_Load(WP11_Slot* slot, int tokenId, WP11_Token* token)
53695373
#endif
53705374
}
53715375

5372-
/* If state still not set but we successfully loaded, set it */
5376+
/* If state still not set but we successfully loaded metadata, set it */
53735377
if (token->state != WP11_TOKEN_STATE_INITIALIZED) {
53745378
token->state = WP11_TOKEN_STATE_INITIALIZED;
53755379
}
@@ -5395,6 +5399,18 @@ static int wp11_Token_Load(WP11_Slot* slot, int tokenId, WP11_Token* token)
53955399
}
53965400
}
53975401

5402+
/* Load the objects - this may fail due to corruption, but migration
5403+
* has already run above so state/flags are set correctly */
5404+
object = token->object;
5405+
for (i = token->objCnt - 1; (ret == 0) && (i >= 0); i--) {
5406+
ret = wp11_Object_Load(object, tokenId, i);
5407+
object = object->next;
5408+
}
5409+
5410+
#ifdef DEBUG_WOLFPKCS11
5411+
printf("wp11_Token_Load: after object loading, ret=%d\n", ret);
5412+
#endif
5413+
53985414
/* If there is no pin, there is no login, so decode now */
53995415
if (WP11_Slot_Has_Empty_Pin(slot) && (ret == 0)) {
54005416
#ifndef WOLFPKCS11_NO_STORE
@@ -5412,6 +5428,9 @@ static int wp11_Token_Load(WP11_Slot* slot, int tokenId, WP11_Token* token)
54125428
}
54135429
else if (ret == NOT_AVAILABLE_E) {
54145430
/* No data to read. */
5431+
#ifdef DEBUG_WOLFPKCS11
5432+
printf("wp11_Token_Load: NOT_AVAILABLE_E path - no token data found\n");
5433+
#endif
54155434
ret = 0;
54165435
}
54175436

0 commit comments

Comments
 (0)