Search in sources :

Example 1 with CertificateParsingException

use of java.security.cert.CertificateParsingException in project XobotOS by xamarin.

the class PKIXCertPathBuilderSpi method build.

protected CertPathBuilderResult build(X509Certificate tbvCert, ExtendedPKIXBuilderParameters pkixParams, List tbvPath) {
    // PKI graph.
    if (tbvPath.contains(tbvCert)) {
        return null;
    }
    // chain.
    if (pkixParams.getExcludedCerts().contains(tbvCert)) {
        return null;
    }
    // test if certificate path exceeds maximum length
    if (pkixParams.getMaxPathLength() != -1) {
        if (tbvPath.size() - 1 > pkixParams.getMaxPathLength()) {
            return null;
        }
    }
    tbvPath.add(tbvCert);
    CertificateFactory cFact;
    CertPathValidator validator;
    CertPathBuilderResult builderResult = null;
    try {
        cFact = CertificateFactory.getInstance("X.509", BouncyCastleProvider.PROVIDER_NAME);
        validator = CertPathValidator.getInstance("PKIX", BouncyCastleProvider.PROVIDER_NAME);
    } catch (Exception e) {
        // cannot happen
        throw new RuntimeException("Exception creating support classes.");
    }
    try {
        // check whether the issuer of <tbvCert> is a TrustAnchor
        if (CertPathValidatorUtilities.findTrustAnchor(tbvCert, pkixParams.getTrustAnchors(), pkixParams.getSigProvider()) != null) {
            // exception message from possibly later tried certification
            // chains
            CertPath certPath = null;
            PKIXCertPathValidatorResult result = null;
            try {
                certPath = cFact.generateCertPath(tbvPath);
            } catch (Exception e) {
                throw new AnnotatedException("Certification path could not be constructed from certificate list.", e);
            }
            try {
                result = (PKIXCertPathValidatorResult) validator.validate(certPath, pkixParams);
            } catch (Exception e) {
                throw new AnnotatedException("Certification path could not be validated.", e);
            }
            return new PKIXCertPathBuilderResult(certPath, result.getTrustAnchor(), result.getPolicyTree(), result.getPublicKey());
        } else {
            // add additional X.509 stores from locations in certificate
            try {
                CertPathValidatorUtilities.addAdditionalStoresFromAltNames(tbvCert, pkixParams);
            } catch (CertificateParsingException e) {
                throw new AnnotatedException("No additiontal X.509 stores can be added from certificate locations.", e);
            }
            Collection issuers = new HashSet();
            // of the stores
            try {
                issuers.addAll(CertPathValidatorUtilities.findIssuerCerts(tbvCert, pkixParams));
            } catch (AnnotatedException e) {
                throw new AnnotatedException("Cannot find issuer certificate for certificate in certification path.", e);
            }
            if (issuers.isEmpty()) {
                throw new AnnotatedException("No issuer certificate for certificate in certification path found.");
            }
            Iterator it = issuers.iterator();
            while (it.hasNext() && builderResult == null) {
                X509Certificate issuer = (X509Certificate) it.next();
                builderResult = build(issuer, pkixParams, tbvPath);
            }
        }
    } catch (AnnotatedException e) {
        certPathException = e;
    }
    if (builderResult == null) {
        tbvPath.remove(tbvCert);
    }
    return builderResult;
}
Also used : CertificateParsingException(java.security.cert.CertificateParsingException) CertPathBuilderResult(java.security.cert.CertPathBuilderResult) PKIXCertPathBuilderResult(java.security.cert.PKIXCertPathBuilderResult) CertificateFactory(java.security.cert.CertificateFactory) CertificateParsingException(java.security.cert.CertificateParsingException) ExtCertPathBuilderException(org.bouncycastle.jce.exception.ExtCertPathBuilderException) InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) CertPathBuilderException(java.security.cert.CertPathBuilderException) X509Certificate(java.security.cert.X509Certificate) CertPathValidator(java.security.cert.CertPathValidator) PKIXCertPathValidatorResult(java.security.cert.PKIXCertPathValidatorResult) PKIXCertPathBuilderResult(java.security.cert.PKIXCertPathBuilderResult) Iterator(java.util.Iterator) Collection(java.util.Collection) CertPath(java.security.cert.CertPath) HashSet(java.util.HashSet)

