blob: 14f2e861708183517608972bc4cc250177b5bfab [file] [log] [blame]
Jim Millercb2ce6f2016-04-13 20:28:18 -07001/**
2 * Copyright (C) 2016 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
17package com.android.server.fingerprint;
18
Jim Miller40e46452016-12-16 18:38:53 -080019import android.hardware.biometrics.fingerprint.V2_1.IBiometricsFingerprint;
Jim Millercb2ce6f2016-04-13 20:28:18 -070020import com.android.internal.logging.MetricsLogger;
Tamas Berghammer383db5eb2016-06-22 15:21:38 +010021import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
Jim Millercb2ce6f2016-04-13 20:28:18 -070022
23import android.content.Context;
24import android.hardware.fingerprint.Fingerprint;
25import android.hardware.fingerprint.FingerprintManager;
Jim Millercb2ce6f2016-04-13 20:28:18 -070026import android.hardware.fingerprint.IFingerprintServiceReceiver;
27import android.os.IBinder;
28import android.os.RemoteException;
Jim Millercb2ce6f2016-04-13 20:28:18 -070029import android.util.Slog;
30
31/**
32 * A class to keep track of the authentication state for a given client.
33 */
34public abstract class AuthenticationClient extends ClientMonitor {
35 private long mOpId;
36
37 public abstract boolean handleFailedAttempt();
38 public abstract void resetFailedAttempts();
39
40 public AuthenticationClient(Context context, long halDeviceId, IBinder token,
Jim Miller837fa7e2016-08-08 20:16:22 -070041 IFingerprintServiceReceiver receiver, int targetUserId, int groupId, long opId,
Jim Millercb2ce6f2016-04-13 20:28:18 -070042 boolean restricted, String owner) {
Jim Miller837fa7e2016-08-08 20:16:22 -070043 super(context, halDeviceId, token, receiver, targetUserId, groupId, restricted, owner);
Jim Millercb2ce6f2016-04-13 20:28:18 -070044 mOpId = opId;
45 }
46
47 @Override
48 public boolean onAuthenticated(int fingerId, int groupId) {
49 boolean result = false;
50 boolean authenticated = fingerId != 0;
51
52 IFingerprintServiceReceiver receiver = getReceiver();
53 if (receiver != null) {
54 try {
55 MetricsLogger.action(getContext(), MetricsEvent.ACTION_FINGERPRINT_AUTH,
56 authenticated);
57 if (!authenticated) {
58 receiver.onAuthenticationFailed(getHalDeviceId());
59 } else {
60 if (DEBUG) {
61 Slog.v(TAG, "onAuthenticated(owner=" + getOwnerString()
62 + ", id=" + fingerId + ", gp=" + groupId + ")");
63 }
64 Fingerprint fp = !getIsRestricted()
65 ? new Fingerprint("" /* TODO */, groupId, fingerId, getHalDeviceId())
66 : null;
Jim Miller837fa7e2016-08-08 20:16:22 -070067 receiver.onAuthenticationSucceeded(getHalDeviceId(), fp, getTargetUserId());
Jim Millercb2ce6f2016-04-13 20:28:18 -070068 }
69 } catch (RemoteException e) {
70 Slog.w(TAG, "Failed to notify Authenticated:", e);
71 result = true; // client failed
72 }
73 } else {
74 result = true; // client not listening
75 }
Jim Millerd1974862016-05-03 18:35:18 -070076 if (!authenticated) {
Jim Millercb2ce6f2016-04-13 20:28:18 -070077 if (receiver != null) {
78 FingerprintUtils.vibrateFingerprintError(getContext());
79 }
80 // allow system-defined limit of number of attempts before giving up
Jim Millerd1974862016-05-03 18:35:18 -070081 boolean inLockoutMode = handleFailedAttempt();
82 // send lockout event in case driver doesn't enforce it.
83 if (inLockoutMode) {
84 try {
85 Slog.w(TAG, "Forcing lockout (fp driver code should do this!)");
86 receiver.onError(getHalDeviceId(),
Jim Miller40e46452016-12-16 18:38:53 -080087 FingerprintManager.FINGERPRINT_ERROR_LOCKOUT, 0 /* vendorCode */);
Jim Millerd1974862016-05-03 18:35:18 -070088 } catch (RemoteException e) {
89 Slog.w(TAG, "Failed to notify lockout:", e);
90 }
91 }
92 result |= inLockoutMode;
Jim Millercb2ce6f2016-04-13 20:28:18 -070093 } else {
94 if (receiver != null) {
95 FingerprintUtils.vibrateFingerprintSuccess(getContext());
96 }
97 result |= true; // we have a valid fingerprint, done
98 resetFailedAttempts();
99 }
100 return result;
101 }
102
103 /**
104 * Start authentication
105 */
106 @Override
107 public int start() {
Jim Miller40e46452016-12-16 18:38:53 -0800108 IBiometricsFingerprint daemon = getFingerprintDaemon();
Jim Millercb2ce6f2016-04-13 20:28:18 -0700109 if (daemon == null) {
110 Slog.w(TAG, "start authentication: no fingeprintd!");
111 return ERROR_ESRCH;
112 }
113 try {
114 final int result = daemon.authenticate(mOpId, getGroupId());
115 if (result != 0) {
116 Slog.w(TAG, "startAuthentication failed, result=" + result);
Jim Millerc57c8d92016-09-30 17:17:59 -0700117 MetricsLogger.histogram(getContext(), "fingeprintd_auth_start_error", result);
Jim Miller40e46452016-12-16 18:38:53 -0800118 onError(FingerprintManager.FINGERPRINT_ERROR_HW_UNAVAILABLE, 0 /* vendorCode */);
Jim Millercb2ce6f2016-04-13 20:28:18 -0700119 return result;
120 }
121 if (DEBUG) Slog.w(TAG, "client " + getOwnerString() + " is authenticating...");
122 } catch (RemoteException e) {
123 Slog.e(TAG, "startAuthentication failed", e);
124 return ERROR_ESRCH;
125 }
126 return 0; // success
127 }
128
129 @Override
130 public int stop(boolean initiatedByClient) {
Jim Miller40e46452016-12-16 18:38:53 -0800131 IBiometricsFingerprint daemon = getFingerprintDaemon();
Jim Millercb2ce6f2016-04-13 20:28:18 -0700132 if (daemon == null) {
133 Slog.w(TAG, "stopAuthentication: no fingeprintd!");
134 return ERROR_ESRCH;
135 }
136 try {
Jim Miller40e46452016-12-16 18:38:53 -0800137 final int result = daemon.cancel();
Jim Millercb2ce6f2016-04-13 20:28:18 -0700138 if (result != 0) {
139 Slog.w(TAG, "stopAuthentication failed, result=" + result);
140 return result;
141 }
142 if (DEBUG) Slog.w(TAG, "client " + getOwnerString() + " is no longer authenticating");
143 } catch (RemoteException e) {
144 Slog.e(TAG, "stopAuthentication failed", e);
145 return ERROR_ESRCH;
146 }
147 return 0; // success
148 }
149
150 @Override
Jim Miller40e46452016-12-16 18:38:53 -0800151 public boolean onEnrollResult(int fingerId, int groupId, int remaining) {
Jim Millercb2ce6f2016-04-13 20:28:18 -0700152 if (DEBUG) Slog.w(TAG, "onEnrollResult() called for authenticate!");
153 return true; // Invalid for Authenticate
154 }
155
156 @Override
Jim Miller40e46452016-12-16 18:38:53 -0800157 public boolean onRemoved(int fingerId, int groupId, int remaining) {
Jim Millercb2ce6f2016-04-13 20:28:18 -0700158 if (DEBUG) Slog.w(TAG, "onRemoved() called for authenticate!");
159 return true; // Invalid for Authenticate
160 }
161
162 @Override
Jim Miller40e46452016-12-16 18:38:53 -0800163 public boolean onEnumerationResult(int fingerId, int groupId, int remaining) {
Jim Millercb2ce6f2016-04-13 20:28:18 -0700164 if (DEBUG) Slog.w(TAG, "onEnumerationResult() called for authenticate!");
165 return true; // Invalid for Authenticate
166 }
167}