Writing Java Card applets is not a hard thing, but yes you need to be familiar with java card API and its standards to pass APDUs.
In this post I will try to explain the mandatory functions for the successful execution of the Java Card applet.
Following functions are necessary for an applet to execute and its installation etc.
In this post I will try to explain the mandatory functions for the successful execution of the Java Card applet.
Following functions are necessary for an applet to execute and its installation etc.
public static void install( byte [] bArray, short bOffset, byte bLength){} public void process(APDU apdu){} protected final void register(){} //or protected final void register( byte [] bArray, short bOffset, byte bLength){} throws SystemException public boolean selectingApplet(){} |
install()
To create an instance of the Applet subclass, the Java Card runtime environment will call this static method first.
All the initialization and memory allocations should be done in this method.
All the memory allocations should be done in this method or in the constructor of the class. It will prevent you from memory overflow error later on.
process()
This method is like a main method of a normal java class. After the installation the execution of APDUs takes place from this method. Each and every APDU comes in this method and get processed as its name suggest ;)
register()
In order to register an instance of the applet with the Java Card Runtime enviroment this method must be called. This method should be called within install() method.
register(parameters)
This method works same as above register() plus it assigns the specified AID bytes to the instance AID bytes.
selectingApplet()
This method is used by the applet process() method to distinguish the SELECT APDU command which selected this applet, from all other SELECT APDU commands which may relate to file or internal applet state selection.
It is a good programming practice that you should return 0x9000 when this function returns true
Ok, I hope you are now familiar with the most important functions of the java card applet. Your class (your own applet) will inherit these functions from the Applet class of thejavacard.framework package by extending the javacard.framework.Applet class.
Now if we sum-up all the above functions and write them in a way so that a complete applet can be written is as below:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
| import javacard.framework.APDU; import javacard.framework.Applet; import javacard.framework.ISO7816; import javacard.framework.ISOException; public class MyOwnApplet extends Applet { public static void install( byte [] bArray, short bOffset, byte bLength) { // GP-compliant JavaCard applet registration new MyOwnApplet().register(bArray, ( short ) (bOffset + 1 ), bArray[bOffset]); } public void process(APDU apdu) { // Good practice: Return 9000 on SELECT if (selectingApplet()) { return ; } byte [] buf = apdu.getBuffer(); switch (buf[ISO7816.OFFSET_INS]) { case ( byte ) 0x00 : break ; default : // good practice: If you don't know the INStruction, say so: ISOException.throwIt(ISO7816.SW_INS_NOT_SUPPORTED); } } } |
Nice article
ReplyDeletethanks
Delete