Search in sources :

Example 1 with PemReader

use of org.bouncycastle.util.io.pem.PemReader 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 PemReader

use of org.bouncycastle.util.io.pem.PemReader in project gocd by gocd.

the class RegistrationJSONizer method fromJson.

public static Registration fromJson(String json) {
    Map map = GSON.fromJson(json, Map.class);
    if (map.isEmpty()) {
        return Registration.createNullPrivateKeyEntry();
    }
    List<Certificate> chain = new ArrayList<>();
    try {
        PemReader reader = new PemReader(new StringReader((String) map.get("agentPrivateKey")));
        KeyFactory kf = KeyFactory.getInstance("RSA");
        PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec(reader.readPemObject().getContent());
        PrivateKey privateKey = kf.generatePrivate(spec);
        String agentCertificate = (String) map.get("agentCertificate");
        PemReader certReader = new PemReader(new StringReader(agentCertificate));
        while (true) {
            PemObject obj = certReader.readPemObject();
            if (obj == null) {
                break;
            }
            chain.add(CertificateFactory.getInstance("X.509").generateCertificate(new ByteArrayInputStream(obj.getContent())));
        }
        return new Registration(privateKey, chain.toArray(new Certificate[chain.size()]));
    } catch (IOException | NoSuchAlgorithmException | CertificateException | InvalidKeySpecException e) {
        throw bomb(e);
    }
}
Also used : PrivateKey(java.security.PrivateKey) ArrayList(java.util.ArrayList) CertificateException(java.security.cert.CertificateException) IOException(java.io.IOException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) PemReader(org.bouncycastle.util.io.pem.PemReader) PemObject(org.bouncycastle.util.io.pem.PemObject) ByteArrayInputStream(java.io.ByteArrayInputStream) PKCS8EncodedKeySpec(java.security.spec.PKCS8EncodedKeySpec) StringReader(java.io.StringReader) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) HashMap(java.util.HashMap) Map(java.util.Map) KeyFactory(java.security.KeyFactory) Certificate(java.security.cert.Certificate)

Example 3 with PemReader

use of org.bouncycastle.util.io.pem.PemReader in project fabric-sdk-java by hyperledger.

the class CryptoPrimitives method certificateToDER.

public byte[] certificateToDER(String certificatePEM) {
    byte[] content = null;
    try (PemReader pemReader = new PemReader(new StringReader(certificatePEM))) {
        final PemObject pemObject = pemReader.readPemObject();
        content = pemObject.getContent();
    } catch (IOException e) {
    // best attempt
    }
    return content;
}
Also used : PemReader(org.bouncycastle.util.io.pem.PemReader) PemObject(org.bouncycastle.util.io.pem.PemObject) StringReader(java.io.StringReader) IOException(java.io.IOException)

Example 4 with PemReader

use of org.bouncycastle.util.io.pem.PemReader in project stdlib by petergeneric.

the class PEMHelper method loadCertificates.

/**
 * Load one or more X.509 Certificates from a PEM file
 *
 * @param pemFile
 * 		A PKCS8 PEM file containing only <code>CERTIFICATE</code> / <code>X.509 CERTIFICATE</code> blocks
 *
 * @return a JKS KeyStore with the certificate aliases "cert<code>index</code>" where index is the 0-based index of the
 * certificate in the PEM
 *
 * @throws RuntimeException
 * 		if a problem occurs
 */
public static KeyStore loadCertificates(final File pemFile) {
    try (final PemReader pem = new PemReader(new FileReader(pemFile))) {
        final KeyStore ks = createEmptyKeyStore();
        int certIndex = 0;
        Object obj;
        while ((obj = parse(pem.readPemObject())) != null) {
            if (obj instanceof Certificate) {
                final Certificate cert = (Certificate) obj;
                ks.setCertificateEntry("cert" + Integer.toString(certIndex++), cert);
            } else {
                throw new RuntimeException("Unknown PEM contents: " + obj + ". Expected a Certificate");
            }
        }
        return ks;
    } catch (Exception e) {
        throw new RuntimeException("Error parsing PEM " + pemFile, e);
    }
}
Also used : PemReader(org.bouncycastle.util.io.pem.PemReader) FileReader(java.io.FileReader) PemObject(org.bouncycastle.util.io.pem.PemObject) KeyStore(java.security.KeyStore) IOException(java.io.IOException) KeyStoreException(java.security.KeyStoreException) CertificateException(java.security.cert.CertificateException) GeneralSecurityException(java.security.GeneralSecurityException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) Certificate(java.security.cert.Certificate)

Example 5 with PemReader

use of org.bouncycastle.util.io.pem.PemReader in project azure-iot-sdk-java by Azure.

the class SecurityProviderX509CertTest method parsePublicKeyCertificateExceptionsWrappedInCertificateException.

// Tests_SRS_SecurityClientDiceEmulator_34_004: [If any exception is encountered while attempting to create the public key certificate instance, this function shall throw a CertificateException.]
@Test(expected = CertificateException.class)
public void parsePublicKeyCertificateExceptionsWrappedInCertificateException() throws CertificateException, IOException {
    // arrange
    new NonStrictExpectations() {

        {
            new PemReader(new StringReader(expectedPublicKeyCertificateString));
            result = new IOException();
        }
    };
    // act
    X509Certificate actualPublicKeyCertificate = Deencapsulation.invoke(SecurityProviderX509Cert.class, "parsePublicKeyCertificate", new Class[] { String.class }, expectedPublicKeyCertificateString);
    // assert
    assertEquals(mockedX509Certificate, actualPublicKeyCertificate);
}
Also used : PemReader(org.bouncycastle.util.io.pem.PemReader) StringReader(java.io.StringReader) IOException(java.io.IOException) X509Certificate(java.security.cert.X509Certificate) Test(org.junit.Test)

Aggregations

PemReader (org.bouncycastle.util.io.pem.PemReader)31 StringReader (java.io.StringReader)20 PemObject (org.bouncycastle.util.io.pem.PemObject)20 IOException (java.io.IOException)13 ByteArrayInputStream (java.io.ByteArrayInputStream)10 X509Certificate (java.security.cert.X509Certificate)10 PKCS8EncodedKeySpec (java.security.spec.PKCS8EncodedKeySpec)9 KeyFactory (java.security.KeyFactory)6 CertificateException (java.security.cert.CertificateException)6 CertificateFactory (java.security.cert.CertificateFactory)6 FileReader (java.io.FileReader)5 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)5 Certificate (java.security.cert.Certificate)5 X509EncodedKeySpec (java.security.spec.X509EncodedKeySpec)5 ArrayList (java.util.ArrayList)5 InputStreamReader (java.io.InputStreamReader)4 PrivateKey (java.security.PrivateKey)4 InvalidKeySpecException (java.security.spec.InvalidKeySpecException)4 PublicKey (java.security.PublicKey)3 BouncyCastleProvider (org.bouncycastle.jce.provider.BouncyCastleProvider)3