Example usage for org.bouncycastle.asn1.x509 GeneralName uniformResourceIdentifier

List of usage examples for org.bouncycastle.asn1.x509 GeneralName uniformResourceIdentifier

Introduction

In this page you can find the example usage for org.bouncycastle.asn1.x509 GeneralName uniformResourceIdentifier.

Prototype

int uniformResourceIdentifier

To view the source code for org.bouncycastle.asn1.x509 GeneralName uniformResourceIdentifier.

Click Source Link

Usage

From source file:eu.emi.security.authn.x509.helpers.proxy.ProxyTracingExtension.java

License:Open Source License

/**
 * Returns the URL inside the proxy tracing data structure.
 * // w  w w.j  ava2 s .c  o m
 * @return The URL in String format.
 */
public String getURL() {
    if (name.getTagNo() != GeneralName.uniformResourceIdentifier)
        return null;

    DERIA5String ia5String = (DERIA5String) name.getName();
    return ia5String.getString();
}

From source file:eu.europa.ec.markt.dss.DSSUtils.java

License:Open Source License

private static String getAccessLocation(final X509Certificate certificate,
        final ASN1ObjectIdentifier accessMethod) {

    try {/*w  w w  .  ja  v  a  2 s.c  o m*/

        final byte[] authInfoAccessExtensionValue = certificate
                .getExtensionValue(Extension.authorityInfoAccess.getId());
        if (null == authInfoAccessExtensionValue) {
            return null;
        }
        /* Parse the extension */
        final ASN1InputStream asn1InputStream = new ASN1InputStream(
                new ByteArrayInputStream(authInfoAccessExtensionValue));
        final DEROctetString oct = (DEROctetString) (asn1InputStream.readObject());
        asn1InputStream.close();
        final ASN1InputStream asn1InputStream2 = new ASN1InputStream(oct.getOctets());
        final AuthorityInformationAccess authorityInformationAccess = AuthorityInformationAccess
                .getInstance(asn1InputStream2.readObject());
        asn1InputStream2.close();

        String accessLocation = null;
        final AccessDescription[] accessDescriptions = authorityInformationAccess.getAccessDescriptions();
        for (final AccessDescription accessDescription : accessDescriptions) {

            // LOG.debug("access method: " + accessDescription.getAccessMethod());
            final boolean correctAccessMethod = accessDescription.getAccessMethod().equals(accessMethod);
            if (!correctAccessMethod) {
                continue;
            }
            GeneralName gn = accessDescription.getAccessLocation();
            if (gn.getTagNo() != GeneralName.uniformResourceIdentifier) {

                // LOG.debug("not a uniform resource identifier");
                continue;
            }
            final DERIA5String str = (DERIA5String) ((DERTaggedObject) gn.toASN1Primitive()).getObject();
            accessLocation = str.getString();
            // The HTTP protocol is preferred.
            if (Protocol.isHttpUrl(accessLocation)) {
                // LOG.debug("access location: " + accessLocation);
                break;
            }
        }
        return accessLocation;
    } catch (final IOException e) {

        // we do nothing
        // LOG.("IO error: " + e.getMessage(), e);
    }
    return null;
}

From source file:eu.europa.ec.markt.dss.validation.certificate.AIACertificateSource.java

License:Open Source License

