use of libcore.java.security.TestKeyStore in project robovm by robovm.
the class X509KeyManagerTest method test_ChooseClientAlias_KeyType.
private void test_ChooseClientAlias_KeyType(String clientKeyType, String caKeyType, String selectedKeyType, boolean succeeds) throws Exception {
TestKeyStore ca = new TestKeyStore.Builder().keyAlgorithms(caKeyType).build();
TestKeyStore client = new TestKeyStore.Builder().keyAlgorithms(clientKeyType).signer(ca.getPrivateKey(caKeyType, caKeyType)).build();
KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
kmf.init(client.keyStore, client.keyPassword);
String[] keyTypes = new String[] { selectedKeyType };
KeyManager[] managers = kmf.getKeyManagers();
for (KeyManager manager : managers) {
if (manager instanceof X509KeyManager) {
String alias = ((X509KeyManager) manager).chooseClientAlias(keyTypes, null, null);
if (succeeds) {
assertNotNull(alias);
} else {
assertNull(alias);
}
}
}
}
use of libcore.java.security.TestKeyStore in project robovm by robovm.
the class PKIXParametersTest method testKeyStoreConstructor.
public void testKeyStoreConstructor() throws Exception {
TestKeyStore server = TestKeyStore.getServer();
KeyStore.PrivateKeyEntry pke = server.getPrivateKey("RSA", "RSA");
char[] password = "password".toCharArray();
// contains CA and server certificates
assertEquals(2, new PKIXParameters(server.keyStore).getTrustAnchors().size());
// just copy server certificates
KeyStore ks = TestKeyStore.createKeyStore();
ks.setKeyEntry("key", pke.getPrivateKey(), password, pke.getCertificateChain());
ks.setCertificateEntry("cert", pke.getCertificateChain()[0]);
assertEquals(1, new PKIXParameters(ks).getTrustAnchors().size());
// should fail with just key, even though cert is present in key entry
try {
KeyStore keyOnly = TestKeyStore.createKeyStore();
keyOnly.setKeyEntry("key", pke.getPrivateKey(), password, pke.getCertificateChain());
new PKIXParameters(keyOnly);
fail();
} catch (InvalidAlgorithmParameterException expected) {
}
// should fail with empty KeyStore
try {
new PKIXParameters(TestKeyStore.createKeyStore());
fail();
} catch (InvalidAlgorithmParameterException expected) {
}
}
use of libcore.java.security.TestKeyStore 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 libcore.java.security.TestKeyStore in project robovm by robovm.
the class SSLSocketTest method test_SSLSocket_getSupportedCipherSuites_connect.
public void test_SSLSocket_getSupportedCipherSuites_connect() throws Exception {
// note the rare usage of non-RSA keys
TestKeyStore testKeyStore = new TestKeyStore.Builder().keyAlgorithms("RSA", "DSA", "EC", "EC_RSA").aliasPrefix("rsa-dsa-ec").ca(true).build();
StringBuilder error = new StringBuilder();
if (StandardNames.IS_RI) {
test_SSLSocket_getSupportedCipherSuites_connect(testKeyStore, StandardNames.JSSE_PROVIDER_NAME, StandardNames.JSSE_PROVIDER_NAME, true, true, error);
} else {
test_SSLSocket_getSupportedCipherSuites_connect(testKeyStore, "HarmonyJSSE", "HarmonyJSSE", false, false, error);
test_SSLSocket_getSupportedCipherSuites_connect(testKeyStore, "AndroidOpenSSL", "AndroidOpenSSL", true, true, error);
test_SSLSocket_getSupportedCipherSuites_connect(testKeyStore, "HarmonyJSSE", "AndroidOpenSSL", false, true, error);
test_SSLSocket_getSupportedCipherSuites_connect(testKeyStore, "AndroidOpenSSL", "HarmonyJSSE", true, false, error);
}
if (error.length() > 0) {
throw new Exception("One or more problems in " + "test_SSLSocket_getSupportedCipherSuites_connect:\n" + error);
}
}
use of libcore.java.security.TestKeyStore in project robovm by robovm.
the class TrustManagerFactoryTest method test_TrustManagerFactory_extendedKeyUsage.
private void test_TrustManagerFactory_extendedKeyUsage(KeyPurposeId keyPurposeId, boolean critical, boolean client, boolean server) throws Exception {
String algorithm = "RSA";
TestKeyStore intermediateCa = TestKeyStore.getIntermediateCa();
TestKeyStore leaf = new TestKeyStore.Builder().keyAlgorithms(new String[] { algorithm }).aliasPrefix("criticalCodeSigning").signer(intermediateCa.getPrivateKey("RSA", "RSA")).rootCa(intermediateCa.getRootCertificate("RSA")).addExtendedKeyUsage(keyPurposeId, critical).build();
// leaf.dump("test_TrustManagerFactory_criticalCodeSigning");
PrivateKeyEntry privateKeyEntry = leaf.getPrivateKey(algorithm, algorithm);
X509Certificate[] chain = (X509Certificate[]) privateKeyEntry.getCertificateChain();
TestKeyStore rootCa = TestKeyStore.getRootCa();
X509TrustManager trustManager = (X509TrustManager) rootCa.trustManagers[0];
try {
trustManager.checkClientTrusted(chain, algorithm);
assertTrue(client);
} catch (Exception e) {
assertFalse(client);
}
try {
trustManager.checkServerTrusted(chain, algorithm);
assertTrue(server);
} catch (Exception e) {
assertFalse(server);
}
}
Aggregations