use of de.flexiprovider.api.keys.PrivateKey in project core by jcryptool.
the class SignatureEngine method init.
@Override
public KeyObject init(IFlexiProviderOperation operation) {
// $NON-NLS-1$
LogUtil.logInfo("initializing signature engine");
this.operation = operation;
char[] password = null;
KeyObject usedKey = null;
try {
signature = Registry.getSignature(operation.getAlgorithmDescriptor().getAlgorithmName());
AlgorithmParameterSpec spec = operation.getAlgorithmDescriptor().getAlgorithmParameterSpec();
if (spec != null) {
signature.setParameters(spec);
}
if (operation.getOperation().equals(OperationType.SIGN)) {
if (operation.getPassword() != null) {
password = operation.getPassword();
} else {
password = promptPassword();
}
if (password == null) {
return null;
}
Key privateKey = (Key) KeyStoreManager.getInstance().getPrivateKey(operation.getKeyStoreAlias(), password);
signature.initSign((PrivateKey) privateKey, FlexiProviderEnginesPlugin.getSecureRandom());
usedKey = new KeyObject(privateKey, password);
// save in the operation if no exception occurred
operation.setPassword(password);
} else {
Certificate certificate = KeyStoreManager.getInstance().getCertificate(operation.getKeyStoreAlias());
Key publicKey = (Key) certificate.getPublicKey();
signature.initVerify((PublicKey) publicKey);
usedKey = new KeyObject(publicKey, password);
}
initialized = true;
} catch (NoSuchAlgorithmException e) {
LogUtil.logError(FlexiProviderEnginesPlugin.PLUGIN_ID, "NoSuchAlgorithmException while initializing a signature", e, // $NON-NLS-1$
true);
return null;
} catch (InvalidAlgorithmParameterException e) {
LogUtil.logError(FlexiProviderEnginesPlugin.PLUGIN_ID, "InvalidAlgorithmParameterException while initializing a signature", e, // $NON-NLS-1$
true);
return null;
} catch (InvalidKeyException e) {
LogUtil.logError(FlexiProviderEnginesPlugin.PLUGIN_ID, Messages.SignatureEngine_5, e, true);
return null;
} catch (UnrecoverableEntryException e) {
JCTMessageDialog.showInfoDialog(new Status(IStatus.INFO, FlexiProviderEnginesPlugin.PLUGIN_ID, Messages.ExAccessKeystorePassword, e));
return null;
} catch (Exception e) {
// $NON-NLS-1$
LogUtil.logError(FlexiProviderEnginesPlugin.PLUGIN_ID, "Exception while initializing a signature", e, true);
return null;
}
return usedKey;
}
use of de.flexiprovider.api.keys.PrivateKey in project core by jcryptool.
the class KeyStoreHelper method makeKeyPairByWizard.
public static KeyStoreAliasNotifier makeKeyPairByWizard(String keyType) {
// $NON-NLS-1$
LogUtil.logInfo("NewKeyPairAction");
Shell shell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();
Wizard wizard = new NewKeyPairWizard(keyType);
WizardDialog dialog = new WizardDialog(shell, wizard);
dialog.setMinimumPageSize(300, 350);
final KeyStoreAliasNotifier resultAlias = new KeyStoreAliasNotifier();
int result = dialog.open();
if (result == Window.OK) {
if (wizard instanceof INewKeyWizard) {
final INewEntryDescriptor nkd = ((INewKeyWizard) wizard).getNewEntryDescriptor();
final Integer[] argument = new Integer[1];
final Integer arg = nkd.getKeyLength();
argument[0] = arg;
final Integer keyLen = argument[0];
// $NON-NLS-1$
LogUtil.logInfo("nkd.getKeyLength: " + argument[0]);
Job job = new // $NON-NLS-1$
Job(// $NON-NLS-1$
"New Key Pair Job") {
@Override
protected IStatus run(IProgressMonitor monitor) {
// $NON-NLS-1$
monitor.beginTask("New KeyPair Task", IProgressMonitor.UNKNOWN);
try {
IMetaKeyGenerator gen = AlgorithmsXMLManager.getInstance().getKeyPairGenerator(nkd.getAlgorithmName());
IMetaLength validKeyLengths = gen.getLengths();
// Check if entered key length is valid
boolean isValidKeyLength = true;
if (validKeyLengths != null) {
isValidKeyLength = (validKeyLengths.getDefaultLength() == keyLen) || (keyLen >= validKeyLengths.getLowerBound() && keyLen <= validKeyLengths.getUpperBound()) || (validKeyLengths.getLengths() != null && validKeyLengths.getLengths().contains(keyLen));
}
if (!isValidKeyLength) {
throw new InvalidAlgorithmParameterException("illegal key length");
}
AlgorithmParameterSpec spec = null;
if (arg != -1) {
if (gen.getParameterSpecClassName() != null) {
spec = Reflector.getInstance().instantiateParameterSpec(gen.getParameterSpecClassName(), argument);
}
}
KeyPairGenerator generator = Registry.getKeyPairGenerator(nkd.getAlgorithmName());
if (spec != null) {
generator.initialize(spec, FlexiProviderKeystorePlugin.getSecureRandom());
} else if (arg != -1) {
generator.initialize(arg, FlexiProviderKeystorePlugin.getSecureRandom());
}
KeyPair keyPair = generator.genKeyPair();
PrivateKey priv = keyPair.getPrivate();
PublicKey pub = keyPair.getPublic();
NewKeyPairDescriptor descriptor = new NewKeyPairDescriptor(nkd, priv, pub);
resultAlias.notifyAboutAlias(AbstractKeyStoreHandler.addKeyPairStatic(descriptor, ((NewKeyPairDescriptor) descriptor).getPrivateKey(), ((NewKeyPairDescriptor) descriptor).getPublicKey()));
} catch (NoSuchAlgorithmException e) {
LogUtil.logError(FlexiProviderKeystorePlugin.PLUGIN_ID, "NoSuchAlgorithmException while generating a key pair", e, true);
} catch (InvalidAlgorithmParameterException e) {
LogUtil.logError(FlexiProviderKeystorePlugin.PLUGIN_ID, "InvalidAlgorithmParameterException while generating a key pair", e, true);
} catch (SecurityException e) {
LogUtil.logError(FlexiProviderKeystorePlugin.PLUGIN_ID, "SecurityException while generating a key pair", e, true);
} catch (IllegalArgumentException e) {
LogUtil.logError(FlexiProviderKeystorePlugin.PLUGIN_ID, "IllegalArgumentException while generating a key pair", e, true);
} catch (ClassNotFoundException e) {
LogUtil.logError(FlexiProviderKeystorePlugin.PLUGIN_ID, "ClassNotFoundException while generating a key pair", e, true);
} catch (NoSuchMethodException e) {
LogUtil.logError(FlexiProviderKeystorePlugin.PLUGIN_ID, "NoSuchMethodException while generating a key pair", e, true);
} catch (InstantiationException e) {
LogUtil.logError(FlexiProviderKeystorePlugin.PLUGIN_ID, "InstantiationException while generating a key pair", e, true);
} catch (IllegalAccessException e) {
LogUtil.logError(FlexiProviderKeystorePlugin.PLUGIN_ID, "IllegalAccessException while generating a key pair", e, true);
} catch (InvocationTargetException e) {
LogUtil.logError(FlexiProviderKeystorePlugin.PLUGIN_ID, "InvocationTargetException while generating a key pair", e, true);
} finally {
monitor.done();
}
return Status.OK_STATUS;
}
@Override
public boolean belongsTo(Object family) {
return family == KEYSTOREHELPER_FAMILY;
}
};
job.setPriority(Job.LONG);
job.setUser(true);
job.schedule();
}
} else {
resultAlias.notifyAboutAlias(null);
}
return resultAlias;
}
use of de.flexiprovider.api.keys.PrivateKey in project core by jcryptool.
the class NewKeyPairHandler method execute.
/**
* @see org.eclipse.jface.action.Action#run()
*/
public Object execute(ExecutionEvent event) {
// $NON-NLS-1$
LogUtil.logInfo("NewKeyPairAction");
shell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();
Wizard wizard = new NewKeyPairWizard();
dialog = new WizardDialog(shell, wizard);
dialog.setMinimumPageSize(300, 350);
int result = dialog.open();
if (result == Window.OK) {
if (wizard instanceof INewKeyWizard) {
final INewEntryDescriptor nkd = ((INewKeyWizard) wizard).getNewEntryDescriptor();
final Integer[] argument = new Integer[1];
final Integer arg = nkd.getKeyLength();
argument[0] = arg;
final Integer keyLen = argument[0];
// $NON-NLS-1$
LogUtil.logInfo("nkd.getKeyLength: " + argument[0]);
Job job = new Job(Messages.NewKeyPairHandler_2) {
@Override
protected IStatus run(IProgressMonitor monitor) {
monitor.beginTask(Messages.NewKeyPairHandler_3, IProgressMonitor.UNKNOWN);
try {
IMetaKeyGenerator gen = AlgorithmsXMLManager.getInstance().getKeyPairGenerator(nkd.getAlgorithmName());
IMetaLength validKeyLengths = gen.getLengths();
// Check if entered key length is valid
boolean isValidKeyLength = true;
if (validKeyLengths != null) {
isValidKeyLength = (validKeyLengths.getDefaultLength() == keyLen) || (keyLen >= validKeyLengths.getLowerBound() && keyLen <= validKeyLengths.getUpperBound()) || (validKeyLengths.getLengths() != null && validKeyLengths.getLengths().contains(keyLen));
}
if (!isValidKeyLength) {
// $NON-NLS-1$
throw new InvalidAlgorithmParameterException("illegal key length");
}
AlgorithmParameterSpec spec = null;
if (arg != -1) {
if (gen.getParameterSpecClassName() != null) {
spec = Reflector.getInstance().instantiateParameterSpec(gen.getParameterSpecClassName(), argument);
}
}
KeyPairGenerator generator = Registry.getKeyPairGenerator(nkd.getAlgorithmName());
if (spec != null) {
generator.initialize(spec, FlexiProviderKeystorePlugin.getSecureRandom());
} else if (arg != -1) {
generator.initialize(arg, FlexiProviderKeystorePlugin.getSecureRandom());
}
KeyPair keyPair = generator.genKeyPair();
PrivateKey priv = keyPair.getPrivate();
PublicKey pub = keyPair.getPublic();
performNewKeyAction(new NewKeyPairDescriptor(nkd, priv, pub));
} catch (NoSuchAlgorithmException e) {
LogUtil.logError(FlexiProviderKeystorePlugin.PLUGIN_ID, "NoSuchAlgorithmException while generating a key pair", e, // $NON-NLS-1$
true);
} catch (InvalidAlgorithmParameterException e) {
LogUtil.logError(FlexiProviderKeystorePlugin.PLUGIN_ID, "InvalidAlgorithmParameterException while generating a key pair", e, // $NON-NLS-1$
true);
} catch (SecurityException e) {
LogUtil.logError(FlexiProviderKeystorePlugin.PLUGIN_ID, "SecurityException while generating a key pair", e, // $NON-NLS-1$
true);
} catch (IllegalArgumentException e) {
LogUtil.logError(FlexiProviderKeystorePlugin.PLUGIN_ID, "IllegalArgumentException while generating a key pair", e, // $NON-NLS-1$
true);
} catch (ClassNotFoundException e) {
LogUtil.logError(FlexiProviderKeystorePlugin.PLUGIN_ID, "ClassNotFoundException while generating a key pair", e, // $NON-NLS-1$
true);
} catch (NoSuchMethodException e) {
LogUtil.logError(FlexiProviderKeystorePlugin.PLUGIN_ID, "NoSuchMethodException while generating a key pair", e, // $NON-NLS-1$
true);
} catch (InstantiationException e) {
LogUtil.logError(FlexiProviderKeystorePlugin.PLUGIN_ID, "InstantiationException while generating a key pair", e, // $NON-NLS-1$
true);
} catch (IllegalAccessException e) {
LogUtil.logError(FlexiProviderKeystorePlugin.PLUGIN_ID, "IllegalAccessException while generating a key pair", e, // $NON-NLS-1$
true);
} catch (InvocationTargetException e) {
LogUtil.logError(FlexiProviderKeystorePlugin.PLUGIN_ID, "InvocationTargetException while generating a key pair", e, // $NON-NLS-1$
true);
} finally {
monitor.done();
}
return Status.OK_STATUS;
}
};
job.setPriority(Job.LONG);
// job.setUser(true);
job.schedule();
}
}
return (null);
}
Aggregations