Search in sources :

Example 1 with InvalidAlgorithmParameterException

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

the class AsymmetricBlockCipherEngine method init.

@Override
public KeyObject init(IFlexiProviderOperation operation) {
    // $NON-NLS-1$
    LogUtil.logInfo("initializing asymmetric block cipher engine");
    this.operation = operation;
    char[] password = null;
    Key key = null;
    try {
        cipher = Registry.getAsymmetricBlockCipher(operation.getAlgorithmDescriptor().getAlgorithmName());
        if (operation.getOperation().equals(OperationType.ENCRYPT)) {
            Certificate certificate = KeyStoreManager.getInstance().getCertificate(operation.getKeyStoreAlias());
            key = (Key) certificate.getPublicKey();
            cipher.initEncrypt(key, operation.getAlgorithmDescriptor().getAlgorithmParameterSpec(), FlexiProviderEnginesPlugin.getSecureRandom());
        } else {
            // password may be contained in the ActionItem, otherwise prompt
            if (operation.getPassword() != null) {
                password = operation.getPassword();
            } else {
                password = promptPassword();
            }
            if (password == null) {
                return null;
            }
            key = (Key) KeyStoreManager.getInstance().getPrivateKey(operation.getKeyStoreAlias(), password);
            cipher.initDecrypt(key, operation.getAlgorithmDescriptor().getAlgorithmParameterSpec());
        }
        // save in the operation if no exception occurred
        operation.setPassword(password);
        initialized = true;
    } catch (NoSuchAlgorithmException e) {
        LogUtil.logError(FlexiProviderEnginesPlugin.PLUGIN_ID, // $NON-NLS-1$
        "NoSuchAlgorithmException while initializing an asymmetric block cipher engine", e, true);
        return null;
    } catch (InvalidKeyException e) {
        LogUtil.logError(FlexiProviderEnginesPlugin.PLUGIN_ID, Messages.AsymmetricBlockCipherEngine_1, e, true);
        return null;
    } catch (InvalidAlgorithmParameterException e) {
        LogUtil.logError(FlexiProviderEnginesPlugin.PLUGIN_ID, // $NON-NLS-1$
        "InvalidAlgorithmParameterException while initializing an asymmetric block cipher engine", e, true);
        return null;
    } catch (UnrecoverableEntryException e) {
        JCTMessageDialog.showInfoDialog(new Status(IStatus.INFO, FlexiProviderEnginesPlugin.PLUGIN_ID, Messages.ExAccessKeystorePassword, e));
        return null;
    } catch (Exception ex) {
        LogUtil.logError(ex);
        return null;
    }
    return new KeyObject(key, password);
}
Also used : Status(org.eclipse.core.runtime.Status) IStatus(org.eclipse.core.runtime.IStatus) InvalidAlgorithmParameterException(de.flexiprovider.api.exceptions.InvalidAlgorithmParameterException) UnrecoverableEntryException(java.security.UnrecoverableEntryException) NoSuchAlgorithmException(de.flexiprovider.api.exceptions.NoSuchAlgorithmException) InvalidKeyException(de.flexiprovider.api.exceptions.InvalidKeyException) Key(de.flexiprovider.api.keys.Key) NoSuchAlgorithmException(de.flexiprovider.api.exceptions.NoSuchAlgorithmException) BadPaddingException(de.flexiprovider.api.exceptions.BadPaddingException) IOException(java.io.IOException) InvalidKeyException(de.flexiprovider.api.exceptions.InvalidKeyException) IllegalBlockSizeException(de.flexiprovider.api.exceptions.IllegalBlockSizeException) InvalidAlgorithmParameterException(de.flexiprovider.api.exceptions.InvalidAlgorithmParameterException) UnrecoverableEntryException(java.security.UnrecoverableEntryException) Certificate(java.security.cert.Certificate)

Example 2 with InvalidAlgorithmParameterException

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

the class AsymmetricHybridCipherEngine method init.

