1 /*
2  * Copyright (c) 2022-2024 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef OHOS_HDI_PIN_AUTH_ALL_IN_ONE_IMPL_H
17 #define OHOS_HDI_PIN_AUTH_ALL_IN_ONE_IMPL_H
18 
19 #include <list>
20 #include <mutex>
21 #include <vector>
22 
23 #include "nocopyable.h"
24 #include "thread_pool.h"
25 
26 #include "pin_auth_hdi.h"
27 #include "pin_auth.h"
28 
29 namespace OHOS {
30 namespace HDI {
31 namespace PinAuth {
32 class AllInOneImpl : public HdiIAllInOneExecutor, public NoCopyable {
33 public:
34     explicit AllInOneImpl(std::shared_ptr<OHOS::UserIam::PinAuth::PinAuth> pinHdi);
35     ~AllInOneImpl() override;
36 
37     int32_t GetExecutorInfo(HdiExecutorInfo &info) override;
38     int32_t OnRegisterFinish(const std::vector<uint64_t> &templateIdList,
39         const std::vector<uint8_t> &frameworkPublicKey, const std::vector<uint8_t> &extraInfo) override;
40     int32_t Cancel(uint64_t scheduleId) override;
41     int32_t SendMessage(uint64_t scheduleId, int32_t srcRole, const std::vector<uint8_t>& msg) override;
42     int32_t SetData(uint64_t scheduleId, uint64_t authSubType, const std::vector<uint8_t> &data,
43         int32_t resultCode) override;
44     int32_t Enroll(uint64_t scheduleId, const std::vector<uint8_t> &extraInfo,
45         const sptr<HdiIExecutorCallback> &callbackObj) override;
46     int32_t Authenticate(uint64_t scheduleId, const std::vector<uint64_t>& templateIdList,
47         const std::vector<uint8_t> &extraInfo, const sptr<HdiIExecutorCallback> &callbackObj) override;
48     int32_t Delete(uint64_t templateId) override;
49     int32_t GetProperty(const std::vector<uint64_t> &templateIdList, const std::vector<int32_t> &propertyTypes,
50         HdiProperty &property) override;
51     int32_t SendCommand(int32_t commandId, const std::vector<uint8_t> &extraInfo,
52         const sptr<HdiIExecutorCallback> &callbackObj) override;
53 
54 private:
55     struct ScheduleInfo {
56         uint64_t scheduleId{0};
57         uint32_t commandId{0};
58         sptr<HdiIExecutorCallback> callback;
59         uint64_t templateId{0};
60         std::vector<uint8_t> algoParameter;
61     };
62 
63     class ScheduleList {
64     public:
65         bool AddScheduleInfo(const ScheduleInfo &scheduleInfo);
66         bool GetAndDelScheduleInfo(uint64_t scheduleId, ScheduleInfo &scheduleInfo);
67         void DelScheduleInfo(uint64_t scheduleId);
68     private:
69         std::mutex mutex_;
70         std::list<struct ScheduleInfo> scheduleInfoList_;
71     };
72 
73 private:
74     int32_t AuthPin(uint64_t scheduleId, uint64_t templateId, const std::vector<uint8_t> &data,
75         std::vector<uint8_t> &resultTlv);
76     int32_t AuthenticateInner(uint64_t scheduleId, uint64_t templateId, std::vector<uint8_t> &algoParameter,
77         const sptr<HdiIExecutorCallback> &callbackObj);
78     int32_t EnrollInner(uint64_t scheduleId, const std::vector<uint8_t> &extraInfo,
79         const sptr<HdiIExecutorCallback> &callbackObj, std::vector<uint8_t> &algoParameter, uint32_t &algoVersion);
80 
81     std::shared_ptr<OHOS::UserIam::PinAuth::PinAuth> pinHdi_;
82     ScheduleList scheduleList_;
83     OHOS::ThreadPool threadPool_;
84 };
85 } // PinAuth
86 } // HDI
87 } // OHOS
88 
89 #endif // OHOS_HDI_PIN_AUTH_ALL_IN_ONE_IMPL_H
90