Search in sources :

Example 1 with TestKeyPair

use of org.apache.harmony.security.tests.support.TestKeyPair in project robovm by robovm.

the class SignedObjectTest method testSignedObject.

public void testSignedObject() throws Exception {
    TestKeyPair tkp = null;
    Properties prop;
    Signature sig = Signature.getInstance("SHA1withDSA");
    try {
        tkp = new TestKeyPair("DSA");
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
        return;
    }
    prop = new Properties();
    prop.put("aaa", "bbb");
    SignedObject so = new SignedObject(prop, tkp.getPrivate(), sig);
    assertEquals("SHA1withDSA", so.getAlgorithm());
    assertEquals(prop, so.getObject());
    assertTrue("verify() failed", so.verify(tkp.getPublic(), sig));
    assertNotNull("signature is null", so.getSignature());
}
Also used : TestKeyPair(org.apache.harmony.security.tests.support.TestKeyPair) Signature(java.security.Signature) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) Properties(java.util.Properties) SignedObject(java.security.SignedObject)

Example 2 with TestKeyPair

use of org.apache.harmony.security.tests.support.TestKeyPair in project robovm by robovm.

the class X509CertSelectorTest method test_setSubjectPublicKeyAlgIDLjava_lang_String.

/**
     * java.security.cert.X509CertSelector#setSubjectPublicKeyAlgID(java.lang.String)
     */
public void test_setSubjectPublicKeyAlgIDLjava_lang_String() throws Exception {
    X509CertSelector selector = new X509CertSelector();
    // RSA (source:
    String pkaid1 = "1.2.840.113549.1.1.1";
    // http://asn1.elibel.tm.fr)
    // DSA (source:
    String pkaid2 = "1.2.840.10040.4.1";
    // http://asn1.elibel.tm.fr)
    PublicKey pkey1 = new TestKeyPair("RSA").getPublic();
    ;
    PublicKey pkey2 = new TestKeyPair("DSA").getPublic();
    ;
    TestCert cert1 = new TestCert(pkey1);
    TestCert cert2 = new TestCert(pkey2);
    selector.setSubjectPublicKeyAlgID(null);
    assertTrue("Any certificate should match in the case of null " + "subjectPublicKeyAlgID criteria.", selector.match(cert1) && selector.match(cert2));
    String[] validOIDs = { "0.0.20", "1.25.0", "2.0.39", "0.2.10", "1.35.15", "2.17.89", "2.5.29.16", "2.5.29.17", "2.5.29.30", "2.5.29.32", "2.5.29.37" };
    for (int i = 0; i < validOIDs.length; i++) {
        selector.setSubjectPublicKeyAlgID(validOIDs[i]);
        assertEquals(validOIDs[i], selector.getSubjectPublicKeyAlgID());
    }
    String[] invalidOIDs = { "0.20", "1.25", "2.39", "3.10" };
    for (int i = 0; i < invalidOIDs.length; i++) {
        try {
            selector.setSubjectPublicKeyAlgID(invalidOIDs[i]);
            fail("IOException wasn't thrown for " + invalidOIDs[i]);
        } catch (IOException expected) {
        }
    }
    selector.setSubjectPublicKeyAlgID(pkaid1);
    assertTrue("The certificate should match the selection criteria.", selector.match(cert1));
    assertFalse("The certificate should not match the selection criteria.", selector.match(cert2));
    selector.setSubjectPublicKeyAlgID(pkaid2);
    assertTrue("The certificate should match the selection criteria.", selector.match(cert2));
}
Also used : TestKeyPair(org.apache.harmony.security.tests.support.TestKeyPair) PublicKey(java.security.PublicKey) X509CertSelector(java.security.cert.X509CertSelector) ASN1OctetString(org.apache.harmony.security.asn1.ASN1OctetString) IOException(java.io.IOException)

Example 3 with TestKeyPair

use of org.apache.harmony.security.tests.support.TestKeyPair 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 4 with TestKeyPair

use of org.apache.harmony.security.tests.support.TestKeyPair 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 5 with TestKeyPair

use of org.apache.harmony.security.tests.support.TestKeyPair 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)

Aggregations

TestKeyPair (org.apache.harmony.security.tests.support.TestKeyPair)20 PublicKey (java.security.PublicKey)18 TrustAnchor (java.security.cert.TrustAnchor)16 X500Principal (javax.security.auth.x500.X500Principal)11 X509Certificate (java.security.cert.X509Certificate)4 ByteArrayInputStream (java.io.ByteArrayInputStream)3 CertificateFactory (java.security.cert.CertificateFactory)3 X509CertSelector (java.security.cert.X509CertSelector)3 IOException (java.io.IOException)2 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)2 KnownFailure (dalvik.annotation.KnownFailure)1 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)1 KeyStore (java.security.KeyStore)1 KeyStoreException (java.security.KeyStoreException)1 NoSuchProviderException (java.security.NoSuchProviderException)1 Signature (java.security.Signature)1 SignedObject (java.security.SignedObject)1 CertificateException (java.security.cert.CertificateException)1 PKIXBuilderParameters (java.security.cert.PKIXBuilderParameters)1 HashSet (java.util.HashSet)1