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_TAG "USER_AUTH_SA"
29
30 namespace OHOS {
31 namespace UserIam {
32 namespace UserAuth {
GetSecUserInfo(int32_t userId,std::shared_ptr<SecureUserInfoInterface> & secUserInfo)33 int32_t UserIdmDatabaseImpl::GetSecUserInfo(int32_t userId, std::shared_ptr<SecureUserInfoInterface> &secUserInfo)
34 {
35 auto hdi = HdiWrapper::GetHdiInstance();
36 if (hdi == nullptr) {
37 IAM_LOGE("bad hdi");
38 return GENERAL_ERROR;
39 }
40
41 std::vector<HdiEnrolledInfo> enrolledInfoVector;
42 uint64_t secureUid = 0;
43 int32_t 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 GENERAL_ERROR;
48 }
49
50 if (enrolledInfoVector.size() == 0) {
51 IAM_LOGE("secUserInfo not found");
52 return NOT_ENROLLED;
53 }
54
55 std::vector<std::shared_ptr<EnrolledInfoInterface>> infoRet;
56 infoRet.reserve(enrolledInfoVector.size());
57
58 for (auto const &info : enrolledInfoVector) {
59 auto enrolledInfo = Common::MakeShared<EnrolledInfoImpl>(userId, info);
60 if (enrolledInfo == nullptr) {
61 IAM_LOGE("bad alloc");
62 return GENERAL_ERROR;
63 }
64 infoRet.emplace_back(enrolledInfo);
65 }
66 secUserInfo = Common::MakeShared<SecureUserInfoImpl>(userId, secureUid, infoRet);
67 if (secUserInfo == nullptr) {
68 IAM_LOGE("bad alloc");
69 return GENERAL_ERROR;
70 }
71 return SUCCESS;
72 }
73
GetCredentialInfo(int32_t userId,AuthType authType,std::vector<std::shared_ptr<CredentialInfoInterface>> & credInfos)74 int32_t UserIdmDatabaseImpl::GetCredentialInfo(int32_t userId, AuthType authType,
75 std::vector<std::shared_ptr<CredentialInfoInterface>> &credInfos)
76 {
77 auto hdi = HdiWrapper::GetHdiInstance();
78 if (hdi == nullptr) {
79 IAM_LOGE("bad hdi");
80 return INVALID_HDI_INTERFACE;
81 }
82
83 std::vector<HdiCredentialInfo> hdiInfos;
84 int32_t ret = hdi->GetCredential(userId, static_cast<HdiAuthType>(authType), hdiInfos);
85 if (ret != HDF_SUCCESS) {
86 IAM_LOGE("GetCredential fail, userId:%{public}d, authType:%{public}d, ret:%{public}d", userId, authType, ret);
87 return GENERAL_ERROR;
88 }
89 credInfos.reserve(hdiInfos.size());
90 for (const auto &hdiInfo : hdiInfos) {
91 auto info = Common::MakeShared<CredentialInfoImpl>(userId, hdiInfo);
92 if (info == nullptr) {
93 IAM_LOGE("bad alloc");
94 return GENERAL_ERROR;
95 }
96 credInfos.emplace_back(info);
97 }
98
99 return SUCCESS;
100 }
101
DeleteUser(int32_t userId,const std::vector<uint8_t> & authToken,std::vector<std::shared_ptr<CredentialInfoInterface>> & credInfos,std::vector<uint8_t> & rootSecret)102 int32_t UserIdmDatabaseImpl::DeleteUser(int32_t userId, const std::vector<uint8_t> &authToken,
103 std::vector<std::shared_ptr<CredentialInfoInterface>> &credInfos, std::vector<uint8_t> &rootSecret)
104 {
105 auto hdi = HdiWrapper::GetHdiInstance();
106 if (hdi == nullptr) {
107 IAM_LOGE("bad hdi");
108 return INVALID_HDI_INTERFACE;
109 }
110
111 std::vector<HdiCredentialInfo> hdiInfos;
112 IamHitraceHelper traceHelper("hdi DeleteUser");
113 int32_t ret = hdi->DeleteUser(userId, authToken, hdiInfos, rootSecret);
114 if (ret != HDF_SUCCESS) {
115 IAM_LOGE("failed to delete user, error code : %{public}d", ret);
116 return ret;
117 }
118
119 for (const auto &hdiInfo : hdiInfos) {
120 auto infoRet = Common::MakeShared<CredentialInfoImpl>(userId, hdiInfo);
121 if (infoRet == nullptr) {
122 IAM_LOGE("bad alloc");
123 return GENERAL_ERROR;
124 }
125 credInfos.emplace_back(infoRet);
126 }
127
128 return SUCCESS;
129 }
130
DeleteUserEnforce(int32_t userId,std::vector<std::shared_ptr<CredentialInfoInterface>> & credInfos)131 int32_t UserIdmDatabaseImpl::DeleteUserEnforce(int32_t userId,
132 std::vector<std::shared_ptr<CredentialInfoInterface>> &credInfos)
133 {
134 auto hdi = HdiWrapper::GetHdiInstance();
135 if (hdi == nullptr) {
136 IAM_LOGE("bad hdi");
137 return INVALID_HDI_INTERFACE;
138 }
139
140 std::vector<HdiCredentialInfo> hdiInfos;
141 IamHitraceHelper traceHelper("hdi EnforceDeleteUser");
142 int32_t ret = hdi->EnforceDeleteUser(userId, hdiInfos);
143 if (ret != HDF_SUCCESS) {
144 IAM_LOGE("failed to enforce delete user, error code : %{public}d", ret);
145 return ret;
146 }
147
148 for (const auto &hdiInfo : hdiInfos) {
149 auto infoRet = Common::MakeShared<CredentialInfoImpl>(userId, hdiInfo);
150 if (infoRet == nullptr) {
151 IAM_LOGE("bad alloc");
152 return GENERAL_ERROR;
153 }
154 credInfos.emplace_back(infoRet);
155 }
156
157 return SUCCESS;
158 }
159
GetAllExtUserInfo(std::vector<std::shared_ptr<UserInfoInterface>> & userInfos)160 int32_t UserIdmDatabaseImpl::GetAllExtUserInfo(std::vector<std::shared_ptr<UserInfoInterface>> &userInfos)
161 {
162 auto hdi = HdiWrapper::GetHdiInstance();
163 if (hdi == nullptr) {
164 IAM_LOGE("bad hdi");
165 return INVALID_HDI_INTERFACE;
166 }
167
168 std::vector<ExtUserInfo> hdiUserInfos;
169 int32_t ret = hdi->GetAllExtUserInfo(hdiUserInfos);
170 if (ret != HDF_SUCCESS) {
171 IAM_LOGE("GetAllExtUserInfo failed, error code :%{public}d", ret);
172 return ret;
173 }
174
175 for (const auto &iter : hdiUserInfos) {
176 auto userInfo = Common::MakeShared<UserInfoImpl>(iter.userId, iter.userInfo);
177 if (userInfo == nullptr) {
178 IAM_LOGE("bad alloc");
179 return GENERAL_ERROR;
180 }
181 userInfos.emplace_back(userInfo);
182 }
183
184 return SUCCESS;
185 }
186
GetCredentialInfoById(uint64_t credentialId,std::shared_ptr<CredentialInfoInterface> & credInfo)187 int32_t UserIdmDatabaseImpl::GetCredentialInfoById(uint64_t credentialId,
188 std::shared_ptr<CredentialInfoInterface> &credInfo)
189 {
190 auto hdi = HdiWrapper::GetHdiInstance();
191 if (hdi == nullptr) {
192 IAM_LOGE("bad hdi");
193 return GENERAL_ERROR;
194 }
195
196 HdiCredentialInfo hdiInfo;
197 int32_t ret = hdi->GetCredentialById(credentialId, hdiInfo);
198 if (ret != HDF_SUCCESS) {
199 IAM_LOGE("GetCredentialById failed, error code : %{public}d", ret);
200 return ret;
201 }
202
203 credInfo = Common::MakeShared<CredentialInfoImpl>(0, hdiInfo);
204 if (credInfo == nullptr) {
205 IAM_LOGE("bad alloc");
206 return GENERAL_ERROR;
207 }
208
209 return SUCCESS;
210 }
211
ClearUnavailableCredential(int32_t userId,std::vector<std::shared_ptr<CredentialInfoInterface>> & credInfos)212 int32_t UserIdmDatabaseImpl::ClearUnavailableCredential(int32_t userId,
213 std::vector<std::shared_ptr<CredentialInfoInterface>> &credInfos)
214 {
215 auto hdi = HdiWrapper::GetHdiInstance();
216 if (hdi == nullptr) {
217 IAM_LOGE("bad hdi");
218 return GENERAL_ERROR;
219 }
220
221 std::vector<HdiCredentialInfo> hdiInfos;
222 std::vector<int32_t> userIds;
223 userIds.push_back(userId);
224 int32_t ret = hdi->ClearUnavailableCredential(userIds, hdiInfos);
225 if (ret != HDF_SUCCESS) {
226 IAM_LOGE("ClearUnavailableCredential failed, error code : %{public}d", ret);
227 return ret;
228 }
229
230 for (const auto &hdiInfo : hdiInfos) {
231 auto infoRet = Common::MakeShared<CredentialInfoImpl>(userId, hdiInfo);
232 if (infoRet == nullptr) {
233 IAM_LOGE("bad alloc");
234 return GENERAL_ERROR;
235 }
236 credInfos.emplace_back(infoRet);
237 }
238
239 return SUCCESS;
240 }
241
Instance()242 UserIdmDatabase &UserIdmDatabase::Instance()
243 {
244 return UserIdmDatabaseImpl::GetInstance();
245 }
246 } // namespace UserAuth
247 } // namespace UserIam
248 } // namespace OHOS