Search in sources :

Example 1 with Encodable

use of com.github.zhenwei.core.util.Encodable in project LinLong-Java by zhenwei1108.

the class CertPathValidatorUtilities method findCertificates.

/**
 * Return a Collection of all certificates or attribute certificates found in the X509Store's that
 * are matching the certSelect criteriums.
 *
 * @param certSelect a {@link Selector} object that will be used to select the certificates
 * @param certStores a List containing only {@link X509Store} objects. These are used to search
 *                   for certificates.
 * @return a Collection of all found {@link X509Certificate} or {@link
 * com.github.zhenwei.provider.x509.X509AttributeCertificate} objects. May be empty but never
 * <code>null</code>.
 */
protected static Collection findCertificates(X509CertStoreSelector certSelect, List certStores) throws AnnotatedException {
    Set certs = new HashSet();
    Iterator iter = certStores.iterator();
    com.github.zhenwei.provider.jcajce.provider.asymmetric.x509.CertificateFactory certFact = new com.github.zhenwei.provider.jcajce.provider.asymmetric.x509.CertificateFactory();
    while (iter.hasNext()) {
        Object obj = iter.next();
        if (obj instanceof Store) {
            Store certStore = (Store) obj;
            try {
                for (Iterator it = certStore.getMatches(certSelect).iterator(); it.hasNext(); ) {
                    Object cert = it.next();
                    if (cert instanceof Encodable) {
                        certs.add(certFact.engineGenerateCertificate(new ByteArrayInputStream(((Encodable) cert).getEncoded())));
                    } else if (cert instanceof Certificate) {
                        certs.add(cert);
                    } else {
                        throw new AnnotatedException("Unknown object found in certificate store.");
                    }
                }
            } catch (StoreException e) {
                throw new AnnotatedException("Problem while picking certificates from X.509 store.", e);
            } catch (IOException e) {
                throw new AnnotatedException("Problem while extracting certificates from X.509 store.", e);
            } catch (CertificateException e) {
                throw new AnnotatedException("Problem while extracting certificates from X.509 store.", e);
            }
        } else {
            CertStore certStore = (CertStore) obj;
            try {
                certs.addAll(certStore.getCertificates(certSelect));
            } catch (CertStoreException e) {
                throw new AnnotatedException("Problem while picking certificates from certificate store.", e);
            }
        }
    }
    return certs;
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) CertStoreException(java.security.cert.CertStoreException) Store(com.github.zhenwei.core.util.Store) CertStore(java.security.cert.CertStore) CertificateException(java.security.cert.CertificateException) IOException(java.io.IOException) CertStoreException(java.security.cert.CertStoreException) StoreException(com.github.zhenwei.core.util.StoreException) ByteArrayInputStream(java.io.ByteArrayInputStream) Iterator(java.util.Iterator) Encodable(com.github.zhenwei.core.util.Encodable) ASN1Encodable(com.github.zhenwei.core.asn1.ASN1Encodable) CertStore(java.security.cert.CertStore) HashSet(java.util.HashSet) X509Certificate(java.security.cert.X509Certificate) Certificate(java.security.cert.Certificate) AnnotatedException(com.github.zhenwei.provider.jce.provider.AnnotatedException)

Aggregations

ASN1Encodable (com.github.zhenwei.core.asn1.ASN1Encodable)1 Encodable (com.github.zhenwei.core.util.Encodable)1 Store (com.github.zhenwei.core.util.Store)1 StoreException (com.github.zhenwei.core.util.StoreException)1 AnnotatedException (com.github.zhenwei.provider.jce.provider.AnnotatedException)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 CertStore (java.security.cert.CertStore)1 CertStoreException (java.security.cert.CertStoreException)1 Certificate (java.security.cert.Certificate)1 CertificateException (java.security.cert.CertificateException)1 X509Certificate (java.security.cert.X509Certificate)1 HashSet (java.util.HashSet)1 Iterator (java.util.Iterator)1 Set (java.util.Set)1