Search in sources :

Example 6 with SCIOException

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

the class IFD method connect.

@Override
public ConnectResponse connect(Connect parameters) {
    try {
        ConnectResponse response;
        // check if the requested handle is valid
        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(ConnectResponse.class, r);
            return response;
        } else {
            try {
                String name = parameters.getIFDName();
                // make sure the slot is connected before attemting to get a slave channel
                cm.openMasterChannel(name);
                byte[] slotHandle = cm.openSlaveChannel(name).p1;
                SingleThreadChannel ch = cm.getSlaveChannel(slotHandle);
                // make connection exclusive
                Boolean exclusive = parameters.isExclusive();
                if (exclusive != null && exclusive == true) {
                    BeginTransaction transact = new BeginTransaction();
                    transact.setSlotHandle(slotHandle);
                    BeginTransactionResponse resp = beginTransaction(transact);
                    if (resp.getResult().getResultMajor().equals(ECardConstants.Major.ERROR)) {
                        // destroy channel, when not successful here
                        ch.shutdown();
                        response = WSHelper.makeResponse(ConnectResponse.class, resp.getResult());
                        return response;
                    }
                }
                // connection established, return result
                response = WSHelper.makeResponse(ConnectResponse.class, WSHelper.makeResultOK());
                response.setSlotHandle(slotHandle);
                return response;
            } catch (NoSuchTerminal | NullPointerException ex) {
                String msg = "The requested terminal does not exist.";
                Result r = WSHelper.makeResultError(ECardConstants.Minor.IFD.Terminal.UNKNOWN_IFD, msg);
                response = WSHelper.makeResponse(ConnectResponse.class, r);
                LOG.warn(msg, ex);
                return response;
            } catch (IllegalStateException ex) {
                String msg = "No card available in the requested terminal.";
                Result r = WSHelper.makeResultError(ECardConstants.Minor.IFD.Terminal.NO_CARD, msg);
                response = WSHelper.makeResponse(ConnectResponse.class, r);
                LOG.warn(msg, ex);
                return response;
            } catch (SCIOException ex) {
                String msg = "Unknown error in the underlying SCIO implementation.";
                Result r = WSHelper.makeResultUnknownError(msg);
                response = WSHelper.makeResponse(ConnectResponse.class, r);
                LOG.warn(msg, ex);
                return response;
            }
        }
    } catch (Exception ex) {
        LOG.warn(ex.getMessage(), ex);
        throwThreadKillException(ex);
        return WSHelper.makeResponse(ConnectResponse.class, WSHelper.makeResult(ex));
    }
}
Also used : NoSuchTerminal(org.openecard.common.ifd.scio.NoSuchTerminal) ConnectResponse(iso.std.iso_iec._24727.tech.schema.ConnectResponse) SingleThreadChannel(org.openecard.ifd.scio.wrapper.SingleThreadChannel) SCIOException(org.openecard.common.ifd.scio.SCIOException) ThreadTerminateException(org.openecard.common.ThreadTerminateException) SCIOException(org.openecard.common.ifd.scio.SCIOException) ExecutionException(java.util.concurrent.ExecutionException) Result(oasis.names.tc.dss._1_0.core.schema.Result) BeginTransactionResponse(iso.std.iso_iec._24727.tech.schema.BeginTransactionResponse) BeginTransaction(iso.std.iso_iec._24727.tech.schema.BeginTransaction)

Example 7 with SCIOException

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

the class IFD method listIFDs.

@Override
public ListIFDsResponse listIFDs(ListIFDs parameters) {
    ListIFDsResponse response;
    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(ListIFDsResponse.class, r);
        return response;
    } else {
        try {
            List<SCIOTerminal> terminals = cm.getTerminals().list();
            ArrayList<String> ifds = new ArrayList<>(terminals.size());
            for (SCIOTerminal next : terminals) {
                ifds.add(next.getName());
            }
            response = WSHelper.makeResponse(ListIFDsResponse.class, WSHelper.makeResultOK());
            response.getIFDName().addAll(ifds);
            return response;
        } catch (SCIOException ex) {
            LOG.warn(ex.getMessage(), ex);
            Result r = WSHelper.makeResultUnknownError(ex.getMessage());
            response = WSHelper.makeResponse(ListIFDsResponse.class, r);
            return response;
        }
    }
}
Also used : ListIFDsResponse(iso.std.iso_iec._24727.tech.schema.ListIFDsResponse) SCIOException(org.openecard.common.ifd.scio.SCIOException) SCIOTerminal(org.openecard.common.ifd.scio.SCIOTerminal) ArrayList(java.util.ArrayList) Result(oasis.names.tc.dss._1_0.core.schema.Result)

