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