nfc-tools / ifdnfc

PC/SC IFD Handler based on libnfc
GNU General Public License v3.0
34 stars 9 forks source link

IFD-NFC is inactive #9

Open eximius313 opened 2 years ago

eximius313 commented 2 years ago

Dear ifdnfc community,

My goal is to use my PN532 reader in Java@RPi4

I've setup libnfc properly with pn532_uart driver My /etc/nfc/libnfc.conf looks like this:

allow_autoscan = true
allow_intrusive_scan = false
log_level = 3
device.name = "IFD-NFC"
device.connstring = "pn532_uart:/dev/ttyUSB0:115200"

and nfc-scan-device returns:

debug   libnfc.general  log_level is set to 3
debug   libnfc.general  allow_autoscan is set to true
debug   libnfc.general  allow_intrusive_scan is set to false
debug   libnfc.general  1 device(s) defined by user
debug   libnfc.general    #0 name: "IFD-NFC", connstring: "pn532_uart:/dev/ttyUSB0:115200"
nfc-scan-device uses libnfc 1.8.0
debug   libnfc.general  0 device(s) found using acr122_usb driver
debug   libnfc.general  0 device(s) found using pn53x_usb driver
1 NFC device(s) found:
debug   libnfc.driver.pn532_uart        Attempt to open: /dev/ttyUSB0 at 115200 baud.
debug   libnfc.bus.uart Serial port speed requested to be set to 115200 baud.
debug   libnfc.chip.pn53x       Diagnose
debug   libnfc.chip.pn53x       Timeout value: 500
debug   libnfc.bus.uart TX: 55 55 00 00 00 00 00 00 00 00 00 00 00 00 00 00
debug   libnfc.chip.pn53x       SAMConfiguration
debug   libnfc.chip.pn53x       Timeout value: 1000
debug   libnfc.bus.uart TX: 00 00 ff 03 fd d4 14 01 17 00
debug   libnfc.bus.uart RX: 00 00 ff 00 ff 00
debug   libnfc.chip.pn53x       PN53x ACKed
debug   libnfc.bus.uart RX: 00 00 ff 02 fe
debug   libnfc.bus.uart RX: d5 15
debug   libnfc.bus.uart RX: 16 00
debug   libnfc.bus.uart TX: 00 00 ff 09 f7 d4 00 00 6c 69 62 6e 66 63 be 00
debug   libnfc.bus.uart RX: 00 00 ff 00 ff 00
debug   libnfc.chip.pn53x       PN53x ACKed
debug   libnfc.bus.uart RX: 00 00 ff 09 f7
debug   libnfc.bus.uart RX: d5 01
debug   libnfc.bus.uart RX: 00 6c 69 62 6e 66 63
debug   libnfc.bus.uart RX: bc 00
debug   libnfc.chip.pn53x       GetFirmwareVersion
debug   libnfc.bus.uart TX: 00 00 ff 02 fe d4 02 2a 00
debug   libnfc.bus.uart RX: 00 00 ff 00 ff 00
debug   libnfc.chip.pn53x       PN53x ACKed
debug   libnfc.bus.uart RX: 00 00 ff 06 fa
debug   libnfc.bus.uart RX: d5 03
debug   libnfc.bus.uart RX: 32 01 06 07
debug   libnfc.bus.uart RX: e8 00
debug   libnfc.chip.pn53x       SetParameters
debug   libnfc.bus.uart TX: 00 00 ff 03 fd d4 12 14 06 00
debug   libnfc.bus.uart RX: 00 00 ff 00 ff 00
debug   libnfc.chip.pn53x       PN53x ACKed
debug   libnfc.bus.uart RX: 00 00 ff 02 fe
debug   libnfc.bus.uart RX: d5 13
debug   libnfc.bus.uart RX: 18 00
debug   libnfc.general  "IFD-NFC" (pn532_uart:/dev/ttyUSB0:115200) has been claimed.
- IFD-NFC:
    pn532_uart:/dev/ttyUSB0:115200
