1 /*
2 * Copyright (c) 2022 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 "sg_classify_client.h"
17
18 #ifndef SECURITY_GUARD_TRIM_MODEL_ANALYSIS
19 #include <future>
20 #include "iremote_broker.h"
21 #include "iservice_registry.h"
22 #include "securec.h"
23 #include "risk_analysis_manager_callback_service.h"
24 #include "risk_analysis_manager_proxy.h"
25 #endif
26
27 #include "security_guard_define.h"
28 #include "security_guard_log.h"
29
30 #ifndef SECURITY_GUARD_TRIM_MODEL_ANALYSIS
31 namespace {
32 constexpr int32_t TIMEOUT_REPLY = 500;
33 }
34
35 using namespace OHOS;
36 using namespace OHOS::Security::SecurityGuard;
37
38 static std::mutex g_mutex;
39
RequestSecurityModelResult(const std::string & devId,uint32_t modelId,const std::string & param,ResultCallback callback)40 static int32_t RequestSecurityModelResult(const std::string &devId, uint32_t modelId,
41 const std::string ¶m, ResultCallback callback)
42 {
43 auto registry = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
44 if (registry == nullptr) {
45 SGLOGE("GetSystemAbilityManager error");
46 return NULL_OBJECT;
47 }
48
49 auto object = registry->GetSystemAbility(RISK_ANALYSIS_MANAGER_SA_ID);
50 auto proxy = iface_cast<RiskAnalysisManagerProxy>(object);
51 if (proxy == nullptr) {
52 SGLOGE("proxy is null");
53 return NULL_OBJECT;
54 }
55
56 sptr<RiskAnalysisManagerCallbackService> stub = new (std::nothrow) RiskAnalysisManagerCallbackService(callback);
57 if (stub == nullptr) {
58 SGLOGE("stub is null");
59 return NULL_OBJECT;
60 }
61 int32_t ret = proxy->RequestSecurityModelResult(devId, modelId, param, stub);
62 SGLOGI("RequestSecurityModelResult result, ret=%{public}d", ret);
63 return ret;
64 }
65 #endif
66 namespace OHOS::Security::SecurityGuard {
RequestSecurityModelResultSync(const std::string & devId,uint32_t modelId,const std::string & param,SecurityModelResult & result)67 int32_t RequestSecurityModelResultSync(const std::string &devId, uint32_t modelId,
68 const std::string ¶m, SecurityModelResult &result)
69 {
70 #ifndef SECURITY_GUARD_TRIM_MODEL_ANALYSIS
71 if (devId.length() >= DEVICE_ID_MAX_LEN) {
72 return BAD_PARAM;
73 }
74 std::unique_lock<std::mutex> lock(g_mutex);
75 auto promise = std::make_shared<std::promise<SecurityModelResult>>();
76 auto future = promise->get_future();
77 auto func = [promise, param] (const std::string &devId, uint32_t modelId,
78 const std::string &result) mutable -> int32_t {
79 SecurityModelResult modelResult = {
80 .devId = devId,
81 .modelId = modelId,
82 .param = param,
83 .result = result
84 };
85 promise->set_value(modelResult);
86 return SUCCESS;
87 };
88
89 int32_t code = RequestSecurityModelResult(devId, modelId, param, func);
90 if (code != SUCCESS) {
91 SGLOGE("RequestSecurityModelResult error, code=%{public}d", code);
92 return code;
93 }
94 std::chrono::milliseconds span(TIMEOUT_REPLY);
95 if (future.wait_for(span) == std::future_status::timeout) {
96 SGLOGE("wait timeout");
97 return TIME_OUT;
98 }
99 result = future.get();
100 return SUCCESS;
101 #else
102 return 0;
103 #endif
104 }
105
RequestSecurityModelResultAsync(const std::string & devId,uint32_t modelId,const std::string & param,SecurityGuardRiskCallback callback)106 int32_t RequestSecurityModelResultAsync(const std::string &devId, uint32_t modelId,
107 const std::string ¶m, SecurityGuardRiskCallback callback)
108 {
109 #ifndef SECURITY_GUARD_TRIM_MODEL_ANALYSIS
110 if (devId.length() >= DEVICE_ID_MAX_LEN) {
111 return BAD_PARAM;
112 }
113 std::unique_lock<std::mutex> lock(g_mutex);
114 auto func = [callback, param] (const std::string &devId,
115 uint32_t modelId, const std::string &result) -> int32_t {
116 callback(SecurityModelResult{devId, modelId, param, result});
117 return SUCCESS;
118 };
119
120 return RequestSecurityModelResult(devId, modelId, param, func);
121 #else
122 return 0;
123 #endif
124 }
125 }
126
127 #ifdef __cplusplus
128 extern "C" {
129 #endif
130 #ifndef SECURITY_GUARD_TRIM_MODEL_ANALYSIS
FillingRequestResult(const OHOS::Security::SecurityGuard::SecurityModelResult & cppResult,::SecurityModelResult * result)131 static int32_t FillingRequestResult(const OHOS::Security::SecurityGuard::SecurityModelResult &cppResult,
132 ::SecurityModelResult *result)
133 {
134 if (cppResult.devId.length() >= DEVICE_ID_MAX_LEN || cppResult.result.length() >= RESULT_MAX_LEN) {
135 return BAD_PARAM;
136 }
137
138 result->modelId = cppResult.modelId;
139 errno_t rc = memcpy_s(result->devId.identity, DEVICE_ID_MAX_LEN, cppResult.devId.c_str(), cppResult.devId.length());
140 if (rc != EOK) {
141 return NULL_OBJECT;
142 }
143 result->devId.length = cppResult.devId.length();
144
145 rc = memcpy_s(result->result, RESULT_MAX_LEN, cppResult.result.c_str(), cppResult.result.length());
146 if (rc != EOK) {
147 return NULL_OBJECT;
148 }
149 result->resultLen = cppResult.result.length();
150
151 SGLOGD("modelId=%{public}u, result=%{public}s", cppResult.modelId, cppResult.result.c_str());
152 return SUCCESS;
153 }
154
CovertDevId(const DeviceIdentify * devId)155 static std::string CovertDevId(const DeviceIdentify *devId)
156 {
157 std::vector<char> id(DEVICE_ID_MAX_LEN, '\0');
158 std::copy(&devId->identity[0], &devId->identity[DEVICE_ID_MAX_LEN - 1], id.begin());
159 return std::string{id.data()};
160 }
161 #endif
RequestSecurityModelResultSync(const DeviceIdentify * devId,uint32_t modelId,::SecurityModelResult * result)162 int32_t RequestSecurityModelResultSync(const DeviceIdentify *devId, uint32_t modelId, ::SecurityModelResult *result)
163 {
164 #ifndef SECURITY_GUARD_TRIM_MODEL_ANALYSIS
165 if (devId == nullptr || result == nullptr || devId->length >= DEVICE_ID_MAX_LEN) {
166 return BAD_PARAM;
167 }
168 OHOS::Security::SecurityGuard::SecurityModelResult tmp;
169 int32_t ret = OHOS::Security::SecurityGuard::RequestSecurityModelResultSync(CovertDevId(devId), modelId, "", tmp);
170 FillingRequestResult(tmp, result);
171 return ret;
172 #else
173 return 0;
174 #endif
175 }
176
RequestSecurityModelResultAsync(const DeviceIdentify * devId,uint32_t modelId,::SecurityGuardRiskCallback callback)177 int32_t RequestSecurityModelResultAsync(const DeviceIdentify *devId, uint32_t modelId,
178 ::SecurityGuardRiskCallback callback)
179 {
180 #ifndef SECURITY_GUARD_TRIM_MODEL_ANALYSIS
181 if (devId == nullptr || devId->length >= DEVICE_ID_MAX_LEN) {
182 return BAD_PARAM;
183 }
184 auto cppCallBack = [callback](const OHOS::Security::SecurityGuard::SecurityModelResult &tmp) {
185 ::SecurityModelResult result{};
186 FillingRequestResult(tmp, &result);
187 callback(&result);
188 };
189 return OHOS::Security::SecurityGuard::RequestSecurityModelResultAsync(CovertDevId(devId), modelId, "", cppCallBack);
190 #else
191 return 0;
192 #endif
193 }
194
195 #ifdef __cplusplus
196 }
197 #endif
198