The APDU communication protocol is used to communicate with the ME mobile phone and comm for project needs. In the process of practice encountered some problems, first recorded as follows.
The APDU protocol, which is the application layer protocol between the smart card and the reader, defines the structure format of the protocol in Iso7816-4[7]. The APDU data has two structures, the APDU structure used by the reader is command APDU, and the APDU structure used in the smart card is in response to APDU,R-APDU (Reponse APDU).
Command APDU
Required parts: CLA, INS, P1, P2
Optional section: LC, data segment, LE
The CLA determines the category of APDU, INS determines which directives to execute, and P1 and P2 are parameters.
The LC determines the length of the data segment, which is the data sent to the smart card, and Le determines the number of bytes the reader expects the smart card to respond to.
Response APDU
Optional section: Data segment
Required section: SW1, SW2
The length of the data segment is determined by the Le command APDU.
SW1 and SW2 are state words.
Card Side APDU Programming practice
Javacard has a series of APDU processing methods that can be called, and details can be viewed in the corresponding documentation. First, I will briefly record some of the problems I have encountered in the course of practice and the corresponding answers.
Before processing a APDU command, first call the GetBuffer command to get a reference to the APDU buffer array, through which the contents of the APDU buffer array can be accessed through a reference to the array.
Public void process (APDU APDU) { byte[] buffer = apdu.getbuffer (); Switch (buffer[iso7816. Offset_ins]) { } }
The first thing to note is the two point, first: When you get a reference buffer array to buffer, note that buffer needs to be defined in the method, which is defined as a local array. Second: The buffer array obtained at this time contains only the command header of APDU, i.e. CLA, INS, P1, P2,P3, excluding subsequent data (that is, the data after P3). To accept the full APDU instruction, you need to use
byte buffer[]=apdu.getbuffer (); short bytesread=apdu.setincomingandreceive ();
Note Apdu.setincomingandreceive () returns the data length, which is the length of the data behind the command header, which is the value of the LC.
Java Smart card APDU learning notes