Search in sources :

Example 1 with ConnectionHandleType

use of iso.std.iso_iec._24727.tech.schema.ConnectionHandleType in project open-ecard by ecsec.

the class IfdEventManager method resetCard.

/**
 * Resets a card given as connection handle.
 *
 * @param cHandleRm {@link ConnectionHandleType} object representing a card which shall be removed.
 * @param cHandleIn {@link ConnectionHandleType} object representing a card which shall be inserted.
 * @param ifaceProtocol Interface protocol of the connected card.
 */
public void resetCard(ConnectionHandleType cHandleRm, ConnectionHandleType cHandleIn, String ifaceProtocol) {
    env.getEventDispatcher().notify(EventType.CARD_REMOVED, new IfdEventObject(cHandleRm, null));
    // determine if the reader has a protected auth path
    IFDCapabilitiesType slotCapabilities = getCapabilities(cHandleRm.getContextHandle(), cHandleRm.getIFDName());
    boolean protectedAuthPath = slotCapabilities != null ? !slotCapabilities.getKeyPadCapability().isEmpty() : false;
    HandlerBuilder chBuilder = HandlerBuilder.create();
    ConnectionHandleType cInNew = chBuilder.setSessionId(sessionId).setCardType(cHandleIn.getRecognitionInfo()).setCardIdentifier(cHandleIn.getRecognitionInfo()).setContextHandle(cHandleIn.getContextHandle()).setIfdName(cHandleIn.getIFDName()).setSlotIdx(BigInteger.ZERO).setSlotHandle(cHandleIn.getSlotHandle()).setProtectedAuthPath(protectedAuthPath).buildConnectionHandle();
    env.getEventDispatcher().notify(EventType.CARD_INSERTED, new IfdEventObject(cInNew));
    if (isRecognize()) {
        Recognizer rec = new Recognizer(env, cInNew, ifaceProtocol);
        Thread recThread = new Thread(rec, "Recoginiton-" + THREAD_NUM.getAndIncrement());
        recThread.start();
    }
}
Also used : ConnectionHandleType(iso.std.iso_iec._24727.tech.schema.ConnectionHandleType) IFDCapabilitiesType(iso.std.iso_iec._24727.tech.schema.IFDCapabilitiesType) IfdEventObject(org.openecard.common.event.IfdEventObject) HandlerBuilder(org.openecard.common.util.HandlerBuilder)

Example 2 with ConnectionHandleType

use of iso.std.iso_iec._24727.tech.schema.ConnectionHandleType in project open-ecard by ecsec.

the class IfdEventRunner method fireEvents.

