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 "trust_group_manager.h"
17
18 #include "device_profile_errors.h"
19 #include "device_profile_log.h"
20 #include "device_profile_storage_manager.h"
21 #include "device_profile_utils.h"
22 #include "dp_device_manager.h"
23 #include "sync_coordinator.h"
24
25 namespace OHOS {
26 namespace DeviceProfile {
27 namespace {
28 const std::string TAG = "TrustGroupManager";
29
30 constexpr int32_t VISIBILITY_PUBLIC = -1;
31 const std::string AUTH_APPID = "device_profile_auth";
32 }
33
34 IMPLEMENT_SINGLE_INSTANCE(TrustGroupManager);
35
from_json(const nlohmann::json & jsonObject,GroupInfo & groupInfo)36 void from_json(const nlohmann::json& jsonObject, GroupInfo& groupInfo)
37 {
38 if (jsonObject.find(FIELD_GROUP_NAME) != jsonObject.end()) {
39 jsonObject.at(FIELD_GROUP_NAME).get_to(groupInfo.groupName);
40 }
41 if (jsonObject.find(FIELD_GROUP_ID) != jsonObject.end()) {
42 jsonObject.at(FIELD_GROUP_ID).get_to(groupInfo.groupId);
43 }
44 if (jsonObject.find(FIELD_GROUP_OWNER) != jsonObject.end()) {
45 jsonObject.at(FIELD_GROUP_OWNER).get_to(groupInfo.groupOwner);
46 }
47 if (jsonObject.find(FIELD_GROUP_TYPE) != jsonObject.end()) {
48 jsonObject.at(FIELD_GROUP_TYPE).get_to(groupInfo.groupType);
49 }
50 if (jsonObject.find(FIELD_GROUP_VISIBILITY) != jsonObject.end()) {
51 jsonObject.at(FIELD_GROUP_VISIBILITY).get_to(groupInfo.groupVisibility);
52 }
53 }
54
InitHichainService()55 bool TrustGroupManager::InitHichainService()
56 {
57 if (hichainGmInstance_ != nullptr) {
58 return true;
59 }
60
61 if (InitDeviceAuthService() != ERR_OK) {
62 HILOGE("auth InitDeviceAuthService failed");
63 return false;
64 }
65
66 hichainGmInstance_ = GetGmInstance();
67 if (hichainGmInstance_ == nullptr) {
68 HILOGE("auth GetGmInstance failed");
69 return false;
70 }
71
72 InitDataChangeListener();
73 HILOGI("init succeeded");
74 return true;
75 }
76
InitDataChangeListener()77 void TrustGroupManager::InitDataChangeListener()
78 {
79 dataChangeListener_.onDeviceUnBound = OnDeviceUnBoundAdapter;
80 if (hichainGmInstance_->regDataChangeListener(AUTH_APPID.c_str(), &dataChangeListener_) != 0) {
81 HILOGE("auth RegDataChangeListener failed");
82 }
83 }
84
CheckTrustGroup(const std::string & deviceId)85 bool TrustGroupManager::CheckTrustGroup(const std::string& deviceId)
86 {
87 if (!InitHichainService()) {
88 HILOGE("auth GetGmInstance failed");
89 return false;
90 }
91
92 uint32_t groupNum = 0;
93 char* returnGroups = nullptr;
94 int32_t ret = hichainGmInstance_->getRelatedGroups(ANY_OS_ACCOUNT, AUTH_APPID.c_str(), deviceId.c_str(),
95 &returnGroups, &groupNum);
96 if (ret != ERR_OK) {
97 HILOGE("failed, ret:%{public}d", ret);
98 return false;
99 }
100 return CheckGroupsInfo(returnGroups, groupNum);
101 }
102
CheckGroupsInfo(const char * returnGroups,uint32_t groupNum)103 bool TrustGroupManager::CheckGroupsInfo(const char* returnGroups, uint32_t groupNum)
104 {
105 if (returnGroups == nullptr || groupNum == 0) {
106 HILOGE("failed, returnGroups is nullptr");
107 return false;
108 }
109
110 nlohmann::json jsonObject = nlohmann::json::parse(returnGroups, nullptr, false);
111 if (jsonObject.is_discarded()) {
112 HILOGE("returnGroups parse error");
113 return false;
114 }
115
116 std::vector<GroupInfo> groupInfos = jsonObject.get<std::vector<GroupInfo>>();
117 for (const auto& groupInfo : groupInfos) {
118 // check group visibility is whether public or not
119 if (groupInfo.groupVisibility != VISIBILITY_PUBLIC) {
120 continue;
121 }
122
123 // check group type is whether (same count or point to point) or not
124 if (groupInfo.groupType == GroupType::IDENTICAL_ACCOUNT_GROUP
125 || groupInfo.groupType == GroupType::PEER_TO_PEER_GROUP) {
126 HILOGI("check success type = %{public}d", groupInfo.groupType);
127 return true;
128 }
129 }
130 HILOGE("check failed, not in trust group");
131 return false;
132 }
133
OnDeviceUnBoundAdapter(const char * peerUdid,const char * groupInfo)134 void TrustGroupManager::OnDeviceUnBoundAdapter(const char* peerUdid, const char* groupInfo)
135 {
136 const std::string udid = peerUdid;
137 if (!CheckDeviceId(udid)) {
138 return;
139 }
140
141 auto removeUnBoundDeviceTask = [udid = std::move(udid)]() {
142 HILOGI("remove unbound deivce profile start, udid = %{public}s",
143 DeviceProfileUtils::AnonymizeDeviceId(udid).c_str());
144 if (GetInstance().CheckTrustGroup(udid)) {
145 HILOGI("unbound device in trust group");
146 return;
147 }
148
149 if (DeviceProfileStorageManager::GetInstance().RemoveUnBoundDeviceProfile(udid) != ERR_OK) {
150 HILOGE("remove unbound device profile failed, udid = %{public}s",
151 DeviceProfileUtils::AnonymizeDeviceId(udid).c_str());
152 } else {
153 HILOGI("remove unbound deivce profile success, udid = %{public}s",
154 DeviceProfileUtils::AnonymizeDeviceId(udid).c_str());
155 }
156 DpDeviceManager::GetInstance().RemoveDeviceIdsByUdid(udid);
157 };
158 if (!SyncCoordinator::GetInstance().DispatchSyncTask(removeUnBoundDeviceTask)) {
159 HILOGE("post task failed");
160 return;
161 }
162 }
163
CheckDeviceId(const std::string udid)164 bool TrustGroupManager::CheckDeviceId(const std::string udid)
165 {
166 std::string localDeviceId;
167 DpDeviceManager::GetInstance().GetLocalDeviceUdid(localDeviceId);
168 if (udid.empty() || localDeviceId.empty()) {
169 HILOGE("device id is empty");
170 return false;
171 }
172
173 if (udid == localDeviceId) {
174 return false;
175 }
176 return true;
177 }
178 } // namespace DeviceProfile
179 } // namespace OHOS