blob: 9a1c894fd668d2ef4d59a474b64a5c6c48725686 [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:
45 return new KeymasterLongArgument(tag, in);
46 case KeymasterDefs.KM_DATE:
47 return new KeymasterDateArgument(tag, in);
48 case KeymasterDefs.KM_BYTES:
49 case KeymasterDefs.KM_BIGNUM:
50 return new KeymasterBlobArgument(tag, in);
51 case KeymasterDefs.KM_BOOL:
52 return new KeymasterBooleanArgument(tag, in);
53 default:
54 throw new ParcelFormatException("Bad tag: " + tag + " at " + pos);
55 }
56 }
57 public KeymasterArgument[] newArray(int size) {
58 return new KeymasterArgument[size];
59 }
60 };
61
62 protected KeymasterArgument(int tag) {
63 this.tag = tag;
64 }
65
66 /**
67 * Writes the value of this argument, if any, to the provided parcel.
68 */
69 public abstract void writeValue(Parcel out);
70
71 @Override
72 public int describeContents() {
73 return 0;
74 }
75
76 @Override
77 public void writeToParcel(Parcel out, int flags) {
78 out.writeInt(tag);
79 writeValue(out);
80 }
81}