@SuppressWarnings("deprecation")
private String getAccessLocation(X509Certificate certificate, DERObjectIdentifier accessMethod) {
    try {//from  w ww  .  j a va2  s .  c  o m

        byte[] authInfoAccessExtensionValue = certificate
                .getExtensionValue(X509Extensions.AuthorityInfoAccess.getId());

        /* If the extension is not there, then return null */
        if (null == authInfoAccessExtensionValue) {
            return null;
        }

        /* Parse the extension */
        AuthorityInformationAccess authorityInformationAccess;
        DEROctetString oct = (DEROctetString) (new ASN1InputStream(
                new ByteArrayInputStream(authInfoAccessExtensionValue)).readObject());
        authorityInformationAccess = new AuthorityInformationAccess(
                (ASN1Sequence) new ASN1InputStream(oct.getOctets()).readObject());

        AccessDescription[] accessDescriptions = authorityInformationAccess.getAccessDescriptions();
        for (AccessDescription accessDescription : accessDescriptions) {
            LOG.fine("access method: " + accessDescription.getAccessMethod());
            boolean correctAccessMethod = accessDescription.getAccessMethod().equals(accessMethod);
            if (!correctAccessMethod) {
                continue;
            }
            GeneralName gn = accessDescription.getAccessLocation();
            if (gn.getTagNo() != GeneralName.uniformResourceIdentifier) {
                LOG.fine("not a uniform resource identifier");
                continue;
            }
            DERIA5String str = (DERIA5String) ((DERTaggedObject) gn.getDERObject()).getObject();
            String accessLocation = str.getString();
            LOG.fine("access location: " + accessLocation);
            return accessLocation;
        }
        return null;

    } catch (IOException e) {
        throw new RuntimeException("IO error: " + e.getMessage(), e);
    }
}

From source file:eu.europa.ec.markt.dss.validation.crl.OnlineCRLSource.java

License:Open Source License

/**
 * Gives back the CRL URI meta-data found within the given X509 certificate.
 * // w w  w  .j  ava 2  s  .  c o  m
 * @param certificate the X509 certificate.
 * @return the CRL URI, or <code>null</code> if the extension is not present.
 * @throws MalformedURLException
 */
@SuppressWarnings("deprecation")
public String getCrlUri(X509Certificate certificate) throws MalformedURLException {
    byte[] crlDistributionPointsValue = certificate
            .getExtensionValue(X509Extensions.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.fine("not a uniform resource identifier");
                continue;
            }
            String str = null;
            if (name.getDERObject() instanceof DERTaggedObject) {
                DERTaggedObject taggedObject = (DERTaggedObject) name.getDERObject();
                DERIA5String derStr = DERIA5String.getInstance(taggedObject.getObject());
                str = derStr.getString();
            } else {
                DERIA5String derStr = DERIA5String.getInstance(name.getDERObject());
                str = derStr.getString();
            }
            if (str != null && (str.startsWith("http://") || str.startsWith("https://"))) {
                return str;
            } else {
                LOG.info("Supports only http:// and https:// protocol for CRL");
            }
        }
    }
    return null;
}

From source file:eu.europa.ec.markt.dss.validation.ocsp.OnlineOCSPSource.java

License:Open Source License

@SuppressWarnings("deprecation")
private String getAccessLocation(X509Certificate certificate, DERObjectIdentifier accessMethod)
        throws IOException {
    byte[] authInfoAccessExtensionValue = certificate
            .getExtensionValue(X509Extensions.AuthorityInfoAccess.getId());
    if (null == authInfoAccessExtensionValue) {
        return null;
    }/*  w ww  .ja v  a 2s  .c om*/
    AuthorityInformationAccess authorityInformationAccess;

    DEROctetString oct = (DEROctetString) (new ASN1InputStream(
            new ByteArrayInputStream(authInfoAccessExtensionValue)).readObject());
    authorityInformationAccess = new AuthorityInformationAccess(
            (ASN1Sequence) new ASN1InputStream(oct.getOctets()).readObject());

    AccessDescription[] accessDescriptions = authorityInformationAccess.getAccessDescriptions();
    for (AccessDescription accessDescription : accessDescriptions) {
        LOG.fine("access method: " + accessDescription.getAccessMethod());
        boolean correctAccessMethod = accessDescription.getAccessMethod().equals(accessMethod);
        if (!correctAccessMethod) {
            continue;
        }
        GeneralName gn = accessDescription.getAccessLocation();
        if (gn.getTagNo() != GeneralName.uniformResourceIdentifier) {
            LOG.fine("not a uniform resource identifier");
            continue;
        }
        DERIA5String str = (DERIA5String) ((DERTaggedObject) gn.getDERObject()).getObject();
        String accessLocation = str.getString();
        LOG.fine("access location: " + accessLocation);
        return accessLocation;
    }
    return null;

}

