diff options
author | Kenny Root <kroot@google.com> | 2012-08-17 21:13:48 -0700 |
---|---|---|
committer | Kenny Root <kroot@google.com> | 2012-08-20 09:48:41 -0700 |
commit | 473c712b19bad992ab4eafcd43175fdce77b913d (patch) | |
tree | 53695829e6276f1ab596ec873b63e3fa9ad2c491 /keystore | |
parent | bc11e52cafa182996a338641c86bf3a07f571b1d (diff) | |
download | frameworks_base-473c712b19bad992ab4eafcd43175fdce77b913d.zip frameworks_base-473c712b19bad992ab4eafcd43175fdce77b913d.tar.gz frameworks_base-473c712b19bad992ab4eafcd43175fdce77b913d.tar.bz2 |
Add getmtime to Android KeyStore API
java.security.KeyStore requires that you be able to get the creation
date for any given entry. We'll approximate that through using the mtime
of the file in the keystore.
Change-Id: I16f74354a6c2e78a1a0b4dc2ae720c5391274e6f
Diffstat (limited to 'keystore')
-rw-r--r-- | keystore/java/android/security/KeyStore.java | 18 | ||||
-rwxr-xr-x | keystore/tests/src/android/security/KeyStoreTest.java | 50 |
2 files changed, 68 insertions, 0 deletions
diff --git a/keystore/java/android/security/KeyStore.java b/keystore/java/android/security/KeyStore.java index f49c429..4637991 100644 --- a/keystore/java/android/security/KeyStore.java +++ b/keystore/java/android/security/KeyStore.java @@ -26,6 +26,7 @@ import java.io.UTFDataFormatException; import java.nio.charset.Charsets; import java.nio.charset.ModifiedUtf8; import java.util.ArrayList; +import java.util.Date; /** * @hide This should not be made public in its present form because it @@ -228,6 +229,23 @@ public class KeyStore { return ungrant(getKeyBytes(key), getUidBytes(uid)); } + private long getmtime(byte[] key) { + final ArrayList<byte[]> values = execute('c', key); + if (values == null || values.isEmpty()) { + return -1L; + } + + return Long.parseLong(new String(values.get(0))) * 1000L; + } + + /** + * Returns the last modification time of the key in milliseconds since the + * epoch. Will return -1L if the key could not be found or other error. + */ + public long getmtime(String key) { + return getmtime(getKeyBytes(key)); + } + public int getLastError() { return mError; } diff --git a/keystore/tests/src/android/security/KeyStoreTest.java b/keystore/tests/src/android/security/KeyStoreTest.java index 9f35b8d..07a2d7b 100755 --- a/keystore/tests/src/android/security/KeyStoreTest.java +++ b/keystore/tests/src/android/security/KeyStoreTest.java @@ -19,9 +19,11 @@ package android.security; import android.app.Activity; import android.security.KeyStore; import android.test.ActivityUnitTestCase; +import android.test.AssertionFailedError; import android.test.suitebuilder.annotation.MediumTest; import java.nio.charset.Charsets; import java.util.Arrays; +import java.util.Date; import java.util.HashSet; /** @@ -403,4 +405,52 @@ public class KeyStoreTest extends ActivityUnitTestCase<Activity> { assertFalse("Should fail to ungrant key to other user second time", mKeyStore.ungrant(TEST_KEYNAME, 0)); } + + /** + * The amount of time to allow before and after expected time for variance + * in timing tests. + */ + private static final long SLOP_TIME_MILLIS = 15000L; + + public void testGetmtime_Success() throws Exception { + assertTrue("Password should work for keystore", + mKeyStore.password(TEST_PASSWD)); + + assertTrue("Should be able to import key when unlocked", + mKeyStore.importKey(TEST_KEYNAME, PRIVKEY_BYTES)); + + long now = System.currentTimeMillis(); + long actual = mKeyStore.getmtime(TEST_KEYNAME); + + long expectedAfter = now - SLOP_TIME_MILLIS; + long expectedBefore = now + SLOP_TIME_MILLIS; + + assertLessThan("Time should be close to current time", expectedBefore, actual); + assertGreaterThan("Time should be close to current time", expectedAfter, actual); + } + + private static void assertLessThan(String explanation, long expectedBefore, long actual) { + if (actual >= expectedBefore) { + throw new AssertionFailedError(explanation + ": actual=" + actual + + ", expected before: " + expectedBefore); + } + } + + private static void assertGreaterThan(String explanation, long expectedAfter, long actual) { + if (actual <= expectedAfter) { + throw new AssertionFailedError(explanation + ": actual=" + actual + + ", expected after: " + expectedAfter); + } + } + + public void testGetmtime_NonExist_Failure() throws Exception { + assertTrue("Password should work for keystore", + mKeyStore.password(TEST_PASSWD)); + + assertTrue("Should be able to import key when unlocked", + mKeyStore.importKey(TEST_KEYNAME, PRIVKEY_BYTES)); + + assertEquals("-1 should be returned for non-existent key", + -1L, mKeyStore.getmtime(TEST_KEYNAME2)); + } } |