2

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
Michael Roland
  • 39,663
  • 10
  • 99
  • 206
  • 3
    Are you sure you pasted the code you are trying to run? There seems to be comma missing between P1 and P2. Also your AID seems 4 (or 5) byte long, so you will not have LC=02. Also, is your DF/EF equal to 010201FF or 01000201FF? You have 0x0002 casted to byte so it will be only 0x02. In the end you have just 4 bytes (if this is supposed to be AID I guess it should be at least 5 byte long to be ISO7816-5 compliant). – Michal Gluchowski Nov 13 '18 at 15:28
  • @MichalGluchowski I have edited and corrected code but there are some queries, as I have to select file and the EF file id given for it is 0002 which should be passed as data parameter. what should be the correct APDU command for it? – Jawad Hassan Soomro Nov 14 '18 at 06:33
  • @MichaelRoland As mentioned, File Identifier should be passed as data parameter, The value is 0002, so how should it be passed?The LC parameter is also necessary as per instruction of client. – Jawad Hassan Soomro Nov 14 '18 at 18:19
  • You should pass it exactly as I showed you `new byte[]{0, 2}`. – Michael Roland Nov 14 '18 at 19:00
  • and what about passing LC parameter , i don't have LE parameter given by Client.@MichaelRoland – Jawad Hassan Soomro Nov 14 '18 at 19:13
  • You don't pass Nc (or Lc) directly. The constructor automatically infers this from the length of the DATA byte array. – Michael Roland Nov 15 '18 at 07:46

1 Answers1

1
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 | ---  |
+------+------+------+------+------+-----------+------+
Michael Roland
  • 39,663
  • 10
  • 99
  • 206