001: package org.bouncycastle.x509;
002:
003: import org.bouncycastle.util.Selector;
004:
005: /**
006: * This class is an <code>Selector</code> like implementation to select
007: * certificates pairs, which are e.g. used for cross certificates. The set of
008: * criteria is given from two
009: * {@link org.bouncycastle.x509.X509CertStoreSelector}s which must be both
010: * matched.
011: *
012: * @see org.bouncycastle.x509.X509AttributeCertificate
013: * @see org.bouncycastle.x509.X509Store
014: */
015: public class X509CertPairStoreSelector implements Selector {
016:
017: private X509CertStoreSelector forwardSelector;
018:
019: private X509CertStoreSelector reverseSelector;
020:
021: private X509CertificatePair certPair;
022:
023: public X509CertPairStoreSelector() {
024: }
025:
026: /**
027: * Returns the certificate pair which is used for testing on equality.
028: *
029: * @return Returns the certificate pair which is checked.
030: */
031: public X509CertificatePair getCertPair() {
032: return certPair;
033: }
034:
035: /**
036: * Set the certificate pair which is used for testing on equality.
037: *
038: * @param certPair The certPairChecking to set.
039: */
040: public void setCertPair(X509CertificatePair certPair) {
041: this .certPair = certPair;
042: }
043:
044: /**
045: * @param forwardSelector The certificate selector for the forward part in
046: * the pair.
047: */
048: public void setForwardSelector(X509CertStoreSelector forwardSelector) {
049: this .forwardSelector = forwardSelector;
050: }
051:
052: /**
053: * @param reverseSelector The certificate selector for the reverse part in
054: * the pair.
055: */
056: public void setReverseSelector(X509CertStoreSelector reverseSelector) {
057: this .reverseSelector = reverseSelector;
058: }
059:
060: /**
061: * Returns a clone of this selector.
062: *
063: * @return A clone of this selector.
064: * @see java.lang.Object#clone()
065: */
066: public Object clone() {
067: X509CertPairStoreSelector cln = new X509CertPairStoreSelector();
068:
069: cln.certPair = certPair;
070:
071: if (forwardSelector != null) {
072: cln
073: .setForwardSelector((X509CertStoreSelector) forwardSelector
074: .clone());
075: }
076:
077: if (reverseSelector != null) {
078: cln
079: .setReverseSelector((X509CertStoreSelector) reverseSelector
080: .clone());
081: }
082:
083: return cln;
084: }
085:
086: /**
087: * Decides if the given certificate pair should be selected. If
088: * <code>obj</code> is not a {@link X509CertificatePair} this method
089: * returns <code>false</code>.
090: *
091: * @param obj The {@link X509CertificatePair} which should be tested.
092: * @return <code>true</code> if the object matches this selector.
093: */
094: public boolean match(Object obj) {
095: try {
096: if (!(obj instanceof X509CertificatePair)) {
097: return false;
098: }
099: X509CertificatePair pair = (X509CertificatePair) obj;
100:
101: if (forwardSelector != null
102: && !forwardSelector.match((Object) pair
103: .getForward())) {
104: return false;
105: }
106:
107: if (reverseSelector != null
108: && !reverseSelector.match((Object) pair
109: .getReverse())) {
110: return false;
111: }
112:
113: if (certPair != null) {
114: return certPair.equals(obj);
115: }
116:
117: return true;
118: } catch (Exception e) {
119: return false;
120: }
121: }
122:
123: /**
124: * Returns the certicate selector for the forward part.
125: *
126: * @return Returns the certicate selector for the forward part.
127: */
128: public X509CertStoreSelector getForwardSelector() {
129: return forwardSelector;
130: }
131:
132: /**
133: * Returns the certicate selector for the reverse part.
134: *
135: * @return Returns the reverse selector for teh reverse part.
136: */
137: public X509CertStoreSelector getReverseSelector() {
138: return reverseSelector;
139: }
140: }
|