4

I have an empty Javacard as below:

user@system$ java -jar gp.jar --list
ISD: A000000003000000 (OP_READY)
     Privs:    SecurityDomain, CardLock, CardTerminate, CardReset, CVMManagement

And I have installed a weird applet on it:

user@system$ java -jar gp.jar --install applet.cap
CAP loaded
user@system$ 

Well, the applet is installed successfully:

user@system$ java -jar gp.jar --list
ISD: A000000003000000 (OP_READY)
     Privs:    SecurityDomain, CardLock, CardTerminate, CardReset, CVMManagement

APP: 01020304050601(SELECTABLE)

PKG: 010203040506(LOADED)

user@system$ 

But when I am try to delete the applet I fail:

user@system$ java -jar gp.jar --delete 01020304050601 --debug --verbose

# Successful Mutual Authentication with Security Level = 01 (CMAC)

A>> 84F28002 0A 4F00<CMAC> 00
A<< E3114F08A0000000030000009F700101C5019E 9000

A>> 84F24002 0A 4F00<CMAC> 00
A<< E3104F07010203040506019F700107C50100 9000

A>> 84F21002 0A 4F00<CMAC> 00
A<< E30F4F060102030405069F700101C50100 9000

A>> 84F22002 0A 4F00<CMAC> 00
A<< E30C4F060102030405069F700101 9000

A>> 84E40000 11 4F0701020304050601<CMAC>
A<< 6985

Could not delete D3646467329901. Some app still active?

I have tried the following forms of delete command too:

user@system$ java -jar gp.jar --delete 01020304050601 --force  ==> Same result
user@system$ java -jar gp.jar --delete 010203040506  ==> Same result

The only successful form which I can use to delete the applet, is as below:

user@system$ java -jar gp.jar --delete 010203040506 --force

# Successful Mutual Authentication with Security Level = 01 (CMAC)

A>> 84F28002 0A 4F00<CMAC> 00
A<< E3114F08A0000000030000009F700101C5019E 9000

A>> 84F24002 0A 4F00<CMAC> 00
A<< E3104F07010203040506019F700107C50100 9000

A>> 84F21002 0A 4F00<CMAC> 00
A<< E30F4F060102030405069F700101C50100 9000

A>> 84F22002 0A 4F00<CMAC> 00
A<< E30C4F060102030405069F700101 9000

A>> 84E40080 10 4F06010203040506<CMAC>
A<< 9000

The applet and its packages is gone now:

user@system$ java -jar gp.jar --list
ISD: A000000003000000 (OP_READY)
     Privs:    SecurityDomain, CardLock, CardTerminate, CardReset, CVMManagement

Question: Why do I need to delete the package to delete the applet itself?

Note that this situation appears for a specific cap file only.

FYI: I have tried different tools (Including gpshell, pyapdu, etc.) and I got same results.

1 Answers1

5

The uninstallation process of the applet does not run successfully, therfore the applet instance cannot be deleted alone, most probably you have static content like private static transient byte array declared in the applet code. In this case you must deinitialize any static objects to null by overriding Applet's uninstall method. Check object ownership in the JCRE to understand the problem

Paul Bastian
  • 2,597
  • 11
  • 26
  • Why JCRE does not do this operation (deinitialization of static objects) automatically? Is this due to limited resources within the card? Moreover, since the owner of the static objects is somehow the package (i.e. the Executable Load File) and the removal of the package destroys the static data used in the applets, it makes sense to me to force the user to delete applets before deleting the package (It also indirectly warns the user that deleting the package will cause the applets to crash). But the reverse process restriction seems unneccessary to me. Am I right? – Ebrahim Ghasemi Dec 15 '20 at 17:12
  • Ah, I know the OP dear Paul. We tried this weird applet on different types of Javacard. On some of them we can delete the applet by alone, but on some of them we need to delete the package with the containing applets. How do you explain this? Which group of cards works properly? – Ebrahim Ghasemi Dec 15 '20 at 17:16
  • And my final note: We couldn't delete the applet or the package by alone. We only could delete the package + all related objects (i.e. containing applets) all together simultaneously using DELETE APDU Command with `P2 = 80` and `data = package AID`. Doesn't `P2 = 80` means/implemented as: 1) delete all the applets first and 2) delete the package after that.? – Ebrahim Ghasemi Dec 15 '20 at 17:19
  • I experienced this with all NXP cards – Paul Bastian Dec 15 '20 at 17:24
  • 2
    I think it is not allowed because you would otherwise lose the reference to the static object and therefore you would have dangling, dead objects. – Paul Bastian Dec 15 '20 at 17:25
  • 1
    I'm currently not working on java card, but I'm sure that we ran into the same problem and our reasoning seemed logical after reading the JCRE carefully multiple times – Paul Bastian Dec 15 '20 at 17:27
  • Thank you. Could this be the source of the problem raised in [this other SO question](https://stackoverflow.com/questions/64848060/delete-apdu-command-with-ssd-aid-in-its-data-field-returns-6985?noredirect=1#comment114911477_64848060)? – Ebrahim Ghasemi Dec 15 '20 at 17:31