📄 nistcertpathtest.java
字号:
package org.bouncycastle.jce.provider.test.nist;import java.io.FileInputStream;import java.io.InputStream;import java.security.Security;import java.security.cert.CertPath;import java.security.cert.CertPathBuilder;import java.security.cert.CertPathBuilderException;import java.security.cert.CertPathValidator;import java.security.cert.CertPathValidatorException;import java.security.cert.CertStore;import java.security.cert.CertificateFactory;import java.security.cert.CollectionCertStoreParameters;import java.security.cert.PKIXBuilderParameters;import java.security.cert.PKIXCertPathBuilderResult;import java.security.cert.PKIXCertPathValidatorResult;import java.security.cert.PKIXParameters;import java.security.cert.TrustAnchor;import java.security.cert.X509CRL;import java.security.cert.X509CertSelector;import java.security.cert.X509Certificate;import java.util.ArrayList;import java.util.Arrays;import java.util.Collections;import java.util.HashMap;import java.util.HashSet;import java.util.List;import java.util.Map;import java.util.Set;import junit.framework.Test;import junit.framework.TestCase;import junit.framework.TestSuite;import org.bouncycastle.asn1.ASN1Encodable;import org.bouncycastle.asn1.x509.X509Extensions;import org.bouncycastle.x509.extension.X509ExtensionUtil;/** * NIST CertPath test data for RFC 3280 */public class NistCertPathTest extends TestCase{ private static final String TEST_DATA_HOME = "bc.test.data.home"; private static final String GOOD_CA_CERT = "GoodCACert"; private static final String GOOD_CA_CRL = "GoodCACRL"; private static final String TRUST_ANCHOR_ROOT_CRL = "TrustAnchorRootCRL"; private static final String TRUST_ANCHOR_ROOT_CERTIFICATE = "TrustAnchorRootCertificate"; private static final char[] PKCS12_PASSWORD = "password".toCharArray(); private static final String ANY_POLICY = "2.5.29.32.0"; private static final String NIST_TEST_POLICY_1 = "2.16.840.1.101.3.2.1.48.1"; private static final String NIST_TEST_POLICY_2 = "2.16.840.1.101.3.2.1.48.2"; private static final String NIST_TEST_POLICY_3 = "2.16.840.1.101.3.2.1.48.3"; private static Map certs = new HashMap(); private static Map crls = new HashMap(); private static Set noPolicies = Collections.EMPTY_SET; private static Set anyPolicy = Collections.singleton(ANY_POLICY); private static Set nistTestPolicy1 = Collections.singleton(NIST_TEST_POLICY_1); private static Set nistTestPolicy2 = Collections.singleton(NIST_TEST_POLICY_2); private static Set nistTestPolicy3 = Collections.singleton(NIST_TEST_POLICY_3); private static Set nistTestPolicy1And2 = new HashSet(Arrays.asList(new String[] { NIST_TEST_POLICY_1, NIST_TEST_POLICY_2 })); public void setUp() { if (Security.getProvider("BC") == null) { Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider()); } } public void testValidSignaturesTest1() throws Exception { doTest(TRUST_ANCHOR_ROOT_CERTIFICATE, new String[] { "ValidCertificatePathTest1EE", GOOD_CA_CERT}, new String[] { GOOD_CA_CRL, TRUST_ANCHOR_ROOT_CRL }); } public void testInvalidCASignatureTest2() throws Exception { doExceptionTest(TRUST_ANCHOR_ROOT_CERTIFICATE, new String[] { "ValidCertificatePathTest1EE", "BadSignedCACert" }, new String[] { "BadSignedCACRL", TRUST_ANCHOR_ROOT_CRL}, 1, "TrustAnchor found but certificate validation failed."); } public void testInvalidEESignatureTest3() throws Exception { doExceptionTest(TRUST_ANCHOR_ROOT_CERTIFICATE, new String[] { GOOD_CA_CERT, "InvalidEESignatureTest3EE" }, new String[] { TRUST_ANCHOR_ROOT_CRL, GOOD_CA_CRL }, 0, "Could not validate certificate signature."); } public void testValidDSASignaturesTest4() throws Exception { doTest(TRUST_ANCHOR_ROOT_CERTIFICATE, new String[] { "DSACACert", "ValidDSASignaturesTest4EE" }, new String[] { TRUST_ANCHOR_ROOT_CRL, "DSACACRL" }); } // 4.1.5 public void testValidDSAParameterInheritanceTest5() throws Exception { doTest(TRUST_ANCHOR_ROOT_CERTIFICATE, new String[] { "DSACACert", "DSAParametersInheritedCACert", "ValidDSAParameterInheritanceTest5EE" }, new String[] { TRUST_ANCHOR_ROOT_CRL, "DSACACRL", "DSAParametersInheritedCACRL" }); } public void testInvalidDSASignaturesTest6() throws Exception { doExceptionTest(TRUST_ANCHOR_ROOT_CERTIFICATE, new String[] { "DSACACert", "InvalidDSASignatureTest6EE" }, new String[] { TRUST_ANCHOR_ROOT_CRL, "DSACACRL" }, 0, "Could not validate certificate signature."); } public void testCANotBeforeDateTest1() throws Exception { doExceptionTest(TRUST_ANCHOR_ROOT_CERTIFICATE, new String[] { "BadnotBeforeDateCACert", "InvalidCAnotBeforeDateTest1EE" }, new String[] { TRUST_ANCHOR_ROOT_CRL, "BadnotBeforeDateCACRL" }, 1, "Could not validate certificate: certificate not valid till 20470101120100GMT+00:00"); } public void testInvalidEENotBeforeDateTest2() throws Exception { doExceptionTest(TRUST_ANCHOR_ROOT_CERTIFICATE, new String[] { GOOD_CA_CERT, "InvalidEEnotBeforeDateTest2EE" }, new String[] { TRUST_ANCHOR_ROOT_CRL, GOOD_CA_CRL }, 0, "Could not validate certificate: certificate not valid till 20470101120100GMT+00:00"); } public void testValidPre2000UTCNotBeforeDateTest3() throws Exception { doTest(TRUST_ANCHOR_ROOT_CERTIFICATE, new String[] { GOOD_CA_CERT, "Validpre2000UTCnotBeforeDateTest3EE" }, new String[] { TRUST_ANCHOR_ROOT_CRL, GOOD_CA_CRL }); } public void testValidGeneralizedTimeNotBeforeDateTest4() throws Exception { doTest(TRUST_ANCHOR_ROOT_CERTIFICATE, new String[] { GOOD_CA_CERT, "ValidGeneralizedTimenotBeforeDateTest4EE" }, new String[] { TRUST_ANCHOR_ROOT_CRL, GOOD_CA_CRL }); } public void testInvalidCANotAfterDateTest5() throws Exception { doExceptionTest(TRUST_ANCHOR_ROOT_CERTIFICATE, new String[] { "BadnotAfterDateCACert", "InvalidCAnotAfterDateTest5EE" }, new String[] { TRUST_ANCHOR_ROOT_CRL, "BadnotAfterDateCACRL" }, 1, "Could not validate certificate: certificate expired on 20020101120100GMT+00:00"); } public void testInvalidEENotAfterDateTest6() throws Exception { doExceptionTest(TRUST_ANCHOR_ROOT_CERTIFICATE, new String[] { GOOD_CA_CERT, "InvalidEEnotAfterDateTest6EE" }, new String[] { TRUST_ANCHOR_ROOT_CRL, GOOD_CA_CRL }, 0, "Could not validate certificate: certificate expired on 20020101120100GMT+00:00"); } public void testInvalidValidPre2000UTCNotAfterDateTest7() throws Exception { doExceptionTest(TRUST_ANCHOR_ROOT_CERTIFICATE, new String[] { GOOD_CA_CERT, "Invalidpre2000UTCEEnotAfterDateTest7EE" }, new String[] { TRUST_ANCHOR_ROOT_CRL, GOOD_CA_CRL }, 0, "Could not validate certificate: certificate expired on 19990101120100GMT+00:00"); } public void testInvalidNegativeSerialNumberTest15() throws Exception { doExceptionTest(TRUST_ANCHOR_ROOT_CERTIFICATE, new String[] { "NegativeSerialNumberCACert", "InvalidNegativeSerialNumberTest15EE" }, new String[] { TRUST_ANCHOR_ROOT_CRL, "NegativeSerialNumberCACRL" }, 0, "Certificate revocation after Fri Apr 20 00:57:20", "reason: keyCompromise"); } // // 4.8 Certificate Policies // public void testAllCertificatesSamePolicyTest1() throws Exception { String[] certList = new String[] { GOOD_CA_CERT, "ValidCertificatePathTest1EE" }; String[] crlList = new String[] { TRUST_ANCHOR_ROOT_CRL, GOOD_CA_CRL }; doTest(TRUST_ANCHOR_ROOT_CERTIFICATE, certList, crlList, noPolicies); doTest(TRUST_ANCHOR_ROOT_CERTIFICATE, certList, crlList, nistTestPolicy1); doExceptionTest(TRUST_ANCHOR_ROOT_CERTIFICATE, certList, crlList, nistTestPolicy2, -1, "Path processing failed on policy."); doTest(TRUST_ANCHOR_ROOT_CERTIFICATE, certList, crlList, nistTestPolicy1And2); } public void testAllCertificatesNoPoliciesTest2() throws Exception { doTest(TRUST_ANCHOR_ROOT_CERTIFICATE, new String[] { "NoPoliciesCACert", "AllCertificatesNoPoliciesTest2EE" }, new String[] { TRUST_ANCHOR_ROOT_CRL, "NoPoliciesCACRL" }); doExceptionTest(TRUST_ANCHOR_ROOT_CERTIFICATE, new String[] { "NoPoliciesCACert", "AllCertificatesNoPoliciesTest2EE" }, new String[] { TRUST_ANCHOR_ROOT_CRL, "NoPoliciesCACRL" }, noPolicies, 1, "No valid policy tree found when one expected."); } public void testDifferentPoliciesTest3() throws Exception { doTest(TRUST_ANCHOR_ROOT_CERTIFICATE, new String[] { GOOD_CA_CERT, "PoliciesP2subCACert", "DifferentPoliciesTest3EE" }, new String[] { TRUST_ANCHOR_ROOT_CRL, GOOD_CA_CRL, "PoliciesP2subCACRL" }); doExceptionTest(TRUST_ANCHOR_ROOT_CERTIFICATE, new String[] { GOOD_CA_CERT, "PoliciesP2subCACert", "DifferentPoliciesTest3EE" }, new String[] { TRUST_ANCHOR_ROOT_CRL, GOOD_CA_CRL, "PoliciesP2subCACRL" }, noPolicies, 1, "No valid policy tree found when one expected."); doExceptionTest(TRUST_ANCHOR_ROOT_CERTIFICATE, new String[] { GOOD_CA_CERT, "PoliciesP2subCACert", "DifferentPoliciesTest3EE" }, new String[] { TRUST_ANCHOR_ROOT_CRL, GOOD_CA_CRL, "PoliciesP2subCACRL" }, nistTestPolicy1And2, 1, "No valid policy tree found when one expected."); } public void testDifferentPoliciesTest4() throws Exception { doExceptionTest(TRUST_ANCHOR_ROOT_CERTIFICATE, new String[] { GOOD_CA_CERT, "GoodsubCACert", "DifferentPoliciesTest4EE" }, new String[] { TRUST_ANCHOR_ROOT_CRL, GOOD_CA_CRL, "GoodsubCACRL" }, 0, "No valid policy tree found when one expected."); } public void testDifferentPoliciesTest5() throws Exception
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -