javax.smartcardio.CardTerminal Java Examples

The following examples show how to use javax.smartcardio.CardTerminal. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: TestDirect.java    From jdk8u60 with GNU General Public License v2.0 7 votes vote down vote up
public static void main(String[] args) throws Exception {
    TerminalFactory terminalFactory = TerminalFactory.getDefault();
    List<CardTerminal> cardTerminals = terminalFactory.terminals().list();
    System.out.println("Terminals: " + cardTerminals);
    if (cardTerminals.isEmpty()) {
        throw new Exception("No card terminals available");
    }
    CardTerminal cardTerminal = cardTerminals.get(0);
    Card card = cardTerminal.connect("DIRECT");
    card.disconnect(true);

    System.out.println("OK.");
}
 
Example #2
Source File: TestDefault.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String[] args) throws Exception {
    TerminalFactory factory = TerminalFactory.getDefault();
    System.out.println("Type: " + factory.getType());
    List<CardTerminal> terminals = factory.terminals().list();
    System.out.println("Terminals: " + terminals);
    if (terminals.isEmpty()) {
        throw new Exception("no terminals");
    }

    System.out.println("OK.");
}
 
Example #3
Source File: Utils.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
static CardTerminal getTerminal(String[] args) throws Exception {
    setLibrary(args);

    TerminalFactory factory = TerminalFactory.getInstance("PC/SC", null);
    System.out.println(factory);

    List<CardTerminal> terminals = factory.terminals().list();
    System.out.println("Terminals: " + terminals);
    if (terminals.isEmpty()) {
        throw new Exception("No card terminals available");
    }
    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");
        }
    }
    return terminal;
}
 
Example #4
Source File: SmartcardTestService.java    From statelearner with Apache License 2.0 6 votes vote down vote up
public SmartcardTestService() throws Exception {
	System.setProperty("sun.security.smartcardio.t0GetResponse", "false");
	System.setProperty("sun.security.smartcardio.t1GetResponse", "false");
	
	//System.out.println("JRE version: " + System.getProperty("java.version"));
	//System.out.println("JRE vendor: " + System.getProperty("java.vendor"));
	
	//System.out.println("JRE spec version: " + System.getProperty("java.specification.version"));
	//System.out.println("JRE spec vendor: " + System.getProperty("java.specification.vendor"));
	
	// Get list of card readers
	List<CardTerminal> terminals = TerminalFactory.getDefault().terminals().list();
	
	if(terminals.size() == 0) {
		throw new Exception("No readers found.");
	}
	
	// Ask user to select a card reader to connect to
	terminal = (CardTerminal)JOptionPane.showInputDialog(null, "Reader", "Select a reader", JOptionPane.QUESTION_MESSAGE, null, terminals.toArray(), terminals.get(0));
	
	if(terminal == null) {
		throw new Exception("No reader selected.");
	}

	System.err.println("Selected reader: " + terminal.toString());
	
	// Connect to card in selected reader
	card = terminal.connect("*");
	channel = card.getBasicChannel();
	
	System.err.println("Connected to card");
}
 
Example #5
Source File: GenericContext.java    From openjavacard-tools with GNU Lesser General Public License v3.0 6 votes vote down vote up
public GenericCard findSingleCard(String prefix, AID application) {
    LOG.debug("findSingleCard()");
    GenericCard card;
    CardTerminal terminal = findSingleTerminal(prefix);
    try {
        // check card presence
        if (!terminal.isCardPresent()) {
            throw new Error("No card present in terminal");
        }
        // create the client
        card = new GenericCard(this, terminal);
    } catch (CardException e) {
        throw new Error("Error detecting card", e);
    }
    return card;
}
 
Example #6
Source File: Utils.java    From jdk8u-dev-jdk with GNU General Public License v2.0 6 votes vote down vote up
static CardTerminal getTerminal(String[] args) throws Exception {
    setLibrary(args);

    TerminalFactory factory = TerminalFactory.getInstance("PC/SC", null);
    System.out.println(factory);

    List<CardTerminal> terminals = factory.terminals().list();
    System.out.println("Terminals: " + terminals);
    if (terminals.isEmpty()) {
        throw new Exception("No card terminals available");
    }
    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");
        }
    }
    return terminal;
}
 
