Joe Bolinger | de94aa0 | 2021-12-09 17:00:32 -0800 | [diff] [blame] | 1 | /* |
Jeff Pu | 5265318 | 2022-10-12 16:27:23 -0400 | [diff] [blame] | 2 | * Copyright (C) 2022 The Android Open Source Project |
Joe Bolinger | de94aa0 | 2021-12-09 17:00:32 -0800 | [diff] [blame] | 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 | |
| 17 | #include "FakeFingerprintEngine.h" |
Jeff Pu | 5265318 | 2022-10-12 16:27:23 -0400 | [diff] [blame] | 18 | #include <regex> |
Jeff Pu | 63f33c7 | 2022-07-28 16:06:23 -0400 | [diff] [blame] | 19 | #include "Fingerprint.h" |
Joe Bolinger | de94aa0 | 2021-12-09 17:00:32 -0800 | [diff] [blame] | 20 | |
Joe Bolinger | de94aa0 | 2021-12-09 17:00:32 -0800 | [diff] [blame] | 21 | #include <android-base/logging.h> |
Jeff Pu | 63f33c7 | 2022-07-28 16:06:23 -0400 | [diff] [blame] | 22 | #include <android-base/parseint.h> |
Joshua McCloskey | c8c0bad | 2022-05-10 05:17:44 +0000 | [diff] [blame] | 23 | |
| 24 | #include <fingerprint.sysprop.h> |
Joe Bolinger | de94aa0 | 2021-12-09 17:00:32 -0800 | [diff] [blame] | 25 | |
Joshua McCloskey | c8c0bad | 2022-05-10 05:17:44 +0000 | [diff] [blame] | 26 | #include "util/CancellationSignal.h" |
Joshua McCloskey | db009a5 | 2022-05-10 05:18:20 +0000 | [diff] [blame] | 27 | #include "util/Util.h" |
Joe Bolinger | de94aa0 | 2021-12-09 17:00:32 -0800 | [diff] [blame] | 28 | |
| 29 | using namespace ::android::fingerprint::virt; |
Jeff Pu | 63f33c7 | 2022-07-28 16:06:23 -0400 | [diff] [blame] | 30 | using ::android::base::ParseInt; |
Joe Bolinger | de94aa0 | 2021-12-09 17:00:32 -0800 | [diff] [blame] | 31 | |
| 32 | namespace aidl::android::hardware::biometrics::fingerprint { |
| 33 | |
Jeff Pu | def5b04 | 2023-05-25 14:28:16 -0400 | [diff] [blame] | 34 | FakeFingerprintEngine::FakeFingerprintEngine() |
Jeff Pu | 29df1e9 | 2023-10-06 14:12:44 +0000 | [diff] [blame] | 35 | : mRandom(std::mt19937::default_seed), |
| 36 | mWorkMode(WorkMode::kIdle), |
| 37 | isLockoutTimerSupported(true) {} |
Jeff Pu | def5b04 | 2023-05-25 14:28:16 -0400 | [diff] [blame] | 38 | |
Joe Bolinger | de94aa0 | 2021-12-09 17:00:32 -0800 | [diff] [blame] | 39 | void FakeFingerprintEngine::generateChallengeImpl(ISessionCallback* cb) { |
| 40 | BEGIN_OP(0); |
| 41 | std::uniform_int_distribution<int64_t> dist; |
| 42 | auto challenge = dist(mRandom); |
Jeff Pu | 6ccd956 | 2024-02-21 10:46:35 -0500 | [diff] [blame^] | 43 | Fingerprint::cfg().set<std::int64_t>("challenge", challenge); |
Joe Bolinger | de94aa0 | 2021-12-09 17:00:32 -0800 | [diff] [blame] | 44 | cb->onChallengeGenerated(challenge); |
| 45 | } |
| 46 | |
| 47 | void FakeFingerprintEngine::revokeChallengeImpl(ISessionCallback* cb, int64_t challenge) { |
| 48 | BEGIN_OP(0); |
Jeff Pu | 6ccd956 | 2024-02-21 10:46:35 -0500 | [diff] [blame^] | 49 | Fingerprint::cfg().setopt<OptInt64>("challenge", std::nullopt); |
Joe Bolinger | de94aa0 | 2021-12-09 17:00:32 -0800 | [diff] [blame] | 50 | cb->onChallengeRevoked(challenge); |
| 51 | } |
| 52 | |
| 53 | void FakeFingerprintEngine::enrollImpl(ISessionCallback* cb, |
| 54 | const keymaster::HardwareAuthToken& hat, |
| 55 | const std::future<void>& cancel) { |
Jeff Pu | def5b04 | 2023-05-25 14:28:16 -0400 | [diff] [blame] | 56 | BEGIN_OP(0); |
Jeff Pu | 5055e3c | 2023-07-27 11:26:50 -0400 | [diff] [blame] | 57 | |
| 58 | // Do proper HAT verification in the real implementation. |
| 59 | if (hat.mac.empty()) { |
| 60 | LOG(ERROR) << "Fail: hat"; |
| 61 | cb->onError(Error::UNABLE_TO_PROCESS, 0 /* vendorError */); |
| 62 | return; |
| 63 | } |
| 64 | |
Jeff Pu | 74e25d2 | 2024-01-08 22:21:20 +0000 | [diff] [blame] | 65 | waitForFingerDown(cb, cancel); |
| 66 | |
Jeff Pu | def5b04 | 2023-05-25 14:28:16 -0400 | [diff] [blame] | 67 | updateContext(WorkMode::kEnroll, cb, const_cast<std::future<void>&>(cancel), 0, hat); |
| 68 | } |
| 69 | |
| 70 | void FakeFingerprintEngine::authenticateImpl(ISessionCallback* cb, int64_t operationId, |
| 71 | const std::future<void>& cancel) { |
| 72 | BEGIN_OP(0); |
Jeff Pu | 74e25d2 | 2024-01-08 22:21:20 +0000 | [diff] [blame] | 73 | |
| 74 | waitForFingerDown(cb, cancel); |
| 75 | |
Jeff Pu | def5b04 | 2023-05-25 14:28:16 -0400 | [diff] [blame] | 76 | updateContext(WorkMode::kAuthenticate, cb, const_cast<std::future<void>&>(cancel), operationId, |
| 77 | keymaster::HardwareAuthToken()); |
| 78 | } |
| 79 | |
| 80 | void FakeFingerprintEngine::detectInteractionImpl(ISessionCallback* cb, |
| 81 | const std::future<void>& cancel) { |
| 82 | BEGIN_OP(0); |
| 83 | |
Jeff Pu | 6ccd956 | 2024-02-21 10:46:35 -0500 | [diff] [blame^] | 84 | auto detectInteractionSupported = Fingerprint::cfg().get<bool>("detect_interaction"); |
Jeff Pu | def5b04 | 2023-05-25 14:28:16 -0400 | [diff] [blame] | 85 | if (!detectInteractionSupported) { |
| 86 | LOG(ERROR) << "Detect interaction is not supported"; |
| 87 | cb->onError(Error::UNABLE_TO_PROCESS, 0 /* vendorError */); |
| 88 | return; |
| 89 | } |
| 90 | |
Jeff Pu | 74e25d2 | 2024-01-08 22:21:20 +0000 | [diff] [blame] | 91 | waitForFingerDown(cb, cancel); |
| 92 | |
Jeff Pu | def5b04 | 2023-05-25 14:28:16 -0400 | [diff] [blame] | 93 | updateContext(WorkMode::kDetectInteract, cb, const_cast<std::future<void>&>(cancel), 0, |
| 94 | keymaster::HardwareAuthToken()); |
| 95 | } |
| 96 | |
| 97 | void FakeFingerprintEngine::updateContext(WorkMode mode, ISessionCallback* cb, |
| 98 | std::future<void>& cancel, int64_t operationId, |
| 99 | const keymaster::HardwareAuthToken& hat) { |
| 100 | mCancel = std::move(cancel); |
| 101 | mWorkMode = mode; |
| 102 | mCb = cb; |
| 103 | mOperationId = operationId; |
| 104 | mHat = hat; |
| 105 | } |
| 106 | |
| 107 | void FakeFingerprintEngine::fingerDownAction() { |
Jeff Pu | 073af18 | 2023-07-12 18:53:52 +0000 | [diff] [blame] | 108 | bool isTerminal = false; |
Jeff Pu | def5b04 | 2023-05-25 14:28:16 -0400 | [diff] [blame] | 109 | LOG(INFO) << __func__; |
| 110 | switch (mWorkMode) { |
| 111 | case WorkMode::kAuthenticate: |
Jeff Pu | 073af18 | 2023-07-12 18:53:52 +0000 | [diff] [blame] | 112 | isTerminal = onAuthenticateFingerDown(mCb, mOperationId, mCancel); |
Jeff Pu | def5b04 | 2023-05-25 14:28:16 -0400 | [diff] [blame] | 113 | break; |
| 114 | case WorkMode::kEnroll: |
Jeff Pu | 073af18 | 2023-07-12 18:53:52 +0000 | [diff] [blame] | 115 | isTerminal = onEnrollFingerDown(mCb, mHat, mCancel); |
Jeff Pu | def5b04 | 2023-05-25 14:28:16 -0400 | [diff] [blame] | 116 | break; |
| 117 | case WorkMode::kDetectInteract: |
Jeff Pu | 073af18 | 2023-07-12 18:53:52 +0000 | [diff] [blame] | 118 | isTerminal = onDetectInteractFingerDown(mCb, mCancel); |
Jeff Pu | def5b04 | 2023-05-25 14:28:16 -0400 | [diff] [blame] | 119 | break; |
| 120 | default: |
| 121 | LOG(WARNING) << "unexpected mode: on fingerDownAction(), " << (int)mWorkMode; |
| 122 | break; |
| 123 | } |
Jeff Pu | 073af18 | 2023-07-12 18:53:52 +0000 | [diff] [blame] | 124 | |
| 125 | if (isTerminal) { |
| 126 | mWorkMode = WorkMode::kIdle; |
| 127 | } |
Jeff Pu | def5b04 | 2023-05-25 14:28:16 -0400 | [diff] [blame] | 128 | } |
| 129 | |
Jeff Pu | 073af18 | 2023-07-12 18:53:52 +0000 | [diff] [blame] | 130 | bool FakeFingerprintEngine::onEnrollFingerDown(ISessionCallback* cb, |
Jeff Pu | 5055e3c | 2023-07-27 11:26:50 -0400 | [diff] [blame] | 131 | const keymaster::HardwareAuthToken&, |
Jeff Pu | def5b04 | 2023-05-25 14:28:16 -0400 | [diff] [blame] | 132 | const std::future<void>& cancel) { |
Jeff Pu | 6ccd956 | 2024-02-21 10:46:35 -0500 | [diff] [blame^] | 133 | BEGIN_OP(getLatency(Fingerprint::cfg().getopt<OptIntVec>("operation_enroll_latency"))); |
Joe Bolinger | de94aa0 | 2021-12-09 17:00:32 -0800 | [diff] [blame] | 134 | |
Jeff Pu | 343ca94 | 2022-09-14 15:56:30 -0400 | [diff] [blame] | 135 | // Force error-out |
Jeff Pu | 6ccd956 | 2024-02-21 10:46:35 -0500 | [diff] [blame^] | 136 | auto err = Fingerprint::cfg().get<std::int32_t>("operation_enroll_error"); |
Jeff Pu | 343ca94 | 2022-09-14 15:56:30 -0400 | [diff] [blame] | 137 | if (err != 0) { |
| 138 | LOG(ERROR) << "Fail: operation_enroll_error"; |
| 139 | auto ec = convertError(err); |
| 140 | cb->onError(ec.first, ec.second); |
Jeff Pu | 073af18 | 2023-07-12 18:53:52 +0000 | [diff] [blame] | 141 | return true; |
Joe Bolinger | de94aa0 | 2021-12-09 17:00:32 -0800 | [diff] [blame] | 142 | } |
| 143 | |
Jeff Pu | 343ca94 | 2022-09-14 15:56:30 -0400 | [diff] [blame] | 144 | // Format is "<id>:<progress_ms-[acquiredInfo..]>,...:<result> |
Jeff Pu | 6ccd956 | 2024-02-21 10:46:35 -0500 | [diff] [blame^] | 145 | auto nextEnroll = Fingerprint::cfg().get<std::string>("next_enrollment"); |
Joshua McCloskey | db009a5 | 2022-05-10 05:18:20 +0000 | [diff] [blame] | 146 | auto parts = Util::split(nextEnroll, ":"); |
Joe Bolinger | de94aa0 | 2021-12-09 17:00:32 -0800 | [diff] [blame] | 147 | if (parts.size() != 3) { |
Jeff Pu | 343ca94 | 2022-09-14 15:56:30 -0400 | [diff] [blame] | 148 | LOG(ERROR) << "Fail: invalid next_enrollment:" << nextEnroll; |
Joe Bolinger | de94aa0 | 2021-12-09 17:00:32 -0800 | [diff] [blame] | 149 | cb->onError(Error::VENDOR, 0 /* vendorError */); |
Jeff Pu | 073af18 | 2023-07-12 18:53:52 +0000 | [diff] [blame] | 150 | return true; |
Joe Bolinger | de94aa0 | 2021-12-09 17:00:32 -0800 | [diff] [blame] | 151 | } |
| 152 | auto enrollmentId = std::stoi(parts[0]); |
Jeff Pu | 484d2e7 | 2023-09-25 15:11:19 +0000 | [diff] [blame] | 153 | auto progress = Util::parseEnrollmentCapture(parts[1]); |
Jeff Pu | 343ca94 | 2022-09-14 15:56:30 -0400 | [diff] [blame] | 154 | for (size_t i = 0; i < progress.size(); i += 2) { |
| 155 | auto left = (progress.size() - i) / 2 - 1; |
| 156 | auto duration = progress[i][0]; |
| 157 | auto acquired = progress[i + 1]; |
| 158 | auto N = acquired.size(); |
Joe Bolinger | de94aa0 | 2021-12-09 17:00:32 -0800 | [diff] [blame] | 159 | |
Jeff Pu | 343ca94 | 2022-09-14 15:56:30 -0400 | [diff] [blame] | 160 | for (int j = 0; j < N; j++) { |
| 161 | SLEEP_MS(duration / N); |
| 162 | |
| 163 | if (shouldCancel(cancel)) { |
| 164 | LOG(ERROR) << "Fail: cancel"; |
| 165 | cb->onError(Error::CANCELED, 0 /* vendorCode */); |
Jeff Pu | 073af18 | 2023-07-12 18:53:52 +0000 | [diff] [blame] | 166 | return true; |
Jeff Pu | 343ca94 | 2022-09-14 15:56:30 -0400 | [diff] [blame] | 167 | } |
| 168 | auto ac = convertAcquiredInfo(acquired[j]); |
| 169 | cb->onAcquired(ac.first, ac.second); |
Joe Bolinger | de94aa0 | 2021-12-09 17:00:32 -0800 | [diff] [blame] | 170 | } |
| 171 | |
Joe Bolinger | de94aa0 | 2021-12-09 17:00:32 -0800 | [diff] [blame] | 172 | if (left == 0 && !IS_TRUE(parts[2])) { // end and failed |
| 173 | LOG(ERROR) << "Fail: requested by caller: " << nextEnroll; |
Jeff Pu | 6ccd956 | 2024-02-21 10:46:35 -0500 | [diff] [blame^] | 174 | Fingerprint::cfg().set<std::string>("next_enrollment", ""); |
Joe Bolinger | de94aa0 | 2021-12-09 17:00:32 -0800 | [diff] [blame] | 175 | cb->onError(Error::UNABLE_TO_PROCESS, 0 /* vendorCode */); |
| 176 | } else { // progress and update props if last time |
Jeff Pu | 343ca94 | 2022-09-14 15:56:30 -0400 | [diff] [blame] | 177 | LOG(INFO) << "onEnroll: " << enrollmentId << " left: " << left; |
Joe Bolinger | de94aa0 | 2021-12-09 17:00:32 -0800 | [diff] [blame] | 178 | if (left == 0) { |
Jeff Pu | 6ccd956 | 2024-02-21 10:46:35 -0500 | [diff] [blame^] | 179 | auto enrollments = Fingerprint::cfg().getopt<OptIntVec>("enrollments"); |
Joe Bolinger | de94aa0 | 2021-12-09 17:00:32 -0800 | [diff] [blame] | 180 | enrollments.emplace_back(enrollmentId); |
Jeff Pu | 6ccd956 | 2024-02-21 10:46:35 -0500 | [diff] [blame^] | 181 | Fingerprint::cfg().setopt<OptIntVec>("enrollments", enrollments); |
| 182 | Fingerprint::cfg().setopt<OptString>("next_enrollment", std::nullopt); |
Jeff Pu | 343ca94 | 2022-09-14 15:56:30 -0400 | [diff] [blame] | 183 | // change authenticatorId after new enrollment |
Jeff Pu | 6ccd956 | 2024-02-21 10:46:35 -0500 | [diff] [blame^] | 184 | auto id = Fingerprint::cfg().get<std::int64_t>("authenticator_id"); |
Jeff Pu | 343ca94 | 2022-09-14 15:56:30 -0400 | [diff] [blame] | 185 | auto newId = id + 1; |
Jeff Pu | 6ccd956 | 2024-02-21 10:46:35 -0500 | [diff] [blame^] | 186 | Fingerprint::cfg().set<std::int64_t>("authenticator_id", newId); |
Joe Bolinger | de94aa0 | 2021-12-09 17:00:32 -0800 | [diff] [blame] | 187 | LOG(INFO) << "Enrolled: " << enrollmentId; |
| 188 | } |
| 189 | cb->onEnrollmentProgress(enrollmentId, left); |
| 190 | } |
| 191 | } |
Jeff Pu | 073af18 | 2023-07-12 18:53:52 +0000 | [diff] [blame] | 192 | |
| 193 | return true; |
Joe Bolinger | de94aa0 | 2021-12-09 17:00:32 -0800 | [diff] [blame] | 194 | } |
| 195 | |
Jeff Pu | 073af18 | 2023-07-12 18:53:52 +0000 | [diff] [blame] | 196 | bool FakeFingerprintEngine::onAuthenticateFingerDown(ISessionCallback* cb, |
Jeff Pu | def5b04 | 2023-05-25 14:28:16 -0400 | [diff] [blame] | 197 | int64_t /* operationId */, |
| 198 | const std::future<void>& cancel) { |
Jeff Pu | 6ccd956 | 2024-02-21 10:46:35 -0500 | [diff] [blame^] | 199 | BEGIN_OP(getLatency(Fingerprint::cfg().getopt<OptIntVec>("operation_authenticate_latency"))); |
Joe Bolinger | de94aa0 | 2021-12-09 17:00:32 -0800 | [diff] [blame] | 200 | |
Jeff Pu | 343ca94 | 2022-09-14 15:56:30 -0400 | [diff] [blame] | 201 | int64_t now = Util::getSystemNanoTime(); |
Jeff Pu | 6ccd956 | 2024-02-21 10:46:35 -0500 | [diff] [blame^] | 202 | int64_t duration = Fingerprint::cfg().get<std::int32_t>("operation_authenticate_duration"); |
| 203 | auto acquired = Fingerprint::cfg().get<std::string>("operation_authenticate_acquired"); |
Jeff Pu | 484d2e7 | 2023-09-25 15:11:19 +0000 | [diff] [blame] | 204 | auto acquiredInfos = Util::parseIntSequence(acquired); |
Jeff Pu | 343ca94 | 2022-09-14 15:56:30 -0400 | [diff] [blame] | 205 | int N = acquiredInfos.size(); |
| 206 | |
| 207 | if (N == 0) { |
| 208 | LOG(ERROR) << "Fail to parse authentiate acquired info: " + acquired; |
| 209 | cb->onError(Error::UNABLE_TO_PROCESS, 0 /* vendorError */); |
Jeff Pu | 073af18 | 2023-07-12 18:53:52 +0000 | [diff] [blame] | 210 | return true; |
Jeff Pu | 343ca94 | 2022-09-14 15:56:30 -0400 | [diff] [blame] | 211 | } |
| 212 | |
Jeff Pu | 5265318 | 2022-10-12 16:27:23 -0400 | [diff] [blame] | 213 | // got lockout? |
Jeff Pu | 073af18 | 2023-07-12 18:53:52 +0000 | [diff] [blame] | 214 | if (checkSensorLockout(cb)) { |
| 215 | return FakeLockoutTracker::LockoutMode::kPermanent == mLockoutTracker.getMode(); |
| 216 | } |
Jeff Pu | 5265318 | 2022-10-12 16:27:23 -0400 | [diff] [blame] | 217 | |
Jeff Pu | 343ca94 | 2022-09-14 15:56:30 -0400 | [diff] [blame] | 218 | int i = 0; |
Joe Bolinger | de94aa0 | 2021-12-09 17:00:32 -0800 | [diff] [blame] | 219 | do { |
Jeff Pu | 6ccd956 | 2024-02-21 10:46:35 -0500 | [diff] [blame^] | 220 | if (Fingerprint::cfg().get<bool>("operation_authenticate_fails")) { |
Joe Bolinger | de94aa0 | 2021-12-09 17:00:32 -0800 | [diff] [blame] | 221 | LOG(ERROR) << "Fail: operation_authenticate_fails"; |
Jeff Pu | 5265318 | 2022-10-12 16:27:23 -0400 | [diff] [blame] | 222 | mLockoutTracker.addFailedAttempt(); |
Jeff Pu | 343ca94 | 2022-09-14 15:56:30 -0400 | [diff] [blame] | 223 | cb->onAuthenticationFailed(); |
Jeff Pu | 073af18 | 2023-07-12 18:53:52 +0000 | [diff] [blame] | 224 | return false; |
Jeff Pu | 343ca94 | 2022-09-14 15:56:30 -0400 | [diff] [blame] | 225 | } |
| 226 | |
Jeff Pu | 6ccd956 | 2024-02-21 10:46:35 -0500 | [diff] [blame^] | 227 | auto err = Fingerprint::cfg().get<std::int32_t>("operation_authenticate_error"); |
Jeff Pu | 343ca94 | 2022-09-14 15:56:30 -0400 | [diff] [blame] | 228 | if (err != 0) { |
| 229 | LOG(ERROR) << "Fail: operation_authenticate_error"; |
| 230 | auto ec = convertError(err); |
| 231 | cb->onError(ec.first, ec.second); |
Jeff Pu | 073af18 | 2023-07-12 18:53:52 +0000 | [diff] [blame] | 232 | return true; /* simply terminating current operation for any user inserted error, |
| 233 | revisit if tests need*/ |
Joe Bolinger | de94aa0 | 2021-12-09 17:00:32 -0800 | [diff] [blame] | 234 | } |
| 235 | |
Jeff Pu | 6ccd956 | 2024-02-21 10:46:35 -0500 | [diff] [blame^] | 236 | if (Fingerprint::cfg().get<bool>("lockout")) { |
Joe Bolinger | de94aa0 | 2021-12-09 17:00:32 -0800 | [diff] [blame] | 237 | LOG(ERROR) << "Fail: lockout"; |
| 238 | cb->onLockoutPermanent(); |
| 239 | cb->onError(Error::HW_UNAVAILABLE, 0 /* vendorError */); |
Jeff Pu | 073af18 | 2023-07-12 18:53:52 +0000 | [diff] [blame] | 240 | return true; |
Joe Bolinger | de94aa0 | 2021-12-09 17:00:32 -0800 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | if (shouldCancel(cancel)) { |
| 244 | LOG(ERROR) << "Fail: cancel"; |
| 245 | cb->onError(Error::CANCELED, 0 /* vendorCode */); |
Jeff Pu | 073af18 | 2023-07-12 18:53:52 +0000 | [diff] [blame] | 246 | return true; |
Joe Bolinger | de94aa0 | 2021-12-09 17:00:32 -0800 | [diff] [blame] | 247 | } |
| 248 | |
Jeff Pu | 343ca94 | 2022-09-14 15:56:30 -0400 | [diff] [blame] | 249 | if (i < N) { |
| 250 | auto ac = convertAcquiredInfo(acquiredInfos[i]); |
| 251 | cb->onAcquired(ac.first, ac.second); |
| 252 | i++; |
Joe Bolinger | de94aa0 | 2021-12-09 17:00:32 -0800 | [diff] [blame] | 253 | } |
| 254 | |
Jeff Pu | 343ca94 | 2022-09-14 15:56:30 -0400 | [diff] [blame] | 255 | SLEEP_MS(duration / N); |
Joshua McCloskey | db009a5 | 2022-05-10 05:18:20 +0000 | [diff] [blame] | 256 | } while (!Util::hasElapsed(now, duration)); |
Joe Bolinger | de94aa0 | 2021-12-09 17:00:32 -0800 | [diff] [blame] | 257 | |
Jeff Pu | 6ccd956 | 2024-02-21 10:46:35 -0500 | [diff] [blame^] | 258 | auto id = Fingerprint::cfg().get<std::int32_t>("enrollment_hit"); |
| 259 | auto enrolls = Fingerprint::cfg().getopt<OptIntVec>("enrollments"); |
Jeff Pu | 343ca94 | 2022-09-14 15:56:30 -0400 | [diff] [blame] | 260 | auto isEnrolled = std::find(enrolls.begin(), enrolls.end(), id) != enrolls.end(); |
| 261 | if (id > 0 && isEnrolled) { |
| 262 | cb->onAuthenticationSucceeded(id, {} /* hat */); |
Jeff Pu | 5265318 | 2022-10-12 16:27:23 -0400 | [diff] [blame] | 263 | mLockoutTracker.reset(); |
Jeff Pu | 073af18 | 2023-07-12 18:53:52 +0000 | [diff] [blame] | 264 | return true; |
Jeff Pu | 343ca94 | 2022-09-14 15:56:30 -0400 | [diff] [blame] | 265 | } else { |
| 266 | LOG(ERROR) << "Fail: fingerprint not enrolled"; |
| 267 | cb->onAuthenticationFailed(); |
Jeff Pu | 5265318 | 2022-10-12 16:27:23 -0400 | [diff] [blame] | 268 | mLockoutTracker.addFailedAttempt(); |
Jeff Pu | 437516e | 2023-06-28 15:21:21 +0000 | [diff] [blame] | 269 | checkSensorLockout(cb); |
Jeff Pu | 8fec556 | 2023-07-20 13:07:04 +0000 | [diff] [blame] | 270 | return false; |
Jeff Pu | 343ca94 | 2022-09-14 15:56:30 -0400 | [diff] [blame] | 271 | } |
Joe Bolinger | de94aa0 | 2021-12-09 17:00:32 -0800 | [diff] [blame] | 272 | } |
| 273 | |
Jeff Pu | 073af18 | 2023-07-12 18:53:52 +0000 | [diff] [blame] | 274 | bool FakeFingerprintEngine::onDetectInteractFingerDown(ISessionCallback* cb, |
Jeff Pu | def5b04 | 2023-05-25 14:28:16 -0400 | [diff] [blame] | 275 | const std::future<void>& cancel) { |
Jeff Pu | 6ccd956 | 2024-02-21 10:46:35 -0500 | [diff] [blame^] | 276 | BEGIN_OP(getLatency( |
| 277 | Fingerprint::cfg().getopt<OptIntVec>("operation_detect_interaction_latency"))); |
Joe Bolinger | de94aa0 | 2021-12-09 17:00:32 -0800 | [diff] [blame] | 278 | |
Jeff Pu | 6ccd956 | 2024-02-21 10:46:35 -0500 | [diff] [blame^] | 279 | int32_t duration = |
| 280 | Fingerprint::cfg().get<std::int32_t>("operation_detect_interaction_duration"); |
Jeff Pu | 5265318 | 2022-10-12 16:27:23 -0400 | [diff] [blame] | 281 | |
Jeff Pu | 6ccd956 | 2024-02-21 10:46:35 -0500 | [diff] [blame^] | 282 | auto acquired = Fingerprint::cfg().get<std::string>("operation_detect_interaction_acquired"); |
Jeff Pu | 484d2e7 | 2023-09-25 15:11:19 +0000 | [diff] [blame] | 283 | auto acquiredInfos = Util::parseIntSequence(acquired); |
Jeff Pu | 343ca94 | 2022-09-14 15:56:30 -0400 | [diff] [blame] | 284 | int N = acquiredInfos.size(); |
| 285 | int64_t now = Util::getSystemNanoTime(); |
| 286 | |
| 287 | if (N == 0) { |
| 288 | LOG(ERROR) << "Fail to parse detect interaction acquired info: " + acquired; |
| 289 | cb->onError(Error::UNABLE_TO_PROCESS, 0 /* vendorError */); |
Jeff Pu | 073af18 | 2023-07-12 18:53:52 +0000 | [diff] [blame] | 290 | return true; |
Joe Bolinger | de94aa0 | 2021-12-09 17:00:32 -0800 | [diff] [blame] | 291 | } |
| 292 | |
Jeff Pu | 343ca94 | 2022-09-14 15:56:30 -0400 | [diff] [blame] | 293 | int i = 0; |
| 294 | do { |
Jeff Pu | 6ccd956 | 2024-02-21 10:46:35 -0500 | [diff] [blame^] | 295 | auto err = Fingerprint::cfg().get<std::int32_t>("operation_detect_interaction_error"); |
Jeff Pu | 343ca94 | 2022-09-14 15:56:30 -0400 | [diff] [blame] | 296 | if (err != 0) { |
| 297 | LOG(ERROR) << "Fail: operation_detect_interaction_error"; |
| 298 | auto ec = convertError(err); |
| 299 | cb->onError(ec.first, ec.second); |
Jeff Pu | 073af18 | 2023-07-12 18:53:52 +0000 | [diff] [blame] | 300 | return true; |
Jeff Pu | 343ca94 | 2022-09-14 15:56:30 -0400 | [diff] [blame] | 301 | } |
| 302 | |
| 303 | if (shouldCancel(cancel)) { |
| 304 | LOG(ERROR) << "Fail: cancel"; |
| 305 | cb->onError(Error::CANCELED, 0 /* vendorCode */); |
Jeff Pu | 073af18 | 2023-07-12 18:53:52 +0000 | [diff] [blame] | 306 | return true; |
Jeff Pu | 343ca94 | 2022-09-14 15:56:30 -0400 | [diff] [blame] | 307 | } |
| 308 | |
| 309 | if (i < N) { |
| 310 | auto ac = convertAcquiredInfo(acquiredInfos[i]); |
| 311 | cb->onAcquired(ac.first, ac.second); |
| 312 | i++; |
| 313 | } |
| 314 | SLEEP_MS(duration / N); |
| 315 | } while (!Util::hasElapsed(now, duration)); |
Joe Bolinger | de94aa0 | 2021-12-09 17:00:32 -0800 | [diff] [blame] | 316 | |
Joe Bolinger | de94aa0 | 2021-12-09 17:00:32 -0800 | [diff] [blame] | 317 | cb->onInteractionDetected(); |
Jeff Pu | 073af18 | 2023-07-12 18:53:52 +0000 | [diff] [blame] | 318 | |
| 319 | return true; |
Joe Bolinger | de94aa0 | 2021-12-09 17:00:32 -0800 | [diff] [blame] | 320 | } |
| 321 | |
| 322 | void FakeFingerprintEngine::enumerateEnrollmentsImpl(ISessionCallback* cb) { |
| 323 | BEGIN_OP(0); |
| 324 | |
| 325 | std::vector<int32_t> ids; |
Jeff Pu | 6ccd956 | 2024-02-21 10:46:35 -0500 | [diff] [blame^] | 326 | for (auto& enrollment : Fingerprint::cfg().getopt<OptIntVec>("enrollments")) { |
Joe Bolinger | de94aa0 | 2021-12-09 17:00:32 -0800 | [diff] [blame] | 327 | auto id = enrollment.value_or(0); |
| 328 | if (id > 0) { |
| 329 | ids.push_back(id); |
| 330 | } |
| 331 | } |
| 332 | |
| 333 | cb->onEnrollmentsEnumerated(ids); |
| 334 | } |
| 335 | |
| 336 | void FakeFingerprintEngine::removeEnrollmentsImpl(ISessionCallback* cb, |
| 337 | const std::vector<int32_t>& enrollmentIds) { |
| 338 | BEGIN_OP(0); |
| 339 | |
| 340 | std::vector<std::optional<int32_t>> newEnrollments; |
| 341 | std::vector<int32_t> removed; |
Jeff Pu | 6ccd956 | 2024-02-21 10:46:35 -0500 | [diff] [blame^] | 342 | for (auto& enrollment : Fingerprint::cfg().getopt<OptIntVec>("enrollments")) { |
Joe Bolinger | de94aa0 | 2021-12-09 17:00:32 -0800 | [diff] [blame] | 343 | auto id = enrollment.value_or(0); |
| 344 | if (std::find(enrollmentIds.begin(), enrollmentIds.end(), id) != enrollmentIds.end()) { |
| 345 | removed.push_back(id); |
| 346 | } else if (id > 0) { |
| 347 | newEnrollments.emplace_back(id); |
| 348 | } |
| 349 | } |
Jeff Pu | 6ccd956 | 2024-02-21 10:46:35 -0500 | [diff] [blame^] | 350 | Fingerprint::cfg().setopt<OptIntVec>("enrollments", newEnrollments); |
Joe Bolinger | de94aa0 | 2021-12-09 17:00:32 -0800 | [diff] [blame] | 351 | |
| 352 | cb->onEnrollmentsRemoved(enrollmentIds); |
| 353 | } |
| 354 | |
| 355 | void FakeFingerprintEngine::getAuthenticatorIdImpl(ISessionCallback* cb) { |
| 356 | BEGIN_OP(0); |
Jeff Pu | 343ca94 | 2022-09-14 15:56:30 -0400 | [diff] [blame] | 357 | int64_t authenticatorId; |
Jeff Pu | 6ccd956 | 2024-02-21 10:46:35 -0500 | [diff] [blame^] | 358 | if (Fingerprint::cfg().getopt<OptIntVec>("enrollments").size() == 0) { |
Jeff Pu | 343ca94 | 2022-09-14 15:56:30 -0400 | [diff] [blame] | 359 | authenticatorId = 0; |
| 360 | } else { |
Jeff Pu | 6ccd956 | 2024-02-21 10:46:35 -0500 | [diff] [blame^] | 361 | authenticatorId = Fingerprint::cfg().get<std::int64_t>("authenticator_id"); |
Jeff Pu | 343ca94 | 2022-09-14 15:56:30 -0400 | [diff] [blame] | 362 | if (authenticatorId == 0) authenticatorId = 1; |
Jeff Pu | 63f33c7 | 2022-07-28 16:06:23 -0400 | [diff] [blame] | 363 | } |
| 364 | cb->onAuthenticatorIdRetrieved(authenticatorId); |
Joe Bolinger | de94aa0 | 2021-12-09 17:00:32 -0800 | [diff] [blame] | 365 | } |
| 366 | |
| 367 | void FakeFingerprintEngine::invalidateAuthenticatorIdImpl(ISessionCallback* cb) { |
| 368 | BEGIN_OP(0); |
Jeff Pu | 343ca94 | 2022-09-14 15:56:30 -0400 | [diff] [blame] | 369 | int64_t newId; |
Jeff Pu | 6ccd956 | 2024-02-21 10:46:35 -0500 | [diff] [blame^] | 370 | if (Fingerprint::cfg().getopt<OptIntVec>("enrollments").size() == 0) { |
Jeff Pu | 343ca94 | 2022-09-14 15:56:30 -0400 | [diff] [blame] | 371 | newId = 0; |
| 372 | } else { |
Jeff Pu | 6ccd956 | 2024-02-21 10:46:35 -0500 | [diff] [blame^] | 373 | auto id = Fingerprint::cfg().get<std::int64_t>("authenticator_id"); |
Jeff Pu | 343ca94 | 2022-09-14 15:56:30 -0400 | [diff] [blame] | 374 | newId = id + 1; |
| 375 | } |
Jeff Pu | 6ccd956 | 2024-02-21 10:46:35 -0500 | [diff] [blame^] | 376 | Fingerprint::cfg().set<std::int64_t>("authenticator_id", newId); |
Joe Bolinger | de94aa0 | 2021-12-09 17:00:32 -0800 | [diff] [blame] | 377 | cb->onAuthenticatorIdInvalidated(newId); |
| 378 | } |
| 379 | |
| 380 | void FakeFingerprintEngine::resetLockoutImpl(ISessionCallback* cb, |
Jeff Pu | 343ca94 | 2022-09-14 15:56:30 -0400 | [diff] [blame] | 381 | const keymaster::HardwareAuthToken& hat) { |
Joe Bolinger | de94aa0 | 2021-12-09 17:00:32 -0800 | [diff] [blame] | 382 | BEGIN_OP(0); |
Jeff Pu | 343ca94 | 2022-09-14 15:56:30 -0400 | [diff] [blame] | 383 | if (hat.mac.empty()) { |
| 384 | LOG(ERROR) << "Fail: hat in resetLockout()"; |
| 385 | cb->onError(Error::UNABLE_TO_PROCESS, 0 /* vendorError */); |
| 386 | return; |
| 387 | } |
Jeff Pu | c6f2146 | 2023-08-04 13:41:37 +0000 | [diff] [blame] | 388 | clearLockout(cb); |
Jeff Pu | 29df1e9 | 2023-10-06 14:12:44 +0000 | [diff] [blame] | 389 | if (isLockoutTimerStarted) isLockoutTimerAborted = true; |
Jeff Pu | c6f2146 | 2023-08-04 13:41:37 +0000 | [diff] [blame] | 390 | } |
| 391 | |
| 392 | void FakeFingerprintEngine::clearLockout(ISessionCallback* cb) { |
Jeff Pu | 6ccd956 | 2024-02-21 10:46:35 -0500 | [diff] [blame^] | 393 | Fingerprint::cfg().set<bool>("lockout", false); |
Joe Bolinger | de94aa0 | 2021-12-09 17:00:32 -0800 | [diff] [blame] | 394 | cb->onLockoutCleared(); |
Jeff Pu | 5265318 | 2022-10-12 16:27:23 -0400 | [diff] [blame] | 395 | mLockoutTracker.reset(); |
Joe Bolinger | de94aa0 | 2021-12-09 17:00:32 -0800 | [diff] [blame] | 396 | } |
| 397 | |
Jeff Pu | 63f33c7 | 2022-07-28 16:06:23 -0400 | [diff] [blame] | 398 | ndk::ScopedAStatus FakeFingerprintEngine::onPointerDownImpl(int32_t /*pointerId*/, int32_t /*x*/, |
| 399 | int32_t /*y*/, float /*minor*/, |
| 400 | float /*major*/) { |
| 401 | BEGIN_OP(0); |
Jeff Pu | def5b04 | 2023-05-25 14:28:16 -0400 | [diff] [blame] | 402 | fingerDownAction(); |
Jeff Pu | 63f33c7 | 2022-07-28 16:06:23 -0400 | [diff] [blame] | 403 | return ndk::ScopedAStatus::ok(); |
| 404 | } |
| 405 | |
| 406 | ndk::ScopedAStatus FakeFingerprintEngine::onPointerUpImpl(int32_t /*pointerId*/) { |
| 407 | BEGIN_OP(0); |
Jeff Pu | 74e25d2 | 2024-01-08 22:21:20 +0000 | [diff] [blame] | 408 | mFingerIsDown = false; |
Jeff Pu | 63f33c7 | 2022-07-28 16:06:23 -0400 | [diff] [blame] | 409 | return ndk::ScopedAStatus::ok(); |
| 410 | } |
| 411 | |
| 412 | ndk::ScopedAStatus FakeFingerprintEngine::onUiReadyImpl() { |
| 413 | BEGIN_OP(0); |
| 414 | return ndk::ScopedAStatus::ok(); |
| 415 | } |
| 416 | |
| 417 | bool FakeFingerprintEngine::getSensorLocationConfig(SensorLocation& out) { |
Jeff Pu | 6ccd956 | 2024-02-21 10:46:35 -0500 | [diff] [blame^] | 418 | auto loc = Fingerprint::cfg().get<std::string>("sensor_location"); |
Jeff Pu | 63f33c7 | 2022-07-28 16:06:23 -0400 | [diff] [blame] | 419 | auto isValidStr = false; |
| 420 | auto dim = Util::split(loc, ":"); |
| 421 | |
| 422 | if (dim.size() < 3 or dim.size() > 4) { |
| 423 | if (!loc.empty()) LOG(WARNING) << "Invalid sensor location input (x:y:radius):" + loc; |
| 424 | return false; |
| 425 | } else { |
| 426 | int32_t x, y, r; |
| 427 | std::string d = ""; |
| 428 | if (dim.size() >= 3) { |
| 429 | isValidStr = ParseInt(dim[0], &x) && ParseInt(dim[1], &y) && ParseInt(dim[2], &r); |
| 430 | } |
| 431 | if (dim.size() >= 4) { |
| 432 | d = dim[3]; |
| 433 | } |
Jeff Pu | def5b04 | 2023-05-25 14:28:16 -0400 | [diff] [blame] | 434 | if (isValidStr) |
| 435 | out = {.sensorLocationX = x, .sensorLocationY = y, .sensorRadius = r, .display = d}; |
Jeff Pu | 63f33c7 | 2022-07-28 16:06:23 -0400 | [diff] [blame] | 436 | |
| 437 | return isValidStr; |
| 438 | } |
| 439 | } |
| 440 | SensorLocation FakeFingerprintEngine::getSensorLocation() { |
| 441 | SensorLocation location; |
| 442 | |
| 443 | if (getSensorLocationConfig(location)) { |
| 444 | return location; |
| 445 | } else { |
| 446 | return defaultSensorLocation(); |
| 447 | } |
| 448 | } |
| 449 | |
| 450 | SensorLocation FakeFingerprintEngine::defaultSensorLocation() { |
Jeff Pu | def5b04 | 2023-05-25 14:28:16 -0400 | [diff] [blame] | 451 | return SensorLocation(); |
Jeff Pu | 63f33c7 | 2022-07-28 16:06:23 -0400 | [diff] [blame] | 452 | } |
Jeff Pu | 343ca94 | 2022-09-14 15:56:30 -0400 | [diff] [blame] | 453 | |
Jeff Pu | 343ca94 | 2022-09-14 15:56:30 -0400 | [diff] [blame] | 454 | std::pair<AcquiredInfo, int32_t> FakeFingerprintEngine::convertAcquiredInfo(int32_t code) { |
| 455 | std::pair<AcquiredInfo, int32_t> res; |
| 456 | if (code > FINGERPRINT_ACQUIRED_VENDOR_BASE) { |
| 457 | res.first = AcquiredInfo::VENDOR; |
| 458 | res.second = code - FINGERPRINT_ACQUIRED_VENDOR_BASE; |
| 459 | } else { |
| 460 | res.first = (AcquiredInfo)code; |
| 461 | res.second = 0; |
| 462 | } |
| 463 | return res; |
| 464 | } |
| 465 | |
| 466 | std::pair<Error, int32_t> FakeFingerprintEngine::convertError(int32_t code) { |
| 467 | std::pair<Error, int32_t> res; |
| 468 | if (code > FINGERPRINT_ERROR_VENDOR_BASE) { |
| 469 | res.first = Error::VENDOR; |
| 470 | res.second = code - FINGERPRINT_ERROR_VENDOR_BASE; |
| 471 | } else { |
| 472 | res.first = (Error)code; |
| 473 | res.second = 0; |
| 474 | } |
| 475 | return res; |
| 476 | } |
| 477 | |
Jeff Pu | 5265318 | 2022-10-12 16:27:23 -0400 | [diff] [blame] | 478 | int32_t FakeFingerprintEngine::getLatency( |
| 479 | const std::vector<std::optional<std::int32_t>>& latencyIn) { |
| 480 | int32_t res = DEFAULT_LATENCY; |
| 481 | |
| 482 | std::vector<int32_t> latency; |
| 483 | for (auto x : latencyIn) |
| 484 | if (x.has_value()) latency.push_back(*x); |
| 485 | |
| 486 | switch (latency.size()) { |
| 487 | case 0: |
| 488 | break; |
| 489 | case 1: |
| 490 | res = latency[0]; |
| 491 | break; |
| 492 | case 2: |
| 493 | res = getRandomInRange(latency[0], latency[1]); |
| 494 | break; |
| 495 | default: |
| 496 | LOG(ERROR) << "ERROR: unexpected input of size " << latency.size(); |
| 497 | break; |
| 498 | } |
| 499 | |
| 500 | return res; |
| 501 | } |
| 502 | |
| 503 | int32_t FakeFingerprintEngine::getRandomInRange(int32_t bound1, int32_t bound2) { |
| 504 | std::uniform_int_distribution<int32_t> dist(std::min(bound1, bound2), std::max(bound1, bound2)); |
| 505 | return dist(mRandom); |
| 506 | } |
| 507 | |
Jeff Pu | 437516e | 2023-06-28 15:21:21 +0000 | [diff] [blame] | 508 | bool FakeFingerprintEngine::checkSensorLockout(ISessionCallback* cb) { |
| 509 | FakeLockoutTracker::LockoutMode lockoutMode = mLockoutTracker.getMode(); |
| 510 | if (lockoutMode == FakeLockoutTracker::LockoutMode::kPermanent) { |
| 511 | LOG(ERROR) << "Fail: lockout permanent"; |
| 512 | cb->onLockoutPermanent(); |
Jeff Pu | c6f2146 | 2023-08-04 13:41:37 +0000 | [diff] [blame] | 513 | isLockoutTimerAborted = true; |
Jeff Pu | 437516e | 2023-06-28 15:21:21 +0000 | [diff] [blame] | 514 | return true; |
| 515 | } else if (lockoutMode == FakeLockoutTracker::LockoutMode::kTimed) { |
| 516 | int64_t timeLeft = mLockoutTracker.getLockoutTimeLeft(); |
| 517 | LOG(ERROR) << "Fail: lockout timed " << timeLeft; |
| 518 | cb->onLockoutTimed(timeLeft); |
Jeff Pu | c6f2146 | 2023-08-04 13:41:37 +0000 | [diff] [blame] | 519 | if (isLockoutTimerSupported && !isLockoutTimerStarted) startLockoutTimer(timeLeft, cb); |
Jeff Pu | 437516e | 2023-06-28 15:21:21 +0000 | [diff] [blame] | 520 | return true; |
| 521 | } |
| 522 | return false; |
| 523 | } |
Jeff Pu | c6f2146 | 2023-08-04 13:41:37 +0000 | [diff] [blame] | 524 | |
| 525 | void FakeFingerprintEngine::startLockoutTimer(int64_t timeout, ISessionCallback* cb) { |
| 526 | BEGIN_OP(0); |
| 527 | std::function<void(ISessionCallback*)> action = |
| 528 | std::bind(&FakeFingerprintEngine::lockoutTimerExpired, this, std::placeholders::_1); |
| 529 | std::thread([timeout, action, cb]() { |
| 530 | std::this_thread::sleep_for(std::chrono::milliseconds(timeout)); |
| 531 | action(cb); |
| 532 | }).detach(); |
| 533 | |
| 534 | isLockoutTimerStarted = true; |
| 535 | } |
| 536 | void FakeFingerprintEngine::lockoutTimerExpired(ISessionCallback* cb) { |
Jeff Pu | 29df1e9 | 2023-10-06 14:12:44 +0000 | [diff] [blame] | 537 | BEGIN_OP(0); |
Jeff Pu | c6f2146 | 2023-08-04 13:41:37 +0000 | [diff] [blame] | 538 | if (!isLockoutTimerAborted) { |
| 539 | clearLockout(cb); |
| 540 | } |
| 541 | isLockoutTimerStarted = false; |
| 542 | isLockoutTimerAborted = false; |
| 543 | } |
Jeff Pu | 74e25d2 | 2024-01-08 22:21:20 +0000 | [diff] [blame] | 544 | |
| 545 | void FakeFingerprintEngine::waitForFingerDown(ISessionCallback* cb, |
| 546 | const std::future<void>& cancel) { |
| 547 | while (!mFingerIsDown) { |
| 548 | if (shouldCancel(cancel)) { |
| 549 | LOG(ERROR) << "waitForFingerDown, Fail: cancel"; |
| 550 | cb->onError(Error::CANCELED, 0 /* vendorCode */); |
| 551 | return; |
| 552 | } |
| 553 | SLEEP_MS(10); |
| 554 | } |
| 555 | } |
| 556 | |
Joe Bolinger | de94aa0 | 2021-12-09 17:00:32 -0800 | [diff] [blame] | 557 | } // namespace aidl::android::hardware::biometrics::fingerprint |