Search in sources :

Example 6 with NoSuchAlgorithmException

use of de.flexiprovider.api.exceptions.NoSuchAlgorithmException 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;
}
Also used : IStatus(org.eclipse.core.runtime.IStatus) Status(org.eclipse.core.runtime.Status) InvalidAlgorithmParameterException(de.flexiprovider.api.exceptions.InvalidAlgorithmParameterException) UnrecoverableEntryException(java.security.UnrecoverableEntryException) NoSuchAlgorithmException(de.flexiprovider.api.exceptions.NoSuchAlgorithmException) InvalidKeyException(de.flexiprovider.api.exceptions.InvalidKeyException) AlgorithmParameterSpec(de.flexiprovider.api.parameters.AlgorithmParameterSpec) PublicKey(de.flexiprovider.api.keys.PublicKey) Key(de.flexiprovider.api.keys.Key) PrivateKey(de.flexiprovider.api.keys.PrivateKey) SignatureException(de.flexiprovider.api.exceptions.SignatureException) UnrecoverableEntryException(java.security.UnrecoverableEntryException) NoSuchAlgorithmException(de.flexiprovider.api.exceptions.NoSuchAlgorithmException) IOException(java.io.IOException) InvalidKeyException(de.flexiprovider.api.exceptions.InvalidKeyException) InvalidAlgorithmParameterException(de.flexiprovider.api.exceptions.InvalidAlgorithmParameterException) Certificate(java.security.cert.Certificate)

Example 7 with NoSuchAlgorithmException

use of de.flexiprovider.api.exceptions.NoSuchAlgorithmException 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;
}
Also used : NoSuchAlgorithmException(de.flexiprovider.api.exceptions.NoSuchAlgorithmException) IMetaKeyGenerator(org.jcryptool.crypto.flexiprovider.descriptors.meta.interfaces.IMetaKeyGenerator) Shell(org.eclipse.swt.widgets.Shell) INewKeyWizard(org.jcryptool.crypto.keystore.descriptors.interfaces.INewKeyWizard) Job(org.eclipse.core.runtime.jobs.Job) NewSecretKeyDescriptor(org.jcryptool.crypto.keystore.descriptors.NewSecretKeyDescriptor) InvalidAlgorithmParameterException(de.flexiprovider.api.exceptions.InvalidAlgorithmParameterException) InvocationTargetException(java.lang.reflect.InvocationTargetException) IMetaLength(org.jcryptool.crypto.flexiprovider.descriptors.meta.interfaces.IMetaLength) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) SecretKey(de.flexiprovider.api.keys.SecretKey) NewSymmetricKeyWizard(org.jcryptool.crypto.flexiprovider.keystore.wizards.NewSymmetricKeyWizard) SecretKeyGenerator(de.flexiprovider.api.keys.SecretKeyGenerator) NewSymmetricKeyWizard(org.jcryptool.crypto.flexiprovider.keystore.wizards.NewSymmetricKeyWizard) Wizard(org.eclipse.jface.wizard.Wizard) INewKeyWizard(org.jcryptool.crypto.keystore.descriptors.interfaces.INewKeyWizard) NewKeyPairWizard(org.jcryptool.crypto.flexiprovider.keystore.wizards.NewKeyPairWizard) WizardDialog(org.eclipse.jface.wizard.WizardDialog) AlgorithmParameterSpec(de.flexiprovider.api.parameters.AlgorithmParameterSpec) INewEntryDescriptor(org.jcryptool.crypto.keystore.descriptors.interfaces.INewEntryDescriptor)

Example 8 with NoSuchAlgorithmException

use of de.flexiprovider.api.exceptions.NoSuchAlgorithmException in project core by jcryptool.

the class AlgorithmParameterSpecElement method getAlgorithmParameterSpec.

private AlgorithmParameterSpec getAlgorithmParameterSpec(String algorithmName, String encoded) {
    if (encoded != null) {
        // $NON-NLS-1$
        LogUtil.logInfo("getAlgorithmParameterSpec");
        try {
            byte[] encodedParams = Base64Coder.decode(encoded);
            AlgorithmParameters params = Registry.getAlgParams(algorithmName);
            params.init(encodedParams);
            return params.getParameterSpec(Registry.getAlgParamSpecClass(algorithmName));
        } catch (IOException e) {
            LogUtil.logError(FlexiProviderOperationsPlugin.PLUGIN_ID, "IOException while decoding AlgorithmParameterParameters", e, true);
        } catch (NoSuchAlgorithmException e) {
            LogUtil.logError(FlexiProviderOperationsPlugin.PLUGIN_ID, "NoSuchAlgorithmException while decoding AlgorithmParameterParameters", e, true);
        } catch (InvalidParameterSpecException e) {
            LogUtil.logError(FlexiProviderOperationsPlugin.PLUGIN_ID, "InvalidParameterSpecException while decoding AlgorithmParameterParameters", e, true);
        }
    }
    return null;
}
Also used : IOException(java.io.IOException) NoSuchAlgorithmException(de.flexiprovider.api.exceptions.NoSuchAlgorithmException) InvalidParameterSpecException(de.flexiprovider.api.exceptions.InvalidParameterSpecException) AlgorithmParameters(de.flexiprovider.api.parameters.AlgorithmParameters)

