Khaled Abdelmohsen | 5ada598 | 2019-10-01 11:17:51 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2019 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 | |
Khaled Abdelmohsen | 3101269 | 2019-10-02 17:54:47 +0100 | [diff] [blame] | 17 | package com.android.server.integrity.model; |
Khaled Abdelmohsen | 5ada598 | 2019-10-01 11:17:51 +0100 | [diff] [blame] | 18 | |
Khaled Abdelmohsen | 2187730 | 2019-10-07 14:56:34 +0100 | [diff] [blame] | 19 | import static com.android.internal.util.Preconditions.checkArgument; |
| 20 | import static com.android.internal.util.Preconditions.checkNotNull; |
| 21 | |
| 22 | import android.annotation.Nullable; |
| 23 | |
Khaled Abdelmohsen | 5ada598 | 2019-10-01 11:17:51 +0100 | [diff] [blame] | 24 | /** |
| 25 | * The app install metadata. |
| 26 | * |
| 27 | * <p>The integrity component retrieves metadata for app installs from package manager, passing it |
| 28 | * to the rule evaluation engine to evaluate the metadata against the rules. |
Khaled Abdelmohsen | 2187730 | 2019-10-07 14:56:34 +0100 | [diff] [blame] | 29 | * |
| 30 | * <p>Instances of this class are immutable. |
Khaled Abdelmohsen | 5ada598 | 2019-10-01 11:17:51 +0100 | [diff] [blame] | 31 | */ |
| 32 | public final class AppInstallMetadata { |
Khaled Abdelmohsen | 2187730 | 2019-10-07 14:56:34 +0100 | [diff] [blame] | 33 | private final String mPackageName; |
Khaled Abdelmohsen | 5ada598 | 2019-10-01 11:17:51 +0100 | [diff] [blame] | 34 | // Raw string encoding for the SHA-256 hash of the certificate of the app. |
Khaled Abdelmohsen | 2187730 | 2019-10-07 14:56:34 +0100 | [diff] [blame] | 35 | private final String mAppCertificate; |
| 36 | private final String mInstallerName; |
Khaled Abdelmohsen | 5ada598 | 2019-10-01 11:17:51 +0100 | [diff] [blame] | 37 | // Raw string encoding for the SHA-256 hash of the certificate of the installer. |
Khaled Abdelmohsen | 2187730 | 2019-10-07 14:56:34 +0100 | [diff] [blame] | 38 | private final String mInstallerCertificate; |
| 39 | private final int mVersionCode; |
| 40 | private final boolean mIsPreInstalled; |
Khaled Abdelmohsen | 5ada598 | 2019-10-01 11:17:51 +0100 | [diff] [blame] | 41 | |
Khaled Abdelmohsen | 2187730 | 2019-10-07 14:56:34 +0100 | [diff] [blame] | 42 | private AppInstallMetadata(Builder builder) { |
| 43 | this.mPackageName = builder.mPackageName; |
| 44 | this.mAppCertificate = builder.mAppCertificate; |
| 45 | this.mInstallerName = builder.mInstallerName; |
| 46 | this.mInstallerCertificate = builder.mInstallerCertificate; |
| 47 | this.mVersionCode = builder.mVersionCode; |
| 48 | this.mIsPreInstalled = builder.mIsPreInstalled; |
| 49 | } |
| 50 | |
| 51 | public String getPackageName() { |
| 52 | return mPackageName; |
| 53 | } |
| 54 | |
| 55 | public String getAppCertificate() { |
| 56 | return mAppCertificate; |
| 57 | } |
| 58 | |
| 59 | @Nullable |
| 60 | public String getInstallerName() { |
| 61 | return mInstallerName; |
| 62 | } |
| 63 | |
| 64 | @Nullable |
| 65 | public String getInstallerCertificate() { |
| 66 | return mInstallerCertificate; |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * @see AppInstallMetadata.Builder#setVersionCode(int) |
| 71 | */ |
| 72 | public int getVersionCode() { |
| 73 | return mVersionCode; |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * @see AppInstallMetadata.Builder#setIsPreInstalled(boolean) |
| 78 | */ |
| 79 | public boolean isPreInstalled() { |
| 80 | return mIsPreInstalled; |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Builder class for constructing {@link AppInstallMetadata} objects. |
| 85 | */ |
| 86 | public static final class Builder { |
| 87 | private String mPackageName; |
| 88 | private String mAppCertificate; |
| 89 | private String mInstallerName; |
| 90 | private String mInstallerCertificate; |
| 91 | private int mVersionCode; |
| 92 | private boolean mIsPreInstalled; |
| 93 | |
| 94 | /** |
| 95 | * Set package name of the app to be installed. |
| 96 | * |
| 97 | * @see AppInstallMetadata#getPackageName() |
| 98 | */ |
| 99 | public Builder setPackageName(String packageName) { |
| 100 | this.mPackageName = checkNotNull(packageName); |
| 101 | return this; |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * Set certificate of the app to be installed. |
| 106 | * |
| 107 | * <p>It is represented as the raw string encoding for the SHA-256 hash of the certificate |
| 108 | * of the app. |
| 109 | * |
| 110 | * @see AppInstallMetadata#getAppCertificate() |
| 111 | */ |
| 112 | public Builder setAppCertificate(String appCertificate) { |
| 113 | this.mAppCertificate = checkNotNull(appCertificate); |
| 114 | return this; |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * Set name of the installer installing the app. |
| 119 | * |
| 120 | * @see AppInstallMetadata#getInstallerName() |
| 121 | */ |
| 122 | public Builder setInstallerName(String installerName) { |
| 123 | this.mInstallerName = checkNotNull(installerName); |
| 124 | return this; |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * Set certificate of the installer installing the app. |
| 129 | * |
| 130 | * <p>It is represented as the raw string encoding for the SHA-256 hash of the certificate |
| 131 | * of the installer. |
| 132 | * |
| 133 | * @see AppInstallMetadata#getInstallerCertificate() |
| 134 | */ |
| 135 | public Builder setInstallerCertificate(String installerCertificate) { |
| 136 | this.mInstallerCertificate = checkNotNull(installerCertificate); |
| 137 | return this; |
| 138 | } |
| 139 | |
| 140 | /** |
| 141 | * Set version code of the app to be installed. |
| 142 | * |
| 143 | * @see AppInstallMetadata#getVersionCode() |
| 144 | */ |
| 145 | public Builder setVersionCode(int versionCode) { |
| 146 | this.mVersionCode = versionCode; |
| 147 | return this; |
| 148 | } |
| 149 | |
| 150 | /** |
| 151 | * Set whether the app is pre-installed on the device or not. |
| 152 | * |
| 153 | * @see AppInstallMetadata#isPreInstalled() |
| 154 | */ |
| 155 | public Builder setIsPreInstalled(boolean isPreInstalled) { |
| 156 | this.mIsPreInstalled = isPreInstalled; |
| 157 | return this; |
| 158 | } |
| 159 | |
| 160 | /** |
| 161 | * Build {@link AppInstallMetadata}. |
| 162 | */ |
| 163 | public AppInstallMetadata build() { |
| 164 | checkArgument(mPackageName != null); |
| 165 | checkArgument(mAppCertificate != null); |
| 166 | return new AppInstallMetadata(this); |
| 167 | } |
Khaled Abdelmohsen | 5ada598 | 2019-10-01 11:17:51 +0100 | [diff] [blame] | 168 | } |
| 169 | } |