From source file:eu.europa.ec.markt.dss.validation102853.crl.OnlineCRLSource.java

License:Open Source License

/**
 * Gives back the CRL URI meta-data found within the given X509 certificate.
 *
 * @param certificateToken the X509 certificate.
 * @return the CRL URI, or {@code null} if the extension is not present.
 * @throws DSSException/*from   w w  w.j  a v a  2s  .co  m*/
 */
public String getCrlUrl(final CertificateToken certificateToken) throws DSSException {

    final byte[] crlDistributionPointsValue = certificateToken.getCRLDistributionPoints();
    if (null == crlDistributionPointsValue) {

        return null;
    }
    ASN1InputStream ais1 = null;
    ASN1InputStream ais2 = null;
    try {

        List<String> urls = new ArrayList<String>();
        final ByteArrayInputStream bais = new ByteArrayInputStream(crlDistributionPointsValue);
        ais1 = new ASN1InputStream(bais);
        final DEROctetString oct = (DEROctetString) (ais1.readObject());
        ais2 = new ASN1InputStream(oct.getOctets());
        final ASN1Sequence seq = (ASN1Sequence) ais2.readObject();
        final CRLDistPoint distPoint = CRLDistPoint.getInstance(seq);
        final DistributionPoint[] distributionPoints = distPoint.getDistributionPoints();
        for (final DistributionPoint distributionPoint : distributionPoints) {

            final DistributionPointName distributionPointName = distributionPoint.getDistributionPoint();
            if (DistributionPointName.FULL_NAME != distributionPointName.getType()) {

                continue;
            }
            final GeneralNames generalNames = (GeneralNames) distributionPointName.getName();
            final GeneralName[] names = generalNames.getNames();
            for (final GeneralName name : names) {

                if (name.getTagNo() != GeneralName.uniformResourceIdentifier) {

                    LOG.debug("Not a uniform resource identifier");
                    continue;
                }
                final String urlStr;
                if (name.toASN1Primitive() instanceof DERTaggedObject) {

                    final DERTaggedObject taggedObject = (DERTaggedObject) name.toASN1Primitive();
                    final DERIA5String derStr = DERIA5String.getInstance(taggedObject.getObject());
                    urlStr = derStr.getString();
                } else {

                    final DERIA5String derStr = DERIA5String.getInstance(name.toASN1Primitive());
                    urlStr = derStr.getString();
                }
                urls.add(urlStr);
            }
        }
        if (preferredProtocol != null) {

            for (final String url : urls) {

                if (preferredProtocol.isTheSame(url)) {
                    return url;
                }
            }
        }
        if (urls.size() > 0) {

            final String url = urls.get(0);
            return url;
        }
        return null;
    } catch (IOException e) {

        throw new DSSException(e);
    } finally {

        DSSUtils.closeQuietly(ais1);
        DSSUtils.closeQuietly(ais2);
    }
}

From source file:eu.europa.ec.markt.dss.validation102853.ocsp.OnlineOCSPSource.java

License:Open Source License

/**
 * Gives back the OCSP URI meta-data found within the given X509 cert.
 *
 * @param certificate the X509 cert.//w  w  w .  j  a va  2s .com
 * @return the OCSP URI, or <code>null</code> if the extension is not present.
 * @throws DSSException
 */
