jurajsarinay / ifdnlnfc

IFD Handler exposing Linux NFC devices to PCSC lite
GNU General Public License v2.0
5 stars 1 forks source link

Proper ATR generation #1

Open StarGate01 opened 2 weeks ago

StarGate01 commented 2 weeks ago

Continued from https://github.com/nfc-tools/libnfc/issues/455#issuecomment-2457427034 .

Generate the actual ATR for ISO14443A and B tags. This requires kernel support of ATS and ATQB reporting, see https://lore.kernel.org/netdev/20241103124525.8392-1-juraj@sarinay.com/ .

For sample ATR implementation see https://github.com/nfc-tools/ifdnfc/blob/master/src/atr.c .

StarGate01 commented 2 weeks ago

I have applied your kernel patch, ATS passthough works, the ATR generated is the one I expected. Great work!

I will leave this issue open until the patch has been upstreamed, I guess. Did you look into ISO14443 Type B tags? Those require the ATQB for their ATR.

Tue Nov  5 17:25:03 2024
 Reader 0: Linux Netlink NFC 00 00
  Event number: 7
  Card state: Card inserted, 
  ATR: 3B 8A 80 01 00 31 C1 73 C8 40 00 00 90 00 90

ATR: 3B 8A 80 01 00 31 C1 73 C8 40 00 00 90 00 90
+ TS = 3B --> Direct Convention
+ T0 = 8A, Y(1): 1000, K: 10 (historical bytes)
  TD(1) = 80 --> Y(i+1) = 1000, Protocol T = 0 
-----
  TD(2) = 01 --> Y(i+1) = 0000, Protocol T = 1 
-----
+ Historical bytes: 00 31 C1 73 C8 40 00 00 90 00
  Category indicator byte: 00 (compact TLV data object)
    Tag: 3, len: 1 (card service data byte)
      Card service data byte: C1
        - Application selection: by full DF name
        - Application selection: by partial DF name
        - EF.DIR and EF.ATR access services: by GET RECORD(s) command
        - Card without MF
    Tag: 7, len: 3 (card capabilities)
      Selection methods: C8
        - DF selection by full DF name
        - DF selection by partial DF name
        - Implicit DF selection
      Data coding byte: 40
        - Behaviour of write functions: write OR
        - Value 'FF' for the first byte of BER-TLV tag fields: invalid
        - Data unit in quartets: 1
      Command chaining, length fields and logical channels: 00
        - Logical channel number assignment: No logical channel
        - Maximum number of logical channels: 1
    Mandatory status indicator (3 last bytes)
      LCS (life card cycle): 00 (No information given)
      SW: 9000 (Normal processing.)
+ TCK = 90 (correct checksum)

Possibly identified card (using /home/christoph/.cache/smartcard_list.txt):
3B 8A 80 01 00 31 C1 73 C8 40 00 00 90 00 90
        NXP PN65o's Internal Secure Element in card emulation mode. (Other)
jurajsarinay commented 2 weeks ago

Did you look into ISO14443 Type B tags?

Those should work, see here. I have a type B passport that ends up with ATR: 3B 88 80 01 E1 F3 5E 11 77 81 95 00 37.

I am not sure about the zero byte that gets appended (also see https://github.com/nfc-tools/ifdnfc/blob/0e48e8e107dc42f68fb472bb06d257503ad45b1d/src/atr.c#L94). The MBL from the ATTRIB response is sometimes mentioned instead, but the kernel does not propagate the information to userspace anyway.