1 /*
2 * Copyright (c) 2024 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 "remote_msg_util.h"
17
18 #include <iomanip>
19 #include <mutex>
20 #include <sstream>
21 #include <string>
22
23 #include "device_manager.h"
24 #include "device_manager_util.h"
25 #include "hdi_wrapper.h"
26 #include "iam_check.h"
27 #include "iam_logger.h"
28 #include "parameter.h"
29 #include "resource_node_pool.h"
30
31 #define LOG_TAG "USER_AUTH_SA"
32
33 namespace OHOS {
34 namespace UserIam {
35 namespace UserAuth {
36 namespace {
37 const uint32_t TRUCKED_NETWORK_ID_PRINT_LEN = 4;
38 const uint32_t TRUCKED_CONTEXT_ID_PRINT_LEN = 4;
39 } // namespace
GetConnectionName(uint64_t contextId,std::string & connectionName)40 bool RemoteMsgUtil::GetConnectionName(uint64_t contextId, std::string &connectionName)
41 {
42 std::string networkId;
43 bool getLocalNetworkIdRet = DeviceManagerUtil::GetInstance().GetLocalDeviceNetWorkId(networkId);
44 IF_FALSE_LOGE_AND_RETURN_VAL(getLocalNetworkIdRet, false);
45
46 std::ostringstream oss;
47 oss << networkId.substr(0, TRUCKED_NETWORK_ID_PRINT_LEN) << ":";
48 oss << std::setw(TRUCKED_CONTEXT_ID_PRINT_LEN) << std::setfill('0') << std::hex << static_cast<uint16_t>(contextId);
49 connectionName = oss.str();
50 return true;
51 }
52
EncodeQueryExecutorInfoReply(const std::vector<ExecutorInfo> & executorInfoArray,const std::vector<uint8_t> & signedRemoteExecutorInfo,Attributes & attr)53 bool RemoteMsgUtil::EncodeQueryExecutorInfoReply(const std::vector<ExecutorInfo> &executorInfoArray,
54 const std::vector<uint8_t> &signedRemoteExecutorInfo, Attributes &attr)
55 {
56 IF_FALSE_LOGE_AND_RETURN_VAL(executorInfoArray.size() != 0, false);
57
58 bool setRemoteExecutorInfoRet =
59 attr.SetUint8ArrayValue(Attributes::ATTR_REMOTE_EXECUTOR_INFO, signedRemoteExecutorInfo);
60 IF_FALSE_LOGE_AND_RETURN_VAL(setRemoteExecutorInfoRet, false);
61
62 return SetExecutorInfoArrayToAttributes(executorInfoArray, attr);
63 }
64
DecodeQueryExecutorInfoReply(const Attributes & attr,std::vector<ExecutorInfo> & executorInfoArray)65 bool RemoteMsgUtil::DecodeQueryExecutorInfoReply(const Attributes &attr, std::vector<ExecutorInfo> &executorInfoArray)
66 {
67 std::vector<uint8_t> signedRemoteExecutorInfo;
68 bool getRemoteExecutorInfoRet =
69 attr.GetUint8ArrayValue(Attributes::ATTR_REMOTE_EXECUTOR_INFO, signedRemoteExecutorInfo);
70 IF_FALSE_LOGE_AND_RETURN_VAL(getRemoteExecutorInfoRet, false);
71
72 return GetExecutorInfoArrayFromAttributes(attr, signedRemoteExecutorInfo, executorInfoArray);
73 }
74
SetExecutorInfoToAttributes(const ExecutorInfo & executorInfo,Attributes & attr)75 bool RemoteMsgUtil::SetExecutorInfoToAttributes(const ExecutorInfo &executorInfo, Attributes &attr)
76 {
77 bool setAuthTypeRet = attr.SetInt32Value(Attributes::ATTR_AUTH_TYPE, executorInfo.authType);
78 IF_FALSE_LOGE_AND_RETURN_VAL(setAuthTypeRet, false);
79
80 bool setExecutorRoleRet = attr.SetInt32Value(Attributes::ATTR_EXECUTOR_ROLE, executorInfo.executorRole);
81 IF_FALSE_LOGE_AND_RETURN_VAL(setExecutorRoleRet, false);
82
83 bool setExecutorSensorHintRet =
84 attr.SetUint32Value(Attributes::ATTR_EXECUTOR_SENSOR_HINT, executorInfo.executorSensorHint);
85 IF_FALSE_LOGE_AND_RETURN_VAL(setExecutorSensorHintRet, false);
86
87 bool setExecutorMatcherRet = attr.SetUint32Value(Attributes::ATTR_EXECUTOR_MATCHER, executorInfo.executorMatcher);
88 IF_FALSE_LOGE_AND_RETURN_VAL(setExecutorMatcherRet, false);
89
90 bool setEslRet = attr.SetInt32Value(Attributes::ATTR_ESL, executorInfo.esl);
91 IF_FALSE_LOGE_AND_RETURN_VAL(setEslRet, false);
92
93 bool setPublicKeyRet = attr.SetUint8ArrayValue(Attributes::ATTR_PUBLIC_KEY, executorInfo.publicKey);
94 IF_FALSE_LOGE_AND_RETURN_VAL(setPublicKeyRet, false);
95
96 bool setDeviceUdidRet = attr.SetStringValue(Attributes::ATTR_DEVICE_UDID, executorInfo.deviceUdid);
97 IF_FALSE_LOGE_AND_RETURN_VAL(setDeviceUdidRet, false);
98
99 return true;
100 }
101
GetExecutorInfoFromAttributes(const Attributes & Attr,std::vector<uint8_t> & signedRemoteExecutorInfo,ExecutorInfo & executorInfo)102 bool RemoteMsgUtil::GetExecutorInfoFromAttributes(const Attributes &Attr,
103 std::vector<uint8_t> &signedRemoteExecutorInfo, ExecutorInfo &executorInfo)
104 {
105 int32_t authType = 0;
106 bool getAuthTypeRet = Attr.GetInt32Value(Attributes::ATTR_AUTH_TYPE, authType);
107 IF_FALSE_LOGE_AND_RETURN_VAL(getAuthTypeRet, false);
108 executorInfo.authType = static_cast<AuthType>(authType);
109
110 int32_t executorRole = 0;
111 bool getExecutorRoleRet = Attr.GetInt32Value(Attributes::ATTR_EXECUTOR_ROLE, executorRole);
112 IF_FALSE_LOGE_AND_RETURN_VAL(getExecutorRoleRet, false);
113 executorInfo.executorRole = static_cast<ExecutorRole>(executorRole);
114
115 bool getExecutorSensorHintRet =
116 Attr.GetUint32Value(Attributes::ATTR_EXECUTOR_SENSOR_HINT, executorInfo.executorSensorHint);
117 IF_FALSE_LOGE_AND_RETURN_VAL(getExecutorSensorHintRet, false);
118
119 bool getExecutorMatcherRet = Attr.GetUint32Value(Attributes::ATTR_EXECUTOR_MATCHER, executorInfo.executorMatcher);
120 IF_FALSE_LOGE_AND_RETURN_VAL(getExecutorMatcherRet, false);
121
122 int32_t esl = 0;
123 bool getEslRet = Attr.GetInt32Value(Attributes::ATTR_ESL, esl);
124 IF_FALSE_LOGE_AND_RETURN_VAL(getEslRet, false);
125 executorInfo.esl = static_cast<ExecutorSecureLevel>(esl);
126
127 bool getPublicKeyRet = Attr.GetUint8ArrayValue(Attributes::ATTR_PUBLIC_KEY, executorInfo.publicKey);
128 IF_FALSE_LOGE_AND_RETURN_VAL(getPublicKeyRet, false);
129
130 bool getDeviceUdidRet = Attr.GetStringValue(Attributes::ATTR_DEVICE_UDID, executorInfo.deviceUdid);
131 IF_FALSE_LOGE_AND_RETURN_VAL(getDeviceUdidRet, false);
132
133 executorInfo.signedRemoteExecutorInfo = signedRemoteExecutorInfo;
134 return true;
135 }
136
SetExecutorInfoArrayToAttributes(const std::vector<ExecutorInfo> & executorInfoArray,Attributes & attr)137 bool RemoteMsgUtil::SetExecutorInfoArrayToAttributes(const std::vector<ExecutorInfo> &executorInfoArray,
138 Attributes &attr)
139 {
140 std::vector<Attributes> attributeArray;
141 for (auto &executorInfo : executorInfoArray) {
142 Attributes item;
143 if (!SetExecutorInfoToAttributes(executorInfo, item)) {
144 IAM_LOGE("SetExecutorInfoToAttributes failed");
145 return false;
146 }
147 attributeArray.push_back(Attributes(item.Serialize()));
148 }
149
150 bool setAttributeArrayRet =
151 attr.SetAttributesArrayValue(Attributes::ATTR_EXECUTOR_REGISTER_INFO_LIST, attributeArray);
152 IF_FALSE_LOGE_AND_RETURN_VAL(setAttributeArrayRet, false);
153
154 return true;
155 }
156
GetExecutorInfoArrayFromAttributes(const Attributes & attr,std::vector<uint8_t> & signedRemoteExecutorInfo,std::vector<ExecutorInfo> & executorInfoArray)157 bool RemoteMsgUtil::GetExecutorInfoArrayFromAttributes(const Attributes &attr,
158 std::vector<uint8_t> &signedRemoteExecutorInfo, std::vector<ExecutorInfo> &executorInfoArray)
159 {
160 std::vector<Attributes> attributeArray;
161 bool getExecutorInfoRet =
162 attr.GetAttributesArrayValue(Attributes::ATTR_EXECUTOR_REGISTER_INFO_LIST, attributeArray);
163 IF_FALSE_LOGE_AND_RETURN_VAL(getExecutorInfoRet, false);
164
165 for (auto &item : attributeArray) {
166 ExecutorInfo executorInfo;
167 if (!GetExecutorInfoFromAttributes(item, signedRemoteExecutorInfo, executorInfo)) {
168 IAM_LOGE("GetExecutorInfoFromAttributes failed");
169 return false;
170 }
171 executorInfoArray.push_back(executorInfo);
172 }
173
174 return true;
175 }
176
GetQueryExecutorInfoReply(const std::vector<int32_t> authTypes,int32_t executorRole,std::string remoteUdid,Attributes & attr)177 bool RemoteMsgUtil::GetQueryExecutorInfoReply(const std::vector<int32_t> authTypes, int32_t executorRole,
178 std::string remoteUdid, Attributes &attr)
179 {
180 auto hdi = HdiWrapper::GetHdiInstance();
181 IF_FALSE_LOGE_AND_RETURN_VAL(hdi != nullptr, false);
182 IF_FALSE_LOGE_AND_RETURN_VAL(authTypes.size() == 1, false);
183
184 std::vector<uint8_t> signedExecutorInfo;
185 int32_t hdiRet = hdi->GetSignedExecutorInfo(authTypes, executorRole, remoteUdid, signedExecutorInfo);
186 IF_FALSE_LOGE_AND_RETURN_VAL(hdiRet == SUCCESS, false);
187
188 std::string localUdid;
189 bool getLocalUdidRet = DeviceManagerUtil::GetInstance().GetLocalDeviceUdid(localUdid);
190 IF_FALSE_LOGE_AND_RETURN_VAL(getLocalUdidRet, false);
191
192 std::vector<ExecutorInfo> executorInfoArray;
193 ResourceNodePool::Instance().Enumerate([&](const std::weak_ptr<ResourceNode> &weakNode) {
194 std::shared_ptr<ResourceNode> node = weakNode.lock();
195 IF_FALSE_LOGE_AND_RETURN(node != nullptr);
196
197 if (node->GetAuthType() != authTypes[0] || node->GetExecutorRole() != executorRole ||
198 localUdid != node->GetExecutorDeviceUdid()) {
199 return;
200 }
201
202 ExecutorInfo executorInfo;
203 executorInfo.authType = node->GetAuthType();
204 executorInfo.executorRole = node->GetExecutorRole();
205 executorInfo.executorSensorHint = node->GetExecutorSensorHint();
206 executorInfo.executorMatcher = node->GetExecutorMatcher();
207 executorInfo.esl = node->GetExecutorEsl();
208 executorInfo.publicKey = node->GetExecutorPublicKey();
209 executorInfo.deviceUdid = node->GetExecutorDeviceUdid();
210 executorInfoArray.push_back(executorInfo);
211 });
212
213 bool encodeQueryExecutorInfoReplyRet =
214 RemoteMsgUtil::EncodeQueryExecutorInfoReply(executorInfoArray, signedExecutorInfo, attr);
215 IF_FALSE_LOGE_AND_RETURN_VAL(encodeQueryExecutorInfoReplyRet, false);
216
217 IAM_LOGI("success");
218 return true;
219 }
220
EncodeAuthParam(const AuthParamInner & authParam,Attributes & attr)221 bool RemoteMsgUtil::EncodeAuthParam(const AuthParamInner &authParam, Attributes &attr)
222 {
223 bool setUserIdRet = attr.SetInt32Value(Attributes::ATTR_USER_ID, authParam.userId);
224 IF_FALSE_LOGE_AND_RETURN_VAL(setUserIdRet, false);
225
226 bool setChallengeRet = attr.SetUint8ArrayValue(Attributes::ATTR_CHALLENGE, authParam.challenge);
227 IF_FALSE_LOGE_AND_RETURN_VAL(setChallengeRet, false);
228
229 bool setAuthTypeRet = attr.SetInt32Value(Attributes::ATTR_AUTH_TYPE, authParam.authType);
230 IF_FALSE_LOGE_AND_RETURN_VAL(setAuthTypeRet, false);
231
232 bool setAuthTrustLevelRet = attr.SetInt32Value(Attributes::ATTR_AUTH_TRUST_LEVEL, authParam.authTrustLevel);
233 IF_FALSE_LOGE_AND_RETURN_VAL(setAuthTrustLevelRet, false);
234 return true;
235 }
236
DecodeAuthParam(const Attributes & attr,AuthParamInner & authParam)237 bool RemoteMsgUtil::DecodeAuthParam(const Attributes &attr, AuthParamInner &authParam)
238 {
239 bool getUserIdRet = attr.GetInt32Value(Attributes::ATTR_USER_ID, authParam.userId);
240 IF_FALSE_LOGE_AND_RETURN_VAL(getUserIdRet, false);
241
242 bool getChallengeRet = attr.GetUint8ArrayValue(Attributes::ATTR_CHALLENGE, authParam.challenge);
243 IF_FALSE_LOGE_AND_RETURN_VAL(getChallengeRet, false);
244
245 int32_t authType;
246 bool getAuthTypeRet = attr.GetInt32Value(Attributes::ATTR_AUTH_TYPE, authType);
247 IF_FALSE_LOGE_AND_RETURN_VAL(getAuthTypeRet, false);
248 authParam.authType = static_cast<AuthType>(authType);
249
250 int32_t authTrustLevel;
251 bool getAuthTrustLevelRet = attr.GetInt32Value(Attributes::ATTR_AUTH_TRUST_LEVEL, authTrustLevel);
252 IF_FALSE_LOGE_AND_RETURN_VAL(getAuthTrustLevelRet, false);
253 authParam.authTrustLevel = static_cast<AuthTrustLevel>(authTrustLevel);
254
255 return true;
256 }
257 } // namespace UserAuth
258 } // namespace UserIam
259 } // namespace OHOS