blob: 8b86d41c8cf0100492d4e9785e7418e66da53be6 [file] [log] [blame]
Alex Klyubin8effa362015-06-24 16:06:55 -07001/*
2 * Copyright (C) 2015 The Android Open Source Project
Chad Brubaker45ff13e2015-01-21 14:00:55 -08003 *
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 *
Alex Klyubin8effa362015-06-24 16:06:55 -07008 * http://www.apache.org/licenses/LICENSE-2.0
Chad Brubaker45ff13e2015-01-21 14:00:55 -08009 *
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
Jeff Sharkey9e8f83d2019-02-28 12:06:45 -070033 public static final @android.annotation.NonNull Parcelable.Creator<KeymasterArgument> CREATOR = new
Chad Brubaker45ff13e2015-01-21 14:00:55 -080034 Parcelable.Creator<KeymasterArgument>() {
Alex Klyubinae6cb7a2015-06-22 18:09:35 -070035 @Override
Chad Brubaker45ff13e2015-01-21 14:00:55 -080036 public KeymasterArgument createFromParcel(Parcel in) {
37 final int pos = in.dataPosition();
38 final int tag = in.readInt();
39 switch (KeymasterDefs.getTagType(tag)) {
40 case KeymasterDefs.KM_ENUM:
41 case KeymasterDefs.KM_ENUM_REP:
Alex Klyubin3e7a9e42015-06-24 15:46:45 -070042 case KeymasterDefs.KM_UINT:
43 case KeymasterDefs.KM_UINT_REP:
Chad Brubaker45ff13e2015-01-21 14:00:55 -080044 return new KeymasterIntArgument(tag, in);
Alex Klyubin3e7a9e42015-06-24 15:46:45 -070045 case KeymasterDefs.KM_ULONG:
46 case KeymasterDefs.KM_ULONG_REP:
Chad Brubaker45ff13e2015-01-21 14:00:55 -080047 return new KeymasterLongArgument(tag, in);
48 case KeymasterDefs.KM_DATE:
49 return new KeymasterDateArgument(tag, in);
50 case KeymasterDefs.KM_BYTES:
51 case KeymasterDefs.KM_BIGNUM:
52 return new KeymasterBlobArgument(tag, in);
53 case KeymasterDefs.KM_BOOL:
54 return new KeymasterBooleanArgument(tag, in);
55 default:
56 throw new ParcelFormatException("Bad tag: " + tag + " at " + pos);
57 }
58 }
Alex Klyubinae6cb7a2015-06-22 18:09:35 -070059
60 @Override
Chad Brubaker45ff13e2015-01-21 14:00:55 -080061 public KeymasterArgument[] newArray(int size) {
62 return new KeymasterArgument[size];
63 }
64 };
65
66 protected KeymasterArgument(int tag) {
67 this.tag = tag;
68 }
69
70 /**
71 * Writes the value of this argument, if any, to the provided parcel.
72 */
73 public abstract void writeValue(Parcel out);
74
75 @Override
76 public int describeContents() {
77 return 0;
78 }
79
80 @Override
81 public void writeToParcel(Parcel out, int flags) {
82 out.writeInt(tag);
83 writeValue(out);
84 }
85}