blob: d172fbc4c00d54d3fcdb6284d8f243415cebecd8 [file] [log] [blame]
Gilad Brettercb51b8b2018-03-22 17:04:51 +02001/*
2 * Copyright (C) 2018 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Kevin Chyn2ffadb32018-06-19 11:29:38 -070017package com.android.server.biometrics.face;
Gilad Brettercb51b8b2018-03-22 17:04:51 +020018
19import static android.Manifest.permission.INTERACT_ACROSS_USERS;
Kevin Chyna24e9fd2018-08-27 12:39:17 -070020import static android.Manifest.permission.MANAGE_BIOMETRIC;
Gilad Brettercb51b8b2018-03-22 17:04:51 +020021import static android.Manifest.permission.RESET_FACE_LOCKOUT;
Kevin Chyna24e9fd2018-08-27 12:39:17 -070022import static android.Manifest.permission.USE_BIOMETRIC_INTERNAL;
Gilad Brettercb51b8b2018-03-22 17:04:51 +020023
24import android.app.ActivityManager;
Gilad Brettercb51b8b2018-03-22 17:04:51 +020025import android.app.AppOpsManager;
Kevin Chyn56e4c3d2019-04-23 11:28:43 -070026import android.app.Notification;
27import android.app.NotificationChannel;
28import android.app.NotificationManager;
29import android.app.PendingIntent;
Gilad Brettercb51b8b2018-03-22 17:04:51 +020030import android.content.Context;
Kevin Chyn56e4c3d2019-04-23 11:28:43 -070031import android.content.Intent;
Gilad Brettercb51b8b2018-03-22 17:04:51 +020032import android.content.pm.UserInfo;
Kevin Chyna56dff72018-06-19 18:41:12 -070033import android.hardware.biometrics.BiometricAuthenticator;
34import android.hardware.biometrics.BiometricConstants;
Kevin Chyn7782d142019-01-18 12:51:33 -080035import android.hardware.biometrics.BiometricsProtoEnums;
Kevin Chyna56dff72018-06-19 18:41:12 -070036import android.hardware.biometrics.IBiometricServiceLockoutResetCallback;
Kevin Chyn23289ef2018-11-28 16:32:36 -080037import android.hardware.biometrics.IBiometricServiceReceiverInternal;
Gilad Brettercb51b8b2018-03-22 17:04:51 +020038import android.hardware.biometrics.face.V1_0.IBiometricsFace;
39import android.hardware.biometrics.face.V1_0.IBiometricsFaceClientCallback;
Kevin Chynb95f1522019-03-04 16:45:15 -080040import android.hardware.biometrics.face.V1_0.OptionalBool;
Kevin Chynd79e24e2018-09-25 12:06:59 -070041import android.hardware.biometrics.face.V1_0.Status;
Gilad Brettercb51b8b2018-03-22 17:04:51 +020042import android.hardware.face.Face;
Kevin Chyna24e9fd2018-08-27 12:39:17 -070043import android.hardware.face.FaceManager;
Gilad Brettercb51b8b2018-03-22 17:04:51 +020044import android.hardware.face.IFaceService;
Gilad Brettercb51b8b2018-03-22 17:04:51 +020045import android.hardware.face.IFaceServiceReceiver;
46import android.os.Binder;
Joe Onoratobf955d22019-03-25 00:16:58 -070047import android.os.Build;
Gilad Brettercb51b8b2018-03-22 17:04:51 +020048import android.os.Environment;
Gilad Brettercb51b8b2018-03-22 17:04:51 +020049import android.os.IBinder;
Joe Onoratodb396002019-04-05 19:49:27 -070050import android.os.NativeHandle;
Gilad Brettercb51b8b2018-03-22 17:04:51 +020051import android.os.RemoteException;
52import android.os.SELinux;
Joe Onorato108413a2019-04-03 18:20:52 -070053import android.os.SystemProperties;
Gilad Brettercb51b8b2018-03-22 17:04:51 +020054import android.os.UserHandle;
55import android.os.UserManager;
Gilad Brettercb51b8b2018-03-22 17:04:51 +020056import android.util.Slog;
57import android.util.proto.ProtoOutputStream;
58
Kevin Chyn56e4c3d2019-04-23 11:28:43 -070059import com.android.internal.R;
Gilad Brettercb51b8b2018-03-22 17:04:51 +020060import com.android.internal.annotations.GuardedBy;
61import com.android.internal.logging.MetricsLogger;
62import com.android.internal.util.DumpUtils;
63import com.android.server.SystemServerInitThreadPool;
Kevin Chyna38653c2019-02-11 17:46:21 -080064import com.android.server.biometrics.AuthenticationClient;
Kevin Chyn355c6bf2018-09-20 22:14:19 -070065import com.android.server.biometrics.BiometricServiceBase;
Kevin Chyn836f2cf2018-08-27 11:06:39 -070066import com.android.server.biometrics.BiometricUtils;
Kevin Chyn4cc49f72019-04-24 13:53:35 -070067import com.android.server.biometrics.Constants;
Kevin Chyn0ce70852019-05-10 10:29:18 -070068import com.android.server.biometrics.EnumerateClient;
Kevin Chyn6737c572019-02-08 16:10:54 -080069import com.android.server.biometrics.RemovalClient;
Gilad Brettercb51b8b2018-03-22 17:04:51 +020070
71import org.json.JSONArray;
72import org.json.JSONException;
73import org.json.JSONObject;
74
75import java.io.File;
76import java.io.FileDescriptor;
Joe Onoratodb396002019-04-05 19:49:27 -070077import java.io.FileOutputStream;
78import java.io.IOException;
Gilad Brettercb51b8b2018-03-22 17:04:51 +020079import java.io.PrintWriter;
80import java.util.ArrayList;
Joe Onoratodb396002019-04-05 19:49:27 -070081import java.util.Arrays;
Gilad Brettercb51b8b2018-03-22 17:04:51 +020082import java.util.List;
Gilad Brettercb51b8b2018-03-22 17:04:51 +020083
84/**
85 * A service to manage multiple clients that want to access the face HAL API.
86 * The service is responsible for maintaining a list of clients and dispatching all
Kevin Chyn51676d22018-11-05 18:00:43 -080087 * face-related events.
Gilad Brettercb51b8b2018-03-22 17:04:51 +020088 *
89 * @hide
90 */
Kevin Chyn355c6bf2018-09-20 22:14:19 -070091public class FaceService extends BiometricServiceBase {
Kevin Chyna56dff72018-06-19 18:41:12 -070092
93 protected static final String TAG = "FaceService";
94 private static final boolean DEBUG = true;
Gilad Brettercb51b8b2018-03-22 17:04:51 +020095 private static final String FACE_DATA_DIR = "facedata";
Gilad Brettercb51b8b2018-03-22 17:04:51 +020096 private static final String ACTION_LOCKOUT_RESET =
Kevin Chyn2ffadb32018-06-19 11:29:38 -070097 "com.android.server.biometrics.face.ACTION_LOCKOUT_RESET";
Kevin Chyne46a2162018-09-20 18:43:01 -070098 private static final int CHALLENGE_TIMEOUT_SEC = 600; // 10 minutes
Gilad Brettercb51b8b2018-03-22 17:04:51 +020099
Kevin Chyn8b7a0372018-09-17 15:06:05 -0700100 private final class FaceAuthClient extends AuthenticationClientImpl {
Kevin Chyn0ce70852019-05-10 10:29:18 -0700101 private int mLastAcquire;
102
Kevin Chyn8b7a0372018-09-17 15:06:05 -0700103 public FaceAuthClient(Context context,
104 DaemonWrapper daemon, long halDeviceId, IBinder token,
105 ServiceListener listener, int targetUserId, int groupId, long opId,
Kevin Chyn87f257a2018-11-27 16:26:07 -0800106 boolean restricted, String owner, int cookie, boolean requireConfirmation) {
Kevin Chyn8b7a0372018-09-17 15:06:05 -0700107 super(context, daemon, halDeviceId, token, listener, targetUserId, groupId, opId,
Kevin Chyn87f257a2018-11-27 16:26:07 -0800108 restricted, owner, cookie, requireConfirmation);
Kevin Chyn8b7a0372018-09-17 15:06:05 -0700109 }
Kevin Chyn7782d142019-01-18 12:51:33 -0800110
111 @Override
112 protected int statsModality() {
113 return FaceService.this.statsModality();
114 }
Kevin Chyna38653c2019-02-11 17:46:21 -0800115
116 @Override
117 public boolean shouldFrameworkHandleLockout() {
118 return false;
119 }
Kevin Chyn56d6b072019-02-13 18:39:01 -0800120
121 @Override
Kevin Chyn0ce70852019-05-10 10:29:18 -0700122 public boolean wasUserDetected() {
123 return mLastAcquire != FaceManager.FACE_ACQUIRED_NOT_DETECTED;
124 }
125
126 @Override
Kevin Chyn56d6b072019-02-13 18:39:01 -0800127 public boolean onAuthenticated(BiometricAuthenticator.Identifier identifier,
128 boolean authenticated, ArrayList<Byte> token) {
129 final boolean result = super.onAuthenticated(identifier, authenticated, token);
130
131 // For face, the authentication lifecycle ends either when
132 // 1) Authenticated == true
133 // 2) Error occurred
134 // 3) Authenticated == false
135 // Fingerprint currently does not end when the third condition is met which is a bug,
136 // but let's leave it as-is for now.
137 return result || !authenticated;
138 }
Kevin Chyn56e4c3d2019-04-23 11:28:43 -0700139
140 @Override
Kevin Chyn4cc49f72019-04-24 13:53:35 -0700141 public int[] getAcquireIgnorelist() {
142 if (isBiometricPrompt()) {
143 return mBiometricPromptIgnoreList;
144 } else {
145 // Keyguard
146 return mKeyguardIgnoreList;
147 }
148 }
149
150 @Override
151 public int[] getAcquireVendorIgnorelist() {
152 if (isBiometricPrompt()) {
153 return mBiometricPromptIgnoreListVendor;
154 } else {
155 // Keyguard
156 return mKeyguardIgnoreListVendor;
157 }
158 }
159
160 @Override
Kevin Chyn56e4c3d2019-04-23 11:28:43 -0700161 public boolean onAcquired(int acquireInfo, int vendorCode) {
162
Kevin Chyn0ce70852019-05-10 10:29:18 -0700163 mLastAcquire = acquireInfo;
164
Kevin Chyn56e4c3d2019-04-23 11:28:43 -0700165 if (acquireInfo == FaceManager.FACE_ACQUIRED_RECALIBRATE) {
166 final String name =
167 getContext().getString(R.string.face_recalibrate_notification_name);
168 final String title =
169 getContext().getString(R.string.face_recalibrate_notification_title);
170 final String content =
171 getContext().getString(R.string.face_recalibrate_notification_content);
172
173 final Intent intent = new Intent("android.settings.FACE_SETTINGS");
174 intent.setPackage("com.android.settings");
175
176 final PendingIntent pendingIntent = PendingIntent.getActivityAsUser(getContext(),
177 0 /* requestCode */, intent, 0 /* flags */, null /* options */,
178 UserHandle.CURRENT);
179
180 final String id = "FaceService";
181
182 NotificationManager nm =
183 getContext().getSystemService(NotificationManager.class);
184 NotificationChannel channel = new NotificationChannel(id, name,
185 NotificationManager.IMPORTANCE_HIGH);
186 Notification notification = new Notification.Builder(getContext(), id)
187 .setSmallIcon(R.drawable.ic_lock)
188 .setContentTitle(title)
189 .setContentText(content)
190 .setSubText(name)
191 .setOnlyAlertOnce(true)
192 .setLocalOnly(true)
193 .setAutoCancel(true)
194 .setCategory(Notification.CATEGORY_SYSTEM)
195 .setContentIntent(pendingIntent)
Kevin Chyn0c3a9982019-06-11 19:09:27 -0700196 .setVisibility(Notification.VISIBILITY_SECRET)
Kevin Chyn56e4c3d2019-04-23 11:28:43 -0700197 .build();
198
199 nm.createNotificationChannel(channel);
200 nm.notifyAsUser(null /* tag */, 0 /* id */, notification, UserHandle.CURRENT);
201 }
202
203 return super.onAcquired(acquireInfo, vendorCode);
204 }
Kevin Chyn8b7a0372018-09-17 15:06:05 -0700205 }
206
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200207 /**
Kevin Chyna56dff72018-06-19 18:41:12 -0700208 * Receives the incoming binder calls from FaceManager.
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200209 */
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200210 private final class FaceServiceWrapper extends IFaceService.Stub {
Kevin Chyna56dff72018-06-19 18:41:12 -0700211
212 /**
213 * The following methods contain common code which is shared in biometrics/common.
214 */
Kevin Chyna38653c2019-02-11 17:46:21 -0800215
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200216 @Override // Binder call
Kevin Chynd79e24e2018-09-25 12:06:59 -0700217 public long generateChallenge(IBinder token) {
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700218 checkPermission(MANAGE_BIOMETRIC);
Kevin Chynd79e24e2018-09-25 12:06:59 -0700219 return startGenerateChallenge(token);
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200220 }
221
222 @Override // Binder call
Kevin Chynd79e24e2018-09-25 12:06:59 -0700223 public int revokeChallenge(IBinder token) {
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700224 checkPermission(MANAGE_BIOMETRIC);
Kevin Chynd79e24e2018-09-25 12:06:59 -0700225 return startRevokeChallenge(token);
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200226 }
227
228 @Override // Binder call
Kevin Chyn1f16c2d2018-12-07 13:06:08 -0800229 public void enroll(final IBinder token, final byte[] cryptoToken,
230 final IFaceServiceReceiver receiver, final String opPackageName,
231 final int[] disabledFeatures) {
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700232 checkPermission(MANAGE_BIOMETRIC);
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200233
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200234 final boolean restricted = isRestricted();
Kevin Chyna56dff72018-06-19 18:41:12 -0700235 final EnrollClientImpl client = new EnrollClientImpl(getContext(), mDaemonWrapper,
236 mHalDeviceId, token, new ServiceListenerImpl(receiver), mCurrentUserId,
Kevin Chyn1429a312019-01-28 16:08:09 -0800237 0 /* groupId */, cryptoToken, restricted, opPackageName, disabledFeatures) {
Kevin Chyn4cc49f72019-04-24 13:53:35 -0700238
239 @Override
240 public int[] getAcquireIgnorelist() {
241 return mEnrollIgnoreList;
242 }
243
244 @Override
245 public int[] getAcquireVendorIgnorelist() {
246 return mEnrollIgnoreListVendor;
247 }
248
Kevin Chyn1429a312019-01-28 16:08:09 -0800249 @Override
250 public boolean shouldVibrate() {
251 return false;
252 }
Kevin Chyn7782d142019-01-18 12:51:33 -0800253
254 @Override
255 protected int statsModality() {
256 return FaceService.this.statsModality();
257 }
Kevin Chyn1429a312019-01-28 16:08:09 -0800258 };
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200259
Kevin Chyn1a878c12019-04-04 15:50:11 -0700260 enrollInternal(client, mCurrentUserId);
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200261 }
262
263 @Override // Binder call
264 public void cancelEnrollment(final IBinder token) {
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700265 checkPermission(MANAGE_BIOMETRIC);
Kevin Chyna56dff72018-06-19 18:41:12 -0700266 cancelEnrollmentInternal(token);
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200267 }
268
269 @Override // Binder call
Kevin Chyn747e29b2019-01-11 17:01:53 -0800270 public void authenticate(final IBinder token, final long opId, int userId,
Kevin Chyna56dff72018-06-19 18:41:12 -0700271 final IFaceServiceReceiver receiver, final int flags,
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700272 final String opPackageName) {
273 checkPermission(USE_BIOMETRIC_INTERNAL);
Kevin Chyn747e29b2019-01-11 17:01:53 -0800274 updateActiveGroup(userId, opPackageName);
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200275 final boolean restricted = isRestricted();
Kevin Chyn8b7a0372018-09-17 15:06:05 -0700276 final AuthenticationClientImpl client = new FaceAuthClient(getContext(),
Kevin Chyna56dff72018-06-19 18:41:12 -0700277 mDaemonWrapper, mHalDeviceId, token, new ServiceListenerImpl(receiver),
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700278 mCurrentUserId, 0 /* groupId */, opId, restricted, opPackageName,
Kevin Chyn87f257a2018-11-27 16:26:07 -0800279 0 /* cookie */, false /* requireConfirmation */);
Kevin Chyna56dff72018-06-19 18:41:12 -0700280 authenticateInternal(client, opId, opPackageName);
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200281 }
282
283 @Override // Binder call
Kevin Chyn87f257a2018-11-27 16:26:07 -0800284 public void prepareForAuthentication(boolean requireConfirmation, IBinder token, long opId,
Kevin Chyn23289ef2018-11-28 16:32:36 -0800285 int groupId, IBiometricServiceReceiverInternal wrapperReceiver,
286 String opPackageName, int cookie, int callingUid, int callingPid,
287 int callingUserId) {
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700288 checkPermission(USE_BIOMETRIC_INTERNAL);
Kevin Chyn41a80902019-02-06 08:12:15 -0800289 updateActiveGroup(groupId, opPackageName);
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700290 final boolean restricted = true; // BiometricPrompt is always restricted
Kevin Chyn8b7a0372018-09-17 15:06:05 -0700291 final AuthenticationClientImpl client = new FaceAuthClient(getContext(),
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700292 mDaemonWrapper, mHalDeviceId, token,
Kevin Chyn87f257a2018-11-27 16:26:07 -0800293 new BiometricPromptServiceListenerImpl(wrapperReceiver),
294 mCurrentUserId, 0 /* groupId */, opId, restricted, opPackageName, cookie,
Kevin Chyn158fefb2019-01-03 18:59:05 -0800295 requireConfirmation);
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700296 authenticateInternal(client, opId, opPackageName, callingUid, callingPid,
297 callingUserId);
298 }
299
300 @Override // Binder call
Kevin Chyn87f257a2018-11-27 16:26:07 -0800301 public void startPreparedClient(int cookie) {
302 checkPermission(MANAGE_BIOMETRIC);
303 startCurrentClient(cookie);
304 }
305
306 @Override // Binder call
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200307 public void cancelAuthentication(final IBinder token, final String opPackageName) {
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700308 checkPermission(USE_BIOMETRIC_INTERNAL);
Kevin Chyna56dff72018-06-19 18:41:12 -0700309 cancelAuthenticationInternal(token, opPackageName);
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200310 }
311
312 @Override // Binder call
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700313 public void cancelAuthenticationFromService(final IBinder token, final String opPackageName,
Kevin Chyne92cdae2018-11-21 16:35:04 -0800314 int callingUid, int callingPid, int callingUserId, boolean fromClient) {
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700315 checkPermission(USE_BIOMETRIC_INTERNAL);
Kevin Chyne92cdae2018-11-21 16:35:04 -0800316 cancelAuthenticationInternal(token, opPackageName, callingUid, callingPid,
317 callingUserId, fromClient);
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700318 }
319
320 @Override // Binder call
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200321 public void setActiveUser(final int userId) {
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700322 checkPermission(MANAGE_BIOMETRIC);
Kevin Chyna56dff72018-06-19 18:41:12 -0700323 setActiveUserInternal(userId);
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200324 }
325
326 @Override // Binder call
Kevin Chyna56dff72018-06-19 18:41:12 -0700327 public void remove(final IBinder token, final int faceId, final int userId,
328 final IFaceServiceReceiver receiver) {
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700329 checkPermission(MANAGE_BIOMETRIC);
Kevin Chyna56dff72018-06-19 18:41:12 -0700330
331 if (token == null) {
332 Slog.w(TAG, "remove(): token is null");
333 return;
334 }
335
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200336 final boolean restricted = isRestricted();
Kevin Chyn4cc49f72019-04-24 13:53:35 -0700337 final RemovalClient client = new RemovalClient(getContext(), getConstants(),
Kevin Chyn6737c572019-02-08 16:10:54 -0800338 mDaemonWrapper, mHalDeviceId, token, new ServiceListenerImpl(receiver), faceId,
339 0 /* groupId */, userId, restricted, token.toString(), getBiometricUtils()) {
Kevin Chyn7782d142019-01-18 12:51:33 -0800340 @Override
341 protected int statsModality() {
342 return FaceService.this.statsModality();
343 }
344 };
Kevin Chyna56dff72018-06-19 18:41:12 -0700345 removeInternal(client);
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200346 }
347
Kevin Chyna56dff72018-06-19 18:41:12 -0700348 @Override
349 public void enumerate(final IBinder token, final int userId,
350 final IFaceServiceReceiver receiver) {
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700351 checkPermission(MANAGE_BIOMETRIC);
Kevin Chyna56dff72018-06-19 18:41:12 -0700352
353 final boolean restricted = isRestricted();
Kevin Chyn4cc49f72019-04-24 13:53:35 -0700354 final EnumerateClient client = new EnumerateClient(getContext(), getConstants(),
Kevin Chyn6737c572019-02-08 16:10:54 -0800355 mDaemonWrapper, mHalDeviceId, token, new ServiceListenerImpl(receiver), userId,
356 userId, restricted, getContext().getOpPackageName()) {
Kevin Chyn7782d142019-01-18 12:51:33 -0800357 @Override
358 protected int statsModality() {
359 return FaceService.this.statsModality();
360 }
361 };
Kevin Chyna56dff72018-06-19 18:41:12 -0700362 enumerateInternal(client);
363 }
364
365 @Override
366 public void addLockoutResetCallback(final IBiometricServiceLockoutResetCallback callback)
367 throws RemoteException {
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700368 checkPermission(USE_BIOMETRIC_INTERNAL);
Kevin Chyna56dff72018-06-19 18:41:12 -0700369 FaceService.super.addLockoutResetCallback(callback);
370 }
371
372 @Override // Binder call
373 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
374 if (!DumpUtils.checkDumpPermission(getContext(), TAG, pw)) {
375 return;
376 }
377
378 final long ident = Binder.clearCallingIdentity();
379 try {
Joe Onoratodb396002019-04-05 19:49:27 -0700380 if (args.length > 1 && "--hal".equals(args[0])) {
381 dumpHal(fd, Arrays.copyOfRange(args, 1, args.length, args.getClass()));
Joe Onoratobf955d22019-03-25 00:16:58 -0700382 } else if (args.length > 0 && "--proto".equals(args[0])) {
Kevin Chyna56dff72018-06-19 18:41:12 -0700383 dumpProto(fd);
384 } else {
385 dumpInternal(pw);
386 }
387 } finally {
388 Binder.restoreCallingIdentity(ident);
389 }
390 }
391
392 /**
393 * The following methods don't use any common code from BiometricService
394 */
395
396 // TODO: refactor out common code here
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200397 @Override // Binder call
398 public boolean isHardwareDetected(long deviceId, String opPackageName) {
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700399 checkPermission(USE_BIOMETRIC_INTERNAL);
Kevin Chyna56dff72018-06-19 18:41:12 -0700400 if (!canUseBiometric(opPackageName, false /* foregroundOnly */,
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200401 Binder.getCallingUid(), Binder.getCallingPid(),
402 UserHandle.getCallingUserId())) {
403 return false;
404 }
405
406 final long token = Binder.clearCallingIdentity();
407 try {
408 IBiometricsFace daemon = getFaceDaemon();
409 return daemon != null && mHalDeviceId != 0;
410 } finally {
411 Binder.restoreCallingIdentity(token);
412 }
413 }
414
415 @Override // Binder call
Kevin Chyna56dff72018-06-19 18:41:12 -0700416 public void rename(final int faceId, final String name) {
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700417 checkPermission(MANAGE_BIOMETRIC);
Kevin Chyna56dff72018-06-19 18:41:12 -0700418 if (!isCurrentUserOrProfile(UserHandle.getCallingUserId())) {
419 return;
420 }
421 mHandler.post(new Runnable() {
422 @Override
423 public void run() {
424 getBiometricUtils().renameBiometricForUser(getContext(), mCurrentUserId,
425 faceId, name);
426 }
427 });
428 }
429
430 @Override // Binder call
431 public List<Face> getEnrolledFaces(int userId, String opPackageName) {
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700432 checkPermission(MANAGE_BIOMETRIC);
Kevin Chyna56dff72018-06-19 18:41:12 -0700433 if (!canUseBiometric(opPackageName, false /* foregroundOnly */,
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200434 Binder.getCallingUid(), Binder.getCallingPid(),
435 UserHandle.getCallingUserId())) {
436 return null;
437 }
438
Kevin Chyn6737c572019-02-08 16:10:54 -0800439 return FaceService.this.getEnrolledTemplates(userId);
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200440 }
441
442 @Override // Binder call
Kevin Chyna56dff72018-06-19 18:41:12 -0700443 public boolean hasEnrolledFaces(int userId, String opPackageName) {
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700444 checkPermission(USE_BIOMETRIC_INTERNAL);
Kevin Chyna56dff72018-06-19 18:41:12 -0700445 if (!canUseBiometric(opPackageName, false /* foregroundOnly */,
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200446 Binder.getCallingUid(), Binder.getCallingPid(),
447 UserHandle.getCallingUserId())) {
448 return false;
449 }
450
Kevin Chyna56dff72018-06-19 18:41:12 -0700451 return FaceService.this.hasEnrolledBiometrics(userId);
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200452 }
453
454 @Override // Binder call
455 public long getAuthenticatorId(String opPackageName) {
456 // In this method, we're not checking whether the caller is permitted to use face
457 // API because current authenticator ID is leaked (in a more contrived way) via Android
458 // Keystore (android.security.keystore package): the user of that API can create a key
459 // which requires face authentication for its use, and then query the key's
460 // characteristics (hidden API) which returns, among other things, face
461 // authenticator ID which was active at key creation time.
462 //
463 // Reason: The part of Android Keystore which runs inside an app's process invokes this
464 // method in certain cases. Those cases are not always where the developer demonstrates
465 // explicit intent to use face functionality. Thus, to avoiding throwing an
466 // unexpected SecurityException this method does not check whether its caller is
467 // permitted to use face API.
468 //
469 // The permission check should be restored once Android Keystore no longer invokes this
470 // method from inside app processes.
471
472 return FaceService.this.getAuthenticatorId(opPackageName);
473 }
474
475 @Override // Binder call
Kevin Chyna38653c2019-02-11 17:46:21 -0800476 public void resetLockout(byte[] token) {
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700477 checkPermission(MANAGE_BIOMETRIC);
Kevin Chyn1d6a2862019-04-02 16:20:21 -0700478
479 if (!FaceService.this.hasEnrolledBiometrics(mCurrentUserId)) {
480 Slog.w(TAG, "Ignoring lockout reset, no templates enrolled");
481 return;
482 }
483
Kevin Chynbe67ce02019-06-10 16:14:22 -0700484 Slog.d(TAG, "Resetting lockout for user: " + mCurrentUserId);
485
Kevin Chyna38653c2019-02-11 17:46:21 -0800486 try {
487 mDaemonWrapper.resetLockout(token);
488 } catch (RemoteException e) {
489 Slog.e(getTag(), "Unable to reset lockout", e);
490 }
Kevin Chyna56dff72018-06-19 18:41:12 -0700491 }
Kevin Chynd79e24e2018-09-25 12:06:59 -0700492
493 @Override
Kevin Chyne62749a2019-04-02 19:33:56 -0700494 public void setFeature(int feature, boolean enabled, final byte[] token,
495 IFaceServiceReceiver receiver) {
Kevin Chynd79e24e2018-09-25 12:06:59 -0700496 checkPermission(MANAGE_BIOMETRIC);
497
Kevin Chyne62749a2019-04-02 19:33:56 -0700498 mHandler.post(() -> {
499 if (!FaceService.this.hasEnrolledBiometrics(mCurrentUserId)) {
500 Slog.e(TAG, "No enrolled biometrics while setting feature: " + feature);
501 return;
Kevin Chynb95f1522019-03-04 16:45:15 -0800502 }
Kevin Chyne62749a2019-04-02 19:33:56 -0700503
504 final ArrayList<Byte> byteToken = new ArrayList<>();
505 for (int i = 0; i < token.length; i++) {
506 byteToken.add(token[i]);
507 }
508
509 // TODO: Support multiple faces
510 final int faceId = getFirstTemplateForUser(mCurrentUserId);
511
512 if (mDaemon != null) {
513 try {
514 final int result = mDaemon.setFeature(feature, enabled, byteToken, faceId);
515 receiver.onFeatureSet(result == Status.OK, feature);
516 } catch (RemoteException e) {
517 Slog.e(getTag(), "Unable to set feature: " + feature
518 + " to enabled:" + enabled, e);
519 }
520 }
521 });
522
Kevin Chynd79e24e2018-09-25 12:06:59 -0700523 }
524
525 @Override
Kevin Chyne62749a2019-04-02 19:33:56 -0700526 public void getFeature(int feature, IFaceServiceReceiver receiver) {
Kevin Chynd79e24e2018-09-25 12:06:59 -0700527 checkPermission(MANAGE_BIOMETRIC);
528
Kevin Chyne62749a2019-04-02 19:33:56 -0700529 mHandler.post(() -> {
530 // This should ideally return tri-state, but the user isn't shown settings unless
531 // they are enrolled so it's fine for now.
532 if (!FaceService.this.hasEnrolledBiometrics(mCurrentUserId)) {
533 Slog.e(TAG, "No enrolled biometrics while getting feature: " + feature);
534 return;
Kevin Chynb95f1522019-03-04 16:45:15 -0800535 }
Kevin Chyne62749a2019-04-02 19:33:56 -0700536
537 // TODO: Support multiple faces
538 final int faceId = getFirstTemplateForUser(mCurrentUserId);
539
540 if (mDaemon != null) {
541 try {
542 OptionalBool result = mDaemon.getFeature(feature, faceId);
543 receiver.onFeatureGet(result.status == Status.OK, feature, result.value);
544 } catch (RemoteException e) {
545 Slog.e(getTag(), "Unable to getRequireAttention", e);
546 }
547 }
548 });
549
Kevin Chynd79e24e2018-09-25 12:06:59 -0700550 }
Kevin Chyn57f119b2018-10-25 12:03:41 -0700551
552 @Override
553 public void userActivity() {
554 checkPermission(MANAGE_BIOMETRIC);
555
556 if (mDaemon != null) {
557 try {
558 mDaemon.userActivity();
559 } catch (RemoteException e) {
560 Slog.e(getTag(), "Unable to send userActivity", e);
561 }
562 }
563 }
Kevin Chynb95f1522019-03-04 16:45:15 -0800564
565 // TODO: Support multiple faces
566 private int getFirstTemplateForUser(int user) {
567 final List<Face> faces = FaceService.this.getEnrolledTemplates(user);
568 if (!faces.isEmpty()) {
569 return faces.get(0).getBiometricId();
570 }
571 return 0;
572 }
Kevin Chyna56dff72018-06-19 18:41:12 -0700573 }
574
575 /**
576 * Receives callbacks from the ClientMonitor implementations. The results are forwarded to
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700577 * BiometricPrompt.
578 */
Kevin Chyne92cdae2018-11-21 16:35:04 -0800579 private class BiometricPromptServiceListenerImpl extends BiometricServiceListener {
Kevin Chyn23289ef2018-11-28 16:32:36 -0800580 BiometricPromptServiceListenerImpl(IBiometricServiceReceiverInternal wrapperReceiver) {
Kevin Chyn87f257a2018-11-27 16:26:07 -0800581 super(wrapperReceiver);
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700582 }
583
584 @Override
585 public void onAcquired(long deviceId, int acquiredInfo, int vendorCode)
586 throws RemoteException {
587 /**
588 * Map the acquired codes onto existing {@link BiometricConstants} acquired codes.
589 */
Kevin Chyne92cdae2018-11-21 16:35:04 -0800590 if (getWrapperReceiver() != null) {
591 getWrapperReceiver().onAcquired(
Kevin Chyn8b7a0372018-09-17 15:06:05 -0700592 FaceManager.getMappedAcquiredInfo(acquiredInfo, vendorCode),
593 FaceManager.getAcquiredString(getContext(), acquiredInfo, vendorCode));
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700594 }
595 }
596
597 @Override
Kevin Chyn87f257a2018-11-27 16:26:07 -0800598 public void onError(long deviceId, int error, int vendorCode, int cookie)
599 throws RemoteException {
Kevin Chyne92cdae2018-11-21 16:35:04 -0800600 if (getWrapperReceiver() != null) {
Kevin Chyn23289ef2018-11-28 16:32:36 -0800601 getWrapperReceiver().onError(cookie, error,
602 FaceManager.getErrorString(getContext(), error, vendorCode));
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700603 }
604 }
605 }
606
607 /**
608 * Receives callbacks from the ClientMonitor implementations. The results are forwarded to
Kevin Chyna56dff72018-06-19 18:41:12 -0700609 * the FaceManager.
610 */
611 private class ServiceListenerImpl implements ServiceListener {
Kevin Chyna56dff72018-06-19 18:41:12 -0700612 private IFaceServiceReceiver mFaceServiceReceiver;
613
614 public ServiceListenerImpl(IFaceServiceReceiver receiver) {
615 mFaceServiceReceiver = receiver;
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200616 }
617
618 @Override
Kevin Chyna56dff72018-06-19 18:41:12 -0700619 public void onEnrollResult(BiometricAuthenticator.Identifier identifier, int remaining)
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200620 throws RemoteException {
Kevin Chyna56dff72018-06-19 18:41:12 -0700621 if (mFaceServiceReceiver != null) {
622 mFaceServiceReceiver.onEnrollResult(identifier.getDeviceId(),
623 identifier.getBiometricId(),
624 remaining);
625 }
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200626 }
Kevin Chyna56dff72018-06-19 18:41:12 -0700627
628 @Override
629 public void onAcquired(long deviceId, int acquiredInfo, int vendorCode)
630 throws RemoteException {
631 if (mFaceServiceReceiver != null) {
632 mFaceServiceReceiver.onAcquired(deviceId, acquiredInfo, vendorCode);
633 }
634 }
635
636 @Override
637 public void onAuthenticationSucceeded(long deviceId,
638 BiometricAuthenticator.Identifier biometric, int userId)
639 throws RemoteException {
640 if (mFaceServiceReceiver != null) {
Kevin Chyn628b7182018-11-13 12:00:48 -0800641 if (biometric == null || biometric instanceof Face) {
Kevin Chyn8d2694a2019-04-11 18:30:40 -0700642 mFaceServiceReceiver.onAuthenticationSucceeded(deviceId, (Face) biometric,
643 userId);
Kevin Chyna56dff72018-06-19 18:41:12 -0700644 } else {
645 Slog.e(TAG, "onAuthenticationSucceeded received non-face biometric");
646 }
647 }
648 }
649
650 @Override
651 public void onAuthenticationFailed(long deviceId) throws RemoteException {
652 if (mFaceServiceReceiver != null) {
653 mFaceServiceReceiver.onAuthenticationFailed(deviceId);
654 }
655 }
656
657 @Override
Kevin Chyn87f257a2018-11-27 16:26:07 -0800658 public void onError(long deviceId, int error, int vendorCode, int cookie)
659 throws RemoteException {
Kevin Chyna56dff72018-06-19 18:41:12 -0700660 if (mFaceServiceReceiver != null) {
661 mFaceServiceReceiver.onError(deviceId, error, vendorCode);
662 }
663 }
664
665 @Override
666 public void onRemoved(BiometricAuthenticator.Identifier identifier,
667 int remaining) throws RemoteException {
668 if (mFaceServiceReceiver != null) {
669 mFaceServiceReceiver.onRemoved(identifier.getDeviceId(),
670 identifier.getBiometricId(), remaining);
671 }
672 }
673
674 @Override
675 public void onEnumerated(BiometricAuthenticator.Identifier identifier, int remaining)
676 throws RemoteException {
677 if (mFaceServiceReceiver != null) {
Kevin Chyn6737c572019-02-08 16:10:54 -0800678 mFaceServiceReceiver.onEnumerated(identifier.getDeviceId(),
679 identifier.getBiometricId(), remaining);
Kevin Chyna56dff72018-06-19 18:41:12 -0700680 }
681 }
682 }
683
Kevin Chyn4cc49f72019-04-24 13:53:35 -0700684 private final FaceConstants mFaceConstants = new FaceConstants();
Kevin Chyna56dff72018-06-19 18:41:12 -0700685
686 @GuardedBy("this")
687 private IBiometricsFace mDaemon;
Kevin Chyna38653c2019-02-11 17:46:21 -0800688 // One of the AuthenticationClient constants
689 private int mCurrentUserLockoutMode;
Kevin Chyna56dff72018-06-19 18:41:12 -0700690
Kevin Chyn4cc49f72019-04-24 13:53:35 -0700691 private int[] mBiometricPromptIgnoreList;
692 private int[] mBiometricPromptIgnoreListVendor;
693 private int[] mKeyguardIgnoreList;
694 private int[] mKeyguardIgnoreListVendor;
695 private int[] mEnrollIgnoreList;
696 private int[] mEnrollIgnoreListVendor;
697
Kevin Chyna56dff72018-06-19 18:41:12 -0700698 /**
699 * Receives callbacks from the HAL.
700 */
701 private IBiometricsFaceClientCallback mDaemonCallback =
702 new IBiometricsFaceClientCallback.Stub() {
Kevin Chyn6737c572019-02-08 16:10:54 -0800703 @Override
704 public void onEnrollResult(final long deviceId, int faceId, int userId,
705 int remaining) {
706 mHandler.post(() -> {
707 final Face face = new Face(getBiometricUtils()
708 .getUniqueName(getContext(), userId), faceId, deviceId);
709 FaceService.super.handleEnrollResult(face, remaining);
Kevin Chyn576811e2019-06-12 14:50:35 -0700710
711 // Enrollment changes the authenticatorId, so update it here.
712 IBiometricsFace daemon = getFaceDaemon();
713 if (remaining == 0 && daemon != null) {
714 try {
715 mAuthenticatorIds.put(userId,
716 hasEnrolledBiometrics(userId) ? daemon.getAuthenticatorId().value
717 : 0L);
718 } catch (RemoteException e) {
719 Slog.e(TAG, "Unable to get authenticatorId", e);
720 }
721 }
Kevin Chyn6737c572019-02-08 16:10:54 -0800722 });
723 }
724
725 @Override
726 public void onAcquired(final long deviceId, final int userId,
727 final int acquiredInfo,
728 final int vendorCode) {
729 mHandler.post(() -> {
730 FaceService.super.handleAcquired(deviceId, acquiredInfo, vendorCode);
731 });
732 }
733
734 @Override
735 public void onAuthenticated(final long deviceId, final int faceId, final int userId,
736 ArrayList<Byte> token) {
737 mHandler.post(() -> {
738 Face face = new Face("", faceId, deviceId);
739 FaceService.super.handleAuthenticated(face, token);
740 });
741 }
742
743 @Override
744 public void onError(final long deviceId, final int userId, final int error,
745 final int vendorCode) {
746 mHandler.post(() -> {
747 FaceService.super.handleError(deviceId, error, vendorCode);
748
749 // TODO: this chunk of code should be common to all biometric services
750 if (error == BiometricConstants.BIOMETRIC_ERROR_HW_UNAVAILABLE) {
751 // If we get HW_UNAVAILABLE, try to connect again later...
752 Slog.w(TAG, "Got ERROR_HW_UNAVAILABLE; try reconnecting next client.");
753 synchronized (this) {
754 mDaemon = null;
755 mHalDeviceId = 0;
756 mCurrentUserId = UserHandle.USER_NULL;
757 }
Kevin Chyna56dff72018-06-19 18:41:12 -0700758 }
Kevin Chyn6737c572019-02-08 16:10:54 -0800759 });
760 }
Kevin Chyna56dff72018-06-19 18:41:12 -0700761
Kevin Chyn6737c572019-02-08 16:10:54 -0800762 @Override
Kevin Chyn31ae8d22019-03-11 14:12:32 -0700763 public void onRemoved(final long deviceId, ArrayList<Integer> faceIds, final int userId) {
Kevin Chyn6737c572019-02-08 16:10:54 -0800764 mHandler.post(() -> {
Kevin Chyn31ae8d22019-03-11 14:12:32 -0700765 if (!faceIds.isEmpty()) {
766 for (int i = 0; i < faceIds.size(); i++) {
767 final Face face = new Face("", faceIds.get(i), deviceId);
768 // Convert to old behavior
769 FaceService.super.handleRemoved(face, faceIds.size() - i - 1);
770 }
771 } else {
772 final Face face = new Face("", 0 /* identifier */, deviceId);
773 FaceService.super.handleRemoved(face, 0 /* remaining */);
774 }
775
Kevin Chyn6737c572019-02-08 16:10:54 -0800776 });
777 }
778
779 @Override
780 public void onEnumerate(long deviceId, ArrayList<Integer> faceIds, int userId)
781 throws RemoteException {
782 mHandler.post(() -> {
783 if (!faceIds.isEmpty()) {
784 for (int i = 0; i < faceIds.size(); i++) {
785 final Face face = new Face("", faceIds.get(i), deviceId);
786 // Convert to old old behavior
787 FaceService.super.handleEnumerate(face, faceIds.size() - i - 1);
788 }
789 } else {
790 // For face, the HIDL contract is to receive an empty list when there are no
791 // templates enrolled. Send a null identifier since we don't consume them
792 // anywhere, and send remaining == 0 to plumb this with existing common code.
793 FaceService.super.handleEnumerate(null /* identifier */, 0);
Kevin Chyna56dff72018-06-19 18:41:12 -0700794 }
Kevin Chyn6737c572019-02-08 16:10:54 -0800795 });
796 }
Kevin Chyna56dff72018-06-19 18:41:12 -0700797
Kevin Chyn6737c572019-02-08 16:10:54 -0800798 @Override
799 public void onLockoutChanged(long duration) {
Kevin Chyna38653c2019-02-11 17:46:21 -0800800 Slog.d(TAG, "onLockoutChanged: " + duration);
801 if (duration == 0) {
802 mCurrentUserLockoutMode = AuthenticationClient.LOCKOUT_NONE;
803 } else if (duration == Long.MAX_VALUE) {
804 mCurrentUserLockoutMode = AuthenticationClient.LOCKOUT_PERMANENT;
805 } else {
806 mCurrentUserLockoutMode = AuthenticationClient.LOCKOUT_TIMED;
807 }
Kevin Chyna56dff72018-06-19 18:41:12 -0700808
Kevin Chyna38653c2019-02-11 17:46:21 -0800809 mHandler.post(() -> {
810 if (duration == 0) {
811 notifyLockoutResetMonitors();
812 }
813 });
Kevin Chyn6737c572019-02-08 16:10:54 -0800814 }
815 };
Kevin Chyna56dff72018-06-19 18:41:12 -0700816
817 /**
818 * Wraps the HAL-specific code and is passed to the ClientMonitor implementations so that they
819 * can be shared between the multiple biometric services.
820 */
821 private final DaemonWrapper mDaemonWrapper = new DaemonWrapper() {
822 @Override
823 public int authenticate(long operationId, int groupId) throws RemoteException {
824 IBiometricsFace daemon = getFaceDaemon();
825 if (daemon == null) {
826 Slog.w(TAG, "authenticate(): no face HAL!");
827 return ERROR_ESRCH;
828 }
829 return daemon.authenticate(operationId);
830 }
831
832 @Override
833 public int cancel() throws RemoteException {
834 IBiometricsFace daemon = getFaceDaemon();
835 if (daemon == null) {
836 Slog.w(TAG, "cancel(): no face HAL!");
837 return ERROR_ESRCH;
838 }
839 return daemon.cancel();
840 }
841
842 @Override
843 public int remove(int groupId, int biometricId) throws RemoteException {
844 IBiometricsFace daemon = getFaceDaemon();
845 if (daemon == null) {
846 Slog.w(TAG, "remove(): no face HAL!");
847 return ERROR_ESRCH;
848 }
849 return daemon.remove(biometricId);
850 }
851
852 @Override
853 public int enumerate() throws RemoteException {
854 IBiometricsFace daemon = getFaceDaemon();
855 if (daemon == null) {
856 Slog.w(TAG, "enumerate(): no face HAL!");
857 return ERROR_ESRCH;
858 }
859 return daemon.enumerate();
860 }
861
862 @Override
Kevin Chyn1f16c2d2018-12-07 13:06:08 -0800863 public int enroll(byte[] cryptoToken, int groupId, int timeout,
864 ArrayList<Integer> disabledFeatures) throws RemoteException {
Kevin Chyna56dff72018-06-19 18:41:12 -0700865 IBiometricsFace daemon = getFaceDaemon();
866 if (daemon == null) {
867 Slog.w(TAG, "enroll(): no face HAL!");
868 return ERROR_ESRCH;
869 }
870 final ArrayList<Byte> token = new ArrayList<>();
871 for (int i = 0; i < cryptoToken.length; i++) {
872 token.add(cryptoToken[i]);
873 }
Kevin Chyn1f16c2d2018-12-07 13:06:08 -0800874 return daemon.enroll(token, timeout, disabledFeatures);
Kevin Chyna56dff72018-06-19 18:41:12 -0700875 }
Kevin Chyna38653c2019-02-11 17:46:21 -0800876
877 @Override
878 public void resetLockout(byte[] cryptoToken) throws RemoteException {
879 IBiometricsFace daemon = getFaceDaemon();
880 if (daemon == null) {
881 Slog.w(TAG, "resetLockout(): no face HAL!");
882 return;
883 }
884 final ArrayList<Byte> token = new ArrayList<>();
885 for (int i = 0; i < cryptoToken.length; i++) {
886 token.add(cryptoToken[i]);
887 }
888 daemon.resetLockout(token);
889 }
Kevin Chyna56dff72018-06-19 18:41:12 -0700890 };
891
892
893 public FaceService(Context context) {
894 super(context);
Kevin Chyn4cc49f72019-04-24 13:53:35 -0700895
896 mBiometricPromptIgnoreList = getContext().getResources()
897 .getIntArray(R.array.config_face_acquire_biometricprompt_ignorelist);
898 mBiometricPromptIgnoreListVendor = getContext().getResources()
899 .getIntArray(R.array.config_face_acquire_vendor_biometricprompt_ignorelist);
900 mKeyguardIgnoreList = getContext().getResources()
901 .getIntArray(R.array.config_face_acquire_keyguard_ignorelist);
902 mKeyguardIgnoreListVendor = getContext().getResources()
903 .getIntArray(R.array.config_face_acquire_vendor_keyguard_ignorelist);
904 mEnrollIgnoreList = getContext().getResources()
905 .getIntArray(R.array.config_face_acquire_enroll_ignorelist);
906 mEnrollIgnoreListVendor = getContext().getResources()
907 .getIntArray(R.array.config_face_acquire_vendor_enroll_ignorelist);
Kevin Chyna56dff72018-06-19 18:41:12 -0700908 }
909
910 @Override
911 public void onStart() {
Kevin Chyn5a2ff5d2018-08-29 19:07:30 -0700912 super.onStart();
Kevin Chyna56dff72018-06-19 18:41:12 -0700913 publishBinderService(Context.FACE_SERVICE, new FaceServiceWrapper());
914 SystemServerInitThreadPool.get().submit(this::getFaceDaemon, TAG + ".onStart");
915 }
916
917 @Override
918 public String getTag() {
919 return TAG;
920 }
921
922 @Override
Kevin Chyn6737c572019-02-08 16:10:54 -0800923 protected DaemonWrapper getDaemonWrapper() {
924 return mDaemonWrapper;
925 }
926
927 @Override
Kevin Chyna56dff72018-06-19 18:41:12 -0700928 protected BiometricUtils getBiometricUtils() {
929 return FaceUtils.getInstance();
930 }
931
932 @Override
Kevin Chyn4cc49f72019-04-24 13:53:35 -0700933 protected Constants getConstants() {
934 return mFaceConstants;
Kevin Chyna56dff72018-06-19 18:41:12 -0700935 }
936
937 @Override
938 protected boolean hasReachedEnrollmentLimit(int userId) {
939 final int limit = getContext().getResources().getInteger(
Kevin Chyn017e76e2018-06-27 18:35:06 -0700940 com.android.internal.R.integer.config_faceMaxTemplatesPerUser);
Kevin Chyn6737c572019-02-08 16:10:54 -0800941 final int enrolled = FaceService.this.getEnrolledTemplates(userId).size();
Kevin Chyna56dff72018-06-19 18:41:12 -0700942 if (enrolled >= limit) {
Kevin Chyn1a878c12019-04-04 15:50:11 -0700943 Slog.w(TAG, "Too many faces registered, user: " + userId);
Kevin Chyna56dff72018-06-19 18:41:12 -0700944 return true;
945 }
946 return false;
947 }
948
949 @Override
Kevin Chyn9ba99912019-01-16 16:24:36 -0800950 public void serviceDied(long cookie) {
951 super.serviceDied(cookie);
952 mDaemon = null;
Jim Miller7b78b222019-02-07 16:47:38 -0800953
954 mCurrentUserId = UserHandle.USER_NULL; // Force updateActiveGroup() to re-evaluate
Kevin Chyn9ba99912019-01-16 16:24:36 -0800955 }
956
957 @Override
Kevin Chyna56dff72018-06-19 18:41:12 -0700958 protected void updateActiveGroup(int userId, String clientPackage) {
959 IBiometricsFace daemon = getFaceDaemon();
960
961 if (daemon != null) {
962 try {
963 userId = getUserOrWorkProfileId(clientPackage, userId);
964 if (userId != mCurrentUserId) {
Kevin Chyn27e33d02019-04-30 01:37:32 +0000965 final File baseDir = Environment.getDataVendorDeDirectory(userId);
Kevin Chyna56dff72018-06-19 18:41:12 -0700966 final File faceDir = new File(baseDir, FACE_DATA_DIR);
967 if (!faceDir.exists()) {
968 if (!faceDir.mkdir()) {
969 Slog.v(TAG, "Cannot make directory: " + faceDir.getAbsolutePath());
970 return;
971 }
972 // Calling mkdir() from this process will create a directory with our
973 // permissions (inherited from the containing dir). This command fixes
974 // the label.
975 if (!SELinux.restorecon(faceDir)) {
976 Slog.w(TAG, "Restorecons failed. Directory will have wrong label.");
977 return;
978 }
979 }
980
981 daemon.setActiveUser(userId, faceDir.getAbsolutePath());
982 mCurrentUserId = userId;
Kevin Chyn576811e2019-06-12 14:50:35 -0700983 mAuthenticatorIds.put(userId,
984 hasEnrolledBiometrics(userId) ? daemon.getAuthenticatorId().value : 0L);
Kevin Chyna56dff72018-06-19 18:41:12 -0700985 }
Kevin Chyna56dff72018-06-19 18:41:12 -0700986 } catch (RemoteException e) {
987 Slog.e(TAG, "Failed to setActiveUser():", e);
988 }
989 }
990 }
991
992 @Override
993 protected String getLockoutResetIntent() {
994 return ACTION_LOCKOUT_RESET;
995 }
996
997 @Override
998 protected String getLockoutBroadcastPermission() {
999 return RESET_FACE_LOCKOUT;
1000 }
1001
1002 @Override
1003 protected long getHalDeviceId() {
1004 return mHalDeviceId;
1005 }
1006
1007 @Override
Kevin Chyna38653c2019-02-11 17:46:21 -08001008 protected void handleUserSwitching(int userId) {
1009 super.handleUserSwitching(userId);
1010 // Will be updated when we get the callback from HAL
1011 mCurrentUserLockoutMode = AuthenticationClient.LOCKOUT_NONE;
1012 }
1013
1014 @Override
Kevin Chyna56dff72018-06-19 18:41:12 -07001015 protected boolean hasEnrolledBiometrics(int userId) {
1016 if (userId != UserHandle.getCallingUserId()) {
1017 checkPermission(INTERACT_ACROSS_USERS);
1018 }
1019 return getBiometricUtils().getBiometricsForUser(getContext(), userId).size() > 0;
1020 }
1021
1022 @Override
1023 protected String getManageBiometricPermission() {
Kevin Chyna24e9fd2018-08-27 12:39:17 -07001024 return MANAGE_BIOMETRIC;
Kevin Chyna56dff72018-06-19 18:41:12 -07001025 }
1026
1027 @Override
1028 protected void checkUseBiometricPermission() {
Kevin Chyna24e9fd2018-08-27 12:39:17 -07001029 // noop for Face. The permission checks are all done on the incoming binder call.
Kevin Chyna56dff72018-06-19 18:41:12 -07001030 }
1031
1032 @Override
Kevin Chynb3c05aa2018-09-21 16:50:32 -07001033 protected boolean checkAppOps(int uid, String opPackageName) {
1034 return mAppOps.noteOp(AppOpsManager.OP_USE_BIOMETRIC, uid, opPackageName)
1035 == AppOpsManager.MODE_ALLOWED;
Kevin Chyna56dff72018-06-19 18:41:12 -07001036 }
1037
1038 @Override
Kevin Chyn6737c572019-02-08 16:10:54 -08001039 protected List<Face> getEnrolledTemplates(int userId) {
1040 return getBiometricUtils().getBiometricsForUser(getContext(), userId);
1041 }
1042
1043 @Override
Kevin Chyna56dff72018-06-19 18:41:12 -07001044 protected void notifyClientActiveCallbacks(boolean isActive) {
1045 // noop for Face.
1046 }
1047
Kevin Chyn7782d142019-01-18 12:51:33 -08001048 @Override
1049 protected int statsModality() {
1050 return BiometricsProtoEnums.MODALITY_FACE;
1051 }
1052
Kevin Chyna38653c2019-02-11 17:46:21 -08001053 @Override
1054 protected int getLockoutMode() {
1055 return mCurrentUserLockoutMode;
1056 }
1057
Kevin Chyna56dff72018-06-19 18:41:12 -07001058 /** Gets the face daemon */
1059 private synchronized IBiometricsFace getFaceDaemon() {
1060 if (mDaemon == null) {
1061 Slog.v(TAG, "mDaemon was null, reconnect to face");
1062 try {
1063 mDaemon = IBiometricsFace.getService();
1064 } catch (java.util.NoSuchElementException e) {
1065 // Service doesn't exist or cannot be opened. Logged below.
1066 } catch (RemoteException e) {
1067 Slog.e(TAG, "Failed to get biometric interface", e);
1068 }
1069 if (mDaemon == null) {
1070 Slog.w(TAG, "face HIDL not available");
1071 return null;
1072 }
1073
1074 mDaemon.asBinder().linkToDeath(this, 0);
1075
1076 try {
1077 mHalDeviceId = mDaemon.setCallback(mDaemonCallback).value;
1078 } catch (RemoteException e) {
1079 Slog.e(TAG, "Failed to open face HAL", e);
1080 mDaemon = null; // try again later!
1081 }
1082
1083 if (DEBUG) Slog.v(TAG, "Face HAL id: " + mHalDeviceId);
1084 if (mHalDeviceId != 0) {
1085 loadAuthenticatorIds();
1086 updateActiveGroup(ActivityManager.getCurrentUser(), null);
Kevin Chyn6737c572019-02-08 16:10:54 -08001087 doTemplateCleanupForUser(ActivityManager.getCurrentUser());
Kevin Chyna56dff72018-06-19 18:41:12 -07001088 } else {
1089 Slog.w(TAG, "Failed to open Face HAL!");
1090 MetricsLogger.count(getContext(), "faced_openhal_error", 1);
1091 mDaemon = null;
1092 }
1093 }
1094 return mDaemon;
1095 }
1096
Kevin Chynd79e24e2018-09-25 12:06:59 -07001097 private long startGenerateChallenge(IBinder token) {
Kevin Chyna56dff72018-06-19 18:41:12 -07001098 IBiometricsFace daemon = getFaceDaemon();
1099 if (daemon == null) {
Kevin Chynd79e24e2018-09-25 12:06:59 -07001100 Slog.w(TAG, "startGenerateChallenge: no face HAL!");
Kevin Chyna56dff72018-06-19 18:41:12 -07001101 return 0;
1102 }
1103 try {
Kevin Chyne46a2162018-09-20 18:43:01 -07001104 return daemon.generateChallenge(CHALLENGE_TIMEOUT_SEC).value;
Kevin Chyna56dff72018-06-19 18:41:12 -07001105 } catch (RemoteException e) {
Kevin Chynd79e24e2018-09-25 12:06:59 -07001106 Slog.e(TAG, "startGenerateChallenge failed", e);
Kevin Chyna56dff72018-06-19 18:41:12 -07001107 }
1108 return 0;
1109 }
1110
Kevin Chynd79e24e2018-09-25 12:06:59 -07001111 private int startRevokeChallenge(IBinder token) {
Kevin Chyna56dff72018-06-19 18:41:12 -07001112 IBiometricsFace daemon = getFaceDaemon();
1113 if (daemon == null) {
Kevin Chynd79e24e2018-09-25 12:06:59 -07001114 Slog.w(TAG, "startRevokeChallenge: no face HAL!");
Kevin Chyna56dff72018-06-19 18:41:12 -07001115 return 0;
1116 }
1117 try {
Kevin Chyn96c92972018-08-31 16:09:31 -07001118 return daemon.revokeChallenge();
Kevin Chyna56dff72018-06-19 18:41:12 -07001119 } catch (RemoteException e) {
Kevin Chynd79e24e2018-09-25 12:06:59 -07001120 Slog.e(TAG, "startRevokeChallenge failed", e);
Kevin Chyna56dff72018-06-19 18:41:12 -07001121 }
1122 return 0;
1123 }
1124
Gilad Brettercb51b8b2018-03-22 17:04:51 +02001125 private void dumpInternal(PrintWriter pw) {
1126 JSONObject dump = new JSONObject();
1127 try {
1128 dump.put("service", "Face Manager");
1129
1130 JSONArray sets = new JSONArray();
1131 for (UserInfo user : UserManager.get(getContext()).getUsers()) {
1132 final int userId = user.getUserHandle().getIdentifier();
Kevin Chyna56dff72018-06-19 18:41:12 -07001133 final int N = getBiometricUtils().getBiometricsForUser(getContext(), userId).size();
Gilad Brettercb51b8b2018-03-22 17:04:51 +02001134 PerformanceStats stats = mPerformanceMap.get(userId);
1135 PerformanceStats cryptoStats = mCryptoPerformanceMap.get(userId);
1136 JSONObject set = new JSONObject();
1137 set.put("id", userId);
Kevin Chyna56dff72018-06-19 18:41:12 -07001138 set.put("count", N);
Gilad Brettercb51b8b2018-03-22 17:04:51 +02001139 set.put("accept", (stats != null) ? stats.accept : 0);
1140 set.put("reject", (stats != null) ? stats.reject : 0);
1141 set.put("acquire", (stats != null) ? stats.acquire : 0);
1142 set.put("lockout", (stats != null) ? stats.lockout : 0);
1143 set.put("permanentLockout", (stats != null) ? stats.permanentLockout : 0);
1144 // cryptoStats measures statistics about secure face transactions
1145 // (e.g. to unlock password storage, make secure purchases, etc.)
1146 set.put("acceptCrypto", (cryptoStats != null) ? cryptoStats.accept : 0);
1147 set.put("rejectCrypto", (cryptoStats != null) ? cryptoStats.reject : 0);
1148 set.put("acquireCrypto", (cryptoStats != null) ? cryptoStats.acquire : 0);
1149 set.put("lockoutCrypto", (cryptoStats != null) ? cryptoStats.lockout : 0);
Kevin Chyna56dff72018-06-19 18:41:12 -07001150 set.put("permanentLockoutCrypto",
1151 (cryptoStats != null) ? cryptoStats.permanentLockout : 0);
Gilad Brettercb51b8b2018-03-22 17:04:51 +02001152 sets.put(set);
1153 }
1154
1155 dump.put("prints", sets);
1156 } catch (JSONException e) {
1157 Slog.e(TAG, "dump formatting failure", e);
1158 }
1159 pw.println(dump);
Kevin Chyn9ba99912019-01-16 16:24:36 -08001160 pw.println("HAL Deaths: " + mHALDeathCount);
1161 mHALDeathCount = 0;
Gilad Brettercb51b8b2018-03-22 17:04:51 +02001162 }
1163
1164 private void dumpProto(FileDescriptor fd) {
1165 final ProtoOutputStream proto = new ProtoOutputStream(fd);
1166 for (UserInfo user : UserManager.get(getContext()).getUsers()) {
1167 final int userId = user.getUserHandle().getIdentifier();
1168
1169 final long userToken = proto.start(FaceServiceDumpProto.USERS);
1170
1171 proto.write(FaceUserStatsProto.USER_ID, userId);
Kevin Chyna56dff72018-06-19 18:41:12 -07001172 proto.write(FaceUserStatsProto.NUM_FACES,
1173 getBiometricUtils().getBiometricsForUser(getContext(), userId).size());
Gilad Brettercb51b8b2018-03-22 17:04:51 +02001174
1175 // Normal face authentications (e.g. lockscreen)
1176 final PerformanceStats normal = mPerformanceMap.get(userId);
1177 if (normal != null) {
1178 final long countsToken = proto.start(FaceUserStatsProto.NORMAL);
1179 proto.write(FaceActionStatsProto.ACCEPT, normal.accept);
1180 proto.write(FaceActionStatsProto.REJECT, normal.reject);
1181 proto.write(FaceActionStatsProto.ACQUIRE, normal.acquire);
1182 proto.write(FaceActionStatsProto.LOCKOUT, normal.lockout);
1183 proto.write(FaceActionStatsProto.LOCKOUT_PERMANENT, normal.lockout);
1184 proto.end(countsToken);
1185 }
1186
1187 // Statistics about secure face transactions (e.g. to unlock password
1188 // storage, make secure purchases, etc.)
1189 final PerformanceStats crypto = mCryptoPerformanceMap.get(userId);
1190 if (crypto != null) {
1191 final long countsToken = proto.start(FaceUserStatsProto.CRYPTO);
1192 proto.write(FaceActionStatsProto.ACCEPT, crypto.accept);
1193 proto.write(FaceActionStatsProto.REJECT, crypto.reject);
1194 proto.write(FaceActionStatsProto.ACQUIRE, crypto.acquire);
1195 proto.write(FaceActionStatsProto.LOCKOUT, crypto.lockout);
1196 proto.write(FaceActionStatsProto.LOCKOUT_PERMANENT, crypto.lockout);
1197 proto.end(countsToken);
1198 }
1199
1200 proto.end(userToken);
1201 }
1202 proto.flush();
Kevin Chyna56dff72018-06-19 18:41:12 -07001203 mPerformanceMap.clear();
1204 mCryptoPerformanceMap.clear();
Gilad Brettercb51b8b2018-03-22 17:04:51 +02001205 }
Joe Onoratobf955d22019-03-25 00:16:58 -07001206
Joe Onoratodb396002019-04-05 19:49:27 -07001207 private void dumpHal(FileDescriptor fd, String[] args) {
Joe Onoratobf955d22019-03-25 00:16:58 -07001208 // WARNING: CDD restricts image data from leaving TEE unencrypted on
1209 // production devices:
1210 // [C-1-10] MUST not allow unencrypted access to identifiable biometric
1211 // data or any data derived from it (such as embeddings) to the
1212 // Application Processor outside the context of the TEE.
1213 // As such, this API should only be enabled for testing purposes on
1214 // engineering and userdebug builds. All modules in the software stack
1215 // MUST enforce final build products do NOT have this functionality.
1216 // Additionally, the following check MUST NOT be removed.
1217 if (!(Build.IS_ENG || Build.IS_USERDEBUG)) {
1218 return;
1219 }
1220
Joe Onorato108413a2019-04-03 18:20:52 -07001221 // Additionally, this flag allows turning off face for a device
1222 // (either permanently through the build or on an individual device).
1223 if (SystemProperties.getBoolean("ro.face.disable_debug_data", false)
1224 || SystemProperties.getBoolean("persist.face.disable_debug_data", false)) {
1225 return;
1226 }
1227
Joe Onoratodb396002019-04-05 19:49:27 -07001228 // The debug method takes two file descriptors. The first is for text
1229 // output, which we will drop. The second is for binary data, which
1230 // will be the protobuf data.
1231 final IBiometricsFace daemon = getFaceDaemon();
1232 if (daemon != null) {
1233 FileOutputStream devnull = null;
1234 try {
1235 devnull = new FileOutputStream("/dev/null");
1236 final NativeHandle handle = new NativeHandle(
1237 new FileDescriptor[] { devnull.getFD(), fd },
1238 new int[0], false);
1239 daemon.debug(handle, new ArrayList<String>(Arrays.asList(args)));
1240 } catch (IOException | RemoteException ex) {
1241 Slog.d(TAG, "error while reading face debugging data", ex);
1242 } finally {
1243 if (devnull != null) {
1244 try {
1245 devnull.close();
1246 } catch (IOException ex) {
1247 }
1248 }
1249 }
Joe Onoratobf955d22019-03-25 00:16:58 -07001250 }
Joe Onoratobf955d22019-03-25 00:16:58 -07001251 }
Kevin Chyn8b7a0372018-09-17 15:06:05 -07001252}