blob: a0c8e2325f770bc60ccd5478bbec9dcf7a51a7fe [file] [log] [blame]
Gilad Brettercb51b8b2018-03-22 17:04:51 +02001/*
2 * Copyright (C) 2018 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
Kevin Chyn2ffadb32018-06-19 11:29:38 -070017package com.android.server.biometrics.face;
Gilad Brettercb51b8b2018-03-22 17:04:51 +020018
19import static android.Manifest.permission.INTERACT_ACROSS_USERS;
Kevin Chyna24e9fd2018-08-27 12:39:17 -070020import static android.Manifest.permission.MANAGE_BIOMETRIC;
Gilad Brettercb51b8b2018-03-22 17:04:51 +020021import static android.Manifest.permission.RESET_FACE_LOCKOUT;
Kevin Chyna24e9fd2018-08-27 12:39:17 -070022import static android.Manifest.permission.USE_BIOMETRIC_INTERNAL;
Ilya Matyukhin0f9da352019-10-03 14:10:01 -070023import static android.hardware.biometrics.BiometricAuthenticator.TYPE_FACE;
Gilad Brettercb51b8b2018-03-22 17:04:51 +020024
25import android.app.ActivityManager;
Gilad Brettercb51b8b2018-03-22 17:04:51 +020026import android.app.AppOpsManager;
Kevin Chyn56e4c3d2019-04-23 11:28:43 -070027import android.app.Notification;
28import android.app.NotificationChannel;
29import android.app.NotificationManager;
30import android.app.PendingIntent;
Gilad Brettercb51b8b2018-03-22 17:04:51 +020031import android.content.Context;
Kevin Chyn56e4c3d2019-04-23 11:28:43 -070032import android.content.Intent;
Gilad Brettercb51b8b2018-03-22 17:04:51 +020033import android.content.pm.UserInfo;
Kevin Chyna56dff72018-06-19 18:41:12 -070034import android.hardware.biometrics.BiometricAuthenticator;
35import android.hardware.biometrics.BiometricConstants;
Kevin Chyn7782d142019-01-18 12:51:33 -080036import android.hardware.biometrics.BiometricsProtoEnums;
Kevin Chyna56dff72018-06-19 18:41:12 -070037import android.hardware.biometrics.IBiometricServiceLockoutResetCallback;
Kevin Chyn23289ef2018-11-28 16:32:36 -080038import android.hardware.biometrics.IBiometricServiceReceiverInternal;
Gilad Brettercb51b8b2018-03-22 17:04:51 +020039import android.hardware.biometrics.face.V1_0.IBiometricsFace;
40import android.hardware.biometrics.face.V1_0.IBiometricsFaceClientCallback;
Kevin Chynb95f1522019-03-04 16:45:15 -080041import android.hardware.biometrics.face.V1_0.OptionalBool;
Kevin Chynd79e24e2018-09-25 12:06:59 -070042import android.hardware.biometrics.face.V1_0.Status;
Gilad Brettercb51b8b2018-03-22 17:04:51 +020043import android.hardware.face.Face;
Kevin Chyna24e9fd2018-08-27 12:39:17 -070044import android.hardware.face.FaceManager;
Gilad Brettercb51b8b2018-03-22 17:04:51 +020045import android.hardware.face.IFaceService;
Gilad Brettercb51b8b2018-03-22 17:04:51 +020046import android.hardware.face.IFaceServiceReceiver;
47import android.os.Binder;
Joe Onoratobf955d22019-03-25 00:16:58 -070048import android.os.Build;
Gilad Brettercb51b8b2018-03-22 17:04:51 +020049import android.os.Environment;
Gilad Brettercb51b8b2018-03-22 17:04:51 +020050import android.os.IBinder;
Joe Onoratodb396002019-04-05 19:49:27 -070051import android.os.NativeHandle;
Gilad Brettercb51b8b2018-03-22 17:04:51 +020052import android.os.RemoteException;
53import android.os.SELinux;
Joe Onorato108413a2019-04-03 18:20:52 -070054import android.os.SystemProperties;
Gilad Brettercb51b8b2018-03-22 17:04:51 +020055import android.os.UserHandle;
56import android.os.UserManager;
joshmccloskey4ceaf6b2019-08-21 11:26:08 -070057import android.provider.Settings;
Gilad Brettercb51b8b2018-03-22 17:04:51 +020058import android.util.Slog;
Gilad Brettercb51b8b2018-03-22 17:04:51 +020059
Kevin Chyn56e4c3d2019-04-23 11:28:43 -070060import com.android.internal.R;
Gilad Brettercb51b8b2018-03-22 17:04:51 +020061import com.android.internal.annotations.GuardedBy;
62import com.android.internal.logging.MetricsLogger;
63import com.android.internal.util.DumpUtils;
64import com.android.server.SystemServerInitThreadPool;
Kevin Chyna38653c2019-02-11 17:46:21 -080065import com.android.server.biometrics.AuthenticationClient;
Kevin Chyn355c6bf2018-09-20 22:14:19 -070066import com.android.server.biometrics.BiometricServiceBase;
Kevin Chyn836f2cf2018-08-27 11:06:39 -070067import com.android.server.biometrics.BiometricUtils;
Ilya Matyukhin9dac8482019-07-09 11:03:07 -070068import com.android.server.biometrics.ClientMonitor;
Kevin Chyn4cc49f72019-04-24 13:53:35 -070069import com.android.server.biometrics.Constants;
Kevin Chyn0ce70852019-05-10 10:29:18 -070070import com.android.server.biometrics.EnumerateClient;
Kevin Chyn6737c572019-02-08 16:10:54 -080071import com.android.server.biometrics.RemovalClient;
Gilad Brettercb51b8b2018-03-22 17:04:51 +020072
73import org.json.JSONArray;
74import org.json.JSONException;
75import org.json.JSONObject;
76
77import java.io.File;
78import java.io.FileDescriptor;
Joe Onoratodb396002019-04-05 19:49:27 -070079import java.io.FileOutputStream;
80import java.io.IOException;
Gilad Brettercb51b8b2018-03-22 17:04:51 +020081import java.io.PrintWriter;
82import java.util.ArrayList;
Joe Onoratodb396002019-04-05 19:49:27 -070083import java.util.Arrays;
Kevin Chyn9bc1d492019-06-21 15:31:50 -070084import java.util.HashMap;
Gilad Brettercb51b8b2018-03-22 17:04:51 +020085import java.util.List;
Kevin Chyn9bc1d492019-06-21 15:31:50 -070086import java.util.Map;
Gilad Brettercb51b8b2018-03-22 17:04:51 +020087
88/**
89 * A service to manage multiple clients that want to access the face HAL API.
90 * The service is responsible for maintaining a list of clients and dispatching all
Kevin Chyn51676d22018-11-05 18:00:43 -080091 * face-related events.
Gilad Brettercb51b8b2018-03-22 17:04:51 +020092 *
93 * @hide
94 */
Kevin Chyn355c6bf2018-09-20 22:14:19 -070095public class FaceService extends BiometricServiceBase {
Kevin Chyna56dff72018-06-19 18:41:12 -070096
97 protected static final String TAG = "FaceService";
98 private static final boolean DEBUG = true;
Gilad Brettercb51b8b2018-03-22 17:04:51 +020099 private static final String FACE_DATA_DIR = "facedata";
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200100 private static final String ACTION_LOCKOUT_RESET =
Kevin Chyn2ffadb32018-06-19 11:29:38 -0700101 "com.android.server.biometrics.face.ACTION_LOCKOUT_RESET";
Kevin Chyne46a2162018-09-20 18:43:01 -0700102 private static final int CHALLENGE_TIMEOUT_SEC = 600; // 10 minutes
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200103
Kevin Chyndbfbed42019-06-13 17:01:30 -0700104 private static final String NOTIFICATION_TAG = "FaceService";
105 private static final int NOTIFICATION_ID = 1;
106
joshmccloskey4ceaf6b2019-08-21 11:26:08 -0700107 private static final String SKIP_KEYGUARD_ACQUIRE_IGNORE_LIST =
108 "com.android.server.biometrics.face.skip_keyguard_acquire_ignore_list";
109
Kevin Chyn9bc1d492019-06-21 15:31:50 -0700110 /**
111 * Events for bugreports.
112 */
113 public static final class AuthenticationEvent {
114 private long mStartTime;
115 private long mLatency;
116 // Only valid if mError is 0
117 private boolean mAuthenticated;
118 private int mError;
119 // Only valid if mError is ERROR_VENDOR
120 private int mVendorError;
121
122 AuthenticationEvent(long startTime, long latency, boolean authenticated, int error,
123 int vendorError) {
124 mStartTime = startTime;
125 mLatency = latency;
126 mAuthenticated = authenticated;
127 mError = error;
128 mVendorError = vendorError;
129 }
130
131 public String toString(Context context) {
132 return "Start: " + mStartTime
133 + "\tLatency: " + mLatency
134 + "\tAuthenticated: " + mAuthenticated
135 + "\tError: " + mError
136 + "\tVendorCode: " + mVendorError
137 + "\t" + FaceManager.getErrorString(context, mError, mVendorError);
138 }
139 }
140
141 /**
142 * Keep a short historical buffer of stats, with an aggregated usage time.
143 */
144 private class UsageStats {
145 static final int EVENT_LOG_SIZE = 100;
146
147 Context mContext;
148 List<AuthenticationEvent> mAuthenticationEvents;
149
150 int acceptCount;
151 int rejectCount;
152 Map<Integer, Integer> mErrorCount;
153
154 long acceptLatency;
155 long rejectLatency;
156 Map<Integer, Long> mErrorLatency;
157
158 UsageStats(Context context) {
159 mAuthenticationEvents = new ArrayList<>();
160 mErrorCount = new HashMap<>();
161 mErrorLatency = new HashMap<>();
162 mContext = context;
163 }
164
165 void addEvent(AuthenticationEvent event) {
166 if (mAuthenticationEvents.size() >= EVENT_LOG_SIZE) {
167 mAuthenticationEvents.remove(0);
168 }
169 mAuthenticationEvents.add(event);
170
171 if (event.mAuthenticated) {
172 acceptCount++;
173 acceptLatency += event.mLatency;
174 } else if (event.mError == 0) {
175 rejectCount++;
176 rejectLatency += event.mLatency;
177 } else {
178 mErrorCount.put(event.mError, mErrorCount.getOrDefault(event.mError, 0) + 1);
179 mErrorLatency.put(event.mError,
180 mErrorLatency.getOrDefault(event.mError, 0l) + event.mLatency);
181 }
182 }
183
184 void print(PrintWriter pw) {
185 pw.println("Events since last reboot: " + mAuthenticationEvents.size());
186 for (int i = 0; i < mAuthenticationEvents.size(); i++) {
187 pw.println(mAuthenticationEvents.get(i).toString(mContext));
188 }
189
190 // Dump aggregated usage stats
191 // TODO: Remove or combine with json dump in a future release
192 pw.println("Accept\tCount: " + acceptCount + "\tLatency: " + acceptLatency
193 + "\tAverage: " + (acceptCount > 0 ? acceptLatency / acceptCount : 0));
194 pw.println("Reject\tCount: " + rejectCount + "\tLatency: " + rejectLatency
195 + "\tAverage: " + (rejectCount > 0 ? rejectLatency / rejectCount : 0));
196
197 for (Integer key : mErrorCount.keySet()) {
198 final int count = mErrorCount.get(key);
199 pw.println("Error" + key + "\tCount: " + count
200 + "\tLatency: " + mErrorLatency.getOrDefault(key, 0l)
201 + "\tAverage: " + (count > 0 ? mErrorLatency.getOrDefault(key, 0l) / count
202 : 0)
203 + "\t" + FaceManager.getErrorString(mContext, key, 0 /* vendorCode */));
204 }
205 }
206 }
207
Kevin Chyn8b7a0372018-09-17 15:06:05 -0700208 private final class FaceAuthClient extends AuthenticationClientImpl {
Kevin Chyn0ce70852019-05-10 10:29:18 -0700209 private int mLastAcquire;
210
Kevin Chyn8b7a0372018-09-17 15:06:05 -0700211 public FaceAuthClient(Context context,
212 DaemonWrapper daemon, long halDeviceId, IBinder token,
213 ServiceListener listener, int targetUserId, int groupId, long opId,
Kevin Chyn87f257a2018-11-27 16:26:07 -0800214 boolean restricted, String owner, int cookie, boolean requireConfirmation) {
Kevin Chyn8b7a0372018-09-17 15:06:05 -0700215 super(context, daemon, halDeviceId, token, listener, targetUserId, groupId, opId,
Kevin Chyn87f257a2018-11-27 16:26:07 -0800216 restricted, owner, cookie, requireConfirmation);
Kevin Chyn8b7a0372018-09-17 15:06:05 -0700217 }
Kevin Chyn7782d142019-01-18 12:51:33 -0800218
219 @Override
220 protected int statsModality() {
221 return FaceService.this.statsModality();
222 }
Kevin Chyna38653c2019-02-11 17:46:21 -0800223
224 @Override
225 public boolean shouldFrameworkHandleLockout() {
226 return false;
227 }
Kevin Chyn56d6b072019-02-13 18:39:01 -0800228
229 @Override
Kevin Chyn0ce70852019-05-10 10:29:18 -0700230 public boolean wasUserDetected() {
Ilya Matyukhinc90507e2019-08-20 15:38:57 -0700231 return mLastAcquire != FaceManager.FACE_ACQUIRED_NOT_DETECTED
232 && mLastAcquire != FaceManager.FACE_ACQUIRED_SENSOR_DIRTY;
Kevin Chyn0ce70852019-05-10 10:29:18 -0700233 }
234
235 @Override
Kevin Chyn56d6b072019-02-13 18:39:01 -0800236 public boolean onAuthenticated(BiometricAuthenticator.Identifier identifier,
237 boolean authenticated, ArrayList<Byte> token) {
238 final boolean result = super.onAuthenticated(identifier, authenticated, token);
239
Kevin Chyn9bc1d492019-06-21 15:31:50 -0700240 mUsageStats.addEvent(new AuthenticationEvent(
241 getStartTimeMs(),
242 System.currentTimeMillis() - getStartTimeMs() /* latency */,
243 authenticated,
244 0 /* error */,
245 0 /* vendorError */));
246
Kevin Chyn56d6b072019-02-13 18:39:01 -0800247 // For face, the authentication lifecycle ends either when
248 // 1) Authenticated == true
249 // 2) Error occurred
250 // 3) Authenticated == false
251 // Fingerprint currently does not end when the third condition is met which is a bug,
252 // but let's leave it as-is for now.
253 return result || !authenticated;
254 }
Kevin Chyn56e4c3d2019-04-23 11:28:43 -0700255
256 @Override
Kevin Chyn9bc1d492019-06-21 15:31:50 -0700257 public boolean onError(long deviceId, int error, int vendorCode) {
258 mUsageStats.addEvent(new AuthenticationEvent(
259 getStartTimeMs(),
260 System.currentTimeMillis() - getStartTimeMs() /* latency */,
261 false /* authenticated */,
262 error,
263 vendorCode));
264
265 return super.onError(deviceId, error, vendorCode);
266 }
267
268 @Override
Kevin Chyn4cc49f72019-04-24 13:53:35 -0700269 public int[] getAcquireIgnorelist() {
270 if (isBiometricPrompt()) {
271 return mBiometricPromptIgnoreList;
272 } else {
273 // Keyguard
274 return mKeyguardIgnoreList;
275 }
276 }
277
278 @Override
279 public int[] getAcquireVendorIgnorelist() {
280 if (isBiometricPrompt()) {
281 return mBiometricPromptIgnoreListVendor;
282 } else {
283 // Keyguard
284 return mKeyguardIgnoreListVendor;
285 }
286 }
287
288 @Override
Kevin Chyn56e4c3d2019-04-23 11:28:43 -0700289 public boolean onAcquired(int acquireInfo, int vendorCode) {
290
Kevin Chyn0ce70852019-05-10 10:29:18 -0700291 mLastAcquire = acquireInfo;
292
Kevin Chyn56e4c3d2019-04-23 11:28:43 -0700293 if (acquireInfo == FaceManager.FACE_ACQUIRED_RECALIBRATE) {
294 final String name =
295 getContext().getString(R.string.face_recalibrate_notification_name);
296 final String title =
297 getContext().getString(R.string.face_recalibrate_notification_title);
298 final String content =
299 getContext().getString(R.string.face_recalibrate_notification_content);
300
301 final Intent intent = new Intent("android.settings.FACE_SETTINGS");
302 intent.setPackage("com.android.settings");
303
304 final PendingIntent pendingIntent = PendingIntent.getActivityAsUser(getContext(),
305 0 /* requestCode */, intent, 0 /* flags */, null /* options */,
306 UserHandle.CURRENT);
307
Kevin Chyndbfbed42019-06-13 17:01:30 -0700308 final String channelName = "FaceEnrollNotificationChannel";
Kevin Chyn56e4c3d2019-04-23 11:28:43 -0700309
Kevin Chyndbfbed42019-06-13 17:01:30 -0700310 NotificationChannel channel = new NotificationChannel(channelName, name,
Kevin Chyn56e4c3d2019-04-23 11:28:43 -0700311 NotificationManager.IMPORTANCE_HIGH);
Kevin Chyndbfbed42019-06-13 17:01:30 -0700312 Notification notification = new Notification.Builder(getContext(), channelName)
Kevin Chyn56e4c3d2019-04-23 11:28:43 -0700313 .setSmallIcon(R.drawable.ic_lock)
314 .setContentTitle(title)
315 .setContentText(content)
316 .setSubText(name)
317 .setOnlyAlertOnce(true)
318 .setLocalOnly(true)
319 .setAutoCancel(true)
320 .setCategory(Notification.CATEGORY_SYSTEM)
321 .setContentIntent(pendingIntent)
Kevin Chyn0c3a9982019-06-11 19:09:27 -0700322 .setVisibility(Notification.VISIBILITY_SECRET)
Kevin Chyn56e4c3d2019-04-23 11:28:43 -0700323 .build();
324
Kevin Chyndbfbed42019-06-13 17:01:30 -0700325 mNotificationManager.createNotificationChannel(channel);
326 mNotificationManager.notifyAsUser(NOTIFICATION_TAG, NOTIFICATION_ID, notification,
327 UserHandle.CURRENT);
Kevin Chyn56e4c3d2019-04-23 11:28:43 -0700328 }
329
330 return super.onAcquired(acquireInfo, vendorCode);
331 }
Kevin Chyn8b7a0372018-09-17 15:06:05 -0700332 }
333
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200334 /**
Kevin Chyna56dff72018-06-19 18:41:12 -0700335 * Receives the incoming binder calls from FaceManager.
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200336 */
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200337 private final class FaceServiceWrapper extends IFaceService.Stub {
Curtis Belmonte45788542019-07-23 11:07:27 -0700338 private static final int ENROLL_TIMEOUT_SEC = 75;
Kevin Chyna56dff72018-06-19 18:41:12 -0700339
340 /**
341 * The following methods contain common code which is shared in biometrics/common.
342 */
Kevin Chyna38653c2019-02-11 17:46:21 -0800343
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200344 @Override // Binder call
Kevin Chynd79e24e2018-09-25 12:06:59 -0700345 public long generateChallenge(IBinder token) {
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700346 checkPermission(MANAGE_BIOMETRIC);
Kevin Chynd79e24e2018-09-25 12:06:59 -0700347 return startGenerateChallenge(token);
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200348 }
349
350 @Override // Binder call
Kevin Chynd79e24e2018-09-25 12:06:59 -0700351 public int revokeChallenge(IBinder token) {
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700352 checkPermission(MANAGE_BIOMETRIC);
Kevin Chyn7a207e42019-07-27 17:50:33 -0700353 mHandler.post(() -> {
354 // TODO(b/137106905): Schedule binder calls in FaceService to avoid deadlocks.
355 if (getCurrentClient() == null) {
356 // if we aren't handling any other HIDL calls (mCurrentClient == null), revoke
357 // the challenge right away.
358 startRevokeChallenge(token);
359 } else {
360 // postpone revoking the challenge until we finish processing the current HIDL
361 // call.
362 mRevokeChallengePending = true;
363 }
364 });
365 return Status.OK;
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200366 }
367
368 @Override // Binder call
Kevin Chyn593e6262019-06-28 13:24:44 -0700369 public void enroll(int userId, final IBinder token, final byte[] cryptoToken,
Kevin Chyn1f16c2d2018-12-07 13:06:08 -0800370 final IFaceServiceReceiver receiver, final String opPackageName,
371 final int[] disabledFeatures) {
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700372 checkPermission(MANAGE_BIOMETRIC);
Kevin Chyn593e6262019-06-28 13:24:44 -0700373 updateActiveGroup(userId, opPackageName);
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200374
Kevin Chyndbfbed42019-06-13 17:01:30 -0700375 mNotificationManager.cancelAsUser(NOTIFICATION_TAG, NOTIFICATION_ID,
376 UserHandle.CURRENT);
377
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200378 final boolean restricted = isRestricted();
Kevin Chyna56dff72018-06-19 18:41:12 -0700379 final EnrollClientImpl client = new EnrollClientImpl(getContext(), mDaemonWrapper,
380 mHalDeviceId, token, new ServiceListenerImpl(receiver), mCurrentUserId,
Curtis Belmonte45788542019-07-23 11:07:27 -0700381 0 /* groupId */, cryptoToken, restricted, opPackageName, disabledFeatures,
382 ENROLL_TIMEOUT_SEC) {
Kevin Chyn4cc49f72019-04-24 13:53:35 -0700383
384 @Override
385 public int[] getAcquireIgnorelist() {
386 return mEnrollIgnoreList;
387 }
388
389 @Override
390 public int[] getAcquireVendorIgnorelist() {
391 return mEnrollIgnoreListVendor;
392 }
393
Kevin Chyn1429a312019-01-28 16:08:09 -0800394 @Override
395 public boolean shouldVibrate() {
396 return false;
397 }
Kevin Chyn7782d142019-01-18 12:51:33 -0800398
399 @Override
400 protected int statsModality() {
401 return FaceService.this.statsModality();
402 }
Kevin Chyn1429a312019-01-28 16:08:09 -0800403 };
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200404
Kevin Chyn1a878c12019-04-04 15:50:11 -0700405 enrollInternal(client, mCurrentUserId);
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200406 }
407
408 @Override // Binder call
409 public void cancelEnrollment(final IBinder token) {
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700410 checkPermission(MANAGE_BIOMETRIC);
Kevin Chyna56dff72018-06-19 18:41:12 -0700411 cancelEnrollmentInternal(token);
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200412 }
413
414 @Override // Binder call
Kevin Chyn747e29b2019-01-11 17:01:53 -0800415 public void authenticate(final IBinder token, final long opId, int userId,
Kevin Chyna56dff72018-06-19 18:41:12 -0700416 final IFaceServiceReceiver receiver, final int flags,
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700417 final String opPackageName) {
418 checkPermission(USE_BIOMETRIC_INTERNAL);
Kevin Chyn747e29b2019-01-11 17:01:53 -0800419 updateActiveGroup(userId, opPackageName);
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200420 final boolean restricted = isRestricted();
Kevin Chyn8b7a0372018-09-17 15:06:05 -0700421 final AuthenticationClientImpl client = new FaceAuthClient(getContext(),
Kevin Chyna56dff72018-06-19 18:41:12 -0700422 mDaemonWrapper, mHalDeviceId, token, new ServiceListenerImpl(receiver),
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700423 mCurrentUserId, 0 /* groupId */, opId, restricted, opPackageName,
Kevin Chyn87f257a2018-11-27 16:26:07 -0800424 0 /* cookie */, false /* requireConfirmation */);
Kevin Chyna56dff72018-06-19 18:41:12 -0700425 authenticateInternal(client, opId, opPackageName);
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200426 }
427
428 @Override // Binder call
Kevin Chyn87f257a2018-11-27 16:26:07 -0800429 public void prepareForAuthentication(boolean requireConfirmation, IBinder token, long opId,
Kevin Chyn23289ef2018-11-28 16:32:36 -0800430 int groupId, IBiometricServiceReceiverInternal wrapperReceiver,
431 String opPackageName, int cookie, int callingUid, int callingPid,
432 int callingUserId) {
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700433 checkPermission(USE_BIOMETRIC_INTERNAL);
Kevin Chyn41a80902019-02-06 08:12:15 -0800434 updateActiveGroup(groupId, opPackageName);
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700435 final boolean restricted = true; // BiometricPrompt is always restricted
Kevin Chyn8b7a0372018-09-17 15:06:05 -0700436 final AuthenticationClientImpl client = new FaceAuthClient(getContext(),
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700437 mDaemonWrapper, mHalDeviceId, token,
Kevin Chyn87f257a2018-11-27 16:26:07 -0800438 new BiometricPromptServiceListenerImpl(wrapperReceiver),
439 mCurrentUserId, 0 /* groupId */, opId, restricted, opPackageName, cookie,
Kevin Chyn158fefb2019-01-03 18:59:05 -0800440 requireConfirmation);
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700441 authenticateInternal(client, opId, opPackageName, callingUid, callingPid,
442 callingUserId);
443 }
444
445 @Override // Binder call
Kevin Chyn87f257a2018-11-27 16:26:07 -0800446 public void startPreparedClient(int cookie) {
447 checkPermission(MANAGE_BIOMETRIC);
448 startCurrentClient(cookie);
449 }
450
451 @Override // Binder call
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200452 public void cancelAuthentication(final IBinder token, final String opPackageName) {
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700453 checkPermission(USE_BIOMETRIC_INTERNAL);
Kevin Chyna56dff72018-06-19 18:41:12 -0700454 cancelAuthenticationInternal(token, opPackageName);
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200455 }
456
457 @Override // Binder call
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700458 public void cancelAuthenticationFromService(final IBinder token, final String opPackageName,
Kevin Chyne92cdae2018-11-21 16:35:04 -0800459 int callingUid, int callingPid, int callingUserId, boolean fromClient) {
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700460 checkPermission(USE_BIOMETRIC_INTERNAL);
Kevin Chyne92cdae2018-11-21 16:35:04 -0800461 cancelAuthenticationInternal(token, opPackageName, callingUid, callingPid,
462 callingUserId, fromClient);
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700463 }
464
465 @Override // Binder call
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200466 public void setActiveUser(final int userId) {
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700467 checkPermission(MANAGE_BIOMETRIC);
Kevin Chyna56dff72018-06-19 18:41:12 -0700468 setActiveUserInternal(userId);
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200469 }
470
471 @Override // Binder call
Kevin Chyna56dff72018-06-19 18:41:12 -0700472 public void remove(final IBinder token, final int faceId, final int userId,
Kevin Chyn593e6262019-06-28 13:24:44 -0700473 final IFaceServiceReceiver receiver, final String opPackageName) {
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700474 checkPermission(MANAGE_BIOMETRIC);
Kevin Chyn593e6262019-06-28 13:24:44 -0700475 updateActiveGroup(userId, opPackageName);
Kevin Chyna56dff72018-06-19 18:41:12 -0700476
477 if (token == null) {
478 Slog.w(TAG, "remove(): token is null");
479 return;
480 }
481
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200482 final boolean restricted = isRestricted();
Kevin Chyn4cc49f72019-04-24 13:53:35 -0700483 final RemovalClient client = new RemovalClient(getContext(), getConstants(),
Kevin Chyn6737c572019-02-08 16:10:54 -0800484 mDaemonWrapper, mHalDeviceId, token, new ServiceListenerImpl(receiver), faceId,
485 0 /* groupId */, userId, restricted, token.toString(), getBiometricUtils()) {
Kevin Chyn7782d142019-01-18 12:51:33 -0800486 @Override
487 protected int statsModality() {
488 return FaceService.this.statsModality();
489 }
490 };
Kevin Chyna56dff72018-06-19 18:41:12 -0700491 removeInternal(client);
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200492 }
493
Kevin Chyna56dff72018-06-19 18:41:12 -0700494 @Override
495 public void enumerate(final IBinder token, final int userId,
496 final IFaceServiceReceiver receiver) {
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700497 checkPermission(MANAGE_BIOMETRIC);
Kevin Chyna56dff72018-06-19 18:41:12 -0700498
499 final boolean restricted = isRestricted();
Kevin Chyn4cc49f72019-04-24 13:53:35 -0700500 final EnumerateClient client = new EnumerateClient(getContext(), getConstants(),
Kevin Chyn6737c572019-02-08 16:10:54 -0800501 mDaemonWrapper, mHalDeviceId, token, new ServiceListenerImpl(receiver), userId,
502 userId, restricted, getContext().getOpPackageName()) {
Kevin Chyn7782d142019-01-18 12:51:33 -0800503 @Override
504 protected int statsModality() {
505 return FaceService.this.statsModality();
506 }
507 };
Kevin Chyna56dff72018-06-19 18:41:12 -0700508 enumerateInternal(client);
509 }
510
511 @Override
512 public void addLockoutResetCallback(final IBiometricServiceLockoutResetCallback callback)
513 throws RemoteException {
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700514 checkPermission(USE_BIOMETRIC_INTERNAL);
Kevin Chyna56dff72018-06-19 18:41:12 -0700515 FaceService.super.addLockoutResetCallback(callback);
516 }
517
518 @Override // Binder call
519 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
520 if (!DumpUtils.checkDumpPermission(getContext(), TAG, pw)) {
521 return;
522 }
523
524 final long ident = Binder.clearCallingIdentity();
525 try {
Joe Onoratodb396002019-04-05 19:49:27 -0700526 if (args.length > 1 && "--hal".equals(args[0])) {
527 dumpHal(fd, Arrays.copyOfRange(args, 1, args.length, args.getClass()));
Kevin Chyna56dff72018-06-19 18:41:12 -0700528 } else {
529 dumpInternal(pw);
530 }
531 } finally {
532 Binder.restoreCallingIdentity(ident);
533 }
534 }
535
536 /**
537 * The following methods don't use any common code from BiometricService
538 */
539
540 // TODO: refactor out common code here
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200541 @Override // Binder call
Ilya Matyukhinf2da1a12019-09-25 15:31:03 -0700542 public boolean isHardwareDetected(String opPackageName) {
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700543 checkPermission(USE_BIOMETRIC_INTERNAL);
Kevin Chyna56dff72018-06-19 18:41:12 -0700544 if (!canUseBiometric(opPackageName, false /* foregroundOnly */,
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200545 Binder.getCallingUid(), Binder.getCallingPid(),
546 UserHandle.getCallingUserId())) {
547 return false;
548 }
549
550 final long token = Binder.clearCallingIdentity();
551 try {
552 IBiometricsFace daemon = getFaceDaemon();
553 return daemon != null && mHalDeviceId != 0;
554 } finally {
555 Binder.restoreCallingIdentity(token);
556 }
557 }
558
559 @Override // Binder call
Kevin Chyna56dff72018-06-19 18:41:12 -0700560 public void rename(final int faceId, final String name) {
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700561 checkPermission(MANAGE_BIOMETRIC);
Kevin Chyna56dff72018-06-19 18:41:12 -0700562 if (!isCurrentUserOrProfile(UserHandle.getCallingUserId())) {
563 return;
564 }
565 mHandler.post(new Runnable() {
566 @Override
567 public void run() {
568 getBiometricUtils().renameBiometricForUser(getContext(), mCurrentUserId,
569 faceId, name);
570 }
571 });
572 }
573
574 @Override // Binder call
575 public List<Face> getEnrolledFaces(int userId, String opPackageName) {
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700576 checkPermission(MANAGE_BIOMETRIC);
Kevin Chyna56dff72018-06-19 18:41:12 -0700577 if (!canUseBiometric(opPackageName, false /* foregroundOnly */,
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200578 Binder.getCallingUid(), Binder.getCallingPid(),
579 UserHandle.getCallingUserId())) {
580 return null;
581 }
582
Kevin Chyn6737c572019-02-08 16:10:54 -0800583 return FaceService.this.getEnrolledTemplates(userId);
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200584 }
585
586 @Override // Binder call
Kevin Chyna56dff72018-06-19 18:41:12 -0700587 public boolean hasEnrolledFaces(int userId, String opPackageName) {
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700588 checkPermission(USE_BIOMETRIC_INTERNAL);
Kevin Chyna56dff72018-06-19 18:41:12 -0700589 if (!canUseBiometric(opPackageName, false /* foregroundOnly */,
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200590 Binder.getCallingUid(), Binder.getCallingPid(),
591 UserHandle.getCallingUserId())) {
592 return false;
593 }
594
Kevin Chyna56dff72018-06-19 18:41:12 -0700595 return FaceService.this.hasEnrolledBiometrics(userId);
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200596 }
597
598 @Override // Binder call
599 public long getAuthenticatorId(String opPackageName) {
600 // In this method, we're not checking whether the caller is permitted to use face
601 // API because current authenticator ID is leaked (in a more contrived way) via Android
602 // Keystore (android.security.keystore package): the user of that API can create a key
603 // which requires face authentication for its use, and then query the key's
604 // characteristics (hidden API) which returns, among other things, face
605 // authenticator ID which was active at key creation time.
606 //
607 // Reason: The part of Android Keystore which runs inside an app's process invokes this
608 // method in certain cases. Those cases are not always where the developer demonstrates
609 // explicit intent to use face functionality. Thus, to avoiding throwing an
610 // unexpected SecurityException this method does not check whether its caller is
611 // permitted to use face API.
612 //
613 // The permission check should be restored once Android Keystore no longer invokes this
614 // method from inside app processes.
615
616 return FaceService.this.getAuthenticatorId(opPackageName);
617 }
618
619 @Override // Binder call
Kevin Chyna38653c2019-02-11 17:46:21 -0800620 public void resetLockout(byte[] token) {
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700621 checkPermission(MANAGE_BIOMETRIC);
Kevin Chyn1d6a2862019-04-02 16:20:21 -0700622
joshmccloskeya4772062019-07-24 17:13:16 -0700623 mHandler.post(() -> {
624 if (!FaceService.this.hasEnrolledBiometrics(mCurrentUserId)) {
625 Slog.w(TAG, "Ignoring lockout reset, no templates enrolled");
626 return;
627 }
Kevin Chyn1d6a2862019-04-02 16:20:21 -0700628
joshmccloskeya4772062019-07-24 17:13:16 -0700629 Slog.d(TAG, "Resetting lockout for user: " + mCurrentUserId);
Kevin Chynbe67ce02019-06-10 16:14:22 -0700630
joshmccloskeya4772062019-07-24 17:13:16 -0700631 try {
632 mDaemonWrapper.resetLockout(token);
633 } catch (RemoteException e) {
634 Slog.e(getTag(), "Unable to reset lockout", e);
635 }
636 });
Kevin Chyna56dff72018-06-19 18:41:12 -0700637 }
Kevin Chynd79e24e2018-09-25 12:06:59 -0700638
639 @Override
Kevin Chyn593e6262019-06-28 13:24:44 -0700640 public void setFeature(int userId, int feature, boolean enabled, final byte[] token,
641 IFaceServiceReceiver receiver, final String opPackageName) {
Kevin Chynd79e24e2018-09-25 12:06:59 -0700642 checkPermission(MANAGE_BIOMETRIC);
643
Kevin Chyne62749a2019-04-02 19:33:56 -0700644 mHandler.post(() -> {
joshmccloskeya4772062019-07-24 17:13:16 -0700645 if (DEBUG) {
646 Slog.d(TAG, "setFeature for user(" + userId + ")");
647 }
648 updateActiveGroup(userId, opPackageName);
Kevin Chyne62749a2019-04-02 19:33:56 -0700649 if (!FaceService.this.hasEnrolledBiometrics(mCurrentUserId)) {
650 Slog.e(TAG, "No enrolled biometrics while setting feature: " + feature);
651 return;
Kevin Chynb95f1522019-03-04 16:45:15 -0800652 }
Kevin Chyne62749a2019-04-02 19:33:56 -0700653
654 final ArrayList<Byte> byteToken = new ArrayList<>();
655 for (int i = 0; i < token.length; i++) {
656 byteToken.add(token[i]);
657 }
658
659 // TODO: Support multiple faces
660 final int faceId = getFirstTemplateForUser(mCurrentUserId);
661
662 if (mDaemon != null) {
663 try {
664 final int result = mDaemon.setFeature(feature, enabled, byteToken, faceId);
665 receiver.onFeatureSet(result == Status.OK, feature);
666 } catch (RemoteException e) {
667 Slog.e(getTag(), "Unable to set feature: " + feature
668 + " to enabled:" + enabled, e);
669 }
670 }
671 });
672
Kevin Chynd79e24e2018-09-25 12:06:59 -0700673 }
674
675 @Override
Kevin Chyn593e6262019-06-28 13:24:44 -0700676 public void getFeature(int userId, int feature, IFaceServiceReceiver receiver,
677 final String opPackageName) {
Kevin Chynd79e24e2018-09-25 12:06:59 -0700678 checkPermission(MANAGE_BIOMETRIC);
679
Kevin Chyne62749a2019-04-02 19:33:56 -0700680 mHandler.post(() -> {
joshmccloskeya4772062019-07-24 17:13:16 -0700681 if (DEBUG) {
682 Slog.d(TAG, "getFeature for user(" + userId + ")");
683 }
684 updateActiveGroup(userId, opPackageName);
Kevin Chyne62749a2019-04-02 19:33:56 -0700685 // This should ideally return tri-state, but the user isn't shown settings unless
686 // they are enrolled so it's fine for now.
687 if (!FaceService.this.hasEnrolledBiometrics(mCurrentUserId)) {
688 Slog.e(TAG, "No enrolled biometrics while getting feature: " + feature);
689 return;
Kevin Chynb95f1522019-03-04 16:45:15 -0800690 }
Kevin Chyne62749a2019-04-02 19:33:56 -0700691
692 // TODO: Support multiple faces
693 final int faceId = getFirstTemplateForUser(mCurrentUserId);
694
695 if (mDaemon != null) {
696 try {
697 OptionalBool result = mDaemon.getFeature(feature, faceId);
698 receiver.onFeatureGet(result.status == Status.OK, feature, result.value);
699 } catch (RemoteException e) {
700 Slog.e(getTag(), "Unable to getRequireAttention", e);
701 }
702 }
703 });
704
Kevin Chynd79e24e2018-09-25 12:06:59 -0700705 }
Kevin Chyn57f119b2018-10-25 12:03:41 -0700706
707 @Override
708 public void userActivity() {
709 checkPermission(MANAGE_BIOMETRIC);
710
711 if (mDaemon != null) {
712 try {
713 mDaemon.userActivity();
714 } catch (RemoteException e) {
715 Slog.e(getTag(), "Unable to send userActivity", e);
716 }
717 }
718 }
Kevin Chynb95f1522019-03-04 16:45:15 -0800719
720 // TODO: Support multiple faces
721 private int getFirstTemplateForUser(int user) {
722 final List<Face> faces = FaceService.this.getEnrolledTemplates(user);
723 if (!faces.isEmpty()) {
724 return faces.get(0).getBiometricId();
725 }
726 return 0;
727 }
Kevin Chyna56dff72018-06-19 18:41:12 -0700728 }
729
730 /**
731 * Receives callbacks from the ClientMonitor implementations. The results are forwarded to
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700732 * BiometricPrompt.
733 */
Kevin Chyne92cdae2018-11-21 16:35:04 -0800734 private class BiometricPromptServiceListenerImpl extends BiometricServiceListener {
Kevin Chyn23289ef2018-11-28 16:32:36 -0800735 BiometricPromptServiceListenerImpl(IBiometricServiceReceiverInternal wrapperReceiver) {
Kevin Chyn87f257a2018-11-27 16:26:07 -0800736 super(wrapperReceiver);
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700737 }
738
739 @Override
740 public void onAcquired(long deviceId, int acquiredInfo, int vendorCode)
741 throws RemoteException {
742 /**
743 * Map the acquired codes onto existing {@link BiometricConstants} acquired codes.
744 */
Kevin Chyne92cdae2018-11-21 16:35:04 -0800745 if (getWrapperReceiver() != null) {
746 getWrapperReceiver().onAcquired(
Kevin Chyn8b7a0372018-09-17 15:06:05 -0700747 FaceManager.getMappedAcquiredInfo(acquiredInfo, vendorCode),
748 FaceManager.getAcquiredString(getContext(), acquiredInfo, vendorCode));
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700749 }
750 }
751
752 @Override
Kevin Chyn87f257a2018-11-27 16:26:07 -0800753 public void onError(long deviceId, int error, int vendorCode, int cookie)
754 throws RemoteException {
Kevin Chyne92cdae2018-11-21 16:35:04 -0800755 if (getWrapperReceiver() != null) {
Ilya Matyukhin0f9da352019-10-03 14:10:01 -0700756 getWrapperReceiver().onError(cookie, TYPE_FACE, error, vendorCode);
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700757 }
758 }
759 }
760
761 /**
762 * Receives callbacks from the ClientMonitor implementations. The results are forwarded to
Kevin Chyna56dff72018-06-19 18:41:12 -0700763 * the FaceManager.
764 */
765 private class ServiceListenerImpl implements ServiceListener {
Kevin Chyna56dff72018-06-19 18:41:12 -0700766 private IFaceServiceReceiver mFaceServiceReceiver;
767
768 public ServiceListenerImpl(IFaceServiceReceiver receiver) {
769 mFaceServiceReceiver = receiver;
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200770 }
771
772 @Override
Kevin Chyna56dff72018-06-19 18:41:12 -0700773 public void onEnrollResult(BiometricAuthenticator.Identifier identifier, int remaining)
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200774 throws RemoteException {
Kevin Chyna56dff72018-06-19 18:41:12 -0700775 if (mFaceServiceReceiver != null) {
776 mFaceServiceReceiver.onEnrollResult(identifier.getDeviceId(),
777 identifier.getBiometricId(),
778 remaining);
779 }
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200780 }
Kevin Chyna56dff72018-06-19 18:41:12 -0700781
782 @Override
783 public void onAcquired(long deviceId, int acquiredInfo, int vendorCode)
784 throws RemoteException {
785 if (mFaceServiceReceiver != null) {
786 mFaceServiceReceiver.onAcquired(deviceId, acquiredInfo, vendorCode);
787 }
788 }
789
790 @Override
791 public void onAuthenticationSucceeded(long deviceId,
792 BiometricAuthenticator.Identifier biometric, int userId)
793 throws RemoteException {
794 if (mFaceServiceReceiver != null) {
Kevin Chyn628b7182018-11-13 12:00:48 -0800795 if (biometric == null || biometric instanceof Face) {
Kevin Chyn8d2694a2019-04-11 18:30:40 -0700796 mFaceServiceReceiver.onAuthenticationSucceeded(deviceId, (Face) biometric,
797 userId);
Kevin Chyna56dff72018-06-19 18:41:12 -0700798 } else {
799 Slog.e(TAG, "onAuthenticationSucceeded received non-face biometric");
800 }
801 }
802 }
803
804 @Override
805 public void onAuthenticationFailed(long deviceId) throws RemoteException {
806 if (mFaceServiceReceiver != null) {
807 mFaceServiceReceiver.onAuthenticationFailed(deviceId);
808 }
809 }
810
811 @Override
Kevin Chyn87f257a2018-11-27 16:26:07 -0800812 public void onError(long deviceId, int error, int vendorCode, int cookie)
813 throws RemoteException {
Kevin Chyna56dff72018-06-19 18:41:12 -0700814 if (mFaceServiceReceiver != null) {
815 mFaceServiceReceiver.onError(deviceId, error, vendorCode);
816 }
817 }
818
819 @Override
820 public void onRemoved(BiometricAuthenticator.Identifier identifier,
821 int remaining) throws RemoteException {
822 if (mFaceServiceReceiver != null) {
823 mFaceServiceReceiver.onRemoved(identifier.getDeviceId(),
824 identifier.getBiometricId(), remaining);
825 }
826 }
827
828 @Override
829 public void onEnumerated(BiometricAuthenticator.Identifier identifier, int remaining)
830 throws RemoteException {
831 if (mFaceServiceReceiver != null) {
Kevin Chyn6737c572019-02-08 16:10:54 -0800832 mFaceServiceReceiver.onEnumerated(identifier.getDeviceId(),
833 identifier.getBiometricId(), remaining);
Kevin Chyna56dff72018-06-19 18:41:12 -0700834 }
835 }
836 }
837
Kevin Chyn4cc49f72019-04-24 13:53:35 -0700838 private final FaceConstants mFaceConstants = new FaceConstants();
Kevin Chyna56dff72018-06-19 18:41:12 -0700839
840 @GuardedBy("this")
841 private IBiometricsFace mDaemon;
Kevin Chyn9bc1d492019-06-21 15:31:50 -0700842 private UsageStats mUsageStats;
Ilya Matyukhin9dac8482019-07-09 11:03:07 -0700843 private boolean mRevokeChallengePending = false;
Kevin Chyna38653c2019-02-11 17:46:21 -0800844 // One of the AuthenticationClient constants
845 private int mCurrentUserLockoutMode;
Kevin Chyna56dff72018-06-19 18:41:12 -0700846
Kevin Chyndbfbed42019-06-13 17:01:30 -0700847 private NotificationManager mNotificationManager;
848
Kevin Chyn4cc49f72019-04-24 13:53:35 -0700849 private int[] mBiometricPromptIgnoreList;
850 private int[] mBiometricPromptIgnoreListVendor;
851 private int[] mKeyguardIgnoreList;
852 private int[] mKeyguardIgnoreListVendor;
853 private int[] mEnrollIgnoreList;
854 private int[] mEnrollIgnoreListVendor;
855
Kevin Chyna56dff72018-06-19 18:41:12 -0700856 /**
857 * Receives callbacks from the HAL.
858 */
859 private IBiometricsFaceClientCallback mDaemonCallback =
860 new IBiometricsFaceClientCallback.Stub() {
Kevin Chyn6737c572019-02-08 16:10:54 -0800861 @Override
862 public void onEnrollResult(final long deviceId, int faceId, int userId,
863 int remaining) {
864 mHandler.post(() -> {
865 final Face face = new Face(getBiometricUtils()
866 .getUniqueName(getContext(), userId), faceId, deviceId);
867 FaceService.super.handleEnrollResult(face, remaining);
Kevin Chyn576811e2019-06-12 14:50:35 -0700868
869 // Enrollment changes the authenticatorId, so update it here.
870 IBiometricsFace daemon = getFaceDaemon();
871 if (remaining == 0 && daemon != null) {
872 try {
873 mAuthenticatorIds.put(userId,
874 hasEnrolledBiometrics(userId) ? daemon.getAuthenticatorId().value
875 : 0L);
876 } catch (RemoteException e) {
877 Slog.e(TAG, "Unable to get authenticatorId", e);
878 }
879 }
Kevin Chyn6737c572019-02-08 16:10:54 -0800880 });
881 }
882
883 @Override
884 public void onAcquired(final long deviceId, final int userId,
885 final int acquiredInfo,
886 final int vendorCode) {
887 mHandler.post(() -> {
888 FaceService.super.handleAcquired(deviceId, acquiredInfo, vendorCode);
889 });
890 }
891
892 @Override
893 public void onAuthenticated(final long deviceId, final int faceId, final int userId,
894 ArrayList<Byte> token) {
895 mHandler.post(() -> {
896 Face face = new Face("", faceId, deviceId);
897 FaceService.super.handleAuthenticated(face, token);
898 });
899 }
900
901 @Override
902 public void onError(final long deviceId, final int userId, final int error,
903 final int vendorCode) {
904 mHandler.post(() -> {
905 FaceService.super.handleError(deviceId, error, vendorCode);
906
907 // TODO: this chunk of code should be common to all biometric services
908 if (error == BiometricConstants.BIOMETRIC_ERROR_HW_UNAVAILABLE) {
909 // If we get HW_UNAVAILABLE, try to connect again later...
910 Slog.w(TAG, "Got ERROR_HW_UNAVAILABLE; try reconnecting next client.");
911 synchronized (this) {
912 mDaemon = null;
913 mHalDeviceId = 0;
914 mCurrentUserId = UserHandle.USER_NULL;
915 }
Kevin Chyna56dff72018-06-19 18:41:12 -0700916 }
Kevin Chyn6737c572019-02-08 16:10:54 -0800917 });
918 }
Kevin Chyna56dff72018-06-19 18:41:12 -0700919
Kevin Chyn6737c572019-02-08 16:10:54 -0800920 @Override
Kevin Chyn31ae8d22019-03-11 14:12:32 -0700921 public void onRemoved(final long deviceId, ArrayList<Integer> faceIds, final int userId) {
Kevin Chyn6737c572019-02-08 16:10:54 -0800922 mHandler.post(() -> {
Kevin Chyn31ae8d22019-03-11 14:12:32 -0700923 if (!faceIds.isEmpty()) {
924 for (int i = 0; i < faceIds.size(); i++) {
925 final Face face = new Face("", faceIds.get(i), deviceId);
926 // Convert to old behavior
927 FaceService.super.handleRemoved(face, faceIds.size() - i - 1);
928 }
929 } else {
930 final Face face = new Face("", 0 /* identifier */, deviceId);
931 FaceService.super.handleRemoved(face, 0 /* remaining */);
932 }
Curtis Belmonte8cfe3712019-09-26 16:02:41 -0700933 Settings.Secure.putIntForUser(getContext().getContentResolver(),
934 Settings.Secure.FACE_UNLOCK_RE_ENROLL, 0, UserHandle.USER_CURRENT);
Kevin Chyn6737c572019-02-08 16:10:54 -0800935 });
936 }
937
938 @Override
939 public void onEnumerate(long deviceId, ArrayList<Integer> faceIds, int userId)
940 throws RemoteException {
941 mHandler.post(() -> {
942 if (!faceIds.isEmpty()) {
943 for (int i = 0; i < faceIds.size(); i++) {
944 final Face face = new Face("", faceIds.get(i), deviceId);
945 // Convert to old old behavior
946 FaceService.super.handleEnumerate(face, faceIds.size() - i - 1);
947 }
948 } else {
949 // For face, the HIDL contract is to receive an empty list when there are no
950 // templates enrolled. Send a null identifier since we don't consume them
951 // anywhere, and send remaining == 0 to plumb this with existing common code.
952 FaceService.super.handleEnumerate(null /* identifier */, 0);
Kevin Chyna56dff72018-06-19 18:41:12 -0700953 }
Kevin Chyn6737c572019-02-08 16:10:54 -0800954 });
955 }
Kevin Chyna56dff72018-06-19 18:41:12 -0700956
Kevin Chyn6737c572019-02-08 16:10:54 -0800957 @Override
958 public void onLockoutChanged(long duration) {
Kevin Chyna38653c2019-02-11 17:46:21 -0800959 Slog.d(TAG, "onLockoutChanged: " + duration);
960 if (duration == 0) {
961 mCurrentUserLockoutMode = AuthenticationClient.LOCKOUT_NONE;
962 } else if (duration == Long.MAX_VALUE) {
963 mCurrentUserLockoutMode = AuthenticationClient.LOCKOUT_PERMANENT;
964 } else {
965 mCurrentUserLockoutMode = AuthenticationClient.LOCKOUT_TIMED;
966 }
Kevin Chyna56dff72018-06-19 18:41:12 -0700967
Kevin Chyna38653c2019-02-11 17:46:21 -0800968 mHandler.post(() -> {
969 if (duration == 0) {
970 notifyLockoutResetMonitors();
971 }
972 });
Kevin Chyn6737c572019-02-08 16:10:54 -0800973 }
974 };
Kevin Chyna56dff72018-06-19 18:41:12 -0700975
976 /**
977 * Wraps the HAL-specific code and is passed to the ClientMonitor implementations so that they
978 * can be shared between the multiple biometric services.
979 */
980 private final DaemonWrapper mDaemonWrapper = new DaemonWrapper() {
981 @Override
982 public int authenticate(long operationId, int groupId) throws RemoteException {
983 IBiometricsFace daemon = getFaceDaemon();
984 if (daemon == null) {
985 Slog.w(TAG, "authenticate(): no face HAL!");
986 return ERROR_ESRCH;
987 }
988 return daemon.authenticate(operationId);
989 }
990
991 @Override
992 public int cancel() throws RemoteException {
993 IBiometricsFace daemon = getFaceDaemon();
994 if (daemon == null) {
995 Slog.w(TAG, "cancel(): no face HAL!");
996 return ERROR_ESRCH;
997 }
998 return daemon.cancel();
999 }
1000
1001 @Override
1002 public int remove(int groupId, int biometricId) throws RemoteException {
1003 IBiometricsFace daemon = getFaceDaemon();
1004 if (daemon == null) {
1005 Slog.w(TAG, "remove(): no face HAL!");
1006 return ERROR_ESRCH;
1007 }
1008 return daemon.remove(biometricId);
1009 }
1010
1011 @Override
1012 public int enumerate() throws RemoteException {
1013 IBiometricsFace daemon = getFaceDaemon();
1014 if (daemon == null) {
1015 Slog.w(TAG, "enumerate(): no face HAL!");
1016 return ERROR_ESRCH;
1017 }
1018 return daemon.enumerate();
1019 }
1020
1021 @Override
Kevin Chyn1f16c2d2018-12-07 13:06:08 -08001022 public int enroll(byte[] cryptoToken, int groupId, int timeout,
1023 ArrayList<Integer> disabledFeatures) throws RemoteException {
Kevin Chyna56dff72018-06-19 18:41:12 -07001024 IBiometricsFace daemon = getFaceDaemon();
1025 if (daemon == null) {
1026 Slog.w(TAG, "enroll(): no face HAL!");
1027 return ERROR_ESRCH;
1028 }
1029 final ArrayList<Byte> token = new ArrayList<>();
1030 for (int i = 0; i < cryptoToken.length; i++) {
1031 token.add(cryptoToken[i]);
1032 }
Kevin Chyn1f16c2d2018-12-07 13:06:08 -08001033 return daemon.enroll(token, timeout, disabledFeatures);
Kevin Chyna56dff72018-06-19 18:41:12 -07001034 }
Kevin Chyna38653c2019-02-11 17:46:21 -08001035
1036 @Override
1037 public void resetLockout(byte[] cryptoToken) throws RemoteException {
1038 IBiometricsFace daemon = getFaceDaemon();
1039 if (daemon == null) {
1040 Slog.w(TAG, "resetLockout(): no face HAL!");
1041 return;
1042 }
1043 final ArrayList<Byte> token = new ArrayList<>();
1044 for (int i = 0; i < cryptoToken.length; i++) {
1045 token.add(cryptoToken[i]);
1046 }
1047 daemon.resetLockout(token);
1048 }
Kevin Chyna56dff72018-06-19 18:41:12 -07001049 };
1050
1051
1052 public FaceService(Context context) {
1053 super(context);
Kevin Chyn4cc49f72019-04-24 13:53:35 -07001054
joshmccloskey4ceaf6b2019-08-21 11:26:08 -07001055 final boolean ignoreKeyguardBlacklist = Settings.Secure.getInt(context.getContentResolver(),
1056 SKIP_KEYGUARD_ACQUIRE_IGNORE_LIST, 0) != 0;
1057
Kevin Chyn9bc1d492019-06-21 15:31:50 -07001058 mUsageStats = new UsageStats(context);
1059
Kevin Chyndbfbed42019-06-13 17:01:30 -07001060 mNotificationManager = getContext().getSystemService(NotificationManager.class);
1061
Kevin Chyn4cc49f72019-04-24 13:53:35 -07001062 mBiometricPromptIgnoreList = getContext().getResources()
1063 .getIntArray(R.array.config_face_acquire_biometricprompt_ignorelist);
1064 mBiometricPromptIgnoreListVendor = getContext().getResources()
1065 .getIntArray(R.array.config_face_acquire_vendor_biometricprompt_ignorelist);
joshmccloskey4ceaf6b2019-08-21 11:26:08 -07001066 mKeyguardIgnoreList = ignoreKeyguardBlacklist ? new int[0] : getContext().getResources()
Kevin Chyn4cc49f72019-04-24 13:53:35 -07001067 .getIntArray(R.array.config_face_acquire_keyguard_ignorelist);
joshmccloskey4ceaf6b2019-08-21 11:26:08 -07001068 mKeyguardIgnoreListVendor =
1069 ignoreKeyguardBlacklist ? new int[0] : getContext().getResources()
1070 .getIntArray(R.array.config_face_acquire_vendor_keyguard_ignorelist);
Kevin Chyn4cc49f72019-04-24 13:53:35 -07001071 mEnrollIgnoreList = getContext().getResources()
1072 .getIntArray(R.array.config_face_acquire_enroll_ignorelist);
1073 mEnrollIgnoreListVendor = getContext().getResources()
1074 .getIntArray(R.array.config_face_acquire_vendor_enroll_ignorelist);
Kevin Chyna56dff72018-06-19 18:41:12 -07001075 }
1076
1077 @Override
Ilya Matyukhin9dac8482019-07-09 11:03:07 -07001078 protected void removeClient(ClientMonitor client) {
1079 super.removeClient(client);
1080 if (mRevokeChallengePending) {
1081 startRevokeChallenge(null);
1082 mRevokeChallengePending = false;
1083 }
1084 }
1085
1086 @Override
Kevin Chyna56dff72018-06-19 18:41:12 -07001087 public void onStart() {
Kevin Chyn5a2ff5d2018-08-29 19:07:30 -07001088 super.onStart();
Kevin Chyna56dff72018-06-19 18:41:12 -07001089 publishBinderService(Context.FACE_SERVICE, new FaceServiceWrapper());
Kevin Chyn8398a712019-06-13 16:44:50 -07001090 // Get the face daemon on FaceService's on thread so SystemServerInitThreadPool isn't
1091 // blocked
Felipe Lemeb68b7692019-10-09 10:43:03 -07001092 SystemServerInitThreadPool.submit(() -> mHandler.post(this::getFaceDaemon),
Kevin Chyn8398a712019-06-13 16:44:50 -07001093 TAG + ".onStart");
Kevin Chyna56dff72018-06-19 18:41:12 -07001094 }
1095
1096 @Override
1097 public String getTag() {
1098 return TAG;
1099 }
1100
1101 @Override
Kevin Chyn6737c572019-02-08 16:10:54 -08001102 protected DaemonWrapper getDaemonWrapper() {
1103 return mDaemonWrapper;
1104 }
1105
1106 @Override
Kevin Chyna56dff72018-06-19 18:41:12 -07001107 protected BiometricUtils getBiometricUtils() {
1108 return FaceUtils.getInstance();
1109 }
1110
1111 @Override
Kevin Chyn4cc49f72019-04-24 13:53:35 -07001112 protected Constants getConstants() {
1113 return mFaceConstants;
Kevin Chyna56dff72018-06-19 18:41:12 -07001114 }
1115
1116 @Override
1117 protected boolean hasReachedEnrollmentLimit(int userId) {
1118 final int limit = getContext().getResources().getInteger(
Kevin Chyn017e76e2018-06-27 18:35:06 -07001119 com.android.internal.R.integer.config_faceMaxTemplatesPerUser);
Kevin Chyn6737c572019-02-08 16:10:54 -08001120 final int enrolled = FaceService.this.getEnrolledTemplates(userId).size();
Kevin Chyna56dff72018-06-19 18:41:12 -07001121 if (enrolled >= limit) {
Kevin Chyn1a878c12019-04-04 15:50:11 -07001122 Slog.w(TAG, "Too many faces registered, user: " + userId);
Kevin Chyna56dff72018-06-19 18:41:12 -07001123 return true;
1124 }
1125 return false;
1126 }
1127
1128 @Override
Kevin Chyn9ba99912019-01-16 16:24:36 -08001129 public void serviceDied(long cookie) {
1130 super.serviceDied(cookie);
1131 mDaemon = null;
Jim Miller7b78b222019-02-07 16:47:38 -08001132
1133 mCurrentUserId = UserHandle.USER_NULL; // Force updateActiveGroup() to re-evaluate
Kevin Chyn9ba99912019-01-16 16:24:36 -08001134 }
1135
1136 @Override
Kevin Chyna56dff72018-06-19 18:41:12 -07001137 protected void updateActiveGroup(int userId, String clientPackage) {
1138 IBiometricsFace daemon = getFaceDaemon();
1139
1140 if (daemon != null) {
1141 try {
1142 userId = getUserOrWorkProfileId(clientPackage, userId);
1143 if (userId != mCurrentUserId) {
Kevin Chyn27e33d02019-04-30 01:37:32 +00001144 final File baseDir = Environment.getDataVendorDeDirectory(userId);
Kevin Chyna56dff72018-06-19 18:41:12 -07001145 final File faceDir = new File(baseDir, FACE_DATA_DIR);
1146 if (!faceDir.exists()) {
1147 if (!faceDir.mkdir()) {
1148 Slog.v(TAG, "Cannot make directory: " + faceDir.getAbsolutePath());
1149 return;
1150 }
1151 // Calling mkdir() from this process will create a directory with our
1152 // permissions (inherited from the containing dir). This command fixes
1153 // the label.
1154 if (!SELinux.restorecon(faceDir)) {
1155 Slog.w(TAG, "Restorecons failed. Directory will have wrong label.");
1156 return;
1157 }
1158 }
1159
1160 daemon.setActiveUser(userId, faceDir.getAbsolutePath());
1161 mCurrentUserId = userId;
Kevin Chyn576811e2019-06-12 14:50:35 -07001162 mAuthenticatorIds.put(userId,
1163 hasEnrolledBiometrics(userId) ? daemon.getAuthenticatorId().value : 0L);
Kevin Chyna56dff72018-06-19 18:41:12 -07001164 }
Kevin Chyna56dff72018-06-19 18:41:12 -07001165 } catch (RemoteException e) {
1166 Slog.e(TAG, "Failed to setActiveUser():", e);
1167 }
1168 }
1169 }
1170
1171 @Override
1172 protected String getLockoutResetIntent() {
1173 return ACTION_LOCKOUT_RESET;
1174 }
1175
1176 @Override
1177 protected String getLockoutBroadcastPermission() {
1178 return RESET_FACE_LOCKOUT;
1179 }
1180
1181 @Override
1182 protected long getHalDeviceId() {
1183 return mHalDeviceId;
1184 }
1185
1186 @Override
Kevin Chyna38653c2019-02-11 17:46:21 -08001187 protected void handleUserSwitching(int userId) {
1188 super.handleUserSwitching(userId);
1189 // Will be updated when we get the callback from HAL
1190 mCurrentUserLockoutMode = AuthenticationClient.LOCKOUT_NONE;
1191 }
1192
1193 @Override
Kevin Chyna56dff72018-06-19 18:41:12 -07001194 protected boolean hasEnrolledBiometrics(int userId) {
1195 if (userId != UserHandle.getCallingUserId()) {
1196 checkPermission(INTERACT_ACROSS_USERS);
1197 }
1198 return getBiometricUtils().getBiometricsForUser(getContext(), userId).size() > 0;
1199 }
1200
1201 @Override
1202 protected String getManageBiometricPermission() {
Kevin Chyna24e9fd2018-08-27 12:39:17 -07001203 return MANAGE_BIOMETRIC;
Kevin Chyna56dff72018-06-19 18:41:12 -07001204 }
1205
1206 @Override
1207 protected void checkUseBiometricPermission() {
Kevin Chyna24e9fd2018-08-27 12:39:17 -07001208 // noop for Face. The permission checks are all done on the incoming binder call.
Kevin Chyna56dff72018-06-19 18:41:12 -07001209 }
1210
1211 @Override
Kevin Chynb3c05aa2018-09-21 16:50:32 -07001212 protected boolean checkAppOps(int uid, String opPackageName) {
1213 return mAppOps.noteOp(AppOpsManager.OP_USE_BIOMETRIC, uid, opPackageName)
1214 == AppOpsManager.MODE_ALLOWED;
Kevin Chyna56dff72018-06-19 18:41:12 -07001215 }
1216
1217 @Override
Kevin Chyn6737c572019-02-08 16:10:54 -08001218 protected List<Face> getEnrolledTemplates(int userId) {
1219 return getBiometricUtils().getBiometricsForUser(getContext(), userId);
1220 }
1221
1222 @Override
Kevin Chyna56dff72018-06-19 18:41:12 -07001223 protected void notifyClientActiveCallbacks(boolean isActive) {
1224 // noop for Face.
1225 }
1226
Kevin Chyn7782d142019-01-18 12:51:33 -08001227 @Override
1228 protected int statsModality() {
1229 return BiometricsProtoEnums.MODALITY_FACE;
1230 }
1231
Kevin Chyna38653c2019-02-11 17:46:21 -08001232 @Override
1233 protected int getLockoutMode() {
1234 return mCurrentUserLockoutMode;
1235 }
1236
Kevin Chyna56dff72018-06-19 18:41:12 -07001237 /** Gets the face daemon */
1238 private synchronized IBiometricsFace getFaceDaemon() {
1239 if (mDaemon == null) {
1240 Slog.v(TAG, "mDaemon was null, reconnect to face");
1241 try {
1242 mDaemon = IBiometricsFace.getService();
1243 } catch (java.util.NoSuchElementException e) {
1244 // Service doesn't exist or cannot be opened. Logged below.
1245 } catch (RemoteException e) {
1246 Slog.e(TAG, "Failed to get biometric interface", e);
1247 }
1248 if (mDaemon == null) {
1249 Slog.w(TAG, "face HIDL not available");
1250 return null;
1251 }
1252
1253 mDaemon.asBinder().linkToDeath(this, 0);
1254
1255 try {
1256 mHalDeviceId = mDaemon.setCallback(mDaemonCallback).value;
1257 } catch (RemoteException e) {
1258 Slog.e(TAG, "Failed to open face HAL", e);
1259 mDaemon = null; // try again later!
1260 }
1261
1262 if (DEBUG) Slog.v(TAG, "Face HAL id: " + mHalDeviceId);
1263 if (mHalDeviceId != 0) {
1264 loadAuthenticatorIds();
1265 updateActiveGroup(ActivityManager.getCurrentUser(), null);
Kevin Chyn6737c572019-02-08 16:10:54 -08001266 doTemplateCleanupForUser(ActivityManager.getCurrentUser());
Kevin Chyna56dff72018-06-19 18:41:12 -07001267 } else {
1268 Slog.w(TAG, "Failed to open Face HAL!");
1269 MetricsLogger.count(getContext(), "faced_openhal_error", 1);
1270 mDaemon = null;
1271 }
1272 }
1273 return mDaemon;
1274 }
1275
Kevin Chynd79e24e2018-09-25 12:06:59 -07001276 private long startGenerateChallenge(IBinder token) {
Kevin Chyna56dff72018-06-19 18:41:12 -07001277 IBiometricsFace daemon = getFaceDaemon();
1278 if (daemon == null) {
Kevin Chynd79e24e2018-09-25 12:06:59 -07001279 Slog.w(TAG, "startGenerateChallenge: no face HAL!");
Kevin Chyna56dff72018-06-19 18:41:12 -07001280 return 0;
1281 }
1282 try {
Kevin Chyne46a2162018-09-20 18:43:01 -07001283 return daemon.generateChallenge(CHALLENGE_TIMEOUT_SEC).value;
Kevin Chyna56dff72018-06-19 18:41:12 -07001284 } catch (RemoteException e) {
Kevin Chynd79e24e2018-09-25 12:06:59 -07001285 Slog.e(TAG, "startGenerateChallenge failed", e);
Kevin Chyna56dff72018-06-19 18:41:12 -07001286 }
1287 return 0;
1288 }
1289
Kevin Chynd79e24e2018-09-25 12:06:59 -07001290 private int startRevokeChallenge(IBinder token) {
Kevin Chyna56dff72018-06-19 18:41:12 -07001291 IBiometricsFace daemon = getFaceDaemon();
1292 if (daemon == null) {
Kevin Chynd79e24e2018-09-25 12:06:59 -07001293 Slog.w(TAG, "startRevokeChallenge: no face HAL!");
Kevin Chyna56dff72018-06-19 18:41:12 -07001294 return 0;
1295 }
1296 try {
Ilya Matyukhin9dac8482019-07-09 11:03:07 -07001297 final int res = daemon.revokeChallenge();
1298 if (res != Status.OK) {
1299 Slog.e(TAG, "revokeChallenge returned " + res);
1300 }
1301 return res;
Kevin Chyna56dff72018-06-19 18:41:12 -07001302 } catch (RemoteException e) {
Kevin Chynd79e24e2018-09-25 12:06:59 -07001303 Slog.e(TAG, "startRevokeChallenge failed", e);
Kevin Chyna56dff72018-06-19 18:41:12 -07001304 }
1305 return 0;
1306 }
1307
Gilad Brettercb51b8b2018-03-22 17:04:51 +02001308 private void dumpInternal(PrintWriter pw) {
1309 JSONObject dump = new JSONObject();
1310 try {
1311 dump.put("service", "Face Manager");
1312
1313 JSONArray sets = new JSONArray();
1314 for (UserInfo user : UserManager.get(getContext()).getUsers()) {
1315 final int userId = user.getUserHandle().getIdentifier();
Kevin Chyna56dff72018-06-19 18:41:12 -07001316 final int N = getBiometricUtils().getBiometricsForUser(getContext(), userId).size();
Gilad Brettercb51b8b2018-03-22 17:04:51 +02001317 PerformanceStats stats = mPerformanceMap.get(userId);
1318 PerformanceStats cryptoStats = mCryptoPerformanceMap.get(userId);
1319 JSONObject set = new JSONObject();
1320 set.put("id", userId);
Kevin Chyna56dff72018-06-19 18:41:12 -07001321 set.put("count", N);
Gilad Brettercb51b8b2018-03-22 17:04:51 +02001322 set.put("accept", (stats != null) ? stats.accept : 0);
1323 set.put("reject", (stats != null) ? stats.reject : 0);
1324 set.put("acquire", (stats != null) ? stats.acquire : 0);
1325 set.put("lockout", (stats != null) ? stats.lockout : 0);
1326 set.put("permanentLockout", (stats != null) ? stats.permanentLockout : 0);
1327 // cryptoStats measures statistics about secure face transactions
1328 // (e.g. to unlock password storage, make secure purchases, etc.)
1329 set.put("acceptCrypto", (cryptoStats != null) ? cryptoStats.accept : 0);
1330 set.put("rejectCrypto", (cryptoStats != null) ? cryptoStats.reject : 0);
1331 set.put("acquireCrypto", (cryptoStats != null) ? cryptoStats.acquire : 0);
1332 set.put("lockoutCrypto", (cryptoStats != null) ? cryptoStats.lockout : 0);
Kevin Chyna56dff72018-06-19 18:41:12 -07001333 set.put("permanentLockoutCrypto",
1334 (cryptoStats != null) ? cryptoStats.permanentLockout : 0);
Gilad Brettercb51b8b2018-03-22 17:04:51 +02001335 sets.put(set);
1336 }
1337
1338 dump.put("prints", sets);
1339 } catch (JSONException e) {
1340 Slog.e(TAG, "dump formatting failure", e);
1341 }
1342 pw.println(dump);
Kevin Chyn9bc1d492019-06-21 15:31:50 -07001343 pw.println("HAL deaths since last reboot: " + mHALDeathCount);
1344
1345 mUsageStats.print(pw);
Gilad Brettercb51b8b2018-03-22 17:04:51 +02001346 }
1347
Joe Onoratodb396002019-04-05 19:49:27 -07001348 private void dumpHal(FileDescriptor fd, String[] args) {
Joe Onoratobf955d22019-03-25 00:16:58 -07001349 // WARNING: CDD restricts image data from leaving TEE unencrypted on
1350 // production devices:
1351 // [C-1-10] MUST not allow unencrypted access to identifiable biometric
1352 // data or any data derived from it (such as embeddings) to the
1353 // Application Processor outside the context of the TEE.
1354 // As such, this API should only be enabled for testing purposes on
1355 // engineering and userdebug builds. All modules in the software stack
1356 // MUST enforce final build products do NOT have this functionality.
1357 // Additionally, the following check MUST NOT be removed.
1358 if (!(Build.IS_ENG || Build.IS_USERDEBUG)) {
1359 return;
1360 }
1361
Joe Onorato108413a2019-04-03 18:20:52 -07001362 // Additionally, this flag allows turning off face for a device
1363 // (either permanently through the build or on an individual device).
1364 if (SystemProperties.getBoolean("ro.face.disable_debug_data", false)
1365 || SystemProperties.getBoolean("persist.face.disable_debug_data", false)) {
1366 return;
1367 }
1368
Joe Onoratodb396002019-04-05 19:49:27 -07001369 // The debug method takes two file descriptors. The first is for text
1370 // output, which we will drop. The second is for binary data, which
1371 // will be the protobuf data.
1372 final IBiometricsFace daemon = getFaceDaemon();
1373 if (daemon != null) {
1374 FileOutputStream devnull = null;
1375 try {
1376 devnull = new FileOutputStream("/dev/null");
1377 final NativeHandle handle = new NativeHandle(
1378 new FileDescriptor[] { devnull.getFD(), fd },
1379 new int[0], false);
1380 daemon.debug(handle, new ArrayList<String>(Arrays.asList(args)));
1381 } catch (IOException | RemoteException ex) {
1382 Slog.d(TAG, "error while reading face debugging data", ex);
1383 } finally {
1384 if (devnull != null) {
1385 try {
1386 devnull.close();
1387 } catch (IOException ex) {
1388 }
1389 }
1390 }
Joe Onoratobf955d22019-03-25 00:16:58 -07001391 }
Joe Onoratobf955d22019-03-25 00:16:58 -07001392 }
Kevin Chyn8b7a0372018-09-17 15:06:05 -07001393}