Search in sources :

Example 1 with CryptokiException

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

the class MiddlewareSAL method dsiRead.

@Override
public DSIReadResponse dsiRead(DSIRead request) {
    DSIReadResponse response = WSHelper.makeResponse(DSIReadResponse.class, WSHelper.makeResultOK());
    try {
        ConnectionHandleType connectionHandle = SALUtils.getConnectionHandle(request);
        CardStateEntry cardStateEntry = SALUtils.getCardStateEntry(states, connectionHandle);
        byte[] applicationID = cardStateEntry.getCurrentCardApplication().getApplicationIdentifier();
        String dsiName = request.getDSIName();
        byte[] slotHandle = connectionHandle.getSlotHandle();
        Assert.assertIncorrectParameter(dsiName, "The parameter DSIName is empty.");
        Assert.securityConditionDataSet(cardStateEntry, applicationID, dsiName, NamedDataServiceActionName.DSI_READ);
        MwSession session = managedSessions.get(slotHandle);
        for (MwCertificate cert : session.getCertificates()) {
            try {
                String label = cert.getLabel();
                if (label.equals(dsiName)) {
                    // read certificate
                    byte[] certificate = cert.getValue();
                    response.setDSIContent(certificate);
                    return response;
                }
            } catch (CryptokiException ex) {
                LOG.warn("Skipping certificate due to error.", ex);
            }
        }
        String msg = "The given DSIName does not related to any know DSI or DataSet.";
        throw new IncorrectParameterException(msg);
    } catch (ECardException e) {
        response.setResult(e.getResult());
    } catch (Exception e) {
        LOG.error(e.getMessage(), e);
        throwThreadKillException(e);
        response.setResult(WSHelper.makeResult(e));
    }
    return response;
}
Also used : ConnectionHandleType(iso.std.iso_iec._24727.tech.schema.ConnectionHandleType) ECardException(org.openecard.common.ECardException) CardStateEntry(org.openecard.common.sal.state.CardStateEntry) CryptokiException(org.openecard.mdlw.sal.exceptions.CryptokiException) IncorrectParameterException(org.openecard.common.sal.exception.IncorrectParameterException) DSIReadResponse(iso.std.iso_iec._24727.tech.schema.DSIReadResponse) ThreadTerminateException(org.openecard.common.ThreadTerminateException) InitializationException(org.openecard.mdlw.sal.exceptions.InitializationException) ECardException(org.openecard.common.ECardException) FinalizationException(org.openecard.mdlw.sal.exceptions.FinalizationException) PinBlockedException(org.openecard.mdlw.sal.exceptions.PinBlockedException) CryptokiException(org.openecard.mdlw.sal.exceptions.CryptokiException) NamedEntityNotFoundException(org.openecard.common.sal.exception.NamedEntityNotFoundException) UnknownProtocolException(org.openecard.common.sal.exception.UnknownProtocolException) TokenException(org.openecard.mdlw.sal.exceptions.TokenException) WSMarshallerException(org.openecard.ws.marshal.WSMarshallerException) IncorrectParameterException(org.openecard.common.sal.exception.IncorrectParameterException) UnsupportedAlgorithmException(org.openecard.crypto.common.UnsupportedAlgorithmException) PinIncorrectException(org.openecard.mdlw.sal.exceptions.PinIncorrectException)

Example 2 with CryptokiException

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

the class MiddlewareSAL method setPinNotAuth.

private void setPinNotAuth(CardStateEntry cardStateEntry) {
    LOG.info("Logout card session.");
    // This method only works in a avery limited way. All PIN DIDs get status unauth here.
    for (DIDInfoType didInfo : Collections.unmodifiableCollection(cardStateEntry.getAuthenticatedDIDs())) {
        if ("urn:oid:1.3.162.15480.3.0.9".equals(didInfo.getDifferentialIdentity().getDIDProtocol())) {
            cardStateEntry.removeAuthenticated(didInfo);
        }
    }
    // logout from session, or middleware doesn't hear the shot
    try {
        MwSession session = managedSessions.get(cardStateEntry.handleCopy().getSlotHandle());
        session.logout();
    } catch (CryptokiException ex) {
        LOG.info("Failed to logout from card.");
    }
}
Also used : DIDInfoType(iso.std.iso_iec._24727.tech.schema.DIDInfoType) CryptokiException(org.openecard.mdlw.sal.exceptions.CryptokiException)

Example 3 with CryptokiException

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

the class MiddlewareSAL method cardApplicationConnect.

