• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     @NonNull private final Context mContext;
35 
36     /** @hide */
FileIntegrityManager(@onNull Context context, @NonNull IFileIntegrityService service)37     public FileIntegrityManager(@NonNull Context context, @NonNull IFileIntegrityService service) {
38         mContext = context;
39         mService = service;
40     }
41 
42     /**
43      * Returns true if APK Verity is supported on the device. When supported, an APK can be
44      * installed with a fs-verity signature (if verified with trusted App Source Certificate) for
45      * continuous on-access verification.
46      */
isApkVeritySupported()47     public boolean isApkVeritySupported() {
48         try {
49             // Go through the service just to avoid exposing the vendor controlled system property
50             // to all apps.
51             return mService.isApkVeritySupported();
52         } catch (RemoteException e) {
53             throw e.rethrowFromSystemServer();
54         }
55     }
56 
57     /**
58      * Returns whether the given certificate can be used to prove app's install source. Always
59      * return false if the feature is not supported.
60      *
61      * <p>A store can use this API to decide if a signature file needs to be downloaded. Also, if a
62      * store has shipped different certificates before (e.g. with stronger and weaker key), it can
63      * also use this API to download the best signature on the running device.
64      *
65      * @return whether the certificate is trusted in the system
66      */
67     @RequiresPermission(anyOf = {
68             android.Manifest.permission.INSTALL_PACKAGES,
69             android.Manifest.permission.REQUEST_INSTALL_PACKAGES
70     })
isAppSourceCertificateTrusted(@onNull X509Certificate certificate)71     public boolean isAppSourceCertificateTrusted(@NonNull X509Certificate certificate)
72             throws CertificateEncodingException {
73         try {
74             return mService.isAppSourceCertificateTrusted(
75                     certificate.getEncoded(), mContext.getOpPackageName());
76         } catch (RemoteException e) {
77             throw e.rethrowFromSystemServer();
78         }
79     }
80 }
81