Example 9 with NoSuchAlgorithmException

use of de.flexiprovider.api.exceptions.NoSuchAlgorithmException in project core by jcryptool.

the class AlgorithmParameterSpecElement method setAlgorithmParameters.

private void setAlgorithmParameters(String algorithmName, AlgorithmParameterSpec spec) {
    if (spec != null) {
        try {
            AlgorithmParameters params = Registry.getAlgParams(algorithmName);
            params.init(spec);
            setText(String.valueOf(Base64Coder.encode(params.getEncoded())));
        } catch (IOException e) {
            LogUtil.logError(FlexiProviderOperationsPlugin.PLUGIN_ID, "IOException while encoding AlgorithmParameterParameters", e, true);
        } catch (NoSuchAlgorithmException e) {
            LogUtil.logError(FlexiProviderOperationsPlugin.PLUGIN_ID, "NoSuchAlgorithmException while encoding AlgorithmParameterParameters", e, true);
        } catch (InvalidParameterSpecException e) {
            LogUtil.logError(FlexiProviderOperationsPlugin.PLUGIN_ID, "InvalidParameterSpecException while encoding AlgorithmParameterParameters", e, true);
        }
    }
}
Also used : IOException(java.io.IOException) NoSuchAlgorithmException(de.flexiprovider.api.exceptions.NoSuchAlgorithmException) InvalidParameterSpecException(de.flexiprovider.api.exceptions.InvalidParameterSpecException) AlgorithmParameters(de.flexiprovider.api.parameters.AlgorithmParameters)

Example 10 with NoSuchAlgorithmException

use of de.flexiprovider.api.exceptions.NoSuchAlgorithmException in project core by jcryptool.

the class ModeParameterSpecElement method setModeParameters.

private void setModeParameters(AlgorithmParameterSpec modeSpec) {
    if (modeSpec != null) {
        try {
            // $NON-NLS-1$
            AlgorithmParameters params = Registry.getAlgParams("Mode");
            params.init(modeSpec);
            setText(String.valueOf(Base64Coder.encode(params.getEncoded())));
        } catch (IOException e) {
            LogUtil.logError(FlexiProviderOperationsPlugin.PLUGIN_ID, "IOException while encoding ModeParameters", e, true);
        } catch (NoSuchAlgorithmException e) {
            LogUtil.logError(FlexiProviderOperationsPlugin.PLUGIN_ID, "NoSuchAlgorithmException while encoding ModeParameters", e, true);
        } catch (InvalidParameterSpecException e) {
            LogUtil.logError(FlexiProviderOperationsPlugin.PLUGIN_ID, "InvalidParameterSpecException while encoding ModeParameters", e, true);
        }
    }
}
Also used : IOException(java.io.IOException) NoSuchAlgorithmException(de.flexiprovider.api.exceptions.NoSuchAlgorithmException) InvalidParameterSpecException(de.flexiprovider.api.exceptions.InvalidParameterSpecException) AlgorithmParameters(de.flexiprovider.api.parameters.AlgorithmParameters)

Aggregations

NoSuchAlgorithmException (de.flexiprovider.api.exceptions.NoSuchAlgorithmException)20 InvalidAlgorithmParameterException (de.flexiprovider.api.exceptions.InvalidAlgorithmParameterException)13 IOException (java.io.IOException)9 InvalidKeyException (de.flexiprovider.api.exceptions.InvalidKeyException)8 AlgorithmParameterSpec (de.flexiprovider.api.parameters.AlgorithmParameterSpec)8 Key (de.flexiprovider.api.keys.Key)7 InvocationTargetException (java.lang.reflect.InvocationTargetException)6 UnrecoverableEntryException (java.security.UnrecoverableEntryException)6 IStatus (org.eclipse.core.runtime.IStatus)6 Status (org.eclipse.core.runtime.Status)6 WizardDialog (org.eclipse.jface.wizard.WizardDialog)5 IMetaKeyGenerator (org.jcryptool.crypto.flexiprovider.descriptors.meta.interfaces.IMetaKeyGenerator)5 InvalidParameterSpecException (de.flexiprovider.api.exceptions.InvalidParameterSpecException)4 AlgorithmParameters (de.flexiprovider.api.parameters.AlgorithmParameters)4 Wizard (org.eclipse.jface.wizard.Wizard)4 IMetaLength (org.jcryptool.crypto.flexiprovider.descriptors.meta.interfaces.IMetaLength)4 INewEntryDescriptor (org.jcryptool.crypto.keystore.descriptors.interfaces.INewEntryDescriptor)4 INewKeyWizard (org.jcryptool.crypto.keystore.descriptors.interfaces.INewKeyWizard)4 PrivateKey (de.flexiprovider.api.keys.PrivateKey)3 PublicKey (de.flexiprovider.api.keys.PublicKey)3