001: package org.bouncycastle.jce.provider.test;
002:
003: import java.io.ByteArrayInputStream;
004: import java.math.BigInteger;
005: import java.security.KeyPair;
006: import java.security.Security;
007: import java.security.cert.CertPath;
008: import java.security.cert.CertPathBuilder;
009: import java.security.cert.CertStore;
010: import java.security.cert.CertificateFactory;
011: import java.security.cert.CollectionCertStoreParameters;
012: import java.security.cert.PKIXBuilderParameters;
013: import java.security.cert.PKIXCertPathBuilderResult;
014: import java.security.cert.TrustAnchor;
015: import java.security.cert.X509CRL;
016: import java.security.cert.X509CertSelector;
017: import java.security.cert.X509Certificate;
018: import java.util.ArrayList;
019: import java.util.Calendar;
020: import java.util.Collections;
021: import java.util.Date;
022: import java.util.HashSet;
023: import java.util.List;
024: import java.util.Set;
025:
026: import org.bouncycastle.jce.provider.BouncyCastleProvider;
027: import org.bouncycastle.util.test.SimpleTestResult;
028: import org.bouncycastle.util.test.Test;
029: import org.bouncycastle.util.test.TestResult;
030:
031: public class CertPathBuilderTest implements Test {
032:
033: public TestResult baseTest() {
034: try {
035: CertificateFactory cf = CertificateFactory.getInstance(
036: "X.509", "BC");
037:
038: // initialise CertStore
039: X509Certificate rootCert = (X509Certificate) cf
040: .generateCertificate(new ByteArrayInputStream(
041: CertPathTest.rootCertBin));
042: X509Certificate interCert = (X509Certificate) cf
043: .generateCertificate(new ByteArrayInputStream(
044: CertPathTest.interCertBin));
045: X509Certificate finalCert = (X509Certificate) cf
046: .generateCertificate(new ByteArrayInputStream(
047: CertPathTest.finalCertBin));
048: X509CRL rootCrl = (X509CRL) cf
049: .generateCRL(new ByteArrayInputStream(
050: CertPathTest.rootCrlBin));
051: X509CRL interCrl = (X509CRL) cf
052: .generateCRL(new ByteArrayInputStream(
053: CertPathTest.interCrlBin));
054: List list = new ArrayList();
055: list.add(rootCert);
056: list.add(interCert);
057: list.add(finalCert);
058: list.add(rootCrl);
059: list.add(interCrl);
060: CollectionCertStoreParameters ccsp = new CollectionCertStoreParameters(
061: list);
062: CertStore store = CertStore.getInstance("Collection", ccsp,
063: "BC");
064: Calendar validDate = Calendar.getInstance();
065: validDate.set(2002, 2, 21, 2, 21, 10);
066:
067: //Searching for rootCert by subjectDN without CRL
068: Set trust = new HashSet();
069: trust.add(new TrustAnchor(rootCert, null));
070:
071: CertPathBuilder cpb = CertPathBuilder.getInstance("PKIX",
072: "BC");
073: X509CertSelector targetConstraints = new X509CertSelector();
074: targetConstraints.setSubject(finalCert
075: .getSubjectX500Principal().getEncoded());
076: PKIXBuilderParameters params = new PKIXBuilderParameters(
077: trust, targetConstraints);
078: params.addCertStore(store);
079: params.setDate(validDate.getTime());
080: PKIXCertPathBuilderResult result = (PKIXCertPathBuilderResult) cpb
081: .build(params);
082: CertPath path = result.getCertPath();
083:
084: if (path.getCertificates().size() != 2) {
085: return new SimpleTestResult(false, this .getName()
086: + ": wrong number of certs in baseTest path");
087: }
088: } catch (Exception e) {
089: return new SimpleTestResult(false, this .getName()
090: + ": exception - " + e.toString(), e);
091: }
092:
093: return new SimpleTestResult(true, this .getName() + ": Okay");
094: }
095:
096: public TestResult v0Test() {
097: try {
098: // create certificates and CRLs
099: KeyPair rootPair = TestUtils.generateRSAKeyPair();
100: KeyPair interPair = TestUtils.generateRSAKeyPair();
101: KeyPair endPair = TestUtils.generateRSAKeyPair();
102:
103: X509Certificate rootCert = TestUtils
104: .generateRootCert(rootPair);
105: X509Certificate interCert = TestUtils
106: .generateIntermediateCert(interPair.getPublic(),
107: rootPair.getPrivate(), rootCert);
108: X509Certificate endCert = TestUtils.generateEndEntityCert(
109: endPair.getPublic(), interPair.getPrivate(),
110: interCert);
111:
112: BigInteger revokedSerialNumber = BigInteger.valueOf(2);
113: X509CRL rootCRL = TestUtils.createCRL(rootCert, rootPair
114: .getPrivate(), revokedSerialNumber);
115: X509CRL interCRL = TestUtils.createCRL(interCert, interPair
116: .getPrivate(), revokedSerialNumber);
117:
118: // create CertStore to support path building
119: List list = new ArrayList();
120:
121: list.add(rootCert);
122: list.add(interCert);
123: list.add(endCert);
124: list.add(rootCRL);
125: list.add(interCRL);
126:
127: CollectionCertStoreParameters params = new CollectionCertStoreParameters(
128: list);
129: CertStore store = CertStore.getInstance("Collection",
130: params);
131:
132: // build the path
133: CertPathBuilder builder = CertPathBuilder.getInstance(
134: "PKIX", "BC");
135: X509CertSelector pathConstraints = new X509CertSelector();
136:
137: pathConstraints.setSubject(endCert
138: .getSubjectX500Principal().getEncoded());
139:
140: PKIXBuilderParameters buildParams = new PKIXBuilderParameters(
141: Collections.singleton(new TrustAnchor(rootCert,
142: null)), pathConstraints);
143:
144: buildParams.addCertStore(store);
145: buildParams.setDate(new Date());
146:
147: PKIXCertPathBuilderResult result = (PKIXCertPathBuilderResult) builder
148: .build(buildParams);
149: CertPath path = result.getCertPath();
150:
151: if (path.getCertificates().size() != 2) {
152: return new SimpleTestResult(false, this .getName()
153: + ": wrong number of certs in v0Test path");
154: }
155: } catch (Exception e) {
156: return new SimpleTestResult(false, this .getName()
157: + ": exception - " + e.toString(), e);
158: }
159:
160: return new SimpleTestResult(true, this .getName() + ": Okay");
161: }
162:
163: /* (non-Javadoc)
164: * @see org.bouncycastle.util.test.Test#perform()
165: */
166: public TestResult perform() {
167: TestResult res = baseTest();
168: if (!res.isSuccessful()) {
169: return res;
170: }
171:
172: return v0Test();
173: }
174:
175: public String getName() {
176: return "CertPathBuilder";
177: }
178:
179: public static void main(String[] args) {
180: Security.addProvider(new BouncyCastleProvider());
181:
182: Test test = new CertPathBuilderTest();
183: TestResult result = test.perform();
184:
185: System.out.println(result.toString());
186: }
187: }
|