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