blob: 2e6f3d84cdee26929d4182d6f6f8903de91e3ca7 [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 Chyndbfbed42019-06-13 17:01:30 -0700100 private static final String NOTIFICATION_TAG = "FaceService";
101 private static final int NOTIFICATION_ID = 1;
102
Kevin Chyn8b7a0372018-09-17 15:06:05 -0700103 private final class FaceAuthClient extends AuthenticationClientImpl {
Kevin Chyn0ce70852019-05-10 10:29:18 -0700104 private int mLastAcquire;
105
Kevin Chyn8b7a0372018-09-17 15:06:05 -0700106 public FaceAuthClient(Context context,
107 DaemonWrapper daemon, long halDeviceId, IBinder token,
108 ServiceListener listener, int targetUserId, int groupId, long opId,
Kevin Chyn87f257a2018-11-27 16:26:07 -0800109 boolean restricted, String owner, int cookie, boolean requireConfirmation) {
Kevin Chyn8b7a0372018-09-17 15:06:05 -0700110 super(context, daemon, halDeviceId, token, listener, targetUserId, groupId, opId,
Kevin Chyn87f257a2018-11-27 16:26:07 -0800111 restricted, owner, cookie, requireConfirmation);
Kevin Chyn8b7a0372018-09-17 15:06:05 -0700112 }
Kevin Chyn7782d142019-01-18 12:51:33 -0800113
114 @Override
115 protected int statsModality() {
116 return FaceService.this.statsModality();
117 }
Kevin Chyna38653c2019-02-11 17:46:21 -0800118
119 @Override
120 public boolean shouldFrameworkHandleLockout() {
121 return false;
122 }
Kevin Chyn56d6b072019-02-13 18:39:01 -0800123
124 @Override
Kevin Chyn0ce70852019-05-10 10:29:18 -0700125 public boolean wasUserDetected() {
126 return mLastAcquire != FaceManager.FACE_ACQUIRED_NOT_DETECTED;
127 }
128
129 @Override
Kevin Chyn56d6b072019-02-13 18:39:01 -0800130 public boolean onAuthenticated(BiometricAuthenticator.Identifier identifier,
131 boolean authenticated, ArrayList<Byte> token) {
132 final boolean result = super.onAuthenticated(identifier, authenticated, token);
133
134 // For face, the authentication lifecycle ends either when
135 // 1) Authenticated == true
136 // 2) Error occurred
137 // 3) Authenticated == false
138 // Fingerprint currently does not end when the third condition is met which is a bug,
139 // but let's leave it as-is for now.
140 return result || !authenticated;
141 }
Kevin Chyn56e4c3d2019-04-23 11:28:43 -0700142
143 @Override
Kevin Chyn4cc49f72019-04-24 13:53:35 -0700144 public int[] getAcquireIgnorelist() {
145 if (isBiometricPrompt()) {
146 return mBiometricPromptIgnoreList;
147 } else {
148 // Keyguard
149 return mKeyguardIgnoreList;
150 }
151 }
152
153 @Override
154 public int[] getAcquireVendorIgnorelist() {
155 if (isBiometricPrompt()) {
156 return mBiometricPromptIgnoreListVendor;
157 } else {
158 // Keyguard
159 return mKeyguardIgnoreListVendor;
160 }
161 }
162
163 @Override
Kevin Chyn56e4c3d2019-04-23 11:28:43 -0700164 public boolean onAcquired(int acquireInfo, int vendorCode) {
165
Kevin Chyn0ce70852019-05-10 10:29:18 -0700166 mLastAcquire = acquireInfo;
167
Kevin Chyn56e4c3d2019-04-23 11:28:43 -0700168 if (acquireInfo == FaceManager.FACE_ACQUIRED_RECALIBRATE) {
169 final String name =
170 getContext().getString(R.string.face_recalibrate_notification_name);
171 final String title =
172 getContext().getString(R.string.face_recalibrate_notification_title);
173 final String content =
174 getContext().getString(R.string.face_recalibrate_notification_content);
175
176 final Intent intent = new Intent("android.settings.FACE_SETTINGS");
177 intent.setPackage("com.android.settings");
178
179 final PendingIntent pendingIntent = PendingIntent.getActivityAsUser(getContext(),
180 0 /* requestCode */, intent, 0 /* flags */, null /* options */,
181 UserHandle.CURRENT);
182
Kevin Chyndbfbed42019-06-13 17:01:30 -0700183 final String channelName = "FaceEnrollNotificationChannel";
Kevin Chyn56e4c3d2019-04-23 11:28:43 -0700184
Kevin Chyndbfbed42019-06-13 17:01:30 -0700185 NotificationChannel channel = new NotificationChannel(channelName, name,
Kevin Chyn56e4c3d2019-04-23 11:28:43 -0700186 NotificationManager.IMPORTANCE_HIGH);
Kevin Chyndbfbed42019-06-13 17:01:30 -0700187 Notification notification = new Notification.Builder(getContext(), channelName)
Kevin Chyn56e4c3d2019-04-23 11:28:43 -0700188 .setSmallIcon(R.drawable.ic_lock)
189 .setContentTitle(title)
190 .setContentText(content)
191 .setSubText(name)
192 .setOnlyAlertOnce(true)
193 .setLocalOnly(true)
194 .setAutoCancel(true)
195 .setCategory(Notification.CATEGORY_SYSTEM)
196 .setContentIntent(pendingIntent)
Kevin Chyn0c3a9982019-06-11 19:09:27 -0700197 .setVisibility(Notification.VISIBILITY_SECRET)
Kevin Chyn56e4c3d2019-04-23 11:28:43 -0700198 .build();
199
Kevin Chyndbfbed42019-06-13 17:01:30 -0700200 mNotificationManager.createNotificationChannel(channel);
201 mNotificationManager.notifyAsUser(NOTIFICATION_TAG, NOTIFICATION_ID, notification,
202 UserHandle.CURRENT);
Kevin Chyn56e4c3d2019-04-23 11:28:43 -0700203 }
204
205 return super.onAcquired(acquireInfo, vendorCode);
206 }
Kevin Chyn8b7a0372018-09-17 15:06:05 -0700207 }
208
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200209 /**
Kevin Chyna56dff72018-06-19 18:41:12 -0700210 * Receives the incoming binder calls from FaceManager.
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200211 */
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200212 private final class FaceServiceWrapper extends IFaceService.Stub {
Kevin Chyna56dff72018-06-19 18:41:12 -0700213
214 /**
215 * The following methods contain common code which is shared in biometrics/common.
216 */
Kevin Chyna38653c2019-02-11 17:46:21 -0800217
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200218 @Override // Binder call
Kevin Chynd79e24e2018-09-25 12:06:59 -0700219 public long generateChallenge(IBinder token) {
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700220 checkPermission(MANAGE_BIOMETRIC);
Kevin Chynd79e24e2018-09-25 12:06:59 -0700221 return startGenerateChallenge(token);
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200222 }
223
224 @Override // Binder call
Kevin Chynd79e24e2018-09-25 12:06:59 -0700225 public int revokeChallenge(IBinder token) {
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700226 checkPermission(MANAGE_BIOMETRIC);
Kevin Chynd79e24e2018-09-25 12:06:59 -0700227 return startRevokeChallenge(token);
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200228 }
229
230 @Override // Binder call
Kevin Chyn1f16c2d2018-12-07 13:06:08 -0800231 public void enroll(final IBinder token, final byte[] cryptoToken,
232 final IFaceServiceReceiver receiver, final String opPackageName,
233 final int[] disabledFeatures) {
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700234 checkPermission(MANAGE_BIOMETRIC);
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200235
Kevin Chyndbfbed42019-06-13 17:01:30 -0700236 mNotificationManager.cancelAsUser(NOTIFICATION_TAG, NOTIFICATION_ID,
237 UserHandle.CURRENT);
238
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200239 final boolean restricted = isRestricted();
Kevin Chyna56dff72018-06-19 18:41:12 -0700240 final EnrollClientImpl client = new EnrollClientImpl(getContext(), mDaemonWrapper,
241 mHalDeviceId, token, new ServiceListenerImpl(receiver), mCurrentUserId,
Kevin Chyn1429a312019-01-28 16:08:09 -0800242 0 /* groupId */, cryptoToken, restricted, opPackageName, disabledFeatures) {
Kevin Chyn4cc49f72019-04-24 13:53:35 -0700243
244 @Override
245 public int[] getAcquireIgnorelist() {
246 return mEnrollIgnoreList;
247 }
248
249 @Override
250 public int[] getAcquireVendorIgnorelist() {
251 return mEnrollIgnoreListVendor;
252 }
253
Kevin Chyn1429a312019-01-28 16:08:09 -0800254 @Override
255 public boolean shouldVibrate() {
256 return false;
257 }
Kevin Chyn7782d142019-01-18 12:51:33 -0800258
259 @Override
260 protected int statsModality() {
261 return FaceService.this.statsModality();
262 }
Kevin Chyn1429a312019-01-28 16:08:09 -0800263 };
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200264
Kevin Chyn1a878c12019-04-04 15:50:11 -0700265 enrollInternal(client, mCurrentUserId);
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200266 }
267
268 @Override // Binder call
269 public void cancelEnrollment(final IBinder token) {
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700270 checkPermission(MANAGE_BIOMETRIC);
Kevin Chyna56dff72018-06-19 18:41:12 -0700271 cancelEnrollmentInternal(token);
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200272 }
273
274 @Override // Binder call
Kevin Chyn747e29b2019-01-11 17:01:53 -0800275 public void authenticate(final IBinder token, final long opId, int userId,
Kevin Chyna56dff72018-06-19 18:41:12 -0700276 final IFaceServiceReceiver receiver, final int flags,
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700277 final String opPackageName) {
278 checkPermission(USE_BIOMETRIC_INTERNAL);
Kevin Chyn747e29b2019-01-11 17:01:53 -0800279 updateActiveGroup(userId, opPackageName);
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200280 final boolean restricted = isRestricted();
Kevin Chyn8b7a0372018-09-17 15:06:05 -0700281 final AuthenticationClientImpl client = new FaceAuthClient(getContext(),
Kevin Chyna56dff72018-06-19 18:41:12 -0700282 mDaemonWrapper, mHalDeviceId, token, new ServiceListenerImpl(receiver),
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700283 mCurrentUserId, 0 /* groupId */, opId, restricted, opPackageName,
Kevin Chyn87f257a2018-11-27 16:26:07 -0800284 0 /* cookie */, false /* requireConfirmation */);
Kevin Chyna56dff72018-06-19 18:41:12 -0700285 authenticateInternal(client, opId, opPackageName);
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200286 }
287
288 @Override // Binder call
Kevin Chyn87f257a2018-11-27 16:26:07 -0800289 public void prepareForAuthentication(boolean requireConfirmation, IBinder token, long opId,
Kevin Chyn23289ef2018-11-28 16:32:36 -0800290 int groupId, IBiometricServiceReceiverInternal wrapperReceiver,
291 String opPackageName, int cookie, int callingUid, int callingPid,
292 int callingUserId) {
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700293 checkPermission(USE_BIOMETRIC_INTERNAL);
Kevin Chyn41a80902019-02-06 08:12:15 -0800294 updateActiveGroup(groupId, opPackageName);
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700295 final boolean restricted = true; // BiometricPrompt is always restricted
Kevin Chyn8b7a0372018-09-17 15:06:05 -0700296 final AuthenticationClientImpl client = new FaceAuthClient(getContext(),
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700297 mDaemonWrapper, mHalDeviceId, token,
Kevin Chyn87f257a2018-11-27 16:26:07 -0800298 new BiometricPromptServiceListenerImpl(wrapperReceiver),
299 mCurrentUserId, 0 /* groupId */, opId, restricted, opPackageName, cookie,
Kevin Chyn158fefb2019-01-03 18:59:05 -0800300 requireConfirmation);
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700301 authenticateInternal(client, opId, opPackageName, callingUid, callingPid,
302 callingUserId);
303 }
304
305 @Override // Binder call
Kevin Chyn87f257a2018-11-27 16:26:07 -0800306 public void startPreparedClient(int cookie) {
307 checkPermission(MANAGE_BIOMETRIC);
308 startCurrentClient(cookie);
309 }
310
311 @Override // Binder call
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200312 public void cancelAuthentication(final IBinder token, final String opPackageName) {
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700313 checkPermission(USE_BIOMETRIC_INTERNAL);
Kevin Chyna56dff72018-06-19 18:41:12 -0700314 cancelAuthenticationInternal(token, opPackageName);
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200315 }
316
317 @Override // Binder call
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700318 public void cancelAuthenticationFromService(final IBinder token, final String opPackageName,
Kevin Chyne92cdae2018-11-21 16:35:04 -0800319 int callingUid, int callingPid, int callingUserId, boolean fromClient) {
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700320 checkPermission(USE_BIOMETRIC_INTERNAL);
Kevin Chyne92cdae2018-11-21 16:35:04 -0800321 cancelAuthenticationInternal(token, opPackageName, callingUid, callingPid,
322 callingUserId, fromClient);
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700323 }
324
325 @Override // Binder call
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200326 public void setActiveUser(final int userId) {
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700327 checkPermission(MANAGE_BIOMETRIC);
Kevin Chyna56dff72018-06-19 18:41:12 -0700328 setActiveUserInternal(userId);
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200329 }
330
331 @Override // Binder call
Kevin Chyna56dff72018-06-19 18:41:12 -0700332 public void remove(final IBinder token, final int faceId, final int userId,
333 final IFaceServiceReceiver receiver) {
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700334 checkPermission(MANAGE_BIOMETRIC);
Kevin Chyna56dff72018-06-19 18:41:12 -0700335
336 if (token == null) {
337 Slog.w(TAG, "remove(): token is null");
338 return;
339 }
340
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200341 final boolean restricted = isRestricted();
Kevin Chyn4cc49f72019-04-24 13:53:35 -0700342 final RemovalClient client = new RemovalClient(getContext(), getConstants(),
Kevin Chyn6737c572019-02-08 16:10:54 -0800343 mDaemonWrapper, mHalDeviceId, token, new ServiceListenerImpl(receiver), faceId,
344 0 /* groupId */, userId, restricted, token.toString(), getBiometricUtils()) {
Kevin Chyn7782d142019-01-18 12:51:33 -0800345 @Override
346 protected int statsModality() {
347 return FaceService.this.statsModality();
348 }
349 };
Kevin Chyna56dff72018-06-19 18:41:12 -0700350 removeInternal(client);
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200351 }
352
Kevin Chyna56dff72018-06-19 18:41:12 -0700353 @Override
354 public void enumerate(final IBinder token, final int userId,
355 final IFaceServiceReceiver receiver) {
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700356 checkPermission(MANAGE_BIOMETRIC);
Kevin Chyna56dff72018-06-19 18:41:12 -0700357
358 final boolean restricted = isRestricted();
Kevin Chyn4cc49f72019-04-24 13:53:35 -0700359 final EnumerateClient client = new EnumerateClient(getContext(), getConstants(),
Kevin Chyn6737c572019-02-08 16:10:54 -0800360 mDaemonWrapper, mHalDeviceId, token, new ServiceListenerImpl(receiver), userId,
361 userId, restricted, getContext().getOpPackageName()) {
Kevin Chyn7782d142019-01-18 12:51:33 -0800362 @Override
363 protected int statsModality() {
364 return FaceService.this.statsModality();
365 }
366 };
Kevin Chyna56dff72018-06-19 18:41:12 -0700367 enumerateInternal(client);
368 }
369
370 @Override
371 public void addLockoutResetCallback(final IBiometricServiceLockoutResetCallback callback)
372 throws RemoteException {
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700373 checkPermission(USE_BIOMETRIC_INTERNAL);
Kevin Chyna56dff72018-06-19 18:41:12 -0700374 FaceService.super.addLockoutResetCallback(callback);
375 }
376
377 @Override // Binder call
378 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
379 if (!DumpUtils.checkDumpPermission(getContext(), TAG, pw)) {
380 return;
381 }
382
383 final long ident = Binder.clearCallingIdentity();
384 try {
Joe Onoratodb396002019-04-05 19:49:27 -0700385 if (args.length > 1 && "--hal".equals(args[0])) {
386 dumpHal(fd, Arrays.copyOfRange(args, 1, args.length, args.getClass()));
Joe Onoratobf955d22019-03-25 00:16:58 -0700387 } else if (args.length > 0 && "--proto".equals(args[0])) {
Kevin Chyna56dff72018-06-19 18:41:12 -0700388 dumpProto(fd);
389 } else {
390 dumpInternal(pw);
391 }
392 } finally {
393 Binder.restoreCallingIdentity(ident);
394 }
395 }
396
397 /**
398 * The following methods don't use any common code from BiometricService
399 */
400
401 // TODO: refactor out common code here
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200402 @Override // Binder call
403 public boolean isHardwareDetected(long deviceId, String opPackageName) {
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700404 checkPermission(USE_BIOMETRIC_INTERNAL);
Kevin Chyna56dff72018-06-19 18:41:12 -0700405 if (!canUseBiometric(opPackageName, false /* foregroundOnly */,
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200406 Binder.getCallingUid(), Binder.getCallingPid(),
407 UserHandle.getCallingUserId())) {
408 return false;
409 }
410
411 final long token = Binder.clearCallingIdentity();
412 try {
413 IBiometricsFace daemon = getFaceDaemon();
414 return daemon != null && mHalDeviceId != 0;
415 } finally {
416 Binder.restoreCallingIdentity(token);
417 }
418 }
419
420 @Override // Binder call
Kevin Chyna56dff72018-06-19 18:41:12 -0700421 public void rename(final int faceId, final String name) {
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700422 checkPermission(MANAGE_BIOMETRIC);
Kevin Chyna56dff72018-06-19 18:41:12 -0700423 if (!isCurrentUserOrProfile(UserHandle.getCallingUserId())) {
424 return;
425 }
426 mHandler.post(new Runnable() {
427 @Override
428 public void run() {
429 getBiometricUtils().renameBiometricForUser(getContext(), mCurrentUserId,
430 faceId, name);
431 }
432 });
433 }
434
435 @Override // Binder call
436 public List<Face> getEnrolledFaces(int userId, String opPackageName) {
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700437 checkPermission(MANAGE_BIOMETRIC);
Kevin Chyna56dff72018-06-19 18:41:12 -0700438 if (!canUseBiometric(opPackageName, false /* foregroundOnly */,
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200439 Binder.getCallingUid(), Binder.getCallingPid(),
440 UserHandle.getCallingUserId())) {
441 return null;
442 }
443
Kevin Chyn6737c572019-02-08 16:10:54 -0800444 return FaceService.this.getEnrolledTemplates(userId);
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200445 }
446
447 @Override // Binder call
Kevin Chyna56dff72018-06-19 18:41:12 -0700448 public boolean hasEnrolledFaces(int userId, String opPackageName) {
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700449 checkPermission(USE_BIOMETRIC_INTERNAL);
Kevin Chyna56dff72018-06-19 18:41:12 -0700450 if (!canUseBiometric(opPackageName, false /* foregroundOnly */,
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200451 Binder.getCallingUid(), Binder.getCallingPid(),
452 UserHandle.getCallingUserId())) {
453 return false;
454 }
455
Kevin Chyna56dff72018-06-19 18:41:12 -0700456 return FaceService.this.hasEnrolledBiometrics(userId);
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200457 }
458
459 @Override // Binder call
460 public long getAuthenticatorId(String opPackageName) {
461 // In this method, we're not checking whether the caller is permitted to use face
462 // API because current authenticator ID is leaked (in a more contrived way) via Android
463 // Keystore (android.security.keystore package): the user of that API can create a key
464 // which requires face authentication for its use, and then query the key's
465 // characteristics (hidden API) which returns, among other things, face
466 // authenticator ID which was active at key creation time.
467 //
468 // Reason: The part of Android Keystore which runs inside an app's process invokes this
469 // method in certain cases. Those cases are not always where the developer demonstrates
470 // explicit intent to use face functionality. Thus, to avoiding throwing an
471 // unexpected SecurityException this method does not check whether its caller is
472 // permitted to use face API.
473 //
474 // The permission check should be restored once Android Keystore no longer invokes this
475 // method from inside app processes.
476
477 return FaceService.this.getAuthenticatorId(opPackageName);
478 }
479
480 @Override // Binder call
Kevin Chyna38653c2019-02-11 17:46:21 -0800481 public void resetLockout(byte[] token) {
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700482 checkPermission(MANAGE_BIOMETRIC);
Kevin Chyn1d6a2862019-04-02 16:20:21 -0700483
484 if (!FaceService.this.hasEnrolledBiometrics(mCurrentUserId)) {
485 Slog.w(TAG, "Ignoring lockout reset, no templates enrolled");
486 return;
487 }
488
Kevin Chynbe67ce02019-06-10 16:14:22 -0700489 Slog.d(TAG, "Resetting lockout for user: " + mCurrentUserId);
490
Kevin Chyna38653c2019-02-11 17:46:21 -0800491 try {
492 mDaemonWrapper.resetLockout(token);
493 } catch (RemoteException e) {
494 Slog.e(getTag(), "Unable to reset lockout", e);
495 }
Kevin Chyna56dff72018-06-19 18:41:12 -0700496 }
Kevin Chynd79e24e2018-09-25 12:06:59 -0700497
498 @Override
Kevin Chyne62749a2019-04-02 19:33:56 -0700499 public void setFeature(int feature, boolean enabled, final byte[] token,
500 IFaceServiceReceiver receiver) {
Kevin Chynd79e24e2018-09-25 12:06:59 -0700501 checkPermission(MANAGE_BIOMETRIC);
502
Kevin Chyne62749a2019-04-02 19:33:56 -0700503 mHandler.post(() -> {
504 if (!FaceService.this.hasEnrolledBiometrics(mCurrentUserId)) {
505 Slog.e(TAG, "No enrolled biometrics while setting feature: " + feature);
506 return;
Kevin Chynb95f1522019-03-04 16:45:15 -0800507 }
Kevin Chyne62749a2019-04-02 19:33:56 -0700508
509 final ArrayList<Byte> byteToken = new ArrayList<>();
510 for (int i = 0; i < token.length; i++) {
511 byteToken.add(token[i]);
512 }
513
514 // TODO: Support multiple faces
515 final int faceId = getFirstTemplateForUser(mCurrentUserId);
516
517 if (mDaemon != null) {
518 try {
519 final int result = mDaemon.setFeature(feature, enabled, byteToken, faceId);
520 receiver.onFeatureSet(result == Status.OK, feature);
521 } catch (RemoteException e) {
522 Slog.e(getTag(), "Unable to set feature: " + feature
523 + " to enabled:" + enabled, e);
524 }
525 }
526 });
527
Kevin Chynd79e24e2018-09-25 12:06:59 -0700528 }
529
530 @Override
Kevin Chyne62749a2019-04-02 19:33:56 -0700531 public void getFeature(int feature, IFaceServiceReceiver receiver) {
Kevin Chynd79e24e2018-09-25 12:06:59 -0700532 checkPermission(MANAGE_BIOMETRIC);
533
Kevin Chyne62749a2019-04-02 19:33:56 -0700534 mHandler.post(() -> {
535 // This should ideally return tri-state, but the user isn't shown settings unless
536 // they are enrolled so it's fine for now.
537 if (!FaceService.this.hasEnrolledBiometrics(mCurrentUserId)) {
538 Slog.e(TAG, "No enrolled biometrics while getting feature: " + feature);
539 return;
Kevin Chynb95f1522019-03-04 16:45:15 -0800540 }
Kevin Chyne62749a2019-04-02 19:33:56 -0700541
542 // TODO: Support multiple faces
543 final int faceId = getFirstTemplateForUser(mCurrentUserId);
544
545 if (mDaemon != null) {
546 try {
547 OptionalBool result = mDaemon.getFeature(feature, faceId);
548 receiver.onFeatureGet(result.status == Status.OK, feature, result.value);
549 } catch (RemoteException e) {
550 Slog.e(getTag(), "Unable to getRequireAttention", e);
551 }
552 }
553 });
554
Kevin Chynd79e24e2018-09-25 12:06:59 -0700555 }
Kevin Chyn57f119b2018-10-25 12:03:41 -0700556
557 @Override
558 public void userActivity() {
559 checkPermission(MANAGE_BIOMETRIC);
560
561 if (mDaemon != null) {
562 try {
563 mDaemon.userActivity();
564 } catch (RemoteException e) {
565 Slog.e(getTag(), "Unable to send userActivity", e);
566 }
567 }
568 }
Kevin Chynb95f1522019-03-04 16:45:15 -0800569
570 // TODO: Support multiple faces
571 private int getFirstTemplateForUser(int user) {
572 final List<Face> faces = FaceService.this.getEnrolledTemplates(user);
573 if (!faces.isEmpty()) {
574 return faces.get(0).getBiometricId();
575 }
576 return 0;
577 }
Kevin Chyna56dff72018-06-19 18:41:12 -0700578 }
579
580 /**
581 * Receives callbacks from the ClientMonitor implementations. The results are forwarded to
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700582 * BiometricPrompt.
583 */
Kevin Chyne92cdae2018-11-21 16:35:04 -0800584 private class BiometricPromptServiceListenerImpl extends BiometricServiceListener {
Kevin Chyn23289ef2018-11-28 16:32:36 -0800585 BiometricPromptServiceListenerImpl(IBiometricServiceReceiverInternal wrapperReceiver) {
Kevin Chyn87f257a2018-11-27 16:26:07 -0800586 super(wrapperReceiver);
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700587 }
588
589 @Override
590 public void onAcquired(long deviceId, int acquiredInfo, int vendorCode)
591 throws RemoteException {
592 /**
593 * Map the acquired codes onto existing {@link BiometricConstants} acquired codes.
594 */
Kevin Chyne92cdae2018-11-21 16:35:04 -0800595 if (getWrapperReceiver() != null) {
596 getWrapperReceiver().onAcquired(
Kevin Chyn8b7a0372018-09-17 15:06:05 -0700597 FaceManager.getMappedAcquiredInfo(acquiredInfo, vendorCode),
598 FaceManager.getAcquiredString(getContext(), acquiredInfo, vendorCode));
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700599 }
600 }
601
602 @Override
Kevin Chyn87f257a2018-11-27 16:26:07 -0800603 public void onError(long deviceId, int error, int vendorCode, int cookie)
604 throws RemoteException {
Kevin Chyne92cdae2018-11-21 16:35:04 -0800605 if (getWrapperReceiver() != null) {
Kevin Chyn23289ef2018-11-28 16:32:36 -0800606 getWrapperReceiver().onError(cookie, error,
607 FaceManager.getErrorString(getContext(), error, vendorCode));
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700608 }
609 }
610 }
611
612 /**
613 * Receives callbacks from the ClientMonitor implementations. The results are forwarded to
Kevin Chyna56dff72018-06-19 18:41:12 -0700614 * the FaceManager.
615 */
616 private class ServiceListenerImpl implements ServiceListener {
Kevin Chyna56dff72018-06-19 18:41:12 -0700617 private IFaceServiceReceiver mFaceServiceReceiver;
618
619 public ServiceListenerImpl(IFaceServiceReceiver receiver) {
620 mFaceServiceReceiver = receiver;
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200621 }
622
623 @Override
Kevin Chyna56dff72018-06-19 18:41:12 -0700624 public void onEnrollResult(BiometricAuthenticator.Identifier identifier, int remaining)
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200625 throws RemoteException {
Kevin Chyna56dff72018-06-19 18:41:12 -0700626 if (mFaceServiceReceiver != null) {
627 mFaceServiceReceiver.onEnrollResult(identifier.getDeviceId(),
628 identifier.getBiometricId(),
629 remaining);
630 }
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200631 }
Kevin Chyna56dff72018-06-19 18:41:12 -0700632
633 @Override
634 public void onAcquired(long deviceId, int acquiredInfo, int vendorCode)
635 throws RemoteException {
636 if (mFaceServiceReceiver != null) {
637 mFaceServiceReceiver.onAcquired(deviceId, acquiredInfo, vendorCode);
638 }
639 }
640
641 @Override
642 public void onAuthenticationSucceeded(long deviceId,
643 BiometricAuthenticator.Identifier biometric, int userId)
644 throws RemoteException {
645 if (mFaceServiceReceiver != null) {
Kevin Chyn628b7182018-11-13 12:00:48 -0800646 if (biometric == null || biometric instanceof Face) {
Kevin Chyn8d2694a2019-04-11 18:30:40 -0700647 mFaceServiceReceiver.onAuthenticationSucceeded(deviceId, (Face) biometric,
648 userId);
Kevin Chyna56dff72018-06-19 18:41:12 -0700649 } else {
650 Slog.e(TAG, "onAuthenticationSucceeded received non-face biometric");
651 }
652 }
653 }
654
655 @Override
656 public void onAuthenticationFailed(long deviceId) throws RemoteException {
657 if (mFaceServiceReceiver != null) {
658 mFaceServiceReceiver.onAuthenticationFailed(deviceId);
659 }
660 }
661
662 @Override
Kevin Chyn87f257a2018-11-27 16:26:07 -0800663 public void onError(long deviceId, int error, int vendorCode, int cookie)
664 throws RemoteException {
Kevin Chyna56dff72018-06-19 18:41:12 -0700665 if (mFaceServiceReceiver != null) {
666 mFaceServiceReceiver.onError(deviceId, error, vendorCode);
667 }
668 }
669
670 @Override
671 public void onRemoved(BiometricAuthenticator.Identifier identifier,
672 int remaining) throws RemoteException {
673 if (mFaceServiceReceiver != null) {
674 mFaceServiceReceiver.onRemoved(identifier.getDeviceId(),
675 identifier.getBiometricId(), remaining);
676 }
677 }
678
679 @Override
680 public void onEnumerated(BiometricAuthenticator.Identifier identifier, int remaining)
681 throws RemoteException {
682 if (mFaceServiceReceiver != null) {
Kevin Chyn6737c572019-02-08 16:10:54 -0800683 mFaceServiceReceiver.onEnumerated(identifier.getDeviceId(),
684 identifier.getBiometricId(), remaining);
Kevin Chyna56dff72018-06-19 18:41:12 -0700685 }
686 }
687 }
688
Kevin Chyn4cc49f72019-04-24 13:53:35 -0700689 private final FaceConstants mFaceConstants = new FaceConstants();
Kevin Chyna56dff72018-06-19 18:41:12 -0700690
691 @GuardedBy("this")
692 private IBiometricsFace mDaemon;
Kevin Chyna38653c2019-02-11 17:46:21 -0800693 // One of the AuthenticationClient constants
694 private int mCurrentUserLockoutMode;
Kevin Chyna56dff72018-06-19 18:41:12 -0700695
Kevin Chyndbfbed42019-06-13 17:01:30 -0700696 private NotificationManager mNotificationManager;
697
Kevin Chyn4cc49f72019-04-24 13:53:35 -0700698 private int[] mBiometricPromptIgnoreList;
699 private int[] mBiometricPromptIgnoreListVendor;
700 private int[] mKeyguardIgnoreList;
701 private int[] mKeyguardIgnoreListVendor;
702 private int[] mEnrollIgnoreList;
703 private int[] mEnrollIgnoreListVendor;
704
Kevin Chyna56dff72018-06-19 18:41:12 -0700705 /**
706 * Receives callbacks from the HAL.
707 */
708 private IBiometricsFaceClientCallback mDaemonCallback =
709 new IBiometricsFaceClientCallback.Stub() {
Kevin Chyn6737c572019-02-08 16:10:54 -0800710 @Override
711 public void onEnrollResult(final long deviceId, int faceId, int userId,
712 int remaining) {
713 mHandler.post(() -> {
714 final Face face = new Face(getBiometricUtils()
715 .getUniqueName(getContext(), userId), faceId, deviceId);
716 FaceService.super.handleEnrollResult(face, remaining);
717 });
718 }
719
720 @Override
721 public void onAcquired(final long deviceId, final int userId,
722 final int acquiredInfo,
723 final int vendorCode) {
724 mHandler.post(() -> {
725 FaceService.super.handleAcquired(deviceId, acquiredInfo, vendorCode);
726 });
727 }
728
729 @Override
730 public void onAuthenticated(final long deviceId, final int faceId, final int userId,
731 ArrayList<Byte> token) {
732 mHandler.post(() -> {
733 Face face = new Face("", faceId, deviceId);
734 FaceService.super.handleAuthenticated(face, token);
735 });
736 }
737
738 @Override
739 public void onError(final long deviceId, final int userId, final int error,
740 final int vendorCode) {
741 mHandler.post(() -> {
742 FaceService.super.handleError(deviceId, error, vendorCode);
743
744 // TODO: this chunk of code should be common to all biometric services
745 if (error == BiometricConstants.BIOMETRIC_ERROR_HW_UNAVAILABLE) {
746 // If we get HW_UNAVAILABLE, try to connect again later...
747 Slog.w(TAG, "Got ERROR_HW_UNAVAILABLE; try reconnecting next client.");
748 synchronized (this) {
749 mDaemon = null;
750 mHalDeviceId = 0;
751 mCurrentUserId = UserHandle.USER_NULL;
752 }
Kevin Chyna56dff72018-06-19 18:41:12 -0700753 }
Kevin Chyn6737c572019-02-08 16:10:54 -0800754 });
755 }
Kevin Chyna56dff72018-06-19 18:41:12 -0700756
Kevin Chyn6737c572019-02-08 16:10:54 -0800757 @Override
Kevin Chyn31ae8d22019-03-11 14:12:32 -0700758 public void onRemoved(final long deviceId, ArrayList<Integer> faceIds, final int userId) {
Kevin Chyn6737c572019-02-08 16:10:54 -0800759 mHandler.post(() -> {
Kevin Chyn31ae8d22019-03-11 14:12:32 -0700760 if (!faceIds.isEmpty()) {
761 for (int i = 0; i < faceIds.size(); i++) {
762 final Face face = new Face("", faceIds.get(i), deviceId);
763 // Convert to old behavior
764 FaceService.super.handleRemoved(face, faceIds.size() - i - 1);
765 }
766 } else {
767 final Face face = new Face("", 0 /* identifier */, deviceId);
768 FaceService.super.handleRemoved(face, 0 /* remaining */);
769 }
770
Kevin Chyn6737c572019-02-08 16:10:54 -0800771 });
772 }
773
774 @Override
775 public void onEnumerate(long deviceId, ArrayList<Integer> faceIds, int userId)
776 throws RemoteException {
777 mHandler.post(() -> {
778 if (!faceIds.isEmpty()) {
779 for (int i = 0; i < faceIds.size(); i++) {
780 final Face face = new Face("", faceIds.get(i), deviceId);
781 // Convert to old old behavior
782 FaceService.super.handleEnumerate(face, faceIds.size() - i - 1);
783 }
784 } else {
785 // For face, the HIDL contract is to receive an empty list when there are no
786 // templates enrolled. Send a null identifier since we don't consume them
787 // anywhere, and send remaining == 0 to plumb this with existing common code.
788 FaceService.super.handleEnumerate(null /* identifier */, 0);
Kevin Chyna56dff72018-06-19 18:41:12 -0700789 }
Kevin Chyn6737c572019-02-08 16:10:54 -0800790 });
791 }
Kevin Chyna56dff72018-06-19 18:41:12 -0700792
Kevin Chyn6737c572019-02-08 16:10:54 -0800793 @Override
794 public void onLockoutChanged(long duration) {
Kevin Chyna38653c2019-02-11 17:46:21 -0800795 Slog.d(TAG, "onLockoutChanged: " + duration);
796 if (duration == 0) {
797 mCurrentUserLockoutMode = AuthenticationClient.LOCKOUT_NONE;
798 } else if (duration == Long.MAX_VALUE) {
799 mCurrentUserLockoutMode = AuthenticationClient.LOCKOUT_PERMANENT;
800 } else {
801 mCurrentUserLockoutMode = AuthenticationClient.LOCKOUT_TIMED;
802 }
Kevin Chyna56dff72018-06-19 18:41:12 -0700803
Kevin Chyna38653c2019-02-11 17:46:21 -0800804 mHandler.post(() -> {
805 if (duration == 0) {
806 notifyLockoutResetMonitors();
807 }
808 });
Kevin Chyn6737c572019-02-08 16:10:54 -0800809 }
810 };
Kevin Chyna56dff72018-06-19 18:41:12 -0700811
812 /**
813 * Wraps the HAL-specific code and is passed to the ClientMonitor implementations so that they
814 * can be shared between the multiple biometric services.
815 */
816 private final DaemonWrapper mDaemonWrapper = new DaemonWrapper() {
817 @Override
818 public int authenticate(long operationId, int groupId) throws RemoteException {
819 IBiometricsFace daemon = getFaceDaemon();
820 if (daemon == null) {
821 Slog.w(TAG, "authenticate(): no face HAL!");
822 return ERROR_ESRCH;
823 }
824 return daemon.authenticate(operationId);
825 }
826
827 @Override
828 public int cancel() throws RemoteException {
829 IBiometricsFace daemon = getFaceDaemon();
830 if (daemon == null) {
831 Slog.w(TAG, "cancel(): no face HAL!");
832 return ERROR_ESRCH;
833 }
834 return daemon.cancel();
835 }
836
837 @Override
838 public int remove(int groupId, int biometricId) throws RemoteException {
839 IBiometricsFace daemon = getFaceDaemon();
840 if (daemon == null) {
841 Slog.w(TAG, "remove(): no face HAL!");
842 return ERROR_ESRCH;
843 }
844 return daemon.remove(biometricId);
845 }
846
847 @Override
848 public int enumerate() throws RemoteException {
849 IBiometricsFace daemon = getFaceDaemon();
850 if (daemon == null) {
851 Slog.w(TAG, "enumerate(): no face HAL!");
852 return ERROR_ESRCH;
853 }
854 return daemon.enumerate();
855 }
856
857 @Override
Kevin Chyn1f16c2d2018-12-07 13:06:08 -0800858 public int enroll(byte[] cryptoToken, int groupId, int timeout,
859 ArrayList<Integer> disabledFeatures) throws RemoteException {
Kevin Chyna56dff72018-06-19 18:41:12 -0700860 IBiometricsFace daemon = getFaceDaemon();
861 if (daemon == null) {
862 Slog.w(TAG, "enroll(): no face HAL!");
863 return ERROR_ESRCH;
864 }
865 final ArrayList<Byte> token = new ArrayList<>();
866 for (int i = 0; i < cryptoToken.length; i++) {
867 token.add(cryptoToken[i]);
868 }
Kevin Chyn1f16c2d2018-12-07 13:06:08 -0800869 return daemon.enroll(token, timeout, disabledFeatures);
Kevin Chyna56dff72018-06-19 18:41:12 -0700870 }
Kevin Chyna38653c2019-02-11 17:46:21 -0800871
872 @Override
873 public void resetLockout(byte[] cryptoToken) throws RemoteException {
874 IBiometricsFace daemon = getFaceDaemon();
875 if (daemon == null) {
876 Slog.w(TAG, "resetLockout(): no face HAL!");
877 return;
878 }
879 final ArrayList<Byte> token = new ArrayList<>();
880 for (int i = 0; i < cryptoToken.length; i++) {
881 token.add(cryptoToken[i]);
882 }
883 daemon.resetLockout(token);
884 }
Kevin Chyna56dff72018-06-19 18:41:12 -0700885 };
886
887
888 public FaceService(Context context) {
889 super(context);
Kevin Chyn4cc49f72019-04-24 13:53:35 -0700890
Kevin Chyndbfbed42019-06-13 17:01:30 -0700891 mNotificationManager = getContext().getSystemService(NotificationManager.class);
892
Kevin Chyn4cc49f72019-04-24 13:53:35 -0700893 mBiometricPromptIgnoreList = getContext().getResources()
894 .getIntArray(R.array.config_face_acquire_biometricprompt_ignorelist);
895 mBiometricPromptIgnoreListVendor = getContext().getResources()
896 .getIntArray(R.array.config_face_acquire_vendor_biometricprompt_ignorelist);
897 mKeyguardIgnoreList = getContext().getResources()
898 .getIntArray(R.array.config_face_acquire_keyguard_ignorelist);
899 mKeyguardIgnoreListVendor = getContext().getResources()
900 .getIntArray(R.array.config_face_acquire_vendor_keyguard_ignorelist);
901 mEnrollIgnoreList = getContext().getResources()
902 .getIntArray(R.array.config_face_acquire_enroll_ignorelist);
903 mEnrollIgnoreListVendor = getContext().getResources()
904 .getIntArray(R.array.config_face_acquire_vendor_enroll_ignorelist);
Kevin Chyna56dff72018-06-19 18:41:12 -0700905 }
906
907 @Override
908 public void onStart() {
Kevin Chyn5a2ff5d2018-08-29 19:07:30 -0700909 super.onStart();
Kevin Chyna56dff72018-06-19 18:41:12 -0700910 publishBinderService(Context.FACE_SERVICE, new FaceServiceWrapper());
Kevin Chyn8398a712019-06-13 16:44:50 -0700911 // Get the face daemon on FaceService's on thread so SystemServerInitThreadPool isn't
912 // blocked
913 SystemServerInitThreadPool.get().submit(() -> mHandler.post(this::getFaceDaemon),
914 TAG + ".onStart");
Kevin Chyna56dff72018-06-19 18:41:12 -0700915 }
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;
983 }
984 mAuthenticatorIds.put(userId,
985 hasEnrolledBiometrics(userId) ? daemon.getAuthenticatorId().value : 0L);
986 } 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}