Example 2 with CertificateParsingException

use of java.security.cert.CertificateParsingException in project nhin-d by DirectProject.

the class CryptoExtensions method getSubjectAddress.

/**
     * Gets the address name associated with the certificate.  It may be an email address or a domain name.
     * @param certificate The certificate to search
     * @return The address of domain associated with a certificate.
     */
public static String getSubjectAddress(X509Certificate certificate) {
    String address = "";
    // check alternative names first
    Collection<List<?>> altNames = null;
    try {
        altNames = certificate.getSubjectAlternativeNames();
    } catch (CertificateParsingException ex) {
    /* no -op */
    }
    if (altNames != null) {
        for (List<?> entries : altNames) {
            if (// should always be the case according the altNames spec, but checking to be defensive
            entries.size() >= 2) {
                Integer nameType = (Integer) entries.get(0);
                // prefer email over over domain?
                if (nameType == RFC822Name_TYPE)
                    address = (String) entries.get(1);
                else if (nameType == DNSName_TYPE && address.isEmpty())
                    address = (String) entries.get(1);
            }
        }
    }
    if (!address.isEmpty())
        return address;
    // can't find issuer address in alt names... try the principal 
    X500Principal issuerPrin = certificate.getSubjectX500Principal();
    // get the domain name
    Map<String, String> oidMap = new HashMap<String, String>();
    // OID for email address
    oidMap.put("1.2.840.113549.1.9.1", "EMAILADDRESS");
    String prinName = issuerPrin.getName(X500Principal.RFC1779, oidMap);
    // see if there is an email address first in the DN
    String searchString = "EMAILADDRESS=";
    int index = prinName.indexOf(searchString);
    if (index == -1) {
        searchString = "CN=";
        // no Email.. check the CN
        index = prinName.indexOf(searchString);
        if (index == -1)
            // no CN... nothing else that can be done from here
            return "";
    }
    // look for a "," to find the end of this attribute
    int endIndex = prinName.indexOf(",", index);
    if (endIndex > -1)
        address = prinName.substring(index + searchString.length(), endIndex);
    else
        address = prinName.substring(index + searchString.length());
    return address;
}
Also used : CertificateParsingException(java.security.cert.CertificateParsingException) HashMap(java.util.HashMap) X500Principal(javax.security.auth.x500.X500Principal) ArrayList(java.util.ArrayList) List(java.util.List) Thumbprint(org.nhindirect.trustbundle.cert.Thumbprint)

Example 3 with CertificateParsingException

use of java.security.cert.CertificateParsingException in project nhin-d by DirectProject.

the class CryptoExtensions method getSubjectAddress.

/**
     * Gets the address name associated with the certificate.  It may be an email address or a domain name.
     * @param certificate The certificate to search
     * @return The address of domain associated with a certificate.
     */
