Search in sources :

Example 1 with CertificatePair

use of com.github.zhenwei.core.asn1.x509.CertificatePair in project LinLong-Java by zhenwei1108.

the class X509CertPairParser method readDERCrossCertificatePair.

private X509CertificatePair readDERCrossCertificatePair(InputStream in) throws IOException, CertificateParsingException {
    ASN1InputStream dIn = new ASN1InputStream(in);
    ASN1Sequence seq = (ASN1Sequence) dIn.readObject();
    CertificatePair pair = CertificatePair.getInstance(seq);
    return new X509CertificatePair(pair);
}
Also used : ASN1InputStream(com.github.zhenwei.core.asn1.ASN1InputStream) ASN1Sequence(com.github.zhenwei.core.asn1.ASN1Sequence) X509CertificatePair(com.github.zhenwei.provider.x509.X509CertificatePair) X509CertificatePair(com.github.zhenwei.provider.x509.X509CertificatePair) CertificatePair(com.github.zhenwei.core.asn1.x509.CertificatePair)

Example 2 with CertificatePair

use of com.github.zhenwei.core.asn1.x509.CertificatePair in project LinLong-Java by zhenwei1108.

the class X509LDAPCertStoreSpi method engineGetCertificates.

public Collection engineGetCertificates(CertSelector selector) throws CertStoreException {
    if (!(selector instanceof X509CertSelector)) {
        throw new CertStoreException("selector is not a X509CertSelector");
    }
    X509CertSelector xselector = (X509CertSelector) selector;
    Set certSet = new HashSet();
    Set set = getEndCertificates(xselector);
    set.addAll(getCACertificates(xselector));
    set.addAll(getCrossCertificates(xselector));
    Iterator it = set.iterator();
    try {
        CertificateFactory cf = CertificateFactory.getInstance("X.509", WeGooProvider.PROVIDER_NAME);
        while (it.hasNext()) {
            byte[] bytes = (byte[]) it.next();
            if (bytes == null || bytes.length == 0) {
                continue;
            }
            List bytesList = new ArrayList();
            bytesList.add(bytes);
            try {
                CertificatePair pair = CertificatePair.getInstance(new ASN1InputStream(bytes).readObject());
                bytesList.clear();
                if (pair.getForward() != null) {
                    bytesList.add(pair.getForward().getEncoded());
                }
                if (pair.getReverse() != null) {
                    bytesList.add(pair.getReverse().getEncoded());
                }
            } catch (IOException e) {
            } catch (IllegalArgumentException e) {
            }
            for (Iterator it2 = bytesList.iterator(); it2.hasNext(); ) {
                ByteArrayInputStream bIn = new ByteArrayInputStream((byte[]) it2.next());
                try {
                    Certificate cert = cf.generateCertificate(bIn);
                    // cert).getSubjectX500Principal());
                    if (xselector.match(cert)) {
                        certSet.add(cert);
                    }
                } catch (Exception e) {
                }
            }
        }
    } catch (Exception e) {
        throw new CertStoreException("certificate cannot be constructed from LDAP result: " + e);
    }
    return certSet;
}
Also used : ASN1InputStream(com.github.zhenwei.core.asn1.ASN1InputStream) HashSet(java.util.HashSet) Set(java.util.Set) CertStoreException(java.security.cert.CertStoreException) ArrayList(java.util.ArrayList) X509CertSelector(java.security.cert.X509CertSelector) IOException(java.io.IOException) CertificateFactory(java.security.cert.CertificateFactory) CertificatePair(com.github.zhenwei.core.asn1.x509.CertificatePair) NamingException(javax.naming.NamingException) InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) IOException(java.io.IOException) CertStoreException(java.security.cert.CertStoreException) ByteArrayInputStream(java.io.ByteArrayInputStream) Iterator(java.util.Iterator) ArrayList(java.util.ArrayList) List(java.util.List) HashSet(java.util.HashSet) Certificate(java.security.cert.Certificate)

Example 3 with CertificatePair

use of com.github.zhenwei.core.asn1.x509.CertificatePair in project LinLong-Java by zhenwei1108.

the class X509CertificatePair method getEncoded.

