Search code examples
nfcsmartcardjavacardapducontactless-smartcard

How to pass any APDU command from currently selected applet to another applet?


Question: Suppose there are multiple instance of an Applet. Currently one Applet instance is selected. Is it possible to pass any APDU command from the selected Applet to another Applet instance? I did not find such any method in JavaCard API version 3.0.4.

Details: While researching the document "Expresspay Mobile Specifications v1.2" for implementing the 'Application Selection' service, I get stuck in the following section 5.3.6. As the document is not open source, I'm quoting the selective part:

"If an Expresspay Mobile Application is selected over the contact less interface using the Partial AID, then it shall pass the SELECT command and subsequent commands to the activated Expresspay Mobile Application instance (which may or may not be the application being selected)."

It states that I need to pass a SELECT command from currently selected applet to the another applet (which is activated!). But I did not find anything in JavaCard API that will pass any command from one applet to another applet. I searched on Oracle Community and found this with no reply!

Edit: Suppose there are three instances A1, A2, A3 of applet A. Applet A has a shareable flag object flag, containing AID of "active" instance, say AID of A2. (Assume that flag may be pre-initialized). Currently instance A1 is selected. Now the terminal sends any APDU command, that will come to instance A1. A1 will check the flag and find out that flag contains AID of A2. So, A1 will pass the APDU command to A2 for further processing. My question is how A1 instance send any APDU command to A2 instance?


Solution

  • "If an Expresspay Mobile Application is selected over the contact less interface using the Partial AID, then it shall pass the SELECT command and subsequent commands to the activated Expresspay Mobile Application instance (which may or may not be the application being selected)."

    Most Java Card smartcards that I came across so far supported selection by partial AID (while the Java Card RE specification has no requirement on this, the GlobalPlatform card specification (see section "Explicit Selection on Basic Logical Channel") does mandate support for selection by partial AID). Hence, the SELECT command will typically cause the currently selected applet to change to an applet with a matching AID.

    So if that newly selected applet is not necessarily the "activated Expresspay Mobile Application instance", then you could possibly pass on all communication through a shareable interface that is provided by the activated applet instance.

    Assume that the SELECT (by partial AID) command causes B to be the currently selected applet and that A is the "activated" applet instance. A would then implement a shareable interface and provides a method that can be used to pass commands to and return responses from A. B can then get access to that shareable interface through getShareableInterfaceObject(AID of A, ...) and can use that interface to call into A in order to forward commands to it. The actual APDU communcation would still have to be done in applet B though. Moreover, it seems rather inefficient to pass commands and responses between B and A. Also, this would not switch the currently selected applet to A, hence subsequent commands would still go to B and would also need to be passed through that interface.

    A much better approach seems to be to let the GlobalPlatform OPEN handle the above requirement (and I assume that's how this is meant to be implemented). For a SELECT (by partial AID) command, the OPEN (see section "Explicit Selection on Basic Logical Channel" of the GP Card specification) will automatically choose the first selectable applet instance. Hence, in order to mark one of your applet instances as active, you would actually deactivate all other applet instances for the contactless interface. In order to do this, your managing applet (CRS application) would need to have the Contactless Activation privilege (and/or each applet instance would need the Contactless Self-Activation privilege to manage its own activation). You can then use the GlobalPlatform API to manage the activation state of each applet instance. See Amendment C to the GP Card specification. The interesting methods of the GP card API seem to be GPCLSystem.getGPCLRegistryEntry(AID) and GPCLRegistryEntry.setCLState(state).