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 "device_info_manager.h"
17 #include "constant_common.h"
18 
19 namespace OHOS {
20 namespace Security {
21 namespace AccessToken {
22 namespace {
23 static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, SECURITY_DOMAIN_ACCESSTOKEN, "DeviceInfoManager"};
24 std::recursive_mutex g_instanceMutex;
25 }
GetInstance()26 DeviceInfoManager &DeviceInfoManager::GetInstance()
27 {
28     static DeviceInfoManager* instance = nullptr;
29     if (instance == nullptr) {
30         std::lock_guard<std::recursive_mutex> lock(g_instanceMutex);
31         if (instance == nullptr) {
32             instance = new DeviceInfoManager();
33         }
34     }
35     return *instance;
36 }
37 
GetDeviceInfo(const std::string & nodeId,DeviceIdType deviceIdType,DeviceInfo & deviceInfo) const38 bool DeviceInfoManager::GetDeviceInfo(
39     const std::string &nodeId, DeviceIdType deviceIdType, DeviceInfo &deviceInfo) const
40 {
41     return DeviceInfoRepository::GetInstance().FindDeviceInfo(nodeId, deviceIdType, deviceInfo);
42 }
43 
ExistDeviceInfo(const std::string & nodeId,DeviceIdType deviceIdType) const44 bool DeviceInfoManager::ExistDeviceInfo(const std::string &nodeId, DeviceIdType deviceIdType) const
45 {
46     DeviceInfo deviceInfo;
47     return DeviceInfoRepository::GetInstance().FindDeviceInfo(nodeId, deviceIdType, deviceInfo);
48 }
49 
AddDeviceInfo(const std::string & networkId,const std::string & universallyUniqueId,const std::string & uniqueDeviceId,const std::string & deviceName,const std::string & deviceType)50 void DeviceInfoManager::AddDeviceInfo(const std::string &networkId, const std::string &universallyUniqueId,
51     const std::string &uniqueDeviceId, const std::string &deviceName, const std::string &deviceType)
52 {
53     if (!DataValidator::IsDeviceIdValid(networkId) ||
54         !DataValidator::IsDeviceIdValid(universallyUniqueId) ||
55         !DataValidator::IsDeviceIdValid(uniqueDeviceId) || deviceName.empty() || deviceType.empty()) {
56         ACCESSTOKEN_LOG_ERROR(LABEL, "AddDeviceInfo: input param is invalid");
57         return;
58     }
59     DeviceInfoRepository::GetInstance().SaveDeviceInfo(
60         networkId, universallyUniqueId, uniqueDeviceId, deviceName, deviceType);
61 }
62 
RemoveAllRemoteDeviceInfo()63 void DeviceInfoManager::RemoveAllRemoteDeviceInfo()
64 {
65     std::string localDevice = ConstantCommon::GetLocalDeviceId();
66 
67     DeviceInfo localDeviceInfoOpt;
68     if (DeviceInfoRepository::GetInstance().FindDeviceInfo(
69         localDevice, DeviceIdType::UNIQUE_DISABILITY_ID, localDeviceInfoOpt)) {
70         DeviceInfoRepository::GetInstance().DeleteAllDeviceInfoExceptOne(localDeviceInfoOpt);
71     }
72 }
73 
RemoveRemoteDeviceInfo(const std::string & nodeId,DeviceIdType deviceIdType)74 void DeviceInfoManager::RemoveRemoteDeviceInfo(const std::string &nodeId, DeviceIdType deviceIdType)
75 {
76     if (!DataValidator::IsDeviceIdValid(nodeId)) {
77         ACCESSTOKEN_LOG_ERROR(LABEL, "RemoveDeviceInfoByNetworkId: nodeId is invalid");
78     } else {
79         DeviceInfo deviceInfo;
80         std::string localDevice = ConstantCommon::GetLocalDeviceId();
81         if (DeviceInfoRepository::GetInstance().FindDeviceInfo(nodeId, deviceIdType, deviceInfo)) {
82             if (deviceInfo.deviceId.uniqueDeviceId != localDevice) {
83                 DeviceInfoRepository::GetInstance().DeleteDeviceInfo(nodeId, deviceIdType);
84             }
85         }
86     }
87 }
88 
ConvertToUniversallyUniqueIdOrFetch(const std::string & nodeId) const89 std::string DeviceInfoManager::ConvertToUniversallyUniqueIdOrFetch(const std::string &nodeId) const
90 {
91     std::string result;
92     if (!DataValidator::IsDeviceIdValid(nodeId)) {
93         ACCESSTOKEN_LOG_ERROR(LABEL, "ConvertToUniversallyUniqueIdOrFetch: nodeId is invalid.");
94         return result;
95     }
96     DeviceInfo deviceInfo;
97     if (!(DeviceInfoRepository::GetInstance().FindDeviceInfo(nodeId, DeviceIdType::UNKNOWN, deviceInfo))) {
98         return result;
99     }
100     std::string universallyUniqueId = deviceInfo.deviceId.universallyUniqueId;
101     if (!universallyUniqueId.empty()) {
102         result = universallyUniqueId;
103         return result;
104     }
105     std::string udid = SoftBusManager::GetInstance().GetUniversallyUniqueIdByNodeId(nodeId);
106     if (!udid.empty()) {
107         result = udid;
108     }
109     return result;
110 }
111 
ConvertToUniqueDeviceIdOrFetch(const std::string & nodeId) const112 std::string DeviceInfoManager::ConvertToUniqueDeviceIdOrFetch(const std::string &nodeId) const
113 {
114     std::string result;
115     if (!DataValidator::IsDeviceIdValid(nodeId)) {
116         ACCESSTOKEN_LOG_ERROR(LABEL, "ConvertToUniqueDeviceIdOrFetch: nodeId is invalid.");
117         return result;
118     }
119     DeviceInfo deviceInfo;
120     if (DeviceInfoRepository::GetInstance().FindDeviceInfo(nodeId, DeviceIdType::UNKNOWN, deviceInfo)) {
121         std::string uniqueDeviceId = deviceInfo.deviceId.uniqueDeviceId;
122         if (uniqueDeviceId.empty()) {
123             std::string udid = SoftBusManager::GetInstance().GetUniqueDeviceIdByNodeId(nodeId);
124             if (!udid.empty()) {
125                 result = udid;
126             } else {
127                 ACCESSTOKEN_LOG_DEBUG(LABEL,
128                     "FindDeviceInfo succeed, udid and local udid is empty, nodeId(%{public}s)",
129                     ConstantCommon::EncryptDevId(nodeId).c_str());
130             }
131         } else {
132             ACCESSTOKEN_LOG_DEBUG(LABEL,
133                 "FindDeviceInfo succeed, udid is empty, nodeId(%{public}s) ",
134                 ConstantCommon::EncryptDevId(nodeId).c_str());
135             result = uniqueDeviceId;
136         }
137     } else {
138         ACCESSTOKEN_LOG_DEBUG(
139             LABEL, "FindDeviceInfo failed, nodeId(%{public}s)",
140             ConstantCommon::EncryptDevId(nodeId).c_str());
141         auto list = DeviceInfoRepository::GetInstance().ListDeviceInfo();
142         auto iter = list.begin();
143         for (; iter != list.end(); iter++) {
144             DeviceInfo info = (*iter);
145             ACCESSTOKEN_LOG_DEBUG(
146                 LABEL, ">>> DeviceInfoRepository device name: %{public}s", info.deviceName.c_str());
147             ACCESSTOKEN_LOG_DEBUG(
148                 LABEL, ">>> DeviceInfoRepository device type: %{public}s", info.deviceType.c_str());
149             ACCESSTOKEN_LOG_DEBUG(LABEL,
150                 ">>> DeviceInfoRepository device network id: %{public}s",
151                 ConstantCommon::EncryptDevId(info.deviceId.networkId).c_str());
152         }
153     }
154     return result;
155 }
156 
IsDeviceUniversallyUniqueId(const std::string & nodeId) const157 bool DeviceInfoManager::IsDeviceUniversallyUniqueId(const std::string &nodeId) const
158 {
159     if (!DataValidator::IsDeviceIdValid(nodeId)) {
160         ACCESSTOKEN_LOG_ERROR(LABEL, "IsDeviceUniversallyUniqueId: nodeId is invalid");
161         return false;
162     }
163     DeviceInfo deviceInfo;
164     if (DeviceInfoRepository::GetInstance().FindDeviceInfo(nodeId, DeviceIdType::UNIVERSALLY_UNIQUE_ID, deviceInfo)) {
165         return deviceInfo.deviceId.universallyUniqueId == nodeId;
166     }
167     return false;
168 }
169 } // namespace AccessToken
170 } // namespace Security
171 } // namespace OHOS
172