public String getAccessLocation(final X509Certificate certificate) throws DSSException {

    final ASN1ObjectIdentifier ocspAccessMethod = X509ObjectIdentifiers.ocspAccessMethod;
    final byte[] authInfoAccessExtensionValue = certificate
            .getExtensionValue(X509Extension.authorityInfoAccess.getId());
    if (null == authInfoAccessExtensionValue) {

        return null;
    }
    ASN1InputStream ais1 = null;
    ASN1InputStream ais2 = null;
    try {

        final ByteArrayInputStream bais = new ByteArrayInputStream(authInfoAccessExtensionValue);
        ais1 = new ASN1InputStream(bais);
        final DEROctetString oct = (DEROctetString) (ais1.readObject());
        ais2 = new ASN1InputStream(oct.getOctets());
        final AuthorityInformationAccess authorityInformationAccess = AuthorityInformationAccess
                .getInstance(ais2.readObject());

        final AccessDescription[] accessDescriptions = authorityInformationAccess.getAccessDescriptions();
        for (AccessDescription accessDescription : accessDescriptions) {

            if (LOG.isDebugEnabled()) {
                LOG.debug("Access method: " + accessDescription.getAccessMethod());
            }
            final boolean correctAccessMethod = accessDescription.getAccessMethod().equals(ocspAccessMethod);
            if (!correctAccessMethod) {

                continue;
            }
            final GeneralName gn = accessDescription.getAccessLocation();
            if (gn.getTagNo() != GeneralName.uniformResourceIdentifier) {

                if (LOG.isDebugEnabled()) {
                    LOG.debug("Not a uniform resource identifier");
                }
                continue;
            }
            final DERIA5String str = (DERIA5String) ((DERTaggedObject) gn.toASN1Primitive()).getObject();
            final String accessLocation = str.getString();
            if (LOG.isDebugEnabled()) {
                LOG.debug("Access location: " + accessLocation);
            }
            return accessLocation;
        }
        return null;
    } catch (IOException e) {
        throw new DSSException(e);
    } finally {

        DSSUtils.closeQuietly(ais1);
        DSSUtils.closeQuietly(ais2);
    }
}

From source file:eu.europa.esig.dss.client.crl.OnlineCRLSource.java

License:Open Source License

/**
 * Gives back the {@code List} of CRL URI meta-data found within the given X509 certificate.
 *
 * @param certificateToken/*w w w . j a  v  a  2s .co m*/
 *            the X509 certificate
 * @return the {@code List} of CRL URI, or {@code null} if the extension is not present
 * @throws DSSException
 */
public List<String> getCrlUrl(final CertificateToken certificateToken) throws DSSException {

    final String id = Extension.cRLDistributionPoints.getId();
    final byte[] crlDistributionPointsBytes = certificateToken.getCertificate().getExtensionValue(id);

    if (null == crlDistributionPointsBytes) {

        return null;
    }
    try {

        final List<String> urls = new ArrayList<String>();
        final ASN1Sequence asn1Sequence = DSSASN1Utils
                .getAsn1SequenceFromDerOctetString(crlDistributionPointsBytes);
        final CRLDistPoint distPoint = CRLDistPoint.getInstance(asn1Sequence);
        final DistributionPoint[] distributionPoints = distPoint.getDistributionPoints();
        for (final DistributionPoint distributionPoint : distributionPoints) {

            final DistributionPointName distributionPointName = distributionPoint.getDistributionPoint();
            if (DistributionPointName.FULL_NAME != distributionPointName.getType()) {
                continue;
            }
            final GeneralNames generalNames = (GeneralNames) distributionPointName.getName();
            final GeneralName[] names = generalNames.getNames();
            for (final GeneralName name : names) {

                if (name.getTagNo() != GeneralName.uniformResourceIdentifier) {

                    LOG.debug("Not a uniform resource identifier");
                    continue;
                }
                ASN1Primitive asn1Primitive = name.toASN1Primitive();
                if (asn1Primitive instanceof DERTaggedObject) {

                    final DERTaggedObject taggedObject = (DERTaggedObject) asn1Primitive;
                    asn1Primitive = taggedObject.getObject();
                }
                final DERIA5String derStr = DERIA5String.getInstance(asn1Primitive);
                final String urlStr = derStr.getString();
                urls.add(urlStr);
            }
        }
        prioritize(urls);
        return urls;
    } catch (Exception e) {
        if (e instanceof DSSException) {
            throw (DSSException) e;
        }
        throw new DSSException(e);
    }
}

