Search in sources :

Example 16 with CryptokiException

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

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

the class MiddlewareSAL method updatePin.

private Result updatePin(DIDUpdateDataType didUpdateData, CardStateEntry cardStateEntry, DIDStructureType didStruct) {
    // make sure the pin is not entered already
    setPinNotAuth(cardStateEntry);
    ConnectionHandleType connectionHandle = cardStateEntry.handleCopy();
    MwSession session = managedSessions.get(connectionHandle.getSlotHandle());
    boolean protectedAuthPath = connectionHandle.getSlotInfo().isProtectedAuthPath();
    try {
        PinChangeDialog dialog = new PinChangeDialog(gui, protectedAuthPath, session);
        dialog.show();
    } catch (CryptokiException ex) {
        return WSHelper.makeResultUnknownError(ex.getMessage());
    }
    return WSHelper.makeResultOK();
}
Also used : ConnectionHandleType(iso.std.iso_iec._24727.tech.schema.ConnectionHandleType) CryptokiException(org.openecard.mdlw.sal.exceptions.CryptokiException)

Example 18 with CryptokiException

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

the class MiddlewareSAL method sign.

@Override
public SignResponse sign(Sign request) {
    SignResponse response = WSHelper.makeResponse(SignResponse.class, WSHelper.makeResultOK());
    try {
        ConnectionHandleType connectionHandle = SALUtils.getConnectionHandle(request);
        CardStateEntry cardStateEntry = SALUtils.getCardStateEntry(states, connectionHandle, false);
        byte[] application = cardStateEntry.getImplicitlySelectedApplicationIdentifier();
        byte[] slotHandle = connectionHandle.getSlotHandle();
        String didName = SALUtils.getDIDName(request);
        byte[] message = request.getMessage();
        Assert.assertIncorrectParameter(message, "The parameter Message is empty.");
        DIDStructureType didStructure = cardStateEntry.getDIDStructure(didName, application);
        Assert.assertNamedEntityNotFound(didStructure, "The given DIDName cannot be found.");
        CryptoMarkerType marker = new CryptoMarkerType(didStructure.getDIDMarker());
        String keyLabel = marker.getLegacyKeyName();
        MwSession session = managedSessions.get(slotHandle);
        for (MwPrivateKey key : session.getPrivateKeys()) {
            String nextLabel = "";
            try {
                nextLabel = key.getKeyLabel();
            } catch (CryptokiException ex) {
                LOG.warn("Error reading key label.", ex);
            }
            LOG.debug("Try to match keys '{}' == '{}'", keyLabel, nextLabel);
            if (keyLabel.equals(nextLabel)) {
                long sigAlg = getPKCS11Alg(marker.getAlgorithmInfo());
                byte[] sig = key.sign(sigAlg, message);
                response.setSignature(sig);
                // set PIN to unauthenticated
                setPinNotAuth(cardStateEntry);
                return response;
            }
        }
        // TODO: use other exception
        String msg = String.format("The given DIDName %s references an unknown key.", didName);
        throw new IncorrectParameterException(msg);
    } catch (ECardException e) {
        LOG.debug(e.getMessage(), 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) CardStateEntry(org.openecard.common.sal.state.CardStateEntry) CryptoMarkerType(org.openecard.crypto.common.sal.did.CryptoMarkerType) 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) ECardException(org.openecard.common.ECardException) SignResponse(iso.std.iso_iec._24727.tech.schema.SignResponse) CryptokiException(org.openecard.mdlw.sal.exceptions.CryptokiException) IncorrectParameterException(org.openecard.common.sal.exception.IncorrectParameterException) DIDStructureType(iso.std.iso_iec._24727.tech.schema.DIDStructureType)

Example 19 with CryptokiException

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

the class MwModule method initialize.

/**
 * Initializes the Cryptoki library.
 *
 * @throws UnsatisfiedLinkError Thrown in case the library could not be found.
 * @throws InitializationException Thrown in case the Middleware could not be initialized.
 */
public void initialize() throws UnsatisfiedLinkError, InitializationException {
    try {
        mw = new MiddleWareWrapper(mwSALConfig);
        mw.initialize();
    } catch (CryptokiException ex) {
        throw new InitializationException("Failed to initalize PKCS#11 middleware.", ex.getErrorCode());
    }
}
Also used : CryptokiException(org.openecard.mdlw.sal.exceptions.CryptokiException) InitializationException(org.openecard.mdlw.sal.exceptions.InitializationException)

Example 20 with CryptokiException

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

the class MwModule method getSlotList.

/**
 * Obtains a list of slots in the system.
 *
 * @param tokenPresent When {@code true}, only return slots with a present token.
 * @return
 * @throws CryptokiException
 */
public List<MwSlot> getSlotList(boolean tokenPresent) throws CryptokiException {
    ArrayList<MwSlot> slots = new ArrayList<>();
    for (long slotId : mw.getSlotList(tokenPresent)) {
        try {
            CkSlot slotInfo = mw.getSlotInfo(slotId);
            MwSlot slot = new MwSlot(mw, this, slotInfo);
            slots.add(slot);
        } catch (CryptokiException ex) {
            long code = ex.getErrorCode();
            if (!(code == CryptokiLibrary.CKR_DEVICE_ERROR || code == CryptokiLibrary.CKR_TOKEN_NOT_PRESENT || code == CryptokiLibrary.CKR_DEVICE_REMOVED)) {
                // unrecoverable error
                throw ex;
            }
            LOG.info("Skipping slot {} due to recoverable error {}.", slotId, code);
        }
    }
    return Collections.unmodifiableList(slots);
}
Also used : CryptokiException(org.openecard.mdlw.sal.exceptions.CryptokiException) ArrayList(java.util.ArrayList) CkSlot(org.openecard.mdlw.sal.struct.CkSlot)

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