npa-tool run fail with "Error: Incorrect parameters in APDU" in second time if successes in first time
Problem Description
the command to run is : ./npa-tool --can="xxxxxx" --read-dg1
In the first time, it returns success, and then I run it again,but the console shows "Error: Incorrect parameters in APDU", the sw value is 0x6987. If I continue to run again, it returns success again. The detail shows in the log file.
Proposed Resolution
the command should return success in the second time.
Steps to reproduce
Logs
The card may expect an encrypted APDU although it has been reset... Could you additionally give the PC/SC log?
Hmm, the card is reset at the end of the first run, which should reset the SM state. There's also nothing else interfering with the card. I'm not sure what's causing the trouble. You could try the following change:
diff --git a/src/tools/npa-tool.c b/src/tools/npa-tool.c
index 65ab73a0..2a451a7b 100644
--- a/src/tools/npa-tool.c
+++ b/src/tools/npa-tool.c
@@ -392,6 +392,8 @@ main (int argc, char **argv)
if (r)
goto err;
+ sc_reset(card, 1);
+
EAC_init();
if (cmdline.cvc_dir_given)
EAC_set_cvc_default_dir(cmdline.cvc_dir_arg);
In opensc.conf, this is handled by disconnect_action = reset. I think at this moment, we have default leave, which would explain this behavior (unless npa-tool or something would do explicit reset after the SM connection would be closed).
npa-tool already explicitly resets the card, when it is done to destroy the secure channel. This can also seen in the PC/SC log. Here, I'm suggesting to start with a reset right after the tool has connected to the card.
①I change the disconnect_action = reset in the opensc.conf, and the error exist still.
②I change the npa-tool.c file to add sc_reset(card, 1), and the error exist still.
00000003 [140220765124352] APDU: 00 B0 9C 00 DF 00000003 [140220765124352] ifdhandler.c:1332:IFDHTransmitToICC() usb:096e/0685:libudev:0:/dev/bus/usb/001/024 (lun: 0) 00106787 [140220765124352] SW: 69 87
That's quite confusing. You could try selecting MF first:
diff --git a/src/tools/npa-tool.c b/src/tools/npa-tool.c
index 65ab73a02..a4b6c6eac 100644
--- a/src/tools/npa-tool.c
+++ b/src/tools/npa-tool.c
@@ -392,6 +392,8 @@ main (int argc, char **argv)
if (r)
goto err;
+ sc_select_file(card, sc_get_mf_path(), NULL);
+
EAC_init();
if (cmdline.cvc_dir_given)
EAC_set_cvc_default_dir(cmdline.cvc_dir_arg);
what kind of card are you using? Do you have some "official middleware", which you could analyze for further commands that are sent to the card before reading files from it?
I add the code to select the MF and it is ok everytime.
The card is the real spanish eID.
