blob: b512475e7971dec7057e3c2ad444387b5972c019 [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;
joshmccloskey0d75a882020-01-21 14:59:46 -0800121 private int mUser;
Kevin Chyn9bc1d492019-06-21 15:31:50 -0700122
123 AuthenticationEvent(long startTime, long latency, boolean authenticated, int error,
joshmccloskey0d75a882020-01-21 14:59:46 -0800124 int vendorError, int user) {
Kevin Chyn9bc1d492019-06-21 15:31:50 -0700125 mStartTime = startTime;
126 mLatency = latency;
127 mAuthenticated = authenticated;
128 mError = error;
129 mVendorError = vendorError;
joshmccloskey0d75a882020-01-21 14:59:46 -0800130 mUser = user;
Kevin Chyn9bc1d492019-06-21 15:31:50 -0700131 }
132
133 public String toString(Context context) {
134 return "Start: " + mStartTime
135 + "\tLatency: " + mLatency
136 + "\tAuthenticated: " + mAuthenticated
137 + "\tError: " + mError
138 + "\tVendorCode: " + mVendorError
joshmccloskey0d75a882020-01-21 14:59:46 -0800139 + "\tUser: " + mUser
Kevin Chyn9bc1d492019-06-21 15:31:50 -0700140 + "\t" + FaceManager.getErrorString(context, mError, mVendorError);
141 }
142 }
143
144 /**
145 * Keep a short historical buffer of stats, with an aggregated usage time.
146 */
147 private class UsageStats {
148 static final int EVENT_LOG_SIZE = 100;
149
150 Context mContext;
151 List<AuthenticationEvent> mAuthenticationEvents;
152
153 int acceptCount;
154 int rejectCount;
155 Map<Integer, Integer> mErrorCount;
156
157 long acceptLatency;
158 long rejectLatency;
159 Map<Integer, Long> mErrorLatency;
160
161 UsageStats(Context context) {
162 mAuthenticationEvents = new ArrayList<>();
163 mErrorCount = new HashMap<>();
164 mErrorLatency = new HashMap<>();
165 mContext = context;
166 }
167
168 void addEvent(AuthenticationEvent event) {
169 if (mAuthenticationEvents.size() >= EVENT_LOG_SIZE) {
170 mAuthenticationEvents.remove(0);
171 }
172 mAuthenticationEvents.add(event);
173
174 if (event.mAuthenticated) {
175 acceptCount++;
176 acceptLatency += event.mLatency;
177 } else if (event.mError == 0) {
178 rejectCount++;
179 rejectLatency += event.mLatency;
180 } else {
181 mErrorCount.put(event.mError, mErrorCount.getOrDefault(event.mError, 0) + 1);
182 mErrorLatency.put(event.mError,
183 mErrorLatency.getOrDefault(event.mError, 0l) + event.mLatency);
184 }
185 }
186
187 void print(PrintWriter pw) {
188 pw.println("Events since last reboot: " + mAuthenticationEvents.size());
189 for (int i = 0; i < mAuthenticationEvents.size(); i++) {
190 pw.println(mAuthenticationEvents.get(i).toString(mContext));
191 }
192
193 // Dump aggregated usage stats
194 // TODO: Remove or combine with json dump in a future release
195 pw.println("Accept\tCount: " + acceptCount + "\tLatency: " + acceptLatency
196 + "\tAverage: " + (acceptCount > 0 ? acceptLatency / acceptCount : 0));
197 pw.println("Reject\tCount: " + rejectCount + "\tLatency: " + rejectLatency
198 + "\tAverage: " + (rejectCount > 0 ? rejectLatency / rejectCount : 0));
199
200 for (Integer key : mErrorCount.keySet()) {
201 final int count = mErrorCount.get(key);
202 pw.println("Error" + key + "\tCount: " + count
203 + "\tLatency: " + mErrorLatency.getOrDefault(key, 0l)
204 + "\tAverage: " + (count > 0 ? mErrorLatency.getOrDefault(key, 0l) / count
205 : 0)
206 + "\t" + FaceManager.getErrorString(mContext, key, 0 /* vendorCode */));
207 }
208 }
209 }
210
Kevin Chyn8b7a0372018-09-17 15:06:05 -0700211 private final class FaceAuthClient extends AuthenticationClientImpl {
Kevin Chyn0ce70852019-05-10 10:29:18 -0700212 private int mLastAcquire;
213
Kevin Chyn8b7a0372018-09-17 15:06:05 -0700214 public FaceAuthClient(Context context,
215 DaemonWrapper daemon, long halDeviceId, IBinder token,
216 ServiceListener listener, int targetUserId, int groupId, long opId,
Kevin Chyn87f257a2018-11-27 16:26:07 -0800217 boolean restricted, String owner, int cookie, boolean requireConfirmation) {
Kevin Chyn8b7a0372018-09-17 15:06:05 -0700218 super(context, daemon, halDeviceId, token, listener, targetUserId, groupId, opId,
Kevin Chyn87f257a2018-11-27 16:26:07 -0800219 restricted, owner, cookie, requireConfirmation);
Kevin Chyn8b7a0372018-09-17 15:06:05 -0700220 }
Kevin Chyn7782d142019-01-18 12:51:33 -0800221
222 @Override
223 protected int statsModality() {
224 return FaceService.this.statsModality();
225 }
Kevin Chyna38653c2019-02-11 17:46:21 -0800226
227 @Override
228 public boolean shouldFrameworkHandleLockout() {
229 return false;
230 }
Kevin Chyn56d6b072019-02-13 18:39:01 -0800231
232 @Override
Kevin Chyn0ce70852019-05-10 10:29:18 -0700233 public boolean wasUserDetected() {
Ilya Matyukhinc90507e2019-08-20 15:38:57 -0700234 return mLastAcquire != FaceManager.FACE_ACQUIRED_NOT_DETECTED
235 && mLastAcquire != FaceManager.FACE_ACQUIRED_SENSOR_DIRTY;
Kevin Chyn0ce70852019-05-10 10:29:18 -0700236 }
237
238 @Override
Kevin Chyn56d6b072019-02-13 18:39:01 -0800239 public boolean onAuthenticated(BiometricAuthenticator.Identifier identifier,
240 boolean authenticated, ArrayList<Byte> token) {
241 final boolean result = super.onAuthenticated(identifier, authenticated, token);
242
Kevin Chyn9bc1d492019-06-21 15:31:50 -0700243 mUsageStats.addEvent(new AuthenticationEvent(
244 getStartTimeMs(),
245 System.currentTimeMillis() - getStartTimeMs() /* latency */,
246 authenticated,
247 0 /* error */,
joshmccloskey0d75a882020-01-21 14:59:46 -0800248 0 /* vendorError */,
249 getTargetUserId()));
Kevin Chyn9bc1d492019-06-21 15:31:50 -0700250
Kevin Chyn56d6b072019-02-13 18:39:01 -0800251 // For face, the authentication lifecycle ends either when
252 // 1) Authenticated == true
253 // 2) Error occurred
254 // 3) Authenticated == false
255 // Fingerprint currently does not end when the third condition is met which is a bug,
256 // but let's leave it as-is for now.
257 return result || !authenticated;
258 }
Kevin Chyn56e4c3d2019-04-23 11:28:43 -0700259
260 @Override
Kevin Chyn9bc1d492019-06-21 15:31:50 -0700261 public boolean onError(long deviceId, int error, int vendorCode) {
262 mUsageStats.addEvent(new AuthenticationEvent(
263 getStartTimeMs(),
264 System.currentTimeMillis() - getStartTimeMs() /* latency */,
265 false /* authenticated */,
266 error,
joshmccloskey0d75a882020-01-21 14:59:46 -0800267 vendorCode,
268 getTargetUserId()));
Kevin Chyn9bc1d492019-06-21 15:31:50 -0700269
270 return super.onError(deviceId, error, vendorCode);
271 }
272
273 @Override
Kevin Chyn4cc49f72019-04-24 13:53:35 -0700274 public int[] getAcquireIgnorelist() {
275 if (isBiometricPrompt()) {
276 return mBiometricPromptIgnoreList;
277 } else {
278 // Keyguard
279 return mKeyguardIgnoreList;
280 }
281 }
282
283 @Override
284 public int[] getAcquireVendorIgnorelist() {
285 if (isBiometricPrompt()) {
286 return mBiometricPromptIgnoreListVendor;
287 } else {
288 // Keyguard
289 return mKeyguardIgnoreListVendor;
290 }
291 }
292
293 @Override
Kevin Chyn56e4c3d2019-04-23 11:28:43 -0700294 public boolean onAcquired(int acquireInfo, int vendorCode) {
295
Kevin Chyn0ce70852019-05-10 10:29:18 -0700296 mLastAcquire = acquireInfo;
297
Kevin Chyn56e4c3d2019-04-23 11:28:43 -0700298 if (acquireInfo == FaceManager.FACE_ACQUIRED_RECALIBRATE) {
299 final String name =
300 getContext().getString(R.string.face_recalibrate_notification_name);
301 final String title =
302 getContext().getString(R.string.face_recalibrate_notification_title);
303 final String content =
304 getContext().getString(R.string.face_recalibrate_notification_content);
305
306 final Intent intent = new Intent("android.settings.FACE_SETTINGS");
307 intent.setPackage("com.android.settings");
308
309 final PendingIntent pendingIntent = PendingIntent.getActivityAsUser(getContext(),
310 0 /* requestCode */, intent, 0 /* flags */, null /* options */,
311 UserHandle.CURRENT);
312
Kevin Chyndbfbed42019-06-13 17:01:30 -0700313 final String channelName = "FaceEnrollNotificationChannel";
Kevin Chyn56e4c3d2019-04-23 11:28:43 -0700314
Kevin Chyndbfbed42019-06-13 17:01:30 -0700315 NotificationChannel channel = new NotificationChannel(channelName, name,
Kevin Chyn56e4c3d2019-04-23 11:28:43 -0700316 NotificationManager.IMPORTANCE_HIGH);
Kevin Chyndbfbed42019-06-13 17:01:30 -0700317 Notification notification = new Notification.Builder(getContext(), channelName)
Kevin Chyn56e4c3d2019-04-23 11:28:43 -0700318 .setSmallIcon(R.drawable.ic_lock)
319 .setContentTitle(title)
320 .setContentText(content)
321 .setSubText(name)
322 .setOnlyAlertOnce(true)
323 .setLocalOnly(true)
324 .setAutoCancel(true)
325 .setCategory(Notification.CATEGORY_SYSTEM)
326 .setContentIntent(pendingIntent)
Kevin Chyn0c3a9982019-06-11 19:09:27 -0700327 .setVisibility(Notification.VISIBILITY_SECRET)
Kevin Chyn56e4c3d2019-04-23 11:28:43 -0700328 .build();
329
Kevin Chyndbfbed42019-06-13 17:01:30 -0700330 mNotificationManager.createNotificationChannel(channel);
331 mNotificationManager.notifyAsUser(NOTIFICATION_TAG, NOTIFICATION_ID, notification,
332 UserHandle.CURRENT);
Kevin Chyn56e4c3d2019-04-23 11:28:43 -0700333 }
334
335 return super.onAcquired(acquireInfo, vendorCode);
336 }
Kevin Chyn8b7a0372018-09-17 15:06:05 -0700337 }
338
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200339 /**
Kevin Chyna56dff72018-06-19 18:41:12 -0700340 * Receives the incoming binder calls from FaceManager.
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200341 */
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200342 private final class FaceServiceWrapper extends IFaceService.Stub {
Curtis Belmonte45788542019-07-23 11:07:27 -0700343 private static final int ENROLL_TIMEOUT_SEC = 75;
Kevin Chyna56dff72018-06-19 18:41:12 -0700344
345 /**
346 * The following methods contain common code which is shared in biometrics/common.
347 */
Kevin Chyna38653c2019-02-11 17:46:21 -0800348
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200349 @Override // Binder call
Kevin Chynd79e24e2018-09-25 12:06:59 -0700350 public long generateChallenge(IBinder token) {
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700351 checkPermission(MANAGE_BIOMETRIC);
Kevin Chynd79e24e2018-09-25 12:06:59 -0700352 return startGenerateChallenge(token);
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200353 }
354
355 @Override // Binder call
Kevin Chynd79e24e2018-09-25 12:06:59 -0700356 public int revokeChallenge(IBinder token) {
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700357 checkPermission(MANAGE_BIOMETRIC);
Kevin Chyn7a207e42019-07-27 17:50:33 -0700358 mHandler.post(() -> {
359 // TODO(b/137106905): Schedule binder calls in FaceService to avoid deadlocks.
360 if (getCurrentClient() == null) {
361 // if we aren't handling any other HIDL calls (mCurrentClient == null), revoke
362 // the challenge right away.
363 startRevokeChallenge(token);
364 } else {
365 // postpone revoking the challenge until we finish processing the current HIDL
366 // call.
367 mRevokeChallengePending = true;
368 }
369 });
370 return Status.OK;
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200371 }
372
373 @Override // Binder call
Kevin Chyn593e6262019-06-28 13:24:44 -0700374 public void enroll(int userId, final IBinder token, final byte[] cryptoToken,
Kevin Chyn1f16c2d2018-12-07 13:06:08 -0800375 final IFaceServiceReceiver receiver, final String opPackageName,
376 final int[] disabledFeatures) {
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700377 checkPermission(MANAGE_BIOMETRIC);
Kevin Chyn593e6262019-06-28 13:24:44 -0700378 updateActiveGroup(userId, opPackageName);
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200379
Kevin Chyndbfbed42019-06-13 17:01:30 -0700380 mNotificationManager.cancelAsUser(NOTIFICATION_TAG, NOTIFICATION_ID,
381 UserHandle.CURRENT);
382
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200383 final boolean restricted = isRestricted();
Kevin Chyna56dff72018-06-19 18:41:12 -0700384 final EnrollClientImpl client = new EnrollClientImpl(getContext(), mDaemonWrapper,
385 mHalDeviceId, token, new ServiceListenerImpl(receiver), mCurrentUserId,
Curtis Belmonte45788542019-07-23 11:07:27 -0700386 0 /* groupId */, cryptoToken, restricted, opPackageName, disabledFeatures,
387 ENROLL_TIMEOUT_SEC) {
Kevin Chyn4cc49f72019-04-24 13:53:35 -0700388
389 @Override
390 public int[] getAcquireIgnorelist() {
391 return mEnrollIgnoreList;
392 }
393
394 @Override
395 public int[] getAcquireVendorIgnorelist() {
396 return mEnrollIgnoreListVendor;
397 }
398
Kevin Chyn1429a312019-01-28 16:08:09 -0800399 @Override
400 public boolean shouldVibrate() {
401 return false;
402 }
Kevin Chyn7782d142019-01-18 12:51:33 -0800403
404 @Override
405 protected int statsModality() {
406 return FaceService.this.statsModality();
407 }
Kevin Chyn1429a312019-01-28 16:08:09 -0800408 };
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200409
Kevin Chyn1a878c12019-04-04 15:50:11 -0700410 enrollInternal(client, mCurrentUserId);
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200411 }
412
413 @Override // Binder call
414 public void cancelEnrollment(final IBinder token) {
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700415 checkPermission(MANAGE_BIOMETRIC);
Kevin Chyna56dff72018-06-19 18:41:12 -0700416 cancelEnrollmentInternal(token);
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200417 }
418
419 @Override // Binder call
Kevin Chyn747e29b2019-01-11 17:01:53 -0800420 public void authenticate(final IBinder token, final long opId, int userId,
Kevin Chyna56dff72018-06-19 18:41:12 -0700421 final IFaceServiceReceiver receiver, final int flags,
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700422 final String opPackageName) {
423 checkPermission(USE_BIOMETRIC_INTERNAL);
Kevin Chyn747e29b2019-01-11 17:01:53 -0800424 updateActiveGroup(userId, opPackageName);
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200425 final boolean restricted = isRestricted();
Kevin Chyn8b7a0372018-09-17 15:06:05 -0700426 final AuthenticationClientImpl client = new FaceAuthClient(getContext(),
Kevin Chyna56dff72018-06-19 18:41:12 -0700427 mDaemonWrapper, mHalDeviceId, token, new ServiceListenerImpl(receiver),
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700428 mCurrentUserId, 0 /* groupId */, opId, restricted, opPackageName,
Kevin Chyn87f257a2018-11-27 16:26:07 -0800429 0 /* cookie */, false /* requireConfirmation */);
Kevin Chyna56dff72018-06-19 18:41:12 -0700430 authenticateInternal(client, opId, opPackageName);
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200431 }
432
433 @Override // Binder call
Kevin Chyn87f257a2018-11-27 16:26:07 -0800434 public void prepareForAuthentication(boolean requireConfirmation, IBinder token, long opId,
Kevin Chyn23289ef2018-11-28 16:32:36 -0800435 int groupId, IBiometricServiceReceiverInternal wrapperReceiver,
436 String opPackageName, int cookie, int callingUid, int callingPid,
437 int callingUserId) {
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700438 checkPermission(USE_BIOMETRIC_INTERNAL);
Kevin Chyn41a80902019-02-06 08:12:15 -0800439 updateActiveGroup(groupId, opPackageName);
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700440 final boolean restricted = true; // BiometricPrompt is always restricted
Kevin Chyn8b7a0372018-09-17 15:06:05 -0700441 final AuthenticationClientImpl client = new FaceAuthClient(getContext(),
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700442 mDaemonWrapper, mHalDeviceId, token,
Kevin Chyn87f257a2018-11-27 16:26:07 -0800443 new BiometricPromptServiceListenerImpl(wrapperReceiver),
444 mCurrentUserId, 0 /* groupId */, opId, restricted, opPackageName, cookie,
Kevin Chyn158fefb2019-01-03 18:59:05 -0800445 requireConfirmation);
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700446 authenticateInternal(client, opId, opPackageName, callingUid, callingPid,
447 callingUserId);
448 }
449
450 @Override // Binder call
Kevin Chyn87f257a2018-11-27 16:26:07 -0800451 public void startPreparedClient(int cookie) {
452 checkPermission(MANAGE_BIOMETRIC);
453 startCurrentClient(cookie);
454 }
455
456 @Override // Binder call
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200457 public void cancelAuthentication(final IBinder token, final String opPackageName) {
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700458 checkPermission(USE_BIOMETRIC_INTERNAL);
Kevin Chyna56dff72018-06-19 18:41:12 -0700459 cancelAuthenticationInternal(token, opPackageName);
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200460 }
461
462 @Override // Binder call
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700463 public void cancelAuthenticationFromService(final IBinder token, final String opPackageName,
Kevin Chyne92cdae2018-11-21 16:35:04 -0800464 int callingUid, int callingPid, int callingUserId, boolean fromClient) {
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700465 checkPermission(USE_BIOMETRIC_INTERNAL);
Kevin Chyne92cdae2018-11-21 16:35:04 -0800466 cancelAuthenticationInternal(token, opPackageName, callingUid, callingPid,
467 callingUserId, fromClient);
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700468 }
469
470 @Override // Binder call
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200471 public void setActiveUser(final int userId) {
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700472 checkPermission(MANAGE_BIOMETRIC);
Kevin Chyna56dff72018-06-19 18:41:12 -0700473 setActiveUserInternal(userId);
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200474 }
475
476 @Override // Binder call
Kevin Chyna56dff72018-06-19 18:41:12 -0700477 public void remove(final IBinder token, final int faceId, final int userId,
Kevin Chyn593e6262019-06-28 13:24:44 -0700478 final IFaceServiceReceiver receiver, final String opPackageName) {
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700479 checkPermission(MANAGE_BIOMETRIC);
Kevin Chyn593e6262019-06-28 13:24:44 -0700480 updateActiveGroup(userId, opPackageName);
Kevin Chyna56dff72018-06-19 18:41:12 -0700481
482 if (token == null) {
483 Slog.w(TAG, "remove(): token is null");
484 return;
485 }
486
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200487 final boolean restricted = isRestricted();
Kevin Chyn4cc49f72019-04-24 13:53:35 -0700488 final RemovalClient client = new RemovalClient(getContext(), getConstants(),
Kevin Chyn6737c572019-02-08 16:10:54 -0800489 mDaemonWrapper, mHalDeviceId, token, new ServiceListenerImpl(receiver), faceId,
490 0 /* groupId */, userId, restricted, token.toString(), getBiometricUtils()) {
Kevin Chyn7782d142019-01-18 12:51:33 -0800491 @Override
492 protected int statsModality() {
493 return FaceService.this.statsModality();
494 }
495 };
Kevin Chyna56dff72018-06-19 18:41:12 -0700496 removeInternal(client);
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200497 }
498
Kevin Chyna56dff72018-06-19 18:41:12 -0700499 @Override
500 public void enumerate(final IBinder token, final int userId,
501 final IFaceServiceReceiver receiver) {
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700502 checkPermission(MANAGE_BIOMETRIC);
Kevin Chyna56dff72018-06-19 18:41:12 -0700503
504 final boolean restricted = isRestricted();
Kevin Chyn4cc49f72019-04-24 13:53:35 -0700505 final EnumerateClient client = new EnumerateClient(getContext(), getConstants(),
Kevin Chyn6737c572019-02-08 16:10:54 -0800506 mDaemonWrapper, mHalDeviceId, token, new ServiceListenerImpl(receiver), userId,
507 userId, restricted, getContext().getOpPackageName()) {
Kevin Chyn7782d142019-01-18 12:51:33 -0800508 @Override
509 protected int statsModality() {
510 return FaceService.this.statsModality();
511 }
512 };
Kevin Chyna56dff72018-06-19 18:41:12 -0700513 enumerateInternal(client);
514 }
515
516 @Override
517 public void addLockoutResetCallback(final IBiometricServiceLockoutResetCallback callback)
518 throws RemoteException {
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700519 checkPermission(USE_BIOMETRIC_INTERNAL);
Kevin Chyna56dff72018-06-19 18:41:12 -0700520 FaceService.super.addLockoutResetCallback(callback);
521 }
522
523 @Override // Binder call
524 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
525 if (!DumpUtils.checkDumpPermission(getContext(), TAG, pw)) {
526 return;
527 }
528
529 final long ident = Binder.clearCallingIdentity();
530 try {
Joe Onoratodb396002019-04-05 19:49:27 -0700531 if (args.length > 1 && "--hal".equals(args[0])) {
532 dumpHal(fd, Arrays.copyOfRange(args, 1, args.length, args.getClass()));
Kevin Chyna56dff72018-06-19 18:41:12 -0700533 } else {
534 dumpInternal(pw);
535 }
536 } finally {
537 Binder.restoreCallingIdentity(ident);
538 }
539 }
540
541 /**
542 * The following methods don't use any common code from BiometricService
543 */
544
545 // TODO: refactor out common code here
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200546 @Override // Binder call
Ilya Matyukhinf2da1a12019-09-25 15:31:03 -0700547 public boolean isHardwareDetected(String opPackageName) {
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700548 checkPermission(USE_BIOMETRIC_INTERNAL);
Kevin Chyna56dff72018-06-19 18:41:12 -0700549 if (!canUseBiometric(opPackageName, false /* foregroundOnly */,
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200550 Binder.getCallingUid(), Binder.getCallingPid(),
551 UserHandle.getCallingUserId())) {
552 return false;
553 }
554
555 final long token = Binder.clearCallingIdentity();
556 try {
557 IBiometricsFace daemon = getFaceDaemon();
558 return daemon != null && mHalDeviceId != 0;
559 } finally {
560 Binder.restoreCallingIdentity(token);
561 }
562 }
563
564 @Override // Binder call
Kevin Chyna56dff72018-06-19 18:41:12 -0700565 public void rename(final int faceId, final String name) {
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700566 checkPermission(MANAGE_BIOMETRIC);
Kevin Chyna56dff72018-06-19 18:41:12 -0700567 if (!isCurrentUserOrProfile(UserHandle.getCallingUserId())) {
568 return;
569 }
570 mHandler.post(new Runnable() {
571 @Override
572 public void run() {
573 getBiometricUtils().renameBiometricForUser(getContext(), mCurrentUserId,
574 faceId, name);
575 }
576 });
577 }
578
579 @Override // Binder call
580 public List<Face> getEnrolledFaces(int userId, String opPackageName) {
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700581 checkPermission(MANAGE_BIOMETRIC);
Kevin Chyna56dff72018-06-19 18:41:12 -0700582 if (!canUseBiometric(opPackageName, false /* foregroundOnly */,
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200583 Binder.getCallingUid(), Binder.getCallingPid(),
584 UserHandle.getCallingUserId())) {
585 return null;
586 }
587
Kevin Chyn6737c572019-02-08 16:10:54 -0800588 return FaceService.this.getEnrolledTemplates(userId);
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200589 }
590
591 @Override // Binder call
Kevin Chyna56dff72018-06-19 18:41:12 -0700592 public boolean hasEnrolledFaces(int userId, String opPackageName) {
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700593 checkPermission(USE_BIOMETRIC_INTERNAL);
Kevin Chyna56dff72018-06-19 18:41:12 -0700594 if (!canUseBiometric(opPackageName, false /* foregroundOnly */,
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200595 Binder.getCallingUid(), Binder.getCallingPid(),
596 UserHandle.getCallingUserId())) {
597 return false;
598 }
599
Kevin Chyna56dff72018-06-19 18:41:12 -0700600 return FaceService.this.hasEnrolledBiometrics(userId);
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200601 }
602
603 @Override // Binder call
604 public long getAuthenticatorId(String opPackageName) {
605 // In this method, we're not checking whether the caller is permitted to use face
606 // API because current authenticator ID is leaked (in a more contrived way) via Android
607 // Keystore (android.security.keystore package): the user of that API can create a key
608 // which requires face authentication for its use, and then query the key's
609 // characteristics (hidden API) which returns, among other things, face
610 // authenticator ID which was active at key creation time.
611 //
612 // Reason: The part of Android Keystore which runs inside an app's process invokes this
613 // method in certain cases. Those cases are not always where the developer demonstrates
614 // explicit intent to use face functionality. Thus, to avoiding throwing an
615 // unexpected SecurityException this method does not check whether its caller is
616 // permitted to use face API.
617 //
618 // The permission check should be restored once Android Keystore no longer invokes this
619 // method from inside app processes.
620
621 return FaceService.this.getAuthenticatorId(opPackageName);
622 }
623
624 @Override // Binder call
Kevin Chyna38653c2019-02-11 17:46:21 -0800625 public void resetLockout(byte[] token) {
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700626 checkPermission(MANAGE_BIOMETRIC);
Kevin Chyn1d6a2862019-04-02 16:20:21 -0700627
joshmccloskeya4772062019-07-24 17:13:16 -0700628 mHandler.post(() -> {
629 if (!FaceService.this.hasEnrolledBiometrics(mCurrentUserId)) {
630 Slog.w(TAG, "Ignoring lockout reset, no templates enrolled");
631 return;
632 }
Kevin Chyn1d6a2862019-04-02 16:20:21 -0700633
joshmccloskeya4772062019-07-24 17:13:16 -0700634 Slog.d(TAG, "Resetting lockout for user: " + mCurrentUserId);
Kevin Chynbe67ce02019-06-10 16:14:22 -0700635
joshmccloskeya4772062019-07-24 17:13:16 -0700636 try {
637 mDaemonWrapper.resetLockout(token);
638 } catch (RemoteException e) {
639 Slog.e(getTag(), "Unable to reset lockout", e);
640 }
641 });
Kevin Chyna56dff72018-06-19 18:41:12 -0700642 }
Kevin Chynd79e24e2018-09-25 12:06:59 -0700643
644 @Override
Kevin Chyn593e6262019-06-28 13:24:44 -0700645 public void setFeature(int userId, int feature, boolean enabled, final byte[] token,
646 IFaceServiceReceiver receiver, final String opPackageName) {
Kevin Chynd79e24e2018-09-25 12:06:59 -0700647 checkPermission(MANAGE_BIOMETRIC);
648
Kevin Chyne62749a2019-04-02 19:33:56 -0700649 mHandler.post(() -> {
joshmccloskeya4772062019-07-24 17:13:16 -0700650 if (DEBUG) {
651 Slog.d(TAG, "setFeature for user(" + userId + ")");
652 }
653 updateActiveGroup(userId, opPackageName);
Kevin Chyne62749a2019-04-02 19:33:56 -0700654 if (!FaceService.this.hasEnrolledBiometrics(mCurrentUserId)) {
655 Slog.e(TAG, "No enrolled biometrics while setting feature: " + feature);
656 return;
Kevin Chynb95f1522019-03-04 16:45:15 -0800657 }
Kevin Chyne62749a2019-04-02 19:33:56 -0700658
659 final ArrayList<Byte> byteToken = new ArrayList<>();
660 for (int i = 0; i < token.length; i++) {
661 byteToken.add(token[i]);
662 }
663
664 // TODO: Support multiple faces
665 final int faceId = getFirstTemplateForUser(mCurrentUserId);
666
667 if (mDaemon != null) {
668 try {
669 final int result = mDaemon.setFeature(feature, enabled, byteToken, faceId);
670 receiver.onFeatureSet(result == Status.OK, feature);
671 } catch (RemoteException e) {
672 Slog.e(getTag(), "Unable to set feature: " + feature
673 + " to enabled:" + enabled, e);
674 }
675 }
676 });
677
Kevin Chynd79e24e2018-09-25 12:06:59 -0700678 }
679
680 @Override
Kevin Chyn593e6262019-06-28 13:24:44 -0700681 public void getFeature(int userId, int feature, IFaceServiceReceiver receiver,
682 final String opPackageName) {
Kevin Chynd79e24e2018-09-25 12:06:59 -0700683 checkPermission(MANAGE_BIOMETRIC);
684
Kevin Chyne62749a2019-04-02 19:33:56 -0700685 mHandler.post(() -> {
joshmccloskeya4772062019-07-24 17:13:16 -0700686 if (DEBUG) {
687 Slog.d(TAG, "getFeature for user(" + userId + ")");
688 }
689 updateActiveGroup(userId, opPackageName);
Kevin Chyne62749a2019-04-02 19:33:56 -0700690 // This should ideally return tri-state, but the user isn't shown settings unless
691 // they are enrolled so it's fine for now.
692 if (!FaceService.this.hasEnrolledBiometrics(mCurrentUserId)) {
693 Slog.e(TAG, "No enrolled biometrics while getting feature: " + feature);
694 return;
Kevin Chynb95f1522019-03-04 16:45:15 -0800695 }
Kevin Chyne62749a2019-04-02 19:33:56 -0700696
697 // TODO: Support multiple faces
698 final int faceId = getFirstTemplateForUser(mCurrentUserId);
699
700 if (mDaemon != null) {
701 try {
702 OptionalBool result = mDaemon.getFeature(feature, faceId);
703 receiver.onFeatureGet(result.status == Status.OK, feature, result.value);
704 } catch (RemoteException e) {
705 Slog.e(getTag(), "Unable to getRequireAttention", e);
706 }
707 }
708 });
709
Kevin Chynd79e24e2018-09-25 12:06:59 -0700710 }
Kevin Chyn57f119b2018-10-25 12:03:41 -0700711
712 @Override
713 public void userActivity() {
714 checkPermission(MANAGE_BIOMETRIC);
715
716 if (mDaemon != null) {
717 try {
718 mDaemon.userActivity();
719 } catch (RemoteException e) {
720 Slog.e(getTag(), "Unable to send userActivity", e);
721 }
722 }
723 }
Kevin Chynb95f1522019-03-04 16:45:15 -0800724
725 // TODO: Support multiple faces
726 private int getFirstTemplateForUser(int user) {
727 final List<Face> faces = FaceService.this.getEnrolledTemplates(user);
728 if (!faces.isEmpty()) {
729 return faces.get(0).getBiometricId();
730 }
731 return 0;
732 }
Kevin Chyna56dff72018-06-19 18:41:12 -0700733 }
734
735 /**
736 * Receives callbacks from the ClientMonitor implementations. The results are forwarded to
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700737 * BiometricPrompt.
738 */
Kevin Chyne92cdae2018-11-21 16:35:04 -0800739 private class BiometricPromptServiceListenerImpl extends BiometricServiceListener {
Kevin Chyn23289ef2018-11-28 16:32:36 -0800740 BiometricPromptServiceListenerImpl(IBiometricServiceReceiverInternal wrapperReceiver) {
Kevin Chyn87f257a2018-11-27 16:26:07 -0800741 super(wrapperReceiver);
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700742 }
743
744 @Override
745 public void onAcquired(long deviceId, int acquiredInfo, int vendorCode)
746 throws RemoteException {
747 /**
748 * Map the acquired codes onto existing {@link BiometricConstants} acquired codes.
749 */
Kevin Chyne92cdae2018-11-21 16:35:04 -0800750 if (getWrapperReceiver() != null) {
751 getWrapperReceiver().onAcquired(
Kevin Chyn8b7a0372018-09-17 15:06:05 -0700752 FaceManager.getMappedAcquiredInfo(acquiredInfo, vendorCode),
753 FaceManager.getAcquiredString(getContext(), acquiredInfo, vendorCode));
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700754 }
755 }
756
757 @Override
Kevin Chyn87f257a2018-11-27 16:26:07 -0800758 public void onError(long deviceId, int error, int vendorCode, int cookie)
759 throws RemoteException {
Kevin Chyne92cdae2018-11-21 16:35:04 -0800760 if (getWrapperReceiver() != null) {
Ilya Matyukhin0f9da352019-10-03 14:10:01 -0700761 getWrapperReceiver().onError(cookie, TYPE_FACE, error, vendorCode);
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700762 }
763 }
764 }
765
766 /**
767 * Receives callbacks from the ClientMonitor implementations. The results are forwarded to
Kevin Chyna56dff72018-06-19 18:41:12 -0700768 * the FaceManager.
769 */
770 private class ServiceListenerImpl implements ServiceListener {
Kevin Chyna56dff72018-06-19 18:41:12 -0700771 private IFaceServiceReceiver mFaceServiceReceiver;
772
773 public ServiceListenerImpl(IFaceServiceReceiver receiver) {
774 mFaceServiceReceiver = receiver;
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200775 }
776
777 @Override
Kevin Chyna56dff72018-06-19 18:41:12 -0700778 public void onEnrollResult(BiometricAuthenticator.Identifier identifier, int remaining)
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200779 throws RemoteException {
Kevin Chyna56dff72018-06-19 18:41:12 -0700780 if (mFaceServiceReceiver != null) {
781 mFaceServiceReceiver.onEnrollResult(identifier.getDeviceId(),
782 identifier.getBiometricId(),
783 remaining);
784 }
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200785 }
Kevin Chyna56dff72018-06-19 18:41:12 -0700786
787 @Override
788 public void onAcquired(long deviceId, int acquiredInfo, int vendorCode)
789 throws RemoteException {
790 if (mFaceServiceReceiver != null) {
791 mFaceServiceReceiver.onAcquired(deviceId, acquiredInfo, vendorCode);
792 }
793 }
794
795 @Override
796 public void onAuthenticationSucceeded(long deviceId,
797 BiometricAuthenticator.Identifier biometric, int userId)
798 throws RemoteException {
799 if (mFaceServiceReceiver != null) {
Kevin Chyn628b7182018-11-13 12:00:48 -0800800 if (biometric == null || biometric instanceof Face) {
Kevin Chyn8d2694a2019-04-11 18:30:40 -0700801 mFaceServiceReceiver.onAuthenticationSucceeded(deviceId, (Face) biometric,
802 userId);
Kevin Chyna56dff72018-06-19 18:41:12 -0700803 } else {
804 Slog.e(TAG, "onAuthenticationSucceeded received non-face biometric");
805 }
806 }
807 }
808
809 @Override
810 public void onAuthenticationFailed(long deviceId) throws RemoteException {
811 if (mFaceServiceReceiver != null) {
812 mFaceServiceReceiver.onAuthenticationFailed(deviceId);
813 }
814 }
815
816 @Override
Kevin Chyn87f257a2018-11-27 16:26:07 -0800817 public void onError(long deviceId, int error, int vendorCode, int cookie)
818 throws RemoteException {
Kevin Chyna56dff72018-06-19 18:41:12 -0700819 if (mFaceServiceReceiver != null) {
820 mFaceServiceReceiver.onError(deviceId, error, vendorCode);
821 }
822 }
823
824 @Override
825 public void onRemoved(BiometricAuthenticator.Identifier identifier,
826 int remaining) throws RemoteException {
827 if (mFaceServiceReceiver != null) {
828 mFaceServiceReceiver.onRemoved(identifier.getDeviceId(),
829 identifier.getBiometricId(), remaining);
830 }
831 }
832
833 @Override
834 public void onEnumerated(BiometricAuthenticator.Identifier identifier, int remaining)
835 throws RemoteException {
836 if (mFaceServiceReceiver != null) {
Kevin Chyn6737c572019-02-08 16:10:54 -0800837 mFaceServiceReceiver.onEnumerated(identifier.getDeviceId(),
838 identifier.getBiometricId(), remaining);
Kevin Chyna56dff72018-06-19 18:41:12 -0700839 }
840 }
841 }
842
Kevin Chyn4cc49f72019-04-24 13:53:35 -0700843 private final FaceConstants mFaceConstants = new FaceConstants();
Kevin Chyna56dff72018-06-19 18:41:12 -0700844
845 @GuardedBy("this")
846 private IBiometricsFace mDaemon;
Kevin Chyn9bc1d492019-06-21 15:31:50 -0700847 private UsageStats mUsageStats;
Ilya Matyukhin9dac8482019-07-09 11:03:07 -0700848 private boolean mRevokeChallengePending = false;
Kevin Chyna38653c2019-02-11 17:46:21 -0800849 // One of the AuthenticationClient constants
850 private int mCurrentUserLockoutMode;
Kevin Chyna56dff72018-06-19 18:41:12 -0700851
Kevin Chyndbfbed42019-06-13 17:01:30 -0700852 private NotificationManager mNotificationManager;
853
Kevin Chyn4cc49f72019-04-24 13:53:35 -0700854 private int[] mBiometricPromptIgnoreList;
855 private int[] mBiometricPromptIgnoreListVendor;
856 private int[] mKeyguardIgnoreList;
857 private int[] mKeyguardIgnoreListVendor;
858 private int[] mEnrollIgnoreList;
859 private int[] mEnrollIgnoreListVendor;
860
Kevin Chyna56dff72018-06-19 18:41:12 -0700861 /**
862 * Receives callbacks from the HAL.
863 */
864 private IBiometricsFaceClientCallback mDaemonCallback =
865 new IBiometricsFaceClientCallback.Stub() {
Kevin Chyn6737c572019-02-08 16:10:54 -0800866 @Override
867 public void onEnrollResult(final long deviceId, int faceId, int userId,
868 int remaining) {
869 mHandler.post(() -> {
870 final Face face = new Face(getBiometricUtils()
871 .getUniqueName(getContext(), userId), faceId, deviceId);
872 FaceService.super.handleEnrollResult(face, remaining);
Kevin Chyn576811e2019-06-12 14:50:35 -0700873
874 // Enrollment changes the authenticatorId, so update it here.
875 IBiometricsFace daemon = getFaceDaemon();
876 if (remaining == 0 && daemon != null) {
877 try {
878 mAuthenticatorIds.put(userId,
879 hasEnrolledBiometrics(userId) ? daemon.getAuthenticatorId().value
880 : 0L);
881 } catch (RemoteException e) {
882 Slog.e(TAG, "Unable to get authenticatorId", e);
883 }
884 }
Kevin Chyn6737c572019-02-08 16:10:54 -0800885 });
886 }
887
888 @Override
889 public void onAcquired(final long deviceId, final int userId,
890 final int acquiredInfo,
891 final int vendorCode) {
892 mHandler.post(() -> {
893 FaceService.super.handleAcquired(deviceId, acquiredInfo, vendorCode);
894 });
895 }
896
897 @Override
898 public void onAuthenticated(final long deviceId, final int faceId, final int userId,
899 ArrayList<Byte> token) {
900 mHandler.post(() -> {
901 Face face = new Face("", faceId, deviceId);
902 FaceService.super.handleAuthenticated(face, token);
903 });
904 }
905
906 @Override
907 public void onError(final long deviceId, final int userId, final int error,
908 final int vendorCode) {
909 mHandler.post(() -> {
910 FaceService.super.handleError(deviceId, error, vendorCode);
911
912 // TODO: this chunk of code should be common to all biometric services
913 if (error == BiometricConstants.BIOMETRIC_ERROR_HW_UNAVAILABLE) {
914 // If we get HW_UNAVAILABLE, try to connect again later...
915 Slog.w(TAG, "Got ERROR_HW_UNAVAILABLE; try reconnecting next client.");
916 synchronized (this) {
917 mDaemon = null;
918 mHalDeviceId = 0;
919 mCurrentUserId = UserHandle.USER_NULL;
920 }
Kevin Chyna56dff72018-06-19 18:41:12 -0700921 }
Kevin Chyn6737c572019-02-08 16:10:54 -0800922 });
923 }
Kevin Chyna56dff72018-06-19 18:41:12 -0700924
Kevin Chyn6737c572019-02-08 16:10:54 -0800925 @Override
Kevin Chyn31ae8d22019-03-11 14:12:32 -0700926 public void onRemoved(final long deviceId, ArrayList<Integer> faceIds, final int userId) {
Kevin Chyn6737c572019-02-08 16:10:54 -0800927 mHandler.post(() -> {
Kevin Chyn31ae8d22019-03-11 14:12:32 -0700928 if (!faceIds.isEmpty()) {
929 for (int i = 0; i < faceIds.size(); i++) {
930 final Face face = new Face("", faceIds.get(i), deviceId);
931 // Convert to old behavior
932 FaceService.super.handleRemoved(face, faceIds.size() - i - 1);
933 }
934 } else {
935 final Face face = new Face("", 0 /* identifier */, deviceId);
936 FaceService.super.handleRemoved(face, 0 /* remaining */);
937 }
Curtis Belmonte8cfe3712019-09-26 16:02:41 -0700938 Settings.Secure.putIntForUser(getContext().getContentResolver(),
939 Settings.Secure.FACE_UNLOCK_RE_ENROLL, 0, UserHandle.USER_CURRENT);
Kevin Chyn6737c572019-02-08 16:10:54 -0800940 });
941 }
942
943 @Override
944 public void onEnumerate(long deviceId, ArrayList<Integer> faceIds, int userId)
945 throws RemoteException {
946 mHandler.post(() -> {
947 if (!faceIds.isEmpty()) {
948 for (int i = 0; i < faceIds.size(); i++) {
949 final Face face = new Face("", faceIds.get(i), deviceId);
950 // Convert to old old behavior
951 FaceService.super.handleEnumerate(face, faceIds.size() - i - 1);
952 }
953 } else {
954 // For face, the HIDL contract is to receive an empty list when there are no
955 // templates enrolled. Send a null identifier since we don't consume them
956 // anywhere, and send remaining == 0 to plumb this with existing common code.
957 FaceService.super.handleEnumerate(null /* identifier */, 0);
Kevin Chyna56dff72018-06-19 18:41:12 -0700958 }
Kevin Chyn6737c572019-02-08 16:10:54 -0800959 });
960 }
Kevin Chyna56dff72018-06-19 18:41:12 -0700961
Kevin Chyn6737c572019-02-08 16:10:54 -0800962 @Override
963 public void onLockoutChanged(long duration) {
Kevin Chyna38653c2019-02-11 17:46:21 -0800964 Slog.d(TAG, "onLockoutChanged: " + duration);
Curtis Belmontec47df862020-01-28 16:58:34 -0800965
Kevin Chyna38653c2019-02-11 17:46:21 -0800966 if (duration == 0) {
967 mCurrentUserLockoutMode = AuthenticationClient.LOCKOUT_NONE;
Curtis Belmontec47df862020-01-28 16:58:34 -0800968 } else if (duration == -1 || duration == Long.MAX_VALUE) {
Kevin Chyna38653c2019-02-11 17:46:21 -0800969 mCurrentUserLockoutMode = AuthenticationClient.LOCKOUT_PERMANENT;
970 } else {
971 mCurrentUserLockoutMode = AuthenticationClient.LOCKOUT_TIMED;
972 }
Kevin Chyna56dff72018-06-19 18:41:12 -0700973
Kevin Chyna38653c2019-02-11 17:46:21 -0800974 mHandler.post(() -> {
975 if (duration == 0) {
976 notifyLockoutResetMonitors();
977 }
978 });
Kevin Chyn6737c572019-02-08 16:10:54 -0800979 }
980 };
Kevin Chyna56dff72018-06-19 18:41:12 -0700981
982 /**
983 * Wraps the HAL-specific code and is passed to the ClientMonitor implementations so that they
984 * can be shared between the multiple biometric services.
985 */
986 private final DaemonWrapper mDaemonWrapper = new DaemonWrapper() {
987 @Override
988 public int authenticate(long operationId, int groupId) throws RemoteException {
989 IBiometricsFace daemon = getFaceDaemon();
990 if (daemon == null) {
991 Slog.w(TAG, "authenticate(): no face HAL!");
992 return ERROR_ESRCH;
993 }
994 return daemon.authenticate(operationId);
995 }
996
997 @Override
998 public int cancel() throws RemoteException {
999 IBiometricsFace daemon = getFaceDaemon();
1000 if (daemon == null) {
1001 Slog.w(TAG, "cancel(): no face HAL!");
1002 return ERROR_ESRCH;
1003 }
1004 return daemon.cancel();
1005 }
1006
1007 @Override
1008 public int remove(int groupId, int biometricId) throws RemoteException {
1009 IBiometricsFace daemon = getFaceDaemon();
1010 if (daemon == null) {
1011 Slog.w(TAG, "remove(): no face HAL!");
1012 return ERROR_ESRCH;
1013 }
1014 return daemon.remove(biometricId);
1015 }
1016
1017 @Override
1018 public int enumerate() throws RemoteException {
1019 IBiometricsFace daemon = getFaceDaemon();
1020 if (daemon == null) {
1021 Slog.w(TAG, "enumerate(): no face HAL!");
1022 return ERROR_ESRCH;
1023 }
1024 return daemon.enumerate();
1025 }
1026
1027 @Override
Kevin Chyn1f16c2d2018-12-07 13:06:08 -08001028 public int enroll(byte[] cryptoToken, int groupId, int timeout,
1029 ArrayList<Integer> disabledFeatures) throws RemoteException {
Kevin Chyna56dff72018-06-19 18:41:12 -07001030 IBiometricsFace daemon = getFaceDaemon();
1031 if (daemon == null) {
1032 Slog.w(TAG, "enroll(): no face HAL!");
1033 return ERROR_ESRCH;
1034 }
1035 final ArrayList<Byte> token = new ArrayList<>();
1036 for (int i = 0; i < cryptoToken.length; i++) {
1037 token.add(cryptoToken[i]);
1038 }
Kevin Chyn1f16c2d2018-12-07 13:06:08 -08001039 return daemon.enroll(token, timeout, disabledFeatures);
Kevin Chyna56dff72018-06-19 18:41:12 -07001040 }
Kevin Chyna38653c2019-02-11 17:46:21 -08001041
1042 @Override
1043 public void resetLockout(byte[] cryptoToken) throws RemoteException {
1044 IBiometricsFace daemon = getFaceDaemon();
1045 if (daemon == null) {
1046 Slog.w(TAG, "resetLockout(): no face HAL!");
1047 return;
1048 }
1049 final ArrayList<Byte> token = new ArrayList<>();
1050 for (int i = 0; i < cryptoToken.length; i++) {
1051 token.add(cryptoToken[i]);
1052 }
1053 daemon.resetLockout(token);
1054 }
Kevin Chyna56dff72018-06-19 18:41:12 -07001055 };
1056
1057
1058 public FaceService(Context context) {
1059 super(context);
Kevin Chyn4cc49f72019-04-24 13:53:35 -07001060
joshmccloskey4ceaf6b2019-08-21 11:26:08 -07001061 final boolean ignoreKeyguardBlacklist = Settings.Secure.getInt(context.getContentResolver(),
1062 SKIP_KEYGUARD_ACQUIRE_IGNORE_LIST, 0) != 0;
1063
Kevin Chyn9bc1d492019-06-21 15:31:50 -07001064 mUsageStats = new UsageStats(context);
1065
Kevin Chyndbfbed42019-06-13 17:01:30 -07001066 mNotificationManager = getContext().getSystemService(NotificationManager.class);
1067
Kevin Chyn4cc49f72019-04-24 13:53:35 -07001068 mBiometricPromptIgnoreList = getContext().getResources()
1069 .getIntArray(R.array.config_face_acquire_biometricprompt_ignorelist);
1070 mBiometricPromptIgnoreListVendor = getContext().getResources()
1071 .getIntArray(R.array.config_face_acquire_vendor_biometricprompt_ignorelist);
joshmccloskey4ceaf6b2019-08-21 11:26:08 -07001072 mKeyguardIgnoreList = ignoreKeyguardBlacklist ? new int[0] : getContext().getResources()
Kevin Chyn4cc49f72019-04-24 13:53:35 -07001073 .getIntArray(R.array.config_face_acquire_keyguard_ignorelist);
joshmccloskey4ceaf6b2019-08-21 11:26:08 -07001074 mKeyguardIgnoreListVendor =
1075 ignoreKeyguardBlacklist ? new int[0] : getContext().getResources()
1076 .getIntArray(R.array.config_face_acquire_vendor_keyguard_ignorelist);
Kevin Chyn4cc49f72019-04-24 13:53:35 -07001077 mEnrollIgnoreList = getContext().getResources()
1078 .getIntArray(R.array.config_face_acquire_enroll_ignorelist);
1079 mEnrollIgnoreListVendor = getContext().getResources()
1080 .getIntArray(R.array.config_face_acquire_vendor_enroll_ignorelist);
Kevin Chyna56dff72018-06-19 18:41:12 -07001081 }
1082
1083 @Override
Ilya Matyukhin9dac8482019-07-09 11:03:07 -07001084 protected void removeClient(ClientMonitor client) {
1085 super.removeClient(client);
1086 if (mRevokeChallengePending) {
1087 startRevokeChallenge(null);
1088 mRevokeChallengePending = false;
1089 }
1090 }
1091
1092 @Override
Kevin Chyna56dff72018-06-19 18:41:12 -07001093 public void onStart() {
Kevin Chyn5a2ff5d2018-08-29 19:07:30 -07001094 super.onStart();
Kevin Chyna56dff72018-06-19 18:41:12 -07001095 publishBinderService(Context.FACE_SERVICE, new FaceServiceWrapper());
Kevin Chyn8398a712019-06-13 16:44:50 -07001096 // Get the face daemon on FaceService's on thread so SystemServerInitThreadPool isn't
1097 // blocked
Felipe Lemeb68b7692019-10-09 10:43:03 -07001098 SystemServerInitThreadPool.submit(() -> mHandler.post(this::getFaceDaemon),
Kevin Chyn8398a712019-06-13 16:44:50 -07001099 TAG + ".onStart");
Kevin Chyna56dff72018-06-19 18:41:12 -07001100 }
1101
1102 @Override
1103 public String getTag() {
1104 return TAG;
1105 }
1106
1107 @Override
Kevin Chyn6737c572019-02-08 16:10:54 -08001108 protected DaemonWrapper getDaemonWrapper() {
1109 return mDaemonWrapper;
1110 }
1111
1112 @Override
Kevin Chyna56dff72018-06-19 18:41:12 -07001113 protected BiometricUtils getBiometricUtils() {
1114 return FaceUtils.getInstance();
1115 }
1116
1117 @Override
Kevin Chyn4cc49f72019-04-24 13:53:35 -07001118 protected Constants getConstants() {
1119 return mFaceConstants;
Kevin Chyna56dff72018-06-19 18:41:12 -07001120 }
1121
1122 @Override
1123 protected boolean hasReachedEnrollmentLimit(int userId) {
1124 final int limit = getContext().getResources().getInteger(
Kevin Chyn017e76e2018-06-27 18:35:06 -07001125 com.android.internal.R.integer.config_faceMaxTemplatesPerUser);
Kevin Chyn6737c572019-02-08 16:10:54 -08001126 final int enrolled = FaceService.this.getEnrolledTemplates(userId).size();
Kevin Chyna56dff72018-06-19 18:41:12 -07001127 if (enrolled >= limit) {
Kevin Chyn1a878c12019-04-04 15:50:11 -07001128 Slog.w(TAG, "Too many faces registered, user: " + userId);
Kevin Chyna56dff72018-06-19 18:41:12 -07001129 return true;
1130 }
1131 return false;
1132 }
1133
1134 @Override
Kevin Chyn9ba99912019-01-16 16:24:36 -08001135 public void serviceDied(long cookie) {
1136 super.serviceDied(cookie);
1137 mDaemon = null;
Jim Miller7b78b222019-02-07 16:47:38 -08001138
1139 mCurrentUserId = UserHandle.USER_NULL; // Force updateActiveGroup() to re-evaluate
Kevin Chyn9ba99912019-01-16 16:24:36 -08001140 }
1141
1142 @Override
Kevin Chyna56dff72018-06-19 18:41:12 -07001143 protected void updateActiveGroup(int userId, String clientPackage) {
1144 IBiometricsFace daemon = getFaceDaemon();
1145
1146 if (daemon != null) {
1147 try {
1148 userId = getUserOrWorkProfileId(clientPackage, userId);
1149 if (userId != mCurrentUserId) {
Kevin Chyn27e33d02019-04-30 01:37:32 +00001150 final File baseDir = Environment.getDataVendorDeDirectory(userId);
Kevin Chyna56dff72018-06-19 18:41:12 -07001151 final File faceDir = new File(baseDir, FACE_DATA_DIR);
1152 if (!faceDir.exists()) {
1153 if (!faceDir.mkdir()) {
1154 Slog.v(TAG, "Cannot make directory: " + faceDir.getAbsolutePath());
1155 return;
1156 }
1157 // Calling mkdir() from this process will create a directory with our
1158 // permissions (inherited from the containing dir). This command fixes
1159 // the label.
1160 if (!SELinux.restorecon(faceDir)) {
1161 Slog.w(TAG, "Restorecons failed. Directory will have wrong label.");
1162 return;
1163 }
1164 }
1165
1166 daemon.setActiveUser(userId, faceDir.getAbsolutePath());
1167 mCurrentUserId = userId;
Kevin Chyn576811e2019-06-12 14:50:35 -07001168 mAuthenticatorIds.put(userId,
1169 hasEnrolledBiometrics(userId) ? daemon.getAuthenticatorId().value : 0L);
Kevin Chyna56dff72018-06-19 18:41:12 -07001170 }
Kevin Chyna56dff72018-06-19 18:41:12 -07001171 } catch (RemoteException e) {
1172 Slog.e(TAG, "Failed to setActiveUser():", e);
1173 }
1174 }
1175 }
1176
1177 @Override
1178 protected String getLockoutResetIntent() {
1179 return ACTION_LOCKOUT_RESET;
1180 }
1181
1182 @Override
1183 protected String getLockoutBroadcastPermission() {
1184 return RESET_FACE_LOCKOUT;
1185 }
1186
1187 @Override
1188 protected long getHalDeviceId() {
1189 return mHalDeviceId;
1190 }
1191
1192 @Override
Kevin Chyna38653c2019-02-11 17:46:21 -08001193 protected void handleUserSwitching(int userId) {
1194 super.handleUserSwitching(userId);
1195 // Will be updated when we get the callback from HAL
1196 mCurrentUserLockoutMode = AuthenticationClient.LOCKOUT_NONE;
1197 }
1198
1199 @Override
Kevin Chyna56dff72018-06-19 18:41:12 -07001200 protected boolean hasEnrolledBiometrics(int userId) {
1201 if (userId != UserHandle.getCallingUserId()) {
1202 checkPermission(INTERACT_ACROSS_USERS);
1203 }
1204 return getBiometricUtils().getBiometricsForUser(getContext(), userId).size() > 0;
1205 }
1206
1207 @Override
1208 protected String getManageBiometricPermission() {
Kevin Chyna24e9fd2018-08-27 12:39:17 -07001209 return MANAGE_BIOMETRIC;
Kevin Chyna56dff72018-06-19 18:41:12 -07001210 }
1211
1212 @Override
1213 protected void checkUseBiometricPermission() {
Kevin Chyna24e9fd2018-08-27 12:39:17 -07001214 // noop for Face. The permission checks are all done on the incoming binder call.
Kevin Chyna56dff72018-06-19 18:41:12 -07001215 }
1216
1217 @Override
Kevin Chynb3c05aa2018-09-21 16:50:32 -07001218 protected boolean checkAppOps(int uid, String opPackageName) {
1219 return mAppOps.noteOp(AppOpsManager.OP_USE_BIOMETRIC, uid, opPackageName)
1220 == AppOpsManager.MODE_ALLOWED;
Kevin Chyna56dff72018-06-19 18:41:12 -07001221 }
1222
1223 @Override
Kevin Chyn6737c572019-02-08 16:10:54 -08001224 protected List<Face> getEnrolledTemplates(int userId) {
1225 return getBiometricUtils().getBiometricsForUser(getContext(), userId);
1226 }
1227
1228 @Override
Kevin Chyna56dff72018-06-19 18:41:12 -07001229 protected void notifyClientActiveCallbacks(boolean isActive) {
1230 // noop for Face.
1231 }
1232
Kevin Chyn7782d142019-01-18 12:51:33 -08001233 @Override
1234 protected int statsModality() {
1235 return BiometricsProtoEnums.MODALITY_FACE;
1236 }
1237
Kevin Chyna38653c2019-02-11 17:46:21 -08001238 @Override
1239 protected int getLockoutMode() {
1240 return mCurrentUserLockoutMode;
1241 }
1242
Kevin Chyna56dff72018-06-19 18:41:12 -07001243 /** Gets the face daemon */
1244 private synchronized IBiometricsFace getFaceDaemon() {
1245 if (mDaemon == null) {
1246 Slog.v(TAG, "mDaemon was null, reconnect to face");
1247 try {
1248 mDaemon = IBiometricsFace.getService();
1249 } catch (java.util.NoSuchElementException e) {
1250 // Service doesn't exist or cannot be opened. Logged below.
1251 } catch (RemoteException e) {
1252 Slog.e(TAG, "Failed to get biometric interface", e);
1253 }
1254 if (mDaemon == null) {
1255 Slog.w(TAG, "face HIDL not available");
1256 return null;
1257 }
1258
1259 mDaemon.asBinder().linkToDeath(this, 0);
1260
1261 try {
1262 mHalDeviceId = mDaemon.setCallback(mDaemonCallback).value;
1263 } catch (RemoteException e) {
1264 Slog.e(TAG, "Failed to open face HAL", e);
1265 mDaemon = null; // try again later!
1266 }
1267
1268 if (DEBUG) Slog.v(TAG, "Face HAL id: " + mHalDeviceId);
1269 if (mHalDeviceId != 0) {
1270 loadAuthenticatorIds();
1271 updateActiveGroup(ActivityManager.getCurrentUser(), null);
Kevin Chyn6737c572019-02-08 16:10:54 -08001272 doTemplateCleanupForUser(ActivityManager.getCurrentUser());
Kevin Chyna56dff72018-06-19 18:41:12 -07001273 } else {
1274 Slog.w(TAG, "Failed to open Face HAL!");
1275 MetricsLogger.count(getContext(), "faced_openhal_error", 1);
1276 mDaemon = null;
1277 }
1278 }
1279 return mDaemon;
1280 }
1281
Kevin Chynd79e24e2018-09-25 12:06:59 -07001282 private long startGenerateChallenge(IBinder token) {
Kevin Chyna56dff72018-06-19 18:41:12 -07001283 IBiometricsFace daemon = getFaceDaemon();
1284 if (daemon == null) {
Kevin Chynd79e24e2018-09-25 12:06:59 -07001285 Slog.w(TAG, "startGenerateChallenge: no face HAL!");
Kevin Chyna56dff72018-06-19 18:41:12 -07001286 return 0;
1287 }
1288 try {
Kevin Chyne46a2162018-09-20 18:43:01 -07001289 return daemon.generateChallenge(CHALLENGE_TIMEOUT_SEC).value;
Kevin Chyna56dff72018-06-19 18:41:12 -07001290 } catch (RemoteException e) {
Kevin Chynd79e24e2018-09-25 12:06:59 -07001291 Slog.e(TAG, "startGenerateChallenge failed", e);
Kevin Chyna56dff72018-06-19 18:41:12 -07001292 }
1293 return 0;
1294 }
1295
Kevin Chynd79e24e2018-09-25 12:06:59 -07001296 private int startRevokeChallenge(IBinder token) {
Kevin Chyna56dff72018-06-19 18:41:12 -07001297 IBiometricsFace daemon = getFaceDaemon();
1298 if (daemon == null) {
Kevin Chynd79e24e2018-09-25 12:06:59 -07001299 Slog.w(TAG, "startRevokeChallenge: no face HAL!");
Kevin Chyna56dff72018-06-19 18:41:12 -07001300 return 0;
1301 }
1302 try {
Ilya Matyukhin9dac8482019-07-09 11:03:07 -07001303 final int res = daemon.revokeChallenge();
1304 if (res != Status.OK) {
1305 Slog.e(TAG, "revokeChallenge returned " + res);
1306 }
1307 return res;
Kevin Chyna56dff72018-06-19 18:41:12 -07001308 } catch (RemoteException e) {
Kevin Chynd79e24e2018-09-25 12:06:59 -07001309 Slog.e(TAG, "startRevokeChallenge failed", e);
Kevin Chyna56dff72018-06-19 18:41:12 -07001310 }
1311 return 0;
1312 }
1313
Gilad Brettercb51b8b2018-03-22 17:04:51 +02001314 private void dumpInternal(PrintWriter pw) {
1315 JSONObject dump = new JSONObject();
1316 try {
1317 dump.put("service", "Face Manager");
1318
1319 JSONArray sets = new JSONArray();
1320 for (UserInfo user : UserManager.get(getContext()).getUsers()) {
1321 final int userId = user.getUserHandle().getIdentifier();
Kevin Chyna56dff72018-06-19 18:41:12 -07001322 final int N = getBiometricUtils().getBiometricsForUser(getContext(), userId).size();
Gilad Brettercb51b8b2018-03-22 17:04:51 +02001323 PerformanceStats stats = mPerformanceMap.get(userId);
1324 PerformanceStats cryptoStats = mCryptoPerformanceMap.get(userId);
1325 JSONObject set = new JSONObject();
1326 set.put("id", userId);
Kevin Chyna56dff72018-06-19 18:41:12 -07001327 set.put("count", N);
Gilad Brettercb51b8b2018-03-22 17:04:51 +02001328 set.put("accept", (stats != null) ? stats.accept : 0);
1329 set.put("reject", (stats != null) ? stats.reject : 0);
1330 set.put("acquire", (stats != null) ? stats.acquire : 0);
1331 set.put("lockout", (stats != null) ? stats.lockout : 0);
1332 set.put("permanentLockout", (stats != null) ? stats.permanentLockout : 0);
1333 // cryptoStats measures statistics about secure face transactions
1334 // (e.g. to unlock password storage, make secure purchases, etc.)
1335 set.put("acceptCrypto", (cryptoStats != null) ? cryptoStats.accept : 0);
1336 set.put("rejectCrypto", (cryptoStats != null) ? cryptoStats.reject : 0);
1337 set.put("acquireCrypto", (cryptoStats != null) ? cryptoStats.acquire : 0);
1338 set.put("lockoutCrypto", (cryptoStats != null) ? cryptoStats.lockout : 0);
Kevin Chyna56dff72018-06-19 18:41:12 -07001339 set.put("permanentLockoutCrypto",
1340 (cryptoStats != null) ? cryptoStats.permanentLockout : 0);
Gilad Brettercb51b8b2018-03-22 17:04:51 +02001341 sets.put(set);
1342 }
1343
1344 dump.put("prints", sets);
1345 } catch (JSONException e) {
1346 Slog.e(TAG, "dump formatting failure", e);
1347 }
1348 pw.println(dump);
Kevin Chyn9bc1d492019-06-21 15:31:50 -07001349 pw.println("HAL deaths since last reboot: " + mHALDeathCount);
1350
1351 mUsageStats.print(pw);
Gilad Brettercb51b8b2018-03-22 17:04:51 +02001352 }
1353
Joe Onoratodb396002019-04-05 19:49:27 -07001354 private void dumpHal(FileDescriptor fd, String[] args) {
Joe Onoratobf955d22019-03-25 00:16:58 -07001355 // WARNING: CDD restricts image data from leaving TEE unencrypted on
1356 // production devices:
1357 // [C-1-10] MUST not allow unencrypted access to identifiable biometric
1358 // data or any data derived from it (such as embeddings) to the
1359 // Application Processor outside the context of the TEE.
1360 // As such, this API should only be enabled for testing purposes on
1361 // engineering and userdebug builds. All modules in the software stack
1362 // MUST enforce final build products do NOT have this functionality.
1363 // Additionally, the following check MUST NOT be removed.
1364 if (!(Build.IS_ENG || Build.IS_USERDEBUG)) {
1365 return;
1366 }
1367
Joe Onorato108413a2019-04-03 18:20:52 -07001368 // Additionally, this flag allows turning off face for a device
1369 // (either permanently through the build or on an individual device).
1370 if (SystemProperties.getBoolean("ro.face.disable_debug_data", false)
1371 || SystemProperties.getBoolean("persist.face.disable_debug_data", false)) {
1372 return;
1373 }
1374
Joe Onoratodb396002019-04-05 19:49:27 -07001375 // The debug method takes two file descriptors. The first is for text
1376 // output, which we will drop. The second is for binary data, which
1377 // will be the protobuf data.
1378 final IBiometricsFace daemon = getFaceDaemon();
1379 if (daemon != null) {
1380 FileOutputStream devnull = null;
1381 try {
1382 devnull = new FileOutputStream("/dev/null");
1383 final NativeHandle handle = new NativeHandle(
1384 new FileDescriptor[] { devnull.getFD(), fd },
1385 new int[0], false);
1386 daemon.debug(handle, new ArrayList<String>(Arrays.asList(args)));
1387 } catch (IOException | RemoteException ex) {
1388 Slog.d(TAG, "error while reading face debugging data", ex);
1389 } finally {
1390 if (devnull != null) {
1391 try {
1392 devnull.close();
1393 } catch (IOException ex) {
1394 }
1395 }
1396 }
Joe Onoratobf955d22019-03-25 00:16:58 -07001397 }
Joe Onoratobf955d22019-03-25 00:16:58 -07001398 }
Kevin Chyn8b7a0372018-09-17 15:06:05 -07001399}