@@ -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