use of java.security.UnrecoverableKeyException in project robovm by robovm.
the class KeyStoreTest method test_KeyStore_getEntry.
public void test_KeyStore_getEntry() throws Exception {
for (KeyStore keyStore : keyStores()) {
try {
keyStore.getEntry(null, null);
fail(keyStore.getType());
} catch (NullPointerException expected) {
}
}
for (KeyStore keyStore : keyStores()) {
populate(keyStore);
// test odd inputs
try {
keyStore.getEntry(null, null);
fail(keyStore.getType());
} catch (NullPointerException expected) {
}
try {
keyStore.getEntry(null, PARAM_KEY);
fail(keyStore.getType());
} catch (NullPointerException expected) {
}
assertNull(keyStore.getEntry("", null));
assertNull(keyStore.getEntry("", PARAM_KEY));
// test case sensitive
if (isReadOnly(keyStore)) {
assertNull(keyStore.getEntry(ALIAS_PRIVATE, PARAM_KEY));
} else {
if (isKeyPasswordSupported(keyStore)) {
assertPrivateKey(keyStore.getEntry(ALIAS_PRIVATE, PARAM_KEY));
} else if (isNullPasswordAllowed(keyStore)) {
assertPrivateKey(keyStore.getEntry(ALIAS_NO_PASSWORD_PRIVATE, null));
}
if (isSecretKeyEnabled(keyStore)) {
assertSecretKey(keyStore.getEntry(ALIAS_SECRET, PARAM_KEY));
} else {
assertNull(keyStore.getEntry(ALIAS_SECRET, PARAM_KEY));
}
if (isCertificateEnabled(keyStore)) {
assertCertificate(keyStore.getEntry(ALIAS_CERTIFICATE, null));
} else {
assertNull(keyStore.getEntry(ALIAS_CERTIFICATE, null));
}
}
// test case insensitive
if (isCaseSensitive(keyStore) || isReadOnly(keyStore)) {
assertNull(keyStore.getEntry(ALIAS_ALT_CASE_PRIVATE, PARAM_KEY));
assertNull(keyStore.getEntry(ALIAS_ALT_CASE_SECRET, PARAM_KEY));
} else {
assertPrivateKey(keyStore.getEntry(ALIAS_ALT_CASE_PRIVATE, PARAM_KEY));
if (isSecretKeyEnabled(keyStore)) {
assertSecretKey(keyStore.getEntry(ALIAS_ALT_CASE_SECRET, PARAM_KEY));
}
}
if (isCaseSensitive(keyStore) || isReadOnly(keyStore)) {
assertNull(keyStore.getEntry(ALIAS_ALT_CASE_CERTIFICATE, null));
} else {
if (isCertificateEnabled(keyStore)) {
assertCertificate(keyStore.getEntry(ALIAS_ALT_CASE_CERTIFICATE, null));
}
}
// test with null passwords
if (isReadOnly(keyStore)) {
assertNull(keyStore.getEntry(ALIAS_NO_PASSWORD_PRIVATE, null));
} else if (isNullPasswordAllowed(keyStore)) {
assertPrivateKey(keyStore.getEntry(ALIAS_NO_PASSWORD_PRIVATE, null));
} else if (isKeyPasswordSupported(keyStore) && isKeyPasswordIgnored(keyStore)) {
assertPrivateKey(keyStore.getEntry(ALIAS_PRIVATE, null));
} else if (isKeyPasswordIgnored(keyStore)) {
try {
keyStore.getEntry(ALIAS_PRIVATE, null);
fail(keyStore.getType());
} catch (Exception e) {
if (e.getClass() != UnrecoverableKeyException.class && e.getClass() != IllegalArgumentException.class) {
throw e;
}
}
}
if (isReadOnly(keyStore)) {
assertNull(keyStore.getEntry(ALIAS_SECRET, null));
} else if (isSecretKeyEnabled(keyStore)) {
try {
keyStore.getEntry(ALIAS_SECRET, null);
fail(keyStore.getType());
} catch (Exception e) {
if (e.getClass() != UnrecoverableKeyException.class && e.getClass() != IllegalArgumentException.class) {
throw e;
}
}
}
// test with bad passwords
if (isReadOnly(keyStore)) {
assertNull(keyStore.getEntry(ALIAS_PRIVATE, PARAM_BAD));
} else if (isKeyPasswordSupported(keyStore) && isKeyPasswordIgnored(keyStore)) {
assertPrivateKey(keyStore.getEntry(ALIAS_PRIVATE, PARAM_BAD));
} else if (isKeyPasswordSupported(keyStore)) {
try {
keyStore.getEntry(ALIAS_PRIVATE, PARAM_BAD);
fail(keyStore.getType());
} catch (UnrecoverableKeyException expected) {
}
}
if (isReadOnly(keyStore)) {
assertNull(keyStore.getEntry(ALIAS_SECRET, PARAM_BAD));
} else if (isSecretKeyEnabled(keyStore)) {
try {
keyStore.getEntry(ALIAS_SECRET, PARAM_BAD);
fail(keyStore.getType());
} catch (UnrecoverableKeyException expected) {
}
}
}
}
use of java.security.UnrecoverableKeyException in project robovm by robovm.
the class SSLParametersImpl method createDefaultKeyManager.
private static X509KeyManager createDefaultKeyManager() throws KeyManagementException {
try {
String algorithm = KeyManagerFactory.getDefaultAlgorithm();
KeyManagerFactory kmf = KeyManagerFactory.getInstance(algorithm);
kmf.init(null, null);
KeyManager[] kms = kmf.getKeyManagers();
return findX509KeyManager(kms);
} catch (NoSuchAlgorithmException e) {
throw new KeyManagementException(e);
} catch (KeyStoreException e) {
throw new KeyManagementException(e);
} catch (UnrecoverableKeyException e) {
throw new KeyManagementException(e);
}
}
use of java.security.UnrecoverableKeyException in project robovm by robovm.
the class KeyStore4Test method testGetKey.
public void testGetKey() {
try {
Key key = keyStore.getKey("keyalias", null);
assertNotNull(key);
} catch (KeyStoreException e) {
fail("unexpected exception: " + e);
} catch (NoSuchAlgorithmException e) {
fail("unexpected exception: " + e);
} catch (UnrecoverableKeyException e) {
fail("unexpected exception: " + e);
}
try {
keyStore.getKey("certalias", null);
fail("expected NoSuchAlgorithmException");
} catch (KeyStoreException e) {
fail("unexpected exception: " + e);
} catch (NoSuchAlgorithmException e) {
// ok
} catch (UnrecoverableKeyException e) {
fail("unexpected exception: " + e);
}
try {
uninitialized.getKey("keyalias", null);
fail("expected KeyStoreException");
} catch (KeyStoreException e) {
// ok
} catch (NoSuchAlgorithmException e) {
fail("unexpected exception: " + e);
} catch (UnrecoverableKeyException e) {
fail("unexpected exception: " + e);
}
try {
keyStore.getKey("unknownalias", null);
fail("expected NoSuchAlgorithmException");
} catch (KeyStoreException e) {
fail("unexpected exception: " + e);
} catch (NoSuchAlgorithmException e) {
// ok
} catch (UnrecoverableKeyException e) {
fail("unexpected exception: " + e);
}
try {
keyStore.getKey("unknownalias", "PASSWORD".toCharArray());
fail("expected UnrecoverableKeyException");
} catch (UnrecoverableKeyException e) {
// ok
} catch (KeyStoreException e) {
fail("unexpected exception: " + e);
} catch (NoSuchAlgorithmException e) {
fail("unexpected exception: " + e);
}
}
use of java.security.UnrecoverableKeyException in project robovm by robovm.
the class KeyManagerFactorySpiTest method test_engineInit_01.
/**
* javax.net.ssl.KeyManagerFactorySpi#KengineInit(KeyStore ks, char[] password)
*/
public void test_engineInit_01() {
KeyManagerFactorySpiImpl kmf = new KeyManagerFactorySpiImpl();
KeyStore ks;
char[] psw = "password".toCharArray();
try {
kmf.engineInit(null, null);
fail("NoSuchAlgorithmException wasn't thrown");
} catch (NoSuchAlgorithmException kse) {
//expected
} catch (Exception e) {
fail(e + " was thrown instead of NoSuchAlgorithmException");
}
try {
kmf.engineInit(null, psw);
fail("KeyStoreException wasn't thrown");
} catch (KeyStoreException uke) {
//expected
} catch (Exception e) {
fail(e + " was thrown instead of KeyStoreException");
}
try {
ks = KeyStore.getInstance(KeyStore.getDefaultType());
kmf.engineInit(ks, null);
fail("UnrecoverableKeyException wasn't thrown");
} catch (UnrecoverableKeyException uke) {
//expected
} catch (Exception e) {
fail(e + " was thrown instead of UnrecoverableKeyException");
}
try {
KeyStore kst = KeyStore.getInstance(KeyStore.getDefaultType());
kst.load(null, null);
kmf.engineInit(kst, psw);
} catch (Exception e) {
fail("Unexpected exception " + e);
}
}
use of java.security.UnrecoverableKeyException in project robovm by robovm.
the class MySslContext method test_getServerSocketFactory.
/**
* Test for <code>getServerSocketFactory()</code>
* <code>getSocketFactory()</code>
* <code>init(KeyManager[] km, TrustManager[] tm, SecureRandom random)</code>
* methods Assertion: returns correspondent object
*
*/
public void test_getServerSocketFactory() throws NoSuchAlgorithmException, KeyManagementException, KeyStoreException, UnrecoverableKeyException {
if (!DEFSupported) {
fail(NotSupportMsg);
return;
}
SSLContext[] sslC = createSSLCon();
assertNotNull("SSLContext objects were not created", sslC);
String tAlg = TrustManagerFactory.getDefaultAlgorithm();
String kAlg = KeyManagerFactory.getDefaultAlgorithm();
if (tAlg == null) {
fail("TrustManagerFactory default algorithm is not defined");
return;
}
if (kAlg == null) {
fail("KeyManagerFactory default algorithm is not defined");
return;
}
KeyManagerFactory kmf = KeyManagerFactory.getInstance(kAlg);
KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());
try {
ks.load(null, null);
} catch (Exception e) {
fail(e + " was thrown for method load(null, null)");
}
kmf.init(ks, new char[10]);
KeyManager[] kms = kmf.getKeyManagers();
TrustManagerFactory tmf = TrustManagerFactory.getInstance(tAlg);
tmf.init(ks);
TrustManager[] tms = tmf.getTrustManagers();
for (int i = 0; i < sslC.length; i++) {
sslC[i].init(kms, tms, new SecureRandom());
assertNotNull("No SSLServerSocketFactory available", sslC[i].getServerSocketFactory());
assertNotNull("No SSLSocketFactory available", sslC[i].getSocketFactory());
}
}
Aggregations