Kevin Chyn | 037c4d5 | 2018-06-11 19:17:32 -0700 | [diff] [blame] | 1 | /* |
| 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 Chyn | 836f2cf | 2018-08-27 11:06:39 -0700 | [diff] [blame] | 17 | package com.android.server.biometrics; |
Kevin Chyn | 037c4d5 | 2018-06-11 19:17:32 -0700 | [diff] [blame] | 18 | |
| 19 | import android.content.Context; |
Kevin Chyn | a56dff7 | 2018-06-19 18:41:12 -0700 | [diff] [blame] | 20 | import android.hardware.biometrics.BiometricAuthenticator; |
Kevin Chyn | 037c4d5 | 2018-06-11 19:17:32 -0700 | [diff] [blame] | 21 | import android.hardware.biometrics.BiometricConstants; |
Kevin Chyn | 7782d14 | 2019-01-18 12:51:33 -0800 | [diff] [blame] | 22 | import android.hardware.biometrics.BiometricsProtoEnums; |
Kevin Chyn | 037c4d5 | 2018-06-11 19:17:32 -0700 | [diff] [blame] | 23 | import android.os.IBinder; |
| 24 | import android.os.RemoteException; |
| 25 | import android.util.Slog; |
| 26 | |
Kevin Chyn | 6cf54e8 | 2018-09-18 19:13:27 -0700 | [diff] [blame] | 27 | import java.util.ArrayList; |
Kevin Chyn | 037c4d5 | 2018-06-11 19:17:32 -0700 | [diff] [blame] | 28 | import java.util.Arrays; |
| 29 | |
| 30 | /** |
| 31 | * A class to keep track of the enrollment state for a given client. |
| 32 | */ |
| 33 | public abstract class EnrollClient extends ClientMonitor { |
| 34 | private static final long MS_PER_SEC = 1000; |
| 35 | private static final int ENROLLMENT_TIMEOUT_MS = 60 * 1000; // 1 minute |
Kevin Chyn | a56dff7 | 2018-06-19 18:41:12 -0700 | [diff] [blame] | 36 | private final byte[] mCryptoToken; |
| 37 | private final BiometricUtils mBiometricUtils; |
Kevin Chyn | 1f16c2d | 2018-12-07 13:06:08 -0800 | [diff] [blame] | 38 | private final int[] mDisabledFeatures; |
Kevin Chyn | 037c4d5 | 2018-06-11 19:17:32 -0700 | [diff] [blame] | 39 | |
Kevin Chyn | 1429a31 | 2019-01-28 16:08:09 -0800 | [diff] [blame] | 40 | public abstract boolean shouldVibrate(); |
| 41 | |
Kevin Chyn | 355c6bf | 2018-09-20 22:14:19 -0700 | [diff] [blame] | 42 | public EnrollClient(Context context, Metrics metrics, |
| 43 | BiometricServiceBase.DaemonWrapper daemon, long halDeviceId, IBinder token, |
| 44 | BiometricServiceBase.ServiceListener listener, int userId, int groupId, |
Kevin Chyn | 1f16c2d | 2018-12-07 13:06:08 -0800 | [diff] [blame] | 45 | byte[] cryptoToken, boolean restricted, String owner, BiometricUtils utils, |
| 46 | final int[] disabledFeatures) { |
Kevin Chyn | 037c4d5 | 2018-06-11 19:17:32 -0700 | [diff] [blame] | 47 | super(context, metrics, daemon, halDeviceId, token, listener, userId, groupId, restricted, |
Kevin Chyn | 87f257a | 2018-11-27 16:26:07 -0800 | [diff] [blame] | 48 | owner, 0 /* cookie */); |
Kevin Chyn | a56dff7 | 2018-06-19 18:41:12 -0700 | [diff] [blame] | 49 | mBiometricUtils = utils; |
Kevin Chyn | 037c4d5 | 2018-06-11 19:17:32 -0700 | [diff] [blame] | 50 | mCryptoToken = Arrays.copyOf(cryptoToken, cryptoToken.length); |
Kevin Chyn | 1f16c2d | 2018-12-07 13:06:08 -0800 | [diff] [blame] | 51 | mDisabledFeatures = Arrays.copyOf(disabledFeatures, disabledFeatures.length); |
Kevin Chyn | 037c4d5 | 2018-06-11 19:17:32 -0700 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | @Override |
Kevin Chyn | 7782d14 | 2019-01-18 12:51:33 -0800 | [diff] [blame] | 55 | protected int statsAction() { |
| 56 | return BiometricsProtoEnums.ACTION_ENROLL; |
| 57 | } |
| 58 | |
| 59 | @Override |
Kevin Chyn | a56dff7 | 2018-06-19 18:41:12 -0700 | [diff] [blame] | 60 | public boolean onEnrollResult(BiometricAuthenticator.Identifier identifier, |
| 61 | int remaining) { |
Kevin Chyn | 037c4d5 | 2018-06-11 19:17:32 -0700 | [diff] [blame] | 62 | if (remaining == 0) { |
Kevin Chyn | a56dff7 | 2018-06-19 18:41:12 -0700 | [diff] [blame] | 63 | mBiometricUtils.addBiometricForUser(getContext(), getTargetUserId(), identifier); |
Kevin Chyn | 037c4d5 | 2018-06-11 19:17:32 -0700 | [diff] [blame] | 64 | } |
Kevin Chyn | 03089b0 | 2019-01-31 12:37:21 -0800 | [diff] [blame] | 65 | notifyUserActivity(); |
Kevin Chyn | a56dff7 | 2018-06-19 18:41:12 -0700 | [diff] [blame] | 66 | return sendEnrollResult(identifier, remaining); |
Kevin Chyn | 037c4d5 | 2018-06-11 19:17:32 -0700 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | /* |
| 70 | * @return true if we're done. |
| 71 | */ |
Kevin Chyn | a56dff7 | 2018-06-19 18:41:12 -0700 | [diff] [blame] | 72 | private boolean sendEnrollResult(BiometricAuthenticator.Identifier identifier, |
| 73 | int remaining) { |
Kevin Chyn | 1429a31 | 2019-01-28 16:08:09 -0800 | [diff] [blame] | 74 | if (shouldVibrate()) { |
| 75 | vibrateSuccess(); |
| 76 | } |
Kevin Chyn | 037c4d5 | 2018-06-11 19:17:32 -0700 | [diff] [blame] | 77 | mMetricsLogger.action(mMetrics.actionBiometricEnroll()); |
| 78 | try { |
Kevin Chyn | a56dff7 | 2018-06-19 18:41:12 -0700 | [diff] [blame] | 79 | getListener().onEnrollResult(identifier, remaining); |
Kevin Chyn | 037c4d5 | 2018-06-11 19:17:32 -0700 | [diff] [blame] | 80 | return remaining == 0; |
| 81 | } catch (RemoteException e) { |
| 82 | Slog.w(getLogTag(), "Failed to notify EnrollResult:", e); |
| 83 | return true; |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | @Override |
| 88 | public int start() { |
| 89 | final int timeout = (int) (ENROLLMENT_TIMEOUT_MS / MS_PER_SEC); |
| 90 | try { |
Kevin Chyn | 1f16c2d | 2018-12-07 13:06:08 -0800 | [diff] [blame] | 91 | final ArrayList<Integer> disabledFeatures = new ArrayList<>(); |
| 92 | for (int i = 0; i < mDisabledFeatures.length; i++) { |
| 93 | disabledFeatures.add(mDisabledFeatures[i]); |
| 94 | } |
| 95 | |
| 96 | final int result = getDaemonWrapper().enroll(mCryptoToken, getGroupId(), timeout, |
| 97 | disabledFeatures); |
Kevin Chyn | 037c4d5 | 2018-06-11 19:17:32 -0700 | [diff] [blame] | 98 | if (result != 0) { |
| 99 | Slog.w(getLogTag(), "startEnroll failed, result=" + result); |
| 100 | mMetricsLogger.histogram(mMetrics.tagEnrollStartError(), result); |
Kevin Chyn | a56dff7 | 2018-06-19 18:41:12 -0700 | [diff] [blame] | 101 | onError(getHalDeviceId(), BiometricConstants.BIOMETRIC_ERROR_HW_UNAVAILABLE, |
| 102 | 0 /* vendorCode */); |
Kevin Chyn | 037c4d5 | 2018-06-11 19:17:32 -0700 | [diff] [blame] | 103 | return result; |
| 104 | } |
| 105 | } catch (RemoteException e) { |
| 106 | Slog.e(getLogTag(), "startEnroll failed", e); |
| 107 | } |
| 108 | return 0; // success |
| 109 | } |
| 110 | |
| 111 | @Override |
| 112 | public int stop(boolean initiatedByClient) { |
| 113 | if (mAlreadyCancelled) { |
| 114 | Slog.w(getLogTag(), "stopEnroll: already cancelled!"); |
| 115 | return 0; |
| 116 | } |
| 117 | |
| 118 | try { |
| 119 | final int result = getDaemonWrapper().cancel(); |
| 120 | if (result != 0) { |
| 121 | Slog.w(getLogTag(), "startEnrollCancel failed, result = " + result); |
| 122 | return result; |
| 123 | } |
| 124 | } catch (RemoteException e) { |
| 125 | Slog.e(getLogTag(), "stopEnrollment failed", e); |
| 126 | } |
| 127 | if (initiatedByClient) { |
Kevin Chyn | a56dff7 | 2018-06-19 18:41:12 -0700 | [diff] [blame] | 128 | onError(getHalDeviceId(), BiometricConstants.BIOMETRIC_ERROR_CANCELED, |
| 129 | 0 /* vendorCode */); |
Kevin Chyn | 037c4d5 | 2018-06-11 19:17:32 -0700 | [diff] [blame] | 130 | } |
| 131 | mAlreadyCancelled = true; |
| 132 | return 0; |
| 133 | } |
| 134 | |
| 135 | @Override |
Kevin Chyn | a56dff7 | 2018-06-19 18:41:12 -0700 | [diff] [blame] | 136 | public boolean onRemoved(BiometricAuthenticator.Identifier identifier, int remaining) { |
Kevin Chyn | 037c4d5 | 2018-06-11 19:17:32 -0700 | [diff] [blame] | 137 | if (DEBUG) Slog.w(getLogTag(), "onRemoved() called for enroll!"); |
| 138 | return true; // Invalid for EnrollClient |
| 139 | } |
| 140 | |
| 141 | @Override |
Kevin Chyn | a56dff7 | 2018-06-19 18:41:12 -0700 | [diff] [blame] | 142 | public boolean onEnumerationResult(BiometricAuthenticator.Identifier identifier, |
| 143 | int remaining) { |
Kevin Chyn | 037c4d5 | 2018-06-11 19:17:32 -0700 | [diff] [blame] | 144 | if (DEBUG) Slog.w(getLogTag(), "onEnumerationResult() called for enroll!"); |
| 145 | return true; // Invalid for EnrollClient |
| 146 | } |
| 147 | |
| 148 | @Override |
Kevin Chyn | b528d69 | 2018-07-20 11:53:14 -0700 | [diff] [blame] | 149 | public boolean onAuthenticated(BiometricAuthenticator.Identifier identifier, |
Kevin Chyn | 6cf54e8 | 2018-09-18 19:13:27 -0700 | [diff] [blame] | 150 | boolean authenticated, ArrayList<Byte> token) { |
Kevin Chyn | 037c4d5 | 2018-06-11 19:17:32 -0700 | [diff] [blame] | 151 | if (DEBUG) Slog.w(getLogTag(), "onAuthenticated() called for enroll!"); |
| 152 | return true; // Invalid for EnrollClient |
| 153 | } |
| 154 | |
| 155 | } |