Example usage for org.bouncycastle.asn1.x509 DistributionPointName getType

List of usage examples for org.bouncycastle.asn1.x509 DistributionPointName getType

Introduction

In this page you can find the example usage for org.bouncycastle.asn1.x509 DistributionPointName getType.

Prototype

public int getType() 

Source Link

Document

Return the tag number applying to the underlying choice.

Usage

From source file:be.fedict.trust.crl.CrlTrustLinker.java

License:Open Source License

/**
 * Gives back the CRL URI meta-data found within the given X509 certificate.
 * //from   w w  w  .  ja va2s .c  om
 * @param certificate
 *            the X509 certificate.
 * @return the CRL URI, or <code>null</code> if the extension is not
 *         present.
 */
public static URI getCrlUri(X509Certificate certificate) {
    byte[] crlDistributionPointsValue = certificate.getExtensionValue(Extension.cRLDistributionPoints.getId());
    if (null == crlDistributionPointsValue) {
        return null;
    }
    ASN1Sequence seq;
    try {
        DEROctetString oct;
        oct = (DEROctetString) (new ASN1InputStream(new ByteArrayInputStream(crlDistributionPointsValue))
                .readObject());
        seq = (ASN1Sequence) new ASN1InputStream(oct.getOctets()).readObject();
    } catch (IOException e) {
        throw new RuntimeException("IO error: " + e.getMessage(), e);
    }
    CRLDistPoint distPoint = CRLDistPoint.getInstance(seq);
    DistributionPoint[] distributionPoints = distPoint.getDistributionPoints();
    for (DistributionPoint distributionPoint : distributionPoints) {
        DistributionPointName distributionPointName = distributionPoint.getDistributionPoint();
        if (DistributionPointName.FULL_NAME != distributionPointName.getType()) {
            continue;
        }
        GeneralNames generalNames = (GeneralNames) distributionPointName.getName();
        GeneralName[] names = generalNames.getNames();
        for (GeneralName name : names) {
            if (name.getTagNo() != GeneralName.uniformResourceIdentifier) {
                LOG.debug("not a uniform resource identifier");
                continue;
            }
            DERIA5String derStr = DERIA5String.getInstance(name.getName());
            String str = derStr.getString();
            if (false == str.startsWith("http")) {
                /*
                 * skip ldap:// protocols
                 */
                LOG.debug("not HTTP/HTTPS: " + str);
                continue;
            }
            URI uri = toURI(str);
            return uri;
        }
    }
    return null;
}

From source file:bluecrystal.bcdeps.helper.DerEncoder.java

License:Open Source License

public static List<String> getCrlDistributionPoints(byte[] crldpExt)
        throws CertificateParsingException, IOException {
    if (crldpExt == null) {
        return new ArrayList<String>();
    }//from  w  w w.  ja v  a  2 s . c o  m
    ASN1InputStream oAsnInStream = new ASN1InputStream(new ByteArrayInputStream(crldpExt));
    ASN1Primitive derObjCrlDP = oAsnInStream.readObject();
    DEROctetString dosCrlDP = (DEROctetString) derObjCrlDP;
    byte[] crldpExtOctets = dosCrlDP.getOctets();
    ASN1InputStream oAsnInStream2 = new ASN1InputStream(new ByteArrayInputStream(crldpExtOctets));
    ASN1Primitive derObj2 = oAsnInStream2.readObject();
    CRLDistPoint distPoint = CRLDistPoint.getInstance(derObj2);
    List<String> crlUrls = new ArrayList<String>();
    for (DistributionPoint dp : distPoint.getDistributionPoints()) {
        DistributionPointName dpn = dp.getDistributionPoint();
        // Look for URIs in fullName
        if (dpn != null && dpn.getType() == DistributionPointName.FULL_NAME) {
            GeneralName[] genNames = GeneralNames.getInstance(dpn.getName()).getNames();
            // Look for an URI
            for (int j = 0; j < genNames.length; j++) {
                if (genNames[j].getTagNo() == GeneralName.uniformResourceIdentifier) {
                    String url = DERIA5String.getInstance(genNames[j].getName()).getString();
                    crlUrls.add(url);
                }
            }
        }
    }
    return crlUrls;
}

From source file:com.infinities.keystone4j.ssl.CRLVerifier.java

License:Apache License

/**
 * 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.
 *//* w w w  .  j ava  2 s .  com*/
