blob: feeb16b3ea62cb76aa751983f441b798b74c0f91 [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;
Gilad Brettercb51b8b2018-03-22 17:04:51 +020023
24import android.app.ActivityManager;
Gilad Brettercb51b8b2018-03-22 17:04:51 +020025import android.app.AppOpsManager;
Gilad Brettercb51b8b2018-03-22 17:04:51 +020026import android.content.Context;
Gilad Brettercb51b8b2018-03-22 17:04:51 +020027import android.content.pm.UserInfo;
Kevin Chyna56dff72018-06-19 18:41:12 -070028import android.hardware.biometrics.BiometricAuthenticator;
29import android.hardware.biometrics.BiometricConstants;
Kevin Chyn7782d142019-01-18 12:51:33 -080030import android.hardware.biometrics.BiometricsProtoEnums;
Kevin Chyna56dff72018-06-19 18:41:12 -070031import android.hardware.biometrics.IBiometricServiceLockoutResetCallback;
Kevin Chyn23289ef2018-11-28 16:32:36 -080032import android.hardware.biometrics.IBiometricServiceReceiverInternal;
Gilad Brettercb51b8b2018-03-22 17:04:51 +020033import android.hardware.biometrics.face.V1_0.IBiometricsFace;
34import android.hardware.biometrics.face.V1_0.IBiometricsFaceClientCallback;
Kevin Chynb95f1522019-03-04 16:45:15 -080035import android.hardware.biometrics.face.V1_0.OptionalBool;
Kevin Chynd79e24e2018-09-25 12:06:59 -070036import android.hardware.biometrics.face.V1_0.Status;
Gilad Brettercb51b8b2018-03-22 17:04:51 +020037import android.hardware.face.Face;
Kevin Chyna24e9fd2018-08-27 12:39:17 -070038import android.hardware.face.FaceManager;
Gilad Brettercb51b8b2018-03-22 17:04:51 +020039import android.hardware.face.IFaceService;
Gilad Brettercb51b8b2018-03-22 17:04:51 +020040import android.hardware.face.IFaceServiceReceiver;
41import android.os.Binder;
Gilad Brettercb51b8b2018-03-22 17:04:51 +020042import android.os.Environment;
Gilad Brettercb51b8b2018-03-22 17:04:51 +020043import android.os.IBinder;
Gilad Brettercb51b8b2018-03-22 17:04:51 +020044import android.os.RemoteException;
45import android.os.SELinux;
Gilad Brettercb51b8b2018-03-22 17:04:51 +020046import android.os.UserHandle;
47import android.os.UserManager;
Gilad Brettercb51b8b2018-03-22 17:04:51 +020048import android.util.Slog;
49import android.util.proto.ProtoOutputStream;
50
51import com.android.internal.annotations.GuardedBy;
52import com.android.internal.logging.MetricsLogger;
53import com.android.internal.util.DumpUtils;
54import com.android.server.SystemServerInitThreadPool;
Kevin Chyna38653c2019-02-11 17:46:21 -080055import com.android.server.biometrics.AuthenticationClient;
Kevin Chyn355c6bf2018-09-20 22:14:19 -070056import com.android.server.biometrics.BiometricServiceBase;
Kevin Chyn836f2cf2018-08-27 11:06:39 -070057import com.android.server.biometrics.BiometricUtils;
Kevin Chyn6737c572019-02-08 16:10:54 -080058import com.android.server.biometrics.EnumerateClient;
Kevin Chyn836f2cf2018-08-27 11:06:39 -070059import com.android.server.biometrics.Metrics;
Kevin Chyn6737c572019-02-08 16:10:54 -080060import com.android.server.biometrics.RemovalClient;
Gilad Brettercb51b8b2018-03-22 17:04:51 +020061
62import org.json.JSONArray;
63import org.json.JSONException;
64import org.json.JSONObject;
65
66import java.io.File;
67import java.io.FileDescriptor;
68import java.io.PrintWriter;
69import java.util.ArrayList;
Gilad Brettercb51b8b2018-03-22 17:04:51 +020070import java.util.List;
Gilad Brettercb51b8b2018-03-22 17:04:51 +020071
72/**
73 * A service to manage multiple clients that want to access the face HAL API.
74 * The service is responsible for maintaining a list of clients and dispatching all
Kevin Chyn51676d22018-11-05 18:00:43 -080075 * face-related events.
Gilad Brettercb51b8b2018-03-22 17:04:51 +020076 *
77 * @hide
78 */
Kevin Chyn355c6bf2018-09-20 22:14:19 -070079public class FaceService extends BiometricServiceBase {
Kevin Chyna56dff72018-06-19 18:41:12 -070080
81 protected static final String TAG = "FaceService";
82 private static final boolean DEBUG = true;
Gilad Brettercb51b8b2018-03-22 17:04:51 +020083 private static final String FACE_DATA_DIR = "facedata";
Gilad Brettercb51b8b2018-03-22 17:04:51 +020084 private static final String ACTION_LOCKOUT_RESET =
Kevin Chyn2ffadb32018-06-19 11:29:38 -070085 "com.android.server.biometrics.face.ACTION_LOCKOUT_RESET";
Kevin Chyne46a2162018-09-20 18:43:01 -070086 private static final int CHALLENGE_TIMEOUT_SEC = 600; // 10 minutes
Gilad Brettercb51b8b2018-03-22 17:04:51 +020087
Kevin Chyn8b7a0372018-09-17 15:06:05 -070088 private final class FaceAuthClient extends AuthenticationClientImpl {
89 public FaceAuthClient(Context context,
90 DaemonWrapper daemon, long halDeviceId, IBinder token,
91 ServiceListener listener, int targetUserId, int groupId, long opId,
Kevin Chyn87f257a2018-11-27 16:26:07 -080092 boolean restricted, String owner, int cookie, boolean requireConfirmation) {
Kevin Chyn8b7a0372018-09-17 15:06:05 -070093 super(context, daemon, halDeviceId, token, listener, targetUserId, groupId, opId,
Kevin Chyn87f257a2018-11-27 16:26:07 -080094 restricted, owner, cookie, requireConfirmation);
Kevin Chyn8b7a0372018-09-17 15:06:05 -070095 }
Kevin Chyn7782d142019-01-18 12:51:33 -080096
97 @Override
98 protected int statsModality() {
99 return FaceService.this.statsModality();
100 }
Kevin Chyna38653c2019-02-11 17:46:21 -0800101
102 @Override
103 public boolean shouldFrameworkHandleLockout() {
104 return false;
105 }
Kevin Chyn56d6b072019-02-13 18:39:01 -0800106
107 @Override
108 public boolean onAuthenticated(BiometricAuthenticator.Identifier identifier,
109 boolean authenticated, ArrayList<Byte> token) {
110 final boolean result = super.onAuthenticated(identifier, authenticated, token);
111
112 // For face, the authentication lifecycle ends either when
113 // 1) Authenticated == true
114 // 2) Error occurred
115 // 3) Authenticated == false
116 // Fingerprint currently does not end when the third condition is met which is a bug,
117 // but let's leave it as-is for now.
118 return result || !authenticated;
119 }
Kevin Chyn8b7a0372018-09-17 15:06:05 -0700120 }
121
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200122 /**
Kevin Chyna56dff72018-06-19 18:41:12 -0700123 * Receives the incoming binder calls from FaceManager.
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200124 */
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200125 private final class FaceServiceWrapper extends IFaceService.Stub {
Kevin Chyna56dff72018-06-19 18:41:12 -0700126
127 /**
128 * The following methods contain common code which is shared in biometrics/common.
129 */
Kevin Chyna38653c2019-02-11 17:46:21 -0800130
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200131 @Override // Binder call
Kevin Chynd79e24e2018-09-25 12:06:59 -0700132 public long generateChallenge(IBinder token) {
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700133 checkPermission(MANAGE_BIOMETRIC);
Kevin Chynd79e24e2018-09-25 12:06:59 -0700134 return startGenerateChallenge(token);
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200135 }
136
137 @Override // Binder call
Kevin Chynd79e24e2018-09-25 12:06:59 -0700138 public int revokeChallenge(IBinder token) {
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700139 checkPermission(MANAGE_BIOMETRIC);
Kevin Chynd79e24e2018-09-25 12:06:59 -0700140 return startRevokeChallenge(token);
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200141 }
142
143 @Override // Binder call
Kevin Chyn1f16c2d2018-12-07 13:06:08 -0800144 public void enroll(final IBinder token, final byte[] cryptoToken,
145 final IFaceServiceReceiver receiver, final String opPackageName,
146 final int[] disabledFeatures) {
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700147 checkPermission(MANAGE_BIOMETRIC);
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200148
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200149 final boolean restricted = isRestricted();
Kevin Chyna56dff72018-06-19 18:41:12 -0700150 final EnrollClientImpl client = new EnrollClientImpl(getContext(), mDaemonWrapper,
151 mHalDeviceId, token, new ServiceListenerImpl(receiver), mCurrentUserId,
Kevin Chyn1429a312019-01-28 16:08:09 -0800152 0 /* groupId */, cryptoToken, restricted, opPackageName, disabledFeatures) {
153 @Override
154 public boolean shouldVibrate() {
155 return false;
156 }
Kevin Chyn7782d142019-01-18 12:51:33 -0800157
158 @Override
159 protected int statsModality() {
160 return FaceService.this.statsModality();
161 }
Kevin Chyn1429a312019-01-28 16:08:09 -0800162 };
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200163
Kevin Chyn1f16c2d2018-12-07 13:06:08 -0800164 enrollInternal(client, UserHandle.getCallingUserId());
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200165 }
166
167 @Override // Binder call
168 public void cancelEnrollment(final IBinder token) {
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700169 checkPermission(MANAGE_BIOMETRIC);
Kevin Chyna56dff72018-06-19 18:41:12 -0700170 cancelEnrollmentInternal(token);
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200171 }
172
173 @Override // Binder call
Kevin Chyn747e29b2019-01-11 17:01:53 -0800174 public void authenticate(final IBinder token, final long opId, int userId,
Kevin Chyna56dff72018-06-19 18:41:12 -0700175 final IFaceServiceReceiver receiver, final int flags,
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700176 final String opPackageName) {
177 checkPermission(USE_BIOMETRIC_INTERNAL);
Kevin Chyn747e29b2019-01-11 17:01:53 -0800178 updateActiveGroup(userId, opPackageName);
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200179 final boolean restricted = isRestricted();
Kevin Chyn8b7a0372018-09-17 15:06:05 -0700180 final AuthenticationClientImpl client = new FaceAuthClient(getContext(),
Kevin Chyna56dff72018-06-19 18:41:12 -0700181 mDaemonWrapper, mHalDeviceId, token, new ServiceListenerImpl(receiver),
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700182 mCurrentUserId, 0 /* groupId */, opId, restricted, opPackageName,
Kevin Chyn87f257a2018-11-27 16:26:07 -0800183 0 /* cookie */, false /* requireConfirmation */);
Kevin Chyna56dff72018-06-19 18:41:12 -0700184 authenticateInternal(client, opId, opPackageName);
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200185 }
186
187 @Override // Binder call
Kevin Chyn87f257a2018-11-27 16:26:07 -0800188 public void prepareForAuthentication(boolean requireConfirmation, IBinder token, long opId,
Kevin Chyn23289ef2018-11-28 16:32:36 -0800189 int groupId, IBiometricServiceReceiverInternal wrapperReceiver,
190 String opPackageName, int cookie, int callingUid, int callingPid,
191 int callingUserId) {
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700192 checkPermission(USE_BIOMETRIC_INTERNAL);
Kevin Chyn41a80902019-02-06 08:12:15 -0800193 updateActiveGroup(groupId, opPackageName);
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700194 final boolean restricted = true; // BiometricPrompt is always restricted
Kevin Chyn8b7a0372018-09-17 15:06:05 -0700195 final AuthenticationClientImpl client = new FaceAuthClient(getContext(),
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700196 mDaemonWrapper, mHalDeviceId, token,
Kevin Chyn87f257a2018-11-27 16:26:07 -0800197 new BiometricPromptServiceListenerImpl(wrapperReceiver),
198 mCurrentUserId, 0 /* groupId */, opId, restricted, opPackageName, cookie,
Kevin Chyn158fefb2019-01-03 18:59:05 -0800199 requireConfirmation);
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700200 authenticateInternal(client, opId, opPackageName, callingUid, callingPid,
201 callingUserId);
202 }
203
204 @Override // Binder call
Kevin Chyn87f257a2018-11-27 16:26:07 -0800205 public void startPreparedClient(int cookie) {
206 checkPermission(MANAGE_BIOMETRIC);
207 startCurrentClient(cookie);
208 }
209
210 @Override // Binder call
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200211 public void cancelAuthentication(final IBinder token, final String opPackageName) {
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700212 checkPermission(USE_BIOMETRIC_INTERNAL);
Kevin Chyna56dff72018-06-19 18:41:12 -0700213 cancelAuthenticationInternal(token, opPackageName);
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200214 }
215
216 @Override // Binder call
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700217 public void cancelAuthenticationFromService(final IBinder token, final String opPackageName,
Kevin Chyne92cdae2018-11-21 16:35:04 -0800218 int callingUid, int callingPid, int callingUserId, boolean fromClient) {
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700219 checkPermission(USE_BIOMETRIC_INTERNAL);
Kevin Chyne92cdae2018-11-21 16:35:04 -0800220 cancelAuthenticationInternal(token, opPackageName, callingUid, callingPid,
221 callingUserId, fromClient);
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700222 }
223
224 @Override // Binder call
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200225 public void setActiveUser(final int userId) {
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700226 checkPermission(MANAGE_BIOMETRIC);
Kevin Chyna56dff72018-06-19 18:41:12 -0700227 setActiveUserInternal(userId);
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200228 }
229
230 @Override // Binder call
Kevin Chyna56dff72018-06-19 18:41:12 -0700231 public void remove(final IBinder token, final int faceId, final int userId,
232 final IFaceServiceReceiver receiver) {
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700233 checkPermission(MANAGE_BIOMETRIC);
Kevin Chyna56dff72018-06-19 18:41:12 -0700234
235 if (token == null) {
236 Slog.w(TAG, "remove(): token is null");
237 return;
238 }
239
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200240 final boolean restricted = isRestricted();
Kevin Chyn6737c572019-02-08 16:10:54 -0800241 final RemovalClient client = new RemovalClient(getContext(), getMetrics(),
242 mDaemonWrapper, mHalDeviceId, token, new ServiceListenerImpl(receiver), faceId,
243 0 /* groupId */, userId, restricted, token.toString(), getBiometricUtils()) {
Kevin Chyn7782d142019-01-18 12:51:33 -0800244 @Override
245 protected int statsModality() {
246 return FaceService.this.statsModality();
247 }
248 };
Kevin Chyna56dff72018-06-19 18:41:12 -0700249 removeInternal(client);
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200250 }
251
Kevin Chyna56dff72018-06-19 18:41:12 -0700252 @Override
253 public void enumerate(final IBinder token, final int userId,
254 final IFaceServiceReceiver receiver) {
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700255 checkPermission(MANAGE_BIOMETRIC);
Kevin Chyna56dff72018-06-19 18:41:12 -0700256
257 final boolean restricted = isRestricted();
Kevin Chyn6737c572019-02-08 16:10:54 -0800258 final EnumerateClient client = new EnumerateClient(getContext(), getMetrics(),
259 mDaemonWrapper, mHalDeviceId, token, new ServiceListenerImpl(receiver), userId,
260 userId, restricted, getContext().getOpPackageName()) {
Kevin Chyn7782d142019-01-18 12:51:33 -0800261 @Override
262 protected int statsModality() {
263 return FaceService.this.statsModality();
264 }
265 };
Kevin Chyna56dff72018-06-19 18:41:12 -0700266 enumerateInternal(client);
267 }
268
269 @Override
270 public void addLockoutResetCallback(final IBiometricServiceLockoutResetCallback callback)
271 throws RemoteException {
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700272 checkPermission(USE_BIOMETRIC_INTERNAL);
Kevin Chyna56dff72018-06-19 18:41:12 -0700273 FaceService.super.addLockoutResetCallback(callback);
274 }
275
276 @Override // Binder call
277 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
278 if (!DumpUtils.checkDumpPermission(getContext(), TAG, pw)) {
279 return;
280 }
281
282 final long ident = Binder.clearCallingIdentity();
283 try {
284 if (args.length > 0 && "--proto".equals(args[0])) {
285 dumpProto(fd);
286 } else {
287 dumpInternal(pw);
288 }
289 } finally {
290 Binder.restoreCallingIdentity(ident);
291 }
292 }
293
294 /**
295 * The following methods don't use any common code from BiometricService
296 */
297
298 // TODO: refactor out common code here
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200299 @Override // Binder call
300 public boolean isHardwareDetected(long deviceId, String opPackageName) {
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700301 checkPermission(USE_BIOMETRIC_INTERNAL);
Kevin Chyna56dff72018-06-19 18:41:12 -0700302 if (!canUseBiometric(opPackageName, false /* foregroundOnly */,
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200303 Binder.getCallingUid(), Binder.getCallingPid(),
304 UserHandle.getCallingUserId())) {
305 return false;
306 }
307
308 final long token = Binder.clearCallingIdentity();
309 try {
310 IBiometricsFace daemon = getFaceDaemon();
311 return daemon != null && mHalDeviceId != 0;
312 } finally {
313 Binder.restoreCallingIdentity(token);
314 }
315 }
316
317 @Override // Binder call
Kevin Chyna56dff72018-06-19 18:41:12 -0700318 public void rename(final int faceId, final String name) {
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700319 checkPermission(MANAGE_BIOMETRIC);
Kevin Chyna56dff72018-06-19 18:41:12 -0700320 if (!isCurrentUserOrProfile(UserHandle.getCallingUserId())) {
321 return;
322 }
323 mHandler.post(new Runnable() {
324 @Override
325 public void run() {
326 getBiometricUtils().renameBiometricForUser(getContext(), mCurrentUserId,
327 faceId, name);
328 }
329 });
330 }
331
332 @Override // Binder call
333 public List<Face> getEnrolledFaces(int userId, String opPackageName) {
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700334 checkPermission(MANAGE_BIOMETRIC);
Kevin Chyna56dff72018-06-19 18:41:12 -0700335 if (!canUseBiometric(opPackageName, false /* foregroundOnly */,
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200336 Binder.getCallingUid(), Binder.getCallingPid(),
337 UserHandle.getCallingUserId())) {
338 return null;
339 }
340
Kevin Chyn6737c572019-02-08 16:10:54 -0800341 return FaceService.this.getEnrolledTemplates(userId);
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200342 }
343
344 @Override // Binder call
Kevin Chyna56dff72018-06-19 18:41:12 -0700345 public boolean hasEnrolledFaces(int userId, String opPackageName) {
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700346 checkPermission(USE_BIOMETRIC_INTERNAL);
Kevin Chyna56dff72018-06-19 18:41:12 -0700347 if (!canUseBiometric(opPackageName, false /* foregroundOnly */,
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200348 Binder.getCallingUid(), Binder.getCallingPid(),
349 UserHandle.getCallingUserId())) {
350 return false;
351 }
352
Kevin Chyna56dff72018-06-19 18:41:12 -0700353 return FaceService.this.hasEnrolledBiometrics(userId);
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200354 }
355
356 @Override // Binder call
357 public long getAuthenticatorId(String opPackageName) {
358 // In this method, we're not checking whether the caller is permitted to use face
359 // API because current authenticator ID is leaked (in a more contrived way) via Android
360 // Keystore (android.security.keystore package): the user of that API can create a key
361 // which requires face authentication for its use, and then query the key's
362 // characteristics (hidden API) which returns, among other things, face
363 // authenticator ID which was active at key creation time.
364 //
365 // Reason: The part of Android Keystore which runs inside an app's process invokes this
366 // method in certain cases. Those cases are not always where the developer demonstrates
367 // explicit intent to use face functionality. Thus, to avoiding throwing an
368 // unexpected SecurityException this method does not check whether its caller is
369 // permitted to use face API.
370 //
371 // The permission check should be restored once Android Keystore no longer invokes this
372 // method from inside app processes.
373
374 return FaceService.this.getAuthenticatorId(opPackageName);
375 }
376
377 @Override // Binder call
Kevin Chyna38653c2019-02-11 17:46:21 -0800378 public void resetLockout(byte[] token) {
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700379 checkPermission(MANAGE_BIOMETRIC);
Kevin Chyna38653c2019-02-11 17:46:21 -0800380 try {
381 mDaemonWrapper.resetLockout(token);
382 } catch (RemoteException e) {
383 Slog.e(getTag(), "Unable to reset lockout", e);
384 }
Kevin Chyna56dff72018-06-19 18:41:12 -0700385 }
Kevin Chynd79e24e2018-09-25 12:06:59 -0700386
387 @Override
Kevin Chynb95f1522019-03-04 16:45:15 -0800388 public boolean setFeature(int feature, boolean enabled, final byte[] token) {
Kevin Chynd79e24e2018-09-25 12:06:59 -0700389 checkPermission(MANAGE_BIOMETRIC);
390
Kevin Chynb95f1522019-03-04 16:45:15 -0800391 if (!FaceService.this.hasEnrolledBiometrics(mCurrentUserId)) {
392 Slog.e(TAG, "No enrolled biometrics while setting feature: " + feature);
393 return false;
394 }
395
Kevin Chynd79e24e2018-09-25 12:06:59 -0700396 final ArrayList<Byte> byteToken = new ArrayList<>();
397 for (int i = 0; i < token.length; i++) {
398 byteToken.add(token[i]);
399 }
400
Kevin Chynb95f1522019-03-04 16:45:15 -0800401 // TODO: Support multiple faces
402 final int faceId = getFirstTemplateForUser(mCurrentUserId);
Kevin Chynd79e24e2018-09-25 12:06:59 -0700403
Kevin Chynb95f1522019-03-04 16:45:15 -0800404 if (mDaemon != null) {
405 try {
406 return mDaemon.setFeature(feature, enabled, byteToken, faceId) == Status.OK;
407 } catch (RemoteException e) {
408 Slog.e(getTag(), "Unable to set feature: " + feature + " to enabled:" + enabled,
409 e);
410 }
411 }
412 return false;
Kevin Chynd79e24e2018-09-25 12:06:59 -0700413 }
414
415 @Override
Kevin Chyn1f16c2d2018-12-07 13:06:08 -0800416 public boolean getFeature(int feature) {
Kevin Chynd79e24e2018-09-25 12:06:59 -0700417 checkPermission(MANAGE_BIOMETRIC);
418
Kevin Chynb95f1522019-03-04 16:45:15 -0800419 // This should ideally return tri-state, but the user isn't shown settings unless
420 // they are enrolled so it's fine for now.
421 if (!FaceService.this.hasEnrolledBiometrics(mCurrentUserId)) {
422 Slog.e(TAG, "No enrolled biometrics while getting feature: " + feature);
423 return false;
Kevin Chynd79e24e2018-09-25 12:06:59 -0700424 }
Kevin Chynb95f1522019-03-04 16:45:15 -0800425
426 // TODO: Support multiple faces
427 final int faceId = getFirstTemplateForUser(mCurrentUserId);
428
429 if (mDaemon != null) {
430 try {
431 OptionalBool result = mDaemon.getFeature(feature, faceId);
432 if (result.status == Status.OK) {
433 return result.value;
434 } else {
435 // Same tri-state comment applies here.
436 return false;
437 }
438 } catch (RemoteException e) {
439 Slog.e(getTag(), "Unable to getRequireAttention", e);
440 }
441 }
442 return false;
Kevin Chynd79e24e2018-09-25 12:06:59 -0700443 }
Kevin Chyn57f119b2018-10-25 12:03:41 -0700444
445 @Override
446 public void userActivity() {
447 checkPermission(MANAGE_BIOMETRIC);
448
449 if (mDaemon != null) {
450 try {
451 mDaemon.userActivity();
452 } catch (RemoteException e) {
453 Slog.e(getTag(), "Unable to send userActivity", e);
454 }
455 }
456 }
Kevin Chynb95f1522019-03-04 16:45:15 -0800457
458 // TODO: Support multiple faces
459 private int getFirstTemplateForUser(int user) {
460 final List<Face> faces = FaceService.this.getEnrolledTemplates(user);
461 if (!faces.isEmpty()) {
462 return faces.get(0).getBiometricId();
463 }
464 return 0;
465 }
Kevin Chyna56dff72018-06-19 18:41:12 -0700466 }
467
468 /**
469 * Receives callbacks from the ClientMonitor implementations. The results are forwarded to
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700470 * BiometricPrompt.
471 */
Kevin Chyne92cdae2018-11-21 16:35:04 -0800472 private class BiometricPromptServiceListenerImpl extends BiometricServiceListener {
Kevin Chyn23289ef2018-11-28 16:32:36 -0800473 BiometricPromptServiceListenerImpl(IBiometricServiceReceiverInternal wrapperReceiver) {
Kevin Chyn87f257a2018-11-27 16:26:07 -0800474 super(wrapperReceiver);
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700475 }
476
477 @Override
478 public void onAcquired(long deviceId, int acquiredInfo, int vendorCode)
479 throws RemoteException {
480 /**
481 * Map the acquired codes onto existing {@link BiometricConstants} acquired codes.
482 */
Kevin Chyne92cdae2018-11-21 16:35:04 -0800483 if (getWrapperReceiver() != null) {
484 getWrapperReceiver().onAcquired(
Kevin Chyn8b7a0372018-09-17 15:06:05 -0700485 FaceManager.getMappedAcquiredInfo(acquiredInfo, vendorCode),
486 FaceManager.getAcquiredString(getContext(), acquiredInfo, vendorCode));
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700487 }
488 }
489
490 @Override
Kevin Chyn87f257a2018-11-27 16:26:07 -0800491 public void onError(long deviceId, int error, int vendorCode, int cookie)
492 throws RemoteException {
Kevin Chyne92cdae2018-11-21 16:35:04 -0800493 if (getWrapperReceiver() != null) {
Kevin Chyn23289ef2018-11-28 16:32:36 -0800494 getWrapperReceiver().onError(cookie, error,
495 FaceManager.getErrorString(getContext(), error, vendorCode));
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700496 }
497 }
498 }
499
500 /**
501 * Receives callbacks from the ClientMonitor implementations. The results are forwarded to
Kevin Chyna56dff72018-06-19 18:41:12 -0700502 * the FaceManager.
503 */
504 private class ServiceListenerImpl implements ServiceListener {
Kevin Chyna56dff72018-06-19 18:41:12 -0700505 private IFaceServiceReceiver mFaceServiceReceiver;
506
507 public ServiceListenerImpl(IFaceServiceReceiver receiver) {
508 mFaceServiceReceiver = receiver;
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200509 }
510
511 @Override
Kevin Chyna56dff72018-06-19 18:41:12 -0700512 public void onEnrollResult(BiometricAuthenticator.Identifier identifier, int remaining)
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200513 throws RemoteException {
Kevin Chyna56dff72018-06-19 18:41:12 -0700514 if (mFaceServiceReceiver != null) {
515 mFaceServiceReceiver.onEnrollResult(identifier.getDeviceId(),
516 identifier.getBiometricId(),
517 remaining);
518 }
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200519 }
Kevin Chyna56dff72018-06-19 18:41:12 -0700520
521 @Override
522 public void onAcquired(long deviceId, int acquiredInfo, int vendorCode)
523 throws RemoteException {
524 if (mFaceServiceReceiver != null) {
525 mFaceServiceReceiver.onAcquired(deviceId, acquiredInfo, vendorCode);
526 }
527 }
528
529 @Override
530 public void onAuthenticationSucceeded(long deviceId,
531 BiometricAuthenticator.Identifier biometric, int userId)
532 throws RemoteException {
533 if (mFaceServiceReceiver != null) {
Kevin Chyn628b7182018-11-13 12:00:48 -0800534 if (biometric == null || biometric instanceof Face) {
Kevin Chyna56dff72018-06-19 18:41:12 -0700535 mFaceServiceReceiver.onAuthenticationSucceeded(deviceId, (Face)biometric);
536 } else {
537 Slog.e(TAG, "onAuthenticationSucceeded received non-face biometric");
538 }
539 }
540 }
541
542 @Override
543 public void onAuthenticationFailed(long deviceId) throws RemoteException {
544 if (mFaceServiceReceiver != null) {
545 mFaceServiceReceiver.onAuthenticationFailed(deviceId);
546 }
547 }
548
549 @Override
Kevin Chyn87f257a2018-11-27 16:26:07 -0800550 public void onError(long deviceId, int error, int vendorCode, int cookie)
551 throws RemoteException {
Kevin Chyna56dff72018-06-19 18:41:12 -0700552 if (mFaceServiceReceiver != null) {
553 mFaceServiceReceiver.onError(deviceId, error, vendorCode);
554 }
555 }
556
557 @Override
558 public void onRemoved(BiometricAuthenticator.Identifier identifier,
559 int remaining) throws RemoteException {
560 if (mFaceServiceReceiver != null) {
561 mFaceServiceReceiver.onRemoved(identifier.getDeviceId(),
562 identifier.getBiometricId(), remaining);
563 }
564 }
565
566 @Override
567 public void onEnumerated(BiometricAuthenticator.Identifier identifier, int remaining)
568 throws RemoteException {
569 if (mFaceServiceReceiver != null) {
Kevin Chyn6737c572019-02-08 16:10:54 -0800570 mFaceServiceReceiver.onEnumerated(identifier.getDeviceId(),
571 identifier.getBiometricId(), remaining);
Kevin Chyna56dff72018-06-19 18:41:12 -0700572 }
573 }
574 }
575
576 private final FaceMetrics mFaceMetrics = new FaceMetrics();
577
578 @GuardedBy("this")
579 private IBiometricsFace mDaemon;
Kevin Chyna38653c2019-02-11 17:46:21 -0800580 // One of the AuthenticationClient constants
581 private int mCurrentUserLockoutMode;
Kevin Chyna56dff72018-06-19 18:41:12 -0700582
583 /**
584 * Receives callbacks from the HAL.
585 */
586 private IBiometricsFaceClientCallback mDaemonCallback =
587 new IBiometricsFaceClientCallback.Stub() {
Kevin Chyn6737c572019-02-08 16:10:54 -0800588 @Override
589 public void onEnrollResult(final long deviceId, int faceId, int userId,
590 int remaining) {
591 mHandler.post(() -> {
592 final Face face = new Face(getBiometricUtils()
593 .getUniqueName(getContext(), userId), faceId, deviceId);
594 FaceService.super.handleEnrollResult(face, remaining);
595 });
596 }
597
598 @Override
599 public void onAcquired(final long deviceId, final int userId,
600 final int acquiredInfo,
601 final int vendorCode) {
602 mHandler.post(() -> {
603 FaceService.super.handleAcquired(deviceId, acquiredInfo, vendorCode);
604 });
605 }
606
607 @Override
608 public void onAuthenticated(final long deviceId, final int faceId, final int userId,
609 ArrayList<Byte> token) {
610 mHandler.post(() -> {
611 Face face = new Face("", faceId, deviceId);
612 FaceService.super.handleAuthenticated(face, token);
613 });
614 }
615
616 @Override
617 public void onError(final long deviceId, final int userId, final int error,
618 final int vendorCode) {
619 mHandler.post(() -> {
620 FaceService.super.handleError(deviceId, error, vendorCode);
621
622 // TODO: this chunk of code should be common to all biometric services
623 if (error == BiometricConstants.BIOMETRIC_ERROR_HW_UNAVAILABLE) {
624 // If we get HW_UNAVAILABLE, try to connect again later...
625 Slog.w(TAG, "Got ERROR_HW_UNAVAILABLE; try reconnecting next client.");
626 synchronized (this) {
627 mDaemon = null;
628 mHalDeviceId = 0;
629 mCurrentUserId = UserHandle.USER_NULL;
630 }
Kevin Chyna56dff72018-06-19 18:41:12 -0700631 }
Kevin Chyn6737c572019-02-08 16:10:54 -0800632 });
633 }
Kevin Chyna56dff72018-06-19 18:41:12 -0700634
Kevin Chyn6737c572019-02-08 16:10:54 -0800635 @Override
636 public void onRemoved(final long deviceId, final int faceId, final int userId,
637 final int remaining) {
638 mHandler.post(() -> {
639 final Face face = new Face("", faceId, deviceId);
640 FaceService.super.handleRemoved(face, remaining);
641 });
642 }
643
644 @Override
645 public void onEnumerate(long deviceId, ArrayList<Integer> faceIds, int userId)
646 throws RemoteException {
647 mHandler.post(() -> {
648 if (!faceIds.isEmpty()) {
649 for (int i = 0; i < faceIds.size(); i++) {
650 final Face face = new Face("", faceIds.get(i), deviceId);
651 // Convert to old old behavior
652 FaceService.super.handleEnumerate(face, faceIds.size() - i - 1);
653 }
654 } else {
655 // For face, the HIDL contract is to receive an empty list when there are no
656 // templates enrolled. Send a null identifier since we don't consume them
657 // anywhere, and send remaining == 0 to plumb this with existing common code.
658 FaceService.super.handleEnumerate(null /* identifier */, 0);
Kevin Chyna56dff72018-06-19 18:41:12 -0700659 }
Kevin Chyn6737c572019-02-08 16:10:54 -0800660 });
661 }
Kevin Chyna56dff72018-06-19 18:41:12 -0700662
Kevin Chyn6737c572019-02-08 16:10:54 -0800663 @Override
664 public void onLockoutChanged(long duration) {
Kevin Chyna38653c2019-02-11 17:46:21 -0800665 Slog.d(TAG, "onLockoutChanged: " + duration);
666 if (duration == 0) {
667 mCurrentUserLockoutMode = AuthenticationClient.LOCKOUT_NONE;
668 } else if (duration == Long.MAX_VALUE) {
669 mCurrentUserLockoutMode = AuthenticationClient.LOCKOUT_PERMANENT;
670 } else {
671 mCurrentUserLockoutMode = AuthenticationClient.LOCKOUT_TIMED;
672 }
Kevin Chyna56dff72018-06-19 18:41:12 -0700673
Kevin Chyna38653c2019-02-11 17:46:21 -0800674 mHandler.post(() -> {
675 if (duration == 0) {
676 notifyLockoutResetMonitors();
677 }
678 });
Kevin Chyn6737c572019-02-08 16:10:54 -0800679 }
680 };
Kevin Chyna56dff72018-06-19 18:41:12 -0700681
682 /**
683 * Wraps the HAL-specific code and is passed to the ClientMonitor implementations so that they
684 * can be shared between the multiple biometric services.
685 */
686 private final DaemonWrapper mDaemonWrapper = new DaemonWrapper() {
687 @Override
688 public int authenticate(long operationId, int groupId) throws RemoteException {
689 IBiometricsFace daemon = getFaceDaemon();
690 if (daemon == null) {
691 Slog.w(TAG, "authenticate(): no face HAL!");
692 return ERROR_ESRCH;
693 }
694 return daemon.authenticate(operationId);
695 }
696
697 @Override
698 public int cancel() throws RemoteException {
699 IBiometricsFace daemon = getFaceDaemon();
700 if (daemon == null) {
701 Slog.w(TAG, "cancel(): no face HAL!");
702 return ERROR_ESRCH;
703 }
704 return daemon.cancel();
705 }
706
707 @Override
708 public int remove(int groupId, int biometricId) throws RemoteException {
709 IBiometricsFace daemon = getFaceDaemon();
710 if (daemon == null) {
711 Slog.w(TAG, "remove(): no face HAL!");
712 return ERROR_ESRCH;
713 }
714 return daemon.remove(biometricId);
715 }
716
717 @Override
718 public int enumerate() throws RemoteException {
719 IBiometricsFace daemon = getFaceDaemon();
720 if (daemon == null) {
721 Slog.w(TAG, "enumerate(): no face HAL!");
722 return ERROR_ESRCH;
723 }
724 return daemon.enumerate();
725 }
726
727 @Override
Kevin Chyn1f16c2d2018-12-07 13:06:08 -0800728 public int enroll(byte[] cryptoToken, int groupId, int timeout,
729 ArrayList<Integer> disabledFeatures) throws RemoteException {
Kevin Chyna56dff72018-06-19 18:41:12 -0700730 IBiometricsFace daemon = getFaceDaemon();
731 if (daemon == null) {
732 Slog.w(TAG, "enroll(): no face HAL!");
733 return ERROR_ESRCH;
734 }
735 final ArrayList<Byte> token = new ArrayList<>();
736 for (int i = 0; i < cryptoToken.length; i++) {
737 token.add(cryptoToken[i]);
738 }
Kevin Chyn1f16c2d2018-12-07 13:06:08 -0800739 return daemon.enroll(token, timeout, disabledFeatures);
Kevin Chyna56dff72018-06-19 18:41:12 -0700740 }
Kevin Chyna38653c2019-02-11 17:46:21 -0800741
742 @Override
743 public void resetLockout(byte[] cryptoToken) throws RemoteException {
744 IBiometricsFace daemon = getFaceDaemon();
745 if (daemon == null) {
746 Slog.w(TAG, "resetLockout(): no face HAL!");
747 return;
748 }
749 final ArrayList<Byte> token = new ArrayList<>();
750 for (int i = 0; i < cryptoToken.length; i++) {
751 token.add(cryptoToken[i]);
752 }
753 daemon.resetLockout(token);
754 }
Kevin Chyna56dff72018-06-19 18:41:12 -0700755 };
756
757
758 public FaceService(Context context) {
759 super(context);
Kevin Chyna56dff72018-06-19 18:41:12 -0700760 }
761
762 @Override
763 public void onStart() {
Kevin Chyn5a2ff5d2018-08-29 19:07:30 -0700764 super.onStart();
Kevin Chyna56dff72018-06-19 18:41:12 -0700765 publishBinderService(Context.FACE_SERVICE, new FaceServiceWrapper());
766 SystemServerInitThreadPool.get().submit(this::getFaceDaemon, TAG + ".onStart");
767 }
768
769 @Override
770 public String getTag() {
771 return TAG;
772 }
773
774 @Override
Kevin Chyn6737c572019-02-08 16:10:54 -0800775 protected DaemonWrapper getDaemonWrapper() {
776 return mDaemonWrapper;
777 }
778
779 @Override
Kevin Chyna56dff72018-06-19 18:41:12 -0700780 protected BiometricUtils getBiometricUtils() {
781 return FaceUtils.getInstance();
782 }
783
784 @Override
Kevin Chyna56dff72018-06-19 18:41:12 -0700785 protected Metrics getMetrics() {
786 return mFaceMetrics;
787 }
788
789 @Override
790 protected boolean hasReachedEnrollmentLimit(int userId) {
791 final int limit = getContext().getResources().getInteger(
Kevin Chyn017e76e2018-06-27 18:35:06 -0700792 com.android.internal.R.integer.config_faceMaxTemplatesPerUser);
Kevin Chyn6737c572019-02-08 16:10:54 -0800793 final int enrolled = FaceService.this.getEnrolledTemplates(userId).size();
Kevin Chyna56dff72018-06-19 18:41:12 -0700794 if (enrolled >= limit) {
795 Slog.w(TAG, "Too many faces registered");
796 return true;
797 }
798 return false;
799 }
800
801 @Override
Kevin Chyn9ba99912019-01-16 16:24:36 -0800802 public void serviceDied(long cookie) {
803 super.serviceDied(cookie);
804 mDaemon = null;
Jim Miller7b78b222019-02-07 16:47:38 -0800805
806 mCurrentUserId = UserHandle.USER_NULL; // Force updateActiveGroup() to re-evaluate
Kevin Chyn9ba99912019-01-16 16:24:36 -0800807 }
808
809 @Override
Kevin Chyna56dff72018-06-19 18:41:12 -0700810 protected void updateActiveGroup(int userId, String clientPackage) {
811 IBiometricsFace daemon = getFaceDaemon();
812
813 if (daemon != null) {
814 try {
815 userId = getUserOrWorkProfileId(clientPackage, userId);
816 if (userId != mCurrentUserId) {
817 final File baseDir = Environment.getDataVendorDeDirectory(userId);
818 final File faceDir = new File(baseDir, FACE_DATA_DIR);
819 if (!faceDir.exists()) {
820 if (!faceDir.mkdir()) {
821 Slog.v(TAG, "Cannot make directory: " + faceDir.getAbsolutePath());
822 return;
823 }
824 // Calling mkdir() from this process will create a directory with our
825 // permissions (inherited from the containing dir). This command fixes
826 // the label.
827 if (!SELinux.restorecon(faceDir)) {
828 Slog.w(TAG, "Restorecons failed. Directory will have wrong label.");
829 return;
830 }
831 }
832
833 daemon.setActiveUser(userId, faceDir.getAbsolutePath());
834 mCurrentUserId = userId;
835 }
836 mAuthenticatorIds.put(userId,
837 hasEnrolledBiometrics(userId) ? daemon.getAuthenticatorId().value : 0L);
838 } catch (RemoteException e) {
839 Slog.e(TAG, "Failed to setActiveUser():", e);
840 }
841 }
842 }
843
844 @Override
845 protected String getLockoutResetIntent() {
846 return ACTION_LOCKOUT_RESET;
847 }
848
849 @Override
850 protected String getLockoutBroadcastPermission() {
851 return RESET_FACE_LOCKOUT;
852 }
853
854 @Override
855 protected long getHalDeviceId() {
856 return mHalDeviceId;
857 }
858
859 @Override
Kevin Chyna38653c2019-02-11 17:46:21 -0800860 protected void handleUserSwitching(int userId) {
861 super.handleUserSwitching(userId);
862 // Will be updated when we get the callback from HAL
863 mCurrentUserLockoutMode = AuthenticationClient.LOCKOUT_NONE;
864 }
865
866 @Override
Kevin Chyna56dff72018-06-19 18:41:12 -0700867 protected boolean hasEnrolledBiometrics(int userId) {
868 if (userId != UserHandle.getCallingUserId()) {
869 checkPermission(INTERACT_ACROSS_USERS);
870 }
871 return getBiometricUtils().getBiometricsForUser(getContext(), userId).size() > 0;
872 }
873
874 @Override
875 protected String getManageBiometricPermission() {
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700876 return MANAGE_BIOMETRIC;
Kevin Chyna56dff72018-06-19 18:41:12 -0700877 }
878
879 @Override
880 protected void checkUseBiometricPermission() {
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700881 // noop for Face. The permission checks are all done on the incoming binder call.
Kevin Chyna56dff72018-06-19 18:41:12 -0700882 }
883
884 @Override
Kevin Chynb3c05aa2018-09-21 16:50:32 -0700885 protected boolean checkAppOps(int uid, String opPackageName) {
886 return mAppOps.noteOp(AppOpsManager.OP_USE_BIOMETRIC, uid, opPackageName)
887 == AppOpsManager.MODE_ALLOWED;
Kevin Chyna56dff72018-06-19 18:41:12 -0700888 }
889
890 @Override
Kevin Chyn6737c572019-02-08 16:10:54 -0800891 protected List<Face> getEnrolledTemplates(int userId) {
892 return getBiometricUtils().getBiometricsForUser(getContext(), userId);
893 }
894
895 @Override
Kevin Chyna56dff72018-06-19 18:41:12 -0700896 protected void notifyClientActiveCallbacks(boolean isActive) {
897 // noop for Face.
898 }
899
Kevin Chyn7782d142019-01-18 12:51:33 -0800900 @Override
901 protected int statsModality() {
902 return BiometricsProtoEnums.MODALITY_FACE;
903 }
904
Kevin Chyna38653c2019-02-11 17:46:21 -0800905 @Override
906 protected int getLockoutMode() {
907 return mCurrentUserLockoutMode;
908 }
909
Kevin Chyna56dff72018-06-19 18:41:12 -0700910 /** Gets the face daemon */
911 private synchronized IBiometricsFace getFaceDaemon() {
912 if (mDaemon == null) {
913 Slog.v(TAG, "mDaemon was null, reconnect to face");
914 try {
915 mDaemon = IBiometricsFace.getService();
916 } catch (java.util.NoSuchElementException e) {
917 // Service doesn't exist or cannot be opened. Logged below.
918 } catch (RemoteException e) {
919 Slog.e(TAG, "Failed to get biometric interface", e);
920 }
921 if (mDaemon == null) {
922 Slog.w(TAG, "face HIDL not available");
923 return null;
924 }
925
926 mDaemon.asBinder().linkToDeath(this, 0);
927
928 try {
929 mHalDeviceId = mDaemon.setCallback(mDaemonCallback).value;
930 } catch (RemoteException e) {
931 Slog.e(TAG, "Failed to open face HAL", e);
932 mDaemon = null; // try again later!
933 }
934
935 if (DEBUG) Slog.v(TAG, "Face HAL id: " + mHalDeviceId);
936 if (mHalDeviceId != 0) {
937 loadAuthenticatorIds();
938 updateActiveGroup(ActivityManager.getCurrentUser(), null);
Kevin Chyn6737c572019-02-08 16:10:54 -0800939 doTemplateCleanupForUser(ActivityManager.getCurrentUser());
Kevin Chyna56dff72018-06-19 18:41:12 -0700940 } else {
941 Slog.w(TAG, "Failed to open Face HAL!");
942 MetricsLogger.count(getContext(), "faced_openhal_error", 1);
943 mDaemon = null;
944 }
945 }
946 return mDaemon;
947 }
948
Kevin Chynd79e24e2018-09-25 12:06:59 -0700949 private long startGenerateChallenge(IBinder token) {
Kevin Chyna56dff72018-06-19 18:41:12 -0700950 IBiometricsFace daemon = getFaceDaemon();
951 if (daemon == null) {
Kevin Chynd79e24e2018-09-25 12:06:59 -0700952 Slog.w(TAG, "startGenerateChallenge: no face HAL!");
Kevin Chyna56dff72018-06-19 18:41:12 -0700953 return 0;
954 }
955 try {
Kevin Chyne46a2162018-09-20 18:43:01 -0700956 return daemon.generateChallenge(CHALLENGE_TIMEOUT_SEC).value;
Kevin Chyna56dff72018-06-19 18:41:12 -0700957 } catch (RemoteException e) {
Kevin Chynd79e24e2018-09-25 12:06:59 -0700958 Slog.e(TAG, "startGenerateChallenge failed", e);
Kevin Chyna56dff72018-06-19 18:41:12 -0700959 }
960 return 0;
961 }
962
Kevin Chynd79e24e2018-09-25 12:06:59 -0700963 private int startRevokeChallenge(IBinder token) {
Kevin Chyna56dff72018-06-19 18:41:12 -0700964 IBiometricsFace daemon = getFaceDaemon();
965 if (daemon == null) {
Kevin Chynd79e24e2018-09-25 12:06:59 -0700966 Slog.w(TAG, "startRevokeChallenge: no face HAL!");
Kevin Chyna56dff72018-06-19 18:41:12 -0700967 return 0;
968 }
969 try {
Kevin Chyn96c92972018-08-31 16:09:31 -0700970 return daemon.revokeChallenge();
Kevin Chyna56dff72018-06-19 18:41:12 -0700971 } catch (RemoteException e) {
Kevin Chynd79e24e2018-09-25 12:06:59 -0700972 Slog.e(TAG, "startRevokeChallenge failed", e);
Kevin Chyna56dff72018-06-19 18:41:12 -0700973 }
974 return 0;
975 }
976
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200977 private void dumpInternal(PrintWriter pw) {
978 JSONObject dump = new JSONObject();
979 try {
980 dump.put("service", "Face Manager");
981
982 JSONArray sets = new JSONArray();
983 for (UserInfo user : UserManager.get(getContext()).getUsers()) {
984 final int userId = user.getUserHandle().getIdentifier();
Kevin Chyna56dff72018-06-19 18:41:12 -0700985 final int N = getBiometricUtils().getBiometricsForUser(getContext(), userId).size();
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200986 PerformanceStats stats = mPerformanceMap.get(userId);
987 PerformanceStats cryptoStats = mCryptoPerformanceMap.get(userId);
988 JSONObject set = new JSONObject();
989 set.put("id", userId);
Kevin Chyna56dff72018-06-19 18:41:12 -0700990 set.put("count", N);
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200991 set.put("accept", (stats != null) ? stats.accept : 0);
992 set.put("reject", (stats != null) ? stats.reject : 0);
993 set.put("acquire", (stats != null) ? stats.acquire : 0);
994 set.put("lockout", (stats != null) ? stats.lockout : 0);
995 set.put("permanentLockout", (stats != null) ? stats.permanentLockout : 0);
996 // cryptoStats measures statistics about secure face transactions
997 // (e.g. to unlock password storage, make secure purchases, etc.)
998 set.put("acceptCrypto", (cryptoStats != null) ? cryptoStats.accept : 0);
999 set.put("rejectCrypto", (cryptoStats != null) ? cryptoStats.reject : 0);
1000 set.put("acquireCrypto", (cryptoStats != null) ? cryptoStats.acquire : 0);
1001 set.put("lockoutCrypto", (cryptoStats != null) ? cryptoStats.lockout : 0);
Kevin Chyna56dff72018-06-19 18:41:12 -07001002 set.put("permanentLockoutCrypto",
1003 (cryptoStats != null) ? cryptoStats.permanentLockout : 0);
Gilad Brettercb51b8b2018-03-22 17:04:51 +02001004 sets.put(set);
1005 }
1006
1007 dump.put("prints", sets);
1008 } catch (JSONException e) {
1009 Slog.e(TAG, "dump formatting failure", e);
1010 }
1011 pw.println(dump);
Kevin Chyn9ba99912019-01-16 16:24:36 -08001012 pw.println("HAL Deaths: " + mHALDeathCount);
1013 mHALDeathCount = 0;
Gilad Brettercb51b8b2018-03-22 17:04:51 +02001014 }
1015
1016 private void dumpProto(FileDescriptor fd) {
1017 final ProtoOutputStream proto = new ProtoOutputStream(fd);
1018 for (UserInfo user : UserManager.get(getContext()).getUsers()) {
1019 final int userId = user.getUserHandle().getIdentifier();
1020
1021 final long userToken = proto.start(FaceServiceDumpProto.USERS);
1022
1023 proto.write(FaceUserStatsProto.USER_ID, userId);
Kevin Chyna56dff72018-06-19 18:41:12 -07001024 proto.write(FaceUserStatsProto.NUM_FACES,
1025 getBiometricUtils().getBiometricsForUser(getContext(), userId).size());
Gilad Brettercb51b8b2018-03-22 17:04:51 +02001026
1027 // Normal face authentications (e.g. lockscreen)
1028 final PerformanceStats normal = mPerformanceMap.get(userId);
1029 if (normal != null) {
1030 final long countsToken = proto.start(FaceUserStatsProto.NORMAL);
1031 proto.write(FaceActionStatsProto.ACCEPT, normal.accept);
1032 proto.write(FaceActionStatsProto.REJECT, normal.reject);
1033 proto.write(FaceActionStatsProto.ACQUIRE, normal.acquire);
1034 proto.write(FaceActionStatsProto.LOCKOUT, normal.lockout);
1035 proto.write(FaceActionStatsProto.LOCKOUT_PERMANENT, normal.lockout);
1036 proto.end(countsToken);
1037 }
1038
1039 // Statistics about secure face transactions (e.g. to unlock password
1040 // storage, make secure purchases, etc.)
1041 final PerformanceStats crypto = mCryptoPerformanceMap.get(userId);
1042 if (crypto != null) {
1043 final long countsToken = proto.start(FaceUserStatsProto.CRYPTO);
1044 proto.write(FaceActionStatsProto.ACCEPT, crypto.accept);
1045 proto.write(FaceActionStatsProto.REJECT, crypto.reject);
1046 proto.write(FaceActionStatsProto.ACQUIRE, crypto.acquire);
1047 proto.write(FaceActionStatsProto.LOCKOUT, crypto.lockout);
1048 proto.write(FaceActionStatsProto.LOCKOUT_PERMANENT, crypto.lockout);
1049 proto.end(countsToken);
1050 }
1051
1052 proto.end(userToken);
1053 }
1054 proto.flush();
Kevin Chyna56dff72018-06-19 18:41:12 -07001055 mPerformanceMap.clear();
1056 mCryptoPerformanceMap.clear();
Gilad Brettercb51b8b2018-03-22 17:04:51 +02001057 }
Kevin Chyn8b7a0372018-09-17 15:06:05 -07001058}