Search in sources :

Example 11 with TrustAnchor

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

the class TrustAnchorTest method testTrustAnchorX509CertificatebyteArray03.

/**
     * Test #3 for <code>TrustAnchor(X509Certificate, byte[])</code>
     * constructor<br>
     * Assertion: creates <code>TrustAnchor</code> instance<br>
     * Test preconditions: <code>null</code> as nameConstraints passed<br>
     * Expected: must pass without any exceptions
     */
public final void testTrustAnchorX509CertificatebyteArray03() throws Exception {
    CertificateFactory certFact = CertificateFactory.getInstance("X509");
    X509Certificate pemCert = (X509Certificate) certFact.generateCertificate(new ByteArrayInputStream(TestUtils.getX509Certificate_v3()));
    try {
        new TrustAnchor(pemCert, null);
    } catch (Exception e) {
        fail("Unexpected exeption " + e.getMessage());
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) TrustAnchor(java.security.cert.TrustAnchor) CertificateFactory(java.security.cert.CertificateFactory) X509Certificate(java.security.cert.X509Certificate) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) CertificateException(java.security.cert.CertificateException)

Example 12 with TrustAnchor

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

the class TrustAnchorTest method testGetTrustedCer02.

/**
     * Test #2 for <code>getCAName()</code> method<br>
     *
     * Assertion: returns ... <code>null</code> if <code>TrustAnchor</code>
     * was not specified as trusted certificate<br>
     * Test preconditions: test object is not specified as trusted certificate<br>
     * Expected: <code>null</code> as return value<br>
     * @throws InvalidKeySpecException
     */
public final void testGetTrustedCer02() throws Exception {
    PublicKey pk = new TestKeyPair(keyAlg).getPublic();
    // sub testcase 1
    TrustAnchor ta = new TrustAnchor(validCaNameRfc2253, pk, null);
    assertNull("null1", ta.getTrustedCert());
    // sub testcase 2
    X500Principal x500p = new X500Principal(validCaNameRfc2253);
    ta = new TrustAnchor(x500p, pk, null);
    assertNull("null2", ta.getTrustedCert());
    X509Certificate cert = new TestCertUtils.TestX509Certificate(x500p, x500p);
    TrustAnchor ta2 = new TrustAnchor(cert, null);
    assertSame(cert, ta2.getTrustedCert());
}
Also used : TestKeyPair(org.apache.harmony.security.tests.support.TestKeyPair) PublicKey(java.security.PublicKey) X500Principal(javax.security.auth.x500.X500Principal) TrustAnchor(java.security.cert.TrustAnchor) X509Certificate(java.security.cert.X509Certificate)

Example 13 with TrustAnchor

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

the class TrustAnchorTest method testToString.

/**
     * Test #1 for <code>toString()</code> method<br>
     *
     * Assertion: returns a formatted string describing the TrustAnchor<br>
     * Test preconditions: valid parameters are passed to the constructors<br>
     * Expected: not null string<br>
     */
public final void testToString() throws Exception {
    PublicKey pk = new TestKeyPair(keyAlg).getPublic();
    TrustAnchor ta1 = new TrustAnchor(validCaNameRfc2253, pk, getFullEncoding());
    assertNotNull(ta1.toString());
    X500Principal x500p = new X500Principal(validCaNameRfc2253);
    TrustAnchor ta2 = new TrustAnchor(x500p, pk, getEncodingNoMinMax());
    assertNotNull(ta2.toString());
    CertificateFactory certFact = CertificateFactory.getInstance("X509");
    X509Certificate pemCert = (X509Certificate) certFact.generateCertificate(new ByteArrayInputStream(TestUtils.getX509Certificate_v3()));
    TrustAnchor ta3 = new TrustAnchor(pemCert, getEncodingPSOnly());
    assertNotNull(ta3.toString());
}
Also used : TestKeyPair(org.apache.harmony.security.tests.support.TestKeyPair) ByteArrayInputStream(java.io.ByteArrayInputStream) PublicKey(java.security.PublicKey) X500Principal(javax.security.auth.x500.X500Principal) TrustAnchor(java.security.cert.TrustAnchor) CertificateFactory(java.security.cert.CertificateFactory) X509Certificate(java.security.cert.X509Certificate)

Example 14 with TrustAnchor

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

the class TrustAnchorTest method testTrustAnchorStringPublicKeybyteArray04.

/**
     * Test #4 for <code>TrustAnchor(String, PublicKey, byte[])</code> constructor<br>
     * Assertion: <code>NullPointerException</code> if <code>caName</code>
     * or <code>caPublicKey</code> parameter is <code>null</code><br>
     * Test preconditions: pass <code>null</code> as mentioned parameter<br>
     * Expected: NullPointerException
     */
