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