Alex Klyubin | ae6cb7a | 2015-06-22 18:09:35 -0700 | [diff] [blame] | 1 | /* |
Alex Klyubin | 8effa36 | 2015-06-24 16:06:55 -0700 | [diff] [blame] | 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 | |
Mathew Inwood | 4dbdcf4 | 2018-08-16 18:49:37 +0100 | [diff] [blame] | 19 | import android.annotation.UnsupportedAppUsage; |
Chad Brubaker | 45ff13e | 2015-01-21 14:00:55 -0800 | [diff] [blame] | 20 | import android.os.Parcel; |
| 21 | import android.os.Parcelable; |
| 22 | |
Alex Klyubin | ae6cb7a | 2015-06-22 18:09:35 -0700 | [diff] [blame] | 23 | import java.math.BigInteger; |
Alex Klyubin | 5927c9f | 2015-04-10 13:28:03 -0700 | [diff] [blame] | 24 | import java.util.ArrayList; |
| 25 | import java.util.Date; |
Chad Brubaker | 45ff13e | 2015-01-21 14:00:55 -0800 | [diff] [blame] | 26 | import java.util.List; |
| 27 | |
| 28 | /** |
| 29 | * @hide |
| 30 | */ |
| 31 | public class KeyCharacteristics implements Parcelable { |
| 32 | public KeymasterArguments swEnforced; |
| 33 | public KeymasterArguments hwEnforced; |
| 34 | |
Jeff Sharkey | 9e8f83d | 2019-02-28 12:06:45 -0700 | [diff] [blame] | 35 | public static final @android.annotation.NonNull Parcelable.Creator<KeyCharacteristics> CREATOR = |
Alex Klyubin | ae6cb7a | 2015-06-22 18:09:35 -0700 | [diff] [blame] | 36 | new Parcelable.Creator<KeyCharacteristics>() { |
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 KeyCharacteristics createFromParcel(Parcel in) { |
| 39 | return new KeyCharacteristics(in); |
| 40 | } |
| 41 | |
Alex Klyubin | 5927c9f | 2015-04-10 13:28:03 -0700 | [diff] [blame] | 42 | @Override |
Chad Brubaker | 45ff13e | 2015-01-21 14:00:55 -0800 | [diff] [blame] | 43 | public KeyCharacteristics[] newArray(int length) { |
| 44 | return new KeyCharacteristics[length]; |
| 45 | } |
| 46 | }; |
| 47 | |
Mathew Inwood | 4dbdcf4 | 2018-08-16 18:49:37 +0100 | [diff] [blame] | 48 | @UnsupportedAppUsage |
Chad Brubaker | 45ff13e | 2015-01-21 14:00:55 -0800 | [diff] [blame] | 49 | public KeyCharacteristics() {} |
| 50 | |
| 51 | protected KeyCharacteristics(Parcel in) { |
| 52 | readFromParcel(in); |
| 53 | } |
| 54 | |
Janis Danisevskis | b0358e7 | 2018-11-02 10:34:07 -0700 | [diff] [blame] | 55 | /** |
| 56 | * Makes a shallow copy of other by copying the other's references to the KeymasterArguments |
| 57 | */ |
| 58 | public void shallowCopyFrom(KeyCharacteristics other) { |
| 59 | this.swEnforced = other.swEnforced; |
| 60 | this.hwEnforced = other.hwEnforced; |
| 61 | } |
| 62 | |
Chad Brubaker | 45ff13e | 2015-01-21 14:00:55 -0800 | [diff] [blame] | 63 | @Override |
| 64 | public int describeContents() { |
| 65 | return 0; |
| 66 | } |
| 67 | |
Alex Klyubin | 5927c9f | 2015-04-10 13:28:03 -0700 | [diff] [blame] | 68 | @Override |
Chad Brubaker | 45ff13e | 2015-01-21 14:00:55 -0800 | [diff] [blame] | 69 | public void writeToParcel(Parcel out, int flags) { |
| 70 | swEnforced.writeToParcel(out, flags); |
| 71 | hwEnforced.writeToParcel(out, flags); |
| 72 | } |
| 73 | |
Mathew Inwood | 4dbdcf4 | 2018-08-16 18:49:37 +0100 | [diff] [blame] | 74 | @UnsupportedAppUsage |
Chad Brubaker | 45ff13e | 2015-01-21 14:00:55 -0800 | [diff] [blame] | 75 | public void readFromParcel(Parcel in) { |
| 76 | swEnforced = KeymasterArguments.CREATOR.createFromParcel(in); |
| 77 | hwEnforced = KeymasterArguments.CREATOR.createFromParcel(in); |
| 78 | } |
Alex Klyubin | 5927c9f | 2015-04-10 13:28:03 -0700 | [diff] [blame] | 79 | |
Alex Klyubin | ae6cb7a | 2015-06-22 18:09:35 -0700 | [diff] [blame] | 80 | /** |
| 81 | * Returns the value of the specified enum tag or {@code defaultValue} if the tag is not |
| 82 | * present. |
| 83 | * |
| 84 | * @throws IllegalArgumentException if {@code tag} is not an enum tag. |
| 85 | */ |
| 86 | public Integer getEnum(int tag) { |
Alex Klyubin | 5927c9f | 2015-04-10 13:28:03 -0700 | [diff] [blame] | 87 | if (hwEnforced.containsTag(tag)) { |
Alex Klyubin | ae6cb7a | 2015-06-22 18:09:35 -0700 | [diff] [blame] | 88 | return hwEnforced.getEnum(tag, -1); |
Alex Klyubin | 5927c9f | 2015-04-10 13:28:03 -0700 | [diff] [blame] | 89 | } else if (swEnforced.containsTag(tag)) { |
Alex Klyubin | ae6cb7a | 2015-06-22 18:09:35 -0700 | [diff] [blame] | 90 | return swEnforced.getEnum(tag, -1); |
Alex Klyubin | 5927c9f | 2015-04-10 13:28:03 -0700 | [diff] [blame] | 91 | } else { |
| 92 | return null; |
| 93 | } |
| 94 | } |
| 95 | |
Alex Klyubin | ae6cb7a | 2015-06-22 18:09:35 -0700 | [diff] [blame] | 96 | /** |
| 97 | * Returns all values of the specified repeating enum tag. |
| 98 | * |
| 99 | * throws IllegalArgumentException if {@code tag} is not a repeating enum tag. |
| 100 | */ |
| 101 | public List<Integer> getEnums(int tag) { |
Alex Klyubin | 5927c9f | 2015-04-10 13:28:03 -0700 | [diff] [blame] | 102 | List<Integer> result = new ArrayList<Integer>(); |
Alex Klyubin | ae6cb7a | 2015-06-22 18:09:35 -0700 | [diff] [blame] | 103 | result.addAll(hwEnforced.getEnums(tag)); |
| 104 | result.addAll(swEnforced.getEnums(tag)); |
Alex Klyubin | 5927c9f | 2015-04-10 13:28:03 -0700 | [diff] [blame] | 105 | return result; |
| 106 | } |
| 107 | |
Alex Klyubin | ae6cb7a | 2015-06-22 18:09:35 -0700 | [diff] [blame] | 108 | /** |
| 109 | * Returns the value of the specified unsigned 32-bit int tag or {@code defaultValue} if the tag |
| 110 | * is not present. |
| 111 | * |
| 112 | * @throws IllegalArgumentException if {@code tag} is not an unsigned 32-bit int tag. |
| 113 | */ |
| 114 | public long getUnsignedInt(int tag, long defaultValue) { |
Alex Klyubin | 708fc940 | 2015-04-28 18:58:47 -0700 | [diff] [blame] | 115 | if (hwEnforced.containsTag(tag)) { |
Alex Klyubin | ae6cb7a | 2015-06-22 18:09:35 -0700 | [diff] [blame] | 116 | return hwEnforced.getUnsignedInt(tag, defaultValue); |
Alex Klyubin | 708fc940 | 2015-04-28 18:58:47 -0700 | [diff] [blame] | 117 | } else { |
Alex Klyubin | ae6cb7a | 2015-06-22 18:09:35 -0700 | [diff] [blame] | 118 | return swEnforced.getUnsignedInt(tag, defaultValue); |
Alex Klyubin | 708fc940 | 2015-04-28 18:58:47 -0700 | [diff] [blame] | 119 | } |
| 120 | } |
| 121 | |
Alex Klyubin | ae6cb7a | 2015-06-22 18:09:35 -0700 | [diff] [blame] | 122 | /** |
| 123 | * Returns all values of the specified repeating unsigned 64-bit long tag. |
| 124 | * |
| 125 | * @throws IllegalArgumentException if {@code tag} is not a repeating unsigned 64-bit long tag. |
| 126 | */ |
| 127 | public List<BigInteger> getUnsignedLongs(int tag) { |
| 128 | List<BigInteger> result = new ArrayList<BigInteger>(); |
| 129 | result.addAll(hwEnforced.getUnsignedLongs(tag)); |
| 130 | result.addAll(swEnforced.getUnsignedLongs(tag)); |
Alex Klyubin | 708fc940 | 2015-04-28 18:58:47 -0700 | [diff] [blame] | 131 | return result; |
| 132 | } |
| 133 | |
Alex Klyubin | ae6cb7a | 2015-06-22 18:09:35 -0700 | [diff] [blame] | 134 | /** |
| 135 | * Returns the value of the specified date tag or {@code null} if the tag is not present. |
| 136 | * |
| 137 | * @throws IllegalArgumentException if {@code tag} is not a date tag or if the tag's value |
| 138 | * represents a time instant which is after {@code 2^63 - 1} milliseconds since Unix |
| 139 | * epoch. |
| 140 | */ |
Alex Klyubin | 5927c9f | 2015-04-10 13:28:03 -0700 | [diff] [blame] | 141 | public Date getDate(int tag) { |
Alex Klyubin | ae6cb7a | 2015-06-22 18:09:35 -0700 | [diff] [blame] | 142 | Date result = swEnforced.getDate(tag, null); |
| 143 | if (result != null) { |
| 144 | return result; |
Alex Klyubin | 5927c9f | 2015-04-10 13:28:03 -0700 | [diff] [blame] | 145 | } |
Alex Klyubin | ae6cb7a | 2015-06-22 18:09:35 -0700 | [diff] [blame] | 146 | return hwEnforced.getDate(tag, null); |
Alex Klyubin | 5927c9f | 2015-04-10 13:28:03 -0700 | [diff] [blame] | 147 | } |
| 148 | |
Alex Klyubin | ae6cb7a | 2015-06-22 18:09:35 -0700 | [diff] [blame] | 149 | /** |
| 150 | * Returns {@code true} if the provided boolean tag is present, {@code false} if absent. |
| 151 | * |
| 152 | * @throws IllegalArgumentException if {@code tag} is not a boolean tag. |
| 153 | */ |
Alex Klyubin | 1eda77a | 2015-04-28 14:21:01 -0700 | [diff] [blame] | 154 | public boolean getBoolean(int tag) { |
| 155 | if (hwEnforced.containsTag(tag)) { |
Alex Klyubin | ae6cb7a | 2015-06-22 18:09:35 -0700 | [diff] [blame] | 156 | return hwEnforced.getBoolean(tag); |
Alex Klyubin | 5927c9f | 2015-04-10 13:28:03 -0700 | [diff] [blame] | 157 | } else { |
Alex Klyubin | ae6cb7a | 2015-06-22 18:09:35 -0700 | [diff] [blame] | 158 | return swEnforced.getBoolean(tag); |
Alex Klyubin | 5927c9f | 2015-04-10 13:28:03 -0700 | [diff] [blame] | 159 | } |
| 160 | } |
Chad Brubaker | 45ff13e | 2015-01-21 14:00:55 -0800 | [diff] [blame] | 161 | } |
| 162 | |