-2

my card is gemalto v2.2.1,T=0 and It doesn't support apdu chain. I saw samples that they are using ExtendedLength but my card not supported. I wrote a code but I have problem with it. Can every body tell me Where do I make mistake or please give me a solution.

public class ap2 extends Applet {

private Object[] FileArray;

private ap2() {

// I want to save three binary data
    FileArray=new Object[3];

}

public static void install(byte bArray[], short bOffset, byte bLength)
        throws ISOException {
    new ap2().register();
}

public void process(APDU arg0) throws ISOException {
    // TODO Auto-generated method stub

}

private void ReadBinaryData(APDU apdu)
{

    short offset = getoffset_read(apdu);
    byte[] buf= apdu.getBuffer();

    //I send file index in p1 from host and get File Index in card
    byte p1value=buf[ISO7816.OFFSET_P1];

    byte[] FileObj=(byte[]) FileArray[p1value];

    short le = apdu.setOutgoing();

    if(le == 0 || le == 256) {
            le = (short)(FileObj.length - offset);
            if(le > 256) le = 256;
        }

    boolean eof = false;
    if((short)(FileObj.length - offset) < le) {
        le = (short)(FileObj.length - offset);
        eof = true;
    }

    apdu.setOutgoingLength(le);
    apdu.sendBytesLong(FileObj, offset, le);

    if(eof) {
        ISOException.throwIt(SW_END_OF_FILE);
    }
}



private short getoffset_read(APDU apdu)
{
    ?????????????????

}



private void WriteBinaryData(APDU apdu)
{
  if(FileCount==3)
  {
      ISOException.throwIt(SW_END_OF_ThreeFILES);
  }
  byte[] buf = apdu.getBuffer();
  short offset = getoffset_write();

  byte lc=buf[ISO7816.OFFSET_LC];

  if((short)(offset + lc) >((byte[])FileArray[FileCount]).length) {
      ISOException.throwIt(SW_WRONG_LENGTH);
  }

  apdu.setIncomingAndReceive();
  Util.arrayCopyNonAtomic(buf, ISO7816.OFFSET_CDATA,(byte[]) FileArray[FileCount],offset,lc);


}

private short getoffset_write()
{
    ????????????????????
}

I don't know how to calculate offset.

Ftm
  • 99
  • 1
  • 11
  • I want to write binarydata from android in card. – Ftm Aug 28 '16 at 07:01
  • You could try to change your variable `DataOffset` into `short`. (Maybe try to debug your code using desktop java first. [jCardSim](https://jcardsim.org/) is an interesting tool to use). It is difficult to understand your internal logic (e.g. the reasons for some checks)...Good luck! – vlp Aug 28 '16 at 17:25
  • 2
    You should study the ISO7816-4 command for updateBinary and stick to that. Thereby you send chunks of 256 byte of data and increment the offset in p1p2 accordingly – Paul Bastian Aug 28 '16 at 17:34
  • Thank you for answer me. I used p1 for recognize block number for example p1=1: data in buffer is first 256 byte and p1=5 : data in buffer is fifth 256 byte, and when I send last block data I send p2=1. Is It correct? – Ftm Aug 29 '16 at 03:34
  • You should stick to the ISO7816-4 command because everyone involved with smartcards will then understand how to use the card implicitly. Your procedure is probably nowhere documented, so it is hard to understand and follow your approach – Paul Bastian Aug 29 '16 at 08:26
  • 1
    also we are missing global / Class variables to udnerstand your code... – Paul Bastian Aug 29 '16 at 08:27
  • I edit my code. Can every body help me? thanks a lot – Ftm Aug 29 '16 at 10:21

1 Answers1

0

The offset is encoded into bytes P1P2 as a short value

short offset = Util.getShort(apdu.getBuffer(), ISO7816.OFFSET_P1);

You shall not select the file by P1 byte, but select it prior to reading/writing data with the Select command. Please study the ISO7816-4 in detail as this will solve all your problems.

Paul Bastian
  • 2,597
  • 11
  • 26