1

I wrote the following program to return all the APDU buffer contents on reception of each APDU command:

package testPack;

import javacard.framework.APDU;
import javacard.framework.Applet;
import javacard.framework.ISOException;

public class BufferReturner extends Applet {

    private BufferReturner() {
    }

    public static void install(byte bArray[], short bOffset, byte bLength) throws ISOException {
        new BufferReturner().register();
    }

    public void process(APDU arg0) throws ISOException {
        if(selectingApplet()){
            return;
        }
        arg0.setOutgoingAndSend((short)0, (short)255);

    }

}

When I send commands though the contact interface using T=0 protocol, I receive the following result:

Connect successful.
Send: 00 A4 04 00 06 01 02 03 04 05 00 00
Recv: 90 00
Time used: 8.000 ms
Send: 00 00 00 00 10 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 00
Send Apdu error: A device attached to the system is not functioning.
Send: 00 00 00 00 10 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 00
Recv: 6C FF
Time used: 5.000 ms
Send: 00 00 00 00 10 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 FF
Recv: 6C FF
Time used: 5.000 ms
Send: 00 00 00 00 10 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 FF
Recv: 6C FF
Time used: 5.000 ms
Send: 00 00 00 00 10 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 FF
Recv: 6C FF
Time used: 6.000 ms
Send: 00 C0 00 00 FF
Send Apdu error: A device attached to the system is not functioning.
Send: 00 C0 00 00 FF
Send Apdu error: A device attached to the system is not functioning.

As you see I can't receive the APDU buffer contents. What's wrong?

Michael Roland
  • 39,663
  • 10
  • 99
  • 206
Ebrahim Ghasemi
  • 5,850
  • 10
  • 52
  • 113
  • Did you try the Get Response APDU to receive data for T=0 ? And can you please check the return code of `apdu.setIncomingAndReceive()` – Paul Bastian Jan 28 '16 at 09:21
  • @PaulBastian Dear Paul, I modified the question.The previous was wrong.-- As you see, I tried Get Response command also, but it didn't helped. (And there is no `setIncommingAndReceive()` method in this program). – Ebrahim Ghasemi Jan 28 '16 at 13:41
  • You need `apdu.setIncomingAndReceive()` to get the `11`s into the APDU buffer. – Paul Bastian Jan 28 '16 at 13:45
  • Did you try sending a smaller number than 255? Maybe your cards buffer isnt big enough. And also try not sending a case4 command but simply with Le and no data. – Paul Bastian Jan 28 '16 at 13:47
  • @PaulBastian, When I add `apdu.setIncomingAndReceive()` everything works fine(Both in `T=0` and `T=1`). So APDU buffer length is OK. When I remove `apdu.setIncomingAndReceive()` line, In `T=1` output is correct (All is zero because I didn't received the data), but in `T=0` output is as above and it is not as I expect. – Ebrahim Ghasemi Jan 28 '16 at 13:58
  • 1
    My guess its an internal error, because the card expects `apdu.setIncomingAndReceive()`, because it knows its a case4 command. And because you don't implement it, the card probably is in a false state because it cannot call `apdu.setOutgoing` before the incoming part is finished. Your T=1 NXP JCOP card is just more relaxed on this issue and continues working – Paul Bastian Jan 28 '16 at 14:02
  • @PaulBastian This card is dual interface. Its contact interface working with `T=0` protocol and have above problem, while its contactless interface working `T=1` and doesn't have the above issue. – Ebrahim Ghasemi Jan 28 '16 at 14:08
  • Ok, debug by checking your internal state with `apdu.getCurrentState()` – Paul Bastian Jan 28 '16 at 14:13

1 Answers1

4

Just like with Transmission error for T=0 JavaCards there is nothing wrong with your card and this is kind of "expected" behavior.

For the setOutgoing() method (setOutgoingAndSend() is just a convenience method wrapping setOutgoing()), the Java Card API specification clearly states:

On a case 4 command, the setIncomingAndReceive() must be invoked prior to calling this method. Otherwise, erroneous behavior may result in T=0 protocol.

Hence, what you see is exactly that "erroneous behavior" indicated in the API specification.

Community
  • 1
  • 1
Michael Roland
  • 39,663
  • 10
  • 99
  • 206