1 /*
2 * Copyright (c) 2021 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 #include "storage_daemon_client.h"
16
17 #include <chrono>
18 #include <ctime>
19 #include <thread>
20
21 #include "iremote_object.h"
22 #include "iremote_proxy.h"
23 #include "iservice_registry.h"
24 #include "storage_service_log.h"
25 #include "system_ability_definition.h"
26
27 namespace {
28 constexpr uint32_t HUKS_SERVICE_SHIFT = 0;
29 constexpr uint32_t STORAGE_DAEMON_SFIFT = 1;
30 constexpr uint32_t CHECK_SERVICE_TIMES = 1000;
31 constexpr uint32_t SLEEP_TIME_PRE_CHECK = 10; // 10ms
32 constexpr uint32_t HUKS_SERVICE_FLAG = (1 << HUKS_SERVICE_SHIFT);
33 constexpr uint32_t STORAGE_SERVICE_FLAG = (1 << STORAGE_DAEMON_SFIFT);
34 constexpr int32_t HUKS_SERVICE_SAID = 3510;
35 constexpr int32_t STORAGE_DAEMON_SAID = OHOS::STORAGE_MANAGER_DAEMON_ID;
36 }
37
38 namespace OHOS {
39 namespace StorageDaemon {
GetStorageDaemonProxy(void)40 sptr<IStorageDaemon> StorageDaemonClient::GetStorageDaemonProxy(void)
41 {
42 auto samgr = OHOS::SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
43 if (samgr == nullptr) {
44 LOGE("samgr empty error");
45 return nullptr;
46 }
47
48 sptr<IRemoteObject> object = samgr->GetSystemAbility(OHOS::STORAGE_MANAGER_DAEMON_ID);
49 if (object == nullptr) {
50 LOGE("storage daemon client samgr ablity empty error");
51 return nullptr;
52 }
53
54 return iface_cast<IStorageDaemon>(object);
55 }
56
CheckServiceStatus(uint32_t serviceFlags)57 bool StorageDaemonClient::CheckServiceStatus(uint32_t serviceFlags)
58 {
59 LOGI("CheckServiceStatus start");
60 auto samgr = OHOS::SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
61 if (samgr == nullptr) {
62 LOGE("samgr empty error");
63 return false;
64 }
65
66 bool exist = false;
67 if ((serviceFlags & HUKS_SERVICE_FLAG) == HUKS_SERVICE_FLAG) {
68 for (uint32_t i = 0; i < CHECK_SERVICE_TIMES; i++) {
69 auto object = samgr->CheckSystemAbility(HUKS_SERVICE_SAID, exist);
70 if (object != nullptr) {
71 break;
72 }
73 std::this_thread::sleep_for(std::chrono::milliseconds(SLEEP_TIME_PRE_CHECK));
74 }
75 if (exist == false) {
76 LOGE("huks service system ability error");
77 return false;
78 }
79 }
80
81 exist = false;
82 if ((serviceFlags & STORAGE_SERVICE_FLAG) == STORAGE_SERVICE_FLAG) {
83 for (uint32_t i = 0; i < CHECK_SERVICE_TIMES; i++) {
84 auto object = samgr->CheckSystemAbility(STORAGE_DAEMON_SAID, exist);
85 if (object != nullptr) {
86 break;
87 }
88 std::this_thread::sleep_for(std::chrono::milliseconds(SLEEP_TIME_PRE_CHECK));
89 }
90 if (exist == false) {
91 LOGE("storage daemon service system ability error");
92 return false;
93 }
94 }
95 LOGI("CheckServiceStatus end, success");
96
97 return true;
98 }
99
StartUser(int32_t userId)100 int32_t StorageDaemonClient::StartUser(int32_t userId)
101 {
102 if (!CheckServiceStatus(STORAGE_SERVICE_FLAG)) {
103 LOGE("service check failed");
104 return -EAGAIN;
105 }
106
107 sptr<IStorageDaemon> client = GetStorageDaemonProxy();
108 if (client == nullptr) {
109 LOGE("get storage daemon service failed");
110 return -EAGAIN;
111 }
112
113 return client->StartUser(userId);
114 }
115
StopUser(int32_t userId)116 int32_t StorageDaemonClient::StopUser(int32_t userId)
117 {
118 if (!CheckServiceStatus(STORAGE_SERVICE_FLAG)) {
119 LOGE("service check failed");
120 return -EAGAIN;
121 }
122
123 sptr<IStorageDaemon> client = GetStorageDaemonProxy();
124 if (client == nullptr) {
125 LOGE("get storage daemon service failed");
126 return -EAGAIN;
127 }
128
129 return client->StopUser(userId);
130 }
131
PrepareUserSpace(uint32_t userId,const std::string & volumId,uint32_t flags)132 int32_t StorageDaemonClient::PrepareUserSpace(uint32_t userId, const std::string &volumId, uint32_t flags)
133 {
134 if (!CheckServiceStatus(HUKS_SERVICE_FLAG | STORAGE_SERVICE_FLAG)) {
135 LOGE("service check failed");
136 return -EAGAIN;
137 }
138
139 sptr<IStorageDaemon> client = GetStorageDaemonProxy();
140 if (client == nullptr) {
141 LOGE("get storage daemon service failed");
142 return -EAGAIN;
143 }
144
145 return client->PrepareUserDirs(userId, flags);
146 }
147
DestroyUserSpace(uint32_t userId,const std::string & volumId,uint32_t flags)148 int32_t StorageDaemonClient::DestroyUserSpace(uint32_t userId, const std::string &volumId, uint32_t flags)
149 {
150 if (!CheckServiceStatus(STORAGE_SERVICE_FLAG)) {
151 LOGE("service check failed");
152 return -EAGAIN;
153 }
154
155 sptr<IStorageDaemon> client = GetStorageDaemonProxy();
156 if (client == nullptr) {
157 LOGE("get storage daemon service failed");
158 return -EAGAIN;
159 }
160
161 return client->DestroyUserDirs(userId, flags);
162 }
163
InitGlobalKey(void)164 int32_t StorageDaemonClient::InitGlobalKey(void)
165 {
166 if (!CheckServiceStatus(HUKS_SERVICE_FLAG | STORAGE_SERVICE_FLAG)) {
167 LOGE("service check failed");
168 return -EAGAIN;
169 }
170
171 sptr<IStorageDaemon> client = GetStorageDaemonProxy();
172 if (client == nullptr) {
173 LOGE("get storage daemon service failed");
174 return -EAGAIN;
175 }
176
177 return client->InitGlobalKey();
178 }
179
InitGlobalUserKeys(void)180 int32_t StorageDaemonClient::InitGlobalUserKeys(void)
181 {
182 if (!CheckServiceStatus(HUKS_SERVICE_FLAG | STORAGE_SERVICE_FLAG)) {
183 LOGE("service check failed");
184 return -EAGAIN;
185 }
186
187 sptr<IStorageDaemon> client = GetStorageDaemonProxy();
188 if (client == nullptr) {
189 LOGE("get storage daemon service failed");
190 return -EAGAIN;
191 }
192
193 return client->InitGlobalUserKeys();
194 }
195
GenerateUserKeys(uint32_t userId,uint32_t flags)196 int32_t StorageDaemonClient::GenerateUserKeys(uint32_t userId, uint32_t flags)
197 {
198 if (!CheckServiceStatus(HUKS_SERVICE_FLAG | STORAGE_SERVICE_FLAG)) {
199 LOGE("service check failed");
200 return -EAGAIN;
201 }
202
203 sptr<IStorageDaemon> client = GetStorageDaemonProxy();
204 if (client == nullptr) {
205 LOGE("get storage daemon service failed");
206 return -EAGAIN;
207 }
208
209 return client->GenerateUserKeys(userId, flags);
210 }
211
DeleteUserKeys(uint32_t userId)212 int32_t StorageDaemonClient::DeleteUserKeys(uint32_t userId)
213 {
214 if (!CheckServiceStatus(HUKS_SERVICE_FLAG | STORAGE_SERVICE_FLAG)) {
215 LOGE("service check failed");
216 return -EAGAIN;
217 }
218
219 sptr<IStorageDaemon> client = GetStorageDaemonProxy();
220 if (client == nullptr) {
221 LOGE("get storage daemon service failed");
222 return -EAGAIN;
223 }
224
225 return client->DeleteUserKeys(userId);
226 }
227
UpdateUserAuth(uint32_t userId,std::string auth,std::string compSecret)228 int32_t StorageDaemonClient::UpdateUserAuth(uint32_t userId, std::string auth, std::string compSecret)
229 {
230 if (!CheckServiceStatus(HUKS_SERVICE_FLAG | STORAGE_SERVICE_FLAG)) {
231 LOGE("service check failed");
232 return -EAGAIN;
233 }
234
235 sptr<IStorageDaemon> client = GetStorageDaemonProxy();
236 if (client == nullptr) {
237 LOGE("get storage daemon service failed");
238 return -EAGAIN;
239 }
240
241 return client->UpdateUserAuth(userId, auth, compSecret);
242 }
243
ActiveUserKey(uint32_t userId,std::string auth,std::string compSecret)244 int32_t StorageDaemonClient::ActiveUserKey(uint32_t userId, std::string auth, std::string compSecret)
245 {
246 if (!CheckServiceStatus(HUKS_SERVICE_FLAG | STORAGE_SERVICE_FLAG)) {
247 LOGE("service check failed");
248 return -EAGAIN;
249 }
250
251 sptr<IStorageDaemon> client = GetStorageDaemonProxy();
252 if (client == nullptr) {
253 LOGE("get storage daemon service failed");
254 return -EAGAIN;
255 }
256
257 return client->ActiveUserKey(userId, auth, compSecret);
258 }
259
InactiveUserKey(uint32_t userId)260 int32_t StorageDaemonClient::InactiveUserKey(uint32_t userId)
261 {
262 if (!CheckServiceStatus(HUKS_SERVICE_FLAG | STORAGE_SERVICE_FLAG)) {
263 LOGE("service check failed");
264 return -EAGAIN;
265 }
266
267 sptr<IStorageDaemon> client = GetStorageDaemonProxy();
268 if (client == nullptr) {
269 LOGE("get storage daemon service failed");
270 return -EAGAIN;
271 }
272
273 return client->InactiveUserKey(userId);
274 }
275
FscryptEnable(const std::string & fscryptOptions)276 int32_t StorageDaemonClient::FscryptEnable(const std::string &fscryptOptions)
277 {
278 int ret = OHOS::StorageDaemon::KeyCtrl::SetFscryptSyspara(fscryptOptions);
279 if (ret) {
280 LOGE("Init fscrypt policy failed ret %{public}d", ret);
281 return ret;
282 }
283
284 return 0;
285 }
286 } // namespace StorageDaemon
287 } // namespace OHOS
288