Victor Hsieh | 20fe1f6 | 2019-09-30 13:36:21 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 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 | |
| 17 | package android.security; |
| 18 | |
| 19 | import android.annotation.NonNull; |
| 20 | import android.annotation.RequiresPermission; |
| 21 | import android.annotation.SystemService; |
| 22 | import android.content.Context; |
| 23 | import android.os.RemoteException; |
| 24 | |
| 25 | import java.security.cert.CertificateEncodingException; |
| 26 | import java.security.cert.X509Certificate; |
| 27 | |
| 28 | /** |
| 29 | * This class provides access to file integrity related operations. |
| 30 | */ |
| 31 | @SystemService(Context.FILE_INTEGRITY_SERVICE) |
| 32 | public final class FileIntegrityManager { |
| 33 | @NonNull private final IFileIntegrityService mService; |
| 34 | |
| 35 | /** @hide */ |
| 36 | public FileIntegrityManager(@NonNull IFileIntegrityService service) { |
| 37 | mService = service; |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * Returns true if APK Verity is supported on the device. When supported, an APK can be |
| 42 | * installed with a fs-verity signature (if verified with trusted App Source Certificate) for |
| 43 | * continuous on-access verification. |
| 44 | */ |
| 45 | public boolean isApkVeritySupported() { |
| 46 | try { |
| 47 | // Go through the service just to avoid exposing the vendor controlled system property |
| 48 | // to all apps. |
| 49 | return mService.isApkVeritySupported(); |
| 50 | } catch (RemoteException e) { |
| 51 | throw e.rethrowFromSystemServer(); |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Returns whether the given certificate can be used to prove app's install source. Always |
| 57 | * return false if the feature is not supported. |
| 58 | * |
| 59 | * <p>A store can use this API to decide if a signature file needs to be downloaded. Also, if a |
| 60 | * store has shipped different certificates before (e.g. with stronger and weaker key), it can |
| 61 | * also use this API to download the best signature on the running device. |
| 62 | * |
| 63 | * @return whether the certificate is trusted in the system |
| 64 | */ |
| 65 | @RequiresPermission(anyOf = { |
| 66 | android.Manifest.permission.INSTALL_PACKAGES, |
| 67 | android.Manifest.permission.REQUEST_INSTALL_PACKAGES |
| 68 | }) |
| 69 | public boolean isAppSourceCertificateTrusted(@NonNull X509Certificate certificate) |
| 70 | throws CertificateEncodingException { |
| 71 | try { |
| 72 | return mService.isAppSourceCertificateTrusted(certificate.getEncoded()); |
| 73 | } catch (RemoteException e) { |
| 74 | throw e.rethrowFromSystemServer(); |
| 75 | } |
| 76 | } |
| 77 | } |