blob: 6bb9636a911c441aae106a7ebfbc9832f7cd5328 [file] [log] [blame]
Alex Klyubincc21bb32015-03-31 16:50:37 -07001/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Alex Klyubind23a1f72015-03-27 14:39:28 -070017package android.security;
18
Alex Klyubinacc835f2015-03-31 15:26:56 -070019import android.security.keymaster.KeyCharacteristics;
Alex Klyubind23a1f72015-03-27 14:39:28 -070020import android.security.keymaster.KeymasterDefs;
21
Alex Klyubinacc835f2015-03-31 15:26:56 -070022import java.util.ArrayList;
23import java.util.Date;
24import java.util.List;
25
Alex Klyubind23a1f72015-03-27 14:39:28 -070026/**
27 * @hide
28 */
29public abstract class KeymasterUtils {
30 private KeymasterUtils() {}
31
32 public static KeymasterException getExceptionForKeymasterError(int keymasterErrorCode) {
33 switch (keymasterErrorCode) {
34 case KeymasterDefs.KM_ERROR_INVALID_AUTHORIZATION_TIMEOUT:
35 // The name of this parameter significantly differs between Keymaster and framework
36 // APIs. Use the framework wording to make life easier for developers.
Alex Klyubin4ab8ea42015-03-27 16:53:44 -070037 return new KeymasterException(keymasterErrorCode,
38 "Invalid user authentication validity duration");
Alex Klyubind23a1f72015-03-27 14:39:28 -070039 default:
Alex Klyubin4ab8ea42015-03-27 16:53:44 -070040 return new KeymasterException(keymasterErrorCode,
41 KeymasterDefs.getErrorMessage(keymasterErrorCode));
Alex Klyubind23a1f72015-03-27 14:39:28 -070042 }
43 }
Alex Klyubinacc835f2015-03-31 15:26:56 -070044
45 public static Integer getInt(KeyCharacteristics keyCharacteristics, int tag) {
46 if (keyCharacteristics.hwEnforced.containsTag(tag)) {
47 return keyCharacteristics.hwEnforced.getInt(tag, -1);
48 } else if (keyCharacteristics.swEnforced.containsTag(tag)) {
49 return keyCharacteristics.swEnforced.getInt(tag, -1);
50 } else {
51 return null;
52 }
53 }
54
55 public static List<Integer> getInts(KeyCharacteristics keyCharacteristics, int tag) {
56 List<Integer> result = new ArrayList<Integer>();
57 result.addAll(keyCharacteristics.hwEnforced.getInts(tag));
58 result.addAll(keyCharacteristics.swEnforced.getInts(tag));
59 return result;
60 }
61
62 public static Date getDate(KeyCharacteristics keyCharacteristics, int tag) {
63 Date result = keyCharacteristics.hwEnforced.getDate(tag, null);
64 if (result == null) {
65 result = keyCharacteristics.swEnforced.getDate(tag, null);
66 }
67 return result;
68 }
69
70 public static boolean getBoolean(KeyCharacteristics keyCharacteristics, int tag) {
71 if (keyCharacteristics.hwEnforced.containsTag(tag)) {
72 return keyCharacteristics.hwEnforced.getBoolean(tag, false);
73 } else {
74 return keyCharacteristics.swEnforced.getBoolean(tag, false);
75 }
76 }
Alex Klyubind23a1f72015-03-27 14:39:28 -070077}