1 /* 2 * Copyright (C) 2022 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 #pragma once 18 #include <memory> 19 #include <mutex> 20 #include <random> 21 #include <thread> 22 #include <unordered_set> 23 #include <aidl/android/hardware/biometrics/fingerprint/BnSession.h> 24 #include <aidl/android/hardware/biometrics/fingerprint/ISessionCallback.h> 25 #include <android-base/unique_fd.h> 26 27 #include "storage.h" 28 29 namespace aidl::android::hardware::biometrics::fingerprint { 30 31 struct Session : public BnSession { 32 enum class State { 33 IDLE, 34 ENROLLING_START, 35 ENROLLING_END, 36 AUTHENTICATING, 37 DETECTING_INTERACTION, 38 }; 39 40 enum class ErrorCode { 41 OK, 42 E_HAT_MAC_EMPTY, 43 E_HAT_WRONG_CHALLENGE, 44 E_INCORRECT_STATE, 45 E_ENROLL_FAILED, 46 }; 47 48 Session(const int32_t sensorId, const int32_t userId, 49 std::shared_ptr<ISessionCallback> scb); 50 ~Session(); 51 52 ndk::ScopedAStatus generateChallenge() override; 53 ndk::ScopedAStatus revokeChallenge(const int64_t challenge) override; 54 ndk::ScopedAStatus enroll(const keymaster::HardwareAuthToken& hat, 55 std::shared_ptr<common::ICancellationSignal>* out) override; 56 ndk::ScopedAStatus authenticate(const int64_t operationId, 57 std::shared_ptr<common::ICancellationSignal>* out) override; 58 ndk::ScopedAStatus detectInteraction( 59 std::shared_ptr<common::ICancellationSignal>* out) override; 60 ndk::ScopedAStatus enumerateEnrollments() override; 61 ndk::ScopedAStatus removeEnrollments(const std::vector<int32_t>& enrollmentIds) override; 62 ndk::ScopedAStatus getAuthenticatorId() override; 63 ndk::ScopedAStatus invalidateAuthenticatorId() override; 64 ndk::ScopedAStatus resetLockout(const keymaster::HardwareAuthToken& hat) override; 65 ndk::ScopedAStatus close() override; 66 67 int64_t generateInt64(); 68 ErrorCode validateHat(const keymaster::HardwareAuthToken& hat) const; 69 bool sensorListenerFuncImpl(); sensorListenerFuncSession70 void sensorListenerFunc() { while (sensorListenerFuncImpl()) {} } 71 void onSensorEventOn(int fid); 72 void onSensorEventOff(); 73 void cancellEnroll(); 74 void cancellAuthenticate(); 75 void cancellDetectInteraction(); 76 onPointerDownSession77 ndk::ScopedAStatus onPointerDown(const int32_t /*pointerId*/, 78 const int32_t /*x*/, const int32_t /*y*/, 79 const float /*minor*/, 80 const float /*major*/) override { 81 return ndk::ScopedAStatus::ok(); 82 } onPointerUpSession83 ndk::ScopedAStatus onPointerUp(const int32_t /*pointerId*/) override { 84 return ndk::ScopedAStatus::ok(); 85 } onUiReadySession86 ndk::ScopedAStatus onUiReady() override { 87 return ndk::ScopedAStatus::ok(); 88 } authenticateWithContextSession89 ndk::ScopedAStatus authenticateWithContext( 90 int64_t operationId, const common::OperationContext& /*context*/, 91 std::shared_ptr<common::ICancellationSignal>* out) override { 92 return authenticate(operationId, out); 93 } enrollWithContextSession94 ndk::ScopedAStatus enrollWithContext( 95 const keymaster::HardwareAuthToken& hat, 96 const common::OperationContext& /*context*/, 97 std::shared_ptr<common::ICancellationSignal>* out) override { 98 return enroll(hat, out); 99 } detectInteractionWithContextSession100 ndk::ScopedAStatus detectInteractionWithContext( 101 const common::OperationContext& /*context*/, 102 std::shared_ptr<common::ICancellationSignal>* out) override { 103 return detectInteraction(out); 104 } onPointerDownWithContextSession105 ndk::ScopedAStatus onPointerDownWithContext(const PointerContext& /*context*/) override { 106 return ndk::ScopedAStatus::ok(); 107 } onPointerUpWithContextSession108 ndk::ScopedAStatus onPointerUpWithContext(const PointerContext& /*context*/) override { 109 return ndk::ScopedAStatus::ok(); 110 } onContextChangedSession111 ndk::ScopedAStatus onContextChanged(const common::OperationContext& /*context*/) override { 112 return ndk::ScopedAStatus::ok(); 113 } onPointerCancelWithContextSession114 ndk::ScopedAStatus onPointerCancelWithContext(const PointerContext& /*context*/) override { 115 return ndk::ScopedAStatus::ok(); 116 } setIgnoreDisplayTouchesSession117 ndk::ScopedAStatus setIgnoreDisplayTouches(bool /*shouldIgnore*/) override { 118 return ndk::ScopedAStatus::ok(); 119 } 120 121 const std::shared_ptr<ISessionCallback> mSessionCb; 122 Storage mStorage; // mMutex 123 std::mt19937_64 mRandom; // mMutex 124 int64_t mEnrollingSecUserId = 0; // mMutex 125 int64_t mAuthChallenge = 0; // mMutex 126 ::android::base::unique_fd mCallerFd; 127 ::android::base::unique_fd mSensorThreadFd; 128 std::thread mSensorListener; 129 std::unordered_set<int64_t> mChallenges; 130 State mState = State::IDLE; // mMutex 131 mutable std::mutex mMutex; 132 }; 133 134 } // namespace aidl::android::hardware::biometrics::fingerprint 135