Search in sources :

Example 1 with X509TrustedCertificateBlock

use of org.bouncycastle.openssl.X509TrustedCertificateBlock in project keystore-explorer by kaikramer.

the class X509CertUtil method loadAsPEM.

private static List<X509Certificate> loadAsPEM(byte[] bytes, CertificateFactory cf) {
    PEMParser pemParser = new PEMParser(new StringReader(new String(bytes)));
    JcaX509CertificateConverter jcaX509CertConverter = new JcaX509CertificateConverter();
    List<X509Certificate> certs = new ArrayList<>();
    try {
        Object pemObject = pemParser.readObject();
        while (pemObject != null) {
            // check for all possible certificate classes
            if (pemObject instanceof X509CertificateHolder) {
                certs.add(jcaX509CertConverter.getCertificate((X509CertificateHolder) pemObject));
            } else if (pemObject instanceof X509TrustedCertificateBlock) {
                X509TrustedCertificateBlock trustedCertBlock = (X509TrustedCertificateBlock) pemObject;
                certs.add(jcaX509CertConverter.getCertificate(trustedCertBlock.getCertificateHolder()));
            } else if (pemObject instanceof ContentInfo) {
                ContentInfo contentInfo = (ContentInfo) pemObject;
                Collection<? extends Certificate> certsFromPkcs7 = cf.generateCertificates(new ByteArrayInputStream(contentInfo.getEncoded()));
                if (!certsFromPkcs7.isEmpty()) {
                    List<X509Certificate> x509Certificates = convertCertificates(certsFromPkcs7);
                    certs.addAll(x509Certificates);
                }
            }
            pemObject = pemParser.readObject();
        }
        return certs;
    } catch (IOException | CertificateException | CryptoException e) {
        return certs;
    }
}
Also used : ArrayList(java.util.ArrayList) CertificateException(java.security.cert.CertificateException) IOException(java.io.IOException) X509Certificate(java.security.cert.X509Certificate) X509TrustedCertificateBlock(org.bouncycastle.openssl.X509TrustedCertificateBlock) PEMParser(org.bouncycastle.openssl.PEMParser) ContentInfo(org.bouncycastle.asn1.cms.ContentInfo) ByteArrayInputStream(java.io.ByteArrayInputStream) JcaX509CertificateConverter(org.bouncycastle.cert.jcajce.JcaX509CertificateConverter) X509CertificateHolder(org.bouncycastle.cert.X509CertificateHolder) StringReader(java.io.StringReader) CryptoException(org.kse.crypto.CryptoException)

Aggregations

ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 StringReader (java.io.StringReader)1 CertificateException (java.security.cert.CertificateException)1 X509Certificate (java.security.cert.X509Certificate)1 ArrayList (java.util.ArrayList)1 ContentInfo (org.bouncycastle.asn1.cms.ContentInfo)1 X509CertificateHolder (org.bouncycastle.cert.X509CertificateHolder)1 JcaX509CertificateConverter (org.bouncycastle.cert.jcajce.JcaX509CertificateConverter)1 PEMParser (org.bouncycastle.openssl.PEMParser)1 X509TrustedCertificateBlock (org.bouncycastle.openssl.X509TrustedCertificateBlock)1 CryptoException (org.kse.crypto.CryptoException)1