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);
}
}
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();
}
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;
}
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());
}
}
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);
}
Aggregations