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 "resource_node_utils.h"
17
18 #include "iam_check.h"
19 #include "iam_hitrace_helper.h"
20 #include "iam_logger.h"
21 #include "resource_node_pool.h"
22
23 #define LOG_LABEL UserIam::Common::LABEL_USER_AUTH_SA
24
25 namespace OHOS {
26 namespace UserIam {
27 namespace UserAuth {
NotifyExecutorToDeleteTemplates(const std::vector<std::shared_ptr<CredentialInfoInterface>> & infos,std::string changeReasonTrace)28 int32_t ResourceNodeUtils::NotifyExecutorToDeleteTemplates(
29 const std::vector<std::shared_ptr<CredentialInfoInterface>> &infos, std::string changeReasonTrace)
30 {
31 if (infos.size() == 0) {
32 IAM_LOGE("bad infos, infos size is 0");
33 return INVALID_PARAMETERS;
34 }
35
36 for (const auto &info : infos) {
37 uint64_t executorIndex = info->GetExecutorIndex();
38
39 auto resourceNode = ResourceNodePool::Instance().Select(executorIndex).lock();
40 if (resourceNode == nullptr) {
41 IAM_LOGE("failed to find ****%{public}hx", static_cast<uint16_t>(executorIndex));
42 continue;
43 }
44 Attributes properties;
45 properties.SetUint32Value(Attributes::ATTR_PROPERTY_MODE, PROPERTY_MODE_DEL);
46 properties.SetUint64Value(Attributes::ATTR_TEMPLATE_ID, info->GetTemplateId());
47 properties.SetStringValue(Attributes::ATTR_TEMPLATE_CHANGE_REASON, changeReasonTrace);
48 IamHitraceHelper traceHelper("NotifyExecutorToDeleteTemplates");
49 int32_t ret = resourceNode->SetProperty(properties);
50 if (ret != SUCCESS) {
51 IAM_LOGE("failed to set property to ****%{public}hx", static_cast<uint16_t>(executorIndex));
52 }
53 }
54
55 return SUCCESS;
56 }
57
SendMsgToExecutor(uint64_t executorIndex,int32_t commandId,const std::vector<uint8_t> & msg)58 void ResourceNodeUtils::SendMsgToExecutor(uint64_t executorIndex, int32_t commandId, const std::vector<uint8_t> &msg)
59 {
60 auto resourceNode = ResourceNodePool::Instance().Select(executorIndex).lock();
61 if (resourceNode == nullptr) {
62 IAM_LOGE("failed to find ****%{public}hx", static_cast<uint16_t>(executorIndex));
63 return;
64 }
65 Attributes properties;
66 // In current version, msg type is not set, temporary use PROPER_MODE_FREEZE
67 bool setAuthPropertyModeRet =
68 properties.SetInt32Value(UserIam::UserAuth::Attributes::ATTR_PROPERTY_MODE, commandId);
69 IF_FALSE_LOGE_AND_RETURN(setAuthPropertyModeRet == true);
70 bool setExtraInfoRet = properties.SetUint8ArrayValue(UserIam::UserAuth::Attributes::ATTR_EXTRA_INFO, msg);
71 IF_FALSE_LOGE_AND_RETURN(setExtraInfoRet == true);
72 int32_t ret = resourceNode->SetProperty(properties);
73 if (ret != SUCCESS) {
74 IAM_LOGE("failed to set property to ****%{public}hx", static_cast<uint16_t>(executorIndex));
75 return;
76 }
77 IAM_LOGI("send msg to ****%{public}hx success", static_cast<uint16_t>(executorIndex));
78 }
79
SetCachedTemplates(uint64_t executorIndex,const std::vector<std::shared_ptr<CredentialInfoInterface>> & infos)80 void ResourceNodeUtils::SetCachedTemplates(uint64_t executorIndex,
81 const std::vector<std::shared_ptr<CredentialInfoInterface>> &infos)
82 {
83 IAM_LOGI("start");
84 auto resourceNode = ResourceNodePool::Instance().Select(executorIndex).lock();
85 if (resourceNode == nullptr) {
86 IAM_LOGE("resourceNode is nullptr");
87 return;
88 }
89
90 std::vector<uint64_t> templateIds;
91 for (auto &info : infos) {
92 templateIds.push_back(info->GetTemplateId());
93 }
94
95 Attributes attr;
96 attr.SetUint32Value(Attributes::ATTR_PROPERTY_MODE, PROPERTY_MODE_SET_CACHED_TEMPLATES);
97 attr.SetUint64ArrayValue(Attributes::ATTR_TEMPLATE_ID_LIST, templateIds);
98
99 int32_t result = resourceNode->SetProperty(attr);
100 if (result != SUCCESS) {
101 IAM_LOGE("set property failed, result = %{public}d", result);
102 return;
103 }
104
105 IAM_LOGI("success");
106 }
107
ClassifyCredInfoByExecutor(const std::vector<std::shared_ptr<CredentialInfoInterface>> & in,std::map<uint64_t,std::vector<std::shared_ptr<CredentialInfoInterface>>> & out)108 ResultCode ResourceNodeUtils::ClassifyCredInfoByExecutor(
109 const std::vector<std::shared_ptr<CredentialInfoInterface>> &in,
110 std::map<uint64_t, std::vector<std::shared_ptr<CredentialInfoInterface>>> &out)
111 {
112 for (auto &cred : in) {
113 if (cred == nullptr) {
114 IAM_LOGE("cred is null");
115 return GENERAL_ERROR;
116 }
117 uint64_t executorIndex = cred->GetExecutorIndex();
118 if (out.find(executorIndex) == out.end()) {
119 out[executorIndex] = std::vector<std::shared_ptr<CredentialInfoInterface>>();
120 }
121 out[executorIndex].push_back(cred);
122 }
123 return SUCCESS;
124 }
125 } // namespace UserAuth
126 } // namespace UserIam
127 } // namespace OHOS