1 /*
2  * Copyright (c) 2023-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 #ifndef SECURITY_COMPONENT_ENHANCE_ADAPTER_H
16 #define SECURITY_COMPONENT_ENHANCE_ADAPTER_H
17 
18 #include <mutex>
19 #include "iremote_object.h"
20 #include "nlohmann/json.hpp"
21 #include "sec_comp_base.h"
22 #include "sec_comp_info.h"
23 
24 namespace OHOS {
25 namespace Security {
26 namespace SecurityComponent {
27 enum EnhanceInterfaceType {
28     SEC_COMP_ENHANCE_INPUT_INTERFACE = 0,
29     SEC_COMP_ENHANCE_SRV_INTERFACE = 1,
30     SEC_COMP_ENHANCE_CLIENT_INTERFACE = 2,
31 };
32 
33 // for multimodalinput to add enhance data to PointerEvent
34 class SecCompInputEnhanceInterface {
35 public:
36     // for multimodalinput to set enhance cfg which is from security component enhance service
37     virtual int32_t SetEnhanceCfg(uint8_t* cfg, uint32_t cfgLen) = 0;
38 
39     // for multimodalinput to get enhance data
40     virtual int32_t GetPointerEventEnhanceData(void* data, uint32_t dataLen,
41         uint8_t* enhanceData, uint32_t& enHancedataLen) = 0;
42 };
43 
44 // for security component service to send command to enhance service
45 class SecCompSrvEnhanceInterface {
46 public:
47     // enable input enhance, then enhance service send config to multimodalinput
48     virtual int32_t EnableInputEnhance() = 0;
49 
50     // disable input enhance
51     virtual int32_t DisableInputEnhance() = 0;
52 
53     // send click event to enhance service for checking extra data validity
54     virtual int32_t CheckExtraInfo(const SecCompClickEvent& clickInfo) = 0;
55 
56     // send component info to enhance service for checking its validity
57     virtual int32_t CheckComponentInfoEnhance(int32_t pid, std::shared_ptr<SecCompBase>& compInfo,
58         const nlohmann::json& jsonComponent) = 0;
59 
60     // get RemoteObject of enhance service to connect it
61     virtual sptr<IRemoteObject> GetEnhanceRemoteObject() = 0;
62 
63     // start enhance service
64     virtual void StartEnhanceService() = 0;
65 
66     // exit enhance service
67     virtual void ExitEnhanceService() = 0;
68 
69     // notify process died
70     virtual void NotifyProcessDied(int32_t pid) = 0;
71 
72     // notify process registered
73     virtual void AddSecurityComponentProcess(int32_t pid) = 0;
74 
75     virtual bool EnhanceSrvSerialize(MessageParcel& input, MessageParcel& output) = 0;
76     virtual bool EnhanceSrvDeserialize(MessageParcel& input, MessageParcel& output,
77         MessageParcel& reply) = 0;
78 };
79 
80 // for client
81 class SecCompClientEnhanceInterface {
82 public:
83     // preprocess component info which is send to security component service, e.g. RegisterSecurityComponent
84     virtual bool EnhanceDataPreprocess(const uintptr_t caller, std::string& componentInfo) = 0;
85     virtual bool EnhanceDataPreprocess(const uintptr_t caller, int32_t scId, std::string& componentInfo) = 0;
86 
87     virtual bool EnhanceClientSerialize(const uintptr_t caller,
88         MessageParcel& input, MessageParcel& output) = 0;
89     virtual bool EnhanceClientDeserialize(const uintptr_t caller, MessageParcel& input,
90         MessageParcel& output) = 0;
91 
92     // regiter scid to enhance client
93     virtual void RegisterScIdEnhance(const uintptr_t caller, int32_t scId) = 0;
94     // unregiter scid to enhance client
95     virtual void UnregisterScIdEnhance(const uintptr_t caller, int32_t scId) = 0;
96 };
97 
98 class SecCompEnhanceAdapter final {
99 public:
100     static void InitEnhanceHandler(EnhanceInterfaceType type);
101     static int32_t SetEnhanceCfg(uint8_t* cfg, uint32_t cfgLen);
102     static int32_t GetPointerEventEnhanceData(void* data, uint32_t dataLen,
103         uint8_t* enhanceData, uint32_t& enHancedataLen);
104 
105     static int32_t CheckExtraInfo(const SecCompClickEvent& clickInfo);
106     static int32_t EnableInputEnhance();
107     static int32_t DisableInputEnhance();
108     static int32_t CheckComponentInfoEnhance(int32_t pid, std::shared_ptr<SecCompBase>& compInfo,
109         const nlohmann::json& jsonComponent);
110     static sptr<IRemoteObject> GetEnhanceRemoteObject();
111     static void StartEnhanceService();
112     static void ExitEnhanceService();
113     static void NotifyProcessDied(int32_t pid);
114 
115     static bool EnhanceDataPreprocess(std::string& componentInfo);
116     static bool EnhanceDataPreprocess(int32_t scId, std::string& componentInfo);
117     static bool EnhanceClientSerialize(MessageParcel& input, MessageParcel& output);
118     static bool EnhanceClientDeserialize(MessageParcel& input, MessageParcel& output);
119     static void RegisterScIdEnhance(int32_t scId);
120     static void UnregisterScIdEnhance(int32_t scId);
121 
122     static void AddSecurityComponentProcess(int32_t pid);
123 
124     static bool EnhanceSrvSerialize(MessageParcel& input, MessageParcel& output);
125     static bool EnhanceSrvDeserialize(MessageParcel& input, MessageParcel& output,
126         MessageParcel& reply);
127     static __attribute__((visibility("default"))) SecCompInputEnhanceInterface* inputHandler;
128     static bool isEnhanceInputHandlerInit;
129 
130     static __attribute__((visibility("default"))) SecCompSrvEnhanceInterface* srvHandler;
131     static bool isEnhanceSrvHandlerInit;
132 
133     static __attribute__((visibility("default"))) SecCompClientEnhanceInterface* clientHandler;
134     static bool isEnhanceClientHandlerInit;
135 
136     static std::mutex initMtx;
137 };
138 typedef SecCompClientEnhanceInterface* (*EnhanceInterface) (void);
139 }  // namespace SecurityComponent
140 }  // namespace Security
141 }  // namespace OHOS
142 #endif  // SECURITY_COMPONENT_ENHANCE_ADAPTER_H
143