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 "iservice_registry.h"
17 #include "securec.h"
18
19 #include "data_collect_manager_idl_proxy.h"
20 #include "security_guard_define.h"
21 #include "security_guard_log.h"
22 #include "security_guard_utils.h"
23 #include "sg_collect_client.h"
24 #include "data_collect_manager.h"
25
26 namespace OHOS::Security::SecurityGuard {
ReportSecurityInfo(const std::shared_ptr<EventInfo> & info)27 int32_t NativeDataCollectKit::ReportSecurityInfo(const std::shared_ptr<EventInfo> &info)
28 {
29 return DataCollectManager::GetInstance().ReportSecurityEvent(info, true);
30 }
31
ReportSecurityInfoAsync(const std::shared_ptr<EventInfo> & info)32 int32_t NativeDataCollectKit::ReportSecurityInfoAsync(const std::shared_ptr<EventInfo> &info)
33 {
34 return DataCollectManager::GetInstance().ReportSecurityEvent(info, false);
35 }
36
SecurityGuardConfigUpdate(int32_t fd,const std::string & name)37 int32_t NativeDataCollectKit::SecurityGuardConfigUpdate(int32_t fd, const std::string &name)
38 {
39 return DataCollectManager::GetInstance().SecurityGuardConfigUpdate(fd, name);
40 }
41
42 } // namespace OHOS::Security::SecurityGuard
43
ReportSecurityInfoImpl(const struct EventInfoSt * info,bool isSync)44 static int32_t ReportSecurityInfoImpl(const struct EventInfoSt *info, bool isSync)
45 {
46 if (info == nullptr || info->contentLen >= CONTENT_MAX_LEN || info->version == nullptr) {
47 return OHOS::Security::SecurityGuard::BAD_PARAM;
48 }
49 int64_t eventId = info->eventId;
50 std::string version = reinterpret_cast<const char *>(info->version);
51 uint8_t tmp[CONTENT_MAX_LEN] = {};
52 (void)memset_s(tmp, CONTENT_MAX_LEN, 0, CONTENT_MAX_LEN);
53 errno_t rc = memcpy_s(tmp, CONTENT_MAX_LEN, info->content, info->contentLen);
54 if (rc != EOK) {
55 return OHOS::Security::SecurityGuard::NULL_OBJECT;
56 }
57 std::string content(reinterpret_cast<const char *>(tmp));
58 auto eventInfo = std::make_shared<OHOS::Security::SecurityGuard::EventInfo>(eventId, version, content);
59 if (isSync) {
60 return OHOS::Security::SecurityGuard::NativeDataCollectKit::ReportSecurityInfo(eventInfo);
61 } else {
62 return OHOS::Security::SecurityGuard::NativeDataCollectKit::ReportSecurityInfoAsync(eventInfo);
63 }
64 }
65
66 #ifdef __cplusplus
67 extern "C" {
68 #endif
69
ReportSecurityInfo(const struct EventInfoSt * info)70 int32_t ReportSecurityInfo(const struct EventInfoSt *info)
71 {
72 return ReportSecurityInfoImpl(info, true);
73 }
74
ReportSecurityInfoAsync(const struct EventInfoSt * info)75 int32_t ReportSecurityInfoAsync(const struct EventInfoSt *info)
76 {
77 return ReportSecurityInfoImpl(info, false);
78 }
79
SecurityGuardConfigUpdate(int32_t fd,const char * fileName)80 int32_t SecurityGuardConfigUpdate(int32_t fd, const char *fileName)
81 {
82 return OHOS::Security::SecurityGuard::NativeDataCollectKit::SecurityGuardConfigUpdate(fd, fileName);
83 }
84 #ifdef __cplusplus
85 }
86 #endif