Search in sources :

Example 11 with PemObject

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

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

the class SimpleSignTest method setUp.

@Before
public void setUp() throws Exception {
    encryptionService = mock(PasswordEncryptor.class);
    systemCrypto = new SystemCrypto("encryption.properties", "signature.properties", encryptionService);
    simpleSign = new SimpleSign(systemCrypto);
    cannedResponse = Resources.toString(Resources.getResource(getClass(), "/SAMLResponse.xml"), Charsets.UTF_8);
    //Normally you would have the cert in a string already but for this test we will have to pull it out of the jks file
    Certificate cert = ((Merlin) systemCrypto.getSignatureCrypto()).getKeyStore().getCertificate("dsa");
    StringWriter writer = new StringWriter();
    PemWriter pemWriter = new PemWriter(writer);
    pemWriter.writeObject(new PemObject("CERTIFICATE", cert.getEncoded()));
    pemWriter.flush();
    dsaCert = writer.toString().replace("-----BEGIN CERTIFICATE-----", "").replace("-----END CERTIFICATE-----", "");
}
Also used : PemObject(org.bouncycastle.util.io.pem.PemObject) StringWriter(java.io.StringWriter) PemWriter(org.bouncycastle.util.io.pem.PemWriter) PasswordEncryptor(org.apache.wss4j.common.crypto.PasswordEncryptor) Certificate(java.security.cert.Certificate) Before(org.junit.Before)

Example 13 with PemObject

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

the class CsrParser method parsePemFormat.

private PKCS10CertificationRequest parsePemFormat(String data) throws IOException {
    PemReader reader = new PemReader(new StringReader(data));
    PemObject pemObject = reader.readPemObject();
    reader.close();
    PKCS10CertificationRequest csr = new PKCS10CertificationRequest(pemObject.getContent());
    return csr;
}
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 14 with PemObject

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

the class Csr method getEncoded.

public String getEncoded() {
    StringWriter stringWriter = new StringWriter();
    try {
        PemWriter writer = new PemWriter(stringWriter);
        PemObjectGenerator pemObject = new PemObject("CERTIFICATE REQUEST", csr.getEncoded());
        writer.writeObject(pemObject);
        writer.close();
    } catch (IOException e) {
        throw new IllegalArgumentException("Error generating PEM", e);
    }
    return stringWriter.toString();
}
Also used : PemObject(org.bouncycastle.util.io.pem.PemObject) PemObjectGenerator(org.bouncycastle.util.io.pem.PemObjectGenerator) StringWriter(java.io.StringWriter) PemWriter(org.bouncycastle.util.io.pem.PemWriter) IOException(java.io.IOException)

Example 15 with PemObject

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

the class Certificates method loadPrivateKey.

public PrivateKey loadPrivateKey(File privateKeyFile) throws IOException, NoSuchAlgorithmException, InvalidKeySpecException, NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException {
    try (PemReader r = new PemReader(new FileReader(privateKeyFile))) {
        PemObject pemObject = r.readPemObject();
        if (pemObject != null) {
            byte[] encodedKey = pemObject.getContent();
            KeySpec keySpec = new PKCS8EncodedKeySpec(encodedKey);
            try {
                return KeyFactory.getInstance("RSA").generatePrivate(keySpec);
            } catch (InvalidKeySpecException ignore) {
                try {
                    return KeyFactory.getInstance("DSA").generatePrivate(keySpec);
                } catch (InvalidKeySpecException ignore2) {
                    try {
                        return KeyFactory.getInstance("EC").generatePrivate(keySpec);
                    } catch (InvalidKeySpecException e) {
                        throw new InvalidKeySpecException("Neither RSA, DSA nor EC worked", e);
                    }
                }
            }
        }
    }
    // Ok, failed to read as PEM file, try and read it as a raw binary private key
    try (DataInputStream in = new DataInputStream(new FileInputStream(privateKeyFile))) {
        byte[] keyBytes = new byte[(int) privateKeyFile.length()];
        in.readFully(keyBytes);
        KeySpec keySpec = new PKCS8EncodedKeySpec(keyBytes);
        return KeyFactory.getInstance(DEFAULT_ENCRYPTION).generatePrivate(keySpec);
    }
}
Also used : PemReader(org.bouncycastle.util.io.pem.PemReader) PemObject(org.bouncycastle.util.io.pem.PemObject) KeySpec(java.security.spec.KeySpec) PKCS8EncodedKeySpec(java.security.spec.PKCS8EncodedKeySpec) PKCS8EncodedKeySpec(java.security.spec.PKCS8EncodedKeySpec) FileReader(java.io.FileReader) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) DataInputStream(java.io.DataInputStream) FileInputStream(java.io.FileInputStream)

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