Can't select AID Card Manager when testing to send APDU

4.3k views Asked by At

I am trying to load an applet into a smart card, before I wanted just to make a little test with the following code :

import javax.smartcardio.*;
import java.util.*;

public class TestSmartCardIO {

    public static String toString(byte[] bytes) {
        StringBuffer sbTmp = new StringBuffer();
        for(byte b : bytes){
            sbTmp.append(String.format("%X", b));
        }
        return sbTmp.toString();
    }

    public static void main(String[] args) {
        try {
            TerminalFactory factory = TerminalFactory.getDefault();
            List terminals = factory.terminals().list();
            System.out.println("Terminals count: " + terminals.size());
            System.out.println("Terminals: " + terminals);

            // Get the first terminal in the list
            CardTerminal terminal = (CardTerminal) terminals.get(0);

            // Establish a connection with the card using
            // "T=0", "T=1", "T=CL" or "*"
            Card card = terminal.connect("*");
            System.out.println("Card: " + card);

            // Get ATR
            byte[] baATR = card.getATR().getBytes();
            System.out.println("ATR: " + TestSmartCardIO.toString(baATR) );

            CardChannel channel = card.getBasicChannel();

            /*SELECT Command
             See GlobalPlatform Card Specification (e.g. 2.2, section 11.9)
             CLA: 00
             INS: A4
             P1: 04 i.e. b3 is set to 1, means select by name
             P2: 00 i.e. first or only occurence
             Lc: 08 i.e. length of AID see below
             Data: A0 00 00 00 03 00 00 00
             AID of the card manager,
             in the future should change to A0 00 00 01 51 00 00*/

             byte[] baCommandAPDU = {(byte) 0x00, (byte) 0xA4, (byte) 0x04, (byte) 0x00, (byte) 0x08, (byte) 0xA0, (byte) 0x00, 
                                    (byte) 0x00, (byte) 0x00, (byte) 0x03, (byte) 0x00, (byte) 0x00, (byte) 0x00};
            System.out.println("APDU >>>: " + TestSmartCardIO.toString(baCommandAPDU));

            ResponseAPDU r = channel.transmit(new CommandAPDU(baCommandAPDU));
            System.out.println("APDU <<<: " + TestSmartCardIO.toString(r.getBytes()));

            // Disconnect
            // true: reset the card after disconnecting card.

            card.disconnect(true);
        } catch(Exception ex)  {
            ex.printStackTrace();
        }
    }
} 

So I just wanted to test if the card is recognized and if I can send APDUs properly. I am trying to select the AID Card Manager by the APDU but I get :

Terminals count: 1
Terminals: [PC/SC terminal OT MicroSD smartcard Reader 1]
Card: PC/SC card in OT MicroSD smartcard Reader 1, protocol T=1, state OK
ATR: 3BDB96081B1FE451F83031C0641A181019005D
APDU >>>: 0A4408A00003000
APDU <<<: 6A82

And SW1 = 6A and SW2 = 82 means that the card does not find the AID Card Manager... Is it normal ? I don't really understand, I wonder if it is linked with the fact that the card uses the protocol T = 1 ? Thank you very much for your help

2

There are 2 answers

0
Maarten Bodewes On BEST ANSWER

Previously Global Platform borrowed the RID (the first 5 bytes of the AID) from VISA. This is because of historical reasons. Global Platform is now a separate entity but Open Platform - as it was once called - was started by (at least) VISA. There are many RID's registered.

However from what I understood, VISA did not want to use Global Platform to use their RID anymore. So a new RID was requested. Instead of the rather low A000000003 RID, Global Platform now used their own: A000000151 RID. Another difference is that the last bytes (which can be anything, up to 15 - 5 = 10 bytes, specified by the organization) now consists of two bytes instead of three. Some OS versions actually get that wrong and still use three 00 bytes.

So you previously had A000000003 000000 for Open Platform and earlier GP implementations and for later cards or Global Platform specifications you have A000000151 0000 to select the card manager. The handling of SELECT is not completely clear from ISO/IEC 7816-4. Generally though if you provide a smaller AID (of at least 5 bytes) within SELECT by NAME then a matching application will be selected.

0
Martin Paljak On

Just a friendly reminder, that working with globalplatform/javacards "in the wild" can be a daunting task, getting things right depends on many factors. You might want to have a look at GlobalPlatformPro (formerly known as GPJ) for this.

It should hide many of the annoying technical details (like trying to figure out the ISD AID) from your daily tasks while also providing you with the source code to study things in detail, if you want to.