OpenSCToken icon indicating copy to clipboard operation
OpenSCToken copied to clipboard

Unable to switch from contact and contactless reader interfaces

Open angelocastaldo opened this issue 2 years ago • 8 comments

Hi, I'm running the CryptoTokenKit using a dual interface reader. At first stage the smart card is inserted into the reader (contact interface). All seems working successfully. Then if I switch the smart card from the contact to the contactless interfacethe CryptoTokenKit stops working. The same happens if I start working with the contactless interface and then switch to the contact one. If I use always the same interface (contact or contactless), without switching, all seems working successfully.

Here is the failure log part:

P:496; T:0x123145460260864 17:29:04.207 [cryptotokenkit] ctx.c:855:sc_context_create: opensc version: 0.22.0 P:496; T:0x123145460260864 17:29:04.207 [cryptotokenkit] reader-cryptotokenkit.m:546:cryptotokenkit_detect_readers: called P:496; T:0x123145460260864 17:29:04.207 [cryptotokenkit] reader-cryptotokenkit.m:554:cryptotokenkit_detect_readers: Probing CryptoTokenKit readers P:496; T:0x123145460260864 17:29:04.207 [cryptotokenkit] reader-cryptotokenkit.m:585:cryptotokenkit_detect_readers: Found new CryptoTokenKit reader 'SDI011 USB Smart Card Reader(1)' P:496; T:0x123145460260864 17:29:09.209 [cryptotokenkit] reader-cryptotokenkit.m:585:cryptotokenkit_detect_readers: Found new CryptoTokenKit reader 'SDI011 USB Smart Card Reader(2)' P:496; T:0x123145460260864 17:29:14.210 [cryptotokenkit] reader-cryptotokenkit.m:597:cryptotokenkit_detect_readers: returning with: 0 (Success) P:496; T:0x123145460260864 17:29:14.210 [cryptotokenkit] ctx.c:895:sc_ctx_use_reader: called P:496; T:0x123145460260864 17:29:14.210 [cryptotokenkit] card.c:259:sc_connect_card: called P:496; T:0x123145460260864 17:29:14.210 [cryptotokenkit] card.c:413:sc_connect_card: returning with: -1104 (Card not present) P:496; T:0x123145460260864 17:29:14.210 [cryptotokenkit] /Users/angelocastaldo/Desktop/Development/OpenSCToken-master/OpenSCToken/Token.m:97:-[OpenSCToken initWithSmartCard:AID:OpenSCDriver:error:]: sc_connect_card: -1104 (Card not present) P:496; T:0x123145460260864 17:29:14.210 [cryptotokenkit] ctx.c:930:sc_release_context: called

Thanks in advance for your support.

angelocastaldo avatar Nov 15 '21 16:11 angelocastaldo

what version are you using? in 0.22.0 the line numbers should be different...

frankmorgner avatar Nov 16 '21 12:11 frankmorgner

it looks like you compiled it yourself. what changes did you make?

frankmorgner avatar Nov 16 '21 12:11 frankmorgner

please try this patch:

diff --git a/src/libopensc/reader-cryptotokenkit.m b/src/libopensc/reader-cryptotokenkit.m
index 1bf85bfc..9eb9db76 100644
--- a/src/libopensc/reader-cryptotokenkit.m
+++ b/src/libopensc/reader-cryptotokenkit.m
@@ -102,9 +102,14 @@ static int cryptotokenkit_detect_card_presence(sc_reader_t *reader)
                case TKSmartCardSlotStateValidCard:
                        // Card properly answered to reset.
                        reader->flags |= SC_READER_CARD_PRESENT;
-                       if ([priv->tksmartcardslot.ATR.bytes length] > SC_MAX_ATR_SIZE)
-                               return SC_ERROR_INTERNAL;
-                       reader->atr.len = [priv->tksmartcardslot.ATR.bytes length];
+                       if ([priv->tksmartcardslot.ATR.bytes length] > SC_MAX_ATR_SIZE) {
+                               sc_log_hex(reader->ctx, "ATR too long for internal buffer; truncating",
+                                               (unsigned char*) [priv->tksmartcardslot.ATR.bytes bytes]
+                                               [priv->tksmartcardslot.ATR.bytes length]);
+                               reader->atr.len = SC_MAX_ATR_SIZE;
+                       } else {
+                               reader->atr.len = [priv->tksmartcardslot.ATR.bytes length];
+                       }
                        memcpy(reader->atr.value, (unsigned char*) [priv->tksmartcardslot.ATR.bytes bytes], reader->atr.len);
                        break;
                case TKSmartCardSlotStateMissing:

frankmorgner avatar Nov 16 '21 13:11 frankmorgner

I'm using version 0.22.0. I added some "sc_log" instruction to better understand the mechanism.

angelocastaldo avatar Nov 16 '21 14:11 angelocastaldo

I try the patch and let you know.

angelocastaldo avatar Nov 16 '21 14:11 angelocastaldo

Hi, unfortunately the proposed solution does not work. I don’t have modified the code apart from some printing (into log) instructions... Please consider this case: inserting a smart card into the reader (contact mode), while entering the card into the contact slot, the contactless interface activates and some APDUs are exchanged. Once the card is completely inserted into the contact slot, the contactless interface stops working and the contact one activates. In several cases, as said in my previous post, the cryptotokenkit blocks. Do you think is it possible to insert a “delay” waiting for “final” interface (contact or contactless) is active? If so, where we could add this delay? Thanks.

angelocastaldo avatar Nov 18 '21 10:11 angelocastaldo

unfortunately the proposed solution does not work.

Do you have the added warning in your log? And if so, could the problem now be that the card isn't detected anymore due to the truncated ATR?

Do you think is it possible to insert a “delay” waiting for “final” interface (contact or contactless) is active? If so, where we could add this delay?

Feel free to experimenting with sleep(), but I'm not sure what you're aiming at with this...

frankmorgner avatar Nov 18 '21 10:11 frankmorgner

Hi, could me give indication where better put the sleep() instructions? Which function(s)? I've put some sleep() in the file Token.m, at the beginning of the function initWithSmartCard but nothig." Thanks

angelocastaldo avatar Nov 19 '21 14:11 angelocastaldo

The contactless and the contactbased reader interfaces are typically registered as two independent readers. Changing the card from one to the other interface, will make the card disappear in one reader (SDI011 USB Smart Card Reader(1)) and appear in the other reader (SDI011 USB Smart Card Reader(2)).

Could you check what kind of card CTK provides to the driver when calling sc_ctx_use_reader()? This should normally be a ready to use card, so it is strange to see -1104 with this input. You can debug this by first inserting the card into the contact based reader, which start OpenSCToken in the background. Then attach to this process for debugging. Then remove the card and insert it into the contact-based interface. The same process should now be used for initialization (OpenSCToken) so you're ready to debug the problem.

frankmorgner avatar Jun 16 '23 07:06 frankmorgner

Closing this issue due to inactivity. Please re-open the ticket if more input is available.

frankmorgner avatar Nov 23 '23 14:11 frankmorgner