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 #include "dm_config_manager.h"
16
17 #include <dlfcn.h>
18
19 #include "dm_anonymous.h"
20 #include "dm_constants.h"
21 #include "dm_log.h"
22 #include "json_config.h"
23 #include "nlohmann/json.hpp"
24
25 namespace OHOS {
26 namespace DistributedHardware {
from_json(const nlohmann::json & jsonObject,AdapterSoLoadInfo & soLoadInfo)27 void from_json(const nlohmann::json &jsonObject, AdapterSoLoadInfo &soLoadInfo)
28 {
29 if (!IsString(jsonObject, "name") || !IsString(jsonObject, "type") || !IsString(jsonObject, "version") ||
30 !IsString(jsonObject, "funcName") || !IsString(jsonObject, "soName") || !IsString(jsonObject, "soPath")) {
31 LOGE("AdapterSoLoadInfo json key Not complete");
32 return;
33 }
34 soLoadInfo.name = jsonObject["name"].get<std::string>();
35 soLoadInfo.type = jsonObject["type"].get<std::string>();
36 soLoadInfo.version = jsonObject["version"].get<std::string>();
37 soLoadInfo.funcName = jsonObject["funcName"].get<std::string>();
38 soLoadInfo.soName = jsonObject["soName"].get<std::string>();
39 soLoadInfo.soPath = jsonObject["soPath"].get<std::string>();
40 }
41
from_json(const nlohmann::json & jsonObject,AuthSoLoadInfo & soLoadInfo)42 void from_json(const nlohmann::json &jsonObject, AuthSoLoadInfo &soLoadInfo)
43 {
44 if (!IsString(jsonObject, "name") || !IsString(jsonObject, "type") || !IsString(jsonObject, "version") ||
45 !IsString(jsonObject, "funcName") || !IsString(jsonObject, "soName") || !IsString(jsonObject, "soPath") ||
46 !IsInt32(jsonObject, "authType")) {
47 LOGE("AuthSoLoadInfo json key Not complete");
48 return;
49 }
50 soLoadInfo.authType = jsonObject["authType"].get<int32_t>();
51 soLoadInfo.name = jsonObject["name"].get<std::string>();
52 soLoadInfo.type = jsonObject["type"].get<std::string>();
53 soLoadInfo.version = jsonObject["version"].get<std::string>();
54 soLoadInfo.funcName = jsonObject["funcName"].get<std::string>();
55 soLoadInfo.soName = jsonObject["soName"].get<std::string>();
56 soLoadInfo.soPath = jsonObject["soPath"].get<std::string>();
57 }
58
GetInstance()59 DmConfigManager &DmConfigManager::GetInstance()
60 {
61 static DmConfigManager instance;
62 return instance;
63 }
64
DmConfigManager()65 DmConfigManager::DmConfigManager()
66 {
67 LOGI("DmConfigManager constructor");
68 do {
69 nlohmann::json adapterJsonObject = nlohmann::json::parse(adapterJsonConfigString, nullptr, false);
70 if (adapterJsonObject.is_discarded()) {
71 LOGE("adapter json config string parse error");
72 break;
73 }
74 if (!IsArray(adapterJsonObject, ADAPTER_LOAD_JSON_KEY)) {
75 LOGE("adapter json config string key not exist");
76 break;
77 }
78 auto soLoadInfo = adapterJsonObject[ADAPTER_LOAD_JSON_KEY].get<std::vector<AdapterSoLoadInfo>>();
79 for (uint32_t i = 0; i < soLoadInfo.size(); i++) {
80 if (soLoadInfo[i].name.size() == 0 || soLoadInfo[i].type.size() == 0 || soLoadInfo[i].version.size() == 0 ||
81 soLoadInfo[i].funcName.size() == 0 || soLoadInfo[i].soName.size() == 0 ||
82 soLoadInfo[i].soPath.size() == 0) {
83 LOGE("adapter json config string exist invalid members");
84 continue;
85 }
86 soLoadInfo[i].soPath = std::string(LIB_LOAD_PATH);
87 soAdapterLoadInfo_[soLoadInfo[i].soName] = soLoadInfo[i];
88 LOGI("soAdapterLoadInfo name is: %s", soLoadInfo[i].name.c_str());
89 LOGI("soAdapterLoadInfo type is: %s", soLoadInfo[i].type.c_str());
90 LOGI("soAdapterLoadInfo version is: %s", soLoadInfo[i].version.c_str());
91 LOGI("soAdapterLoadInfo funcName is: %s", soLoadInfo[i].funcName.c_str());
92 LOGI("soAdapterLoadInfo soName is: %s", soLoadInfo[i].soName.c_str());
93 LOGI("soAdapterLoadInfo soPath is: %s", soLoadInfo[i].soPath.c_str());
94 }
95 } while (0);
96
97 do {
98 nlohmann::json authJsonObject = nlohmann::json::parse(authJsonConfigString, nullptr, false);
99 if (authJsonObject.is_discarded()) {
100 LOGE("auth json config string parse error!\n");
101 break;
102 }
103 if (!IsArray(authJsonObject, AUTH_LOAD_JSON_KEY)) {
104 LOGE("auth json config string key not exist!\n");
105 break;
106 }
107 auto soLoadInfo = authJsonObject[AUTH_LOAD_JSON_KEY].get<std::vector<AuthSoLoadInfo>>();
108 for (uint32_t i = 0; i < soLoadInfo.size(); i++) {
109 if (soLoadInfo[i].name.size() == 0 || soLoadInfo[i].type.size() == 0 || soLoadInfo[i].version.size() == 0 ||
110 soLoadInfo[i].funcName.size() == 0 || soLoadInfo[i].soName.size() == 0 ||
111 soLoadInfo[i].soPath.size() == 0) {
112 LOGE("adapter json config string exist invalid members");
113 continue;
114 }
115 soLoadInfo[i].soPath = std::string(LIB_LOAD_PATH);
116 soAuthLoadInfo_[soLoadInfo[i].authType] = soLoadInfo[i];
117 LOGI("soAuthLoadInfo name is: %s", soLoadInfo[i].name.c_str());
118 LOGI("soAuthLoadInfo type is: %s", soLoadInfo[i].type.c_str());
119 LOGI("soAuthLoadInfo version is: %s", soLoadInfo[i].version.c_str());
120 LOGI("soAuthLoadInfo funcName is: %s", soLoadInfo[i].funcName.c_str());
121 LOGI("soAuthLoadInfo soName is: %s", soLoadInfo[i].soName.c_str());
122 LOGI("soAuthLoadInfo soPath is: %s", soLoadInfo[i].soPath.c_str());
123 LOGI("soAuthLoadInfo authType is: %d", soLoadInfo[i].authType);
124 }
125 } while (0);
126 }
127
~DmConfigManager()128 DmConfigManager::~DmConfigManager()
129 {
130 void *so_handle = nullptr;
131 for (auto iter = soAdapterLoadInfo_.begin(); iter != soAdapterLoadInfo_.end(); iter++) {
132 std::string soPathName = (iter->second).soPath + (iter->second).soName;
133 so_handle = dlopen(soPathName.c_str(), RTLD_NOW | RTLD_NOLOAD);
134 if (so_handle != nullptr) {
135 dlclose(so_handle);
136 }
137 }
138 for (auto iter = soAuthLoadInfo_.begin(); iter != soAuthLoadInfo_.end(); iter++) {
139 std::string soPathName = (iter->second).soPath + (iter->second).soName;
140 so_handle = dlopen(soPathName.c_str(), RTLD_NOW | RTLD_NOLOAD);
141 if (so_handle != nullptr) {
142 dlclose(so_handle);
143 }
144 }
145 LOGI("DmAdapterManager destructor");
146 }
147
GetDecisionAdapter(const std::string & soName)148 std::shared_ptr<IDecisionAdapter> DmConfigManager::GetDecisionAdapter(const std::string &soName)
149 {
150 if (soName.empty()) {
151 LOGE("soName size is zero");
152 return nullptr;
153 }
154 auto soInfoIter = soAdapterLoadInfo_.find(soName);
155 if (soInfoIter == soAdapterLoadInfo_.end() || (soInfoIter->second).type != std::string(DECISION_JSON_TYPE_KEY)) {
156 LOGE("not find so info or type key not match");
157 return nullptr;
158 }
159 std::unique_lock<std::mutex> locker(decisionAdapterMutex_);
160 auto ptrIter = decisionAdapterPtr_.find(soName);
161 if (ptrIter != decisionAdapterPtr_.end()) {
162 return decisionAdapterPtr_[soName];
163 }
164 void *so_handle = nullptr;
165 std::string soPathName = (soInfoIter->second).soPath + (soInfoIter->second).soName;
166 so_handle = dlopen(soPathName.c_str(), RTLD_NOW | RTLD_NOLOAD);
167 if (so_handle == nullptr) {
168 so_handle = dlopen(soPathName.c_str(), RTLD_NOW);
169 if (so_handle == nullptr) {
170 LOGE("load decision so %s failed", soName.c_str());
171 return nullptr;
172 }
173 }
174 dlerror();
175 auto func = (CreateIDecisionAdapterFuncPtr)dlsym(so_handle, (soInfoIter->second).funcName.c_str());
176 if (dlerror() != nullptr || func == nullptr) {
177 LOGE("Create object function is not exist");
178 return nullptr;
179 }
180 std::shared_ptr<IDecisionAdapter> iDecisionAdapter(func());
181 decisionAdapterPtr_[soName] = iDecisionAdapter;
182 return decisionAdapterPtr_[soName];
183 }
184
GetCryptoAdapter(const std::string & soName)185 std::shared_ptr<ICryptoAdapter> DmConfigManager::GetCryptoAdapter(const std::string &soName)
186 {
187 if (soName.empty()) {
188 LOGE("soName size is zero");
189 return nullptr;
190 }
191
192 auto soInfoIter = soAdapterLoadInfo_.find(soName);
193 if (soInfoIter == soAdapterLoadInfo_.end() || (soInfoIter->second).type != std::string(CPYPTO_JSON_TYPE_KEY)) {
194 LOGE("not find so info or type key not match");
195 return nullptr;
196 }
197
198 std::unique_lock<std::mutex> locker(cryptoAdapterMutex_);
199 auto ptrIter = cryptoAdapterPtr_.find(soName);
200 if (ptrIter != cryptoAdapterPtr_.end()) {
201 return cryptoAdapterPtr_[soName];
202 }
203
204 void *so_handle = nullptr;
205 std::string soPathName = (soInfoIter->second).soPath + (soInfoIter->second).soName;
206 so_handle = dlopen(soPathName.c_str(), RTLD_NOW | RTLD_NOLOAD);
207 if (so_handle == nullptr) {
208 so_handle = dlopen(soPathName.c_str(), RTLD_NOW);
209 if (so_handle == nullptr) {
210 LOGE("load crypto so %s failed", soName.c_str());
211 return nullptr;
212 }
213 }
214
215 dlerror();
216 auto func = (CreateICryptoAdapterFuncPtr)dlsym(so_handle, (soInfoIter->second).funcName.c_str());
217 if (dlerror() != nullptr || func == nullptr) {
218 LOGE("Create object function is not exist");
219 return nullptr;
220 }
221
222 std::shared_ptr<ICryptoAdapter> iCryptoAdapter(func());
223 cryptoAdapterPtr_[soName] = iCryptoAdapter;
224 return cryptoAdapterPtr_[soName];
225 }
226
GetAuthAdapter(std::map<int32_t,std::shared_ptr<IAuthentication>> & authAdapter)227 void DmConfigManager::GetAuthAdapter(std::map<int32_t, std::shared_ptr<IAuthentication>> &authAdapter)
228 {
229 authAdapter.clear();
230 for (auto iter = soAuthLoadInfo_.begin(); iter != soAuthLoadInfo_.end(); iter++) {
231 if ((iter->second).type != std::string(AUTH_JSON_TYPE_KEY)) {
232 LOGE("type key not match");
233 continue;
234 }
235
236 void *so_handle = nullptr;
237 std::string soPathName = (iter->second).soPath + (iter->second).soName;
238 so_handle = dlopen(soPathName.c_str(), RTLD_NOW | RTLD_NOLOAD);
239 if (so_handle == nullptr) {
240 so_handle = dlopen(soPathName.c_str(), RTLD_NOW);
241 if (so_handle == nullptr) {
242 LOGE("load auth so %s failed", (iter->second).soName.c_str());
243 continue;
244 }
245 }
246
247 dlerror();
248 auto func = (CreateIAuthAdapterFuncPtr)dlsym(so_handle, (iter->second).funcName.c_str());
249 if (dlerror() != nullptr || func == nullptr) {
250 LOGE("Create object function is not exist");
251 continue;
252 }
253
254 std::shared_ptr<IAuthentication> iAuthentication(func());
255 authAdapter[iter->first] = iAuthentication;
256 LOGI("so name: %s, auth type: %d", (iter->second).soName.c_str(), iter->first);
257 }
258 }
259 } // namespace DistributedHardware
260 } // namespace OHOS