Search in sources :

Example 1 with CardTerminals

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

the class TestMultiplePresent method main.

public static void main(String[] args) throws Exception {
    Utils.setLibrary(args);
    TerminalFactory factory = Utils.getTerminalFactory(null);
    if (factory == null) {
        System.out.println("Skipping the test: " + "no card terminals available");
        return;
    }
    System.out.println(factory);
    CardTerminals terminals = factory.terminals();
    List<CardTerminal> list = terminals.list();
    System.out.println("Terminals: " + list);
    boolean multipleReaders = true;
    if (list.size() < 2) {
        if (list.isEmpty()) {
            System.out.println("Skipping the test: " + "no card terminals available");
            return;
        }
        System.out.println("Only one reader present, using simplified test");
        multipleReaders = false;
    }
    while (true) {
        boolean present = false;
        for (CardTerminal terminal : list) {
            present |= terminal.isCardPresent();
        }
        if (present == false) {
            break;
        }
        System.out.println("*** Remove all cards!");
        Thread.sleep(1000);
    }
    System.out.println("OK");
    List<CardTerminal> result;
    result = terminals.list(CARD_PRESENT);
    if (result.size() != 0) {
        throw new Exception("List not empty: " + result);
    }
    if (terminals.waitForChange(1000)) {
        throw new Exception("no timeout");
    }
    if (terminals.waitForChange(1000)) {
        throw new Exception("no timeout");
    }
    result = terminals.list(CARD_PRESENT);
    if (result.size() != 0) {
        throw new Exception("List not empty: " + result);
    }
    System.out.println("*** Insert card!");
    terminals.waitForChange();
    result = terminals.list(CARD_INSERTION);
    System.out.println(result);
    if (result.size() != 1) {
        throw new Exception("no card present");
    }
    CardTerminal t1 = result.get(0);
    result = terminals.list(CARD_INSERTION);
    System.out.println(result);
    if ((result.size() != 1) || (result.get(0) != t1)) {
        throw new Exception("no card present");
    }
    if (terminals.list(CARD_REMOVAL).size() != 0) {
        throw new Exception("List not empty: " + result);
    }
    if (terminals.list(CARD_REMOVAL).size() != 0) {
        throw new Exception("List not empty: " + result);
    }
    if (terminals.waitForChange(1000)) {
        throw new Exception("no timeout");
    }
    if (terminals.list(CARD_REMOVAL).size() != 0) {
        throw new Exception("List not empty: " + result);
    }
    if (multipleReaders) {
        System.out.println("*** Insert card into other reader!");
        terminals.waitForChange();
        result = terminals.list(CARD_INSERTION);
        System.out.println(result);
        if (result.size() != 1) {
            throw new Exception("no card present");
        }
        CardTerminal t2 = result.get(0);
        if (t1.getName().equals(t2.getName())) {
            throw new Exception("same terminal");
        }
        System.out.println("*** Remove card from 2nd reader!");
        terminals.waitForChange();
        if (terminals.list(CARD_INSERTION).size() != 0) {
            throw new Exception("List not empty: " + result);
        }
        result = terminals.list(CARD_REMOVAL);
        if ((result.size() != 1) || (result.get(0) != t2)) {
            throw new Exception("no or wrong terminal");
        }
    }
    System.out.println("*** Remove card from 1st reader!");
    terminals.waitForChange();
    if (terminals.list(CARD_INSERTION).size() != 0) {
        throw new Exception("List not empty: " + result);
    }
    result = terminals.list(CARD_REMOVAL);
    if ((result.size() != 1) || (result.get(0) != t1)) {
        throw new Exception("no or wrong terminal");
    }
    System.out.println("OK.");
}
Also used : TerminalFactory(javax.smartcardio.TerminalFactory) CardTerminals(javax.smartcardio.CardTerminals) CardTerminal(javax.smartcardio.CardTerminal)

Example 2 with CardTerminals

use of javax.smartcardio.CardTerminals in project open-ecard by ecsec.

the class PCSCTest method connect.

