Home > Articles > Programming > Java

  • Print
  • + Share This
From the author of

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.

Listing 2. Compiling the CardTest Applet

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.

Listing 3. Compiling the CardTest Applet

01 set _CLASSES=%JC_HOME%\lib\apduio.jar; . . .;%JC_HOME%\lib\api21.jar;%JC_HOME%\lib\capdump.jar;
02 xcopy /s %JC_HOME%\api21_export_files\*.* exp
03 java -classpath %_CLASSES% com.sun.javacard.converter.Converter -config CardTest.opt

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

-out EXP JCA CAP
-exportpath exp
-applet 0x1:0x0:0x0:0x0:0x1:0x3:0x1:0x0:0x1:0x1 com.marinilli.CardTest
com.marinilli
0x1:0x0:0x0:0x0:0x1:0x3:0x1:0x0:0x1 1.0

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:

java -classpath %_CLASSES% com.sun.javacard.converter.Exp2Text -classdir com/marinilli

Such a file is reported in Listing 5.

Listing 5. The Human-Readable Representation of the CardApplet EXP File

export file { // com/marinilli
 magic 00FACADE // in hex
 minor_version 1
 major_version 2
 constant_pool_count 2
 constant_pool {
  Constant_Utf8_info {
   tag 1
   length 19
   bytes com/marinilli
  }
  CONSTANT_Package_info {
   tag 13
   flags 0
   name_index 0 // com/marinilli
   minor_version 0
   major_version 1
   aid_length 9
   aid 0x1:0x0:0x0:0x0:0x1:0x3:0x1:0x0:0x1
  }
 }
 this_package 1
 export_class_count 0
 export_classes {
 }
}

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).

java -classpath %_CLASSES% com.sun.javacard.scriptgen.Main com\marinilli\javacard\test.cap -nobeginend 

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.

  • + Share This
  • 🔖 Save To Your Account