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 "user_idm_database_impl.h"
17
18 #include "securec.h"
19
20 #include "attributes.h"
21 #include "enrolled_info_impl.h"
22 #include "hdi_wrapper.h"
23 #include "iam_logger.h"
24 #include "iam_ptr.h"
25 #include "iam_hitrace_helper.h"
26 #include "iam_common_defines.h"
27
28 #define LOG_LABEL UserIam::Common::LABEL_USER_AUTH_SA
29
30 namespace OHOS {
31 namespace UserIam {
32 namespace UserAuth {
GetSecUserInfo(int32_t userId)33 std::shared_ptr<SecureUserInfoInterface> UserIdmDatabaseImpl::GetSecUserInfo(int32_t userId)
34 {
35 auto hdi = HdiWrapper::GetHdiInstance();
36 if (hdi == nullptr) {
37 IAM_LOGE("bad hdi");
38 return nullptr;
39 }
40
41 std::vector<HdiEnrolledInfo> enrolledInfoVector;
42 uint64_t secureUid = 0;
43 HdiPinSubType pinSubType;
44 int32_t ret = hdi->GetUserInfo(userId, secureUid, pinSubType, enrolledInfoVector);
45 if (ret != HDF_SUCCESS) {
46 IAM_LOGE("GetSecureInfo failed, error code : %{public}d", ret);
47 return nullptr;
48 }
49
50 std::vector<std::shared_ptr<EnrolledInfoInterface>> infoRet;
51 infoRet.reserve(enrolledInfoVector.size());
52
53 for (auto const &info : enrolledInfoVector) {
54 auto enrolledInfo = Common::MakeShared<EnrolledInfoImpl>(userId, info);
55 if (enrolledInfo == nullptr) {
56 IAM_LOGE("bad alloc");
57 return nullptr;
58 }
59 infoRet.emplace_back(enrolledInfo);
60 }
61 auto secInfoRet = Common::MakeShared<SecureUserInfoImpl>(userId,
62 static_cast<PinSubType>(pinSubType), secureUid, infoRet);
63 if (secInfoRet == nullptr) {
64 IAM_LOGE("bad alloc");
65 return nullptr;
66 }
67 return secInfoRet;
68 }
69
GetCredentialInfo(int32_t userId,AuthType authType)70 std::vector<std::shared_ptr<CredentialInfoInterface>> UserIdmDatabaseImpl::GetCredentialInfo(int32_t userId,
71 AuthType authType)
72 {
73 std::vector<std::shared_ptr<CredentialInfoInterface>> infoRet;
74 auto hdi = HdiWrapper::GetHdiInstance();
75 if (hdi == nullptr) {
76 IAM_LOGE("bad hdi");
77 return infoRet;
78 }
79
80 std::vector<HdiCredentialInfo> hdiInfos;
81 int32_t ret = hdi->GetCredential(userId, static_cast<HdiAuthType>(authType), hdiInfos);
82 if (ret != HDF_SUCCESS) {
83 IAM_LOGE("GetCredential failed, error code : %{public}d", ret);
84 return infoRet;
85 }
86 infoRet.reserve(hdiInfos.size());
87 for (const auto &hdiInfo : hdiInfos) {
88 auto info = Common::MakeShared<CredentialInfoImpl>(userId, hdiInfo);
89 if (info == nullptr) {
90 IAM_LOGE("bad alloc");
91 return infoRet;
92 }
93 infoRet.emplace_back(info);
94 }
95
96 return infoRet;
97 }
98
DeleteCredentialInfo(int32_t userId,uint64_t credentialId,const std::vector<uint8_t> & authToken,std::shared_ptr<CredentialInfoInterface> & credInfo)99 int32_t UserIdmDatabaseImpl::DeleteCredentialInfo(int32_t userId, uint64_t credentialId,
100 const std::vector<uint8_t> &authToken, std::shared_ptr<CredentialInfoInterface> &credInfo)
101 {
102 auto hdi = HdiWrapper::GetHdiInstance();
103 if (hdi == nullptr) {
104 IAM_LOGE("bad hdi");
105 return INVALID_HDI_INTERFACE;
106 }
107
108 HdiCredentialInfo hdiInfo = {};
109 IamHitraceHelper traceHelper("hdi DeleteCredential");
110 int32_t ret = hdi->DeleteCredential(userId, credentialId, authToken, hdiInfo);
111 if (ret != HDF_SUCCESS) {
112 IAM_LOGE("failed to delete credential, error code : %{public}d", ret);
113 return ret;
114 }
115
116 auto info = Common::MakeShared<CredentialInfoImpl>(userId, hdiInfo);
117 if (info == nullptr) {
118 IAM_LOGE("bad alloc");
119 return GENERAL_ERROR;
120 }
121 credInfo = info;
122 return SUCCESS;
123 }
124
DeleteUser(int32_t userId,const std::vector<uint8_t> & authToken,std::vector<std::shared_ptr<CredentialInfoInterface>> & credInfos)125 int32_t UserIdmDatabaseImpl::DeleteUser(int32_t userId, const std::vector<uint8_t> &authToken,
126 std::vector<std::shared_ptr<CredentialInfoInterface>> &credInfos)
127 {
128 auto hdi = HdiWrapper::GetHdiInstance();
129 if (hdi == nullptr) {
130 IAM_LOGE("bad hdi");
131 return INVALID_HDI_INTERFACE;
132 }
133
134 std::vector<HdiCredentialInfo> hdiInfos;
135 IamHitraceHelper traceHelper("hdi DeleteUser");
136 int32_t ret = hdi->DeleteUser(userId, authToken, hdiInfos);
137 if (ret != HDF_SUCCESS) {
138 IAM_LOGE("failed to delete user, error code : %{public}d", ret);
139 return ret;
140 }
141
142 for (const auto &hdiInfo : hdiInfos) {
143 auto infoRet = Common::MakeShared<CredentialInfoImpl>(userId, hdiInfo);
144 if (infoRet == nullptr) {
145 IAM_LOGE("bad alloc");
146 return GENERAL_ERROR;
147 }
148 credInfos.emplace_back(infoRet);
149 }
150
151 return SUCCESS;
152 }
153
DeleteUserEnforce(int32_t userId,std::vector<std::shared_ptr<CredentialInfoInterface>> & credInfos)154 int32_t UserIdmDatabaseImpl::DeleteUserEnforce(int32_t userId,
155 std::vector<std::shared_ptr<CredentialInfoInterface>> &credInfos)
156 {
157 auto hdi = HdiWrapper::GetHdiInstance();
158 if (hdi == nullptr) {
159 IAM_LOGE("bad hdi");
160 return INVALID_HDI_INTERFACE;
161 }
162
163 std::vector<HdiCredentialInfo> hdiInfos;
164 IamHitraceHelper traceHelper("hdi EnforceDeleteUser");
165 int32_t ret = hdi->EnforceDeleteUser(userId, hdiInfos);
166 if (ret != HDF_SUCCESS) {
167 IAM_LOGE("failed to enforce delete user, error code : %{public}d", ret);
168 return ret;
169 }
170
171 for (const auto &hdiInfo : hdiInfos) {
172 auto infoRet = Common::MakeShared<CredentialInfoImpl>(userId, hdiInfo);
173 if (infoRet == nullptr) {
174 IAM_LOGE("bad alloc");
175 return GENERAL_ERROR;
176 }
177 credInfos.emplace_back(infoRet);
178 }
179
180 return SUCCESS;
181 }
182
GetAllExtUserInfo()183 std::vector<std::shared_ptr<UserInfoInterface>> UserIdmDatabaseImpl::GetAllExtUserInfo()
184 {
185 std::vector<std::shared_ptr<UserInfoInterface>> infoRet;
186
187 auto hdi = HdiWrapper::GetHdiInstance();
188 if (hdi == nullptr) {
189 IAM_LOGE("bad hdi");
190 return infoRet;
191 }
192
193 std::vector<ExtUserInfo> userInfos;
194 int32_t ret = hdi->GetAllExtUserInfo(userInfos);
195 if (ret != HDF_SUCCESS) {
196 IAM_LOGE("GetAllExtUserInfo failed, error code :%{public}d", ret);
197 return infoRet;
198 }
199
200 for (const auto &iter : userInfos) {
201 auto userInfo = Common::MakeShared<UserInfoImpl>(iter.userId, iter.userInfo);
202 if (userInfo == nullptr) {
203 IAM_LOGE("bad alloc");
204 return infoRet;
205 }
206 infoRet.emplace_back(userInfo);
207 }
208
209 return infoRet;
210 }
211
Instance()212 UserIdmDatabase &UserIdmDatabase::Instance()
213 {
214 return UserIdmDatabaseImpl::GetInstance();
215 }
216 } // namespace UserAuth
217 } // namespace UserIam
218 } // namespace OHOS