Command APDU returning 6985 (Conditions of use not satisfied) in result

2.9k Views Asked by At

I am working on reading a smart card in Java. When I execute the following code below, the card returns 6985 (Conditions of use not satisfied) as a result.

  TerminalFactory factory = TerminalFactory.getDefault();
  List<CardTerminal> terminals = factory.terminals().list();
  System.out.println("Terminals: " + terminals);

  if (terminals != null && !terminals.isEmpty()) {
   // Use the first terminal
   CardTerminal terminal = terminals.get(0);

   // Connect with the card
   Card card = terminal.connect("*");
   System.out.println("card: " + card);
   CardChannel channel = card.getBasicChannel();

   CommandAPDU commandAPDU = new CommandAPDU(0x00, 0xA4, 0x00, 0x0C,
   new byte[]{0002},0,0x01);

   ResponseAPDU responseCheck = channel.transmit(commandApdu);
   System.out.println(responseCheck.getSW1()+":"+responseCheck.getSW2()+":"+
   commandApdu.toString());

The parameters provided by the client are:

  • CLA = 00
  • INS = A4
  • P1 = 00
  • P2 = 0C
  • LC = 02
  • Data = XXXX (The data passed here is File Identifier),As I want to select EF file so EFID for the file given by client is 0002
1

There are 1 best solutions below

0
On
CommandAPDU commandAPDU = new CommandAPDU(0x00, 0xA4, 0x00, 0x0C, new byte[]{0002},0,0x01);

won't do what you expect it to do.

new byte[]{0002} will give you a byte array with one byte of value 2. Also, the ,0,0x01); (last two parameters) will make the constructor only pick that one byte from the DATA array. So your APDU will look like this:

+------+------+------+------+------+------+------+
| CLA  | INS  | P1   | P2   | Lc   | DATA | Le   |
| 0x00 | 0xA4 | 0x00 | 0x0C | 0x01 | 0x02 | ---  |
+------+------+------+------+------+------+------+

This is probably not what you expected. Did you want new byte[]{0, 2} instead? Using

CommandAPDU commandAPDU = new CommandAPDU(0x00, 0xA4, 0x00, 0x0C, new byte[]{0, 2}, 256)

would result in the following APDU (note that Le is present and set to 0 (Ne = 256); Lc is automatically infered from the size of the DATA array):

+------+------+------+------+------+-----------+------+
| CLA  | INS  | P1   | P2   | Lc   | DATA      | Le   |
| 0x00 | 0xA4 | 0x00 | 0x0C | 0x02 | 0x00 0x02 | 0x00 |
+------+------+------+------+------+-----------+------+

Or using

CommandAPDU commandAPDU = new CommandAPDU(0x00, 0xA4, 0x00, 0x0C, new byte[]{0, 2})

would result in the following APDU (note that Le is absent (Ne = 0); Lc is automatically infered from the size of the DATA array):

+------+------+------+------+------+-----------+------+
| CLA  | INS  | P1   | P2   | Lc   | DATA      | Le   |
| 0x00 | 0xA4 | 0x00 | 0x0C | 0x02 | 0x00 0x02 | ---  |
+------+------+------+------+------+-----------+------+