Page 1 of 1

Finding a constant that can be replaced with ISO7816.SW_PIN_REQUIRED to make my javacard work fine

Posted: Mon Sep 14, 2015 2:58 am
by user143842157722377
Hello everyone.
I'm trying to implement wallet applet using JCIDE. But now i am experiencing a problem.
Maybe this is just a stupid mistake.
MY CODE:

Code: Select all

package walletapp;

import javacard.framework.*;

public class wallet extends Applet
{
     /* constants declaration */
    final static byte CLA =(byte)0x80;
    final static byte Deposit = (byte) 0x10;
    final static byte Debit = (byte) 0x20;
    final static byte Balance = (byte) 0x30;
    final static byte Validate = (byte) 0x40;
    // maximum number of incorrect tries before the
    // PIN is blocked
    final static byte PinTryLimit =(byte)0x03;
    // maximum size PIN
    final static byte MaxPinSize =(byte)0x04;
    final static short SW_NEGATIVE_BALANCE = (short)0x6910;
    /* instance variables declaration */
    OwnerPIN pin;
    byte balance;
    byte buffer[];
    private wallet()
    {
        //constructor
        pin = new OwnerPIN(PinTryLimit, MaxPinSize);
        balance = 0;
        register();
    } // end of the constructor

    public static void install(APDU apdu)
    {
        // create a wallet app instance
        new wallet();
    }

    public boolean select()
    {
        pin.reset();
        return true;
    }

    public void process(APDU apdu)
    {
        // transfer incoming and outgoing APDU commands between card and smart card reader
        buffer = apdu.getBuffer();

        if (buffer[ISO7816.OFFSET_CLA] != CLA)
        {
            ISOException.throwIt(ISO7816.SW_CLA_NOT_SUPPORTED);
        }

        switch (buffer[ISO7816.OFFSET_INS])
        {
          case Balance:   
             getBalance(apdu);
             return;
          case Debit:   
             debit(apdu);
             return;
          case Deposit:   
             deposit(apdu);
             return;
          case Validate:   
             validate(apdu);
             return;
          default:   
             ISOException.throwIt(ISO7816.SW_INS_NOT_SUPPORTED);
        }
    }   

    private void deposit(APDU apdu)
    {
        // access authentication
        if ( ! pin.isValidated() )
          ISOException.throwIt (ISO7816.SW_PIN_REQUIRED);

        byte numBytes = (byte) (buffer[ISO7816.OFFSET_LC]);
        byte byteRead =(byte)(apdu.setIncomingAndReceive());
        if (byteRead != 1)
          ISOException.throwIt(ISO7816.SW_WRONG_LENGTH);
        balance = (byte)(balance + buffer[ISO7816.OFFSET_CDATA]);
        return;
        }

    private void debit(APDU apdu)
    {
        // access authentication
        if ( ! pin.isValidated() )
          ISOException.throwIt(ISO7816.SW_PIN_REQUIRED);
        byte numBytes = (byte)(buffer[ISO7816.OFFSET_LC]);
        byte byteRead =
        (byte)(apdu.setIncomingAndReceive());
        if (byteRead != 1)
          ISOException.throwIt(ISO7816.SW_WRONG_LENGTH);
        // balance can not be negative
        if ( ( balance - buffer[ISO7816.OFFSET_CDATA]) < 0 )
          ISOException.throwIt(SW_NEGATIVE_BALANCE);
        balance = (byte)
          (balance - buffer[ISO7816.OFFSET_CDATA]);
     }

    private void getBalance(APDU apdu)
    {
        // access authentication
        if ( ! pin.isValidated() )
          ISOException.throwIt(ISO7816.SW_PIN_REQUIRED);
        apdu.setOutgoing();
        apdu.setOutgoingLength((byte)1);
        buffer[0] = balance;
        apdu.sendBytes((short)0, (short)1);
     }
     
    private void validate(APDU apdu)
    {
        byte byteRead =(byte)(apdu.setIncomingAndReceive());
        pin.check(buffer, ISO7816.OFFSET_CDATA, byteRead);
    }
}


The applet runs with the error:
C:\WORKSP~1\WALLET~2\src\WALLET~1\wallet.java:76: cannot find symbol
symbol : variable SW_PIN_REQUIRED
location: interface javacard.framework.ISO7816
ISOException.throwIt (ISO7816.SW_PIN_REQUIRED);

Is there any other default constant that can be replaced with ISO7816.SW_PIN_REQUIRED? I want my applet to work fine.
Thx a lot.

Re: Finding a constant that can be replaced with ISO7816.SW_PIN_REQUIRED to make my javacard work fine

Posted: Mon Nov 02, 2015 7:59 am
by Tolice
The ISO7816.SW_PIN_REQUIRED is not accord with ISO7816. You can use the constant existed in ISO7816 standard or you can redefine it yourself.