debug   libnfc.chip.pn53x       ReadRegister
debug   libnfc.bus.uart TX: 00 00 ff 0c f4 d4 06 63 02 63 03 63 0d 63 38 63 3d b0 00
debug   libnfc.bus.uart RX: 00 00 ff 00 ff 00
debug   libnfc.chip.pn53x       PN53x ACKed
debug   libnfc.bus.uart RX: 00 00 ff 07 f9
debug   libnfc.bus.uart RX: d5 07
debug   libnfc.bus.uart RX: 00 00 00 00 00
debug   libnfc.bus.uart RX: 24 00
debug   libnfc.chip.pn53x       PN53X_REG_CIU_TxMode (Defines the transmission data rate and framing during transmission)
debug   libnfc.chip.pn53x       PN53X_REG_CIU_RxMode (Defines the transmission data rate and framing during receiving)
debug   libnfc.chip.pn53x       WriteRegister
debug   libnfc.bus.uart TX: 00 00 ff 08 f8 d4 08 63 02 80 63 03 80 59 00
debug   libnfc.bus.uart RX: 00 00 ff 00 ff 00
debug   libnfc.chip.pn53x       PN53x ACKed
debug   libnfc.bus.uart RX: 00 00 ff 02 fe
debug   libnfc.bus.uart RX: d5 09
debug   libnfc.bus.uart RX: 22 00
debug   libnfc.chip.pn53x       InRelease
debug   libnfc.bus.uart TX: 00 00 ff 03 fd d4 52 00 da 00
debug   libnfc.bus.uart RX: 00 00 ff 00 ff 00
debug   libnfc.chip.pn53x       PN53x ACKed
debug   libnfc.bus.uart RX: 00 00 ff 03 fd
debug   libnfc.bus.uart RX: d5 53
debug   libnfc.bus.uart RX: 00
debug   libnfc.bus.uart RX: d8 00
debug   libnfc.general  set_property_bool NP_ACTIVATE_FIELD False
debug   libnfc.chip.pn53x       RFConfiguration
debug   libnfc.bus.uart TX: 00 00 ff 04 fc d4 32 01 00 f9 00
debug   libnfc.bus.uart RX: 00 00 ff 00 ff 00
debug   libnfc.chip.pn53x       PN53x ACKed
debug   libnfc.bus.uart RX: 00 00 ff 02 fe
debug   libnfc.bus.uart RX: d5 33
debug   libnfc.bus.uart RX: f8 00
debug   libnfc.chip.pn53x       PowerDown
debug   libnfc.bus.uart TX: 00 00 ff 03 fd d4 16 f0 26 00
debug   libnfc.bus.uart RX: 00 00 ff 00 ff 00
debug   libnfc.chip.pn53x       PN53x ACKed
debug   libnfc.bus.uart RX: 00 00 ff 03 fd
debug   libnfc.bus.uart RX: d5 17
debug   libnfc.bus.uart RX: 00
debug   libnfc.bus.uart RX: 14 00

Then I've compiled ifdnfc and while service pcscd status gives me:

● pcscd.service - PC/SC Smart Card Daemon
     Loaded: loaded (/lib/systemd/system/pcscd.service; indirect; vendor preset: enabled)
     Active: active (running) since Thu 2022-02-10 15:30:57 GMT; 17s ago

and my /etc/reader.conf.d/libifdnfc looks like this:

FRIENDLYNAME "IFD-NFC"
LIBPATH /usr/local/lib/libifdnfc.so
CHANNELID 0

Unfortunately ifdnfc-activate returns:

debug   libnfc.general  log_level is set to 3
debug   libnfc.general  allow_autoscan is set to true
debug   libnfc.general  allow_intrusive_scan is set to false
debug   libnfc.general  1 device(s) defined by user
debug   libnfc.general    #0 name: "IFD-NFC", connstring: "pn532_uart:/dev/ttyUSB0:115200"
debug   libnfc.general  0 device(s) found using acr122_usb driver
debug   libnfc.general  0 device(s) found using pn53x_usb driver
Activating ifdnfc with "pn532_uart:/dev/ttyUSB0:115200"...
IFD-NFC is inactive.

I'm already on aarch64 bit, because on 32bit systemifdnfc-activate was returning Insufficient buffer

My Java program:

import java.util.List;

import javax.smartcardio.CardException;
import javax.smartcardio.CardTerminal;
import javax.smartcardio.TerminalFactory;

public class Simple {

  public static void main(String[] args) throws CardException {
    TerminalFactory factory = TerminalFactory.getDefault();
    List<CardTerminal> terminals = factory.terminals().list();
    System.out.println("Terminals: " + terminals);
  }
}

returns [] terminals... What can I do in order to make ifdnfc "active"? Kind regards

eximius313 commented 2 years ago

Ok, for future generations, terminals are not found due to some bug described here So I must run my app with java -Dsun.security.smartcardio.library=/usr/lib/aarch64-linux-gnu/libpcsclite.so.1 MyApp

nevetheless ifdnfc-activate still returns IFD-NFC is inactive. and no card is found. Neither from my app, neither from pcsc_scan:

