Search code examples
javasmartcardapdu

getATR() does not reset the smart card in javax.smartcardio


I'm programming a small application for a smart card with Java using javax.smartcardio package, and I have a some problem with the ATR (Answer to reset). The card.getATR() method does not reset the card. It returns the ATR bytes, but it does not return the card to MF state.

That is my example:

TerminalFactory factory = TerminalFactory.getDefault();
List<CardTerminal> terminals = factory.terminals().list();

// get the first terminal
CardTerminal terminal = terminals.get(0);
// establish a connection with the card
Card card = terminal.connect("T=0");

ATR atr = card.getATR(); //atr.getBytes() <- correct

CardChannel channel = card.getBasicChannel();

ResponseAPDU r;
// STEP 1. select file in MF
r = channel.transmit(new CommandAPDU(new byte[]{0,(byte)0xA4,(byte)0x02,(byte)0x0C,(byte)0x02,0,(byte)0x02,0}));
// r.getBytes() == 90 00 <- correct

// STEP 2. select DF
r = channel.transmit(new CommandAPDU(new byte[]{0,(byte)0xA4,(byte)0x04,(byte)0x0C,(byte)0x02,(byte)0x05,0}));
// r.getBytes() == 90 00 <- correct

atr = card.getATR(); // answer to reset

// STEP 3. select file in MF, as in STEP 1
r = channel.transmit(new CommandAPDU(new byte[]{0,(byte)0xA4,(byte)0x02,(byte)0x0C,(byte)0x02,0,(byte)0x02,0}));
// r.getBytes() == 6A 82 <- fail, not found

card.disconnect(false);

Where is my mistake? Or how i can to reset the card?


Solution

  • Card.getATR() does not specify that it resets the card. It simply returns the ATR that has been requested from the card. You should probably use disconnect(true) instead, and then reconnect to the card.