public static List<String> getCrlDistributionPoints(X509Certificate cert)
        throws CertificateParsingException, IOException {
    byte[] crldpExt = cert.getExtensionValue(X509Extension.cRLDistributionPoints.getId());
    if (crldpExt == null) {
        return new ArrayList<String>();
    }
    ASN1InputStream oAsnInStream = null;
    ASN1InputStream oAsnInStream2 = null;
    try {
        oAsnInStream = new ASN1InputStream(new ByteArrayInputStream(crldpExt));
        DERObject derObjCrlDP = oAsnInStream.readObject();
        DEROctetString dosCrlDP = (DEROctetString) derObjCrlDP;
        byte[] crldpExtOctets = dosCrlDP.getOctets();
        oAsnInStream2 = new ASN1InputStream(new ByteArrayInputStream(crldpExtOctets));
        DERObject derObj2 = oAsnInStream2.readObject();
        CRLDistPoint distPoint = CRLDistPoint.getInstance(derObj2);
        List<String> crlUrls = new ArrayList<String>();
        for (DistributionPoint dp : distPoint.getDistributionPoints()) {
            DistributionPointName dpn = dp.getDistributionPoint();
            // Look for URIs in fullName
            if (dpn != null && dpn.getType() == DistributionPointName.FULL_NAME) {
                GeneralName[] genNames = GeneralNames.getInstance(dpn.getName()).getNames();
                // Look for an URI
                for (int j = 0; j < genNames.length; j++) {
                    if (genNames[j].getTagNo() == GeneralName.uniformResourceIdentifier) {
                        String url = DERIA5String.getInstance(genNames[j].getName()).getString();
                        crlUrls.add(url);
                    }
                }
            }
        }
        return crlUrls;
    } finally {
        if (oAsnInStream != null) {
            oAsnInStream.close();
        }

        if (oAsnInStream2 != null) {
            oAsnInStream2.close();
        }
    }
}

From source file:com.itextpdf.signatures.CertificateUtil.java

License:Open Source License

/**
 * Gets the URL of the Certificate Revocation List for a Certificate
 * @param certificate   the Certificate//from   ww  w. j  a v a2  s.  c om
 * @return   the String where you can check if the certificate was revoked
 * @throws CertificateParsingException
 * @throws IOException
 */
public static String getCRLURL(X509Certificate certificate) throws CertificateParsingException {
    ASN1Primitive obj;
    try {
        obj = getExtensionValue(certificate, Extension.cRLDistributionPoints.getId());
    } catch (IOException e) {
        obj = (ASN1Primitive) null;
    }
    if (obj == null) {
        return null;
    }
    CRLDistPoint dist = CRLDistPoint.getInstance(obj);
    DistributionPoint[] dists = dist.getDistributionPoints();
    for (DistributionPoint p : dists) {
        DistributionPointName distributionPointName = p.getDistributionPoint();
        if (DistributionPointName.FULL_NAME != distributionPointName.getType()) {
            continue;
        }
        GeneralNames generalNames = (GeneralNames) distributionPointName.getName();
        GeneralName[] names = generalNames.getNames();
        for (GeneralName name : names) {
            if (name.getTagNo() != GeneralName.uniformResourceIdentifier) {
                continue;
            }
            DERIA5String derStr = DERIA5String.getInstance((ASN1TaggedObject) name.toASN1Primitive(), false);
            return derStr.getString();
        }
    }
    return null;
}

From source file:com.itextpdf.text.pdf.security.CertificateUtil.java

License:Open Source License

/**
 * Gets the URL of the Certificate Revocation List for a Certificate
 * @param certificate   the Certificate//from   w w w  .  j  a v a2 s. c o m
 * @return   the String where you can check if the certificate was revoked
 * @throws CertificateParsingException
 * @throws IOException 
 */
public static String getCRLURL(X509Certificate certificate) throws CertificateParsingException {
    ASN1Primitive obj;
    try {
        obj = getExtensionValue(certificate, Extension.cRLDistributionPoints.getId());
    } catch (IOException e) {
        obj = null;
    }
    if (obj == null) {
        return null;
    }
    CRLDistPoint dist = CRLDistPoint.getInstance(obj);
    DistributionPoint[] dists = dist.getDistributionPoints();
    for (DistributionPoint p : dists) {
        DistributionPointName distributionPointName = p.getDistributionPoint();
        if (DistributionPointName.FULL_NAME != distributionPointName.getType()) {
            continue;
        }
        GeneralNames generalNames = (GeneralNames) distributionPointName.getName();
        GeneralName[] names = generalNames.getNames();
        for (GeneralName name : names) {
            if (name.getTagNo() != GeneralName.uniformResourceIdentifier) {
                continue;
            }
            DERIA5String derStr = DERIA5String.getInstance((ASN1TaggedObject) name.toASN1Primitive(), false);
            return derStr.getString();
        }
    }
    return null;
}

