001: package org.bouncycastle.jce.provider.test;
002:
003: import org.bouncycastle.jce.provider.BouncyCastleProvider;
004: import org.bouncycastle.util.test.SimpleTest;
005:
006: import java.io.ByteArrayInputStream;
007: import java.security.Security;
008: import java.security.cert.CertStore;
009: import java.security.cert.CertificateFactory;
010: import java.security.cert.CollectionCertStoreParameters;
011: import java.security.cert.X509CRL;
012: import java.security.cert.X509CRLSelector;
013: import java.security.cert.X509CertSelector;
014: import java.security.cert.X509Certificate;
015: import java.util.ArrayList;
016: import java.util.Collection;
017: import java.util.Iterator;
018: import java.util.List;
019:
020: public class CertStoreTest extends SimpleTest {
021:
022: public void performTest() throws Exception {
023: basicTest();
024: orderTest();
025: }
026:
027: private void basicTest() throws Exception {
028: CertificateFactory cf = CertificateFactory.getInstance("X.509",
029: "BC");
030:
031: X509Certificate rootCert = (X509Certificate) cf
032: .generateCertificate(new ByteArrayInputStream(
033: CertPathTest.rootCertBin));
034: X509Certificate interCert = (X509Certificate) cf
035: .generateCertificate(new ByteArrayInputStream(
036: CertPathTest.interCertBin));
037: X509Certificate finalCert = (X509Certificate) cf
038: .generateCertificate(new ByteArrayInputStream(
039: CertPathTest.finalCertBin));
040: X509CRL rootCrl = (X509CRL) cf
041: .generateCRL(new ByteArrayInputStream(
042: CertPathTest.rootCrlBin));
043: X509CRL interCrl = (X509CRL) cf
044: .generateCRL(new ByteArrayInputStream(
045: CertPathTest.interCrlBin));
046:
047: // Testing CollectionCertStore generation from List
048: List list = new ArrayList();
049: list.add(rootCert);
050: list.add(interCert);
051: list.add(finalCert);
052: list.add(rootCrl);
053: list.add(interCrl);
054: CollectionCertStoreParameters ccsp = new CollectionCertStoreParameters(
055: list);
056: CertStore store = CertStore.getInstance("Collection", ccsp,
057: "BC");
058:
059: // Searching for rootCert by subjectDN
060: X509CertSelector targetConstraints = new X509CertSelector();
061: targetConstraints.setSubject(rootCert.getSubjectX500Principal()
062: .getName());
063: Collection certs = store.getCertificates(targetConstraints);
064: if (certs.size() != 1 || !certs.contains(rootCert)) {
065: fail("rootCert not found by subjectDN");
066: }
067:
068: // Searching for rootCert by subjectDN encoded as byte
069: targetConstraints = new X509CertSelector();
070: targetConstraints.setSubject(rootCert.getSubjectX500Principal()
071: .getEncoded());
072: certs = store.getCertificates(targetConstraints);
073: if (certs.size() != 1 || !certs.contains(rootCert)) {
074: fail("rootCert not found by encoded subjectDN");
075: }
076:
077: // Searching for rootCert by public key encoded as byte
078: targetConstraints = new X509CertSelector();
079: targetConstraints.setSubjectPublicKey(rootCert.getPublicKey()
080: .getEncoded());
081: certs = store.getCertificates(targetConstraints);
082: if (certs.size() != 1 || !certs.contains(rootCert)) {
083: fail("rootCert not found by encoded public key");
084: }
085:
086: // Searching for interCert by issuerDN
087: targetConstraints = new X509CertSelector();
088: targetConstraints.setIssuer(rootCert.getSubjectX500Principal()
089: .getEncoded());
090: certs = store.getCertificates(targetConstraints);
091: if (certs.size() != 2) {
092: fail("did not found 2 certs");
093: }
094: if (!certs.contains(rootCert)) {
095: fail("rootCert not found");
096: }
097: if (!certs.contains(interCert)) {
098: fail("interCert not found");
099: }
100:
101: // Searching for rootCrl by issuerDN
102: X509CRLSelector targetConstraintsCRL = new X509CRLSelector();
103: targetConstraintsCRL.addIssuerName(rootCrl
104: .getIssuerX500Principal().getEncoded());
105: Collection crls = store.getCRLs(targetConstraintsCRL);
106: if (crls.size() != 1 || !crls.contains(rootCrl)) {
107: fail("rootCrl not found");
108: }
109: }
110:
111: private void orderTest() throws Exception {
112: CertificateFactory cf = CertificateFactory.getInstance("X.509",
113: "BC");
114:
115: X509Certificate rootCert = (X509Certificate) cf
116: .generateCertificate(new ByteArrayInputStream(
117: CertPathTest.rootCertBin));
118: X509Certificate interCert = (X509Certificate) cf
119: .generateCertificate(new ByteArrayInputStream(
120: CertPathTest.interCertBin));
121: X509Certificate finalCert = (X509Certificate) cf
122: .generateCertificate(new ByteArrayInputStream(
123: CertPathTest.finalCertBin));
124:
125: List list = new ArrayList();
126: list.add(rootCert);
127: list.add(interCert);
128: list.add(finalCert);
129: CollectionCertStoreParameters ccsp = new CollectionCertStoreParameters(
130: list);
131: CertStore store = CertStore.getInstance("Collection", ccsp,
132: "BC");
133:
134: Iterator certs = store.getCertificates(null).iterator();
135:
136: if (!certs.next().equals(rootCert)) {
137: fail("root ordering wrong");
138: }
139: if (!certs.next().equals(interCert)) {
140: fail("mid ordering wrong");
141: }
142: if (!certs.next().equals(finalCert)) {
143: fail("final ordering wrong");
144: }
145:
146: list = new ArrayList();
147: list.add(finalCert);
148: list.add(interCert);
149: list.add(rootCert);
150: ccsp = new CollectionCertStoreParameters(list);
151: store = CertStore.getInstance("Collection", ccsp, "BC");
152:
153: certs = store.getCertificates(null).iterator();
154:
155: if (!certs.next().equals(finalCert)) {
156: fail("reverse final ordering wrong");
157: }
158: if (!certs.next().equals(interCert)) {
159: fail("reverse mid ordering wrong");
160: }
161: if (!certs.next().equals(rootCert)) {
162: fail("reverse root ordering wrong");
163: }
164:
165: X509CRL rootCrl = (X509CRL) cf
166: .generateCRL(new ByteArrayInputStream(
167: CertPathTest.rootCrlBin));
168: X509CRL interCrl = (X509CRL) cf
169: .generateCRL(new ByteArrayInputStream(
170: CertPathTest.interCrlBin));
171:
172: list = new ArrayList();
173: list.add(finalCert);
174: list.add(rootCrl);
175: list.add(interCrl);
176:
177: ccsp = new CollectionCertStoreParameters(list);
178: store = CertStore.getInstance("Collection", ccsp, "BC");
179:
180: Iterator crls = store.getCRLs(null).iterator();
181:
182: if (!crls.next().equals(rootCrl)) {
183: fail("root crl ordering wrong");
184: }
185: if (!crls.next().equals(interCrl)) {
186: fail("mid crl ordering wrong");
187: }
188:
189: list = new ArrayList();
190: list.add(finalCert);
191: list.add(interCrl);
192: list.add(rootCrl);
193: ccsp = new CollectionCertStoreParameters(list);
194: store = CertStore.getInstance("Collection", ccsp, "BC");
195:
196: crls = store.getCRLs(null).iterator();
197:
198: if (!crls.next().equals(interCrl)) {
199: fail("reverse mid crl ordering wrong");
200: }
201: if (!crls.next().equals(rootCrl)) {
202: fail("reverse root crl ordering wrong");
203: }
204: }
205:
206: public String getName() {
207: return "CertStore";
208: }
209:
210: public static void main(String[] args) {
211: Security.addProvider(new BouncyCastleProvider());
212:
213: runTest(new CertStoreTest());
214: }
215:
216: }
|