Search code examples
javaappletsmartcardjavacardglobalplatform

How to verify if an applet is already installed on a Java Card


I'm writing a program that is able to upload an applet automacaly once the card is inserted into the reader.

I've done this part and i"d like to be able to verify if the applet is already installed on the card before trying to install it again. I'm using the javax.smartcardio package

this what what i think i'm supposed to use in order to send the command :

            Card card = terminal.connect("T=0");
            CardChannel channel = card.getBasicChannel();
            ResponseAPDU resp = channel
                                .transmit(new CommandAPDU());

but i don't know how to send the command using may applet's AID. My applet's AID is 01020304050607080900


Solution

  • Each applet has an unique AID (application identifier). You could list all installed applications using the GlobalPlatform GET STATUS command, but the easiest way is to execute just a SELECT command. If the applet is installed the SELECT command will return a response ending with 9000 as status word. 6a82 means "file not found" - this indicates the file is not found, i.e. the applet is not installed.

    The command is:

    00A40404 <AID length> <your AID>
    
    byte[] aid = {(byte)0x01, (byte)0x02, (byte)0x03, (byte)0x04, (byte)0x05, (byte)0x06, (byte)0x07, (byte)0x08, (byte)0x09, (byte)0x00};
    ResponseAPDU resp = channel
                                    .transmit(new new CommandAPDU(0x00, 0xA4, 0x04, 0x00, aid, 0xFF));
    if (resp.sw == 0x9000) {
    // applet is already installed
    }
    

    Take note, that if the applet installation has failed, only the package will be installed, the applet will not have instantiated and cannot be detected this way, but I assume you already have handled this during the installation process.

    Another alternative is just to let the installation happen, usually the error reported during the installation process would be 0x6A89, meaning "file already exists".