shahbaz
shahbaz

Reputation: 11

How to write/read X509 certificate in javacard 2.2.1

I want to store three certificates.

I've got two related questions:

  1. how to write method Get_Offset_Read(), Get_Offset_Write() and calculate offset in buffer for write and read in buffer?
  2. I use an ObjectArray to store certificate data. Is that a correct way of storing such data?

Here's the code:

public class writeApplet extends Applet {
     private Object[] FileArray;
     private byte  FileCount;
     private writeApplet() {
     FileArray=new Object[3];
     FileCount=0;
     }
     public static void install(byte bArray[], short bOffset, byte bLength)
        throws ISOException {
      new writeApplet().register();
     }
     public void process(APDU arg0) throws ISOException {
    //….   
     }

     private void ReadBinaryData(APDU apdu)
      {

      //How to write Get_Offset_Read();
       short offset = Get_Offset_Read();

        byte[] buf= apdu.getBuffer();

       // p1value is certificate index in FileArray 
       byte p1value=buf[ISO7816.OFFSET_P1];

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

        short le = apdu.setOutgoing();

        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 void WriteBinaryData(APDU apdu)
    {
       if(FileCount==3)
       {
         ISOException.throwIt(SW_END_OF_ThreeFILES);
       }
       byte[] buf = apdu.getBuffer();
       short offset =Get_Offset_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);                                          
      FileCount++;
    }
}

Upvotes: 0

Views: 573

Answers (1)

Maarten Bodewes
Maarten Bodewes

Reputation: 94078

The idea to store persistent arrays in an object array is certainly not wrong. So the rest of this answer will focus on the offset issue.


Generally file operations are is implemented by following the ISO 7816-4 specifications.

In that case you've got a minimum of three - not two - methods to create:

  • CREATE FILE - creates a file (of a certain size)
  • UPDATE BINARY - writes (part of) a file using the offset in P1/P2
  • READ BINARY - reads (part of) a file using the offset in P1/P2

The offset indicated is of course the offset in the file rather than the offset in the buffer.

I'll skip the UPDATE BINARY and READ BINARY with Odd INS as they are generally only needed for files over 32KiB.


If you want to keep to your current design then you could store the offset in the command data (CDATA) section. Alternatively you could write the file in blocks of a certain size and indicate the block number in P2.

In my opinion an addition CREATE FILE command should be preferred. Currently the methods for reading and creation/writing of the file are not symmetric.

Upvotes: 1

Related Questions