3
votes

i was wondering if i could send back some data from the javacard applet when it is selected.

since select() method returns a boolean value i don't know how to return data bytes from it.

can anyone help me with this? i want the applet to return a simple byte array along with the status word 9000 (which is default for success), when i send the select command to the card.

ie, when i send the following command

00A4040006010203040506

i want a response like,

010203049000

(first four bytes are the data returned from the applet) TIA. thanks in advance..

3

3 Answers

4
votes

I guess you do the "good practice" of "if selectingApplet() then return" in process? You need to process the incoming APDU instead of simple return.

You can return data to select the normal way, but be careful to return 0x9000 if the select was successful.

0
votes

Yes it is possible to return data during applet selection.

The select() method is normally called by the platform during applet selection. You can do some logic inside this method and return true if you want your applet to be selected or false if not. After calling this method, if your applet was successfully selected, the platform will then call the APDU.process method where you can handle the Select command like any other APDU commands in your applet.

However, your command APDU should indicate an Le field if you want a response data. You can change your command APDU to 00 A4 04 00 06 01 02 03 04 05 06 00 to return all the response data available.

As for returning 9000, just make sure to exit the APDU.process method without throwing an exception or you can throw an ISOException with 9000 value. I prefer the former.

0
votes

check out the below code

if (selectingApplet())
    {
        byte[] apduBuffer = apdu.getBuffer();
        
        apduBuffer[0] = 0x01;
        apduBuffer[1] = 0x02;
        apduBuffer[2] = 0x03;
        apduBuffer[3] = 0x04;
        
        apdu.setOutgoingAndSend((short)0, (short)4);
        return;
    }

after upload and install the applet, try to select the applet, AID is 112233445566

>> /send 00A4040006112233445566
>> 00 A4 04 00 06 11 22 33 44 55 66
<< 01 02 03 04 90 00

another way to return data when select applet

private static final byte[] STACK_OVERFLOW = {'S','T','A','C','K',' ','O','V','E','R','F','L','O','W'};

if (selectingApplet())
    {
        byte[] apduBuffer = apdu.getBuffer();
        //copy array STACK_OVERFLOW to apduBuffer
        Util.arrayCopyNonAtomic(STACK_OVERFLOW,(short)0,apduBuffer,(short)0,(short)STACK_OVERFLOW.length);
        //set and set buffer with STACK_OVERFLOW array length
        apdu.setOutgoingAndSend((short)0, (short)STACK_OVERFLOW.length);
        return;
    }