1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
|
package org.spongycastle.jce.provider.test;
import java.io.ByteArrayInputStream;
import java.security.PublicKey;
import java.security.Security;
import java.security.cert.*;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.spongycastle.jce.provider.BouncyCastleProvider;
import org.spongycastle.util.test.SimpleTestResult;
import org.spongycastle.util.test.Test;
import org.spongycastle.util.test.TestResult;
public class CertPathValidatorTest
implements Test
{
public TestResult perform()
{
try
{
CertificateFactory cf = CertificateFactory.getInstance("X.509", "SC");
// initialise CertStore
X509Certificate rootCert = (X509Certificate)cf.generateCertificate(new ByteArrayInputStream(CertPathTest.rootCertBin));
X509Certificate interCert = (X509Certificate)cf.generateCertificate(new ByteArrayInputStream(CertPathTest.interCertBin));
X509Certificate finalCert = (X509Certificate)cf.generateCertificate(new ByteArrayInputStream(CertPathTest.finalCertBin));
X509CRL rootCrl = (X509CRL)cf.generateCRL(new ByteArrayInputStream(CertPathTest.rootCrlBin));
X509CRL interCrl = (X509CRL)cf.generateCRL(new ByteArrayInputStream(CertPathTest.interCrlBin));
List list = new ArrayList();
list.add( rootCert );
list.add( interCert );
list.add( finalCert );
list.add( rootCrl );
list.add( interCrl );
CollectionCertStoreParameters ccsp = new CollectionCertStoreParameters( list );
CertStore store = CertStore.getInstance("Collection", ccsp );
Calendar validDate = Calendar.getInstance();
validDate.set(2002,2,21,2,21,10);
//validating path
List certchain = new ArrayList();
certchain.add( finalCert );
certchain.add( interCert );
CertPath cp = CertificateFactory.getInstance("X.509","SC").generateCertPath( certchain );
Set trust = new HashSet();
trust.add( new TrustAnchor( rootCert, null ) );
CertPathValidator cpv = CertPathValidator.getInstance("PKIX","SC");
PKIXParameters param = new PKIXParameters( trust );
param.addCertStore(store);
param.setDate( validDate.getTime() );
PKIXCertPathValidatorResult result =
(PKIXCertPathValidatorResult) cpv.validate(cp, param);
PolicyNode policyTree = result.getPolicyTree();
PublicKey subjectPublicKey = result.getPublicKey();
}
catch (Exception e)
{
e.printStackTrace();
return new SimpleTestResult(false, this.getName() + ": exception - " + e.toString());
}
return new SimpleTestResult(true, this.getName() + ": Okay");
}
public String getName()
{
return "CertPathValidator";
}
public static void main(
String[] args)
{
Security.addProvider(new BouncyCastleProvider());
Test test = new CertPathValidatorTest();
TestResult result = test.perform();
System.out.println(result.toString());
}
}
|