A Java Card Primer_ Part 2—a Java Card Example _ a Demonstrative Applet
A Java Card Primer_ Part 2—a Java Card Example _ a Demonstrative Applet
In this second of two articles on the Java Card platform, Mauro Marinilli shows you a simple yet complete example of a Java
Card applet development.
In order to run the code in this example, it is necessary to have installed the Java Card Development Kit Version 2.1.2 or higher from Sun. The kit is
available for downloads at http://www.javasoft.com/products/javacard/.
As for the scripts, you will use simple Windows scripting that can be adapted easily to any Unix-like command-line environment.
A Demonstrative Applet
Listing 1 shows an example of a Java Card applet. It is very simple, but will serve the purpose of illustrating the deployment of Java Card applets from
the Java source code to the final data transfer into the chip card.
2. Run the converter tool provided with the development kit, which obtains a CAP file.
3. Convert the CAP file in a sequence of low-level APDU commands to install the applet onto the card.
Listing 1 shows the CardTest applet. It is only a demonstrative applet— it does not perform any useful task or use any standard APDU type in the
processing. Nevertheless, it helps you follow all the main steps from the source development to a basic, on-card applet installation.
package com.marinilli;
import javacard.framework.*;
/**
* An example Java Card Applet
* This applet writes back dummy byte sequences.
* It shows the Java Card applet development process only.
*
* @author Mauro Marinilli
*/
/**
* Constructor.
* Only this class's install method can create the applet object.
*/
private CardTest() {
//perform some initialization here
// ...
register();//register this instance
}
/**
* Installs this applet.
* @param byteArray the array containing installation parameters
* @param offset the starting offset in byteArray
* @param length the length in bytes of the parameter data in byteArray
*/
public static void install(byte[] byteArray, short offset, byte length) {
new CardTest();
}
/**
* Implementation of the standard method for processing an incoming APDU.
* @param apdu the incoming APDU
* @exception ISOException with ISO 7816-4 response bytes
*/
public void process(APDU apdu) {
byte buffer[] = apdu.getBuffer();
if (buffer[ISO7816.OFFSET_CLA] == THIS_CLA) {
switch (buffer[ISO7816.OFFSET_INS]) {
case INITIALIZE_TRANSACTION:
writeBack(apdu, INIT_SEQUENCE);
break;
case COMPLETE_TRANSACTION:
writeBack(apdu, COMPLETE_SEQUENCE);
break;
case INITIALIZE_UPDATE:
writeBack(apdu, INIT_UPDATE_SEQUENCE);
break;
case COMPLETE_UPDATE:
writeBack(apdu, COMPLETE_UPDATE_SEQUENCE);
break;
default:
ISOException.throwIt(ISO7816.SW_INS_NOT_SUPPORTED);
}
}
}
/**
* An example method that writes back a given byte array through the apdu.
*/
protected void writeBack(APDU apdu, byte[] bytes) {
byte buffer[] = apdu.getBuffer();
// output header
apdu.sendBytes( (short)0, (short) 3);
// writes data
apdu.sendBytesLong( bytes, (short) 0, (short) 0 );
}
Because we are interested in providing the overall picture, not in the details of Java Card programming, the proposed code in Listing 1 is not
discussed.
Development Steps
Listing 2 shows the commands to compile the example. Note the use of the -g option, which is needed because the converter tool determines the
local variable types by checking the LocalVariableTable attribute within the .class file. Such an attribute is generated only if the -g option is used.
set JC_HOME=...
javac -g -classpath % JC_HOME%\lib\api21.jar CardTest.java
Then, when the CardTest.class file is produced, you need to transform it into a CAP file by using the converter utility, together with an optional input
file (called in the example CardTest.opt) that declares all input options for the conversion process. Such a file is shown in Listing 3.
Line 1 of Listing 3, for brevity, omitted to mention all the JAR libraries found in the /lib directory of the installed Java Card Development Kit. In line 2,
the export files for the standard libraries are copied into a temporary directory, from which you will access them from the -exportpath option in
Listing 4. EXP files are much like C header files—needed for linking and other utility purposes.
Listing 4. The Converter Input Options File Cardtest.opt
The options file (listed in Listing 4) instructs the converter tool to produce EXP, JCA, and CAP files as output of the conversion. JCA files are pseudo-
assembly text files from which CAP files can be created (they are not used here). The -exportpath command-line switch defines where the required
EXP files (needed for linking libraries) are located. The -applet option defines the applet's AID and its java class. In this example, the AID is totally
invented. Finally, the package name, package AID, and major version, followed by a minor version (separated by a dot) are provided to the converter.
Sun also supplies a tool for viewing the contents of an EXP file. When invoking it, as in the following, it produces the text representation of the EXP
file created by the converter from the applet class file:
You still have to physically transfer the CAP file onto your smart card. Sun's development kit provides a utility for sending APDUs to the smart card.
The last step is to create the APDUs to be sent to the smart card. The installer itself has been implemented as an applet.
Using the follow command, you launch the script generator (the -nobeginend option avoids including the standard CAP Begin and CAP End APDU
commands that you have to customize later).
As output, you obtain the list of APDU commands (a sequence of bytes) that define your CAP file.
Modify this latter file by cutting the text header and footer, leaving only the APDU commands and other commands, as specified in Sun's
documentation.
Then, you obtain another generated file, given in input at a suitable CAD via the APDUtool, which installs your applet on the card in a three-step
sequence:
1. The standard installer applet (already resident on the card) is instructed to download the applet using an APDU sequence.
2. Data is transferred via APDUs to the installer applet that writes it in memory.
3. Finally, always using the installer applet, you create an instance of your newly downloaded class that is ready for execution.
There are also deployment options for Java Card applets, such as ROM applets.
Conclusion
This article discussed the Java Card platform in some detail. The interested reader can compile the class source code here, or visit the Sun Java Card
Web site for more information.
Any feedback on the topics discussed here is warmly welcome. Please contact me through my Web site at http://www.marinilli.com/.