Example #7
Source File: TestDefault.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String[] args) throws Exception {
    TerminalFactory factory = TerminalFactory.getDefault();
    System.out.println("Type: " + factory.getType());
    List<CardTerminal> terminals = factory.terminals().list();
    if (terminals.isEmpty()) {
        System.out.println("Skipping the test: " +
                "no card terminals available");
        return;
    }
    System.out.println("Terminals: " + terminals);

    System.out.println("OK.");
}
 
Example #8
Source File: Utils.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
static CardTerminal getTerminal(String[] args) throws Exception {
    setLibrary(args);

    TerminalFactory factory = TerminalFactory.getInstance("PC/SC", null);
    System.out.println(factory);

    List<CardTerminal> terminals = factory.terminals().list();
    System.out.println("Terminals: " + terminals);
    if (terminals.isEmpty()) {
        throw new Exception("No card terminals available");
    }
    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");
        }
    }
    return terminal;
}
 
Example #9
Source File: Utils.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
static CardTerminal getTerminal(String[] args) throws Exception {
    setLibrary(args);

    TerminalFactory factory = TerminalFactory.getInstance("PC/SC", null);
    System.out.println(factory);

    List<CardTerminal> terminals = factory.terminals().list();
    System.out.println("Terminals: " + terminals);
    if (terminals.isEmpty()) {
        throw new Exception("No card terminals available");
    }
    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");
        }
    }
    return terminal;
}
 
Example #10
Source File: FancyChooser.java    From apdu4j with MIT License 6 votes vote down vote up
private FancyChooser(Terminal terminal, Screen screen, CardTerminals monitorObject, List<CardTerminal> terminals) {
    if (monitorObject != null)
        monitor = new MonitorThread(monitorObject);
    else monitor = null;
    this.terminal = terminal;
    this.screen = screen;
    gui = new MultiWindowTextGUI(screen, new DefaultWindowManager(), new EmptySpace(TextColor.ANSI.BLUE));
    this.initialList = terminals;
    // Create UI elements
    mainWindow = new BasicWindow(" apdu4j ");
    mainWindow.setCloseWindowWithEscape(true);
    mainWindow.setHints(Arrays.asList(Window.Hint.FIT_TERMINAL_WINDOW, Window.Hint.CENTERED));

    mainPanel = new Panel();
    mainPanel.setLayoutManager(new BorderLayout());
    mainPanel.setLayoutManager(new LinearLayout(Direction.VERTICAL));
    mainActions = new ActionListBox();
    mainActions.setLayoutData(BorderLayout.Location.CENTER);
    mainPanel.addComponent(mainActions);
    mainPanel.addComponent(new EmptySpace(new TerminalSize(0, 1)));
    quitButton = new Button("Cancel and quit", () -> mainWindow.close());
    quitButton.setLayoutData(LinearLayout.createLayoutData(LinearLayout.Alignment.End));
    mainPanel.addComponent(quitButton);
    //mainPanel.setLayoutData(LinearLayout.createLayoutData(LinearLayout.Alignment.End));
    mainWindow.setComponent(mainPanel);
}
 
Example #11
Source File: Main.java    From openjavacard-ndef with GNU General Public License v3.0 5 votes vote down vote up
public static final void main(String[] arguments) {
    PrintStream os = System.out;
    TerminalFactory tf = TerminalFactory.getDefault();
    CardTerminals terminals = tf.terminals();

    try {
        // default command is info
        String command = "info";

        // check if user specified a reader as first argument
        if(arguments.length >= 1) {
            // if yes then use that reader
            String terminalName = arguments[0];
            CardTerminal terminal = terminals.getTerminal(terminalName);
            if(terminal == null) {
                throw new RuntimeException("Could not find terminal \"" + terminalName + "\"");
            }
            // check if the user also specified a command
            if(arguments.length >= 2) {
                command = arguments[1];
            }
            // split off the command arguments
            String[] commandArguments = new String[0];
            if(arguments.length > 2) {
                commandArguments = Arrays.copyOfRange(arguments, 2, arguments.length);
            }
            // run the command
            runCommand(terminal, command, commandArguments);
        } else {
            // else list all readers
            os.println("Available terminals:");
            for (CardTerminal t : terminals.list()) {
                os.println("  \"" + t.getName() + "\"");
            }
        }
    } catch (CardException e) {
        e.printStackTrace();
    }
}
 
