1:
37:
38:
39: package ;
40:
41: import ;
42: import ;
43: import ;
44: import ;
45: import ;
46: import ;
47: import ;
48: import ;
49: import ;
50: import ;
51:
52: import ;
53:
54:
58: public class X509CertSelectorImpl implements CertSelector
59: {
60:
61:
62:
63:
64: private Set issuerNames;
65: private Set subjectNames;
66:
67:
68:
69:
70: public X509CertSelectorImpl()
71: {
72: issuerNames = new HashSet();
73: subjectNames = new HashSet();
74: }
75:
76:
77:
78:
79: public void addIssuerName(byte[] issuerName) throws IOException
80: {
81: issuerNames.add(new X500DistinguishedName(issuerName));
82: }
83:
84: public void addIssuerName(String issuerName)
85: {
86: issuerNames.add(new X500DistinguishedName(issuerName));
87: }
88:
89: public void addIssuerName(Principal issuerName) throws IOException
90: {
91: if (issuerName instanceof X500DistinguishedName)
92: issuerNames.add(issuerName);
93: else if (issuerName instanceof X500Principal)
94: issuerNames.add(new X500DistinguishedName(((X500Principal) issuerName).getEncoded()));
95: else
96: issuerNames.add(new X500DistinguishedName(issuerName.getName()));
97: }
98:
99: public Collection getIssuerNames()
100: {
101: return Collections.unmodifiableSet(issuerNames);
102: }
103:
104: public void addSubjectName(byte[] subjectName) throws IOException
105: {
106: subjectNames.add(new X500DistinguishedName(subjectName));
107: }
108:
109: public void addSubjectName(String subjectName) throws IOException
110: {
111: subjectNames.add(new X500DistinguishedName(subjectName));
112: }
113:
114: public void addSubjectName(Principal subjectName) throws IOException
115: {
116: if (subjectName instanceof X500DistinguishedName)
117: subjectNames.add(subjectName);
118: else if (subjectName instanceof X500Principal)
119: subjectNames.add(new X500DistinguishedName(((X500Principal) subjectName).getEncoded()));
120: else
121: subjectNames.add(new X500DistinguishedName(subjectName.getName()));
122: }
123:
124: public Collection getSubjectNames()
125: {
126: return Collections.unmodifiableSet(subjectNames);
127: }
128:
129: public Object clone()
130: {
131: X509CertSelectorImpl copy = new X509CertSelectorImpl();
132: copy.issuerNames.addAll(issuerNames);
133: copy.subjectNames.addAll(subjectNames);
134: return copy;
135: }
136:
137: public boolean match(Certificate cert)
138: {
139: if (!(cert instanceof X509Certificate))
140: return false;
141: boolean matchIssuer = false;
142: boolean matchSubject = false;
143: try
144: {
145: Principal p = ((X509Certificate) cert).getIssuerDN();
146: X500DistinguishedName thisName = null;
147: if (p instanceof X500DistinguishedName)
148: thisName = (X500DistinguishedName) p;
149: else if (p instanceof X500Principal)
150: thisName = new X500DistinguishedName(((X500Principal) p).getEncoded());
151: else
152: thisName = new X500DistinguishedName(p.getName());
153: if (issuerNames.isEmpty())
154: matchIssuer = true;
155: else
156: {
157: for (Iterator it = issuerNames.iterator(); it.hasNext(); )
158: {
159: X500DistinguishedName name = (X500DistinguishedName) it.next();
160: if (thisName.equals(name))
161: {
162: matchIssuer = true;
163: break;
164: }
165: }
166: }
167:
168: p = ((X509Certificate) cert).getSubjectDN();
169: thisName = null;
170: if (p instanceof X500DistinguishedName)
171: thisName = (X500DistinguishedName) p;
172: else if (p instanceof X500Principal)
173: thisName = new X500DistinguishedName(((X500Principal) p).getEncoded());
174: else
175: thisName = new X500DistinguishedName(p.getName());
176: if (subjectNames.isEmpty())
177: matchSubject = true;
178: else
179: {
180: for (Iterator it = subjectNames.iterator(); it.hasNext(); )
181: {
182: X500DistinguishedName name = (X500DistinguishedName) it.next();
183: if (thisName.equals(name))
184: {
185: matchSubject = true;
186: break;
187: }
188: }
189: }
190: }
191: catch (Exception x)
192: {
193: }
194: return matchIssuer && matchSubject;
195: }
196: }