1 /*
2 * Copyright (C) 2025 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 #include "accessibility_security_component_manager.h"
17 #include "hilog_wrapper.h"
18 #include "accessibility_element_info.h"
19 #ifdef ACCESSIBILITY_SECURITY_COMPONENT
20 #include "sec_comp_enhance_kit.h"
21 #include "sec_comp_enhance_adapter.h"
22 #endif // ACCESSIBILITY_SECURITY_COMPONENT
23
24 namespace OHOS {
25 namespace Accessibility {
26
27 constexpr uint32_t MAX_HMAC_SIZE = 160;
28
SetEnhanceConfig(const AccessibilitySecCompRawdata & rawData)29 int32_t AccessibilitySecurityComponentManager::SetEnhanceConfig(const AccessibilitySecCompRawdata& rawData)
30 {
31 HILOG_INFO();
32 #ifdef ACCESSIBILITY_SECURITY_COMPONENT
33 int32_t result = Security::SecurityComponent::SecCompEnhanceKit::SetEnhanceCfg(rawData.data, rawData.size);
34 HILOG_INFO("SetEnhanceCfg result: %{public}d", result);
35 return result;
36 #else
37 return RET_OK;
38 #endif // ACCESSIBILITY_SECURITY_COMPONENT
39 }
40
GenerateActionArgumentsWithHMAC(const ActionType & action,int64_t uniqueId,std::string bundleName,const std::map<std::string,std::string> & arguments)41 std::map<std::string, std::string> AccessibilitySecurityComponentManager::GenerateActionArgumentsWithHMAC(
42 const ActionType &action, int64_t uniqueId, std::string bundleName,
43 const std::map<std::string, std::string> &arguments)
44 {
45 HILOG_INFO("actionType: %{public}d", action);
46 #ifndef ACCESSIBILITY_SECURITY_COMPONENT
47 return arguments;
48 #else
49 std::map<std::string, std::string> actionArguments(arguments);
50 if (action != ACCESSIBILITY_ACTION_CLICK) {
51 return actionArguments;
52 }
53
54 std::unique_ptr<AccessibilitySecCompPoint> point = std::make_unique<AccessibilitySecCompPoint>();
55 if (point == nullptr) {
56 HILOG_ERROR("create point failed");
57 return actionArguments;
58 }
59
60 int64_t timeStamp = std::chrono::duration_cast<std::chrono::milliseconds>(
61 std::chrono::system_clock::now().time_since_epoch()).count();
62 std::string timeStr = std::to_string(timeStamp);
63
64 point->uniqueId = uniqueId;
65 errno_t ret = memcpy_s(point->bundleName, MAX_BUNDLE_NAME_LEN, bundleName.c_str(), bundleName.size());
66 if (ret != EOK) {
67 HILOG_ERROR("point bundleName memcpy_s failed.");
68 return actionArguments;
69 }
70 point->timeStamp = timeStamp;
71
72 uint32_t dataLen = sizeof(*point);
73 uint8_t outBuf[MAX_HMAC_SIZE + 1] = { 0 };
74 uint8_t *enHanceData = reinterpret_cast<uint8_t *>(&outBuf[0]);
75 uint32_t enHanceDataLen = MAX_HMAC_SIZE;
76 int32_t result = Security::SecurityComponent::SecCompEnhanceKit::GetPointerEventEnhanceData(
77 point.get(), dataLen, enHanceData, enHanceDataLen);
78 HILOG_INFO("result: %{public}d", result);
79 if (result != 0 || enHanceDataLen > MAX_HMAC_SIZE) {
80 HILOG_ERROR("GetPointerEventEnhanceData failed!");
81 return actionArguments;
82 }
83 std::vector<uint8_t> vecEnHanceData(enHanceData, enHanceData + enHanceDataLen);
84 std::string strEnHanceData(vecEnHanceData.begin(), vecEnHanceData.end());
85 actionArguments[ACTION_ARGU_CLICK_ENHANCE_DATA] = strEnHanceData;
86 actionArguments[ACTION_ARGU_CLICK_TIMESTAMP] = timeStr;
87 return actionArguments;
88 #endif // ACCESSIBILITY_SECURITY_COMPONENT
89 }
90 } // namespace Accessibility
91 } // namespace OHOS