| Alex Klyubin | d23a1f7 | 2015-03-27 14:39:28 -0700 | [diff] [blame] | 1 | package android.security; |
| 2 | |
| Alex Klyubin | acc835f | 2015-03-31 15:26:56 -0700 | [diff] [blame^] | 3 | import android.security.keymaster.KeyCharacteristics; |
| Alex Klyubin | d23a1f7 | 2015-03-27 14:39:28 -0700 | [diff] [blame] | 4 | import android.security.keymaster.KeymasterDefs; |
| 5 | |
| Alex Klyubin | acc835f | 2015-03-31 15:26:56 -0700 | [diff] [blame^] | 6 | import java.util.ArrayList; |
| 7 | import java.util.Date; |
| 8 | import java.util.List; |
| 9 | |
| Alex Klyubin | d23a1f7 | 2015-03-27 14:39:28 -0700 | [diff] [blame] | 10 | /** |
| 11 | * @hide |
| 12 | */ |
| 13 | public abstract class KeymasterUtils { |
| 14 | private KeymasterUtils() {} |
| 15 | |
| 16 | public static KeymasterException getExceptionForKeymasterError(int keymasterErrorCode) { |
| 17 | switch (keymasterErrorCode) { |
| 18 | case KeymasterDefs.KM_ERROR_INVALID_AUTHORIZATION_TIMEOUT: |
| 19 | // The name of this parameter significantly differs between Keymaster and framework |
| 20 | // APIs. Use the framework wording to make life easier for developers. |
| Alex Klyubin | 4ab8ea4 | 2015-03-27 16:53:44 -0700 | [diff] [blame] | 21 | return new KeymasterException(keymasterErrorCode, |
| 22 | "Invalid user authentication validity duration"); |
| Alex Klyubin | d23a1f7 | 2015-03-27 14:39:28 -0700 | [diff] [blame] | 23 | default: |
| Alex Klyubin | 4ab8ea4 | 2015-03-27 16:53:44 -0700 | [diff] [blame] | 24 | return new KeymasterException(keymasterErrorCode, |
| 25 | KeymasterDefs.getErrorMessage(keymasterErrorCode)); |
| Alex Klyubin | d23a1f7 | 2015-03-27 14:39:28 -0700 | [diff] [blame] | 26 | } |
| 27 | } |
| Alex Klyubin | acc835f | 2015-03-31 15:26:56 -0700 | [diff] [blame^] | 28 | |
| 29 | public static Integer getInt(KeyCharacteristics keyCharacteristics, int tag) { |
| 30 | if (keyCharacteristics.hwEnforced.containsTag(tag)) { |
| 31 | return keyCharacteristics.hwEnforced.getInt(tag, -1); |
| 32 | } else if (keyCharacteristics.swEnforced.containsTag(tag)) { |
| 33 | return keyCharacteristics.swEnforced.getInt(tag, -1); |
| 34 | } else { |
| 35 | return null; |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | public static List<Integer> getInts(KeyCharacteristics keyCharacteristics, int tag) { |
| 40 | List<Integer> result = new ArrayList<Integer>(); |
| 41 | result.addAll(keyCharacteristics.hwEnforced.getInts(tag)); |
| 42 | result.addAll(keyCharacteristics.swEnforced.getInts(tag)); |
| 43 | return result; |
| 44 | } |
| 45 | |
| 46 | public static Date getDate(KeyCharacteristics keyCharacteristics, int tag) { |
| 47 | Date result = keyCharacteristics.hwEnforced.getDate(tag, null); |
| 48 | if (result == null) { |
| 49 | result = keyCharacteristics.swEnforced.getDate(tag, null); |
| 50 | } |
| 51 | return result; |
| 52 | } |
| 53 | |
| 54 | public static boolean getBoolean(KeyCharacteristics keyCharacteristics, int tag) { |
| 55 | if (keyCharacteristics.hwEnforced.containsTag(tag)) { |
| 56 | return keyCharacteristics.hwEnforced.getBoolean(tag, false); |
| 57 | } else { |
| 58 | return keyCharacteristics.swEnforced.getBoolean(tag, false); |
| 59 | } |
| 60 | } |
| Alex Klyubin | d23a1f7 | 2015-03-27 14:39:28 -0700 | [diff] [blame] | 61 | } |