aboutsummaryrefslogtreecommitdiffstats
path: root/sdk/src/java/lineageos/trust/TrustInterface.java
blob: 2df0b755ecc96b0c41dcc521f869be6451c66131 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
/**
 * Copyright (c) 2018, The LineageOS Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package lineageos.trust;

import android.content.Context;
import android.os.IBinder;
import android.os.RemoteException;
import android.os.ServiceManager;
import android.util.Log;

import lineageos.app.LineageContextConstants;

public class TrustInterface {
    /**
     * Allows an application to use the Trust interface to display trusted
     * security messages to the user.
     * This is a system-only permission, user-installed apps cannot use it
     */
    public static final String TRUST_INTERFACE_PERMISSION = "lineageos.permission.TRUST_INTERFACE";

    /**
     * Unable to determine status, an error occured
     *
     * @see #getLevelForFeature
     */
    public static final int ERROR_UNDEFINED = -1;

    /**
     * Trust feature status: good. The feature is in a configuration
     * that will help protect the user's data
     *
     * @see #getLevelForFeature
     */
    public static final int TRUST_FEATURE_LEVEL_GOOD = 0;

    /**
     * Trust feature status: poor. The feature is in a configuration
     * that might be used to harm the user's data
     *
     * @see #getLevelForFeature
     */
    public static final int TRUST_FEATURE_LEVEL_POOR = 1;

    /**
     * Trust feature status: bad. The feature is in a configuration
     * that is dangerous for the user's data
     *
     * @see #getLevelForFeature
     */
    public static final int TRUST_FEATURE_LEVEL_BAD = 2;

    /**
     * Trust feature indicator: SELinux status
     *
     * Possible status:
     *    * {@link #TRUST_FEATURE_LEVEL_GOOD}: enforcing
     *    * {@link #TRUST_FEATURE_LEVEL_BAD}: permissive / disabled
     *
     * @see #getLevelForFeature
     */
    public static final int TRUST_FEATURE_SELINUX = 0;

    /**
     * Trust feature indicator: Root access
     *
     * Possible status:
     *    * {@link #TRUST_FEATURE_LEVEL_GOOD}: disabled
     *    * {@link #TRUST_FEATURE_LEVEL_POOR}: ADB only
     *    * {@link #TRUST_FEATURE_LEVEL_BAD}: apps and ADB
     *
     * @see #getLevelForFeature
     */
    public static final int TRUST_FEATURE_ROOT = 1;

    /**
     * Trust feature indicator: Platform Security patches
     *
     * Possible status:
     *    * {@link #TRUST_FEATURE_LEVEL_GOOD}: less than 3 months old
     *    * {@link #TRUST_FEATURE_LEVEL_POOR}: less than 9 months old
     *    * {@link #TRUST_FEATURE_LEVEL_BAD}: older than 9 months
     *
     * @see #getLevelForFeature
     */
    public static final int TRUST_FEATURE_PLATFORM_SECURITY_PATCH = 2;

    /**
     * Trust feature indicator: Vendor Security patches
     *
     * Possible status:
     *    * {@link #TRUST_FEATURE_LEVEL_GOOD}: less than 3 months old
     *    * {@link #TRUST_FEATURE_LEVEL_POOR}: less than 9 months old
     *    * {@link #TRUST_FEATURE_LEVEL_BAD}: older than 9 months
     *
     * @see #getLevelForFeature
     */
    public static final int TRUST_FEATURE_VENDOR_SECURITY_PATCH = 3;

    /**
     * Trust feature indicator: Encryption
     *
     * Some older devices have a significant performance loss when running
     * encrypted, so the status will be different when Trust is executed on
     *  these devices.
     *
     * Possible status for old device:
     *    * {@link #TRUST_FEATURE_LEVEL_GOOD}: enabled
     *    * {@link #TRUST_FEATURE_LEVEL_POOR}: disabled
     *
     * Possible status for recent device:
     *    * {@link #TRUST_FEATURE_LEVEL_GOOD}: enabled
     *    * {@link #TRUST_FEATURE_LEVEL_BAD}: disabled
     *
     * @see #getLevelForFeature
     */
    public static final int TRUST_FEATURE_ENCRYPTION = 4;

    /**
     * Trust feature indicator: Keys
     *
     * Possible status:
     *    * {@link #TRUST_FEATURE_LEVEL_GOOD}: signed with private keys
     *    * {@link #TRUST_FEATURE_LEVEL_BAD}: signed with public or inline keys
     *
     * @see #getLevelForFeature
     */
    public static final int TRUST_FEATURE_KEYS = 5;

    /**
     * Trust warning: SELinux
     *
     * When {@link #TRUST_FEATURE_SELINUX} is not {@link #TRUST_FEATURE_LEVEL_GOOD}
     * notify the user about the issue
     *
     * @see #postNotificationForFeature
     */
    public static final int TRUST_WARN_SELINUX = 1;

    /**
     * Trust warning: Root access
     *
     * When {@link #TRUST_FEATURE_ROOT} is not {@link #TRUST_FEATURE_LEVEL_GOOD}
     * notify the user about the issue
     *
     * @see #postNotificationForFeature
     */
    public static final int TRUST_WARN_ROOT = 1 << 1;

    /**
     * Trust warning: Public Key build signature
     *
     * When {@link #TRUST_FEATURE_KEYS} is not {@link #TRUST_FEATURE_LEVEL_GOOD}
     * notify the user about the issue
     *
     * @see #postNotificationForFeature
     */
    public static final int TRUST_WARN_PUBLIC_KEY = 1 << 2;

    /**
     * Max / default value for warnings status
     *
     * Includes all the TRUST_WARN_
     *
     * @see #postNotificationForFeature
     * @hide
     */
    public static final int TRUST_WARN_MAX_VALUE =
            TRUST_WARN_SELINUX |
            TRUST_WARN_ROOT |
            TRUST_WARN_PUBLIC_KEY;

    private static final String TAG = "TrustInterface";

    private static ITrustInterface sService;
    private static TrustInterface sInstance;

    private Context mContext;

    private TrustInterface(Context context) {
        Context appContext = context.getApplicationContext();
        mContext = appContext == null ? context : appContext;
        sService = getService();
        if (context.getPackageManager().hasSystemFeature(
                LineageContextConstants.Features.TRUST) && sService == null) {
            throw new RuntimeException("Unable to get TrustInterfaceService. The service" +
                    " either crashed, was not started, or the interface has been called to early" +
                    " in SystemServer init");
                }
    }

    /**
     * Get or create an instance of the {@link lineageos.trust.TrustInterface}
     *
     * @param context Used to get the service
     * @return {@link TrustInterface}
     */
    public static TrustInterface getInstance(Context context) {
        if (sInstance == null) {
            sInstance = new TrustInterface(context);
        }
        return sInstance;
    }

    /** @hide **/
    public static ITrustInterface getService() {
        if (sService != null) {
            return sService;
        }
        IBinder b = ServiceManager.getService(LineageContextConstants.LINEAGE_TRUST_INTERFACE);
        sService = ITrustInterface.Stub.asInterface(b);

        if (b == null) {
            Log.e(TAG, "null service. SAD!");
            return null;
        }

        sService = ITrustInterface.Stub.asInterface(b);
        return sService;
    }

    public boolean postNotificationForFeature(int feature) {
        if (sService == null) {
            return false;
        }
        try {
            return sService.postNotificationForFeature(feature);
        } catch (RemoteException e) {
            Log.e(TAG, e.getLocalizedMessage(), e);
        }
        return false;
    }

    public boolean removeNotificationForFeature(int feature) {
        if (sService == null) {
            return false;
        }
        try {
            return sService.removeNotificationForFeature(feature);
        } catch (RemoteException e) {
            Log.e(TAG, e.getLocalizedMessage(), e);
        }
        return false;
    }

    public int getLevelForFeature(int feature) {
        if (sService == null) {
            return ERROR_UNDEFINED;
        }
        try {
            return sService.getLevelForFeature(feature);
        } catch (RemoteException e) {
            Log.e(TAG, e.getLocalizedMessage(), e);
        }
        return ERROR_UNDEFINED;
    }

    /** @hide */
    public void runTest() {
        if (sService == null) {
            return;
        }
        try {
            sService.runTest();
        } catch (RemoteException e) {
            Log.e(TAG, e.getLocalizedMessage(), e);
        }
        return;
    }
}