Skip to content

Commit da7dbee

Browse files
Add comprehensive debug logging for token migration
- Enable DEBUG_WOLFPKCS11 in both old and PR builds - Add wolfPKCS11_Debugging_On() calls via dlsym in test programs - Add debug prints to wp11_Token_Load showing pre/post migration state - Add debug prints to WP11_Slot_CheckUserPin showing which condition fails - Add debug prints to WP11_Slot_Has_Empty_Pin showing cached/checked results - This will help diagnose why C_Login still fails with CKR_USER_PIN_NOT_INITIALIZED Co-Authored-By: [email protected] <[email protected]>
1 parent 0d340af commit da7dbee

File tree

2 files changed

+96
-4
lines changed

2 files changed

+96
-4
lines changed

.github/workflows/tpm-corruption-test.yml

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ jobs:
7979
working-directory: ./wolfpkcs11-old
8080
run: |
8181
./autogen.sh
82-
./configure --enable-singlethreaded --enable-wolftpm --disable-dh CFLAGS="-DWOLFPKCS11_TPM_STORE"
82+
./configure --enable-singlethreaded --enable-wolftpm --disable-dh --enable-debug CFLAGS="-DWOLFPKCS11_TPM_STORE -DDEBUG_WOLFPKCS11"
8383
make
8484
8585
# Initialize token
@@ -92,6 +92,7 @@ jobs:
9292
- name: Create corruption test program
9393
run: |
9494
cat > corruption_test.c << 'EOF'
95+
#define _GNU_SOURCE
9596
#include <stdio.h>
9697
#include <stdlib.h>
9798
#include <string.h>
@@ -115,13 +116,24 @@ jobs:
115116
int load_library(const char* path) {
116117
CK_RV rv;
117118
CK_C_GetFunctionList pC_GetFunctionList;
119+
typedef void (*dbg_on_fn)(void);
120+
dbg_on_fn dbg_on;
118121
119122
libHandle = dlopen(path, RTLD_NOW | RTLD_LOCAL);
120123
if (!libHandle) {
121124
printf("ERROR: Failed to load library: %s\n", dlerror());
122125
return -1;
123126
}
124127
128+
/* Enable debug output */
129+
dbg_on = (dbg_on_fn)dlsym(libHandle, "wolfPKCS11_Debugging_On");
130+
if (dbg_on) {
131+
printf("Enabling wolfPKCS11 debug output...\n");
132+
dbg_on();
133+
} else {
134+
fprintf(stderr, "Warning: wolfPKCS11_Debugging_On symbol not found\n");
135+
}
136+
125137
pC_GetFunctionList = (CK_C_GetFunctionList)dlsym(libHandle, "C_GetFunctionList");
126138
if (!pC_GetFunctionList) {
127139
printf("ERROR: Failed to get C_GetFunctionList: %s\n", dlerror());
@@ -291,13 +303,14 @@ jobs:
291303
working-directory: ./wolfpkcs11-pr
292304
run: |
293305
./autogen.sh
294-
./configure --enable-singlethreaded --enable-wolftpm --disable-dh CFLAGS="-DWOLFPKCS11_TPM_STORE"
306+
./configure --enable-singlethreaded --enable-wolftpm --disable-dh --enable-debug CFLAGS="-DWOLFPKCS11_TPM_STORE -DDEBUG_WOLFPKCS11"
295307
make
296308
297309
# Create test program to access corrupted state
298310
- name: Create access test program
299311
run: |
300312
cat > access_test.c << 'EOF'
313+
#define _GNU_SOURCE
301314
#include <stdio.h>
302315
#include <stdlib.h>
303316
#include <string.h>
@@ -320,13 +333,24 @@ jobs:
320333
int load_library(const char* path) {
321334
CK_RV rv;
322335
CK_C_GetFunctionList pC_GetFunctionList;
336+
typedef void (*dbg_on_fn)(void);
337+
dbg_on_fn dbg_on;
323338
324339
libHandle = dlopen(path, RTLD_NOW | RTLD_LOCAL);
325340
if (!libHandle) {
326341
printf("ERROR: Failed to load library: %s\n", dlerror());
327342
return -1;
328343
}
329344
345+
/* Enable debug output */
346+
dbg_on = (dbg_on_fn)dlsym(libHandle, "wolfPKCS11_Debugging_On");
347+
if (dbg_on) {
348+
printf("Enabling wolfPKCS11 debug output...\n");
349+
dbg_on();
350+
} else {
351+
fprintf(stderr, "Warning: wolfPKCS11_Debugging_On symbol not found\n");
352+
}
353+
330354
pC_GetFunctionList = (CK_C_GetFunctionList)dlsym(libHandle, "C_GetFunctionList");
331355
if (!pC_GetFunctionList) {
332356
printf("ERROR: Failed to get C_GetFunctionList: %s\n", dlerror());

src/internal.c

Lines changed: 70 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -791,29 +791,54 @@ int WP11_Slot_Has_Empty_Pin(WP11_Slot* slot)
791791
if (slot == NULL)
792792
return 0;
793793

794+
#ifdef DEBUG_WOLFPKCS11
795+
printf("WP11_Slot_Has_Empty_Pin: tokenFlags=0x%x, USER_PIN_SET=%d, userPinEmpty=%d\n",
796+
slot->token.tokenFlags,
797+
!!(slot->token.tokenFlags & WP11_TOKEN_FLAG_USER_PIN_SET),
798+
slot->token.userPinEmpty);
799+
#endif
800+
794801
if (slot->token.tokenFlags & WP11_TOKEN_FLAG_USER_PIN_SET) {
795802
switch (slot->token.userPinEmpty) {
796803
case 1:
797804
/* Empty user PIN */
805+
#ifdef DEBUG_WOLFPKCS11
806+
printf("WP11_Slot_Has_Empty_Pin: returning 1 (cached empty)\n");
807+
#endif
798808
return 1;
799809
case 2:
800810
/* Non-empty user PIN */
811+
#ifdef DEBUG_WOLFPKCS11
812+
printf("WP11_Slot_Has_Empty_Pin: returning 0 (cached non-empty)\n");
813+
#endif
801814
return 0;
802815
default:
803816
/* Cache result as WP11_Slot_CheckUserPin is very expensive */
817+
#ifdef DEBUG_WOLFPKCS11
818+
printf("WP11_Slot_Has_Empty_Pin: checking with empty PIN...\n");
819+
#endif
804820
if (WP11_Slot_CheckUserPin(slot, (char*)"", 0) == 0) {
805821
/* Empty user PIN */
806822
slot->token.userPinEmpty = 1;
823+
#ifdef DEBUG_WOLFPKCS11
824+
printf("WP11_Slot_Has_Empty_Pin: returning 1 (checked empty)\n");
825+
#endif
807826
return 1;
808827
}
809828
else {
810829
/* Non-empty user PIN */
811830
slot->token.userPinEmpty = 2;
831+
#ifdef DEBUG_WOLFPKCS11
832+
printf("WP11_Slot_Has_Empty_Pin: returning 0 (checked non-empty)\n");
833+
#endif
812834
return 0;
813835
}
814836
}
815837
}
816838

839+
#ifdef DEBUG_WOLFPKCS11
840+
printf("WP11_Slot_Has_Empty_Pin: returning 0 (USER_PIN_SET not set)\n");
841+
#endif
817842
return 0;
818843
}
819844

@@ -5289,6 +5314,14 @@ static int wp11_Token_Load(WP11_Slot* slot, int tokenId, WP11_Token* token)
52895314
if (ret == 0) {
52905315
int needMigration = 0;
52915316

5317+
#ifdef DEBUG_WOLFPKCS11
5318+
printf("wp11_Token_Load: PRE-MIGRATION tokenId=%d, state=%d, "
5319+
"tokenFlags=0x%x, userPinLen=%d, soPinLen=%d, objCnt=%d, "
5320+
"nextObjId=%d\n",
5321+
tokenId, token->state, token->tokenFlags, token->userPinLen,
5322+
token->soPinLen, token->objCnt, token->nextObjId);
5323+
#endif
5324+
52925325
/* Migration logic for old versions that didn't persist state field
52935326
* or tokenFlags properly. Detect if token is initialized but state
52945327
* field is not set, or if PIN flags are missing. */
@@ -5299,6 +5332,12 @@ static int wp11_Token_Load(WP11_Slot* slot, int tokenId, WP11_Token* token)
52995332
(token->tokenFlags & WP11_TOKEN_FLAG_SO_PIN_SET);
53005333
int hasObjects = (token->objCnt > 0) || (token->object != NULL);
53015334

5335+
#ifdef DEBUG_WOLFPKCS11
5336+
printf("Token migration: state != INITIALIZED, checking heuristics: "
5337+
"hasUserPin=%d, hasSoPin=%d, hasObjects=%d\n",
5338+
hasUserPin, hasSoPin, hasObjects);
5339+
#endif
5340+
53025341
if (hasUserPin || hasSoPin || hasObjects) {
53035342
/* Token is initialized but state not set - migrate */
53045343
token->state = WP11_TOKEN_STATE_INITIALIZED;
@@ -5335,9 +5374,18 @@ static int wp11_Token_Load(WP11_Slot* slot, int tokenId, WP11_Token* token)
53355374
token->state = WP11_TOKEN_STATE_INITIALIZED;
53365375
}
53375376

5377+
#ifdef DEBUG_WOLFPKCS11
5378+
printf("wp11_Token_Load: POST-MIGRATION state=%d, tokenFlags=0x%x, "
5379+
"needMigration=%d\n",
5380+
token->state, token->tokenFlags, needMigration);
5381+
#endif
5382+
53385383
/* Persist migrated token back to storage for one-time fix */
53395384
if (needMigration) {
53405385
int saveRet = wp11_Token_Store(token, tokenId);
5386+
#ifdef DEBUG_WOLFPKCS11
5387+
printf("Token migration: wp11_Token_Store returned %d\n", saveRet);
5388+
#endif
53415389
if (saveRet != 0) {
53425390
#ifdef DEBUG_WOLFPKCS11
53435391
printf("Token migration: failed to persist state (ret=%d), "
@@ -6184,10 +6232,27 @@ int WP11_Slot_CheckUserPin(WP11_Slot* slot, char* pin, int pinLen)
61846232

61856233
WP11_Lock_LockRO(&slot->lock);
61866234
token = &slot->token;
6187-
if (token->state != WP11_TOKEN_STATE_INITIALIZED)
6235+
#ifdef DEBUG_WOLFPKCS11
6236+
printf("WP11_Slot_CheckUserPin: state=%d, tokenFlags=0x%x, USER_PIN_SET=%d, "
6237+
"userPinLen=%d, soPinLen=%d, objCnt=%d\n",
6238+
token->state, token->tokenFlags,
6239+
!!(token->tokenFlags & WP11_TOKEN_FLAG_USER_PIN_SET),
6240+
token->userPinLen, token->soPinLen, token->objCnt);
6241+
#endif
6242+
if (token->state != WP11_TOKEN_STATE_INITIALIZED) {
61886243
ret = PIN_NOT_SET_E;
6189-
if (!(token->tokenFlags & WP11_TOKEN_FLAG_USER_PIN_SET))
6244+
#ifdef DEBUG_WOLFPKCS11
6245+
printf("WP11_Slot_CheckUserPin: FAILED - state != INITIALIZED (state=%d)\n",
6246+
token->state);
6247+
#endif
6248+
}
6249+
if (!(token->tokenFlags & WP11_TOKEN_FLAG_USER_PIN_SET)) {
61906250
ret = PIN_NOT_SET_E;
6251+
#ifdef DEBUG_WOLFPKCS11
6252+
printf("WP11_Slot_CheckUserPin: FAILED - USER_PIN_SET flag not set "
6253+
"(tokenFlags=0x%x)\n", token->tokenFlags);
6254+
#endif
6255+
}
61916256

61926257
if (ret == 0) {
61936258
WP11_Lock_UnlockRO(&slot->lock);
@@ -6203,6 +6268,9 @@ int WP11_Slot_CheckUserPin(WP11_Slot* slot, char* pin, int pinLen)
62036268
ret = PIN_INVALID_E;
62046269
WP11_Lock_UnlockRO(&slot->lock);
62056270

6271+
#ifdef DEBUG_WOLFPKCS11
6272+
printf("WP11_Slot_CheckUserPin: returning ret=%d\n", ret);
6273+
#endif
62066274
return ret;
62076275
}
62086276

0 commit comments

Comments
 (0)