blob: e51f6777446d981d86338fb12b5246764965ba53 [file] [log] [blame]
Ilya Matyukhina9a3c852020-08-18 03:09:41 -07001/*
2 * Copyright (C) 2020 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Ilya Matyukhin48ff8962021-02-22 13:13:13 -080017#include "Session.h"
18
Kevin Chyn146f6c82021-02-10 11:32:35 -080019#include <android-base/logging.h>
Ilya Matyukhina9a3c852020-08-18 03:09:41 -070020
Joshua McCloskeyc8c0bad2022-05-10 05:17:44 +000021#include "util/CancellationSignal.h"
Ilya Matyukhina9a3c852020-08-18 03:09:41 -070022
23namespace aidl::android::hardware::biometrics::fingerprint {
24
Ilya Matyukhin48ff8962021-02-22 13:13:13 -080025Session::Session(int sensorId, int userId, std::shared_ptr<ISessionCallback> cb,
26 FakeFingerprintEngine* engine, WorkerThread* worker)
27 : mSensorId(sensorId),
28 mUserId(userId),
29 mCb(std::move(cb)),
30 mEngine(engine),
31 mWorker(worker),
32 mScheduledState(SessionState::IDLING),
33 mCurrentState(SessionState::IDLING) {
34 CHECK_GE(mSensorId, 0);
35 CHECK_GE(mUserId, 0);
36 CHECK(mEngine);
37 CHECK(mWorker);
38 CHECK(mCb);
39}
Ilya Matyukhina9a3c852020-08-18 03:09:41 -070040
Ilya Matyukhin48ff8962021-02-22 13:13:13 -080041void Session::scheduleStateOrCrash(SessionState state) {
Ilya Matyukhinaea213b2021-03-23 19:01:42 -070042 // TODO(b/166800618): call enterIdling from the terminal callbacks and restore these checks.
43 // CHECK(mScheduledState == SessionState::IDLING);
44 // CHECK(mCurrentState == SessionState::IDLING);
Ilya Matyukhin48ff8962021-02-22 13:13:13 -080045 mScheduledState = state;
46}
Ilya Matyukhina9a3c852020-08-18 03:09:41 -070047
Ilya Matyukhinaea213b2021-03-23 19:01:42 -070048void Session::enterStateOrCrash(SessionState state) {
Ilya Matyukhin48ff8962021-02-22 13:13:13 -080049 CHECK(mScheduledState == state);
f48dc9fc2021-03-10 04:40:58 +000050 mCurrentState = state;
Ilya Matyukhin48ff8962021-02-22 13:13:13 -080051 mScheduledState = SessionState::IDLING;
Ilya Matyukhin48ff8962021-02-22 13:13:13 -080052}
53
Ilya Matyukhinaea213b2021-03-23 19:01:42 -070054void Session::enterIdling() {
55 // TODO(b/166800618): call enterIdling from the terminal callbacks and rethink this conditional.
56 if (mCurrentState != SessionState::CLOSED) {
57 mCurrentState = SessionState::IDLING;
58 }
Ilya Matyukhin48ff8962021-02-22 13:13:13 -080059}
60
61bool Session::isClosed() {
62 return mCurrentState == SessionState::CLOSED;
63}
64
Ilya Matyukhinaea213b2021-03-23 19:01:42 -070065ndk::ScopedAStatus Session::generateChallenge() {
Kevin Chyn146f6c82021-02-10 11:32:35 -080066 LOG(INFO) << "generateChallenge";
Ilya Matyukhin48ff8962021-02-22 13:13:13 -080067 scheduleStateOrCrash(SessionState::GENERATING_CHALLENGE);
68
Ilya Matyukhinaea213b2021-03-23 19:01:42 -070069 mWorker->schedule(Callable::from([this] {
70 enterStateOrCrash(SessionState::GENERATING_CHALLENGE);
f8c8e4c62021-03-05 05:12:58 +000071 mEngine->generateChallengeImpl(mCb.get());
Ilya Matyukhinaea213b2021-03-23 19:01:42 -070072 enterIdling();
Ilya Matyukhin48ff8962021-02-22 13:13:13 -080073 }));
74
Ilya Matyukhin3d54f452020-10-15 17:32:09 -070075 return ndk::ScopedAStatus::ok();
76}
77
Ilya Matyukhinaea213b2021-03-23 19:01:42 -070078ndk::ScopedAStatus Session::revokeChallenge(int64_t challenge) {
Kevin Chyn146f6c82021-02-10 11:32:35 -080079 LOG(INFO) << "revokeChallenge";
Ilya Matyukhin48ff8962021-02-22 13:13:13 -080080 scheduleStateOrCrash(SessionState::REVOKING_CHALLENGE);
81
Ilya Matyukhinaea213b2021-03-23 19:01:42 -070082 mWorker->schedule(Callable::from([this, challenge] {
83 enterStateOrCrash(SessionState::REVOKING_CHALLENGE);
Ilya Matyukhin48ff8962021-02-22 13:13:13 -080084 mEngine->revokeChallengeImpl(mCb.get(), challenge);
Ilya Matyukhinaea213b2021-03-23 19:01:42 -070085 enterIdling();
Ilya Matyukhin48ff8962021-02-22 13:13:13 -080086 }));
87
Ilya Matyukhin3d54f452020-10-15 17:32:09 -070088 return ndk::ScopedAStatus::ok();
89}
90
Ilya Matyukhinaea213b2021-03-23 19:01:42 -070091ndk::ScopedAStatus Session::enroll(const keymaster::HardwareAuthToken& hat,
Ilya Matyukhin48ff8962021-02-22 13:13:13 -080092 std::shared_ptr<common::ICancellationSignal>* out) {
Kevin Chyn146f6c82021-02-10 11:32:35 -080093 LOG(INFO) << "enroll";
Ilya Matyukhin48ff8962021-02-22 13:13:13 -080094 scheduleStateOrCrash(SessionState::ENROLLING);
95
96 std::promise<void> cancellationPromise;
97 auto cancFuture = cancellationPromise.get_future();
98
Ilya Matyukhinaea213b2021-03-23 19:01:42 -070099 mWorker->schedule(Callable::from([this, hat, cancFuture = std::move(cancFuture)] {
100 enterStateOrCrash(SessionState::ENROLLING);
Ilya Matyukhin48ff8962021-02-22 13:13:13 -0800101 if (shouldCancel(cancFuture)) {
102 mCb->onError(Error::CANCELED, 0 /* vendorCode */);
103 } else {
Joe Bolingerde94aa02021-12-09 17:00:32 -0800104 mEngine->enrollImpl(mCb.get(), hat, cancFuture);
Ilya Matyukhin48ff8962021-02-22 13:13:13 -0800105 }
Ilya Matyukhinaea213b2021-03-23 19:01:42 -0700106 enterIdling();
Ilya Matyukhin48ff8962021-02-22 13:13:13 -0800107 }));
108
109 *out = SharedRefBase::make<CancellationSignal>(std::move(cancellationPromise));
Ilya Matyukhina9a3c852020-08-18 03:09:41 -0700110 return ndk::ScopedAStatus::ok();
111}
112
Ilya Matyukhinaea213b2021-03-23 19:01:42 -0700113ndk::ScopedAStatus Session::authenticate(int64_t operationId,
Ilya Matyukhin124e70a2021-02-12 13:00:15 -0800114 std::shared_ptr<common::ICancellationSignal>* out) {
Kevin Chyn146f6c82021-02-10 11:32:35 -0800115 LOG(INFO) << "authenticate";
Ilya Matyukhin48ff8962021-02-22 13:13:13 -0800116 scheduleStateOrCrash(SessionState::AUTHENTICATING);
117
118 std::promise<void> cancPromise;
119 auto cancFuture = cancPromise.get_future();
120
Ilya Matyukhinaea213b2021-03-23 19:01:42 -0700121 mWorker->schedule(Callable::from([this, operationId, cancFuture = std::move(cancFuture)] {
122 enterStateOrCrash(SessionState::AUTHENTICATING);
123 if (shouldCancel(cancFuture)) {
124 mCb->onError(Error::CANCELED, 0 /* vendorCode */);
125 } else {
Joe Bolingerde94aa02021-12-09 17:00:32 -0800126 mEngine->authenticateImpl(mCb.get(), operationId, cancFuture);
Ilya Matyukhinaea213b2021-03-23 19:01:42 -0700127 }
128 enterIdling();
129 }));
Ilya Matyukhin48ff8962021-02-22 13:13:13 -0800130
131 *out = SharedRefBase::make<CancellationSignal>(std::move(cancPromise));
Ilya Matyukhina9a3c852020-08-18 03:09:41 -0700132 return ndk::ScopedAStatus::ok();
133}
134
Ilya Matyukhinaea213b2021-03-23 19:01:42 -0700135ndk::ScopedAStatus Session::detectInteraction(std::shared_ptr<common::ICancellationSignal>* out) {
Kevin Chyn146f6c82021-02-10 11:32:35 -0800136 LOG(INFO) << "detectInteraction";
Ilya Matyukhin48ff8962021-02-22 13:13:13 -0800137 scheduleStateOrCrash(SessionState::DETECTING_INTERACTION);
138
139 std::promise<void> cancellationPromise;
140 auto cancFuture = cancellationPromise.get_future();
141
Ilya Matyukhinaea213b2021-03-23 19:01:42 -0700142 mWorker->schedule(Callable::from([this, cancFuture = std::move(cancFuture)] {
143 enterStateOrCrash(SessionState::DETECTING_INTERACTION);
Ilya Matyukhin48ff8962021-02-22 13:13:13 -0800144 if (shouldCancel(cancFuture)) {
145 mCb->onError(Error::CANCELED, 0 /* vendorCode */);
146 } else {
Joe Bolingerde94aa02021-12-09 17:00:32 -0800147 mEngine->detectInteractionImpl(mCb.get(), cancFuture);
Ilya Matyukhin48ff8962021-02-22 13:13:13 -0800148 }
Ilya Matyukhinaea213b2021-03-23 19:01:42 -0700149 enterIdling();
Ilya Matyukhin48ff8962021-02-22 13:13:13 -0800150 }));
151
152 *out = SharedRefBase::make<CancellationSignal>(std::move(cancellationPromise));
Ilya Matyukhina9a3c852020-08-18 03:09:41 -0700153 return ndk::ScopedAStatus::ok();
154}
155
Ilya Matyukhinaea213b2021-03-23 19:01:42 -0700156ndk::ScopedAStatus Session::enumerateEnrollments() {
Kevin Chyn146f6c82021-02-10 11:32:35 -0800157 LOG(INFO) << "enumerateEnrollments";
Ilya Matyukhin48ff8962021-02-22 13:13:13 -0800158 scheduleStateOrCrash(SessionState::ENUMERATING_ENROLLMENTS);
159
Ilya Matyukhinaea213b2021-03-23 19:01:42 -0700160 mWorker->schedule(Callable::from([this] {
161 enterStateOrCrash(SessionState::ENUMERATING_ENROLLMENTS);
Ilya Matyukhin48ff8962021-02-22 13:13:13 -0800162 mEngine->enumerateEnrollmentsImpl(mCb.get());
Ilya Matyukhinaea213b2021-03-23 19:01:42 -0700163 enterIdling();
Ilya Matyukhin48ff8962021-02-22 13:13:13 -0800164 }));
165
Ilya Matyukhina9a3c852020-08-18 03:09:41 -0700166 return ndk::ScopedAStatus::ok();
167}
168
Ilya Matyukhinaea213b2021-03-23 19:01:42 -0700169ndk::ScopedAStatus Session::removeEnrollments(const std::vector<int32_t>& enrollmentIds) {
Jeff Pu63f33c72022-07-28 16:06:23 -0400170 LOG(INFO) << "removeEnrollments, size:" << enrollmentIds.size();
Ilya Matyukhin48ff8962021-02-22 13:13:13 -0800171 scheduleStateOrCrash(SessionState::REMOVING_ENROLLMENTS);
172
Ilya Matyukhinaea213b2021-03-23 19:01:42 -0700173 mWorker->schedule(Callable::from([this, enrollmentIds] {
174 enterStateOrCrash(SessionState::REMOVING_ENROLLMENTS);
Ilya Matyukhin48ff8962021-02-22 13:13:13 -0800175 mEngine->removeEnrollmentsImpl(mCb.get(), enrollmentIds);
Ilya Matyukhinaea213b2021-03-23 19:01:42 -0700176 enterIdling();
Ilya Matyukhin48ff8962021-02-22 13:13:13 -0800177 }));
178
Ilya Matyukhina9a3c852020-08-18 03:09:41 -0700179 return ndk::ScopedAStatus::ok();
180}
181
Ilya Matyukhinaea213b2021-03-23 19:01:42 -0700182ndk::ScopedAStatus Session::getAuthenticatorId() {
Kevin Chyn146f6c82021-02-10 11:32:35 -0800183 LOG(INFO) << "getAuthenticatorId";
Ilya Matyukhin48ff8962021-02-22 13:13:13 -0800184 scheduleStateOrCrash(SessionState::GETTING_AUTHENTICATOR_ID);
185
Ilya Matyukhinaea213b2021-03-23 19:01:42 -0700186 mWorker->schedule(Callable::from([this] {
187 enterStateOrCrash(SessionState::GETTING_AUTHENTICATOR_ID);
Ilya Matyukhin48ff8962021-02-22 13:13:13 -0800188 mEngine->getAuthenticatorIdImpl(mCb.get());
Ilya Matyukhinaea213b2021-03-23 19:01:42 -0700189 enterIdling();
Ilya Matyukhin48ff8962021-02-22 13:13:13 -0800190 }));
191
Ilya Matyukhina9a3c852020-08-18 03:09:41 -0700192 return ndk::ScopedAStatus::ok();
193}
194
Ilya Matyukhinaea213b2021-03-23 19:01:42 -0700195ndk::ScopedAStatus Session::invalidateAuthenticatorId() {
Kevin Chyn146f6c82021-02-10 11:32:35 -0800196 LOG(INFO) << "invalidateAuthenticatorId";
Ilya Matyukhin48ff8962021-02-22 13:13:13 -0800197 scheduleStateOrCrash(SessionState::INVALIDATING_AUTHENTICATOR_ID);
198
Ilya Matyukhinaea213b2021-03-23 19:01:42 -0700199 mWorker->schedule(Callable::from([this] {
200 enterStateOrCrash(SessionState::INVALIDATING_AUTHENTICATOR_ID);
Ilya Matyukhin48ff8962021-02-22 13:13:13 -0800201 mEngine->invalidateAuthenticatorIdImpl(mCb.get());
Ilya Matyukhinaea213b2021-03-23 19:01:42 -0700202 enterIdling();
Ilya Matyukhin48ff8962021-02-22 13:13:13 -0800203 }));
204
Kevin Chyn6e862c32020-09-16 18:27:37 -0700205 return ndk::ScopedAStatus::ok();
206}
207
Ilya Matyukhinaea213b2021-03-23 19:01:42 -0700208ndk::ScopedAStatus Session::resetLockout(const keymaster::HardwareAuthToken& hat) {
Kevin Chyn146f6c82021-02-10 11:32:35 -0800209 LOG(INFO) << "resetLockout";
Ilya Matyukhin48ff8962021-02-22 13:13:13 -0800210 scheduleStateOrCrash(SessionState::RESETTING_LOCKOUT);
211
Ilya Matyukhinaea213b2021-03-23 19:01:42 -0700212 mWorker->schedule(Callable::from([this, hat] {
213 enterStateOrCrash(SessionState::RESETTING_LOCKOUT);
Ilya Matyukhin48ff8962021-02-22 13:13:13 -0800214 mEngine->resetLockoutImpl(mCb.get(), hat);
Ilya Matyukhinaea213b2021-03-23 19:01:42 -0700215 enterIdling();
Ilya Matyukhin48ff8962021-02-22 13:13:13 -0800216 }));
217
Ilya Matyukhina9a3c852020-08-18 03:09:41 -0700218 return ndk::ScopedAStatus::ok();
219}
220
Ilya Matyukhinaea213b2021-03-23 19:01:42 -0700221ndk::ScopedAStatus Session::close() {
Ilya Matyukhin71005c52021-02-17 12:44:14 -0800222 LOG(INFO) << "close";
Ilya Matyukhinaea213b2021-03-23 19:01:42 -0700223 // TODO(b/166800618): call enterIdling from the terminal callbacks and restore this check.
224 // CHECK(mCurrentState == SessionState::IDLING) << "Can't close a non-idling session.
225 // Crashing.";
Ilya Matyukhin48ff8962021-02-22 13:13:13 -0800226 mCurrentState = SessionState::CLOSED;
Ilya Matyukhincbbfa932021-03-22 13:25:15 -0700227 mCb->onSessionClosed();
Ilya Matyukhin71005c52021-02-17 12:44:14 -0800228 return ndk::ScopedAStatus::ok();
229}
230
Jeff Pu63f33c72022-07-28 16:06:23 -0400231ndk::ScopedAStatus Session::onPointerDown(int32_t pointerId, int32_t x, int32_t y, float minor,
232 float major) {
Kevin Chyn146f6c82021-02-10 11:32:35 -0800233 LOG(INFO) << "onPointerDown";
Jeff Pu63f33c72022-07-28 16:06:23 -0400234 mWorker->schedule(Callable::from([this, pointerId, x, y, minor, major] {
235 mEngine->onPointerDownImpl(pointerId, x, y, minor, major);
236 enterIdling();
237 }));
Ilya Matyukhina9a3c852020-08-18 03:09:41 -0700238 return ndk::ScopedAStatus::ok();
239}
240
Jeff Pu63f33c72022-07-28 16:06:23 -0400241ndk::ScopedAStatus Session::onPointerUp(int32_t pointerId) {
Kevin Chyn146f6c82021-02-10 11:32:35 -0800242 LOG(INFO) << "onPointerUp";
Jeff Pu63f33c72022-07-28 16:06:23 -0400243 mWorker->schedule(Callable::from([this, pointerId] {
244 mEngine->onPointerUpImpl(pointerId);
245 enterIdling();
246 }));
Ilya Matyukhina9a3c852020-08-18 03:09:41 -0700247 return ndk::ScopedAStatus::ok();
248}
249
250ndk::ScopedAStatus Session::onUiReady() {
Kevin Chyn146f6c82021-02-10 11:32:35 -0800251 LOG(INFO) << "onUiReady";
Jeff Pu63f33c72022-07-28 16:06:23 -0400252 mWorker->schedule(Callable::from([this] {
253 mEngine->onUiReadyImpl();
254 enterIdling();
255 }));
Ilya Matyukhina9a3c852020-08-18 03:09:41 -0700256 return ndk::ScopedAStatus::ok();
257}
Ilya Matyukhin48ff8962021-02-22 13:13:13 -0800258
Joe Bolinger13cb0fb2021-12-03 12:45:48 -0800259ndk::ScopedAStatus Session::authenticateWithContext(
260 int64_t operationId, const common::OperationContext& /*context*/,
261 std::shared_ptr<common::ICancellationSignal>* out) {
262 return authenticate(operationId, out);
263}
264
265ndk::ScopedAStatus Session::enrollWithContext(const keymaster::HardwareAuthToken& hat,
266 const common::OperationContext& /*context*/,
267 std::shared_ptr<common::ICancellationSignal>* out) {
268 return enroll(hat, out);
269}
270
271ndk::ScopedAStatus Session::detectInteractionWithContext(
272 const common::OperationContext& /*context*/,
273 std::shared_ptr<common::ICancellationSignal>* out) {
274 return detectInteraction(out);
275}
276
277ndk::ScopedAStatus Session::onPointerDownWithContext(const PointerContext& context) {
278 return onPointerDown(context.pointerId, context.x, context.y, context.minor, context.major);
279}
280
281ndk::ScopedAStatus Session::onPointerUpWithContext(const PointerContext& context) {
282 return onPointerUp(context.pointerId);
283}
284
Joe Bolinger25e98232022-01-24 18:56:23 +0000285ndk::ScopedAStatus Session::onContextChanged(const common::OperationContext& /*context*/) {
286 return ndk::ScopedAStatus::ok();
287}
288
Ilya Matyukhina9a3c852020-08-18 03:09:41 -0700289} // namespace aidl::android::hardware::biometrics::fingerprint