@Override
public KeyObject init(IFlexiProviderOperation operation) {
    // $NON-NLS-1$
    LogUtil.logInfo("initializing asymmetric hybrid cipher engine");
    this.operation = operation;
    char[] password = null;
    Key key = null;
    try {
        cipher = Registry.getAsymmetricHybridCipher(operation.getAlgorithmDescriptor().getAlgorithmName());
        if (operation.getOperation().equals(OperationType.DECRYPT)) {
            // password may be contained in the ActionItem, otherwise prompt
            if (operation.getPassword() != null) {
                password = operation.getPassword();
            } else {
                password = promptPassword();
            }
            if (password == null) {
                return null;
            }
            key = (Key) KeyStoreManager.getInstance().getPrivateKey(operation.getKeyStoreAlias(), password);
            cipher.initDecrypt(key, operation.getAlgorithmDescriptor().getAlgorithmParameterSpec());
        } else {
            Certificate certificate = KeyStoreManager.getInstance().getCertificate(operation.getKeyStoreAlias());
            key = (Key) certificate.getPublicKey();
            cipher.initEncrypt(key, operation.getAlgorithmDescriptor().getAlgorithmParameterSpec(), FlexiProviderEnginesPlugin.getSecureRandom());
        }
        // save in the operation if no exception occurred
        operation.setPassword(password);
        initialized = true;
    } catch (NoSuchAlgorithmException e) {
        LogUtil.logError(FlexiProviderEnginesPlugin.PLUGIN_ID, "NoSuchAlgorithmException while initializing a cipher engine", e, // $NON-NLS-1$
        true);
        return null;
    } catch (InvalidKeyException e) {
        LogUtil.logError(FlexiProviderEnginesPlugin.PLUGIN_ID, Messages.AsymmetricHybridCipherEngine_1, e, true);
        return null;
    } catch (InvalidAlgorithmParameterException e) {
        LogUtil.logError(FlexiProviderEnginesPlugin.PLUGIN_ID, "InvalidAlgorithmParameterException while initializing a cipher engine", e, // $NON-NLS-1$
        true);
        return null;
    } catch (UnrecoverableEntryException e) {
        JCTMessageDialog.showInfoDialog(new Status(IStatus.INFO, FlexiProviderEnginesPlugin.PLUGIN_ID, Messages.ExAccessKeystorePassword, e));
        return null;
    } catch (UnsupportedOperationException e) {
        LogUtil.logError(FlexiProviderEnginesPlugin.PLUGIN_ID, "UnsupportedOperationException while initializing a cipher engine", e, // $NON-NLS-1$
        true);
        return null;
    } catch (Exception e) {
        LogUtil.logError(e);
        return null;
    }
    return new KeyObject(key, password);
}
Also used : Status(org.eclipse.core.runtime.Status) IStatus(org.eclipse.core.runtime.IStatus) InvalidAlgorithmParameterException(de.flexiprovider.api.exceptions.InvalidAlgorithmParameterException) UnrecoverableEntryException(java.security.UnrecoverableEntryException) NoSuchAlgorithmException(de.flexiprovider.api.exceptions.NoSuchAlgorithmException) InvalidKeyException(de.flexiprovider.api.exceptions.InvalidKeyException) Key(de.flexiprovider.api.keys.Key) NoSuchAlgorithmException(de.flexiprovider.api.exceptions.NoSuchAlgorithmException) InvalidKeyException(de.flexiprovider.api.exceptions.InvalidKeyException) InvalidAlgorithmParameterException(de.flexiprovider.api.exceptions.InvalidAlgorithmParameterException) UnrecoverableEntryException(java.security.UnrecoverableEntryException) Certificate(java.security.cert.Certificate)

Example 3 with InvalidAlgorithmParameterException

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

the class BlockCipherEngine method init.

