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