private void fireEvents(@Nonnull List<IFDStatusType> diff) {
    for (IFDStatusType term : diff) {
        String ifdName = term.getIFDName();
        // find out if the terminal is new, or only a slot got updated
        IFDStatusType oldTerm = getCorresponding(ifdName, currentState);
        boolean terminalAdded = oldTerm == null;
        IFDCapabilitiesType slotCapabilities = getCapabilities(ifdName);
        if (terminalAdded) {
            // TERMINAL ADDED
            // make copy of term
            oldTerm = new IFDStatusType();
            oldTerm.setIFDName(ifdName);
            oldTerm.setConnected(true);
            // add to current list
            currentState.add(oldTerm);
            // create event
            ConnectionHandleType h = makeConnectionHandle(ifdName, null, slotCapabilities);
            LOG.debug("Found a terminal added event ({}).", ifdName);
            env.getEventDispatcher().notify(EventType.TERMINAL_ADDED, new IfdEventObject(h));
        }
        // check each slot
        for (SlotStatusType slot : term.getSlotStatus()) {
            SlotStatusType oldSlot = getCorresponding(slot.getIndex(), oldTerm.getSlotStatus());
            boolean cardPresent = slot.isCardAvailable();
            boolean cardWasPresent = oldSlot != null && oldSlot.isCardAvailable();
            if (cardPresent && !cardWasPresent) {
                // CARD INSERTED
                // copy slot and add to list
                SlotStatusType newSlot = oldSlot;
                if (newSlot == null) {
                    newSlot = new SlotStatusType();
                    oldTerm.getSlotStatus().add(newSlot);
                }
                newSlot.setIndex(slot.getIndex());
                newSlot.setCardAvailable(true);
                newSlot.setATRorATS(slot.getATRorATS());
                // create event
                LOG.debug("Found a card insert event ({}).", ifdName);
                LOG.info("Card with ATR={} inserted.", ByteUtils.toHexString(slot.getATRorATS()));
                ConnectionHandleType handle = makeUnknownCardHandle(ifdName, newSlot, slotCapabilities);
                env.getEventDispatcher().notify(EventType.CARD_INSERTED, new IfdEventObject(handle));
                try {
                    SingleThreadChannel ch = cm.openMasterChannel(ifdName);
                    if (evtManager.isRecognize()) {
                        String proto = ch.getChannel().getCard().getProtocol().toUri();
                        evtManager.threadPool.submit(new Recognizer(env, handle, proto));
                    }
                } catch (NoSuchTerminal | SCIOException ex) {
                    LOG.error("Failed to connect card, nevertheless sending CARD_INSERTED event.", ex);
                }
            } else if (!terminalAdded && !cardPresent && cardWasPresent) {
                // this makes only sense when the terminal was already there
                // CARD REMOVED
                // remove slot entry
                BigInteger idx = oldSlot.getIndex();
                Iterator<SlotStatusType> it = oldTerm.getSlotStatus().iterator();
                while (it.hasNext()) {
                    SlotStatusType next = it.next();
                    if (idx.equals(next.getIndex())) {
                        it.remove();
                        break;
                    }
                }
                LOG.debug("Found a card removed event ({}).", ifdName);
                ConnectionHandleType h = makeConnectionHandle(ifdName, idx, slotCapabilities);
                env.getEventDispatcher().notify(EventType.CARD_REMOVED, new IfdEventObject(h));
            }
        }
        // terminal removed event comes after card removed events
        boolean terminalPresent = term.isConnected();
        if (!terminalPresent) {
            // TERMINAL REMOVED
            Iterator<IFDStatusType> it = currentState.iterator();
            while (it.hasNext()) {
                IFDStatusType toDel = it.next();
                if (toDel.getIFDName().equals(term.getIFDName())) {
                    it.remove();
                }
            }
            ConnectionHandleType h = makeConnectionHandle(ifdName, null, slotCapabilities);
            LOG.debug("Found a terminal removed event ({}).", ifdName);
            env.getEventDispatcher().notify(EventType.TERMINAL_REMOVED, new IfdEventObject(h));
        }
    }
}
Also used : ConnectionHandleType(iso.std.iso_iec._24727.tech.schema.ConnectionHandleType) NoSuchTerminal(org.openecard.common.ifd.scio.NoSuchTerminal) SingleThreadChannel(org.openecard.ifd.scio.wrapper.SingleThreadChannel) SCIOException(org.openecard.common.ifd.scio.SCIOException) IFDCapabilitiesType(iso.std.iso_iec._24727.tech.schema.IFDCapabilitiesType) Iterator(java.util.Iterator) BigInteger(java.math.BigInteger) IFDStatusType(iso.std.iso_iec._24727.tech.schema.IFDStatusType) SlotStatusType(iso.std.iso_iec._24727.tech.schema.SlotStatusType) IfdEventObject(org.openecard.common.event.IfdEventObject)

Example 3 with ConnectionHandleType

use of iso.std.iso_iec._24727.tech.schema.ConnectionHandleType in project open-ecard by ecsec.

the class Recognizer method recognizeSlot.

