1 /*
2  * Copyright (c) 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 EL5_FILEKEY_MANAGER_SERVICE_H
17 #define EL5_FILEKEY_MANAGER_SERVICE_H
18 
19 #include <singleton.h>
20 #include "nocopyable.h"
21 
22 #include "accesstoken_kit.h"
23 #ifdef COMMON_EVENT_SERVICE_ENABLE
24 #include "el5_filkey_manager_subscriber.h"
25 #endif
26 #include "el5_filekey_manager_stub.h"
27 #include "el5_filekey_service_ext_interface.h"
28 #ifdef EVENTHANDLER_ENABLE
29 #include "event_handler.h"
30 #endif
31 
32 namespace OHOS {
33 namespace Security {
34 namespace AccessToken {
35 enum class ServiceRunningState { STATE_NOT_START, STATE_RUNNING };
36 
37 class El5FilekeyManagerService : public El5FilekeyManagerStub {
38 public:
39     El5FilekeyManagerService();
40     virtual ~El5FilekeyManagerService();
41 
42     int32_t Init();
43 
44     int32_t AcquireAccess(DataLockType type) override;
45     int32_t ReleaseAccess(DataLockType type) override;
46     int32_t GenerateAppKey(uint32_t uid, const std::string& bundleName, std::string& keyId) override;
47     int32_t DeleteAppKey(const std::string& bundleName, int32_t userId) override;
48     int32_t GetUserAppKey(int32_t userId, bool getAllFlag,
49         std::vector<std::pair<int32_t, std::string>> &keyInfos) override;
50     int32_t ChangeUserAppkeysLoadInfo(int32_t userId, std::vector<std::pair<std::string, bool>> &loadInfos) override;
51     int32_t SetFilePathPolicy() override;
52     int32_t RegisterCallback(const sptr<El5FilekeyCallbackInterface> &callback) override;
53 
54     int32_t SetPolicyScreenLocked();
55     int32_t HandleUserCommonEvent(const std::string &eventName, int32_t userId);
56     void PostDelayedUnloadTask(uint32_t delayedTime);
57     void CancelDelayedUnloadTask();
58     int Dump(int fd, const std::vector<std::u16string>& args) override;
59 
60 private:
61     ServiceRunningState serviceRunningState_ = ServiceRunningState::STATE_NOT_START;
62 
63     bool IsSystemApp();
64     int32_t CheckReqLockPermission(DataLockType type, bool& isApp);
65     bool VerifyNativeCallingProcess(const std::string &validCaller, const AccessTokenID &callerTokenId);
66     bool VerifyHapCallingProcess(int32_t userId, const std::string &validCaller, const AccessTokenID &callerTokenId);
67 
68     El5FilekeyServiceExtInterface* service_ = nullptr;
69 #ifdef COMMON_EVENT_SERVICE_ENABLE
70     std::shared_ptr<El5FilekeyManagerSubscriber> subscriber_;
71 #endif
72 #ifdef EVENTHANDLER_ENABLE
73     std::shared_ptr<AppExecFwk::EventHandler> unloadHandler_;
74 #endif
75     void *handler_{nullptr};
76 
77     DISALLOW_COPY_AND_MOVE(El5FilekeyManagerService);
78 };
79 }  // namespace AccessToken
80 }  // namespace Security
81 }  // namespace OHOS
82 #endif  // EL5_FILEKEY_MANAGER_SERVICE_H
83