blob: 46143558eed7f146aa681b7b36ce7e54209339d5 [file] [log] [blame]
Adrian Roos82142c22014-03-27 14:56:59 +01001/*
2 * Copyright (C) 2014 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 com.android.server.trust;
18
Lingjun Li93a145f2017-01-23 17:13:35 -080019import android.annotation.TargetApi;
Jim Millerd4efaac2014-08-14 18:02:45 -070020import android.app.AlarmManager;
21import android.app.PendingIntent;
Jim Miller604e7552014-07-18 19:00:02 -070022import android.app.admin.DevicePolicyManager;
Jim Millerd4efaac2014-08-14 18:02:45 -070023import android.content.BroadcastReceiver;
Adrian Roos82142c22014-03-27 14:56:59 +010024import android.content.ComponentName;
25import android.content.Context;
26import android.content.Intent;
Jim Millerd4efaac2014-08-14 18:02:45 -070027import android.content.IntentFilter;
Adrian Roos82142c22014-03-27 14:56:59 +010028import android.content.ServiceConnection;
Jim Millerd4efaac2014-08-14 18:02:45 -070029import android.net.Uri;
Adrian Roos8f211582014-07-29 15:09:57 +020030import android.os.Binder;
Lingjun Li93a145f2017-01-23 17:13:35 -080031import android.os.Build;
Adrian Roos82142c22014-03-27 14:56:59 +010032import android.os.Handler;
33import android.os.IBinder;
34import android.os.Message;
Jim Millerd4efaac2014-08-14 18:02:45 -070035import android.os.PatternMatcher;
Jim Millere303bf42014-08-26 17:12:29 -070036import android.os.PersistableBundle;
Adrian Roos82142c22014-03-27 14:56:59 +010037import android.os.RemoteException;
Adrian Roosc5f95ce2014-07-24 16:00:46 +020038import android.os.SystemClock;
Adrian Roos82142c22014-03-27 14:56:59 +010039import android.os.UserHandle;
Adrian Roos82142c22014-03-27 14:56:59 +010040import android.service.trust.ITrustAgentService;
41import android.service.trust.ITrustAgentServiceCallback;
Lingjun Li93a145f2017-01-23 17:13:35 -080042import android.service.trust.TrustAgentService;
43import android.util.Log;
44import android.util.Slog;
Lucas Dupinef886542018-01-03 16:03:07 -080045
Adrian Roosa43fd032015-03-09 19:10:15 +010046import java.util.Collections;
Jim Miller604e7552014-07-18 19:00:02 -070047import java.util.List;
Adrian Roos82142c22014-03-27 14:56:59 +010048
49/**
50 * A wrapper around a TrustAgentService interface. Coordinates communication between
51 * TrustManager and the actual TrustAgent.
52 */
Lingjun Li93a145f2017-01-23 17:13:35 -080053@TargetApi(Build.VERSION_CODES.LOLLIPOP)
Adrian Roos82142c22014-03-27 14:56:59 +010054public class TrustAgentWrapper {
Jim Millerd4efaac2014-08-14 18:02:45 -070055 private static final String EXTRA_COMPONENT_NAME = "componentName";
56 private static final String TRUST_EXPIRED_ACTION = "android.server.trust.TRUST_EXPIRED_ACTION";
Jim Miller76b9b8b2014-08-22 17:04:57 -070057 private static final String PERMISSION = android.Manifest.permission.PROVIDE_TRUST_AGENT;
Adrian Roos5d639782016-07-21 11:43:02 -070058 private static final boolean DEBUG = TrustManagerService.DEBUG;
Adrian Roos82142c22014-03-27 14:56:59 +010059 private static final String TAG = "TrustAgentWrapper";
60
Adrian Roos7a4f3d42014-05-02 12:12:20 +020061 private static final int MSG_GRANT_TRUST = 1;
Adrian Roos82142c22014-03-27 14:56:59 +010062 private static final int MSG_REVOKE_TRUST = 2;
63 private static final int MSG_TRUST_TIMEOUT = 3;
Adrian Roosc5f95ce2014-07-24 16:00:46 +020064 private static final int MSG_RESTART_TIMEOUT = 4;
Adrian Roos8f211582014-07-29 15:09:57 +020065 private static final int MSG_SET_TRUST_AGENT_FEATURES_COMPLETED = 5;
Adrian Roos7861c662014-07-25 15:37:28 +020066 private static final int MSG_MANAGING_TRUST = 6;
Lingjun Li93a145f2017-01-23 17:13:35 -080067 private static final int MSG_ADD_ESCROW_TOKEN = 7;
68 private static final int MSG_REMOVE_ESCROW_TOKEN = 8;
69 private static final int MSG_ESCROW_TOKEN_STATE = 9;
70 private static final int MSG_UNLOCK_USER = 10;
Lucas Dupinef886542018-01-03 16:03:07 -080071 private static final int MSG_SHOW_KEYGUARD_ERROR_MESSAGE = 11;
Adrian Roosc5f95ce2014-07-24 16:00:46 +020072
73 /**
74 * Time in uptime millis that we wait for the service connection, both when starting
75 * and when the service disconnects.
76 */
77 private static final long RESTART_TIMEOUT_MILLIS = 5 * 60000;
Adrian Roos82142c22014-03-27 14:56:59 +010078
Adrian Roos7a4f3d42014-05-02 12:12:20 +020079 /**
80 * Long extra for {@link #MSG_GRANT_TRUST}
81 */
82 private static final String DATA_DURATION = "duration";
Lingjun Li93a145f2017-01-23 17:13:35 -080083 private static final String DATA_ESCROW_TOKEN = "escrow_token";
84 private static final String DATA_HANDLE = "handle";
85 private static final String DATA_USER_ID = "user_id";
Lucas Dupinef886542018-01-03 16:03:07 -080086 private static final String DATA_MESSAGE = "message";
Adrian Roos7a4f3d42014-05-02 12:12:20 +020087
Adrian Roos82142c22014-03-27 14:56:59 +010088 private final TrustManagerService mTrustManagerService;
89 private final int mUserId;
90 private final Context mContext;
91 private final ComponentName mName;
92
93 private ITrustAgentService mTrustAgentService;
Adrian Roosc5f95ce2014-07-24 16:00:46 +020094 private boolean mBound;
95 private long mScheduledRestartUptimeMillis;
Jim Miller76b9b8b2014-08-22 17:04:57 -070096 private long mMaximumTimeToLock; // from DevicePolicyManager
Adrian Roos517b3a42016-03-03 14:58:33 -080097 private boolean mPendingSuccessfulUnlock = false;
Adrian Roos82142c22014-03-27 14:56:59 +010098
99 // Trust state
100 private boolean mTrusted;
Adrian Roos7e03dfc2014-05-16 16:06:28 +0200101 private CharSequence mMessage;
Jim Miller604e7552014-07-18 19:00:02 -0700102 private boolean mTrustDisabledByDpm;
Adrian Roos7861c662014-07-25 15:37:28 +0200103 private boolean mManagingTrust;
Adrian Roos8f211582014-07-29 15:09:57 +0200104 private IBinder mSetTrustAgentFeaturesToken;
Jim Millerd4efaac2014-08-14 18:02:45 -0700105 private AlarmManager mAlarmManager;
106 private final Intent mAlarmIntent;
Jim Miller76b9b8b2014-08-22 17:04:57 -0700107 private PendingIntent mAlarmPendingIntent;
Jim Millerd4efaac2014-08-14 18:02:45 -0700108
109 private final BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
110 @Override
111 public void onReceive(Context context, Intent intent) {
112 ComponentName component = intent.getParcelableExtra(EXTRA_COMPONENT_NAME);
113 if (TRUST_EXPIRED_ACTION.equals(intent.getAction())
114 && mName.equals(component)) {
115 mHandler.removeMessages(MSG_TRUST_TIMEOUT);
116 mHandler.sendEmptyMessage(MSG_TRUST_TIMEOUT);
117 }
118 }
119 };
Adrian Roos82142c22014-03-27 14:56:59 +0100120
121 private final Handler mHandler = new Handler() {
122 @Override
123 public void handleMessage(Message msg) {
124 switch (msg.what) {
Adrian Roos7a4f3d42014-05-02 12:12:20 +0200125 case MSG_GRANT_TRUST:
Adrian Roos7d59b4f2014-05-27 20:01:31 +0200126 if (!isConnected()) {
127 Log.w(TAG, "Agent is not connected, cannot grant trust: "
128 + mName.flattenToShortString());
129 return;
130 }
Adrian Roos82142c22014-03-27 14:56:59 +0100131 mTrusted = true;
Adrian Roos7e03dfc2014-05-16 16:06:28 +0200132 mMessage = (CharSequence) msg.obj;
Adrian Roos94e15a52015-04-16 12:23:18 -0700133 int flags = msg.arg1;
Adrian Roos7a4f3d42014-05-02 12:12:20 +0200134 long durationMs = msg.getData().getLong(DATA_DURATION);
135 if (durationMs > 0) {
Jim Miller76b9b8b2014-08-22 17:04:57 -0700136 final long duration;
137 if (mMaximumTimeToLock != 0) {
138 // Enforce DevicePolicyManager timeout. This is here as a safeguard to
139 // ensure trust agents are evaluating trust state at least as often as
140 // the policy dictates. Admins that want more guarantees should be using
141 // DevicePolicyManager#KEYGUARD_DISABLE_TRUST_AGENTS.
142 duration = Math.min(durationMs, mMaximumTimeToLock);
143 if (DEBUG) {
Adrian Roos5d639782016-07-21 11:43:02 -0700144 Slog.d(TAG, "DPM lock timeout in effect. Timeout adjusted from "
Jim Miller76b9b8b2014-08-22 17:04:57 -0700145 + durationMs + " to " + duration);
146 }
147 } else {
148 duration = durationMs;
149 }
150 long expiration = SystemClock.elapsedRealtime() + duration;
151 mAlarmPendingIntent = PendingIntent.getBroadcast(mContext, 0, mAlarmIntent,
Jim Millerd4efaac2014-08-14 18:02:45 -0700152 PendingIntent.FLAG_CANCEL_CURRENT);
Jim Miller76b9b8b2014-08-22 17:04:57 -0700153 mAlarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, expiration,
154 mAlarmPendingIntent);
Adrian Roos7a4f3d42014-05-02 12:12:20 +0200155 }
156 mTrustManagerService.mArchive.logGrantTrust(mUserId, mName,
157 (mMessage != null ? mMessage.toString() : null),
Adrian Roos94e15a52015-04-16 12:23:18 -0700158 durationMs, flags);
159 mTrustManagerService.updateTrust(mUserId, flags);
Adrian Roos82142c22014-03-27 14:56:59 +0100160 break;
161 case MSG_TRUST_TIMEOUT:
Adrian Roos5d639782016-07-21 11:43:02 -0700162 if (DEBUG) Slog.d(TAG, "Trust timed out : " + mName.flattenToShortString());
Adrian Roos7a4f3d42014-05-02 12:12:20 +0200163 mTrustManagerService.mArchive.logTrustTimeout(mUserId, mName);
Jim Millerd4efaac2014-08-14 18:02:45 -0700164 onTrustTimeout();
Adrian Roos82142c22014-03-27 14:56:59 +0100165 // Fall through.
166 case MSG_REVOKE_TRUST:
167 mTrusted = false;
168 mMessage = null;
Adrian Roos7a4f3d42014-05-02 12:12:20 +0200169 mHandler.removeMessages(MSG_TRUST_TIMEOUT);
170 if (msg.what == MSG_REVOKE_TRUST) {
171 mTrustManagerService.mArchive.logRevokeTrust(mUserId, mName);
172 }
Adrian Roos94e15a52015-04-16 12:23:18 -0700173 mTrustManagerService.updateTrust(mUserId, 0);
Adrian Roos82142c22014-03-27 14:56:59 +0100174 break;
Adrian Roosc5f95ce2014-07-24 16:00:46 +0200175 case MSG_RESTART_TIMEOUT:
Adrian Roos5d639782016-07-21 11:43:02 -0700176 Slog.w(TAG, "Connection attempt to agent " + mName.flattenToShortString()
177 + " timed out, rebinding");
Adrian Roosfc29e0b2014-11-11 12:55:44 +0100178 destroy();
Adrian Roosc5f95ce2014-07-24 16:00:46 +0200179 mTrustManagerService.resetAgent(mName, mUserId);
180 break;
Adrian Roos8f211582014-07-29 15:09:57 +0200181 case MSG_SET_TRUST_AGENT_FEATURES_COMPLETED:
182 IBinder token = (IBinder) msg.obj;
183 boolean result = msg.arg1 != 0;
184 if (mSetTrustAgentFeaturesToken == token) {
185 mSetTrustAgentFeaturesToken = null;
186 if (mTrustDisabledByDpm && result) {
Adrian Roos5d639782016-07-21 11:43:02 -0700187 if (DEBUG) Slog.d(TAG, "Re-enabling agent because it acknowledged "
188 + "enabled features: " + mName.flattenToShortString());
Adrian Roos8f211582014-07-29 15:09:57 +0200189 mTrustDisabledByDpm = false;
Adrian Roos94e15a52015-04-16 12:23:18 -0700190 mTrustManagerService.updateTrust(mUserId, 0);
Adrian Roos8f211582014-07-29 15:09:57 +0200191 }
192 } else {
Adrian Roos5d639782016-07-21 11:43:02 -0700193 if (DEBUG) Slog.w(TAG, "Ignoring MSG_SET_TRUST_AGENT_FEATURES_COMPLETED "
194 + "with obsolete token: " + mName.flattenToShortString());
Adrian Roos8f211582014-07-29 15:09:57 +0200195 }
Jim Miller604e7552014-07-18 19:00:02 -0700196 break;
Adrian Roos7861c662014-07-25 15:37:28 +0200197 case MSG_MANAGING_TRUST:
198 mManagingTrust = msg.arg1 != 0;
199 if (!mManagingTrust) {
200 mTrusted = false;
201 mMessage = null;
202 }
203 mTrustManagerService.mArchive.logManagingTrust(mUserId, mName, mManagingTrust);
Adrian Roos94e15a52015-04-16 12:23:18 -0700204 mTrustManagerService.updateTrust(mUserId, 0);
Adrian Roos7861c662014-07-25 15:37:28 +0200205 break;
Lingjun Li93a145f2017-01-23 17:13:35 -0800206 case MSG_ADD_ESCROW_TOKEN: {
207 byte[] eToken = msg.getData().getByteArray(DATA_ESCROW_TOKEN);
208 int userId = msg.getData().getInt(DATA_USER_ID);
209 long handle = mTrustManagerService.addEscrowToken(eToken, userId);
Lingjun Li44196d32017-02-17 18:32:23 -0800210 boolean resultDeliverred = false;
Lingjun Li93a145f2017-01-23 17:13:35 -0800211 try {
Lingjun Li44196d32017-02-17 18:32:23 -0800212 if (mTrustAgentService != null) {
213 mTrustAgentService.onEscrowTokenAdded(
214 eToken, handle, UserHandle.of(userId));
215 resultDeliverred = true;
216 }
Lingjun Li93a145f2017-01-23 17:13:35 -0800217 } catch (RemoteException e) {
218 onError(e);
219 }
Lingjun Li44196d32017-02-17 18:32:23 -0800220
221 if (!resultDeliverred) {
222 mTrustManagerService.removeEscrowToken(handle, userId);
223 }
Lingjun Li93a145f2017-01-23 17:13:35 -0800224 break;
225 }
226 case MSG_ESCROW_TOKEN_STATE: {
227 long handle = msg.getData().getLong(DATA_HANDLE);
228 int userId = msg.getData().getInt(DATA_USER_ID);
229 boolean active = mTrustManagerService.isEscrowTokenActive(handle, userId);
230 try {
Lingjun Li44196d32017-02-17 18:32:23 -0800231 if (mTrustAgentService != null) {
232 mTrustAgentService.onTokenStateReceived(handle,
233 active ? TrustAgentService.TOKEN_STATE_ACTIVE
234 : TrustAgentService.TOKEN_STATE_INACTIVE);
235 }
Lingjun Li93a145f2017-01-23 17:13:35 -0800236 } catch (RemoteException e) {
237 onError(e);
238 }
239 break;
240 }
241 case MSG_REMOVE_ESCROW_TOKEN: {
242 long handle = msg.getData().getLong(DATA_HANDLE);
243 int userId = msg.getData().getInt(DATA_USER_ID);
244 boolean success = mTrustManagerService.removeEscrowToken(handle, userId);
245 try {
Lingjun Li44196d32017-02-17 18:32:23 -0800246 if (mTrustAgentService != null) {
247 mTrustAgentService.onEscrowTokenRemoved(handle, success);
248 }
Lingjun Li93a145f2017-01-23 17:13:35 -0800249 } catch (RemoteException e) {
250 onError(e);
251 }
252 break;
253 }
254 case MSG_UNLOCK_USER: {
255 long handle = msg.getData().getLong(DATA_HANDLE);
256 int userId = msg.getData().getInt(DATA_USER_ID);
257 byte[] eToken = msg.getData().getByteArray(DATA_ESCROW_TOKEN);
258 mTrustManagerService.unlockUserWithToken(handle, eToken, userId);
259 break;
260 }
Lucas Dupinef886542018-01-03 16:03:07 -0800261 case MSG_SHOW_KEYGUARD_ERROR_MESSAGE: {
262 CharSequence message = msg.getData().getCharSequence(DATA_MESSAGE);
263 mTrustManagerService.showKeyguardErrorMessage(message);
264 break;
265 }
Adrian Roos82142c22014-03-27 14:56:59 +0100266 }
267 }
268 };
269
270 private ITrustAgentServiceCallback mCallback = new ITrustAgentServiceCallback.Stub() {
271
Adrian Roos7e03dfc2014-05-16 16:06:28 +0200272 @Override
Adrian Roos94e15a52015-04-16 12:23:18 -0700273 public void grantTrust(CharSequence userMessage, long durationMs, int flags) {
Adrian Roos5d639782016-07-21 11:43:02 -0700274 if (DEBUG) Slog.d(TAG, "enableTrust(" + userMessage + ", durationMs = " + durationMs
Adrian Roos94e15a52015-04-16 12:23:18 -0700275 + ", flags = " + flags + ")");
Adrian Roos82142c22014-03-27 14:56:59 +0100276
Adrian Roos7a4f3d42014-05-02 12:12:20 +0200277 Message msg = mHandler.obtainMessage(
Adrian Roos94e15a52015-04-16 12:23:18 -0700278 MSG_GRANT_TRUST, flags, 0, userMessage);
Adrian Roos7a4f3d42014-05-02 12:12:20 +0200279 msg.getData().putLong(DATA_DURATION, durationMs);
280 msg.sendToTarget();
Adrian Roos82142c22014-03-27 14:56:59 +0100281 }
282
Adrian Roos7e03dfc2014-05-16 16:06:28 +0200283 @Override
Adrian Roos82142c22014-03-27 14:56:59 +0100284 public void revokeTrust() {
Adrian Roos5d639782016-07-21 11:43:02 -0700285 if (DEBUG) Slog.d(TAG, "revokeTrust()");
Adrian Roos82142c22014-03-27 14:56:59 +0100286 mHandler.sendEmptyMessage(MSG_REVOKE_TRUST);
287 }
Adrian Roos7861c662014-07-25 15:37:28 +0200288
289 @Override
290 public void setManagingTrust(boolean managingTrust) {
Adrian Roos5d639782016-07-21 11:43:02 -0700291 if (DEBUG) Slog.d(TAG, "managingTrust()");
Adrian Roos7861c662014-07-25 15:37:28 +0200292 mHandler.obtainMessage(MSG_MANAGING_TRUST, managingTrust ? 1 : 0, 0).sendToTarget();
293 }
Adrian Roos8f211582014-07-29 15:09:57 +0200294
295 @Override
Jim Millere303bf42014-08-26 17:12:29 -0700296 public void onConfigureCompleted(boolean result, IBinder token) {
Adrian Roos5d639782016-07-21 11:43:02 -0700297 if (DEBUG) Slog.d(TAG, "onSetTrustAgentFeaturesEnabledCompleted(result=" + result);
Adrian Roos8f211582014-07-29 15:09:57 +0200298 mHandler.obtainMessage(MSG_SET_TRUST_AGENT_FEATURES_COMPLETED,
299 result ? 1 : 0, 0, token).sendToTarget();
300 }
Lingjun Li93a145f2017-01-23 17:13:35 -0800301
302 @Override
303 public void addEscrowToken(byte[] token, int userId) {
304 if (mContext.getResources()
305 .getBoolean(com.android.internal.R.bool.config_allowEscrowTokenForTrustAgent)) {
Lingjun Li44196d32017-02-17 18:32:23 -0800306 throw new SecurityException("Escrow token API is not allowed.");
Lingjun Li93a145f2017-01-23 17:13:35 -0800307 }
308
309 if (DEBUG) Slog.d(TAG, "adding escrow token for user " + userId);
310 Message msg = mHandler.obtainMessage(MSG_ADD_ESCROW_TOKEN);
311 msg.getData().putByteArray(DATA_ESCROW_TOKEN, token);
312 msg.getData().putInt(DATA_USER_ID, userId);
313 msg.sendToTarget();
314 }
315
316 @Override
317 public void isEscrowTokenActive(long handle, int userId) {
318 if (mContext.getResources()
319 .getBoolean(com.android.internal.R.bool.config_allowEscrowTokenForTrustAgent)) {
Lingjun Li44196d32017-02-17 18:32:23 -0800320 throw new SecurityException("Escrow token API is not allowed.");
Lingjun Li93a145f2017-01-23 17:13:35 -0800321 }
322
323 if (DEBUG) Slog.d(TAG, "checking the state of escrow token on user " + userId);
324 Message msg = mHandler.obtainMessage(MSG_ESCROW_TOKEN_STATE);
325 msg.getData().putLong(DATA_HANDLE, handle);
326 msg.getData().putInt(DATA_USER_ID, userId);
327 msg.sendToTarget();
328 }
329
330 @Override
331 public void removeEscrowToken(long handle, int userId) {
332 if (mContext.getResources()
333 .getBoolean(com.android.internal.R.bool.config_allowEscrowTokenForTrustAgent)) {
Lingjun Li44196d32017-02-17 18:32:23 -0800334 throw new SecurityException("Escrow token API is not allowed.");
Lingjun Li93a145f2017-01-23 17:13:35 -0800335 }
336
337 if (DEBUG) Slog.d(TAG, "removing escrow token on user " + userId);
338 Message msg = mHandler.obtainMessage(MSG_REMOVE_ESCROW_TOKEN);
339 msg.getData().putLong(DATA_HANDLE, handle);
340 msg.getData().putInt(DATA_USER_ID, userId);
341 msg.sendToTarget();
342 }
343
344 @Override
345 public void unlockUserWithToken(long handle, byte[] token, int userId) {
346 if (mContext.getResources()
347 .getBoolean(com.android.internal.R.bool.config_allowEscrowTokenForTrustAgent)) {
Lingjun Li44196d32017-02-17 18:32:23 -0800348 throw new SecurityException("Escrow token API is not allowed.");
Lingjun Li93a145f2017-01-23 17:13:35 -0800349 }
350
351 if (DEBUG) Slog.d(TAG, "unlocking user " + userId);
352 Message msg = mHandler.obtainMessage(MSG_UNLOCK_USER);
353 msg.getData().putInt(DATA_USER_ID, userId);
354 msg.getData().putLong(DATA_HANDLE, handle);
355 msg.getData().putByteArray(DATA_ESCROW_TOKEN, token);
356 msg.sendToTarget();
357 }
Lucas Dupinef886542018-01-03 16:03:07 -0800358
359 @Override
360 public void showKeyguardErrorMessage(CharSequence message) {
361 if (DEBUG) Slog.d(TAG, "Showing keyguard error message: " + message);
362 Message msg = mHandler.obtainMessage(MSG_SHOW_KEYGUARD_ERROR_MESSAGE);
363 msg.getData().putCharSequence(DATA_MESSAGE, message);
364 msg.sendToTarget();
365 }
Adrian Roos82142c22014-03-27 14:56:59 +0100366 };
367
368 private final ServiceConnection mConnection = new ServiceConnection() {
369 @Override
370 public void onServiceConnected(ComponentName name, IBinder service) {
Adrian Roos5d639782016-07-21 11:43:02 -0700371 if (DEBUG) Slog.d(TAG, "TrustAgent started : " + name.flattenToString());
Adrian Roosc5f95ce2014-07-24 16:00:46 +0200372 mHandler.removeMessages(MSG_RESTART_TIMEOUT);
Adrian Roos82142c22014-03-27 14:56:59 +0100373 mTrustAgentService = ITrustAgentService.Stub.asInterface(service);
Adrian Roos7d59b4f2014-05-27 20:01:31 +0200374 mTrustManagerService.mArchive.logAgentConnected(mUserId, name);
Adrian Roos82142c22014-03-27 14:56:59 +0100375 setCallback(mCallback);
Adrian Roos8f211582014-07-29 15:09:57 +0200376 updateDevicePolicyFeatures();
Adrian Roos481a6df2014-11-20 19:48:56 +0100377
Adrian Roos517b3a42016-03-03 14:58:33 -0800378 if (mPendingSuccessfulUnlock) {
379 onUnlockAttempt(true);
380 mPendingSuccessfulUnlock = false;
381 }
382
Adrian Roos481a6df2014-11-20 19:48:56 +0100383 if (mTrustManagerService.isDeviceLockedInner(mUserId)) {
384 onDeviceLocked();
385 } else {
386 onDeviceUnlocked();
387 }
Adrian Roos82142c22014-03-27 14:56:59 +0100388 }
389
390 @Override
391 public void onServiceDisconnected(ComponentName name) {
Adrian Roos5d639782016-07-21 11:43:02 -0700392 if (DEBUG) Slog.d(TAG, "TrustAgent disconnected : " + name.flattenToShortString());
Adrian Roos82142c22014-03-27 14:56:59 +0100393 mTrustAgentService = null;
Adrian Roos7861c662014-07-25 15:37:28 +0200394 mManagingTrust = false;
Adrian Roos8f211582014-07-29 15:09:57 +0200395 mSetTrustAgentFeaturesToken = null;
Adrian Roos7a4f3d42014-05-02 12:12:20 +0200396 mTrustManagerService.mArchive.logAgentDied(mUserId, name);
Adrian Roos82142c22014-03-27 14:56:59 +0100397 mHandler.sendEmptyMessage(MSG_REVOKE_TRUST);
Adrian Roosc5f95ce2014-07-24 16:00:46 +0200398 if (mBound) {
399 scheduleRestart();
400 }
Jim Miller604e7552014-07-18 19:00:02 -0700401 // mTrustDisabledByDpm maintains state
Jim Miller604e7552014-07-18 19:00:02 -0700402 }
403 };
Adrian Roos82142c22014-03-27 14:56:59 +0100404
405 public TrustAgentWrapper(Context context, TrustManagerService trustManagerService,
406 Intent intent, UserHandle user) {
407 mContext = context;
408 mTrustManagerService = trustManagerService;
Jim Millerd4efaac2014-08-14 18:02:45 -0700409 mAlarmManager = (AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE);
Adrian Roos82142c22014-03-27 14:56:59 +0100410 mUserId = user.getIdentifier();
411 mName = intent.getComponent();
Jim Millerd4efaac2014-08-14 18:02:45 -0700412
413 mAlarmIntent = new Intent(TRUST_EXPIRED_ACTION).putExtra(EXTRA_COMPONENT_NAME, mName);
414 mAlarmIntent.setData(Uri.parse(mAlarmIntent.toUri(Intent.URI_INTENT_SCHEME)));
Jim Miller76b9b8b2014-08-22 17:04:57 -0700415 mAlarmIntent.setPackage(context.getPackageName());
Jim Millerd4efaac2014-08-14 18:02:45 -0700416
417 final IntentFilter alarmFilter = new IntentFilter(TRUST_EXPIRED_ACTION);
418 alarmFilter.addDataScheme(mAlarmIntent.getScheme());
419 final String pathUri = mAlarmIntent.toUri(Intent.URI_INTENT_SCHEME);
420 alarmFilter.addDataPath(pathUri, PatternMatcher.PATTERN_LITERAL);
Jim Millerd4efaac2014-08-14 18:02:45 -0700421
Adrian Roosc5f95ce2014-07-24 16:00:46 +0200422 // Schedules a restart for when connecting times out. If the connection succeeds,
423 // the restart is canceled in mCallback's onConnected.
424 scheduleRestart();
Dianne Hackbornd69e4c12015-04-24 09:54:54 -0700425 mBound = context.bindServiceAsUser(intent, mConnection,
426 Context.BIND_AUTO_CREATE | Context.BIND_FOREGROUND_SERVICE, user);
Adrian Roos1221b062015-03-26 12:29:51 -0700427 if (mBound) {
428 mContext.registerReceiver(mBroadcastReceiver, alarmFilter, PERMISSION, null);
429 } else {
Adrian Roosc5f95ce2014-07-24 16:00:46 +0200430 Log.e(TAG, "Can't bind to TrustAgent " + mName.flattenToShortString());
Adrian Roos82142c22014-03-27 14:56:59 +0100431 }
432 }
433
434 private void onError(Exception e) {
Lingjun Li93a145f2017-01-23 17:13:35 -0800435 Slog.w(TAG , "Exception ", e);
Adrian Roos82142c22014-03-27 14:56:59 +0100436 }
437
Jim Millerd4efaac2014-08-14 18:02:45 -0700438 private void onTrustTimeout() {
439 try {
440 if (mTrustAgentService != null) mTrustAgentService.onTrustTimeout();
441 } catch (RemoteException e) {
442 onError(e);
443 }
444 }
Adrian Roos481a6df2014-11-20 19:48:56 +0100445
Adrian Roos82142c22014-03-27 14:56:59 +0100446 /**
447 * @see android.service.trust.TrustAgentService#onUnlockAttempt(boolean)
448 */
449 public void onUnlockAttempt(boolean successful) {
450 try {
Adrian Roos517b3a42016-03-03 14:58:33 -0800451 if (mTrustAgentService != null) {
452 mTrustAgentService.onUnlockAttempt(successful);
453 } else {
454 mPendingSuccessfulUnlock = successful;
455 }
Adrian Roos82142c22014-03-27 14:56:59 +0100456 } catch (RemoteException e) {
457 onError(e);
458 }
459 }
460
Adrian Roos481a6df2014-11-20 19:48:56 +0100461 /**
Zachary Iqbal327323d2017-01-12 14:41:13 -0800462 * @see android.service.trust.TrustAgentService#onUnlockLockout(int)
463 */
464 public void onUnlockLockout(int timeoutMs) {
465 try {
466 if (mTrustAgentService != null) {
467 mTrustAgentService.onUnlockLockout(timeoutMs);
468 }
469 } catch (RemoteException e) {
470 onError(e);
471 }
472 }
473
474 /**
Adrian Roos481a6df2014-11-20 19:48:56 +0100475 * @see android.service.trust.TrustAgentService#onDeviceLocked()
476 */
477 public void onDeviceLocked() {
478 try {
479 if (mTrustAgentService != null) mTrustAgentService.onDeviceLocked();
480 } catch (RemoteException e) {
481 onError(e);
482 }
483 }
484
485 /**
486 * @see android.service.trust.TrustAgentService#onDeviceUnlocked()
487 */
488 public void onDeviceUnlocked() {
489 try {
490 if (mTrustAgentService != null) mTrustAgentService.onDeviceUnlocked();
491 } catch (RemoteException e) {
492 onError(e);
493 }
494 }
495
Ram Periathiruvadi32d53552019-02-19 13:25:46 -0800496 /**
497 * @see android.service.trust.TrustAgentService#onTokenStateReceived()
498 *
499 */
500 public void onEscrowTokenActivated(long handle, int userId) {
501 if (DEBUG) Slog.d(TAG, "onEscrowTokenActivated: " + handle + " user: " + userId);
502 if (mTrustAgentService != null) {
503 try {
504 mTrustAgentService.onTokenStateReceived(handle,
505 TrustAgentService.TOKEN_STATE_ACTIVE);
506 } catch (RemoteException e) {
507 onError(e);
508 }
509 }
510 }
Adrian Roos82142c22014-03-27 14:56:59 +0100511 private void setCallback(ITrustAgentServiceCallback callback) {
512 try {
513 if (mTrustAgentService != null) {
514 mTrustAgentService.setCallback(callback);
515 }
516 } catch (RemoteException e) {
517 onError(e);
518 }
519 }
520
Adrian Roos8f211582014-07-29 15:09:57 +0200521 boolean updateDevicePolicyFeatures() {
Jim Miller604e7552014-07-18 19:00:02 -0700522 boolean trustDisabled = false;
Adrian Roos5d639782016-07-21 11:43:02 -0700523 if (DEBUG) Slog.d(TAG, "updateDevicePolicyFeatures(" + mName + ")");
Jim Miller604e7552014-07-18 19:00:02 -0700524 try {
525 if (mTrustAgentService != null) {
526 DevicePolicyManager dpm =
527 (DevicePolicyManager) mContext.getSystemService(Context.DEVICE_POLICY_SERVICE);
Adrian Roos8f211582014-07-29 15:09:57 +0200528
Jim Millere303bf42014-08-26 17:12:29 -0700529 if ((dpm.getKeyguardDisabledFeatures(null, mUserId)
Adrian Roos8f211582014-07-29 15:09:57 +0200530 & DevicePolicyManager.KEYGUARD_DISABLE_TRUST_AGENTS) != 0) {
Jim Millere303bf42014-08-26 17:12:29 -0700531 List<PersistableBundle> config = dpm.getTrustAgentConfiguration(
532 null, mName, mUserId);
Adrian Roos8f211582014-07-29 15:09:57 +0200533 trustDisabled = true;
Adrian Roos5d639782016-07-21 11:43:02 -0700534 if (DEBUG) Slog.d(TAG, "Detected trust agents disabled. Config = " + config);
Jim Millere303bf42014-08-26 17:12:29 -0700535 if (config != null && config.size() > 0) {
Adrian Roos8f211582014-07-29 15:09:57 +0200536 if (DEBUG) {
Adrian Roos5d639782016-07-21 11:43:02 -0700537 Slog.d(TAG, "TrustAgent " + mName.flattenToShortString()
Jim Millere303bf42014-08-26 17:12:29 -0700538 + " disabled until it acknowledges "+ config);
Jim Miller604e7552014-07-18 19:00:02 -0700539 }
Adrian Roos8f211582014-07-29 15:09:57 +0200540 mSetTrustAgentFeaturesToken = new Binder();
Jim Millere303bf42014-08-26 17:12:29 -0700541 mTrustAgentService.onConfigure(config, mSetTrustAgentFeaturesToken);
Jim Miller604e7552014-07-18 19:00:02 -0700542 }
Adrian Roosa43fd032015-03-09 19:10:15 +0100543 } else {
544 mTrustAgentService.onConfigure(Collections.EMPTY_LIST, null);
Jim Miller604e7552014-07-18 19:00:02 -0700545 }
Pavel Grafov28939982017-10-03 15:11:52 +0100546 final long maxTimeToLock = dpm.getMaximumTimeToLock(null, mUserId);
Jim Miller76b9b8b2014-08-22 17:04:57 -0700547 if (maxTimeToLock != mMaximumTimeToLock) {
548 // If the timeout changes, cancel the alarm and send a timeout event to have
549 // the agent re-evaluate trust.
550 mMaximumTimeToLock = maxTimeToLock;
551 if (mAlarmPendingIntent != null) {
552 mAlarmManager.cancel(mAlarmPendingIntent);
553 mAlarmPendingIntent = null;
554 mHandler.sendEmptyMessage(MSG_TRUST_TIMEOUT);
555 }
556 }
Jim Miller604e7552014-07-18 19:00:02 -0700557 }
558 } catch (RemoteException e) {
559 onError(e);
560 }
561 if (mTrustDisabledByDpm != trustDisabled) {
562 mTrustDisabledByDpm = trustDisabled;
Adrian Roos94e15a52015-04-16 12:23:18 -0700563 mTrustManagerService.updateTrust(mUserId, 0);
Jim Miller604e7552014-07-18 19:00:02 -0700564 }
565 return trustDisabled;
566 }
567
Adrian Roos82142c22014-03-27 14:56:59 +0100568 public boolean isTrusted() {
Adrian Roos7861c662014-07-25 15:37:28 +0200569 return mTrusted && mManagingTrust && !mTrustDisabledByDpm;
570 }
571
572 public boolean isManagingTrust() {
573 return mManagingTrust && !mTrustDisabledByDpm;
Adrian Roos82142c22014-03-27 14:56:59 +0100574 }
575
Adrian Roos7e03dfc2014-05-16 16:06:28 +0200576 public CharSequence getMessage() {
Adrian Roos82142c22014-03-27 14:56:59 +0100577 return mMessage;
578 }
579
Adrian Roosfc29e0b2014-11-11 12:55:44 +0100580 public void destroy() {
581 mHandler.removeMessages(MSG_RESTART_TIMEOUT);
582
Adrian Roosc5f95ce2014-07-24 16:00:46 +0200583 if (!mBound) {
584 return;
585 }
Adrian Roos5d639782016-07-21 11:43:02 -0700586 if (DEBUG) Slog.d(TAG, "TrustAgent unbound : " + mName.flattenToShortString());
Adrian Roos7d59b4f2014-05-27 20:01:31 +0200587 mTrustManagerService.mArchive.logAgentStopped(mUserId, mName);
Adrian Roos82142c22014-03-27 14:56:59 +0100588 mContext.unbindService(mConnection);
Adrian Roosc5f95ce2014-07-24 16:00:46 +0200589 mBound = false;
Adrian Roos1221b062015-03-26 12:29:51 -0700590 mContext.unregisterReceiver(mBroadcastReceiver);
Adrian Roos7d59b4f2014-05-27 20:01:31 +0200591 mTrustAgentService = null;
Adrian Roos8f211582014-07-29 15:09:57 +0200592 mSetTrustAgentFeaturesToken = null;
Adrian Roos7d59b4f2014-05-27 20:01:31 +0200593 mHandler.sendEmptyMessage(MSG_REVOKE_TRUST);
Adrian Roos82142c22014-03-27 14:56:59 +0100594 }
Adrian Roos7a4f3d42014-05-02 12:12:20 +0200595
596 public boolean isConnected() {
597 return mTrustAgentService != null;
598 }
Adrian Roosc5f95ce2014-07-24 16:00:46 +0200599
600 public boolean isBound() {
601 return mBound;
602 }
603
604 /**
605 * If not connected, returns the time at which the agent is restarted.
606 *
607 * @return restart time in uptime millis.
608 */
609 public long getScheduledRestartUptimeMillis() {
610 return mScheduledRestartUptimeMillis;
611 }
612
613 private void scheduleRestart() {
614 mHandler.removeMessages(MSG_RESTART_TIMEOUT);
615 mScheduledRestartUptimeMillis = SystemClock.uptimeMillis() + RESTART_TIMEOUT_MILLIS;
616 mHandler.sendEmptyMessageAtTime(MSG_RESTART_TIMEOUT, mScheduledRestartUptimeMillis);
617 }
Adrian Roos82142c22014-03-27 14:56:59 +0100618}