From source file:com.yacme.ext.oxsit.cust_it.security.crl.X509CertRL.java

License:Open Source License

public static String[] getCrlDistributionPoint(X509Certificate certificate) throws CertificateParsingException {
    try {/*from   www . ja  va 2 s  . com*/
        //trova i DP (OID="2.5.29.31") nel certificato
        DERObject obj = getExtensionValue(certificate, "2.5.29.31");

        if (obj == null) {
            //nessun DP presente
            return null;
        }
        CRLDistPoint crldp = CRLDistPoint.getInstance(obj);
        DistributionPoint[] dp = crldp.getDistributionPoints();
        String[] urls = new String[5];

        int p = 0;
        for (int i = 0; i < dp.length; i++) {
            DistributionPointName dpn = dp[i].getDistributionPoint();
            //custom toString
            if (dpn.getType() == DistributionPointName.FULL_NAME) {
                //stx = stx+"fullName:" + term;
            } else {
                //stx = stx+"nameRelativeToCRLIssuer:" + term;                  
            }

            GeneralNames gnx = GeneralNames.getInstance(dpn.getName());
            GeneralName[] gn = gnx.getNames();

            for (int y = 0; y < gn.length; y++) {
                String aNm = decodeAGeneralName(gn[y]);
                if (aNm != null) {
                    urls[p++] = aNm;
                }
            }
        }
        return urls;
    } catch (Throwable e) {
        e.printStackTrace();
        throw new CertificateParsingException(e.toString());
    }
}

From source file:com.zimbra.cs.service.authenticator.CertUtil.java

License:Open Source License

private void printCRLDistributionPoints(PrintStream outStream) throws Exception {

    outStream.format("X509v3 CRL Distribution Points: \n");

    String extOid = X509Extension.cRLDistributionPoints.getId(); // 2.5.29.31
    byte[] extVal = cert.getExtensionValue(extOid);
    if (extVal == null) {
        return;// ww  w. j a v  a  2  s  . co  m
    }

    /* http://download.oracle.com/javase/6/docs/api/java/security/cert/X509Extension.html#getExtensionValue(java.lang.String)
     *
       The ASN.1 definition for this is:
            
     Extensions  ::=  SEQUENCE SIZE (1..MAX) OF Extension
            
     Extension  ::=  SEQUENCE  {
         extnId        OBJECT IDENTIFIER,
         critical      BOOLEAN DEFAULT FALSE,
         extnValue     OCTET STRING
                       -- contains a DER encoding of a value
                       -- of the type registered for use with
                       -- the extnId object identifier value
     }
     */

    byte[] extnValue = DEROctetString.getInstance(ASN1Object.fromByteArray(extVal)).getOctets();

    CRLDistPoint crlDistPoint = CRLDistPoint.getInstance(ASN1Object.fromByteArray(extnValue));
    DistributionPoint[] distPoints = crlDistPoint.getDistributionPoints();

    for (DistributionPoint distPoint : distPoints) {
        DistributionPointName distPointName = distPoint.getDistributionPoint();
        int type = distPointName.getType();

        if (DistributionPointName.FULL_NAME == type) {
            outStream.format("Full Name: \n");
            GeneralNames generalNames = GeneralNames.getInstance(distPointName.getName());
            GeneralName[] names = generalNames.getNames();
            for (GeneralName generalname : names) {
                int tag = generalname.getTagNo();
                if (GeneralName.uniformResourceIdentifier == tag) {
                    DEREncodable name = generalname.getName();
                    DERIA5String str = DERIA5String.getInstance(name);
                    String value = str.getString();
                    outStream.format("    %s\n", value);
                } else {
                    outStream.format("tag %d not yet implemented", tag);
                }
            }
        } else {
            outStream.format("type %d not yet implemented", type);
        }
    }
}

From source file:de.mendelson.util.security.cert.KeystoreCertificate.java

