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 failed, error code : %{public}d", 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
DeleteCredentialInfo(int32_t userId,uint64_t credentialId,const std::vector<uint8_t> & authToken,std::shared_ptr<CredentialInfoInterface> & credInfo)102 int32_t UserIdmDatabaseImpl::DeleteCredentialInfo(int32_t userId, uint64_t credentialId,
103 const std::vector<uint8_t> &authToken, std::shared_ptr<CredentialInfoInterface> &credInfo)
104 {
105 auto hdi = HdiWrapper::GetHdiInstance();
106 if (hdi == nullptr) {
107 IAM_LOGE("bad hdi");
108 return INVALID_HDI_INTERFACE;
109 }
110
111 HdiCredentialInfo hdiInfo = {};
112 IamHitraceHelper traceHelper("hdi DeleteCredential");
113 int32_t ret = hdi->DeleteCredential(userId, credentialId, authToken, hdiInfo);
114 if (ret != HDF_SUCCESS) {
115 IAM_LOGE("failed to delete credential, error code : %{public}d", ret);
116 return ret;
117 }
118
119 auto info = Common::MakeShared<CredentialInfoImpl>(userId, hdiInfo);
120 if (info == nullptr) {
121 IAM_LOGE("bad alloc");
122 return GENERAL_ERROR;
123 }
124 credInfo = info;
125 return SUCCESS;
126 }
127
DeleteUser(int32_t userId,const std::vector<uint8_t> & authToken,std::vector<std::shared_ptr<CredentialInfoInterface>> & credInfos,std::vector<uint8_t> & rootSecret)128 int32_t UserIdmDatabaseImpl::DeleteUser(int32_t userId, const std::vector<uint8_t> &authToken,
129 std::vector<std::shared_ptr<CredentialInfoInterface>> &credInfos, std::vector<uint8_t> &rootSecret)
130 {
131 auto hdi = HdiWrapper::GetHdiInstance();
132 if (hdi == nullptr) {
133 IAM_LOGE("bad hdi");
134 return INVALID_HDI_INTERFACE;
135 }
136
137 std::vector<HdiCredentialInfo> hdiInfos;
138 IamHitraceHelper traceHelper("hdi DeleteUser");
139 int32_t ret = hdi->DeleteUser(userId, authToken, hdiInfos, rootSecret);
140 if (ret != HDF_SUCCESS) {
141 IAM_LOGE("failed to delete user, error code : %{public}d", ret);
142 return ret;
143 }
144
145 for (const auto &hdiInfo : hdiInfos) {
146 auto infoRet = Common::MakeShared<CredentialInfoImpl>(userId, hdiInfo);
147 if (infoRet == nullptr) {
148 IAM_LOGE("bad alloc");
149 return GENERAL_ERROR;
150 }
151 credInfos.emplace_back(infoRet);
152 }
153
154 return SUCCESS;
155 }
156
DeleteUserEnforce(int32_t userId,std::vector<std::shared_ptr<CredentialInfoInterface>> & credInfos)157 int32_t UserIdmDatabaseImpl::DeleteUserEnforce(int32_t userId,
158 std::vector<std::shared_ptr<CredentialInfoInterface>> &credInfos)
159 {
160 auto hdi = HdiWrapper::GetHdiInstance();
161 if (hdi == nullptr) {
162 IAM_LOGE("bad hdi");
163 return INVALID_HDI_INTERFACE;
164 }
165
166 std::vector<HdiCredentialInfo> hdiInfos;
167 IamHitraceHelper traceHelper("hdi EnforceDeleteUser");
168 int32_t ret = hdi->EnforceDeleteUser(userId, hdiInfos);
169 if (ret != HDF_SUCCESS) {
170 IAM_LOGE("failed to enforce delete user, error code : %{public}d", ret);
171 return ret;
172 }
173
174 for (const auto &hdiInfo : hdiInfos) {
175 auto infoRet = Common::MakeShared<CredentialInfoImpl>(userId, hdiInfo);
176 if (infoRet == nullptr) {
177 IAM_LOGE("bad alloc");
178 return GENERAL_ERROR;
179 }
180 credInfos.emplace_back(infoRet);
181 }
182
183 return SUCCESS;
184 }
185
GetAllExtUserInfo(std::vector<std::shared_ptr<UserInfoInterface>> & userInfos)186 int32_t UserIdmDatabaseImpl::GetAllExtUserInfo(std::vector<std::shared_ptr<UserInfoInterface>> &userInfos)
187 {
188 auto hdi = HdiWrapper::GetHdiInstance();
189 if (hdi == nullptr) {
190 IAM_LOGE("bad hdi");
191 return INVALID_HDI_INTERFACE;
192 }
193
194 std::vector<ExtUserInfo> hdiUserInfos;
195 int32_t ret = hdi->GetAllExtUserInfo(hdiUserInfos);
196 if (ret != HDF_SUCCESS) {
197 IAM_LOGE("GetAllExtUserInfo failed, error code :%{public}d", ret);
198 return ret;
199 }
200
201 for (const auto &iter : hdiUserInfos) {
202 auto userInfo = Common::MakeShared<UserInfoImpl>(iter.userId, iter.userInfo);
203 if (userInfo == nullptr) {
204 IAM_LOGE("bad alloc");
205 return GENERAL_ERROR;
206 }
207 userInfos.emplace_back(userInfo);
208 }
209
210 return SUCCESS;
211 }
212
GetCredentialInfoById(uint64_t credentialId,std::shared_ptr<CredentialInfoInterface> & credInfo)213 int32_t UserIdmDatabaseImpl::GetCredentialInfoById(uint64_t credentialId,
214 std::shared_ptr<CredentialInfoInterface> &credInfo)
215 {
216 auto hdi = HdiWrapper::GetHdiInstance();
217 if (hdi == nullptr) {
218 IAM_LOGE("bad hdi");
219 return GENERAL_ERROR;
220 }
221
222 HdiCredentialInfo hdiInfo;
223 int32_t ret = hdi->GetCredentialById(credentialId, hdiInfo);
224 if (ret != HDF_SUCCESS) {
225 IAM_LOGE("GetCredentialById failed, error code : %{public}d", ret);
226 return ret;
227 }
228
229 credInfo = Common::MakeShared<CredentialInfoImpl>(0, hdiInfo);
230 if (credInfo == nullptr) {
231 IAM_LOGE("bad alloc");
232 return GENERAL_ERROR;
233 }
234
235 return SUCCESS;
236 }
237
Instance()238 UserIdmDatabase &UserIdmDatabase::Instance()
239 {
240 return UserIdmDatabaseImpl::GetInstance();
241 }
242 } // namespace UserAuth
243 } // namespace UserIam
244 } // namespace OHOS