use of org.kse.crypto.CryptoException in project keystore-explorer by kaikramer.
the class X509CertUtil method loadCertificates.
/**
* Load one or more certificates from the specified stream.
*
* @param is
* Stream to load certificates from
* @return The certificates
* @throws CryptoException
* Problem encountered while loading the certificate(s)
*/
public static X509Certificate[] loadCertificates(InputStream is) throws CryptoException {
byte[] certsBytes = null;
try {
certsBytes = ReadUtil.readFully(is);
// fix common input certificate problems by converting PEM/B64 to DER
certsBytes = fixCommonInputCertProblems(certsBytes);
is = new ByteArrayInputStream(certsBytes);
CertificateFactory cf = CertificateFactory.getInstance(X509_CERT_TYPE, BOUNCY_CASTLE.jce());
Collection<? extends Certificate> certs = cf.generateCertificates(is);
ArrayList<X509Certificate> loadedCerts = new ArrayList<X509Certificate>();
for (Iterator<? extends Certificate> itr = certs.iterator(); itr.hasNext(); ) {
X509Certificate cert = (X509Certificate) itr.next();
if (cert != null) {
loadedCerts.add(cert);
}
}
return loadedCerts.toArray(new X509Certificate[loadedCerts.size()]);
} catch (IOException | NoSuchProviderException ex) {
throw new CryptoException(res.getString("NoLoadCertificate.exception.message"), ex);
} catch (CertificateException ex) {
// Failed to load certificates, may be pki path encoded - try loading as that
try {
return loadCertificatesPkiPath(new ByteArrayInputStream(certsBytes));
} catch (CryptoException ex2) {
throw new CryptoException(res.getString("NoLoadCertificate.exception.message"), ex);
}
} finally {
IOUtils.closeQuietly(is);
}
}
use of org.kse.crypto.CryptoException in project keystore-explorer by kaikramer.
the class X509CertificateGenerator method generateVersion3.
private X509Certificate generateVersion3(X500Name subject, X500Name issuer, Date validityStart, Date validityEnd, PublicKey publicKey, PrivateKey privateKey, SignatureType signatureType, BigInteger serialNumber, X509Extension extensions, Provider provider) throws CryptoException, CertIOException {
Date notBefore = validityStart == null ? new Date() : validityStart;
Date notAfter = validityEnd == null ? new Date(notBefore.getTime() + TimeUnit.DAYS.toMillis(365)) : validityEnd;
JcaX509v3CertificateBuilder certBuilder = new JcaX509v3CertificateBuilder(issuer, serialNumber, notBefore, notAfter, subject, publicKey);
if (extensions != null) {
for (String oid : extensions.getCriticalExtensionOIDs()) {
certBuilder.addExtension(new ASN1ObjectIdentifier(oid), true, getExtensionValue(extensions, oid));
}
for (String oid : extensions.getNonCriticalExtensionOIDs()) {
certBuilder.addExtension(new ASN1ObjectIdentifier(oid), false, getExtensionValue(extensions, oid));
}
}
try {
ContentSigner certSigner = null;
if (provider == null) {
certSigner = new JcaContentSignerBuilder(signatureType.jce()).setProvider("BC").build(privateKey);
} else {
certSigner = new JcaContentSignerBuilder(signatureType.jce()).setProvider(provider).build(privateKey);
}
return new JcaX509CertificateConverter().setProvider("BC").getCertificate(certBuilder.build(certSigner));
} catch (CertificateException | IllegalStateException | OperatorCreationException ex) {
throw new CryptoException(res.getString("CertificateGenFailed.exception.message"), ex);
}
}
use of org.kse.crypto.CryptoException in project keystore-explorer by kaikramer.
the class ExamineClipboardAction method showCert.
private void showCert(InputStream is) throws CryptoException {
X509Certificate[] certs = null;
try {
certs = X509CertUtil.loadCertificates(is);
if (certs.length == 0) {
JOptionPane.showMessageDialog(frame, res.getString("ExamineClipboardAction.NoCertsFound.message"), res.getString("ExamineClipboardAction.OpenCertificate.Title"), JOptionPane.WARNING_MESSAGE);
}
} catch (Exception ex) {
String problemStr = res.getString("ExamineClipboardAction.NoOpenCert.Problem");
String[] causes = new String[] { res.getString("ExamineClipboardAction.NotCert.Cause"), res.getString("ExamineClipboardAction.CorruptedCert.Cause") };
Problem problem = new Problem(problemStr, causes, ex);
DProblem dProblem = new DProblem(frame, res.getString("ExamineClipboardAction.ProblemOpeningCert.Title"), problem);
dProblem.setLocationRelativeTo(frame);
dProblem.setVisible(true);
}
if (certs != null && certs.length > 0) {
DViewCertificate dViewCertificate = new DViewCertificate(frame, res.getString("ExamineClipboardAction.CertDetails.Title"), certs, kseFrame, DViewCertificate.IMPORT);
dViewCertificate.setLocationRelativeTo(frame);
dViewCertificate.setVisible(true);
}
}
use of org.kse.crypto.CryptoException in project keystore-explorer by kaikramer.
the class MsPvkUtil method decryptPrivateKeyBlob.
private static byte[] decryptPrivateKeyBlob(byte[] encryptedPvk, byte[] rc4Key) throws CryptoException {
try {
SecretKeySpec rc4KeySpec = new SecretKeySpec(rc4Key, "RC4");
Cipher rc42 = Cipher.getInstance("RC4");
rc42.init(Cipher.DECRYPT_MODE, rc4KeySpec);
byte[] decryptedKeyBlob = rc42.doFinal(encryptedPvk);
// First four bytes will be "RSA2" if successful for an RSA private key
if (// R
(decryptedKeyBlob[0] == 82) && // S
(decryptedKeyBlob[1] == 83) && // A
(decryptedKeyBlob[2] == 65) && // 2
(decryptedKeyBlob[3] == 50)) {
return decryptedKeyBlob;
} else // First four bytes will be "DSS2" if successful for a DSA private key
if (// D
(decryptedKeyBlob[0] == 68) && // S
(decryptedKeyBlob[1] == 83) && // S
(decryptedKeyBlob[2] == 83) && // 2
(decryptedKeyBlob[3] == 50)) {
return decryptedKeyBlob;
} else {
return null;
}
} catch (GeneralSecurityException ex) {
throw new CryptoException(res.getString("PrivateKeyBlobRc4DecryptionFailed.exception.message"), ex);
}
}
use of org.kse.crypto.CryptoException in project keystore-explorer by kaikramer.
the class MsPvkUtil method getInternal.
private static byte[] getInternal(PrivateKey privateKey, int keyType) throws CryptoException {
try {
// Write PVK to a byte buffer set up to write little endian
ByteBuffer bb = ByteBuffer.wrap(new byte[PVK_BUFFER_LENGTH]);
bb.order(ByteOrder.LITTLE_ENDIAN);
// Write magic number, reserved and and key type fields
writeReservedMagicKeyType(bb, keyType);
// Get unencrypted private key blob
byte[] privateKeyBlob = null;
if (privateKey instanceof RSAPrivateCrtKey) {
privateKeyBlob = rsaPrivateKeyToBlob((RSAPrivateCrtKey) privateKey);
} else {
privateKeyBlob = dsaPrivateKeyToBlob((DSAPrivateKey) privateKey);
}
// Write type field - unencrypted
UnsignedUtil.putInt(bb, PVK_UNENCRYPTED);
// Write salt length - unencrypted so no salt, length = 0
UnsignedUtil.putInt(bb, UNENCRYPTED_SALT_LENGTH);
// Write key length field - length of the blob plus length of blob header
long keyLength = privateKeyBlob.length + BLOB_HEADER_LENGTH;
UnsignedUtil.putInt(bb, keyLength);
// Write private key blob header
writePrivateKeyBlobHeader(bb, keyType, privateKey);
// Write private key blob
bb.put(privateKeyBlob);
byte[] pvk = getBufferBytes(bb);
return pvk;
} catch (IOException ex) {
throw new CryptoException(res.getString("NoGetMsPvk.exception.message"), ex);
}
}
Aggregations