Search in sources :

Example 1 with Builder

use of java.security.KeyStore.Builder in project robovm by robovm.

the class KeyManagerFactoryTest method test_KeyManagerFactory.

private void test_KeyManagerFactory(KeyManagerFactory kmf) throws Exception {
    assertNotNull(kmf);
    assertNotNull(kmf.getAlgorithm());
    assertNotNull(kmf.getProvider());
    // before init
    try {
        kmf.getKeyManagers();
        fail();
    } catch (IllegalStateException expected) {
    }
    // init with null ManagerFactoryParameters
    try {
        kmf.init(null);
        fail();
    } catch (InvalidAlgorithmParameterException expected) {
    }
    // init with useless ManagerFactoryParameters
    try {
        kmf.init(new UselessManagerFactoryParameters());
        fail();
    } catch (InvalidAlgorithmParameterException expected) {
    }
    // init with KeyStoreBuilderParameters ManagerFactoryParameters
    PasswordProtection pp = new PasswordProtection(getTestKeyStore().storePassword);
    Builder builder = Builder.newInstance(getTestKeyStore().keyStore, pp);
    KeyStoreBuilderParameters ksbp = new KeyStoreBuilderParameters(builder);
    if (supportsManagerFactoryParameters(kmf.getAlgorithm())) {
        kmf.init(ksbp);
        test_KeyManagerFactory_getKeyManagers(kmf, false);
    } else {
        try {
            kmf.init(ksbp);
            fail();
        } catch (InvalidAlgorithmParameterException expected) {
        }
    }
    // init with null for default behavior
    kmf.init(null, null);
    test_KeyManagerFactory_getKeyManagers(kmf, true);
    // init with specific key store and password
    kmf.init(getTestKeyStore().keyStore, getTestKeyStore().storePassword);
    test_KeyManagerFactory_getKeyManagers(kmf, false);
}
Also used : InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) KeyStoreBuilderParameters(javax.net.ssl.KeyStoreBuilderParameters) Builder(java.security.KeyStore.Builder) PasswordProtection(java.security.KeyStore.PasswordProtection)

Example 2 with Builder

use of java.security.KeyStore.Builder in project robovm by robovm.

the class KeyStoreBuilderParametersTest method test_init_List.

public void test_init_List() {
    TestKeyStore testKeyStore1 = TestKeyStore.getClient();
    TestKeyStore testKeyStore2 = TestKeyStore.getServer();
    Builder builder1 = Builder.newInstance(testKeyStore1.keyStore, new PasswordProtection(testKeyStore1.storePassword));
    Builder builder2 = Builder.newInstance(testKeyStore2.keyStore, new PasswordProtection(testKeyStore2.storePassword));
    List list = Arrays.asList(builder1, builder2);
    KeyStoreBuilderParameters ksbp = new KeyStoreBuilderParameters(list);
    assertNotNull(ksbp);
    assertNotNull(ksbp.getParameters());
    assertNotSame(list, ksbp.getParameters());
    assertEquals(2, ksbp.getParameters().size());
    assertSame(builder1, ksbp.getParameters().get(0));
    assertSame(builder2, ksbp.getParameters().get(1));
    // confirm result is not modifiable
    try {
        ksbp.getParameters().set(0, builder2);
        fail();
    } catch (UnsupportedOperationException expected) {
    }
    // confirm result is a copy of original
    list.set(0, builder2);
    assertSame(builder1, ksbp.getParameters().get(0));
}
Also used : TestKeyStore(libcore.java.security.TestKeyStore) KeyStoreBuilderParameters(javax.net.ssl.KeyStoreBuilderParameters) Builder(java.security.KeyStore.Builder) List(java.util.List) PasswordProtection(java.security.KeyStore.PasswordProtection)

Example 3 with Builder

use of java.security.KeyStore.Builder in project jitsi by jitsi.

the class CertificateServiceImpl method loadKeyStore.

private Builder loadKeyStore(final CertificateConfigEntry entry) throws KeyStoreException {
    final File f = new File(entry.getKeyStore());
    final KeyStoreType kt = entry.getKeyStoreType();
    if ("PKCS11".equals(kt.getName())) {
        String config = "name=" + f.getName() + "\nlibrary=" + f.getAbsoluteFile();
        try {
            Class<?> pkcs11c = Class.forName("sun.security.pkcs11.SunPKCS11");
            Constructor<?> c = pkcs11c.getConstructor(InputStream.class);
            Provider p = (Provider) c.newInstance(new ByteArrayInputStream(config.getBytes()));
            Security.insertProviderAt(p, 0);
        } catch (Exception e) {
            logger.error("Tried to access the PKCS11 provider on an " + "unsupported platform or the load failed", e);
        }
    }
    KeyStore.Builder ksBuilder = KeyStore.Builder.newInstance(kt.getName(), null, f, new KeyStore.CallbackHandlerProtection(new CallbackHandler() {

        public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
            for (Callback cb : callbacks) {
                if (!(cb instanceof PasswordCallback))
                    throw new UnsupportedCallbackException(cb);
                PasswordCallback pwcb = (PasswordCallback) cb;
                if (entry.isSavePassword()) {
                    pwcb.setPassword(entry.getKeyStorePassword().toCharArray());
                    return;
                } else {
                    AuthenticationWindowService authenticationWindowService = CertificateVerificationActivator.getAuthenticationWindowService();
                    if (authenticationWindowService == null) {
                        logger.error("No AuthenticationWindowService " + "implementation");
                        throw new IOException("User cancel");
                    }
                    AuthenticationWindowService.AuthenticationWindow aw = authenticationWindowService.create(f.getName(), null, kt.getName(), false, false, null, null, null, null, null, null, null);
                    aw.setAllowSavePassword(false);
                    aw.setVisible(true);
                    if (!aw.isCanceled())
                        pwcb.setPassword(aw.getPassword());
                    else
                        throw new IOException("User cancel");
                }
            }
        }
    }));
    return ksBuilder;
}
Also used : Builder(java.security.KeyStore.Builder)

