blob: 72bac7494804c56429b0f2b5e0e325b839dcd235 [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;
Gilad Brettercb51b8b2018-03-22 17:04:51 +020057
Kevin Chyn56e4c3d2019-04-23 11:28:43 -070058import com.android.internal.R;
Gilad Brettercb51b8b2018-03-22 17:04:51 +020059import com.android.internal.annotations.GuardedBy;
60import com.android.internal.logging.MetricsLogger;
61import com.android.internal.util.DumpUtils;
62import com.android.server.SystemServerInitThreadPool;
Kevin Chyna38653c2019-02-11 17:46:21 -080063import com.android.server.biometrics.AuthenticationClient;
Kevin Chyn355c6bf2018-09-20 22:14:19 -070064import com.android.server.biometrics.BiometricServiceBase;
Kevin Chyn836f2cf2018-08-27 11:06:39 -070065import com.android.server.biometrics.BiometricUtils;
Kevin Chyn4cc49f72019-04-24 13:53:35 -070066import com.android.server.biometrics.Constants;
Kevin Chyn0ce70852019-05-10 10:29:18 -070067import com.android.server.biometrics.EnumerateClient;
Kevin Chyn6737c572019-02-08 16:10:54 -080068import com.android.server.biometrics.RemovalClient;
Gilad Brettercb51b8b2018-03-22 17:04:51 +020069
70import org.json.JSONArray;
71import org.json.JSONException;
72import org.json.JSONObject;
73
74import java.io.File;
75import java.io.FileDescriptor;
Joe Onoratodb396002019-04-05 19:49:27 -070076import java.io.FileOutputStream;
77import java.io.IOException;
Gilad Brettercb51b8b2018-03-22 17:04:51 +020078import java.io.PrintWriter;
79import java.util.ArrayList;
Joe Onoratodb396002019-04-05 19:49:27 -070080import java.util.Arrays;
Kevin Chyn9bc1d492019-06-21 15:31:50 -070081import java.util.HashMap;
Gilad Brettercb51b8b2018-03-22 17:04:51 +020082import java.util.List;
Kevin Chyn9bc1d492019-06-21 15:31:50 -070083import java.util.Map;
Gilad Brettercb51b8b2018-03-22 17:04:51 +020084
85/**
86 * A service to manage multiple clients that want to access the face HAL API.
87 * The service is responsible for maintaining a list of clients and dispatching all
Kevin Chyn51676d22018-11-05 18:00:43 -080088 * face-related events.
Gilad Brettercb51b8b2018-03-22 17:04:51 +020089 *
90 * @hide
91 */
Kevin Chyn355c6bf2018-09-20 22:14:19 -070092public class FaceService extends BiometricServiceBase {
Kevin Chyna56dff72018-06-19 18:41:12 -070093
94 protected static final String TAG = "FaceService";
95 private static final boolean DEBUG = true;
Gilad Brettercb51b8b2018-03-22 17:04:51 +020096 private static final String FACE_DATA_DIR = "facedata";
Gilad Brettercb51b8b2018-03-22 17:04:51 +020097 private static final String ACTION_LOCKOUT_RESET =
Kevin Chyn2ffadb32018-06-19 11:29:38 -070098 "com.android.server.biometrics.face.ACTION_LOCKOUT_RESET";
Kevin Chyne46a2162018-09-20 18:43:01 -070099 private static final int CHALLENGE_TIMEOUT_SEC = 600; // 10 minutes
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200100
Kevin Chyndbfbed42019-06-13 17:01:30 -0700101 private static final String NOTIFICATION_TAG = "FaceService";
102 private static final int NOTIFICATION_ID = 1;
103
Kevin Chyn9bc1d492019-06-21 15:31:50 -0700104 /**
105 * Events for bugreports.
106 */
107 public static final class AuthenticationEvent {
108 private long mStartTime;
109 private long mLatency;
110 // Only valid if mError is 0
111 private boolean mAuthenticated;
112 private int mError;
113 // Only valid if mError is ERROR_VENDOR
114 private int mVendorError;
115
116 AuthenticationEvent(long startTime, long latency, boolean authenticated, int error,
117 int vendorError) {
118 mStartTime = startTime;
119 mLatency = latency;
120 mAuthenticated = authenticated;
121 mError = error;
122 mVendorError = vendorError;
123 }
124
125 public String toString(Context context) {
126 return "Start: " + mStartTime
127 + "\tLatency: " + mLatency
128 + "\tAuthenticated: " + mAuthenticated
129 + "\tError: " + mError
130 + "\tVendorCode: " + mVendorError
131 + "\t" + FaceManager.getErrorString(context, mError, mVendorError);
132 }
133 }
134
135 /**
136 * Keep a short historical buffer of stats, with an aggregated usage time.
137 */
138 private class UsageStats {
139 static final int EVENT_LOG_SIZE = 100;
140
141 Context mContext;
142 List<AuthenticationEvent> mAuthenticationEvents;
143
144 int acceptCount;
145 int rejectCount;
146 Map<Integer, Integer> mErrorCount;
147
148 long acceptLatency;
149 long rejectLatency;
150 Map<Integer, Long> mErrorLatency;
151
152 UsageStats(Context context) {
153 mAuthenticationEvents = new ArrayList<>();
154 mErrorCount = new HashMap<>();
155 mErrorLatency = new HashMap<>();
156 mContext = context;
157 }
158
159 void addEvent(AuthenticationEvent event) {
160 if (mAuthenticationEvents.size() >= EVENT_LOG_SIZE) {
161 mAuthenticationEvents.remove(0);
162 }
163 mAuthenticationEvents.add(event);
164
165 if (event.mAuthenticated) {
166 acceptCount++;
167 acceptLatency += event.mLatency;
168 } else if (event.mError == 0) {
169 rejectCount++;
170 rejectLatency += event.mLatency;
171 } else {
172 mErrorCount.put(event.mError, mErrorCount.getOrDefault(event.mError, 0) + 1);
173 mErrorLatency.put(event.mError,
174 mErrorLatency.getOrDefault(event.mError, 0l) + event.mLatency);
175 }
176 }
177
178 void print(PrintWriter pw) {
179 pw.println("Events since last reboot: " + mAuthenticationEvents.size());
180 for (int i = 0; i < mAuthenticationEvents.size(); i++) {
181 pw.println(mAuthenticationEvents.get(i).toString(mContext));
182 }
183
184 // Dump aggregated usage stats
185 // TODO: Remove or combine with json dump in a future release
186 pw.println("Accept\tCount: " + acceptCount + "\tLatency: " + acceptLatency
187 + "\tAverage: " + (acceptCount > 0 ? acceptLatency / acceptCount : 0));
188 pw.println("Reject\tCount: " + rejectCount + "\tLatency: " + rejectLatency
189 + "\tAverage: " + (rejectCount > 0 ? rejectLatency / rejectCount : 0));
190
191 for (Integer key : mErrorCount.keySet()) {
192 final int count = mErrorCount.get(key);
193 pw.println("Error" + key + "\tCount: " + count
194 + "\tLatency: " + mErrorLatency.getOrDefault(key, 0l)
195 + "\tAverage: " + (count > 0 ? mErrorLatency.getOrDefault(key, 0l) / count
196 : 0)
197 + "\t" + FaceManager.getErrorString(mContext, key, 0 /* vendorCode */));
198 }
199 }
200 }
201
Kevin Chyn8b7a0372018-09-17 15:06:05 -0700202 private final class FaceAuthClient extends AuthenticationClientImpl {
Kevin Chyn0ce70852019-05-10 10:29:18 -0700203 private int mLastAcquire;
204
Kevin Chyn8b7a0372018-09-17 15:06:05 -0700205 public FaceAuthClient(Context context,
206 DaemonWrapper daemon, long halDeviceId, IBinder token,
207 ServiceListener listener, int targetUserId, int groupId, long opId,
Kevin Chyn87f257a2018-11-27 16:26:07 -0800208 boolean restricted, String owner, int cookie, boolean requireConfirmation) {
Kevin Chyn8b7a0372018-09-17 15:06:05 -0700209 super(context, daemon, halDeviceId, token, listener, targetUserId, groupId, opId,
Kevin Chyn87f257a2018-11-27 16:26:07 -0800210 restricted, owner, cookie, requireConfirmation);
Kevin Chyn8b7a0372018-09-17 15:06:05 -0700211 }
Kevin Chyn7782d142019-01-18 12:51:33 -0800212
213 @Override
214 protected int statsModality() {
215 return FaceService.this.statsModality();
216 }
Kevin Chyna38653c2019-02-11 17:46:21 -0800217
218 @Override
219 public boolean shouldFrameworkHandleLockout() {
220 return false;
221 }
Kevin Chyn56d6b072019-02-13 18:39:01 -0800222
223 @Override
Kevin Chyn0ce70852019-05-10 10:29:18 -0700224 public boolean wasUserDetected() {
225 return mLastAcquire != FaceManager.FACE_ACQUIRED_NOT_DETECTED;
226 }
227
228 @Override
Kevin Chyn56d6b072019-02-13 18:39:01 -0800229 public boolean onAuthenticated(BiometricAuthenticator.Identifier identifier,
230 boolean authenticated, ArrayList<Byte> token) {
231 final boolean result = super.onAuthenticated(identifier, authenticated, token);
232
Kevin Chyn9bc1d492019-06-21 15:31:50 -0700233 mUsageStats.addEvent(new AuthenticationEvent(
234 getStartTimeMs(),
235 System.currentTimeMillis() - getStartTimeMs() /* latency */,
236 authenticated,
237 0 /* error */,
238 0 /* vendorError */));
239
Kevin Chyn56d6b072019-02-13 18:39:01 -0800240 // For face, the authentication lifecycle ends either when
241 // 1) Authenticated == true
242 // 2) Error occurred
243 // 3) Authenticated == false
244 // Fingerprint currently does not end when the third condition is met which is a bug,
245 // but let's leave it as-is for now.
246 return result || !authenticated;
247 }
Kevin Chyn56e4c3d2019-04-23 11:28:43 -0700248
249 @Override
Kevin Chyn9bc1d492019-06-21 15:31:50 -0700250 public boolean onError(long deviceId, int error, int vendorCode) {
251 mUsageStats.addEvent(new AuthenticationEvent(
252 getStartTimeMs(),
253 System.currentTimeMillis() - getStartTimeMs() /* latency */,
254 false /* authenticated */,
255 error,
256 vendorCode));
257
258 return super.onError(deviceId, error, vendorCode);
259 }
260
261 @Override
Kevin Chyn4cc49f72019-04-24 13:53:35 -0700262 public int[] getAcquireIgnorelist() {
263 if (isBiometricPrompt()) {
264 return mBiometricPromptIgnoreList;
265 } else {
266 // Keyguard
267 return mKeyguardIgnoreList;
268 }
269 }
270
271 @Override
272 public int[] getAcquireVendorIgnorelist() {
273 if (isBiometricPrompt()) {
274 return mBiometricPromptIgnoreListVendor;
275 } else {
276 // Keyguard
277 return mKeyguardIgnoreListVendor;
278 }
279 }
280
281 @Override
Kevin Chyn56e4c3d2019-04-23 11:28:43 -0700282 public boolean onAcquired(int acquireInfo, int vendorCode) {
283
Kevin Chyn0ce70852019-05-10 10:29:18 -0700284 mLastAcquire = acquireInfo;
285
Kevin Chyn56e4c3d2019-04-23 11:28:43 -0700286 if (acquireInfo == FaceManager.FACE_ACQUIRED_RECALIBRATE) {
287 final String name =
288 getContext().getString(R.string.face_recalibrate_notification_name);
289 final String title =
290 getContext().getString(R.string.face_recalibrate_notification_title);
291 final String content =
292 getContext().getString(R.string.face_recalibrate_notification_content);
293
294 final Intent intent = new Intent("android.settings.FACE_SETTINGS");
295 intent.setPackage("com.android.settings");
296
297 final PendingIntent pendingIntent = PendingIntent.getActivityAsUser(getContext(),
298 0 /* requestCode */, intent, 0 /* flags */, null /* options */,
299 UserHandle.CURRENT);
300
Kevin Chyndbfbed42019-06-13 17:01:30 -0700301 final String channelName = "FaceEnrollNotificationChannel";
Kevin Chyn56e4c3d2019-04-23 11:28:43 -0700302
Kevin Chyndbfbed42019-06-13 17:01:30 -0700303 NotificationChannel channel = new NotificationChannel(channelName, name,
Kevin Chyn56e4c3d2019-04-23 11:28:43 -0700304 NotificationManager.IMPORTANCE_HIGH);
Kevin Chyndbfbed42019-06-13 17:01:30 -0700305 Notification notification = new Notification.Builder(getContext(), channelName)
Kevin Chyn56e4c3d2019-04-23 11:28:43 -0700306 .setSmallIcon(R.drawable.ic_lock)
307 .setContentTitle(title)
308 .setContentText(content)
309 .setSubText(name)
310 .setOnlyAlertOnce(true)
311 .setLocalOnly(true)
312 .setAutoCancel(true)
313 .setCategory(Notification.CATEGORY_SYSTEM)
314 .setContentIntent(pendingIntent)
Kevin Chyn0c3a9982019-06-11 19:09:27 -0700315 .setVisibility(Notification.VISIBILITY_SECRET)
Kevin Chyn56e4c3d2019-04-23 11:28:43 -0700316 .build();
317
Kevin Chyndbfbed42019-06-13 17:01:30 -0700318 mNotificationManager.createNotificationChannel(channel);
319 mNotificationManager.notifyAsUser(NOTIFICATION_TAG, NOTIFICATION_ID, notification,
320 UserHandle.CURRENT);
Kevin Chyn56e4c3d2019-04-23 11:28:43 -0700321 }
322
323 return super.onAcquired(acquireInfo, vendorCode);
324 }
Kevin Chyn8b7a0372018-09-17 15:06:05 -0700325 }
326
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200327 /**
Kevin Chyna56dff72018-06-19 18:41:12 -0700328 * Receives the incoming binder calls from FaceManager.
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200329 */
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200330 private final class FaceServiceWrapper extends IFaceService.Stub {
Kevin Chyna56dff72018-06-19 18:41:12 -0700331
332 /**
333 * The following methods contain common code which is shared in biometrics/common.
334 */
Kevin Chyna38653c2019-02-11 17:46:21 -0800335
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200336 @Override // Binder call
Kevin Chynd79e24e2018-09-25 12:06:59 -0700337 public long generateChallenge(IBinder token) {
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700338 checkPermission(MANAGE_BIOMETRIC);
Kevin Chynd79e24e2018-09-25 12:06:59 -0700339 return startGenerateChallenge(token);
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200340 }
341
342 @Override // Binder call
Kevin Chynd79e24e2018-09-25 12:06:59 -0700343 public int revokeChallenge(IBinder token) {
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700344 checkPermission(MANAGE_BIOMETRIC);
Kevin Chynd79e24e2018-09-25 12:06:59 -0700345 return startRevokeChallenge(token);
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200346 }
347
348 @Override // Binder call
Kevin Chyn593e6262019-06-28 13:24:44 -0700349 public void enroll(int userId, final IBinder token, final byte[] cryptoToken,
Kevin Chyn1f16c2d2018-12-07 13:06:08 -0800350 final IFaceServiceReceiver receiver, final String opPackageName,
351 final int[] disabledFeatures) {
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700352 checkPermission(MANAGE_BIOMETRIC);
Kevin Chyn593e6262019-06-28 13:24:44 -0700353 updateActiveGroup(userId, opPackageName);
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200354
Kevin Chyndbfbed42019-06-13 17:01:30 -0700355 mNotificationManager.cancelAsUser(NOTIFICATION_TAG, NOTIFICATION_ID,
356 UserHandle.CURRENT);
357
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200358 final boolean restricted = isRestricted();
Kevin Chyna56dff72018-06-19 18:41:12 -0700359 final EnrollClientImpl client = new EnrollClientImpl(getContext(), mDaemonWrapper,
360 mHalDeviceId, token, new ServiceListenerImpl(receiver), mCurrentUserId,
Kevin Chyn1429a312019-01-28 16:08:09 -0800361 0 /* groupId */, cryptoToken, restricted, opPackageName, disabledFeatures) {
Kevin Chyn4cc49f72019-04-24 13:53:35 -0700362
363 @Override
364 public int[] getAcquireIgnorelist() {
365 return mEnrollIgnoreList;
366 }
367
368 @Override
369 public int[] getAcquireVendorIgnorelist() {
370 return mEnrollIgnoreListVendor;
371 }
372
Kevin Chyn1429a312019-01-28 16:08:09 -0800373 @Override
374 public boolean shouldVibrate() {
375 return false;
376 }
Kevin Chyn7782d142019-01-18 12:51:33 -0800377
378 @Override
379 protected int statsModality() {
380 return FaceService.this.statsModality();
381 }
Kevin Chyn1429a312019-01-28 16:08:09 -0800382 };
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200383
Kevin Chyn1a878c12019-04-04 15:50:11 -0700384 enrollInternal(client, mCurrentUserId);
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200385 }
386
387 @Override // Binder call
388 public void cancelEnrollment(final IBinder token) {
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700389 checkPermission(MANAGE_BIOMETRIC);
Kevin Chyna56dff72018-06-19 18:41:12 -0700390 cancelEnrollmentInternal(token);
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200391 }
392
393 @Override // Binder call
Kevin Chyn747e29b2019-01-11 17:01:53 -0800394 public void authenticate(final IBinder token, final long opId, int userId,
Kevin Chyna56dff72018-06-19 18:41:12 -0700395 final IFaceServiceReceiver receiver, final int flags,
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700396 final String opPackageName) {
397 checkPermission(USE_BIOMETRIC_INTERNAL);
Kevin Chyn747e29b2019-01-11 17:01:53 -0800398 updateActiveGroup(userId, opPackageName);
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200399 final boolean restricted = isRestricted();
Kevin Chyn8b7a0372018-09-17 15:06:05 -0700400 final AuthenticationClientImpl client = new FaceAuthClient(getContext(),
Kevin Chyna56dff72018-06-19 18:41:12 -0700401 mDaemonWrapper, mHalDeviceId, token, new ServiceListenerImpl(receiver),
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700402 mCurrentUserId, 0 /* groupId */, opId, restricted, opPackageName,
Kevin Chyn87f257a2018-11-27 16:26:07 -0800403 0 /* cookie */, false /* requireConfirmation */);
Kevin Chyna56dff72018-06-19 18:41:12 -0700404 authenticateInternal(client, opId, opPackageName);
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200405 }
406
407 @Override // Binder call
Kevin Chyn87f257a2018-11-27 16:26:07 -0800408 public void prepareForAuthentication(boolean requireConfirmation, IBinder token, long opId,
Kevin Chyn23289ef2018-11-28 16:32:36 -0800409 int groupId, IBiometricServiceReceiverInternal wrapperReceiver,
410 String opPackageName, int cookie, int callingUid, int callingPid,
411 int callingUserId) {
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700412 checkPermission(USE_BIOMETRIC_INTERNAL);
Kevin Chyn41a80902019-02-06 08:12:15 -0800413 updateActiveGroup(groupId, opPackageName);
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700414 final boolean restricted = true; // BiometricPrompt is always restricted
Kevin Chyn8b7a0372018-09-17 15:06:05 -0700415 final AuthenticationClientImpl client = new FaceAuthClient(getContext(),
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700416 mDaemonWrapper, mHalDeviceId, token,
Kevin Chyn87f257a2018-11-27 16:26:07 -0800417 new BiometricPromptServiceListenerImpl(wrapperReceiver),
418 mCurrentUserId, 0 /* groupId */, opId, restricted, opPackageName, cookie,
Kevin Chyn158fefb2019-01-03 18:59:05 -0800419 requireConfirmation);
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700420 authenticateInternal(client, opId, opPackageName, callingUid, callingPid,
421 callingUserId);
422 }
423
424 @Override // Binder call
Kevin Chyn87f257a2018-11-27 16:26:07 -0800425 public void startPreparedClient(int cookie) {
426 checkPermission(MANAGE_BIOMETRIC);
427 startCurrentClient(cookie);
428 }
429
430 @Override // Binder call
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200431 public void cancelAuthentication(final IBinder token, final String opPackageName) {
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700432 checkPermission(USE_BIOMETRIC_INTERNAL);
Kevin Chyna56dff72018-06-19 18:41:12 -0700433 cancelAuthenticationInternal(token, opPackageName);
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200434 }
435
436 @Override // Binder call
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700437 public void cancelAuthenticationFromService(final IBinder token, final String opPackageName,
Kevin Chyne92cdae2018-11-21 16:35:04 -0800438 int callingUid, int callingPid, int callingUserId, boolean fromClient) {
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700439 checkPermission(USE_BIOMETRIC_INTERNAL);
Kevin Chyne92cdae2018-11-21 16:35:04 -0800440 cancelAuthenticationInternal(token, opPackageName, callingUid, callingPid,
441 callingUserId, fromClient);
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700442 }
443
444 @Override // Binder call
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200445 public void setActiveUser(final int userId) {
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700446 checkPermission(MANAGE_BIOMETRIC);
Kevin Chyna56dff72018-06-19 18:41:12 -0700447 setActiveUserInternal(userId);
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200448 }
449
450 @Override // Binder call
Kevin Chyna56dff72018-06-19 18:41:12 -0700451 public void remove(final IBinder token, final int faceId, final int userId,
Kevin Chyn593e6262019-06-28 13:24:44 -0700452 final IFaceServiceReceiver receiver, final String opPackageName) {
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700453 checkPermission(MANAGE_BIOMETRIC);
Kevin Chyn593e6262019-06-28 13:24:44 -0700454 updateActiveGroup(userId, opPackageName);
Kevin Chyna56dff72018-06-19 18:41:12 -0700455
456 if (token == null) {
457 Slog.w(TAG, "remove(): token is null");
458 return;
459 }
460
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200461 final boolean restricted = isRestricted();
Kevin Chyn4cc49f72019-04-24 13:53:35 -0700462 final RemovalClient client = new RemovalClient(getContext(), getConstants(),
Kevin Chyn6737c572019-02-08 16:10:54 -0800463 mDaemonWrapper, mHalDeviceId, token, new ServiceListenerImpl(receiver), faceId,
464 0 /* groupId */, userId, restricted, token.toString(), getBiometricUtils()) {
Kevin Chyn7782d142019-01-18 12:51:33 -0800465 @Override
466 protected int statsModality() {
467 return FaceService.this.statsModality();
468 }
469 };
Kevin Chyna56dff72018-06-19 18:41:12 -0700470 removeInternal(client);
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200471 }
472
Kevin Chyna56dff72018-06-19 18:41:12 -0700473 @Override
474 public void enumerate(final IBinder token, final int userId,
475 final IFaceServiceReceiver receiver) {
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700476 checkPermission(MANAGE_BIOMETRIC);
Kevin Chyna56dff72018-06-19 18:41:12 -0700477
478 final boolean restricted = isRestricted();
Kevin Chyn4cc49f72019-04-24 13:53:35 -0700479 final EnumerateClient client = new EnumerateClient(getContext(), getConstants(),
Kevin Chyn6737c572019-02-08 16:10:54 -0800480 mDaemonWrapper, mHalDeviceId, token, new ServiceListenerImpl(receiver), userId,
481 userId, restricted, getContext().getOpPackageName()) {
Kevin Chyn7782d142019-01-18 12:51:33 -0800482 @Override
483 protected int statsModality() {
484 return FaceService.this.statsModality();
485 }
486 };
Kevin Chyna56dff72018-06-19 18:41:12 -0700487 enumerateInternal(client);
488 }
489
490 @Override
491 public void addLockoutResetCallback(final IBiometricServiceLockoutResetCallback callback)
492 throws RemoteException {
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700493 checkPermission(USE_BIOMETRIC_INTERNAL);
Kevin Chyna56dff72018-06-19 18:41:12 -0700494 FaceService.super.addLockoutResetCallback(callback);
495 }
496
497 @Override // Binder call
498 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
499 if (!DumpUtils.checkDumpPermission(getContext(), TAG, pw)) {
500 return;
501 }
502
503 final long ident = Binder.clearCallingIdentity();
504 try {
Joe Onoratodb396002019-04-05 19:49:27 -0700505 if (args.length > 1 && "--hal".equals(args[0])) {
506 dumpHal(fd, Arrays.copyOfRange(args, 1, args.length, args.getClass()));
Kevin Chyna56dff72018-06-19 18:41:12 -0700507 } else {
508 dumpInternal(pw);
509 }
510 } finally {
511 Binder.restoreCallingIdentity(ident);
512 }
513 }
514
515 /**
516 * The following methods don't use any common code from BiometricService
517 */
518
519 // TODO: refactor out common code here
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200520 @Override // Binder call
521 public boolean isHardwareDetected(long deviceId, String opPackageName) {
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700522 checkPermission(USE_BIOMETRIC_INTERNAL);
Kevin Chyna56dff72018-06-19 18:41:12 -0700523 if (!canUseBiometric(opPackageName, false /* foregroundOnly */,
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200524 Binder.getCallingUid(), Binder.getCallingPid(),
525 UserHandle.getCallingUserId())) {
526 return false;
527 }
528
529 final long token = Binder.clearCallingIdentity();
530 try {
531 IBiometricsFace daemon = getFaceDaemon();
532 return daemon != null && mHalDeviceId != 0;
533 } finally {
534 Binder.restoreCallingIdentity(token);
535 }
536 }
537
538 @Override // Binder call
Kevin Chyna56dff72018-06-19 18:41:12 -0700539 public void rename(final int faceId, final String name) {
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700540 checkPermission(MANAGE_BIOMETRIC);
Kevin Chyna56dff72018-06-19 18:41:12 -0700541 if (!isCurrentUserOrProfile(UserHandle.getCallingUserId())) {
542 return;
543 }
544 mHandler.post(new Runnable() {
545 @Override
546 public void run() {
547 getBiometricUtils().renameBiometricForUser(getContext(), mCurrentUserId,
548 faceId, name);
549 }
550 });
551 }
552
553 @Override // Binder call
554 public List<Face> getEnrolledFaces(int userId, String opPackageName) {
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700555 checkPermission(MANAGE_BIOMETRIC);
Kevin Chyna56dff72018-06-19 18:41:12 -0700556 if (!canUseBiometric(opPackageName, false /* foregroundOnly */,
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200557 Binder.getCallingUid(), Binder.getCallingPid(),
558 UserHandle.getCallingUserId())) {
559 return null;
560 }
561
Kevin Chyn6737c572019-02-08 16:10:54 -0800562 return FaceService.this.getEnrolledTemplates(userId);
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200563 }
564
565 @Override // Binder call
Kevin Chyna56dff72018-06-19 18:41:12 -0700566 public boolean hasEnrolledFaces(int userId, String opPackageName) {
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700567 checkPermission(USE_BIOMETRIC_INTERNAL);
Kevin Chyna56dff72018-06-19 18:41:12 -0700568 if (!canUseBiometric(opPackageName, false /* foregroundOnly */,
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200569 Binder.getCallingUid(), Binder.getCallingPid(),
570 UserHandle.getCallingUserId())) {
571 return false;
572 }
573
Kevin Chyna56dff72018-06-19 18:41:12 -0700574 return FaceService.this.hasEnrolledBiometrics(userId);
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200575 }
576
577 @Override // Binder call
578 public long getAuthenticatorId(String opPackageName) {
579 // In this method, we're not checking whether the caller is permitted to use face
580 // API because current authenticator ID is leaked (in a more contrived way) via Android
581 // Keystore (android.security.keystore package): the user of that API can create a key
582 // which requires face authentication for its use, and then query the key's
583 // characteristics (hidden API) which returns, among other things, face
584 // authenticator ID which was active at key creation time.
585 //
586 // Reason: The part of Android Keystore which runs inside an app's process invokes this
587 // method in certain cases. Those cases are not always where the developer demonstrates
588 // explicit intent to use face functionality. Thus, to avoiding throwing an
589 // unexpected SecurityException this method does not check whether its caller is
590 // permitted to use face API.
591 //
592 // The permission check should be restored once Android Keystore no longer invokes this
593 // method from inside app processes.
594
595 return FaceService.this.getAuthenticatorId(opPackageName);
596 }
597
598 @Override // Binder call
Kevin Chyna38653c2019-02-11 17:46:21 -0800599 public void resetLockout(byte[] token) {
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700600 checkPermission(MANAGE_BIOMETRIC);
Kevin Chyn1d6a2862019-04-02 16:20:21 -0700601
602 if (!FaceService.this.hasEnrolledBiometrics(mCurrentUserId)) {
603 Slog.w(TAG, "Ignoring lockout reset, no templates enrolled");
604 return;
605 }
606
Kevin Chynbe67ce02019-06-10 16:14:22 -0700607 Slog.d(TAG, "Resetting lockout for user: " + mCurrentUserId);
608
Kevin Chyna38653c2019-02-11 17:46:21 -0800609 try {
610 mDaemonWrapper.resetLockout(token);
611 } catch (RemoteException e) {
612 Slog.e(getTag(), "Unable to reset lockout", e);
613 }
Kevin Chyna56dff72018-06-19 18:41:12 -0700614 }
Kevin Chynd79e24e2018-09-25 12:06:59 -0700615
616 @Override
Kevin Chyn593e6262019-06-28 13:24:44 -0700617 public void setFeature(int userId, int feature, boolean enabled, final byte[] token,
618 IFaceServiceReceiver receiver, final String opPackageName) {
Kevin Chynd79e24e2018-09-25 12:06:59 -0700619 checkPermission(MANAGE_BIOMETRIC);
Kevin Chyn593e6262019-06-28 13:24:44 -0700620 updateActiveGroup(userId, opPackageName);
Kevin Chynd79e24e2018-09-25 12:06:59 -0700621
Kevin Chyne62749a2019-04-02 19:33:56 -0700622 mHandler.post(() -> {
623 if (!FaceService.this.hasEnrolledBiometrics(mCurrentUserId)) {
624 Slog.e(TAG, "No enrolled biometrics while setting feature: " + feature);
625 return;
Kevin Chynb95f1522019-03-04 16:45:15 -0800626 }
Kevin Chyne62749a2019-04-02 19:33:56 -0700627
628 final ArrayList<Byte> byteToken = new ArrayList<>();
629 for (int i = 0; i < token.length; i++) {
630 byteToken.add(token[i]);
631 }
632
633 // TODO: Support multiple faces
634 final int faceId = getFirstTemplateForUser(mCurrentUserId);
635
636 if (mDaemon != null) {
637 try {
638 final int result = mDaemon.setFeature(feature, enabled, byteToken, faceId);
639 receiver.onFeatureSet(result == Status.OK, feature);
640 } catch (RemoteException e) {
641 Slog.e(getTag(), "Unable to set feature: " + feature
642 + " to enabled:" + enabled, e);
643 }
644 }
645 });
646
Kevin Chynd79e24e2018-09-25 12:06:59 -0700647 }
648
649 @Override
Kevin Chyn593e6262019-06-28 13:24:44 -0700650 public void getFeature(int userId, int feature, IFaceServiceReceiver receiver,
651 final String opPackageName) {
Kevin Chynd79e24e2018-09-25 12:06:59 -0700652 checkPermission(MANAGE_BIOMETRIC);
Kevin Chyn593e6262019-06-28 13:24:44 -0700653 updateActiveGroup(userId, opPackageName);
Kevin Chynd79e24e2018-09-25 12:06:59 -0700654
Kevin Chyne62749a2019-04-02 19:33:56 -0700655 mHandler.post(() -> {
656 // This should ideally return tri-state, but the user isn't shown settings unless
657 // they are enrolled so it's fine for now.
658 if (!FaceService.this.hasEnrolledBiometrics(mCurrentUserId)) {
659 Slog.e(TAG, "No enrolled biometrics while getting feature: " + feature);
660 return;
Kevin Chynb95f1522019-03-04 16:45:15 -0800661 }
Kevin Chyne62749a2019-04-02 19:33:56 -0700662
663 // TODO: Support multiple faces
664 final int faceId = getFirstTemplateForUser(mCurrentUserId);
665
666 if (mDaemon != null) {
667 try {
668 OptionalBool result = mDaemon.getFeature(feature, faceId);
669 receiver.onFeatureGet(result.status == Status.OK, feature, result.value);
670 } catch (RemoteException e) {
671 Slog.e(getTag(), "Unable to getRequireAttention", e);
672 }
673 }
674 });
675
Kevin Chynd79e24e2018-09-25 12:06:59 -0700676 }
Kevin Chyn57f119b2018-10-25 12:03:41 -0700677
678 @Override
679 public void userActivity() {
680 checkPermission(MANAGE_BIOMETRIC);
681
682 if (mDaemon != null) {
683 try {
684 mDaemon.userActivity();
685 } catch (RemoteException e) {
686 Slog.e(getTag(), "Unable to send userActivity", e);
687 }
688 }
689 }
Kevin Chynb95f1522019-03-04 16:45:15 -0800690
691 // TODO: Support multiple faces
692 private int getFirstTemplateForUser(int user) {
693 final List<Face> faces = FaceService.this.getEnrolledTemplates(user);
694 if (!faces.isEmpty()) {
695 return faces.get(0).getBiometricId();
696 }
697 return 0;
698 }
Kevin Chyna56dff72018-06-19 18:41:12 -0700699 }
700
701 /**
702 * Receives callbacks from the ClientMonitor implementations. The results are forwarded to
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700703 * BiometricPrompt.
704 */
Kevin Chyne92cdae2018-11-21 16:35:04 -0800705 private class BiometricPromptServiceListenerImpl extends BiometricServiceListener {
Kevin Chyn23289ef2018-11-28 16:32:36 -0800706 BiometricPromptServiceListenerImpl(IBiometricServiceReceiverInternal wrapperReceiver) {
Kevin Chyn87f257a2018-11-27 16:26:07 -0800707 super(wrapperReceiver);
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700708 }
709
710 @Override
711 public void onAcquired(long deviceId, int acquiredInfo, int vendorCode)
712 throws RemoteException {
713 /**
714 * Map the acquired codes onto existing {@link BiometricConstants} acquired codes.
715 */
Kevin Chyne92cdae2018-11-21 16:35:04 -0800716 if (getWrapperReceiver() != null) {
717 getWrapperReceiver().onAcquired(
Kevin Chyn8b7a0372018-09-17 15:06:05 -0700718 FaceManager.getMappedAcquiredInfo(acquiredInfo, vendorCode),
719 FaceManager.getAcquiredString(getContext(), acquiredInfo, vendorCode));
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700720 }
721 }
722
723 @Override
Kevin Chyn87f257a2018-11-27 16:26:07 -0800724 public void onError(long deviceId, int error, int vendorCode, int cookie)
725 throws RemoteException {
Kevin Chyne92cdae2018-11-21 16:35:04 -0800726 if (getWrapperReceiver() != null) {
Kevin Chyn23289ef2018-11-28 16:32:36 -0800727 getWrapperReceiver().onError(cookie, error,
728 FaceManager.getErrorString(getContext(), error, vendorCode));
Kevin Chyna24e9fd2018-08-27 12:39:17 -0700729 }
730 }
731 }
732
733 /**
734 * Receives callbacks from the ClientMonitor implementations. The results are forwarded to
Kevin Chyna56dff72018-06-19 18:41:12 -0700735 * the FaceManager.
736 */
737 private class ServiceListenerImpl implements ServiceListener {
Kevin Chyna56dff72018-06-19 18:41:12 -0700738 private IFaceServiceReceiver mFaceServiceReceiver;
739
740 public ServiceListenerImpl(IFaceServiceReceiver receiver) {
741 mFaceServiceReceiver = receiver;
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200742 }
743
744 @Override
Kevin Chyna56dff72018-06-19 18:41:12 -0700745 public void onEnrollResult(BiometricAuthenticator.Identifier identifier, int remaining)
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200746 throws RemoteException {
Kevin Chyna56dff72018-06-19 18:41:12 -0700747 if (mFaceServiceReceiver != null) {
748 mFaceServiceReceiver.onEnrollResult(identifier.getDeviceId(),
749 identifier.getBiometricId(),
750 remaining);
751 }
Gilad Brettercb51b8b2018-03-22 17:04:51 +0200752 }
Kevin Chyna56dff72018-06-19 18:41:12 -0700753
754 @Override
755 public void onAcquired(long deviceId, int acquiredInfo, int vendorCode)
756 throws RemoteException {
757 if (mFaceServiceReceiver != null) {
758 mFaceServiceReceiver.onAcquired(deviceId, acquiredInfo, vendorCode);
759 }
760 }
761
762 @Override
763 public void onAuthenticationSucceeded(long deviceId,
764 BiometricAuthenticator.Identifier biometric, int userId)
765 throws RemoteException {
766 if (mFaceServiceReceiver != null) {
Kevin Chyn628b7182018-11-13 12:00:48 -0800767 if (biometric == null || biometric instanceof Face) {
Kevin Chyn8d2694a2019-04-11 18:30:40 -0700768 mFaceServiceReceiver.onAuthenticationSucceeded(deviceId, (Face) biometric,
769 userId);
Kevin Chyna56dff72018-06-19 18:41:12 -0700770 } else {
771 Slog.e(TAG, "onAuthenticationSucceeded received non-face biometric");
772 }
773 }
774 }
775
776 @Override
777 public void onAuthenticationFailed(long deviceId) throws RemoteException {
778 if (mFaceServiceReceiver != null) {
779 mFaceServiceReceiver.onAuthenticationFailed(deviceId);
780 }
781 }
782
783 @Override
Kevin Chyn87f257a2018-11-27 16:26:07 -0800784 public void onError(long deviceId, int error, int vendorCode, int cookie)
785 throws RemoteException {
Kevin Chyna56dff72018-06-19 18:41:12 -0700786 if (mFaceServiceReceiver != null) {
787 mFaceServiceReceiver.onError(deviceId, error, vendorCode);
788 }
789 }
790
791 @Override
792 public void onRemoved(BiometricAuthenticator.Identifier identifier,
793 int remaining) throws RemoteException {
794 if (mFaceServiceReceiver != null) {
795 mFaceServiceReceiver.onRemoved(identifier.getDeviceId(),
796 identifier.getBiometricId(), remaining);
797 }
798 }
799
800 @Override
801 public void onEnumerated(BiometricAuthenticator.Identifier identifier, int remaining)
802 throws RemoteException {
803 if (mFaceServiceReceiver != null) {
Kevin Chyn6737c572019-02-08 16:10:54 -0800804 mFaceServiceReceiver.onEnumerated(identifier.getDeviceId(),
805 identifier.getBiometricId(), remaining);
Kevin Chyna56dff72018-06-19 18:41:12 -0700806 }
807 }
808 }
809
Kevin Chyn4cc49f72019-04-24 13:53:35 -0700810 private final FaceConstants mFaceConstants = new FaceConstants();
Kevin Chyna56dff72018-06-19 18:41:12 -0700811
812 @GuardedBy("this")
813 private IBiometricsFace mDaemon;
Kevin Chyn9bc1d492019-06-21 15:31:50 -0700814 private UsageStats mUsageStats;
Kevin Chyna38653c2019-02-11 17:46:21 -0800815 // One of the AuthenticationClient constants
816 private int mCurrentUserLockoutMode;
Kevin Chyna56dff72018-06-19 18:41:12 -0700817
Kevin Chyndbfbed42019-06-13 17:01:30 -0700818 private NotificationManager mNotificationManager;
819
Kevin Chyn4cc49f72019-04-24 13:53:35 -0700820 private int[] mBiometricPromptIgnoreList;
821 private int[] mBiometricPromptIgnoreListVendor;
822 private int[] mKeyguardIgnoreList;
823 private int[] mKeyguardIgnoreListVendor;
824 private int[] mEnrollIgnoreList;
825 private int[] mEnrollIgnoreListVendor;
826
Kevin Chyna56dff72018-06-19 18:41:12 -0700827 /**
828 * Receives callbacks from the HAL.
829 */
830 private IBiometricsFaceClientCallback mDaemonCallback =
831 new IBiometricsFaceClientCallback.Stub() {
Kevin Chyn6737c572019-02-08 16:10:54 -0800832 @Override
833 public void onEnrollResult(final long deviceId, int faceId, int userId,
834 int remaining) {
835 mHandler.post(() -> {
836 final Face face = new Face(getBiometricUtils()
837 .getUniqueName(getContext(), userId), faceId, deviceId);
838 FaceService.super.handleEnrollResult(face, remaining);
Kevin Chyn576811e2019-06-12 14:50:35 -0700839
840 // Enrollment changes the authenticatorId, so update it here.
841 IBiometricsFace daemon = getFaceDaemon();
842 if (remaining == 0 && daemon != null) {
843 try {
844 mAuthenticatorIds.put(userId,
845 hasEnrolledBiometrics(userId) ? daemon.getAuthenticatorId().value
846 : 0L);
847 } catch (RemoteException e) {
848 Slog.e(TAG, "Unable to get authenticatorId", e);
849 }
850 }
Kevin Chyn6737c572019-02-08 16:10:54 -0800851 });
852 }
853
854 @Override
855 public void onAcquired(final long deviceId, final int userId,
856 final int acquiredInfo,
857 final int vendorCode) {
858 mHandler.post(() -> {
859 FaceService.super.handleAcquired(deviceId, acquiredInfo, vendorCode);
860 });
861 }
862
863 @Override
864 public void onAuthenticated(final long deviceId, final int faceId, final int userId,
865 ArrayList<Byte> token) {
866 mHandler.post(() -> {
867 Face face = new Face("", faceId, deviceId);
868 FaceService.super.handleAuthenticated(face, token);
869 });
870 }
871
872 @Override
873 public void onError(final long deviceId, final int userId, final int error,
874 final int vendorCode) {
875 mHandler.post(() -> {
876 FaceService.super.handleError(deviceId, error, vendorCode);
877
878 // TODO: this chunk of code should be common to all biometric services
879 if (error == BiometricConstants.BIOMETRIC_ERROR_HW_UNAVAILABLE) {
880 // If we get HW_UNAVAILABLE, try to connect again later...
881 Slog.w(TAG, "Got ERROR_HW_UNAVAILABLE; try reconnecting next client.");
882 synchronized (this) {
883 mDaemon = null;
884 mHalDeviceId = 0;
885 mCurrentUserId = UserHandle.USER_NULL;
886 }
Kevin Chyna56dff72018-06-19 18:41:12 -0700887 }
Kevin Chyn6737c572019-02-08 16:10:54 -0800888 });
889 }
Kevin Chyna56dff72018-06-19 18:41:12 -0700890
Kevin Chyn6737c572019-02-08 16:10:54 -0800891 @Override
Kevin Chyn31ae8d22019-03-11 14:12:32 -0700892 public void onRemoved(final long deviceId, ArrayList<Integer> faceIds, final int userId) {
Kevin Chyn6737c572019-02-08 16:10:54 -0800893 mHandler.post(() -> {
Kevin Chyn31ae8d22019-03-11 14:12:32 -0700894 if (!faceIds.isEmpty()) {
895 for (int i = 0; i < faceIds.size(); i++) {
896 final Face face = new Face("", faceIds.get(i), deviceId);
897 // Convert to old behavior
898 FaceService.super.handleRemoved(face, faceIds.size() - i - 1);
899 }
900 } else {
901 final Face face = new Face("", 0 /* identifier */, deviceId);
902 FaceService.super.handleRemoved(face, 0 /* remaining */);
903 }
904
Kevin Chyn6737c572019-02-08 16:10:54 -0800905 });
906 }
907
908 @Override
909 public void onEnumerate(long deviceId, ArrayList<Integer> faceIds, int userId)
910 throws RemoteException {
911 mHandler.post(() -> {
912 if (!faceIds.isEmpty()) {
913 for (int i = 0; i < faceIds.size(); i++) {
914 final Face face = new Face("", faceIds.get(i), deviceId);
915 // Convert to old old behavior
916 FaceService.super.handleEnumerate(face, faceIds.size() - i - 1);
917 }
918 } else {
919 // For face, the HIDL contract is to receive an empty list when there are no
920 // templates enrolled. Send a null identifier since we don't consume them
921 // anywhere, and send remaining == 0 to plumb this with existing common code.
922 FaceService.super.handleEnumerate(null /* identifier */, 0);
Kevin Chyna56dff72018-06-19 18:41:12 -0700923 }
Kevin Chyn6737c572019-02-08 16:10:54 -0800924 });
925 }
Kevin Chyna56dff72018-06-19 18:41:12 -0700926
Kevin Chyn6737c572019-02-08 16:10:54 -0800927 @Override
928 public void onLockoutChanged(long duration) {
Kevin Chyna38653c2019-02-11 17:46:21 -0800929 Slog.d(TAG, "onLockoutChanged: " + duration);
930 if (duration == 0) {
931 mCurrentUserLockoutMode = AuthenticationClient.LOCKOUT_NONE;
932 } else if (duration == Long.MAX_VALUE) {
933 mCurrentUserLockoutMode = AuthenticationClient.LOCKOUT_PERMANENT;
934 } else {
935 mCurrentUserLockoutMode = AuthenticationClient.LOCKOUT_TIMED;
936 }
Kevin Chyna56dff72018-06-19 18:41:12 -0700937
Kevin Chyna38653c2019-02-11 17:46:21 -0800938 mHandler.post(() -> {
939 if (duration == 0) {
940 notifyLockoutResetMonitors();
941 }
942 });
Kevin Chyn6737c572019-02-08 16:10:54 -0800943 }
944 };
Kevin Chyna56dff72018-06-19 18:41:12 -0700945
946 /**
947 * Wraps the HAL-specific code and is passed to the ClientMonitor implementations so that they
948 * can be shared between the multiple biometric services.
949 */
950 private final DaemonWrapper mDaemonWrapper = new DaemonWrapper() {
951 @Override
952 public int authenticate(long operationId, int groupId) throws RemoteException {
953 IBiometricsFace daemon = getFaceDaemon();
954 if (daemon == null) {
955 Slog.w(TAG, "authenticate(): no face HAL!");
956 return ERROR_ESRCH;
957 }
958 return daemon.authenticate(operationId);
959 }
960
961 @Override
962 public int cancel() throws RemoteException {
963 IBiometricsFace daemon = getFaceDaemon();
964 if (daemon == null) {
965 Slog.w(TAG, "cancel(): no face HAL!");
966 return ERROR_ESRCH;
967 }
968 return daemon.cancel();
969 }
970
971 @Override
972 public int remove(int groupId, int biometricId) throws RemoteException {
973 IBiometricsFace daemon = getFaceDaemon();
974 if (daemon == null) {
975 Slog.w(TAG, "remove(): no face HAL!");
976 return ERROR_ESRCH;
977 }
978 return daemon.remove(biometricId);
979 }
980
981 @Override
982 public int enumerate() throws RemoteException {
983 IBiometricsFace daemon = getFaceDaemon();
984 if (daemon == null) {
985 Slog.w(TAG, "enumerate(): no face HAL!");
986 return ERROR_ESRCH;
987 }
988 return daemon.enumerate();
989 }
990
991 @Override
Kevin Chyn1f16c2d2018-12-07 13:06:08 -0800992 public int enroll(byte[] cryptoToken, int groupId, int timeout,
993 ArrayList<Integer> disabledFeatures) throws RemoteException {
Kevin Chyna56dff72018-06-19 18:41:12 -0700994 IBiometricsFace daemon = getFaceDaemon();
995 if (daemon == null) {
996 Slog.w(TAG, "enroll(): no face HAL!");
997 return ERROR_ESRCH;
998 }
999 final ArrayList<Byte> token = new ArrayList<>();
1000 for (int i = 0; i < cryptoToken.length; i++) {
1001 token.add(cryptoToken[i]);
1002 }
Kevin Chyn1f16c2d2018-12-07 13:06:08 -08001003 return daemon.enroll(token, timeout, disabledFeatures);
Kevin Chyna56dff72018-06-19 18:41:12 -07001004 }
Kevin Chyna38653c2019-02-11 17:46:21 -08001005
1006 @Override
1007 public void resetLockout(byte[] cryptoToken) throws RemoteException {
1008 IBiometricsFace daemon = getFaceDaemon();
1009 if (daemon == null) {
1010 Slog.w(TAG, "resetLockout(): no face HAL!");
1011 return;
1012 }
1013 final ArrayList<Byte> token = new ArrayList<>();
1014 for (int i = 0; i < cryptoToken.length; i++) {
1015 token.add(cryptoToken[i]);
1016 }
1017 daemon.resetLockout(token);
1018 }
Kevin Chyna56dff72018-06-19 18:41:12 -07001019 };
1020
1021
1022 public FaceService(Context context) {
1023 super(context);
Kevin Chyn4cc49f72019-04-24 13:53:35 -07001024
Kevin Chyn9bc1d492019-06-21 15:31:50 -07001025 mUsageStats = new UsageStats(context);
1026
Kevin Chyndbfbed42019-06-13 17:01:30 -07001027 mNotificationManager = getContext().getSystemService(NotificationManager.class);
1028
Kevin Chyn4cc49f72019-04-24 13:53:35 -07001029 mBiometricPromptIgnoreList = getContext().getResources()
1030 .getIntArray(R.array.config_face_acquire_biometricprompt_ignorelist);
1031 mBiometricPromptIgnoreListVendor = getContext().getResources()
1032 .getIntArray(R.array.config_face_acquire_vendor_biometricprompt_ignorelist);
1033 mKeyguardIgnoreList = getContext().getResources()
1034 .getIntArray(R.array.config_face_acquire_keyguard_ignorelist);
1035 mKeyguardIgnoreListVendor = getContext().getResources()
1036 .getIntArray(R.array.config_face_acquire_vendor_keyguard_ignorelist);
1037 mEnrollIgnoreList = getContext().getResources()
1038 .getIntArray(R.array.config_face_acquire_enroll_ignorelist);
1039 mEnrollIgnoreListVendor = getContext().getResources()
1040 .getIntArray(R.array.config_face_acquire_vendor_enroll_ignorelist);
Kevin Chyna56dff72018-06-19 18:41:12 -07001041 }
1042
1043 @Override
1044 public void onStart() {
Kevin Chyn5a2ff5d2018-08-29 19:07:30 -07001045 super.onStart();
Kevin Chyna56dff72018-06-19 18:41:12 -07001046 publishBinderService(Context.FACE_SERVICE, new FaceServiceWrapper());
Kevin Chyn8398a712019-06-13 16:44:50 -07001047 // Get the face daemon on FaceService's on thread so SystemServerInitThreadPool isn't
1048 // blocked
1049 SystemServerInitThreadPool.get().submit(() -> mHandler.post(this::getFaceDaemon),
1050 TAG + ".onStart");
Kevin Chyna56dff72018-06-19 18:41:12 -07001051 }
1052
1053 @Override
1054 public String getTag() {
1055 return TAG;
1056 }
1057
1058 @Override
Kevin Chyn6737c572019-02-08 16:10:54 -08001059 protected DaemonWrapper getDaemonWrapper() {
1060 return mDaemonWrapper;
1061 }
1062
1063 @Override
Kevin Chyna56dff72018-06-19 18:41:12 -07001064 protected BiometricUtils getBiometricUtils() {
1065 return FaceUtils.getInstance();
1066 }
1067
1068 @Override
Kevin Chyn4cc49f72019-04-24 13:53:35 -07001069 protected Constants getConstants() {
1070 return mFaceConstants;
Kevin Chyna56dff72018-06-19 18:41:12 -07001071 }
1072
1073 @Override
1074 protected boolean hasReachedEnrollmentLimit(int userId) {
1075 final int limit = getContext().getResources().getInteger(
Kevin Chyn017e76e2018-06-27 18:35:06 -07001076 com.android.internal.R.integer.config_faceMaxTemplatesPerUser);
Kevin Chyn6737c572019-02-08 16:10:54 -08001077 final int enrolled = FaceService.this.getEnrolledTemplates(userId).size();
Kevin Chyna56dff72018-06-19 18:41:12 -07001078 if (enrolled >= limit) {
Kevin Chyn1a878c12019-04-04 15:50:11 -07001079 Slog.w(TAG, "Too many faces registered, user: " + userId);
Kevin Chyna56dff72018-06-19 18:41:12 -07001080 return true;
1081 }
1082 return false;
1083 }
1084
1085 @Override
Kevin Chyn9ba99912019-01-16 16:24:36 -08001086 public void serviceDied(long cookie) {
1087 super.serviceDied(cookie);
1088 mDaemon = null;
Jim Miller7b78b222019-02-07 16:47:38 -08001089
1090 mCurrentUserId = UserHandle.USER_NULL; // Force updateActiveGroup() to re-evaluate
Kevin Chyn9ba99912019-01-16 16:24:36 -08001091 }
1092
1093 @Override
Kevin Chyna56dff72018-06-19 18:41:12 -07001094 protected void updateActiveGroup(int userId, String clientPackage) {
1095 IBiometricsFace daemon = getFaceDaemon();
1096
1097 if (daemon != null) {
1098 try {
1099 userId = getUserOrWorkProfileId(clientPackage, userId);
1100 if (userId != mCurrentUserId) {
Kevin Chyn27e33d02019-04-30 01:37:32 +00001101 final File baseDir = Environment.getDataVendorDeDirectory(userId);
Kevin Chyna56dff72018-06-19 18:41:12 -07001102 final File faceDir = new File(baseDir, FACE_DATA_DIR);
1103 if (!faceDir.exists()) {
1104 if (!faceDir.mkdir()) {
1105 Slog.v(TAG, "Cannot make directory: " + faceDir.getAbsolutePath());
1106 return;
1107 }
1108 // Calling mkdir() from this process will create a directory with our
1109 // permissions (inherited from the containing dir). This command fixes
1110 // the label.
1111 if (!SELinux.restorecon(faceDir)) {
1112 Slog.w(TAG, "Restorecons failed. Directory will have wrong label.");
1113 return;
1114 }
1115 }
1116
1117 daemon.setActiveUser(userId, faceDir.getAbsolutePath());
1118 mCurrentUserId = userId;
Kevin Chyn576811e2019-06-12 14:50:35 -07001119 mAuthenticatorIds.put(userId,
1120 hasEnrolledBiometrics(userId) ? daemon.getAuthenticatorId().value : 0L);
Kevin Chyna56dff72018-06-19 18:41:12 -07001121 }
Kevin Chyna56dff72018-06-19 18:41:12 -07001122 } catch (RemoteException e) {
1123 Slog.e(TAG, "Failed to setActiveUser():", e);
1124 }
1125 }
1126 }
1127
1128 @Override
1129 protected String getLockoutResetIntent() {
1130 return ACTION_LOCKOUT_RESET;
1131 }
1132
1133 @Override
1134 protected String getLockoutBroadcastPermission() {
1135 return RESET_FACE_LOCKOUT;
1136 }
1137
1138 @Override
1139 protected long getHalDeviceId() {
1140 return mHalDeviceId;
1141 }
1142
1143 @Override
Kevin Chyna38653c2019-02-11 17:46:21 -08001144 protected void handleUserSwitching(int userId) {
1145 super.handleUserSwitching(userId);
1146 // Will be updated when we get the callback from HAL
1147 mCurrentUserLockoutMode = AuthenticationClient.LOCKOUT_NONE;
1148 }
1149
1150 @Override
Kevin Chyna56dff72018-06-19 18:41:12 -07001151 protected boolean hasEnrolledBiometrics(int userId) {
1152 if (userId != UserHandle.getCallingUserId()) {
1153 checkPermission(INTERACT_ACROSS_USERS);
1154 }
1155 return getBiometricUtils().getBiometricsForUser(getContext(), userId).size() > 0;
1156 }
1157
1158 @Override
1159 protected String getManageBiometricPermission() {
Kevin Chyna24e9fd2018-08-27 12:39:17 -07001160 return MANAGE_BIOMETRIC;
Kevin Chyna56dff72018-06-19 18:41:12 -07001161 }
1162
1163 @Override
1164 protected void checkUseBiometricPermission() {
Kevin Chyna24e9fd2018-08-27 12:39:17 -07001165 // noop for Face. The permission checks are all done on the incoming binder call.
Kevin Chyna56dff72018-06-19 18:41:12 -07001166 }
1167
1168 @Override
Kevin Chynb3c05aa2018-09-21 16:50:32 -07001169 protected boolean checkAppOps(int uid, String opPackageName) {
1170 return mAppOps.noteOp(AppOpsManager.OP_USE_BIOMETRIC, uid, opPackageName)
1171 == AppOpsManager.MODE_ALLOWED;
Kevin Chyna56dff72018-06-19 18:41:12 -07001172 }
1173
1174 @Override
Kevin Chyn6737c572019-02-08 16:10:54 -08001175 protected List<Face> getEnrolledTemplates(int userId) {
1176 return getBiometricUtils().getBiometricsForUser(getContext(), userId);
1177 }
1178
1179 @Override
Kevin Chyna56dff72018-06-19 18:41:12 -07001180 protected void notifyClientActiveCallbacks(boolean isActive) {
1181 // noop for Face.
1182 }
1183
Kevin Chyn7782d142019-01-18 12:51:33 -08001184 @Override
1185 protected int statsModality() {
1186 return BiometricsProtoEnums.MODALITY_FACE;
1187 }
1188
Kevin Chyna38653c2019-02-11 17:46:21 -08001189 @Override
1190 protected int getLockoutMode() {
1191 return mCurrentUserLockoutMode;
1192 }
1193
Kevin Chyna56dff72018-06-19 18:41:12 -07001194 /** Gets the face daemon */
1195 private synchronized IBiometricsFace getFaceDaemon() {
1196 if (mDaemon == null) {
1197 Slog.v(TAG, "mDaemon was null, reconnect to face");
1198 try {
1199 mDaemon = IBiometricsFace.getService();
1200 } catch (java.util.NoSuchElementException e) {
1201 // Service doesn't exist or cannot be opened. Logged below.
1202 } catch (RemoteException e) {
1203 Slog.e(TAG, "Failed to get biometric interface", e);
1204 }
1205 if (mDaemon == null) {
1206 Slog.w(TAG, "face HIDL not available");
1207 return null;
1208 }
1209
1210 mDaemon.asBinder().linkToDeath(this, 0);
1211
1212 try {
1213 mHalDeviceId = mDaemon.setCallback(mDaemonCallback).value;
1214 } catch (RemoteException e) {
1215 Slog.e(TAG, "Failed to open face HAL", e);
1216 mDaemon = null; // try again later!
1217 }
1218
1219 if (DEBUG) Slog.v(TAG, "Face HAL id: " + mHalDeviceId);
1220 if (mHalDeviceId != 0) {
1221 loadAuthenticatorIds();
1222 updateActiveGroup(ActivityManager.getCurrentUser(), null);
Kevin Chyn6737c572019-02-08 16:10:54 -08001223 doTemplateCleanupForUser(ActivityManager.getCurrentUser());
Kevin Chyna56dff72018-06-19 18:41:12 -07001224 } else {
1225 Slog.w(TAG, "Failed to open Face HAL!");
1226 MetricsLogger.count(getContext(), "faced_openhal_error", 1);
1227 mDaemon = null;
1228 }
1229 }
1230 return mDaemon;
1231 }
1232
Kevin Chynd79e24e2018-09-25 12:06:59 -07001233 private long startGenerateChallenge(IBinder token) {
Kevin Chyna56dff72018-06-19 18:41:12 -07001234 IBiometricsFace daemon = getFaceDaemon();
1235 if (daemon == null) {
Kevin Chynd79e24e2018-09-25 12:06:59 -07001236 Slog.w(TAG, "startGenerateChallenge: no face HAL!");
Kevin Chyna56dff72018-06-19 18:41:12 -07001237 return 0;
1238 }
1239 try {
Kevin Chyne46a2162018-09-20 18:43:01 -07001240 return daemon.generateChallenge(CHALLENGE_TIMEOUT_SEC).value;
Kevin Chyna56dff72018-06-19 18:41:12 -07001241 } catch (RemoteException e) {
Kevin Chynd79e24e2018-09-25 12:06:59 -07001242 Slog.e(TAG, "startGenerateChallenge failed", e);
Kevin Chyna56dff72018-06-19 18:41:12 -07001243 }
1244 return 0;
1245 }
1246
Kevin Chynd79e24e2018-09-25 12:06:59 -07001247 private int startRevokeChallenge(IBinder token) {
Kevin Chyna56dff72018-06-19 18:41:12 -07001248 IBiometricsFace daemon = getFaceDaemon();
1249 if (daemon == null) {
Kevin Chynd79e24e2018-09-25 12:06:59 -07001250 Slog.w(TAG, "startRevokeChallenge: no face HAL!");
Kevin Chyna56dff72018-06-19 18:41:12 -07001251 return 0;
1252 }
1253 try {
Kevin Chyn96c92972018-08-31 16:09:31 -07001254 return daemon.revokeChallenge();
Kevin Chyna56dff72018-06-19 18:41:12 -07001255 } catch (RemoteException e) {
Kevin Chynd79e24e2018-09-25 12:06:59 -07001256 Slog.e(TAG, "startRevokeChallenge failed", e);
Kevin Chyna56dff72018-06-19 18:41:12 -07001257 }
1258 return 0;
1259 }
1260
Gilad Brettercb51b8b2018-03-22 17:04:51 +02001261 private void dumpInternal(PrintWriter pw) {
1262 JSONObject dump = new JSONObject();
1263 try {
1264 dump.put("service", "Face Manager");
1265
1266 JSONArray sets = new JSONArray();
1267 for (UserInfo user : UserManager.get(getContext()).getUsers()) {
1268 final int userId = user.getUserHandle().getIdentifier();
Kevin Chyna56dff72018-06-19 18:41:12 -07001269 final int N = getBiometricUtils().getBiometricsForUser(getContext(), userId).size();
Gilad Brettercb51b8b2018-03-22 17:04:51 +02001270 PerformanceStats stats = mPerformanceMap.get(userId);
1271 PerformanceStats cryptoStats = mCryptoPerformanceMap.get(userId);
1272 JSONObject set = new JSONObject();
1273 set.put("id", userId);
Kevin Chyna56dff72018-06-19 18:41:12 -07001274 set.put("count", N);
Gilad Brettercb51b8b2018-03-22 17:04:51 +02001275 set.put("accept", (stats != null) ? stats.accept : 0);
1276 set.put("reject", (stats != null) ? stats.reject : 0);
1277 set.put("acquire", (stats != null) ? stats.acquire : 0);
1278 set.put("lockout", (stats != null) ? stats.lockout : 0);
1279 set.put("permanentLockout", (stats != null) ? stats.permanentLockout : 0);
1280 // cryptoStats measures statistics about secure face transactions
1281 // (e.g. to unlock password storage, make secure purchases, etc.)
1282 set.put("acceptCrypto", (cryptoStats != null) ? cryptoStats.accept : 0);
1283 set.put("rejectCrypto", (cryptoStats != null) ? cryptoStats.reject : 0);
1284 set.put("acquireCrypto", (cryptoStats != null) ? cryptoStats.acquire : 0);
1285 set.put("lockoutCrypto", (cryptoStats != null) ? cryptoStats.lockout : 0);
Kevin Chyna56dff72018-06-19 18:41:12 -07001286 set.put("permanentLockoutCrypto",
1287 (cryptoStats != null) ? cryptoStats.permanentLockout : 0);
Gilad Brettercb51b8b2018-03-22 17:04:51 +02001288 sets.put(set);
1289 }
1290
1291 dump.put("prints", sets);
1292 } catch (JSONException e) {
1293 Slog.e(TAG, "dump formatting failure", e);
1294 }
1295 pw.println(dump);
Kevin Chyn9bc1d492019-06-21 15:31:50 -07001296 pw.println("HAL deaths since last reboot: " + mHALDeathCount);
1297
1298 mUsageStats.print(pw);
Gilad Brettercb51b8b2018-03-22 17:04:51 +02001299 }
1300
Joe Onoratodb396002019-04-05 19:49:27 -07001301 private void dumpHal(FileDescriptor fd, String[] args) {
Joe Onoratobf955d22019-03-25 00:16:58 -07001302 // WARNING: CDD restricts image data from leaving TEE unencrypted on
1303 // production devices:
1304 // [C-1-10] MUST not allow unencrypted access to identifiable biometric
1305 // data or any data derived from it (such as embeddings) to the
1306 // Application Processor outside the context of the TEE.
1307 // As such, this API should only be enabled for testing purposes on
1308 // engineering and userdebug builds. All modules in the software stack
1309 // MUST enforce final build products do NOT have this functionality.
1310 // Additionally, the following check MUST NOT be removed.
1311 if (!(Build.IS_ENG || Build.IS_USERDEBUG)) {
1312 return;
1313 }
1314
Joe Onorato108413a2019-04-03 18:20:52 -07001315 // Additionally, this flag allows turning off face for a device
1316 // (either permanently through the build or on an individual device).
1317 if (SystemProperties.getBoolean("ro.face.disable_debug_data", false)
1318 || SystemProperties.getBoolean("persist.face.disable_debug_data", false)) {
1319 return;
1320 }
1321
Joe Onoratodb396002019-04-05 19:49:27 -07001322 // The debug method takes two file descriptors. The first is for text
1323 // output, which we will drop. The second is for binary data, which
1324 // will be the protobuf data.
1325 final IBiometricsFace daemon = getFaceDaemon();
1326 if (daemon != null) {
1327 FileOutputStream devnull = null;
1328 try {
1329 devnull = new FileOutputStream("/dev/null");
1330 final NativeHandle handle = new NativeHandle(
1331 new FileDescriptor[] { devnull.getFD(), fd },
1332 new int[0], false);
1333 daemon.debug(handle, new ArrayList<String>(Arrays.asList(args)));
1334 } catch (IOException | RemoteException ex) {
1335 Slog.d(TAG, "error while reading face debugging data", ex);
1336 } finally {
1337 if (devnull != null) {
1338 try {
1339 devnull.close();
1340 } catch (IOException ex) {
1341 }
1342 }
1343 }
Joe Onoratobf955d22019-03-25 00:16:58 -07001344 }
Joe Onoratobf955d22019-03-25 00:16:58 -07001345 }
Kevin Chyn8b7a0372018-09-17 15:06:05 -07001346}