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
- Factory Reset APDU
- How to Run the Samples
- Running the Samples
- Using JShell as a Simulator Client
- Running Sample JShell Scripts
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:
- In a command prompt, navigate to the desired sample directory and execute
build.bator./build.shdepending on your operating system, to build all necessary modules. - 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
- In a sample application’s subdirectory, execute the
runscript.-
By default, running
run.bator./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
pcscparameter 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-LiteandIFDHandler(see Linux - Install and Set Up the PCSC-Lite and IFD Handler), a sample can communicate indirectly with the Simulator throughPCSC-Lite. To use this driver, include thepcscparameter 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:
-
Running the HelloWorld Sample from the Command Line - A minimal applet utilizing the simplest source code and meta-files that demonstrates the base structure of a Java Card applet that developers can use to develop, deploy, create, execute, delete, and unload a standalone module.
-
Running the Channels Sample from the Command Line - Demonstrates the use of logical channels, which allows selecting multiple applets at the same time.
-
Running the Utility Sample from the Command Line - Demonstrates the use of the utility APIs in an applet to simulate stock trading and portfolio management.
-
Running the Wallet Sample from the Command Line - Demonstrates a simplified cash card application.
-
Running the ObjectDeletion Sample from the Command Line - Demonstrates applet and package deletion and the object deletion mechanism that removes unreachable objects.
-
Running the SignatureMessageRecovery Sample from the Command Line - Demonstrates message recovery. This sample is only included in bundles with cryptography extensions.
-
Running the ArrayViews Sample from the Command Line - Demonstrates a client/server application sharing data using array views.
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:
-
Open a Command Prompt window and perform the following:
-
Navigate to the
JC_HOME_SIMULATOR\runtime\bindirectory. -
Start the Simulator by entering one of the following commands at the command prompt:
jcsw(on Windows systems) or./jcsl(on Linux systems).
-
-
Open a second Command Prompt window and perform the following:
-
Navigate to the
JC_HOME_SIMULATOR\samples\HelloWorlddirectory. -
Enter either
build.bator./build.sh(according to your operating system) in order to build Java Card applet and Java client. -
Enter either
run.bator./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:
-
The
ConnectionManagerapplet manages the connection. -
AccountAccessorapplet manages the account.
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:
-
Open a Command Prompt window and perform the following:
-
Navigate to the
JC_HOME_SIMULATOR\runtime\bindirectory. -
Start the Simulator by entering one of the following commands at the command prompt:
jcsw(on Windows systems) or./jcsl(on Linux systems).
-
-
Open a second Command Prompt window and perform the following:
-
Navigate to the
JC_HOME_SIMULATOR\samples\Channelsdirectory. -
Enter either
build.bator./build.sh(according to your operating system) in order to build Java Card applets and Java client. -
Enter either
run.bator./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:
-
PIN Protection - PIN protected access to the application. Uses the standard PIN API in the Java Card platform to protect access to the applet.
-
Storage of Portfolio - Storage of portfolio information on the card. The applet uses a portfolio constructed TLV to store the information regarding all the stocks that the user currently holds. The information is stored in the form of
stockInfoconstructed TLV. EachstockInfoTLV contains the following:-
Stock symbol
-
Number of stocks
-
Last Trade Constructed TLV
-
Number of stocks
-
Stock Price
-
-
Stock Trading - The applet assists the user in buying and selling stocks by creating a “signed” purchasing or selling request for the broker in the form of a stock purchase request constructed TLV or sell stock request constructed TLV. Before the request is generated, the applet checks to see if the user has enough stocks in case the request is to sell the stock and enough account balance if the request is to buy new stock. The request is sent back to the terminal where the terminal application may retrieve the TLV from the response APDU and send it to the broker.
If the trade is successful, the broker sends back a confirmation message in the form of a sell confirmation TLV or purchase confirmation TLV. The applet retrieves the information from the confirmation TLV and updates the portfolio as follows:
-
If a new stock is bought, the applet creates a new constructed
stockInfoTLV to store the new stock information. -
If the user already had a stock, the number of stocks the user currently holds, and the last trade information is updated accordingly.
-
If the user, because of the trade, has 0 stocks of a certain company, the
stockInfoTLV for that stock is removed from the portfolio constructed TLV.
-
-
Retrieval of complete portfolio information from the card.
-
Get Information On a Stock - Retrieval of information on a particular stock in the portfolio. User may use this feature to get information regarding a specific stock rather than retrieving the whole portfolio. If a stock is not found, the appropriate exception is thrown. The information is returned in the form of a
stockInfoTLV that contains the following:-
Stock symbol
-
Number of stocks
-
Last trade constructed TLV
-
Number of stocks
-
Stock price
-
-
Assistance for the user in creating a stock purchase request for the broker.
-
Assistance for the user in creating a sell stock request for the broker.
-
On receiving a trade confirmation, update the portfolio accordingly.
-
Get information on current user account balance.
Follow one of these sets of instructions to run this sample:
Running the Utility Sample from the Command Line
To run the Utility sample:
-
Open a Command Prompt window and perform the following:
-
Navigate to the
JC_HOME_SIMULATOR\runtime\bindirectory. -
Start the Simulator by entering one of the following commands at the command prompt:
jcsw(on Windows systems) or./jcsl(on Linux).
-
-
Open a second Command Prompt window and perform the following:
-
Navigate to the
JC_HOME_SIMULATOR\samples\Utilitydirectory. -
Enter either
build.bator./build.sh(according to your operating system) in order to build Java Card applet and Java client. -
Enter either
run.bator./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:
-
Open a Command Prompt window and perform the following:
-
Navigate to the
JC_HOME_SIMULATOR\runtime\bindirectory. -
Start the Simulator by entering one of the following commands at the command prompt:
jcsw(on Windows systems) or./jcsl(on Linux systems)
-
-
Open a second Command Prompt window and perform the following:
- Navigate to the
JC_HOME_SIMULATOR\samples\Walletdirectory. - Enter either
build.bator./build.sh(according to your operating system) in order to build Java Card applet and Java client. - Enter either
run.bator./run.sh(according to your operating system) to load the applet into the Simulator, send all APDUs and clean up afterwards.
- Navigate to the
ObjectDeletion Sample
The ObjectDeletion sample demonstrates the object deletion mechanism, applet deletion and package deletion:
- “Stage preparation”: Demonstrates the object deletion mechanism and verifies that memory for objects referenced from transient memory of type
CLEAR_ON_DESELECTis reclaimed after an applet is deselected. - “Stage exec_1_1”: Demonstrates the object deletion mechanism and verifies that memory for objects referenced from transient memory of type
CLEAR_ON_RESETis reclaimed after card reset. - “Stage exec_1_2”: Performs applet deletion, package deletion and employs the
AppletEvent.uninstall()method to uninstall an applet. The sample verifies that all transient memory of typeCLEAR_ON_RESETandCLEAR_ON_DESELECTis returned to the memory manager. The sample also demonstrates the use of theAppletEvent.uninstall()method.
Follow one of these sets of instructions to run this sample:
- Running the ObjectDeletion Sample in Eclipse
- Running the ObjectDeletion Sample from the Command Line
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):
-
Open a Command Prompt window and perform the following:
-
Navigate to the
JC_HOME_SIMULATOR\runtime\bindirectory. -
Start the Simulator by entering one of the following commands at the command prompt:
jcsw(on Windows systems) or./jcsl(on Linux systems).
-
-
Open a second Command Prompt window, perform the following:
-
Navigate to the
JC_HOME_SIMULATOR\samples\ObjectDeletiondirectory. -
Enter either
build.bator./build.shcommand at the command prompt (according to your operating system) in order to build Java Card applets and Java client. -
Enter either
run.bator./run.sh(according to your operating system) to load the applets into the Simulator, send all APDUs and clean up afterwards.
-
-
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:
-
The script sends the sample application a large message to sign.
-
The application initializes the signature object with algorithm
Signature.ALG_RSA_SHA_ISO9796_MRand 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. -
The script then simulates the verification phase in which it sends the signature data to the sample application.
-
The application recovers the partial message and returns back to the script.
-
The script sends the remainder of the message to the application to verify the signature.
-
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:
-
The script sends the sample application a small message to be signed.
-
The application initializes the signature object with the algorithm
Signature.ALG_RSA_SHA_ISO9796_MRand signs the message. Because the message is small enough, the application returns the signature data to the script. -
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 in Eclipse
- Running the SignatureMessageRecovery Sample from the Command Line
Running the SignatureMessageRecovery Sample from the Command Line
To run the SignatureMessageRecovery sample:
-
Open a Command Prompt window and perform the following:
-
Navigate to the
JC_HOME_SIMULATOR\runtime\bindirectory. -
Start the Simulator by entering one of the following commands at the command prompt:
jcsw(on Windows systems) or./jcsl(on Linux systems).
-
-
Open a second Command Prompt window and perform the following:
-
Navigate to the
JC_HOME_SIMULATOR\samples\SignatureMessageRecoverydirectory. -
Enter either
build.bator./build.sh(according to your operating system) in order to build Java Card applet and Java client. -
Enter either
run.bator./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:
-
The user invokes a combination of the
updateandsignmethods to generate a signature based on message data provided by the user. -
The
signmethod 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:
- The user initializes the
signatureobject with signature at the very beginning so it can get the recoverable data at the earliest. - The user invokes a combination of the
updateandverifymethods to verify the signature based on the message data provided by the user. - The
verifymethod 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:
- Applet
ClientApplet - Library package
MyShareable - Applet
ServerApplet
Several techniques are demonstrated here:
- 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
- Usage of
ShareableInterfacefor communication purposes - Resource file contents using
javacard.framework.Resources
Note:
The converter generates the application files one by one where the package
MyShareablemust 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):
-
Open a Command Prompt window and perform the following:
-
Navigate to the
JC_HOME_SIMULATOR\runtime\bindirectory. -
Start the Simulator by entering one of the following command at the command prompt:
jcsw(on Windows systems) or./jcsl(on Linux systems)
-
-
Open a second Command Prompt window and perform the following:
-
Navigate to the
JC_HOME_SIMULATOR\samples\ArrayViewsdirectory. -
Enter either
build.bator./build.sh(according to your operating system) in order to build Java Card applets and Java client. -
Enter either
run.bator./run.sh(according to your operating system) to load the applets into the Simulator, send all APDUs and clean up afterwards.
-
-
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:
-
Start the Simulator. See Java Card Development Kit Simulator Command Line.
-
Open a separate command prompt or terminal and change to the
client/JShellScriptsdirectory underJC_HOME_SIMULATOR. -
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.jshandstartup-commands.jsh. - On Windows, enter
-
At the JShell prompt, enter
help()to display the available methods orworkflowExamples()to display usage examples. To connect to a Simulator running on localhost port 9025, enterconnect(). To use a different host or port, enterconnect(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:
samples/HelloWorld/run-sample-helloworld.jshsamples/Channels/run-sample-channels.jshsamples/ArrayViews/run-sample-arrayviews.jsh
To run a sample script:
-
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/deliverablesdirectory underJC_HOME_SIMULATOR. If the CAP files are generated in another location, update the paths in the sample script. -
Start the Simulator and the JShell client as described in Using JShell as a Simulator Client. The sample scripts use
connect()to connect tolocalhoston port9025. To use a different host or port, update the script to callconnect(host, port)with the required values. -
At the JShell prompt, enter
/openfollowed 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 theJC_HOME_SIMULATORenvironment variable before entering the command. The placeholder is not expanded automatically. Keep the quotation marks when the path contains spaces.Alternatively, open the sample
.jshfile in a text editor, copy its contents, and paste them into the active JShell session.