Java Card 3 Platform: Programming Notes
Java Card 3 Platform: Programming Notes
Java Card 3 Platform: Programming Notes
Programming Notes
E59598-02
This software and related documentation are provided under a license agreement containing restrictions on
use and disclosure and are protected by intellectual property laws. Except as expressly permitted in your
license agreement or allowed by law, you may not use, copy, reproduce, translate, broadcast, modify,
license, transmit, distribute, exhibit, perform, publish, or display any part, in any form, or by any means.
Reverse engineering, disassembly, or decompilation of this software, unless required by law for
interoperability, is prohibited.
The information contained herein is subject to change without notice and is not warranted to be error-free. If
you find any errors, please report them to us in writing.
If this is software or related documentation that is delivered to the U.S. Government or anyone licensing it on
behalf of the U.S. Government, then the following notice is applicable:
U.S. GOVERNMENT END USERS: Oracle programs (including any operating system, integrated software,
any programs embedded, installed or activated on delivered hardware, and modifications of such programs)
and Oracle computer documentation or other Oracle data delivered to or accessed by U.S. Government end
users are "commercial computer software" or “commercial computer software documentation” pursuant to the
applicable Federal Acquisition Regulation and agency-specific supplemental regulations. As such, the use,
reproduction, duplication, release, display, disclosure, modification, preparation of derivative works, and/or
adaptation of i) Oracle programs (including any operating system, integrated software, any programs
embedded, installed or activated on delivered hardware, and modifications of such programs), ii) Oracle
computer documentation and/or iii) other Oracle data, is subject to the rights and limitations specified in the
license contained in the applicable contract. The terms governing the U.S. Government’s use of Oracle cloud
services are defined by the applicable contract for such services. No other rights are granted to the U.S.
Government.
This software or hardware is developed for general use in a variety of information management applications.
It is not developed or intended for use in any inherently dangerous applications, including applications that
may create a risk of personal injury. If you use this software or hardware in dangerous applications, then you
shall be responsible to take all appropriate fail-safe, backup, redundancy, and other measures to ensure its
safe use. Oracle Corporation and its affiliates disclaim any liability for any damages caused by use of this
software or hardware in dangerous applications.
Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of
their respective owners.
Intel and Intel Inside are trademarks or registered trademarks of Intel Corporation. All SPARC trademarks are
used under license and are trademarks or registered trademarks of SPARC International, Inc. AMD, Epyc,
and the AMD logo are trademarks or registered trademarks of Advanced Micro Devices. UNIX is a registered
trademark of The Open Group.
This software or hardware and documentation may provide access to or information about content, products,
and services from third parties. Oracle Corporation and its affiliates are not responsible for and expressly
disclaim all warranties of any kind with respect to third-party content, products, and services unless otherwise
set forth in an applicable agreement between you and Oracle. Oracle Corporation and its affiliates will not be
responsible for any loss, costs, or damages incurred due to your access to or use of third-party content,
products, or services, except as set forth in an applicable agreement between you and Oracle.
Contents
Preface
Audience vi
Before You Read This Book vi
How This Document Is Organized vi
Documentation Accessibility vii
Related Documents vii
Conventions vii
iii
Writing ISO/IEC 7816-4:2013 Compliant Applets 2-9
ISO/IEC 7816-4:2013 Compliant Applet Example 2-9
Non-MultiSelectable Applets and Shareable Objects 2-10
ISO/IEC 7816-4:2013 Specific APDU Commands for Logical Channel
Management 2-11
MANAGE CHANNEL OPEN 2-11
MANAGE CHANNEL CLOSE 2-12
SELECT FILE 2-13
iv
javacardx.framework.ExtendedLength Interface 4-3
APDU Parsing with the javacard.framework.APDU Class 4-3
Creating an Applet That Can Send and Receive Extended Length APDUs 4-3
Glossary
v
Preface
Preface
This book contains tips and guidelines for developers of Java Card applets and for
developers of vendor-specific frameworks. This book covers several topics that are
substantially different from programming models found in earlier versions of the Java
Card platform and is not meant to comprehensively introduce or cover general
programming topics.
Java Card technology combines a subset of the Java programming language with a
runtime environment optimized for smart cards and similar small-memory embedded
devices. The goal of Java Card technology is to bring many of the benefits of the Java
programming language to the resource-constrained world of smart cards.
The Java Card API is compatible with international standards such as ISO7816 and
industry-specific standards such as Europay, Master Card, Visa (EMV).
Audience
This book is for applet developers using the Application Programming Interface for the
Java Card Platform, Version 3.0.5, Classic Edition to implement applet management,
multiselectable applets, logical channels, Remote Method Invocation (RMI), and
extended APDUs for the Java Card platform.
This book is also for developers who are considering creating a vendor-specific
framework based on version 3.0.5 of the Java Card technology specifications.
vi
Preface
Documentation Accessibility
For information about Oracle's commitment to accessibility, visit the Oracle
Accessibility Program website at http://www.oracle.com/pls/topic/lookup?
ctx=acc&id=docacc.
Related Documents
References to various documents or products are made in this manual. Have the
following documents available:
• Java Card 3 Platform Development Kit User's Guide Classic Edition, Version 3.0.5
• Java Card 3 Platform Runtime Specification, Classic Edition Version 3.0.5
• Java Card 3 Platform Virtual Machine Specification, Classic Edition Version 3.0.5
• Java Card Technology for Smart Cards by Zhiqun Chen (Addison-Wesley, 2000).
• Off-Card Verifier for the Java Card™ Platform, Version 2.2.1, White Paper (Sun
Microsystems, Inc., 2003) , Sun Microsystems, Inc.
• The Java Programming Language (Java Series), Second Edition by Ken Arnold
and James Gosling (Addison-Wesley, 1998).
• The Java Virtual Machine Specification (Java Series), Second Edition by Tim
Lindholm and Frank Yellin (Addison-Wesley, 1999).
• The Java Class Libraries: An Annotated Reference, Second Edition (Java Series)
by Patrick Chan, Rosanna Lee and Doug Kramer (Addison-Wesley, 1999).
• ISO/IEC 7816 Specification Parts 1-6.
Conventions
The following text conventions are used in this document:
Convention Meaning
boldface Boldface type indicates graphical user interface elements associated with
an action, or terms defined in text or the glossary.
italic Italic type indicates book titles, emphasis, or placeholder variables for which
you supply particular values.
monospace Monospace type indicates commands within a paragraph, URLs, code in
examples, text that appears on the screen, or text that you enter.
vii
1
Object, Package and Applet Deletion
This chapter describes how to use the object deletion mechanism and the package
and applet deletion features of the Java Card 3 Platform.
This chapter includes the following topics:
• Object Deletion Mechanism
• Package and Applet Deletion
In the following code example, the method updates the buffer capacity to the given
value. If it is not empty, the method creates a new buffer and removes the old one by
requesting the object deletion mechanism.
/**
* The following method updates the buffer size by removing
* the old buffer object from the memory by requesting
* object deletion and creates a new one with the
* required size.
*/
void updateBuffer(byte requiredSize){
try{
1-1
Chapter 1
Package and Applet Deletion
1-2
Chapter 1
Package and Applet Deletion
• Applet instances or objects that either belong to the package, or that belong to a
package that depends on the package to be deleted
Package deletion will not succeed if any of the following conditions exist:
• A reachable instance of a class belonging to the package exists on the card.
• Another package on the card depends on the package.
• A reset or power failure occurs after the deletion process begins, but before it
completes.
To ensure that a package can be easily removed from the card, avoid writing and
downloading other packages that might be dependent on it. If other packages on the
card depend on it, you must remove all dependent packages before you can remove
this package from the card memory.
1-3
Chapter 1
Package and Applet Deletion
1-4
2
Working with Logical Channels
The Java Card 3 Platform can support up to twenty logical channels per active
interface. Logical channels allow the concurrent execution of multiple applications on
the card, allowing a terminal to handle different tasks at the same time.
This chapter includes the following topics:
• Dual Interface Cards
• Applets and Logical Channels
• The MultiSelectable Interface
• Writing Applets For Concurrent Logical Channels
Non-MultiSelectable Applets
An error is returned to the terminal when an applet that is not designed to be aware of
multiple channels is either selected more than once on different channels or is
selected concurrently with other applets in the same package.
You can have several non-multiselectable applets operating simultaneously on
different channels, as long as they do not interfere with each other's data while they
are active. For example, you can open up to 4 channels and run a distinct applet on
each as long as they do not interoperate. You can control their operation by
multiplexing commands into the APDU communications channel. If the applets are
2-1
Chapter 2
The MultiSelectable Interface
independent of each other, then the results will be the same as if each of these applets
were running one at a time, each in a separate session.
Note:
If an applet in any package implements the MultiSelectable interface,
then all applets in the package must also implement the MultiSelectable
interface. It is not possible to have multiselectable and non-multiselectable
applets in the same package.
2-2
Chapter 2
Writing Applets for Concurrent Logical Channels
Only when the last applet instance from the entire package is deselected does a call to
Applet.deselect() occur, resulting in the erasure of CLEAR_ON_DESELECT transients.
2-3
Chapter 2
Writing Applets for Concurrent Logical Channels
Note:
The applet is still required to implement the Applet.select() and the
Applet.deselect() methods in addition to the MultiSelectable
interface. These methods handle applet selection and deselection behavior
when a multiselection situation does not happen.
Related Topics
• MultiSelectable Applet Example
• Handling Channel Information on APDU Commands
• Writing ISO 7816-4:2005 Compliant Applets
• Non-MultiSelectable Applets and Shareable Objects
• ISO 7816-4:2005 Specific APDU Commands for Logical Channel Management
2-4
Chapter 2
Writing Applets for Concurrent Logical Channels
The following methods are responsible for clearing and setting the data:
To achieve the behavior specified above, you must modify the selection and
deselection methods in your sample applet.
The code for Applet.select(), which is invoked when this applet is the first to
become active in the package, can be implemented like this:
2-5
Chapter 2
Writing Applets for Concurrent Logical Channels
In the case of deselection, the applet data must be cleared. The method
MultiSelectable.deselect(boolean) can be implemented so that it clears applet
data only if the applet is no longer active. For example:
If this applet is the last one to be deactivated from the package, it also must clear
package data. This situation results in a call to Applet.deselect(). This method can
be implemented like this:
However, a note of caution: while the MANAGE CHANNEL command CLA byte follows
the encoding as described below, its P2 parameter does not. The logical channel
numbers in its P2 parameter are correctly encoded in the range of [0...19].
2-6
Chapter 2
Writing Applets for Concurrent Logical Channels
Interindustry Space
CLA Remarks
0x0X Type 4, last or only command in chain
0x5X Type 16, no SM, not last command in chain (paired with 0x4X)
0x7X Type 16, SM, not last command in chain (paired with 0x06X)
Type 16:
b8 b7 b6 b5 b4 b3 b2 b1
0 1 y x z z z z
Notation:
x = Command Chaining bit
2-7
Chapter 2
Writing Applets for Concurrent Logical Channels
0xDX Type 16, no SM, not last command in chain (paired with 0xCX)
0xFX Type 16, SM, not last command in chain (paired with 0xEX)
Type 16:
b8 b7 b6 b5 b4 b3 b2 b1
1 1 y x z z z z
Logical Channels
When an APDU command is received, the card processes it and determines whether
the command has logical channel information encoding. If logical channel information
is encoded, then the card sends the APDU command to the respective channel. All
other APDU commands are forwarded to the card's basic channel (0).
The X nibble is responsible for logical channels and secure message encoding. Only
the two least significant bits of the nibble are used for channel encoding, which ranges
from 0 to 3. For example, the command 0x21 forwards the command to the card's
basic channel (0), because the CLA byte with the nibble 0x2X does not contain logical
channel information.
Just as the deselection and selection mechanisms must be written to take into
consideration a multiple-channel environment, it is important to write the
Applet.process() method so that it handles channel information correctly. Due to the
fact that some APDUs can be digitally signed, the APDU command is passed to the
applet's process method as it is sent by the terminal. That means any logical channel
information is not cleared and is passed intact to the applet. The applet must deal with
this situation.
...
//Applet's process method
public void process(APDU apdu) {
byte[] buffer = apdu.getBuffer();
2-8
Chapter 2
Writing Applets for Concurrent Logical Channels
if (apdu.isISOInterindustryCLA()) {
if (Applet.selectingApplet()) {
return;
} else {
ISOException.throwIt (ISO7816.SW_CLA_NOT_SUPPORTED);
}
}
...
2-9
Chapter 2
Writing Applets for Concurrent Logical Channels
Note:
If such an event occurs, it will also be a multiselection situation, where the
applet is also selected on the newly opened channel.
In this example, the code to identify the applet selection case is included in the
implementation of the MultiSelectable.select(boolean) method:
2-10
Chapter 2
Writing Applets for Concurrent Logical Channels
• It is not possible to select more than one applet simultaneously from a package if
any of the applets you want to select does not implement the MultiSelectable
interface.
• It is not possible to invoke methods of a Shareable object belonging to a non-
multiselectable applet when an applet, belonging to the same group context, is
active.
2-11
Chapter 2
Writing Applets for Concurrent Logical Channels
2-12
Chapter 2
Writing Applets for Concurrent Logical Channels
This command closes channel R. Channel R must not be the basic channel (it can be
channel 1, 2, ...19 only).
SELECT FILE
In response to the SELECT FILE command, the dispatcher follows this procedure:
2-13
3
Developing RMI Applications for the Java
Card Platform
This chapter describes how to write remote method invocation (RMI) applications for
the Java Card 3 Platform. Because the Java Card specifications state that Java Card
RMI is optional, verify that your targeted card supports Java Card RMI before using
these APIs.
This chapter includes the following topics:
• Steps to Develop an RMI Applet for the Java Card 3 Platform
• RMI Program Example
• Add Security Support
Generating Stubs
The Java Card RMI Client framework requires stubs only when the
remote_ref_with_class format is used for passing remote references. These stubs of
remote classes of applets must be pre-generated and available on the client. When
the remote_ref_with_interfaces format is used, stubs are not necessary.
In this example, the Java RMI Compiler (rmic) is used to generate these stubs.
In the command:
• path includes the path to the remote class of your sample applet and to the file
tools.jar
• output_dir is the directory in which to place the resulting stubs
3-1
Chapter 3
RMI Program Example
Note:
You need to generate stubs only for remote classes that list a remote
interface in their implements clause.
The file tools.jar, provided in the Java Card Development Kit contains compiled
implementations of packages javacard.framework, javacard.security,
javacardx.biometry, javacardx.external and javacardx.framework.tlv. Classes
in these packages might be referenced by Java Card RMI applets and thus might be
needed by the rmic to generate stubs.
Main Program
As for any Java Card RMI program, the first step is to define the interface to be used
as contract between the server (the Java Card technology-based application) and its
clients (the terminal applications):
package examples.purse;
import java.rmi.*;
import javacard.framework.*;
public interface Purse extends Remote {
public static final short MAX_AMOUNT = 400;
3-2
Chapter 3
RMI Program Example
Related Topics
• Implement a Remote Interface
• Define the Constructor for the Remote Object
• Provide an Implementation for Each Remote Method
package examples.purse;
import javacard.framework.*;
import javacard.framework.service.*;
import java.rmi.*;
PurseImpl() {
super();
balance = 0;
}
3-3
Chapter 3
RMI Program Example
UserException.throwIt(REQUEST_FAILED);
balance -= amount;
return balance;
}
Here, the remote interface is the Purse interface, which declares the remotely
accessible methods. By implementing this interface, the class establishes a contract
between itself and the compiler, by which the class promises that it will provide method
bodies for all the methods declared in the interface:
public class PurseImpl extends CardRemoteObject implements Purse
3-4
Chapter 3
RMI Program Example
An operation is only allowed if the value of its parameter is compatible with the current
state of the purse object. In this particular case, the application only checks that the
amounts handled are positive and that the balance of the purse always remains
positive.
In Java Card RMI, the arguments to and return values from remote methods are
restricted. The main reason for this limitation is that the Java Card 3 platform does not
support object serialization. The following are the rules for the Java Card 3 platform:
• The arguments to remote methods can be of any supported integral type (such
as boolean, byte, short and int), or any single-dimensional arrays of these
integral types.
Note:
The int type is optionally supported on the Java Card 3 platform, so
applications that use this type might not run on all platforms.
• The return value from a remote method can be any type supported as arguments,
as well as any remote interface type. The method can also return void.
On the other hand, object passing in Java Card RMI follows the normal RMI rules:
• By default, non-remote objects are passed by copy, which means that all data
members of an object are copied, except those marked static or transient. In
the case of the Java Card 3 platform, this rule is trivial to apply, because the only
objects concerned are arrays of integral types.
• Remote objects are passed by reference. In the case of the Java Card 3 platform,
remote objects can only be passed as return values. A reference to a remote
object is actually a reference to a stub, which is a client-side proxy for the remote
objects. Stubs are needed only when the format remote_ref_with_class is used
for passing remote references. When another format, such as
remote_ref_with_interfaces, is used, stubs are not necessary. Stubs are
described in Generate the Stubs.
Note:
Even though the semantics of the Java Card 3 platform transient arrays
are somewhat similar to transient fields in the Java programming
language, different rules apply. The Java Card 3 platform contents are
copied in Java Card RMI and passed by value when they are returned
from a remote method.
3-5
Chapter 3
RMI Program Example
A class can define methods not specified in a remote interface, but they can only be
invoked on-card within the Java Card VM and cannot be invoked remotely.
Sample Applet
In the Java Card 3 platform, all applications must include a class that inherits from
javacard.framework.Applet, which will provide an interface with the outside world.
This also applies to applications that are based on remote objects, for two main
reasons:
• The remote objects must be instantiated and initialized, which can be done in an
applet's install method.
• The remote objects must communicate with the outside world, which can be done
in an applet's process method.
For conversion, an applet should be assigned with an AID known on the client side,
0x00;0x01:0x02:0x03:0x04:0x05:0x06:0x07:0x08, since this AID is used in the client
program.
The following is the basic code for such an applet:
package examples.purse;
import javacard.framework.*;
import javacard.framework.service.*;
import java.rmi.*;
private PurseApplet() {
// Allocates an RMI service and sets for the Java Card platform
// the initial reference
RemoteService rmi = new RMIService(new PurseImpl());
// Allocates a dispatcher for the remote service
dispatcher = new Dispatcher((short) 1);
dispatcher.addService(rmi, Dispatcher.PROCESS_COMMAND);
}
Related Topics
• Preparing and Registering the Remote Object
• Processing the Incoming Commands
3-6
Chapter 3
RMI Program Example
Then, a dispatcher is created and initialized. A dispatcher is the glue among several
services. In this example, the initialization is quite simple, because there is a single
service to initialize:
dispatcher = new Dispatcher((short)1);
dispatcher.addService(rmi, Dispatcher.PROCESS_COMMAND);
Finally, the applet must register itself to the Java Card RE to be made selectable. This
is done in the install method, where the applet constructor is invoked and
immediately registered:
(new PurseApplet()).register();
Client Example
Client applications run on a terminal supporting a Java Virtual Machine environment
such as Java Platform, Standard Edition or Java Platform, Micro Edition (Java ME).
The PurseClient application interacts with the remote stub classes generated by a
stub generation tool and the Java Card platform-specific information managed by the
3-7
Chapter 3
RMI Program Example
The client example below uses standard Java RMIC compiler-generated client-side
stubs. The client application as well as the Java Card client-side framework rely on the
APDU I/O library for managing and communicating with the card reader and the card
on which the Java Card applet PurseApplet resides. This makes the client application
very portable on Java SE platforms. See the Java Card 3 Platform Development Kit
User Guide, Classic Edition Version 3.0.5 for information on the APDU I/O library.
The following example shows a very simple PurseClient application that is the client
application of the Java Card technology-based program PurseApplet:
import examples.purse.*;
import javacard.framework.UserException;
3-8
Chapter 3
RMI Program Example
}
}
}
Related Topics
• Initializing and Shutting Down the Card Connection
• Creating and Using a CardAccessor Object
• Selecting the Java Card Applet and Obtaining the Initial Reference
• Using Remote Objects in Remote Method Invocations
• Generate the Stubs
Note:
ApduIOCardAccessor takes its settings from the file jcclient.properties.
For example. when the RMIPurse sample demo client application runs, the
JC_CLASSIC_HOME/samples/classic_applets/RMIPurse/client
directory containing the properties file is included in the CLASSPATH. The
directory in which you installed the developer's kit is indicated as
JC_CLASSIC_HOME.
On Microsoft Windows platforms, use backslashes in directory paths, instead
of forward slashes.
The following code shows opening and closing the connection using the RMI client
framework:
CardAccessor ca = null;
// The following line initializes card connection according to
// parameters listed in the jcclient.properties file:
ca = new ApduIOCardAccessor();
...
// The following line powers down the card and closes the
connection:
ca.closeCard();
3-9
Chapter 3
RMI Program Example
the card. The CardAccessor object is then provided as a parameter during construction
of the JavaCardRMIConnect class to initiate an RMI dialog for the Java Card platform
as shown in the following code:
// create an RMI connection object for the Java Card platform
JavaCardRMIConnect jcRMI = new JavaCardRMIConnect( myCS );
Selecting the Java Card Applet and Obtaining the Initial Reference
To invoke methods on the remote objects of PurseApplet on the card, it must first be
selected by using the AID as shown in the following code:
Then, the client must obtain the initial reference remote object for PurseApplet.
JavaCardRMIConnect returns an instance of a stub class corresponding to the
PurseImpl class on the card, which implements the Purse interface. The client
application knows beforehand that the PurseApplet's initial remote reference
implements the Purse interface and therefore casts it appropriately as shown in the
following code:
// obtain the initial reference to the Purse interface
Purse myPurse = (Purse) jcRMI.getInitialReference();
Note:
A UserException exception thrown by the remote method is caught by the
client code in normal Java programming language style.
3-10
Chapter 3
RMI Program Example
remote stub classes and the remote interface class files it uses to be accessible in its
classpath.
The stub class PurseImpl_Stub.class for the PurseImpl class is produced by running
the standard JDK compiler. The directory where you installed the developer's kit is
indicated by JC_CLASSIC_HOME. For example, from the examples/purse directory, enter
the following command:
rmic -classpath ../..;%JC_CLASSIC_HOME%/lib/tools.jar -d ../..
-v1.2 examples.purse.PurseImpl
For PurseClient to run correctly on the terminal, the following files must be present in
the examples/purse directory and accessible either from its classpath or from class
loaders:
• PurseImpl_Stub.class
• Purse.class
Figure 3-2 Terminal Sends a SELECT Command to the Smart Card, Which
Returns FCI
Later, when the PurseClient application calls the debit method of the remote
interface Purse, the PurseImpl_Stub object sends an INVOKE command to the card
via the CardAccessor object, identifying the remote object reference, interface,
3-11
Chapter 3
Add Security Support
method, and parameter data for method invocation. The RMIService instance of
PurseApplet unmarshalls this information and invokes the debit method of the
PurseImpl instance, and returns the return value in the response RETURN APDU
(shown in Figure 3-3).
Figure 3-3 Terminal Sends an INVOKE Command to the Smart Card, Which
Returns a Value
package examples.securepurse;
import javacard.framework.*;
import javacard.framework.service.*;
import java.rmi.*;
SecurePurseImpl(SecurityService security) {
this.security = security;
}
3-12
Chapter 3
Add Security Support
)
|| (!security
.isAuthenticated(SecurityService.PRINCIPAL_CARDHO
LDER)))
UserException.throwIt(REQUEST_FAILED);
if (( amount < 0 )|| ( balance < amount ))
UserException.throwIt(REQUEST_FAILED);
balance -= amount;
return balance;
}
Related Topics
• Initialize a Security Service
• Use the Service to Check the Current Security Status
• Security Service Example
• More Secure Applet
• Client Changes to Support Security
• CustomCardAccessor Class for Authentication and Signing
3-13
Chapter 3
Add Security Support
parameter to its constructor. This is exactly what happens here, where the reference to
the object is stored in a dedicated private field:
private SecurityService security ;
if ((!security
.isCommandSecure(SecurityService.PROPERTY_INPUT_INTEGRITY)
)
|| (!security
.isAuthenticated(SecurityService.ID_CARDHOLDER)))
UserException.throwIt(REQUEST_FAILED);
If one of the two conditions is not satisfied, the remote object throws an exception.
This exception is caught by the dispatcher and forwarded to the client.
package com.sun.javacard.samples.SecureRMIDemo;
import javacard.framework.*;
import javacard.framework.service.*;
3-14
Chapter 3
Add Security Support
...
public MySecurityService() {
// initialize the PINs
...
}
public boolean processDataIn(APDU apdu) {
if(selectingApplet()) {
// reset all flags
...
}
else {
return preprocessCommandAPDU(apdu);
}
}
public boolean isCommandSecure(byte properties) throws
ServiceException {
// return the value of appropriate flag
....
}
public boolean isAuthenticated(short principal) throws
ServiceException {
// return the value of appropriate flag
....
}
private byte authenticated;
private boolean preprocessCommandAPDU(APDU apdu) {
receiveInData(apdu);
if(checkAndRemoveChecksum(apdu)) {
3-15
Chapter 3
Add Security Support
data
}
private boolean isAuthenticate(APDU command) {
// check values of CLA and INS bytes
}
}
package examples.securepurse;
import javacard.framework.*;
import javacard.framework.service.*;
import java.rmi.*;
import com.sun.javacard.samples.SecureRMIDemo.MySecurityService;
private SecurePurseApplet() {
SecurityService sec;
// First get a security service
sec = new MySecurityService();
// Allocates an RMI service for the Java Card platform and
// sets the initial reference
RemoteService rmi = new RMIService(new SecurePurseImpl(sec));
// Allocates and initializes a dispatcher for the remote object
dispatcher = new Dispatcher((short) 2);
dispatcher.addService(rmi, Dispatcher.PROCESS_COMMAND);
dispatcher.addService(sec, Dispatcher.PROCESS_INPUT_DATA);
}
The security service that is used by the remote object must be initialized at some
point. Here, this is done in the constructor for the SecurePurseApplet:
The initialization then goes on with the initialization of the Java Card RMI service. The
only new thing here is that the remote object being allocated and set as the initial
reference is now a SecurePurseImpl:
3-16
Chapter 3
Add Security Support
Next, the dispatcher must be initialized. Here, it must dispatch simple Java Card RMI
requests and security-related requests (such as EXTERNAL AUTHENTICATE). In fact, the
security service handles these requests directly. First, allocate a dispatcher and inform
it that it will delegate commands to two different services:
dispatcher = new Dispatcher((short)2);
Then, register services with the dispatcher. The security service is registered as a
service that performs preprocessing operations on incoming commands, and the Java
Card RMI service is registered as a service that processes the command requested:
dispatcher.addService(rmi, Dispatcher.PROCESS_COMMAND);
dispatcher.addService(sec, Dispatcher.PROCESS_INPUT_DATA);
The rest of the class (install and process methods) remain unchanged.
import examples.purse.*;
import javacard.framework.UserException;
3-17
Chapter 3
Add Security Support
if (! cca.authenticateUser( PRINCIPAL_CARDHOLDER_ID )) {
// handle error
}
package examples.securepurseclient;
3-18
Chapter 3
Add Security Support
return super.exchangeAPDU(dataWithMAC);
}
3-19
4
Using Extended APDU
This chapter describes the Extended APDU and how it can be used to allow large
amounts of data to be sent to the card, processed appropriately, and sent back to the
terminal.
The Extended APDU feature is especially beneficial to applications that deal with large
amounts of information, such as signature verification, biometrics verification and
image storage and retrieval. These are more easily implemented if the underlying
transport protocol is T=1. Applets developed for T=0 cards need special logic and care
to work correctly.
This chapter includes the following topics:
• Extended APDU Nominal Cases
• Extended APDU Format
• Extended APDU Limits
• Creating an Applet That Can Send and Receive Extended Length APDUs
4-1
Chapter 4
Extended APDU Limits
Notation
Nc = command data length
Ne = expected response data length
Nr = actual response data length
The encoding rules are defined as:
For LC:
• If LC field is absent, Nc = 0.
• If LC is present as one byte with values between 01 and FF, then Nc = 1..255
accordingly, and it will be a short field.
• If LC is present as an extended field, then it will be three bytes in length: byte one
will be 00, bytes two and three will contain a 16-bit value representing the length of
the data Nc with values between 1 and 65535.
For LE:
• If LE is absent, Ne = 0.
• If LE is one byte:
– A value between 01 and FF will indicate Ne = 1..255.
– A value of 00 will indicate Ne = 256.
If LE is an extended field:
• LC and LE must be in the same format.
• An LE field value between 0001 and FFFF will indicate Ne = 1..65535.
• An LE field value of 0000 will indicate Ne= 65536.
4-2
Chapter 4
Creating an Applet That Can Send and Receive Extended Length APDUs
0..32,767. Lengths of 32,768 and beyond are not supported by the Java Card 3
platform at this time.
This section includes the following topics:
• javacardx.framework.ExtendedLength Interface
• APDU Parsing with the javacard.framework.APDU Class
javacardx.framework.ExtendedLength Interface
By implementing the javacardx.apdu.ExtendedLength interface, applets indicate that
they are capable of processing, receiving, and replying to extended APDU commands.
The Java Card RE does not deliver extended APDU commands to applets that do not
implement this interface (it would throw an ISOException with reason code
ISO7816.SW_WRONG_LENGTH). In addition, the Java Card RE does not allow applets to
send reply data lengths greater than 256, if the interface is not implemented by the
applet.
The APDU buffer in Java Card applications reflects the structure of the extended
APDU as defined in the ISO/IEC 7816-3 specification. In T=1, this representation is
straightforward and precise; however, in T=0, adaptations are needed for some cases.
Specifically, a case 2E APDU sent over T=0 transport will not show its extended LE
value in the APDU buffer. Instead, a P3 value of '00' will always be transmitted and
interpreted as 32,767 if the applet implements ExtendedLength, or interpreted as 256 if
it does not.
The Java Card RE analyzes the APDU type coming into the card and determines its
type based on the rules defined in the ISO/IEC 7816-3 specification. Because case 2E
commands look like case 2S commands in T=0, the Java Card RE is not able to
distinguish this particular case.
To get the value of LC and the data offset inside the APDU buffer use these two APIs
in javacard.framework.APDU:
4-3
Chapter 4
Creating an Applet That Can Send and Receive Extended Length APDUs
...
import javacard.framework.*;
import javacardx.apdu.ExtendedLength;
...
public MyApplet extends Applet implements
ExtendedLength {
...
}
2. Write your applet and Applet.process(..) method as you would with any other
applets. For consistency, it is advisable that your process(..) code begin like the
one below:
if (apdu.isISOInterindustryCLA()) {
if (this.selectingApplet()) {
return;
} else {
ISOException.throwIt (ISO7816.SW_CLA_NOT_SUPPORTED);
}
}
switch (buffer[ISO7816.OFFSET_INS]) {
case CHOICE_1:
...
return;
case CHOICE_2:
...
...
default:
ISOException.throwIt (ISO7816.SW_INS_NOT_SUPPORTED);
}
}
3. For cases 3S, 4S, 3E and 4E, write the method to handle incoming data. Use the
API so that your applet properly handles extended, as well as non-extended,
cases.
void receiveData(APDU apdu) {
byte[] buffer = apdu.getBuffer();
short LC = apdu.getIncomingLength();
4-4
Chapter 4
Creating an Applet That Can Send and Receive Extended Length APDUs
recvLen = apdu.receiveBytes(dataOffset);
}
// Done
}
4. For case 2S, 2E, write the method handling data output. A method could look
something like this:
void sendData(APDU apdu) {
byte[] buffer = apdu.getBuffer();
short LE = apdu.setOutgoing();
short toSend = ...
if (LE != toSend) {
apdu.setOutgoingLength(toSend);
}
4-5
Glossary
active applet instance
an applet instance that is selected on at least one of the logical channels.
A unique AID is assigned for each package. In addition, a unique AID is assigned for
each applet in the package. The package AID and the default AID for each applet
defined in the package are specified in the CAP file. They are supplied to the converter
when the CAP file is generated.
APDU
an acronym for Application Protocol Data Unit as defined in ISO 7816-4.
applet
within the context of this document, a Java Card applet, which is the basic unit of
selection, context, functionality, and security in Java Card technology.
applet developer
a person creating an applet using Java Card technology.
applet firewall
the mechanism that prevents unauthorized accesses to objects in contexts other than
currently active context.
applet package
see library package.
Glossary-1
Glossary
atomic operation
an operation that either completes in its entirety or no part of the operation completes
at all.
atomicity
property in which a particular operation is atomic. Atomicity of data updates
guarantees that data are not corrupted in case of power loss or card removal.
ATR
an acronym for Answer to Reset. An ATR is a string of bytes sent by the Java Card
platform after a reset condition.
binary compatibility
in a Java Card system, a change in a Java programming language package results in
a new CAP file. A new CAP file is binary compatible with (equivalently, does not break
compatibility with) a preexisting CAP file if another CAP file converted using the export
file of the preexisting CAP file can link with the new CAP file without errors.
CAD
an acronym for Card Acceptance Device. A CAD can integrate a card reader and is
the device in which the card is inserted. Sometimes the CAD is called the card reader.
CAP file
the CAP file is produced by the Converter and is the standard file format for the binary
compatibility of the Java Card platform. A CAP file contains an executable binary
representation of the classes of a Java programming language package. The CAP file
also contains the CAP file components (see also CAP file component). The CAP files
produced by the converter are contained in Java Archive (JAR) files.
The following components are optional: the Applet, Export, and Debug. The Applet
component is included only if one or more Applets are defined in the package. The
Export component is included only if classes in other packages may import elements
in the package defined. The Debug component is optional. It contains all of the data
necessary for debugging a package.
Glossary-2
Glossary
card session
a card session begins with the insertion of the card into the CAD. The card is then able
to exchange streams of APDUs with the CAD. The card session ends when the card is
removed from the CAD.
constant pool
the constant pool contains variable-length structures representing various string
constants, class names, field names, and other constants referred to within the CAP file
and the Export File structure. Each of the constant pool entries, including entry zero, is
a variable-length structure whose format is indicated by its first tag byte. There are no
ordering constraints on entries in the constant pool. One constant pool is associated
with each package.
There are differences between the Java platform constant pool and the Java Card
technology-based constant pool. For example, in the Java platform constant pool there
is one constant type for method references, while in the Java Card constant pool,
there are three constant types for method references. The additional information
provided by a constant type in Java Card technologies simplifies resolution of
references.
context
protected object space associated with each applet package and Java Card RE. All
objects owned by an applet belong to context of the applet's package.
Converter
a piece of software that preprocesses all of the Java programming language class files
that make up a package, and converts the package to a CAP file. The Converter also
produces an export file.
default applet
an applet that is selected by default on a logical channel when it is opened. If an applet
is designated the default applet on a particular logical channel on the Java Card
platform, it becomes the active applet by default when that logical channel is opened
using the basic channel.
Glossary-3
Glossary
EEPROM
an acronym for Electrically Erasable, Programmable Read Only Memory.
Export file
a file produced by the Converter that represents the fields and methods of a package
that can be imported by classes in other packages.
finalization
the process by which a Java virtual machine (VM) allows an unreferenced object
instance to release non-memory resources (for example, close and open files) prior to
reclaiming the object's memory. Finalization is only performed on an object when that
object is ready to be garbage collected (meaning, there are no references to the
object).
Finalization is not supported by the Java Card virtual machine. There is no finalize()
method to be called automatically by the Java Card virtual machine.
firewall
see applet firewall.
flash memory
a type of persistent mutable memory. It is more efficient in space and power than
EEPROM. Flash memory can be read bit by bit but can be updated only as a block.
Thus, flash memory is typically used for storing additional programs or large chunks of
data that are updated as a whole.
framework
the set of classes that implement the API. This includes core and extension packages.
Responsibilities include applet selection, sending APDU bytes, and managing
atomicity.
garbage collection
the process by which dynamically allocated storage is automatically reclaimed during
the execution of a program.
heap
a common pool of free memory usable by a program. A part of the computer's memory
used for dynamic memory allocation, in which blocks of memory are used in an
arbitrary order. The Java Card virtual machine's heap is not required to be garbage
collected. Objects allocated from the heap are not necessarily reclaimed.
installer
the on-card mechanism to download and install CAP files. The installer receives
executable binary from the off-card installation program, writes the binary into the
Glossary-4
Glossary
smart card memory, links it with the other classes on the card, and creates and
initializes any data structures used internally by the Java Card Runtime Environment.
installation program
the off-card mechanism that employs a card acceptance device (CAD) to transmit the
executable binary in a CAP file to the installer running on the card.
permanent - references to permanent Java Card RE entry point objects can be stored
and freely reused. Examples of these objects are Java Card RE-owned AID instances.
library package
a Java programming language package that does not contain any non-abstract
classes that extend the class javacard.framework.Applet. An applet package
contains one or more non-abstract classes that extend the
javacard.framework.Applet class.
logical channel
as seen at the card edge, works as a logical link to an application on the card. A
logical channel establishes a communications session between a card applet and the
terminal. Commands issued on a specific logical channel are forwarded to the active
Glossary-5
Glossary
applet on that logical channel. For more information, see the ISO/IEC 7816
Specification, Part 4. (http://www.iso.org).
multiselectable applets
implements the javacard.framework.MultiSelectable interface. Multiselectable
applets can be selected on multiple logical channels at the same time. They can also
accept other applets belonging to the same package being selected simultaneously.
multiselected applet
an applet instance that is selected and, therefore, active on more than one logical
channel simultaneously.
namespace
a set of names in which all names are unique.
native method
a method that is not implemented in the Java programming language, but in another
language. The CAP file format does not support native methods.
nibble
four bits.
normalization (URI)
the process of removing unnecessary "." and ".." segments from the path component
of a hierarchical URI.
Normalizer
a software tool that allows Java applications programmed for the Java Card Platform,
Version 2.2.2, to be deployed on both the Java Card 3 Platform, Connected Edition
and on the Java Card 3 Platform, Classic Edition. It also allows Java applications
packaged for Version 2.2.2 to be transformed through the normalization process and
then repackaged for deployment on both the Connected and Classic Editions.
object owner
the applet instance within the currently active context when the object is instantiated.
An object can be owned by an applet instance, or by the Java Card RE.
Glossary-6
Glossary
PCD
an acronym for Proximity Coupling Device. The PCD is a contactless card reader
device.
persistent object
persistent objects and their values persist from one CAD session to the next,
indefinitely. Objects are persistent by default. Persistent object values are updated
atomically using transactions. The term persistent does not mean there is an object-
oriented database on the card or that objects are serialized and deserialized, just that
the objects are not lost when the card loses power.
PIX
see AID (application identifier).
reference implementation
a fully functional and compatible implementation of a given technology. It enables
developers to build prototypes of applications based on the technology.
remote interface
an interface which extends, directly or indirectly, the interface java.rmi.Remote.
Each method declaration in the remote interface or its super-interfaces includes the
exception java.rmi.RemoteException (or one of its superclasses) in its throws clause.
In addition, Java Card RMI imposes additional constraints on the definition of remote
methods. These constraints are as a result of the Java Card platform language subset
and other feature limitations.
remote methods
the methods of a remote interface.
remote object
an object whose remote methods can be invoked remotely from the CAD client. A
remote object is described by one or more remote interfaces.
RID
see AID (application identifier).
Glossary-7
Glossary
RMI
an acronym for Remote Method Invocation. RMI is a mechanism for invoking instance
methods on objects located on remote virtual machines (meaning, a virtual machine
other than that of the invoker).
ROM
memory used for storing the fixed program of the card. A smart card's ROM contains
operating system routines as well as permanent data and user applications. No power
is needed to hold data in this kind of memory. ROM cannot be written to after the card
is manufactured. Writing a binary image to the ROM is called masking and occurs
during the chip manufacturing process.
runtime environment
see Java Card Runtime Environment (Java Card RE).
shareable interface
an interface that defines a set of shared methods. These interface methods can be
invoked from an applet in one context when the object implementing them is owned by
an applet in another context.
smart card
a card that stores and processes information through the electronic circuits embedded
in silicon in the substrate of its body. Unlike magnetic stripe cards, smart cards carry
both processing power and information. They do not require access to remote
databases at the time of a transaction.
terminal
a Card Acceptance Device that is typically a computer in its own right and can
integrate a card reader as one of its components. In addition to being a smart card
reader, a terminal can process data exchanged between itself and the smart card.
thread
the basic unit of program execution. A process can have several threads running
concurrently each performing a different job, such as waiting for events or performing
a time consuming job that the program doesn't need to complete before going on.
When a thread has finished its job, it is suspended or destroyed.
The Java Card virtual machine can support only a single thread of execution. Java
Card technology programs cannot use class Thread or any of the thread-related
keywords in the Java programming language.
transaction
an atomic operation in which the developer defines the extent of the operation by
indicating in the program code the beginning and end of the transaction.
Glossary-8
Glossary
transient object
the state of transient objects do not persist from one CAD session to the next, and are
reset to a default state at specified intervals. Updates to the values of transient objects
are not atomic and are not affected by transactions.
verification
a process performed on a CAP file that ensures that the binary representation of the
package is structurally correct.
Glossary-9