Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pkcs11: Fix C_WaitForSlotEvent() not working in Windows #2919

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

llogar
Copy link
Contributor

@llogar llogar commented Oct 29, 2023

If there are any readers present when C_WaitForSlotEvent() is called, C_WaitForSlotEvent() returns immediately and does not wait for the card/slot event to happen. Apparently the MSDN documentation for SCardGetStatusChange() is inaccurate/incomplete and the dwCurrentState for \\?PnP?\Notification reader should contain the number of readers in it's HiWord to work properly. See https://stackoverflow.com/questions/16370909.

If there are any readers present when C_WaitForSlotEvent() is called,
C_WaitForSlotEvent() returns immediately and does not wait for the card/slot event to happen.
Apparently the MSDN documentation for SCardGetStatusChange() is inaccurate/incomplete and
the dwCurrentState for \\?PnP?\Notification reader should contain the number of readers in it's
HiWord to work properly. See https://stackoverflow.com/questions/16370909.
@Jakuje
Copy link
Member

Jakuje commented Oct 31, 2023

@LudovicRousseau are you aware of this issue of windows implementation of pcsc? Is it known issue? Would pcsc-lite handle this input too to avoid the ifdefs?

@LudovicRousseau
Copy link
Member

I confirm that Windows includes the number of readers in the high 16 bits of .dwEventState.

The line https://github.com/OpenSC/OpenSC/blob/master/src/libopensc/reader-pcsc.c#L1708 should be enough to copy the state in the expected format:

rsp->dwCurrentState = rsp->dwEventState;

I do not need the proposed patch in pcsc_scan for Windows for example.

The first call to SCardGetStatusChange() WILL return if .dwCurrentState contains SCARD_STATE_UNAWARE. This should happen both on Windows and Linux and is the expected behaviour.
I don't see why this patch is needed.

@llogar
Copy link
Contributor Author

llogar commented Oct 31, 2023

I confirm that Windows includes the number of readers in the high 16 bits of .dwEventState.

Hmm, the only reference to the number of readers I've found and mentioned in my PR suggested that the number of readers should be put into the high 16 bits of .dwCurrentState. And if done so, the .dwEventState (after SCardGetStatusChange() returns) does not hold the number of readers in it's high 16 bits. Hence the need to also update rsp->dwCurrentState.

If I put (per your suggestion) number of readers into the high 16 bits of .dwEventState then updating rsp->dwCurrentState is not necessary, as the number of readers is carried over from the .dwEventState with the assignment you quoted.

In both cases one has to set the number of readers (one way or another) at least once in the beginning, otherwise SCardGetStatusChange() won't block and pcsc_wait_for_event() will return immediately with SC_ERROR_EVENT_TIMEOUT?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants