Alex Klyubin | 8effa36 | 2015-06-24 16:06:55 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 The Android Open Source Project |
Chad Brubaker | 45ff13e | 2015-01-21 14:00:55 -0800 | [diff] [blame] | 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 | * |
Alex Klyubin | 8effa36 | 2015-06-24 16:06:55 -0700 | [diff] [blame] | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
Chad Brubaker | 45ff13e | 2015-01-21 14:00:55 -0800 | [diff] [blame] | 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 | |
Jeff Sharkey | 9e8f83d | 2019-02-28 12:06:45 -0700 | [diff] [blame] | 33 | public static final @android.annotation.NonNull Parcelable.Creator<KeymasterArgument> CREATOR = new |
Chad Brubaker | 45ff13e | 2015-01-21 14:00:55 -0800 | [diff] [blame] | 34 | Parcelable.Creator<KeymasterArgument>() { |
Alex Klyubin | ae6cb7a | 2015-06-22 18:09:35 -0700 | [diff] [blame] | 35 | @Override |
Chad Brubaker | 45ff13e | 2015-01-21 14:00:55 -0800 | [diff] [blame] | 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: |
Alex Klyubin | 3e7a9e4 | 2015-06-24 15:46:45 -0700 | [diff] [blame] | 42 | case KeymasterDefs.KM_UINT: |
| 43 | case KeymasterDefs.KM_UINT_REP: |
Chad Brubaker | 45ff13e | 2015-01-21 14:00:55 -0800 | [diff] [blame] | 44 | return new KeymasterIntArgument(tag, in); |
Alex Klyubin | 3e7a9e4 | 2015-06-24 15:46:45 -0700 | [diff] [blame] | 45 | case KeymasterDefs.KM_ULONG: |
| 46 | case KeymasterDefs.KM_ULONG_REP: |
Chad Brubaker | 45ff13e | 2015-01-21 14:00:55 -0800 | [diff] [blame] | 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 | } |
Alex Klyubin | ae6cb7a | 2015-06-22 18:09:35 -0700 | [diff] [blame] | 59 | |
| 60 | @Override |
Chad Brubaker | 45ff13e | 2015-01-21 14:00:55 -0800 | [diff] [blame] | 61 | 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 | } |