public static String getSubjectAddress(X509Certificate certificate) {
    String address = "";
    // check alternative names first
    Collection<List<?>> altNames = null;
    try {
        altNames = certificate.getSubjectAlternativeNames();
    } catch (CertificateParsingException ex) {
    /* no -op */
    }
    if (altNames != null) {
        for (List<?> entries : altNames) {
            if (// should always be the case according the altNames spec, but checking to be defensive
            entries.size() >= 2) {
                Integer nameType = (Integer) entries.get(0);
                // prefer email over over domain?
                if (nameType == RFC822Name_TYPE)
                    address = (String) entries.get(1);
                else if (nameType == DNSName_TYPE && address.isEmpty())
                    address = (String) entries.get(1);
            }
        }
    }
    if (!address.isEmpty())
        return address;
    // can't find issuer address in alt names... try the principal 
    X500Principal issuerPrin = certificate.getSubjectX500Principal();
    // get the domain name
    Map<String, String> oidMap = new HashMap<String, String>();
    // OID for email address
    oidMap.put("1.2.840.113549.1.9.1", "EMAILADDRESS");
    String prinName = issuerPrin.getName(X500Principal.RFC1779, oidMap);
    // see if there is an email address first in the DN
    String searchString = "EMAILADDRESS=";
    int index = prinName.indexOf(searchString);
    if (index == -1) {
        searchString = "CN=";
        // no Email.. check the CN
        index = prinName.indexOf(searchString);
        if (index == -1)
            // no CN... nothing else that can be done from here
            return "";
    }
    // look for a "," to find the end of this attribute
    int endIndex = prinName.indexOf(",", index);
    if (endIndex > -1)
        address = prinName.substring(index + searchString.length(), endIndex);
    else
        address = prinName.substring(index + searchString.length());
    return address;
}
Also used : CertificateParsingException(java.security.cert.CertificateParsingException) HashMap(java.util.HashMap) X500Principal(javax.security.auth.x500.X500Principal) ArrayList(java.util.ArrayList) List(java.util.List) Thumbprint(org.nhindirect.stagent.cert.Thumbprint)

Example 4 with CertificateParsingException

use of java.security.cert.CertificateParsingException in project nhin-d by DirectProject.

the class TrustChainValidator method getIssuerAddress.

private String getIssuerAddress(X509Certificate certificate) {
    String address = "";
    // check alternative names first
    Collection<List<?>> altNames = null;
    try {
        altNames = certificate.getIssuerAlternativeNames();
    } catch (CertificateParsingException ex) {
    /* no -op */
    }
    if (altNames != null) {
        for (List<?> entries : altNames) {
            if (// should always be the case according the altNames spec, but checking to be defensive
            entries.size() >= 2) {
                Integer nameType = (Integer) entries.get(0);
                // prefer email over over domain?
                if (nameType == RFC822Name_TYPE)
                    address = (String) entries.get(1);
                else if (nameType == DNSName_TYPE && address.isEmpty())
                    address = (String) entries.get(1);
            }
        }
    }
    if (!address.isEmpty())
        return address;
    // can't find issuer address in alt names... try the principal 
    X500Principal issuerPrin = certificate.getIssuerX500Principal();
    // get the domain name
    Map<String, String> oidMap = new HashMap<String, String>();
    // OID for email address
    oidMap.put("1.2.840.113549.1.9.1", "EMAILADDRESS");
    String prinName = issuerPrin.getName(X500Principal.RFC1779, oidMap);
    // see if there is an email address first in the DN
    String searchString = "EMAILADDRESS=";
    int index = prinName.indexOf(searchString);
    if (index == -1) {
        searchString = "CN=";
        // no Email.. check the CN
        index = prinName.indexOf(searchString);
        if (index == -1)
            // no CN... nothing else that can be done from here
            return "";
    }
    // look for a "," to find the end of this attribute
    int endIndex = prinName.indexOf(",", index);
    if (endIndex > -1)
        address = prinName.substring(index + searchString.length(), endIndex);
    else
        address = prinName.substring(index + searchString.length());
    return address;
}
Also used : CertificateParsingException(java.security.cert.CertificateParsingException) HashMap(java.util.HashMap) X500Principal(javax.security.auth.x500.X500Principal) ArrayList(java.util.ArrayList) List(java.util.List) ASN1OctetString(org.bouncycastle.asn1.ASN1OctetString) Thumbprint(org.nhindirect.stagent.cert.Thumbprint)

Example 5 with CertificateParsingException

use of java.security.cert.CertificateParsingException in project OpenAM by OpenRock.

the class AMCertStore method getCertificate.

/**
     * Return matched certificate from ldap certificate store 
     */
