Search in sources :

Example 1 with TokenException

use of org.openecard.mdlw.sal.exceptions.TokenException in project open-ecard by ecsec.

the class MwEventRunner method run.

@Override
public void run() {
    LOG.debug("Start event loop.");
    while (true) {
        try {
            LOG.debug("Waiting for Middleware event.");
            long slotId;
            if (supportsBlockingWait) {
                slotId = mwModule.waitForSlotEvent(0);
            } else if (supportsNonBlockingWait) {
                // TODO: this polling causes to flood logs in case debug is enabled for the wait call
                slotId = mwModule.waitForSlotEvent(1);
                if (slotId == -1) {
                    // nothing changed
                    try {
                        Thread.sleep(1000);
                        continue;
                    } catch (InterruptedException ex) {
                        LOG.debug("Middleware Event Runner interrupted.");
                        return;
                    }
                }
            } else {
                throw new IllegalStateException("This point should never be reached");
            }
            LOG.debug("Middleware event detected.");
            // Flag to check if Terminal was removed
            boolean isProcessed = false;
            // find actual slot object
            for (MwSlot slot : this.mwModule.getSlotList(false)) {
                if (isHwSlot(slot) && slot.getSlotInfo().getSlotID() == slotId) {
                    isProcessed = true;
                    String ifdName = slot.getSlotInfo().getSlotDescription();
                    LOG.debug("Slot event recognized, slotId={}, ifdName={}.", slotId, ifdName);
                    try {
                        slot.getTokenInfo().getLabel();
                        // send card inserted
                        this.sendCardInserted(slot);
                        // send recognized
                        this.sendCardRecognized(slot);
                    } catch (TokenException | SessionException ex) {
                        LOG.debug("Error requesting token information.", ex);
                        this.sendCardRemoved(slot);
                    }
                }
            }
            if (!isProcessed) {
                this.sendTerminalRemoved(slotId);
            }
        } catch (CryptokiException ex) {
            // handle downgrade of the wait method
            if (ex.getErrorCode() == CryptokiLibrary.CKR_FUNCTION_NOT_SUPPORTED) {
                if (supportsBlockingWait) {
                    LOG.info("Blocking wait is not supported. Falling back to non-blocking wait.");
                    supportsBlockingWait = false;
                    continue;
                } else if (supportsNonBlockingWait) {
                    LOG.info("Non-blocking wait is not supported. Terminating event thread.");
                    supportsNonBlockingWait = false;
                    return;
                }
            }
            LOG.error("Unrecoverable error during operation on the token list.", ex);
            try {
                Thread.sleep(10000);
            } catch (InterruptedException ex1) {
                LOG.debug("Middleware Event Runner interrupted.");
                return;
            }
        } catch (RuntimeException ex) {
            LOG.error("Unexpected exception occurred in Middleware Event Runner.", ex);
            throw ex;
        }
    }
}
Also used : MwSlot(org.openecard.mdlw.sal.MwSlot) CryptokiException(org.openecard.mdlw.sal.exceptions.CryptokiException) TokenException(org.openecard.mdlw.sal.exceptions.TokenException) SessionException(org.openecard.mdlw.sal.exceptions.SessionException)

Example 2 with TokenException

use of org.openecard.mdlw.sal.exceptions.TokenException in project open-ecard by ecsec.

the class MiddlewareSAL method augmentCardInfo.

private CardInfoType augmentCardInfo(@Nonnull ConnectionHandleType handle, @Nonnull CardInfoType template, @Nonnull CardSpecType cardSpec) {
    boolean needsConnect = handle.getSlotHandle() == null;
    try {
        // connect card, so that we have a session
        MwSession session;
        if (needsConnect) {
            MwSlot slot = getMatchingSlot(handle.getIFDName(), handle.getSlotIndex());
            if (slot != null) {
                session = slot.openSession();
            } else {
                throw new TokenException("No card available in this slot.", CryptokiLibrary.CKR_TOKEN_NOT_PRESENT);
            }
        } else {
            session = managedSessions.get(handle.getSlotHandle());
        }
        if (session != null) {
            CIFCreator cc = new CIFCreator(session, template, cardSpec);
            CardInfoType cif = cc.addTokenInfo();
            LOG.info("Finished augmenting CardInfo file.");
            return cif;
        } else {
            LOG.warn("Card not available for object info retrieval anymore.");
            return null;
        }
    } catch (WSMarshallerException ex) {
        throw new RuntimeException("Failed to marshal CIF file.", ex);
    } catch (CryptokiException ex) {
        throw new RuntimeException("Error in PKCS#11 module while requesting CIF data.", ex);
    }
}
Also used : CardInfoType(iso.std.iso_iec._24727.tech.schema.CardInfoType) WSMarshallerException(org.openecard.ws.marshal.WSMarshallerException) CryptokiException(org.openecard.mdlw.sal.exceptions.CryptokiException) TokenException(org.openecard.mdlw.sal.exceptions.TokenException)

Example 3 with TokenException

use of org.openecard.mdlw.sal.exceptions.TokenException in project open-ecard by ecsec.

the class MwEventRunner method initRunner.

void initRunner() throws CryptokiException {
    for (MwSlot slot : this.mwModule.getSlotList(TokenState.NotPresent)) {
        if (isHwSlot(slot)) {
            this.sendTerminalAdded(slot);
            String ifdName = slot.getSlotInfo().getSlotDescription();
            try {
                slot.getTokenInfo().getLabel();
                this.sendCardInserted(slot);
                // send recognized
                this.sendCardRecognized(slot);
            } catch (TokenException | SessionException e) {
                LOG.debug("Error getting token information, no card present in the requested slot.", e);
            }
        }
    }
}
Also used : MwSlot(org.openecard.mdlw.sal.MwSlot) TokenException(org.openecard.mdlw.sal.exceptions.TokenException) SessionException(org.openecard.mdlw.sal.exceptions.SessionException)

Aggregations

TokenException (org.openecard.mdlw.sal.exceptions.TokenException)3 MwSlot (org.openecard.mdlw.sal.MwSlot)2 CryptokiException (org.openecard.mdlw.sal.exceptions.CryptokiException)2 SessionException (org.openecard.mdlw.sal.exceptions.SessionException)2 CardInfoType (iso.std.iso_iec._24727.tech.schema.CardInfoType)1 WSMarshallerException (org.openecard.ws.marshal.WSMarshallerException)1