Search code examples
javacardapduglobalplatform

How to detect interface of incoming command?


I have a dual interface Java card that contains my applet. I want to have two different APDU response for a single command from different interfaces.

For example I want to respond to 00 10 00 00 APDU command with "Contact" when the command is received from the contact interface and respond "ContactLess" when this command is received from the contactless interface.

So, is there any method in the Java Card APIs or Global Platform APIs to detect the incoming command's interface?


Solution

  • There is a method called getProtocol() in the javacard.framework.APDU class:

    public static byte getProtocol()

    Returns the ISO 7816 transport protocol type, T=1 or T=0 in the low nibble and the transport media in the upper nibble in use.

    The interface is encoded in the upper nibble of the returned byte:

    final byte transportMedia = (byte) (APDU.getProtocol() & APDU.PROTOCOL_MEDIA_MASK); 
    final boolean isContactless = (transportMedia == APDU.PROTOCOL_MEDIA_CONTACTLESS_TYPE_A) || 
             (transportMedia == APDU.PROTOCOL_MEDIA_CONTACTLESS_TYPE_B);