2

I wanted to know how I can get the data send through an APDU command if buffer[ISO7816.OFFSET_LC] is >=3.

Actually I am using

if (numBytes == 1) {
    shortAmount = (short) buffer[ISO7816.OFFSET_CDATA];
} else if (numBytes == 2) {
    shortAmount = (short) Util.getShort(buffer, ISO7816.OFFSET_CDATA);
} else if(numBytes == 3) {
    //how to get the all data contained in the APDU?
}
Maarten Bodewes
  • 90,524
  • 13
  • 150
  • 263
knk
  • 45
  • 2
  • 9

3 Answers3

4

For most Java Card implementations there is no support for the int base type. This means you cannot store more than 16 bits in one single variable.

You can however store it in an array. And - when you think about it - it already is. The APDU buffer is nothing other than a byte array in transient memory (RAM, usually) *1.

So how you handle the APDU data in the APDU buffer is up to you:

  • you could parse structures inside the APDU buffer;
  • you could copy it to another array, either persistent (EEPROM / Flash using new byte[size]) or transient memory (RAM, through JCSystem.makeTransientByteArray())
  • you could convert it to shorts using Util.getShort() and store the result in a short array similar to a byte array;
  • you could use it as input to any API that allows input from a byte array, such as one of the Key#setValue commands, OwnerPIN#check(), signature verification etc. etc..

Or you can perform any combination of above.

If you want to perform calculations with larger values you'll have to implement those or have access to special libraries. For 32 bit integer calculations have a look at my X-mas special answer. This also shows that any kind of calculations on the data are possible, even if you have to implement them yourself. Java Card is Turing-complete, so as long as you don't run out of CPU time or memory you can do any possible calculation on it.


*1 OK, the APDU buffer is a rather special array in the way it is handled by the Java Card system, but during processing of data it just acts as a normal Java Card byte array.

Maarten Bodewes
  • 90,524
  • 13
  • 150
  • 263
1

Store the data in to a byte array.

Key point is, you need to process the data as a byte array if its size is >= 3. For processing however, you can develop your own API's or you can use the available one.

ouamso
  • 31
  • 6
0

If you want to interpret the data sent through an APDU as an integer (== 4 bytes), then you need to store the data in to a byte array. Now depending upon the use case, you can use a JCInteger class provided by "Maarten Bodewes" to further process the data.

Key point is, you need to process the data as a byte array if its size is >= 3. For processing however, you can develop your own API's or you can use the available one.

hsg
  • 656
  • 4
  • 10