Search in sources :

Example 51 with KeyStoreException

use of java.security.KeyStoreException in project robovm by robovm.

the class KeyStoreBuilderTest method testNewInstanceKeyStoreProtectionParameter.

/*
     * test for method newInstance(KeyStore, KeyStore.ProtectionParameter)
     */
public void testNewInstanceKeyStoreProtectionParameter() throws KeyStoreException, NoSuchAlgorithmException, IOException, CertificateException {
    try {
        KeyStore.Builder.newInstance(null, null);
        fail("NullPointerException must be thrown");
    } catch (NullPointerException e) {
    // expected
    }
    try {
        KeyStore.Builder.newInstance(null, protPass);
        fail("NullPointerException must be thrown");
    } catch (NullPointerException e) {
    // expected
    }
    KeyStore.Builder ksB;
    KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());
    try {
        KeyStore.Builder.newInstance(ks, null);
        fail("NullPointerException must be thrown when ProtectionParameter is null");
    } catch (NullPointerException e) {
    // expected
    }
    KeyStore.PasswordProtection protPass1 = new KeyStore.PasswordProtection(pass);
    KeyStore.ProtectionParameter[] pp = { protPass, protPass1, callbackHand, myProtParam };
    for (int i = 0; i < pp.length; i++) {
        ks = KeyStore.getInstance(KeyStore.getDefaultType());
        try {
            KeyStore.Builder.newInstance(ks, pp[i]);
            fail("IllegalArgumentException must be thrown because KeyStore was not initialized");
        } catch (IllegalArgumentException e) {
        // expected
        }
        ks.load(null, pass);
        ksB = KeyStore.Builder.newInstance(ks, pp[i]);
        assertEquals("Incorrect KeyStore", ksB.getKeyStore().size(), 0);
        ksB = KeyStore.Builder.newInstance(ks, pp[i]);
        // verification getKeyStore() and getProtectionParameter(String
        // alias)
        assertEquals("Incorrect KeyStore", ks, ksB.getKeyStore());
        try {
            ksB.getProtectionParameter(null);
            fail("NullPointerException must be thrown");
        } catch (NullPointerException e) {
        }
        try {
            assertEquals(ksB.getProtectionParameter("aaa"), pp[i]);
        } catch (KeyStoreException e) {
            fail("Unexpected: " + e.toString() + " was thrown");
        }
        try {
            assertEquals(ksB.getProtectionParameter("Bad alias"), pp[i]);
        } catch (KeyStoreException e) {
        // KeyStoreException might be thrown because there is no entry
        // with such alias
        }
        try {
            assertEquals(ksB.getProtectionParameter(""), pp[i]);
        } catch (KeyStoreException e) {
        // KeyStoreException might be thrown because there is no entry
        // with such alias
        }
        KeyStore.ProtectionParameter pPar = ksB.getProtectionParameter("aaa");
        switch(i) {
            case 0:
                assertTrue(pPar instanceof KeyStore.PasswordProtection);
                break;
            case 1:
                assertTrue(pPar instanceof KeyStore.PasswordProtection);
                break;
            case 2:
                assertTrue(pPar instanceof KeyStore.CallbackHandlerProtection);
                break;
            case 3:
                assertTrue(pPar instanceof MyProtectionParameter);
                break;
            default:
                fail("Incorrect protection parameter");
        }
        assertEquals(pPar, pp[i]);
    }
}
Also used : KeyStoreException(java.security.KeyStoreException) KeyStore(java.security.KeyStore)

Example 52 with KeyStoreException

use of java.security.KeyStoreException in project robovm by robovm.

the class KeyStoreBuilderTest method testNewInstanceStringProviderFileProtectionParameter.

/*
     * Test for methods: <code>newInstance(String type, Provider provider, File
     * file, ProtectionParameter protectionParameter)</code> <code>getKeyStore()</code>
     * <code>getProtectionParameter(String alias)</code> Assertions: throws
     * NullPointerException if type, file or protectionParameter is null; throws
     * IllegalArgumentException if file does not exist or is not file; throws
     * IllegalArgumentException if ProtectionParameter is not PasswordProtection
     * or CallbackHandlerProtection; returns new object
     *
     * getKeyStore() returns specified keystore; getProtectionParameter(String
     * alias) throws NullPointerException when alias is null; throws
     * KeyStoreException when alias is not available; returns
     * ProtectionParameter which is used in newInstance(...)
     *
     */
