001: package org.bouncycastle.jce.provider;
002:
003: import org.bouncycastle.jce.X509LDAPCertStoreParameters;
004: import org.bouncycastle.util.Selector;
005: import org.bouncycastle.util.StoreException;
006: import org.bouncycastle.x509.X509CertPairStoreSelector;
007: import org.bouncycastle.x509.X509CertStoreSelector;
008: import org.bouncycastle.x509.X509CertificatePair;
009: import org.bouncycastle.x509.X509StoreParameters;
010: import org.bouncycastle.x509.X509StoreSpi;
011: import org.bouncycastle.x509.util.LDAPStoreHelper;
012:
013: import java.util.Collection;
014: import java.util.Collections;
015: import java.util.HashSet;
016: import java.util.Iterator;
017: import java.util.Set;
018:
019: /**
020: * A SPI implementation of Bouncy Castle <code>X509Store</code> for getting
021: * certificates form a LDAP directory.
022: *
023: * @see org.bouncycastle.x509.X509Store
024: */
025: public class X509StoreLDAPCerts extends X509StoreSpi {
026:
027: private LDAPStoreHelper helper;
028:
029: public X509StoreLDAPCerts() {
030: }
031:
032: /**
033: * Initializes this LDAP cert store implementation.
034: *
035: * @param params <code>X509LDAPCertStoreParameters</code>.
036: * @throws IllegalArgumentException if <code>params</code> is not an instance of
037: * <code>X509LDAPCertStoreParameters</code>.
038: */
039: public void engineInit(X509StoreParameters params) {
040: if (!(params instanceof X509LDAPCertStoreParameters)) {
041: throw new IllegalArgumentException(
042: "Initialization parameters must be an instance of "
043: + X509LDAPCertStoreParameters.class
044: .getName() + ".");
045: }
046: helper = new LDAPStoreHelper(
047: (X509LDAPCertStoreParameters) params);
048: }
049:
050: /**
051: * Returns a collection of matching certificates from the LDAP location.
052: * <p/>
053: * The selector must be a of type <code>X509CertStoreSelector</code>. If
054: * it is not an empty collection is returned.
055: * <p/>
056: * The implementation searches only for CA certificates, if the method
057: * {@link java.security.cert.X509CertSelector#getBasicConstraints()} is
058: * greater or equal to 0. If it is -2 only end certificates are searched.
059: * <p/>
060: * The subject and the serial number for end certificates should be
061: * reasonable criterias for a selector.
062: *
063: * @param selector The selector to use for finding.
064: * @return A collection with the matches.
065: * @throws StoreException if an exception occurs while searching.
066: */
067: public Collection engineGetMatches(Selector selector)
068: throws StoreException {
069: if (!(selector instanceof X509CertStoreSelector)) {
070: return Collections.EMPTY_SET;
071: }
072: X509CertStoreSelector xselector = (X509CertStoreSelector) selector;
073: Set set = new HashSet();
074: // test if only CA certificates should be selected
075: if (xselector.getBasicConstraints() > 0) {
076: set.addAll(helper.getCACertificates(xselector));
077: set
078: .addAll(getCertificatesFromCrossCertificatePairs(xselector));
079: }
080: // only end certificates should be selected
081: else if (xselector.getBasicConstraints() == -2) {
082: set.addAll(helper.getUserCertificates(xselector));
083: }
084: // nothing specified
085: else {
086: set.addAll(helper.getUserCertificates(xselector));
087: set.addAll(helper.getCACertificates(xselector));
088: set
089: .addAll(getCertificatesFromCrossCertificatePairs(xselector));
090: }
091: return set;
092: }
093:
094: private Collection getCertificatesFromCrossCertificatePairs(
095: X509CertStoreSelector xselector) throws StoreException {
096: Set set = new HashSet();
097: X509CertPairStoreSelector ps = new X509CertPairStoreSelector();
098:
099: ps.setForwardSelector(xselector);
100: ps.setReverseSelector(new X509CertStoreSelector());
101:
102: Set crossCerts = new HashSet(helper
103: .getCrossCertificatePairs(ps));
104: Set forward = new HashSet();
105: Set reverse = new HashSet();
106: Iterator it = crossCerts.iterator();
107: while (it.hasNext()) {
108: X509CertificatePair pair = (X509CertificatePair) it.next();
109: if (pair.getForward() != null) {
110: forward.add(pair.getForward());
111: }
112: if (pair.getReverse() != null) {
113: reverse.add(pair.getReverse());
114: }
115: }
116: set.addAll(forward);
117: set.addAll(reverse);
118: return set;
119: }
120: }
|