blob: 82f65c72a6f01bcd69dd90759db5a86631802aaf [file] [log] [blame]
Chad Brubaker45ff13e2015-01-21 14:00:55 -08001/**
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
17package android.security.keymaster;
18
19import android.os.Parcel;
20import android.os.Parcelable;
21
22import java.util.ArrayList;
23import java.util.Date;
24import java.util.List;
25
26/**
27 * Utility class for the java side of user specified Keymaster arguments.
28 * <p>
29 * Serialization code for this and subclasses must be kept in sync with system/security/keystore
30 * @hide
31 */
32public class KeymasterArguments implements Parcelable {
33 List<KeymasterArgument> mArguments;
34
35 public static final Parcelable.Creator<KeymasterArguments> CREATOR = new
36 Parcelable.Creator<KeymasterArguments>() {
Alex Klyubin5927c9f2015-04-10 13:28:03 -070037 @Override
Chad Brubaker45ff13e2015-01-21 14:00:55 -080038 public KeymasterArguments createFromParcel(Parcel in) {
39 return new KeymasterArguments(in);
40 }
Alex Klyubin5927c9f2015-04-10 13:28:03 -070041
42 @Override
Chad Brubaker45ff13e2015-01-21 14:00:55 -080043 public KeymasterArguments[] newArray(int size) {
44 return new KeymasterArguments[size];
45 }
46 };
47
48 public KeymasterArguments() {
49 mArguments = new ArrayList<KeymasterArgument>();
50 }
51
52 private KeymasterArguments(Parcel in) {
53 mArguments = in.createTypedArrayList(KeymasterArgument.CREATOR);
54 }
55
56 public void addInt(int tag, int value) {
57 mArguments.add(new KeymasterIntArgument(tag, value));
58 }
59
Alex Klyubin5927c9f2015-04-10 13:28:03 -070060 public void addInts(int tag, int... values) {
61 for (int value : values) {
62 addInt(tag, value);
63 }
64 }
65
Chad Brubakerb543b3932015-04-16 14:30:30 -070066 public void addLongs(int tag, long... values) {
67 for (long value : values) {
68 addLong(tag, value);
69 }
70 }
71
Chad Brubaker45ff13e2015-01-21 14:00:55 -080072 public void addBoolean(int tag) {
73 mArguments.add(new KeymasterBooleanArgument(tag));
74 }
75
76 public void addLong(int tag, long value) {
77 mArguments.add(new KeymasterLongArgument(tag, value));
78 }
79
80 public void addBlob(int tag, byte[] value) {
81 mArguments.add(new KeymasterBlobArgument(tag, value));
82 }
83
84 public void addDate(int tag, Date value) {
85 mArguments.add(new KeymasterDateArgument(tag, value));
86 }
87
88 private KeymasterArgument getArgumentByTag(int tag) {
89 for (KeymasterArgument arg : mArguments) {
90 if (arg.tag == tag) {
91 return arg;
92 }
93 }
94 return null;
95 }
96
97 public boolean containsTag(int tag) {
98 return getArgumentByTag(tag) != null;
99 }
100
101 public int getInt(int tag, int defaultValue) {
102 switch (KeymasterDefs.getTagType(tag)) {
103 case KeymasterDefs.KM_ENUM:
104 case KeymasterDefs.KM_INT:
105 break; // Accepted types
106 case KeymasterDefs.KM_INT_REP:
107 case KeymasterDefs.KM_ENUM_REP:
108 throw new IllegalArgumentException("Repeatable tags must use getInts: " + tag);
109 default:
110 throw new IllegalArgumentException("Tag is not an int type: " + tag);
111 }
112 KeymasterArgument arg = getArgumentByTag(tag);
113 if (arg == null) {
114 return defaultValue;
115 }
116 return ((KeymasterIntArgument) arg).value;
117 }
118
119 public long getLong(int tag, long defaultValue) {
Chad Brubakerb543b3932015-04-16 14:30:30 -0700120 switch (KeymasterDefs.getTagType(tag)) {
121 case KeymasterDefs.KM_LONG:
122 break; // Accepted type
123 case KeymasterDefs.KM_LONG_REP:
124 throw new IllegalArgumentException("Repeatable tags must use getLongs: " + tag);
125 default:
126 throw new IllegalArgumentException("Tag is not a long type: " + tag);
Chad Brubaker45ff13e2015-01-21 14:00:55 -0800127 }
128 KeymasterArgument arg = getArgumentByTag(tag);
129 if (arg == null) {
130 return defaultValue;
131 }
132 return ((KeymasterLongArgument) arg).value;
133 }
134
135 public Date getDate(int tag, Date defaultValue) {
136 if (KeymasterDefs.getTagType(tag) != KeymasterDefs.KM_DATE) {
137 throw new IllegalArgumentException("Tag is not a date type: " + tag);
138 }
139 KeymasterArgument arg = getArgumentByTag(tag);
140 if (arg == null) {
141 return defaultValue;
142 }
143 return ((KeymasterDateArgument) arg).date;
144 }
145
146 public boolean getBoolean(int tag, boolean defaultValue) {
147 if (KeymasterDefs.getTagType(tag) != KeymasterDefs.KM_BOOL) {
148 throw new IllegalArgumentException("Tag is not a boolean type: " + tag);
149 }
150 KeymasterArgument arg = getArgumentByTag(tag);
151 if (arg == null) {
152 return defaultValue;
153 }
154 return true;
155 }
156
157 public byte[] getBlob(int tag, byte[] defaultValue) {
158 switch (KeymasterDefs.getTagType(tag)) {
159 case KeymasterDefs.KM_BYTES:
160 case KeymasterDefs.KM_BIGNUM:
161 break; // Allowed types.
162 default:
163 throw new IllegalArgumentException("Tag is not a blob type: " + tag);
164 }
165 KeymasterArgument arg = getArgumentByTag(tag);
166 if (arg == null) {
167 return defaultValue;
168 }
169 return ((KeymasterBlobArgument) arg).blob;
170 }
171
172 public List<Integer> getInts(int tag) {
173 switch (KeymasterDefs.getTagType(tag)) {
174 case KeymasterDefs.KM_INT_REP:
175 case KeymasterDefs.KM_ENUM_REP:
176 break; // Allowed types.
177 default:
178 throw new IllegalArgumentException("Tag is not a repeating type: " + tag);
179 }
180 List<Integer> values = new ArrayList<Integer>();
181 for (KeymasterArgument arg : mArguments) {
182 if (arg.tag == tag) {
183 values.add(((KeymasterIntArgument) arg).value);
184 }
185 }
186 return values;
187 }
188
Chad Brubakerb543b3932015-04-16 14:30:30 -0700189 public List<Long> getLongs(int tag) {
190 if (KeymasterDefs.getTagType(tag) != KeymasterDefs.KM_LONG_REP) {
191 throw new IllegalArgumentException("Tag is not a repeating long: " + tag);
192 }
193 List<Long> values = new ArrayList<Long>();
194 for (KeymasterArgument arg : mArguments) {
195 if (arg.tag == tag) {
196 values.add(((KeymasterLongArgument) arg).value);
197 }
198 }
199 return values;
200 }
201
Chad Brubaker45ff13e2015-01-21 14:00:55 -0800202 public int size() {
203 return mArguments.size();
204 }
205
206 @Override
207 public void writeToParcel(Parcel out, int flags) {
208 out.writeTypedList(mArguments);
209 }
210
211 public void readFromParcel(Parcel in) {
212 in.readTypedList(mArguments, KeymasterArgument.CREATOR);
213 }
214
215 @Override
216 public int describeContents() {
217 return 0;
218 }
219}