public void testNewInstanceStringProviderFileProtectionParameter() throws Exception {
    File fl = File.createTempFile("KSBuilder_ImplTest", "keystore");
    fl.deleteOnExit();
    KeyStore.Builder ksB;
    KeyStore.Builder ksB1;
    KeyStore ks = null;
    KeyStore ks1 = null;
    MyProtectionParameter myPP = new MyProtectionParameter(new byte[5]);
    // check exceptions
    try {
        KeyStore.Builder.newInstance(null, defaultProvider, fl, protPass);
        fail("NullPointerException must be thrown when type is null");
    } catch (NullPointerException e) {
    }
    try {
        KeyStore.Builder.newInstance(KeyStore.getDefaultType(), defaultProvider, null, protPass);
        fail("NullPointerException must be thrown when file is null");
    } catch (NullPointerException e) {
    }
    try {
        KeyStore.Builder.newInstance(KeyStore.getDefaultType(), defaultProvider, fl, null);
        fail("NullPointerException must be thrown when ProtectionParameter is null");
    } catch (NullPointerException e) {
    }
    try {
        KeyStore.Builder.newInstance(KeyStore.getDefaultType(), defaultProvider, fl, myPP);
        fail("IllegalArgumentException must be thrown when ProtectionParameter is not correct");
    } catch (IllegalArgumentException e) {
    }
    try {
        KeyStore.Builder.newInstance(KeyStore.getDefaultType(), defaultProvider, new File(fl.getAbsolutePath().concat("should_absent")), protPass);
        fail("IllegalArgumentException must be thrown when file does not exist");
    } catch (IllegalArgumentException e) {
    }
    try {
        // 'file' param points to directory
        KeyStore.Builder.newInstance(KeyStore.getDefaultType(), defaultProvider, fl.getParentFile(), protPass);
        fail("IllegalArgumentException must be thrown when file does not exist");
    } catch (IllegalArgumentException e) {
    }
    ksB = KeyStore.Builder.newInstance(KeyStore.getDefaultType(), defaultProvider, fl, protPass);
    try {
        ksB.getKeyStore();
        fail("KeyStoreException must be throw because file is empty");
    } catch (KeyStoreException e) {
    }
    fl = createKS();
    // Exception Tests with custom ProtectionParameter
    try {
        KeyStore.Builder.newInstance(KeyStore.getDefaultType(), null, fl, myPP);
        fail("IllegalArgumentException must be " + "thrown for incorrect ProtectionParameter");
    } catch (IllegalArgumentException e) {
    }
    try {
        KeyStore.Builder.newInstance(KeyStore.getDefaultType(), defaultProvider, fl, myPP);
        fail("IllegalArgumentException must be " + "thrown for incorrect ProtectionParameter");
    } catch (IllegalArgumentException e) {
    }
    // Tests with PasswordProtection
    ksB = KeyStore.Builder.newInstance(KeyStore.getDefaultType(), null, fl, protPass);
    ksB1 = KeyStore.Builder.newInstance(KeyStore.getDefaultType(), defaultProvider, fl, protPass);
    try {
        ks = ksB.getKeyStore();
    } catch (KeyStoreException e) {
        fail("Unexpected KeyException was thrown");
    }
    try {
        ks1 = ksB1.getKeyStore();
    } catch (KeyStoreException e) {
        fail("Unexpected KeyException was thrown: " + e.getMessage());
    }
    assertEquals("Incorrect KeyStore size", ks.size(), ks1.size());
    ;
    for (Enumeration<String> aliases = ks.aliases(); aliases.hasMoreElements(); ) {
        String aName = aliases.nextElement();
        try {
            assertEquals("Incorrect ProtectionParameter", ksB.getProtectionParameter(aName), protPass);
        } catch (Exception e) {
            fail("Unexpected: " + e.toString() + " was thrown for alias: " + aName);
        }
    }
    ksB.getKeyStore();
    try {
        assertEquals(ksB.getProtectionParameter("Bad alias"), protPass);
    } catch (KeyStoreException e) {
    // KeyStoreException might be thrown because there is no entry
    // with such alias
    }
    for (Enumeration<String> aliases = ks1.aliases(); aliases.hasMoreElements(); ) {
        String aName = aliases.nextElement();
        assertEquals("Incorrect ProtectionParameter", ksB1.getProtectionParameter(aName), protPass);
    }
    try {
        assertEquals(ksB1.getProtectionParameter("Bad alias"), protPass);
    } catch (KeyStoreException e) {
    // KeyStoreException might be thrown because there is no entry
    // with such alias
    }
    // Tests with CallbackHandlerProtection
    ksB = KeyStore.Builder.newInstance(KeyStore.getDefaultType(), null, fl, callbackHand);
    ksB1 = KeyStore.Builder.newInstance(KeyStore.getDefaultType(), defaultProvider, fl, callbackHand);
    try {
        ks = ksB.getKeyStore();
        fail("KeyStoreException must be thrown for incorrect " + "ProtectionParameter");
    } catch (KeyStoreException e) {
    }
    try {
        ks1 = ksB1.getKeyStore();
        fail("KeyStoreException must be thrown for incorrect " + "ProtectionParameter");
    } catch (KeyStoreException e) {
    }
    assertEquals("Incorrect KeyStore size", ks.size(), ks1.size());
    for (Enumeration<String> aliases = ks.aliases(); aliases.hasMoreElements(); ) {
        String aName = aliases.nextElement();
        try {
            assertEquals("Incorrect ProtectionParameter", ksB.getProtectionParameter(aName), callbackHand);
        } catch (Exception e) {
            fail("Unexpected: " + e.toString() + " was thrown for alias: " + aName);
        }
    }
    for (Enumeration<String> iter = ks1.aliases(); iter.hasMoreElements(); ) {
        String aName = iter.nextElement();
        assertEquals("Incorrect ProtectionParameter", ksB1.getProtectionParameter(aName), callbackHand);
    }
}
Also used : KeyStoreException(java.security.KeyStoreException) File(java.io.File) KeyStore(java.security.KeyStore) IOException(java.io.IOException) KeyStoreException(java.security.KeyStoreException) CertificateException(java.security.cert.CertificateException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException)

