Search in sources :

Example 1 with PemObject

use of org.bouncycastle.util.io.pem.PemObject in project neo4j by neo4j.

the class Certificates method loadCertificates.

public Certificate[] loadCertificates(File certFile) throws CertificateException, IOException {
    CertificateFactory certFactory = CertificateFactory.getInstance(CERTIFICATE_TYPE);
    Collection<Certificate> certificates = new LinkedList<>();
    try (PemReader r = new PemReader(new FileReader(certFile))) {
        for (PemObject pemObject = r.readPemObject(); pemObject != null; pemObject = r.readPemObject()) {
            byte[] encodedCert = pemObject.getContent();
            certificates.addAll(certFactory.generateCertificates(new ByteArrayInputStream(encodedCert)));
        }
    }
    if (certificates.size() == 0) {
        // Ok, failed to read as PEM file, try and read it as raw binary certificate
        try (FileInputStream in = new FileInputStream(certFile)) {
            certificates = (Collection<Certificate>) certFactory.generateCertificates(in);
        }
    }
    return certificates.toArray(new Certificate[certificates.size()]);
}
Also used : PemReader(org.bouncycastle.util.io.pem.PemReader) PemObject(org.bouncycastle.util.io.pem.PemObject) ByteArrayInputStream(java.io.ByteArrayInputStream) FileReader(java.io.FileReader) CertificateFactory(java.security.cert.CertificateFactory) LinkedList(java.util.LinkedList) FileInputStream(java.io.FileInputStream) X509Certificate(java.security.cert.X509Certificate) Certificate(java.security.cert.Certificate)

Example 2 with PemObject

use of org.bouncycastle.util.io.pem.PemObject in project neo4j by neo4j.

the class Certificates method writePem.

private void writePem(String type, byte[] encodedContent, File path) throws IOException {
    path.getParentFile().mkdirs();
    try (PemWriter writer = new PemWriter(new FileWriter(path))) {
        writer.writeObject(new PemObject(type, encodedContent));
        writer.flush();
    }
    path.setReadable(false, false);
    path.setWritable(false, false);
    path.setReadable(true);
    path.setWritable(true);
}
Also used : PemObject(org.bouncycastle.util.io.pem.PemObject) PemWriter(org.bouncycastle.util.io.pem.PemWriter) FileWriter(java.io.FileWriter)

Example 3 with PemObject

use of org.bouncycastle.util.io.pem.PemObject in project platformlayer by platformlayer.

the class KeyParser method parse.

public Object parse(String s) {
    Object key = null;
    if (key == null) {
        if (s.contains(BEGIN_PRIVATE_KEY)) {
            String payload = s.substring(s.indexOf(BEGIN_PRIVATE_KEY) + BEGIN_PRIVATE_KEY.length());
            if (payload.contains(END_PRIVATE_KEY)) {
                payload = payload.substring(0, payload.indexOf(END_PRIVATE_KEY));
                key = tryParsePemFormat(payload);
            }
        }
    }
    if (key == null) {
        try {
            PemReader reader = new PemReader(new StringReader(s));
            PemObject pemObject = reader.readPemObject();
            reader.close();
            PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(pemObject.getContent());
            KeyFactory kf = KeyFactory.getInstance("RSA");
            PrivateKey privateKey = kf.generatePrivate(keySpec);
            if (privateKey instanceof RSAPrivateCrtKey) {
                RSAPrivateCrtKey rsaPrivateCrtKey = (RSAPrivateCrtKey) privateKey;
                RSAPublicKeySpec publicKeySpec = new java.security.spec.RSAPublicKeySpec(rsaPrivateCrtKey.getModulus(), rsaPrivateCrtKey.getPublicExponent());
                PublicKey publicKey = kf.generatePublic(publicKeySpec);
                key = new KeyPair(publicKey, privateKey);
            } else {
                key = privateKey;
            }
        } catch (Exception e) {
            log.debug("Error reading pem data", e);
            return null;
        }
    }
    if (key == null) {
        try {
            // TODO: Check if looks like base64??
            byte[] fromBase64 = Base64.decode(s);
            key = parse(fromBase64);
        } catch (Exception e) {
            log.debug("Cannot decode as base64", e);
        }
    }
    return key;
}
Also used : KeyPair(java.security.KeyPair) PrivateKey(java.security.PrivateKey) RSAPrivateCrtKey(java.security.interfaces.RSAPrivateCrtKey) PublicKey(java.security.PublicKey) RSAPublicKeySpec(java.security.spec.RSAPublicKeySpec) PemReader(org.bouncycastle.util.io.pem.PemReader) PemObject(org.bouncycastle.util.io.pem.PemObject) PKCS8EncodedKeySpec(java.security.spec.PKCS8EncodedKeySpec) StringReader(java.io.StringReader) PemObject(org.bouncycastle.util.io.pem.PemObject) KeyFactory(java.security.KeyFactory)

