blob: 28fee4eaa9a82856343d9fcc664cc861a62d9584 [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
46import com.android.internal.policy.IKeyguardDismissCallback;
47
Adrian Roosa43fd032015-03-09 19:10:15 +010048import java.util.Collections;
Jim Miller604e7552014-07-18 19:00:02 -070049import java.util.List;
Adrian Roos82142c22014-03-27 14:56:59 +010050
51/**
52 * A wrapper around a TrustAgentService interface. Coordinates communication between
53 * TrustManager and the actual TrustAgent.
54 */
Lingjun Li93a145f2017-01-23 17:13:35 -080055@TargetApi(Build.VERSION_CODES.LOLLIPOP)
Adrian Roos82142c22014-03-27 14:56:59 +010056public class TrustAgentWrapper {
Jim Millerd4efaac2014-08-14 18:02:45 -070057 private static final String EXTRA_COMPONENT_NAME = "componentName";
58 private static final String TRUST_EXPIRED_ACTION = "android.server.trust.TRUST_EXPIRED_ACTION";
Jim Miller76b9b8b2014-08-22 17:04:57 -070059 private static final String PERMISSION = android.Manifest.permission.PROVIDE_TRUST_AGENT;
Adrian Roos5d639782016-07-21 11:43:02 -070060 private static final boolean DEBUG = TrustManagerService.DEBUG;
Adrian Roos82142c22014-03-27 14:56:59 +010061 private static final String TAG = "TrustAgentWrapper";
62
Adrian Roos7a4f3d42014-05-02 12:12:20 +020063 private static final int MSG_GRANT_TRUST = 1;
Adrian Roos82142c22014-03-27 14:56:59 +010064 private static final int MSG_REVOKE_TRUST = 2;
65 private static final int MSG_TRUST_TIMEOUT = 3;
Adrian Roosc5f95ce2014-07-24 16:00:46 +020066 private static final int MSG_RESTART_TIMEOUT = 4;
Adrian Roos8f211582014-07-29 15:09:57 +020067 private static final int MSG_SET_TRUST_AGENT_FEATURES_COMPLETED = 5;
Adrian Roos7861c662014-07-25 15:37:28 +020068 private static final int MSG_MANAGING_TRUST = 6;
Lingjun Li93a145f2017-01-23 17:13:35 -080069 private static final int MSG_ADD_ESCROW_TOKEN = 7;
70 private static final int MSG_REMOVE_ESCROW_TOKEN = 8;
71 private static final int MSG_ESCROW_TOKEN_STATE = 9;
72 private static final int MSG_UNLOCK_USER = 10;
Lucas Dupinef886542018-01-03 16:03:07 -080073 private static final int MSG_SHOW_KEYGUARD_ERROR_MESSAGE = 11;
Adrian Roosc5f95ce2014-07-24 16:00:46 +020074
75 /**
76 * Time in uptime millis that we wait for the service connection, both when starting
77 * and when the service disconnects.
78 */
79 private static final long RESTART_TIMEOUT_MILLIS = 5 * 60000;
Adrian Roos82142c22014-03-27 14:56:59 +010080
Adrian Roos7a4f3d42014-05-02 12:12:20 +020081 /**
82 * Long extra for {@link #MSG_GRANT_TRUST}
83 */
84 private static final String DATA_DURATION = "duration";
Lingjun Li93a145f2017-01-23 17:13:35 -080085 private static final String DATA_ESCROW_TOKEN = "escrow_token";
86 private static final String DATA_HANDLE = "handle";
87 private static final String DATA_USER_ID = "user_id";
Lucas Dupinef886542018-01-03 16:03:07 -080088 private static final String DATA_MESSAGE = "message";
Adrian Roos7a4f3d42014-05-02 12:12:20 +020089
Adrian Roos82142c22014-03-27 14:56:59 +010090 private final TrustManagerService mTrustManagerService;
91 private final int mUserId;
92 private final Context mContext;
93 private final ComponentName mName;
94
95 private ITrustAgentService mTrustAgentService;
Adrian Roosc5f95ce2014-07-24 16:00:46 +020096 private boolean mBound;
97 private long mScheduledRestartUptimeMillis;
Jim Miller76b9b8b2014-08-22 17:04:57 -070098 private long mMaximumTimeToLock; // from DevicePolicyManager
Adrian Roos517b3a42016-03-03 14:58:33 -080099 private boolean mPendingSuccessfulUnlock = false;
Adrian Roos82142c22014-03-27 14:56:59 +0100100
101 // Trust state
102 private boolean mTrusted;
Adrian Roos7e03dfc2014-05-16 16:06:28 +0200103 private CharSequence mMessage;
Jim Miller604e7552014-07-18 19:00:02 -0700104 private boolean mTrustDisabledByDpm;
Adrian Roos7861c662014-07-25 15:37:28 +0200105 private boolean mManagingTrust;
Adrian Roos8f211582014-07-29 15:09:57 +0200106 private IBinder mSetTrustAgentFeaturesToken;
Jim Millerd4efaac2014-08-14 18:02:45 -0700107 private AlarmManager mAlarmManager;
108 private final Intent mAlarmIntent;
Jim Miller76b9b8b2014-08-22 17:04:57 -0700109 private PendingIntent mAlarmPendingIntent;
Jim Millerd4efaac2014-08-14 18:02:45 -0700110
111 private final BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
112 @Override
113 public void onReceive(Context context, Intent intent) {
114 ComponentName component = intent.getParcelableExtra(EXTRA_COMPONENT_NAME);
115 if (TRUST_EXPIRED_ACTION.equals(intent.getAction())
116 && mName.equals(component)) {
117 mHandler.removeMessages(MSG_TRUST_TIMEOUT);
118 mHandler.sendEmptyMessage(MSG_TRUST_TIMEOUT);
119 }
120 }
121 };
Adrian Roos82142c22014-03-27 14:56:59 +0100122
123 private final Handler mHandler = new Handler() {
124 @Override
125 public void handleMessage(Message msg) {
126 switch (msg.what) {
Adrian Roos7a4f3d42014-05-02 12:12:20 +0200127 case MSG_GRANT_TRUST:
Adrian Roos7d59b4f2014-05-27 20:01:31 +0200128 if (!isConnected()) {
129 Log.w(TAG, "Agent is not connected, cannot grant trust: "
130 + mName.flattenToShortString());
131 return;
132 }
Adrian Roos82142c22014-03-27 14:56:59 +0100133 mTrusted = true;
Adrian Roos7e03dfc2014-05-16 16:06:28 +0200134 mMessage = (CharSequence) msg.obj;
Adrian Roos94e15a52015-04-16 12:23:18 -0700135 int flags = msg.arg1;
Adrian Roos7a4f3d42014-05-02 12:12:20 +0200136 long durationMs = msg.getData().getLong(DATA_DURATION);
137 if (durationMs > 0) {
Jim Miller76b9b8b2014-08-22 17:04:57 -0700138 final long duration;
139 if (mMaximumTimeToLock != 0) {
140 // Enforce DevicePolicyManager timeout. This is here as a safeguard to
141 // ensure trust agents are evaluating trust state at least as often as
142 // the policy dictates. Admins that want more guarantees should be using
143 // DevicePolicyManager#KEYGUARD_DISABLE_TRUST_AGENTS.
144 duration = Math.min(durationMs, mMaximumTimeToLock);
145 if (DEBUG) {
Adrian Roos5d639782016-07-21 11:43:02 -0700146 Slog.d(TAG, "DPM lock timeout in effect. Timeout adjusted from "
Jim Miller76b9b8b2014-08-22 17:04:57 -0700147 + durationMs + " to " + duration);
148 }
149 } else {
150 duration = durationMs;
151 }
152 long expiration = SystemClock.elapsedRealtime() + duration;
153 mAlarmPendingIntent = PendingIntent.getBroadcast(mContext, 0, mAlarmIntent,
Jim Millerd4efaac2014-08-14 18:02:45 -0700154 PendingIntent.FLAG_CANCEL_CURRENT);
Jim Miller76b9b8b2014-08-22 17:04:57 -0700155 mAlarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, expiration,
156 mAlarmPendingIntent);
Adrian Roos7a4f3d42014-05-02 12:12:20 +0200157 }
158 mTrustManagerService.mArchive.logGrantTrust(mUserId, mName,
159 (mMessage != null ? mMessage.toString() : null),
Adrian Roos94e15a52015-04-16 12:23:18 -0700160 durationMs, flags);
161 mTrustManagerService.updateTrust(mUserId, flags);
Adrian Roos82142c22014-03-27 14:56:59 +0100162 break;
163 case MSG_TRUST_TIMEOUT:
Adrian Roos5d639782016-07-21 11:43:02 -0700164 if (DEBUG) Slog.d(TAG, "Trust timed out : " + mName.flattenToShortString());
Adrian Roos7a4f3d42014-05-02 12:12:20 +0200165 mTrustManagerService.mArchive.logTrustTimeout(mUserId, mName);
Jim Millerd4efaac2014-08-14 18:02:45 -0700166 onTrustTimeout();
Adrian Roos82142c22014-03-27 14:56:59 +0100167 // Fall through.
168 case MSG_REVOKE_TRUST:
169 mTrusted = false;
170 mMessage = null;
Adrian Roos7a4f3d42014-05-02 12:12:20 +0200171 mHandler.removeMessages(MSG_TRUST_TIMEOUT);
172 if (msg.what == MSG_REVOKE_TRUST) {
173 mTrustManagerService.mArchive.logRevokeTrust(mUserId, mName);
174 }
Adrian Roos94e15a52015-04-16 12:23:18 -0700175 mTrustManagerService.updateTrust(mUserId, 0);
Adrian Roos82142c22014-03-27 14:56:59 +0100176 break;
Adrian Roosc5f95ce2014-07-24 16:00:46 +0200177 case MSG_RESTART_TIMEOUT:
Adrian Roos5d639782016-07-21 11:43:02 -0700178 Slog.w(TAG, "Connection attempt to agent " + mName.flattenToShortString()
179 + " timed out, rebinding");
Adrian Roosfc29e0b2014-11-11 12:55:44 +0100180 destroy();
Adrian Roosc5f95ce2014-07-24 16:00:46 +0200181 mTrustManagerService.resetAgent(mName, mUserId);
182 break;
Adrian Roos8f211582014-07-29 15:09:57 +0200183 case MSG_SET_TRUST_AGENT_FEATURES_COMPLETED:
184 IBinder token = (IBinder) msg.obj;
185 boolean result = msg.arg1 != 0;
186 if (mSetTrustAgentFeaturesToken == token) {
187 mSetTrustAgentFeaturesToken = null;
188 if (mTrustDisabledByDpm && result) {
Adrian Roos5d639782016-07-21 11:43:02 -0700189 if (DEBUG) Slog.d(TAG, "Re-enabling agent because it acknowledged "
190 + "enabled features: " + mName.flattenToShortString());
Adrian Roos8f211582014-07-29 15:09:57 +0200191 mTrustDisabledByDpm = false;
Adrian Roos94e15a52015-04-16 12:23:18 -0700192 mTrustManagerService.updateTrust(mUserId, 0);
Adrian Roos8f211582014-07-29 15:09:57 +0200193 }
194 } else {
Adrian Roos5d639782016-07-21 11:43:02 -0700195 if (DEBUG) Slog.w(TAG, "Ignoring MSG_SET_TRUST_AGENT_FEATURES_COMPLETED "
196 + "with obsolete token: " + mName.flattenToShortString());
Adrian Roos8f211582014-07-29 15:09:57 +0200197 }
Jim Miller604e7552014-07-18 19:00:02 -0700198 break;
Adrian Roos7861c662014-07-25 15:37:28 +0200199 case MSG_MANAGING_TRUST:
200 mManagingTrust = msg.arg1 != 0;
201 if (!mManagingTrust) {
202 mTrusted = false;
203 mMessage = null;
204 }
205 mTrustManagerService.mArchive.logManagingTrust(mUserId, mName, mManagingTrust);
Adrian Roos94e15a52015-04-16 12:23:18 -0700206 mTrustManagerService.updateTrust(mUserId, 0);
Adrian Roos7861c662014-07-25 15:37:28 +0200207 break;
Lingjun Li93a145f2017-01-23 17:13:35 -0800208 case MSG_ADD_ESCROW_TOKEN: {
209 byte[] eToken = msg.getData().getByteArray(DATA_ESCROW_TOKEN);
210 int userId = msg.getData().getInt(DATA_USER_ID);
211 long handle = mTrustManagerService.addEscrowToken(eToken, userId);
Lingjun Li44196d32017-02-17 18:32:23 -0800212 boolean resultDeliverred = false;
Lingjun Li93a145f2017-01-23 17:13:35 -0800213 try {
Lingjun Li44196d32017-02-17 18:32:23 -0800214 if (mTrustAgentService != null) {
215 mTrustAgentService.onEscrowTokenAdded(
216 eToken, handle, UserHandle.of(userId));
217 resultDeliverred = true;
218 }
Lingjun Li93a145f2017-01-23 17:13:35 -0800219 } catch (RemoteException e) {
220 onError(e);
221 }
Lingjun Li44196d32017-02-17 18:32:23 -0800222
223 if (!resultDeliverred) {
224 mTrustManagerService.removeEscrowToken(handle, userId);
225 }
Lingjun Li93a145f2017-01-23 17:13:35 -0800226 break;
227 }
228 case MSG_ESCROW_TOKEN_STATE: {
229 long handle = msg.getData().getLong(DATA_HANDLE);
230 int userId = msg.getData().getInt(DATA_USER_ID);
231 boolean active = mTrustManagerService.isEscrowTokenActive(handle, userId);
232 try {
Lingjun Li44196d32017-02-17 18:32:23 -0800233 if (mTrustAgentService != null) {
234 mTrustAgentService.onTokenStateReceived(handle,
235 active ? TrustAgentService.TOKEN_STATE_ACTIVE
236 : TrustAgentService.TOKEN_STATE_INACTIVE);
237 }
Lingjun Li93a145f2017-01-23 17:13:35 -0800238 } catch (RemoteException e) {
239 onError(e);
240 }
241 break;
242 }
243 case MSG_REMOVE_ESCROW_TOKEN: {
244 long handle = msg.getData().getLong(DATA_HANDLE);
245 int userId = msg.getData().getInt(DATA_USER_ID);
246 boolean success = mTrustManagerService.removeEscrowToken(handle, userId);
247 try {
Lingjun Li44196d32017-02-17 18:32:23 -0800248 if (mTrustAgentService != null) {
249 mTrustAgentService.onEscrowTokenRemoved(handle, success);
250 }
Lingjun Li93a145f2017-01-23 17:13:35 -0800251 } catch (RemoteException e) {
252 onError(e);
253 }
254 break;
255 }
256 case MSG_UNLOCK_USER: {
257 long handle = msg.getData().getLong(DATA_HANDLE);
258 int userId = msg.getData().getInt(DATA_USER_ID);
259 byte[] eToken = msg.getData().getByteArray(DATA_ESCROW_TOKEN);
260 mTrustManagerService.unlockUserWithToken(handle, eToken, userId);
261 break;
262 }
Lucas Dupinef886542018-01-03 16:03:07 -0800263 case MSG_SHOW_KEYGUARD_ERROR_MESSAGE: {
264 CharSequence message = msg.getData().getCharSequence(DATA_MESSAGE);
265 mTrustManagerService.showKeyguardErrorMessage(message);
266 break;
267 }
Adrian Roos82142c22014-03-27 14:56:59 +0100268 }
269 }
270 };
271
272 private ITrustAgentServiceCallback mCallback = new ITrustAgentServiceCallback.Stub() {
273
Adrian Roos7e03dfc2014-05-16 16:06:28 +0200274 @Override
Adrian Roos94e15a52015-04-16 12:23:18 -0700275 public void grantTrust(CharSequence userMessage, long durationMs, int flags) {
Adrian Roos5d639782016-07-21 11:43:02 -0700276 if (DEBUG) Slog.d(TAG, "enableTrust(" + userMessage + ", durationMs = " + durationMs
Adrian Roos94e15a52015-04-16 12:23:18 -0700277 + ", flags = " + flags + ")");
Adrian Roos82142c22014-03-27 14:56:59 +0100278
Adrian Roos7a4f3d42014-05-02 12:12:20 +0200279 Message msg = mHandler.obtainMessage(
Adrian Roos94e15a52015-04-16 12:23:18 -0700280 MSG_GRANT_TRUST, flags, 0, userMessage);
Adrian Roos7a4f3d42014-05-02 12:12:20 +0200281 msg.getData().putLong(DATA_DURATION, durationMs);
282 msg.sendToTarget();
Adrian Roos82142c22014-03-27 14:56:59 +0100283 }
284
Adrian Roos7e03dfc2014-05-16 16:06:28 +0200285 @Override
Adrian Roos82142c22014-03-27 14:56:59 +0100286 public void revokeTrust() {
Adrian Roos5d639782016-07-21 11:43:02 -0700287 if (DEBUG) Slog.d(TAG, "revokeTrust()");
Adrian Roos82142c22014-03-27 14:56:59 +0100288 mHandler.sendEmptyMessage(MSG_REVOKE_TRUST);
289 }
Adrian Roos7861c662014-07-25 15:37:28 +0200290
291 @Override
292 public void setManagingTrust(boolean managingTrust) {
Adrian Roos5d639782016-07-21 11:43:02 -0700293 if (DEBUG) Slog.d(TAG, "managingTrust()");
Adrian Roos7861c662014-07-25 15:37:28 +0200294 mHandler.obtainMessage(MSG_MANAGING_TRUST, managingTrust ? 1 : 0, 0).sendToTarget();
295 }
Adrian Roos8f211582014-07-29 15:09:57 +0200296
297 @Override
Jim Millere303bf42014-08-26 17:12:29 -0700298 public void onConfigureCompleted(boolean result, IBinder token) {
Adrian Roos5d639782016-07-21 11:43:02 -0700299 if (DEBUG) Slog.d(TAG, "onSetTrustAgentFeaturesEnabledCompleted(result=" + result);
Adrian Roos8f211582014-07-29 15:09:57 +0200300 mHandler.obtainMessage(MSG_SET_TRUST_AGENT_FEATURES_COMPLETED,
301 result ? 1 : 0, 0, token).sendToTarget();
302 }
Lingjun Li93a145f2017-01-23 17:13:35 -0800303
304 @Override
305 public void addEscrowToken(byte[] token, int userId) {
306 if (mContext.getResources()
307 .getBoolean(com.android.internal.R.bool.config_allowEscrowTokenForTrustAgent)) {
Lingjun Li44196d32017-02-17 18:32:23 -0800308 throw new SecurityException("Escrow token API is not allowed.");
Lingjun Li93a145f2017-01-23 17:13:35 -0800309 }
310
311 if (DEBUG) Slog.d(TAG, "adding escrow token for user " + userId);
312 Message msg = mHandler.obtainMessage(MSG_ADD_ESCROW_TOKEN);
313 msg.getData().putByteArray(DATA_ESCROW_TOKEN, token);
314 msg.getData().putInt(DATA_USER_ID, userId);
315 msg.sendToTarget();
316 }
317
318 @Override
319 public void isEscrowTokenActive(long handle, int userId) {
320 if (mContext.getResources()
321 .getBoolean(com.android.internal.R.bool.config_allowEscrowTokenForTrustAgent)) {
Lingjun Li44196d32017-02-17 18:32:23 -0800322 throw new SecurityException("Escrow token API is not allowed.");
Lingjun Li93a145f2017-01-23 17:13:35 -0800323 }
324
325 if (DEBUG) Slog.d(TAG, "checking the state of escrow token on user " + userId);
326 Message msg = mHandler.obtainMessage(MSG_ESCROW_TOKEN_STATE);
327 msg.getData().putLong(DATA_HANDLE, handle);
328 msg.getData().putInt(DATA_USER_ID, userId);
329 msg.sendToTarget();
330 }
331
332 @Override
333 public void removeEscrowToken(long handle, int userId) {
334 if (mContext.getResources()
335 .getBoolean(com.android.internal.R.bool.config_allowEscrowTokenForTrustAgent)) {
Lingjun Li44196d32017-02-17 18:32:23 -0800336 throw new SecurityException("Escrow token API is not allowed.");
Lingjun Li93a145f2017-01-23 17:13:35 -0800337 }
338
339 if (DEBUG) Slog.d(TAG, "removing escrow token on user " + userId);
340 Message msg = mHandler.obtainMessage(MSG_REMOVE_ESCROW_TOKEN);
341 msg.getData().putLong(DATA_HANDLE, handle);
342 msg.getData().putInt(DATA_USER_ID, userId);
343 msg.sendToTarget();
344 }
345
346 @Override
347 public void unlockUserWithToken(long handle, byte[] token, int userId) {
348 if (mContext.getResources()
349 .getBoolean(com.android.internal.R.bool.config_allowEscrowTokenForTrustAgent)) {
Lingjun Li44196d32017-02-17 18:32:23 -0800350 throw new SecurityException("Escrow token API is not allowed.");
Lingjun Li93a145f2017-01-23 17:13:35 -0800351 }
352
353 if (DEBUG) Slog.d(TAG, "unlocking user " + userId);
354 Message msg = mHandler.obtainMessage(MSG_UNLOCK_USER);
355 msg.getData().putInt(DATA_USER_ID, userId);
356 msg.getData().putLong(DATA_HANDLE, handle);
357 msg.getData().putByteArray(DATA_ESCROW_TOKEN, token);
358 msg.sendToTarget();
359 }
Lucas Dupinef886542018-01-03 16:03:07 -0800360
361 @Override
362 public void showKeyguardErrorMessage(CharSequence message) {
363 if (DEBUG) Slog.d(TAG, "Showing keyguard error message: " + message);
364 Message msg = mHandler.obtainMessage(MSG_SHOW_KEYGUARD_ERROR_MESSAGE);
365 msg.getData().putCharSequence(DATA_MESSAGE, message);
366 msg.sendToTarget();
367 }
Adrian Roos82142c22014-03-27 14:56:59 +0100368 };
369
370 private final ServiceConnection mConnection = new ServiceConnection() {
371 @Override
372 public void onServiceConnected(ComponentName name, IBinder service) {
Adrian Roos5d639782016-07-21 11:43:02 -0700373 if (DEBUG) Slog.d(TAG, "TrustAgent started : " + name.flattenToString());
Adrian Roosc5f95ce2014-07-24 16:00:46 +0200374 mHandler.removeMessages(MSG_RESTART_TIMEOUT);
Adrian Roos82142c22014-03-27 14:56:59 +0100375 mTrustAgentService = ITrustAgentService.Stub.asInterface(service);
Adrian Roos7d59b4f2014-05-27 20:01:31 +0200376 mTrustManagerService.mArchive.logAgentConnected(mUserId, name);
Adrian Roos82142c22014-03-27 14:56:59 +0100377 setCallback(mCallback);
Adrian Roos8f211582014-07-29 15:09:57 +0200378 updateDevicePolicyFeatures();
Adrian Roos481a6df2014-11-20 19:48:56 +0100379
Adrian Roos517b3a42016-03-03 14:58:33 -0800380 if (mPendingSuccessfulUnlock) {
381 onUnlockAttempt(true);
382 mPendingSuccessfulUnlock = false;
383 }
384
Adrian Roos481a6df2014-11-20 19:48:56 +0100385 if (mTrustManagerService.isDeviceLockedInner(mUserId)) {
386 onDeviceLocked();
387 } else {
388 onDeviceUnlocked();
389 }
Adrian Roos82142c22014-03-27 14:56:59 +0100390 }
391
392 @Override
393 public void onServiceDisconnected(ComponentName name) {
Adrian Roos5d639782016-07-21 11:43:02 -0700394 if (DEBUG) Slog.d(TAG, "TrustAgent disconnected : " + name.flattenToShortString());
Adrian Roos82142c22014-03-27 14:56:59 +0100395 mTrustAgentService = null;
Adrian Roos7861c662014-07-25 15:37:28 +0200396 mManagingTrust = false;
Adrian Roos8f211582014-07-29 15:09:57 +0200397 mSetTrustAgentFeaturesToken = null;
Adrian Roos7a4f3d42014-05-02 12:12:20 +0200398 mTrustManagerService.mArchive.logAgentDied(mUserId, name);
Adrian Roos82142c22014-03-27 14:56:59 +0100399 mHandler.sendEmptyMessage(MSG_REVOKE_TRUST);
Adrian Roosc5f95ce2014-07-24 16:00:46 +0200400 if (mBound) {
401 scheduleRestart();
402 }
Jim Miller604e7552014-07-18 19:00:02 -0700403 // mTrustDisabledByDpm maintains state
Jim Miller604e7552014-07-18 19:00:02 -0700404 }
405 };
Adrian Roos82142c22014-03-27 14:56:59 +0100406
407 public TrustAgentWrapper(Context context, TrustManagerService trustManagerService,
408 Intent intent, UserHandle user) {
409 mContext = context;
410 mTrustManagerService = trustManagerService;
Jim Millerd4efaac2014-08-14 18:02:45 -0700411 mAlarmManager = (AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE);
Adrian Roos82142c22014-03-27 14:56:59 +0100412 mUserId = user.getIdentifier();
413 mName = intent.getComponent();
Jim Millerd4efaac2014-08-14 18:02:45 -0700414
415 mAlarmIntent = new Intent(TRUST_EXPIRED_ACTION).putExtra(EXTRA_COMPONENT_NAME, mName);
416 mAlarmIntent.setData(Uri.parse(mAlarmIntent.toUri(Intent.URI_INTENT_SCHEME)));
Jim Miller76b9b8b2014-08-22 17:04:57 -0700417 mAlarmIntent.setPackage(context.getPackageName());
Jim Millerd4efaac2014-08-14 18:02:45 -0700418
419 final IntentFilter alarmFilter = new IntentFilter(TRUST_EXPIRED_ACTION);
420 alarmFilter.addDataScheme(mAlarmIntent.getScheme());
421 final String pathUri = mAlarmIntent.toUri(Intent.URI_INTENT_SCHEME);
422 alarmFilter.addDataPath(pathUri, PatternMatcher.PATTERN_LITERAL);
Jim Millerd4efaac2014-08-14 18:02:45 -0700423
Adrian Roosc5f95ce2014-07-24 16:00:46 +0200424 // Schedules a restart for when connecting times out. If the connection succeeds,
425 // the restart is canceled in mCallback's onConnected.
426 scheduleRestart();
Dianne Hackbornd69e4c12015-04-24 09:54:54 -0700427 mBound = context.bindServiceAsUser(intent, mConnection,
428 Context.BIND_AUTO_CREATE | Context.BIND_FOREGROUND_SERVICE, user);
Adrian Roos1221b062015-03-26 12:29:51 -0700429 if (mBound) {
430 mContext.registerReceiver(mBroadcastReceiver, alarmFilter, PERMISSION, null);
431 } else {
Adrian Roosc5f95ce2014-07-24 16:00:46 +0200432 Log.e(TAG, "Can't bind to TrustAgent " + mName.flattenToShortString());
Adrian Roos82142c22014-03-27 14:56:59 +0100433 }
434 }
435
436 private void onError(Exception e) {
Lingjun Li93a145f2017-01-23 17:13:35 -0800437 Slog.w(TAG , "Exception ", e);
Adrian Roos82142c22014-03-27 14:56:59 +0100438 }
439
Jim Millerd4efaac2014-08-14 18:02:45 -0700440 private void onTrustTimeout() {
441 try {
442 if (mTrustAgentService != null) mTrustAgentService.onTrustTimeout();
443 } catch (RemoteException e) {
444 onError(e);
445 }
446 }
Adrian Roos481a6df2014-11-20 19:48:56 +0100447
Adrian Roos82142c22014-03-27 14:56:59 +0100448 /**
449 * @see android.service.trust.TrustAgentService#onUnlockAttempt(boolean)
450 */
451 public void onUnlockAttempt(boolean successful) {
452 try {
Adrian Roos517b3a42016-03-03 14:58:33 -0800453 if (mTrustAgentService != null) {
454 mTrustAgentService.onUnlockAttempt(successful);
455 } else {
456 mPendingSuccessfulUnlock = successful;
457 }
Adrian Roos82142c22014-03-27 14:56:59 +0100458 } catch (RemoteException e) {
459 onError(e);
460 }
461 }
462
Adrian Roos481a6df2014-11-20 19:48:56 +0100463 /**
Zachary Iqbal327323d2017-01-12 14:41:13 -0800464 * @see android.service.trust.TrustAgentService#onUnlockLockout(int)
465 */
466 public void onUnlockLockout(int timeoutMs) {
467 try {
468 if (mTrustAgentService != null) {
469 mTrustAgentService.onUnlockLockout(timeoutMs);
470 }
471 } catch (RemoteException e) {
472 onError(e);
473 }
474 }
475
476 /**
Adrian Roos481a6df2014-11-20 19:48:56 +0100477 * @see android.service.trust.TrustAgentService#onDeviceLocked()
478 */
479 public void onDeviceLocked() {
480 try {
481 if (mTrustAgentService != null) mTrustAgentService.onDeviceLocked();
482 } catch (RemoteException e) {
483 onError(e);
484 }
485 }
486
487 /**
488 * @see android.service.trust.TrustAgentService#onDeviceUnlocked()
489 */
490 public void onDeviceUnlocked() {
491 try {
492 if (mTrustAgentService != null) mTrustAgentService.onDeviceUnlocked();
493 } catch (RemoteException e) {
494 onError(e);
495 }
496 }
497
Adrian Roos82142c22014-03-27 14:56:59 +0100498 private void setCallback(ITrustAgentServiceCallback callback) {
499 try {
500 if (mTrustAgentService != null) {
501 mTrustAgentService.setCallback(callback);
502 }
503 } catch (RemoteException e) {
504 onError(e);
505 }
506 }
507
Adrian Roos8f211582014-07-29 15:09:57 +0200508 boolean updateDevicePolicyFeatures() {
Jim Miller604e7552014-07-18 19:00:02 -0700509 boolean trustDisabled = false;
Adrian Roos5d639782016-07-21 11:43:02 -0700510 if (DEBUG) Slog.d(TAG, "updateDevicePolicyFeatures(" + mName + ")");
Jim Miller604e7552014-07-18 19:00:02 -0700511 try {
512 if (mTrustAgentService != null) {
513 DevicePolicyManager dpm =
514 (DevicePolicyManager) mContext.getSystemService(Context.DEVICE_POLICY_SERVICE);
Adrian Roos8f211582014-07-29 15:09:57 +0200515
Jim Millere303bf42014-08-26 17:12:29 -0700516 if ((dpm.getKeyguardDisabledFeatures(null, mUserId)
Adrian Roos8f211582014-07-29 15:09:57 +0200517 & DevicePolicyManager.KEYGUARD_DISABLE_TRUST_AGENTS) != 0) {
Jim Millere303bf42014-08-26 17:12:29 -0700518 List<PersistableBundle> config = dpm.getTrustAgentConfiguration(
519 null, mName, mUserId);
Adrian Roos8f211582014-07-29 15:09:57 +0200520 trustDisabled = true;
Adrian Roos5d639782016-07-21 11:43:02 -0700521 if (DEBUG) Slog.d(TAG, "Detected trust agents disabled. Config = " + config);
Jim Millere303bf42014-08-26 17:12:29 -0700522 if (config != null && config.size() > 0) {
Adrian Roos8f211582014-07-29 15:09:57 +0200523 if (DEBUG) {
Adrian Roos5d639782016-07-21 11:43:02 -0700524 Slog.d(TAG, "TrustAgent " + mName.flattenToShortString()
Jim Millere303bf42014-08-26 17:12:29 -0700525 + " disabled until it acknowledges "+ config);
Jim Miller604e7552014-07-18 19:00:02 -0700526 }
Adrian Roos8f211582014-07-29 15:09:57 +0200527 mSetTrustAgentFeaturesToken = new Binder();
Jim Millere303bf42014-08-26 17:12:29 -0700528 mTrustAgentService.onConfigure(config, mSetTrustAgentFeaturesToken);
Jim Miller604e7552014-07-18 19:00:02 -0700529 }
Adrian Roosa43fd032015-03-09 19:10:15 +0100530 } else {
531 mTrustAgentService.onConfigure(Collections.EMPTY_LIST, null);
Jim Miller604e7552014-07-18 19:00:02 -0700532 }
Pavel Grafov28939982017-10-03 15:11:52 +0100533 final long maxTimeToLock = dpm.getMaximumTimeToLock(null, mUserId);
Jim Miller76b9b8b2014-08-22 17:04:57 -0700534 if (maxTimeToLock != mMaximumTimeToLock) {
535 // If the timeout changes, cancel the alarm and send a timeout event to have
536 // the agent re-evaluate trust.
537 mMaximumTimeToLock = maxTimeToLock;
538 if (mAlarmPendingIntent != null) {
539 mAlarmManager.cancel(mAlarmPendingIntent);
540 mAlarmPendingIntent = null;
541 mHandler.sendEmptyMessage(MSG_TRUST_TIMEOUT);
542 }
543 }
Jim Miller604e7552014-07-18 19:00:02 -0700544 }
545 } catch (RemoteException e) {
546 onError(e);
547 }
548 if (mTrustDisabledByDpm != trustDisabled) {
549 mTrustDisabledByDpm = trustDisabled;
Adrian Roos94e15a52015-04-16 12:23:18 -0700550 mTrustManagerService.updateTrust(mUserId, 0);
Jim Miller604e7552014-07-18 19:00:02 -0700551 }
552 return trustDisabled;
553 }
554
Adrian Roos82142c22014-03-27 14:56:59 +0100555 public boolean isTrusted() {
Adrian Roos7861c662014-07-25 15:37:28 +0200556 return mTrusted && mManagingTrust && !mTrustDisabledByDpm;
557 }
558
559 public boolean isManagingTrust() {
560 return mManagingTrust && !mTrustDisabledByDpm;
Adrian Roos82142c22014-03-27 14:56:59 +0100561 }
562
Adrian Roos7e03dfc2014-05-16 16:06:28 +0200563 public CharSequence getMessage() {
Adrian Roos82142c22014-03-27 14:56:59 +0100564 return mMessage;
565 }
566
Adrian Roosfc29e0b2014-11-11 12:55:44 +0100567 public void destroy() {
568 mHandler.removeMessages(MSG_RESTART_TIMEOUT);
569
Adrian Roosc5f95ce2014-07-24 16:00:46 +0200570 if (!mBound) {
571 return;
572 }
Adrian Roos5d639782016-07-21 11:43:02 -0700573 if (DEBUG) Slog.d(TAG, "TrustAgent unbound : " + mName.flattenToShortString());
Adrian Roos7d59b4f2014-05-27 20:01:31 +0200574 mTrustManagerService.mArchive.logAgentStopped(mUserId, mName);
Adrian Roos82142c22014-03-27 14:56:59 +0100575 mContext.unbindService(mConnection);
Adrian Roosc5f95ce2014-07-24 16:00:46 +0200576 mBound = false;
Adrian Roos1221b062015-03-26 12:29:51 -0700577 mContext.unregisterReceiver(mBroadcastReceiver);
Adrian Roos7d59b4f2014-05-27 20:01:31 +0200578 mTrustAgentService = null;
Adrian Roos8f211582014-07-29 15:09:57 +0200579 mSetTrustAgentFeaturesToken = null;
Adrian Roos7d59b4f2014-05-27 20:01:31 +0200580 mHandler.sendEmptyMessage(MSG_REVOKE_TRUST);
Adrian Roos82142c22014-03-27 14:56:59 +0100581 }
Adrian Roos7a4f3d42014-05-02 12:12:20 +0200582
583 public boolean isConnected() {
584 return mTrustAgentService != null;
585 }
Adrian Roosc5f95ce2014-07-24 16:00:46 +0200586
587 public boolean isBound() {
588 return mBound;
589 }
590
591 /**
592 * If not connected, returns the time at which the agent is restarted.
593 *
594 * @return restart time in uptime millis.
595 */
596 public long getScheduledRestartUptimeMillis() {
597 return mScheduledRestartUptimeMillis;
598 }
599
600 private void scheduleRestart() {
601 mHandler.removeMessages(MSG_RESTART_TIMEOUT);
602 mScheduledRestartUptimeMillis = SystemClock.uptimeMillis() + RESTART_TIMEOUT_MILLIS;
603 mHandler.sendEmptyMessageAtTime(MSG_RESTART_TIMEOUT, mScheduledRestartUptimeMillis);
604 }
Adrian Roos82142c22014-03-27 14:56:59 +0100605}