Example 53 with KeyStoreException

use of java.security.KeyStoreException in project robovm by robovm.

the class KeyStore2Test method test_getCertificateLjava_lang_String.

/**
     * java.security.KeyStore#getCertificate(java.lang.String)
     */
public void test_getCertificateLjava_lang_String() throws Exception {
    // Test for method java.security.cert.Certificate
    // java.security.KeyStore.getCertificate(java.lang.String)
    CertificateFactory cf = CertificateFactory.getInstance("X.509");
    X509Certificate[] cert = new X509Certificate[2];
    cert[0] = (X509Certificate) cf.generateCertificate(certArray);
    cert[1] = (X509Certificate) cf.generateCertificate(certArray2);
    KeyStore keyTest = KeyStore.getInstance(KeyStore.getDefaultType());
    try {
        keyTest.getCertificate("anAlias");
        fail();
    } catch (KeyStoreException expected) {
    }
    keyTest.load(null, null);
    // alias 1
    PublicKey pub = cert[0].getPublicKey();
    keyTest.setCertificateEntry("alias1", cert[0]);
    Certificate certRes = keyTest.getCertificate("alias1");
    assertEquals("the public key of the certificate from getCertificate() " + "did not equal the original certificate", pub, certRes.getPublicKey());
    // alias 2
    keyTest.setCertificateEntry("alias2", cert[0]);
    // testing for a certificate chain
    Certificate cert2 = keyTest.getCertificate("alias2");
    assertEquals("the certificate for alias2 is supposed to exist", cert2, cert[0]);
}
Also used : PublicKey(java.security.PublicKey) KeyStoreException(java.security.KeyStoreException) CertificateFactory(java.security.cert.CertificateFactory) KeyStore(java.security.KeyStore) X509Certificate(java.security.cert.X509Certificate) X509Certificate(java.security.cert.X509Certificate) Certificate(java.security.cert.Certificate)

