Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Card Read Data from Array

I have created an Smartcard application where i can store data up to 60KB in an Byte Array. But when i read the Array multiple Times i get an error and i cant access the Data anymore.

Code create Array:

public void createFile(short fileID, short fileSize) {
    short index = getFileIndex(fileID);

    if(listFiles[index] == null) {
        listFiles[index] = new byte[fileSize];
    }

    listfileSizes[index] = fileSize;
}

Code Read Data:

public byte[] readDataFromFile(short fileID, short fileOffset, short length) {

    short selFileSize = getFileSize(fileID);
    byte[] data = new byte[length];

    if (selFileSize < (short)(fileOffset + length)) {
        ISOException.throwIt(ISO7816.SW_FILE_FULL); 
    }

    Util.arrayCopy(getFile(fileID), fileOffset, data, (short)0, length);

    return (byte[])data;
}

Code access Read:

short data_length = Util.getShort(buf, (short)(offset_cdata + 2));
    short file_offset = Util.getShort(buf, offset_cdata);

    if(p2 == (byte)0x01) {

        Util.arrayCopy(myfile.readDataFromFile(myfile.keepassData1, file_offset, data_length), (short)0, buf, (short)0, data_length);

    } else if (p2 == (byte)0x02) {

        Util.arrayCopy(myfile.readDataFromFile(myfile.keepassData2, file_offset, data_length), (short)0, buf, (short)0, data_length);

    }

when i reinstall the app i can read and write, but only a few time until the data is blocked. I get the error 6f00.

like image 628
schisskiss Avatar asked Aug 27 '26 20:08

schisskiss


1 Answers

Your applet is running out of the persistent memory, hence the error.

This line

byte[] data = new byte[length];

allocates a new persistent byte array each time the method is called! This object is never deallocated, because Java Card does not support any automatic garbage collector.

You should copy the data directly into the APDU buffer:

private final byte[] readDataFromFile(short fileID, short fileOffset, short length, byte[] outBuffer, short outOffset) {
    final short selFileSize = getFileSize(fileID);
    if (selFileSize < (short)(fileOffset + length)) {
        ISOException.throwIt(ISO7816.SW_FILE_FULL); 
    }
    Util.arrayCopyNonAtomic(getFile(fileID), fileOffset, outBuffer, outOffset, length);
}
like image 198
vojta Avatar answered Aug 30 '26 10:08

vojta



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!