Search in sources :

Example 1 with SecretKey

use of de.flexiprovider.api.keys.SecretKey in project core by jcryptool.

the class MacEngine method init.

@Override
public KeyObject init(IFlexiProviderOperation operation) {
    // $NON-NLS-1$
    LogUtil.logInfo("initializing mac engine");
    this.operation = operation;
    char[] password = null;
    Key key = null;
    // password may be contained in the ActionItem, otherwise prompt
    if (operation.getPassword() != null) {
        password = operation.getPassword();
    } else if (!operation.useCustomKey()) {
        // farndt - prompt only if custom key is not to be used
        password = promptPassword();
    }
    if (password != null && !operation.useCustomKey()) {
        try {
            key = (Key) KeyStoreManager.getInstance().getSecretKey(operation.getKeyStoreAlias(), password);
            // save in the operation if no exception occurred
            operation.setPassword(password);
        } catch (UnrecoverableEntryException e) {
            JCTMessageDialog.showInfoDialog(new Status(IStatus.INFO, FlexiProviderEnginesPlugin.PLUGIN_ID, Messages.ExAccessKeystorePassword, e));
            return null;
        } catch (Exception e) {
            LogUtil.logError(FlexiProviderEnginesPlugin.PLUGIN_ID, "Exception while accessing a secret key", e, // $NON-NLS-1$
            true);
            return null;
        }
    } else {
        // farndt - use custom key
        key = new CustomKey(operation.getKeyBytes());
    }
    if (key != null) {
        try {
            mac = Registry.getMAC(operation.getAlgorithmDescriptor().getAlgorithmName());
            AlgorithmParameterSpec spec = operation.getAlgorithmDescriptor().getAlgorithmParameterSpec();
            if (spec != null) {
                mac.init((SecretKey) key, spec);
            } else {
                mac.init((SecretKey) key);
            }
            initialized = true;
        } catch (NoSuchAlgorithmException e) {
            LogUtil.logError(FlexiProviderEnginesPlugin.PLUGIN_ID, "NoSuchAlgorithmException while initializing a mac", e, // $NON-NLS-1$
            true);
            return null;
        } catch (InvalidKeyException e) {
            LogUtil.logError(FlexiProviderEnginesPlugin.PLUGIN_ID, Messages.MacEngine_2, e, true);
            return null;
        } catch (InvalidAlgorithmParameterException e) {
            LogUtil.logError(FlexiProviderEnginesPlugin.PLUGIN_ID, "InvalidAlgorithmParameterException while initializing a mac", e, // $NON-NLS-1$
            true);
            return null;
        }
    }
    return new KeyObject(key, password);
}
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) SecretKey(de.flexiprovider.api.keys.SecretKey) Key(de.flexiprovider.api.keys.Key) HMacKey(de.flexiprovider.core.mac.HMacKey) PartInitException(org.eclipse.ui.PartInitException) 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)

Example 2 with SecretKey

use of de.flexiprovider.api.keys.SecretKey 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 3 with SecretKey

use of de.flexiprovider.api.keys.SecretKey in project core by jcryptool.

the class NewSymmetricKeyHandler method execute.

/**
 * @see org.eclipse.jface.action.Action#run()
 */