Example 54 with KeyStoreException

use of java.security.KeyStoreException in project robovm by robovm.

the class KeyStore2Test method test_containsAliasLjava_lang_String.

/**
     * java.security.KeyStore#containsAlias(java.lang.String)
     */
public void test_containsAliasLjava_lang_String() throws Exception {
    // Test for method boolean
    // java.security.KeyStore.containsAlias(java.lang.String)
    CertificateFactory cf = CertificateFactory.getInstance("X.509");
    X509Certificate[] cert = new X509Certificate[2];
    cert[0] = (X509Certificate) cf.generateCertificate(certArray);
    cert[1] = (X509Certificate) cf.generateCertificate(certArray2);
    KeyStore keyTest = KeyStore.getInstance(KeyStore.getDefaultType());
    try {
        keyTest.containsAlias("alias1");
        fail();
    } catch (KeyStoreException expected) {
    }
    keyTest.load(null, null);
    // alias 1
    keyTest.setCertificateEntry("alias1", cert[0]);
    // alias 2
    keyTest.setCertificateEntry("alias2", cert[0]);
    assertTrue("alias1 does not exist", keyTest.containsAlias("alias1"));
    assertTrue("alias2 does not exist", keyTest.containsAlias("alias2"));
    assertFalse("alias3 exists", keyTest.containsAlias("alias3"));
    try {
        keyTest.containsAlias(null);
        fail();
    } catch (NullPointerException expected) {
    }
}
Also used : KeyStoreException(java.security.KeyStoreException) CertificateFactory(java.security.cert.CertificateFactory) KeyStore(java.security.KeyStore) X509Certificate(java.security.cert.X509Certificate)

Example 55 with KeyStoreException

use of java.security.KeyStoreException in project robovm by robovm.

the class KeyStore2Test method test_size.

/**
     * java.security.KeyStore#size()
     */
public void test_size() throws Exception {
    // Test for method int java.security.KeyStore.size()
    CertificateFactory cf = CertificateFactory.getInstance("X.509");
    X509Certificate[] cert = new X509Certificate[2];
    cert[0] = (X509Certificate) cf.generateCertificate(certArray);
    cert[1] = (X509Certificate) cf.generateCertificate(certArray2);
    KeyStore keyTest = KeyStore.getInstance(KeyStore.getDefaultType());
    try {
        keyTest.size();
        fail();
    } catch (KeyStoreException expected) {
    }
    keyTest.load(null, null);
    // alias 1
    keyTest.setCertificateEntry("alias1", cert[0]);
    // alias 2
    keyTest.setKeyEntry("alias2", getPrivateKey(), pssWord, cert);
    // alias 3
    keyTest.setCertificateEntry("alias3", cert[1]);
    assertEquals("the size of the keyStore is not 3", 3, keyTest.size());
}
Also used : KeyStoreException(java.security.KeyStoreException) CertificateFactory(java.security.cert.CertificateFactory) KeyStore(java.security.KeyStore) X509Certificate(java.security.cert.X509Certificate)

Aggregations

KeyStoreException (java.security.KeyStoreException)342 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)176 IOException (java.io.IOException)153 CertificateException (java.security.cert.CertificateException)126 KeyStore (java.security.KeyStore)122 X509Certificate (java.security.cert.X509Certificate)100 UnrecoverableKeyException (java.security.UnrecoverableKeyException)85 Certificate (java.security.cert.Certificate)62 KeyManagementException (java.security.KeyManagementException)52 CertificateFactory (java.security.cert.CertificateFactory)37 ByteArrayInputStream (java.io.ByteArrayInputStream)33 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)32 PrivateKey (java.security.PrivateKey)31 InputStream (java.io.InputStream)30 TrustManagerFactory (javax.net.ssl.TrustManagerFactory)30 FileNotFoundException (java.io.FileNotFoundException)29 InvalidKeyException (java.security.InvalidKeyException)29 TrustManager (javax.net.ssl.TrustManager)28 SSLContext (javax.net.ssl.SSLContext)27 BadPaddingException (javax.crypto.BadPaddingException)26