blob: a87a455fa2c1a800ecc98cc7166962f6c2600ba0 [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 }
Haining Chenc06c4812020-01-13 20:38:53 -0800743
744 @Override // Binder call
745 public void initConfiguredStrength(int strength) {
746 checkPermission(USE_BIOMETRIC_INTERNAL);
747 initConfiguredStrengthInternal(strength);
748 }
Kevin Chyna56dff72018-06-19 18:41:12 -0700749 }
750
751 /**
752 * Receives callbacks from the ClientMonitor implementations. The results are forwarded to
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700753 * BiometricPrompt.
754 */
Kevin Chyne92cdae2018-11-21 16:35:04 -0800755 private class BiometricPromptServiceListenerImpl extends BiometricServiceListener {
Kevin Chyn23289ef2018-11-28 16:32:36 -0800756 BiometricPromptServiceListenerImpl(IBiometricServiceReceiverInternal wrapperReceiver) {
Kevin Chyn87f257a2018-11-27 16:26:07 -0800757 super(wrapperReceiver);
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700758 }
759
760 @Override
761 public void onAcquired(long deviceId, int acquiredInfo, int vendorCode)
762 throws RemoteException {
763 /**
764 * Map the acquired codes onto existing {@link BiometricConstants} acquired codes.
765 */
Kevin Chyne92cdae2018-11-21 16:35:04 -0800766 if (getWrapperReceiver() != null) {
767 getWrapperReceiver().onAcquired(
Kevin Chyn8b7a0372018-09-17 15:06:05 -0700768 FaceManager.getMappedAcquiredInfo(acquiredInfo, vendorCode),
769 FaceManager.getAcquiredString(getContext(), acquiredInfo, vendorCode));
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700770 }
771 }
772
773 @Override
Kevin Chyn87f257a2018-11-27 16:26:07 -0800774 public void onError(long deviceId, int error, int vendorCode, int cookie)
775 throws RemoteException {
Kevin Chyne92cdae2018-11-21 16:35:04 -0800776 if (getWrapperReceiver() != null) {
Ilya Matyukhin0f9da352019-10-03 14:10:01 -0700777 getWrapperReceiver().onError(cookie, TYPE_FACE, error, vendorCode);
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700778 }
779 }
780 }
781
782 /**
783 * Receives callbacks from the ClientMonitor implementations. The results are forwarded to
Kevin Chyna56dff72018-06-19 18:41:12 -0700784 * the FaceManager.
785 */
786 private class ServiceListenerImpl implements ServiceListener {
Kevin Chyna56dff72018-06-19 18:41:12 -0700787 private IFaceServiceReceiver mFaceServiceReceiver;
788
789 public ServiceListenerImpl(IFaceServiceReceiver receiver) {
790 mFaceServiceReceiver = receiver;
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200791 }
792
793 @Override
Kevin Chyna56dff72018-06-19 18:41:12 -0700794 public void onEnrollResult(BiometricAuthenticator.Identifier identifier, int remaining)
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200795 throws RemoteException {
Kevin Chyna56dff72018-06-19 18:41:12 -0700796 if (mFaceServiceReceiver != null) {
797 mFaceServiceReceiver.onEnrollResult(identifier.getDeviceId(),
798 identifier.getBiometricId(),
799 remaining);
800 }
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200801 }
Kevin Chyna56dff72018-06-19 18:41:12 -0700802
803 @Override
804 public void onAcquired(long deviceId, int acquiredInfo, int vendorCode)
805 throws RemoteException {
806 if (mFaceServiceReceiver != null) {
807 mFaceServiceReceiver.onAcquired(deviceId, acquiredInfo, vendorCode);
808 }
809 }
810
811 @Override
812 public void onAuthenticationSucceeded(long deviceId,
813 BiometricAuthenticator.Identifier biometric, int userId)
814 throws RemoteException {
815 if (mFaceServiceReceiver != null) {
Kevin Chyn628b7182018-11-13 12:00:48 -0800816 if (biometric == null || biometric instanceof Face) {
Kevin Chyn8d2694a2019-04-11 18:30:40 -0700817 mFaceServiceReceiver.onAuthenticationSucceeded(deviceId, (Face) biometric,
Haining Chenc06c4812020-01-13 20:38:53 -0800818 userId, isStrongBiometric());
Kevin Chyna56dff72018-06-19 18:41:12 -0700819 } else {
820 Slog.e(TAG, "onAuthenticationSucceeded received non-face biometric");
821 }
822 }
823 }
824
825 @Override
826 public void onAuthenticationFailed(long deviceId) throws RemoteException {
827 if (mFaceServiceReceiver != null) {
828 mFaceServiceReceiver.onAuthenticationFailed(deviceId);
829 }
830 }
831
832 @Override
Kevin Chyn87f257a2018-11-27 16:26:07 -0800833 public void onError(long deviceId, int error, int vendorCode, int cookie)
834 throws RemoteException {
Kevin Chyna56dff72018-06-19 18:41:12 -0700835 if (mFaceServiceReceiver != null) {
836 mFaceServiceReceiver.onError(deviceId, error, vendorCode);
837 }
838 }
839
840 @Override
841 public void onRemoved(BiometricAuthenticator.Identifier identifier,
842 int remaining) throws RemoteException {
843 if (mFaceServiceReceiver != null) {
844 mFaceServiceReceiver.onRemoved(identifier.getDeviceId(),
845 identifier.getBiometricId(), remaining);
846 }
847 }
848
849 @Override
850 public void onEnumerated(BiometricAuthenticator.Identifier identifier, int remaining)
851 throws RemoteException {
852 if (mFaceServiceReceiver != null) {
Kevin Chyn6737c572019-02-08 16:10:54 -0800853 mFaceServiceReceiver.onEnumerated(identifier.getDeviceId(),
854 identifier.getBiometricId(), remaining);
Kevin Chyna56dff72018-06-19 18:41:12 -0700855 }
856 }
857 }
858
Kevin Chyn4cc49f72019-04-24 13:53:35 -0700859 private final FaceConstants mFaceConstants = new FaceConstants();
Kevin Chyna56dff72018-06-19 18:41:12 -0700860
861 @GuardedBy("this")
862 private IBiometricsFace mDaemon;
Kevin Chyn9bc1d492019-06-21 15:31:50 -0700863 private UsageStats mUsageStats;
Ilya Matyukhin9dac8482019-07-09 11:03:07 -0700864 private boolean mRevokeChallengePending = false;
Kevin Chyna38653c2019-02-11 17:46:21 -0800865 // One of the AuthenticationClient constants
866 private int mCurrentUserLockoutMode;
Kevin Chyna56dff72018-06-19 18:41:12 -0700867
Kevin Chyndbfbed42019-06-13 17:01:30 -0700868 private NotificationManager mNotificationManager;
869
Kevin Chyn4cc49f72019-04-24 13:53:35 -0700870 private int[] mBiometricPromptIgnoreList;
871 private int[] mBiometricPromptIgnoreListVendor;
872 private int[] mKeyguardIgnoreList;
873 private int[] mKeyguardIgnoreListVendor;
874 private int[] mEnrollIgnoreList;
875 private int[] mEnrollIgnoreListVendor;
876
Kevin Chyna56dff72018-06-19 18:41:12 -0700877 /**
878 * Receives callbacks from the HAL.
879 */
880 private IBiometricsFaceClientCallback mDaemonCallback =
881 new IBiometricsFaceClientCallback.Stub() {
Kevin Chyn6737c572019-02-08 16:10:54 -0800882 @Override
883 public void onEnrollResult(final long deviceId, int faceId, int userId,
884 int remaining) {
885 mHandler.post(() -> {
886 final Face face = new Face(getBiometricUtils()
887 .getUniqueName(getContext(), userId), faceId, deviceId);
888 FaceService.super.handleEnrollResult(face, remaining);
Kevin Chyn576811e2019-06-12 14:50:35 -0700889
890 // Enrollment changes the authenticatorId, so update it here.
891 IBiometricsFace daemon = getFaceDaemon();
892 if (remaining == 0 && daemon != null) {
893 try {
894 mAuthenticatorIds.put(userId,
895 hasEnrolledBiometrics(userId) ? daemon.getAuthenticatorId().value
896 : 0L);
897 } catch (RemoteException e) {
898 Slog.e(TAG, "Unable to get authenticatorId", e);
899 }
900 }
Kevin Chyn6737c572019-02-08 16:10:54 -0800901 });
902 }
903
904 @Override
905 public void onAcquired(final long deviceId, final int userId,
906 final int acquiredInfo,
907 final int vendorCode) {
908 mHandler.post(() -> {
909 FaceService.super.handleAcquired(deviceId, acquiredInfo, vendorCode);
910 });
911 }
912
913 @Override
914 public void onAuthenticated(final long deviceId, final int faceId, final int userId,
915 ArrayList<Byte> token) {
916 mHandler.post(() -> {
917 Face face = new Face("", faceId, deviceId);
918 FaceService.super.handleAuthenticated(face, token);
919 });
920 }
921
922 @Override
923 public void onError(final long deviceId, final int userId, final int error,
924 final int vendorCode) {
925 mHandler.post(() -> {
926 FaceService.super.handleError(deviceId, error, vendorCode);
927
928 // TODO: this chunk of code should be common to all biometric services
929 if (error == BiometricConstants.BIOMETRIC_ERROR_HW_UNAVAILABLE) {
930 // If we get HW_UNAVAILABLE, try to connect again later...
931 Slog.w(TAG, "Got ERROR_HW_UNAVAILABLE; try reconnecting next client.");
932 synchronized (this) {
933 mDaemon = null;
934 mHalDeviceId = 0;
935 mCurrentUserId = UserHandle.USER_NULL;
936 }
Kevin Chyna56dff72018-06-19 18:41:12 -0700937 }
Kevin Chyn6737c572019-02-08 16:10:54 -0800938 });
939 }
Kevin Chyna56dff72018-06-19 18:41:12 -0700940
Kevin Chyn6737c572019-02-08 16:10:54 -0800941 @Override
Kevin Chyn31ae8d22019-03-11 14:12:32 -0700942 public void onRemoved(final long deviceId, ArrayList<Integer> faceIds, final int userId) {
Kevin Chyn6737c572019-02-08 16:10:54 -0800943 mHandler.post(() -> {
Kevin Chyn31ae8d22019-03-11 14:12:32 -0700944 if (!faceIds.isEmpty()) {
945 for (int i = 0; i < faceIds.size(); i++) {
946 final Face face = new Face("", faceIds.get(i), deviceId);
947 // Convert to old behavior
948 FaceService.super.handleRemoved(face, faceIds.size() - i - 1);
949 }
950 } else {
951 final Face face = new Face("", 0 /* identifier */, deviceId);
952 FaceService.super.handleRemoved(face, 0 /* remaining */);
953 }
Curtis Belmonte8cfe3712019-09-26 16:02:41 -0700954 Settings.Secure.putIntForUser(getContext().getContentResolver(),
955 Settings.Secure.FACE_UNLOCK_RE_ENROLL, 0, UserHandle.USER_CURRENT);
Kevin Chyn6737c572019-02-08 16:10:54 -0800956 });
957 }
958
959 @Override
960 public void onEnumerate(long deviceId, ArrayList<Integer> faceIds, int userId)
961 throws RemoteException {
962 mHandler.post(() -> {
963 if (!faceIds.isEmpty()) {
964 for (int i = 0; i < faceIds.size(); i++) {
965 final Face face = new Face("", faceIds.get(i), deviceId);
966 // Convert to old old behavior
967 FaceService.super.handleEnumerate(face, faceIds.size() - i - 1);
968 }
969 } else {
970 // For face, the HIDL contract is to receive an empty list when there are no
971 // templates enrolled. Send a null identifier since we don't consume them
972 // anywhere, and send remaining == 0 to plumb this with existing common code.
973 FaceService.super.handleEnumerate(null /* identifier */, 0);
Kevin Chyna56dff72018-06-19 18:41:12 -0700974 }
Kevin Chyn6737c572019-02-08 16:10:54 -0800975 });
976 }
Kevin Chyna56dff72018-06-19 18:41:12 -0700977
Kevin Chyn6737c572019-02-08 16:10:54 -0800978 @Override
979 public void onLockoutChanged(long duration) {
Kevin Chyna38653c2019-02-11 17:46:21 -0800980 Slog.d(TAG, "onLockoutChanged: " + duration);
Curtis Belmontec47df862020-01-28 16:58:34 -0800981
Kevin Chyna38653c2019-02-11 17:46:21 -0800982 if (duration == 0) {
983 mCurrentUserLockoutMode = AuthenticationClient.LOCKOUT_NONE;
Curtis Belmontec47df862020-01-28 16:58:34 -0800984 } else if (duration == -1 || duration == Long.MAX_VALUE) {
Kevin Chyna38653c2019-02-11 17:46:21 -0800985 mCurrentUserLockoutMode = AuthenticationClient.LOCKOUT_PERMANENT;
986 } else {
987 mCurrentUserLockoutMode = AuthenticationClient.LOCKOUT_TIMED;
988 }
Kevin Chyna56dff72018-06-19 18:41:12 -0700989
Kevin Chyna38653c2019-02-11 17:46:21 -0800990 mHandler.post(() -> {
991 if (duration == 0) {
992 notifyLockoutResetMonitors();
993 }
994 });
Kevin Chyn6737c572019-02-08 16:10:54 -0800995 }
996 };
Kevin Chyna56dff72018-06-19 18:41:12 -0700997
998 /**
999 * Wraps the HAL-specific code and is passed to the ClientMonitor implementations so that they
1000 * can be shared between the multiple biometric services.
1001 */
1002 private final DaemonWrapper mDaemonWrapper = new DaemonWrapper() {
1003 @Override
Ilya Matyukhinef410e32020-02-04 13:39:48 -08001004 public int authenticate(long operationId, int groupId, NativeHandle windowId)
1005 throws RemoteException {
Kevin Chyna56dff72018-06-19 18:41:12 -07001006 IBiometricsFace daemon = getFaceDaemon();
1007 if (daemon == null) {
1008 Slog.w(TAG, "authenticate(): no face HAL!");
1009 return ERROR_ESRCH;
1010 }
1011 return daemon.authenticate(operationId);
1012 }
1013
1014 @Override
1015 public int cancel() throws RemoteException {
1016 IBiometricsFace daemon = getFaceDaemon();
1017 if (daemon == null) {
1018 Slog.w(TAG, "cancel(): no face HAL!");
1019 return ERROR_ESRCH;
1020 }
1021 return daemon.cancel();
1022 }
1023
1024 @Override
1025 public int remove(int groupId, int biometricId) throws RemoteException {
1026 IBiometricsFace daemon = getFaceDaemon();
1027 if (daemon == null) {
1028 Slog.w(TAG, "remove(): no face HAL!");
1029 return ERROR_ESRCH;
1030 }
1031 return daemon.remove(biometricId);
1032 }
1033
1034 @Override
1035 public int enumerate() throws RemoteException {
1036 IBiometricsFace daemon = getFaceDaemon();
1037 if (daemon == null) {
1038 Slog.w(TAG, "enumerate(): no face HAL!");
1039 return ERROR_ESRCH;
1040 }
1041 return daemon.enumerate();
1042 }
1043
1044 @Override
Kevin Chyn1f16c2d2018-12-07 13:06:08 -08001045 public int enroll(byte[] cryptoToken, int groupId, int timeout,
Ilya Matyukhinef410e32020-02-04 13:39:48 -08001046 ArrayList<Integer> disabledFeatures, NativeHandle windowId) throws RemoteException {
Kevin Chyna56dff72018-06-19 18:41:12 -07001047 IBiometricsFace daemon = getFaceDaemon();
1048 if (daemon == null) {
1049 Slog.w(TAG, "enroll(): no face HAL!");
1050 return ERROR_ESRCH;
1051 }
1052 final ArrayList<Byte> token = new ArrayList<>();
1053 for (int i = 0; i < cryptoToken.length; i++) {
1054 token.add(cryptoToken[i]);
1055 }
Ilya Matyukhinef410e32020-02-04 13:39:48 -08001056 android.hardware.biometrics.face.V1_1.IBiometricsFace daemon11 =
1057 android.hardware.biometrics.face.V1_1.IBiometricsFace.castFrom(
1058 daemon);
1059 if (daemon11 != null) {
1060 return daemon11.enroll_1_1(token, timeout, disabledFeatures, windowId);
1061 } else if (windowId == null) {
1062 return daemon.enroll(token, timeout, disabledFeatures);
1063 } else {
1064 Slog.e(TAG, "enroll(): windowId is only supported in @1.1 HAL");
1065 return ERROR_ESRCH;
1066 }
Kevin Chyna56dff72018-06-19 18:41:12 -07001067 }
Kevin Chyna38653c2019-02-11 17:46:21 -08001068
1069 @Override
1070 public void resetLockout(byte[] cryptoToken) throws RemoteException {
1071 IBiometricsFace daemon = getFaceDaemon();
1072 if (daemon == null) {
1073 Slog.w(TAG, "resetLockout(): no face HAL!");
1074 return;
1075 }
1076 final ArrayList<Byte> token = new ArrayList<>();
1077 for (int i = 0; i < cryptoToken.length; i++) {
1078 token.add(cryptoToken[i]);
1079 }
1080 daemon.resetLockout(token);
1081 }
Kevin Chyna56dff72018-06-19 18:41:12 -07001082 };
1083
1084
1085 public FaceService(Context context) {
1086 super(context);
Kevin Chyn4cc49f72019-04-24 13:53:35 -07001087
joshmccloskey4ceaf6b2019-08-21 11:26:08 -07001088 final boolean ignoreKeyguardBlacklist = Settings.Secure.getInt(context.getContentResolver(),
1089 SKIP_KEYGUARD_ACQUIRE_IGNORE_LIST, 0) != 0;
1090
Kevin Chyn9bc1d492019-06-21 15:31:50 -07001091 mUsageStats = new UsageStats(context);
1092
Kevin Chyndbfbed42019-06-13 17:01:30 -07001093 mNotificationManager = getContext().getSystemService(NotificationManager.class);
1094
Kevin Chyn4cc49f72019-04-24 13:53:35 -07001095 mBiometricPromptIgnoreList = getContext().getResources()
1096 .getIntArray(R.array.config_face_acquire_biometricprompt_ignorelist);
1097 mBiometricPromptIgnoreListVendor = getContext().getResources()
1098 .getIntArray(R.array.config_face_acquire_vendor_biometricprompt_ignorelist);
joshmccloskey4ceaf6b2019-08-21 11:26:08 -07001099 mKeyguardIgnoreList = ignoreKeyguardBlacklist ? new int[0] : getContext().getResources()
Kevin Chyn4cc49f72019-04-24 13:53:35 -07001100 .getIntArray(R.array.config_face_acquire_keyguard_ignorelist);
joshmccloskey4ceaf6b2019-08-21 11:26:08 -07001101 mKeyguardIgnoreListVendor =
1102 ignoreKeyguardBlacklist ? new int[0] : getContext().getResources()
1103 .getIntArray(R.array.config_face_acquire_vendor_keyguard_ignorelist);
Kevin Chyn4cc49f72019-04-24 13:53:35 -07001104 mEnrollIgnoreList = getContext().getResources()
1105 .getIntArray(R.array.config_face_acquire_enroll_ignorelist);
1106 mEnrollIgnoreListVendor = getContext().getResources()
1107 .getIntArray(R.array.config_face_acquire_vendor_enroll_ignorelist);
Kevin Chyna56dff72018-06-19 18:41:12 -07001108 }
1109
1110 @Override
Ilya Matyukhin9dac8482019-07-09 11:03:07 -07001111 protected void removeClient(ClientMonitor client) {
1112 super.removeClient(client);
1113 if (mRevokeChallengePending) {
1114 startRevokeChallenge(null);
1115 mRevokeChallengePending = false;
1116 }
1117 }
1118
1119 @Override
Kevin Chyna56dff72018-06-19 18:41:12 -07001120 public void onStart() {
Kevin Chyn5a2ff5d2018-08-29 19:07:30 -07001121 super.onStart();
Kevin Chyna56dff72018-06-19 18:41:12 -07001122 publishBinderService(Context.FACE_SERVICE, new FaceServiceWrapper());
Kevin Chyn8398a712019-06-13 16:44:50 -07001123 // Get the face daemon on FaceService's on thread so SystemServerInitThreadPool isn't
1124 // blocked
Felipe Lemeb68b7692019-10-09 10:43:03 -07001125 SystemServerInitThreadPool.submit(() -> mHandler.post(this::getFaceDaemon),
Kevin Chyn8398a712019-06-13 16:44:50 -07001126 TAG + ".onStart");
Kevin Chyna56dff72018-06-19 18:41:12 -07001127 }
1128
1129 @Override
1130 public String getTag() {
1131 return TAG;
1132 }
1133
1134 @Override
Kevin Chyn6737c572019-02-08 16:10:54 -08001135 protected DaemonWrapper getDaemonWrapper() {
1136 return mDaemonWrapper;
1137 }
1138
1139 @Override
Kevin Chyna56dff72018-06-19 18:41:12 -07001140 protected BiometricUtils getBiometricUtils() {
1141 return FaceUtils.getInstance();
1142 }
1143
1144 @Override
Kevin Chyn4cc49f72019-04-24 13:53:35 -07001145 protected Constants getConstants() {
1146 return mFaceConstants;
Kevin Chyna56dff72018-06-19 18:41:12 -07001147 }
1148
1149 @Override
1150 protected boolean hasReachedEnrollmentLimit(int userId) {
1151 final int limit = getContext().getResources().getInteger(
Kevin Chyn017e76e2018-06-27 18:35:06 -07001152 com.android.internal.R.integer.config_faceMaxTemplatesPerUser);
Kevin Chyn6737c572019-02-08 16:10:54 -08001153 final int enrolled = FaceService.this.getEnrolledTemplates(userId).size();
Kevin Chyna56dff72018-06-19 18:41:12 -07001154 if (enrolled >= limit) {
Kevin Chyn1a878c12019-04-04 15:50:11 -07001155 Slog.w(TAG, "Too many faces registered, user: " + userId);
Kevin Chyna56dff72018-06-19 18:41:12 -07001156 return true;
1157 }
1158 return false;
1159 }
1160
1161 @Override
Kevin Chyn9ba99912019-01-16 16:24:36 -08001162 public void serviceDied(long cookie) {
1163 super.serviceDied(cookie);
1164 mDaemon = null;
Jim Miller7b78b222019-02-07 16:47:38 -08001165
1166 mCurrentUserId = UserHandle.USER_NULL; // Force updateActiveGroup() to re-evaluate
Kevin Chyn9ba99912019-01-16 16:24:36 -08001167 }
1168
1169 @Override
Kevin Chyna56dff72018-06-19 18:41:12 -07001170 protected void updateActiveGroup(int userId, String clientPackage) {
1171 IBiometricsFace daemon = getFaceDaemon();
1172
1173 if (daemon != null) {
1174 try {
1175 userId = getUserOrWorkProfileId(clientPackage, userId);
1176 if (userId != mCurrentUserId) {
Kevin Chyn27e33d02019-04-30 01:37:32 +00001177 final File baseDir = Environment.getDataVendorDeDirectory(userId);
Kevin Chyna56dff72018-06-19 18:41:12 -07001178 final File faceDir = new File(baseDir, FACE_DATA_DIR);
1179 if (!faceDir.exists()) {
1180 if (!faceDir.mkdir()) {
1181 Slog.v(TAG, "Cannot make directory: " + faceDir.getAbsolutePath());
1182 return;
1183 }
1184 // Calling mkdir() from this process will create a directory with our
1185 // permissions (inherited from the containing dir). This command fixes
1186 // the label.
1187 if (!SELinux.restorecon(faceDir)) {
1188 Slog.w(TAG, "Restorecons failed. Directory will have wrong label.");
1189 return;
1190 }
1191 }
1192
1193 daemon.setActiveUser(userId, faceDir.getAbsolutePath());
1194 mCurrentUserId = userId;
Kevin Chyn576811e2019-06-12 14:50:35 -07001195 mAuthenticatorIds.put(userId,
1196 hasEnrolledBiometrics(userId) ? daemon.getAuthenticatorId().value : 0L);
Kevin Chyna56dff72018-06-19 18:41:12 -07001197 }
Kevin Chyna56dff72018-06-19 18:41:12 -07001198 } catch (RemoteException e) {
1199 Slog.e(TAG, "Failed to setActiveUser():", e);
1200 }
1201 }
1202 }
1203
1204 @Override
1205 protected String getLockoutResetIntent() {
1206 return ACTION_LOCKOUT_RESET;
1207 }
1208
1209 @Override
1210 protected String getLockoutBroadcastPermission() {
1211 return RESET_FACE_LOCKOUT;
1212 }
1213
1214 @Override
1215 protected long getHalDeviceId() {
1216 return mHalDeviceId;
1217 }
1218
1219 @Override
Kevin Chyna38653c2019-02-11 17:46:21 -08001220 protected void handleUserSwitching(int userId) {
1221 super.handleUserSwitching(userId);
1222 // Will be updated when we get the callback from HAL
1223 mCurrentUserLockoutMode = AuthenticationClient.LOCKOUT_NONE;
1224 }
1225
1226 @Override
Kevin Chyna56dff72018-06-19 18:41:12 -07001227 protected boolean hasEnrolledBiometrics(int userId) {
1228 if (userId != UserHandle.getCallingUserId()) {
1229 checkPermission(INTERACT_ACROSS_USERS);
1230 }
1231 return getBiometricUtils().getBiometricsForUser(getContext(), userId).size() > 0;
1232 }
1233
1234 @Override
1235 protected String getManageBiometricPermission() {
Kevin Chyna24e9fd2018-08-27 12:39:17 -07001236 return MANAGE_BIOMETRIC;
Kevin Chyna56dff72018-06-19 18:41:12 -07001237 }
1238
1239 @Override
1240 protected void checkUseBiometricPermission() {
Kevin Chyna24e9fd2018-08-27 12:39:17 -07001241 // noop for Face. The permission checks are all done on the incoming binder call.
Kevin Chyna56dff72018-06-19 18:41:12 -07001242 }
1243
1244 @Override
Kevin Chynb3c05aa2018-09-21 16:50:32 -07001245 protected boolean checkAppOps(int uid, String opPackageName) {
1246 return mAppOps.noteOp(AppOpsManager.OP_USE_BIOMETRIC, uid, opPackageName)
1247 == AppOpsManager.MODE_ALLOWED;
Kevin Chyna56dff72018-06-19 18:41:12 -07001248 }
1249
1250 @Override
Kevin Chyn6737c572019-02-08 16:10:54 -08001251 protected List<Face> getEnrolledTemplates(int userId) {
1252 return getBiometricUtils().getBiometricsForUser(getContext(), userId);
1253 }
1254
1255 @Override
Kevin Chyna56dff72018-06-19 18:41:12 -07001256 protected void notifyClientActiveCallbacks(boolean isActive) {
1257 // noop for Face.
1258 }
1259
Kevin Chyn7782d142019-01-18 12:51:33 -08001260 @Override
1261 protected int statsModality() {
1262 return BiometricsProtoEnums.MODALITY_FACE;
1263 }
1264
Kevin Chyna38653c2019-02-11 17:46:21 -08001265 @Override
1266 protected int getLockoutMode() {
1267 return mCurrentUserLockoutMode;
1268 }
1269
Kevin Chyna56dff72018-06-19 18:41:12 -07001270 /** Gets the face daemon */
1271 private synchronized IBiometricsFace getFaceDaemon() {
1272 if (mDaemon == null) {
1273 Slog.v(TAG, "mDaemon was null, reconnect to face");
1274 try {
1275 mDaemon = IBiometricsFace.getService();
1276 } catch (java.util.NoSuchElementException e) {
1277 // Service doesn't exist or cannot be opened. Logged below.
1278 } catch (RemoteException e) {
1279 Slog.e(TAG, "Failed to get biometric interface", e);
1280 }
1281 if (mDaemon == null) {
1282 Slog.w(TAG, "face HIDL not available");
1283 return null;
1284 }
1285
1286 mDaemon.asBinder().linkToDeath(this, 0);
1287
1288 try {
1289 mHalDeviceId = mDaemon.setCallback(mDaemonCallback).value;
1290 } catch (RemoteException e) {
1291 Slog.e(TAG, "Failed to open face HAL", e);
1292 mDaemon = null; // try again later!
1293 }
1294
1295 if (DEBUG) Slog.v(TAG, "Face HAL id: " + mHalDeviceId);
1296 if (mHalDeviceId != 0) {
1297 loadAuthenticatorIds();
1298 updateActiveGroup(ActivityManager.getCurrentUser(), null);
Kevin Chyn6737c572019-02-08 16:10:54 -08001299 doTemplateCleanupForUser(ActivityManager.getCurrentUser());
Kevin Chyna56dff72018-06-19 18:41:12 -07001300 } else {
1301 Slog.w(TAG, "Failed to open Face HAL!");
1302 MetricsLogger.count(getContext(), "faced_openhal_error", 1);
1303 mDaemon = null;
1304 }
1305 }
1306 return mDaemon;
1307 }
1308
Kevin Chynd79e24e2018-09-25 12:06:59 -07001309 private long startGenerateChallenge(IBinder token) {
Kevin Chyna56dff72018-06-19 18:41:12 -07001310 IBiometricsFace daemon = getFaceDaemon();
1311 if (daemon == null) {
Kevin Chynd79e24e2018-09-25 12:06:59 -07001312 Slog.w(TAG, "startGenerateChallenge: no face HAL!");
Kevin Chyna56dff72018-06-19 18:41:12 -07001313 return 0;
1314 }
1315 try {
Kevin Chyne46a2162018-09-20 18:43:01 -07001316 return daemon.generateChallenge(CHALLENGE_TIMEOUT_SEC).value;
Kevin Chyna56dff72018-06-19 18:41:12 -07001317 } catch (RemoteException e) {
Kevin Chynd79e24e2018-09-25 12:06:59 -07001318 Slog.e(TAG, "startGenerateChallenge failed", e);
Kevin Chyna56dff72018-06-19 18:41:12 -07001319 }
1320 return 0;
1321 }
1322
Kevin Chynd79e24e2018-09-25 12:06:59 -07001323 private int startRevokeChallenge(IBinder token) {
Kevin Chyna56dff72018-06-19 18:41:12 -07001324 IBiometricsFace daemon = getFaceDaemon();
1325 if (daemon == null) {
Kevin Chynd79e24e2018-09-25 12:06:59 -07001326 Slog.w(TAG, "startRevokeChallenge: no face HAL!");
Kevin Chyna56dff72018-06-19 18:41:12 -07001327 return 0;
1328 }
1329 try {
Ilya Matyukhin9dac8482019-07-09 11:03:07 -07001330 final int res = daemon.revokeChallenge();
1331 if (res != Status.OK) {
1332 Slog.e(TAG, "revokeChallenge returned " + res);
1333 }
1334 return res;
Kevin Chyna56dff72018-06-19 18:41:12 -07001335 } catch (RemoteException e) {
Kevin Chynd79e24e2018-09-25 12:06:59 -07001336 Slog.e(TAG, "startRevokeChallenge failed", e);
Kevin Chyna56dff72018-06-19 18:41:12 -07001337 }
1338 return 0;
1339 }
1340
Gilad Brettercb51b8b2018-03-22 17:04:51 +02001341 private void dumpInternal(PrintWriter pw) {
1342 JSONObject dump = new JSONObject();
1343 try {
1344 dump.put("service", "Face Manager");
1345
1346 JSONArray sets = new JSONArray();
1347 for (UserInfo user : UserManager.get(getContext()).getUsers()) {
1348 final int userId = user.getUserHandle().getIdentifier();
Kevin Chyna56dff72018-06-19 18:41:12 -07001349 final int N = getBiometricUtils().getBiometricsForUser(getContext(), userId).size();
Gilad Brettercb51b8b2018-03-22 17:04:51 +02001350 PerformanceStats stats = mPerformanceMap.get(userId);
1351 PerformanceStats cryptoStats = mCryptoPerformanceMap.get(userId);
1352 JSONObject set = new JSONObject();
1353 set.put("id", userId);
Kevin Chyna56dff72018-06-19 18:41:12 -07001354 set.put("count", N);
Gilad Brettercb51b8b2018-03-22 17:04:51 +02001355 set.put("accept", (stats != null) ? stats.accept : 0);
1356 set.put("reject", (stats != null) ? stats.reject : 0);
1357 set.put("acquire", (stats != null) ? stats.acquire : 0);
1358 set.put("lockout", (stats != null) ? stats.lockout : 0);
1359 set.put("permanentLockout", (stats != null) ? stats.permanentLockout : 0);
1360 // cryptoStats measures statistics about secure face transactions
1361 // (e.g. to unlock password storage, make secure purchases, etc.)
1362 set.put("acceptCrypto", (cryptoStats != null) ? cryptoStats.accept : 0);
1363 set.put("rejectCrypto", (cryptoStats != null) ? cryptoStats.reject : 0);
1364 set.put("acquireCrypto", (cryptoStats != null) ? cryptoStats.acquire : 0);
1365 set.put("lockoutCrypto", (cryptoStats != null) ? cryptoStats.lockout : 0);
Kevin Chyna56dff72018-06-19 18:41:12 -07001366 set.put("permanentLockoutCrypto",
1367 (cryptoStats != null) ? cryptoStats.permanentLockout : 0);
Gilad Brettercb51b8b2018-03-22 17:04:51 +02001368 sets.put(set);
1369 }
1370
1371 dump.put("prints", sets);
1372 } catch (JSONException e) {
1373 Slog.e(TAG, "dump formatting failure", e);
1374 }
1375 pw.println(dump);
Kevin Chyn9bc1d492019-06-21 15:31:50 -07001376 pw.println("HAL deaths since last reboot: " + mHALDeathCount);
1377
1378 mUsageStats.print(pw);
Gilad Brettercb51b8b2018-03-22 17:04:51 +02001379 }
1380
Joe Onoratodb396002019-04-05 19:49:27 -07001381 private void dumpHal(FileDescriptor fd, String[] args) {
Joe Onoratobf955d22019-03-25 00:16:58 -07001382 // WARNING: CDD restricts image data from leaving TEE unencrypted on
1383 // production devices:
1384 // [C-1-10] MUST not allow unencrypted access to identifiable biometric
1385 // data or any data derived from it (such as embeddings) to the
1386 // Application Processor outside the context of the TEE.
1387 // As such, this API should only be enabled for testing purposes on
1388 // engineering and userdebug builds. All modules in the software stack
1389 // MUST enforce final build products do NOT have this functionality.
1390 // Additionally, the following check MUST NOT be removed.
1391 if (!(Build.IS_ENG || Build.IS_USERDEBUG)) {
1392 return;
1393 }
1394
Joe Onorato108413a2019-04-03 18:20:52 -07001395 // Additionally, this flag allows turning off face for a device
1396 // (either permanently through the build or on an individual device).
1397 if (SystemProperties.getBoolean("ro.face.disable_debug_data", false)
1398 || SystemProperties.getBoolean("persist.face.disable_debug_data", false)) {
1399 return;
1400 }
1401
Joe Onoratodb396002019-04-05 19:49:27 -07001402 // The debug method takes two file descriptors. The first is for text
1403 // output, which we will drop. The second is for binary data, which
1404 // will be the protobuf data.
1405 final IBiometricsFace daemon = getFaceDaemon();
1406 if (daemon != null) {
1407 FileOutputStream devnull = null;
1408 try {
1409 devnull = new FileOutputStream("/dev/null");
1410 final NativeHandle handle = new NativeHandle(
1411 new FileDescriptor[] { devnull.getFD(), fd },
1412 new int[0], false);
1413 daemon.debug(handle, new ArrayList<String>(Arrays.asList(args)));
1414 } catch (IOException | RemoteException ex) {
1415 Slog.d(TAG, "error while reading face debugging data", ex);
1416 } finally {
1417 if (devnull != null) {
1418 try {
1419 devnull.close();
1420 } catch (IOException ex) {
1421 }
1422 }
1423 }
Joe Onoratobf955d22019-03-25 00:16:58 -07001424 }
Joe Onoratobf955d22019-03-25 00:16:58 -07001425 }
Kevin Chyn8b7a0372018-09-17 15:06:05 -07001426}