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