|
| 1 | +package org.bouncycastle.pkix.test; |
| 2 | + |
| 3 | +import java.security.Security; |
| 4 | +import java.security.cert.CertPath; |
| 5 | +import java.security.cert.CertPathBuilder; |
| 6 | +import java.security.cert.CertPathValidator; |
| 7 | +import java.security.cert.CertStore; |
| 8 | +import java.security.cert.CertificateFactory; |
| 9 | +import java.security.cert.CollectionCertStoreParameters; |
| 10 | +import java.security.cert.PKIXBuilderParameters; |
| 11 | +import java.security.cert.PKIXCertPathBuilderResult; |
| 12 | +import java.security.cert.PKIXParameters; |
| 13 | +import java.security.cert.TrustAnchor; |
| 14 | +import java.security.cert.X509CertSelector; |
| 15 | +import java.security.cert.X509Certificate; |
| 16 | +import java.util.ArrayList; |
| 17 | +import java.util.Collections; |
| 18 | +import java.util.Date; |
| 19 | +import java.util.HashSet; |
| 20 | +import java.util.List; |
| 21 | +import java.util.Set; |
| 22 | + |
| 23 | +import junit.framework.TestCase; |
| 24 | +import org.bouncycastle.jce.provider.BouncyCastleProvider; |
| 25 | +import org.bouncycastle.pkix.jcajce.PKIXCertPathReviewer; |
| 26 | +import org.bouncycastle.test.TestResourceFinder; |
| 27 | + |
| 28 | +public class CheckNameConstraintsTest |
| 29 | + extends TestCase |
| 30 | +{ |
| 31 | + public void testPKIXCertPathReviewer() |
| 32 | + throws Exception |
| 33 | + { |
| 34 | + Security.addProvider(new BouncyCastleProvider()); |
| 35 | + |
| 36 | + CertificateFactory cf = CertificateFactory.getInstance("X.509", "BC"); |
| 37 | + |
| 38 | + X509Certificate root = (X509Certificate) cf.generateCertificate(TestResourceFinder.findTestResource("pkix", "mal-root.crt")); |
| 39 | + X509Certificate ca1 = (X509Certificate) cf.generateCertificate(TestResourceFinder.findTestResource("pkix", "mal-ca1.crt")); |
| 40 | + X509Certificate ca2 = (X509Certificate) cf.generateCertificate(TestResourceFinder.findTestResource("pkix", "mal-ca2.crt")); |
| 41 | + X509Certificate leaf = (X509Certificate) cf.generateCertificate(TestResourceFinder.findTestResource("pkix", "mal-leaf.crt")); |
| 42 | + |
| 43 | + List certchain = new ArrayList(); |
| 44 | + certchain.add(root); |
| 45 | + certchain.add(ca1); |
| 46 | + certchain.add(ca2); |
| 47 | + certchain.add(leaf); |
| 48 | + |
| 49 | + CertPath cp = cf.generateCertPath(certchain); |
| 50 | + |
| 51 | + Set trust = new HashSet(); |
| 52 | + trust.add(new TrustAnchor(root, null)); |
| 53 | + PKIXParameters param = new PKIXParameters(trust); |
| 54 | + |
| 55 | + PKIXCertPathReviewer certPathReviewer = new PKIXCertPathReviewer(); |
| 56 | + certPathReviewer.init(cp, param); |
| 57 | + |
| 58 | + assertFalse(certPathReviewer.isValidCertPath()); // hit |
| 59 | + } |
| 60 | + |
| 61 | + public void testPKIXCertPathBuilder() |
| 62 | + throws Exception |
| 63 | + { |
| 64 | + Security.addProvider(new BouncyCastleProvider()); |
| 65 | + |
| 66 | + CertificateFactory cf = CertificateFactory.getInstance("X.509", "BC"); |
| 67 | + X509Certificate rootCert = (X509Certificate) cf.generateCertificate(TestResourceFinder.findTestResource("pkix", "mal-root.crt")); |
| 68 | + X509Certificate endCert = (X509Certificate) cf.generateCertificate(TestResourceFinder.findTestResource("pkix", "mal-ca1.crt")); |
| 69 | + |
| 70 | + // create CertStore to support path building |
| 71 | + List list = new ArrayList(); |
| 72 | + list.add(endCert); |
| 73 | + |
| 74 | + CollectionCertStoreParameters params = new CollectionCertStoreParameters(list); |
| 75 | + CertStore store = CertStore.getInstance("Collection", params, "BC"); |
| 76 | + |
| 77 | + // build the path |
| 78 | + CertPathBuilder builder = CertPathBuilder.getInstance("PKIX", "BC"); |
| 79 | + X509CertSelector pathConstraints = new X509CertSelector(); |
| 80 | + |
| 81 | + pathConstraints.setCertificate(endCert); |
| 82 | + |
| 83 | + PKIXBuilderParameters buildParams = new PKIXBuilderParameters(Collections.singleton(new TrustAnchor(rootCert, null)), pathConstraints); |
| 84 | + |
| 85 | + buildParams.addCertStore(store); |
| 86 | + buildParams.setDate(new Date()); |
| 87 | + buildParams.setRevocationEnabled(false); |
| 88 | + |
| 89 | + PKIXCertPathBuilderResult result = (PKIXCertPathBuilderResult)builder.build(buildParams); |
| 90 | + CertPath path = result.getCertPath(); |
| 91 | + |
| 92 | + if (path.getCertificates().size() != 1) |
| 93 | + { |
| 94 | + fail("wrong number of certs in testPKIXCertPathBuilder path"); |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + public void testPKIXCertPathValidator() |
| 99 | + throws Exception |
| 100 | + { |
| 101 | + Security.addProvider(new BouncyCastleProvider()); |
| 102 | + |
| 103 | + CertificateFactory cf = CertificateFactory.getInstance("X.509", "BC"); |
| 104 | + |
| 105 | + X509Certificate rootCert = (X509Certificate) cf.generateCertificate(TestResourceFinder.findTestResource("pkix", "mal-root.crt")); |
| 106 | + X509Certificate endCert = (X509Certificate) cf.generateCertificate(TestResourceFinder.findTestResource("pkix", "mal-ca1.crt")); |
| 107 | + |
| 108 | + List list = new ArrayList(); |
| 109 | + list.add(endCert); |
| 110 | + |
| 111 | + CertPath certPath = cf.generateCertPath(list); |
| 112 | + |
| 113 | + Set trust = new HashSet(); |
| 114 | + trust.add(new TrustAnchor(rootCert, null)); |
| 115 | + |
| 116 | + CertPathValidator cpv = CertPathValidator.getInstance("PKIX", "BC"); |
| 117 | + PKIXParameters param = new PKIXParameters(trust); |
| 118 | + param.setRevocationEnabled(false); |
| 119 | + |
| 120 | + cpv.validate(certPath, param); |
| 121 | + } |
| 122 | +} |
0 commit comments