This discussion is archived
1 Reply Latest reply: Dec 12, 2012 11:44 PM by Umer RSS

Can't understand java card. Please help

936354 Newbie
Currently Being Moderated
I generated the following application from netbeans. However, when I either sent the script file or using the java card service( in the service pane)
The breakpoints at install and select were never hit. By the way, what is apdu.sendbytes(0,2); is it forwarding some bytes from somewhere?
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package TimeAttend;

import javacard.framework.APDU;
import javacard.framework.Applet;
import javacard.framework.ISO7816;
import javacard.framework.ISOException;
import javacard.framework.OwnerPIN;
import javacard.framework.Util;

/**
 *
 * @author Garfield
 */
public class TimeAttendApplet extends javacard.framework.Applet {

    private byte[] echoBytes;
    private static final short LENGTH_ECHO_BYTES = 256;
    OwnerPIN pin;
      // code of CLA byte in the command APDU header
    final static byte Test_CLA = (byte) 0x80;

    final static byte SAY_HI =  (byte) 0x50;
    
    // maximum number of incorrect tries before the
    // PIN is blocked
    final static byte PIN_TRY_LIMIT = (byte) 0x03;
    // maximum size PIN
    final static byte MAX_PIN_SIZE = (byte) 0x08;
    /**
     * Installs this applet.
     * 
     * @param bArray
     *            the array containing installation parameters
     * @param bOffset
     *            the starting offset in bArray
     * @param bLength
     *            the length in bytes of the parameter data in bArray
     */
    public static void install(byte[] bArray, short bOffset, byte bLength) {
        new TimeAttendApplet(bArray, bOffset, bLength);
    }

    /**
     * Only this class's install method should create the applet object.
     */
    protected TimeAttendApplet(byte[] bArray, short bOffset, byte bLength) {
         // It is good programming practice to allocate
        // all the memory that an applet needs during
        // its lifetime inside the constructor
        pin = new OwnerPIN(PIN_TRY_LIMIT, MAX_PIN_SIZE);

        byte iLen = bArray[bOffset]; // aid length
        bOffset = (short) (bOffset + iLen + 1);
        byte cLen = bArray[bOffset]; // info length
        bOffset = (short) (bOffset + cLen + 1);
        byte aLen = bArray[bOffset]; // applet data length

        // The installation parameters contain the PIN
        // initialization value
        pin.update(bArray, (short) (bOffset + 1), aLen);
        echoBytes = new byte[LENGTH_ECHO_BYTES];
        register();
         
    }
    
    /**
     *
     * @return
     */
    /*@Override*/
    public boolean select() {

        // The applet declines to be selected
        // if the pin is blocked.
        if (pin.getTriesRemaining() == 0) {
            return false;
        }

        return true;

    }// end of select method

    //<editor-fold defaultstate="collapsed" desc="comment">
    /*@Override*/
    //</editor-fold>
    public void deselect() {

        // reset the pin value
        pin.reset();

    }

    /**
     * Processes an incoming APDU.
     * 
     * @see APDU
     * @param apdu
     *            the incoming APDU
     */
    public void process(APDU apdu) {
        //Insert your code here
           byte[] buffer = apdu.getBuffer();
        // check SELECT APDU command

        if (apdu.isISOInterindustryCLA()) {
            if (buffer[ISO7816.OFFSET_INS] == (byte) (0xA4)) {
                return;
            }
            ISOException.throwIt(ISO7816.SW_CLA_NOT_SUPPORTED);
        }

        // verify the reset of commands have the
        // correct CLA byte, which specifies the
        // command structure
        if (buffer[ISO7816.OFFSET_CLA] != Test_CLA) {
            ISOException.throwIt(ISO7816.SW_CLA_NOT_SUPPORTED);
        }

        switch (buffer[ISO7816.OFFSET_INS]) {
            case SAY_HI:
                apdu.sendBytes((short) 0, (short) 2);
                // echo data
                
                break;
             default:
                ISOException.throwIt(ISO7816.SW_INS_NOT_SUPPORTED);
        }

    }
}
  • 1. Re: Can't understand java card.
    Umer Journeyer
    Currently Being Moderated
    Hi,

    You should study the API of java card. Google can help you better in this case.
    The breakpoints should be hit when you will install( execute) your applet in the simulator and method select is called when you send select applet APDU to the applet.

    For the apdu.sendbytes(0,2); read this: :http://www.win.tue.nl/pinpasjc/docs/apis/jc222/javacard/framework/APDU.html#sendBytes%28short,%20short%29

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points