blob: 31c3d4d7b24e31b91f850775eb2c3c71952da4a9 [file] [log] [blame]
Gilad Brettercb51b8b2018-03-22 17:04:51 +02001/*
2 * Copyright (C) 2018 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Kevin Chyn2ffadb32018-06-19 11:29:38 -070017package com.android.server.biometrics.face;
Gilad Brettercb51b8b2018-03-22 17:04:51 +020018
19import static android.Manifest.permission.INTERACT_ACROSS_USERS;
Kevin Chyna24e9fd2018-08-27 12:39:17 -070020import static android.Manifest.permission.MANAGE_BIOMETRIC;
Gilad Brettercb51b8b2018-03-22 17:04:51 +020021import static android.Manifest.permission.RESET_FACE_LOCKOUT;
Kevin Chyna24e9fd2018-08-27 12:39:17 -070022import static android.Manifest.permission.USE_BIOMETRIC_INTERNAL;
Ilya Matyukhin0f9da352019-10-03 14:10:01 -070023import static android.hardware.biometrics.BiometricAuthenticator.TYPE_FACE;
Gilad Brettercb51b8b2018-03-22 17:04:51 +020024
25import android.app.ActivityManager;
Gilad Brettercb51b8b2018-03-22 17:04:51 +020026import android.app.AppOpsManager;
Kevin Chyn56e4c3d2019-04-23 11:28:43 -070027import android.app.Notification;
28import android.app.NotificationChannel;
29import android.app.NotificationManager;
30import android.app.PendingIntent;
Gilad Brettercb51b8b2018-03-22 17:04:51 +020031import android.content.Context;
Kevin Chyn56e4c3d2019-04-23 11:28:43 -070032import android.content.Intent;
Gilad Brettercb51b8b2018-03-22 17:04:51 +020033import android.content.pm.UserInfo;
Kevin Chyna56dff72018-06-19 18:41:12 -070034import android.hardware.biometrics.BiometricAuthenticator;
35import android.hardware.biometrics.BiometricConstants;
Kevin Chyn7782d142019-01-18 12:51:33 -080036import android.hardware.biometrics.BiometricsProtoEnums;
Ilya Matyukhinef410e32020-02-04 13:39:48 -080037import android.hardware.biometrics.IBiometricNativeHandle;
Kevin Chyna56dff72018-06-19 18:41:12 -070038import android.hardware.biometrics.IBiometricServiceLockoutResetCallback;
Kevin Chyn23289ef2018-11-28 16:32:36 -080039import android.hardware.biometrics.IBiometricServiceReceiverInternal;
Gilad Brettercb51b8b2018-03-22 17:04:51 +020040import android.hardware.biometrics.face.V1_0.IBiometricsFace;
41import android.hardware.biometrics.face.V1_0.IBiometricsFaceClientCallback;
Kevin Chynb95f1522019-03-04 16:45:15 -080042import android.hardware.biometrics.face.V1_0.OptionalBool;
Kevin Chynd79e24e2018-09-25 12:06:59 -070043import android.hardware.biometrics.face.V1_0.Status;
Gilad Brettercb51b8b2018-03-22 17:04:51 +020044import android.hardware.face.Face;
Kevin Chyna24e9fd2018-08-27 12:39:17 -070045import android.hardware.face.FaceManager;
Gilad Brettercb51b8b2018-03-22 17:04:51 +020046import android.hardware.face.IFaceService;
Gilad Brettercb51b8b2018-03-22 17:04:51 +020047import android.hardware.face.IFaceServiceReceiver;
48import android.os.Binder;
Joe Onoratobf955d22019-03-25 00:16:58 -070049import android.os.Build;
Gilad Brettercb51b8b2018-03-22 17:04:51 +020050import android.os.Environment;
Gilad Brettercb51b8b2018-03-22 17:04:51 +020051import android.os.IBinder;
Joe Onoratodb396002019-04-05 19:49:27 -070052import android.os.NativeHandle;
Gilad Brettercb51b8b2018-03-22 17:04:51 +020053import android.os.RemoteException;
54import android.os.SELinux;
Joe Onorato108413a2019-04-03 18:20:52 -070055import android.os.SystemProperties;
Gilad Brettercb51b8b2018-03-22 17:04:51 +020056import android.os.UserHandle;
57import android.os.UserManager;
joshmccloskey4ceaf6b2019-08-21 11:26:08 -070058import android.provider.Settings;
Gilad Brettercb51b8b2018-03-22 17:04:51 +020059import android.util.Slog;
Gilad Brettercb51b8b2018-03-22 17:04:51 +020060
Kevin Chyn56e4c3d2019-04-23 11:28:43 -070061import com.android.internal.R;
Gilad Brettercb51b8b2018-03-22 17:04:51 +020062import com.android.internal.annotations.GuardedBy;
63import com.android.internal.logging.MetricsLogger;
64import com.android.internal.util.DumpUtils;
65import com.android.server.SystemServerInitThreadPool;
Kevin Chyna38653c2019-02-11 17:46:21 -080066import com.android.server.biometrics.AuthenticationClient;
Kevin Chyn355c6bf2018-09-20 22:14:19 -070067import com.android.server.biometrics.BiometricServiceBase;
Kevin Chyn836f2cf2018-08-27 11:06:39 -070068import com.android.server.biometrics.BiometricUtils;
Ilya Matyukhin9dac8482019-07-09 11:03:07 -070069import com.android.server.biometrics.ClientMonitor;
Kevin Chyn4cc49f72019-04-24 13:53:35 -070070import com.android.server.biometrics.Constants;
Kevin Chyn0ce70852019-05-10 10:29:18 -070071import com.android.server.biometrics.EnumerateClient;
Kevin Chyn6737c572019-02-08 16:10:54 -080072import com.android.server.biometrics.RemovalClient;
Gilad Brettercb51b8b2018-03-22 17:04:51 +020073
74import org.json.JSONArray;
75import org.json.JSONException;
76import org.json.JSONObject;
77
78import java.io.File;
79import java.io.FileDescriptor;
Joe Onoratodb396002019-04-05 19:49:27 -070080import java.io.FileOutputStream;
81import java.io.IOException;
Gilad Brettercb51b8b2018-03-22 17:04:51 +020082import java.io.PrintWriter;
83import java.util.ArrayList;
Joe Onoratodb396002019-04-05 19:49:27 -070084import java.util.Arrays;
Kevin Chyn9bc1d492019-06-21 15:31:50 -070085import java.util.HashMap;
Gilad Brettercb51b8b2018-03-22 17:04:51 +020086import java.util.List;
Kevin Chyn9bc1d492019-06-21 15:31:50 -070087import java.util.Map;
Gilad Brettercb51b8b2018-03-22 17:04:51 +020088
89/**
90 * A service to manage multiple clients that want to access the face HAL API.
91 * The service is responsible for maintaining a list of clients and dispatching all
Kevin Chyn51676d22018-11-05 18:00:43 -080092 * face-related events.
Gilad Brettercb51b8b2018-03-22 17:04:51 +020093 *
94 * @hide
95 */
Kevin Chyn355c6bf2018-09-20 22:14:19 -070096public class FaceService extends BiometricServiceBase {
Kevin Chyna56dff72018-06-19 18:41:12 -070097
98 protected static final String TAG = "FaceService";
99 private static final boolean DEBUG = true;
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200100 private static final String FACE_DATA_DIR = "facedata";
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200101 private static final String ACTION_LOCKOUT_RESET =
Kevin Chyn2ffadb32018-06-19 11:29:38 -0700102 "com.android.server.biometrics.face.ACTION_LOCKOUT_RESET";
Kevin Chyne46a2162018-09-20 18:43:01 -0700103 private static final int CHALLENGE_TIMEOUT_SEC = 600; // 10 minutes
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200104
Kevin Chyndbfbed42019-06-13 17:01:30 -0700105 private static final String NOTIFICATION_TAG = "FaceService";
106 private static final int NOTIFICATION_ID = 1;
107
joshmccloskey4ceaf6b2019-08-21 11:26:08 -0700108 private static final String SKIP_KEYGUARD_ACQUIRE_IGNORE_LIST =
109 "com.android.server.biometrics.face.skip_keyguard_acquire_ignore_list";
110
Kevin Chyn9bc1d492019-06-21 15:31:50 -0700111 /**
112 * Events for bugreports.
113 */
114 public static final class AuthenticationEvent {
115 private long mStartTime;
116 private long mLatency;
117 // Only valid if mError is 0
118 private boolean mAuthenticated;
119 private int mError;
120 // Only valid if mError is ERROR_VENDOR
121 private int mVendorError;
joshmccloskey0d75a882020-01-21 14:59:46 -0800122 private int mUser;
Kevin Chyn9bc1d492019-06-21 15:31:50 -0700123
124 AuthenticationEvent(long startTime, long latency, boolean authenticated, int error,
joshmccloskey0d75a882020-01-21 14:59:46 -0800125 int vendorError, int user) {
Kevin Chyn9bc1d492019-06-21 15:31:50 -0700126 mStartTime = startTime;
127 mLatency = latency;
128 mAuthenticated = authenticated;
129 mError = error;
130 mVendorError = vendorError;
joshmccloskey0d75a882020-01-21 14:59:46 -0800131 mUser = user;
Kevin Chyn9bc1d492019-06-21 15:31:50 -0700132 }
133
134 public String toString(Context context) {
135 return "Start: " + mStartTime
136 + "\tLatency: " + mLatency
137 + "\tAuthenticated: " + mAuthenticated
138 + "\tError: " + mError
139 + "\tVendorCode: " + mVendorError
joshmccloskey0d75a882020-01-21 14:59:46 -0800140 + "\tUser: " + mUser
Kevin Chyn9bc1d492019-06-21 15:31:50 -0700141 + "\t" + FaceManager.getErrorString(context, mError, mVendorError);
142 }
143 }
144
145 /**
146 * Keep a short historical buffer of stats, with an aggregated usage time.
147 */
148 private class UsageStats {
149 static final int EVENT_LOG_SIZE = 100;
150
151 Context mContext;
152 List<AuthenticationEvent> mAuthenticationEvents;
153
154 int acceptCount;
155 int rejectCount;
156 Map<Integer, Integer> mErrorCount;
157
158 long acceptLatency;
159 long rejectLatency;
160 Map<Integer, Long> mErrorLatency;
161
162 UsageStats(Context context) {
163 mAuthenticationEvents = new ArrayList<>();
164 mErrorCount = new HashMap<>();
165 mErrorLatency = new HashMap<>();
166 mContext = context;
167 }
168
169 void addEvent(AuthenticationEvent event) {
170 if (mAuthenticationEvents.size() >= EVENT_LOG_SIZE) {
171 mAuthenticationEvents.remove(0);
172 }
173 mAuthenticationEvents.add(event);
174
175 if (event.mAuthenticated) {
176 acceptCount++;
177 acceptLatency += event.mLatency;
178 } else if (event.mError == 0) {
179 rejectCount++;
180 rejectLatency += event.mLatency;
181 } else {
182 mErrorCount.put(event.mError, mErrorCount.getOrDefault(event.mError, 0) + 1);
183 mErrorLatency.put(event.mError,
184 mErrorLatency.getOrDefault(event.mError, 0l) + event.mLatency);
185 }
186 }
187
188 void print(PrintWriter pw) {
189 pw.println("Events since last reboot: " + mAuthenticationEvents.size());
190 for (int i = 0; i < mAuthenticationEvents.size(); i++) {
191 pw.println(mAuthenticationEvents.get(i).toString(mContext));
192 }
193
194 // Dump aggregated usage stats
195 // TODO: Remove or combine with json dump in a future release
196 pw.println("Accept\tCount: " + acceptCount + "\tLatency: " + acceptLatency
197 + "\tAverage: " + (acceptCount > 0 ? acceptLatency / acceptCount : 0));
198 pw.println("Reject\tCount: " + rejectCount + "\tLatency: " + rejectLatency
199 + "\tAverage: " + (rejectCount > 0 ? rejectLatency / rejectCount : 0));
200
201 for (Integer key : mErrorCount.keySet()) {
202 final int count = mErrorCount.get(key);
203 pw.println("Error" + key + "\tCount: " + count
204 + "\tLatency: " + mErrorLatency.getOrDefault(key, 0l)
205 + "\tAverage: " + (count > 0 ? mErrorLatency.getOrDefault(key, 0l) / count
206 : 0)
207 + "\t" + FaceManager.getErrorString(mContext, key, 0 /* vendorCode */));
208 }
209 }
210 }
211
Kevin Chyn8b7a0372018-09-17 15:06:05 -0700212 private final class FaceAuthClient extends AuthenticationClientImpl {
Kevin Chyn0ce70852019-05-10 10:29:18 -0700213 private int mLastAcquire;
214
Kevin Chyn8b7a0372018-09-17 15:06:05 -0700215 public FaceAuthClient(Context context,
216 DaemonWrapper daemon, long halDeviceId, IBinder token,
217 ServiceListener listener, int targetUserId, int groupId, long opId,
Ilya Matyukhinef410e32020-02-04 13:39:48 -0800218 boolean restricted, String owner, int cookie, boolean requireConfirmation,
219 IBiometricNativeHandle windowId) {
Kevin Chyn8b7a0372018-09-17 15:06:05 -0700220 super(context, daemon, halDeviceId, token, listener, targetUserId, groupId, opId,
Ilya Matyukhinef410e32020-02-04 13:39:48 -0800221 restricted, owner, cookie, requireConfirmation, windowId);
Kevin Chyn8b7a0372018-09-17 15:06:05 -0700222 }
Kevin Chyn7782d142019-01-18 12:51:33 -0800223
224 @Override
225 protected int statsModality() {
226 return FaceService.this.statsModality();
227 }
Kevin Chyna38653c2019-02-11 17:46:21 -0800228
229 @Override
230 public boolean shouldFrameworkHandleLockout() {
231 return false;
232 }
Kevin Chyn56d6b072019-02-13 18:39:01 -0800233
234 @Override
Kevin Chyn0ce70852019-05-10 10:29:18 -0700235 public boolean wasUserDetected() {
Ilya Matyukhinc90507e2019-08-20 15:38:57 -0700236 return mLastAcquire != FaceManager.FACE_ACQUIRED_NOT_DETECTED
237 && mLastAcquire != FaceManager.FACE_ACQUIRED_SENSOR_DIRTY;
Kevin Chyn0ce70852019-05-10 10:29:18 -0700238 }
239
240 @Override
Kevin Chyn56d6b072019-02-13 18:39:01 -0800241 public boolean onAuthenticated(BiometricAuthenticator.Identifier identifier,
242 boolean authenticated, ArrayList<Byte> token) {
243 final boolean result = super.onAuthenticated(identifier, authenticated, token);
244
Kevin Chyn9bc1d492019-06-21 15:31:50 -0700245 mUsageStats.addEvent(new AuthenticationEvent(
246 getStartTimeMs(),
247 System.currentTimeMillis() - getStartTimeMs() /* latency */,
248 authenticated,
249 0 /* error */,
joshmccloskey0d75a882020-01-21 14:59:46 -0800250 0 /* vendorError */,
251 getTargetUserId()));
Kevin Chyn9bc1d492019-06-21 15:31:50 -0700252
Kevin Chyn56d6b072019-02-13 18:39:01 -0800253 // For face, the authentication lifecycle ends either when
254 // 1) Authenticated == true
255 // 2) Error occurred
256 // 3) Authenticated == false
257 // Fingerprint currently does not end when the third condition is met which is a bug,
258 // but let's leave it as-is for now.
259 return result || !authenticated;
260 }
Kevin Chyn56e4c3d2019-04-23 11:28:43 -0700261
262 @Override
Kevin Chyn9bc1d492019-06-21 15:31:50 -0700263 public boolean onError(long deviceId, int error, int vendorCode) {
264 mUsageStats.addEvent(new AuthenticationEvent(
265 getStartTimeMs(),
266 System.currentTimeMillis() - getStartTimeMs() /* latency */,
267 false /* authenticated */,
268 error,
joshmccloskey0d75a882020-01-21 14:59:46 -0800269 vendorCode,
270 getTargetUserId()));
Kevin Chyn9bc1d492019-06-21 15:31:50 -0700271
272 return super.onError(deviceId, error, vendorCode);
273 }
274
275 @Override
Kevin Chyn4cc49f72019-04-24 13:53:35 -0700276 public int[] getAcquireIgnorelist() {
277 if (isBiometricPrompt()) {
278 return mBiometricPromptIgnoreList;
279 } else {
280 // Keyguard
281 return mKeyguardIgnoreList;
282 }
283 }
284
285 @Override
286 public int[] getAcquireVendorIgnorelist() {
287 if (isBiometricPrompt()) {
288 return mBiometricPromptIgnoreListVendor;
289 } else {
290 // Keyguard
291 return mKeyguardIgnoreListVendor;
292 }
293 }
294
295 @Override
Kevin Chyn56e4c3d2019-04-23 11:28:43 -0700296 public boolean onAcquired(int acquireInfo, int vendorCode) {
297
Kevin Chyn0ce70852019-05-10 10:29:18 -0700298 mLastAcquire = acquireInfo;
299
Kevin Chyn56e4c3d2019-04-23 11:28:43 -0700300 if (acquireInfo == FaceManager.FACE_ACQUIRED_RECALIBRATE) {
301 final String name =
302 getContext().getString(R.string.face_recalibrate_notification_name);
303 final String title =
304 getContext().getString(R.string.face_recalibrate_notification_title);
305 final String content =
306 getContext().getString(R.string.face_recalibrate_notification_content);
307
308 final Intent intent = new Intent("android.settings.FACE_SETTINGS");
309 intent.setPackage("com.android.settings");
310
311 final PendingIntent pendingIntent = PendingIntent.getActivityAsUser(getContext(),
312 0 /* requestCode */, intent, 0 /* flags */, null /* options */,
313 UserHandle.CURRENT);
314
Kevin Chyndbfbed42019-06-13 17:01:30 -0700315 final String channelName = "FaceEnrollNotificationChannel";
Kevin Chyn56e4c3d2019-04-23 11:28:43 -0700316
Kevin Chyndbfbed42019-06-13 17:01:30 -0700317 NotificationChannel channel = new NotificationChannel(channelName, name,
Kevin Chyn56e4c3d2019-04-23 11:28:43 -0700318 NotificationManager.IMPORTANCE_HIGH);
Kevin Chyndbfbed42019-06-13 17:01:30 -0700319 Notification notification = new Notification.Builder(getContext(), channelName)
Kevin Chyn56e4c3d2019-04-23 11:28:43 -0700320 .setSmallIcon(R.drawable.ic_lock)
321 .setContentTitle(title)
322 .setContentText(content)
323 .setSubText(name)
324 .setOnlyAlertOnce(true)
325 .setLocalOnly(true)
326 .setAutoCancel(true)
327 .setCategory(Notification.CATEGORY_SYSTEM)
328 .setContentIntent(pendingIntent)
Kevin Chyn0c3a9982019-06-11 19:09:27 -0700329 .setVisibility(Notification.VISIBILITY_SECRET)
Kevin Chyn56e4c3d2019-04-23 11:28:43 -0700330 .build();
331
Kevin Chyndbfbed42019-06-13 17:01:30 -0700332 mNotificationManager.createNotificationChannel(channel);
333 mNotificationManager.notifyAsUser(NOTIFICATION_TAG, NOTIFICATION_ID, notification,
334 UserHandle.CURRENT);
Kevin Chyn56e4c3d2019-04-23 11:28:43 -0700335 }
336
337 return super.onAcquired(acquireInfo, vendorCode);
338 }
Kevin Chyn8b7a0372018-09-17 15:06:05 -0700339 }
340
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200341 /**
Kevin Chyna56dff72018-06-19 18:41:12 -0700342 * Receives the incoming binder calls from FaceManager.
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200343 */
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200344 private final class FaceServiceWrapper extends IFaceService.Stub {
Curtis Belmonte45788542019-07-23 11:07:27 -0700345 private static final int ENROLL_TIMEOUT_SEC = 75;
Kevin Chyna56dff72018-06-19 18:41:12 -0700346
347 /**
348 * The following methods contain common code which is shared in biometrics/common.
349 */
Kevin Chyna38653c2019-02-11 17:46:21 -0800350
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200351 @Override // Binder call
Kevin Chynd79e24e2018-09-25 12:06:59 -0700352 public long generateChallenge(IBinder token) {
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700353 checkPermission(MANAGE_BIOMETRIC);
Kevin Chynd79e24e2018-09-25 12:06:59 -0700354 return startGenerateChallenge(token);
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200355 }
356
357 @Override // Binder call
Kevin Chynd79e24e2018-09-25 12:06:59 -0700358 public int revokeChallenge(IBinder token) {
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700359 checkPermission(MANAGE_BIOMETRIC);
Kevin Chyn7a207e42019-07-27 17:50:33 -0700360 mHandler.post(() -> {
361 // TODO(b/137106905): Schedule binder calls in FaceService to avoid deadlocks.
362 if (getCurrentClient() == null) {
363 // if we aren't handling any other HIDL calls (mCurrentClient == null), revoke
364 // the challenge right away.
365 startRevokeChallenge(token);
366 } else {
367 // postpone revoking the challenge until we finish processing the current HIDL
368 // call.
369 mRevokeChallengePending = true;
370 }
371 });
372 return Status.OK;
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200373 }
374
375 @Override // Binder call
Kevin Chyn593e6262019-06-28 13:24:44 -0700376 public void enroll(int userId, final IBinder token, final byte[] cryptoToken,
Kevin Chyn1f16c2d2018-12-07 13:06:08 -0800377 final IFaceServiceReceiver receiver, final String opPackageName,
Ilya Matyukhinef410e32020-02-04 13:39:48 -0800378 final int[] disabledFeatures, IBiometricNativeHandle windowId) {
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700379 checkPermission(MANAGE_BIOMETRIC);
Kevin Chyn593e6262019-06-28 13:24:44 -0700380 updateActiveGroup(userId, opPackageName);
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200381
Kevin Chyndbfbed42019-06-13 17:01:30 -0700382 mNotificationManager.cancelAsUser(NOTIFICATION_TAG, NOTIFICATION_ID,
383 UserHandle.CURRENT);
384
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200385 final boolean restricted = isRestricted();
Kevin Chyna56dff72018-06-19 18:41:12 -0700386 final EnrollClientImpl client = new EnrollClientImpl(getContext(), mDaemonWrapper,
387 mHalDeviceId, token, new ServiceListenerImpl(receiver), mCurrentUserId,
Curtis Belmonte45788542019-07-23 11:07:27 -0700388 0 /* groupId */, cryptoToken, restricted, opPackageName, disabledFeatures,
Ilya Matyukhinef410e32020-02-04 13:39:48 -0800389 ENROLL_TIMEOUT_SEC, windowId) {
Kevin Chyn4cc49f72019-04-24 13:53:35 -0700390
391 @Override
392 public int[] getAcquireIgnorelist() {
393 return mEnrollIgnoreList;
394 }
395
396 @Override
397 public int[] getAcquireVendorIgnorelist() {
398 return mEnrollIgnoreListVendor;
399 }
400
Kevin Chyn1429a312019-01-28 16:08:09 -0800401 @Override
402 public boolean shouldVibrate() {
403 return false;
404 }
Kevin Chyn7782d142019-01-18 12:51:33 -0800405
406 @Override
407 protected int statsModality() {
408 return FaceService.this.statsModality();
409 }
Kevin Chyn1429a312019-01-28 16:08:09 -0800410 };
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200411
Kevin Chyn1a878c12019-04-04 15:50:11 -0700412 enrollInternal(client, mCurrentUserId);
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200413 }
414
415 @Override // Binder call
Ilya Matyukhinef410e32020-02-04 13:39:48 -0800416 public void enrollRemotely(int userId, final IBinder token, final byte[] cryptoToken,
417 final IFaceServiceReceiver receiver, final String opPackageName,
418 final int[] disabledFeatures) {
419 checkPermission(MANAGE_BIOMETRIC);
420 // TODO(b/145027036): Implement this.
421 }
422
423 @Override // Binder call
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200424 public void cancelEnrollment(final IBinder token) {
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700425 checkPermission(MANAGE_BIOMETRIC);
Kevin Chyna56dff72018-06-19 18:41:12 -0700426 cancelEnrollmentInternal(token);
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200427 }
428
429 @Override // Binder call
Kevin Chyn747e29b2019-01-11 17:01:53 -0800430 public void authenticate(final IBinder token, final long opId, int userId,
Kevin Chyna56dff72018-06-19 18:41:12 -0700431 final IFaceServiceReceiver receiver, final int flags,
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700432 final String opPackageName) {
433 checkPermission(USE_BIOMETRIC_INTERNAL);
Kevin Chyn747e29b2019-01-11 17:01:53 -0800434 updateActiveGroup(userId, opPackageName);
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200435 final boolean restricted = isRestricted();
Kevin Chyn8b7a0372018-09-17 15:06:05 -0700436 final AuthenticationClientImpl client = new FaceAuthClient(getContext(),
Kevin Chyna56dff72018-06-19 18:41:12 -0700437 mDaemonWrapper, mHalDeviceId, token, new ServiceListenerImpl(receiver),
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700438 mCurrentUserId, 0 /* groupId */, opId, restricted, opPackageName,
Ilya Matyukhinef410e32020-02-04 13:39:48 -0800439 0 /* cookie */, false /* requireConfirmation */, null /* windowId */);
Kevin Chyna56dff72018-06-19 18:41:12 -0700440 authenticateInternal(client, opId, opPackageName);
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200441 }
442
443 @Override // Binder call
Kevin Chyn87f257a2018-11-27 16:26:07 -0800444 public void prepareForAuthentication(boolean requireConfirmation, IBinder token, long opId,
Kevin Chyn23289ef2018-11-28 16:32:36 -0800445 int groupId, IBiometricServiceReceiverInternal wrapperReceiver,
446 String opPackageName, int cookie, int callingUid, int callingPid,
447 int callingUserId) {
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700448 checkPermission(USE_BIOMETRIC_INTERNAL);
Kevin Chyn41a80902019-02-06 08:12:15 -0800449 updateActiveGroup(groupId, opPackageName);
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700450 final boolean restricted = true; // BiometricPrompt is always restricted
Kevin Chyn8b7a0372018-09-17 15:06:05 -0700451 final AuthenticationClientImpl client = new FaceAuthClient(getContext(),
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700452 mDaemonWrapper, mHalDeviceId, token,
Kevin Chyn87f257a2018-11-27 16:26:07 -0800453 new BiometricPromptServiceListenerImpl(wrapperReceiver),
454 mCurrentUserId, 0 /* groupId */, opId, restricted, opPackageName, cookie,
Ilya Matyukhinef410e32020-02-04 13:39:48 -0800455 requireConfirmation, null /* windowId */);
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700456 authenticateInternal(client, opId, opPackageName, callingUid, callingPid,
457 callingUserId);
458 }
459
460 @Override // Binder call
Kevin Chyn87f257a2018-11-27 16:26:07 -0800461 public void startPreparedClient(int cookie) {
462 checkPermission(MANAGE_BIOMETRIC);
463 startCurrentClient(cookie);
464 }
465
466 @Override // Binder call
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200467 public void cancelAuthentication(final IBinder token, final String opPackageName) {
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700468 checkPermission(USE_BIOMETRIC_INTERNAL);
Kevin Chyna56dff72018-06-19 18:41:12 -0700469 cancelAuthenticationInternal(token, opPackageName);
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200470 }
471
472 @Override // Binder call
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700473 public void cancelAuthenticationFromService(final IBinder token, final String opPackageName,
Kevin Chyne92cdae2018-11-21 16:35:04 -0800474 int callingUid, int callingPid, int callingUserId, boolean fromClient) {
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700475 checkPermission(USE_BIOMETRIC_INTERNAL);
Kevin Chyne92cdae2018-11-21 16:35:04 -0800476 cancelAuthenticationInternal(token, opPackageName, callingUid, callingPid,
477 callingUserId, fromClient);
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700478 }
479
480 @Override // Binder call
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200481 public void setActiveUser(final int userId) {
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700482 checkPermission(MANAGE_BIOMETRIC);
Kevin Chyna56dff72018-06-19 18:41:12 -0700483 setActiveUserInternal(userId);
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200484 }
485
486 @Override // Binder call
Kevin Chyna56dff72018-06-19 18:41:12 -0700487 public void remove(final IBinder token, final int faceId, final int userId,
Kevin Chyn593e6262019-06-28 13:24:44 -0700488 final IFaceServiceReceiver receiver, final String opPackageName) {
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700489 checkPermission(MANAGE_BIOMETRIC);
Kevin Chyn593e6262019-06-28 13:24:44 -0700490 updateActiveGroup(userId, opPackageName);
Kevin Chyna56dff72018-06-19 18:41:12 -0700491
492 if (token == null) {
493 Slog.w(TAG, "remove(): token is null");
494 return;
495 }
496
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200497 final boolean restricted = isRestricted();
Kevin Chyn4cc49f72019-04-24 13:53:35 -0700498 final RemovalClient client = new RemovalClient(getContext(), getConstants(),
Kevin Chyn6737c572019-02-08 16:10:54 -0800499 mDaemonWrapper, mHalDeviceId, token, new ServiceListenerImpl(receiver), faceId,
500 0 /* groupId */, userId, restricted, token.toString(), getBiometricUtils()) {
Kevin Chyn7782d142019-01-18 12:51:33 -0800501 @Override
502 protected int statsModality() {
503 return FaceService.this.statsModality();
504 }
505 };
Kevin Chyna56dff72018-06-19 18:41:12 -0700506 removeInternal(client);
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200507 }
508
Kevin Chyna56dff72018-06-19 18:41:12 -0700509 @Override
510 public void enumerate(final IBinder token, final int userId,
511 final IFaceServiceReceiver receiver) {
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700512 checkPermission(MANAGE_BIOMETRIC);
Kevin Chyna56dff72018-06-19 18:41:12 -0700513
514 final boolean restricted = isRestricted();
Kevin Chyn4cc49f72019-04-24 13:53:35 -0700515 final EnumerateClient client = new EnumerateClient(getContext(), getConstants(),
Kevin Chyn6737c572019-02-08 16:10:54 -0800516 mDaemonWrapper, mHalDeviceId, token, new ServiceListenerImpl(receiver), userId,
517 userId, restricted, getContext().getOpPackageName()) {
Kevin Chyn7782d142019-01-18 12:51:33 -0800518 @Override
519 protected int statsModality() {
520 return FaceService.this.statsModality();
521 }
522 };
Kevin Chyna56dff72018-06-19 18:41:12 -0700523 enumerateInternal(client);
524 }
525
526 @Override
527 public void addLockoutResetCallback(final IBiometricServiceLockoutResetCallback callback)
528 throws RemoteException {
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700529 checkPermission(USE_BIOMETRIC_INTERNAL);
Kevin Chyna56dff72018-06-19 18:41:12 -0700530 FaceService.super.addLockoutResetCallback(callback);
531 }
532
533 @Override // Binder call
534 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
535 if (!DumpUtils.checkDumpPermission(getContext(), TAG, pw)) {
536 return;
537 }
538
539 final long ident = Binder.clearCallingIdentity();
540 try {
Joe Onoratodb396002019-04-05 19:49:27 -0700541 if (args.length > 1 && "--hal".equals(args[0])) {
542 dumpHal(fd, Arrays.copyOfRange(args, 1, args.length, args.getClass()));
Kevin Chyna56dff72018-06-19 18:41:12 -0700543 } else {
544 dumpInternal(pw);
545 }
546 } finally {
547 Binder.restoreCallingIdentity(ident);
548 }
549 }
550
551 /**
552 * The following methods don't use any common code from BiometricService
553 */
554
555 // TODO: refactor out common code here
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200556 @Override // Binder call
Ilya Matyukhinf2da1a12019-09-25 15:31:03 -0700557 public boolean isHardwareDetected(String opPackageName) {
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700558 checkPermission(USE_BIOMETRIC_INTERNAL);
Kevin Chyna56dff72018-06-19 18:41:12 -0700559 if (!canUseBiometric(opPackageName, false /* foregroundOnly */,
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200560 Binder.getCallingUid(), Binder.getCallingPid(),
561 UserHandle.getCallingUserId())) {
562 return false;
563 }
564
565 final long token = Binder.clearCallingIdentity();
566 try {
567 IBiometricsFace daemon = getFaceDaemon();
568 return daemon != null && mHalDeviceId != 0;
569 } finally {
570 Binder.restoreCallingIdentity(token);
571 }
572 }
573
574 @Override // Binder call
Kevin Chyna56dff72018-06-19 18:41:12 -0700575 public void rename(final int faceId, final String name) {
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700576 checkPermission(MANAGE_BIOMETRIC);
Kevin Chyna56dff72018-06-19 18:41:12 -0700577 if (!isCurrentUserOrProfile(UserHandle.getCallingUserId())) {
578 return;
579 }
580 mHandler.post(new Runnable() {
581 @Override
582 public void run() {
583 getBiometricUtils().renameBiometricForUser(getContext(), mCurrentUserId,
584 faceId, name);
585 }
586 });
587 }
588
589 @Override // Binder call
590 public List<Face> getEnrolledFaces(int userId, String opPackageName) {
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700591 checkPermission(MANAGE_BIOMETRIC);
Kevin Chyna56dff72018-06-19 18:41:12 -0700592 if (!canUseBiometric(opPackageName, false /* foregroundOnly */,
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200593 Binder.getCallingUid(), Binder.getCallingPid(),
594 UserHandle.getCallingUserId())) {
595 return null;
596 }
597
Kevin Chyn6737c572019-02-08 16:10:54 -0800598 return FaceService.this.getEnrolledTemplates(userId);
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200599 }
600
601 @Override // Binder call
Kevin Chyna56dff72018-06-19 18:41:12 -0700602 public boolean hasEnrolledFaces(int userId, String opPackageName) {
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700603 checkPermission(USE_BIOMETRIC_INTERNAL);
Kevin Chyna56dff72018-06-19 18:41:12 -0700604 if (!canUseBiometric(opPackageName, false /* foregroundOnly */,
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200605 Binder.getCallingUid(), Binder.getCallingPid(),
606 UserHandle.getCallingUserId())) {
607 return false;
608 }
609
Kevin Chyna56dff72018-06-19 18:41:12 -0700610 return FaceService.this.hasEnrolledBiometrics(userId);
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200611 }
612
613 @Override // Binder call
614 public long getAuthenticatorId(String opPackageName) {
615 // In this method, we're not checking whether the caller is permitted to use face
616 // API because current authenticator ID is leaked (in a more contrived way) via Android
617 // Keystore (android.security.keystore package): the user of that API can create a key
618 // which requires face authentication for its use, and then query the key's
619 // characteristics (hidden API) which returns, among other things, face
620 // authenticator ID which was active at key creation time.
621 //
622 // Reason: The part of Android Keystore which runs inside an app's process invokes this
623 // method in certain cases. Those cases are not always where the developer demonstrates
624 // explicit intent to use face functionality. Thus, to avoiding throwing an
625 // unexpected SecurityException this method does not check whether its caller is
626 // permitted to use face API.
627 //
628 // The permission check should be restored once Android Keystore no longer invokes this
629 // method from inside app processes.
630
631 return FaceService.this.getAuthenticatorId(opPackageName);
632 }
633
634 @Override // Binder call
Kevin Chyna38653c2019-02-11 17:46:21 -0800635 public void resetLockout(byte[] token) {
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700636 checkPermission(MANAGE_BIOMETRIC);
Kevin Chyn1d6a2862019-04-02 16:20:21 -0700637
joshmccloskeya4772062019-07-24 17:13:16 -0700638 mHandler.post(() -> {
639 if (!FaceService.this.hasEnrolledBiometrics(mCurrentUserId)) {
640 Slog.w(TAG, "Ignoring lockout reset, no templates enrolled");
641 return;
642 }
Kevin Chyn1d6a2862019-04-02 16:20:21 -0700643
joshmccloskeya4772062019-07-24 17:13:16 -0700644 Slog.d(TAG, "Resetting lockout for user: " + mCurrentUserId);
Kevin Chynbe67ce02019-06-10 16:14:22 -0700645
joshmccloskeya4772062019-07-24 17:13:16 -0700646 try {
647 mDaemonWrapper.resetLockout(token);
648 } catch (RemoteException e) {
649 Slog.e(getTag(), "Unable to reset lockout", e);
650 }
651 });
Kevin Chyna56dff72018-06-19 18:41:12 -0700652 }
Kevin Chynd79e24e2018-09-25 12:06:59 -0700653
654 @Override
Kevin Chyn593e6262019-06-28 13:24:44 -0700655 public void setFeature(int userId, int feature, boolean enabled, final byte[] token,
656 IFaceServiceReceiver receiver, final String opPackageName) {
Kevin Chynd79e24e2018-09-25 12:06:59 -0700657 checkPermission(MANAGE_BIOMETRIC);
658
Kevin Chyne62749a2019-04-02 19:33:56 -0700659 mHandler.post(() -> {
joshmccloskeya4772062019-07-24 17:13:16 -0700660 if (DEBUG) {
661 Slog.d(TAG, "setFeature for user(" + userId + ")");
662 }
663 updateActiveGroup(userId, opPackageName);
Kevin Chyne62749a2019-04-02 19:33:56 -0700664 if (!FaceService.this.hasEnrolledBiometrics(mCurrentUserId)) {
665 Slog.e(TAG, "No enrolled biometrics while setting feature: " + feature);
666 return;
Kevin Chynb95f1522019-03-04 16:45:15 -0800667 }
Kevin Chyne62749a2019-04-02 19:33:56 -0700668
669 final ArrayList<Byte> byteToken = new ArrayList<>();
670 for (int i = 0; i < token.length; i++) {
671 byteToken.add(token[i]);
672 }
673
674 // TODO: Support multiple faces
675 final int faceId = getFirstTemplateForUser(mCurrentUserId);
676
677 if (mDaemon != null) {
678 try {
679 final int result = mDaemon.setFeature(feature, enabled, byteToken, faceId);
680 receiver.onFeatureSet(result == Status.OK, feature);
681 } catch (RemoteException e) {
682 Slog.e(getTag(), "Unable to set feature: " + feature
683 + " to enabled:" + enabled, e);
684 }
685 }
686 });
687
Kevin Chynd79e24e2018-09-25 12:06:59 -0700688 }
689
690 @Override
Kevin Chyn593e6262019-06-28 13:24:44 -0700691 public void getFeature(int userId, int feature, IFaceServiceReceiver receiver,
692 final String opPackageName) {
Kevin Chynd79e24e2018-09-25 12:06:59 -0700693 checkPermission(MANAGE_BIOMETRIC);
694
Kevin Chyne62749a2019-04-02 19:33:56 -0700695 mHandler.post(() -> {
joshmccloskeya4772062019-07-24 17:13:16 -0700696 if (DEBUG) {
697 Slog.d(TAG, "getFeature for user(" + userId + ")");
698 }
699 updateActiveGroup(userId, opPackageName);
Kevin Chyne62749a2019-04-02 19:33:56 -0700700 // This should ideally return tri-state, but the user isn't shown settings unless
701 // they are enrolled so it's fine for now.
702 if (!FaceService.this.hasEnrolledBiometrics(mCurrentUserId)) {
703 Slog.e(TAG, "No enrolled biometrics while getting feature: " + feature);
704 return;
Kevin Chynb95f1522019-03-04 16:45:15 -0800705 }
Kevin Chyne62749a2019-04-02 19:33:56 -0700706
707 // TODO: Support multiple faces
708 final int faceId = getFirstTemplateForUser(mCurrentUserId);
709
710 if (mDaemon != null) {
711 try {
712 OptionalBool result = mDaemon.getFeature(feature, faceId);
713 receiver.onFeatureGet(result.status == Status.OK, feature, result.value);
714 } catch (RemoteException e) {
715 Slog.e(getTag(), "Unable to getRequireAttention", e);
716 }
717 }
718 });
719
Kevin Chynd79e24e2018-09-25 12:06:59 -0700720 }
Kevin Chyn57f119b2018-10-25 12:03:41 -0700721
722 @Override
723 public void userActivity() {
724 checkPermission(MANAGE_BIOMETRIC);
725
726 if (mDaemon != null) {
727 try {
728 mDaemon.userActivity();
729 } catch (RemoteException e) {
730 Slog.e(getTag(), "Unable to send userActivity", e);
731 }
732 }
733 }
Kevin Chynb95f1522019-03-04 16:45:15 -0800734
735 // TODO: Support multiple faces
736 private int getFirstTemplateForUser(int user) {
737 final List<Face> faces = FaceService.this.getEnrolledTemplates(user);
738 if (!faces.isEmpty()) {
739 return faces.get(0).getBiometricId();
740 }
741 return 0;
742 }
Kevin Chyna56dff72018-06-19 18:41:12 -0700743 }
744
745 /**
746 * Receives callbacks from the ClientMonitor implementations. The results are forwarded to
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700747 * BiometricPrompt.
748 */
Kevin Chyne92cdae2018-11-21 16:35:04 -0800749 private class BiometricPromptServiceListenerImpl extends BiometricServiceListener {
Kevin Chyn23289ef2018-11-28 16:32:36 -0800750 BiometricPromptServiceListenerImpl(IBiometricServiceReceiverInternal wrapperReceiver) {
Kevin Chyn87f257a2018-11-27 16:26:07 -0800751 super(wrapperReceiver);
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700752 }
753
754 @Override
755 public void onAcquired(long deviceId, int acquiredInfo, int vendorCode)
756 throws RemoteException {
757 /**
758 * Map the acquired codes onto existing {@link BiometricConstants} acquired codes.
759 */
Kevin Chyne92cdae2018-11-21 16:35:04 -0800760 if (getWrapperReceiver() != null) {
761 getWrapperReceiver().onAcquired(
Kevin Chyn8b7a0372018-09-17 15:06:05 -0700762 FaceManager.getMappedAcquiredInfo(acquiredInfo, vendorCode),
763 FaceManager.getAcquiredString(getContext(), acquiredInfo, vendorCode));
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700764 }
765 }
766
767 @Override
Kevin Chyn87f257a2018-11-27 16:26:07 -0800768 public void onError(long deviceId, int error, int vendorCode, int cookie)
769 throws RemoteException {
Kevin Chyne92cdae2018-11-21 16:35:04 -0800770 if (getWrapperReceiver() != null) {
Ilya Matyukhin0f9da352019-10-03 14:10:01 -0700771 getWrapperReceiver().onError(cookie, TYPE_FACE, error, vendorCode);
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700772 }
773 }
774 }
775
776 /**
777 * Receives callbacks from the ClientMonitor implementations. The results are forwarded to
Kevin Chyna56dff72018-06-19 18:41:12 -0700778 * the FaceManager.
779 */
780 private class ServiceListenerImpl implements ServiceListener {
Kevin Chyna56dff72018-06-19 18:41:12 -0700781 private IFaceServiceReceiver mFaceServiceReceiver;
782
783 public ServiceListenerImpl(IFaceServiceReceiver receiver) {
784 mFaceServiceReceiver = receiver;
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200785 }
786
787 @Override
Kevin Chyna56dff72018-06-19 18:41:12 -0700788 public void onEnrollResult(BiometricAuthenticator.Identifier identifier, int remaining)
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200789 throws RemoteException {
Kevin Chyna56dff72018-06-19 18:41:12 -0700790 if (mFaceServiceReceiver != null) {
791 mFaceServiceReceiver.onEnrollResult(identifier.getDeviceId(),
792 identifier.getBiometricId(),
793 remaining);
794 }
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200795 }
Kevin Chyna56dff72018-06-19 18:41:12 -0700796
797 @Override
798 public void onAcquired(long deviceId, int acquiredInfo, int vendorCode)
799 throws RemoteException {
800 if (mFaceServiceReceiver != null) {
801 mFaceServiceReceiver.onAcquired(deviceId, acquiredInfo, vendorCode);
802 }
803 }
804
805 @Override
806 public void onAuthenticationSucceeded(long deviceId,
807 BiometricAuthenticator.Identifier biometric, int userId)
808 throws RemoteException {
809 if (mFaceServiceReceiver != null) {
Kevin Chyn628b7182018-11-13 12:00:48 -0800810 if (biometric == null || biometric instanceof Face) {
Kevin Chyn8d2694a2019-04-11 18:30:40 -0700811 mFaceServiceReceiver.onAuthenticationSucceeded(deviceId, (Face) biometric,
812 userId);
Kevin Chyna56dff72018-06-19 18:41:12 -0700813 } else {
814 Slog.e(TAG, "onAuthenticationSucceeded received non-face biometric");
815 }
816 }
817 }
818
819 @Override
820 public void onAuthenticationFailed(long deviceId) throws RemoteException {
821 if (mFaceServiceReceiver != null) {
822 mFaceServiceReceiver.onAuthenticationFailed(deviceId);
823 }
824 }
825
826 @Override
Kevin Chyn87f257a2018-11-27 16:26:07 -0800827 public void onError(long deviceId, int error, int vendorCode, int cookie)
828 throws RemoteException {
Kevin Chyna56dff72018-06-19 18:41:12 -0700829 if (mFaceServiceReceiver != null) {
830 mFaceServiceReceiver.onError(deviceId, error, vendorCode);
831 }
832 }
833
834 @Override
835 public void onRemoved(BiometricAuthenticator.Identifier identifier,
836 int remaining) throws RemoteException {
837 if (mFaceServiceReceiver != null) {
838 mFaceServiceReceiver.onRemoved(identifier.getDeviceId(),
839 identifier.getBiometricId(), remaining);
840 }
841 }
842
843 @Override
844 public void onEnumerated(BiometricAuthenticator.Identifier identifier, int remaining)
845 throws RemoteException {
846 if (mFaceServiceReceiver != null) {
Kevin Chyn6737c572019-02-08 16:10:54 -0800847 mFaceServiceReceiver.onEnumerated(identifier.getDeviceId(),
848 identifier.getBiometricId(), remaining);
Kevin Chyna56dff72018-06-19 18:41:12 -0700849 }
850 }
851 }
852
Kevin Chyn4cc49f72019-04-24 13:53:35 -0700853 private final FaceConstants mFaceConstants = new FaceConstants();
Kevin Chyna56dff72018-06-19 18:41:12 -0700854
855 @GuardedBy("this")
856 private IBiometricsFace mDaemon;
Kevin Chyn9bc1d492019-06-21 15:31:50 -0700857 private UsageStats mUsageStats;
Ilya Matyukhin9dac8482019-07-09 11:03:07 -0700858 private boolean mRevokeChallengePending = false;
Kevin Chyna38653c2019-02-11 17:46:21 -0800859 // One of the AuthenticationClient constants
860 private int mCurrentUserLockoutMode;
Kevin Chyna56dff72018-06-19 18:41:12 -0700861
Kevin Chyndbfbed42019-06-13 17:01:30 -0700862 private NotificationManager mNotificationManager;
863
Kevin Chyn4cc49f72019-04-24 13:53:35 -0700864 private int[] mBiometricPromptIgnoreList;
865 private int[] mBiometricPromptIgnoreListVendor;
866 private int[] mKeyguardIgnoreList;
867 private int[] mKeyguardIgnoreListVendor;
868 private int[] mEnrollIgnoreList;
869 private int[] mEnrollIgnoreListVendor;
870
Kevin Chyna56dff72018-06-19 18:41:12 -0700871 /**
872 * Receives callbacks from the HAL.
873 */
874 private IBiometricsFaceClientCallback mDaemonCallback =
875 new IBiometricsFaceClientCallback.Stub() {
Kevin Chyn6737c572019-02-08 16:10:54 -0800876 @Override
877 public void onEnrollResult(final long deviceId, int faceId, int userId,
878 int remaining) {
879 mHandler.post(() -> {
880 final Face face = new Face(getBiometricUtils()
881 .getUniqueName(getContext(), userId), faceId, deviceId);
882 FaceService.super.handleEnrollResult(face, remaining);
Kevin Chyn576811e2019-06-12 14:50:35 -0700883
884 // Enrollment changes the authenticatorId, so update it here.
885 IBiometricsFace daemon = getFaceDaemon();
886 if (remaining == 0 && daemon != null) {
887 try {
888 mAuthenticatorIds.put(userId,
889 hasEnrolledBiometrics(userId) ? daemon.getAuthenticatorId().value
890 : 0L);
891 } catch (RemoteException e) {
892 Slog.e(TAG, "Unable to get authenticatorId", e);
893 }
894 }
Kevin Chyn6737c572019-02-08 16:10:54 -0800895 });
896 }
897
898 @Override
899 public void onAcquired(final long deviceId, final int userId,
900 final int acquiredInfo,
901 final int vendorCode) {
902 mHandler.post(() -> {
903 FaceService.super.handleAcquired(deviceId, acquiredInfo, vendorCode);
904 });
905 }
906
907 @Override
908 public void onAuthenticated(final long deviceId, final int faceId, final int userId,
909 ArrayList<Byte> token) {
910 mHandler.post(() -> {
911 Face face = new Face("", faceId, deviceId);
912 FaceService.super.handleAuthenticated(face, token);
913 });
914 }
915
916 @Override
917 public void onError(final long deviceId, final int userId, final int error,
918 final int vendorCode) {
919 mHandler.post(() -> {
920 FaceService.super.handleError(deviceId, error, vendorCode);
921
922 // TODO: this chunk of code should be common to all biometric services
923 if (error == BiometricConstants.BIOMETRIC_ERROR_HW_UNAVAILABLE) {
924 // If we get HW_UNAVAILABLE, try to connect again later...
925 Slog.w(TAG, "Got ERROR_HW_UNAVAILABLE; try reconnecting next client.");
926 synchronized (this) {
927 mDaemon = null;
928 mHalDeviceId = 0;
929 mCurrentUserId = UserHandle.USER_NULL;
930 }
Kevin Chyna56dff72018-06-19 18:41:12 -0700931 }
Kevin Chyn6737c572019-02-08 16:10:54 -0800932 });
933 }
Kevin Chyna56dff72018-06-19 18:41:12 -0700934
Kevin Chyn6737c572019-02-08 16:10:54 -0800935 @Override
Kevin Chyn31ae8d22019-03-11 14:12:32 -0700936 public void onRemoved(final long deviceId, ArrayList<Integer> faceIds, final int userId) {
Kevin Chyn6737c572019-02-08 16:10:54 -0800937 mHandler.post(() -> {
Kevin Chyn31ae8d22019-03-11 14:12:32 -0700938 if (!faceIds.isEmpty()) {
939 for (int i = 0; i < faceIds.size(); i++) {
940 final Face face = new Face("", faceIds.get(i), deviceId);
941 // Convert to old behavior
942 FaceService.super.handleRemoved(face, faceIds.size() - i - 1);
943 }
944 } else {
945 final Face face = new Face("", 0 /* identifier */, deviceId);
946 FaceService.super.handleRemoved(face, 0 /* remaining */);
947 }
Curtis Belmonte8cfe3712019-09-26 16:02:41 -0700948 Settings.Secure.putIntForUser(getContext().getContentResolver(),
949 Settings.Secure.FACE_UNLOCK_RE_ENROLL, 0, UserHandle.USER_CURRENT);
Kevin Chyn6737c572019-02-08 16:10:54 -0800950 });
951 }
952
953 @Override
954 public void onEnumerate(long deviceId, ArrayList<Integer> faceIds, int userId)
955 throws RemoteException {
956 mHandler.post(() -> {
957 if (!faceIds.isEmpty()) {
958 for (int i = 0; i < faceIds.size(); i++) {
959 final Face face = new Face("", faceIds.get(i), deviceId);
960 // Convert to old old behavior
961 FaceService.super.handleEnumerate(face, faceIds.size() - i - 1);
962 }
963 } else {
964 // For face, the HIDL contract is to receive an empty list when there are no
965 // templates enrolled. Send a null identifier since we don't consume them
966 // anywhere, and send remaining == 0 to plumb this with existing common code.
967 FaceService.super.handleEnumerate(null /* identifier */, 0);
Kevin Chyna56dff72018-06-19 18:41:12 -0700968 }
Kevin Chyn6737c572019-02-08 16:10:54 -0800969 });
970 }
Kevin Chyna56dff72018-06-19 18:41:12 -0700971
Kevin Chyn6737c572019-02-08 16:10:54 -0800972 @Override
973 public void onLockoutChanged(long duration) {
Kevin Chyna38653c2019-02-11 17:46:21 -0800974 Slog.d(TAG, "onLockoutChanged: " + duration);
Curtis Belmontec47df862020-01-28 16:58:34 -0800975
Kevin Chyna38653c2019-02-11 17:46:21 -0800976 if (duration == 0) {
977 mCurrentUserLockoutMode = AuthenticationClient.LOCKOUT_NONE;
Curtis Belmontec47df862020-01-28 16:58:34 -0800978 } else if (duration == -1 || duration == Long.MAX_VALUE) {
Kevin Chyna38653c2019-02-11 17:46:21 -0800979 mCurrentUserLockoutMode = AuthenticationClient.LOCKOUT_PERMANENT;
980 } else {
981 mCurrentUserLockoutMode = AuthenticationClient.LOCKOUT_TIMED;
982 }
Kevin Chyna56dff72018-06-19 18:41:12 -0700983
Kevin Chyna38653c2019-02-11 17:46:21 -0800984 mHandler.post(() -> {
985 if (duration == 0) {
986 notifyLockoutResetMonitors();
987 }
988 });
Kevin Chyn6737c572019-02-08 16:10:54 -0800989 }
990 };
Kevin Chyna56dff72018-06-19 18:41:12 -0700991
992 /**
993 * Wraps the HAL-specific code and is passed to the ClientMonitor implementations so that they
994 * can be shared between the multiple biometric services.
995 */
996 private final DaemonWrapper mDaemonWrapper = new DaemonWrapper() {
997 @Override
Ilya Matyukhinef410e32020-02-04 13:39:48 -0800998 public int authenticate(long operationId, int groupId, NativeHandle windowId)
999 throws RemoteException {
Kevin Chyna56dff72018-06-19 18:41:12 -07001000 IBiometricsFace daemon = getFaceDaemon();
1001 if (daemon == null) {
1002 Slog.w(TAG, "authenticate(): no face HAL!");
1003 return ERROR_ESRCH;
1004 }
1005 return daemon.authenticate(operationId);
1006 }
1007
1008 @Override
1009 public int cancel() throws RemoteException {
1010 IBiometricsFace daemon = getFaceDaemon();
1011 if (daemon == null) {
1012 Slog.w(TAG, "cancel(): no face HAL!");
1013 return ERROR_ESRCH;
1014 }
1015 return daemon.cancel();
1016 }
1017
1018 @Override
1019 public int remove(int groupId, int biometricId) throws RemoteException {
1020 IBiometricsFace daemon = getFaceDaemon();
1021 if (daemon == null) {
1022 Slog.w(TAG, "remove(): no face HAL!");
1023 return ERROR_ESRCH;
1024 }
1025 return daemon.remove(biometricId);
1026 }
1027
1028 @Override
1029 public int enumerate() throws RemoteException {
1030 IBiometricsFace daemon = getFaceDaemon();
1031 if (daemon == null) {
1032 Slog.w(TAG, "enumerate(): no face HAL!");
1033 return ERROR_ESRCH;
1034 }
1035 return daemon.enumerate();
1036 }
1037
1038 @Override
Kevin Chyn1f16c2d2018-12-07 13:06:08 -08001039 public int enroll(byte[] cryptoToken, int groupId, int timeout,
Ilya Matyukhinef410e32020-02-04 13:39:48 -08001040 ArrayList<Integer> disabledFeatures, NativeHandle windowId) throws RemoteException {
Kevin Chyna56dff72018-06-19 18:41:12 -07001041 IBiometricsFace daemon = getFaceDaemon();
1042 if (daemon == null) {
1043 Slog.w(TAG, "enroll(): no face HAL!");
1044 return ERROR_ESRCH;
1045 }
1046 final ArrayList<Byte> token = new ArrayList<>();
1047 for (int i = 0; i < cryptoToken.length; i++) {
1048 token.add(cryptoToken[i]);
1049 }
Ilya Matyukhinef410e32020-02-04 13:39:48 -08001050 android.hardware.biometrics.face.V1_1.IBiometricsFace daemon11 =
1051 android.hardware.biometrics.face.V1_1.IBiometricsFace.castFrom(
1052 daemon);
1053 if (daemon11 != null) {
1054 return daemon11.enroll_1_1(token, timeout, disabledFeatures, windowId);
1055 } else if (windowId == null) {
1056 return daemon.enroll(token, timeout, disabledFeatures);
1057 } else {
1058 Slog.e(TAG, "enroll(): windowId is only supported in @1.1 HAL");
1059 return ERROR_ESRCH;
1060 }
Kevin Chyna56dff72018-06-19 18:41:12 -07001061 }
Kevin Chyna38653c2019-02-11 17:46:21 -08001062
1063 @Override
1064 public void resetLockout(byte[] cryptoToken) throws RemoteException {
1065 IBiometricsFace daemon = getFaceDaemon();
1066 if (daemon == null) {
1067 Slog.w(TAG, "resetLockout(): no face HAL!");
1068 return;
1069 }
1070 final ArrayList<Byte> token = new ArrayList<>();
1071 for (int i = 0; i < cryptoToken.length; i++) {
1072 token.add(cryptoToken[i]);
1073 }
1074 daemon.resetLockout(token);
1075 }
Kevin Chyna56dff72018-06-19 18:41:12 -07001076 };
1077
1078
1079 public FaceService(Context context) {
1080 super(context);
Kevin Chyn4cc49f72019-04-24 13:53:35 -07001081
joshmccloskey4ceaf6b2019-08-21 11:26:08 -07001082 final boolean ignoreKeyguardBlacklist = Settings.Secure.getInt(context.getContentResolver(),
1083 SKIP_KEYGUARD_ACQUIRE_IGNORE_LIST, 0) != 0;
1084
Kevin Chyn9bc1d492019-06-21 15:31:50 -07001085 mUsageStats = new UsageStats(context);
1086
Kevin Chyndbfbed42019-06-13 17:01:30 -07001087 mNotificationManager = getContext().getSystemService(NotificationManager.class);
1088
Kevin Chyn4cc49f72019-04-24 13:53:35 -07001089 mBiometricPromptIgnoreList = getContext().getResources()
1090 .getIntArray(R.array.config_face_acquire_biometricprompt_ignorelist);
1091 mBiometricPromptIgnoreListVendor = getContext().getResources()
1092 .getIntArray(R.array.config_face_acquire_vendor_biometricprompt_ignorelist);
joshmccloskey4ceaf6b2019-08-21 11:26:08 -07001093 mKeyguardIgnoreList = ignoreKeyguardBlacklist ? new int[0] : getContext().getResources()
Kevin Chyn4cc49f72019-04-24 13:53:35 -07001094 .getIntArray(R.array.config_face_acquire_keyguard_ignorelist);
joshmccloskey4ceaf6b2019-08-21 11:26:08 -07001095 mKeyguardIgnoreListVendor =
1096 ignoreKeyguardBlacklist ? new int[0] : getContext().getResources()
1097 .getIntArray(R.array.config_face_acquire_vendor_keyguard_ignorelist);
Kevin Chyn4cc49f72019-04-24 13:53:35 -07001098 mEnrollIgnoreList = getContext().getResources()
1099 .getIntArray(R.array.config_face_acquire_enroll_ignorelist);
1100 mEnrollIgnoreListVendor = getContext().getResources()
1101 .getIntArray(R.array.config_face_acquire_vendor_enroll_ignorelist);
Kevin Chyna56dff72018-06-19 18:41:12 -07001102 }
1103
1104 @Override
Ilya Matyukhin9dac8482019-07-09 11:03:07 -07001105 protected void removeClient(ClientMonitor client) {
1106 super.removeClient(client);
1107 if (mRevokeChallengePending) {
1108 startRevokeChallenge(null);
1109 mRevokeChallengePending = false;
1110 }
1111 }
1112
1113 @Override
Kevin Chyna56dff72018-06-19 18:41:12 -07001114 public void onStart() {
Kevin Chyn5a2ff5d2018-08-29 19:07:30 -07001115 super.onStart();
Kevin Chyna56dff72018-06-19 18:41:12 -07001116 publishBinderService(Context.FACE_SERVICE, new FaceServiceWrapper());
Kevin Chyn8398a712019-06-13 16:44:50 -07001117 // Get the face daemon on FaceService's on thread so SystemServerInitThreadPool isn't
1118 // blocked
Felipe Lemeb68b7692019-10-09 10:43:03 -07001119 SystemServerInitThreadPool.submit(() -> mHandler.post(this::getFaceDaemon),
Kevin Chyn8398a712019-06-13 16:44:50 -07001120 TAG + ".onStart");
Kevin Chyna56dff72018-06-19 18:41:12 -07001121 }
1122
1123 @Override
1124 public String getTag() {
1125 return TAG;
1126 }
1127
1128 @Override
Kevin Chyn6737c572019-02-08 16:10:54 -08001129 protected DaemonWrapper getDaemonWrapper() {
1130 return mDaemonWrapper;
1131 }
1132
1133 @Override
Kevin Chyna56dff72018-06-19 18:41:12 -07001134 protected BiometricUtils getBiometricUtils() {
1135 return FaceUtils.getInstance();
1136 }
1137
1138 @Override
Kevin Chyn4cc49f72019-04-24 13:53:35 -07001139 protected Constants getConstants() {
1140 return mFaceConstants;
Kevin Chyna56dff72018-06-19 18:41:12 -07001141 }
1142
1143 @Override
1144 protected boolean hasReachedEnrollmentLimit(int userId) {
1145 final int limit = getContext().getResources().getInteger(
Kevin Chyn017e76e2018-06-27 18:35:06 -07001146 com.android.internal.R.integer.config_faceMaxTemplatesPerUser);
Kevin Chyn6737c572019-02-08 16:10:54 -08001147 final int enrolled = FaceService.this.getEnrolledTemplates(userId).size();
Kevin Chyna56dff72018-06-19 18:41:12 -07001148 if (enrolled >= limit) {
Kevin Chyn1a878c12019-04-04 15:50:11 -07001149 Slog.w(TAG, "Too many faces registered, user: " + userId);
Kevin Chyna56dff72018-06-19 18:41:12 -07001150 return true;
1151 }
1152 return false;
1153 }
1154
1155 @Override
Kevin Chyn9ba99912019-01-16 16:24:36 -08001156 public void serviceDied(long cookie) {
1157 super.serviceDied(cookie);
1158 mDaemon = null;
Jim Miller7b78b222019-02-07 16:47:38 -08001159
1160 mCurrentUserId = UserHandle.USER_NULL; // Force updateActiveGroup() to re-evaluate
Kevin Chyn9ba99912019-01-16 16:24:36 -08001161 }
1162
1163 @Override
Kevin Chyna56dff72018-06-19 18:41:12 -07001164 protected void updateActiveGroup(int userId, String clientPackage) {
1165 IBiometricsFace daemon = getFaceDaemon();
1166
1167 if (daemon != null) {
1168 try {
1169 userId = getUserOrWorkProfileId(clientPackage, userId);
1170 if (userId != mCurrentUserId) {
Kevin Chyn27e33d02019-04-30 01:37:32 +00001171 final File baseDir = Environment.getDataVendorDeDirectory(userId);
Kevin Chyna56dff72018-06-19 18:41:12 -07001172 final File faceDir = new File(baseDir, FACE_DATA_DIR);
1173 if (!faceDir.exists()) {
1174 if (!faceDir.mkdir()) {
1175 Slog.v(TAG, "Cannot make directory: " + faceDir.getAbsolutePath());
1176 return;
1177 }
1178 // Calling mkdir() from this process will create a directory with our
1179 // permissions (inherited from the containing dir). This command fixes
1180 // the label.
1181 if (!SELinux.restorecon(faceDir)) {
1182 Slog.w(TAG, "Restorecons failed. Directory will have wrong label.");
1183 return;
1184 }
1185 }
1186
1187 daemon.setActiveUser(userId, faceDir.getAbsolutePath());
1188 mCurrentUserId = userId;
Kevin Chyn576811e2019-06-12 14:50:35 -07001189 mAuthenticatorIds.put(userId,
1190 hasEnrolledBiometrics(userId) ? daemon.getAuthenticatorId().value : 0L);
Kevin Chyna56dff72018-06-19 18:41:12 -07001191 }
Kevin Chyna56dff72018-06-19 18:41:12 -07001192 } catch (RemoteException e) {
1193 Slog.e(TAG, "Failed to setActiveUser():", e);
1194 }
1195 }
1196 }
1197
1198 @Override
1199 protected String getLockoutResetIntent() {
1200 return ACTION_LOCKOUT_RESET;
1201 }
1202
1203 @Override
1204 protected String getLockoutBroadcastPermission() {
1205 return RESET_FACE_LOCKOUT;
1206 }
1207
1208 @Override
1209 protected long getHalDeviceId() {
1210 return mHalDeviceId;
1211 }
1212
1213 @Override
Kevin Chyna38653c2019-02-11 17:46:21 -08001214 protected void handleUserSwitching(int userId) {
1215 super.handleUserSwitching(userId);
1216 // Will be updated when we get the callback from HAL
1217 mCurrentUserLockoutMode = AuthenticationClient.LOCKOUT_NONE;
1218 }
1219
1220 @Override
Kevin Chyna56dff72018-06-19 18:41:12 -07001221 protected boolean hasEnrolledBiometrics(int userId) {
1222 if (userId != UserHandle.getCallingUserId()) {
1223 checkPermission(INTERACT_ACROSS_USERS);
1224 }
1225 return getBiometricUtils().getBiometricsForUser(getContext(), userId).size() > 0;
1226 }
1227
1228 @Override
1229 protected String getManageBiometricPermission() {
Kevin Chyna24e9fd2018-08-27 12:39:17 -07001230 return MANAGE_BIOMETRIC;
Kevin Chyna56dff72018-06-19 18:41:12 -07001231 }
1232
1233 @Override
1234 protected void checkUseBiometricPermission() {
Kevin Chyna24e9fd2018-08-27 12:39:17 -07001235 // noop for Face. The permission checks are all done on the incoming binder call.
Kevin Chyna56dff72018-06-19 18:41:12 -07001236 }
1237
1238 @Override
Kevin Chynb3c05aa2018-09-21 16:50:32 -07001239 protected boolean checkAppOps(int uid, String opPackageName) {
1240 return mAppOps.noteOp(AppOpsManager.OP_USE_BIOMETRIC, uid, opPackageName)
1241 == AppOpsManager.MODE_ALLOWED;
Kevin Chyna56dff72018-06-19 18:41:12 -07001242 }
1243
1244 @Override
Kevin Chyn6737c572019-02-08 16:10:54 -08001245 protected List<Face> getEnrolledTemplates(int userId) {
1246 return getBiometricUtils().getBiometricsForUser(getContext(), userId);
1247 }
1248
1249 @Override
Kevin Chyna56dff72018-06-19 18:41:12 -07001250 protected void notifyClientActiveCallbacks(boolean isActive) {
1251 // noop for Face.
1252 }
1253
Kevin Chyn7782d142019-01-18 12:51:33 -08001254 @Override
1255 protected int statsModality() {
1256 return BiometricsProtoEnums.MODALITY_FACE;
1257 }
1258
Kevin Chyna38653c2019-02-11 17:46:21 -08001259 @Override
1260 protected int getLockoutMode() {
1261 return mCurrentUserLockoutMode;
1262 }
1263
Kevin Chyna56dff72018-06-19 18:41:12 -07001264 /** Gets the face daemon */
1265 private synchronized IBiometricsFace getFaceDaemon() {
1266 if (mDaemon == null) {
1267 Slog.v(TAG, "mDaemon was null, reconnect to face");
1268 try {
1269 mDaemon = IBiometricsFace.getService();
1270 } catch (java.util.NoSuchElementException e) {
1271 // Service doesn't exist or cannot be opened. Logged below.
1272 } catch (RemoteException e) {
1273 Slog.e(TAG, "Failed to get biometric interface", e);
1274 }
1275 if (mDaemon == null) {
1276 Slog.w(TAG, "face HIDL not available");
1277 return null;
1278 }
1279
1280 mDaemon.asBinder().linkToDeath(this, 0);
1281
1282 try {
1283 mHalDeviceId = mDaemon.setCallback(mDaemonCallback).value;
1284 } catch (RemoteException e) {
1285 Slog.e(TAG, "Failed to open face HAL", e);
1286 mDaemon = null; // try again later!
1287 }
1288
1289 if (DEBUG) Slog.v(TAG, "Face HAL id: " + mHalDeviceId);
1290 if (mHalDeviceId != 0) {
1291 loadAuthenticatorIds();
1292 updateActiveGroup(ActivityManager.getCurrentUser(), null);
Kevin Chyn6737c572019-02-08 16:10:54 -08001293 doTemplateCleanupForUser(ActivityManager.getCurrentUser());
Kevin Chyna56dff72018-06-19 18:41:12 -07001294 } else {
1295 Slog.w(TAG, "Failed to open Face HAL!");
1296 MetricsLogger.count(getContext(), "faced_openhal_error", 1);
1297 mDaemon = null;
1298 }
1299 }
1300 return mDaemon;
1301 }
1302
Kevin Chynd79e24e2018-09-25 12:06:59 -07001303 private long startGenerateChallenge(IBinder token) {
Kevin Chyna56dff72018-06-19 18:41:12 -07001304 IBiometricsFace daemon = getFaceDaemon();
1305 if (daemon == null) {
Kevin Chynd79e24e2018-09-25 12:06:59 -07001306 Slog.w(TAG, "startGenerateChallenge: no face HAL!");
Kevin Chyna56dff72018-06-19 18:41:12 -07001307 return 0;
1308 }
1309 try {
Kevin Chyne46a2162018-09-20 18:43:01 -07001310 return daemon.generateChallenge(CHALLENGE_TIMEOUT_SEC).value;
Kevin Chyna56dff72018-06-19 18:41:12 -07001311 } catch (RemoteException e) {
Kevin Chynd79e24e2018-09-25 12:06:59 -07001312 Slog.e(TAG, "startGenerateChallenge failed", e);
Kevin Chyna56dff72018-06-19 18:41:12 -07001313 }
1314 return 0;
1315 }
1316
Kevin Chynd79e24e2018-09-25 12:06:59 -07001317 private int startRevokeChallenge(IBinder token) {
Kevin Chyna56dff72018-06-19 18:41:12 -07001318 IBiometricsFace daemon = getFaceDaemon();
1319 if (daemon == null) {
Kevin Chynd79e24e2018-09-25 12:06:59 -07001320 Slog.w(TAG, "startRevokeChallenge: no face HAL!");
Kevin Chyna56dff72018-06-19 18:41:12 -07001321 return 0;
1322 }
1323 try {
Ilya Matyukhin9dac8482019-07-09 11:03:07 -07001324 final int res = daemon.revokeChallenge();
1325 if (res != Status.OK) {
1326 Slog.e(TAG, "revokeChallenge returned " + res);
1327 }
1328 return res;
Kevin Chyna56dff72018-06-19 18:41:12 -07001329 } catch (RemoteException e) {
Kevin Chynd79e24e2018-09-25 12:06:59 -07001330 Slog.e(TAG, "startRevokeChallenge failed", e);
Kevin Chyna56dff72018-06-19 18:41:12 -07001331 }
1332 return 0;
1333 }
1334
Gilad Brettercb51b8b2018-03-22 17:04:51 +02001335 private void dumpInternal(PrintWriter pw) {
1336 JSONObject dump = new JSONObject();
1337 try {
1338 dump.put("service", "Face Manager");
1339
1340 JSONArray sets = new JSONArray();
1341 for (UserInfo user : UserManager.get(getContext()).getUsers()) {
1342 final int userId = user.getUserHandle().getIdentifier();
Kevin Chyna56dff72018-06-19 18:41:12 -07001343 final int N = getBiometricUtils().getBiometricsForUser(getContext(), userId).size();
Gilad Brettercb51b8b2018-03-22 17:04:51 +02001344 PerformanceStats stats = mPerformanceMap.get(userId);
1345 PerformanceStats cryptoStats = mCryptoPerformanceMap.get(userId);
1346 JSONObject set = new JSONObject();
1347 set.put("id", userId);
Kevin Chyna56dff72018-06-19 18:41:12 -07001348 set.put("count", N);
Gilad Brettercb51b8b2018-03-22 17:04:51 +02001349 set.put("accept", (stats != null) ? stats.accept : 0);
1350 set.put("reject", (stats != null) ? stats.reject : 0);
1351 set.put("acquire", (stats != null) ? stats.acquire : 0);
1352 set.put("lockout", (stats != null) ? stats.lockout : 0);
1353 set.put("permanentLockout", (stats != null) ? stats.permanentLockout : 0);
1354 // cryptoStats measures statistics about secure face transactions
1355 // (e.g. to unlock password storage, make secure purchases, etc.)
1356 set.put("acceptCrypto", (cryptoStats != null) ? cryptoStats.accept : 0);
1357 set.put("rejectCrypto", (cryptoStats != null) ? cryptoStats.reject : 0);
1358 set.put("acquireCrypto", (cryptoStats != null) ? cryptoStats.acquire : 0);
1359 set.put("lockoutCrypto", (cryptoStats != null) ? cryptoStats.lockout : 0);
Kevin Chyna56dff72018-06-19 18:41:12 -07001360 set.put("permanentLockoutCrypto",
1361 (cryptoStats != null) ? cryptoStats.permanentLockout : 0);
Gilad Brettercb51b8b2018-03-22 17:04:51 +02001362 sets.put(set);
1363 }
1364
1365 dump.put("prints", sets);
1366 } catch (JSONException e) {
1367 Slog.e(TAG, "dump formatting failure", e);
1368 }
1369 pw.println(dump);
Kevin Chyn9bc1d492019-06-21 15:31:50 -07001370 pw.println("HAL deaths since last reboot: " + mHALDeathCount);
1371
1372 mUsageStats.print(pw);
Gilad Brettercb51b8b2018-03-22 17:04:51 +02001373 }
1374
Joe Onoratodb396002019-04-05 19:49:27 -07001375 private void dumpHal(FileDescriptor fd, String[] args) {
Joe Onoratobf955d22019-03-25 00:16:58 -07001376 // WARNING: CDD restricts image data from leaving TEE unencrypted on
1377 // production devices:
1378 // [C-1-10] MUST not allow unencrypted access to identifiable biometric
1379 // data or any data derived from it (such as embeddings) to the
1380 // Application Processor outside the context of the TEE.
1381 // As such, this API should only be enabled for testing purposes on
1382 // engineering and userdebug builds. All modules in the software stack
1383 // MUST enforce final build products do NOT have this functionality.
1384 // Additionally, the following check MUST NOT be removed.
1385 if (!(Build.IS_ENG || Build.IS_USERDEBUG)) {
1386 return;
1387 }
1388
Joe Onorato108413a2019-04-03 18:20:52 -07001389 // Additionally, this flag allows turning off face for a device
1390 // (either permanently through the build or on an individual device).
1391 if (SystemProperties.getBoolean("ro.face.disable_debug_data", false)
1392 || SystemProperties.getBoolean("persist.face.disable_debug_data", false)) {
1393 return;
1394 }
1395
Joe Onoratodb396002019-04-05 19:49:27 -07001396 // The debug method takes two file descriptors. The first is for text
1397 // output, which we will drop. The second is for binary data, which
1398 // will be the protobuf data.
1399 final IBiometricsFace daemon = getFaceDaemon();
1400 if (daemon != null) {
1401 FileOutputStream devnull = null;
1402 try {
1403 devnull = new FileOutputStream("/dev/null");
1404 final NativeHandle handle = new NativeHandle(
1405 new FileDescriptor[] { devnull.getFD(), fd },
1406 new int[0], false);
1407 daemon.debug(handle, new ArrayList<String>(Arrays.asList(args)));
1408 } catch (IOException | RemoteException ex) {
1409 Slog.d(TAG, "error while reading face debugging data", ex);
1410 } finally {
1411 if (devnull != null) {
1412 try {
1413 devnull.close();
1414 } catch (IOException ex) {
1415 }
1416 }
1417 }
Joe Onoratobf955d22019-03-25 00:16:58 -07001418 }
Joe Onoratobf955d22019-03-25 00:16:58 -07001419 }
Kevin Chyn8b7a0372018-09-17 15:06:05 -07001420}