Reading big file from a javacard applet

你。 提交于 2019-12-19 04:16:34

问题


I am writing an applet that stores 3 files of different sizes 5 Kb, 7 Kb and 11 Kb. I have got no problems with storing the files inside the applet. But when I try to read them back, I can only read the first two (smaller files). The third file throws an exception:

javax.smartcardio.CardException: Could not obtain response
at sun.security.smartcardio.ChannelImpl.doTransmit(Unknown Source)
at sun.security.smartcardio.ChannelImpl.transmit(Unknown Source)

I have tried to figure out the problem and I have found out it has to do with the size of the file. So I created a test file of size 7 Kb and incremented this file bit by bit. It worked until I reached 7905 bytes. It means 7905 bytes is the maximum number of bytes I can read from the applet. I am chaining the response using sample code:

public void readFile(APDU apdu, short[] offset, short selectedFile, short MAX_APDU_SEN,       byte OFFSET_SENT) {
    byte[] file = getFile(selectedFile); 
    if (file == null) {
    + ISOException.throwIt(ISO7816.SW_FILE_NOT_FOUND);+
    }
    // work out how many bytes to send this time and how many will be left
    short remain = (short) (file.length - offset[OFFSET_SENT]);
    boolean chain = remain > MAX_APDU_SEN;
    short sendLen = chain ? MAX_APDU_SEN : remain;
    apdu.setOutgoing();
    apdu.setOutgoingLength(sendLen);
    apdu.sendBytesLong(file, offset[OFFSET_SENT], sendLen);
    // Check to see if there are more APDU's to send
    if (chain) {
    +offset[OFFSET_SENT] = sendLen; // count the bytes sent
    ISOException.throwIt(ISO7816.SW_BYTES_REMAINING_00); // indicate there are more bytes     to come
    } else {+
    offset[OFFSET_SENT] = 0; // no more bytes to send
    }
}

I have tried two different types of cards i.e JC 2.2.1 (36Kb) and JC 2.2.2 (80Kb) compatible cards but they all behave the same.

Any help please?


回答1:


Reading a file is typically not done using chaining, since the host application can conveniently specify the start offset in P1/P2 - at least in an READ BINARY command as specified in ISO 7816-4. I assume, that even for a chained response the card wants to prepare the data in a buffer, whose limited size I assume to be the reason for your problem.



来源:https://stackoverflow.com/questions/15645679/reading-big-file-from-a-javacard-applet

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!