diff --git a/common/include/device_profile_errors.h b/common/include/device_profile_errors.h index 5dff3e9..00e99dc 100755 --- a/common/include/device_profile_errors.h +++ b/common/include/device_profile_errors.h @@ -40,6 +40,7 @@ enum { ERR_DP_POST_TASK_FAILED = 98566153, ERR_DP_DEVICE_SYNC_BUSY = 98566154, ERR_DP_PERMISSION_DENIED = 98566155, + ERR_DP_UNTRUSTED_GROUP = 98566156, }; } // namespace DeviceProfile } // namespace OHOS diff --git a/services/core/BUILD.gn b/services/core/BUILD.gn index f6f6844..51ad152 100644 --- a/services/core/BUILD.gn +++ b/services/core/BUILD.gn @@ -38,6 +38,7 @@ ohos_shared_library("distributed_device_profile") { install_enable = true sources = [ "src/authority/authority_manager.cpp", + "src/authority/trust_group_manager.cpp", "src/contentsensor/content_collector.cpp", "src/contentsensor/content_sensor_manager.cpp", "src/contentsensor/device_info_collector.cpp", @@ -69,6 +70,7 @@ ohos_shared_library("distributed_device_profile") { configs = [ ":device_profile_core_config" ] external_deps = [ + "deviceauth_standard:deviceauth_sdk", "distributeddatamgr:distributeddata_inner", "dsoftbus_standard:softbus_client", "eventhandler:libeventhandler", diff --git a/services/core/include/authority/trust_group_manager.h b/services/core/include/authority/trust_group_manager.h new file mode 100644 index 0000000..e2da097 --- /dev/null +++ b/services/core/include/authority/trust_group_manager.h @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OHOS_DISTRIBUTED_DEVICE_PROFILE_TRUST_GROUP_MANAGER_H +#define OHOS_DISTRIBUTED_DEVICE_PROFILE_TRUST_GROUP_MANAGER_H + +#include + +#include "device_auth.h" +#include "device_auth_defines.h" +#include "nlohmann/json.hpp" +#include "single_instance.h" + +namespace OHOS { +namespace DeviceProfile { +struct GroupInfo { + std::string groupName; + std::string groupId; + std::string groupOwner; + int32_t groupType; + int32_t groupVisibility; + + GroupInfo() : groupName(""), groupId(""), groupOwner(""), groupType(0), groupVisibility(0) {} +}; + +void from_json(const nlohmann::json& jsonObject, GroupInfo& groupInfo); + +class TrustGroupManager { + DECLARE_SINGLE_INSTANCE(TrustGroupManager); + +public: + void Init(); + bool CheckTrustGroup(const std::string& deviceId); + +private: + const DeviceGroupManager* hichainGmInstance_ = nullptr; +}; +} // namespace DeviceProfile +} // namespace OHOS +#endif // OHOS_DISTRIBUTED_DEVICE_PROFILE_TRUST_GROUP_MANAGER_H diff --git a/services/core/include/dbstorage/device_profile_storage.h b/services/core/include/dbstorage/device_profile_storage.h index 69d19f5..64e6ad6 100755 --- a/services/core/include/dbstorage/device_profile_storage.h +++ b/services/core/include/dbstorage/device_profile_storage.h @@ -64,6 +64,7 @@ private: DistributedKv::Status GetKvStore(); void DeleteKvStore(); void SubscribeDeviceProfileKvStore(); + bool CheckTrustGroup(const std::vector& deviceIdList); private: DistributedKv::Options options_; diff --git a/services/core/src/authority/trust_group_manager.cpp b/services/core/src/authority/trust_group_manager.cpp new file mode 100644 index 0000000..b30dc54 --- /dev/null +++ b/services/core/src/authority/trust_group_manager.cpp @@ -0,0 +1,109 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "trust_group_manager.h" + +#include "device_profile_errors.h" +#include "device_profile_log.h" + +namespace OHOS { +namespace DeviceProfile { +namespace { +const std::string TAG = "TrustGroupManager"; + +constexpr int32_t VISIBILITY_PUBLIC = -1; +const std::string AUTH_APPID = "device_profile_auth"; +} + +IMPLEMENT_SINGLE_INSTANCE(TrustGroupManager); + +void from_json(const nlohmann::json& jsonObject, GroupInfo& groupInfo) +{ + jsonObject.at(FIELD_GROUP_NAME).get_to(groupInfo.groupName); + jsonObject.at(FIELD_GROUP_ID).get_to(groupInfo.groupId); + jsonObject.at(FIELD_GROUP_OWNER).get_to(groupInfo.groupOwner); + jsonObject.at(FIELD_GROUP_TYPE).get_to(groupInfo.groupType); + jsonObject.at(FIELD_GROUP_VISIBILITY).get_to(groupInfo.groupVisibility); +} + +void TrustGroupManager::Init() +{ + if (InitDeviceAuthService() != ERR_OK) { + HILOGE("auth InitDeviceAuthService failed"); + return; + } + + hichainGmInstance_ = GetGmInstance(); + if (hichainGmInstance_ == nullptr) { + HILOGE("auth GetGmInstance failed"); + return; + } + HILOGI("init succeeded"); +} + +bool TrustGroupManager::CheckTrustGroup(const std::string& deviceId) +{ + if (hichainGmInstance_ == nullptr) { + Init(); + } + + if (hichainGmInstance_ == nullptr) { + HILOGE("auth GetGmInstance failed"); + return false; + } + + uint32_t groupNum = 0; + char* returnGroups = nullptr; + int32_t ret = hichainGmInstance_->getRelatedGroups(AUTH_APPID.c_str(), deviceId.c_str(), + &returnGroups, &groupNum); + if (ret != ERR_OK) { + HILOGE("faild , ret: %d", ret); + return false; + } + + if (returnGroups == nullptr || groupNum == 0) { + HILOGE("faild, returnGroups is nullptr"); + return false; + } + + nlohmann::json jsonObject = nlohmann::json::parse(returnGroups, nullptr, false); + if (jsonObject.is_discarded()) { + HILOGE("returnGroups parse error"); + return false; + } + + std::vector groupInfos = jsonObject.get>(); + if (groupInfos.empty()) { + HILOGE("failed, groupInfos is empty"); + return false; + } + for (const auto& groupInfo : groupInfos) { + // check group visibility is whether public or not + if (groupInfo.groupVisibility != VISIBILITY_PUBLIC) { + continue; + } + + // check group type is whether (same count or point to point) or not + if (groupInfo.groupType == GroupType::IDENTICAL_ACCOUNT_GROUP + || groupInfo.groupType == GroupType::PEER_TO_PEER_GROUP) { + HILOGI("check success type = %{public}d", groupInfo.groupType); + return true; + } + } + HILOGE("check failed, not in trust group"); + return false; +} +} // namespace DeviceProfile +} // namespace OHOS \ No newline at end of file diff --git a/services/core/src/dbstorage/device_profile_storage.cpp b/services/core/src/dbstorage/device_profile_storage.cpp index 55718b6..4b5bc79 100755 --- a/services/core/src/dbstorage/device_profile_storage.cpp +++ b/services/core/src/dbstorage/device_profile_storage.cpp @@ -18,10 +18,13 @@ #include #include +#include "device_manager.h" #include "device_profile_errors.h" #include "device_profile_log.h" #include "device_profile_storage_manager.h" +#include "device_profile_utils.h" #include "service_characteristic_profile.h" +#include "trust_group_manager.h" #include "datetime_ex.h" #include "profile_change_handler.h" @@ -33,7 +36,6 @@ using namespace std::chrono_literals; namespace { const std::string TAG = "DeviceProfileStorage"; - constexpr int32_t RETRY_TIMES_GET_KVSTORE = 10; } @@ -249,15 +251,41 @@ int32_t DeviceProfileStorage::SyncDeviceProfile(const std::vector& SyncMode syncMode) { HILOGI("called"); + if (!CheckTrustGroup(deviceIdList)) { + return ERR_DP_UNTRUSTED_GROUP; + } + std::unique_lock writeLock(storageLock_); if (kvStorePtr_ == nullptr) { return ERR_DP_INVALID_PARAMS; } + Status status = kvStorePtr_->Sync(deviceIdList, static_cast(syncMode)); if (status != Status::SUCCESS) { HILOGE("sync failed, error = %{public}d", status); } return static_cast(status); } + +bool DeviceProfileStorage::CheckTrustGroup(const std::vector& deviceIdList) +{ + if (deviceIdList.empty()) { + HILOGE("device list is empty"); + return false; + } + for (const auto& deviceId : deviceIdList) { + std::string udid; + if (!DeviceManager::GetInstance().TransformDeviceId(deviceId, udid, DeviceIdType::UDID)) { + HILOGE("%{public}s transform to udid failed", DeviceProfileUtils::AnonymizeDeviceId(deviceId).c_str()); + return false; + } + + if (!TrustGroupManager::GetInstance().CheckTrustGroup(udid)) { + HILOGE("%{public}s not in trust group", DeviceProfileUtils::AnonymizeDeviceId(deviceId).c_str()); + return false; + } + } + return true; +} } // namespace DeviceProfile } // namespace OHOS diff --git a/services/core/src/distributed_device_profile_service.cpp b/services/core/src/distributed_device_profile_service.cpp index 2010f2f..8ed50a0 100644 --- a/services/core/src/distributed_device_profile_service.cpp +++ b/services/core/src/distributed_device_profile_service.cpp @@ -23,8 +23,8 @@ #include "device_profile_storage_manager.h" #include "service_characteristic_profile.h" #include "subscribe_manager.h" - #include "system_ability_definition.h" +#include "trust_group_manager.h" namespace OHOS { namespace DeviceProfile { @@ -50,10 +50,6 @@ bool DistributedDeviceProfileService::Init() HILOGE("DeviceProfileStorageManager init failed"); return false; } - if (!ContentSensorManager::GetInstance().Init()) { - HILOGE("ContentSensorManager init failed"); - return false; - } if (!SubscribeManager::GetInstance().Init()) { HILOGE("SubscribeManager init failed"); return false; @@ -62,6 +58,8 @@ bool DistributedDeviceProfileService::Init() HILOGE("AuthorityManager init failed"); return false; } + TrustGroupManager::GetInstance().Init(); + ContentSensorManager::GetInstance().Init(); HILOGI("init succeeded"); return true; } diff --git a/services/core/src/subscribemanager/subscribe_manager.cpp b/services/core/src/subscribemanager/subscribe_manager.cpp index 26c338b..8057cf0 100755 --- a/services/core/src/subscribemanager/subscribe_manager.cpp +++ b/services/core/src/subscribemanager/subscribe_manager.cpp @@ -40,6 +40,10 @@ bool SubscribeManager::Init() { subscriberDeathRecipient_ = sptr( new (std::nothrow) SubscriberDeathRecipient); + if (subscriberDeathRecipient_ == nullptr) { + HILOGE("null death recipient"); + return false; + } HILOGI("init succeeded"); return true; }