Search in sources :

Example 6 with PemReader

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

the class PEMImporter method readCertificateChain.

private static List<X509Certificate> readCertificateChain(File certificateChainFile) throws IOException, GeneralSecurityException {
    final List<X509Certificate> certs = new ArrayList<>();
    try (final PemReader pemReader = new PemReader(Files.newBufferedReader(certificateChainFile.toPath()))) {
        final PemObject pemObject = pemReader.readPemObject();
        final CertificateFactory certificateFactory = CertificateFactory.getInstance("X509");
        final ByteArrayInputStream bais = new ByteArrayInputStream(pemObject.getContent());
        for (final Certificate cert : certificateFactory.generateCertificates(bais)) {
            if (cert instanceof X509Certificate) {
                certs.add((X509Certificate) cert);
            }
        }
        if (certs.isEmpty()) {
            throw new IllegalStateException("Unable to decode certificate chain");
        }
    }
    return certs;
}
Also used : PemReader(org.bouncycastle.util.io.pem.PemReader) PemObject(org.bouncycastle.util.io.pem.PemObject) ByteArrayInputStream(java.io.ByteArrayInputStream) ArrayList(java.util.ArrayList) CertificateFactory(java.security.cert.CertificateFactory) X509Certificate(java.security.cert.X509Certificate) X509Certificate(java.security.cert.X509Certificate) Certificate(java.security.cert.Certificate)

Example 7 with PemReader

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

the class CertUtils method pemToPrivateKey.

public static PrivateKey pemToPrivateKey(final String pem) throws InvalidKeySpecException, IOException {
    final PemReader pr = new PemReader(new StringReader(pem));
    final PemObject pemObject = pr.readPemObject();
    final KeyFactory keyFactory = getKeyFactory();
    return keyFactory.generatePrivate(new PKCS8EncodedKeySpec(pemObject.getContent()));
}
Also used : PemReader(org.bouncycastle.util.io.pem.PemReader) PemObject(org.bouncycastle.util.io.pem.PemObject) PKCS8EncodedKeySpec(java.security.spec.PKCS8EncodedKeySpec) StringReader(java.io.StringReader) KeyFactory(java.security.KeyFactory)

Example 8 with PemReader

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

the class CertificateHelper method parseChain.

public static List<Certificate> parseChain(final String chain) throws IOException, CertificateException {
    Preconditions.checkNotNull(chain);
    final List<Certificate> certs = new ArrayList<Certificate>();
    try (final PemReader pemReader = new PemReader(new StringReader(chain))) {
        final PemObject pemObject = pemReader.readPemObject();
        final CertificateFactory certificateFactory = CertificateFactory.getInstance("X509");
        final ByteArrayInputStream bais = new ByteArrayInputStream(pemObject.getContent());
        for (final Certificate cert : certificateFactory.generateCertificates(bais)) {
            if (cert instanceof X509Certificate) {
                certs.add(cert);
            }
        }
        if (certs.isEmpty()) {
            throw new IllegalStateException("Unable to decode certificate chain");
        }
    }
    return certs;
}
Also used : PemReader(org.bouncycastle.util.io.pem.PemReader) PemObject(org.bouncycastle.util.io.pem.PemObject) ByteArrayInputStream(java.io.ByteArrayInputStream) ArrayList(java.util.ArrayList) StringReader(java.io.StringReader) CertificateFactory(java.security.cert.CertificateFactory) X509Certificate(java.security.cert.X509Certificate) X509Certificate(java.security.cert.X509Certificate) Certificate(java.security.cert.Certificate)

Example 9 with PemReader

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

the class IotHubSSLContextTest method parsePublicKeyCertificateExceptionsWrappedInCertificateException.

// Tests_SRS_IOTHUBSSLCONTEXT_34_034: [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(IotHubSSLContext.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)

Example 10 with PemReader

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

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