Skip to content

Commit fb15a7d

Browse files
Jakujefrankmorgner
authored andcommitted
reader-pcsc: Handle yubikey reinsertion
When the application (NSS) does not use WaitForSlotEvent and just opportunistically tries to detect card and reader removals with C_GetSlotInfo() and C_GetSessionInfo(), we might get errors in various plcaes, in the sc_lock() function, when we try to transfer other messages or when we ask for the reader status. This is generally too late to call any disconnect functions because no PC/SC handles are valid anymore. The reader state from PCSC is searched by name so we can be pretty sure it is very similar reader (with same name as the old one) and I hope we can reuse the reader structure and just call the pcsc_connect() on that as we do with invalid handles. Otherwise we detect this issue in the refresh_attributes() (called from C_GetSlotInfo()), where we can report the slot change in the expected manner. Fixes #1822
1 parent 0ea1152 commit fb15a7d

File tree

1 file changed

+4
-1
lines changed

1 file changed

+4
-1
lines changed

src/libopensc/reader-pcsc.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,7 @@ static int pcsc_internal_transmit(sc_reader_t *reader,
247247
case SCARD_W_REMOVED_CARD:
248248
return SC_ERROR_CARD_REMOVED;
249249
case SCARD_E_INVALID_HANDLE:
250+
case SCARD_E_INVALID_VALUE:
250251
case SCARD_E_READER_UNAVAILABLE:
251252
pcsc_connect(reader);
252253
/* return failure so that upper layers will be notified */
@@ -415,7 +416,7 @@ static int refresh_attributes(sc_reader_t *reader)
415416
unsigned char atr[SC_MAX_ATR_SIZE];
416417
rv = priv->gpriv->SCardStatus(priv->pcsc_card, NULL,
417418
&readers_len, &cstate, &prot, atr, &atr_len);
418-
if (rv == (LONG)SCARD_W_REMOVED_CARD)
419+
if (rv == (LONG)SCARD_W_REMOVED_CARD || rv == (LONG)SCARD_E_INVALID_VALUE)
419420
reader->flags |= SC_READER_CARD_CHANGED;
420421
}
421422
} else {
@@ -660,6 +661,8 @@ static int pcsc_lock(sc_reader_t *reader)
660661
PCSC_TRACE(reader, "SCardBeginTransaction returned", rv);
661662

662663
switch (rv) {
664+
case SCARD_E_INVALID_VALUE:
665+
/* This is retuned in case of the same reader was re-attached */
663666
case SCARD_E_INVALID_HANDLE:
664667
case SCARD_E_READER_UNAVAILABLE:
665668
r = pcsc_connect(reader);

0 commit comments

Comments
 (0)