Search in sources :

Example 1 with IImportDescriptor

use of org.jcryptool.crypto.keystore.descriptors.interfaces.IImportDescriptor in project core by jcryptool.

the class AbstractImportKeyStoreEntryHandler method performImportAction.

protected void performImportAction(IImportDescriptor descriptor, Object importedObject) throws IllegalArgumentException {
    if (descriptor.getKeyStoreEntryType().equals(KeyType.SECRETKEY)) {
        if (importedObject instanceof SecretKey) {
            // $NON-NLS-1$
            LogUtil.logInfo("importing secret key");
            addSecretKey(descriptor, (SecretKey) importedObject);
        } else {
            throw new IllegalArgumentException("Parameter is not as expected an instance of SecretKey");
        }
    } else if (descriptor.getKeyStoreEntryType().equals(KeyType.KEYPAIR)) {
        if (importedObject instanceof PFX) {
            // $NON-NLS-1$
            LogUtil.logInfo("importing pfx");
            PFX pfx = (PFX) importedObject;
            try {
                char[] password = promptPassword();
                if (password == null)
                    return;
                SafeBag safeBag = pfx.getAuthSafe().getSafeContents(0).getSafeBag(0);
                PKCS8ShroudedKeyBag kBag = (PKCS8ShroudedKeyBag) safeBag.getBagValue();
                PrivateKey privKey = kBag.getPrivateKey(password);
                SafeBag certBag = pfx.getAuthSafe().getSafeContents(1, password).getSafeBag(0);
                CertBag cBag = (CertBag) certBag.getBagValue();
                PublicKey pubKey = cBag.getCertificate().getPublicKey();
                int keySize = -1;
                if (pubKey instanceof RSAPublicKey)
                    keySize = ((RSAPublicKey) pubKey).getN().bitLength();
                else if (pubKey instanceof DSAPublicKey)
                    keySize = ((DSAPublicKey) pubKey).getParameters().getP().bitLength();
                // TODO: Add keySize calculation for the remaining
                // algorithms.
                ImportDescriptor newDescriptor = new ImportDescriptor(descriptor.getContactName(), privKey.getAlgorithm(), KeyType.KEYPAIR, descriptor.getFileName(), descriptor.getPassword(), descriptor.getProvider(), keySize);
                addKeyPair(newDescriptor, privKey, pubKey);
            } catch (ASN1Exception e) {
                LogUtil.logError(KeyStorePlugin.PLUGIN_ID, "error while importing key pair", e, true);
            } catch (IOException e) {
                LogUtil.logError(KeyStorePlugin.PLUGIN_ID, "error while importing key pair", e, false);
            } catch (GeneralSecurityException e) {
                LogUtil.logError(KeyStorePlugin.PLUGIN_ID, "error while importing key pair", e, true);
            }
        } else {
            throw new IllegalArgumentException("Parameter is not an instance of PFX, as expected");
        }
    } else if (descriptor.getKeyStoreEntryType().equals(KeyType.PUBLICKEY)) {
        if (importedObject instanceof Certificate) {
            // $NON-NLS-1$
            LogUtil.logInfo("importing certificate");
            addCertificate(descriptor, (Certificate) importedObject);
        } else {
            throw new IllegalArgumentException("Parameter is not an instance of Certificate, as expected");
        }
    }
}
Also used : PKCS8ShroudedKeyBag(codec.pkcs12.PKCS8ShroudedKeyBag) PFX(codec.pkcs12.PFX) PrivateKey(java.security.PrivateKey) RSAPublicKey(de.flexiprovider.core.rsa.interfaces.RSAPublicKey) PublicKey(java.security.PublicKey) DSAPublicKey(de.flexiprovider.core.dsa.interfaces.DSAPublicKey) ASN1Exception(codec.asn1.ASN1Exception) GeneralSecurityException(java.security.GeneralSecurityException) IOException(java.io.IOException) SafeBag(codec.pkcs12.SafeBag) DSAPublicKey(de.flexiprovider.core.dsa.interfaces.DSAPublicKey) SecretKey(javax.crypto.SecretKey) CertBag(codec.pkcs12.CertBag) RSAPublicKey(de.flexiprovider.core.rsa.interfaces.RSAPublicKey) IImportDescriptor(org.jcryptool.crypto.keystore.descriptors.interfaces.IImportDescriptor) ImportDescriptor(org.jcryptool.crypto.keystore.descriptors.ImportDescriptor) Certificate(java.security.cert.Certificate)

