blob: 9adde35045cbac0a46ae5763e411b26b854a9b6a [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;
21import android.os.ParcelFormatException;
22
23/**
24 * Base class for the Java side of a Keymaster tagged argument.
25 * <p>
26 * Serialization code for this and subclasses must be kept in sync with system/security/keystore
27 * and with hardware/libhardware/include/hardware/keymaster_defs.h
28 * @hide
29 */
30abstract class KeymasterArgument implements Parcelable {
31 public final int tag;
32
33 public static final Parcelable.Creator<KeymasterArgument> CREATOR = new
34 Parcelable.Creator<KeymasterArgument>() {
35 public KeymasterArgument createFromParcel(Parcel in) {
36 final int pos = in.dataPosition();
37 final int tag = in.readInt();
38 switch (KeymasterDefs.getTagType(tag)) {
39 case KeymasterDefs.KM_ENUM:
40 case KeymasterDefs.KM_ENUM_REP:
41 case KeymasterDefs.KM_INT:
42 case KeymasterDefs.KM_INT_REP:
43 return new KeymasterIntArgument(tag, in);
44 case KeymasterDefs.KM_LONG:
Chad Brubakerb543b3932015-04-16 14:30:30 -070045 case KeymasterDefs.KM_LONG_REP:
Chad Brubaker45ff13e2015-01-21 14:00:55 -080046 return new KeymasterLongArgument(tag, in);
47 case KeymasterDefs.KM_DATE:
48 return new KeymasterDateArgument(tag, in);
49 case KeymasterDefs.KM_BYTES:
50 case KeymasterDefs.KM_BIGNUM:
51 return new KeymasterBlobArgument(tag, in);
52 case KeymasterDefs.KM_BOOL:
53 return new KeymasterBooleanArgument(tag, in);
54 default:
55 throw new ParcelFormatException("Bad tag: " + tag + " at " + pos);
56 }
57 }
58 public KeymasterArgument[] newArray(int size) {
59 return new KeymasterArgument[size];
60 }
61 };
62
63 protected KeymasterArgument(int tag) {
64 this.tag = tag;
65 }
66
67 /**
68 * Writes the value of this argument, if any, to the provided parcel.
69 */
70 public abstract void writeValue(Parcel out);
71
72 @Override
73 public int describeContents() {
74 return 0;
75 }
76
77 @Override
78 public void writeToParcel(Parcel out, int flags) {
79 out.writeInt(tag);
80 writeValue(out);
81 }
82}