javacard - Default selected applet doesn't return right value -


i wanted answer this question wrote below program:

package test;  import javacard.framework.apdu; import javacard.framework.iso7816; import javacard.framework.applet; import javacard.framework.isoexception; import javacard.framework.util;  public class test extends applet {      public static final byte[] res = { (byte) 0x00, (byte) 0x00, (byte) 0x3b,             (byte) 0xad, (byte) 0x3f, (byte) 0x00, (byte) 0x01, (byte) 0x00,             (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x16,             (byte) 0xb3, (byte) 0x03, (byte) 0x06, (byte) 0x04, (byte) 0x00,             (byte) 0x83, (byte) 0x8a, (byte) 0x83, (byte) 0x8a, (byte) 0x00,             (byte) 0x03, (byte) 0x00, (byte) 0x00, (byte) 0x3b, (byte) 0xad,             (byte) 0x00, (byte) 0x00, (byte) 0x3b, (byte) 0xad, (byte) 0x2f,             (byte) 0x06, (byte) 0x02 };      public static void install(byte[] barray, short boffset, byte blength) {         new test.test()                 .register(barray, (short) (boffset + 1), barray[boffset]);     }      public void process(apdu apdu) {         if (selectingapplet()) {             return;         }          byte[] buf = apdu.getbuffer();          if (buf[iso7816.offset_cla] == 0xa0 && buf[iso7816.offset_ins] == 0xa4 && buf[iso7816.offset_p1] == 0x00 && buf[iso7816.offset_p2] == 0x00                 && buf[iso7816.offset_lc] == 0x02 && buf[iso7816.offset_lc+1] == 0x7f && buf[iso7816.offset_lc+2] == 0x20) {             isoexception.throwit((short) 0x9f23);         } else if (buf[iso7816.offset_cla] == 0xa0 && buf[iso7816.offset_ins] == 0xc0 && buf[iso7816.offset_p1] == 0x00                 && buf[iso7816.offset_p2] == 0x00 && buf[iso7816.offset_ext_cdata] == 0x23) {             util.arraycopynonatomic(res, (short)0, buf, (short)0, (short)35);             apdu.setoutgoingandsend((short)0, (short)35);          }                 else                     isoexception.throwit((short)0x9090);     } } 

and install default selected using globalplatformpro tool:

cmd> gp -install e:\soq.cap -default  cmd> 

well, now, expect have following communication :

>> in: 0xa0 a4 00 00 02 7f 20  << out: 0x9f 23  >> in : 0xa0 c0 00 00 23  << out: 0x00 00 3b ad 3f 00 01 00 00 00 00 00 16 b3 03 06 04 00 83 8a 83 8a 00 03 00 00 3b ad 00 00 3b ad 2f 06 02 

but in real, have below communication using opensc :

cmd> osc.exe -s a0a40000027f20 -s a0c0000023 using reader card: acs ccid usb reader 0 sending: a0 a4 00 00 02 7f 20 received (sw1=0x90, sw2=0x90) sending: a0 c0 00 00 23 received (sw1=0x90, sw2=0x90) 

update:

as dear @vojta said in answer, cast constants bytes below :

//. //. these parts didn't changed //.  public void process(apdu apdu) {     if (selectingapplet()) {         return;     }      byte[] buf = apdu.getbuffer();      if (buf[iso7816.offset_cla] == (byte)0xa0 && buf[iso7816.offset_ins] == (byte) 0xa4 && buf[iso7816.offset_p1] == (byte) 0x00&& buf[iso7816.offset_p2] == (byte) 0x00              && buf[iso7816.offset_lc] == (byte) 0x02 && buf[iso7816.offset_lc + 1] == (byte) 0x7f  && buf[iso7816.offset_lc + 2] == (byte) 0x20) {         isoexception.throwit((short) 0x9f23);     } else if (buf[iso7816.offset_cla] == (byte) 0xa0 && buf[iso7816.offset_ins] == (byte) 0xc0  && buf[iso7816.offset_p1] == (byte) 0x00              && buf[iso7816.offset_p2] == (byte) 0x00  && buf[iso7816.offset_ext_cdata] == (byte) 0x23 ) {         util.arraycopynonatomic(res, (short) 0, buf, (short) 0, (short) 35);         apdu.setoutgoingandsend((short) 0, (short) 35);      } else {         isoexception.throwit((short) 0x9090);     }  //. //. these parts didn't changed //. 

after installing above applet default selected applet, receive following reslts:

commandline> osc.exe -s a0a40000027f20 -s a0c0000023 using reader card: acs ccid usb reader 0 sending: a0 a4 00 00 02 7f 20 received (sw1=0x9f, sw2=0x23) sending: a0 c0 00 00 23 received (sw1=0x90, sw2=0x90) 

well, see above, receive correct answer first apdu command, answer of second apdu command not expected.

i modify second comparison section below :

//. //. these parts didn't changed //.  else if (buf[iso7816.offset_cla] == (byte) 0xa0 && buf[iso7816.offset_ins] == (byte) 0xc0  && buf[iso7816.offset_p1] == (byte) 0x00                  && buf[iso7816.offset_p2] == (byte) 0x00  && buf[iso7816.offset_p2+1] == (byte) 0x23 ) {   //. //. these parts didn't changed //. 

well, see replaced buf[iso7816.offset_ext_cdata] buf[iso7816.offset_p2+1], , works wanted:

commandline> osc.exe -s a0a40000027f20 -s a0c0000023 using reader card: acs ccid usb reader 0 sending: a0 a4 00 00 02 7f 20 received (sw1=0x9f, sw2=0x23) sending: a0 c0 00 00 23 received (sw1=0x90, sw2=0x00): 00 00 3b ad 3f 00 01 00 00 00 00 00 16 b3 03 06 ..;.?........... 04 00 83 8a 83 8a 00 03 00 00 3b ad 00 00 3b ad ..........;...;. 2f 06 02                                        /.. 

i don't have idea why must use iso7816.offset_p2+1 instead of iso7816.offset_ext_cdata! thought 0x23 in a0c0000023 apdu command, must consider le!(and not lc).

a0 , a4 values greater 128. java card bytes signed, unfortunately. have cast short constants bytes before comparison.


Comments