• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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(std::vector<std::shared_ptr<UserInfoInterface>> & userInfos)181 int32_t UserIdmDatabaseImpl::GetAllExtUserInfo(std::vector<std::shared_ptr<UserInfoInterface>> &userInfos)
182 {
183     auto hdi = HdiWrapper::GetHdiInstance();
184     if (hdi == nullptr) {
185         IAM_LOGE("bad hdi");
186         return INVALID_HDI_INTERFACE;
187     }
188 
189     std::vector<ExtUserInfo> hdiUserInfos;
190     int32_t ret = hdi->GetAllExtUserInfo(hdiUserInfos);
191     if (ret != HDF_SUCCESS) {
192         IAM_LOGE("GetAllExtUserInfo failed, error code :%{public}d", ret);
193         return ret;
194     }
195 
196     for (const auto &iter : hdiUserInfos) {
197         auto userInfo = Common::MakeShared<UserInfoImpl>(iter.userId, iter.userInfo);
198         if (userInfo == nullptr) {
199             IAM_LOGE("bad alloc");
200             return GENERAL_ERROR;
201         }
202         userInfos.emplace_back(userInfo);
203     }
204 
205     return SUCCESS;
206 }
207 
GetCredentialInfoById(uint64_t credentialId,std::shared_ptr<CredentialInfoInterface> & credInfo)208 int32_t UserIdmDatabaseImpl::GetCredentialInfoById(uint64_t credentialId,
209     std::shared_ptr<CredentialInfoInterface> &credInfo)
210 {
211     auto hdi = HdiWrapper::GetHdiInstance();
212     if (hdi == nullptr) {
213         IAM_LOGE("bad hdi");
214         return GENERAL_ERROR;
215     }
216 
217     HdiCredentialInfo hdiInfo;
218     int32_t ret = hdi->GetCredentialById(credentialId, hdiInfo);
219     if (ret != HDF_SUCCESS) {
220         IAM_LOGE("GetCredentialById failed, error code : %{public}d", ret);
221         return ret;
222     }
223 
224     credInfo = Common::MakeShared<CredentialInfoImpl>(0, hdiInfo);
225     if (credInfo == nullptr) {
226         IAM_LOGE("bad alloc");
227         return GENERAL_ERROR;
228     }
229 
230     return SUCCESS;
231 }
232 
Instance()233 UserIdmDatabase &UserIdmDatabase::Instance()
234 {
235     return UserIdmDatabaseImpl::GetInstance();
236 }
237 } // namespace UserAuth
238 } // namespace UserIam
239 } // namespace OHOS