3 Running the Simulator and the Samples

A number of example programs are provided with the development kit.

All of these samples, showing up basic functionality, are located under JC_HOME_SIMULATOR\samples.

This chapter contains the following sections:

Java Card Development Kit Simulator Command Line

The Simulator supports the following command line parameters.

Table 3-1 Command Line Parameters

Parameter Description
-h or -help Prints help screen showing all available command line options.
-f=<file> Read (more) config options from the file.
-log_level=<level> Selects the level of details provided in the log. Log detail levels are: finest, finer, fine, config, info (default), warning, severe
-log_groups=<groups> List the groups to include in logs. A comma separated list of groups in [hal,gp,jcvm,jcre,jcapi,jcdb]
-i=<input file> Specifies the input file to load a binary image into Non-Volatile Memory.
-o=<output file> Specifies the output file to store the binary image for Non-Volatile Memory.
-enable-factory-reset Enables the proprietary factory reset APDU. The APDU is disabled by default. See Factory Reset APDU.
-iin=<hexstring> Initializes the Issuer Identification Number (8 byte max).
-cin=<hexstring> Initializes the Card Image Number (10 bytes max).
-lcEncoding=<type> Sets the APDU Logical Channel encoding: type4 or type4type16 (default).
-p=<number> Starts the Simulator on the specified port number (default is 9025).
-enable-preview Allow classes to depend on preview features of this release.
-debug_port=<number> Starts the debugger on the specified port number.

For example, on Linux:

$ ./jcsl -p=9000 -log_level=finest
INFO   |msg|000001|grp:hal | Oracle Java Card Simulator (v26.1 - <release date>)
INFO   |msg|000002|grp:hal |  - Java Card v3.2
INFO   |msg|000003|grp:hal |  - GP Card v2.3

Example config file for option “-f”:

# Set communication port
-p=9025
# Set log level
-log_level=finest
# Set debug port
-debug_port=8000

For example, on Windows:

>jcsw.exe -p=9000 -log_level=finest
INFO   |msg|000001|grp:hal | Oracle Java Card Simulator (v26.1 - <release date>)
INFO   |msg|000002|grp:hal |  - Java Card v3.2
INFO   |msg|000003|grp:hal |  - GP Card v2.3

Factory Reset APDU

To enable the factory reset APDU, start the Simulator with the -enable-factory-reset option.

On Linux, use:

$ ./jcsl -enable-factory-reset

On Windows, use:

>jcsw.exe -enable-factory-reset

Send the following proprietary APDU on the basic logical channel to perform the reset.

80 72 11 22 00

The APDU has class byte 80, instruction byte 72, P1 11, P2 22, and an empty command-data field (Lc equal to 00). On success, the Simulator returns 9000, clears non-volatile memory, and reinitializes the card. This is destructive: installed packages, applet instances, and persisted application data are removed.

The command requires no APDU data. A non-zero Lc returns 6700 (SW_WRONG_LENGTH), and a P1 or P2 value other than 11 and 22 returns 6B00 (SW_WRONG_P1P2).

How to Run the Samples

Note that before you can run the samples, the file JC_HOME_SIMULATOR/samples/client.config.properties needs to be configured accordingly. For example, lines like these:

A000000151000000_scp03enc_<KVN>=<Enter your 32-bit enc key here>
A000000151000000_scp03mac_<KVN>=<Enter your 32-bit mac key here>
A000000151000000_scp03dek_<KVN>=<Enter your 32-bit dek key here>

must be changed to something like this:

A000000151000000_scp03enc_10=1111111111111111111111111111111111111111111111111111111111111111
A000000151000000_scp03mac_10=2222222222222222222222222222222222222222222222222222222222222222
A000000151000000_scp03dek_10=3333333333333333333333333333333333333333333333333333333333333333

For additional information on configuring these properties, refer to Configuring the Java Card Development Kit Simulator.

Each sample directory contains an applet folder and a client folder. You can use the Eclipse Plug-in or the script files (build.bat / run.bat for Windows, build.sh / run.sh for Linux) to be used from the command line to build and run the samples. In either case, the outcome is the same: The development kit tools are used to convert and verify the class files.

The Java Card runtime environment, jcsw.exe (on Windows), jcsl (on Linux) respectively, simulates a Java Card 3.2 platform on a smart card. Applets are installed into the runtime environment, and it simulates interaction with a card reader.

To build and run the samples, refer to Running the Samples in Eclipse.

Building and Running the Samples from the Command Line

All available Java Card samples are located under the JC_HOME_SIMULATOR\samples directory. Each sample resides in a subdirectory and consists of its own set of batch/shell script files for creation and execution on Windows and Linux.

Important: The batch/shell script files in the JC_HOME_SIMULATOR\samples directory are shared components used by the scripts in each sample application’s subdirectory. These files are not intended to be executed directly.

How to build and run a sample:

  1. In a command prompt, navigate to the desired sample directory and execute build.bat or ./build.sh depending on your operating system, to build all necessary modules.
  2. In a separate command prompt, start the Java Card Simulator by running the executable, depending on your operating system:
    • %JC_HOME_SIMULATOR%\runtime\bin\jcsw.exe
    • ${JC_HOME_SIMULATOR}/runtime/bin/jcsl
  3. In a sample application’s subdirectory, execute the run script.
    • By default, running run.bat or ./run.sh (depending on your operating system) connects to the Simulator using its internal socket interface.

    • On Windows, if you have installed the optional Oracle Java Card PCSC Driver, a sample can communicate indirectly with the Simulator through the PCSC driver. To use this driver, include the pcsc parameter with the virtual card reader’s name to be used:

      run.bat "pcsc:Oracle Java Card PCSC Reader 0"

      Refer to Windows – Install and Set Up the PCSC Driver (optional) for additional information.

    • On Linux, if you have installed the optional PCSC-Lite and IFDHandler (see Linux - Install and Set Up the PCSC-Lite and IFD Handler), a sample can communicate indirectly with the Simulator through PCSC-Lite. To use this driver, include the pcsc parameter with the virtual card reader’s name to be used:

      ./run.sh "pcsc:Oracle_Java_Card_PCSC_Reader_0 00 00"

      Note:

      Since the name of the virtual card reader may contain spaces, you must enclose the entire name (including the leading ”pcsc:” string) in double quotation marks.

Running the Samples

Before running the samples, please ensure you have read the Configuration Before Running the Samples section. The following sections describe the Java Card Development Kit samples in order of increasing complexity and provide procedures for running them:

HelloWorld Sample

The HelloWorld sample demonstrates the base structure of a Java Card applet.

Follow one of these sets of instructions to run this sample:

Running the HelloWorld Sample from the Command Line

To run the HelloWorld sample:

  1. Open a Command Prompt window and perform the following:

    1. Navigate to the JC_HOME_SIMULATOR\runtime\bin directory.

    2. Start the Simulator by entering one of the following commands at the command prompt:

      jcsw (on Windows systems) or ./jcsl (on Linux systems).

  2. Open a second Command Prompt window and perform the following:

    1. Navigate to the JC_HOME_SIMULATOR\samples\HelloWorld directory.

    2. Enter either build.bat or ./build.sh (according to your operating system) in order to build Java Card applet and Java client.

    3. Enter either run.bat or ./run.sh (according to your operating system) to load the applet into the Simulator, send all APDUs and clean up afterwards.

Channels Sample

The Channels sample demonstrates the behavior of Java Card technology-based logical channels by showing how two applets that interact with each other can each be selected for use at the same time.

The Channels sample also demonstrates the use of ExtendedLength APDU.

The Channels sample mimics the behavior of a device connected to a network service. A connection manager tracks whether the device is connected to the service and whether the connection is local or remote.

While it is connected, the user’s account is debited on a unit of time basis. The debit rate is based on whether the connection is local or remote.

