1 /* 2 * Copyright (C) 2021 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 com.android.server.pm; 18 19 import static com.android.server.pm.PackageManagerService.PACKAGE_MIME_TYPE; 20 21 import android.annotation.Nullable; 22 import android.content.Context; 23 import android.content.Intent; 24 import android.content.pm.PackageInstaller; 25 import android.content.pm.PackageManager; 26 import android.net.Uri; 27 import android.os.UserHandle; 28 import android.provider.Settings; 29 30 final class VerificationUtils { 31 /** 32 * The default maximum time to wait for the verification agent to return in 33 * milliseconds. 34 */ 35 private static final long DEFAULT_VERIFICATION_TIMEOUT = 10 * 1000; 36 37 /** 38 * The default maximum time to wait for the verification agent to return in 39 * milliseconds. 40 */ 41 private static final long DEFAULT_STREAMING_VERIFICATION_TIMEOUT = 3 * 1000; 42 getVerificationTimeout(Context context, boolean streaming)43 public static long getVerificationTimeout(Context context, boolean streaming) { 44 if (streaming) { 45 return getDefaultStreamingVerificationTimeout(context); 46 } 47 return getDefaultVerificationTimeout(context); 48 } 49 50 /** 51 * Get the default verification agent timeout. Used for both the APK verifier and the 52 * intent filter verifier. 53 * 54 * @return verification timeout in milliseconds 55 */ getDefaultVerificationTimeout(Context context)56 public static long getDefaultVerificationTimeout(Context context) { 57 long timeout = Settings.Global.getLong(context.getContentResolver(), 58 Settings.Global.PACKAGE_VERIFIER_TIMEOUT, DEFAULT_VERIFICATION_TIMEOUT); 59 // The setting can be used to increase the timeout but not decrease it, since that is 60 // equivalent to disabling the verifier. 61 return Math.max(timeout, DEFAULT_VERIFICATION_TIMEOUT); 62 } 63 64 /** 65 * Get the default verification agent timeout for streaming installations. 66 * 67 * @return verification timeout in milliseconds 68 */ getDefaultStreamingVerificationTimeout(Context context)69 public static long getDefaultStreamingVerificationTimeout(Context context) { 70 long timeout = Settings.Global.getLong(context.getContentResolver(), 71 Settings.Global.PACKAGE_STREAMING_VERIFIER_TIMEOUT, 72 DEFAULT_STREAMING_VERIFICATION_TIMEOUT); 73 // The setting can be used to increase the timeout but not decrease it, since that is 74 // equivalent to disabling the verifier. 75 return Math.max(timeout, DEFAULT_STREAMING_VERIFICATION_TIMEOUT); 76 } 77 broadcastPackageVerified(int verificationId, Uri packageUri, int verificationCode, @Nullable String rootHashString, int dataLoaderType, UserHandle user, Context context)78 public static void broadcastPackageVerified(int verificationId, Uri packageUri, 79 int verificationCode, @Nullable String rootHashString, int dataLoaderType, 80 UserHandle user, Context context) { 81 final Intent intent = new Intent(Intent.ACTION_PACKAGE_VERIFIED); 82 intent.setDataAndType(packageUri, PACKAGE_MIME_TYPE); 83 intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); 84 intent.putExtra(PackageManager.EXTRA_VERIFICATION_ID, verificationId); 85 intent.putExtra(PackageManager.EXTRA_VERIFICATION_RESULT, verificationCode); 86 if (rootHashString != null) { 87 intent.putExtra(PackageManager.EXTRA_VERIFICATION_ROOT_HASH, rootHashString); 88 } 89 intent.putExtra(PackageInstaller.EXTRA_DATA_LOADER_TYPE, dataLoaderType); 90 91 context.sendBroadcastAsUser(intent, user, 92 android.Manifest.permission.PACKAGE_VERIFICATION_AGENT); 93 } 94 } 95