Search in sources :

Example 61 with Extension

use of org.bouncycastle.asn1.x509.Extension in project nhin-d by DirectProject.

the class TrustChainValidator method downloadCertsFromAIA.

/**
	 * Downloads certificates from the AIA URL and returns the result as a collection of certificates.
	 * @param url The URL listed in the AIA extension to locate the certificates.
	 * @return The certificates downloaded from the AIA extension URL
	 */
@SuppressWarnings("unchecked")
protected Collection<X509Certificate> downloadCertsFromAIA(String url) throws NHINDException {
    InputStream inputStream = null;
    Collection<? extends Certificate> retVal = null;
    try {
        // in this case the cert is a binary representation
        // of the CERT URL... transform to a string
        final URL certURL = new URL(url);
        final URLConnection connection = certURL.openConnection();
        // the connection is not actually made until the input stream
        // is open, so set the timeouts before getting the stream
        connection.setConnectTimeout(DEFAULT_URL_CONNECTION_TIMEOUT);
        connection.setReadTimeout(DEFAULT_URL_READ_TIMEOUT);
        // open the URL as in input stream
        inputStream = connection.getInputStream();
        // download the 
        retVal = CertificateFactory.getInstance("X.509").generateCertificates(inputStream);
    } catch (Exception e) {
        throw new NHINDException("Failed to download certificates from AIA extension.", e);
    } finally {
        IOUtils.closeQuietly(inputStream);
    }
    return (Collection<X509Certificate>) retVal;
}
Also used : ASN1InputStream(org.bouncycastle.asn1.ASN1InputStream) InputStream(java.io.InputStream) Collection(java.util.Collection) NHINDException(org.nhindirect.stagent.NHINDException) URL(java.net.URL) URLConnection(java.net.URLConnection) CertificateParsingException(java.security.cert.CertificateParsingException) AddressException(javax.mail.internet.AddressException) PolicyProcessException(org.nhindirect.policy.PolicyProcessException) NHINDException(org.nhindirect.stagent.NHINDException)

Example 62 with Extension

use of org.bouncycastle.asn1.x509.Extension in project nhin-d by DirectProject.

the class TrustChainValidator method getIntermediateCertsByAIA.

/**
     * Retrieves intermediate certificate using the AIA extension.
     * @param certificate The certificate to search for AIA extensions.
     * @return Returns a collection of intermediate certs using the AIA extension.  If the AIA extension does not exists
     * or the certificate cannot be downloaded from the URL, then an empty list is returned.
     */
protected Collection<X509Certificate> getIntermediateCertsByAIA(X509Certificate certificate) {
    final Collection<X509Certificate> retVal = new ArrayList<X509Certificate>();
    // check to see if there are extensions
    final AuthorityInfoAccessExtentionField aiaField = new AuthorityInfoAccessExtentionField(false);
    try {
        // we can get all names from the AuthorityInfoAccessExtentionField objects
        aiaField.injectReferenceValue(certificate);
        final Collection<String> urlPairs = aiaField.getPolicyValue().getPolicyValue();
        // look through all of the values (if they exist) for caIssuers
        for (String urlPair : urlPairs) {
            if (urlPair.startsWith(CA_ISSUER_CHECK_STRING)) {
                // the url pair is in the format of caIssuer:URL... need to break it 
                // apart to get the url
                final String url = urlPair.substring(CA_ISSUER_CHECK_STRING.length());
                // now pull the certificate from the URL
                try {
                    final Collection<X509Certificate> intermCerts = downloadCertsFromAIA(url);
                    retVal.addAll(intermCerts);
                } catch (NHINDException e) {
                    LOGGER.warn("Intermediate cert cannot be resolved from AIA extension.", e);
                }
            }
        }
    }///CLOVER:OFF
     catch (PolicyProcessException e) {
        LOGGER.warn("Intermediate cert cannot be resolved from AIA extension.", e);
    }
    return retVal;
}
Also used : AuthorityInfoAccessExtentionField(org.nhindirect.policy.x509.AuthorityInfoAccessExtentionField) ArrayList(java.util.ArrayList) ASN1OctetString(org.bouncycastle.asn1.ASN1OctetString) NHINDException(org.nhindirect.stagent.NHINDException) X509Certificate(java.security.cert.X509Certificate) PolicyProcessException(org.nhindirect.policy.PolicyProcessException)

Example 63 with Extension

use of org.bouncycastle.asn1.x509.Extension in project robovm by robovm.

the class X509CRLEntryObject method getExtensionOIDs.

private Set getExtensionOIDs(boolean critical) {
    Extensions extensions = c.getExtensions();
    if (extensions != null) {
        Set set = new HashSet();
        Enumeration e = extensions.oids();
        while (e.hasMoreElements()) {
            ASN1ObjectIdentifier oid = (ASN1ObjectIdentifier) e.nextElement();
            Extension ext = extensions.getExtension(oid);
            if (critical == ext.isCritical()) {
                set.add(oid.getId());
            }
        }
        return set;
    }
    return null;
}
Also used : Extension(org.bouncycastle.asn1.x509.Extension) X509Extension(org.bouncycastle.asn1.x509.X509Extension) Set(java.util.Set) HashSet(java.util.HashSet) Enumeration(java.util.Enumeration) Extensions(org.bouncycastle.asn1.x509.Extensions) ASN1ObjectIdentifier(org.bouncycastle.asn1.ASN1ObjectIdentifier) HashSet(java.util.HashSet)

