Talking between Java Card applets with shareable interfaces
Java Card applets live behind a firewall. One applet cannot simply new another applet, grab a reference and call a method. Cross-applet calls go through the one door the JCRE provides: a shareable interface.
That door has more rules than a method signature suggests. To pin them down I built a deliberately small case: a ticket-service applet owns a balance, a client applet buys tickets through the service, and an unauthorized applet tries the same call. Four classes in four packages, run on a contactless D600 card. I measured what could be measured; spec-only boundaries are called out separately.
One door, two contexts
Section titled “One door, two contexts”The Java Card firewall isolates package contexts: applets in different packages cannot touch each other’s objects. The exception is an interface that extends javacard.framework.Shareable. When a client invokes such an interface, the JCRE switches the active context to the server, runs the method there, and switches back on return.
So a shareable interface is not an ordinary Java interface:
- the interface must extend
Shareable(itself an empty marker interface); - the server applet implements it and decides in
getShareableInterfaceObjectwhich object, if any, to hand out; - the client obtains the reference from
JCSystem.getAppletShareableInterfaceObject, casts it to the interface type and calls it; - the method body runs in the server context, while object references passed by the client are still subject to the firewall.
The demo has four packages, one of them a deliberate rogue:
| Package | Content | Purpose |
|---|---|---|
com.javacardwiki.sio.api |
TicketService |
Contract only; both sides import it |
com.javacardwiki.sio.server |
TicketServerApplet |
Owns the balance, implements TicketService |
com.javacardwiki.sio.client |
TicketClientApplet |
Buys tickets through the SIO |
com.javacardwiki.sio.rogue |
TicketRogueApplet |
Unauthorized caller, used to verify the refusal path |
Putting the contract in its own package is not tidiness for its own sake. The client imports the contract, not the server implementation, so the server can be replaced without recompiling the client. Conversely, while any client imports the contract package, the JCRE refuses to delete that package.
Contract, server, client
Section titled “Contract, server, client”The contract keeps the business methods and a few probes used only to observe behaviour:
package com.javacardwiki.sio.api;
import javacard.framework.Shareable;
public interface TicketService extends Shareable { short addTickets(short count); short getTickets();
// Demo probes only; a real service would not expose these short callerAidLength(); byte callerAidByte(short index); short fill(byte[] buffer, short offset, short length); void fail();}The server grants access in getShareableInterfaceObject, which the JCRE calls when a client asks for the SIO. clientAID is supplied by the JCRE:
public Shareable getShareableInterfaceObject(AID clientAID, byte parameter) { if (!clientAID.equals(clientAppletAid)) { return null; }
callerAidLength = clientAID.getBytes(callerAid, (short)0); return this;}
public short addTickets(short count) { requireCaller(); // re-check the caller on every invocation tickets += count; return tickets;}
private AID requireCaller() { AID caller = JCSystem.getPreviousContextAID(); if (caller == null || !caller.equals(clientAppletAid)) { ISOException.throwIt(ISO7816.SW_SECURITY_STATUS_NOT_SATISFIED); } return caller;}The client fetches once, stores the reference, and calls it:
private TicketService getService() { if (service == null) { Shareable sio = JCSystem.getAppletShareableInterfaceObject(serverAid, (byte)0); if (sio == null) { ISOException.throwIt(ISO7816.SW_FILE_NOT_FOUND); } service = (TicketService)sio; } return service;}Storing the SIO in the service field is convenient, but it has a lifecycle consequence described below.
What showed up on the card
Section titled “What showed up on the card”The test environment is the same card as the benchmark: the contactless reader ACS ACR1552 1S CL Reader PICC 0, CPLC D600 (commonly JCOP 4.5 / J3R452; card signals and performance data). The client commands returned:
| Observation | Measured result |
|---|---|
clientAID passed to getShareableInterfaceObject |
F053494F434C4901 — 8 bytes, exactly the client applet AID |
JCSystem.getPreviousContextAID() inside a method |
the same 8-byte applet AID |
The client’s own JCSystem.getAID() |
also the same AID |
getShareableInterfaceObject count after two commands |
still 1, so the client cached the SIO |
| Unauthorized rogue applet asks for the SIO | 0000, i.e. null |
Ordinary byte[] passed to the server, server writes it |
server catches SecurityException, returns -1; the array is unchanged |
Array created with JCSystem.makeGlobalArray, passed to the server |
works; all four bytes become A5 |
| Storing that global array in a client field | SecurityException on assignment (probe returned -6) |
Server calls ISOException.throwIt(0x6A80) |
client catches reason 6A80 |
| Client starts a transaction, SIO adds 100, client aborts | balance is 1 before and after; the server’s write rolled back |
gp --delete on the server applet while the client caches the SIO |
0x6985, deletion refused |
gp --delete on the contract package while the client imports it |
0x6985, deletion refused |
The easiest wrong assumption is AID granularity. getPreviousContextAID() returns the AID of the applet instance that was active at the last context switch, not the package AID. The measured 8-byte value confirms it. Authenticate by instance AID; isolation is per package: applets in one package share a context and do not need an SIO to reach each other, while cross-package calls should use an applet-AID allow-list in the server, not a package AID comparison.
Five things that actually trip people up
Section titled “Five things that actually trip people up”1. Ordinary arrays do not cross; use a global array.
Hand the server a client byte[] and the first write raises SecurityException. That is not an implementation quirk: an ordinary array belongs to the context that created it, and another context may only access its own arrays or a global array.
Java Card 3.0.5 lets an applet create one:
byte[] global = (byte[])JCSystem.makeGlobalArray(JCSystem.ARRAY_TYPE_BYTE, (short)4);The API arrived in 3.0.4, so 2.2.2 code cannot rely on it. More importantly, a global array is a temporary object: it can be used for the current command but must not be assigned to a field, instance variable or array element. My first version stored the result of makeGlobalArray in a client field, and the command returned SecurityException; with a local variable, the server wrote A5 A5 A5 A5 without complaint.
2. Do not use exceptions to refuse in getShareableInterfaceObject.
Return null. The specification requires the client to be unable to distinguish “server AID not found”, “server does not participate”, “caller not allowed” and “the server threw an exception”: the JCRE swallows exceptions from getShareableInterfaceObject, and the client sees only null. The client must handle null; it cannot assume the server is present.
3. Use ISOException for errors that cross the boundary.
ISOException.throwIt(sw) uses an exception instance owned by the JCRE, which is safe across contexts; the client caught reason 6A80. An exception object created by the server can be caught by the client, but reading its fields or calling its methods may then hit the firewall. Put the status in the ISO exception or in a return value; do not ask the client to interpret an object owned by the server.
4. Transactions cross the SIO; the server must not start its own.
After a client calls beginTransaction(), the server’s persistent writes during an SIO call belong to the same transaction. A client abortTransaction() rolls them back — measured: balance 1 before and after. Conversely, the server should not call beginTransaction() inside an SIO method: the transaction is a card-wide singleton, and starting one while the client already has one returns IN_PROGRESS.
5. Caching the SIO is convenient, but it blocks deletion of the server.
Storing the SIO in a client field really does avoid repeated getShareableInterfaceObject calls (the count stayed at 1 across two commands). The price is that the server applet owns an object referenced by a client instance, and the JCRE guarantees that deleting the server applet fails — gp --delete returned 0x6985. For the same reason, deleting the contract package fails while a client imports it.
If the server must be replaceable, do not keep the SIO forever: fetch it before use and check for null, or accept a maintenance order of “delete the client first, then the server”. That ordering is easy to get wrong during upgrades, especially after splitting contract, server and client into three packages.
What one SIO call costs
Section titled “What one SIO call costs”An SIO call is a context switch, not a local method call. To measure it, the client got two loop commands: one repeatedly calls getTickets() (which performs getPreviousContextAID plus an AID comparison on every call), the other repeatedly calls ping() (returns a constant, no authorization). The host timed the whole APDU at n = 0 / 1000 / 2000 and fitted the slope, using the same differential method as the benchmark:
| Call | Per-call cost (fitted slope) |
|---|---|
ping(), no authorization |
about 187 µs |
getTickets(), caller checked every time |
about 787 µs |
Fixed APDU cost (n = 0 intercept) |
about 4.9 ms |
On this card, even the cheapest SIO call costs almost 0.2 ms, and adding the per-call getPreviousContextAID and AID comparison pushes it close to 0.8 ms. Authorization dominates; an SIO is not a microsecond-level local method. If an operation would loop hundreds of times, move the loop into the server and batch it instead of bouncing between the two contexts.
Reproducing it
Section titled “Reproducing it”Put the four classes into the four packages described above and convert them in order: contract, server, client, rogue. One tool detail: the contract package has no applet, so gp --install reports “CAP file has no applets”; use gp --load to load the package, then --install the applet packages. The load order is contract → server → client; uninstall is the reverse.
gp -r "<reader>" --load api.capgp -r "<reader>" --install server.capgp -r "<reader>" --install client.capThe client AID is F053494F434C4901; select it and send the APDUs from the table above. To test the rogue path, select F053494F524F4701 instead and send 8001000000; it should return 0000.
This case covers only a single channel and a single client. Two spec boundaries are still worth remembering: a non-MultiSelectable server already active on another logical channel makes getAppletShareableInterfaceObject throw SecurityException, and RMI (javacard.framework.service) is an optional component rather than something every card implements, so it is not required for cross-applet calls.
One last caveat: these behaviours were measured on a Java Card 3.0.5 API, CPLC D600 JCOP card; the card’s getVersion() reports only 0x0300, which says nothing about the patch level. On another card or JCRE implementation, the array, exception and deletion error codes can differ. Run these probes before writing business code; it is more reliable than copying the documentation.