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