Search in sources :

Example 16 with SCIOException

use of org.openecard.common.ifd.scio.SCIOException in project open-ecard by ecsec.

the class SingleThreadChannel method connectCard.

private static SCIOCard connectCard(SCIOTerminal term) throws SCIOException {
    SCIOCard card;
    try {
        card = term.connect(SCIOProtocol.T1);
    } catch (SCIOException e1) {
        try {
            card = term.connect(SCIOProtocol.TCL);
        } catch (SCIOException e2) {
            try {
                card = term.connect(SCIOProtocol.T0);
            } catch (SCIOException e3) {
                try {
                    card = term.connect(SCIOProtocol.ANY);
                } catch (SCIOException ex) {
                    throw new SCIOException("Reader refused to connect card with any protocol.", ex.getCode());
                }
            }
        }
    }
    LOG.info("Card connected with protocol {}.", card.getProtocol());
    return card;
}
Also used : SCIOException(org.openecard.common.ifd.scio.SCIOException) SCIOCard(org.openecard.common.ifd.scio.SCIOCard)

Example 17 with SCIOException

use of org.openecard.common.ifd.scio.SCIOException in project open-ecard by ecsec.

the class EventWatcher method updateState.

private void updateState(TerminalWatcher.StateChangeEvent event) {
    String name = event.getTerminal();
    if (event.getState() == TerminalWatcher.EventType.TERMINAL_ADDED) {
        currentState.add(createEmptyState(name));
    } else {
        Iterator<IFDStatusType> it = currentState.iterator();
        while (it.hasNext()) {
            IFDStatusType next = it.next();
            SlotStatusType slot = next.getSlotStatus().get(0);
            if (next.getIFDName().equals(name)) {
                switch(event.getState()) {
                    case CARD_INSERTED:
                        try {
                            SingleThreadChannel ch = cm.openMasterChannel(name);
                            slot.setCardAvailable(true);
                            slot.setATRorATS(ch.getChannel().getCard().getATR().getBytes());
                        } catch (NoSuchTerminal | SCIOException ex) {
                            LOG.error("Failed to open master channel for terminal '" + name + "'.", ex);
                            slot.setCardAvailable(false);
                            cm.closeMasterChannel(name);
                        }
                        break;
                    case CARD_REMOVED:
                        cm.closeMasterChannel(name);
                        slot.setCardAvailable(false);
                        break;
                    case TERMINAL_REMOVED:
                        // just in case
                        slot.setCardAvailable(false);
                        next.setConnected(false);
                        break;
                }
                // no need to look any further
                break;
            }
        }
    }
}
Also used : NoSuchTerminal(org.openecard.common.ifd.scio.NoSuchTerminal) SingleThreadChannel(org.openecard.ifd.scio.wrapper.SingleThreadChannel) SCIOException(org.openecard.common.ifd.scio.SCIOException) IFDStatusType(iso.std.iso_iec._24727.tech.schema.IFDStatusType) SlotStatusType(iso.std.iso_iec._24727.tech.schema.SlotStatusType)

Example 18 with SCIOException

use of org.openecard.common.ifd.scio.SCIOException in project open-ecard by ecsec.

the class IFD method controlIFD.

/**
 * Note: the first byte of the command data is the control code.
 */