Example 2 with IImportDescriptor

use of org.jcryptool.crypto.keystore.descriptors.interfaces.IImportDescriptor in project core by jcryptool.

the class ImportKeyHandler method execute.

public Object execute(ExecutionEvent event) {
    shell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();
    Wizard wizard = new ImportWizard();
    dialog = new WizardDialog(shell, wizard);
    dialog.setMinimumPageSize(300, 350);
    int result = dialog.open();
    if (result == Window.OK) {
        if (wizard instanceof IImportWizard) {
            IImportDescriptor desc = ((IImportWizard) wizard).getImportDescriptor();
            IPath path = new Path(desc.getFileName());
            if (desc.getKeyStoreEntryType().equals(KeyType.SECRETKEY)) {
                SecretKey key = ImportManager.getInstance().importSecretKey(path);
                performImportAction(new ImportDescriptor(desc.getContactName(), key.getAlgorithm(), KeyType.SECRETKEY, desc.getFileName(), desc.getPassword(), // $NON-NLS-1$
                "FlexiCore", -1), key);
            } else if (desc.getKeyStoreEntryType().equals(KeyType.KEYPAIR)) {
                PFX pfx = ImportManager.getInstance().importPFX(path);
                performImportAction(desc, pfx);
            } else if (desc.getKeyStoreEntryType().equals(KeyType.PUBLICKEY)) {
                Certificate cert = ImportManager.getInstance().importCertificate(path);
                performImportAction(desc, cert);
            }
        }
    }
    return (null);
}
Also used : IPath(org.eclipse.core.runtime.IPath) Path(org.eclipse.core.runtime.Path) SecretKey(javax.crypto.SecretKey) PFX(codec.pkcs12.PFX) ImportWizard(org.jcryptool.crypto.flexiprovider.keystore.wizards.ImportWizard) IImportWizard(org.jcryptool.crypto.keystore.descriptors.interfaces.IImportWizard) IPath(org.eclipse.core.runtime.IPath) IImportWizard(org.jcryptool.crypto.keystore.descriptors.interfaces.IImportWizard) IImportDescriptor(org.jcryptool.crypto.keystore.descriptors.interfaces.IImportDescriptor) ImportDescriptor(org.jcryptool.crypto.keystore.descriptors.ImportDescriptor) Wizard(org.eclipse.jface.wizard.Wizard) ImportWizard(org.jcryptool.crypto.flexiprovider.keystore.wizards.ImportWizard) IImportWizard(org.jcryptool.crypto.keystore.descriptors.interfaces.IImportWizard) WizardDialog(org.eclipse.jface.wizard.WizardDialog) IImportDescriptor(org.jcryptool.crypto.keystore.descriptors.interfaces.IImportDescriptor) Certificate(java.security.cert.Certificate)

Aggregations

PFX (codec.pkcs12.PFX)2 Certificate (java.security.cert.Certificate)2 SecretKey (javax.crypto.SecretKey)2 ImportDescriptor (org.jcryptool.crypto.keystore.descriptors.ImportDescriptor)2 IImportDescriptor (org.jcryptool.crypto.keystore.descriptors.interfaces.IImportDescriptor)2 ASN1Exception (codec.asn1.ASN1Exception)1 CertBag (codec.pkcs12.CertBag)1 PKCS8ShroudedKeyBag (codec.pkcs12.PKCS8ShroudedKeyBag)1 SafeBag (codec.pkcs12.SafeBag)1 DSAPublicKey (de.flexiprovider.core.dsa.interfaces.DSAPublicKey)1 RSAPublicKey (de.flexiprovider.core.rsa.interfaces.RSAPublicKey)1 IOException (java.io.IOException)1 GeneralSecurityException (java.security.GeneralSecurityException)1 PrivateKey (java.security.PrivateKey)1 PublicKey (java.security.PublicKey)1 IPath (org.eclipse.core.runtime.IPath)1 Path (org.eclipse.core.runtime.Path)1 Wizard (org.eclipse.jface.wizard.Wizard)1 WizardDialog (org.eclipse.jface.wizard.WizardDialog)1 ImportWizard (org.jcryptool.crypto.flexiprovider.keystore.wizards.ImportWizard)1