Example 4 with Builder

use of java.security.KeyStore.Builder in project robovm by robovm.

the class KeyStoreTest method test_KeyStore_Builder.

public void test_KeyStore_Builder() throws Exception {
    for (KeyStore keyStore : keyStores()) {
        keyStore.load(null, null);
        try {
            Builder.newInstance(keyStore, null);
            fail(keyStore.getType());
        } catch (NullPointerException expected) {
        }
    }
    for (KeyStore keyStore : keyStores()) {
        try {
            Builder.newInstance(keyStore.getType(), keyStore.getProvider(), null);
            fail(keyStore.getType());
        } catch (NullPointerException expected) {
        }
    }
    for (KeyStore keyStore : keyStores()) {
        try {
            Builder.newInstance(null, null, null, null);
            fail(keyStore.getType());
        } catch (NullPointerException expected) {
        }
        try {
            Builder.newInstance(keyStore.getType(), keyStore.getProvider(), null, null);
            fail(keyStore.getType());
        } catch (NullPointerException expected) {
        }
    }
    for (KeyStore keyStore : keyStores()) {
        keyStore.load(null, null);
        Builder builder = Builder.newInstance(keyStore, PARAM_STORE);
        try {
            builder.getProtectionParameter(null);
            fail(keyStore.getType());
        } catch (NullPointerException expected) {
        }
        assertEquals(keyStore, builder.getKeyStore());
        try {
            builder.getProtectionParameter(null);
            fail(keyStore.getType());
        } catch (NullPointerException expected) {
        }
        assertEquals(PARAM_STORE, builder.getProtectionParameter(""));
    }
    for (KeyStore keyStore : keyStores()) {
        populate(keyStore);
        File file = File.createTempFile("keystore", keyStore.getProvider().getName());
        OutputStream os = null;
        try {
            os = new FileOutputStream(file);
            if (isLoadStoreUnsupported(keyStore) || isReadOnly(keyStore)) {
                try {
                    keyStore.store(os, PASSWORD_STORE);
                    fail(keyStore.getType());
                } catch (UnsupportedOperationException expected) {
                }
                continue;
            }
            keyStore.store(os, PASSWORD_STORE);
            os.close();
            Builder builder = Builder.newInstance(keyStore.getType(), keyStore.getProvider(), file, PARAM_STORE);
            assertEquals(keyStore.getType(), builder.getKeyStore().getType());
            assertEquals(keyStore.getProvider(), builder.getKeyStore().getProvider());
            assertEquals(PARAM_STORE, builder.getProtectionParameter(""));
            assertEqualsKeyStores(file, PASSWORD_STORE, keyStore);
        } finally {
            try {
                if (os != null) {
                    os.close();
                }
            } catch (IOException ignored) {
            }
            file.delete();
        }
    }
    for (KeyStore keyStore : keyStores()) {
        if (isLoadStoreUnsupported(keyStore)) {
            continue;
        }
        Builder builder = Builder.newInstance(keyStore.getType(), keyStore.getProvider(), PARAM_STORE);
        assertEquals(keyStore.getType(), builder.getKeyStore().getType());
        assertEquals(keyStore.getProvider(), builder.getKeyStore().getProvider());
        assertEquals(PARAM_STORE, builder.getProtectionParameter(""));
    }
}
Also used : Builder(java.security.KeyStore.Builder) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) KeyStore(java.security.KeyStore) File(java.io.File)

Example 5 with Builder

use of java.security.KeyStore.Builder in project robovm by robovm.

the class KeyStoreBuilderParametersTest method test_init_Builder.

public void test_init_Builder() {
    TestKeyStore testKeyStore = TestKeyStore.getClient();
    Builder builder = Builder.newInstance(testKeyStore.keyStore, new PasswordProtection(testKeyStore.storePassword));
    KeyStoreBuilderParameters ksbp = new KeyStoreBuilderParameters(builder);
    assertNotNull(ksbp);
    assertNotNull(ksbp.getParameters());
    assertEquals(1, ksbp.getParameters().size());
    assertSame(builder, ksbp.getParameters().get(0));
}
Also used : TestKeyStore(libcore.java.security.TestKeyStore) KeyStoreBuilderParameters(javax.net.ssl.KeyStoreBuilderParameters) Builder(java.security.KeyStore.Builder) PasswordProtection(java.security.KeyStore.PasswordProtection)

Aggregations

Builder (java.security.KeyStore.Builder)5 PasswordProtection (java.security.KeyStore.PasswordProtection)3 KeyStoreBuilderParameters (javax.net.ssl.KeyStoreBuilderParameters)3 TestKeyStore (libcore.java.security.TestKeyStore)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1 OutputStream (java.io.OutputStream)1 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)1 KeyStore (java.security.KeyStore)1 List (java.util.List)1