Example 64 with Extension

use of org.bouncycastle.asn1.x509.Extension in project robovm by robovm.

the class X509CRLObject method isRevoked.

/**
     * Checks whether the given certificate is on this CRL.
     *
     * @param cert the certificate to check for.
     * @return true if the given certificate is on this CRL,
     * false otherwise.
     */
public boolean isRevoked(Certificate cert) {
    if (!cert.getType().equals("X.509")) {
        throw new RuntimeException("X.509 CRL used with non X.509 Cert");
    }
    TBSCertList.CRLEntry[] certs = c.getRevokedCertificates();
    X500Name caName = c.getIssuer();
    if (certs != null) {
        BigInteger serial = ((X509Certificate) cert).getSerialNumber();
        for (int i = 0; i < certs.length; i++) {
            if (isIndirect && certs[i].hasExtensions()) {
                Extension currentCaName = certs[i].getExtensions().getExtension(Extension.certificateIssuer);
                if (currentCaName != null) {
                    caName = X500Name.getInstance(GeneralNames.getInstance(currentCaName.getParsedValue()).getNames()[0].getName());
                }
            }
            if (certs[i].getUserCertificate().getValue().equals(serial)) {
                X500Name issuer;
                if (cert instanceof X509Certificate) {
                    issuer = X500Name.getInstance(((X509Certificate) cert).getIssuerX500Principal().getEncoded());
                } else {
                    try {
                        issuer = org.bouncycastle.asn1.x509.Certificate.getInstance(cert.getEncoded()).getIssuer();
                    } catch (CertificateEncodingException e) {
                        throw new RuntimeException("Cannot process certificate");
                    }
                }
                if (!caName.equals(issuer)) {
                    return false;
                }
                return true;
            }
        }
    }
    return false;
}
Also used : Extension(org.bouncycastle.asn1.x509.Extension) BigInteger(java.math.BigInteger) CertificateEncodingException(java.security.cert.CertificateEncodingException) X509CRLEntry(java.security.cert.X509CRLEntry) X500Name(org.bouncycastle.asn1.x500.X500Name) X509Certificate(java.security.cert.X509Certificate) IssuingDistributionPoint(org.bouncycastle.asn1.x509.IssuingDistributionPoint) CRLDistPoint(org.bouncycastle.asn1.x509.CRLDistPoint)

Example 65 with Extension

use of org.bouncycastle.asn1.x509.Extension in project robovm by robovm.

the class X509CRLObject method getRevokedCertificate.

public X509CRLEntry getRevokedCertificate(BigInteger serialNumber) {
    Enumeration certs = c.getRevokedCertificateEnumeration();
    // the issuer
    X500Name previousCertificateIssuer = null;
    while (certs.hasMoreElements()) {
        TBSCertList.CRLEntry entry = (TBSCertList.CRLEntry) certs.nextElement();
        if (serialNumber.equals(entry.getUserCertificate().getValue())) {
            return new X509CRLEntryObject(entry, isIndirect, previousCertificateIssuer);
        }
        if (isIndirect && entry.hasExtensions()) {
            Extension currentCaName = entry.getExtensions().getExtension(Extension.certificateIssuer);
            if (currentCaName != null) {
                previousCertificateIssuer = X500Name.getInstance(GeneralNames.getInstance(currentCaName.getParsedValue()).getNames()[0].getName());
            }
        }
    }
    return null;
}
Also used : Extension(org.bouncycastle.asn1.x509.Extension) Enumeration(java.util.Enumeration) TBSCertList(org.bouncycastle.asn1.x509.TBSCertList) X500Name(org.bouncycastle.asn1.x500.X500Name) X509CRLEntry(java.security.cert.X509CRLEntry)

Aggregations

IOException (java.io.IOException)52 Enumeration (java.util.Enumeration)37 ArrayList (java.util.ArrayList)36 ExtCertPathValidatorException (org.bouncycastle.jce.exception.ExtCertPathValidatorException)36 List (java.util.List)35 CertPathValidatorException (java.security.cert.CertPathValidatorException)34 X509Certificate (java.security.cert.X509Certificate)34 GeneralSecurityException (java.security.GeneralSecurityException)33 CertificateExpiredException (java.security.cert.CertificateExpiredException)31 CertificateNotYetValidException (java.security.cert.CertificateNotYetValidException)31 CRLDistPoint (org.bouncycastle.asn1.x509.CRLDistPoint)31 IssuingDistributionPoint (org.bouncycastle.asn1.x509.IssuingDistributionPoint)31 DistributionPoint (org.bouncycastle.asn1.x509.DistributionPoint)28 CertPathBuilderException (java.security.cert.CertPathBuilderException)26 Extension (org.bouncycastle.asn1.x509.Extension)25 ASN1InputStream (org.bouncycastle.asn1.ASN1InputStream)22 ASN1Sequence (org.bouncycastle.asn1.ASN1Sequence)22 HashSet (java.util.HashSet)21 Set (java.util.Set)21 ASN1ObjectIdentifier (org.bouncycastle.asn1.ASN1ObjectIdentifier)20