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() |
| 35 | : mRandom(std::mt19937::default_seed), mWorkMode(WorkMode::kIdle) {} |
| 36 | |
Joe Bolinger | de94aa0 | 2021-12-09 17:00:32 -0800 | [diff] [blame] | 37 | void FakeFingerprintEngine::generateChallengeImpl(ISessionCallback* cb) { |
| 38 | BEGIN_OP(0); |
| 39 | std::uniform_int_distribution<int64_t> dist; |
| 40 | auto challenge = dist(mRandom); |
| 41 | FingerprintHalProperties::challenge(challenge); |
| 42 | cb->onChallengeGenerated(challenge); |
| 43 | } |
| 44 | |
| 45 | void FakeFingerprintEngine::revokeChallengeImpl(ISessionCallback* cb, int64_t challenge) { |
| 46 | BEGIN_OP(0); |
| 47 | FingerprintHalProperties::challenge({}); |
| 48 | cb->onChallengeRevoked(challenge); |
| 49 | } |
| 50 | |
| 51 | void FakeFingerprintEngine::enrollImpl(ISessionCallback* cb, |
| 52 | const keymaster::HardwareAuthToken& hat, |
| 53 | const std::future<void>& cancel) { |
Jeff Pu | def5b04 | 2023-05-25 14:28:16 -0400 | [diff] [blame] | 54 | BEGIN_OP(0); |
Jeff Pu | 5055e3c | 2023-07-27 11:26:50 -0400 | [diff] [blame] | 55 | |
| 56 | // Do proper HAT verification in the real implementation. |
| 57 | if (hat.mac.empty()) { |
| 58 | LOG(ERROR) << "Fail: hat"; |
| 59 | cb->onError(Error::UNABLE_TO_PROCESS, 0 /* vendorError */); |
| 60 | return; |
| 61 | } |
| 62 | |
Jeff Pu | def5b04 | 2023-05-25 14:28:16 -0400 | [diff] [blame] | 63 | updateContext(WorkMode::kEnroll, cb, const_cast<std::future<void>&>(cancel), 0, hat); |
| 64 | } |
| 65 | |
| 66 | void FakeFingerprintEngine::authenticateImpl(ISessionCallback* cb, int64_t operationId, |
| 67 | const std::future<void>& cancel) { |
| 68 | BEGIN_OP(0); |
| 69 | updateContext(WorkMode::kAuthenticate, cb, const_cast<std::future<void>&>(cancel), operationId, |
| 70 | keymaster::HardwareAuthToken()); |
| 71 | } |
| 72 | |
| 73 | void FakeFingerprintEngine::detectInteractionImpl(ISessionCallback* cb, |
| 74 | const std::future<void>& cancel) { |
| 75 | BEGIN_OP(0); |
| 76 | |
| 77 | auto detectInteractionSupported = |
| 78 | FingerprintHalProperties::detect_interaction().value_or(false); |
| 79 | if (!detectInteractionSupported) { |
| 80 | LOG(ERROR) << "Detect interaction is not supported"; |
| 81 | cb->onError(Error::UNABLE_TO_PROCESS, 0 /* vendorError */); |
| 82 | return; |
| 83 | } |
| 84 | |
| 85 | updateContext(WorkMode::kDetectInteract, cb, const_cast<std::future<void>&>(cancel), 0, |
| 86 | keymaster::HardwareAuthToken()); |
| 87 | } |
| 88 | |
| 89 | void FakeFingerprintEngine::updateContext(WorkMode mode, ISessionCallback* cb, |
| 90 | std::future<void>& cancel, int64_t operationId, |
| 91 | const keymaster::HardwareAuthToken& hat) { |
| 92 | mCancel = std::move(cancel); |
| 93 | mWorkMode = mode; |
| 94 | mCb = cb; |
| 95 | mOperationId = operationId; |
| 96 | mHat = hat; |
| 97 | } |
| 98 | |
| 99 | void FakeFingerprintEngine::fingerDownAction() { |
Jeff Pu | 073af18 | 2023-07-12 18:53:52 +0000 | [diff] [blame] | 100 | bool isTerminal = false; |
Jeff Pu | def5b04 | 2023-05-25 14:28:16 -0400 | [diff] [blame] | 101 | LOG(INFO) << __func__; |
| 102 | switch (mWorkMode) { |
| 103 | case WorkMode::kAuthenticate: |
Jeff Pu | 073af18 | 2023-07-12 18:53:52 +0000 | [diff] [blame] | 104 | isTerminal = onAuthenticateFingerDown(mCb, mOperationId, mCancel); |
Jeff Pu | def5b04 | 2023-05-25 14:28:16 -0400 | [diff] [blame] | 105 | break; |
| 106 | case WorkMode::kEnroll: |
Jeff Pu | 073af18 | 2023-07-12 18:53:52 +0000 | [diff] [blame] | 107 | isTerminal = onEnrollFingerDown(mCb, mHat, mCancel); |
Jeff Pu | def5b04 | 2023-05-25 14:28:16 -0400 | [diff] [blame] | 108 | break; |
| 109 | case WorkMode::kDetectInteract: |
Jeff Pu | 073af18 | 2023-07-12 18:53:52 +0000 | [diff] [blame] | 110 | isTerminal = onDetectInteractFingerDown(mCb, mCancel); |
Jeff Pu | def5b04 | 2023-05-25 14:28:16 -0400 | [diff] [blame] | 111 | break; |
| 112 | default: |
| 113 | LOG(WARNING) << "unexpected mode: on fingerDownAction(), " << (int)mWorkMode; |
| 114 | break; |
| 115 | } |
Jeff Pu | 073af18 | 2023-07-12 18:53:52 +0000 | [diff] [blame] | 116 | |
| 117 | if (isTerminal) { |
| 118 | mWorkMode = WorkMode::kIdle; |
| 119 | } |
Jeff Pu | def5b04 | 2023-05-25 14:28:16 -0400 | [diff] [blame] | 120 | } |
| 121 | |
Jeff Pu | 073af18 | 2023-07-12 18:53:52 +0000 | [diff] [blame] | 122 | bool FakeFingerprintEngine::onEnrollFingerDown(ISessionCallback* cb, |
Jeff Pu | 5055e3c | 2023-07-27 11:26:50 -0400 | [diff] [blame] | 123 | const keymaster::HardwareAuthToken&, |
Jeff Pu | def5b04 | 2023-05-25 14:28:16 -0400 | [diff] [blame] | 124 | const std::future<void>& cancel) { |
Jeff Pu | 5265318 | 2022-10-12 16:27:23 -0400 | [diff] [blame] | 125 | BEGIN_OP(getLatency(FingerprintHalProperties::operation_enroll_latency())); |
Joe Bolinger | de94aa0 | 2021-12-09 17:00:32 -0800 | [diff] [blame] | 126 | |
Jeff Pu | 343ca94 | 2022-09-14 15:56:30 -0400 | [diff] [blame] | 127 | // Force error-out |
| 128 | auto err = FingerprintHalProperties::operation_enroll_error().value_or(0); |
| 129 | if (err != 0) { |
| 130 | LOG(ERROR) << "Fail: operation_enroll_error"; |
| 131 | auto ec = convertError(err); |
| 132 | cb->onError(ec.first, ec.second); |
Jeff Pu | 073af18 | 2023-07-12 18:53:52 +0000 | [diff] [blame] | 133 | return true; |
Joe Bolinger | de94aa0 | 2021-12-09 17:00:32 -0800 | [diff] [blame] | 134 | } |
| 135 | |
Jeff Pu | 343ca94 | 2022-09-14 15:56:30 -0400 | [diff] [blame] | 136 | // Format is "<id>:<progress_ms-[acquiredInfo..]>,...:<result> |
Joe Bolinger | de94aa0 | 2021-12-09 17:00:32 -0800 | [diff] [blame] | 137 | auto nextEnroll = FingerprintHalProperties::next_enrollment().value_or(""); |
Joshua McCloskey | db009a5 | 2022-05-10 05:18:20 +0000 | [diff] [blame] | 138 | auto parts = Util::split(nextEnroll, ":"); |
Joe Bolinger | de94aa0 | 2021-12-09 17:00:32 -0800 | [diff] [blame] | 139 | if (parts.size() != 3) { |
Jeff Pu | 343ca94 | 2022-09-14 15:56:30 -0400 | [diff] [blame] | 140 | LOG(ERROR) << "Fail: invalid next_enrollment:" << nextEnroll; |
Joe Bolinger | de94aa0 | 2021-12-09 17:00:32 -0800 | [diff] [blame] | 141 | cb->onError(Error::VENDOR, 0 /* vendorError */); |
Jeff Pu | 073af18 | 2023-07-12 18:53:52 +0000 | [diff] [blame] | 142 | return true; |
Joe Bolinger | de94aa0 | 2021-12-09 17:00:32 -0800 | [diff] [blame] | 143 | } |
| 144 | auto enrollmentId = std::stoi(parts[0]); |
Jeff Pu | 343ca94 | 2022-09-14 15:56:30 -0400 | [diff] [blame] | 145 | auto progress = parseEnrollmentCapture(parts[1]); |
| 146 | for (size_t i = 0; i < progress.size(); i += 2) { |
| 147 | auto left = (progress.size() - i) / 2 - 1; |
| 148 | auto duration = progress[i][0]; |
| 149 | auto acquired = progress[i + 1]; |
| 150 | auto N = acquired.size(); |
Joe Bolinger | de94aa0 | 2021-12-09 17:00:32 -0800 | [diff] [blame] | 151 | |
Jeff Pu | 343ca94 | 2022-09-14 15:56:30 -0400 | [diff] [blame] | 152 | for (int j = 0; j < N; j++) { |
| 153 | SLEEP_MS(duration / N); |
| 154 | |
| 155 | if (shouldCancel(cancel)) { |
| 156 | LOG(ERROR) << "Fail: cancel"; |
| 157 | cb->onError(Error::CANCELED, 0 /* vendorCode */); |
Jeff Pu | 073af18 | 2023-07-12 18:53:52 +0000 | [diff] [blame] | 158 | return true; |
Jeff Pu | 343ca94 | 2022-09-14 15:56:30 -0400 | [diff] [blame] | 159 | } |
| 160 | auto ac = convertAcquiredInfo(acquired[j]); |
| 161 | cb->onAcquired(ac.first, ac.second); |
Joe Bolinger | de94aa0 | 2021-12-09 17:00:32 -0800 | [diff] [blame] | 162 | } |
| 163 | |
Joe Bolinger | de94aa0 | 2021-12-09 17:00:32 -0800 | [diff] [blame] | 164 | if (left == 0 && !IS_TRUE(parts[2])) { // end and failed |
| 165 | LOG(ERROR) << "Fail: requested by caller: " << nextEnroll; |
| 166 | FingerprintHalProperties::next_enrollment({}); |
| 167 | cb->onError(Error::UNABLE_TO_PROCESS, 0 /* vendorCode */); |
| 168 | } else { // progress and update props if last time |
Jeff Pu | 343ca94 | 2022-09-14 15:56:30 -0400 | [diff] [blame] | 169 | LOG(INFO) << "onEnroll: " << enrollmentId << " left: " << left; |
Joe Bolinger | de94aa0 | 2021-12-09 17:00:32 -0800 | [diff] [blame] | 170 | if (left == 0) { |
| 171 | auto enrollments = FingerprintHalProperties::enrollments(); |
| 172 | enrollments.emplace_back(enrollmentId); |
| 173 | FingerprintHalProperties::enrollments(enrollments); |
| 174 | FingerprintHalProperties::next_enrollment({}); |
Jeff Pu | 343ca94 | 2022-09-14 15:56:30 -0400 | [diff] [blame] | 175 | // change authenticatorId after new enrollment |
| 176 | auto id = FingerprintHalProperties::authenticator_id().value_or(0); |
| 177 | auto newId = id + 1; |
| 178 | FingerprintHalProperties::authenticator_id(newId); |
Joe Bolinger | de94aa0 | 2021-12-09 17:00:32 -0800 | [diff] [blame] | 179 | LOG(INFO) << "Enrolled: " << enrollmentId; |
| 180 | } |
| 181 | cb->onEnrollmentProgress(enrollmentId, left); |
| 182 | } |
| 183 | } |
Jeff Pu | 073af18 | 2023-07-12 18:53:52 +0000 | [diff] [blame] | 184 | |
| 185 | return true; |
Joe Bolinger | de94aa0 | 2021-12-09 17:00:32 -0800 | [diff] [blame] | 186 | } |
| 187 | |
Jeff Pu | 073af18 | 2023-07-12 18:53:52 +0000 | [diff] [blame] | 188 | bool FakeFingerprintEngine::onAuthenticateFingerDown(ISessionCallback* cb, |
Jeff Pu | def5b04 | 2023-05-25 14:28:16 -0400 | [diff] [blame] | 189 | int64_t /* operationId */, |
| 190 | const std::future<void>& cancel) { |
Jeff Pu | 5265318 | 2022-10-12 16:27:23 -0400 | [diff] [blame] | 191 | BEGIN_OP(getLatency(FingerprintHalProperties::operation_authenticate_latency())); |
Joe Bolinger | de94aa0 | 2021-12-09 17:00:32 -0800 | [diff] [blame] | 192 | |
Jeff Pu | 343ca94 | 2022-09-14 15:56:30 -0400 | [diff] [blame] | 193 | int64_t now = Util::getSystemNanoTime(); |
| 194 | int64_t duration = FingerprintHalProperties::operation_authenticate_duration().value_or(10); |
| 195 | auto acquired = FingerprintHalProperties::operation_authenticate_acquired().value_or("1"); |
| 196 | auto acquiredInfos = parseIntSequence(acquired); |
| 197 | int N = acquiredInfos.size(); |
| 198 | |
| 199 | if (N == 0) { |
| 200 | LOG(ERROR) << "Fail to parse authentiate acquired info: " + acquired; |
| 201 | cb->onError(Error::UNABLE_TO_PROCESS, 0 /* vendorError */); |
Jeff Pu | 073af18 | 2023-07-12 18:53:52 +0000 | [diff] [blame] | 202 | return true; |
Jeff Pu | 343ca94 | 2022-09-14 15:56:30 -0400 | [diff] [blame] | 203 | } |
| 204 | |
Jeff Pu | 5265318 | 2022-10-12 16:27:23 -0400 | [diff] [blame] | 205 | // got lockout? |
Jeff Pu | 073af18 | 2023-07-12 18:53:52 +0000 | [diff] [blame] | 206 | if (checkSensorLockout(cb)) { |
| 207 | return FakeLockoutTracker::LockoutMode::kPermanent == mLockoutTracker.getMode(); |
| 208 | } |
Jeff Pu | 5265318 | 2022-10-12 16:27:23 -0400 | [diff] [blame] | 209 | |
Jeff Pu | 343ca94 | 2022-09-14 15:56:30 -0400 | [diff] [blame] | 210 | int i = 0; |
Joe Bolinger | de94aa0 | 2021-12-09 17:00:32 -0800 | [diff] [blame] | 211 | do { |
| 212 | if (FingerprintHalProperties::operation_authenticate_fails().value_or(false)) { |
| 213 | LOG(ERROR) << "Fail: operation_authenticate_fails"; |
Jeff Pu | 5265318 | 2022-10-12 16:27:23 -0400 | [diff] [blame] | 214 | mLockoutTracker.addFailedAttempt(); |
Jeff Pu | 343ca94 | 2022-09-14 15:56:30 -0400 | [diff] [blame] | 215 | cb->onAuthenticationFailed(); |
Jeff Pu | 073af18 | 2023-07-12 18:53:52 +0000 | [diff] [blame] | 216 | return false; |
Jeff Pu | 343ca94 | 2022-09-14 15:56:30 -0400 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | auto err = FingerprintHalProperties::operation_authenticate_error().value_or(0); |
| 220 | if (err != 0) { |
| 221 | LOG(ERROR) << "Fail: operation_authenticate_error"; |
| 222 | auto ec = convertError(err); |
| 223 | cb->onError(ec.first, ec.second); |
Jeff Pu | 073af18 | 2023-07-12 18:53:52 +0000 | [diff] [blame] | 224 | return true; /* simply terminating current operation for any user inserted error, |
| 225 | revisit if tests need*/ |
Joe Bolinger | de94aa0 | 2021-12-09 17:00:32 -0800 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | if (FingerprintHalProperties::lockout().value_or(false)) { |
| 229 | LOG(ERROR) << "Fail: lockout"; |
| 230 | cb->onLockoutPermanent(); |
| 231 | cb->onError(Error::HW_UNAVAILABLE, 0 /* vendorError */); |
Jeff Pu | 073af18 | 2023-07-12 18:53:52 +0000 | [diff] [blame] | 232 | return true; |
Joe Bolinger | de94aa0 | 2021-12-09 17:00:32 -0800 | [diff] [blame] | 233 | } |
| 234 | |
| 235 | if (shouldCancel(cancel)) { |
| 236 | LOG(ERROR) << "Fail: cancel"; |
| 237 | cb->onError(Error::CANCELED, 0 /* vendorCode */); |
Jeff Pu | 073af18 | 2023-07-12 18:53:52 +0000 | [diff] [blame] | 238 | return true; |
Joe Bolinger | de94aa0 | 2021-12-09 17:00:32 -0800 | [diff] [blame] | 239 | } |
| 240 | |
Jeff Pu | 343ca94 | 2022-09-14 15:56:30 -0400 | [diff] [blame] | 241 | if (i < N) { |
| 242 | auto ac = convertAcquiredInfo(acquiredInfos[i]); |
| 243 | cb->onAcquired(ac.first, ac.second); |
| 244 | i++; |
Joe Bolinger | de94aa0 | 2021-12-09 17:00:32 -0800 | [diff] [blame] | 245 | } |
| 246 | |
Jeff Pu | 343ca94 | 2022-09-14 15:56:30 -0400 | [diff] [blame] | 247 | SLEEP_MS(duration / N); |
Joshua McCloskey | db009a5 | 2022-05-10 05:18:20 +0000 | [diff] [blame] | 248 | } while (!Util::hasElapsed(now, duration)); |
Joe Bolinger | de94aa0 | 2021-12-09 17:00:32 -0800 | [diff] [blame] | 249 | |
Jeff Pu | 343ca94 | 2022-09-14 15:56:30 -0400 | [diff] [blame] | 250 | auto id = FingerprintHalProperties::enrollment_hit().value_or(0); |
| 251 | auto enrolls = FingerprintHalProperties::enrollments(); |
| 252 | auto isEnrolled = std::find(enrolls.begin(), enrolls.end(), id) != enrolls.end(); |
| 253 | if (id > 0 && isEnrolled) { |
| 254 | cb->onAuthenticationSucceeded(id, {} /* hat */); |
Jeff Pu | 5265318 | 2022-10-12 16:27:23 -0400 | [diff] [blame] | 255 | mLockoutTracker.reset(); |
Jeff Pu | 073af18 | 2023-07-12 18:53:52 +0000 | [diff] [blame] | 256 | return true; |
Jeff Pu | 343ca94 | 2022-09-14 15:56:30 -0400 | [diff] [blame] | 257 | } else { |
| 258 | LOG(ERROR) << "Fail: fingerprint not enrolled"; |
| 259 | cb->onAuthenticationFailed(); |
Jeff Pu | 5265318 | 2022-10-12 16:27:23 -0400 | [diff] [blame] | 260 | mLockoutTracker.addFailedAttempt(); |
Jeff Pu | 437516e | 2023-06-28 15:21:21 +0000 | [diff] [blame] | 261 | checkSensorLockout(cb); |
Jeff Pu | 8fec556 | 2023-07-20 13:07:04 +0000 | [diff] [blame] | 262 | return false; |
Jeff Pu | 343ca94 | 2022-09-14 15:56:30 -0400 | [diff] [blame] | 263 | } |
Joe Bolinger | de94aa0 | 2021-12-09 17:00:32 -0800 | [diff] [blame] | 264 | } |
| 265 | |
Jeff Pu | 073af18 | 2023-07-12 18:53:52 +0000 | [diff] [blame] | 266 | bool FakeFingerprintEngine::onDetectInteractFingerDown(ISessionCallback* cb, |
Jeff Pu | def5b04 | 2023-05-25 14:28:16 -0400 | [diff] [blame] | 267 | const std::future<void>& cancel) { |
Jeff Pu | 5265318 | 2022-10-12 16:27:23 -0400 | [diff] [blame] | 268 | BEGIN_OP(getLatency(FingerprintHalProperties::operation_detect_interaction_latency())); |
Joe Bolinger | de94aa0 | 2021-12-09 17:00:32 -0800 | [diff] [blame] | 269 | |
Jeff Pu | 343ca94 | 2022-09-14 15:56:30 -0400 | [diff] [blame] | 270 | int64_t duration = |
| 271 | FingerprintHalProperties::operation_detect_interaction_duration().value_or(10); |
Jeff Pu | 5265318 | 2022-10-12 16:27:23 -0400 | [diff] [blame] | 272 | |
Jeff Pu | 343ca94 | 2022-09-14 15:56:30 -0400 | [diff] [blame] | 273 | auto acquired = FingerprintHalProperties::operation_detect_interaction_acquired().value_or("1"); |
| 274 | auto acquiredInfos = parseIntSequence(acquired); |
| 275 | int N = acquiredInfos.size(); |
| 276 | int64_t now = Util::getSystemNanoTime(); |
| 277 | |
| 278 | if (N == 0) { |
| 279 | LOG(ERROR) << "Fail to parse detect interaction acquired info: " + acquired; |
| 280 | cb->onError(Error::UNABLE_TO_PROCESS, 0 /* vendorError */); |
Jeff Pu | 073af18 | 2023-07-12 18:53:52 +0000 | [diff] [blame] | 281 | return true; |
Joe Bolinger | de94aa0 | 2021-12-09 17:00:32 -0800 | [diff] [blame] | 282 | } |
| 283 | |
Jeff Pu | 343ca94 | 2022-09-14 15:56:30 -0400 | [diff] [blame] | 284 | int i = 0; |
| 285 | do { |
| 286 | auto err = FingerprintHalProperties::operation_detect_interaction_error().value_or(0); |
| 287 | if (err != 0) { |
| 288 | LOG(ERROR) << "Fail: operation_detect_interaction_error"; |
| 289 | auto ec = convertError(err); |
| 290 | cb->onError(ec.first, ec.second); |
Jeff Pu | 073af18 | 2023-07-12 18:53:52 +0000 | [diff] [blame] | 291 | return true; |
Jeff Pu | 343ca94 | 2022-09-14 15:56:30 -0400 | [diff] [blame] | 292 | } |
| 293 | |
| 294 | if (shouldCancel(cancel)) { |
| 295 | LOG(ERROR) << "Fail: cancel"; |
| 296 | cb->onError(Error::CANCELED, 0 /* vendorCode */); |
Jeff Pu | 073af18 | 2023-07-12 18:53:52 +0000 | [diff] [blame] | 297 | return true; |
Jeff Pu | 343ca94 | 2022-09-14 15:56:30 -0400 | [diff] [blame] | 298 | } |
| 299 | |
| 300 | if (i < N) { |
| 301 | auto ac = convertAcquiredInfo(acquiredInfos[i]); |
| 302 | cb->onAcquired(ac.first, ac.second); |
| 303 | i++; |
| 304 | } |
| 305 | SLEEP_MS(duration / N); |
| 306 | } while (!Util::hasElapsed(now, duration)); |
Joe Bolinger | de94aa0 | 2021-12-09 17:00:32 -0800 | [diff] [blame] | 307 | |
| 308 | auto id = FingerprintHalProperties::enrollment_hit().value_or(0); |
| 309 | auto enrolls = FingerprintHalProperties::enrollments(); |
| 310 | auto isEnrolled = std::find(enrolls.begin(), enrolls.end(), id) != enrolls.end(); |
| 311 | if (id <= 0 || !isEnrolled) { |
| 312 | LOG(ERROR) << "Fail: not enrolled"; |
| 313 | cb->onError(Error::UNABLE_TO_PROCESS, 0 /* vendorError */); |
Jeff Pu | 073af18 | 2023-07-12 18:53:52 +0000 | [diff] [blame] | 314 | return true; |
Joe Bolinger | de94aa0 | 2021-12-09 17:00:32 -0800 | [diff] [blame] | 315 | } |
| 316 | |
| 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; |
| 326 | for (auto& enrollment : FingerprintHalProperties::enrollments()) { |
| 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; |
| 342 | for (auto& enrollment : FingerprintHalProperties::enrollments()) { |
| 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 | } |
| 350 | FingerprintHalProperties::enrollments(newEnrollments); |
| 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; |
| 358 | if (FingerprintHalProperties::enrollments().size() == 0) { |
| 359 | authenticatorId = 0; |
| 360 | } else { |
| 361 | authenticatorId = FingerprintHalProperties::authenticator_id().value_or(0); |
| 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; |
| 370 | if (FingerprintHalProperties::enrollments().size() == 0) { |
| 371 | newId = 0; |
| 372 | } else { |
| 373 | auto id = FingerprintHalProperties::authenticator_id().value_or(0); |
| 374 | newId = id + 1; |
| 375 | } |
Joe Bolinger | de94aa0 | 2021-12-09 17:00:32 -0800 | [diff] [blame] | 376 | FingerprintHalProperties::authenticator_id(newId); |
| 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); |
| 389 | isLockoutTimerAborted = true; |
| 390 | } |
| 391 | |
| 392 | void FakeFingerprintEngine::clearLockout(ISessionCallback* cb) { |
Joe Bolinger | de94aa0 | 2021-12-09 17:00:32 -0800 | [diff] [blame] | 393 | FingerprintHalProperties::lockout(false); |
| 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); |
| 408 | return ndk::ScopedAStatus::ok(); |
| 409 | } |
| 410 | |
| 411 | ndk::ScopedAStatus FakeFingerprintEngine::onUiReadyImpl() { |
| 412 | BEGIN_OP(0); |
| 413 | return ndk::ScopedAStatus::ok(); |
| 414 | } |
| 415 | |
| 416 | bool FakeFingerprintEngine::getSensorLocationConfig(SensorLocation& out) { |
| 417 | auto loc = FingerprintHalProperties::sensor_location().value_or(""); |
| 418 | auto isValidStr = false; |
| 419 | auto dim = Util::split(loc, ":"); |
| 420 | |
| 421 | if (dim.size() < 3 or dim.size() > 4) { |
| 422 | if (!loc.empty()) LOG(WARNING) << "Invalid sensor location input (x:y:radius):" + loc; |
| 423 | return false; |
| 424 | } else { |
| 425 | int32_t x, y, r; |
| 426 | std::string d = ""; |
| 427 | if (dim.size() >= 3) { |
| 428 | isValidStr = ParseInt(dim[0], &x) && ParseInt(dim[1], &y) && ParseInt(dim[2], &r); |
| 429 | } |
| 430 | if (dim.size() >= 4) { |
| 431 | d = dim[3]; |
| 432 | } |
Jeff Pu | def5b04 | 2023-05-25 14:28:16 -0400 | [diff] [blame] | 433 | if (isValidStr) |
| 434 | out = {.sensorLocationX = x, .sensorLocationY = y, .sensorRadius = r, .display = d}; |
Jeff Pu | 63f33c7 | 2022-07-28 16:06:23 -0400 | [diff] [blame] | 435 | |
| 436 | return isValidStr; |
| 437 | } |
| 438 | } |
| 439 | SensorLocation FakeFingerprintEngine::getSensorLocation() { |
| 440 | SensorLocation location; |
| 441 | |
| 442 | if (getSensorLocationConfig(location)) { |
| 443 | return location; |
| 444 | } else { |
| 445 | return defaultSensorLocation(); |
| 446 | } |
| 447 | } |
| 448 | |
| 449 | SensorLocation FakeFingerprintEngine::defaultSensorLocation() { |
Jeff Pu | def5b04 | 2023-05-25 14:28:16 -0400 | [diff] [blame] | 450 | return SensorLocation(); |
Jeff Pu | 63f33c7 | 2022-07-28 16:06:23 -0400 | [diff] [blame] | 451 | } |
Jeff Pu | 343ca94 | 2022-09-14 15:56:30 -0400 | [diff] [blame] | 452 | |
| 453 | std::vector<int32_t> FakeFingerprintEngine::parseIntSequence(const std::string& str, |
| 454 | const std::string& sep) { |
| 455 | std::vector<std::string> seqs = Util::split(str, sep); |
| 456 | std::vector<int32_t> res; |
| 457 | |
| 458 | for (const auto& seq : seqs) { |
| 459 | int32_t val; |
| 460 | if (ParseInt(seq, &val)) { |
| 461 | res.push_back(val); |
| 462 | } else { |
| 463 | LOG(WARNING) << "Invalid int sequence:" + str; |
| 464 | res.clear(); |
| 465 | break; |
| 466 | } |
| 467 | } |
| 468 | |
| 469 | return res; |
| 470 | } |
| 471 | |
Jeff Pu | 5265318 | 2022-10-12 16:27:23 -0400 | [diff] [blame] | 472 | bool FakeFingerprintEngine::parseEnrollmentCaptureSingle(const std::string& str, |
| 473 | std::vector<std::vector<int32_t>>& res) { |
Jeff Pu | 343ca94 | 2022-09-14 15:56:30 -0400 | [diff] [blame] | 474 | std::vector<int32_t> defaultAcquiredInfo = {(int32_t)AcquiredInfo::GOOD}; |
Jeff Pu | 343ca94 | 2022-09-14 15:56:30 -0400 | [diff] [blame] | 475 | bool aborted = true; |
| 476 | |
Jeff Pu | 5265318 | 2022-10-12 16:27:23 -0400 | [diff] [blame] | 477 | do { |
| 478 | std::smatch sms; |
| 479 | // Parses strings like "1000-[5,1]" or "500" |
| 480 | std::regex ex("((\\d+)(-\\[([\\d|,]+)\\])?)"); |
| 481 | if (!regex_match(str.cbegin(), str.cend(), sms, ex)) break; |
| 482 | int32_t duration; |
| 483 | if (!ParseInt(sms.str(2), &duration)) break; |
| 484 | res.push_back({duration}); |
| 485 | if (!sms.str(4).empty()) { |
| 486 | auto acqv = parseIntSequence(sms.str(4)); |
| 487 | if (acqv.empty()) break; |
| 488 | res.push_back(acqv); |
Jeff Pu | 343ca94 | 2022-09-14 15:56:30 -0400 | [diff] [blame] | 489 | } else |
| 490 | res.push_back(defaultAcquiredInfo); |
Jeff Pu | 5265318 | 2022-10-12 16:27:23 -0400 | [diff] [blame] | 491 | aborted = false; |
| 492 | } while (0); |
Jeff Pu | 343ca94 | 2022-09-14 15:56:30 -0400 | [diff] [blame] | 493 | |
Jeff Pu | 5265318 | 2022-10-12 16:27:23 -0400 | [diff] [blame] | 494 | return !aborted; |
| 495 | } |
| 496 | |
| 497 | std::vector<std::vector<int32_t>> FakeFingerprintEngine::parseEnrollmentCapture( |
| 498 | const std::string& str) { |
| 499 | std::vector<std::vector<int32_t>> res; |
| 500 | |
| 501 | std::string s(str); |
| 502 | s.erase(std::remove_if(s.begin(), s.end(), ::isspace), s.end()); |
| 503 | bool aborted = false; |
| 504 | std::smatch sms; |
| 505 | // Parses strings like "1000-[5,1],500,800-[6,5,1]" |
| 506 | // ---------- --- ----------- |
| 507 | // into parts: A B C |
| 508 | while (regex_search(s, sms, std::regex("^(,)?(\\d+(-\\[[\\d|,]+\\])?)"))) { |
| 509 | if (!parseEnrollmentCaptureSingle(sms.str(2), res)) { |
| 510 | aborted = true; |
| 511 | break; |
| 512 | } |
| 513 | s = sms.suffix(); |
Jeff Pu | 343ca94 | 2022-09-14 15:56:30 -0400 | [diff] [blame] | 514 | } |
Jeff Pu | 5265318 | 2022-10-12 16:27:23 -0400 | [diff] [blame] | 515 | if (aborted || s.length() != 0) { |
Jeff Pu | 343ca94 | 2022-09-14 15:56:30 -0400 | [diff] [blame] | 516 | res.clear(); |
Jeff Pu | 5265318 | 2022-10-12 16:27:23 -0400 | [diff] [blame] | 517 | LOG(ERROR) << "Failed to parse enrollment captures:" + str; |
Jeff Pu | 343ca94 | 2022-09-14 15:56:30 -0400 | [diff] [blame] | 518 | } |
| 519 | |
| 520 | return res; |
| 521 | } |
| 522 | |
| 523 | std::pair<AcquiredInfo, int32_t> FakeFingerprintEngine::convertAcquiredInfo(int32_t code) { |
| 524 | std::pair<AcquiredInfo, int32_t> res; |
| 525 | if (code > FINGERPRINT_ACQUIRED_VENDOR_BASE) { |
| 526 | res.first = AcquiredInfo::VENDOR; |
| 527 | res.second = code - FINGERPRINT_ACQUIRED_VENDOR_BASE; |
| 528 | } else { |
| 529 | res.first = (AcquiredInfo)code; |
| 530 | res.second = 0; |
| 531 | } |
| 532 | return res; |
| 533 | } |
| 534 | |
| 535 | std::pair<Error, int32_t> FakeFingerprintEngine::convertError(int32_t code) { |
| 536 | std::pair<Error, int32_t> res; |
| 537 | if (code > FINGERPRINT_ERROR_VENDOR_BASE) { |
| 538 | res.first = Error::VENDOR; |
| 539 | res.second = code - FINGERPRINT_ERROR_VENDOR_BASE; |
| 540 | } else { |
| 541 | res.first = (Error)code; |
| 542 | res.second = 0; |
| 543 | } |
| 544 | return res; |
| 545 | } |
| 546 | |
Jeff Pu | 5265318 | 2022-10-12 16:27:23 -0400 | [diff] [blame] | 547 | int32_t FakeFingerprintEngine::getLatency( |
| 548 | const std::vector<std::optional<std::int32_t>>& latencyIn) { |
| 549 | int32_t res = DEFAULT_LATENCY; |
| 550 | |
| 551 | std::vector<int32_t> latency; |
| 552 | for (auto x : latencyIn) |
| 553 | if (x.has_value()) latency.push_back(*x); |
| 554 | |
| 555 | switch (latency.size()) { |
| 556 | case 0: |
| 557 | break; |
| 558 | case 1: |
| 559 | res = latency[0]; |
| 560 | break; |
| 561 | case 2: |
| 562 | res = getRandomInRange(latency[0], latency[1]); |
| 563 | break; |
| 564 | default: |
| 565 | LOG(ERROR) << "ERROR: unexpected input of size " << latency.size(); |
| 566 | break; |
| 567 | } |
| 568 | |
| 569 | return res; |
| 570 | } |
| 571 | |
| 572 | int32_t FakeFingerprintEngine::getRandomInRange(int32_t bound1, int32_t bound2) { |
| 573 | std::uniform_int_distribution<int32_t> dist(std::min(bound1, bound2), std::max(bound1, bound2)); |
| 574 | return dist(mRandom); |
| 575 | } |
| 576 | |
Jeff Pu | 437516e | 2023-06-28 15:21:21 +0000 | [diff] [blame] | 577 | bool FakeFingerprintEngine::checkSensorLockout(ISessionCallback* cb) { |
| 578 | FakeLockoutTracker::LockoutMode lockoutMode = mLockoutTracker.getMode(); |
| 579 | if (lockoutMode == FakeLockoutTracker::LockoutMode::kPermanent) { |
| 580 | LOG(ERROR) << "Fail: lockout permanent"; |
| 581 | cb->onLockoutPermanent(); |
Jeff Pu | c6f2146 | 2023-08-04 13:41:37 +0000 | [diff] [blame] | 582 | isLockoutTimerAborted = true; |
Jeff Pu | 437516e | 2023-06-28 15:21:21 +0000 | [diff] [blame] | 583 | return true; |
| 584 | } else if (lockoutMode == FakeLockoutTracker::LockoutMode::kTimed) { |
| 585 | int64_t timeLeft = mLockoutTracker.getLockoutTimeLeft(); |
| 586 | LOG(ERROR) << "Fail: lockout timed " << timeLeft; |
| 587 | cb->onLockoutTimed(timeLeft); |
Jeff Pu | c6f2146 | 2023-08-04 13:41:37 +0000 | [diff] [blame] | 588 | if (isLockoutTimerSupported && !isLockoutTimerStarted) startLockoutTimer(timeLeft, cb); |
Jeff Pu | 437516e | 2023-06-28 15:21:21 +0000 | [diff] [blame] | 589 | return true; |
| 590 | } |
| 591 | return false; |
| 592 | } |
Jeff Pu | c6f2146 | 2023-08-04 13:41:37 +0000 | [diff] [blame] | 593 | |
| 594 | void FakeFingerprintEngine::startLockoutTimer(int64_t timeout, ISessionCallback* cb) { |
| 595 | BEGIN_OP(0); |
| 596 | std::function<void(ISessionCallback*)> action = |
| 597 | std::bind(&FakeFingerprintEngine::lockoutTimerExpired, this, std::placeholders::_1); |
| 598 | std::thread([timeout, action, cb]() { |
| 599 | std::this_thread::sleep_for(std::chrono::milliseconds(timeout)); |
| 600 | action(cb); |
| 601 | }).detach(); |
| 602 | |
| 603 | isLockoutTimerStarted = true; |
| 604 | } |
| 605 | void FakeFingerprintEngine::lockoutTimerExpired(ISessionCallback* cb) { |
| 606 | if (!isLockoutTimerAborted) { |
| 607 | clearLockout(cb); |
| 608 | } |
| 609 | isLockoutTimerStarted = false; |
| 610 | isLockoutTimerAborted = false; |
| 611 | } |
Joe Bolinger | de94aa0 | 2021-12-09 17:00:32 -0800 | [diff] [blame] | 612 | } // namespace aidl::android::hardware::biometrics::fingerprint |