Search in sources :

Example 1 with CardException

use of javax.smartcardio.CardException 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 2 with CardException

use of javax.smartcardio.CardException 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 3 with CardException

use of javax.smartcardio.CardException 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 4 with CardException

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

Example 5 with CardException

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

the class TestControl 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);
    byte[] data = new byte[] { 2 };
    try {
        byte[] resp = card.transmitControlCommand(IOCTL_SMARTCARD_VENDOR_IFD_EXCHANGE, data);
        System.out.println("Firmware: " + toString(resp));
        throw new Exception();
    } catch (CardException e) {
        // we currently don't know of any control commands that work with
        // our readers. call the function just to make sure we don't crash
        // or throw the wrong exception
        System.out.println("OK: " + e);
        e.printStackTrace(System.out);
    }
    try {
        card.transmitControlCommand(IOCTL_SMARTCARD_VENDOR_IFD_EXCHANGE, null);
    } catch (NullPointerException e) {
        System.out.println("OK: " + e);
    }
    // disconnect
    card.disconnect(true);
    System.out.println("OK.");
}
Also used : CardTerminal(javax.smartcardio.CardTerminal) CardException(javax.smartcardio.CardException) CardException(javax.smartcardio.CardException) Card(javax.smartcardio.Card)

Aggregations

CardException (javax.smartcardio.CardException)9 CardTerminal (javax.smartcardio.CardTerminal)6 ApduConnectionException (es.gob.jmulticard.apdu.connection.ApduConnectionException)3 ApduConnectionOpenedInExclusiveModeException (es.gob.jmulticard.apdu.connection.ApduConnectionOpenedInExclusiveModeException)3 CardNotPresentException (es.gob.jmulticard.apdu.connection.CardNotPresentException)3 LostChannelException (es.gob.jmulticard.apdu.connection.LostChannelException)3 NoReadersFoundException (es.gob.jmulticard.apdu.connection.NoReadersFoundException)3 Card (javax.smartcardio.Card)3 CommandAPDU (javax.smartcardio.CommandAPDU)3 CardChannel (javax.smartcardio.CardChannel)2 ResponseAPDU (javax.smartcardio.ResponseAPDU)2 SCIOException (org.openecard.common.ifd.scio.SCIOException)2 ResponseApdu (es.gob.jmulticard.apdu.ResponseApdu)1 GetResponseApduCommand (es.gob.jmulticard.apdu.iso7816four.GetResponseApduCommand)1 ArrayList (java.util.ArrayList)1 CardTerminals (javax.smartcardio.CardTerminals)1 CardCommandAPDU (org.openecard.common.apdu.common.CardCommandAPDU)1 CardResponseAPDU (org.openecard.common.apdu.common.CardResponseAPDU)1 SCIOErrorCode (org.openecard.common.ifd.scio.SCIOErrorCode)1 SCIOTerminal (org.openecard.common.ifd.scio.SCIOTerminal)1