Skip to content

Conversation

@frankmorgner
Copy link
Member

Checklist
  • Documentation is added or updated
  • New files have a LGPL 2.1 license statement
  • PKCS#11 module is tested
  • Windows minidriver is tested
  • macOS tokend is tested

dengert and others added 3 commits March 10, 2022 09:41
Issue OpenSC#2517 reports that minidriver may be presented with new handles
to PCSC, even if card is moved from one reader to another.

The previous code assumed a change in handles would always point at same reader.
and if reader was changed, caller would do two operations: card is removed from one
reader i.e. call CardDeleteContext and some time later when card is inserted,
call CardAcquireContext. Thus previous code would not to call reinit_card_for.

But this is not the case. So when handles change, the new handles are used to probe
the new reader to obtain reader name to compare to old reader name.
If they are the same returns to not call reinit_card.
If they are different returns to call reinit_card.

The assumption is if card is removed, it could be modified on another system
then inserted on original system which changed data.

pcsc_new_reader will set ctx->flags & SC_CTX_FLAG_TERMINATE so no operations
are mistakenly done to old reader and to signal second call to pcsc_new_reader
when called to setup the new reader, to free the old reader and reset
SC_CTX_FLAG_TERMINATE.

 Date:      Thu Mar 10 10:04:00 2022 -0600
 Changes to be committed:
	modified:   src/libopensc/reader-pcsc.c
	modified:   src/minidriver/minidriver.c
 On branch windows-changed-readers
 Changes to be committed:
	modified:   libopensc/opensc.h
	modified:   libopensc/reader-pcsc.c
	modified:   minidriver/minidriver.c
@frankmorgner frankmorgner marked this pull request as draft March 15, 2022 14:24
@frankmorgner frankmorgner mentioned this pull request Mar 15, 2022
5 tasks
@jozsefd
Copy link
Contributor

jozsefd commented Mar 16, 2022

I have tested this solution: it works correctly in the special case when the card is moved between 2 readers and a running Windows application uses the card's key containers. The minidrives recognizes the need for a reinit_card_for():

