ISO/IEC 7816-4:2013 Compliant Applet Example

This example demonstrates how to copy the security state from the applet selected in the origin channel into the new channel.

In this example, the state information is stored in the array appState inside the applet:

StateObj appState[MAX_CHANNELS];    // Holds the security state
                                    // for each logical channel

You can use the APDU.getCLAChannel() and the JCSystem.getAssignedChannel() methods to identify if the applet selection case corresponds to an ISO/IEC 7816-4 case where the security state needs to be copied.

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:

   public boolean select(boolean appInstAlreadySelected) {
                ...
                // Obtain logical channels information
                // This call returns the channel where 
                // the command was issued
                byte origChannel = APDU.getCLAChannel();
                // This call returns the channel where the applet is being
                // selected
                byte targetChannel = JCSystem.getAssignedChannel();
                if (origChannel == targetChannel) {
                        // This is a SELECT FILE command.
                        // Do processing here.
                        ...
                }
                if (origChannel == 0) {
                        // This is a MANAGE CHANNEL command from channel 0.
                        // ISO 7816-4 state sharing case does not 
                        // apply here.
                        // Do processing here.
                        ...
                } else {
                        // Since origChannel != 0, the special 
                        // ISO 7816-4 case applies.
                        // Copy security state from origin channel 
                        // to assigned logical channel.
                        appState[targetChannel] = appState[origChannel];
                        // Do further processing here
                        ...
                }
                ...
        }

Refer to the API documentation in the in JC_CLASSIC_HOME\docs for more information about the APIs.