Khaled Abdelmohsen | e959bae | 2019-10-04 17:01:13 +0100 | [diff] [blame] | 1 | /* |
Omer Nebil Yaveroglu | 15395f5 | 2020-01-22 12:14:44 +0000 | [diff] [blame] | 2 | * Copyright (C) 2020 The Android Open Source Project |
Khaled Abdelmohsen | e959bae | 2019-10-04 17:01:13 +0100 | [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 | * |
| 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 | |
Song Pan | 75147d5 | 2019-11-19 00:57:46 +0000 | [diff] [blame] | 17 | package android.content.integrity; |
Khaled Abdelmohsen | e959bae | 2019-10-04 17:01:13 +0100 | [diff] [blame] | 18 | |
Khaled Abdelmohsen | be0454d | 2019-10-07 10:24:18 +0100 | [diff] [blame] | 19 | import static com.android.internal.util.Preconditions.checkArgument; |
Khaled Abdelmohsen | be0454d | 2019-10-07 10:24:18 +0100 | [diff] [blame] | 20 | |
Song Pan | 097f65d | 2019-11-10 18:02:52 +0000 | [diff] [blame] | 21 | import android.annotation.IntDef; |
| 22 | import android.annotation.NonNull; |
Song Pan | 097f65d | 2019-11-10 18:02:52 +0000 | [diff] [blame] | 23 | import android.os.Parcel; |
| 24 | import android.os.Parcelable; |
Khaled Abdelmohsen | e959bae | 2019-10-04 17:01:13 +0100 | [diff] [blame] | 25 | |
Song Pan | 097f65d | 2019-11-10 18:02:52 +0000 | [diff] [blame] | 26 | import com.android.internal.annotations.VisibleForTesting; |
| 27 | |
| 28 | import java.lang.annotation.Retention; |
| 29 | import java.lang.annotation.RetentionPolicy; |
Omer Nebil Yaveroglu | 15395f5 | 2020-01-22 12:14:44 +0000 | [diff] [blame] | 30 | import java.nio.charset.StandardCharsets; |
| 31 | import java.security.MessageDigest; |
| 32 | import java.security.NoSuchAlgorithmException; |
Khaled Abdelmohsen | f5f9814 | 2019-10-10 17:29:53 +0100 | [diff] [blame] | 33 | import java.util.Objects; |
| 34 | |
Khaled Abdelmohsen | e959bae | 2019-10-04 17:01:13 +0100 | [diff] [blame] | 35 | /** |
| 36 | * Represents a simple formula consisting of an app install metadata field and a value. |
| 37 | * |
| 38 | * <p>Instances of this class are immutable. |
Song Pan | 097f65d | 2019-11-10 18:02:52 +0000 | [diff] [blame] | 39 | * |
| 40 | * @hide |
Khaled Abdelmohsen | e959bae | 2019-10-04 17:01:13 +0100 | [diff] [blame] | 41 | */ |
Song Pan | 097f65d | 2019-11-10 18:02:52 +0000 | [diff] [blame] | 42 | @VisibleForTesting |
Omer Nebil Yaveroglu | 15395f5 | 2020-01-22 12:14:44 +0000 | [diff] [blame] | 43 | public abstract class AtomicFormula extends IntegrityFormula { |
Khaled Abdelmohsen | 29be964 | 2019-10-08 18:34:45 +0100 | [diff] [blame] | 44 | |
Anton Hansson | 57b6af0 | 2019-12-10 15:38:59 +0000 | [diff] [blame] | 45 | /** @hide */ |
Song Pan | 097f65d | 2019-11-10 18:02:52 +0000 | [diff] [blame] | 46 | @IntDef( |
| 47 | value = { |
Omer Nebil Yaveroglu | 15395f5 | 2020-01-22 12:14:44 +0000 | [diff] [blame] | 48 | PACKAGE_NAME, |
| 49 | APP_CERTIFICATE, |
| 50 | INSTALLER_NAME, |
| 51 | INSTALLER_CERTIFICATE, |
| 52 | VERSION_CODE, |
| 53 | PRE_INSTALLED, |
Song Pan | 097f65d | 2019-11-10 18:02:52 +0000 | [diff] [blame] | 54 | }) |
| 55 | @Retention(RetentionPolicy.SOURCE) |
Omer Nebil Yaveroglu | 15395f5 | 2020-01-22 12:14:44 +0000 | [diff] [blame] | 56 | public @interface Key { |
| 57 | } |
Song Pan | 097f65d | 2019-11-10 18:02:52 +0000 | [diff] [blame] | 58 | |
Anton Hansson | 57b6af0 | 2019-12-10 15:38:59 +0000 | [diff] [blame] | 59 | /** @hide */ |
Omer Nebil Yaveroglu | 15395f5 | 2020-01-22 12:14:44 +0000 | [diff] [blame] | 60 | @IntDef(value = {EQ, GT, GTE}) |
Song Pan | 097f65d | 2019-11-10 18:02:52 +0000 | [diff] [blame] | 61 | @Retention(RetentionPolicy.SOURCE) |
Omer Nebil Yaveroglu | 15395f5 | 2020-01-22 12:14:44 +0000 | [diff] [blame] | 62 | public @interface Operator { |
| 63 | } |
Song Pan | 097f65d | 2019-11-10 18:02:52 +0000 | [diff] [blame] | 64 | |
Song Pan | 44e5aa5 | 2019-11-26 11:38:17 +0000 | [diff] [blame] | 65 | /** |
| 66 | * Package name of the app. |
| 67 | * |
| 68 | * <p>Can only be used in {@link StringAtomicFormula}. |
| 69 | */ |
Song Pan | 097f65d | 2019-11-10 18:02:52 +0000 | [diff] [blame] | 70 | public static final int PACKAGE_NAME = 0; |
Song Pan | 44e5aa5 | 2019-11-26 11:38:17 +0000 | [diff] [blame] | 71 | |
| 72 | /** |
| 73 | * SHA-256 of the app certificate of the app. |
| 74 | * |
| 75 | * <p>Can only be used in {@link StringAtomicFormula}. |
| 76 | */ |
Song Pan | 097f65d | 2019-11-10 18:02:52 +0000 | [diff] [blame] | 77 | public static final int APP_CERTIFICATE = 1; |
Song Pan | 44e5aa5 | 2019-11-26 11:38:17 +0000 | [diff] [blame] | 78 | |
| 79 | /** |
| 80 | * Package name of the installer. Will be empty string if installed by the system (e.g., adb). |
| 81 | * |
| 82 | * <p>Can only be used in {@link StringAtomicFormula}. |
| 83 | */ |
Song Pan | 097f65d | 2019-11-10 18:02:52 +0000 | [diff] [blame] | 84 | public static final int INSTALLER_NAME = 2; |
Song Pan | 44e5aa5 | 2019-11-26 11:38:17 +0000 | [diff] [blame] | 85 | |
| 86 | /** |
| 87 | * SHA-256 of the cert of the installer. Will be empty string if installed by the system (e.g., |
| 88 | * adb). |
| 89 | * |
| 90 | * <p>Can only be used in {@link StringAtomicFormula}. |
| 91 | */ |
Song Pan | 097f65d | 2019-11-10 18:02:52 +0000 | [diff] [blame] | 92 | public static final int INSTALLER_CERTIFICATE = 3; |
Song Pan | 44e5aa5 | 2019-11-26 11:38:17 +0000 | [diff] [blame] | 93 | |
| 94 | /** |
| 95 | * Version code of the app. |
| 96 | * |
Omer Nebil Yaveroglu | 15395f5 | 2020-01-22 12:14:44 +0000 | [diff] [blame] | 97 | * <p>Can only be used in {@link LongAtomicFormula}. |
Song Pan | 44e5aa5 | 2019-11-26 11:38:17 +0000 | [diff] [blame] | 98 | */ |
Song Pan | 097f65d | 2019-11-10 18:02:52 +0000 | [diff] [blame] | 99 | public static final int VERSION_CODE = 4; |
Song Pan | 44e5aa5 | 2019-11-26 11:38:17 +0000 | [diff] [blame] | 100 | |
| 101 | /** |
| 102 | * If the app is pre-installed on the device. |
| 103 | * |
| 104 | * <p>Can only be used in {@link BooleanAtomicFormula}. |
| 105 | */ |
Song Pan | 097f65d | 2019-11-10 18:02:52 +0000 | [diff] [blame] | 106 | public static final int PRE_INSTALLED = 5; |
| 107 | |
| 108 | public static final int EQ = 0; |
Omer Nebil Yaveroglu | 15395f5 | 2020-01-22 12:14:44 +0000 | [diff] [blame] | 109 | public static final int GT = 1; |
| 110 | public static final int GTE = 2; |
Song Pan | 097f65d | 2019-11-10 18:02:52 +0000 | [diff] [blame] | 111 | |
| 112 | private final @Key int mKey; |
| 113 | |
| 114 | public AtomicFormula(@Key int key) { |
Khaled Abdelmohsen | 5809a5f | 2019-11-25 18:20:46 +0000 | [diff] [blame] | 115 | checkArgument(isValidKey(key), String.format("Unknown key: %d", key)); |
Song Pan | 097f65d | 2019-11-10 18:02:52 +0000 | [diff] [blame] | 116 | mKey = key; |
Khaled Abdelmohsen | e959bae | 2019-10-04 17:01:13 +0100 | [diff] [blame] | 117 | } |
| 118 | |
Omer Nebil Yaveroglu | 15395f5 | 2020-01-22 12:14:44 +0000 | [diff] [blame] | 119 | /** An {@link AtomicFormula} with an key and long value. */ |
| 120 | public static final class LongAtomicFormula extends AtomicFormula implements Parcelable { |
| 121 | private final Long mValue; |
| 122 | private final @Operator Integer mOperator; |
Song Pan | 097f65d | 2019-11-10 18:02:52 +0000 | [diff] [blame] | 123 | |
| 124 | /** |
Omer Nebil Yaveroglu | 15395f5 | 2020-01-22 12:14:44 +0000 | [diff] [blame] | 125 | * Constructs an empty {@link LongAtomicFormula}. This should only be used as a base. |
| 126 | * |
| 127 | * <p>This formula will always return false. |
| 128 | * |
| 129 | * @throws IllegalArgumentException if {@code key} cannot be used with long value |
| 130 | */ |
| 131 | public LongAtomicFormula(@Key int key) { |
| 132 | super(key); |
| 133 | checkArgument( |
| 134 | key == VERSION_CODE, |
| 135 | String.format( |
| 136 | "Key %s cannot be used with LongAtomicFormula", keyToString(key))); |
| 137 | mValue = null; |
| 138 | mOperator = null; |
| 139 | } |
| 140 | |
| 141 | /** |
| 142 | * Constructs a new {@link LongAtomicFormula}. |
Song Pan | 097f65d | 2019-11-10 18:02:52 +0000 | [diff] [blame] | 143 | * |
| 144 | * <p>This formula will hold if and only if the corresponding information of an install |
| 145 | * specified by {@code key} is of the correct relationship to {@code value} as specified by |
| 146 | * {@code operator}. |
| 147 | * |
Omer Nebil Yaveroglu | 15395f5 | 2020-01-22 12:14:44 +0000 | [diff] [blame] | 148 | * @throws IllegalArgumentException if {@code key} cannot be used with long value |
Song Pan | 097f65d | 2019-11-10 18:02:52 +0000 | [diff] [blame] | 149 | */ |
Omer Nebil Yaveroglu | 15395f5 | 2020-01-22 12:14:44 +0000 | [diff] [blame] | 150 | public LongAtomicFormula(@Key int key, @Operator int operator, long value) { |
Song Pan | 097f65d | 2019-11-10 18:02:52 +0000 | [diff] [blame] | 151 | super(key); |
| 152 | checkArgument( |
| 153 | key == VERSION_CODE, |
Omer Nebil Yaveroglu | 15395f5 | 2020-01-22 12:14:44 +0000 | [diff] [blame] | 154 | String.format( |
| 155 | "Key %s cannot be used with LongAtomicFormula", keyToString(key))); |
| 156 | checkArgument( |
| 157 | isValidOperator(operator), String.format("Unknown operator: %d", operator)); |
Song Pan | 097f65d | 2019-11-10 18:02:52 +0000 | [diff] [blame] | 158 | mOperator = operator; |
| 159 | mValue = value; |
| 160 | } |
| 161 | |
Omer Nebil Yaveroglu | 15395f5 | 2020-01-22 12:14:44 +0000 | [diff] [blame] | 162 | LongAtomicFormula(Parcel in) { |
Song Pan | 097f65d | 2019-11-10 18:02:52 +0000 | [diff] [blame] | 163 | super(in.readInt()); |
Omer Nebil Yaveroglu | 15395f5 | 2020-01-22 12:14:44 +0000 | [diff] [blame] | 164 | mValue = in.readLong(); |
Song Pan | 097f65d | 2019-11-10 18:02:52 +0000 | [diff] [blame] | 165 | mOperator = in.readInt(); |
| 166 | } |
| 167 | |
| 168 | @NonNull |
Omer Nebil Yaveroglu | 15395f5 | 2020-01-22 12:14:44 +0000 | [diff] [blame] | 169 | public static final Creator<LongAtomicFormula> CREATOR = |
| 170 | new Creator<LongAtomicFormula>() { |
Song Pan | 097f65d | 2019-11-10 18:02:52 +0000 | [diff] [blame] | 171 | @Override |
Omer Nebil Yaveroglu | 15395f5 | 2020-01-22 12:14:44 +0000 | [diff] [blame] | 172 | public LongAtomicFormula createFromParcel(Parcel in) { |
| 173 | return new LongAtomicFormula(in); |
Song Pan | 097f65d | 2019-11-10 18:02:52 +0000 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | @Override |
Omer Nebil Yaveroglu | 15395f5 | 2020-01-22 12:14:44 +0000 | [diff] [blame] | 177 | public LongAtomicFormula[] newArray(int size) { |
| 178 | return new LongAtomicFormula[size]; |
Song Pan | 097f65d | 2019-11-10 18:02:52 +0000 | [diff] [blame] | 179 | } |
| 180 | }; |
| 181 | |
| 182 | @Override |
Omer Nebil Yaveroglu | 15395f5 | 2020-01-22 12:14:44 +0000 | [diff] [blame] | 183 | public int getTag() { |
| 184 | return IntegrityFormula.LONG_ATOMIC_FORMULA_TAG; |
| 185 | } |
| 186 | |
| 187 | @Override |
| 188 | public boolean matches(AppInstallMetadata appInstallMetadata) { |
| 189 | if (mValue == null || mOperator == null) { |
| 190 | return false; |
| 191 | } |
| 192 | |
| 193 | long metadataValue = getLongMetadataValue(appInstallMetadata, getKey()); |
Song Pan | 097f65d | 2019-11-10 18:02:52 +0000 | [diff] [blame] | 194 | switch (mOperator) { |
| 195 | case EQ: |
| 196 | return metadataValue == mValue; |
Song Pan | 097f65d | 2019-11-10 18:02:52 +0000 | [diff] [blame] | 197 | case GT: |
| 198 | return metadataValue > mValue; |
Omer Nebil Yaveroglu | 15395f5 | 2020-01-22 12:14:44 +0000 | [diff] [blame] | 199 | case GTE: |
| 200 | return metadataValue >= mValue; |
Song Pan | 097f65d | 2019-11-10 18:02:52 +0000 | [diff] [blame] | 201 | default: |
Omer Nebil Yaveroglu | 15395f5 | 2020-01-22 12:14:44 +0000 | [diff] [blame] | 202 | throw new IllegalArgumentException( |
| 203 | String.format("Unexpected operator %d", mOperator)); |
Song Pan | 097f65d | 2019-11-10 18:02:52 +0000 | [diff] [blame] | 204 | } |
| 205 | } |
| 206 | |
| 207 | @Override |
Omer Nebil Yaveroglu | 84f7c3f | 2020-01-29 12:18:10 +0000 | [diff] [blame] | 208 | public boolean isAppCertificateFormula() { |
| 209 | return false; |
| 210 | } |
| 211 | |
| 212 | @Override |
| 213 | public boolean isInstallerFormula() { |
| 214 | return false; |
| 215 | } |
| 216 | |
| 217 | @Override |
Song Pan | 097f65d | 2019-11-10 18:02:52 +0000 | [diff] [blame] | 218 | public String toString() { |
Omer Nebil Yaveroglu | 15395f5 | 2020-01-22 12:14:44 +0000 | [diff] [blame] | 219 | if (mValue == null || mOperator == null) { |
| 220 | return String.format("(%s)", keyToString(getKey())); |
| 221 | } |
Song Pan | 097f65d | 2019-11-10 18:02:52 +0000 | [diff] [blame] | 222 | return String.format( |
| 223 | "(%s %s %s)", keyToString(getKey()), operatorToString(mOperator), mValue); |
| 224 | } |
| 225 | |
| 226 | @Override |
| 227 | public boolean equals(Object o) { |
| 228 | if (this == o) { |
| 229 | return true; |
| 230 | } |
| 231 | if (o == null || getClass() != o.getClass()) { |
| 232 | return false; |
| 233 | } |
Omer Nebil Yaveroglu | 15395f5 | 2020-01-22 12:14:44 +0000 | [diff] [blame] | 234 | LongAtomicFormula that = (LongAtomicFormula) o; |
Song Pan | 44e5aa5 | 2019-11-26 11:38:17 +0000 | [diff] [blame] | 235 | return getKey() == that.getKey() |
| 236 | && mValue == that.mValue |
| 237 | && mOperator == that.mOperator; |
Song Pan | 097f65d | 2019-11-10 18:02:52 +0000 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | @Override |
| 241 | public int hashCode() { |
| 242 | return Objects.hash(getKey(), mOperator, mValue); |
| 243 | } |
| 244 | |
| 245 | @Override |
| 246 | public int describeContents() { |
| 247 | return 0; |
| 248 | } |
| 249 | |
| 250 | @Override |
| 251 | public void writeToParcel(@NonNull Parcel dest, int flags) { |
Omer Nebil Yaveroglu | 15395f5 | 2020-01-22 12:14:44 +0000 | [diff] [blame] | 252 | if (mValue == null || mOperator == null) { |
| 253 | throw new IllegalStateException("Cannot write an empty LongAtomicFormula."); |
| 254 | } |
Song Pan | 097f65d | 2019-11-10 18:02:52 +0000 | [diff] [blame] | 255 | dest.writeInt(getKey()); |
Omer Nebil Yaveroglu | 15395f5 | 2020-01-22 12:14:44 +0000 | [diff] [blame] | 256 | dest.writeLong(mValue); |
Song Pan | 097f65d | 2019-11-10 18:02:52 +0000 | [diff] [blame] | 257 | dest.writeInt(mOperator); |
| 258 | } |
| 259 | |
Omer Nebil Yaveroglu | 15395f5 | 2020-01-22 12:14:44 +0000 | [diff] [blame] | 260 | public Long getValue() { |
Khaled Abdelmohsen | 0a181cd | 2019-11-12 12:24:38 +0000 | [diff] [blame] | 261 | return mValue; |
| 262 | } |
| 263 | |
Omer Nebil Yaveroglu | 15395f5 | 2020-01-22 12:14:44 +0000 | [diff] [blame] | 264 | public Integer getOperator() { |
Khaled Abdelmohsen | 0a181cd | 2019-11-12 12:24:38 +0000 | [diff] [blame] | 265 | return mOperator; |
| 266 | } |
| 267 | |
Khaled Abdelmohsen | 5809a5f | 2019-11-25 18:20:46 +0000 | [diff] [blame] | 268 | private static boolean isValidOperator(int operator) { |
| 269 | return operator == EQ |
Khaled Abdelmohsen | 5809a5f | 2019-11-25 18:20:46 +0000 | [diff] [blame] | 270 | || operator == GT |
Omer Nebil Yaveroglu | 15395f5 | 2020-01-22 12:14:44 +0000 | [diff] [blame] | 271 | || operator == GTE; |
| 272 | } |
| 273 | |
| 274 | private static long getLongMetadataValue(AppInstallMetadata appInstallMetadata, int key) { |
| 275 | switch (key) { |
| 276 | case AtomicFormula.VERSION_CODE: |
| 277 | return appInstallMetadata.getVersionCode(); |
| 278 | default: |
| 279 | throw new IllegalStateException("Unexpected key in IntAtomicFormula" + key); |
| 280 | } |
Khaled Abdelmohsen | 5809a5f | 2019-11-25 18:20:46 +0000 | [diff] [blame] | 281 | } |
Khaled Abdelmohsen | e959bae | 2019-10-04 17:01:13 +0100 | [diff] [blame] | 282 | } |
| 283 | |
Song Pan | 097f65d | 2019-11-10 18:02:52 +0000 | [diff] [blame] | 284 | /** An {@link AtomicFormula} with a key and string value. */ |
| 285 | public static final class StringAtomicFormula extends AtomicFormula implements Parcelable { |
| 286 | private final String mValue; |
Khaled Abdelmohsen | 1efff87 | 2019-11-25 16:44:20 +0000 | [diff] [blame] | 287 | // Indicates whether the value is the actual value or the hashed value. |
Omer Nebil Yaveroglu | 15395f5 | 2020-01-22 12:14:44 +0000 | [diff] [blame] | 288 | private final Boolean mIsHashedValue; |
| 289 | |
| 290 | /** |
| 291 | * Constructs an empty {@link StringAtomicFormula}. This should only be used as a base. |
| 292 | * |
| 293 | * <p>An empty formula will always match to false. |
| 294 | * |
| 295 | * @throws IllegalArgumentException if {@code key} cannot be used with string value |
| 296 | */ |
| 297 | public StringAtomicFormula(@Key int key) { |
| 298 | super(key); |
| 299 | checkArgument( |
| 300 | key == PACKAGE_NAME |
| 301 | || key == APP_CERTIFICATE |
| 302 | || key == INSTALLER_CERTIFICATE |
| 303 | || key == INSTALLER_NAME, |
| 304 | String.format( |
| 305 | "Key %s cannot be used with StringAtomicFormula", keyToString(key))); |
| 306 | mValue = null; |
| 307 | mIsHashedValue = null; |
| 308 | } |
Khaled Abdelmohsen | e959bae | 2019-10-04 17:01:13 +0100 | [diff] [blame] | 309 | |
Song Pan | 097f65d | 2019-11-10 18:02:52 +0000 | [diff] [blame] | 310 | /** |
| 311 | * Constructs a new {@link StringAtomicFormula}. |
| 312 | * |
| 313 | * <p>This formula will hold if and only if the corresponding information of an install |
| 314 | * specified by {@code key} equals {@code value}. |
| 315 | * |
Song Pan | 44e5aa5 | 2019-11-26 11:38:17 +0000 | [diff] [blame] | 316 | * @throws IllegalArgumentException if {@code key} cannot be used with string value |
Song Pan | 097f65d | 2019-11-10 18:02:52 +0000 | [diff] [blame] | 317 | */ |
Omer Nebil Yaveroglu | 15395f5 | 2020-01-22 12:14:44 +0000 | [diff] [blame] | 318 | public StringAtomicFormula(@Key int key, @NonNull String value, boolean isHashed) { |
Song Pan | 097f65d | 2019-11-10 18:02:52 +0000 | [diff] [blame] | 319 | super(key); |
| 320 | checkArgument( |
| 321 | key == PACKAGE_NAME |
| 322 | || key == APP_CERTIFICATE |
| 323 | || key == INSTALLER_CERTIFICATE |
| 324 | || key == INSTALLER_NAME, |
| 325 | String.format( |
| 326 | "Key %s cannot be used with StringAtomicFormula", keyToString(key))); |
| 327 | mValue = value; |
Omer Nebil Yaveroglu | 15395f5 | 2020-01-22 12:14:44 +0000 | [diff] [blame] | 328 | mIsHashedValue = isHashed; |
| 329 | } |
| 330 | |
| 331 | /** |
| 332 | * Constructs a new {@link StringAtomicFormula} together with handling the necessary |
| 333 | * hashing for the given key. |
| 334 | * |
| 335 | * <p> The value will be hashed with SHA256 and the hex digest will be computed; for |
| 336 | * all cases except when the key is PACKAGE_NAME or INSTALLER_NAME and the value |
| 337 | * is less than 33 characters. |
| 338 | * |
| 339 | * @throws IllegalArgumentException if {@code key} cannot be used with string value. |
| 340 | */ |
| 341 | public StringAtomicFormula(@Key int key, @NonNull String value) { |
| 342 | super(key); |
| 343 | checkArgument( |
| 344 | key == PACKAGE_NAME |
| 345 | || key == APP_CERTIFICATE |
| 346 | || key == INSTALLER_CERTIFICATE |
| 347 | || key == INSTALLER_NAME, |
| 348 | String.format( |
| 349 | "Key %s cannot be used with StringAtomicFormula", keyToString(key))); |
| 350 | mValue = hashValue(key, value); |
| 351 | mIsHashedValue = !mValue.equals(value); |
Song Pan | 097f65d | 2019-11-10 18:02:52 +0000 | [diff] [blame] | 352 | } |
Khaled Abdelmohsen | e959bae | 2019-10-04 17:01:13 +0100 | [diff] [blame] | 353 | |
Song Pan | 097f65d | 2019-11-10 18:02:52 +0000 | [diff] [blame] | 354 | StringAtomicFormula(Parcel in) { |
| 355 | super(in.readInt()); |
| 356 | mValue = in.readStringNoHelper(); |
Khaled Abdelmohsen | 1efff87 | 2019-11-25 16:44:20 +0000 | [diff] [blame] | 357 | mIsHashedValue = in.readByte() != 0; |
Song Pan | 097f65d | 2019-11-10 18:02:52 +0000 | [diff] [blame] | 358 | } |
| 359 | |
| 360 | @NonNull |
| 361 | public static final Creator<StringAtomicFormula> CREATOR = |
| 362 | new Creator<StringAtomicFormula>() { |
| 363 | @Override |
| 364 | public StringAtomicFormula createFromParcel(Parcel in) { |
| 365 | return new StringAtomicFormula(in); |
| 366 | } |
| 367 | |
| 368 | @Override |
| 369 | public StringAtomicFormula[] newArray(int size) { |
| 370 | return new StringAtomicFormula[size]; |
| 371 | } |
| 372 | }; |
| 373 | |
| 374 | @Override |
Omer Nebil Yaveroglu | 15395f5 | 2020-01-22 12:14:44 +0000 | [diff] [blame] | 375 | public int getTag() { |
| 376 | return IntegrityFormula.STRING_ATOMIC_FORMULA_TAG; |
Song Pan | 097f65d | 2019-11-10 18:02:52 +0000 | [diff] [blame] | 377 | } |
| 378 | |
| 379 | @Override |
Omer Nebil Yaveroglu | 15395f5 | 2020-01-22 12:14:44 +0000 | [diff] [blame] | 380 | public boolean matches(AppInstallMetadata appInstallMetadata) { |
| 381 | if (mValue == null || mIsHashedValue == null) { |
| 382 | return false; |
| 383 | } |
| 384 | return getStringMetadataValue(appInstallMetadata, getKey()).equals(mValue); |
Song Pan | 44e5aa5 | 2019-11-26 11:38:17 +0000 | [diff] [blame] | 385 | } |
| 386 | |
| 387 | @Override |
Omer Nebil Yaveroglu | 84f7c3f | 2020-01-29 12:18:10 +0000 | [diff] [blame] | 388 | public boolean isAppCertificateFormula() { |
| 389 | return getKey() == APP_CERTIFICATE; |
| 390 | } |
| 391 | |
| 392 | @Override |
| 393 | public boolean isInstallerFormula() { |
| 394 | return getKey() == INSTALLER_NAME || getKey() == INSTALLER_CERTIFICATE; |
| 395 | } |
| 396 | |
| 397 | @Override |
Song Pan | 097f65d | 2019-11-10 18:02:52 +0000 | [diff] [blame] | 398 | public String toString() { |
Omer Nebil Yaveroglu | 15395f5 | 2020-01-22 12:14:44 +0000 | [diff] [blame] | 399 | if (mValue == null || mIsHashedValue == null) { |
| 400 | return String.format("(%s)", keyToString(getKey())); |
| 401 | } |
Song Pan | 097f65d | 2019-11-10 18:02:52 +0000 | [diff] [blame] | 402 | return String.format("(%s %s %s)", keyToString(getKey()), operatorToString(EQ), mValue); |
| 403 | } |
| 404 | |
| 405 | @Override |
| 406 | public boolean equals(Object o) { |
| 407 | if (this == o) { |
| 408 | return true; |
| 409 | } |
| 410 | if (o == null || getClass() != o.getClass()) { |
| 411 | return false; |
| 412 | } |
| 413 | StringAtomicFormula that = (StringAtomicFormula) o; |
| 414 | return getKey() == that.getKey() && Objects.equals(mValue, that.mValue); |
| 415 | } |
| 416 | |
| 417 | @Override |
| 418 | public int hashCode() { |
| 419 | return Objects.hash(getKey(), mValue); |
| 420 | } |
| 421 | |
| 422 | @Override |
| 423 | public int describeContents() { |
| 424 | return 0; |
| 425 | } |
| 426 | |
| 427 | @Override |
| 428 | public void writeToParcel(@NonNull Parcel dest, int flags) { |
Omer Nebil Yaveroglu | 15395f5 | 2020-01-22 12:14:44 +0000 | [diff] [blame] | 429 | if (mValue == null || mIsHashedValue == null) { |
| 430 | throw new IllegalStateException("Cannot write an empty StringAtomicFormula."); |
| 431 | } |
Song Pan | 097f65d | 2019-11-10 18:02:52 +0000 | [diff] [blame] | 432 | dest.writeInt(getKey()); |
| 433 | dest.writeStringNoHelper(mValue); |
Khaled Abdelmohsen | 1efff87 | 2019-11-25 16:44:20 +0000 | [diff] [blame] | 434 | dest.writeByte((byte) (mIsHashedValue ? 1 : 0)); |
Song Pan | 097f65d | 2019-11-10 18:02:52 +0000 | [diff] [blame] | 435 | } |
| 436 | |
Khaled Abdelmohsen | 0a181cd | 2019-11-12 12:24:38 +0000 | [diff] [blame] | 437 | public String getValue() { |
| 438 | return mValue; |
| 439 | } |
| 440 | |
Omer Nebil Yaveroglu | 15395f5 | 2020-01-22 12:14:44 +0000 | [diff] [blame] | 441 | public Boolean getIsHashedValue() { |
Khaled Abdelmohsen | 1efff87 | 2019-11-25 16:44:20 +0000 | [diff] [blame] | 442 | return mIsHashedValue; |
| 443 | } |
| 444 | |
Omer Nebil Yaveroglu | 15395f5 | 2020-01-22 12:14:44 +0000 | [diff] [blame] | 445 | private static String getStringMetadataValue( |
| 446 | AppInstallMetadata appInstallMetadata, int key) { |
| 447 | switch (key) { |
| 448 | case AtomicFormula.PACKAGE_NAME: |
Song Pan | 097f65d | 2019-11-10 18:02:52 +0000 | [diff] [blame] | 449 | return appInstallMetadata.getPackageName(); |
Omer Nebil Yaveroglu | 15395f5 | 2020-01-22 12:14:44 +0000 | [diff] [blame] | 450 | case AtomicFormula.APP_CERTIFICATE: |
Song Pan | 097f65d | 2019-11-10 18:02:52 +0000 | [diff] [blame] | 451 | return appInstallMetadata.getAppCertificate(); |
Omer Nebil Yaveroglu | 15395f5 | 2020-01-22 12:14:44 +0000 | [diff] [blame] | 452 | case AtomicFormula.INSTALLER_CERTIFICATE: |
Song Pan | 097f65d | 2019-11-10 18:02:52 +0000 | [diff] [blame] | 453 | return appInstallMetadata.getInstallerCertificate(); |
Omer Nebil Yaveroglu | 15395f5 | 2020-01-22 12:14:44 +0000 | [diff] [blame] | 454 | case AtomicFormula.INSTALLER_NAME: |
Song Pan | 097f65d | 2019-11-10 18:02:52 +0000 | [diff] [blame] | 455 | return appInstallMetadata.getInstallerName(); |
| 456 | default: |
| 457 | throw new IllegalStateException( |
Omer Nebil Yaveroglu | 15395f5 | 2020-01-22 12:14:44 +0000 | [diff] [blame] | 458 | "Unexpected key in StringAtomicFormula: " + key); |
| 459 | } |
| 460 | } |
| 461 | |
| 462 | private static String hashValue(@Key int key, String value) { |
| 463 | // Hash the string value unless it is a PACKAGE_NAME or INSTALLER_NAME and the value is |
| 464 | // less than 33 characters. |
| 465 | if (value.length() <= 32) { |
| 466 | if (key == PACKAGE_NAME || key == INSTALLER_NAME) { |
| 467 | return value; |
| 468 | } |
| 469 | } |
| 470 | return hash(value); |
| 471 | } |
| 472 | |
| 473 | private static String hash(String value) { |
| 474 | try { |
| 475 | MessageDigest messageDigest = MessageDigest.getInstance("SHA-256"); |
| 476 | byte[] hashBytes = messageDigest.digest(value.getBytes(StandardCharsets.UTF_8)); |
| 477 | return IntegrityUtils.getHexDigest(hashBytes); |
| 478 | } catch (NoSuchAlgorithmException e) { |
| 479 | throw new RuntimeException("SHA-256 algorithm not found", e); |
Song Pan | 097f65d | 2019-11-10 18:02:52 +0000 | [diff] [blame] | 480 | } |
| 481 | } |
Khaled Abdelmohsen | e959bae | 2019-10-04 17:01:13 +0100 | [diff] [blame] | 482 | } |
| 483 | |
Song Pan | 097f65d | 2019-11-10 18:02:52 +0000 | [diff] [blame] | 484 | /** An {@link AtomicFormula} with a key and boolean value. */ |
| 485 | public static final class BooleanAtomicFormula extends AtomicFormula implements Parcelable { |
Omer Nebil Yaveroglu | 15395f5 | 2020-01-22 12:14:44 +0000 | [diff] [blame] | 486 | private final Boolean mValue; |
| 487 | |
| 488 | /** |
| 489 | * Constructs an empty {@link BooleanAtomicFormula}. This should only be used as a base. |
| 490 | * |
| 491 | * <p>An empty formula will always match to false. |
| 492 | * |
| 493 | * @throws IllegalArgumentException if {@code key} cannot be used with boolean value |
| 494 | */ |
| 495 | public BooleanAtomicFormula(@Key int key) { |
| 496 | super(key); |
| 497 | checkArgument( |
| 498 | key == PRE_INSTALLED, |
| 499 | String.format( |
| 500 | "Key %s cannot be used with BooleanAtomicFormula", keyToString(key))); |
| 501 | mValue = null; |
| 502 | } |
Song Pan | 097f65d | 2019-11-10 18:02:52 +0000 | [diff] [blame] | 503 | |
| 504 | /** |
| 505 | * Constructs a new {@link BooleanAtomicFormula}. |
| 506 | * |
| 507 | * <p>This formula will hold if and only if the corresponding information of an install |
| 508 | * specified by {@code key} equals {@code value}. |
| 509 | * |
Song Pan | 44e5aa5 | 2019-11-26 11:38:17 +0000 | [diff] [blame] | 510 | * @throws IllegalArgumentException if {@code key} cannot be used with boolean value |
Song Pan | 097f65d | 2019-11-10 18:02:52 +0000 | [diff] [blame] | 511 | */ |
| 512 | public BooleanAtomicFormula(@Key int key, boolean value) { |
| 513 | super(key); |
| 514 | checkArgument( |
| 515 | key == PRE_INSTALLED, |
| 516 | String.format( |
| 517 | "Key %s cannot be used with BooleanAtomicFormula", keyToString(key))); |
| 518 | mValue = value; |
| 519 | } |
| 520 | |
| 521 | BooleanAtomicFormula(Parcel in) { |
| 522 | super(in.readInt()); |
| 523 | mValue = in.readByte() != 0; |
| 524 | } |
| 525 | |
| 526 | @NonNull |
| 527 | public static final Creator<BooleanAtomicFormula> CREATOR = |
| 528 | new Creator<BooleanAtomicFormula>() { |
| 529 | @Override |
| 530 | public BooleanAtomicFormula createFromParcel(Parcel in) { |
| 531 | return new BooleanAtomicFormula(in); |
| 532 | } |
| 533 | |
| 534 | @Override |
| 535 | public BooleanAtomicFormula[] newArray(int size) { |
| 536 | return new BooleanAtomicFormula[size]; |
| 537 | } |
| 538 | }; |
| 539 | |
| 540 | @Override |
Omer Nebil Yaveroglu | 15395f5 | 2020-01-22 12:14:44 +0000 | [diff] [blame] | 541 | public int getTag() { |
| 542 | return IntegrityFormula.BOOLEAN_ATOMIC_FORMULA_TAG; |
Song Pan | 097f65d | 2019-11-10 18:02:52 +0000 | [diff] [blame] | 543 | } |
| 544 | |
| 545 | @Override |
Omer Nebil Yaveroglu | 15395f5 | 2020-01-22 12:14:44 +0000 | [diff] [blame] | 546 | public boolean matches(AppInstallMetadata appInstallMetadata) { |
| 547 | if (mValue == null) { |
| 548 | return false; |
| 549 | } |
| 550 | return getBooleanMetadataValue(appInstallMetadata, getKey()) == mValue; |
Song Pan | 44e5aa5 | 2019-11-26 11:38:17 +0000 | [diff] [blame] | 551 | } |
| 552 | |
| 553 | @Override |
Omer Nebil Yaveroglu | 84f7c3f | 2020-01-29 12:18:10 +0000 | [diff] [blame] | 554 | public boolean isAppCertificateFormula() { |
| 555 | return false; |
| 556 | } |
| 557 | |
| 558 | @Override |
| 559 | public boolean isInstallerFormula() { |
| 560 | return false; |
| 561 | } |
| 562 | |
| 563 | @Override |
Song Pan | 097f65d | 2019-11-10 18:02:52 +0000 | [diff] [blame] | 564 | public String toString() { |
Omer Nebil Yaveroglu | 15395f5 | 2020-01-22 12:14:44 +0000 | [diff] [blame] | 565 | if (mValue == null) { |
| 566 | return String.format("(%s)", keyToString(getKey())); |
| 567 | } |
Song Pan | 097f65d | 2019-11-10 18:02:52 +0000 | [diff] [blame] | 568 | return String.format("(%s %s %s)", keyToString(getKey()), operatorToString(EQ), mValue); |
| 569 | } |
| 570 | |
| 571 | @Override |
| 572 | public boolean equals(Object o) { |
| 573 | if (this == o) { |
| 574 | return true; |
| 575 | } |
| 576 | if (o == null || getClass() != o.getClass()) { |
| 577 | return false; |
| 578 | } |
| 579 | BooleanAtomicFormula that = (BooleanAtomicFormula) o; |
| 580 | return getKey() == that.getKey() && mValue == that.mValue; |
| 581 | } |
| 582 | |
| 583 | @Override |
| 584 | public int hashCode() { |
| 585 | return Objects.hash(getKey(), mValue); |
| 586 | } |
| 587 | |
| 588 | @Override |
| 589 | public int describeContents() { |
| 590 | return 0; |
| 591 | } |
| 592 | |
| 593 | @Override |
| 594 | public void writeToParcel(@NonNull Parcel dest, int flags) { |
Omer Nebil Yaveroglu | 15395f5 | 2020-01-22 12:14:44 +0000 | [diff] [blame] | 595 | if (mValue == null) { |
| 596 | throw new IllegalStateException("Cannot write an empty BooleanAtomicFormula."); |
| 597 | } |
Song Pan | 097f65d | 2019-11-10 18:02:52 +0000 | [diff] [blame] | 598 | dest.writeInt(getKey()); |
| 599 | dest.writeByte((byte) (mValue ? 1 : 0)); |
| 600 | } |
| 601 | |
Omer Nebil Yaveroglu | 15395f5 | 2020-01-22 12:14:44 +0000 | [diff] [blame] | 602 | public Boolean getValue() { |
Khaled Abdelmohsen | 0a181cd | 2019-11-12 12:24:38 +0000 | [diff] [blame] | 603 | return mValue; |
| 604 | } |
| 605 | |
Omer Nebil Yaveroglu | 15395f5 | 2020-01-22 12:14:44 +0000 | [diff] [blame] | 606 | private static boolean getBooleanMetadataValue( |
| 607 | AppInstallMetadata appInstallMetadata, int key) { |
| 608 | switch (key) { |
| 609 | case AtomicFormula.PRE_INSTALLED: |
Song Pan | 097f65d | 2019-11-10 18:02:52 +0000 | [diff] [blame] | 610 | return appInstallMetadata.isPreInstalled(); |
| 611 | default: |
| 612 | throw new IllegalStateException( |
Omer Nebil Yaveroglu | 15395f5 | 2020-01-22 12:14:44 +0000 | [diff] [blame] | 613 | "Unexpected key in BooleanAtomicFormula: " + key); |
Song Pan | 097f65d | 2019-11-10 18:02:52 +0000 | [diff] [blame] | 614 | } |
| 615 | } |
Khaled Abdelmohsen | e959bae | 2019-10-04 17:01:13 +0100 | [diff] [blame] | 616 | } |
| 617 | |
Song Pan | 097f65d | 2019-11-10 18:02:52 +0000 | [diff] [blame] | 618 | public int getKey() { |
Khaled Abdelmohsen | e959bae | 2019-10-04 17:01:13 +0100 | [diff] [blame] | 619 | return mKey; |
| 620 | } |
| 621 | |
Song Pan | 44e5aa5 | 2019-11-26 11:38:17 +0000 | [diff] [blame] | 622 | static String keyToString(int key) { |
Khaled Abdelmohsen | be0454d | 2019-10-07 10:24:18 +0100 | [diff] [blame] | 623 | switch (key) { |
| 624 | case PACKAGE_NAME: |
Song Pan | 097f65d | 2019-11-10 18:02:52 +0000 | [diff] [blame] | 625 | return "PACKAGE_NAME"; |
Khaled Abdelmohsen | be0454d | 2019-10-07 10:24:18 +0100 | [diff] [blame] | 626 | case APP_CERTIFICATE: |
Song Pan | 097f65d | 2019-11-10 18:02:52 +0000 | [diff] [blame] | 627 | return "APP_CERTIFICATE"; |
Khaled Abdelmohsen | be0454d | 2019-10-07 10:24:18 +0100 | [diff] [blame] | 628 | case VERSION_CODE: |
Song Pan | 097f65d | 2019-11-10 18:02:52 +0000 | [diff] [blame] | 629 | return "VERSION_CODE"; |
| 630 | case INSTALLER_NAME: |
| 631 | return "INSTALLER_NAME"; |
| 632 | case INSTALLER_CERTIFICATE: |
| 633 | return "INSTALLER_CERTIFICATE"; |
| 634 | case PRE_INSTALLED: |
| 635 | return "PRE_INSTALLED"; |
Khaled Abdelmohsen | be0454d | 2019-10-07 10:24:18 +0100 | [diff] [blame] | 636 | default: |
Song Pan | 097f65d | 2019-11-10 18:02:52 +0000 | [diff] [blame] | 637 | throw new IllegalArgumentException("Unknown key " + key); |
Khaled Abdelmohsen | be0454d | 2019-10-07 10:24:18 +0100 | [diff] [blame] | 638 | } |
Song Pan | 097f65d | 2019-11-10 18:02:52 +0000 | [diff] [blame] | 639 | } |
| 640 | |
Song Pan | 44e5aa5 | 2019-11-26 11:38:17 +0000 | [diff] [blame] | 641 | static String operatorToString(int op) { |
Song Pan | 097f65d | 2019-11-10 18:02:52 +0000 | [diff] [blame] | 642 | switch (op) { |
| 643 | case EQ: |
| 644 | return "EQ"; |
Song Pan | 097f65d | 2019-11-10 18:02:52 +0000 | [diff] [blame] | 645 | case GT: |
| 646 | return "GT"; |
Omer Nebil Yaveroglu | 15395f5 | 2020-01-22 12:14:44 +0000 | [diff] [blame] | 647 | case GTE: |
| 648 | return "GTE"; |
Song Pan | 097f65d | 2019-11-10 18:02:52 +0000 | [diff] [blame] | 649 | default: |
| 650 | throw new IllegalArgumentException("Unknown operator " + op); |
Khaled Abdelmohsen | be0454d | 2019-10-07 10:24:18 +0100 | [diff] [blame] | 651 | } |
| 652 | } |
Khaled Abdelmohsen | 5809a5f | 2019-11-25 18:20:46 +0000 | [diff] [blame] | 653 | |
| 654 | private static boolean isValidKey(int key) { |
| 655 | return key == PACKAGE_NAME |
| 656 | || key == APP_CERTIFICATE |
| 657 | || key == VERSION_CODE |
| 658 | || key == INSTALLER_NAME |
| 659 | || key == INSTALLER_CERTIFICATE |
| 660 | || key == PRE_INSTALLED; |
| 661 | } |
Khaled Abdelmohsen | e959bae | 2019-10-04 17:01:13 +0100 | [diff] [blame] | 662 | } |