From source file:eu.europa.esig.dss.client.ocsp.OnlineOCSPSource.java

License:Open Source License

/**
 * Gives back the OCSP URI meta-data found within the given X509 cert.
 *
 * @param certificate/*from   www  .  j  a va2  s .  c  o m*/
 *            the cert token.
 * @return the OCSP URI, or <code>null</code> if the extension is not present.
 * @throws DSSException
 */
public String getAccessLocation(final CertificateToken certificate) throws DSSException {
    final byte[] authInfoAccessExtensionValue = certificate.getCertificate()
            .getExtensionValue(Extension.authorityInfoAccess.getId());
    if (ArrayUtils.isEmpty(authInfoAccessExtensionValue)) {
        return null;
    }

    ASN1InputStream ais1 = null;
    ASN1InputStream ais2 = null;
    try {
        ais1 = new ASN1InputStream(authInfoAccessExtensionValue);
        final DEROctetString oct = (DEROctetString) (ais1.readObject());
        ais2 = new ASN1InputStream(oct.getOctets());
        final AuthorityInformationAccess authorityInformationAccess = AuthorityInformationAccess
                .getInstance(ais2.readObject());

        final AccessDescription[] accessDescriptions = authorityInformationAccess.getAccessDescriptions();
        for (AccessDescription accessDescription : accessDescriptions) {
            if (logger.isDebugEnabled()) {
                logger.debug("Access method OID : " + accessDescription.getAccessMethod());
            }
            final boolean correctAccessMethod = X509ObjectIdentifiers.ocspAccessMethod
                    .equals(accessDescription.getAccessMethod());
            if (!correctAccessMethod) {
                continue;
            }
            final GeneralName gn = accessDescription.getAccessLocation();
            if (gn.getTagNo() != GeneralName.uniformResourceIdentifier) {

                if (logger.isDebugEnabled()) {
                    logger.debug("Not a uniform resource identifier");
                }
                continue;
            }
            final DERIA5String str = (DERIA5String) ((DERTaggedObject) gn.toASN1Primitive()).getObject();
            final String accessLocation = str.getString();
            if (logger.isDebugEnabled()) {
                logger.debug("Access location: " + accessLocation);
            }
            return accessLocation;
        }
        return null;
    } catch (IOException e) {
        throw new DSSException(e);
    } finally {
        IOUtils.closeQuietly(ais1);
        IOUtils.closeQuietly(ais2);
    }
}

From source file:eu.europa.esig.dss.DSSASN1Utils.java

License:Open Source License

public static List<String> getAccessLocations(final CertificateToken certificate) {
    final byte[] authInfoAccessExtensionValue = certificate.getCertificate()
            .getExtensionValue(Extension.authorityInfoAccess.getId());
    if (null == authInfoAccessExtensionValue) {
        return null;
    }// ww  w.j ava2 s .c  o m

    // Parse the extension
    ASN1Sequence asn1Sequence = null;
    try {
        asn1Sequence = DSSASN1Utils.getAsn1SequenceFromDerOctetString(authInfoAccessExtensionValue);
    } catch (DSSException e) {
        return null;
    }

    AuthorityInformationAccess authorityInformationAccess = AuthorityInformationAccess
            .getInstance(asn1Sequence);
    AccessDescription[] accessDescriptions = authorityInformationAccess.getAccessDescriptions();

    List<String> locationsUrls = new ArrayList<String>();
    for (AccessDescription accessDescription : accessDescriptions) {
        if (X509ObjectIdentifiers.id_ad_caIssuers.equals(accessDescription.getAccessMethod())) {
            GeneralName gn = accessDescription.getAccessLocation();
            if (GeneralName.uniformResourceIdentifier == gn.getTagNo()) {
                DERIA5String str = (DERIA5String) ((DERTaggedObject) gn.toASN1Primitive()).getObject();
                locationsUrls.add(str.getString());
            }
        }
    }
    return locationsUrls;
}