public byte[] getEncoded() throws CertificateEncodingException {
    Certificate f = null;
    Certificate r = null;
    try {
        if (forward != null) {
            f = Certificate.getInstance(new ASN1InputStream(forward.getEncoded()).readObject());
            if (f == null) {
                throw new CertificateEncodingException("unable to get encoding for forward");
            }
        }
        if (reverse != null) {
            r = Certificate.getInstance(new ASN1InputStream(reverse.getEncoded()).readObject());
            if (r == null) {
                throw new CertificateEncodingException("unable to get encoding for reverse");
            }
        }
        return new CertificatePair(f, r).getEncoded(ASN1Encoding.DER);
    } catch (IllegalArgumentException e) {
        throw new ExtCertificateEncodingException(e.toString(), e);
    } catch (IOException e) {
        throw new ExtCertificateEncodingException(e.toString(), e);
    }
}
Also used : ASN1InputStream(com.github.zhenwei.core.asn1.ASN1InputStream) CertificateEncodingException(java.security.cert.CertificateEncodingException) IOException(java.io.IOException) CertificatePair(com.github.zhenwei.core.asn1.x509.CertificatePair) X509Certificate(java.security.cert.X509Certificate) Certificate(com.github.zhenwei.core.asn1.x509.Certificate)

Example 4 with CertificatePair

use of com.github.zhenwei.core.asn1.x509.CertificatePair in project LinLong-Java by zhenwei1108.

the class LDAPStoreHelper method createCrossCertificatePairs.

private Set createCrossCertificatePairs(List list, X509CertPairStoreSelector xselector) throws StoreException {
    Set certPairSet = new HashSet();
    int i = 0;
    while (i < list.size()) {
        X509CertificatePair pair;
        try {
            // first try to decode it as certificate pair
            try {
                X509CertPairParser parser = new X509CertPairParser();
                parser.engineInit(new ByteArrayInputStream((byte[]) list.get(i)));
                pair = (X509CertificatePair) parser.engineRead();
            } catch (StreamParsingException e) {
                // now try it to construct it the forward and reverse
                // certificate
                byte[] forward = (byte[]) list.get(i);
                byte[] reverse = (byte[]) list.get(i + 1);
                pair = new X509CertificatePair(new CertificatePair(Certificate.getInstance(new ASN1InputStream(forward).readObject()), Certificate.getInstance(new ASN1InputStream(reverse).readObject())));
                i++;
            }
            if (xselector.match((Object) pair)) {
                certPairSet.add(pair);
            }
        } catch (CertificateParsingException e) {
        // try next
        } catch (IOException e) {
        // try next
        }
        i++;
    }
    return certPairSet;
}
Also used : X509CertPairParser(com.github.zhenwei.provider.jce.provider.X509CertPairParser) ASN1InputStream(com.github.zhenwei.core.asn1.ASN1InputStream) HashSet(java.util.HashSet) Set(java.util.Set) X509CertificatePair(com.github.zhenwei.provider.x509.X509CertificatePair) CertificateParsingException(java.security.cert.CertificateParsingException) ByteArrayInputStream(java.io.ByteArrayInputStream) IOException(java.io.IOException) CertificatePair(com.github.zhenwei.core.asn1.x509.CertificatePair) X509CertificatePair(com.github.zhenwei.provider.x509.X509CertificatePair) HashSet(java.util.HashSet)

Aggregations

ASN1InputStream (com.github.zhenwei.core.asn1.ASN1InputStream)4 CertificatePair (com.github.zhenwei.core.asn1.x509.CertificatePair)4 IOException (java.io.IOException)3 X509CertificatePair (com.github.zhenwei.provider.x509.X509CertificatePair)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 HashSet (java.util.HashSet)2 Set (java.util.Set)2 ASN1Sequence (com.github.zhenwei.core.asn1.ASN1Sequence)1 Certificate (com.github.zhenwei.core.asn1.x509.Certificate)1 X509CertPairParser (com.github.zhenwei.provider.jce.provider.X509CertPairParser)1 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)1 CertStoreException (java.security.cert.CertStoreException)1 Certificate (java.security.cert.Certificate)1 CertificateEncodingException (java.security.cert.CertificateEncodingException)1 CertificateFactory (java.security.cert.CertificateFactory)1 CertificateParsingException (java.security.cert.CertificateParsingException)1 X509CertSelector (java.security.cert.X509CertSelector)1 X509Certificate (java.security.cert.X509Certificate)1 ArrayList (java.util.ArrayList)1 Iterator (java.util.Iterator)1