P:10956; T:14044 2022-03-16 16:32:12.419 [cardmod] CardReadFile
P:10956; T:14044 2022-03-16 16:32:12.419 [cardmod] pszDirectoryName = mscp, pszFileName = cmapfile, dwFlags = 0, pcbData=00AFE178, ppbData=00AFE17C
P:10956; T:14044 2022-03-16 16:32:12.420 [cardmod] check_reader_status for CardReadFile
P:10956; T:14044 2022-03-16 16:32:12.420 [cardmod] MD_Function:check_card_status:396 called
P:10956; T:14044 2022-03-16 16:32:12.420 [cardmod] MD_Function:check_card_status:406 returning with: 0x00000000
P:10956; T:14044 2022-03-16 16:32:12.421 [cardmod] sizeof(size_t):4 sizeof(ULONG_PTR):4 sizeof(__int3264):4 sizeof pCardData->hSCardCtx:4
P:10956; T:14044 2022-03-16 16:32:12.421 [cardmod] pCardData->hSCardCtx:0xCD030002 hScard:0xEA010001
P:10956; T:14044 2022-03-16 16:32:12.422 [cardmod] HANDLES CHANGED from  vs->hSCardCtx:0xCD010003 vs->hScard:0xEA020000
P:10956; T:14044 2022-03-16 16:32:12.422 [cardmod] Reader name changed from "FujitsuTechnologySolutions GmbH USB CCID 0" to "Fujitsu Keyboard KB100 SCR eSIG 0"
P:10956; T:14044 2022-03-16 16:32:12.422 [cardmod] disassociate_card
P:10956; T:14044 2022-03-16 16:32:12.423 [cardmod] sc_pkcs15_unbind
P:10956; T:14044 2022-03-16 16:32:12.423 [cardmod] pkcs15.c:1317:sc_pkcs15_unbind: called
P:10956; T:14044 2022-03-16 16:32:12.423 [cardmod] pkcs15-pin.c:863:sc_pkcs15_pincache_clear: called
P:10956; T:14044 2022-03-16 16:32:12.424 [cardmod] sc_disconnect_card
P:10956; T:14044 2022-03-16 16:32:12.424 [cardmod] card.c:414:sc_disconnect_card: called
P:10956; T:14044 2022-03-16 16:32:12.424 [cardmod] card.c:436:sc_disconnect_card: returning with: 0 (Success)
P:10956; T:14044 2022-03-16 16:32:12.424 [cardmod] MD_Function:associate_card:7151 called
P:10956; T:14044 2022-03-16 16:32:12.425 [cardmod] ctx.c:895:sc_ctx_use_reader: called
P:10956; T:14044 2022-03-16 16:32:12.425 [cardmod] reader-pcsc.c:2552:pcsc_use_reader: called
P:10956; T:14044 2022-03-16 16:32:12.425 [cardmod] Probing PC/SC reader
P:10956; T:14044 2022-03-16 16:32:12.425 [cardmod] Adding new PC/SC reader 'Fujitsu Keyboard KB100 SCR eSIG 0'
P:10956; T:14044 2022-03-16 16:32:12.426 [cardmod] Fujitsu Keyboard KB100 SCR eSIG 0 check
P:10956; T:14044 2022-03-16 16:32:12.426 [cardmod] current  state: 0x00070122
P:10956; T:14044 2022-03-16 16:32:12.426 [cardmod] previous state: 0x00000000
P:10956; T:14044 2022-03-16 16:32:12.427 [cardmod] card present, changed
P:10956; T:14044 2022-03-16 16:32:12.427 [cardmod] reader-pcsc.c:1155:detect_reader_features: called
P:10956; T:14044 2022-03-16 16:32:12.427 [cardmod] Requesting reader features ... 
... (card reinit omitted)
P:10956; T:14044 2022-03-16 16:32:12.907 [cardmod] MD_Function:CardReadFile:4218 returning with: 0x00000000

@frankmorgner frankmorgner marked this pull request as ready for review April 19, 2022 21:26
@frankmorgner frankmorgner merged commit 1b05d76 into OpenSC:master Apr 19, 2022
@frankmorgner
Copy link
Member Author

Thank you for testing

xhanulik pushed a commit to xhanulik/OpenSC that referenced this pull request May 11, 2022
Issue OpenSC#2517 reports that minidriver may be presented with new handles
to PCSC, even if card is moved from one reader to another.

The previous code assumed a change in handles would always point at same reader.
and if reader was changed, caller would do two operations: card is removed from one
reader i.e. call CardDeleteContext and some time later when card is inserted,
call CardAcquireContext. Thus previous code would not to call reinit_card_for.

But this is not the case. So when handles change, the new handles are used to probe
the new reader to obtain reader name to compare to old reader name.
If they are the same returns to not call reinit_card.
If they are different returns to call reinit_card.

The assumption is if card is removed, it could be modified on another system
then inserted on original system which changed data.
AlexandreGonzalo pushed a commit to AlexandreGonzalo/OpenSC that referenced this pull request Nov 14, 2022
Issue OpenSC#2517 reports that minidriver may be presented with new handles
to PCSC, even if card is moved from one reader to another.

The previous code assumed a change in handles would always point at same reader.
and if reader was changed, caller would do two operations: card is removed from one
reader i.e. call CardDeleteContext and some time later when card is inserted,
call CardAcquireContext. Thus previous code would not to call reinit_card_for.

But this is not the case. So when handles change, the new handles are used to probe
the new reader to obtain reader name to compare to old reader name.
If they are the same returns to not call reinit_card.
If they are different returns to call reinit_card.

The assumption is if card is removed, it could be modified on another system
then inserted on original system which changed data.
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.

3 participants