Search in sources :

Example 51 with CertPath

use of java.security.cert.CertPath in project robovm by robovm.

the class CertificateFactoryTest method testGenerateCertPath.

/* CertPath tests */
public void testGenerateCertPath() throws Exception {
    KeyHolder ca = generateCertificate(true, null);
    KeyHolder cert1 = generateCertificate(true, ca);
    KeyHolder cert2 = generateCertificate(false, cert1);
    KeyHolder cert3 = generateCertificate(false, cert2);
    List<X509Certificate> certs = new ArrayList<X509Certificate>();
    certs.add(cert3.certificate);
    certs.add(cert2.certificate);
    certs.add(cert1.certificate);
    List<X509Certificate> duplicatedCerts = new ArrayList<X509Certificate>(certs);
    duplicatedCerts.add(cert2.certificate);
    Provider[] providers = Security.getProviders("CertificateFactory.X509");
    for (Provider p : providers) {
        final CertificateFactory cf = CertificateFactory.getInstance("X.509", p);
        // Duplicate certificates can cause an exception.
        {
            final CertPath duplicatedPath = cf.generateCertPath(duplicatedCerts);
            try {
                duplicatedPath.getEncoded();
                if (StandardNames.IS_RI) {
                    fail("duplicate certificates should cause failure: " + p.getName());
                }
            } catch (CertificateEncodingException expected) {
                if (!StandardNames.IS_RI) {
                    fail("duplicate certificates should pass: " + p.getName());
                }
            }
        }
        testCertPathEncoding(cf, certs, null);
        /* Make sure all encoding entries are the same. */
        final Iterator<String> it1 = cf.getCertPathEncodings();
        final Iterator<String> it2 = cf.generateCertPath(certs).getEncodings();
        for (; ; ) {
            assertEquals(p.getName(), it1.hasNext(), it2.hasNext());
            if (!it1.hasNext()) {
                break;
            }
            String encoding = it1.next();
            assertEquals(p.getName(), encoding, it2.next());
            try {
                it1.remove();
                fail("Should not be able to remove from iterator");
            } catch (UnsupportedOperationException expected) {
            }
            try {
                it2.remove();
                fail("Should not be able to remove from iterator");
            } catch (UnsupportedOperationException expected) {
            }
            /* Now test using this encoding. */
            testCertPathEncoding(cf, certs, encoding);
        }
    }
}
Also used : ArrayList(java.util.ArrayList) CertificateEncodingException(java.security.cert.CertificateEncodingException) CertPath(java.security.cert.CertPath) CertificateFactory(java.security.cert.CertificateFactory) X509Certificate(java.security.cert.X509Certificate) Provider(java.security.Provider)

Example 52 with CertPath

use of java.security.cert.CertPath in project robovm by robovm.

the class CodeSignerTest method testEqualsObject.

/**
     * Test various assertions about equals()
     */
public final void testEqualsObject() {
    CodeSigner one = new CodeSigner(cpath, ts);
    CodeSigner two = new CodeSigner(cpath, ts);
    CodeSigner three = new CodeSigner(cpath, null);
    CertPath cpath2 = TestCertUtils.genCertPath(5, 3);
    CodeSigner four = new CodeSigner(cpath2, null);
    assertTrue(one.equals(one));
    assertTrue(one.equals(two));
    assertTrue(two.equals(one));
    assertFalse(one.equals(three));
    assertFalse(three.equals(one));
    assertTrue(three.equals(three));
    // different CertPaths
    assertFalse(three.equals(four));
    // special cases
    assertFalse(one.equals(null));
    assertFalse(one.equals(new Object()));
}
Also used : CertPath(java.security.cert.CertPath) CodeSigner(java.security.CodeSigner)

Example 53 with CertPath

use of java.security.cert.CertPath in project robovm by robovm.

the class CertificateFactory3Test method testGenerateCertPath02.

/**
     * Test for
     * <code>generateCertPath(InputStream inStream, String encoding)</code>
     * method Assertion: returns CertPath with 1 Certificate
     */