The sample employs two applets to simulate the behavior of logical channels:

When the user turns on the device, the ConnectionManager applet is selected. The ConnectionManager implements the ExtendedLength interface to handle APDUs with larger data segments such as the ones used for key exchange in the sample. Every unit of time the terminal sends a message containing the area code to the card.

When the user wants to use the service, the AccountAccessor applet is selected on another logical channel so that the terminal can query the balance. The AccountAccessor can return the balance only if the ConnectionManager is active. The ConnectionManager applet sets the connection and tracks the connection status.

Based on the value of an area code variable, the ConnectionManager determines whether the connection is local or remote. AccountAccessor uses this information to debit the account at the appropriate rate. The connection is disabled when the user completes the call or when the account is deleted

Follow one of these sets of instructions to run this sample:

Running the Channels Sample from the Command Line

To run the Channels sample:

  1. Open a Command Prompt window and perform the following:

    1. Navigate to the JC_HOME_SIMULATOR\runtime\bin directory.

    2. Start the Simulator by entering one of the following commands at the command prompt:

      jcsw (on Windows systems) or ./jcsl (on Linux systems).

  2. Open a second Command Prompt window and perform the following:

    1. Navigate to the JC_HOME_SIMULATOR\samples\Channels directory.

    2. Enter either build.bat or ./build.sh (according to your operating system) in order to build Java Card applets and Java client.

    3. Enter either run.bat or ./run.sh (according to your operating system) to load the applet into the Simulator, send all APDUs and clean up afterwards.

Utility Sample

The Utility sample demonstrates how you can use the utility APIs in an application. This applet is a simple version of a hypothetical broker applet that is used to assist the user in buying and selling stocks. The applet uses constructed TLVs and primitive TLVs to manage the portfolio. The communication with the broker is also in the form of TLVs and uses the math API to determine the value of a trade. It also uses the integer API to construct an integer from byte array and set integers in byte arrays for TLV objects.

This applet provides the following features:

Follow one of these sets of instructions to run this sample:

Running the Utility Sample from the Command Line

To run the Utility sample:

  1. Open a Command Prompt window and perform the following:

    1. Navigate to the JC_HOME_SIMULATOR\runtime\bin directory.

    2. Start the Simulator by entering one of the following commands at the command prompt:

      jcsw (on Windows systems) or ./jcsl (on Linux).

  2. Open a second Command Prompt window and perform the following:

    1. Navigate to the JC_HOME_SIMULATOR\samples\Utility directory.

    2. Enter either build.bat or ./build.sh (according to your operating system) in order to build Java Card applet and Java client.

    3. Enter either run.bat or ./run.sh (according to your operating system) to load the applet into the Simulator, send all APDUs and clean up afterwards.

Wallet Sample

The Wallet sample demonstrates a simplified cash card application. It keeps a balance and exercises some Java Card API features such as the use of a PIN to control access to the applet.

Follow one of these sets of instructions to run this sample:

Running the Wallet Sample from the Command Line

To run the Wallet sample:

  1. Open a Command Prompt window and perform the following:

    1. Navigate to the JC_HOME_SIMULATOR\runtime\bin directory.

    2. Start the Simulator by entering one of the following commands at the command prompt:

      jcsw (on Windows systems) or ./jcsl (on Linux systems)

  2. Open a second Command Prompt window and perform the following:

    1. Navigate to the JC_HOME_SIMULATOR\samples\Wallet directory.
    2. Enter either build.bat or ./build.sh (according to your operating system) in order to build Java Card applet and Java client.
    3. Enter either run.bat or ./run.sh (according to your operating system) to load the applet into the Simulator, send all APDUs and clean up afterwards.

ObjectDeletion Sample

The ObjectDeletion sample demonstrates the object deletion mechanism, applet deletion and package deletion:

Follow one of these sets of instructions to run this sample:

Running the ObjectDeletion Sample from the Command Line

To run the ObjectDeletion sample using command line (on Linux systems make sure that executable flags for executable files are set correctly):

  1. Open a Command Prompt window and perform the following:

    1. Navigate to the JC_HOME_SIMULATOR\runtime\bin directory.

    2. Start the Simulator by entering one of the following commands at the command prompt:

      jcsw (on Windows systems) or ./jcsl (on Linux systems).

  2. Open a second Command Prompt window, perform the following:

    1. Navigate to the JC_HOME_SIMULATOR\samples\ObjectDeletion directory.

    2. Enter either build.bat or ./build.sh command at the command prompt (according to your operating system) in order to build Java Card applets and Java client.

    3. Enter either run.bat or ./run.sh (according to your operating system) to load the applets into the Simulator, send all APDUs and clean up afterwards.

  3. Inspect client’s output on the console – all APDUs should have been confirmed by the simulator with a positive response APDU (0x9000).

SignatureMessageRecovery Sample

Message recovery refers to the mechanism whereby part of the message used to create the message digest is also included as padding in the signature block. During signature verification, the message data padding does not need to be explicitly sent to the verifying entity, it can automatically be extracted from the signature block.

This sample consists of two scripts representing two scenarios for Signature with Message Recovery. The first script demonstrates a scenario in which the message to sign is large enough that only some part of it is included in the signature padding (hence the name “Partial Recovery”). The sequence of events resulting from running this script are:

  1. The script sends the sample application a large message to sign.

  2. The application initializes the signature object with algorithm Signature.ALG_RSA_SHA_ISO9796_MR and signs the message. Because the message is too large to fit in the signature, the application returns back to the script the number of bytes of original message that is embedded in the signature data. The application also returns back to the script the signature data.

  3. The script then simulates the verification phase in which it sends the signature data to the sample application.

  4. The application recovers the partial message and returns back to the script.

  5. The script sends the remainder of the message to the application to verify the signature.

  6. The application verifies the signature against the entire message and returns success.

The second script shows the scenario in which the message to sign is small enough that the entire message itself becomes part of the signature padding (hence the name “Full Recovery” since you can recover the full message from the signature itself).

The sequence of events resulting from running this script are:

  1. The script sends the sample application a small message to be signed.

  2. The application initializes the signature object with the algorithm Signature.ALG_RSA_SHA_ISO9796_MR and signs the message. Because the message is small enough, the application returns the signature data to the script.

  3. The script then simulates the verification phase in which it sends the signature data to the sample application asking it to verify the message.

    The application recovers the original message from the signature data and also verifies the signature, then returns the original data back to the script. If the signature verification fails, it returns an error code.

Follow one of these set of instructions to run the sample:

Running the SignatureMessageRecovery Sample from the Command Line

To run the SignatureMessageRecovery sample:

  1. Open a Command Prompt window and perform the following:

    1. Navigate to the JC_HOME_SIMULATOR\runtime\bin directory.

    2. Start the Simulator by entering one of the following commands at the command prompt:

      jcsw (on Windows systems) or ./jcsl (on Linux systems).

  2. Open a second Command Prompt window and perform the following:

    1. Navigate to the JC_HOME_SIMULATOR\samples\SignatureMessageRecovery directory.

    2. Enter either build.bat or ./build.sh (according to your operating system) in order to build Java Card applet and Java client.

    3. Enter either run.bat or ./run.sh (according to your operating system) to load the applet into the Simulator, send all APDUs and clean up afterwards.

Signing with Message Recovery

The order of operations for signing a message is as follows:

  1. The user invokes a combination of the update and sign methods to generate a signature based on message data provided by the user.

  2. The sign method returns an indication to the user of the portion of the message that was included as padding in the signature.

    This is required so that the user knows what remaining data must still be sent along with the signature block.

Verifying with Message Recovery

The order of operations for verifying a signed message is as follows:

  1. The user initializes the signature object with signature at the very beginning so it can get the recoverable data at the earliest.
  2. The user invokes a combination of the update and verify methods to verify the signature based on the message data provided by the user.
  3. The verify method verifies the signature by comparing the accumulated hash with the hash in the message representative recovered during initialization.

