Question

I am encountering a problem when I create a Directory File. I am obviously not creating a new Object, but only the reference - just still not sure where/why!

DirectoryFile[] arrayDF = new DirectoryFile[8];

now when I create a new DF:

numapp++;
arrayDF[numApp] = new DirectoryFile(aid);

and right after that I simply print them out in a loop

APDU apdu = APDU.getCurrentAPDU();
apdu.setOutgoing();
apdu.setOutgoingLength((short) 21);

for (i = 2; i <= numApp; i++) {          
     apdu.sendBytesLong(arrayDF[i].getAID(), (short) 0, (short) 6);

}

where numApp is the number of Applications/DFs, starting with 2, since 1 is the MasterFile. for aid = a00000000001 I get

A0 00 00 00 00 01 90 00                         .........
Status: No Error

as response for aid = a00000000002 I get

A0 00 00 00 00 02 A0 00 00 00 00 02 90 00    ................
Status: No Error

for aid = a00000000003 I get

A0 00 00 00 00 03 A0 00 00 00 00 03 A0 00 00 00 00 03 90 00    ................
    Status: No Error

So all the already saved AIDs get somehow overwritten. In DirectoryFile I do as follows

public DirectoryFile(byte[] aid) {
        super(aid);
        for (byte i = 0; i < activatedFiles.length; i++)
            activatedFiles[i] = false;

    }

where super(aid) calls the constructor of File.java

     public abstract class File {
            public byte[] aid = new byte[6];

            public File (byte[] aid) {
                this.aid = aid; 
            }
            public byte[] getAID() {
                return aid;
            }
     }

Where is my mistake in doing this?

After a bit more testing I at least found out that the classes (DirectoryFile, File) should work just fine:

 aDF[j] = new DirectoryFile(aid1);
      j++;
      aDF[j] = new DirectoryFile(aid2);
      j++;
      aDF[j] = new DirectoryFile(aid3);
      j++;
      aDF[j] = new DirectoryFile(aid4);
      j++;
      APDU apdu = APDU.getCurrentAPDU();
      apdu.setOutgoing();
      byte[] myi = new byte[1];
      apdu.setOutgoingLength((short) 28);
      for (j = 0; i < 4; i++) {      
        myi[0] = i;       
        apdu.sendBytesLong(myi, (short) 0, (short) 1);
        apdu.sendBytesLong(aDF[i].getAID(), (short) 0, (short) 6);

will print

00 A0 00 00 00 00 01 01 A0 00 00 00 00 02 02 A0    ................
    00 00 00 00 03 03 A0 00 00 00 00 04 90 00          ..............
Status: No Error
Was it helpful?

Solution

Your File implementation is not correct, you first generate an array of six bytes, but instead of copying the AID into it - using Util.arrayCopy() - you simply assign it the reference to the passed in array, throwing a way the reference to the previously generated aid buffer. So if the values of the passed in AID changes value, the value of the AID in File also changes.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top