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);
}
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));
}
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;
}
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(""));
}
}
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));
}
Aggregations