OpenSC icon indicating copy to clipboard operation
OpenSC copied to clipboard

npa-tool run fail with "Error: Incorrect parameters in APDU" in second time if successes in first time

Open AKing23T opened this issue 5 years ago • 9 comments

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

AKing23T avatar Mar 04 '21 06:03 AKing23T

The card may expect an encrypted APDU although it has been reset... Could you additionally give the PC/SC log?

frankmorgner avatar Mar 04 '21 09:03 frankmorgner

pcscd.log

AKing23T avatar Mar 04 '21 09:03 AKing23T

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);

frankmorgner avatar Mar 04 '21 10:03 frankmorgner

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).

Jakuje avatar Mar 04 '21 21:03 Jakuje

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.

frankmorgner avatar Mar 04 '21 22:03 frankmorgner

①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

AKing23T avatar Mar 05 '21 03:03 AKing23T

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?

frankmorgner avatar Mar 06 '21 22:03 frankmorgner

I add the code to select the MF and it is ok everytime.

The card is the real spanish eID. 1 2

AKing23T avatar Mar 08 '21 02:03 AKing23T