Search code examples
pythonsmartcardpcscpyscard

Problem sending an APDU command with pyscard


I am developing a simple application involving an ACR1251 smart card reader. Currently I don't have it with me, but it's a time-sensitive project, so I'm using vsmartcard to emulate a reader and a ISO 7816 card.

The code I'm having problems with is:

from smartcard.System import readers

reader = readers()[0]
conn = reader.createConnection()
conn.connect()
response, sw1, sw2 = conn.transmit([0xFF, 0xCA, 0x00, 0x00, 0x00]) # Get Data UID

The response codes are 0x69 0x88, that mean "Incorrect secure messaging (SM) data object". I haven't found anything mentioning SM in pyscard's documentation, and googling wasn't successful either.

Am I missing something in my code? This is definitely possible, as I am trying to do this in the simplest way possible and pyscard is quite low level, and the documentation is not really aimed at doing simple things.

Also it could be a problem with the emulator, in that case I guess I'd just have to wait for customs to process the reader.


Solution

  • For a card compatible with the ISO 7816-standard family (parts 4, 8 and 9 are essential), your specified class byte of 0xFF in fact indicates secure messaging, which is in contradiction with the remaining bytes (too short for a MAC)

    The APDU you give looks more like the pseudo-APDU to address non-processor card as e. g. MIFARE or to sent a command to the reader (as opposed to the card); there is considerable overlap of this possibilities.

    As a first step you could try other class byte values, 0x00 (pure ISO) and 0xA0 (GSM-heritage) being the the most promising. The structure of the class byte is explained in 7816, part 4.

    The supported commands should be in the card user's manual, and even if you manage to get the Get Data to succeed,you will need it urgently later for more complex functionality. (Should you think, that ISO 7816 cards are generic or seriously compatible among each others: wrong and there is is no "generic 7816" card in the real world.)