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