summaryrefslogtreecommitdiffstats
path: root/keystore/java/android/security/KeyStore.java
diff options
context:
space:
mode:
Diffstat (limited to 'keystore/java/android/security/KeyStore.java')
-rw-r--r--keystore/java/android/security/KeyStore.java70
1 files changed, 38 insertions, 32 deletions
diff --git a/keystore/java/android/security/KeyStore.java b/keystore/java/android/security/KeyStore.java
index 7183688..2a48834 100644
--- a/keystore/java/android/security/KeyStore.java
+++ b/keystore/java/android/security/KeyStore.java
@@ -32,16 +32,21 @@ import java.util.ArrayList;
* preclude the use of hardware crypto.
*/
public class KeyStore {
- public static final int NO_ERROR = 1;
- public static final int LOCKED = 2;
- public static final int UNINITIALIZED = 3;
- public static final int SYSTEM_ERROR = 4;
- public static final int PROTOCOL_ERROR = 5;
- public static final int PERMISSION_DENIED = 6;
- public static final int KEY_NOT_FOUND = 7;
- public static final int VALUE_CORRUPTED = 8;
- public static final int UNDEFINED_ACTION = 9;
- public static final int WRONG_PASSWORD = 10;
+
+ // ResponseCodes
+ private static final int NO_ERROR = 1;
+ private static final int LOCKED = 2;
+ private static final int UNINITIALIZED = 3;
+ private static final int SYSTEM_ERROR = 4;
+ private static final int PROTOCOL_ERROR = 5;
+ private static final int PERMISSION_DENIED = 6;
+ private static final int KEY_NOT_FOUND = 7;
+ private static final int VALUE_CORRUPTED = 8;
+ private static final int UNDEFINED_ACTION = 9;
+ private static final int WRONG_PASSWORD = 10;
+
+ // States
+ public enum State { UNLOCKED, LOCKED, UNINITIALIZED };
private static final LocalSocketAddress sAddress = new LocalSocketAddress(
"keystore", LocalSocketAddress.Namespace.RESERVED);
@@ -54,31 +59,35 @@ public class KeyStore {
return new KeyStore();
}
- public int test() {
+ public State state() {
execute('t');
- return mError;
+ switch (mError) {
+ case NO_ERROR: return State.UNLOCKED;
+ case LOCKED: return State.LOCKED;
+ case UNINITIALIZED: return State.UNINITIALIZED;
+ default: throw new AssertionError(mError);
+ }
}
- public byte[] get(byte[] key) {
+ private byte[] get(byte[] key) {
ArrayList<byte[]> values = execute('g', key);
return (values == null || values.isEmpty()) ? null : values.get(0);
}
- public String get(String key) {
- byte[] value = get(getBytes(key));
- return (value == null) ? null : toString(value);
+ public byte[] get(String key) {
+ return get(getBytes(key));
}
- public boolean put(byte[] key, byte[] value) {
+ private boolean put(byte[] key, byte[] value) {
execute('i', key, value);
return mError == NO_ERROR;
}
- public boolean put(String key, String value) {
- return put(getBytes(key), getBytes(value));
+ public boolean put(String key, byte[] value) {
+ return put(getBytes(key), value);
}
- public boolean delete(byte[] key) {
+ private boolean delete(byte[] key) {
execute('d', key);
return mError == NO_ERROR;
}
@@ -87,7 +96,7 @@ public class KeyStore {
return delete(getBytes(key));
}
- public boolean contains(byte[] key) {
+ private boolean contains(byte[] key) {
execute('e', key);
return mError == NO_ERROR;
}
@@ -118,19 +127,11 @@ public class KeyStore {
return mError == NO_ERROR;
}
- public boolean password(byte[] oldPassword, byte[] newPassword) {
- execute('p', oldPassword, newPassword);
+ private boolean password(byte[] password) {
+ execute('p', password);
return mError == NO_ERROR;
}
- public boolean password(String oldPassword, String newPassword) {
- return password(getBytes(oldPassword), getBytes(newPassword));
- }
-
- public boolean password(byte[] password) {
- return password(password, password);
- }
-
public boolean password(String password) {
return password(getBytes(password));
}
@@ -140,7 +141,7 @@ public class KeyStore {
return mError == NO_ERROR;
}
- public boolean unlock(byte[] password) {
+ private boolean unlock(byte[] password) {
execute('u', password);
return mError == NO_ERROR;
}
@@ -149,6 +150,11 @@ public class KeyStore {
return unlock(getBytes(password));
}
+ public boolean isEmpty() {
+ execute('z');
+ return mError == KEY_NOT_FOUND;
+ }
+
public int getLastError() {
return mError;
}