use of org.jcryptool.crypto.flexiprovider.descriptors.meta.interfaces.IMetaKeyGenerator in project core by jcryptool.
the class NewKeyPairWizardPage method showLengths.
private void showLengths(String name) {
keyStrengthCombo.removeAll();
keyStrengthCombo.setEditable(true);
IMetaKeyGenerator keyGen = AlgorithmsXMLManager.getInstance().getKeyPairGenerator(name);
if (keyGen.getLengths() != null) {
int defaultLength = keyGen.getLengths().getDefaultLength();
if (keyGen.getLengths().getLengths() != null) {
List<Integer> lengths = keyGen.getLengths().getLengths();
Iterator<Integer> it = lengths.iterator();
while (it.hasNext()) {
keyStrengthCombo.add(String.valueOf(it.next()));
}
keyStrengthCombo.setText(String.valueOf(defaultLength));
keyStrengthCombo.setEditable(false);
} else if (keyGen.getLengths().getLowerBound() >= 0 && keyGen.getLengths().getUpperBound() > 0) {
int lowerBound = keyGen.getLengths().getLowerBound();
int upperBound = keyGen.getLengths().getUpperBound();
// $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
String display = "(" + String.valueOf(lowerBound) + " - " + String.valueOf(upperBound) + ")";
keyStrengthCombo.add(display);
keyStrengthCombo.setText(String.valueOf(defaultLength));
} else {
keyStrengthCombo.add(String.valueOf(defaultLength));
keyStrengthCombo.setText(String.valueOf(defaultLength));
keyStrengthCombo.setEditable(false);
}
}
}
use of org.jcryptool.crypto.flexiprovider.descriptors.meta.interfaces.IMetaKeyGenerator in project core by jcryptool.
the class NewSymmetricKeyWizardPage method initAlgorithmsCombo.
private void initAlgorithmsCombo() {
secretKeyGenerators = AlgorithmsXMLManager.getInstance().getSecretKeyGenerators();
Iterator<IMetaKeyGenerator> it = secretKeyGenerators.iterator();
List<String> generators = new LinkedList<String>();
while (it.hasNext()) {
IMetaKeyGenerator current = it.next();
// $NON-NLS-1$
String allNames = "";
Iterator<String> namesIt = current.getNames().iterator();
while (namesIt.hasNext()) {
// $NON-NLS-1$
allNames += namesIt.next() + ", ";
}
allNames = allNames.substring(0, allNames.length() - 2);
String generatorId = allNames;
if (current.getOID() != null) {
// $NON-NLS-1$ //$NON-NLS-2$
generatorId += " (OID: " + current.getOID().getStringOID() + ")";
}
if (KeyStoreAlias.isOperationMatchingKeyId(generatorId, keyType)) {
generators.add(generatorId);
}
}
Collections.sort(generators);
algorithmCombo.setItems(generators.toArray(new String[0]));
algorithmCombo.select(0);
algorithmCombo.setVisibleItemCount(10);
}
use of org.jcryptool.crypto.flexiprovider.descriptors.meta.interfaces.IMetaKeyGenerator in project core by jcryptool.
the class FlexiProviderRootElement method initKeyGenerators.
@SuppressWarnings("unchecked")
private static void initKeyGenerators(RegistryType type, Element keyGeneratorParent) {
List<Element> keyGeneratorChildren = new ArrayList<Element>(keyGeneratorParent.getChildren());
Iterator<Element> it = keyGeneratorChildren.iterator();
while (it.hasNext()) {
Element current = it.next();
String className = current.getAttributeValue(AlgorithmsXMLConstants._class);
IMetaKeyGenerator meta = new MetaKeyGenerator(className);
// oid
IMetaOID oid = null;
if (current.getAttributeValue(AlgorithmsXMLConstants._oid) != null) {
oid = new MetaOID(current.getAttributeValue(AlgorithmsXMLConstants._oid));
meta.setOID(oid);
}
// names
List<String> names = new ArrayList<String>();
if (current.getChild(AlgorithmsXMLConstants._Names) != null) {
if (current.getChild(AlgorithmsXMLConstants._Names).getText().contains(",")) {
// $NON-NLS-1$
StringTokenizer tokenizer = // $NON-NLS-1$
new StringTokenizer(current.getChild(AlgorithmsXMLConstants._Names).getText(), ",");
while (tokenizer.hasMoreTokens()) {
names.add(tokenizer.nextToken());
}
} else {
names.add(current.getChild(AlgorithmsXMLConstants._Names).getText());
}
}
meta.setNames(names);
// parameter spec
if (current.getChild(AlgorithmsXMLConstants._ParameterSpec) != null) {
meta.setParameterSpecClassName(current.getChild(AlgorithmsXMLConstants._ParameterSpec).getAttributeValue(AlgorithmsXMLConstants._class));
}
// parameter generators
if (current.getChild(AlgorithmsXMLConstants._ParameterGenerator) != null) {
meta.setParameterGeneratorClassName(current.getChild(AlgorithmsXMLConstants._ParameterGenerator).getAttributeValue(AlgorithmsXMLConstants._class));
meta.setParamGenParameterSpecClassName(current.getChild(AlgorithmsXMLConstants._ParameterGenerator).getChild(AlgorithmsXMLConstants._ParameterSpec).getAttributeValue(AlgorithmsXMLConstants._class));
}
// lengths
if (current.getChild(AlgorithmsXMLConstants._KeyStrengths) != null) {
int defaultLength = Integer.valueOf(current.getChild(AlgorithmsXMLConstants._KeyStrengths).getAttributeValue(AlgorithmsXMLConstants._default));
IMetaLength metaLength = new MetaLength(defaultLength);
if (current.getChild(AlgorithmsXMLConstants._KeyStrengths).getText() != null) {
String text = current.getChild(AlgorithmsXMLConstants._KeyStrengths).getText();
if (text.contains(",")) {
// $NON-NLS-1$
List<Integer> lengths = new ArrayList<Integer>(2);
// $NON-NLS-1$
StringTokenizer tokenizer = new StringTokenizer(text, ",");
while (tokenizer.hasMoreTokens()) {
lengths.add(Integer.valueOf(tokenizer.nextToken()));
}
metaLength.setLengths(lengths);
} else if (text.contains("...")) {
// $NON-NLS-1$
// $NON-NLS-1$
int lowerBound = Integer.valueOf(text.substring(0, text.indexOf("...")));
// $NON-NLS-1$
int upperBound = Integer.valueOf(text.substring(text.indexOf("...") + 3, text.length()));
metaLength.setBounds(lowerBound, upperBound);
}
}
meta.setLengths(metaLength);
}
add(type, meta);
}
}
use of org.jcryptool.crypto.flexiprovider.descriptors.meta.interfaces.IMetaKeyGenerator in project core by jcryptool.
the class KeyStoreHelper method makeSymmetricKeyByWizard.
public static KeyStoreAliasNotifier makeSymmetricKeyByWizard(String keyType) {
// $NON-NLS-1$
LogUtil.logInfo("NewSymmetricKeyAction");
Shell shell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();
Wizard wizard = new NewSymmetricKeyWizard(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];
argument[0] = nkd.getKeyLength();
final Integer keyLen = argument[0];
// $NON-NLS-1$
LogUtil.logInfo("key strength: " + argument[0]);
Job job = new // $NON-NLS-1$
Job(// $NON-NLS-1$
"New SecretKey Job") {
@Override
protected IStatus run(IProgressMonitor monitor) {
// $NON-NLS-1$
monitor.beginTask("New SecretKey Task", IProgressMonitor.UNKNOWN);
try {
IMetaKeyGenerator gen = AlgorithmsXMLManager.getInstance().getSecretKeyGenerator(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 (gen.getParameterSpecClassName() != null) {
spec = Reflector.getInstance().instantiateParameterSpec(gen.getParameterSpecClassName(), argument);
}
SecretKeyGenerator generator = Registry.getSecretKeyGenerator(nkd.getAlgorithmName());
if (spec != null) {
// $NON-NLS-1$
LogUtil.logInfo("initializing generator with spec");
generator.init(spec, FlexiProviderKeystorePlugin.getSecureRandom());
} else {
generator.init(FlexiProviderKeystorePlugin.getSecureRandom());
}
SecretKey key = generator.generateKey();
INewEntryDescriptor descriptor = new NewSecretKeyDescriptor(nkd, key);
resultAlias.notifyAboutAlias(AbstractKeyStoreHandler.addSecretKeyStatic(descriptor, ((NewSecretKeyDescriptor) descriptor).getSecretKey()));
} catch (SecurityException e) {
LogUtil.logError(FlexiProviderKeystorePlugin.PLUGIN_ID, "SecurityException while generating a secret key", e, true);
} catch (IllegalArgumentException e) {
LogUtil.logError(FlexiProviderKeystorePlugin.PLUGIN_ID, "IllegalArgumentException while generating a secret key", e, true);
} catch (ClassNotFoundException e) {
LogUtil.logError(FlexiProviderKeystorePlugin.PLUGIN_ID, "ClassNotFoundException while generating a secret key", e, true);
} catch (NoSuchMethodException e) {
LogUtil.logError(FlexiProviderKeystorePlugin.PLUGIN_ID, "NoSuchMethodException while generating a secret key", e, true);
} catch (InstantiationException e) {
LogUtil.logError(FlexiProviderKeystorePlugin.PLUGIN_ID, "InstantiationException while generating a secret key", e, true);
} catch (IllegalAccessException e) {
LogUtil.logError(FlexiProviderKeystorePlugin.PLUGIN_ID, "IllegalAccessException while generating a secret key", e, true);
} catch (InvocationTargetException e) {
LogUtil.logError(FlexiProviderKeystorePlugin.PLUGIN_ID, "InvocationTargetException while generating a secret key", e, true);
} catch (NoSuchAlgorithmException e) {
LogUtil.logError(FlexiProviderKeystorePlugin.PLUGIN_ID, "NoSuchAlgorithmException while generating a secret key", e, true);
} catch (InvalidAlgorithmParameterException e) {
LogUtil.logError(FlexiProviderKeystorePlugin.PLUGIN_ID, "InvalidAlgorithmParameterException while generating a secret key", e, true);
}
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 org.jcryptool.crypto.flexiprovider.descriptors.meta.interfaces.IMetaKeyGenerator in project core by jcryptool.
the class IntegratorHandler method execute.
/**
* runs the action: setup the algorithm and executed the specified operation
*/
@Override
public Object execute(ExecutionEvent event) {
AlgorithmDescriptor descriptor = getAlgorithmDescriptor(FLEXIPROVIDER_ALGORITHM_NAME);
if (descriptor == null) {
MessageBox messageBox = new MessageBox(getActiveWorkbenchWindow().getShell());
// $NON-NLS-1$
messageBox.setText(Messages.getString("DummyAction.error"));
// $NON-NLS-1$
messageBox.setMessage(Messages.getString("DummyAction.8"));
messageBox.open();
return (null);
}
String readableNameExtension;
switch(algorithmType) {
case TYPE_RANDOM_NUMBER_GENERATOR:
// $NON-NLS-1$
readableNameExtension = Messages.getString("DummyAction.random_number_generator");
break;
case TYPE_SIGNATURE:
// $NON-NLS-1$
readableNameExtension = Messages.getString("DummyAction.signature");
break;
case TYPE_MESSAGE_DIGEST:
// $NON-NLS-1$
readableNameExtension = Messages.getString("DummyAction.message_digest");
break;
case TYPE_MESSAGE_AUTHTIFICATION_CODE:
// $NON-NLS-1$
readableNameExtension = Messages.getString("DummyAction.message_authentification_code");
break;
default:
// $NON-NLS-1$
readableNameExtension = Messages.getString("DummyAction.encryption");
}
// Get which key lengths are valid for this algorithm
int[] validKeyLengths = null;
IMetaKeyGenerator keyGen = AlgorithmsXMLManager.getInstance().getSecretKeyGenerator(FLEXIPROVIDER_ALGORITHM_NAME);
if (keyGen != null) {
if (keyGen.getLengths().getLengths() != null) {
validKeyLengths = new int[keyGen.getLengths().getLengths().size()];
for (int i = 0; i < validKeyLengths.length; i++) validKeyLengths[i] = keyGen.getLengths().getLengths().get(i);
} else {
validKeyLengths = new int[1];
validKeyLengths[0] = keyGen.getLengths().getDefaultLength();
}
}
wizard = new IntegratorWizard(READABLE_ALGORITHM_NAME + readableNameExtension, READABLE_ALGORITHM_NAME, HEADER_DESCRIPTION, SHOW_OPERATION_GROUP, SHOW_PADDING_GROUP, SHOW_KEY, SHOW_KEY_SOURCE_GROUP, validKeyLengths, SHOW_SIGNATURE_GROUP, SHOW_RANDOM_GROUP, SHOW_MESSAGE_DIGEST_GROUP, algorithmType);
WizardDialog dialog = new WizardDialog(getActiveWorkbenchWindow().getShell(), wizard);
dialog.setHelpAvailable(true);
switch(dialog.open()) {
case Window.OK:
if (algorithmType == TYPE_CIPHER_BLOCK) {
descriptor = new BlockCipherDescriptor(descriptor.getAlgorithmName(), AlgorithmsXMLManager.getInstance().getMode(wizard.getMode().getDescription()).getID(), AlgorithmsXMLManager.getInstance().getPaddingScheme(wizard.getPadding().getPaddingSchemeName()).getID(), null, descriptor.getAlgorithmParameterSpec());
}
if (algorithmType == TYPE_RANDOM_NUMBER_GENERATOR) {
if (wizard.doFilter())
descriptor = new SecureRandomDescriptor(descriptor.getAlgorithmName(), wizard.getRandomSize(), wizard.getFilter());
else
descriptor = new SecureRandomDescriptor(descriptor.getAlgorithmName(), wizard.getRandomSize());
}
IntegratorOperation operation = new IntegratorOperation(descriptor);
// $NON-NLS-1$
operation.setEntryName("");
// $NON-NLS-1$
operation.setInput(Messages.getString("InputType"));
// $NON-NLS-1$
operation.setOutput("<Editor>");
operation.setSignature(wizard.signature());
operation.setUseCustomKey(wizard.useCustomKey());
if (wizard.useCustomKey())
operation.setKeyBytes(wizard.getCustomKey());
try {
if (// $NON-NLS-1$
SHOW_KEY != null && !SHOW_KEY.equals("") && !wizard.useCustomKey())
operation.setKeyStoreAlias(wizard.getKey());
if (wizard.encrypt()) {
// explicit encrypt
if (descriptor.getType() == RegistryType.SIGNATURE) {
operation.setOperation(OperationType.VERIFY);
} else {
operation.setOperation(OperationType.ENCRYPT);
}
} else {
// implicit decrypt
if (descriptor.getType() == RegistryType.SIGNATURE) {
operation.setOperation(OperationType.SIGN);
} else {
operation.setOperation(OperationType.DECRYPT);
}
}
if (SHOW_MESSAGE_DIGEST_GROUP > 0 && !wizard.encrypt()) {
try {
MessageDigest digest = Registry.getMessageDigest(operation.getAlgorithmDescriptor().getAlgorithmName());
InputStream inputStream = EditorsManager.getInstance().getActiveEditorContentInputStream();
int i;
while ((i = inputStream.read()) != -1) {
digest.update((byte) i);
}
byte[] checksumAsBytes = digest.digest();
// $NON-NLS-1$
String checksum = "";
for (byte b : checksumAsBytes) {
String temp = Integer.toHexString((int) b);
// $NON-NLS-1$ //$NON-NLS-2$
checksum += (temp.length() == 1 ? "0" : "") + temp.substring(Math.max(0, temp.length() - 2));
}
String expectedChecksum = wizard.getExpectedChecksum();
if (checksum.equalsIgnoreCase(expectedChecksum)) {
MessageBox messageBox = new MessageBox(getActiveWorkbenchWindow().getShell(), SWT.ICON_WORKING);
// $NON-NLS-1$
messageBox.setText(Messages.getString("IntegratorAction.0"));
// $NON-NLS-1$
messageBox.setMessage(Messages.getString("IntegratorAction.1"));
messageBox.open();
} else {
MessageBox messageBox = new MessageBox(getActiveWorkbenchWindow().getShell(), SWT.ICON_ERROR);
// $NON-NLS-1$
messageBox.setText(Messages.getString("IntegratorAction.2"));
messageBox.setMessage(// $NON-NLS-1$
NLS.bind(// $NON-NLS-1$
Messages.getString("IntegratorAction.3"), new Object[] { checksum.toLowerCase(), expectedChecksum.toLowerCase() }));
messageBox.open();
}
} catch (NoSuchAlgorithmException e) {
LogUtil.logError(IntegratorPlugin.PLUGIN_ID, "NoSuchAlgorithmException while initializing a message digest", e, // $NON-NLS-1$
true);
}
} else {
PerformOperationManager.getInstance().firePerformOperation(operation);
}
if (operation.getOperation() == OperationType.SIGN) {
MessageBox messageBox = new MessageBox(getActiveWorkbenchWindow().getShell(), SWT.NONE);
// $NON-NLS-1$
messageBox.setText(Messages.getString("DummyAction.13"));
// $NON-NLS-1$
messageBox.setMessage(Messages.getString("DummyAction.14") + wizard.signature());
messageBox.open();
}
} catch (IOException ex) {
LogUtil.logError(ex);
}
break;
case Window.CANCEL:
break;
}
return (null);
}
Aggregations