public void testGenerateCertPath02() throws Exception {
    CertificateFactory[] certFs = initCertFs();
    assertNotNull("CertificateFactory objects were not created", certFs);
    for (int i = 0; i < certFs.length; i++) {
        CertPath certPath = null;
        InputStream fis = Support_Resources.getResourceStream(fileCertPathPki);
        certPath = certFs[i].generateCertPath(fis, "PkiPath");
        fis.close();
        assertEquals(defaultType, certPath.getType());
        List<? extends Certificate> list1 = certPath.getCertificates();
        assertFalse("Result list is empty", list1.isEmpty());
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) CertPath(java.security.cert.CertPath) CertificateFactory(java.security.cert.CertificateFactory)

Example 54 with CertPath

use of java.security.cert.CertPath in project robovm by robovm.

the class CertificateFactory3Test method testGenerateCertPath03.

/**
     * Test for <code>generateCertPath(InputStream inStream)</code> method
     * Assertion: returns CertPath with 1 Certificate
     */
public void testGenerateCertPath03() throws Exception {
    String certPathEncoding = "PkiPath";
    CertificateFactory[] certFs = initCertFs();
    assertNotNull("CertificateFactory objects were not created", certFs);
    for (int i = 0; i < certFs.length; i++) {
        Iterator<String> it = certFs[0].getCertPathEncodings();
        assertTrue("no CertPath encodings", it.hasNext());
        assertEquals("Incorrect default encoding", certPathEncoding, it.next());
        CertPath certPath = null;
        InputStream fis = Support_Resources.getResourceStream(fileCertPathPki);
        certPath = certFs[i].generateCertPath(fis);
        fis.close();
        assertEquals(defaultType, certPath.getType());
        List<? extends Certificate> list1 = certPath.getCertificates();
        assertFalse("Result list is empty", list1.isEmpty());
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) CertPath(java.security.cert.CertPath) CertificateFactory(java.security.cert.CertificateFactory)

Example 55 with CertPath

use of java.security.cert.CertPath in project robovm by robovm.

the class PKIXCertPathBuilderResultTest method testGetCertPath.

/**
     * Test for <code>getCertPath()</code> method<br>
     * Assertion: the built and validated <code>CertPath</code>
     * (never <code>null</code>)
     * @throws NoSuchAlgorithmException
     * @throws InvalidKeySpecException
     */
public final void testGetCertPath() throws Exception {
    TrustAnchor ta = TestUtils.getTrustAnchor();
    if (ta == null) {
        fail(getName() + ": not performed (could not create test TrustAnchor)");
    }
    CertPath cp = new MyCertPath(testEncoding);
    CertPathBuilderResult r = new PKIXCertPathBuilderResult(cp, ta, TestUtils.getPolicyTree(), testPublicKey);
    // must return the same reference
    // as passed to the constructor
    assertSame(cp, r.getCertPath());
}
Also used : MyCertPath(org.apache.harmony.security.tests.support.cert.MyCertPath) PKIXCertPathBuilderResult(java.security.cert.PKIXCertPathBuilderResult) CertPathBuilderResult(java.security.cert.CertPathBuilderResult) PKIXCertPathBuilderResult(java.security.cert.PKIXCertPathBuilderResult) TrustAnchor(java.security.cert.TrustAnchor) MyCertPath(org.apache.harmony.security.tests.support.cert.MyCertPath) CertPath(java.security.cert.CertPath)

Aggregations

CertPath (java.security.cert.CertPath)86 X509Certificate (java.security.cert.X509Certificate)36 CertificateFactory (java.security.cert.CertificateFactory)29 Certificate (java.security.cert.Certificate)19 CertPathValidator (java.security.cert.CertPathValidator)18 CertPathValidatorException (java.security.cert.CertPathValidatorException)18 MyCertPath (org.apache.harmony.security.tests.support.cert.MyCertPath)17 CertificateException (java.security.cert.CertificateException)15 ArrayList (java.util.ArrayList)15 PKIXParameters (java.security.cert.PKIXParameters)14 MyFailingCertPath (org.apache.harmony.security.tests.support.cert.MyFailingCertPath)14 TrustAnchor (java.security.cert.TrustAnchor)12 HashSet (java.util.HashSet)12 ByteArrayInputStream (java.io.ByteArrayInputStream)11 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)11 CertPathBuilderResult (java.security.cert.CertPathBuilderResult)11 PKIXBuilderParameters (java.security.cert.PKIXBuilderParameters)10 PKIXCertPathValidatorResult (java.security.cert.PKIXCertPathValidatorResult)10 X509CertSelector (java.security.cert.X509CertSelector)10 CertPathBuilder (java.security.cert.CertPathBuilder)9