Search code examples
pythonsmartcardapdusmartcard-readerpyscard

Howto list files on a smartcard with pyscard


I have to read out a file from a smartcard. The card is written by a digital tachograph that monitors vehicle movements. I could connect to the smartcard reader with psycard (http://pyscard.sourceforge.net/user-guide.html) but then I don't know how to list files on the card and how to download them. I was digging the ISO standards and it looks like I have to use ISO/IEC 7816 standard, and dedicated files (http://www.cardwerk.com/smartcards/smartcard_standard_ISO7816-4_5_basic_organizations.aspx) but I don't know how to do it from pyscard?

There are already programs out there (for example: http://en.tachoterminal.net/products/tachoterminal-reader/) that do exactly the same thing, but they are stand alone programs. I need to integrate this functionality into another software.

I don't know which protocol to use. Can somebody please help me?


Solution

  • In this answer I assume you are refering to a tachograph driver card according to the ECC regulation No 3821/85. In that case, you are right that the smartcard communicates using ISO/IEC 7816-4 APDUs and that it exposes a file structure based on ISO/IEC 7816-4.

    Once you opened a connection to the card using pyscard, you can call the transmit() method on that connection object to transsceive APDUs (also see Ludovic Rousseau's blog:

    connection = reader.createConnection()
    connection.connect()
    data, sw1, sw2 = connection.transmit(cmd)
    

    The communication flow with the tachograph driver card would be something like the following:

    1. SELECT tachograph application

      00 A4 04 0C 06 FF544143484F
      

      For this command, cmd would be:

      cmd = [ 0x00, 0xA4, 0x04, 0x0C, 0x06, 0xFF, 0x54, 0x41, 0x43, 0x48, 0x4F ]
      
    2. SELECT elementary file by file identifier (xxxx)

      00 A4 02 0C 02 xxxx
      
    3. READ BINARY to get file data (zz bytes from offset xxyy)

      00 B0 xx yy zz
      

    You can find a complete specification of the driver card's protocol and data structures as part of the regulation document (here).