Search in sources :

Example 11 with SecretKeyEntry

use of java.security.KeyStore.SecretKeyEntry in project robovm by robovm.

the class KeyStoreTest method test_KeyStore_setEntry.

public void test_KeyStore_setEntry() throws Exception {
    for (KeyStore keyStore : keyStores()) {
        keyStore.load(null, null);
        try {
            keyStore.setEntry(null, null, null);
            fail(keyStore.getType());
        } catch (NullPointerException expected) {
        }
    }
    for (KeyStore keyStore : keyStores()) {
        keyStore.load(null, null);
        try {
            keyStore.setEntry(ALIAS_PRIVATE, getPrivateKey(), new FakeProtectionParameter());
            fail("Should not accept unknown ProtectionParameter: " + keyStore.getProvider());
        } catch (KeyStoreException expected) {
        }
    }
    for (KeyStore keyStore : keyStores()) {
        keyStore.load(null, null);
        // test odd inputs
        try {
            keyStore.setEntry(null, null, null);
            fail(keyStore.getType());
        } catch (Exception e) {
            if (e.getClass() != NullPointerException.class && e.getClass() != KeyStoreException.class) {
                throw e;
            }
        }
        try {
            keyStore.setEntry(null, null, PARAM_KEY);
            fail(keyStore.getType());
        } catch (Exception e) {
            if (e.getClass() != NullPointerException.class && e.getClass() != KeyStoreException.class) {
                throw e;
            }
        }
        try {
            keyStore.setEntry("", null, PARAM_KEY);
            fail(keyStore.getType());
        } catch (NullPointerException expected) {
        }
    }
    for (KeyStore keyStore : keyStores()) {
        clearKeyStore(keyStore);
        // test case sensitive
        assertNull(keyStore.getKey(ALIAS_PRIVATE, PASSWORD_KEY));
        if (isReadOnly(keyStore)) {
            try {
                keyStore.setEntry(ALIAS_PRIVATE, getPrivateKey(), PARAM_KEY);
                fail(keyStore.getType());
            } catch (UnsupportedOperationException expected) {
            }
            continue;
        }
        if (isKeyPasswordSupported(keyStore)) {
            keyStore.setEntry(ALIAS_PRIVATE, getPrivateKey(), PARAM_KEY);
            assertPrivateKey(keyStore.getKey(ALIAS_PRIVATE, PASSWORD_KEY));
            assertCertificateChain(keyStore.getCertificateChain(ALIAS_PRIVATE));
        }
        if (isNullPasswordAllowed(keyStore)) {
            keyStore.setEntry(ALIAS_NO_PASSWORD_PRIVATE, getPrivateKey(), null);
            assertPrivateKey(keyStore.getKey(ALIAS_NO_PASSWORD_PRIVATE, null));
            assertCertificateChain(keyStore.getCertificateChain(ALIAS_NO_PASSWORD_PRIVATE));
        }
        if (isSecretKeyEnabled(keyStore)) {
            assertNull(keyStore.getKey(ALIAS_SECRET, PASSWORD_KEY));
            keyStore.setEntry(ALIAS_SECRET, new SecretKeyEntry(getSecretKey()), PARAM_KEY);
            assertSecretKey(keyStore.getKey(ALIAS_SECRET, PASSWORD_KEY));
        } else {
            try {
                keyStore.setKeyEntry(ALIAS_SECRET, getSecretKey(), PASSWORD_KEY, null);
                fail(keyStore.getType());
            } catch (KeyStoreException expected) {
            }
        }
        if (isCertificateEnabled(keyStore)) {
            assertNull(keyStore.getCertificate(ALIAS_CERTIFICATE));
            keyStore.setEntry(ALIAS_CERTIFICATE, new TrustedCertificateEntry(getPrivateKey().getCertificate()), null);
            assertCertificate(keyStore.getCertificate(ALIAS_CERTIFICATE));
        } else {
            try {
                keyStore.setEntry(ALIAS_CERTIFICATE, new TrustedCertificateEntry(getPrivateKey().getCertificate()), null);
                fail(keyStore.getType());
            } catch (KeyStoreException expected) {
            }
        }
        if (isKeyPasswordSupported(keyStore)) {
            keyStore.setEntry(ALIAS_UNICODE_PRIVATE, getPrivateKey(), PARAM_KEY);
            assertPrivateKey(keyStore.getKey(ALIAS_UNICODE_PRIVATE, PASSWORD_KEY));
            assertCertificateChain(keyStore.getCertificateChain(ALIAS_UNICODE_PRIVATE));
        }
        if (isNullPasswordAllowed(keyStore)) {
            keyStore.setEntry(ALIAS_UNICODE_NO_PASSWORD_PRIVATE, getPrivateKey(), null);
            assertPrivateKey(keyStore.getKey(ALIAS_UNICODE_NO_PASSWORD_PRIVATE, null));
            assertCertificateChain(keyStore.getCertificateChain(ALIAS_UNICODE_NO_PASSWORD_PRIVATE));
        }
        if (isSecretKeyEnabled(keyStore)) {
            assertNull(keyStore.getKey(ALIAS_UNICODE_SECRET, PASSWORD_KEY));
            keyStore.setEntry(ALIAS_UNICODE_SECRET, new SecretKeyEntry(getSecretKey()), PARAM_KEY);
            assertSecretKey(keyStore.getKey(ALIAS_UNICODE_SECRET, PASSWORD_KEY));
        } else {
            try {
                keyStore.setKeyEntry(ALIAS_UNICODE_SECRET, getSecretKey(), PASSWORD_KEY, null);
                fail(keyStore.getType());
            } catch (KeyStoreException expected) {
            }
        }
    }
    for (KeyStore keyStore : keyStores()) {
        populate(keyStore);
        if (isReadOnly(keyStore)) {
            assertNull(keyStore.getKey(ALIAS_PRIVATE, PASSWORD_KEY));
            assertNull(keyStore.getKey(ALIAS_ALT_CASE_PRIVATE, PASSWORD_KEY));
            assertNull(keyStore.getKey(ALIAS_SECRET, PASSWORD_KEY));
            assertNull(keyStore.getKey(ALIAS_ALT_CASE_SECRET, PASSWORD_KEY));
        } else if (isCaseSensitive(keyStore)) {
            if (isKeyPasswordSupported(keyStore)) {
                assertPrivateKey(keyStore.getKey(ALIAS_PRIVATE, PASSWORD_KEY));
                assertNull(keyStore.getKey(ALIAS_ALT_CASE_PRIVATE, PASSWORD_KEY));
                keyStore.setEntry(ALIAS_ALT_CASE_PRIVATE, getPrivateKey2(), PARAM_KEY);
                assertPrivateKey(keyStore.getKey(ALIAS_PRIVATE, PASSWORD_KEY));
                assertPrivateKey2(keyStore.getKey(ALIAS_ALT_CASE_PRIVATE, PASSWORD_KEY));
            }
            if (isNullPasswordAllowed(keyStore)) {
                assertPrivateKey(keyStore.getKey(ALIAS_NO_PASSWORD_PRIVATE, null));
                assertNull(keyStore.getKey(ALIAS_ALT_CASE_NO_PASSWORD_PRIVATE, null));
                keyStore.setEntry(ALIAS_ALT_CASE_NO_PASSWORD_PRIVATE, getPrivateKey2(), null);
                assertPrivateKey(keyStore.getKey(ALIAS_NO_PASSWORD_PRIVATE, null));
                assertPrivateKey2(keyStore.getKey(ALIAS_ALT_CASE_NO_PASSWORD_PRIVATE, null));
            }
            if (isSecretKeyEnabled(keyStore)) {
                assertSecretKey(keyStore.getKey(ALIAS_SECRET, PASSWORD_KEY));
                assertNull(keyStore.getKey(ALIAS_ALT_CASE_SECRET, PASSWORD_KEY));
                keyStore.setEntry(ALIAS_ALT_CASE_SECRET, new SecretKeyEntry(getSecretKey2()), PARAM_KEY);
                assertSecretKey(keyStore.getKey(ALIAS_SECRET, PASSWORD_KEY));
                assertSecretKey2(keyStore.getKey(ALIAS_ALT_CASE_SECRET, PASSWORD_KEY));
            }
            if (isCertificateEnabled(keyStore)) {
                assertCertificate(keyStore.getCertificate(ALIAS_CERTIFICATE));
                assertNull(keyStore.getCertificate(ALIAS_ALT_CASE_CERTIFICATE));
                keyStore.setEntry(ALIAS_ALT_CASE_CERTIFICATE, new TrustedCertificateEntry(getPrivateKey2().getCertificate()), null);
                assertCertificate(keyStore.getCertificate(ALIAS_CERTIFICATE));
                assertCertificate2(keyStore.getCertificate(ALIAS_ALT_CASE_CERTIFICATE));
                keyStore.setEntry(ALIAS_UNICODE_CERTIFICATE, new TrustedCertificateEntry(getPrivateKey().getCertificate()), null);
                assertCertificate(keyStore.getCertificate(ALIAS_UNICODE_CERTIFICATE));
            }
        } else {
            assertPrivateKey(keyStore.getKey(ALIAS_PRIVATE, PASSWORD_KEY));
            assertPrivateKey(keyStore.getKey(ALIAS_ALT_CASE_PRIVATE, PASSWORD_KEY));
            keyStore.setEntry(ALIAS_ALT_CASE_PRIVATE, getPrivateKey2(), PARAM_KEY);
            assertPrivateKey2(keyStore.getKey(ALIAS_PRIVATE, PASSWORD_KEY));
            assertPrivateKey2(keyStore.getKey(ALIAS_ALT_CASE_PRIVATE, PASSWORD_KEY));
            if (isSecretKeyEnabled(keyStore)) {
                assertSecretKey(keyStore.getKey(ALIAS_SECRET, PASSWORD_KEY));
                assertSecretKey(keyStore.getKey(ALIAS_ALT_CASE_SECRET, PASSWORD_KEY));
                keyStore.setEntry(ALIAS_ALT_CASE_SECRET, new SecretKeyEntry(getSecretKey2()), PARAM_KEY);
                assertSecretKey2(keyStore.getKey(ALIAS_SECRET, PASSWORD_KEY));
                assertSecretKey2(keyStore.getKey(ALIAS_ALT_CASE_SECRET, PASSWORD_KEY));
            }
            if (isCertificateEnabled(keyStore)) {
                assertCertificate(keyStore.getCertificate(ALIAS_CERTIFICATE));
                assertCertificate(keyStore.getCertificate(ALIAS_ALT_CASE_CERTIFICATE));
                keyStore.setEntry(ALIAS_ALT_CASE_CERTIFICATE, new TrustedCertificateEntry(getPrivateKey2().getCertificate()), null);
                assertCertificate2(keyStore.getCertificate(ALIAS_CERTIFICATE));
                assertCertificate2(keyStore.getCertificate(ALIAS_ALT_CASE_CERTIFICATE));
                keyStore.setEntry(ALIAS_UNICODE_CERTIFICATE, new TrustedCertificateEntry(getPrivateKey().getCertificate()), null);
                assertCertificate(keyStore.getCertificate(ALIAS_UNICODE_CERTIFICATE));
            }
        }
    }
    for (KeyStore keyStore : keyStores()) {
        keyStore.load(null, null);
        // test with null/non-null passwords
        if (isReadOnly(keyStore)) {
            try {
                keyStore.setEntry(ALIAS_PRIVATE, getPrivateKey(), null);
                fail(keyStore.getType());
            } catch (UnsupportedOperationException expected) {
            }
            try {
                keyStore.setEntry(ALIAS_SECRET, new SecretKeyEntry(getSecretKey()), null);
                fail(keyStore.getType());
            } catch (UnsupportedOperationException expected) {
            }
            try {
                keyStore.setEntry(ALIAS_CERTIFICATE, new TrustedCertificateEntry(getPrivateKey().getCertificate()), null);
                fail(keyStore.getType());
            } catch (UnsupportedOperationException expected) {
            }
            continue;
        }
        if (isNullPasswordAllowed(keyStore) || isKeyPasswordIgnored(keyStore)) {
            keyStore.setEntry(ALIAS_PRIVATE, getPrivateKey(), null);
            assertPrivateKey(keyStore.getKey(ALIAS_PRIVATE, null));
        } else {
            try {
                keyStore.setEntry(ALIAS_PRIVATE, getPrivateKey(), null);
                fail(keyStore.getType());
            } catch (Exception e) {
                if (e.getClass() != UnrecoverableKeyException.class && e.getClass() != IllegalArgumentException.class && e.getClass() != KeyStoreException.class) {
                    throw e;
                }
            }
        }
        if (isSecretKeyEnabled(keyStore)) {
            if (isNullPasswordAllowed(keyStore) || isKeyPasswordIgnored(keyStore)) {
                keyStore.setEntry(ALIAS_SECRET, new SecretKeyEntry(getSecretKey()), null);
                assertSecretKey(keyStore.getKey(ALIAS_SECRET, null));
            } else {
                try {
                    keyStore.setEntry(ALIAS_SECRET, new SecretKeyEntry(getSecretKey()), null);
                    fail(keyStore.getType());
                } catch (Exception e) {
                    if (e.getClass() != UnrecoverableKeyException.class && e.getClass() != IllegalArgumentException.class && e.getClass() != KeyStoreException.class) {
                        throw e;
                    }
                }
            }
        }
        if (isCertificateEnabled(keyStore)) {
            if (isNullPasswordAllowed(keyStore) || isKeyPasswordIgnored(keyStore)) {
                keyStore.setEntry(ALIAS_CERTIFICATE, new TrustedCertificateEntry(getPrivateKey().getCertificate()), PARAM_KEY);
                assertCertificate(keyStore.getCertificate(ALIAS_CERTIFICATE));
            } else {
                try {
                    keyStore.setEntry(ALIAS_CERTIFICATE, new TrustedCertificateEntry(getPrivateKey().getCertificate()), PARAM_KEY);
                    fail(keyStore.getType());
                } catch (KeyStoreException expected) {
                }
            }
        }
    }
}
Also used : UnrecoverableKeyException(java.security.UnrecoverableKeyException) KeyStoreException(java.security.KeyStoreException) SecretKeyEntry(java.security.KeyStore.SecretKeyEntry) KeyStore(java.security.KeyStore) KeyStoreException(java.security.KeyStoreException) UnrecoverableKeyException(java.security.UnrecoverableKeyException) IOException(java.io.IOException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) TrustedCertificateEntry(java.security.KeyStore.TrustedCertificateEntry)

