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