Search in sources :

Example 1 with CardChannel

use of javax.smartcardio.CardChannel in project jdk8u_jdk by JetBrains.

the class TestChannel method main.

public static void main(String[] args) throws Exception {
    CardTerminal terminal = getTerminal(args);
    if (terminal == null) {
        System.out.println("Skipping the test: " + "no card terminals available");
        return;
    }
    // establish a connection with the card
    Card card = terminal.connect("T=0");
    System.out.println("card: " + card);
    CardChannel basicChannel = card.getBasicChannel();
    try {
        basicChannel.transmit(new CommandAPDU(openChannel));
    } catch (IllegalArgumentException e) {
        System.out.println("OK: " + e);
    }
    try {
        basicChannel.transmit(new CommandAPDU(closeChannel));
    } catch (IllegalArgumentException e) {
        System.out.println("OK: " + e);
    }
    byte[] atr = card.getATR().getBytes();
    System.out.println("atr: " + toString(atr));
    // semi-accurate test to see if the card appears to support logical channels
    boolean supportsChannels = false;
    for (int i = 0; i < atr.length; i++) {
        if (atr[i] == 0x73) {
            supportsChannels = true;
            break;
        }
    }
    if (supportsChannels == false) {
        System.out.println("Card does not support logical channels, skipping...");
    } else {
        CardChannel channel = card.openLogicalChannel();
        System.out.println("channel: " + channel);
        /*
        // XXX bug in Oberthur card??
        System.out.println("Transmitting...");
        transmitTestCommand(channel);
        System.out.println("OK");
/**/
        channel.close();
    }
    // disconnect
    card.disconnect(true);
    System.out.println("OK.");
}
Also used : CardChannel(javax.smartcardio.CardChannel) CardTerminal(javax.smartcardio.CardTerminal) CommandAPDU(javax.smartcardio.CommandAPDU) Card(javax.smartcardio.Card)

Example 2 with CardChannel

use of javax.smartcardio.CardChannel in project jdk8u_jdk by JetBrains.

the class TestTransmit method main.

public static void main(String[] args) throws Exception {
    CardTerminal terminal = getTerminal(args);
    if (terminal == null) {
        System.out.println("Skipping the test: " + "no card terminals available");
        return;
    }
    Card card = terminal.connect("T=0");
    CardChannel channel = card.getBasicChannel();
    BufferedReader reader = new BufferedReader(new FileReader("apdu.log"));
    byte[] command = null;
    while (true) {
        String line = reader.readLine();
        if (line == null) {
            break;
        }
        if (line.startsWith(CMD_MARKER)) {
            System.out.println(line);
            line = line.substring(CMD_MARKER.length());
            command = parse(line);
        } else if (line.startsWith(RES_MARKER)) {
            System.out.println(line);
            line = line.substring(RES_MARKER.length());
            Bytes response = parseWildcard(line);
            CommandAPDU capdu = new CommandAPDU(command);
            ResponseAPDU rapdu = channel.transmit(capdu);
            byte[] received = rapdu.getBytes();
            if (received.length != response.bytes.length) {
                throw new Exception("Length mismatch: " + toString(received));
            }
            for (int i = 0; i < received.length; i++) {
                byte mask = response.mask[i];
                if ((received[i] & response.mask[i]) != response.bytes[i]) {
                    throw new Exception("Mismatch: " + toString(received));
                }
            }
        }
    // else ignore
    }
    // disconnect
    card.disconnect(true);
    System.out.println("OK.");
}
Also used : CardChannel(javax.smartcardio.CardChannel) BufferedReader(java.io.BufferedReader) FileReader(java.io.FileReader) ResponseAPDU(javax.smartcardio.ResponseAPDU) CardTerminal(javax.smartcardio.CardTerminal) CommandAPDU(javax.smartcardio.CommandAPDU) IOException(java.io.IOException) Card(javax.smartcardio.Card)

Example 3 with CardChannel

use of javax.smartcardio.CardChannel in project jdk8u_jdk by JetBrains.

the class TestExclusive method main.

