Hi, this is related to my March 21 post:
FileView.updateBinary() question/problem with SJA5
I’m seeing a possible problem using FileView to read/write USIM files with the SJA5-S17. When I use FileView.updateBinary() from processToolkit() it works fine. But when I attempt to update the file from process() it always overlays the first part of the file with 5 bytes of unexpected data, one random non-zero byte followed by d6000000.
Here is some test that can recreate the issue. It updates EF.LOCI with the value in P1 repeated 11 times, then reads it back. It will then return 22 bytes – the value written followed by the value read back.
package org.mayhayze;
import javacard.framework.*;
import uicc.toolkit.*;
import uicc.access.*;
import uicc.usim.access.*;
import uicc.system.UICCPlatform;
import uicc.usim.toolkit.ToolkitConstants;
public final class Test42 extends Applet implements ToolkitInterface {
private static final byte[] usimAID = {
(byte)0xa0,(byte)0x00,(byte)0x00,(byte)0x00,(byte)0x87,(byte)0x10,(byte)0x02,(byte)0xff,
(byte)0xff,(byte)0xff,(byte)0xff,(byte)0x89,(byte)0x07,(byte)0x09,(byte)0x00,(byte)0x00
};
private byte[] thisAID;
private ToolkitRegistry regEntry;
private FileView file;
private Test42() {}
private void init(byte[] bArray, short bOffset, byte bLength) {
register();
regEntry =
ToolkitRegistrySystem.getEntry();
regEntry.setEvent(ToolkitConstants.EVENT_UNRECOGNIZED_ENVELOPE);
regEntry.setEvent(ToolkitConstants.EVENT_PROACTIVE_HANDLER_AVAILABLE);
regEntry.setEvent(ToolkitConstants.EVENT_FIRST_COMMAND_AFTER_ATR);
regEntry.requestPollInterval((short)30);
file = UICCSystem.getTheFileView(
usimAID,
(short)0,
(byte)16,
JCSystem.CLEAR_ON_RESET);
thisAID = new byte[33];
byte len = bArray[bOffset];
thisAID[0] = len;
Util.arrayCopyNonAtomic(
bArray,
(short)(bOffset + 1),
thisAID,
(short)1,
(short)(len));
}
public static void install(byte[] bArray, short bOffset, byte bLength) {
Test42 applet = new Test42();
applet.init(bArray, bOffset, bLength);
}
public void processToolkit(short event) throws ISOException {}
public void process(APDU apdu) throws ISOException {
short lc = apdu.setIncomingAndReceive();
byte[] apduBuf = apdu.getBuffer();
if (selectingApplet()) {
byte aidLen = thisAID[0];
apduBuf[(short)0] = (byte)(0x6f);
apduBuf[(short)1] = (byte)(aidLen + 2);
apduBuf[(short)2] = (byte)(0x85);
Util.arrayCopyNonAtomic(
thisAID,
(short)0,
apduBuf,
(short)3,
(short)(aidLen + 1));
apdu.setOutgoingAndSend((short)0, (short)(aidLen + 4));
return;
}
byte cla = (byte)(apduBuf[ISO7816.OFFSET_CLA] & (byte)0xfc);
byte ins = apduBuf[ISO7816.OFFSET_INS];
byte p1 = apduBuf[ISO7816.OFFSET_P1];
if ((cla != (byte)0x80) || (ins != (byte)0xe2)) {
ISOException.throwIt(ISO7816.SW_INS_NOT_SUPPORTED);
}
Util.arrayFillNonAtomic(
apduBuf,
(short)0,
(short)11,
p1);
file.select(USIMConstants.FID_EF_LOCI);
file.updateBinary(
(short)0,
apduBuf,
(short)0,
(short)11);
file.readBinary(
(short)0,
apduBuf,
(short)11,
(short)11);
short le = apdu.setOutgoing();
if (le < (short)22) {
ISOException.throwIt(ISO7816.SW_WRONG_LENGTH);
}
apdu.setOutgoingLength((short)22);
apdu.sendBytes((short)0, (short)22);
}
}
To build the test, I used these libs. The behaviour is consistent across several library versions though. The behaviour is also consistent in a card reader or through a modem using AT+CSIM.
sdk-jc/jc221_kit/api_export_files
lib-etsi/102.241/13.01.01/exports
lib-gp/1.5/exports
Here are the pySim command you can use to see the effect (assuming the applet is built for AID F00000005500123456789abc. Note you can also separately select EF.LOCI from the pySim shell and see that the wrong data is written to the file:
apdu --raw 00a404000CF00000005500123456789abc
Warning: received unexpected response data, incorrect APDU-case (3, should be 4, missing Le field?)!
SW: 9000, RESP: 6f0e850cf00000005500123456789abc
apdu --raw 80e2550000
SW: 9000, RESP: 555555555555555555555502d6000000555555555555
apdu --raw 80e2aa0000
SW: 9000, RESP: aaaaaaaaaaaaaaaaaaaaaa6ed6000000aaaaaaaaaaaa
Note: here is the GP command to install the applet:
java -jar gp.jar --key-dek %KIK% --key-enc %KIC% --key-mac %KID% --params c900ea26800aFF00000001000000000081180001000010a0000000871002ffffffff8907090000010000 --install mayhayze.cap --verbose
When you’re done, this will clean it up (assuming it’s built for AID F00000005500123456789abc/F000000055)
java -jar gp.jar --key-dek %KIK% --key-enc %KIC% --key-mac %KID% --delete F00000005500123456789abc
java -jar gp.jar --key-dek %KIK% --key-enc %KIC% --key-mac %KID% --delete F000000055 --verbose