Search in sources :

Example 46 with CRLDistPoint

use of com.github.zhenwei.core.asn1.x509.CRLDistPoint in project peppol-commons by phax.

the class CRLHelper method getAllDistributionPoints.

/**
 * Extracts all CRL distribution point URLs from the "CRL Distribution Point"
 * extension in a X.509 certificate. If CRL distribution point extension is
 * unavailable, returns an empty list.
 *
 * @param aCert
 *        The certificate to extract the CRLs from
 * @return Never <code>null</code> but maybe empty list of distribution
 *         points.
 */
@Nonnull
public static ICommonsList<String> getAllDistributionPoints(@Nonnull final X509Certificate aCert) {
    ValueEnforcer.notNull(aCert, "Certificate");
    final ICommonsList<String> ret = new CommonsArrayList<>();
    // Gets the DER-encoded OCTET string for the extension value for
    // CRLDistributionPoints
    final byte[] aExtensionValue = aCert.getExtensionValue(Extension.cRLDistributionPoints.getId());
    if (aExtensionValue != null) {
        // crlDPExtensionValue is encoded in ASN.1 format.
        try (final ASN1InputStream asn1In = new ASN1InputStream(aExtensionValue)) {
            // DER (Distinguished Encoding Rules) is one of ASN.1 encoding rules
            // defined in ITU-T X.690, 2002, specification.
            // ASN.1 encoding rules can be used to encode any data object into a
            // binary file. Read the object in octets.
            final CRLDistPoint aDistPoint;
            try {
                final DEROctetString crlDEROctetString = (DEROctetString) asn1In.readObject();
                // Get Input stream in octets
                try (final ASN1InputStream asn1InOctets = new ASN1InputStream(crlDEROctetString.getOctets())) {
                    final ASN1Primitive crlDERObject = asn1InOctets.readObject();
                    aDistPoint = CRLDistPoint.getInstance(crlDERObject);
                }
            } catch (final IOException e) {
                throw new UncheckedIOException(e);
            }
            // Loop through ASN1Encodable DistributionPoints
            for (final DistributionPoint dp : aDistPoint.getDistributionPoints()) {
                // get ASN1Encodable DistributionPointName
                final DistributionPointName dpn = dp.getDistributionPoint();
                if (dpn != null && dpn.getType() == DistributionPointName.FULL_NAME) {
                    // Create ASN1Encodable General Names
                    final GeneralName[] aGenNames = GeneralNames.getInstance(dpn.getName()).getNames();
                    // Look for a URI
                    for (final GeneralName aGenName : aGenNames) {
                        if (aGenName.getTagNo() == GeneralName.uniformResourceIdentifier) {
                            // DERIA5String contains an ascii string.
                            // A IA5String is a restricted character string type in the
                            // ASN.1 notation
                            final String sURL = ASN1IA5String.getInstance(aGenName.getName()).getString().trim();
                            ret.add(sURL);
                        }
                    }
                }
            }
        } catch (final IOException ex) {
            throw new UncheckedIOException(ex);
        }
    }
    return ret;
}
Also used : ASN1InputStream(org.bouncycastle.asn1.ASN1InputStream) DistributionPointName(org.bouncycastle.asn1.x509.DistributionPointName) UncheckedIOException(java.io.UncheckedIOException) DEROctetString(org.bouncycastle.asn1.DEROctetString) ASN1IA5String(org.bouncycastle.asn1.ASN1IA5String) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) DEROctetString(org.bouncycastle.asn1.DEROctetString) DistributionPoint(org.bouncycastle.asn1.x509.DistributionPoint) GeneralName(org.bouncycastle.asn1.x509.GeneralName) CRLDistPoint(org.bouncycastle.asn1.x509.CRLDistPoint) ASN1Primitive(org.bouncycastle.asn1.ASN1Primitive) CommonsArrayList(com.helger.commons.collection.impl.CommonsArrayList) Nonnull(javax.annotation.Nonnull)

Aggregations

CRLDistPoint (org.bouncycastle.asn1.x509.CRLDistPoint)34 DistributionPoint (org.bouncycastle.asn1.x509.DistributionPoint)30 GeneralName (org.bouncycastle.asn1.x509.GeneralName)29 IOException (java.io.IOException)24 DistributionPointName (org.bouncycastle.asn1.x509.DistributionPointName)22 ArrayList (java.util.ArrayList)17 GeneralNames (org.bouncycastle.asn1.x509.GeneralNames)15 DERIA5String (org.bouncycastle.asn1.DERIA5String)14 CertPathValidatorException (java.security.cert.CertPathValidatorException)13 CRLDistPoint (com.github.zhenwei.core.asn1.x509.CRLDistPoint)11 GeneralSecurityException (java.security.GeneralSecurityException)11 ASN1Primitive (org.bouncycastle.asn1.ASN1Primitive)11 DistributionPoint (com.github.zhenwei.core.asn1.x509.DistributionPoint)10 GeneralName (com.github.zhenwei.core.asn1.x509.GeneralName)10 CRLException (java.security.cert.CRLException)10 ASN1InputStream (org.bouncycastle.asn1.ASN1InputStream)10 DistributionPointName (com.github.zhenwei.core.asn1.x509.DistributionPointName)9 ASN1OctetString (org.bouncycastle.asn1.ASN1OctetString)9 CertStoreException (java.security.cert.CertStoreException)8 HashSet (java.util.HashSet)7