Using reader plug'n play mechanism
Scanning present readers...
0: IFD-NFC 00 00

Thu Feb 10 21:54:14 2022
 Reader 0: IFD-NFC 00 00
  Event number: 0
  Card state: Card removed,
nzedler commented 1 year ago

I got a similar problem with my SCL3711 reader, it shows "Card removed" although nfc-list detects the a card.

The output of pcsc_scan after installing ifdnfc:

Using reader plug'n play mechanism
Scanning present readers...
0: SCM Micro SCL3711 Contactless Reader & NFC 00 00

Sun Nov 20 16:01:48 2022
 Reader 0: SCM Micro SCL3711 Contactless Reader & NFC 00 00
  Event number: 0
  Card state: Card removed,

vs. the output of nfc-list:

nfc-list uses libnfc 1.8.0
NFC device: SCM Micro / SCL3711-NFC&RW opened
1 ISO14443A passive target(s) found:
    ATQA (SENS_RES): 00 44
       UID (NFCID1): 04 9a 59 8a aa 6c 80
      SAK (SEL_RES): 08

Any hints on how to detect the card?

agorgl commented 1 year ago

Same here. I'm trying to bridge a pn532 nfc reader with pcscd to use a security token as a smartcard with gnupg (as scdaemon talks to pcscd for smartcard interactions).

After installing ccid libnfc pcsclite pcsc-tools ifdnfc-git packages in Arch, enabling pcscd.socket and setting up /etc/nfc/libnfc.conf with:

device.name = "IFD-NFC"
device.connstring = "pn532_uart:/dev/ttyUSB0"

and /etc/reader.conf.d/ifdnfc with:

FRIENDLYNAME "IFD-NFC"
LIBPATH      /usr/lib/libifdnfc.so
CHANNELID    0

All nfc-* tools from libnfc seem to be working, but pcsc_scan stays in:

Using reader plug'n play mechanism
Scanning present readers...
0: IFD-NFC 00 00

Wed Mar  8 13:24:46 2023
 Reader 0: IFD-NFC 00 00
  Event number: 0
  Card state: Card removed,
 \
agorgl commented 1 year ago

I think I just found out how this is supposed to be used! If I first fire up pcsc_scan to activate pcscd service (and keep it open) and call ifdnfc-activate after that I get:

2 NFC devices found, please select one:
[0] IFD-NFC   (pn532_uart:/dev/ttyUSB0)
[1] IFD-NFC 00 00     (pcsc:IFD-NFC 00 00)
>> 0
Activating ifdnfc with "pn532_uart:/dev/ttyUSB0"...
IFD-NFC is inactive.

Selecting the first option (0) enables the ifdnfc pcsc driver and scanning in pcsc_scan works!

I'm not sure though why it still reports the device as inactive, I'll have to dig into the source to find that out

agorgl commented 1 year ago

I found the issue! The ifdnfc-activate binary allocates a single byte receive buffer here: https://github.com/nfc-tools/ifdnfc/blob/0e48e8e107dc42f68fb472bb06d257503ad45b1d/src/ifdnfc-activate.c#L45 that in turn is passed by to SCardControl here: https://github.com/nfc-tools/ifdnfc/blob/0e48e8e107dc42f68fb472bb06d257503ad45b1d/src/ifdnfc-activate.c#L171 that calls the libpcsclite library here: https://github.com/LudovicRousseau/PCSC/blob/a98d3858602f94c5e9a0876353e02c0f2cd2dcaa/src/winscard_clnt.c#L2209 that sends a message on the pcscd here: https://github.com/LudovicRousseau/PCSC/blob/a98d3858602f94c5e9a0876353e02c0f2cd2dcaa/src/winscard_svc.c#L703 that is passed in the wincard implementation here: https://github.com/LudovicRousseau/PCSC/blob/a98d3858602f94c5e9a0876353e02c0f2cd2dcaa/src/winscard.c#L1301 that calls the ifdnfc's IFDHControl implementation through IFDControl here: https://github.com/LudovicRousseau/PCSC/blob/a98d3858602f94c5e9a0876353e02c0f2cd2dcaa/src/winscard.c#L1349 that seems to handle the request successfully, but we get a SCARD_E_INSUFFICIENT_BUFFER as a result, probably from here: https://github.com/LudovicRousseau/PCSC/blob/a98d3858602f94c5e9a0876353e02c0f2cd2dcaa/src/winscard_clnt.c#L2276

So increasing the receive buffer to fit the whole response (that contains the device name) fixes the inactive message problem!