1 /*
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 
17 #include <aidl/android/hardware/biometrics/common/BnCancellationSignal.h>
18 #include <android-base/logging.h>
19 
20 #include "Session.h"
21 
22 namespace aidl::android::hardware::biometrics::face {
23 
24 class CancellationSignal : public common::BnCancellationSignal {
25   private:
26     std::shared_ptr<ISessionCallback> cb_;
27 
28   public:
CancellationSignal(std::shared_ptr<ISessionCallback> cb)29     explicit CancellationSignal(std::shared_ptr<ISessionCallback> cb) : cb_(std::move(cb)) {}
30 
cancel()31     ndk::ScopedAStatus cancel() override {
32         cb_->onError(Error::CANCELED, 0 /* vendorCode */);
33         return ndk::ScopedAStatus::ok();
34     }
35 };
36 
Session(std::shared_ptr<ISessionCallback> cb)37 Session::Session(std::shared_ptr<ISessionCallback> cb)
38     : cb_(std::move(cb)), mRandom(std::mt19937::default_seed) {}
39 
generateChallenge()40 ndk::ScopedAStatus Session::generateChallenge() {
41     LOG(INFO) << "generateChallenge";
42     if (cb_) {
43         std::uniform_int_distribution<int64_t> dist;
44         auto challenge = dist(mRandom);
45         cb_->onChallengeGenerated(challenge);
46     }
47     return ndk::ScopedAStatus::ok();
48 }
49 
revokeChallenge(int64_t challenge)50 ndk::ScopedAStatus Session::revokeChallenge(int64_t challenge) {
51     LOG(INFO) << "revokeChallenge";
52     if (cb_) {
53         cb_->onChallengeRevoked(challenge);
54     }
55     return ndk::ScopedAStatus::ok();
56 }
57 
getEnrollmentConfig(EnrollmentType,std::vector<EnrollmentStageConfig> * return_val)58 ndk::ScopedAStatus Session::getEnrollmentConfig(EnrollmentType /*enrollmentType*/,
59                                                 std::vector<EnrollmentStageConfig>* return_val) {
60     *return_val = {};
61     return ndk::ScopedAStatus::ok();
62 }
63 
enroll(const keymaster::HardwareAuthToken &,EnrollmentType,const std::vector<Feature> &,const std::optional<NativeHandle> &,std::shared_ptr<biometrics::common::ICancellationSignal> *)64 ndk::ScopedAStatus Session::enroll(
65         const keymaster::HardwareAuthToken& /*hat*/, EnrollmentType /*enrollmentType*/,
66         const std::vector<Feature>& /*features*/,
67         const std::optional<NativeHandle>& /*previewSurface*/,
68         std::shared_ptr<biometrics::common::ICancellationSignal>* /*return_val*/) {
69     LOG(INFO) << "enroll";
70     if (cb_) {
71         cb_->onError(Error::UNABLE_TO_PROCESS, 0 /* vendorError */);
72     }
73     return ndk::ScopedAStatus::ok();
74 }
75 
authenticate(int64_t,std::shared_ptr<common::ICancellationSignal> * return_val)76 ndk::ScopedAStatus Session::authenticate(int64_t /*keystoreOperationId*/,
77                                          std::shared_ptr<common::ICancellationSignal>* return_val) {
78     LOG(INFO) << "authenticate";
79     if (cb_) {
80         cb_->onError(Error::UNABLE_TO_PROCESS, 0 /* vendorCode */);
81     }
82     *return_val = SharedRefBase::make<CancellationSignal>(cb_);
83     return ndk::ScopedAStatus::ok();
84 }
85 
detectInteraction(std::shared_ptr<common::ICancellationSignal> *)86 ndk::ScopedAStatus Session::detectInteraction(
87         std::shared_ptr<common::ICancellationSignal>* /*return_val*/) {
88     LOG(INFO) << "detectInteraction";
89     return ndk::ScopedAStatus::ok();
90 }
91 
enumerateEnrollments()92 ndk::ScopedAStatus Session::enumerateEnrollments() {
93     LOG(INFO) << "enumerateEnrollments";
94     if (cb_) {
95         cb_->onEnrollmentsEnumerated(std::vector<int32_t>());
96     }
97     return ndk::ScopedAStatus::ok();
98 }
99 
removeEnrollments(const std::vector<int32_t> &)100 ndk::ScopedAStatus Session::removeEnrollments(const std::vector<int32_t>& /*enrollmentIds*/) {
101     LOG(INFO) << "removeEnrollments";
102     if (cb_) {
103         cb_->onEnrollmentsRemoved(std::vector<int32_t>());
104     }
105     return ndk::ScopedAStatus::ok();
106 }
107 
getFeatures()108 ndk::ScopedAStatus Session::getFeatures() {
109     LOG(INFO) << "getFeatures";
110     if (cb_) {
111         // Must error out with UNABLE_TO_PROCESS when no faces are enrolled.
112         cb_->onError(Error::UNABLE_TO_PROCESS, 0 /* vendorCode */);
113     }
114     return ndk::ScopedAStatus::ok();
115 }
116 
setFeature(const keymaster::HardwareAuthToken &,Feature,bool)117 ndk::ScopedAStatus Session::setFeature(const keymaster::HardwareAuthToken& /*hat*/,
118                                        Feature /*feature*/, bool /*enabled*/) {
119     LOG(INFO) << "setFeature";
120     return ndk::ScopedAStatus::ok();
121 }
122 
getAuthenticatorId()123 ndk::ScopedAStatus Session::getAuthenticatorId() {
124     LOG(INFO) << "getAuthenticatorId";
125     if (cb_) {
126         cb_->onAuthenticatorIdRetrieved(0 /* authenticatorId */);
127     }
128     return ndk::ScopedAStatus::ok();
129 }
130 
invalidateAuthenticatorId()131 ndk::ScopedAStatus Session::invalidateAuthenticatorId() {
132     LOG(INFO) << "invalidateAuthenticatorId";
133     if (cb_) {
134         cb_->onAuthenticatorIdInvalidated(0);
135     }
136     return ndk::ScopedAStatus::ok();
137 }
138 
resetLockout(const keymaster::HardwareAuthToken &)139 ndk::ScopedAStatus Session::resetLockout(const keymaster::HardwareAuthToken& /*hat*/) {
140     LOG(INFO) << "resetLockout";
141     if (cb_) {
142         cb_->onLockoutCleared();
143     }
144     return ndk::ScopedAStatus::ok();
145 }
146 
close()147 ndk::ScopedAStatus Session::close() {
148     if (cb_) {
149         cb_->onSessionClosed();
150     }
151     return ndk::ScopedAStatus::ok();
152 }
153 
154 }  // namespace aidl::android::hardware::biometrics::face
155