Search in sources :

Example 6 with CardTerminal

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

the class TestDirect method main.

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.");
}
Also used : TerminalFactory(javax.smartcardio.TerminalFactory) CardTerminal(javax.smartcardio.CardTerminal) Card(javax.smartcardio.Card)

Example 7 with CardTerminal

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

use of javax.smartcardio.CardTerminal in project jmulticard by ctt-gob-es.

the class SmartcardIoConnection method open.

/**
 * {@inheritDoc}
 */
@Override
public void open() throws ApduConnectionException {
    // Desactivamos las respuestas automaticas para evitar los problemas con el canal seguro
    // $NON-NLS-1$ //$NON-NLS-2$
    System.setProperty("sun.security.smartcardio.t0GetResponse", "false");
    // $NON-NLS-1$ //$NON-NLS-2$
    System.setProperty("sun.security.smartcardio.t1GetResponse", "false");
    if (isExclusiveUse() && isOpen()) {
        throw new ApduConnectionOpenedInExclusiveModeException();
    }
    final List<CardTerminal> terminales;
    try {
        terminales = TerminalFactory.getDefault().terminals().list();
    } catch (final Exception e) {
        throw new NoReadersFoundException(// $NON-NLS-1$
        "No se han podido listar los lectores del sistema: " + e, // $NON-NLS-1$
        e);
    }
    try {
        if (terminales.size() < 1) {
            throw new NoReadersFoundException();
        }
        if (this.terminalNumber == -1) {
            final long[] cadsWithCard = getTerminals(true);
            if (cadsWithCard.length > 0) {
                this.terminalNumber = (int) cadsWithCard[0];
            } else {
                throw new ApduConnectionException(// $NON-NLS-1$
                "En el sistema no hay ningun terminal con tarjeta insertada");
            }
        }
        if (terminales.size() <= this.terminalNumber) {
            throw new ApduConnectionException(// $NON-NLS-1$
            "No se detecto el lector de tarjetas numero " + Integer.toString(this.terminalNumber));
        }
        this.card = terminales.get(this.terminalNumber).connect(this.protocol.toString());
    } catch (final javax.smartcardio.CardNotPresentException e) {
        throw new CardNotPresentException(e);
    } catch (final CardException e) {
        throw new ApduConnectionException(// $NON-NLS-1$ //$NON-NLS-2$
        "No se ha podido abrir la conexion con el lector de tarjetas numero " + Integer.toString(this.terminalNumber) + ": " + e, // $NON-NLS-1$ //$NON-NLS-2$
        e);
    }
    if (this.exclusive) {
        try {
            this.card.beginExclusive();
        } catch (final CardException e) {
            throw new ApduConnectionException(// $NON-NLS-1$ //$NON-NLS-2$
            "No se ha podido abrir la conexion exclusiva con el lector de tarjetas numero " + Integer.toString(this.terminalNumber) + ": " + e, // $NON-NLS-1$ //$NON-NLS-2$
            e);
        }
    }
    this.canal = this.card.getBasicChannel();
}
Also used : CardTerminal(javax.smartcardio.CardTerminal) CardException(javax.smartcardio.CardException) ApduConnectionOpenedInExclusiveModeException(es.gob.jmulticard.apdu.connection.ApduConnectionOpenedInExclusiveModeException) CardNotPresentException(es.gob.jmulticard.apdu.connection.CardNotPresentException) CardNotPresentException(es.gob.jmulticard.apdu.connection.CardNotPresentException) ApduConnectionException(es.gob.jmulticard.apdu.connection.ApduConnectionException) CardException(javax.smartcardio.CardException) ApduConnectionOpenedInExclusiveModeException(es.gob.jmulticard.apdu.connection.ApduConnectionOpenedInExclusiveModeException) NoReadersFoundException(es.gob.jmulticard.apdu.connection.NoReadersFoundException) LostChannelException(es.gob.jmulticard.apdu.connection.LostChannelException) NoReadersFoundException(es.gob.jmulticard.apdu.connection.NoReadersFoundException) ApduConnectionException(es.gob.jmulticard.apdu.connection.ApduConnectionException)

Example 9 with CardTerminal

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

the class PCSCTerminals method list.

public List<SCIOTerminal> list(State state, boolean firstTry) throws SCIOException {
    LOG.trace("Entering list().");
    try {
        CardTerminals.State scState = convertState(state);
        // get terminals with the specified state from the SmartcardIO
        List<CardTerminal> scList = terminals.list(scState);
        ArrayList<SCIOTerminal> list = convertTerminals(scList);
        LOG.trace("Leaving list().");
        return Collections.unmodifiableList(list);
    } catch (CardException ex) {
        SCIOErrorCode code = getCode(ex);
        if (code == SCIOErrorCode.SCARD_E_NO_READERS_AVAILABLE) {
            LOG.debug("No reader available exception.");
            return Collections.emptyList();
        } else if (code == SCIOErrorCode.SCARD_E_NO_SERVICE || code == SCIOErrorCode.SCARD_E_SERVICE_STOPPED) {
            if (firstTry) {
                LOG.debug("No service available exception, reloading PCSC and trying again.");
                reloadFactory();
                return list(state, false);
            } else {
                LOG.debug("No service available exception, returning empty list.");
                return Collections.emptyList();
            }
        }
        String msg = "Failed to retrieve list from terminals instance.";
        LOG.error(msg, ex);
        throw new SCIOException(msg, code, ex);
    }
}
Also used : SCIOException(org.openecard.common.ifd.scio.SCIOException) SCIOTerminal(org.openecard.common.ifd.scio.SCIOTerminal) SCIOErrorCode(org.openecard.common.ifd.scio.SCIOErrorCode) CardTerminals(javax.smartcardio.CardTerminals) CardTerminal(javax.smartcardio.CardTerminal) CardException(javax.smartcardio.CardException)

Example 10 with CardTerminal

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

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