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