/**
 * Get extension values for CRL Distribution Points as a string list or an
 * empty list if an exception occured or the extension doesnt exist OID
 * 2.5.29.31// ww w.java2 s.  c o  m
 */
public List<String> getCrlDistributionURLs() {
    List<String> ulrList = new ArrayList<String>();
    //CRL destribution points has OID 2.5.29.31
    byte[] extensionValue = this.certificate.getExtensionValue("2.5.29.31");
    if (extensionValue == null) {
        return (ulrList);
    }
    try {
        byte[] octedBytes = ((ASN1OctetString) ASN1Primitive.fromByteArray(extensionValue)).getOctets();
        CRLDistPoint distPoint = CRLDistPoint.getInstance(ASN1Primitive.fromByteArray(octedBytes));
        DistributionPoint[] points = distPoint.getDistributionPoints();
        for (DistributionPoint point : points) {
            DistributionPointName distributionPointName = point.getDistributionPoint();
            if (distributionPointName != null) {
                if (distributionPointName.getType() == DistributionPointName.FULL_NAME) {
                    GeneralNames generalNames = (GeneralNames) distributionPointName.getName();
                    for (GeneralName generalName : generalNames.getNames()) {
                        //generalName.getTagNo() is GeneralName.uniformResourceIdentifier in this case
                        ulrList.add(((ASN1String) generalName.getName()).getString());
                    }
                }
            }
        }
    } catch (Exception e) {
        //nop
    }
    return (ulrList);
}

From source file:demo.sts.provider.cert.CRLVerifier.java

License:Apache License

/**
 * 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.
 */// ww w  .java2 s  .co  m
public static List<String> getCrlDistributionPoints(X509Certificate cert)
        throws CertificateParsingException, IOException {
    byte[] crldpExt = cert.getExtensionValue(X509Extensions.CRLDistributionPoints.getId());
    if (crldpExt == null) {
        return new ArrayList<String>();
    }
    ASN1InputStream oAsnInStream = new ASN1InputStream(new ByteArrayInputStream(crldpExt));
    DERObject derObjCrlDP = oAsnInStream.readObject();
    DEROctetString dosCrlDP = (DEROctetString) derObjCrlDP;
    byte[] crldpExtOctets = dosCrlDP.getOctets();
    ASN1InputStream oAsnInStream2 = new ASN1InputStream(new ByteArrayInputStream(crldpExtOctets));
    DERObject derObj2 = oAsnInStream2.readObject();
    CRLDistPoint distPoint = CRLDistPoint.getInstance(derObj2);
    List<String> crlUrls = new ArrayList<String>();
    for (DistributionPoint dp : distPoint.getDistributionPoints()) {
        DistributionPointName dpn = dp.getDistributionPoint();
        // Look for URIs in fullName
        if (dpn != null && dpn.getType() == DistributionPointName.FULL_NAME) {
            GeneralName[] genNames = GeneralNames.getInstance(dpn.getName()).getNames();
            // Look for an URI
            for (int j = 0; j < genNames.length; j++) {
                if (genNames[j].getTagNo() == GeneralName.uniformResourceIdentifier) {
                    String url = DERIA5String.getInstance(genNames[j].getName()).getString();
                    crlUrls.add(url);
                }
            }
        }
    }
    return crlUrls;
}

From source file:eu.emi.security.authn.x509.helpers.pkipath.bc.FixedBCPKIXCertPathReviewer.java

License:Open Source License

protected Vector getCRLDistUrls(CRLDistPoint crlDistPoints) {
    Vector urls = new Vector();

    if (crlDistPoints != null) {
        DistributionPoint[] distPoints = crlDistPoints.getDistributionPoints();
        if (distPoints == null)
            return urls;
        for (int i = 0; i < distPoints.length; i++) {
            DistributionPointName dp_name = distPoints[i].getDistributionPoint();
            if (dp_name != null && dp_name.getType() == DistributionPointName.FULL_NAME) {
                GeneralName[] generalNames = GeneralNames.getInstance(dp_name.getName()).getNames();
                for (int j = 0; j < generalNames.length; j++) {
                    if (generalNames[j].getTagNo() == GeneralName.uniformResourceIdentifier) {
                        String url = ((DERIA5String) generalNames[j].getName()).getString();
                        urls.add(url);/*ww w. j  av  a 2 s . co  m*/
                    }
                }
            }
        }
    }
    return urls;
}