4 Replies Latest reply: Mar 6, 2013 11:57 AM by 995197 RSS

    Sending command apdu with a byte array as CDATA

    995197
      Hi,
      I am learning java card as part of my final year project. So far I think I can do most of the basic things but I have got stuck at one particular point.

      I know that there are different constructors for creating a command apdu object and a number of these constructors take an array of bytes as CDATA values.

      My problem is, how to access this array of data in the card side because apdu.getBuffer() returns an array of integers (bytes)? And what is actually on apdu.getBuffer()[ISO7816.OFFSET_CDATA)] location when you send command apdu object using such a constuctor?

      regards

      Edited by: 992194 on 06-Mar-2013 06:12
        • 1. Re: Sending command apdu with a byte array as CDATA
          fgrieu
          On the card side, and baring the use of the<tt> ExtendedLength </tt>gear,<tt> apdu.getBuffer()[ISO7816.OFFSET_CDATA] </tt>is the first byte of the command data.
          The next byte is <tt> apdu.getBuffer()[ISO7816.OFFSET_CDATA+1] </tt>, and so on.

          For efficiency, it is customary to cache<tt> apdu.getBuffer() </tt>as a variable.

          See the code sample coming with<tt> javacard.framework.APDU </tt>
          http://www.win.tue.nl/pinpasjc/docs/apis/jc222/javacard/framework/APDU.html
          • 2. Re: Sending command apdu with a byte array as CDATA
            995197
            Thanx fgrieu for the prompt response. I think I should have mentioned earlier that my card use jc 2.2.1 version, and i have read from different places that this version does not support ExtendedLength facility.
            Also I understand the semantics of apdu.getBuffer()[ISO7816.OFFSET_CDATA] that is the first byte of the command data. My question is, this command data was initially supplied as an array of bytes. Something like this:

            +new CommandAPDU(CLA, INS, P1, P2, DATA_ARRAY, Le)+

            So when you call:

            byte [] buffer = apdu.getBuffer()

            So does this mean that the byte values inside DATA_ARRAY automatically occupy locations +buffer[ISO7816.OFFSET_CDATA]+ onwards inside the buffer? Or their is a mechanism of extracting the DATA_ARRAY array itself?

            regards
            • 3. Re: Sending command apdu with a byte array as CDATA
              fgrieu
              992194 wrote:
              (..) I should have mentioned earlier that my card use jc 2.2.1 version, and i have read from different places that this version does not support ExtendedLength facility.
              Indeed.
              Also I understand the semantics of apdu.getBuffer()[ISO7816.OFFSET_CDATA] that is the first byte of the command data. My question is, this command data was initially supplied as an array of bytes. Something like this:

              +new CommandAPDU(CLA, INS, P1, P2, DATA_ARRAY, Le)+

              So when you call:

              byte [] buffer = apdu.getBuffer()

              So does this mean that the byte values inside DATA_ARRAY automatically occupy locations +buffer[ISO7816.OFFSET_CDATA]+ onwards inside the buffer?
              Yes. The length would be<tt> (short)(buffer[ISO7816.OFFSET_LC]&0xFF) </tt>. Notice the<tt> &0xFF </tt> is a must above 127 bytes.
              Or their is a mechanism of extracting the DATA_ARRAY array itself?
              No.

              In fact, in the interest of performance and portability in environments with little memory, the usual coding style is to pass<tt> buffer </tt>, an offset within that, and the length; rather than making an object, which would require a copy. Welcome to the real world of Java Card.
              • 4. Re: Sending command apdu with a byte array as CDATA
                995197
                Now I understand, thanx fgrieu.

                regards