2

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?

ally
  • 73
  • 1
  • 1
  • 5

1 Answers1

2

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.

guidot
  • 5,095
  • 2
  • 25
  • 37
  • Thanx guidot. I removed the chaining and made my host application reads small chunks of 256 bytes, and each time increase the offset by 256. It works fine. I even tried to read a 20 Kb file and the results were positive. So I assume chaining can not withstand very large files. I migh be wrong anyway. – ally Mar 27 '13 at 23:08
  • You might still try extended length APDUs to get bigger chunks, but the supported limit depends on the card. Fewer commands to be sent mean less overhead, so you may save some time, which may be noticable for a 20 kByte file. – guidot Mar 28 '13 at 07:36
  • @ally,Can you share code with me I am new to Java card and having same issue to store file of bigger size. – Suraj Jul 23 '14 at 01:11