Skip to content

Commit

Permalink
Handle bogus ACS ACR122U PICC reader
Browse files Browse the repository at this point in the history
Fix `list index out of range` for mifare classic cards

Traceback (most recent call last):
  File "test.py", line 17, in <module>
    res = CARDSERVICE.connection.transmit(COMMAND, CARDSERVICE.connection.T1_protocol)
  File "/nix/store/n4c9kwr2471qkqqd8bw33rnkrk28agf1-python3.8-pyscard-1.9.9/lib/python3.8/site-packages/smartcard/CardConnectionDecorator.py", line 82, in transmit
    return self.component.transmit(bytes, protocol)
  File "/nix/store/n4c9kwr2471qkqqd8bw33rnkrk28agf1-python3.8-pyscard-1.9.9/lib/python3.8/site-packages/smartcard/CardConnection.py", line 146, in transmit
    data, sw1, sw2 = self.doTransmit(bytes, protocol)
  File "/nix/store/n4c9kwr2471qkqqd8bw33rnkrk28agf1-python3.8-pyscard-1.9.9/lib/python3.8/site-packages/smartcard/pcsc/PCSCCardConnection.py", line 205, in doTransmit
    sw1 = (response[-2] + 256) % 256
IndexError: list index out of range

The reader ACS ACR122U PICC may return an error code but without
indicating the CCID frame is an error. So the driver reports 0 bytes
with no error.

00000003 APDU: 00 A4 04 00 08 A0 00 00 05 27 47 11 17
00000002 ifdhandler.c:1302:IFDHTransmitToICC() usb:072f/2200:libudev:0:/dev/bus/usb/001/054 (lun: 0)
00000002 commands.c:1755:CmdXfrBlockTPDU_T0() T=0: 13 bytes
00000004 -> 000000 6F 0D 00 00 00 00 08 00 00 00 00 A4 04 00 08 A0 00 00 05 27 47 11 17
00006627 <- 000000 80 00 00 00 00 00 08 00 FE 00
00000017 SW:
00000005 winscard.c:1640:SCardTransmit() UnrefReader() count was: 2
00000004 winscard_svc.c:685:ContextThread() TRANSMIT rv=0x0 for client 15

The reader reports 0xFE (no card present) in Error byte but does not
indicate the frame is an error frame in the Status byte.

See " When trying to read Mifare classic card, there is an IndexError #101 "
#101
  • Loading branch information
Max Hausch authored and LudovicRousseau committed Nov 27, 2020
1 parent ba51b70 commit 5a424cc
Showing 1 changed file with 4 additions and 0 deletions.
4 changes: 4 additions & 0 deletions smartcard/pcsc/PCSCCardConnection.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,10 @@ def doTransmit(self, bytes, protocol=None):
dictProtocolHeader[pcscprotocolheader] + '. ' + \
SCardGetErrorMessage(hresult))

if len(response) < 2:
raise CardConnectionException(
'Card returned no valid response')

sw1 = (response[-2] + 256) % 256
sw2 = (response[-1] + 256) % 256

Expand Down

0 comments on commit 5a424cc

Please sign in to comment.