Java Card Applet Development

In this blog we will be learning how to develop a Java Card Applet. First, let us understand few terms:

JavaCard – denotes a Java Card technology-enabled smart card. Java Card technology allows applets written in the Java language to be executed on a smart card. The minimum system requirement is 16 kilobytes of read-only memory (ROM), 8 kilobytes of EEPROM, and 256 bytes of random access memory (RAM).

The system architecture of JavaCard is shown below:

architecture-100158125-orig

When a Java Card is inserted into a card acceptance device (CAD), the CAD selects an applet on the card and sends it a series of commands to execute. Each applet is identified and selected by its application identifier (AID). Commands such as the selection command are formatted and transmitted in the form of application protocol data units (APDUs). Applets reply to each APDU command with a status word (SW) that indicates the result of the operation. An applet can optionally reply to an APDU command with other data.

As with any other software development, first architect the applet, you should first go through the design phase. In this phase you will:

  • Specify the functions of the applet
  • Request and assign AIDs to both the applet and the package containing the applet class
  • Design the class structure of the applet programs
  • Define the interface between the applet and the terminal application

Specifying AIDs
Most applications with which you are familiar are named and identified by a string name. In Java Card technology, however, each applet is identified and selected by an AID. Also, each Java package is assigned an AID. This is because a package, when loaded on a card, is linked with other packages, which have already been placed on the card via their AIDs. This naming convention is in conformance with the smart card specification as defined in ISO 7816.
An AID is a sequence of bytes between 5 and 16 bytes in length.

Application identifier (AID)

National registered application provider (RID)

Proprietary application identifier extension (PIX)

5 bytes

0 to 11 bytes

ISO controls the assignment of RIDs to companies, with each company obtaining its own unique RID from the ISO. Companies manage assignment of PIXs for AIDs.

Defining the class structure and method functions of the applet
A Java Card applet class must extend from the javacard.framework.Applet class. This class is the super class for all applets residing on a Java Card. It defines the common methods an applet must support in order to interact with the JCRE during its lifetime.

Public and protected methods defined in the class javacard.framework.Applet

Method summary
deselect ()

Called by the JCRE to inform the currently selected applet that another (or the same) applet will be selected.

public Shareable getShareableInterfaceObject (AID client AID, byte parameter)

Called by the JCRE to obtain a sharable interface object from this server applet on behalf of a request from a client applet.

public static void install (byte[] bArray, short bOffset, byte bLength)

The JCRE calls this static method to create an instance of the Applet subclass.

public abstract void process (APDU apdu)

Called by the JCRE to process an incoming APDU command.

protected final void register ()

This method is used by the applet to register this applet instance with the JCRE and assign the default AID in the CAD file to the applet instance.

protected final void register (byte[] bArray, short bOffset, byte bLength)

This method is used by the applet to register this applet instance with the JCRE and to assign the specified AID in the array bArray to the applet instance.

public boolean select ()

Called by the JCRE to inform this applet that it has been selected.

protected final boolean selectingApplet ()

This method is used by the applet

process()

method to distinguish the

SELECT APDU

command that selected this applet from all other

SELECT APDU

APDU commands that may relate to file or internal applet state selection.

Methods defined in this class are called by the JCRE when the JCRE receives APDU commands from the CAD.
After the applet code has been properly loaded on a Java Card and linked with other packages on the card, an applet’s life starts when an applet instance is created and registered with the JCRE’s registry table. An applet must implement the static method install() to create an applet instance and register the instance with the JCRE by invoking one of the two register() methods. The install() method takes a byte array as a parameter. This array contains the installation parameters for initializing or personalizing the applet instance.

In the process() method, the applet interprets each APDU command and performs the task specified by the command. For each command APDU, the applet responds to the CAD by sending back a response APDU, which informs the CAD of the result of processing the command APDU. The process() method in class javacard.framework.Applet is an abstract method: a subclass of the Applet class must override this method to implement an applet’s functions.

The getShareableInterfaceObject method is intended for inter-applet communication. It is invoked by a client applet to request a sharable interface object from the server applet. The default implementation of this method returns null.

The applet processes an APDU command by invoking methods on the APDU object. In general, the applet performs the following steps:

Step 1. Retrieve the APDU buffer
The applet invokes the getBuffer() method to obtain a reference to the APDU buffer, which contains the message.

Step 2. Receive data
If the command APDU contains optional data, the applet must direct the APDU object to receive incoming data by invoking the setIncomingAndReceive() method.

Step 3. Return data
After processing the command APDU, the applet can also return data to the CAD in the response APDU.

Step 4. Return status word
Upon a successful return from the process() method, the JCRE automatically sends 0x9000 to indicate normal processing. At any point, if the applet detects any error, the applet can throw an ISOException by invoking the static method ISOException.throwIt(short reason).

With this blog you have the basic concepts of java card technology to get you started writing your own java card applets. For the specifications of the Java Card APIs, the Java Card Virtual Machine, and the Java Card Run time Environment, you can visit:
Java Card Technology

Keep Coding!

Chao!

 

Leave a comment

Create a free website or blog at WordPress.com.

Up ↑