Example #12
Source File: TestDefault.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {
    TerminalFactory factory = TerminalFactory.getDefault();
    System.out.println("Type: " + factory.getType());
    List<CardTerminal> terminals = factory.terminals().list();
    System.out.println("Terminals: " + terminals);
    if (terminals.isEmpty()) {
        throw new Exception("no terminals");
    }

    System.out.println("OK.");
}
 
Example #13
Source File: TestPresent.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {
    TerminalFactory factory = TerminalFactory.getInstance("PC/SC", null);
    System.out.println(factory);

    List<CardTerminal> terminals = factory.terminals().list();
    System.out.println("Terminals: " + terminals);
    if (terminals.isEmpty()) {
        throw new Exception("No card terminals available");
    }
    CardTerminal terminal = terminals.get(0);

    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.");
}
 
Example #14
Source File: TestDirect.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {
    TerminalFactory terminalFactory = TerminalFactory.getDefault();
    List<CardTerminal> cardTerminals = terminalFactory.terminals().list();
    System.out.println("Terminals: " + cardTerminals);
    if (cardTerminals.isEmpty()) {
        throw new Exception("No card terminals available");
    }
    CardTerminal cardTerminal = cardTerminals.get(0);
    Card card = cardTerminal.connect("DIRECT");
    card.disconnect(true);

    System.out.println("OK.");
}
 
Example #15
Source File: Utils.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
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;
    }
}
 
Example #16
Source File: PCSCProvider.java    From apdu4j with MIT License 5 votes vote down vote up
@Override
public Optional<BIBO> getBIBO(String spec) {
    try {
        String protocol = System.getProperty(CardTerminalApp.PROTOCOL_PROPERTY, "*");
        Optional<CardTerminal> terminal = TerminalManager.getInstance(TerminalManager.getTerminalFactory().terminals()).dwim(spec, null, null);
        if (terminal.isPresent()) {
            Card card = terminal.get().connect(protocol);
            return Optional.ofNullable(CardBIBO.wrap(card));
        }
    } catch (CardException e) {
        logger.error("Could not get a CardBIBO for {}: {}", spec, e.getMessage(), e);
    }
    return Optional.empty();
}
 
Example #17
Source File: FancyChooser.java    From apdu4j with MIT License 5 votes vote down vote up
@Override
public Optional<CardTerminal> call() {
    try {
        setSelection(initialList);

        // Start monitor thread
        if (monitor != null)
            monitor.start();

        screen.startScreen();
        gui.addWindow(mainWindow);
        gui.waitForWindowToClose(mainWindow);

        terminal.clearScreen();
        screen.stopScreen();
        terminal.close();

        if (monitor != null) {
            monitor.interrupt();
        }
        // on OSX at least this prevents the print from last column
        System.out.println();
        return Optional.ofNullable(chosenOne);
    } catch (IOException e) {
        logger.error("Could not run: " + e.getMessage());
    }
    return Optional.empty();
}
 
Example #18
Source File: Utils.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
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;
    }
}
 
Example #19
Source File: TestDirect.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {
    TerminalFactory terminalFactory = TerminalFactory.getDefault();
    List<CardTerminal> cardTerminals = terminalFactory.terminals().list();
    if (cardTerminals.isEmpty()) {
        System.out.println("Skipping the test: " +
                "no card terminals available");
        return;
    }
    System.out.println("Terminals: " + cardTerminals);
    CardTerminal cardTerminal = cardTerminals.get(0);
    Card card = cardTerminal.connect("DIRECT");
    card.disconnect(true);

    System.out.println("OK.");
}
 
Example #20
Source File: TestDefault.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {
    TerminalFactory factory = TerminalFactory.getDefault();
    System.out.println("Type: " + factory.getType());
    List<CardTerminal> terminals = factory.terminals().list();
    if (terminals.isEmpty()) {
        System.out.println("Skipping the test: " +
                "no card terminals available");
        return;
    }
    System.out.println("Terminals: " + terminals);

    System.out.println("OK.");
}
 
Example #21
Source File: TestDefault.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {
    TerminalFactory factory = TerminalFactory.getDefault();
    System.out.println("Type: " + factory.getType());
    List<CardTerminal> terminals = factory.terminals().list();
    if (terminals.isEmpty()) {
        System.out.println("Skipping the test: " +
                "no card terminals available");
        return;
    }
    System.out.println("Terminals: " + terminals);

    System.out.println("OK.");
}
 
