| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.security.keymaster; |
| 18 |
|
| 19 |
import android.compat.annotation.UnsupportedAppUsage; |
| 20 |
import android.os.Build; |
| 21 |
import android.os.Parcel; |
| 22 |
import android.os.Parcelable; |
| 23 |
|
| 24 |
import java.math.BigInteger; |
| 25 |
import java.util.ArrayList; |
| 26 |
import java.util.Date; |
| 27 |
import java.util.List; |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
public class KeymasterArguments implements Parcelable { |
| 36 |
|
| 37 |
private static final long UINT32_RANGE = 1L << 32; |
| 38 |
public static final long UINT32_MAX_VALUE = UINT32_RANGE - 1; |
| 39 |
|
| 40 |
private static final BigInteger UINT64_RANGE = BigInteger.ONE.shiftLeft(64); |
| 41 |
public static final BigInteger UINT64_MAX_VALUE = UINT64_RANGE.subtract(BigInteger.ONE); |
| 42 |
|
| 43 |
private List<KeymasterArgument> mArguments; |
| 44 |
|
| 45 |
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) |
| 46 |
public static final @android.annotation.NonNull Parcelable.Creator<KeymasterArguments> CREATOR = new |
| 47 |
Parcelable.Creator<KeymasterArguments>() { |
| 48 |
@Override |
| 49 |
public KeymasterArguments createFromParcel(Parcel in) { |
| 50 |
return new KeymasterArguments(in); |
| 51 |
} |
| 52 |
|
| 53 |
@Override |
| 54 |
public KeymasterArguments[] newArray(int size) { |
| 55 |
return new KeymasterArguments[size]; |
| 56 |
} |
| 57 |
}; |
| 58 |
|
| 59 |
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) |
| 60 |
public KeymasterArguments() { |
| 61 |
mArguments = new ArrayList<KeymasterArgument>(); |
| 62 |
} |
| 63 |
|
| 64 |
private KeymasterArguments(Parcel in) { |
| 65 |
mArguments = in.createTypedArrayList(KeymasterArgument.CREATOR); |
| 66 |
} |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) |
| 74 |
public void addEnum(int tag, int value) { |
| 75 |
int tagType = KeymasterDefs.getTagType(tag); |
| 76 |
if ((tagType != KeymasterDefs.KM_ENUM) && (tagType != KeymasterDefs.KM_ENUM_REP)) { |
| 77 |
throw new IllegalArgumentException("Not an enum or repeating enum tag: " + tag); |
| 78 |
} |
| 79 |
addEnumTag(tag, value); |
| 80 |
} |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
public void addEnums(int tag, int... values) { |
| 88 |
if (KeymasterDefs.getTagType(tag) != KeymasterDefs.KM_ENUM_REP) { |
| 89 |
throw new IllegalArgumentException("Not a repeating enum tag: " + tag); |
| 90 |
} |
| 91 |
for (int value : values) { |
| 92 |
addEnumTag(tag, value); |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
public int getEnum(int tag, int defaultValue) { |
| 103 |
if (KeymasterDefs.getTagType(tag) != KeymasterDefs.KM_ENUM) { |
| 104 |
throw new IllegalArgumentException("Not an enum tag: " + tag); |
| 105 |
} |
| 106 |
KeymasterArgument arg = getArgumentByTag(tag); |
| 107 |
if (arg == null) { |
| 108 |
return defaultValue; |
| 109 |
} |
| 110 |
return getEnumTagValue(arg); |
| 111 |
} |
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
public List<Integer> getEnums(int tag) { |
| 119 |
if (KeymasterDefs.getTagType(tag) != KeymasterDefs.KM_ENUM_REP) { |
| 120 |
throw new IllegalArgumentException("Not a repeating enum tag: " + tag); |
| 121 |
} |
| 122 |
List<Integer> values = new ArrayList<Integer>(); |
| 123 |
for (KeymasterArgument arg : mArguments) { |
| 124 |
if (arg.tag == tag) { |
| 125 |
values.add(getEnumTagValue(arg)); |
| 126 |
} |
| 127 |
} |
| 128 |
return values; |
| 129 |
} |
| 130 |
|
| 131 |
private void addEnumTag(int tag, int value) { |
| 132 |
mArguments.add(new KeymasterIntArgument(tag, value)); |
| 133 |
} |
| 134 |
|
| 135 |
private int getEnumTagValue(KeymasterArgument arg) { |
| 136 |
return ((KeymasterIntArgument) arg).value; |
| 137 |
} |
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) |
| 146 |
public void addUnsignedInt(int tag, long value) { |
| 147 |
int tagType = KeymasterDefs.getTagType(tag); |
| 148 |
if ((tagType != KeymasterDefs.KM_UINT) && (tagType != KeymasterDefs.KM_UINT_REP)) { |
| 149 |
throw new IllegalArgumentException("Not an int or repeating int tag: " + tag); |
| 150 |
} |
| 151 |
|
| 152 |
if ((value < 0) || (value > UINT32_MAX_VALUE)) { |
| 153 |
throw new IllegalArgumentException("Int tag value out of range: " + value); |
| 154 |
} |
| 155 |
mArguments.add(new KeymasterIntArgument(tag, (int) value)); |
| 156 |
} |
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
public long getUnsignedInt(int tag, long defaultValue) { |
| 165 |
if (KeymasterDefs.getTagType(tag) != KeymasterDefs.KM_UINT) { |
| 166 |
throw new IllegalArgumentException("Not an int tag: " + tag); |
| 167 |
} |
| 168 |
KeymasterArgument arg = getArgumentByTag(tag); |
| 169 |
if (arg == null) { |
| 170 |
return defaultValue; |
| 171 |
} |
| 172 |
|
| 173 |
return ((KeymasterIntArgument) arg).value & 0xffffffffL; |
| 174 |
} |
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) |
| 183 |
public void addUnsignedLong(int tag, BigInteger value) { |
| 184 |
int tagType = KeymasterDefs.getTagType(tag); |
| 185 |
if ((tagType != KeymasterDefs.KM_ULONG) && (tagType != KeymasterDefs.KM_ULONG_REP)) { |
| 186 |
throw new IllegalArgumentException("Not a long or repeating long tag: " + tag); |
| 187 |
} |
| 188 |
addLongTag(tag, value); |
| 189 |
} |
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
public List<BigInteger> getUnsignedLongs(int tag) { |
| 197 |
if (KeymasterDefs.getTagType(tag) != KeymasterDefs.KM_ULONG_REP) { |
| 198 |
throw new IllegalArgumentException("Tag is not a repeating long: " + tag); |
| 199 |
} |
| 200 |
List<BigInteger> values = new ArrayList<BigInteger>(); |
| 201 |
for (KeymasterArgument arg : mArguments) { |
| 202 |
if (arg.tag == tag) { |
| 203 |
values.add(getLongTagValue(arg)); |
| 204 |
} |
| 205 |
} |
| 206 |
return values; |
| 207 |
} |
| 208 |
|
| 209 |
private void addLongTag(int tag, BigInteger value) { |
| 210 |
|
| 211 |
if ((value.signum() == -1) || (value.compareTo(UINT64_MAX_VALUE) > 0)) { |
| 212 |
throw new IllegalArgumentException("Long tag value out of range: " + value); |
| 213 |
} |
| 214 |
mArguments.add(new KeymasterLongArgument(tag, value.longValue())); |
| 215 |
} |
| 216 |
|
| 217 |
private BigInteger getLongTagValue(KeymasterArgument arg) { |
| 218 |
|
| 219 |
|
| 220 |
return toUint64(((KeymasterLongArgument) arg).value); |
| 221 |
} |
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
public void addBoolean(int tag) { |
| 230 |
if (KeymasterDefs.getTagType(tag) != KeymasterDefs.KM_BOOL) { |
| 231 |
throw new IllegalArgumentException("Not a boolean tag: " + tag); |
| 232 |
} |
| 233 |
mArguments.add(new KeymasterBooleanArgument(tag)); |
| 234 |
} |
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
public boolean getBoolean(int tag) { |
| 242 |
if (KeymasterDefs.getTagType(tag) != KeymasterDefs.KM_BOOL) { |
| 243 |
throw new IllegalArgumentException("Not a boolean tag: " + tag); |
| 244 |
} |
| 245 |
KeymasterArgument arg = getArgumentByTag(tag); |
| 246 |
if (arg == null) { |
| 247 |
return false; |
| 248 |
} |
| 249 |
return true; |
| 250 |
} |
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
public void addBytes(int tag, byte[] value) { |
| 258 |
if (KeymasterDefs.getTagType(tag) != KeymasterDefs.KM_BYTES) { |
| 259 |
throw new IllegalArgumentException("Not a bytes tag: " + tag); |
| 260 |
} |
| 261 |
if (value == null) { |
| 262 |
throw new NullPointerException("value == nulll"); |
| 263 |
} |
| 264 |
mArguments.add(new KeymasterBlobArgument(tag, value)); |
| 265 |
} |
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
public byte[] getBytes(int tag, byte[] defaultValue) { |
| 274 |
if (KeymasterDefs.getTagType(tag) != KeymasterDefs.KM_BYTES) { |
| 275 |
throw new IllegalArgumentException("Not a bytes tag: " + tag); |
| 276 |
} |
| 277 |
KeymasterArgument arg = getArgumentByTag(tag); |
| 278 |
if (arg == null) { |
| 279 |
return defaultValue; |
| 280 |
} |
| 281 |
return ((KeymasterBlobArgument) arg).blob; |
| 282 |
} |
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
public void addDate(int tag, Date value) { |
| 291 |
if (KeymasterDefs.getTagType(tag) != KeymasterDefs.KM_DATE) { |
| 292 |
throw new IllegalArgumentException("Not a date tag: " + tag); |
| 293 |
} |
| 294 |
if (value == null) { |
| 295 |
throw new NullPointerException("value == nulll"); |
| 296 |
} |
| 297 |
|
| 298 |
|
| 299 |
if (value.getTime() < 0) { |
| 300 |
throw new IllegalArgumentException("Date tag value out of range: " + value); |
| 301 |
} |
| 302 |
mArguments.add(new KeymasterDateArgument(tag, value)); |
| 303 |
} |
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
public void addDateIfNotNull(int tag, Date value) { |
| 313 |
if (KeymasterDefs.getTagType(tag) != KeymasterDefs.KM_DATE) { |
| 314 |
throw new IllegalArgumentException("Not a date tag: " + tag); |
| 315 |
} |
| 316 |
if (value != null) { |
| 317 |
addDate(tag, value); |
| 318 |
} |
| 319 |
} |
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
public Date getDate(int tag, Date defaultValue) { |
| 330 |
if (KeymasterDefs.getTagType(tag) != KeymasterDefs.KM_DATE) { |
| 331 |
throw new IllegalArgumentException("Tag is not a date type: " + tag); |
| 332 |
} |
| 333 |
KeymasterArgument arg = getArgumentByTag(tag); |
| 334 |
if (arg == null) { |
| 335 |
return defaultValue; |
| 336 |
} |
| 337 |
Date result = ((KeymasterDateArgument) arg).date; |
| 338 |
|
| 339 |
|
| 340 |
if (result.getTime() < 0) { |
| 341 |
throw new IllegalArgumentException("Tag value too large. Tag: " + tag); |
| 342 |
} |
| 343 |
return result; |
| 344 |
} |
| 345 |
|
| 346 |
private KeymasterArgument getArgumentByTag(int tag) { |
| 347 |
for (KeymasterArgument arg : mArguments) { |
| 348 |
if (arg.tag == tag) { |
| 349 |
return arg; |
| 350 |
} |
| 351 |
} |
| 352 |
return null; |
| 353 |
} |
| 354 |
|
| 355 |
public boolean containsTag(int tag) { |
| 356 |
return getArgumentByTag(tag) != null; |
| 357 |
} |
| 358 |
|
| 359 |
public int size() { |
| 360 |
return mArguments.size(); |
| 361 |
} |
| 362 |
|
| 363 |
@Override |
| 364 |
public void writeToParcel(Parcel out, int flags) { |
| 365 |
out.writeTypedList(mArguments); |
| 366 |
} |
| 367 |
|
| 368 |
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) |
| 369 |
public void readFromParcel(Parcel in) { |
| 370 |
in.readTypedList(mArguments, KeymasterArgument.CREATOR); |
| 371 |
} |
| 372 |
|
| 373 |
@Override |
| 374 |
public int describeContents() { |
| 375 |
return 0; |
| 376 |
} |
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
public static BigInteger toUint64(long value) { |
| 383 |
if (value >= 0) { |
| 384 |
return BigInteger.valueOf(value); |
| 385 |
} else { |
| 386 |
return BigInteger.valueOf(value).add(UINT64_RANGE); |
| 387 |
} |
| 388 |
} |
| 389 |
} |