blob: 6639b3d5c0b54e441cc8ded17c520bcb5dab422c [file] [log] [blame]
Winson14ff7172019-10-23 10:42:27 -07001/*
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
17package android.content.pm.parsing;
18
19import static android.os.Trace.TRACE_TAG_PACKAGE_MANAGER;
20
Artur Satayev2ebb31c2020-01-08 12:24:36 +000021import android.compat.annotation.UnsupportedAppUsage;
Winson14ff7172019-10-23 10:42:27 -070022import android.content.pm.PackageInfo;
23import android.content.pm.PackageManager;
24import android.content.pm.PackageParser;
25import android.content.pm.VerifierInfo;
26import android.content.res.ApkAssets;
27import android.content.res.XmlResourceParser;
28import android.os.Trace;
29import android.util.ArrayMap;
30import android.util.AttributeSet;
31import android.util.Pair;
32import android.util.Slog;
33
34import com.android.internal.R;
35import com.android.internal.util.ArrayUtils;
36
37import libcore.io.IoUtils;
38
39import org.xmlpull.v1.XmlPullParser;
40import org.xmlpull.v1.XmlPullParserException;
41
42import java.io.File;
43import java.io.FileDescriptor;
44import java.io.IOException;
45import java.security.PublicKey;
46import java.util.ArrayList;
47import java.util.Arrays;
48import java.util.List;
49
50/** @hide */
51public class ApkLiteParseUtils {
52
53 private static final String TAG = ApkParseUtils.TAG;
54
55 // TODO(b/135203078): Consolidate constants
56 private static final int DEFAULT_MIN_SDK_VERSION = 1;
57 private static final int DEFAULT_TARGET_SDK_VERSION = 0;
58
59 private static final int PARSE_DEFAULT_INSTALL_LOCATION =
60 PackageInfo.INSTALL_LOCATION_UNSPECIFIED;
61
62 /**
63 * Parse only lightweight details about the package at the given location.
64 * Automatically detects if the package is a monolithic style (single APK
65 * file) or cluster style (directory of APKs).
66 * <p>
67 * This performs sanity checking on cluster style packages, such as
68 * requiring identical package name and version codes, a single base APK,
69 * and unique split names.
70 *
71 * @see PackageParser#parsePackage(File, int)
72 */
73 @UnsupportedAppUsage
74 public static PackageParser.PackageLite parsePackageLite(File packageFile, int flags)
75 throws PackageParser.PackageParserException {
76 if (packageFile.isDirectory()) {
77 return parseClusterPackageLite(packageFile, flags);
78 } else {
79 return parseMonolithicPackageLite(packageFile, flags);
80 }
81 }
82
83 public static PackageParser.PackageLite parseMonolithicPackageLite(File packageFile, int flags)
84 throws PackageParser.PackageParserException {
85 Trace.traceBegin(TRACE_TAG_PACKAGE_MANAGER, "parseApkLite");
86 final PackageParser.ApkLite baseApk = parseApkLite(packageFile, flags);
87 final String packagePath = packageFile.getAbsolutePath();
88 Trace.traceEnd(TRACE_TAG_PACKAGE_MANAGER);
89 return new PackageParser.PackageLite(packagePath, baseApk, null, null, null, null,
90 null, null);
91 }
92
93 public static PackageParser.PackageLite parseClusterPackageLite(File packageDir, int flags)
94 throws PackageParser.PackageParserException {
95 final File[] files = packageDir.listFiles();
96 if (ArrayUtils.isEmpty(files)) {
97 throw new PackageParser.PackageParserException(
98 PackageManager.INSTALL_PARSE_FAILED_NOT_APK, "No packages found in split");
99 }
100
101 String packageName = null;
102 int versionCode = 0;
103
104 Trace.traceBegin(TRACE_TAG_PACKAGE_MANAGER, "parseApkLite");
105 final ArrayMap<String, PackageParser.ApkLite> apks = new ArrayMap<>();
106 for (File file : files) {
107 if (PackageParser.isApkFile(file)) {
108 final PackageParser.ApkLite lite = parseApkLite(file, flags);
109
110 // Assert that all package names and version codes are
111 // consistent with the first one we encounter.
112 if (packageName == null) {
113 packageName = lite.packageName;
114 versionCode = lite.versionCode;
115 } else {
116 if (!packageName.equals(lite.packageName)) {
117 throw new PackageParser.PackageParserException(
118 PackageManager.INSTALL_PARSE_FAILED_BAD_MANIFEST,
119 "Inconsistent package " + lite.packageName + " in " + file
120 + "; expected " + packageName);
121 }
122 if (versionCode != lite.versionCode) {
123 throw new PackageParser.PackageParserException(
124 PackageManager.INSTALL_PARSE_FAILED_BAD_MANIFEST,
125 "Inconsistent version " + lite.versionCode + " in " + file
126 + "; expected " + versionCode);
127 }
128 }
129
130 // Assert that each split is defined only oncuses-static-libe
131 if (apks.put(lite.splitName, lite) != null) {
132 throw new PackageParser.PackageParserException(
133 PackageManager.INSTALL_PARSE_FAILED_BAD_MANIFEST,
134 "Split name " + lite.splitName
135 + " defined more than once; most recent was " + file);
136 }
137 }
138 }
139 Trace.traceEnd(TRACE_TAG_PACKAGE_MANAGER);
140
141 final PackageParser.ApkLite baseApk = apks.remove(null);
142 if (baseApk == null) {
143 throw new PackageParser.PackageParserException(
144 PackageManager.INSTALL_PARSE_FAILED_BAD_MANIFEST,
145 "Missing base APK in " + packageDir);
146 }
147
148 // Always apply deterministic ordering based on splitName
149 final int size = apks.size();
150
151 String[] splitNames = null;
152 boolean[] isFeatureSplits = null;
153 String[] usesSplitNames = null;
154 String[] configForSplits = null;
155 String[] splitCodePaths = null;
156 int[] splitRevisionCodes = null;
157 if (size > 0) {
158 splitNames = new String[size];
159 isFeatureSplits = new boolean[size];
160 usesSplitNames = new String[size];
161 configForSplits = new String[size];
162 splitCodePaths = new String[size];
163 splitRevisionCodes = new int[size];
164
165 splitNames = apks.keySet().toArray(splitNames);
166 Arrays.sort(splitNames, PackageParser.sSplitNameComparator);
167
168 for (int i = 0; i < size; i++) {
169 final PackageParser.ApkLite apk = apks.get(splitNames[i]);
170 usesSplitNames[i] = apk.usesSplitName;
171 isFeatureSplits[i] = apk.isFeatureSplit;
172 configForSplits[i] = apk.configForSplit;
173 splitCodePaths[i] = apk.codePath;
174 splitRevisionCodes[i] = apk.revisionCode;
175 }
176 }
177
178 final String codePath = packageDir.getAbsolutePath();
179 return new PackageParser.PackageLite(codePath, baseApk, splitNames, isFeatureSplits,
180 usesSplitNames, configForSplits, splitCodePaths, splitRevisionCodes);
181 }
182
183 /**
184 * Utility method that retrieves lightweight details about a single APK
185 * file, including package name, split name, and install location.
186 *
187 * @param apkFile path to a single APK
188 * @param flags optional parse flags, such as
189 * {@link PackageParser#PARSE_COLLECT_CERTIFICATES}
190 */
191 public static PackageParser.ApkLite parseApkLite(File apkFile, int flags)
192 throws PackageParser.PackageParserException {
193 return parseApkLiteInner(apkFile, null, null, flags);
194 }
195
196 /**
197 * Utility method that retrieves lightweight details about a single APK
198 * file, including package name, split name, and install location.
199 *
200 * @param fd already open file descriptor of an apk file
201 * @param debugPathName arbitrary text name for this file, for debug output
202 * @param flags optional parse flags, such as
203 * {@link PackageParser#PARSE_COLLECT_CERTIFICATES}
204 */
205 public static PackageParser.ApkLite parseApkLite(FileDescriptor fd, String debugPathName,
206 int flags) throws PackageParser.PackageParserException {
207 return parseApkLiteInner(null, fd, debugPathName, flags);
208 }
209
210 private static PackageParser.ApkLite parseApkLiteInner(File apkFile, FileDescriptor fd,
211 String debugPathName, int flags) throws PackageParser.PackageParserException {
212 final String apkPath = fd != null ? debugPathName : apkFile.getAbsolutePath();
213
214 XmlResourceParser parser = null;
215 ApkAssets apkAssets = null;
216 try {
217 try {
218 apkAssets = fd != null
219 ? ApkAssets.loadFromFd(fd, debugPathName, false, false)
220 : ApkAssets.loadFromPath(apkPath);
221 } catch (IOException e) {
222 throw new PackageParser.PackageParserException(
223 PackageManager.INSTALL_PARSE_FAILED_NOT_APK,
224 "Failed to parse " + apkPath, e);
225 }
226
227 parser = apkAssets.openXml(PackageParser.ANDROID_MANIFEST_FILENAME);
228
229 final PackageParser.SigningDetails signingDetails;
230 if ((flags & PackageParser.PARSE_COLLECT_CERTIFICATES) != 0) {
231 final boolean skipVerify = (flags & PackageParser.PARSE_IS_SYSTEM_DIR) != 0;
232 Trace.traceBegin(TRACE_TAG_PACKAGE_MANAGER, "collectCertificates");
233 try {
234 signingDetails =
235 ApkParseUtils.collectCertificates(apkFile.getAbsolutePath(), skipVerify,
236 false, PackageParser.SigningDetails.UNKNOWN);
237 } finally {
238 Trace.traceEnd(TRACE_TAG_PACKAGE_MANAGER);
239 }
240 } else {
241 signingDetails = PackageParser.SigningDetails.UNKNOWN;
242 }
243
244 final AttributeSet attrs = parser;
245 return parseApkLite(apkPath, parser, attrs, signingDetails);
246
247 } catch (XmlPullParserException | IOException | RuntimeException e) {
248 Slog.w(TAG, "Failed to parse " + apkPath, e);
249 throw new PackageParser.PackageParserException(
250 PackageManager.INSTALL_PARSE_FAILED_UNEXPECTED_EXCEPTION,
251 "Failed to parse " + apkPath, e);
252 } finally {
253 IoUtils.closeQuietly(parser);
254 if (apkAssets != null) {
255 try {
256 apkAssets.close();
257 } catch (Throwable ignored) {
258 }
259 }
260 // TODO(b/72056911): Implement AutoCloseable on ApkAssets.
261 }
262 }
263
264 private static PackageParser.ApkLite parseApkLite(
265 String codePath, XmlPullParser parser, AttributeSet attrs,
266 PackageParser.SigningDetails signingDetails)
267 throws IOException, XmlPullParserException, PackageParser.PackageParserException {
268 final Pair<String, String> packageSplit = PackageParser.parsePackageSplitNames(
269 parser, attrs);
270
271 int installLocation = PARSE_DEFAULT_INSTALL_LOCATION;
272 int versionCode = 0;
273 int versionCodeMajor = 0;
274 int targetSdkVersion = DEFAULT_TARGET_SDK_VERSION;
275 int minSdkVersion = DEFAULT_MIN_SDK_VERSION;
276 int revisionCode = 0;
277 boolean coreApp = false;
278 boolean debuggable = false;
279 boolean multiArch = false;
280 boolean use32bitAbi = false;
281 boolean extractNativeLibs = true;
282 boolean isolatedSplits = false;
283 boolean isFeatureSplit = false;
284 boolean isSplitRequired = false;
285 boolean useEmbeddedDex = false;
286 String configForSplit = null;
287 String usesSplitName = null;
288
289 for (int i = 0; i < attrs.getAttributeCount(); i++) {
290 final String attr = attrs.getAttributeName(i);
291 switch (attr) {
292 case "installLocation":
293 installLocation = attrs.getAttributeIntValue(i,
294 PARSE_DEFAULT_INSTALL_LOCATION);
295 break;
296 case "versionCode":
297 versionCode = attrs.getAttributeIntValue(i, 0);
298 break;
299 case "versionCodeMajor":
300 versionCodeMajor = attrs.getAttributeIntValue(i, 0);
301 break;
302 case "revisionCode":
303 revisionCode = attrs.getAttributeIntValue(i, 0);
304 break;
305 case "coreApp":
306 coreApp = attrs.getAttributeBooleanValue(i, false);
307 break;
308 case "isolatedSplits":
309 isolatedSplits = attrs.getAttributeBooleanValue(i, false);
310 break;
311 case "configForSplit":
312 configForSplit = attrs.getAttributeValue(i);
313 break;
314 case "isFeatureSplit":
315 isFeatureSplit = attrs.getAttributeBooleanValue(i, false);
316 break;
317 case "isSplitRequired":
318 isSplitRequired = attrs.getAttributeBooleanValue(i, false);
319 break;
320 }
321 }
322
323 // Only search the tree when the tag is the direct child of <manifest> tag
324 int type;
325 final int searchDepth = parser.getDepth() + 1;
326
327 final List<VerifierInfo> verifiers = new ArrayList<>();
328 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT
329 && (type != XmlPullParser.END_TAG || parser.getDepth() >= searchDepth)) {
330 if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
331 continue;
332 }
333
334 if (parser.getDepth() != searchDepth) {
335 continue;
336 }
337
338 if (PackageParser.TAG_PACKAGE_VERIFIER.equals(parser.getName())) {
339 final VerifierInfo verifier = parseVerifier(attrs);
340 if (verifier != null) {
341 verifiers.add(verifier);
342 }
343 } else if (PackageParser.TAG_APPLICATION.equals(parser.getName())) {
344 for (int i = 0; i < attrs.getAttributeCount(); ++i) {
345 final String attr = attrs.getAttributeName(i);
346 switch (attr) {
347 case "debuggable":
348 debuggable = attrs.getAttributeBooleanValue(i, false);
349 break;
350 case "multiArch":
351 multiArch = attrs.getAttributeBooleanValue(i, false);
352 break;
353 case "use32bitAbi":
354 use32bitAbi = attrs.getAttributeBooleanValue(i, false);
355 break;
356 case "extractNativeLibs":
357 extractNativeLibs = attrs.getAttributeBooleanValue(i, true);
358 break;
359 case "useEmbeddedDex":
360 useEmbeddedDex = attrs.getAttributeBooleanValue(i, false);
361 break;
362 }
363 }
364 } else if (PackageParser.TAG_USES_SPLIT.equals(parser.getName())) {
365 if (usesSplitName != null) {
366 Slog.w(TAG, "Only one <uses-split> permitted. Ignoring others.");
367 continue;
368 }
369
370 usesSplitName = attrs.getAttributeValue(PackageParser.ANDROID_RESOURCES, "name");
371 if (usesSplitName == null) {
372 throw new PackageParser.PackageParserException(
373 PackageManager.INSTALL_PARSE_FAILED_MANIFEST_MALFORMED,
374 "<uses-split> tag requires 'android:name' attribute");
375 }
376 } else if (PackageParser.TAG_USES_SDK.equals(parser.getName())) {
377 for (int i = 0; i < attrs.getAttributeCount(); ++i) {
378 final String attr = attrs.getAttributeName(i);
379 if ("targetSdkVersion".equals(attr)) {
380 targetSdkVersion = attrs.getAttributeIntValue(i,
381 DEFAULT_TARGET_SDK_VERSION);
382 }
383 if ("minSdkVersion".equals(attr)) {
384 minSdkVersion = attrs.getAttributeIntValue(i, DEFAULT_MIN_SDK_VERSION);
385 }
386 }
387 }
388 }
389
390 return new PackageParser.ApkLite(codePath, packageSplit.first, packageSplit.second,
391 isFeatureSplit, configForSplit, usesSplitName, isSplitRequired, versionCode,
392 versionCodeMajor, revisionCode, installLocation, verifiers, signingDetails,
393 coreApp, debuggable, multiArch, use32bitAbi, useEmbeddedDex, extractNativeLibs,
394 isolatedSplits, minSdkVersion, targetSdkVersion);
395 }
396
397 public static VerifierInfo parseVerifier(AttributeSet attrs) {
398 String packageName = null;
399 String encodedPublicKey = null;
400
401 final int attrCount = attrs.getAttributeCount();
402 for (int i = 0; i < attrCount; i++) {
403 final int attrResId = attrs.getAttributeNameResource(i);
404 switch (attrResId) {
405 case R.attr.name:
406 packageName = attrs.getAttributeValue(i);
407 break;
408
409 case R.attr.publicKey:
410 encodedPublicKey = attrs.getAttributeValue(i);
411 break;
412 }
413 }
414
415 if (packageName == null || packageName.length() == 0) {
416 Slog.i(TAG, "verifier package name was null; skipping");
417 return null;
418 }
419
420 final PublicKey publicKey = PackageParser.parsePublicKey(encodedPublicKey);
421 if (publicKey == null) {
422 Slog.i(TAG, "Unable to parse verifier public key for " + packageName);
423 return null;
424 }
425
426 return new VerifierInfo(packageName, publicKey);
427 }
428}