@Override
public KeyObject init(IFlexiProviderOperation operation) {
    // $NON-NLS-1$
    LogUtil.logInfo("initializing block cipher engine");
    this.operation = operation;
    char[] password = new char[4];
    Key key = null;
    if (operation.useCustomKey()) {
        try {
            SecretKeyFactory secretKeyFactory = SecretKeyFactory.getInstance(operation.getAlgorithmDescriptor().getAlgorithmName(), // $NON-NLS-1$
            "FlexiCore");
            SecretKeySpec keySpec = new SecretKeySpec(operation.getKeyBytes(), operation.getAlgorithmDescriptor().getAlgorithmName());
            key = (Key) secretKeyFactory.generateSecret(keySpec);
        } catch (Exception e) {
            LogUtil.logError(FlexiProviderEnginesPlugin.PLUGIN_ID, "Exception while creating key", e, // $NON-NLS-1$
            true);
            return null;
        }
    } else {
        // password may be contained in the ActionItem, otherwise prompt
        if (operation.getPassword() != null) {
            password = operation.getPassword();
        } else {
            password = promptPassword();
        }
        if (password != null) {
            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;
            }
        }
    }
    if (key != null) {
        try {
            String fullCipherName = // Name of algorithm
            operation.getAlgorithmDescriptor().getAlgorithmName() + // $NON-NLS-1$
            "/" + // Name of mode
            ((BlockCipherDescriptor) operation.getAlgorithmDescriptor()).getMode() + // $NON-NLS-1$
            "/" + // Name of padding
            ((BlockCipherDescriptor) operation.getAlgorithmDescriptor()).getPadding();
            cipher = Registry.getBlockCipher(fullCipherName);
            if (operation.getOperation().equals(OperationType.ENCRYPT)) {
                ((BlockCipher) cipher).initEncrypt(key, ((BlockCipherDescriptor) operation.getAlgorithmDescriptor()).getModeParameters(), operation.getAlgorithmDescriptor().getAlgorithmParameterSpec(), FlexiProviderEnginesPlugin.getSecureRandom());
            } else {
                ((BlockCipher) cipher).initDecrypt(key, ((BlockCipherDescriptor) operation.getAlgorithmDescriptor()).getModeParameters(), operation.getAlgorithmDescriptor().getAlgorithmParameterSpec());
            }
            initialized = true;
        } catch (NoSuchAlgorithmException e) {
            LogUtil.logError(FlexiProviderEnginesPlugin.PLUGIN_ID, "NoSuchAlgorithmException while initializing a block cipher engine", e, // $NON-NLS-1$
            true);
            return null;
        } catch (NoSuchPaddingException e) {
            LogUtil.logError(FlexiProviderEnginesPlugin.PLUGIN_ID, "NoSuchPaddingException while initializing a block cipher engine", e, // $NON-NLS-1$
            true);
            return null;
        } catch (InvalidKeyException e) {
            LogUtil.logError(FlexiProviderEnginesPlugin.PLUGIN_ID, Messages.BlockCipherEngine_5, e, true);
            return null;
        } catch (InvalidAlgorithmParameterException e) {
            LogUtil.logError(FlexiProviderEnginesPlugin.PLUGIN_ID, "InvalidAlgorithmParameterException while initializing a block cipher engine", e, // $NON-NLS-1$
            true);
            return null;
        }
    }
    return new KeyObject(key, password);
}
Also used : Status(org.eclipse.core.runtime.Status) IStatus(org.eclipse.core.runtime.IStatus) InvalidAlgorithmParameterException(de.flexiprovider.api.exceptions.InvalidAlgorithmParameterException) BlockCipher(de.flexiprovider.api.BlockCipher) NoSuchPaddingException(de.flexiprovider.api.exceptions.NoSuchPaddingException) NoSuchAlgorithmException(de.flexiprovider.api.exceptions.NoSuchAlgorithmException) InvalidKeyException(de.flexiprovider.api.exceptions.InvalidKeyException) NoSuchAlgorithmException(de.flexiprovider.api.exceptions.NoSuchAlgorithmException) InvalidKeyException(de.flexiprovider.api.exceptions.InvalidKeyException) InvalidAlgorithmParameterException(de.flexiprovider.api.exceptions.InvalidAlgorithmParameterException) NoSuchPaddingException(de.flexiprovider.api.exceptions.NoSuchPaddingException) UnrecoverableEntryException(java.security.UnrecoverableEntryException) SecretKeySpec(de.flexiprovider.api.keys.SecretKeySpec) UnrecoverableEntryException(java.security.UnrecoverableEntryException) SecretKeyFactory(javax.crypto.SecretKeyFactory) Key(de.flexiprovider.api.keys.Key)

Example 4 with InvalidAlgorithmParameterException

use of de.flexiprovider.api.exceptions.InvalidAlgorithmParameterException 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 5 with InvalidAlgorithmParameterException

use of de.flexiprovider.api.exceptions.InvalidAlgorithmParameterException 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)

Aggregations

InvalidAlgorithmParameterException (de.flexiprovider.api.exceptions.InvalidAlgorithmParameterException)13 NoSuchAlgorithmException (de.flexiprovider.api.exceptions.NoSuchAlgorithmException)13 AlgorithmParameterSpec (de.flexiprovider.api.parameters.AlgorithmParameterSpec)8 InvalidKeyException (de.flexiprovider.api.exceptions.InvalidKeyException)7 Key (de.flexiprovider.api.keys.Key)6 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 IOException (java.io.IOException)4 Wizard (org.eclipse.jface.wizard.Wizard)4 WizardDialog (org.eclipse.jface.wizard.WizardDialog)4 IMetaKeyGenerator (org.jcryptool.crypto.flexiprovider.descriptors.meta.interfaces.IMetaKeyGenerator)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 SecretKey (de.flexiprovider.api.keys.SecretKey)3 Certificate (java.security.cert.Certificate)3