Search in sources :

Example 1 with CardTerminal

use of javax.smartcardio.CardTerminal 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 CardTerminal

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

the class TestConnect method main.

public static void main(String[] args) throws Exception {
    CardTerminal terminal = getTerminal(args, "SunPCSC");
    if (terminal == null) {
        System.out.println("Skipping the test: " + "no card terminals available");
        return;
    }
    Card card = terminal.connect("*");
    System.out.println("card: " + card);
    if (card.getProtocol().equals("T=0") == false) {
        throw new Exception("Not T=0 protocol");
    }
    transmit(card);
    card.disconnect(true);
    try {
        transmit(card);
        throw new Exception("transmitted to disconnected card");
    } catch (IllegalStateException e) {
        System.out.println("OK: " + e);
    }
    try {
        card = terminal.connect("T=Foo");
        System.out.println(card);
        throw new Exception("connected via T=Foo");
    } catch (IllegalArgumentException e) {
        System.out.println("OK: " + e);
    }
    card = terminal.connect("T=0");
    System.out.println(card);
    if (card.getProtocol().equals("T=0") == false) {
        throw new Exception("Not T=0 protocol");
    }
    transmit(card);
    card.disconnect(false);
    card = terminal.connect("*");
    System.out.println("card: " + card);
    if (card.getProtocol().equals("T=0") == false) {
        throw new Exception("Not T=0 protocol");
    }
    transmit(card);
    card.disconnect(true);
    System.out.println("OK.");
}
Also used : CardTerminal(javax.smartcardio.CardTerminal) Card(javax.smartcardio.Card)

Example 3 with CardTerminal

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

the class Utils method getTerminal.

static CardTerminal getTerminal(String[] args, String provider) throws Exception {
    setLibrary(args);
    try {
        TerminalFactory factory = (provider == null) ? TerminalFactory.getInstance("PC/SC", null) : TerminalFactory.getInstance("PC/SC", null, provider);
        System.out.println(factory);
        List<CardTerminal> terminals = factory.terminals().list();
        System.out.println("Terminals: " + terminals);
        if (terminals.isEmpty()) {
            return null;
        }
        CardTerminal terminal = terminals.get(0);
        if (terminal.isCardPresent() == false) {
            System.out.println("*** Insert card");
            if (terminal.waitForCardPresent(20 * 1000) == false) {
                throw new Exception("no card available");
            }
        }
        System.out.println("card present: " + terminal.isCardPresent());
        return terminal;
    } catch (NoSuchAlgorithmException e) {
        Throwable cause = e.getCause();
        if (cause != null && cause.getMessage().startsWith("PC/SC not available")) {
            return null;
        }
        throw e;
    }
}
Also used : TerminalFactory(javax.smartcardio.TerminalFactory) CardTerminal(javax.smartcardio.CardTerminal) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) IOException(java.io.IOException)

Example 4 with CardTerminal

use of javax.smartcardio.CardTerminal 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 5 with CardTerminal

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

the class TestPresent 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;
    }
    while (terminal.isCardPresent()) {
        System.out.println("*** Remove card!");
        Thread.sleep(1000);
    }
    Timer timer = new Timer();
    System.out.println("Testing waitForCardAbsent() with card already absent...");
    isTrue(terminal.waitForCardAbsent(10));
    timer.print();
    isTrue(terminal.waitForCardAbsent(100));
    timer.print();
    isTrue(terminal.waitForCardAbsent(10000));
    timer.print();
    isTrue(terminal.waitForCardAbsent(0));
    timer.print();
    System.out.println("Testing waitForCardPresent() timeout...");
    isFalse(terminal.waitForCardPresent(10));
    timer.print();
    isFalse(terminal.waitForCardPresent(100));
    timer.print();
    isFalse(terminal.waitForCardPresent(1000));
    timer.print();
    isFalse(terminal.isCardPresent());
    isFalse(terminal.isCardPresent());
    System.out.println("*** Insert card!");
    isTrue(terminal.waitForCardPresent(0));
    timer.print();
    isTrue(terminal.isCardPresent());
    isTrue(terminal.isCardPresent());
    System.out.println("Testing waitForCardPresent() with card already present...");
    isTrue(terminal.waitForCardPresent(0));
    timer.print();
    isTrue(terminal.waitForCardPresent(10000));
    timer.print();
    isTrue(terminal.waitForCardPresent(100));
    timer.print();
    isTrue(terminal.waitForCardPresent(10));
    timer.print();
    System.out.println("Testing waitForCardAbsent() timeout...");
    isFalse(terminal.waitForCardAbsent(1000));
    timer.print();
    isFalse(terminal.waitForCardAbsent(100));
    timer.print();
    isFalse(terminal.waitForCardAbsent(10));
    timer.print();
    System.out.println("*** Remove card!");
    isTrue(terminal.waitForCardAbsent(0));
    timer.print();
    isFalse(terminal.isCardPresent());
    isFalse(terminal.isCardPresent());
    System.out.println("OK.");
}
Also used : CardTerminal(javax.smartcardio.CardTerminal)

Aggregations

CardTerminal (javax.smartcardio.CardTerminal)15 Card (javax.smartcardio.Card)8 CardException (javax.smartcardio.CardException)7 TerminalFactory (javax.smartcardio.TerminalFactory)5 CardChannel (javax.smartcardio.CardChannel)4 CardTerminals (javax.smartcardio.CardTerminals)3 ApduConnectionException (es.gob.jmulticard.apdu.connection.ApduConnectionException)2 ApduConnectionOpenedInExclusiveModeException (es.gob.jmulticard.apdu.connection.ApduConnectionOpenedInExclusiveModeException)2 CardNotPresentException (es.gob.jmulticard.apdu.connection.CardNotPresentException)2 LostChannelException (es.gob.jmulticard.apdu.connection.LostChannelException)2 NoReadersFoundException (es.gob.jmulticard.apdu.connection.NoReadersFoundException)2 IOException (java.io.IOException)2 CommandAPDU (javax.smartcardio.CommandAPDU)2 BufferedReader (java.io.BufferedReader)1 FileReader (java.io.FileReader)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 ResponseAPDU (javax.smartcardio.ResponseAPDU)1 SCIOErrorCode (org.openecard.common.ifd.scio.SCIOErrorCode)1