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 IAM_LOGI("send msg to ****%{public}hx begin", static_cast<uint16_t>(executorIndex));
66 auto resourceNode = ResourceNodePool::Instance().Select(executorIndex).lock();
67 if (resourceNode == nullptr) {
68 IAM_LOGE("failed to find ****%{public}hx", static_cast<uint16_t>(executorIndex));
69 return;
70 }
71 Attributes properties;
72 // In current version, msg type is not set, temporary use PROPER_MODE_FREEZE
73 bool setAuthPropertyModeRet =
74 properties.SetInt32Value(UserIam::UserAuth::Attributes::ATTR_PROPERTY_MODE, commandId);
75 IF_FALSE_LOGE_AND_RETURN(setAuthPropertyModeRet == true);
76 bool setExtraInfoRet = properties.SetUint8ArrayValue(UserIam::UserAuth::Attributes::ATTR_EXTRA_INFO, msg);
77 IF_FALSE_LOGE_AND_RETURN(setExtraInfoRet == true);
78 int32_t ret = resourceNode->SetProperty(properties);
79 if (ret != SUCCESS) {
80 IAM_LOGE("failed to set property to ****%{public}hx", static_cast<uint16_t>(executorIndex));
81 return;
82 }
83 IAM_LOGI("send msg to ****%{public}hx success", static_cast<uint16_t>(executorIndex));
84 }
85
SetCachedTemplates(uint64_t executorIndex,const std::vector<std::shared_ptr<CredentialInfoInterface>> & infos)86 void ResourceNodeUtils::SetCachedTemplates(uint64_t executorIndex,
87 const std::vector<std::shared_ptr<CredentialInfoInterface>> &infos)
88 {
89 IAM_LOGI("start");
90 auto resourceNode = ResourceNodePool::Instance().Select(executorIndex).lock();
91 if (resourceNode == nullptr) {
92 IAM_LOGE("resourceNode is nullptr");
93 return;
94 }
95
96 std::vector<uint64_t> templateIds;
97 for (auto &info : infos) {
98 if (info == nullptr) {
99 IAM_LOGE("info is null");
100 continue;
101 }
102 templateIds.push_back(info->GetTemplateId());
103 }
104
105 Attributes attr;
106 attr.SetUint32Value(Attributes::ATTR_PROPERTY_MODE, PROPERTY_MODE_SET_CACHED_TEMPLATES);
107 attr.SetUint64ArrayValue(Attributes::ATTR_TEMPLATE_ID_LIST, templateIds);
108
109 int32_t result = resourceNode->SetProperty(attr);
110 if (result != SUCCESS) {
111 IAM_LOGE("set property failed, result = %{public}d", result);
112 return;
113 }
114
115 IAM_LOGI("success");
116 }
117
ClassifyCredInfoByExecutor(const std::vector<std::shared_ptr<CredentialInfoInterface>> & in,std::map<uint64_t,std::vector<std::shared_ptr<CredentialInfoInterface>>> & out)118 ResultCode ResourceNodeUtils::ClassifyCredInfoByExecutor(
119 const std::vector<std::shared_ptr<CredentialInfoInterface>> &in,
120 std::map<uint64_t, std::vector<std::shared_ptr<CredentialInfoInterface>>> &out)
121 {
122 for (auto &cred : in) {
123 if (cred == nullptr) {
124 IAM_LOGE("cred is null");
125 return GENERAL_ERROR;
126 }
127 uint64_t executorIndex = cred->GetExecutorIndex();
128 if (out.find(executorIndex) == out.end()) {
129 out[executorIndex] = std::vector<std::shared_ptr<CredentialInfoInterface>>();
130 }
131 out[executorIndex].push_back(cred);
132 }
133 return SUCCESS;
134 }
135 } // namespace UserAuth
136 } // namespace UserIam
137 } // namespace OHOS