private ConnectionHandleType recognizeSlot() {
    RecognitionInfo rInfo = null;
    RecognitionInfo currentInfo = handle.getRecognitionInfo();
    if (currentInfo == null || currentInfo.getCardIdentifier() == null) {
        LOG.error("Current recognition info or CardIdentifier is null.");
    } else if (env.getCIFProvider().needsRecognition(handle.getRecognitionInfo().getCardIdentifier())) {
        try {
            CardRecognition cr = env.getRecognition();
            rInfo = cr.recognizeCard(handle.getContextHandle(), handle.getIFDName(), handle.getSlotIndex());
        } catch (RecognitionException ex) {
        // ignore, card is just unknown
        }
    }
    if (rInfo != null) {
        ConnectionHandleType newHandle = HandlerUtils.copyHandle(handle);
        newHandle.getRecognitionInfo().setCardType(rInfo.getCardType());
        // Remove card identifier (ATR/ATS) as TR-03112-4 states that this should contain the ATR/ATS for unknown
        // cards and the ICCSN or something similar for known cards. Until we extract the ICCSN just remove the ATR.
        newHandle.getRecognitionInfo().setCardIdentifier(null);
        return newHandle;
    } else {
        return null;
    }
}
Also used : ConnectionHandleType(iso.std.iso_iec._24727.tech.schema.ConnectionHandleType) RecognitionInfo(iso.std.iso_iec._24727.tech.schema.ConnectionHandleType.RecognitionInfo) CardRecognition(org.openecard.common.interfaces.CardRecognition) RecognitionException(org.openecard.common.interfaces.RecognitionException)

Example 4 with ConnectionHandleType

use of iso.std.iso_iec._24727.tech.schema.ConnectionHandleType in project open-ecard by ecsec.

the class Recognizer method run.

@Override
public void run() {
    ConnectionHandleType newHandle = recognizeSlot();
    if (newHandle != null) {
        LOG.debug("Found a recognized card event ({}).", handle.getIFDName());
        env.getEventDispatcher().notify(EventType.CARD_RECOGNIZED, new IfdEventObject(newHandle, ifaceProtocol));
    }
}
Also used : ConnectionHandleType(iso.std.iso_iec._24727.tech.schema.ConnectionHandleType) IfdEventObject(org.openecard.common.event.IfdEventObject)

Example 5 with ConnectionHandleType

use of iso.std.iso_iec._24727.tech.schema.ConnectionHandleType 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)

Aggregations

ConnectionHandleType (iso.std.iso_iec._24727.tech.schema.ConnectionHandleType)110 CardStateEntry (org.openecard.common.sal.state.CardStateEntry)47 ECardException (org.openecard.common.ECardException)43 IncorrectParameterException (org.openecard.common.sal.exception.IncorrectParameterException)37 ThreadTerminateException (org.openecard.common.ThreadTerminateException)36 NamedEntityNotFoundException (org.openecard.common.sal.exception.NamedEntityNotFoundException)34 UnknownProtocolException (org.openecard.common.sal.exception.UnknownProtocolException)34 TLVException (org.openecard.common.tlv.TLVException)29 AddonNotFoundException (org.openecard.addon.AddonNotFoundException)28 InappropriateProtocolForActionException (org.openecard.common.sal.exception.InappropriateProtocolForActionException)28 NameExistsException (org.openecard.common.sal.exception.NameExistsException)28 PrerequisitesNotSatisfiedException (org.openecard.common.sal.exception.PrerequisitesNotSatisfiedException)28 SecurityConditionNotSatisfiedException (org.openecard.common.sal.exception.SecurityConditionNotSatisfiedException)28 UnknownConnectionHandleException (org.openecard.common.sal.exception.UnknownConnectionHandleException)28 DIDStructureType (iso.std.iso_iec._24727.tech.schema.DIDStructureType)22 Publish (org.openecard.common.interfaces.Publish)17 CardApplicationConnectResponse (iso.std.iso_iec._24727.tech.schema.CardApplicationConnectResponse)15 ArrayList (java.util.ArrayList)15 CardApplicationConnect (iso.std.iso_iec._24727.tech.schema.CardApplicationConnect)14 CardApplicationPathType (iso.std.iso_iec._24727.tech.schema.CardApplicationPathType)14