public Object execute(ExecutionEvent event) {
    // $NON-NLS-1$
    LogUtil.logInfo("NewSymmetricKeyAction");
    shell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();
    Wizard wizard = new NewSymmetricKeyWizard();
    dialog = new WizardDialog(shell, wizard);
    dialog.setMinimumPageSize(300, 350);
    int result = dialog.open();
    if (result == Window.OK) {
        if (wizard instanceof INewKeyWizard) {
            INewEntryDescriptor nkd = ((INewKeyWizard) wizard).getNewEntryDescriptor();
            Integer[] argument = new Integer[1];
            argument[0] = nkd.getKeyLength();
            Integer keyLen = argument[0];
            // $NON-NLS-1$
            LogUtil.logInfo("key strength: " + argument[0]);
            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();
                performNewKeyAction(new NewSecretKeyDescriptor(nkd, key));
            } 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 (null);
}
Also used : NewSecretKeyDescriptor(org.jcryptool.crypto.keystore.descriptors.NewSecretKeyDescriptor) InvalidAlgorithmParameterException(de.flexiprovider.api.exceptions.InvalidAlgorithmParameterException) NoSuchAlgorithmException(de.flexiprovider.api.exceptions.NoSuchAlgorithmException) IMetaKeyGenerator(org.jcryptool.crypto.flexiprovider.descriptors.meta.interfaces.IMetaKeyGenerator) InvocationTargetException(java.lang.reflect.InvocationTargetException) IMetaLength(org.jcryptool.crypto.flexiprovider.descriptors.meta.interfaces.IMetaLength) SecretKey(de.flexiprovider.api.keys.SecretKey) NewSymmetricKeyWizard(org.jcryptool.crypto.flexiprovider.keystore.wizards.NewSymmetricKeyWizard) SecretKeyGenerator(de.flexiprovider.api.keys.SecretKeyGenerator) INewKeyWizard(org.jcryptool.crypto.keystore.descriptors.interfaces.INewKeyWizard) NewSymmetricKeyWizard(org.jcryptool.crypto.flexiprovider.keystore.wizards.NewSymmetricKeyWizard) Wizard(org.eclipse.jface.wizard.Wizard) INewKeyWizard(org.jcryptool.crypto.keystore.descriptors.interfaces.INewKeyWizard) WizardDialog(org.eclipse.jface.wizard.WizardDialog) AlgorithmParameterSpec(de.flexiprovider.api.parameters.AlgorithmParameterSpec) INewEntryDescriptor(org.jcryptool.crypto.keystore.descriptors.interfaces.INewEntryDescriptor)

Example 4 with SecretKey

use of de.flexiprovider.api.keys.SecretKey in project core by jcryptool.

the class PBESecretKeyContentProvider method getKeySpecElements.

@Override
protected List<TableEntry> getKeySpecElements(Key key) {
    List<TableEntry> paramElements = new ArrayList<TableEntry>();
    try {
        PBEKeyFactory keyFactory = new PBEKeyFactory();
        PBEKeySpec keySpec = (PBEKeySpec) keyFactory.getKeySpec((SecretKey) key, PBEKeySpec.class);
        if (keySpec == null)
            return null;
        paramElements.add(// $NON-NLS-2$
        new TableEntry(Messages.ContentProvider_iterationcount, "" + keySpec.getIterationCount()));
        // $NON-NLS-2$
        paramElements.add(new TableEntry(Messages.ContentProvider_keylength, "" + keySpec.getKeyLength()));
        paramElements.add(new TableEntry(Messages.ContentProvider_password, // $NON-NLS-2$
        "" + Arrays.toString(keySpec.getPassword())));
        // $NON-NLS-2$
        paramElements.add(new TableEntry(Messages.ContentProvider_salt, "" + Arrays.toString(keySpec.getSalt())));
    } catch (ClassCastException e) {
        return null;
    } catch (InvalidKeySpecException e) {
        return null;
    }
    return paramElements;
}
Also used : TableEntry(org.jcryptool.crypto.keystore.ui.dialogs.TableEntry) PBEKeySpec(de.flexiprovider.core.pbe.PBEKeySpec) SecretKey(de.flexiprovider.api.keys.SecretKey) ECSecretKey(de.flexiprovider.ec.keys.ECSecretKey) PBEKeyFactory(de.flexiprovider.core.pbe.PBEKeyFactory) ArrayList(java.util.ArrayList) InvalidKeySpecException(de.flexiprovider.api.exceptions.InvalidKeySpecException)

Aggregations

SecretKey (de.flexiprovider.api.keys.SecretKey)4 InvalidAlgorithmParameterException (de.flexiprovider.api.exceptions.InvalidAlgorithmParameterException)3 NoSuchAlgorithmException (de.flexiprovider.api.exceptions.NoSuchAlgorithmException)3 AlgorithmParameterSpec (de.flexiprovider.api.parameters.AlgorithmParameterSpec)3 SecretKeyGenerator (de.flexiprovider.api.keys.SecretKeyGenerator)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 Wizard (org.eclipse.jface.wizard.Wizard)2 WizardDialog (org.eclipse.jface.wizard.WizardDialog)2 IMetaKeyGenerator (org.jcryptool.crypto.flexiprovider.descriptors.meta.interfaces.IMetaKeyGenerator)2 IMetaLength (org.jcryptool.crypto.flexiprovider.descriptors.meta.interfaces.IMetaLength)2 NewSymmetricKeyWizard (org.jcryptool.crypto.flexiprovider.keystore.wizards.NewSymmetricKeyWizard)2 NewSecretKeyDescriptor (org.jcryptool.crypto.keystore.descriptors.NewSecretKeyDescriptor)2 INewEntryDescriptor (org.jcryptool.crypto.keystore.descriptors.interfaces.INewEntryDescriptor)2 INewKeyWizard (org.jcryptool.crypto.keystore.descriptors.interfaces.INewKeyWizard)2 InvalidKeyException (de.flexiprovider.api.exceptions.InvalidKeyException)1 InvalidKeySpecException (de.flexiprovider.api.exceptions.InvalidKeySpecException)1 Key (de.flexiprovider.api.keys.Key)1 HMacKey (de.flexiprovider.core.mac.HMacKey)1 PBEKeyFactory (de.flexiprovider.core.pbe.PBEKeyFactory)1 PBEKeySpec (de.flexiprovider.core.pbe.PBEKeySpec)1