@Override
public CardApplicationConnectResponse cardApplicationConnect(CardApplicationConnect request) {
    CardApplicationConnectResponse response = WSHelper.makeResponse(CardApplicationConnectResponse.class, WSHelper.makeResultOK());
    try {
        CardApplicationPathType cardAppPath = request.getCardApplicationPath();
        Assert.assertIncorrectParameter(cardAppPath, "The parameter CardAppPathRequest is empty.");
        Set<CardStateEntry> cardStateEntrySet = states.getMatchingEntries(cardAppPath, false);
        Assert.assertIncorrectParameter(cardStateEntrySet, "The given ConnectionHandle is invalid.");
        /*
	     * [TR-03112-4] If the provided path fragments are valid for more than one card application
	     * the eCard-API-Framework SHALL return any of the possible choices.
             */
        CardStateEntry cardStateEntry = cardStateEntrySet.iterator().next();
        ConnectionHandleType handle = cardStateEntry.handleCopy();
        cardStateEntry = cardStateEntry.derive(handle);
        byte[] applicationID = cardStateEntry.getImplicitlySelectedApplicationIdentifier();
        Assert.securityConditionApplication(cardStateEntry, applicationID, ConnectionServiceActionName.CARD_APPLICATION_CONNECT);
        // find matching slot and associate it with the slotHandle
        MwSlot slot = getMatchingSlot(handle.getIFDName(), handle.getSlotIndex());
        if (slot != null) {
            // open session
            MwSession session = slot.openSession();
            // save values in maps
            byte[] slotHandle = ValueGenerators.generateRandom(64);
            handle.setSlotHandle(slotHandle);
            managedSlots.put(slotHandle, slot);
            managedSessions.put(slotHandle, session);
        } else {
            throw new IncorrectParameterException("No slot found for requestet handle.");
        }
        cardStateEntry.setSlotHandle(handle.getSlotHandle());
        // reset the ef FCP
        cardStateEntry.unsetFCPOfSelectedEF();
        states.addEntry(cardStateEntry);
        response.setConnectionHandle(cardStateEntry.handleCopy());
        response.getConnectionHandle().setCardApplication(applicationID);
    } catch (ECardException e) {
        response.setResult(e.getResult());
    } catch (CryptokiException ex) {
        String msg = "Error in Middleware.";
        LOG.error(msg, ex);
        response.setResult(WSHelper.makeResultError(ECardConstants.Minor.Disp.COMM_ERROR, msg));
    }
    return response;
}
Also used : CardApplicationPathType(iso.std.iso_iec._24727.tech.schema.CardApplicationPathType) ConnectionHandleType(iso.std.iso_iec._24727.tech.schema.ConnectionHandleType) ECardException(org.openecard.common.ECardException) CardStateEntry(org.openecard.common.sal.state.CardStateEntry) CryptokiException(org.openecard.mdlw.sal.exceptions.CryptokiException) IncorrectParameterException(org.openecard.common.sal.exception.IncorrectParameterException) CardApplicationConnectResponse(iso.std.iso_iec._24727.tech.schema.CardApplicationConnectResponse)

Example 4 with CryptokiException

use of org.openecard.mdlw.sal.exceptions.CryptokiException 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 5 with CryptokiException

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

the class MwStateCallback method addEntry.

public boolean addEntry(MwEventObject o) {
    try {
        ConnectionHandleType handle = o.getHandle();
        MwSlot slot = o.getMwSlot();
        MwToken token = slot.getTokenInfo();
        String cardType = null;
        String type = String.format("%s_%s", token.getManufacturerID(), token.getModel());
        for (MiddlewareConfig mwConfig : mwConfigs) {
            cardType = mwConfig.mapMiddlewareType(type);
            if (cardType != null) {
                break;
            }
        }
        CardInfoType cif = null;
        if (cardType != null) {
            cif = env.getCIFProvider().getCardInfo(handle, cardType);
        }
        if (cif == null) {
            LOG.warn("Unknown card recognized by Middleware.");
            return false;
        }
        // create new entry in card states
        CardStateEntry entry = new CardStateEntry(handle, cif, null);
        states.addEntry(entry);
        return true;
    } catch (CryptokiException ex) {
        LOG.info("Cryptoki Token invalid.", ex);
    } catch (RuntimeException ex) {
        LOG.error("Error in CIF augmentation process.", ex);
    }
    return false;
}
Also used : ConnectionHandleType(iso.std.iso_iec._24727.tech.schema.ConnectionHandleType) MwSlot(org.openecard.mdlw.sal.MwSlot) CardStateEntry(org.openecard.common.sal.state.CardStateEntry) CardInfoType(iso.std.iso_iec._24727.tech.schema.CardInfoType) CryptokiException(org.openecard.mdlw.sal.exceptions.CryptokiException) MiddlewareConfig(org.openecard.mdlw.sal.config.MiddlewareConfig) MwToken(org.openecard.mdlw.sal.MwToken)

Aggregations

CryptokiException (org.openecard.mdlw.sal.exceptions.CryptokiException)24 ArrayList (java.util.ArrayList)7 NativeLong (com.sun.jna.NativeLong)5 ConnectionHandleType (iso.std.iso_iec._24727.tech.schema.ConnectionHandleType)5 PinBlockedException (org.openecard.mdlw.sal.exceptions.PinBlockedException)5 PinIncorrectException (org.openecard.mdlw.sal.exceptions.PinIncorrectException)5 TokenException (org.openecard.mdlw.sal.exceptions.TokenException)5 NativeLongByReference (com.sun.jna.ptr.NativeLongByReference)4 DIDInfoType (iso.std.iso_iec._24727.tech.schema.DIDInfoType)4 CardStateEntry (org.openecard.common.sal.state.CardStateEntry)4 UnsupportedAlgorithmException (org.openecard.crypto.common.UnsupportedAlgorithmException)4 CK_ATTRIBUTE (org.openecard.mdlw.sal.cryptoki.CK_ATTRIBUTE)4 InitializationException (org.openecard.mdlw.sal.exceptions.InitializationException)4 ECardException (org.openecard.common.ECardException)3 ThreadTerminateException (org.openecard.common.ThreadTerminateException)3 IncorrectParameterException (org.openecard.common.sal.exception.IncorrectParameterException)3 WSMarshallerException (org.openecard.ws.marshal.WSMarshallerException)3 AccessControlListType (iso.std.iso_iec._24727.tech.schema.AccessControlListType)2 AccessRuleType (iso.std.iso_iec._24727.tech.schema.AccessRuleType)2 CardInfoType (iso.std.iso_iec._24727.tech.schema.CardInfoType)2