Example 4 with PemObject

use of org.bouncycastle.util.io.pem.PemObject in project platformlayer by platformlayer.

the class SimpleCertificateAuthority method parseCsr.

private static PKCS10CertificationRequest parseCsr(String csr) throws IOException {
    PemReader reader = new PemReader(new StringReader(csr));
    PemObject pemObject = reader.readPemObject();
    reader.close();
    PKCS10CertificationRequest csrHolder = new PKCS10CertificationRequest(pemObject.getContent());
    return csrHolder;
}
Also used : PKCS10CertificationRequest(org.bouncycastle.pkcs.PKCS10CertificationRequest) PemReader(org.bouncycastle.util.io.pem.PemReader) PemObject(org.bouncycastle.util.io.pem.PemObject) StringReader(java.io.StringReader)

Example 5 with PemObject

use of org.bouncycastle.util.io.pem.PemObject in project keywhiz by square.

the class ExpirationExtractor method expirationFromEncodedCertificateChain.

@Nullable
public static Instant expirationFromEncodedCertificateChain(byte[] content) {
    PemReader reader = new PemReader(new InputStreamReader(new ByteArrayInputStream(content), UTF_8));
    PemObject object;
    try {
        object = reader.readPemObject();
    } catch (IOException e) {
        // Should never occur (reading form byte array)
        throw Throwables.propagate(e);
    }
    Instant earliest = null;
    while (object != null) {
        if (object.getType().equals("CERTIFICATE")) {
            Instant expiry = expirationFromRawCertificate(object.getContent());
            if (earliest == null || expiry.isBefore(earliest)) {
                earliest = expiry;
            }
        }
        try {
            object = reader.readPemObject();
        } catch (IOException e) {
            // Should never occur (reading form byte array)
            throw Throwables.propagate(e);
        }
    }
    return earliest;
}
Also used : PemReader(org.bouncycastle.util.io.pem.PemReader) PemObject(org.bouncycastle.util.io.pem.PemObject) InputStreamReader(java.io.InputStreamReader) ByteArrayInputStream(java.io.ByteArrayInputStream) Instant(java.time.Instant) IOException(java.io.IOException) Nullable(javax.annotation.Nullable)

Aggregations

PemObject (org.bouncycastle.util.io.pem.PemObject)16 PemReader (org.bouncycastle.util.io.pem.PemReader)9 IOException (java.io.IOException)7 StringReader (java.io.StringReader)6 Certificate (java.security.cert.Certificate)5 PemWriter (org.bouncycastle.util.io.pem.PemWriter)5 ByteArrayInputStream (java.io.ByteArrayInputStream)4 StringWriter (java.io.StringWriter)4 PrivateKey (java.security.PrivateKey)4 PKCS8EncodedKeySpec (java.security.spec.PKCS8EncodedKeySpec)4 KeyFactory (java.security.KeyFactory)3 KeyPair (java.security.KeyPair)3 X509Certificate (java.security.cert.X509Certificate)3 RSAPrivateCrtKey (java.security.interfaces.RSAPrivateCrtKey)3 ArrayList (java.util.ArrayList)3 BigInteger (java.math.BigInteger)2 PublicKey (java.security.PublicKey)2 CertificateFactory (java.security.cert.CertificateFactory)2 DSAPrivateKey (java.security.interfaces.DSAPrivateKey)2 RSAPrivateKey (java.security.interfaces.RSAPrivateKey)2