use of java.security.KeyStore.PasswordProtection 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.PasswordProtection 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.PasswordProtection in project Resurrection_packages_apps_Settings by ResurrectionRemix.
the class CertInstallerHelper method extractCertificate.
private void extractCertificate(String certFile, String password) {
InputStream in = null;
final byte[] raw;
java.security.KeyStore keystore = null;
try {
// Read .p12 file from SDCARD and extract with password
in = new FileInputStream(new File(Environment.getExternalStorageDirectory(), certFile));
raw = Streams.readFully(in);
keystore = java.security.KeyStore.getInstance("PKCS12");
PasswordProtection passwordProtection = new PasswordProtection(password.toCharArray());
keystore.load(new ByteArrayInputStream(raw), passwordProtection.getPassword());
// Install certificates and private keys
Enumeration<String> aliases = keystore.aliases();
if (!aliases.hasMoreElements()) {
Assert.fail("key store failed to put in keychain");
}
ArrayList<String> aliasesList = Collections.list(aliases);
// The keystore is initialized for each test case, there will
// be only one alias in the keystore
Assert.assertEquals(1, aliasesList.size());
String alias = aliasesList.get(0);
java.security.KeyStore.Entry entry = keystore.getEntry(alias, passwordProtection);
Log.d(TAG, "extracted alias = " + alias + ", entry=" + entry.getClass());
if (entry instanceof PrivateKeyEntry) {
Assert.assertTrue(installFrom((PrivateKeyEntry) entry));
}
} catch (IOException e) {
Assert.fail("Failed to read certficate: " + e);
} catch (KeyStoreException e) {
Log.e(TAG, "failed to extract certificate" + e);
} catch (NoSuchAlgorithmException e) {
Log.e(TAG, "failed to extract certificate" + e);
} catch (CertificateException e) {
Log.e(TAG, "failed to extract certificate" + e);
} catch (UnrecoverableEntryException e) {
Log.e(TAG, "failed to extract certificate" + e);
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
Log.e(TAG, "close FileInputStream error: " + e);
}
}
}
}
use of java.security.KeyStore.PasswordProtection in project j2objc by google.
the class KeyStoreTest method testKeyStoreCreate.
public void testKeyStoreCreate() {
KeyStore keyStore = null;
try {
keyStore = KeyStore.getInstance(algorithmName);
} catch (KeyStoreException e) {
fail(e.getMessage());
}
try {
keyStore.load(null, "the secret password".toCharArray());
} catch (NoSuchAlgorithmException e) {
fail(e.getMessage());
} catch (CertificateException e) {
fail(e.getMessage());
} catch (IOException e) {
fail(e.getMessage());
}
CertificateFactory certificateFactory = null;
try {
certificateFactory = CertificateFactory.getInstance("X.509");
} catch (CertificateException e) {
fail(e.getMessage());
}
Certificate certificate = null;
try {
certificate = certificateFactory.generateCertificate(new ByteArrayInputStream(encodedCertificate.getBytes()));
} catch (CertificateException e) {
fail(e.getMessage());
}
KeyPairGenerator generator = null;
try {
generator = KeyPairGenerator.getInstance(certificate.getPublicKey().getAlgorithm());
} catch (NoSuchAlgorithmException e) {
fail(e.getMessage());
}
KeyPair keyPair = generator.generateKeyPair();
PrivateKeyEntry privateKeyEntry = new PrivateKeyEntry(keyPair.getPrivate(), new Certificate[] { certificate });
try {
keyStore.setEntry("aPrivateKey", privateKeyEntry, new PasswordProtection("the key password".toCharArray()));
} catch (KeyStoreException e) {
fail(e.getMessage());
}
try {
assertTrue(keyStore.containsAlias("aPrivateKey"));
} catch (KeyStoreException e) {
fail(e.getMessage());
}
try {
PrivateKeyEntry entry = (PrivateKeyEntry) keyStore.getEntry("aPrivateKey", new PasswordProtection("the key password".toCharArray()));
PrivateKey privateKey = entry.getPrivateKey();
assertEquals(keyPair.getPrivate(), privateKey);
} catch (NoSuchAlgorithmException e) {
fail(e.getMessage());
} catch (UnrecoverableEntryException e) {
fail(e.getMessage());
} catch (KeyStoreException e) {
fail(e.getMessage());
}
try {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
keyStore.store(stream, "the keystore password".toCharArray());
assertTrue("keystore not written", stream.size() > 0);
} catch (KeyStoreException e) {
fail(e.getMessage());
} catch (NoSuchAlgorithmException e) {
fail(e.getMessage());
} catch (CertificateException e) {
fail(e.getMessage());
} catch (IOException e) {
fail(e.getMessage());
}
}
use of java.security.KeyStore.PasswordProtection 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