use of org.jcryptool.crypto.keystore.descriptors.ImportDescriptor 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");
}
}
}
use of org.jcryptool.crypto.keystore.descriptors.ImportDescriptor 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);
}
Aggregations