@Override
public ControlIFDResponse controlIFD(ControlIFD parameters) {
    ControlIFDResponse response;
    if (!hasContext()) {
        String msg = "Context not initialized.";
        Result r = WSHelper.makeResultError(ECardConstants.Minor.IFD.INVALID_SLOT_HANDLE, msg);
        response = WSHelper.makeResponse(ControlIFDResponse.class, r);
        return response;
    }
    byte[] handle = parameters.getSlotHandle();
    byte[] command = parameters.getCommand();
    if (handle == null || command == null) {
        String msg = "Missing parameter.";
        Result r = WSHelper.makeResultUnknownError(msg);
        response = WSHelper.makeResponse(ControlIFDResponse.class, r);
        return response;
    }
    byte ctrlCode = command[0];
    command = Arrays.copyOfRange(command, 1, command.length);
    try {
        SingleThreadChannel ch = cm.getSlaveChannel(handle);
        TerminalInfo info = new TerminalInfo(cm, ch);
        Integer featureCode = info.getFeatureCodes().get(Integer.valueOf(ctrlCode));
        // see if the terminal can deal with that
        if (featureCode != null) {
            byte[] resultCommand = ch.transmitControlCommand(featureCode, command);
            // evaluate result
            Result result = evaluateControlIFDRAPDU(resultCommand);
            response = WSHelper.makeResponse(ControlIFDResponse.class, result);
            response.setResponse(resultCommand);
            return response;
        } else {
            String msg = "The terminal is not capable of performing the requested action.";
            Result r = WSHelper.makeResultUnknownError(msg);
            response = WSHelper.makeResponse(ControlIFDResponse.class, r);
            return response;
        }
    } catch (NoSuchChannel | IllegalStateException ex) {
        String msg = "The card or the terminal is not available anymore.";
        Result r = WSHelper.makeResultError(ECardConstants.Minor.IFD.Terminal.UNKNOWN_IFD, msg);
        response = WSHelper.makeResponse(ControlIFDResponse.class, r);
        LOG.warn(msg, ex);
        return response;
    } catch (SCIOException ex) {
        String msg = "Unknown error while sending transmit control command.";
        Result r = WSHelper.makeResultUnknownError(msg);
        response = WSHelper.makeResponse(ControlIFDResponse.class, r);
        LOG.warn(msg, ex);
        return response;
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) BigInteger(java.math.BigInteger) ControlIFDResponse(iso.std.iso_iec._24727.tech.schema.ControlIFDResponse) SingleThreadChannel(org.openecard.ifd.scio.wrapper.SingleThreadChannel) NoSuchChannel(org.openecard.ifd.scio.wrapper.NoSuchChannel) SCIOException(org.openecard.common.ifd.scio.SCIOException) TerminalInfo(org.openecard.ifd.scio.wrapper.TerminalInfo) Result(oasis.names.tc.dss._1_0.core.schema.Result)

Example 19 with SCIOException

use of org.openecard.common.ifd.scio.SCIOException in project open-ecard by ecsec.

the class IFD method getStatus.

@Override
public GetStatusResponse getStatus(GetStatus parameters) {
    GetStatusResponse response;
    // you thought of a different IFD obviously
    if (!ByteUtils.compare(ctxHandle, parameters.getContextHandle())) {
        String msg = "Invalid context handle specified.";
        Result r = WSHelper.makeResultError(ECardConstants.Minor.IFD.INVALID_CONTEXT_HANDLE, msg);
        response = WSHelper.makeResponse(GetStatusResponse.class, r);
        return response;
    }
    // get specific ifd or all if no specific one is requested
    List<SCIOTerminal> ifds = new LinkedList<>();
    try {
        String requestedIfd = parameters.getIFDName();
        if (requestedIfd != null) {
            try {
                SCIOTerminal t = cm.getTerminals().getTerminal(requestedIfd);
                ifds.add(t);
            } catch (NoSuchTerminal ex) {
                String msg = "The requested IFD name does not exist.";
                LOG.warn(msg, ex);
                String minor = ECardConstants.Minor.IFD.Terminal.UNKNOWN_IFD;
                Result r = WSHelper.makeResult(ECardConstants.Major.ERROR, minor, msg);
                response = WSHelper.makeResponse(GetStatusResponse.class, r);
                return response;
            }
        } else {
            ifds.addAll(cm.getTerminals().list());
        }
    } catch (SCIOException ex) {
        String msg = "Failed to get list with the terminals.";
        LOG.warn(msg, ex);
        response = WSHelper.makeResponse(GetStatusResponse.class, WSHelper.makeResultUnknownError(msg));
        return response;
    }
    // request status for each ifd
    ArrayList<IFDStatusType> status = new ArrayList<>(ifds.size());
    for (SCIOTerminal ifd : ifds) {
        TerminalInfo info;
        try {
            SingleThreadChannel channel = cm.openMasterChannel(ifd.getName());
            info = new TerminalInfo(cm, channel);
        } catch (NoSuchTerminal | SCIOException ex) {
            // continue without a channel
            info = new TerminalInfo(cm, ifd);
        }
        try {
            IFDStatusType s = info.getStatus();
            status.add(s);
        } catch (SCIOException ex) {
            if (ex.getCode() != SCIOErrorCode.SCARD_W_UNPOWERED_CARD && ex.getCode() != SCIOErrorCode.SCARD_W_UNRESPONSIVE_CARD && ex.getCode() != SCIOErrorCode.SCARD_W_UNSUPPORTED_CARD && ex.getCode() != SCIOErrorCode.SCARD_E_PROTO_MISMATCH) {
                String msg = String.format("Failed to determine status of terminal '%s'.", ifd.getName());
                LOG.warn(msg, ex);
                Result r = WSHelper.makeResultUnknownError(msg);
                response = WSHelper.makeResponse(GetStatusResponse.class, r);
                return response;
            } else {
                // fall througth if there is a card which can not be connected
                LOG.info("Ignoring failed status request from terminal.", ex);
            }
        }
    }
    // everything worked out well
    response = WSHelper.makeResponse(GetStatusResponse.class, WSHelper.makeResultOK());
    response.getIFDStatus().addAll(status);
    return response;
}
Also used : NoSuchTerminal(org.openecard.common.ifd.scio.NoSuchTerminal) GetStatusResponse(iso.std.iso_iec._24727.tech.schema.GetStatusResponse) SCIOException(org.openecard.common.ifd.scio.SCIOException) SingleThreadChannel(org.openecard.ifd.scio.wrapper.SingleThreadChannel) SCIOTerminal(org.openecard.common.ifd.scio.SCIOTerminal) ArrayList(java.util.ArrayList) TerminalInfo(org.openecard.ifd.scio.wrapper.TerminalInfo) LinkedList(java.util.LinkedList) Result(oasis.names.tc.dss._1_0.core.schema.Result) IFDStatusType(iso.std.iso_iec._24727.tech.schema.IFDStatusType)

Aggregations

SCIOException (org.openecard.common.ifd.scio.SCIOException)19 Result (oasis.names.tc.dss._1_0.core.schema.Result)10 SingleThreadChannel (org.openecard.ifd.scio.wrapper.SingleThreadChannel)10 NoSuchTerminal (org.openecard.common.ifd.scio.NoSuchTerminal)7 ExecutionException (java.util.concurrent.ExecutionException)6 IFDStatusType (iso.std.iso_iec._24727.tech.schema.IFDStatusType)5 ThreadTerminateException (org.openecard.common.ThreadTerminateException)5 NoSuchChannel (org.openecard.ifd.scio.wrapper.NoSuchChannel)5 SlotStatusType (iso.std.iso_iec._24727.tech.schema.SlotStatusType)4 SCIOTerminal (org.openecard.common.ifd.scio.SCIOTerminal)4 BigInteger (java.math.BigInteger)3 ArrayList (java.util.ArrayList)3 LinkedList (java.util.LinkedList)3 BeginTransactionResponse (iso.std.iso_iec._24727.tech.schema.BeginTransactionResponse)2 ConnectionHandleType (iso.std.iso_iec._24727.tech.schema.ConnectionHandleType)2 IFDCapabilitiesType (iso.std.iso_iec._24727.tech.schema.IFDCapabilitiesType)2 CardException (javax.smartcardio.CardException)2 SCIOErrorCode (org.openecard.common.ifd.scio.SCIOErrorCode)2 TerminalInfo (org.openecard.ifd.scio.wrapper.TerminalInfo)2 IsoDep (android.nfc.tech.IsoDep)1