Example #22
Source File: TestDirect.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {
    TerminalFactory terminalFactory = TerminalFactory.getDefault();
    List<CardTerminal> cardTerminals = terminalFactory.terminals().list();
    if (cardTerminals.isEmpty()) {
        System.out.println("Skipping the test: " +
                "no card terminals available");
        return;
    }
    System.out.println("Terminals: " + cardTerminals);
    CardTerminal cardTerminal = cardTerminals.get(0);
    Card card = cardTerminal.connect("DIRECT");
    card.disconnect(true);

    System.out.println("OK.");
}
 
Example #23
Source File: FancyChooser.java    From apdu4j with MIT License 5 votes vote down vote up
@Override
public void run() {
    while (!isInterrupted()) {
        try {
            // To be able to interrupt this thread, we poll often
            boolean changed = terms.waitForChange(1000);
            List<CardTerminal> l = terms.list();
            if (changed) {
                setSelection(l);
            }
        } catch (CardException e) {
            logger.error("Failed: " + e.getMessage());
        }
    }
}
 
Example #24
Source File: TestDefault.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {
    TerminalFactory factory = TerminalFactory.getDefault();
    System.out.println("Type: " + factory.getType());
    List<CardTerminal> terminals = factory.terminals().list();
    if (terminals.isEmpty()) {
        System.out.println("Skipping the test: " +
                "no card terminals available");
        return;
    }
    System.out.println("Terminals: " + terminals);

    System.out.println("OK.");
}
 
Example #25
Source File: TestDirect.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {
    TerminalFactory terminalFactory = TerminalFactory.getDefault();
    List<CardTerminal> cardTerminals = terminalFactory.terminals().list();
    if (cardTerminals.isEmpty()) {
        System.out.println("Skipping the test: " +
                "no card terminals available");
        return;
    }
    System.out.println("Terminals: " + cardTerminals);
    CardTerminal cardTerminal = cardTerminals.get(0);
    Card card = cardTerminal.connect("DIRECT");
    card.disconnect(true);

    System.out.println("OK.");
}
 
Example #26
Source File: SignUtils.java    From Websocket-Smart-Card-Signer with GNU Affero General Public License v3.0 5 votes vote down vote up
public static ArrayList<String> getConnectedCardATR(){
    ArrayList<String> ret = new ArrayList<String>();
    try{
        List<CardTerminal> terminalList = TerminalFactory.getDefault().terminals().list();
        
        for(CardTerminal terminal:terminalList)
            if(terminal.isCardPresent()){
                javax.smartcardio.Card card = terminal.connect("*");
                ret.add(StringUtils.toHexString(card.getATR().getBytes()));
                card.disconnect(false);
            }
    }catch(Exception ex){}
    return ret;
}
 
Example #27
Source File: CardManager.java    From JCMathLib with MIT License 5 votes vote down vote up
private Card waitForCard(CardTerminals terminals)
        throws CardException {
    while (true) {
        for (CardTerminal ct : terminals
                .list(CardTerminals.State.CARD_INSERTION)) {

            return ct.connect("*");
        }
        terminals.waitForChange();
    }
}
 
Example #28
Source File: Utils.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
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;
    }
}
 
Example #29
Source File: TestDefault.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {
    TerminalFactory factory = TerminalFactory.getDefault();
    System.out.println("Type: " + factory.getType());
    List<CardTerminal> terminals = factory.terminals().list();
    if (terminals.isEmpty()) {
        System.out.println("Skipping the test: " +
                "no card terminals available");
        return;
    }
    System.out.println("Terminals: " + terminals);

    System.out.println("OK.");
}
 
Example #30
Source File: TestDirect.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {
    TerminalFactory terminalFactory = TerminalFactory.getDefault();
    List<CardTerminal> cardTerminals = terminalFactory.terminals().list();
    if (cardTerminals.isEmpty()) {
        System.out.println("Skipping the test: " +
                "no card terminals available");
        return;
    }
    System.out.println("Terminals: " + cardTerminals);
    CardTerminal cardTerminal = cardTerminals.get(0);
    Card card = cardTerminal.connect("DIRECT");
    card.disconnect(true);

    System.out.println("OK.");
}