0

I've encountered a problem while declaring a BigNumber datatype in my javacard applet. The applet loads properly into the simulator if i just comment the declaration. The problem to be precise is while loading the import.cap file (jcshell: Error code: 6a80 (Wrong data))

java card kit 2.2.2 using

  import javacard.framework.APDU;
  import javacard.framework.Applet;
  import javacard.framework.ISO7816;
  import javacard.framework.ISOException;
  import javacard.framework.JCSystem;
  import javacardx.framework.math.BigNumber;

 public class LargeBal extends Applet {

 // CLA byte
public static final byte BANK_CLA = (byte) 0x80;

// INS byte
public static final byte INS_GET_BALANCE = 0X02;
public static final byte INS_CREDIT = 0X04;
public static final byte INS_DEBIT = 0X06;

/**
 * SW bytes for Arithmetic exception
 */
final static short INVALID_NUMBER_FORMAT = 0x6308;

/**
 * Initial account balance
 */
final static byte[] INITIAL_ACCOUNT_BALANCE = { (byte) 0x01, (byte) 0x00,
        (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00 };

// Amount of money in user's account

private BigNumber accountBalance;

// Big number for temporary calculation
BigNumber tempBigNum;

// temporary buffer used as scratch space

byte[] scratchSpace;

private LargeBal() {

     accountBalance = new BigNumber((byte) 8);

    // initialize account balance to 100,000.00
     accountBalance.init(INITIAL_ACCOUNT_BALANCE, (byte) 0,
     (byte) INITIAL_ACCOUNT_BALANCE.length, BigNumber.FORMAT_BCD);

    // initialize the temporary big number
     tempBigNum = new BigNumber(BigNumber.getMaxBytesSupported());

    // initialize the scratchSpace
    scratchSpace = JCSystem.makeTransientByteArray((short) 10,
            JCSystem.CLEAR_ON_DESELECT);
    register();
}

public static void install(byte[] bArray, short bOffset, byte bLength) {
    // GP-compliant JavaCard applet registration
    new LargeBal();
}

public void process(APDU apdu) {
    // Good practice: Return 9000 on SELECT
    if (selectingApplet()) {
        return;
    }

    byte[] buf = apdu.getBuffer();
    switch (buf[ISO7816.OFFSET_INS]) {
    case INS_GET_BALANCE:
         getBalance(apdu, buf);
        break;
    case INS_CREDIT:

        break;
    case INS_DEBIT:

        break;
    default:
        // good practice: If you don't know the INStruction, say so:
        ISOException.throwIt(ISO7816.SW_INS_NOT_SUPPORTED);
    }
}

private void getBalance(APDU apdu, byte[] buffer) {

    if (buffer[ISO7816.OFFSET_P1] == BigNumber.FORMAT_BCD) {
        accountBalance.toBytes(buffer, (short) 0, (short) 8,
                BigNumber.FORMAT_BCD);
    } else if (buffer[ISO7816.OFFSET_P1] == BigNumber.FORMAT_HEX) {
        accountBalance.toBytes(buffer, (short) 0, (short) 8,
                BigNumber.FORMAT_HEX);
    } else
        ISOException.throwIt(INVALID_NUMBER_FORMAT);

    apdu.setOutgoingAndSend((short) 0, (short) 8);
}

}

Rakesh
  • 29
  • 6
  • Would you please add installation process output also? Are you sure your card is Java Card 2.2.2 compatible? – Ebrahim Ghasemi Mar 12 '15 at 11:40
  • @Abraham I am working on jcop simulator and it is java card 2.2.2 compatible.cap file is created. Header.cap loaded, Directory.cap loaded, loading Import.cap is failed. status: Wrong data. jcshell: Error code: 6a80 (Wrong data) jcshell: Wrong response APDU: 6A80 – Rakesh Mar 12 '15 at 11:59
  • I loaded your program on NetBeans simulator successfully! And it works fine. So, as Mr.Roland mentioned in his answer, I think it is something related to your simulator/card compatibility with the math library package. – Ebrahim Ghasemi Mar 12 '15 at 12:34
  • I think Michael's answer is the right answer. So you can check it using the "v" mark beside the question. – Ebrahim Ghasemi Mar 14 '15 at 08:15

1 Answers1

5

javacardx.framework.math is an optional package. Thus, not all cards/emulators implement this. In your case, it seems that the card does not implement javacardx.framework.math.BigNumber. Consequently it refuses to load/install the applet.

From the Runtime Environment Specification, Java Card Platform, Version 2.2.2 (section 9.7):

Optional Extension Packages

Some API packages in the Java Card technology are designated as extension packages and may be optionally supported by an implementation. But, if supported, all the classes in the package and its subpackages must be implemented by the platform and reside on the card.

The following are optional Java Card technology extension packages:

  • javacardx.apdu [...]
  • javacardx.biometry [...]
  • javacardx.crypto [...]
  • javacardx.external [...]
  • javacardx.framework [...] If implemented, this package must include all the contained subpackages - util, math, and tlv.
Michael Roland
  • 39,663
  • 10
  • 99
  • 206
  • Is `if (buffer[ISO7816.OFFSET_P1] == BigNumber.FORMAT_BCD)` a correct expression? I think in the left he has a `byte` type value, and in the right it is not, right? What is the meaning of this line? – Ebrahim Ghasemi Mar 12 '15 at 12:11
  • 2
    @Abraham Comparing a byte to a byte is a perfectly valid expression (`buffer[ISO7816.OFFSET_P1]` is of type `byte` and `FORMAT_BCD` is defined as `public static final` **`byte`** `FORMAT_BCD = 1`). – Michael Roland Mar 12 '15 at 12:27
  • @MichaelRoland Do you have any information to check which optional packages works with card/emulators. – Rakesh Mar 12 '15 at 17:42
  • No, I suggest that you request such information directly from the card/chip/OS manufacturer. – Michael Roland Mar 12 '15 at 20:55
  • 1
    @MichaelRoland Where in the JavaCard 2.2 specs are the optional packages listed? I'm trying to find documentation to confirm that javacardfx.framework.math is actually marked as optional by Oracle/Sun, and not just frequently left out. I didn't find anything in the JCVM spec, which seems to describe other optional components such as the `int` type and garbage collection. – Brian H. Feb 01 '19 at 08:16
  • Thanks! I was searching for BigNumber (and other classes) in the documentation instead of searching for the package name. – Brian H. Feb 01 '19 at 23:42