public final void testTrustAnchorStringPublicKeybyteArray04() throws Exception {
    PublicKey pk = new TestKeyPair(keyAlg).getPublic();
    // sub testcase 1: 'caName' param is null
    try {
        new TrustAnchor((String) null, pk, getEncodingPSOnly());
        fail("NullPointerException has not been thrown");
    } catch (NullPointerException ok) {
    }
    // sub testcase 2: 'caPublicKey' param is null
    try {
        new TrustAnchor(validCaNameRfc2253, null, getEncodingPSOnly());
        fail("NullPointerException has not been thrown");
    } catch (NullPointerException ok) {
    }
    // sub testcase 3: 'caName' and 'caPublicKey' params are null
    try {
        new TrustAnchor((String) null, null, getEncodingPSOnly());
        fail("NullPointerException has not been thrown");
    } catch (NullPointerException ok) {
    }
    // sub testcase 4: 'caName' param is empty
    try {
        new TrustAnchor("", pk, getEncodingPSOnly());
        fail("IllegalArgumentException has not been thrown");
    } catch (IllegalArgumentException ok) {
    }
    // sub testcase 5: 'caName' param is incorrect distinguished name
    try {
        new TrustAnchor("AID.11.12=A", pk, getEncodingPSOnly());
        fail("IllegalArgumentException has not been thrown");
    } catch (IllegalArgumentException ok) {
    }
}
Also used : TestKeyPair(org.apache.harmony.security.tests.support.TestKeyPair) PublicKey(java.security.PublicKey) TrustAnchor(java.security.cert.TrustAnchor)

Example 15 with TrustAnchor

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

the class TrustAnchorTest method testTrustAnchorX509CertificatebyteArray01.

/**
     * Test #1 for <code>TrustAnchor(X509Certificate, byte[])</code>
     * constructor<br>
     * Assertion: creates <code>TrustAnchor</code> instance<br>
     * Test preconditions: valid parameters passed<br>
     * Expected: must pass without any exceptions
     */
public final void testTrustAnchorX509CertificatebyteArray01() throws CertificateException {
    CertificateFactory certFact = CertificateFactory.getInstance("X509");
    X509Certificate pemCert = (X509Certificate) certFact.generateCertificate(new ByteArrayInputStream(TestUtils.getX509Certificate_v3()));
    // sub testcase 1
    TrustAnchor ta1 = new TrustAnchor(pemCert, getFullEncoding());
    assertNull(ta1.getCA());
    assertNull(ta1.getCAName());
    assertNull(ta1.getCAPublicKey());
    assertTrue(Arrays.equals(getFullEncoding(), ta1.getNameConstraints()));
    assertEquals(pemCert, ta1.getTrustedCert());
    // sub testcase 2
    TrustAnchor ta2 = new TrustAnchor(pemCert, getEncodingPSOnly());
    assertNull(ta2.getCA());
    assertNull(ta2.getCAName());
    assertNull(ta2.getCAPublicKey());
    assertTrue(Arrays.equals(getEncodingPSOnly(), ta2.getNameConstraints()));
    assertEquals(pemCert, ta2.getTrustedCert());
    // sub testcase 3
    TrustAnchor ta3 = new TrustAnchor(pemCert, getEncodingESOnly());
    assertNull(ta3.getCA());
    assertNull(ta3.getCAName());
    assertNull(ta3.getCAPublicKey());
    assertTrue(Arrays.equals(getEncodingESOnly(), ta3.getNameConstraints()));
    assertEquals(pemCert, ta3.getTrustedCert());
    // sub testcase 4
    TrustAnchor ta4 = new TrustAnchor(pemCert, getEncodingNoMinMax());
    assertNull(ta4.getCA());
    assertNull(ta4.getCAName());
    assertNull(ta4.getCAPublicKey());
    assertTrue(Arrays.equals(getEncodingNoMinMax(), ta4.getNameConstraints()));
    assertEquals(pemCert, ta4.getTrustedCert());
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) TrustAnchor(java.security.cert.TrustAnchor) CertificateFactory(java.security.cert.CertificateFactory) X509Certificate(java.security.cert.X509Certificate)

Aggregations

TrustAnchor (java.security.cert.TrustAnchor)85 X509Certificate (java.security.cert.X509Certificate)34 PublicKey (java.security.PublicKey)24 X500Principal (javax.security.auth.x500.X500Principal)21 PKIXParameters (java.security.cert.PKIXParameters)17 TestKeyPair (org.apache.harmony.security.tests.support.TestKeyPair)16 HashSet (java.util.HashSet)15 CertificateFactory (java.security.cert.CertificateFactory)14 PKIXBuilderParameters (java.security.cert.PKIXBuilderParameters)13 PKIXCertPathValidatorResult (java.security.cert.PKIXCertPathValidatorResult)13 PKIXCertPathBuilderResult (java.security.cert.PKIXCertPathBuilderResult)12 X509CertSelector (java.security.cert.X509CertSelector)12 CertPathValidatorException (java.security.cert.CertPathValidatorException)11 ArrayList (java.util.ArrayList)11 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)10 ByteArrayInputStream (java.io.ByteArrayInputStream)8 IOException (java.io.IOException)7 CertPath (java.security.cert.CertPath)7 CertificateException (java.security.cert.CertificateException)7 Iterator (java.util.Iterator)7