public static void main(String[] args) throws Exception {
    CardTerminal terminal = getTerminal(args);
    if (terminal == null) {
        System.out.println("Skipping the test: " + "no card terminals available");
        return;
    }
    // establish a connection with the card
    Card card = terminal.connect("T=0");
    System.out.println("card: " + card);
    Thread thread = new Thread(new OtherThread(card));
    thread.setDaemon(true);
    thread.start();
    card.beginExclusive();
    exclusive = true;
    Thread.sleep(1000);
    System.out.println("=1=resuming...");
    CardChannel channel = card.getBasicChannel();
    System.out.println("=1=Transmitting...");
    transmitTestCommand(channel);
    System.out.println("=1=OK");
    try {
        card.beginExclusive();
    } catch (CardException e) {
        System.out.println("=1=OK: " + e);
    }
    card.endExclusive();
    try {
        card.endExclusive();
    } catch (IllegalStateException e) {
        System.out.println("=1=OK: " + e);
    }
    exclusive = false;
    Thread.sleep(1000);
    // disconnect
    card.disconnect(true);
    if (!otherOK) {
        throw new Exception("Secondary thread failed");
    }
    System.out.println("=1=OK.");
}
Also used : CardChannel(javax.smartcardio.CardChannel) CardTerminal(javax.smartcardio.CardTerminal) CardException(javax.smartcardio.CardException) CardException(javax.smartcardio.CardException) Card(javax.smartcardio.Card)

Example 4 with CardChannel

use of javax.smartcardio.CardChannel in project jdk8u_jdk by JetBrains.

the class TestConnect method transmit.

private static void transmit(Card card) throws Exception {
    CardChannel channel = card.getBasicChannel();
    System.out.println("Transmitting...");
    transmitTestCommand(channel);
}
Also used : CardChannel(javax.smartcardio.CardChannel)

Example 5 with CardChannel

use of javax.smartcardio.CardChannel in project jdk8u_jdk by JetBrains.

the class TestConnectAgain method main.

public static void main(String[] args) throws Exception {
    CardTerminal terminal = getTerminal(args);
    if (terminal == null) {
        System.out.println("Skipping the test: " + "no card terminals available");
        return;
    }
    Card card = terminal.connect("T=0");
    CardChannel channel = card.getBasicChannel();
    transmitTestCommand(channel);
    Card card2 = terminal.connect("*");
    if (card != card2) {
        throw new Exception("Different card object");
    }
    card2 = terminal.connect("T=0");
    if (card != card2) {
        throw new Exception("Different card object");
    }
    System.out.println("Remove card!");
    terminal.waitForCardAbsent(0);
    try {
        transmitTestCommand(channel);
        throw new Exception();
    } catch (CardException e) {
        System.out.println("OK: " + e);
    }
    System.out.println("Insert card!");
    terminal.waitForCardPresent(0);
    try {
        transmitTestCommand(channel);
        throw new Exception();
    } catch (IllegalStateException e) {
        System.out.println("OK: " + e);
    }
    card = terminal.connect("*");
    if (card == card2) {
        throw new Exception("Old card object");
    }
    try {
        transmitTestCommand(channel);
        throw new Exception();
    } catch (IllegalStateException e) {
        System.out.println("OK: " + e);
    }
    channel = card.getBasicChannel();
    transmitTestCommand(channel);
    card2 = terminal.connect("*");
    if (card != card2) {
        throw new Exception("Different card object");
    }
    // disconnect
    card.disconnect(true);
    System.out.println("OK.");
}
Also used : CardChannel(javax.smartcardio.CardChannel) CardTerminal(javax.smartcardio.CardTerminal) CardException(javax.smartcardio.CardException) CardException(javax.smartcardio.CardException) Card(javax.smartcardio.Card)

Aggregations

CardChannel (javax.smartcardio.CardChannel)5 Card (javax.smartcardio.Card)4 CardTerminal (javax.smartcardio.CardTerminal)4 CardException (javax.smartcardio.CardException)2 CommandAPDU (javax.smartcardio.CommandAPDU)2 BufferedReader (java.io.BufferedReader)1 FileReader (java.io.FileReader)1 IOException (java.io.IOException)1 ResponseAPDU (javax.smartcardio.ResponseAPDU)1