diff options
author | Alex Klyubin <klyubin@google.com> | 2015-03-31 19:50:13 -0700 |
---|---|---|
committer | Alex Klyubin <klyubin@google.com> | 2015-03-31 19:50:13 -0700 |
commit | c8e557470fc94733c9340c4c67ee69c225bbaa70 (patch) | |
tree | 42658c56aa30ae20ec8a828745008f74587eb991 /keystore/java/android/security/KeyStoreKeyConstraints.java | |
parent | b7a34e4955beae2cee81a27e7c240316078bbf2f (diff) |
Hook in user authenticators and their exceptions.
Bug: 18088752
Change-Id: I2835dbe51d09587a3081597c6aaf536aa1427e24
Diffstat (limited to 'keystore/java/android/security/KeyStoreKeyConstraints.java')
-rw-r--r-- | keystore/java/android/security/KeyStoreKeyConstraints.java | 86 |
1 files changed, 86 insertions, 0 deletions
diff --git a/keystore/java/android/security/KeyStoreKeyConstraints.java b/keystore/java/android/security/KeyStoreKeyConstraints.java index c011083c41fa..c27ccb167054 100644 --- a/keystore/java/android/security/KeyStoreKeyConstraints.java +++ b/keystore/java/android/security/KeyStoreKeyConstraints.java @@ -23,7 +23,10 @@ import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.util.Arrays; import java.util.Collection; +import java.util.Collections; +import java.util.HashSet; import java.util.Locale; +import java.util.Set; /** * Constraints for {@code AndroidKeyStore} keys. @@ -520,4 +523,87 @@ public abstract class KeyStoreKeyConstraints { } } } + + @Retention(RetentionPolicy.SOURCE) + @IntDef({UserAuthenticator.LOCK_SCREEN}) + public @interface UserAuthenticatorEnum {} + + /** + * User authenticators which can be used to restrict/protect access to keys. + */ + public static abstract class UserAuthenticator { + private UserAuthenticator() {} + + /** Lock screen. */ + public static final int LOCK_SCREEN = 1; + + /** + * @hide + */ + public static int toKeymaster(@UserAuthenticatorEnum int userAuthenticator) { + switch (userAuthenticator) { + case LOCK_SCREEN: + return LOCK_SCREEN; + default: + throw new IllegalArgumentException( + "Unknown user authenticator: " + userAuthenticator); + } + } + + /** + * @hide + */ + public static @UserAuthenticatorEnum int fromKeymaster(int userAuthenticator) { + switch (userAuthenticator) { + case LOCK_SCREEN: + return LOCK_SCREEN; + default: + throw new IllegalArgumentException( + "Unknown user authenticator: " + userAuthenticator); + } + } + + /** + * @hide + */ + public static int allToKeymaster(Set<Integer> userAuthenticators) { + int result = 0; + for (@UserAuthenticatorEnum int userAuthenticator : userAuthenticators) { + result |= toKeymaster(userAuthenticator); + } + return result; + } + + /** + * @hide + */ + public static Set<Integer> allFromKeymaster(int userAuthenticators) { + int userAuthenticator = 1; + Set<Integer> result = null; + while (userAuthenticators != 0) { + if ((userAuthenticators & 1) != 0) { + if (result == null) { + result = new HashSet<Integer>(); + } + result.add(fromKeymaster(userAuthenticator)); + } + userAuthenticators >>>= 1; + userAuthenticator <<= 1; + } + return (result != null) ? result : Collections.<Integer>emptySet(); + } + + /** + * @hide + */ + public static String toString(@UserAuthenticatorEnum int userAuthenticator) { + switch (userAuthenticator) { + case LOCK_SCREEN: + return "LOCK_SCREEN"; + default: + throw new IllegalArgumentException( + "Unknown user authenticator: " + userAuthenticator); + } + } + } } |