Chad Brubaker | 45ff13e | 2015-01-21 14:00:55 -0800 | [diff] [blame] | 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 | |
| 22 | import java.util.ArrayList; |
| 23 | import java.util.Date; |
| 24 | import java.util.List; |
| 25 | |
| 26 | /** |
| 27 | * Utility class for the java side of user specified Keymaster arguments. |
| 28 | * <p> |
| 29 | * Serialization code for this and subclasses must be kept in sync with system/security/keystore |
| 30 | * @hide |
| 31 | */ |
| 32 | public class KeymasterArguments implements Parcelable { |
| 33 | List<KeymasterArgument> mArguments; |
| 34 | |
| 35 | public static final Parcelable.Creator<KeymasterArguments> CREATOR = new |
| 36 | Parcelable.Creator<KeymasterArguments>() { |
Alex Klyubin | 5927c9f | 2015-04-10 13:28:03 -0700 | [diff] [blame] | 37 | @Override |
Chad Brubaker | 45ff13e | 2015-01-21 14:00:55 -0800 | [diff] [blame] | 38 | public KeymasterArguments createFromParcel(Parcel in) { |
| 39 | return new KeymasterArguments(in); |
| 40 | } |
Alex Klyubin | 5927c9f | 2015-04-10 13:28:03 -0700 | [diff] [blame] | 41 | |
| 42 | @Override |
Chad Brubaker | 45ff13e | 2015-01-21 14:00:55 -0800 | [diff] [blame] | 43 | public KeymasterArguments[] newArray(int size) { |
| 44 | return new KeymasterArguments[size]; |
| 45 | } |
| 46 | }; |
| 47 | |
| 48 | public KeymasterArguments() { |
| 49 | mArguments = new ArrayList<KeymasterArgument>(); |
| 50 | } |
| 51 | |
| 52 | private KeymasterArguments(Parcel in) { |
| 53 | mArguments = in.createTypedArrayList(KeymasterArgument.CREATOR); |
| 54 | } |
| 55 | |
| 56 | public void addInt(int tag, int value) { |
| 57 | mArguments.add(new KeymasterIntArgument(tag, value)); |
| 58 | } |
| 59 | |
Alex Klyubin | 5927c9f | 2015-04-10 13:28:03 -0700 | [diff] [blame] | 60 | public void addInts(int tag, int... values) { |
| 61 | for (int value : values) { |
| 62 | addInt(tag, value); |
| 63 | } |
| 64 | } |
| 65 | |
Chad Brubaker | 45ff13e | 2015-01-21 14:00:55 -0800 | [diff] [blame] | 66 | public void addBoolean(int tag) { |
| 67 | mArguments.add(new KeymasterBooleanArgument(tag)); |
| 68 | } |
| 69 | |
| 70 | public void addLong(int tag, long value) { |
| 71 | mArguments.add(new KeymasterLongArgument(tag, value)); |
| 72 | } |
| 73 | |
| 74 | public void addBlob(int tag, byte[] value) { |
| 75 | mArguments.add(new KeymasterBlobArgument(tag, value)); |
| 76 | } |
| 77 | |
| 78 | public void addDate(int tag, Date value) { |
| 79 | mArguments.add(new KeymasterDateArgument(tag, value)); |
| 80 | } |
| 81 | |
| 82 | private KeymasterArgument getArgumentByTag(int tag) { |
| 83 | for (KeymasterArgument arg : mArguments) { |
| 84 | if (arg.tag == tag) { |
| 85 | return arg; |
| 86 | } |
| 87 | } |
| 88 | return null; |
| 89 | } |
| 90 | |
| 91 | public boolean containsTag(int tag) { |
| 92 | return getArgumentByTag(tag) != null; |
| 93 | } |
| 94 | |
| 95 | public int getInt(int tag, int defaultValue) { |
| 96 | switch (KeymasterDefs.getTagType(tag)) { |
| 97 | case KeymasterDefs.KM_ENUM: |
| 98 | case KeymasterDefs.KM_INT: |
| 99 | break; // Accepted types |
| 100 | case KeymasterDefs.KM_INT_REP: |
| 101 | case KeymasterDefs.KM_ENUM_REP: |
| 102 | throw new IllegalArgumentException("Repeatable tags must use getInts: " + tag); |
| 103 | default: |
| 104 | throw new IllegalArgumentException("Tag is not an int type: " + tag); |
| 105 | } |
| 106 | KeymasterArgument arg = getArgumentByTag(tag); |
| 107 | if (arg == null) { |
| 108 | return defaultValue; |
| 109 | } |
| 110 | return ((KeymasterIntArgument) arg).value; |
| 111 | } |
| 112 | |
| 113 | public long getLong(int tag, long defaultValue) { |
| 114 | if (KeymasterDefs.getTagType(tag) != KeymasterDefs.KM_LONG) { |
| 115 | throw new IllegalArgumentException("Tag is not a long type: " + tag); |
| 116 | } |
| 117 | KeymasterArgument arg = getArgumentByTag(tag); |
| 118 | if (arg == null) { |
| 119 | return defaultValue; |
| 120 | } |
| 121 | return ((KeymasterLongArgument) arg).value; |
| 122 | } |
| 123 | |
| 124 | public Date getDate(int tag, Date defaultValue) { |
| 125 | if (KeymasterDefs.getTagType(tag) != KeymasterDefs.KM_DATE) { |
| 126 | throw new IllegalArgumentException("Tag is not a date type: " + tag); |
| 127 | } |
| 128 | KeymasterArgument arg = getArgumentByTag(tag); |
| 129 | if (arg == null) { |
| 130 | return defaultValue; |
| 131 | } |
| 132 | return ((KeymasterDateArgument) arg).date; |
| 133 | } |
| 134 | |
| 135 | public boolean getBoolean(int tag, boolean defaultValue) { |
| 136 | if (KeymasterDefs.getTagType(tag) != KeymasterDefs.KM_BOOL) { |
| 137 | throw new IllegalArgumentException("Tag is not a boolean type: " + tag); |
| 138 | } |
| 139 | KeymasterArgument arg = getArgumentByTag(tag); |
| 140 | if (arg == null) { |
| 141 | return defaultValue; |
| 142 | } |
| 143 | return true; |
| 144 | } |
| 145 | |
| 146 | public byte[] getBlob(int tag, byte[] defaultValue) { |
| 147 | switch (KeymasterDefs.getTagType(tag)) { |
| 148 | case KeymasterDefs.KM_BYTES: |
| 149 | case KeymasterDefs.KM_BIGNUM: |
| 150 | break; // Allowed types. |
| 151 | default: |
| 152 | throw new IllegalArgumentException("Tag is not a blob type: " + tag); |
| 153 | } |
| 154 | KeymasterArgument arg = getArgumentByTag(tag); |
| 155 | if (arg == null) { |
| 156 | return defaultValue; |
| 157 | } |
| 158 | return ((KeymasterBlobArgument) arg).blob; |
| 159 | } |
| 160 | |
| 161 | public List<Integer> getInts(int tag) { |
| 162 | switch (KeymasterDefs.getTagType(tag)) { |
| 163 | case KeymasterDefs.KM_INT_REP: |
| 164 | case KeymasterDefs.KM_ENUM_REP: |
| 165 | break; // Allowed types. |
| 166 | default: |
| 167 | throw new IllegalArgumentException("Tag is not a repeating type: " + tag); |
| 168 | } |
| 169 | List<Integer> values = new ArrayList<Integer>(); |
| 170 | for (KeymasterArgument arg : mArguments) { |
| 171 | if (arg.tag == tag) { |
| 172 | values.add(((KeymasterIntArgument) arg).value); |
| 173 | } |
| 174 | } |
| 175 | return values; |
| 176 | } |
| 177 | |
| 178 | public int size() { |
| 179 | return mArguments.size(); |
| 180 | } |
| 181 | |
| 182 | @Override |
| 183 | public void writeToParcel(Parcel out, int flags) { |
| 184 | out.writeTypedList(mArguments); |
| 185 | } |
| 186 | |
| 187 | public void readFromParcel(Parcel in) { |
| 188 | in.readTypedList(mArguments, KeymasterArgument.CREATOR); |
| 189 | } |
| 190 | |
| 191 | @Override |
| 192 | public int describeContents() { |
| 193 | return 0; |
| 194 | } |
| 195 | } |