ArrayViews Sample

The ArrayViews sample comes with the following three source files:

Several techniques are demonstrated here:

  1. A client - server application (two separate applets) shares data using array views: Using this allows applications to access a certain part of an array as if it was a standalone array
  2. Usage of ShareableInterface for communication purposes
  3. Resource file contents using javacard.framework.Resources

Note:

The converter generates the application files one by one where the package MyShareable must be the first one to be converted, as it is needed for the other two applications as well.

This section contains:

Running the ArrayViews Sample from the Command Line

To run the ArrayViews sample using command line (on Linux systems make sure that executable flags for executable files are set correctly):

  1. Open a Command Prompt window and perform the following:

    1. Navigate to the JC_HOME_SIMULATOR\runtime\bin directory.

    2. Start the Simulator by entering one of the following command at the command prompt:

      jcsw (on Windows systems) or ./jcsl (on Linux systems)

  2. Open a second Command Prompt window and perform the following:

    1. Navigate to the JC_HOME_SIMULATOR\samples\ArrayViews directory.

    2. Enter either build.bat or ./build.sh (according to your operating system) in order to build Java Card applets and Java client.

    3. Enter either run.bat or ./run.sh (according to your operating system) to load the applets into the Simulator, send all APDUs and clean up afterwards.

  3. Inspect the client’s output on the console – all APDUs should have been confirmed by the simulator with a positive response APDU (0x9000).

Using JShell as a Simulator Client

The Java Card Development Kit Simulator includes startup scripts that configure JShell as a client for the Simulator. The scripts provide methods for loading CAP files, installing and selecting applets, and sending APDUs.

Before you begin, set JC_HOME_SIMULATOR to the Simulator installation directory and configure the samples/client.config.properties file in that directory. See Configuring the Java Card Development Kit Simulator.

To start the JShell client:

  1. Start the Simulator. See Java Card Development Kit Simulator Command Line.

  2. Open a separate command prompt or terminal and change to the client/JShellScripts directory under JC_HOME_SIMULATOR.

  3. Run the launcher for your operating system:

    • On Windows, enter jcshell.bat.
    • On Linux, enter ./jcshell.sh.

    The launcher starts JShell and loads startup-simu.jsh and startup-commands.jsh.

  4. At the JShell prompt, enter help() to display the available methods or workflowExamples() to display usage examples. To connect to a Simulator running on localhost port 9025, enter connect(). To use a different host or port, enter connect(host, port).

You can extend the startup scripts with additional methods and workflows to support your application development and testing requirements.

Running Sample JShell Scripts

The HelloWorld, Channels, and ArrayViews samples include JShell scripts that load CAP files, install applets, and send APDUs. After completing the sample operations, each script uninstalls the applets, unloads the CAP files, and disconnects from the Simulator.

The scripts are located in the following directories under JC_HOME_SIMULATOR:

To run a sample script:

  1. Build the sample as described in Building and Running the Samples from the Command Line. The scripts read CAP files from the sample’s applet/deliverables directory under JC_HOME_SIMULATOR. If the CAP files are generated in another location, update the paths in the sample script.

  2. Start the Simulator and the JShell client as described in Using JShell as a Simulator Client. The sample scripts use connect() to connect to localhost on port 9025. To use a different host or port, update the script to call connect(host, port) with the required values.

  3. At the JShell prompt, enter /open followed by the full path to the sample script. For example:

    /open "<JC_HOME_SIMULATOR>/samples/HelloWorld/run-sample-helloworld.jsh"

    Replace <JC_HOME_SIMULATOR> with the value of the JC_HOME_SIMULATOR environment variable before entering the command. The placeholder is not expanded automatically. Keep the quotation marks when the path contains spaces.

    Alternatively, open the sample .jsh file in a text editor, copy its contents, and paste them into the active JShell session.