use of java.security.KeyStore in project robovm by robovm.
the class KeyStoreTest method test_KeyStore_deleteEntry.
public void test_KeyStore_deleteEntry() throws Exception {
for (KeyStore keyStore : keyStores()) {
try {
keyStore.deleteEntry(null);
fail(keyStore.getType());
} catch (KeyStoreException expected) {
}
}
for (KeyStore keyStore : keyStores()) {
keyStore.load(null, null);
if (isReadOnly(keyStore)) {
try {
keyStore.deleteEntry(null);
fail(keyStore.getType());
} catch (UnsupportedOperationException expected) {
}
continue;
}
// test odd inputs
try {
keyStore.deleteEntry(null);
fail(keyStore.getType());
} catch (Exception e) {
if (e.getClass() != NullPointerException.class && e.getClass() != KeyStoreException.class) {
throw e;
}
}
keyStore.deleteEntry("");
}
for (KeyStore keyStore : keyStores()) {
populate(keyStore);
if (isReadOnly(keyStore)) {
try {
keyStore.deleteEntry(ALIAS_PRIVATE);
} catch (UnsupportedOperationException e) {
}
continue;
}
// test case sensitive
if (isKeyPasswordSupported(keyStore)) {
assertPrivateKey(keyStore.getKey(ALIAS_PRIVATE, PASSWORD_KEY));
assertCertificateChain(keyStore.getCertificateChain(ALIAS_PRIVATE));
keyStore.deleteEntry(ALIAS_PRIVATE);
assertNull(keyStore.getKey(ALIAS_PRIVATE, PASSWORD_KEY));
}
if (isNullPasswordAllowed(keyStore)) {
assertPrivateKey(keyStore.getKey(ALIAS_NO_PASSWORD_PRIVATE, null));
assertCertificateChain(keyStore.getCertificateChain(ALIAS_NO_PASSWORD_PRIVATE));
keyStore.deleteEntry(ALIAS_NO_PASSWORD_PRIVATE);
assertNull(keyStore.getKey(ALIAS_NO_PASSWORD_PRIVATE, null));
}
if (isSecretKeyEnabled(keyStore)) {
assertSecretKey(keyStore.getKey(ALIAS_SECRET, PASSWORD_KEY));
keyStore.deleteEntry(ALIAS_SECRET);
assertNull(keyStore.getKey(ALIAS_SECRET, PASSWORD_KEY));
} else {
keyStore.deleteEntry(ALIAS_SECRET);
}
if (isCertificateEnabled(keyStore)) {
assertCertificate(keyStore.getCertificate(ALIAS_CERTIFICATE));
keyStore.deleteEntry(ALIAS_CERTIFICATE);
assertNull(keyStore.getCertificate(ALIAS_CERTIFICATE));
} else {
keyStore.deleteEntry(ALIAS_CERTIFICATE);
}
}
for (KeyStore keyStore : keyStores()) {
populate(keyStore);
if (isCaseSensitive(keyStore)) {
if (isKeyPasswordSupported(keyStore)) {
assertPrivateKey(keyStore.getKey(ALIAS_PRIVATE, PASSWORD_KEY));
keyStore.deleteEntry(ALIAS_ALT_CASE_PRIVATE);
assertPrivateKey(keyStore.getKey(ALIAS_PRIVATE, PASSWORD_KEY));
}
if (isNullPasswordAllowed(keyStore)) {
assertPrivateKey(keyStore.getKey(ALIAS_NO_PASSWORD_PRIVATE, null));
keyStore.deleteEntry(ALIAS_ALT_CASE_NO_PASSWORD_PRIVATE);
assertPrivateKey(keyStore.getKey(ALIAS_NO_PASSWORD_PRIVATE, null));
}
if (isSecretKeyEnabled(keyStore)) {
assertSecretKey(keyStore.getKey(ALIAS_SECRET, PASSWORD_KEY));
keyStore.deleteEntry(ALIAS_ALT_CASE_SECRET);
assertSecretKey(keyStore.getKey(ALIAS_SECRET, PASSWORD_KEY));
} else {
keyStore.deleteEntry(ALIAS_SECRET);
}
if (isCertificateEnabled(keyStore)) {
assertCertificate(keyStore.getCertificate(ALIAS_CERTIFICATE));
keyStore.deleteEntry(ALIAS_ALT_CASE_CERTIFICATE);
assertCertificate(keyStore.getCertificate(ALIAS_CERTIFICATE));
} else {
keyStore.deleteEntry(ALIAS_CERTIFICATE);
}
}
}
}
use of java.security.KeyStore in project robovm by robovm.
the class KeyStoreTest method test_KeyStore_isCertificateEntry.
public void test_KeyStore_isCertificateEntry() throws Exception {
for (KeyStore keyStore : keyStores()) {
try {
keyStore.isCertificateEntry(null);
fail(keyStore.getType());
} catch (KeyStoreException expected) {
}
}
for (KeyStore keyStore : keyStores()) {
keyStore.load(null, null);
if (isCertificateEnabled(keyStore)) {
try {
keyStore.isCertificateEntry(null);
fail(keyStore.getType());
} catch (NullPointerException expected) {
}
} else {
assertFalse(keyStore.isCertificateEntry(null));
}
assertFalse(keyStore.isCertificateEntry(""));
}
for (KeyStore keyStore : keyStores()) {
populate(keyStore);
assertFalse(keyStore.isCertificateEntry(""));
if (isKeyPasswordSupported(keyStore)) {
assertFalse(keyStore.isCertificateEntry(ALIAS_PRIVATE));
}
if (isNullPasswordAllowed(keyStore)) {
assertFalse(keyStore.isCertificateEntry(ALIAS_NO_PASSWORD_PRIVATE));
}
assertFalse(keyStore.isCertificateEntry(ALIAS_SECRET));
assertEquals(isCertificateEnabled(keyStore) && !isReadOnly(keyStore), keyStore.isCertificateEntry(ALIAS_CERTIFICATE));
assertFalse(keyStore.isCertificateEntry(ALIAS_ALT_CASE_PRIVATE));
assertFalse(keyStore.isCertificateEntry(ALIAS_ALT_CASE_SECRET));
assertEquals(!isCaseSensitive(keyStore) && isCertificateEnabled(keyStore) && !isReadOnly(keyStore), keyStore.isCertificateEntry(ALIAS_ALT_CASE_CERTIFICATE));
}
}
use of java.security.KeyStore in project robovm by robovm.
the class KeyStoreTest method keyStores.
public static List<KeyStore> keyStores() throws Exception {
List<KeyStore> keyStores = new ArrayList<KeyStore>();
Provider[] providers = Security.getProviders();
for (Provider provider : providers) {
Set<Provider.Service> services = provider.getServices();
for (Provider.Service service : services) {
String type = service.getType();
if (!type.equals("KeyStore")) {
continue;
}
String algorithm = service.getAlgorithm();
KeyStore ks = KeyStore.getInstance(algorithm, provider);
assertEquals(provider, ks.getProvider());
assertEquals(algorithm, ks.getType());
if (!isUnsupported(ks)) {
keyStores.add(ks);
}
}
}
return keyStores;
}
use of java.security.KeyStore in project robovm by robovm.
the class KeyStoreTest method test_KeyStore_getDefaultType.
public void test_KeyStore_getDefaultType() throws Exception {
String type = KeyStore.getDefaultType();
assertNotNull(type);
KeyStore ks = KeyStore.getInstance(type);
assertNotNull(ks);
assertEquals(type, ks.getType());
}
use of java.security.KeyStore 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) {
}
}
Aggregations