• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "base_remote_command.h"
16 
17 #include "accesstoken_log.h"
18 #include "data_validator.h"
19 
20 namespace OHOS {
21 namespace Security {
22 namespace AccessToken {
23 namespace {
24 static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, SECURITY_DOMAIN_ACCESSTOKEN, "BaseRemoteCommand"};
25 }
26 
FromRemoteProtocolJson(const nlohmann::json & jsonObject)27 void BaseRemoteCommand::FromRemoteProtocolJson(const nlohmann::json& jsonObject)
28 {
29     if (jsonObject.find("commandName") != jsonObject.end() && jsonObject.at("commandName").is_string()) {
30         remoteProtocol_.commandName = jsonObject.at("commandName").get<std::string>();
31     }
32     if (jsonObject.find("uniqueId") != jsonObject.end() && jsonObject.at("uniqueId").is_string()) {
33         remoteProtocol_.uniqueId = jsonObject.at("uniqueId").get<std::string>();
34     }
35     if (jsonObject.find("requestVersion") != jsonObject.end() && jsonObject.at("requestVersion").is_number()) {
36         remoteProtocol_.requestVersion = jsonObject.at("requestVersion").get<int32_t>();
37     }
38     if (jsonObject.find("srcDeviceId") != jsonObject.end() && jsonObject.at("srcDeviceId").is_string()) {
39         remoteProtocol_.srcDeviceId = jsonObject.at("srcDeviceId").get<std::string>();
40     }
41     if (jsonObject.find("srcDeviceLevel") != jsonObject.end() && jsonObject.at("srcDeviceLevel").is_string()) {
42         remoteProtocol_.srcDeviceLevel = jsonObject.at("srcDeviceLevel").get<std::string>();
43     }
44     if (jsonObject.find("dstDeviceId") != jsonObject.end() && jsonObject.at("dstDeviceId").is_string()) {
45         remoteProtocol_.dstDeviceId = jsonObject.at("dstDeviceId").get<std::string>();
46     }
47     if (jsonObject.find("dstDeviceLevel") != jsonObject.end() && jsonObject.at("dstDeviceLevel").is_string()) {
48         remoteProtocol_.dstDeviceLevel = jsonObject.at("dstDeviceLevel").get<std::string>();
49     }
50     if (jsonObject.find("statusCode") != jsonObject.end() && jsonObject.at("statusCode").is_number()) {
51         remoteProtocol_.statusCode = jsonObject.at("statusCode").get<int32_t>();
52     }
53     if (jsonObject.find("message") != jsonObject.end() && jsonObject.at("message").is_string()) {
54         remoteProtocol_.message = jsonObject.at("message").get<std::string>();
55     }
56     if (jsonObject.find("responseVersion") != jsonObject.end() && jsonObject.at("responseVersion").is_number()) {
57         remoteProtocol_.responseVersion = jsonObject.at("responseVersion").get<int32_t>();
58     }
59     if (jsonObject.find("responseDeviceId") != jsonObject.end() && jsonObject.at("responseDeviceId").is_string()) {
60         remoteProtocol_.responseDeviceId = jsonObject.at("responseDeviceId").get<std::string>();
61     }
62 }
63 
ToRemoteProtocolJson()64 nlohmann::json BaseRemoteCommand::ToRemoteProtocolJson()
65 {
66     nlohmann::json j;
67     j["commandName"] = remoteProtocol_.commandName;
68     j["uniqueId"] = remoteProtocol_.uniqueId;
69     j["requestVersion"] = remoteProtocol_.requestVersion;
70     j["srcDeviceId"] = remoteProtocol_.srcDeviceId;
71     j["srcDeviceLevel"] = remoteProtocol_.srcDeviceLevel;
72     j["dstDeviceId"] = remoteProtocol_.dstDeviceId;
73     j["dstDeviceLevel"] = remoteProtocol_.dstDeviceLevel;
74     j["statusCode"] = remoteProtocol_.statusCode;
75     j["message"] = remoteProtocol_.message;
76     j["responseVersion"] = remoteProtocol_.responseVersion;
77     j["responseDeviceId"] = remoteProtocol_.responseDeviceId;
78     return j;
79 }
80 
ToNativeTokenInfoJson(const NativeTokenInfo & tokenInfo)81 nlohmann::json BaseRemoteCommand::ToNativeTokenInfoJson(const NativeTokenInfo& tokenInfo)
82 {
83     nlohmann::json DcapsJson = nlohmann::json(tokenInfo.dcap);
84     nlohmann::json nativeTokenJson = nlohmann::json {
85         {"processName", tokenInfo.processName},
86         {"apl", tokenInfo.apl},
87         {"version", tokenInfo.ver},
88         {"tokenId", tokenInfo.tokenID},
89         {"tokenAttr", tokenInfo.tokenAttr},
90         {"dcaps", DcapsJson},
91     };
92     return nativeTokenJson;
93 }
94 
ToPermStateJson(nlohmann::json & permStateJson,const PermissionStateFull & state)95 void BaseRemoteCommand::ToPermStateJson(nlohmann::json& permStateJson, const PermissionStateFull& state)
96 {
97     if (state.resDeviceID.size() != state.grantStatus.size() || state.resDeviceID.size() != state.grantFlags.size()) {
98         ACCESSTOKEN_LOG_DEBUG(LABEL, "state grant config size is invalid");
99         return;
100     }
101     nlohmann::json permConfigsJson;
102     int size = (int)state.resDeviceID.size();
103     for (int i = 0; i < size; i++) {
104         nlohmann::json permConfigJson = nlohmann::json {
105             {"resDeviceID", state.resDeviceID[i]},
106             {"grantStatus", state.grantStatus[i]},
107             {"grantFlags", state.grantFlags[i]},
108         };
109         permConfigsJson.emplace_back(permConfigJson);
110     }
111 
112     permStateJson["permissionName"] = state.permissionName;
113     permStateJson["isGeneral"] = state.isGeneral;
114     permStateJson["grantConfig"] = permConfigsJson;
115 }
116 
ToHapTokenInfosJson(const HapTokenInfoForSync & tokenInfo)117 nlohmann::json BaseRemoteCommand::ToHapTokenInfosJson(const HapTokenInfoForSync& tokenInfo)
118 {
119     nlohmann::json permStatesJson;
120     for (auto& permState : tokenInfo.permStateList) {
121         nlohmann::json permStateJson;
122         ToPermStateJson(permStateJson, permState);
123         permStatesJson.emplace_back(permStateJson);
124     }
125 
126     nlohmann::json hapTokensJson = nlohmann::json {
127         {"version", tokenInfo.baseInfo.ver},
128         {"tokenID", tokenInfo.baseInfo.tokenID},
129         {"tokenAttr", tokenInfo.baseInfo.tokenAttr},
130         {"userID", tokenInfo.baseInfo.userID},
131         {"bundleName", tokenInfo.baseInfo.bundleName},
132         {"instIndex", tokenInfo.baseInfo.instIndex},
133         {"appID", tokenInfo.baseInfo.appID},
134         {"deviceID", tokenInfo.baseInfo.deviceID},
135         {"apl", tokenInfo.baseInfo.apl},
136         {"permState", permStatesJson}
137     };
138     return hapTokensJson;
139 }
140 
FromHapTokenBasicInfoJson(const nlohmann::json & hapTokenJson,HapTokenInfo & hapTokenBasicInfo)141 void BaseRemoteCommand::FromHapTokenBasicInfoJson(const nlohmann::json& hapTokenJson,
142     HapTokenInfo& hapTokenBasicInfo)
143 {
144     if (hapTokenJson.find("version") != hapTokenJson.end() && hapTokenJson.at("version").is_number()) {
145         hapTokenJson.at("version").get_to(hapTokenBasicInfo.ver);
146     }
147     if (hapTokenJson.find("tokenID") != hapTokenJson.end() && hapTokenJson.at("tokenID").is_number()) {
148         hapTokenJson.at("tokenID").get_to(hapTokenBasicInfo.tokenID);
149     }
150     if (hapTokenJson.find("tokenAttr") != hapTokenJson.end() && hapTokenJson.at("tokenAttr").is_number()) {
151         hapTokenJson.at("tokenAttr").get_to(hapTokenBasicInfo.tokenAttr);
152     }
153     if (hapTokenJson.find("userID") != hapTokenJson.end() && hapTokenJson.at("userID").is_number()) {
154         hapTokenJson.at("userID").get_to(hapTokenBasicInfo.userID);
155     }
156     if (hapTokenJson.find("bundleName") != hapTokenJson.end() && hapTokenJson.at("bundleName").is_string()) {
157         hapTokenJson.at("bundleName").get_to(hapTokenBasicInfo.bundleName);
158     }
159     if (hapTokenJson.find("instIndex") != hapTokenJson.end() && hapTokenJson.at("instIndex").is_number()) {
160         hapTokenJson.at("instIndex").get_to(hapTokenBasicInfo.instIndex);
161     }
162     if (hapTokenJson.find("appID") != hapTokenJson.end() && hapTokenJson.at("appID").is_string()) {
163         hapTokenJson.at("appID").get_to(hapTokenBasicInfo.appID);
164     }
165     if (hapTokenJson.find("deviceID") != hapTokenJson.end() && hapTokenJson.at("deviceID").is_string()) {
166         hapTokenJson.at("deviceID").get_to(hapTokenBasicInfo.deviceID);
167     }
168     if (hapTokenJson.find("apl") != hapTokenJson.end() && hapTokenJson.at("apl").is_number()) {
169         int apl = hapTokenJson.at("apl").get<int>();
170         if (DataValidator::IsAplNumValid(apl)) {
171             hapTokenBasicInfo.apl = (ATokenAplEnum)apl;
172         }
173     }
174 }
175 
FromPermStateListJson(const nlohmann::json & hapTokenJson,std::vector<PermissionStateFull> & permStateList)176 void BaseRemoteCommand::FromPermStateListJson(const nlohmann::json& hapTokenJson,
177     std::vector<PermissionStateFull>& permStateList)
178 {
179     if (hapTokenJson.find("permState") != hapTokenJson.end()
180         && hapTokenJson.at("permState").is_array()
181         && hapTokenJson.at("permState").size() > 0) {
182         nlohmann::json permissionsJson = hapTokenJson.at("permState").get<nlohmann::json>();
183         for (auto permissionJson : permissionsJson) {
184             PermissionStateFull permission;
185             if (permissionJson.find("permissionName") == permissionJson.end()
186                 || !permissionJson.at("permissionName").is_string()
187                 || permissionJson.find("isGeneral") == permissionJson.end()
188                 || !permissionJson.at("isGeneral").is_boolean()
189                 || permissionJson.find("grantConfig") == permissionJson.end()
190                 || !permissionJson.at("grantConfig").is_array()
191                 || permissionJson.at("grantConfig").size() == 0) {
192                 continue;
193             }
194             permissionJson.at("permissionName").get_to(permission.permissionName);
195             permissionJson.at("isGeneral").get_to(permission.isGeneral);
196             nlohmann::json grantConfigsJson = permissionJson.at("grantConfig").get<nlohmann::json>();
197             for (auto grantConfigJson :grantConfigsJson) {
198                 if (grantConfigJson.find("resDeviceID") == grantConfigJson.end()
199                     || !grantConfigJson.at("resDeviceID").is_string()
200                     || grantConfigJson.find("grantStatus") == grantConfigJson.end()
201                     || !grantConfigJson.at("grantStatus").is_number()
202                     || grantConfigJson.find("grantFlags") == grantConfigJson.end()
203                     || !grantConfigJson.at("grantFlags").is_number()) {
204                     continue;
205                 }
206                 std::string deviceID;
207                 grantConfigJson.at("resDeviceID").get_to(deviceID);
208                 int grantStatus;
209                 grantConfigJson.at("grantStatus").get_to(grantStatus);
210                 int grantFlags;
211                 grantConfigJson.at("grantFlags").get_to(grantFlags);
212                 permission.resDeviceID.emplace_back(deviceID);
213                 permission.grantStatus.emplace_back(grantStatus);
214                 permission.grantFlags.emplace_back(grantFlags);
215             }
216             permStateList.emplace_back(permission);
217         }
218     }
219 }
220 
FromHapTokenInfoJson(const nlohmann::json & hapTokenJson,HapTokenInfoForSync & hapTokenInfo)221 void BaseRemoteCommand::FromHapTokenInfoJson(const nlohmann::json& hapTokenJson,
222     HapTokenInfoForSync& hapTokenInfo)
223 {
224     FromHapTokenBasicInfoJson(hapTokenJson, hapTokenInfo.baseInfo);
225     if (hapTokenInfo.baseInfo.tokenID == 0) {
226         ACCESSTOKEN_LOG_ERROR(LABEL, "Hap token basic info is error.");
227         return;
228     }
229     FromPermStateListJson(hapTokenJson, hapTokenInfo.permStateList);
230 }
231 
FromNativeTokenInfoJson(const nlohmann::json & nativeTokenJson,NativeTokenInfo & nativeTokenInfo)232 void BaseRemoteCommand::FromNativeTokenInfoJson(const nlohmann::json& nativeTokenJson,
233     NativeTokenInfo& nativeTokenInfo)
234 {
235     if (nativeTokenJson.find("processName") != nativeTokenJson.end() && nativeTokenJson.at("processName").is_string()) {
236         nativeTokenInfo.processName = nativeTokenJson.at("processName").get<std::string>();
237     }
238     if (nativeTokenJson.find("apl") != nativeTokenJson.end() && nativeTokenJson.at("apl").is_number()) {
239         int apl = nativeTokenJson.at("apl").get<int>();
240         if (DataValidator::IsAplNumValid(apl)) {
241             nativeTokenInfo.apl = (ATokenAplEnum)apl;
242         }
243     }
244     if (nativeTokenJson.find("version") != nativeTokenJson.end() && nativeTokenJson.at("version").is_number()) {
245         nativeTokenInfo.ver = (unsigned char)nativeTokenJson.at("version").get<int32_t>();
246     }
247     if (nativeTokenJson.find("tokenId") != nativeTokenJson.end() && nativeTokenJson.at("tokenId").is_number()) {
248         nativeTokenInfo.tokenID = (unsigned)nativeTokenJson.at("tokenId").get<int32_t>();
249     }
250     if (nativeTokenJson.find("tokenAttr") != nativeTokenJson.end() && nativeTokenJson.at("tokenAttr").is_number()) {
251         nativeTokenInfo.tokenAttr = (unsigned)nativeTokenJson.at("tokenAttr").get<int32_t>();
252     }
253     if (nativeTokenJson.find("dcaps") != nativeTokenJson.end() && nativeTokenJson.at("dcaps").is_array()
254         && nativeTokenJson.at("dcaps").size() > 0 && (nativeTokenJson.at("dcaps"))[0].is_string()) {
255         nativeTokenInfo.dcap = nativeTokenJson.at("dcaps").get<std::vector<std::string>>();
256     }
257 }
258 }  // namespace AccessToken
259 }  // namespace Security
260 }  // namespace OHOS
261