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.compat.annotation.UnsupportedAppUsage; 20 import android.os.Parcel; 21 import android.os.Parcelable; 22 23 import java.math.BigInteger; 24 import java.util.ArrayList; 25 import java.util.Date; 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 35 public static final @android.annotation.NonNull Parcelable.Creator<KeyCharacteristics> CREATOR = 36 new Parcelable.Creator<KeyCharacteristics>() { 37 @Override 38 public KeyCharacteristics createFromParcel(Parcel in) { 39 return new KeyCharacteristics(in); 40 } 41 42 @Override 43 public KeyCharacteristics[] newArray(int length) { 44 return new KeyCharacteristics[length]; 45 } 46 }; 47 48 @UnsupportedAppUsage KeyCharacteristics()49 public KeyCharacteristics() {} 50 KeyCharacteristics(Parcel in)51 protected KeyCharacteristics(Parcel in) { 52 readFromParcel(in); 53 } 54 55 /** 56 * Makes a shallow copy of other by copying the other's references to the KeymasterArguments 57 */ shallowCopyFrom(KeyCharacteristics other)58 public void shallowCopyFrom(KeyCharacteristics other) { 59 this.swEnforced = other.swEnforced; 60 this.hwEnforced = other.hwEnforced; 61 } 62 63 @Override describeContents()64 public int describeContents() { 65 return 0; 66 } 67 68 @Override writeToParcel(Parcel out, int flags)69 public void writeToParcel(Parcel out, int flags) { 70 swEnforced.writeToParcel(out, flags); 71 hwEnforced.writeToParcel(out, flags); 72 } 73 74 @UnsupportedAppUsage readFromParcel(Parcel in)75 public void readFromParcel(Parcel in) { 76 swEnforced = KeymasterArguments.CREATOR.createFromParcel(in); 77 hwEnforced = KeymasterArguments.CREATOR.createFromParcel(in); 78 } 79 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 */ getEnum(int tag)86 public Integer getEnum(int tag) { 87 if (hwEnforced.containsTag(tag)) { 88 return hwEnforced.getEnum(tag, -1); 89 } else if (swEnforced.containsTag(tag)) { 90 return swEnforced.getEnum(tag, -1); 91 } else { 92 return null; 93 } 94 } 95 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 */ getEnums(int tag)101 public List<Integer> getEnums(int tag) { 102 List<Integer> result = new ArrayList<Integer>(); 103 result.addAll(hwEnforced.getEnums(tag)); 104 result.addAll(swEnforced.getEnums(tag)); 105 return result; 106 } 107 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 */ getUnsignedInt(int tag, long defaultValue)114 public long getUnsignedInt(int tag, long defaultValue) { 115 if (hwEnforced.containsTag(tag)) { 116 return hwEnforced.getUnsignedInt(tag, defaultValue); 117 } else { 118 return swEnforced.getUnsignedInt(tag, defaultValue); 119 } 120 } 121 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 */ getUnsignedLongs(int tag)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)); 131 return result; 132 } 133 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 */ getDate(int tag)141 public Date getDate(int tag) { 142 Date result = swEnforced.getDate(tag, null); 143 if (result != null) { 144 return result; 145 } 146 return hwEnforced.getDate(tag, null); 147 } 148 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 */ getBoolean(int tag)154 public boolean getBoolean(int tag) { 155 if (hwEnforced.containsTag(tag)) { 156 return hwEnforced.getBoolean(tag); 157 } else { 158 return swEnforced.getBoolean(tag); 159 } 160 } 161 } 162 163