blob: d2d14829d5972c1ec4f9f71bce221d37452e3794 [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 Chynd79e24e2018-09-25 12:06:59 -070035import android.hardware.biometrics.face.V1_0.Status;
Gilad Brettercb51b8b2018-03-22 17:04:51 +020036import android.hardware.face.Face;
Kevin Chyna24e9fd2018-08-27 12:39:17 -070037import android.hardware.face.FaceManager;
Gilad Brettercb51b8b2018-03-22 17:04:51 +020038import android.hardware.face.IFaceService;
Gilad Brettercb51b8b2018-03-22 17:04:51 +020039import android.hardware.face.IFaceServiceReceiver;
40import android.os.Binder;
Gilad Brettercb51b8b2018-03-22 17:04:51 +020041import android.os.Environment;
Gilad Brettercb51b8b2018-03-22 17:04:51 +020042import android.os.IBinder;
Gilad Brettercb51b8b2018-03-22 17:04:51 +020043import android.os.RemoteException;
44import android.os.SELinux;
Gilad Brettercb51b8b2018-03-22 17:04:51 +020045import android.os.UserHandle;
46import android.os.UserManager;
Gilad Brettercb51b8b2018-03-22 17:04:51 +020047import android.util.Slog;
48import android.util.proto.ProtoOutputStream;
49
50import com.android.internal.annotations.GuardedBy;
51import com.android.internal.logging.MetricsLogger;
52import com.android.internal.util.DumpUtils;
53import com.android.server.SystemServerInitThreadPool;
Kevin Chyna38653c2019-02-11 17:46:21 -080054import com.android.server.biometrics.AuthenticationClient;
Kevin Chyn355c6bf2018-09-20 22:14:19 -070055import com.android.server.biometrics.BiometricServiceBase;
Kevin Chyn836f2cf2018-08-27 11:06:39 -070056import com.android.server.biometrics.BiometricUtils;
Kevin Chyn6737c572019-02-08 16:10:54 -080057import com.android.server.biometrics.ClientMonitor;
58import 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 Chyn1f16c2d2018-12-07 13:06:08 -0800388 public int setFeature(int feature, boolean enabled, final byte[] token) {
Kevin Chynd79e24e2018-09-25 12:06:59 -0700389 checkPermission(MANAGE_BIOMETRIC);
390
391 final ArrayList<Byte> byteToken = new ArrayList<>();
392 for (int i = 0; i < token.length; i++) {
393 byteToken.add(token[i]);
394 }
395
396 int result;
397 try {
Kevin Chyn1f16c2d2018-12-07 13:06:08 -0800398 result = mDaemon != null ? mDaemon.setFeature(feature, enabled, byteToken)
Kevin Chynd79e24e2018-09-25 12:06:59 -0700399 : Status.INTERNAL_ERROR;
400 } catch (RemoteException e) {
Kevin Chyn1f16c2d2018-12-07 13:06:08 -0800401 Slog.e(getTag(), "Unable to set feature: " + feature + " to enabled:" + enabled,
402 e);
Kevin Chynd79e24e2018-09-25 12:06:59 -0700403 result = Status.INTERNAL_ERROR;
404 }
405
406 return result;
407 }
408
409 @Override
Kevin Chyn1f16c2d2018-12-07 13:06:08 -0800410 public boolean getFeature(int feature) {
Kevin Chynd79e24e2018-09-25 12:06:59 -0700411 checkPermission(MANAGE_BIOMETRIC);
412
Kevin Chynd79e24e2018-09-25 12:06:59 -0700413 boolean result = true;
414 try {
Kevin Chyn1f16c2d2018-12-07 13:06:08 -0800415 result = mDaemon != null ? mDaemon.getFeature(feature) : true;
Kevin Chynd79e24e2018-09-25 12:06:59 -0700416 } catch (RemoteException e) {
Kevin Chyn57f119b2018-10-25 12:03:41 -0700417 Slog.e(getTag(), "Unable to getRequireAttention", e);
Kevin Chynd79e24e2018-09-25 12:06:59 -0700418 }
419 return result;
420 }
Kevin Chyn57f119b2018-10-25 12:03:41 -0700421
422 @Override
423 public void userActivity() {
424 checkPermission(MANAGE_BIOMETRIC);
425
426 if (mDaemon != null) {
427 try {
428 mDaemon.userActivity();
429 } catch (RemoteException e) {
430 Slog.e(getTag(), "Unable to send userActivity", e);
431 }
432 }
433 }
Kevin Chyna56dff72018-06-19 18:41:12 -0700434 }
435
436 /**
437 * Receives callbacks from the ClientMonitor implementations. The results are forwarded to
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700438 * BiometricPrompt.
439 */
Kevin Chyne92cdae2018-11-21 16:35:04 -0800440 private class BiometricPromptServiceListenerImpl extends BiometricServiceListener {
Kevin Chyn23289ef2018-11-28 16:32:36 -0800441 BiometricPromptServiceListenerImpl(IBiometricServiceReceiverInternal wrapperReceiver) {
Kevin Chyn87f257a2018-11-27 16:26:07 -0800442 super(wrapperReceiver);
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700443 }
444
445 @Override
446 public void onAcquired(long deviceId, int acquiredInfo, int vendorCode)
447 throws RemoteException {
448 /**
449 * Map the acquired codes onto existing {@link BiometricConstants} acquired codes.
450 */
Kevin Chyne92cdae2018-11-21 16:35:04 -0800451 if (getWrapperReceiver() != null) {
452 getWrapperReceiver().onAcquired(
Kevin Chyn8b7a0372018-09-17 15:06:05 -0700453 FaceManager.getMappedAcquiredInfo(acquiredInfo, vendorCode),
454 FaceManager.getAcquiredString(getContext(), acquiredInfo, vendorCode));
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700455 }
456 }
457
458 @Override
Kevin Chyn87f257a2018-11-27 16:26:07 -0800459 public void onError(long deviceId, int error, int vendorCode, int cookie)
460 throws RemoteException {
Kevin Chyne92cdae2018-11-21 16:35:04 -0800461 if (getWrapperReceiver() != null) {
Kevin Chyn23289ef2018-11-28 16:32:36 -0800462 getWrapperReceiver().onError(cookie, error,
463 FaceManager.getErrorString(getContext(), error, vendorCode));
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700464 }
465 }
466 }
467
468 /**
469 * Receives callbacks from the ClientMonitor implementations. The results are forwarded to
Kevin Chyna56dff72018-06-19 18:41:12 -0700470 * the FaceManager.
471 */
472 private class ServiceListenerImpl implements ServiceListener {
Kevin Chyna56dff72018-06-19 18:41:12 -0700473 private IFaceServiceReceiver mFaceServiceReceiver;
474
475 public ServiceListenerImpl(IFaceServiceReceiver receiver) {
476 mFaceServiceReceiver = receiver;
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200477 }
478
479 @Override
Kevin Chyna56dff72018-06-19 18:41:12 -0700480 public void onEnrollResult(BiometricAuthenticator.Identifier identifier, int remaining)
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200481 throws RemoteException {
Kevin Chyna56dff72018-06-19 18:41:12 -0700482 if (mFaceServiceReceiver != null) {
483 mFaceServiceReceiver.onEnrollResult(identifier.getDeviceId(),
484 identifier.getBiometricId(),
485 remaining);
486 }
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200487 }
Kevin Chyna56dff72018-06-19 18:41:12 -0700488
489 @Override
490 public void onAcquired(long deviceId, int acquiredInfo, int vendorCode)
491 throws RemoteException {
492 if (mFaceServiceReceiver != null) {
493 mFaceServiceReceiver.onAcquired(deviceId, acquiredInfo, vendorCode);
494 }
495 }
496
497 @Override
498 public void onAuthenticationSucceeded(long deviceId,
499 BiometricAuthenticator.Identifier biometric, int userId)
500 throws RemoteException {
501 if (mFaceServiceReceiver != null) {
Kevin Chyn628b7182018-11-13 12:00:48 -0800502 if (biometric == null || biometric instanceof Face) {
Kevin Chyna56dff72018-06-19 18:41:12 -0700503 mFaceServiceReceiver.onAuthenticationSucceeded(deviceId, (Face)biometric);
504 } else {
505 Slog.e(TAG, "onAuthenticationSucceeded received non-face biometric");
506 }
507 }
508 }
509
510 @Override
511 public void onAuthenticationFailed(long deviceId) throws RemoteException {
512 if (mFaceServiceReceiver != null) {
513 mFaceServiceReceiver.onAuthenticationFailed(deviceId);
514 }
515 }
516
517 @Override
Kevin Chyn87f257a2018-11-27 16:26:07 -0800518 public void onError(long deviceId, int error, int vendorCode, int cookie)
519 throws RemoteException {
Kevin Chyna56dff72018-06-19 18:41:12 -0700520 if (mFaceServiceReceiver != null) {
521 mFaceServiceReceiver.onError(deviceId, error, vendorCode);
522 }
523 }
524
525 @Override
526 public void onRemoved(BiometricAuthenticator.Identifier identifier,
527 int remaining) throws RemoteException {
528 if (mFaceServiceReceiver != null) {
529 mFaceServiceReceiver.onRemoved(identifier.getDeviceId(),
530 identifier.getBiometricId(), remaining);
531 }
532 }
533
534 @Override
535 public void onEnumerated(BiometricAuthenticator.Identifier identifier, int remaining)
536 throws RemoteException {
537 if (mFaceServiceReceiver != null) {
Kevin Chyn6737c572019-02-08 16:10:54 -0800538 mFaceServiceReceiver.onEnumerated(identifier.getDeviceId(),
539 identifier.getBiometricId(), remaining);
Kevin Chyna56dff72018-06-19 18:41:12 -0700540 }
541 }
542 }
543
544 private final FaceMetrics mFaceMetrics = new FaceMetrics();
545
546 @GuardedBy("this")
547 private IBiometricsFace mDaemon;
Kevin Chyna38653c2019-02-11 17:46:21 -0800548 // One of the AuthenticationClient constants
549 private int mCurrentUserLockoutMode;
Kevin Chyna56dff72018-06-19 18:41:12 -0700550
551 /**
552 * Receives callbacks from the HAL.
553 */
554 private IBiometricsFaceClientCallback mDaemonCallback =
555 new IBiometricsFaceClientCallback.Stub() {
Kevin Chyn6737c572019-02-08 16:10:54 -0800556 @Override
557 public void onEnrollResult(final long deviceId, int faceId, int userId,
558 int remaining) {
559 mHandler.post(() -> {
560 final Face face = new Face(getBiometricUtils()
561 .getUniqueName(getContext(), userId), faceId, deviceId);
562 FaceService.super.handleEnrollResult(face, remaining);
563 });
564 }
565
566 @Override
567 public void onAcquired(final long deviceId, final int userId,
568 final int acquiredInfo,
569 final int vendorCode) {
570 mHandler.post(() -> {
571 FaceService.super.handleAcquired(deviceId, acquiredInfo, vendorCode);
572 });
573 }
574
575 @Override
576 public void onAuthenticated(final long deviceId, final int faceId, final int userId,
577 ArrayList<Byte> token) {
578 mHandler.post(() -> {
579 Face face = new Face("", faceId, deviceId);
580 FaceService.super.handleAuthenticated(face, token);
581 });
582 }
583
584 @Override
585 public void onError(final long deviceId, final int userId, final int error,
586 final int vendorCode) {
587 mHandler.post(() -> {
588 FaceService.super.handleError(deviceId, error, vendorCode);
589
590 // TODO: this chunk of code should be common to all biometric services
591 if (error == BiometricConstants.BIOMETRIC_ERROR_HW_UNAVAILABLE) {
592 // If we get HW_UNAVAILABLE, try to connect again later...
593 Slog.w(TAG, "Got ERROR_HW_UNAVAILABLE; try reconnecting next client.");
594 synchronized (this) {
595 mDaemon = null;
596 mHalDeviceId = 0;
597 mCurrentUserId = UserHandle.USER_NULL;
598 }
Kevin Chyna56dff72018-06-19 18:41:12 -0700599 }
Kevin Chyn6737c572019-02-08 16:10:54 -0800600 });
601 }
Kevin Chyna56dff72018-06-19 18:41:12 -0700602
Kevin Chyn6737c572019-02-08 16:10:54 -0800603 @Override
604 public void onRemoved(final long deviceId, final int faceId, final int userId,
605 final int remaining) {
606 mHandler.post(() -> {
607 final Face face = new Face("", faceId, deviceId);
608 FaceService.super.handleRemoved(face, remaining);
609 });
610 }
611
612 @Override
613 public void onEnumerate(long deviceId, ArrayList<Integer> faceIds, int userId)
614 throws RemoteException {
615 mHandler.post(() -> {
616 if (!faceIds.isEmpty()) {
617 for (int i = 0; i < faceIds.size(); i++) {
618 final Face face = new Face("", faceIds.get(i), deviceId);
619 // Convert to old old behavior
620 FaceService.super.handleEnumerate(face, faceIds.size() - i - 1);
621 }
622 } else {
623 // For face, the HIDL contract is to receive an empty list when there are no
624 // templates enrolled. Send a null identifier since we don't consume them
625 // anywhere, and send remaining == 0 to plumb this with existing common code.
626 FaceService.super.handleEnumerate(null /* identifier */, 0);
Kevin Chyna56dff72018-06-19 18:41:12 -0700627 }
Kevin Chyn6737c572019-02-08 16:10:54 -0800628 });
629 }
Kevin Chyna56dff72018-06-19 18:41:12 -0700630
Kevin Chyn6737c572019-02-08 16:10:54 -0800631 @Override
632 public void onLockoutChanged(long duration) {
Kevin Chyna38653c2019-02-11 17:46:21 -0800633 Slog.d(TAG, "onLockoutChanged: " + duration);
634 if (duration == 0) {
635 mCurrentUserLockoutMode = AuthenticationClient.LOCKOUT_NONE;
636 } else if (duration == Long.MAX_VALUE) {
637 mCurrentUserLockoutMode = AuthenticationClient.LOCKOUT_PERMANENT;
638 } else {
639 mCurrentUserLockoutMode = AuthenticationClient.LOCKOUT_TIMED;
640 }
Kevin Chyna56dff72018-06-19 18:41:12 -0700641
Kevin Chyna38653c2019-02-11 17:46:21 -0800642 mHandler.post(() -> {
643 if (duration == 0) {
644 notifyLockoutResetMonitors();
645 }
646 });
Kevin Chyn6737c572019-02-08 16:10:54 -0800647 }
648 };
Kevin Chyna56dff72018-06-19 18:41:12 -0700649
650 /**
651 * Wraps the HAL-specific code and is passed to the ClientMonitor implementations so that they
652 * can be shared between the multiple biometric services.
653 */
654 private final DaemonWrapper mDaemonWrapper = new DaemonWrapper() {
655 @Override
656 public int authenticate(long operationId, int groupId) throws RemoteException {
657 IBiometricsFace daemon = getFaceDaemon();
658 if (daemon == null) {
659 Slog.w(TAG, "authenticate(): no face HAL!");
660 return ERROR_ESRCH;
661 }
662 return daemon.authenticate(operationId);
663 }
664
665 @Override
666 public int cancel() throws RemoteException {
667 IBiometricsFace daemon = getFaceDaemon();
668 if (daemon == null) {
669 Slog.w(TAG, "cancel(): no face HAL!");
670 return ERROR_ESRCH;
671 }
672 return daemon.cancel();
673 }
674
675 @Override
676 public int remove(int groupId, int biometricId) throws RemoteException {
677 IBiometricsFace daemon = getFaceDaemon();
678 if (daemon == null) {
679 Slog.w(TAG, "remove(): no face HAL!");
680 return ERROR_ESRCH;
681 }
682 return daemon.remove(biometricId);
683 }
684
685 @Override
686 public int enumerate() throws RemoteException {
687 IBiometricsFace daemon = getFaceDaemon();
688 if (daemon == null) {
689 Slog.w(TAG, "enumerate(): no face HAL!");
690 return ERROR_ESRCH;
691 }
692 return daemon.enumerate();
693 }
694
695 @Override
Kevin Chyn1f16c2d2018-12-07 13:06:08 -0800696 public int enroll(byte[] cryptoToken, int groupId, int timeout,
697 ArrayList<Integer> disabledFeatures) throws RemoteException {
Kevin Chyna56dff72018-06-19 18:41:12 -0700698 IBiometricsFace daemon = getFaceDaemon();
699 if (daemon == null) {
700 Slog.w(TAG, "enroll(): no face HAL!");
701 return ERROR_ESRCH;
702 }
703 final ArrayList<Byte> token = new ArrayList<>();
704 for (int i = 0; i < cryptoToken.length; i++) {
705 token.add(cryptoToken[i]);
706 }
Kevin Chyn1f16c2d2018-12-07 13:06:08 -0800707 return daemon.enroll(token, timeout, disabledFeatures);
Kevin Chyna56dff72018-06-19 18:41:12 -0700708 }
Kevin Chyna38653c2019-02-11 17:46:21 -0800709
710 @Override
711 public void resetLockout(byte[] cryptoToken) throws RemoteException {
712 IBiometricsFace daemon = getFaceDaemon();
713 if (daemon == null) {
714 Slog.w(TAG, "resetLockout(): no face HAL!");
715 return;
716 }
717 final ArrayList<Byte> token = new ArrayList<>();
718 for (int i = 0; i < cryptoToken.length; i++) {
719 token.add(cryptoToken[i]);
720 }
721 daemon.resetLockout(token);
722 }
Kevin Chyna56dff72018-06-19 18:41:12 -0700723 };
724
725
726 public FaceService(Context context) {
727 super(context);
Kevin Chyna56dff72018-06-19 18:41:12 -0700728 }
729
730 @Override
731 public void onStart() {
Kevin Chyn5a2ff5d2018-08-29 19:07:30 -0700732 super.onStart();
Kevin Chyna56dff72018-06-19 18:41:12 -0700733 publishBinderService(Context.FACE_SERVICE, new FaceServiceWrapper());
734 SystemServerInitThreadPool.get().submit(this::getFaceDaemon, TAG + ".onStart");
735 }
736
737 @Override
738 public String getTag() {
739 return TAG;
740 }
741
742 @Override
Kevin Chyn6737c572019-02-08 16:10:54 -0800743 protected DaemonWrapper getDaemonWrapper() {
744 return mDaemonWrapper;
745 }
746
747 @Override
Kevin Chyna56dff72018-06-19 18:41:12 -0700748 protected BiometricUtils getBiometricUtils() {
749 return FaceUtils.getInstance();
750 }
751
752 @Override
Kevin Chyna56dff72018-06-19 18:41:12 -0700753 protected Metrics getMetrics() {
754 return mFaceMetrics;
755 }
756
757 @Override
758 protected boolean hasReachedEnrollmentLimit(int userId) {
759 final int limit = getContext().getResources().getInteger(
Kevin Chyn017e76e2018-06-27 18:35:06 -0700760 com.android.internal.R.integer.config_faceMaxTemplatesPerUser);
Kevin Chyn6737c572019-02-08 16:10:54 -0800761 final int enrolled = FaceService.this.getEnrolledTemplates(userId).size();
Kevin Chyna56dff72018-06-19 18:41:12 -0700762 if (enrolled >= limit) {
763 Slog.w(TAG, "Too many faces registered");
764 return true;
765 }
766 return false;
767 }
768
769 @Override
Kevin Chyn9ba99912019-01-16 16:24:36 -0800770 public void serviceDied(long cookie) {
771 super.serviceDied(cookie);
772 mDaemon = null;
Jim Miller7b78b222019-02-07 16:47:38 -0800773
774 mCurrentUserId = UserHandle.USER_NULL; // Force updateActiveGroup() to re-evaluate
Kevin Chyn9ba99912019-01-16 16:24:36 -0800775 }
776
777 @Override
Kevin Chyna56dff72018-06-19 18:41:12 -0700778 protected void updateActiveGroup(int userId, String clientPackage) {
779 IBiometricsFace daemon = getFaceDaemon();
780
781 if (daemon != null) {
782 try {
783 userId = getUserOrWorkProfileId(clientPackage, userId);
784 if (userId != mCurrentUserId) {
785 final File baseDir = Environment.getDataVendorDeDirectory(userId);
786 final File faceDir = new File(baseDir, FACE_DATA_DIR);
787 if (!faceDir.exists()) {
788 if (!faceDir.mkdir()) {
789 Slog.v(TAG, "Cannot make directory: " + faceDir.getAbsolutePath());
790 return;
791 }
792 // Calling mkdir() from this process will create a directory with our
793 // permissions (inherited from the containing dir). This command fixes
794 // the label.
795 if (!SELinux.restorecon(faceDir)) {
796 Slog.w(TAG, "Restorecons failed. Directory will have wrong label.");
797 return;
798 }
799 }
800
801 daemon.setActiveUser(userId, faceDir.getAbsolutePath());
802 mCurrentUserId = userId;
803 }
804 mAuthenticatorIds.put(userId,
805 hasEnrolledBiometrics(userId) ? daemon.getAuthenticatorId().value : 0L);
806 } catch (RemoteException e) {
807 Slog.e(TAG, "Failed to setActiveUser():", e);
808 }
809 }
810 }
811
812 @Override
813 protected String getLockoutResetIntent() {
814 return ACTION_LOCKOUT_RESET;
815 }
816
817 @Override
818 protected String getLockoutBroadcastPermission() {
819 return RESET_FACE_LOCKOUT;
820 }
821
822 @Override
823 protected long getHalDeviceId() {
824 return mHalDeviceId;
825 }
826
827 @Override
Kevin Chyna38653c2019-02-11 17:46:21 -0800828 protected void handleUserSwitching(int userId) {
829 super.handleUserSwitching(userId);
830 // Will be updated when we get the callback from HAL
831 mCurrentUserLockoutMode = AuthenticationClient.LOCKOUT_NONE;
832 }
833
834 @Override
Kevin Chyna56dff72018-06-19 18:41:12 -0700835 protected boolean hasEnrolledBiometrics(int userId) {
836 if (userId != UserHandle.getCallingUserId()) {
837 checkPermission(INTERACT_ACROSS_USERS);
838 }
839 return getBiometricUtils().getBiometricsForUser(getContext(), userId).size() > 0;
840 }
841
842 @Override
843 protected String getManageBiometricPermission() {
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700844 return MANAGE_BIOMETRIC;
Kevin Chyna56dff72018-06-19 18:41:12 -0700845 }
846
847 @Override
848 protected void checkUseBiometricPermission() {
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700849 // noop for Face. The permission checks are all done on the incoming binder call.
Kevin Chyna56dff72018-06-19 18:41:12 -0700850 }
851
852 @Override
Kevin Chynb3c05aa2018-09-21 16:50:32 -0700853 protected boolean checkAppOps(int uid, String opPackageName) {
854 return mAppOps.noteOp(AppOpsManager.OP_USE_BIOMETRIC, uid, opPackageName)
855 == AppOpsManager.MODE_ALLOWED;
Kevin Chyna56dff72018-06-19 18:41:12 -0700856 }
857
858 @Override
Kevin Chyn6737c572019-02-08 16:10:54 -0800859 protected List<Face> getEnrolledTemplates(int userId) {
860 return getBiometricUtils().getBiometricsForUser(getContext(), userId);
861 }
862
863 @Override
Kevin Chyna56dff72018-06-19 18:41:12 -0700864 protected void notifyClientActiveCallbacks(boolean isActive) {
865 // noop for Face.
866 }
867
Kevin Chyn7782d142019-01-18 12:51:33 -0800868 @Override
869 protected int statsModality() {
870 return BiometricsProtoEnums.MODALITY_FACE;
871 }
872
Kevin Chyna38653c2019-02-11 17:46:21 -0800873 @Override
874 protected int getLockoutMode() {
875 return mCurrentUserLockoutMode;
876 }
877
Kevin Chyna56dff72018-06-19 18:41:12 -0700878 /** Gets the face daemon */
879 private synchronized IBiometricsFace getFaceDaemon() {
880 if (mDaemon == null) {
881 Slog.v(TAG, "mDaemon was null, reconnect to face");
882 try {
883 mDaemon = IBiometricsFace.getService();
884 } catch (java.util.NoSuchElementException e) {
885 // Service doesn't exist or cannot be opened. Logged below.
886 } catch (RemoteException e) {
887 Slog.e(TAG, "Failed to get biometric interface", e);
888 }
889 if (mDaemon == null) {
890 Slog.w(TAG, "face HIDL not available");
891 return null;
892 }
893
894 mDaemon.asBinder().linkToDeath(this, 0);
895
896 try {
897 mHalDeviceId = mDaemon.setCallback(mDaemonCallback).value;
898 } catch (RemoteException e) {
899 Slog.e(TAG, "Failed to open face HAL", e);
900 mDaemon = null; // try again later!
901 }
902
903 if (DEBUG) Slog.v(TAG, "Face HAL id: " + mHalDeviceId);
904 if (mHalDeviceId != 0) {
905 loadAuthenticatorIds();
906 updateActiveGroup(ActivityManager.getCurrentUser(), null);
Kevin Chyn6737c572019-02-08 16:10:54 -0800907 doTemplateCleanupForUser(ActivityManager.getCurrentUser());
Kevin Chyna56dff72018-06-19 18:41:12 -0700908 } else {
909 Slog.w(TAG, "Failed to open Face HAL!");
910 MetricsLogger.count(getContext(), "faced_openhal_error", 1);
911 mDaemon = null;
912 }
913 }
914 return mDaemon;
915 }
916
Kevin Chynd79e24e2018-09-25 12:06:59 -0700917 private long startGenerateChallenge(IBinder token) {
Kevin Chyna56dff72018-06-19 18:41:12 -0700918 IBiometricsFace daemon = getFaceDaemon();
919 if (daemon == null) {
Kevin Chynd79e24e2018-09-25 12:06:59 -0700920 Slog.w(TAG, "startGenerateChallenge: no face HAL!");
Kevin Chyna56dff72018-06-19 18:41:12 -0700921 return 0;
922 }
923 try {
Kevin Chyne46a2162018-09-20 18:43:01 -0700924 return daemon.generateChallenge(CHALLENGE_TIMEOUT_SEC).value;
Kevin Chyna56dff72018-06-19 18:41:12 -0700925 } catch (RemoteException e) {
Kevin Chynd79e24e2018-09-25 12:06:59 -0700926 Slog.e(TAG, "startGenerateChallenge failed", e);
Kevin Chyna56dff72018-06-19 18:41:12 -0700927 }
928 return 0;
929 }
930
Kevin Chynd79e24e2018-09-25 12:06:59 -0700931 private int startRevokeChallenge(IBinder token) {
Kevin Chyna56dff72018-06-19 18:41:12 -0700932 IBiometricsFace daemon = getFaceDaemon();
933 if (daemon == null) {
Kevin Chynd79e24e2018-09-25 12:06:59 -0700934 Slog.w(TAG, "startRevokeChallenge: no face HAL!");
Kevin Chyna56dff72018-06-19 18:41:12 -0700935 return 0;
936 }
937 try {
Kevin Chyn96c92972018-08-31 16:09:31 -0700938 return daemon.revokeChallenge();
Kevin Chyna56dff72018-06-19 18:41:12 -0700939 } catch (RemoteException e) {
Kevin Chynd79e24e2018-09-25 12:06:59 -0700940 Slog.e(TAG, "startRevokeChallenge failed", e);
Kevin Chyna56dff72018-06-19 18:41:12 -0700941 }
942 return 0;
943 }
944
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200945 private void dumpInternal(PrintWriter pw) {
946 JSONObject dump = new JSONObject();
947 try {
948 dump.put("service", "Face Manager");
949
950 JSONArray sets = new JSONArray();
951 for (UserInfo user : UserManager.get(getContext()).getUsers()) {
952 final int userId = user.getUserHandle().getIdentifier();
Kevin Chyna56dff72018-06-19 18:41:12 -0700953 final int N = getBiometricUtils().getBiometricsForUser(getContext(), userId).size();
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200954 PerformanceStats stats = mPerformanceMap.get(userId);
955 PerformanceStats cryptoStats = mCryptoPerformanceMap.get(userId);
956 JSONObject set = new JSONObject();
957 set.put("id", userId);
Kevin Chyna56dff72018-06-19 18:41:12 -0700958 set.put("count", N);
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200959 set.put("accept", (stats != null) ? stats.accept : 0);
960 set.put("reject", (stats != null) ? stats.reject : 0);
961 set.put("acquire", (stats != null) ? stats.acquire : 0);
962 set.put("lockout", (stats != null) ? stats.lockout : 0);
963 set.put("permanentLockout", (stats != null) ? stats.permanentLockout : 0);
964 // cryptoStats measures statistics about secure face transactions
965 // (e.g. to unlock password storage, make secure purchases, etc.)
966 set.put("acceptCrypto", (cryptoStats != null) ? cryptoStats.accept : 0);
967 set.put("rejectCrypto", (cryptoStats != null) ? cryptoStats.reject : 0);
968 set.put("acquireCrypto", (cryptoStats != null) ? cryptoStats.acquire : 0);
969 set.put("lockoutCrypto", (cryptoStats != null) ? cryptoStats.lockout : 0);
Kevin Chyna56dff72018-06-19 18:41:12 -0700970 set.put("permanentLockoutCrypto",
971 (cryptoStats != null) ? cryptoStats.permanentLockout : 0);
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200972 sets.put(set);
973 }
974
975 dump.put("prints", sets);
976 } catch (JSONException e) {
977 Slog.e(TAG, "dump formatting failure", e);
978 }
979 pw.println(dump);
Kevin Chyn9ba99912019-01-16 16:24:36 -0800980 pw.println("HAL Deaths: " + mHALDeathCount);
981 mHALDeathCount = 0;
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200982 }
983
984 private void dumpProto(FileDescriptor fd) {
985 final ProtoOutputStream proto = new ProtoOutputStream(fd);
986 for (UserInfo user : UserManager.get(getContext()).getUsers()) {
987 final int userId = user.getUserHandle().getIdentifier();
988
989 final long userToken = proto.start(FaceServiceDumpProto.USERS);
990
991 proto.write(FaceUserStatsProto.USER_ID, userId);
Kevin Chyna56dff72018-06-19 18:41:12 -0700992 proto.write(FaceUserStatsProto.NUM_FACES,
993 getBiometricUtils().getBiometricsForUser(getContext(), userId).size());
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200994
995 // Normal face authentications (e.g. lockscreen)
996 final PerformanceStats normal = mPerformanceMap.get(userId);
997 if (normal != null) {
998 final long countsToken = proto.start(FaceUserStatsProto.NORMAL);
999 proto.write(FaceActionStatsProto.ACCEPT, normal.accept);
1000 proto.write(FaceActionStatsProto.REJECT, normal.reject);
1001 proto.write(FaceActionStatsProto.ACQUIRE, normal.acquire);
1002 proto.write(FaceActionStatsProto.LOCKOUT, normal.lockout);
1003 proto.write(FaceActionStatsProto.LOCKOUT_PERMANENT, normal.lockout);
1004 proto.end(countsToken);
1005 }
1006
1007 // Statistics about secure face transactions (e.g. to unlock password
1008 // storage, make secure purchases, etc.)
1009 final PerformanceStats crypto = mCryptoPerformanceMap.get(userId);
1010 if (crypto != null) {
1011 final long countsToken = proto.start(FaceUserStatsProto.CRYPTO);
1012 proto.write(FaceActionStatsProto.ACCEPT, crypto.accept);
1013 proto.write(FaceActionStatsProto.REJECT, crypto.reject);
1014 proto.write(FaceActionStatsProto.ACQUIRE, crypto.acquire);
1015 proto.write(FaceActionStatsProto.LOCKOUT, crypto.lockout);
1016 proto.write(FaceActionStatsProto.LOCKOUT_PERMANENT, crypto.lockout);
1017 proto.end(countsToken);
1018 }
1019
1020 proto.end(userToken);
1021 }
1022 proto.flush();
Kevin Chyna56dff72018-06-19 18:41:12 -07001023 mPerformanceMap.clear();
1024 mCryptoPerformanceMap.clear();
Gilad Brettercb51b8b2018-03-22 17:04:51 +02001025 }
Kevin Chyn8b7a0372018-09-17 15:06:05 -07001026}