use of org.kse.utilities.asn1.Asn1Exception 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.kse.utilities.asn1.Asn1Exception in project core by jcryptool.
the class ImportManager method importPFX.
public PFX importPFX(IPath path) {
BufferedInputStream is;
try {
IFileStore fileStore = EFS.getStore(URIUtil.toURI(path));
is = new BufferedInputStream(fileStore.openInputStream(EFS.NONE, null));
PFX pfx = new PFX();
DERDecoder decoder = new DERDecoder(is);
pfx.decode(decoder);
decoder.close();
return pfx;
} catch (CoreException e) {
LogUtil.logError(FlexiProviderKeystorePlugin.PLUGIN_ID, "CoreException while accessing a file store", e, true);
} catch (ASN1Exception e) {
LogUtil.logError(FlexiProviderKeystorePlugin.PLUGIN_ID, "ASN1Exception while decoding a pfx", e, true);
} catch (IOException e) {
LogUtil.logError(FlexiProviderKeystorePlugin.PLUGIN_ID, "IOException while decoding a pfx", e, false);
}
return null;
}
use of org.kse.utilities.asn1.Asn1Exception in project keystore-explorer by kaikramer.
the class DViewCertificate method asn1DumpPressed.
private void asn1DumpPressed() {
X509Certificate cert = getSelectedCertificate();
try {
DViewAsn1Dump dViewAsn1Dump = new DViewAsn1Dump(this, cert);
dViewAsn1Dump.setLocationRelativeTo(this);
dViewAsn1Dump.setVisible(true);
} catch (Asn1Exception ex) {
DError dError = new DError(this, ex);
dError.setLocationRelativeTo(this);
dError.setVisible(true);
} catch (IOException ex) {
DError dError = new DError(this, ex);
dError.setLocationRelativeTo(this);
dError.setVisible(true);
}
}
use of org.kse.utilities.asn1.Asn1Exception in project keystore-explorer by kaikramer.
the class DViewPublicKey method asn1DumpPressed.
private void asn1DumpPressed() {
try {
DViewAsn1Dump dViewAsn1Dump = new DViewAsn1Dump(this, publicKey);
dViewAsn1Dump.setLocationRelativeTo(this);
dViewAsn1Dump.setVisible(true);
} catch (Asn1Exception ex) {
DError dError = new DError(this, ex);
dError.setLocationRelativeTo(this);
dError.setVisible(true);
} catch (IOException ex) {
DError dError = new DError(this, ex);
dError.setLocationRelativeTo(this);
dError.setVisible(true);
}
}
use of org.kse.utilities.asn1.Asn1Exception in project jmulticard by ctt-gob-es.
the class SmartCafePkcs15Applet method preloadCertificates.
private void preloadCertificates() throws FileNotFoundException, Iso7816FourCardException, IOException, Asn1Exception, TlvException {
selectMasterFile();
// Seleccionamos el ODF, no nos devuelve FCI ni nada
selectFileById(ODF_PATH);
// Leemos el ODF
final byte[] odfBytes = readBinaryComplete(162);
final Odf odf = new Odf();
odf.setDerValue(odfBytes);
// Sacamos del ODF la ruta del CDF
final Path cdfPath = odf.getCdfPath();
// Leemos el CDF
final Cdf cdf = new Cdf();
try {
selectMasterFile();
final byte[] cdfBytes = selectFileByIdAndRead(cdfPath.getPathBytes());
cdf.setDerValue(cdfBytes);
} catch (final Exception e) {
throw new ApduConnectionException(// $NON-NLS-1$
"No se ha podido cargar el CDF de la tarjeta: " + e, // $NON-NLS-1$
e);
}
final CertificateFactory cf;
try {
// $NON-NLS-1$
cf = CertificateFactory.getInstance("X.509");
} catch (final CertificateException e) {
// $NON-NLS-1$
throw new IOException("Error obteniendo la factoria de certificados X.509: " + e, e);
}
if (cdf.getCertificateCount() < 1) {
// $NON-NLS-1$
LOGGER.warning("La tarjeta no contiene ningun certificado");
}
for (int i = 0; i < cdf.getCertificateCount(); i++) {
try {
int fileLength = -1;
Location certLocation = new Location(cdf.getCertificatePath(i));
while (certLocation != null) {
final byte[] id = certLocation.getFile();
try {
fileLength = selectFileById(id);
} catch (final FileNotFoundException e) {
System.out.println(// $NON-NLS-1$//$NON-NLS-2$
"El CDF indicaba un certificado en la ruta '" + certLocation + "', pero un elemento de esta no existe, se ignorara: " + e);
}
certLocation = certLocation.getChild();
}
final byte[] certBytes;
if (fileLength > 0) {
certBytes = readBinaryComplete(fileLength);
} else {
// A veces hay punteros que apuntan a localizaciones vacias
LOGGER.warning(// $NON-NLS-1$ //$NON-NLS-2$
"El certificado " + i + " del dispositivo esta vacio");
continue;
}
CERTS_BY_ALIAS.put(cdf.getCertificateAlias(i), (X509Certificate) cf.generateCertificate(new ByteArrayInputStream(certBytes)));
} catch (final Exception e) {
// Puede darse el caso de que el puntero apunte a algo que no es un certificado
LOGGER.severe(// $NON-NLS-1$ //$NON-NLS-2$
"Error en la lectura del certificado " + i + " del dispositivo: " + e);
continue;
}
}
}
Aggregations