Example 8 with SCIOException

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

the class IFD method disconnect.

@Override
public synchronized DisconnectResponse disconnect(Disconnect parameters) {
    try {
        DisconnectResponse response;
        if (!hasContext()) {
            String msg = "Context not initialized.";
            Result r = WSHelper.makeResultError(ECardConstants.Minor.IFD.INVALID_SLOT_HANDLE, msg);
            response = WSHelper.makeResponse(DisconnectResponse.class, r);
            return response;
        }
        try {
            byte[] handle = parameters.getSlotHandle();
            SingleThreadChannel ch = cm.getSlaveChannel(handle);
            cm.closeSlaveChannel(handle);
            // process actions
            SCIOCard card = ch.getChannel().getCard();
            ActionType action = parameters.getAction();
            if (ActionType.RESET == action) {
                String ifdName = card.getTerminal().getName();
                SingleThreadChannel master = cm.getMasterChannel(ifdName);
                HandlerBuilder builder = HandlerBuilder.create();
                ConnectionHandleType cHandleIn = builder.setCardType(ECardConstants.UNKNOWN_CARD).setCardIdentifier(card.getATR().getBytes()).setContextHandle(ctxHandle).setIfdName(ifdName).setSlotIdx(BigInteger.ZERO).buildConnectionHandle();
                builder = HandlerBuilder.create();
                ConnectionHandleType cHandleRm = builder.setContextHandle(ctxHandle).setIfdName(ifdName).setSlotIdx(BigInteger.ZERO).buildConnectionHandle();
                try {
                    master.reconnect();
                    evManager.resetCard(cHandleRm, cHandleIn, card.getProtocol().toUri());
                } catch (IllegalStateException ex) {
                    LOG.warn("Card reconnect failed, trying to establish new card connection.", ex);
                    cm.closeMasterChannel(ifdName);
                    LOG.debug("Master channel closed successfully.");
                    try {
                        cm.getMasterChannel(ifdName);
                        LOG.debug("New card connection established successfully.");
                        evManager.resetCard(cHandleRm, cHandleIn, card.getProtocol().toUri());
                    } catch (NoSuchTerminal ex2) {
                        LOG.error("No terminal present anymore.", ex);
                    }
                }
            }
            // TODO: take care of other actions (probably over ControlIFD)
            // the default is to not disconnect the card, because all existing connections would be broken
            response = WSHelper.makeResponse(DisconnectResponse.class, WSHelper.makeResultOK());
            return response;
        } catch (NoSuchChannel ex) {
            String msg = "No card available in the requested terminal.";
            Result r = WSHelper.makeResultError(ECardConstants.Minor.IFD.INVALID_SLOT_HANDLE, msg);
            response = WSHelper.makeResponse(DisconnectResponse.class, r);
            LOG.warn(msg, ex);
            return response;
        } catch (SCIOException ex) {
            String msg = "Unknown error in the underlying SCIO implementation.";
            Result r = WSHelper.makeResultUnknownError(msg);
            response = WSHelper.makeResponse(DisconnectResponse.class, r);
            LOG.warn(msg, ex);
            return response;
        }
    } catch (Exception ex) {
        LOG.warn(ex.getMessage(), ex);
        throwThreadKillException(ex);
        return WSHelper.makeResponse(DisconnectResponse.class, WSHelper.makeResult(ex));
    }
}
Also used : ConnectionHandleType(iso.std.iso_iec._24727.tech.schema.ConnectionHandleType) ActionType(iso.std.iso_iec._24727.tech.schema.ActionType) NoSuchTerminal(org.openecard.common.ifd.scio.NoSuchTerminal) SingleThreadChannel(org.openecard.ifd.scio.wrapper.SingleThreadChannel) NoSuchChannel(org.openecard.ifd.scio.wrapper.NoSuchChannel) SCIOException(org.openecard.common.ifd.scio.SCIOException) SCIOCard(org.openecard.common.ifd.scio.SCIOCard) ThreadTerminateException(org.openecard.common.ThreadTerminateException) SCIOException(org.openecard.common.ifd.scio.SCIOException) ExecutionException(java.util.concurrent.ExecutionException) Result(oasis.names.tc.dss._1_0.core.schema.Result) DisconnectResponse(iso.std.iso_iec._24727.tech.schema.DisconnectResponse) HandlerBuilder(org.openecard.common.util.HandlerBuilder)