Example 12 with SecretKeyEntry

use of java.security.KeyStore.SecretKeyEntry in project android_frameworks_base by crdroidandroid.

the class AndroidKeyStoreSpi method engineSetEntry.

@Override
public void engineSetEntry(String alias, Entry entry, ProtectionParameter param) throws KeyStoreException {
    if (entry == null) {
        throw new KeyStoreException("entry == null");
    }
    Credentials.deleteAllTypesForAlias(mKeyStore, alias, mUid);
    if (entry instanceof java.security.KeyStore.TrustedCertificateEntry) {
        java.security.KeyStore.TrustedCertificateEntry trE = (java.security.KeyStore.TrustedCertificateEntry) entry;
        engineSetCertificateEntry(alias, trE.getTrustedCertificate());
        return;
    }
    if (entry instanceof PrivateKeyEntry) {
        PrivateKeyEntry prE = (PrivateKeyEntry) entry;
        setPrivateKeyEntry(alias, prE.getPrivateKey(), prE.getCertificateChain(), param);
    } else if (entry instanceof SecretKeyEntry) {
        SecretKeyEntry secE = (SecretKeyEntry) entry;
        setSecretKeyEntry(alias, secE.getSecretKey(), param);
    } else {
        throw new KeyStoreException("Entry must be a PrivateKeyEntry, SecretKeyEntry or TrustedCertificateEntry" + "; was " + entry);
    }
}
Also used : KeyStoreException(java.security.KeyStoreException) SecretKeyEntry(java.security.KeyStore.SecretKeyEntry) KeyStore(android.security.KeyStore) PrivateKeyEntry(java.security.KeyStore.PrivateKeyEntry)

Aggregations

SecretKeyEntry (java.security.KeyStore.SecretKeyEntry)12 KeyStoreException (java.security.KeyStoreException)7 SecretKey (javax.crypto.SecretKey)6 KeyStore (android.security.KeyStore)5 PrivateKeyEntry (java.security.KeyStore.PrivateKeyEntry)5 SecretKeySpec (javax.crypto.spec.SecretKeySpec)5 KeyStore (java.security.KeyStore)2 Entry (java.security.KeyStore.Entry)2 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)2 EncryptionException (com.evolveum.midpoint.prism.crypto.EncryptionException)1 Protector (com.evolveum.midpoint.prism.crypto.Protector)1 ProtectorImpl (com.evolveum.midpoint.prism.crypto.ProtectorImpl)1 IOException (java.io.IOException)1 ProtectionParameter (java.security.KeyStore.ProtectionParameter)1 TrustedCertificateEntry (java.security.KeyStore.TrustedCertificateEntry)1 Provider (java.security.Provider)1 UnrecoverableEntryException (java.security.UnrecoverableEntryException)1 UnrecoverableKeyException (java.security.UnrecoverableKeyException)1 HashMap (java.util.HashMap)1 Test (org.junit.Test)1