Search in sources :

Example 1 with KeyStoreBuilderParameters

use of javax.net.ssl.KeyStoreBuilderParameters in project OpenAM by OpenRock.

the class AMX509KeyManagerImpl method initX509KeyManager.

// create sunX509KeyManager
//
// for example:
//     Create/load a keystore
//     Get instance of a "SunX509" KeyManagerFactory "kmf"
//     init the KeyManagerFactory with the keystore
public X509KeyManager initX509KeyManager(String ksType, String ksFile, String ksProvider, AMCallbackHandler cbHandle) {
    KeyManagerFactory kmf = null;
    // initialize KeyStore and get KeyManagerFactory 
    try {
        bundle = amCache.getResBundle(bundleName, Locale.getDefault());
        KeyStore.CallbackHandlerProtection callback = null;
        if (cbHandle != null) {
            callback = new KeyStore.CallbackHandlerProtection(cbHandle);
        } else {
            String passwdPrompt = bundle.getString("KeyStorePrompt");
            callback = new KeyStore.CallbackHandlerProtection(new AMCallbackHandler(passwdPrompt));
        }
        if (ksType.equalsIgnoreCase("JKS") || ksType.equalsIgnoreCase("PKCS12")) {
            builder = KeyStore.Builder.newInstance(ksType, Security.getProvider(ksProvider), new File(ksFile), callback);
        } else if (keyStoreType.equalsIgnoreCase("PKCS11")) {
            builder = KeyStore.Builder.newInstance(ksType, Security.getProvider(ksProvider), callback);
        }
        KeyStoreBuilderParameters param = new KeyStoreBuilderParameters(builder);
        kmf = KeyManagerFactory.getInstance(algorithm, provider);
        kmf.init(param);
    } catch (Exception e) {
        debug.error(e.toString());
    }
    return (X509KeyManager) kmf.getKeyManagers()[0];
}
Also used : KeyStoreBuilderParameters(javax.net.ssl.KeyStoreBuilderParameters) X509KeyManager(javax.net.ssl.X509KeyManager) KeyStore(java.security.KeyStore) File(java.io.File) KeyManagerFactory(javax.net.ssl.KeyManagerFactory)

Example 2 with KeyStoreBuilderParameters

use of javax.net.ssl.KeyStoreBuilderParameters in project robovm by robovm.

the class KeyStoreBuilderParametersTest method test_Constructor02.

/**
     * javax.net.ssl.KeyStoreBuilderParameters#KeyStoreBuilderParameters(List parameters)
     */
public void test_Constructor02() {
    // Null parameter
    try {
        KeyStoreBuilderParameters ksp = new KeyStoreBuilderParameters((List) null);
        fail();
    } catch (NullPointerException expected) {
    }
    // Empty parameter
    List lsEmpty = new ArrayList<String>();
    try {
        KeyStoreBuilderParameters ksp = new KeyStoreBuilderParameters(lsEmpty);
        fail();
    } catch (IllegalArgumentException expected) {
    }
    // Not null parameter
    List lsFiled = new ArrayList<String>();
    lsFiled.add("Parameter1");
    lsFiled.add("Parameter2");
    new KeyStoreBuilderParameters(lsFiled);
}
Also used : KeyStoreBuilderParameters(javax.net.ssl.KeyStoreBuilderParameters) ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList)

Example 3 with KeyStoreBuilderParameters

use of javax.net.ssl.KeyStoreBuilderParameters in project robovm by robovm.

the class KeyStoreBuilderParametersTest method test_Constructor01.

/**
     * javax.net.ssl.KeyStoreBuilderParameters#KeyStoreBuilderParameters(KeyStore.Builder builder)
     */
public void test_Constructor01() {
    // Null parameter
    try {
        new KeyStoreBuilderParameters((KeyStore.Builder) null);
        fail();
    } catch (NullPointerException expected) {
    }
    // Not null parameter
    KeyStore.ProtectionParameter pp = new ProtectionParameterImpl();
    KeyStore.Builder bld = KeyStore.Builder.newInstance("testType", null, pp);
    assertNotNull("Null object KeyStore.Builder", bld);
    KeyStoreBuilderParameters ksp = new KeyStoreBuilderParameters(bld);
    assertNotNull(ksp.getParameters());
}
Also used : KeyStoreBuilderParameters(javax.net.ssl.KeyStoreBuilderParameters) KeyStore(java.security.KeyStore)

Example 4 with KeyStoreBuilderParameters

use of javax.net.ssl.KeyStoreBuilderParameters 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 5 with KeyStoreBuilderParameters

use of javax.net.ssl.KeyStoreBuilderParameters 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)

Aggregations

KeyStoreBuilderParameters (javax.net.ssl.KeyStoreBuilderParameters)8 KeyStore (java.security.KeyStore)3 Builder (java.security.KeyStore.Builder)3 PasswordProtection (java.security.KeyStore.PasswordProtection)3 List (java.util.List)3 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)2 ArrayList (java.util.ArrayList)2 KeyManagerFactory (javax.net.ssl.KeyManagerFactory)2 TestKeyStore (libcore.java.security.TestKeyStore)2 File (java.io.File)1 ManagerFactoryParameters (javax.net.ssl.ManagerFactoryParameters)1 X509KeyManager (javax.net.ssl.X509KeyManager)1