Example 9 with SCIOException

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

the class IFD method endTransaction.

@Override
public EndTransactionResponse endTransaction(EndTransaction parameters) {
    try {
        EndTransactionResponse response;
        if (!hasContext()) {
            String msg = "Context not initialized.";
            Result r = WSHelper.makeResultError(ECardConstants.Minor.IFD.INVALID_SLOT_HANDLE, msg);
            response = WSHelper.makeResponse(EndTransactionResponse.class, r);
            return response;
        }
        try {
            byte[] handle = parameters.getSlotHandle();
            SingleThreadChannel ch = cm.getSlaveChannel(handle);
            ch.endExclusive();
        } catch (NoSuchChannel | IllegalStateException ex) {
            String msg = "No card with transaction available in the requested terminal.";
            Result r = WSHelper.makeResultError(ECardConstants.Minor.IFD.INVALID_SLOT_HANDLE, msg);
            response = WSHelper.makeResponse(EndTransactionResponse.class, r);
            LOG.warn(msg, ex);
            return response;
        } catch (SCIOException ex) {
            String msg = "Unknown error in the underlying SCIO implementation.";
            Result r = WSHelper.makeResultUnknownError(msg);
            response = WSHelper.makeResponse(EndTransactionResponse.class, r);
            LOG.warn(msg, ex);
            return response;
        }
        response = WSHelper.makeResponse(EndTransactionResponse.class, WSHelper.makeResultOK());
        return response;
    } catch (Exception ex) {
        LOG.warn(ex.getMessage(), ex);
        throwThreadKillException(ex);
        return WSHelper.makeResponse(EndTransactionResponse.class, WSHelper.makeResult(ex));
    }
}
Also used : SingleThreadChannel(org.openecard.ifd.scio.wrapper.SingleThreadChannel) NoSuchChannel(org.openecard.ifd.scio.wrapper.NoSuchChannel) SCIOException(org.openecard.common.ifd.scio.SCIOException) EndTransactionResponse(iso.std.iso_iec._24727.tech.schema.EndTransactionResponse) ThreadTerminateException(org.openecard.common.ThreadTerminateException) SCIOException(org.openecard.common.ifd.scio.SCIOException) ExecutionException(java.util.concurrent.ExecutionException) Result(oasis.names.tc.dss._1_0.core.schema.Result)

Example 10 with SCIOException

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

the class TerminalInfo method getPACECapabilities.

public List<PACECapabilities.PACECapability> getPACECapabilities() throws SCIOException {
    List<PACECapabilities.PACECapability> result = new LinkedList<>();
    if (PACECapabilities == null) {
        if (isConnected()) {
            if (supportsPace()) {
                int ctrlCode = getPaceCtrlCode();
                ExecutePACERequest.Function paceFunc = ExecutePACERequest.Function.GetReaderPACECapabilities;
                byte[] getCapabilityRequest = new ExecutePACERequest(paceFunc).toBytes();
                byte[] response = channel.transmitControlCommand(ctrlCode, getCapabilityRequest);
                ExecutePACEResponse paceResponse = new ExecutePACEResponse(response);
                if (paceResponse.isError()) {
                    String msg = "PACE is advertised but the result iss errornous.\n";
                    msg += paceResponse.getResult().getResultMessage().getValue();
                    throw new SCIOException(msg, SCIOErrorCode.SCARD_F_UNKNOWN_ERROR);
                }
                PACECapabilities cap = new PACECapabilities(paceResponse.getData());
                PACECapabilities = cap.getFeaturesEnum();
                result.addAll(PACECapabilities);
            }
        }
    } else {
        result.addAll(PACECapabilities);
    }
    return Collections.unmodifiableList(result);
}
Also used : PACECapabilities(org.openecard.common.ifd.PACECapabilities) SCIOException(org.openecard.common.ifd.scio.SCIOException) ExecutePACEResponse(org.openecard.ifd.scio.reader.ExecutePACEResponse) ExecutePACERequest(org.openecard.ifd.scio.reader.ExecutePACERequest) LinkedList(java.util.LinkedList)

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