javacard - How to understand Java Card APDU commands? -
i trying learn programming java cards. have started it. but, not finding complete e-book.
how understand apdu commands?
for example, how can understand meaning if cla = 10101010? or, if ins = 10101010?
is there intuitive guide available on net?
apdu commands queue of binary numbers in following form:
cla | ins | p1 | p2 | lc | cdata | le
the first 4 sections, i.e cla , ins , p1 , p2 mandatory in apdu commands , each 1 has 1 byte length. these one-byte-length sections stand class, instruction, parameter1 , parameter2 respectively.
the last 3 sections, i.e lc , cdata , le optional.lc encoding of nc, encoding of length of cdata field. le encoding of ne, encoding of maximum response data may send. based on presence or absence of these sections, have 4 case apdu commands, below:
- case1:
cla | ins | p1 | p2
- case2:
cla | ins | p1 | p2 | le
- case3:
cla | ins | p1 | p2 | lc | data
- case4:
cla | ins | p1 | p2 | lc | data | le
the length of cdata different different commands , different applets. based on length of cdata (i.e lc) , length of maximum response data may send (i.e le), have type of apdu commands:
- normal/short apdu commands, when lc and le smaller
0xff
- extended length apdu commands, when lc and/or le greater
0xff
.
so length of these sections have:
lc : 1 byte short apdu commands , 3 byte (they specify length, because enough) extended apdu commands.
data : different lengths.
le : same lc.
how can understand apdu commands?
answer:
when write applet, specify response of applet different apdu commands receive in future. card manager applet also. commands support defined in card's specifications/datasheet. cards globalplatform , iso7816 compliant, must support mandatory apdu commands defined in these documents. example, 0xa4
defined select file command in iso7816-4 standard, if see apdu xx a4 xx xx
sending card manager, can conclude related select file
.
note can choose 1 value different functions in different applets. example in following, applet1 return 0x6990
in reception of 00 b0 xx xx
apdu commands, while applet2 return 0x6991
in reception of same command:
applet1:
public class soq extends applet { private soq() { } public static void install(byte barray[], short boffset, byte blength) throws isoexception { new soq().register(); } public void process(apdu arg0) throws isoexception { byte buffer[] = arg0.getbuffer(); if(buffer[iso7816.offset_cla]==0x00 && buffer[iso7816.offset_ins]==0xb0){ isoexception.throwit((short)0x6990); } } }
output:
opensc: opensc-tool.exe -s 00a404000b0102030405060708090000 -s 00b00000 -s 00b00 100 using reader card: acs ccid usb reader 0 sending: 00 a4 04 00 0b 01 02 03 04 05 06 07 08 09 00 00 received (sw1=0x90, sw2=0x90) sending: 00 b0 00 00 received (sw1=0x69, sw2=0x90) sending: 00 b0 01 00 received (sw1=0x69, sw2=0x90)
applet2:
public class soq extends applet { private soq() { } public static void install(byte barray[], short boffset, byte blength) throws isoexception { new soq().register(); } public void process(apdu arg0) throws isoexception { byte buffer[] = arg0.getbuffer(); if(buffer[iso7816.offset_cla]==0x00 && buffer[iso7816.offset_ins]==0xb0){ isoexception.throwit((short)0x6991); } } }
output:
opensc: opensc-tool.exe -s 00a404000b0102030405060708090000 -s 00b00000 -s 00b00 100 using reader card: acs ccid usb reader 0 sending: 00 a4 04 00 0b 01 02 03 04 05 06 07 08 09 00 00 received (sw1=0x90, sw2=0x00) sending: 00 b0 00 00 received (sw1=0x69, sw2=0x91) sending: 00 b0 01 00 received (sw1=0x69, sw2=0x91)
so final , short answer question (how can understand apdu commands?) is:
you dealing applet?
you defined supported commands , forms, yourself!
you dealing applet (card manager, example)?
you need source code of applet or documentation supported commands , forms or standard/specification that applet compliant (global platform card managers example).
note: have same apdu responses.
Comments
Post a Comment