blob: 660bd2e0d62e36de25605532d5fe7dbcc56134c8 [file] [log] [blame]
Khaled Abdelmohsen5ada5982019-10-01 11:17:51 +01001/*
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 Abdelmohsen31012692019-10-02 17:54:47 +010017package com.android.server.integrity.model;
Khaled Abdelmohsen5ada5982019-10-01 11:17:51 +010018
Khaled Abdelmohsen21877302019-10-07 14:56:34 +010019import static com.android.internal.util.Preconditions.checkArgument;
20import static com.android.internal.util.Preconditions.checkNotNull;
21
22import android.annotation.Nullable;
23
Khaled Abdelmohsen5ada5982019-10-01 11:17:51 +010024/**
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 Abdelmohsen21877302019-10-07 14:56:34 +010029 *
30 * <p>Instances of this class are immutable.
Khaled Abdelmohsen5ada5982019-10-01 11:17:51 +010031 */
32public final class AppInstallMetadata {
Khaled Abdelmohsen21877302019-10-07 14:56:34 +010033 private final String mPackageName;
Khaled Abdelmohsen5ada5982019-10-01 11:17:51 +010034 // Raw string encoding for the SHA-256 hash of the certificate of the app.
Khaled Abdelmohsen21877302019-10-07 14:56:34 +010035 private final String mAppCertificate;
36 private final String mInstallerName;
Khaled Abdelmohsen5ada5982019-10-01 11:17:51 +010037 // Raw string encoding for the SHA-256 hash of the certificate of the installer.
Khaled Abdelmohsen21877302019-10-07 14:56:34 +010038 private final String mInstallerCertificate;
39 private final int mVersionCode;
40 private final boolean mIsPreInstalled;
Khaled Abdelmohsen5ada5982019-10-01 11:17:51 +010041
Khaled Abdelmohsen21877302019-10-07 14:56:34 +010042 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 Abdelmohsen5ada5982019-10-01 11:17:51 +0100168 }
169}