public X509Certificate getCertificate() {
    /*
         * Lookup the certificate in the LDAP certificate
         * directory and compare the values.
         */
    try (Connection ldc = getConnection()) {
        if (ldc == null) {
            return null;
        }
        ConnectionEntryReader results = getSearchResults(ldc, USERCERTIFICATE, USERCERTIFICATE_BINARY, CACERTIFICATE, CACERTIFICATE_BINARY);
        while (results != null && results.hasNext()) {
            // "Found search results for: " + cn , 2);
            if (results.isEntry()) {
                SearchResultEntry entry = results.readEntry();
                /*
                     * Retrieve the certificate from the store
                     */
                Attribute certAttribute = entry.getAttribute(USERCERTIFICATE);
                if (certAttribute == null) {
                    certAttribute = entry.getAttribute(USERCERTIFICATE_BINARY);
                    if (certAttribute == null) {
                        // an end-entity certificate can be a CA certificate
                        certAttribute = entry.getAttribute(CACERTIFICATE);
                        if (certAttribute == null) {
                            certAttribute = entry.getAttribute(CACERTIFICATE_BINARY);
                        }
                        if (certAttribute == null) {
                            debug.message("AMCertStore.getCertificate: Certificate - get usercertificate is null ");
                            continue;
                        }
                    }
                }
                for (ByteString value : certAttribute) {
                    byte[] bytes = value.toByteArray();
                    ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
                    X509Certificate c = null;
                    try {
                        c = (X509Certificate) cf.generateCertificate(bis);
                    } catch (CertificateParsingException e) {
                        debug.error("AMCertStore.getCertificate : " + "Error in Certificate parsing : ", e);
                    }
                    if (c != null) {
                        return c;
                    }
                }
            // inner while
            } else {
                SearchResultReference reference = results.readReference();
                debug.warning("Got an LDAP reference - only expected entries. Ignoring: {}", reference);
            }
        }
    // outer while  
    } catch (Exception e) {
        debug.error("AMCertStore.getCertificate : " + "Certificate - Error finding registered certificate = ", e);
    }
    return null;
}
Also used : ConnectionEntryReader(org.forgerock.opendj.ldif.ConnectionEntryReader) CertificateParsingException(java.security.cert.CertificateParsingException) Attribute(org.forgerock.opendj.ldap.Attribute) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteString(org.forgerock.opendj.ldap.ByteString) Connection(org.forgerock.opendj.ldap.Connection) SearchResultReference(org.forgerock.opendj.ldap.responses.SearchResultReference) X509Certificate(java.security.cert.X509Certificate) CertificateParsingException(java.security.cert.CertificateParsingException) LdapException(org.forgerock.opendj.ldap.LdapException) GeneralSecurityException(java.security.GeneralSecurityException) IOException(java.io.IOException) CertificateException(java.security.cert.CertificateException) SearchResultEntry(org.forgerock.opendj.ldap.responses.SearchResultEntry)

Aggregations

CertificateParsingException (java.security.cert.CertificateParsingException)72 List (java.util.List)25 IOException (java.io.IOException)18 ArrayList (java.util.ArrayList)18 X509Certificate (java.security.cert.X509Certificate)15 CertificateException (java.security.cert.CertificateException)13 Collection (java.util.Collection)12 X500Principal (javax.security.auth.x500.X500Principal)11 BigInteger (java.math.BigInteger)8 InvalidKeyException (java.security.InvalidKeyException)7 HashMap (java.util.HashMap)7 DERIA5String (org.bouncycastle.asn1.DERIA5String)7 DEROctetString (org.bouncycastle.asn1.DEROctetString)7 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)6 NoSuchProviderException (java.security.NoSuchProviderException)6 SignatureException (java.security.SignatureException)6 CertificateEncodingException (java.security.cert.CertificateEncodingException)6 CertificateExpiredException (java.security.cert.CertificateExpiredException)6 CertificateNotYetValidException (java.security.cert.CertificateNotYetValidException)6 GeneralName (org.bouncycastle.asn1.x509.GeneralName)6