1 /* 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 17 package android.security.keymaster; 18 19 import android.os.Parcel; 20 import android.os.Parcelable; 21 import 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 */ 30 abstract class KeymasterArgument implements Parcelable { 31 public final int tag; 32 33 public static final @android.annotation.NonNull Parcelable.Creator<KeymasterArgument> CREATOR = new 34 Parcelable.Creator<KeymasterArgument>() { 35 @Override 36 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: 42 case KeymasterDefs.KM_UINT: 43 case KeymasterDefs.KM_UINT_REP: 44 return new KeymasterIntArgument(tag, in); 45 case KeymasterDefs.KM_ULONG: 46 case KeymasterDefs.KM_ULONG_REP: 47 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 } 59 60 @Override 61 public KeymasterArgument[] newArray(int size) { 62 return new KeymasterArgument[size]; 63 } 64 }; 65 KeymasterArgument(int tag)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 */ writeValue(Parcel out)73 public abstract void writeValue(Parcel out); 74 75 @Override describeContents()76 public int describeContents() { 77 return 0; 78 } 79 80 @Override writeToParcel(Parcel out, int flags)81 public void writeToParcel(Parcel out, int flags) { 82 out.writeInt(tag); 83 writeValue(out); 84 } 85 } 86