private void connect() {
    try {
        // File libPcscLite = new File("/usr/lib/libpcsclite.so.1");
        // if (libPcscLite.exists()) {
        // System.setProperty("sun.security.smartcardio.library", libPcscLite.getAbsolutePath());
        // }
        TerminalFactory t = TerminalFactory.getInstance("PC/SCs", null);
        // TerminalFactory t = TerminalFactory.getDefault();
        CardTerminals c = t.terminals();
        logger.info("Card terminals: {}", c.list().size());
        List terminals = c.list();
        if (terminals.isEmpty()) {
            logger.info("No presend cards!");
        } else {
            for (int i = 0; i < terminals.size(); i++) {
                CardTerminal ct = (CardTerminal) terminals.get(i);
                if (ct.isCardPresent()) {
                    Card card = ct.connect("*");
                    connection = card.getBasicChannel();
                    logger.info("Card found at card terminal " + i + ": ", card.toString());
                }
            }
        }
    } catch (Exception ex) {
        logger.error(ex.getMessage(), ex);
    }
}
Also used : TerminalFactory(javax.smartcardio.TerminalFactory) CardTerminals(javax.smartcardio.CardTerminals) List(java.util.List) CardTerminal(javax.smartcardio.CardTerminal) CardException(javax.smartcardio.CardException) Card(javax.smartcardio.Card)

Example 3 with CardTerminals

use of javax.smartcardio.CardTerminals in project open-ecard by ecsec.

the class PCSCFactory method reloadPCSC.

void reloadPCSC() {
    try {
        // code taken from http://stackoverflow.com/questions/16921785/
        Class pcscterminal = Class.forName("sun.security.smartcardio.PCSCTerminals");
        Field contextId = pcscterminal.getDeclaredField("contextId");
        contextId.setAccessible(true);
        if (contextId.getLong(pcscterminal) != 0L) {
            // First get a new context value
            Class pcsc = Class.forName("sun.security.smartcardio.PCSC");
            Method SCardEstablishContext = pcsc.getDeclaredMethod("SCardEstablishContext", Integer.TYPE);
            SCardEstablishContext.setAccessible(true);
            Field SCARD_SCOPE_USER = pcsc.getDeclaredField("SCARD_SCOPE_USER");
            SCARD_SCOPE_USER.setAccessible(true);
            long newId = ((Long) SCardEstablishContext.invoke(pcsc, SCARD_SCOPE_USER.getInt(pcsc)));
            contextId.setLong(pcscterminal, newId);
            // Then clear the terminals in cache
            loadPCSC();
            CardTerminals terminals = terminalFactory.terminals();
            Field fieldTerminals = pcscterminal.getDeclaredField("terminals");
            fieldTerminals.setAccessible(true);
            Class classMap = Class.forName("java.util.Map");
            Method clearMap = classMap.getDeclaredMethod("clear");
            clearMap.invoke(fieldTerminals.get(terminals));
        }
    } catch (NoSuchAlgorithmException ex) {
        // if it worked once it will work again
        String msg = "PCSC changed it's algorithm. There is something really wrong.";
        LOG.error(msg, ex);
        throw new RuntimeException("PCSC changed it's algorithm. There is something really wrong.");
    } catch (ClassNotFoundException | IllegalAccessException | InvocationTargetException | NoSuchFieldException | NoSuchMethodException | SecurityException ex) {
        LOG.error("Failed to perform reflection magic to reload TerminalFactory.", ex);
    }
}
Also used : CardTerminals(javax.smartcardio.CardTerminals) Method(java.lang.reflect.Method) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvocationTargetException(java.lang.reflect.InvocationTargetException) Field(java.lang.reflect.Field)

Aggregations

CardTerminals (javax.smartcardio.CardTerminals)3 CardTerminal (javax.smartcardio.CardTerminal)2 TerminalFactory (javax.smartcardio.TerminalFactory)2 Field (java.lang.reflect.Field)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Method (java.lang.reflect.Method)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1 List (java.util.List)1 Card (javax.smartcardio.Card)1 CardException (javax.smartcardio.CardException)1