• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021-2024 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 <functional>
17 #include <iostream>
18 #include <map>
19 #include <string>
20 #include <vector>
21 
22 #include "client/storage_manager_client.h"
23 #include "storage_daemon_client.h"
24 #include "storage_service_log.h"
25 #include "utils/file_utils.h"
26 
27 constexpr int32_t ARG_CNT_2 = 2;
28 constexpr size_t INDEX_1 = 1;
29 constexpr size_t INDEX_2 = 2;
30 
31 #ifdef SDC_TEST_ENABLE
32 constexpr int32_t ARG_CNT_4 = 4;
33 constexpr int32_t ARG_CNT_5 = 5;
34 constexpr int32_t ARG_CNT_6 = 6;
35 constexpr size_t INDEX_3 = 3;
36 constexpr size_t INDEX_4 = 4;
37 constexpr size_t INDEX_5 = 5;
38 #endif
39 
InitGlobalKey(const std::vector<std::string> & args)40 static int32_t InitGlobalKey(const std::vector<std::string> &args)
41 {
42     (void)args;
43     return OHOS::StorageDaemon::StorageDaemonClient::InitGlobalKey();
44 }
45 
InitMainUser(const std::vector<std::string> & args)46 static int32_t InitMainUser(const std::vector<std::string> &args)
47 {
48     (void)args;
49     return OHOS::StorageDaemon::StorageDaemonClient::InitGlobalUserKeys();
50 }
51 
52 #ifdef SDC_TEST_ENABLE
GenerateUserKeys(const std::vector<std::string> & args)53 static int32_t GenerateUserKeys(const std::vector<std::string> &args)
54 {
55     if (args.size() < ARG_CNT_5) {
56         LOGE("Parameter nums is less than 5, please retry");
57         return -EINVAL;
58     }
59     uint32_t userId;
60     uint32_t flags;
61     // 3 means take the fourth argument of args, 4 means take the fifth argument of args
62     if ((OHOS::StorageDaemon::StringToUint32(args[INDEX_3], userId) == false) ||
63         (OHOS::StorageDaemon::StringToUint32(args[INDEX_4], flags) == false)) {
64         LOGE("Parameter input error, please retry");
65         return -EINVAL;
66     }
67     return OHOS::StorageDaemon::StorageDaemonClient::GenerateUserKeys(userId, flags);
68 }
69 
PrepareUserSpace(const std::vector<std::string> & args)70 static int32_t PrepareUserSpace(const std::vector<std::string> &args)
71 {
72     if (args.size() < ARG_CNT_5) {
73         LOGE("Parameter nums is less than 5, please retry");
74         return -EINVAL;
75     }
76     uint32_t userId;
77     uint32_t flags;
78     // 3 means take the fourth argument of args, 4 means take the fifth argument of args
79     if ((OHOS::StorageDaemon::StringToUint32(args[INDEX_3], userId) == false) ||
80         (OHOS::StorageDaemon::StringToUint32(args[INDEX_4], flags) == false)) {
81         LOGE("Parameter input error, please retry");
82         return -EINVAL;
83     }
84     return OHOS::StorageDaemon::StorageDaemonClient::PrepareUserDirs(userId, flags);
85 }
86 
DeleteUserKeys(const std::vector<std::string> & args)87 static int32_t DeleteUserKeys(const std::vector<std::string> &args)
88 {
89     if (args.size() < ARG_CNT_4) {
90         LOGE("Parameter nums is less than 4, please retry");
91         return -EINVAL;
92     }
93     uint32_t userId;
94     // 3 means take the fourth argument of args
95     if (OHOS::StorageDaemon::StringToUint32(args[INDEX_3], userId) == false) {
96         LOGE("Parameter input error, please retry");
97         return -EINVAL;
98     }
99     return OHOS::StorageDaemon::StorageDaemonClient::DeleteUserKeys(userId);
100 }
101 
DestroyUserSpace(const std::vector<std::string> & args)102 static int32_t DestroyUserSpace(const std::vector<std::string> &args)
103 {
104     if (args.size() < ARG_CNT_5) {
105         LOGE("Parameter nums is less than 5, please retry");
106         return -EINVAL;
107     }
108     uint32_t userId;
109     uint32_t flags;
110     // 3 means take the fourth argument of args, 4 means take the fifth argument of args
111     if (OHOS::StorageDaemon::StringToUint32(args[INDEX_3], userId) == false ||
112         OHOS::StorageDaemon::StringToUint32(args[INDEX_4], flags) == false) {
113         LOGE("Parameter input error, please retry");
114         return -EINVAL;
115     }
116     return OHOS::StorageDaemon::StorageDaemonClient::DestroyUserDirs(userId, flags);
117 }
118 
UpdateUserAuth(const std::vector<std::string> & args)119 static int32_t UpdateUserAuth(const std::vector<std::string> &args)
120 {
121     if (args.size() < ARG_CNT_5) {
122         LOGE("Parameter nums is less than 5, please retry");
123         return -EINVAL;
124     }
125     uint32_t userId;
126     // 3 means take the fourth argument of args
127     if (OHOS::StorageDaemon::StringToUint32(args[INDEX_3], userId) == false) {
128         LOGE("Parameter input error, please retry");
129         return -EINVAL;
130     }
131 
132     // 4 means take the fifth argument of args, 5 means take the sixth argument of args
133     if (args.size() == ARG_CNT_6) {
134         std::vector<uint8_t> oldSecret(args[INDEX_4].begin(), args[INDEX_4].end());
135         std::vector<uint8_t> newSecret(args[INDEX_5].begin(), args[INDEX_5].end());
136         return OHOS::StorageDaemon::StorageDaemonClient::UpdateUserAuth(userId, 0, {}, oldSecret, newSecret);
137     }
138     std::vector<uint8_t> newSecret(args[INDEX_4].begin(), args[INDEX_4].end());
139     return OHOS::StorageDaemon::StorageDaemonClient::UpdateUserAuth(userId, 0, {}, {}, newSecret);
140 }
141 
ActiveUserKey(const std::vector<std::string> & args)142 static int32_t ActiveUserKey(const std::vector<std::string> &args)
143 {
144     if (args.size() < ARG_CNT_4) {
145         LOGE("Parameter nums is less than 4, please retry");
146         return -EINVAL;
147     }
148     uint32_t userId;
149     // 3 means take the fourth argument of args
150     if (OHOS::StorageDaemon::StringToUint32(args[INDEX_3], userId) == false) {
151         LOGE("Parameter input error, please retry");
152         return -EINVAL;
153     }
154     // 4 means take the fifth argument of args
155     if (args.size() == ARG_CNT_5) {
156         std::vector<uint8_t> secret(args[INDEX_4].begin(), args[INDEX_4].end());
157         return OHOS::StorageDaemon::StorageDaemonClient::ActiveUserKey(userId, {}, secret);
158     }
159     return OHOS::StorageDaemon::StorageDaemonClient::ActiveUserKey(userId, {}, {});
160 }
161 
InactiveUserKey(const std::vector<std::string> & args)162 static int32_t InactiveUserKey(const std::vector<std::string> &args)
163 {
164     if (args.size() < ARG_CNT_4) {
165         LOGE("Parameter nums is less than 4, please retry");
166         return -EINVAL;
167     }
168     uint32_t userId;
169     // 3 means take the fourth argument of args
170     if (OHOS::StorageDaemon::StringToUint32(args[INDEX_3], userId) == false) {
171         LOGE("Parameter input error, please retry");
172         return -EINVAL;
173     }
174     return OHOS::StorageDaemon::StorageDaemonClient::InactiveUserKey(userId);
175 }
176 
LockUserScreen(const std::vector<std::string> & args)177 static int32_t LockUserScreen(const std::vector<std::string> &args)
178 {
179     if (args.size() < ARG_CNT_4) {
180         LOGE("Parameter nums is less than 4, please retry");
181         return -EINVAL;
182     }
183     uint32_t userId;
184     // 3 means take the fourth argument of args
185     if (OHOS::StorageDaemon::StringToUint32(args[INDEX_3], userId) == false) {
186         LOGE("Parameter input error, please retry");
187         return -EINVAL;
188     }
189     return OHOS::StorageDaemon::StorageDaemonClient::LockUserScreen(userId);
190 }
191 
UnlockUserScreen(const std::vector<std::string> & args)192 static int32_t UnlockUserScreen(const std::vector<std::string> &args)
193 {
194     if (args.size() < ARG_CNT_4) {
195         LOGE("Parameter nums is less than 4, please retry");
196         return -EINVAL;
197     }
198     uint32_t userId;
199     // 3 means take the fourth argument of args
200     if (OHOS::StorageDaemon::StringToUint32(args[INDEX_3], userId) == false) {
201         LOGE("Parameter input error, please retry");
202         return -EINVAL;
203     }
204     // 4 means take the fifth argument of args
205     if (args.size() == ARG_CNT_5) {
206         std::vector<uint8_t> secret(args[INDEX_4].begin(), args[INDEX_4].end());
207         return OHOS::StorageDaemon::StorageDaemonClient::UnlockUserScreen(userId, {}, secret);
208     }
209     return OHOS::StorageDaemon::StorageDaemonClient::UnlockUserScreen(userId, {}, {});
210 }
211 
GetFileEncryptStatus(const std::vector<std::string> & args)212 int32_t GetFileEncryptStatus(const std::vector<std::string> &args)
213 {
214     if (args.size() < ARG_CNT_5) {
215         LOGE("Parameter nums is less than 4, please retry");
216         return -EINVAL;
217     }
218     uint32_t userId;
219     // 3 means take the fourth argument of args
220     if (OHOS::StorageDaemon::StringToUint32(args[INDEX_3], userId) == false) {
221         LOGE("Parameter input error, please retry");
222         return -EINVAL;
223     }
224     uint32_t judge;
225     if (OHOS::StorageDaemon::StringToUint32(args[INDEX_4], judge) == false) {
226         LOGE("Parameter input error, please retry");
227         return -EINVAL;
228     }
229     bool isEncrypted = true;
230     bool judgeFlag = judge != 0;
231     return OHOS::StorageDaemon::StorageDaemonClient::GetFileEncryptStatus(userId, isEncrypted, judgeFlag);
232 }
233 
EnableFscrypt(const std::vector<std::string> & args)234 static int32_t EnableFscrypt(const std::vector<std::string> &args)
235 {
236     if (args.size() < ARG_CNT_4) {
237         LOGE("Parameter nums is less than 4, please retry");
238         return -EINVAL;
239     }
240     auto option = args[INDEX_3]; // cmd no.3 param is the option
241     return OHOS::StorageDaemon::StorageDaemonClient::FscryptEnable(option);
242 }
243 
UpdateKeyContext(const std::vector<std::string> & args)244 static int32_t UpdateKeyContext(const std::vector<std::string> &args)
245 {
246     if (args.size() < ARG_CNT_5) {
247         LOGE("Parameter nums is less than 5, please retry");
248         return -EINVAL;
249     }
250 
251     uint32_t userId;
252     bool needRemoveTmpKey;
253 
254     // 3 means take the fourth argument of args
255     if (OHOS::StorageDaemon::StringToUint32(args[INDEX_3], userId) == false) {
256         LOGE("Parameter input error, please retry");
257         return -EINVAL;
258     }
259     if (OHOS::StorageDaemon::StringToBool(args[INDEX_4], needRemoveTmpKey) == false) {
260         LOGE("Parameter input error, please retry");
261         return -EINVAL;
262     }
263 
264     return OHOS::StorageDaemon::StorageDaemonClient::UpdateKeyContext(userId, needRemoveTmpKey);
265 }
266 
GenerateAppkey(const std::vector<std::string> & args)267 static int32_t GenerateAppkey(const std::vector<std::string> &args)
268 {
269     if (args.size() < ARG_CNT_4) {
270         LOGE("Parameter nums is less than 4, please retry");
271         return -EINVAL;
272     }
273     uint32_t userId;
274     // 3 means take the fourth argument of args
275     if (OHOS::StorageDaemon::StringToUint32(args[INDEX_3], userId) == false) {
276         LOGE("Parameter input error, please retry");
277         return -EINVAL;
278     }
279     uint32_t hashId = 0;
280     std::string keyId;
281     return OHOS::StorageDaemon::StorageDaemonClient::GenerateAppkey(userId, hashId, keyId);
282 }
283 
DeleteAppkey(const std::vector<std::string> & args)284 static int32_t DeleteAppkey(const std::vector<std::string> &args)
285 {
286     if (args.size() < ARG_CNT_4) {
287         LOGE("Parameter nums is less than 4, please retry");
288         return -EINVAL;
289     }
290     uint32_t userId;
291     // 3 means take the fourth argument of args
292     if (OHOS::StorageDaemon::StringToUint32(args[INDEX_3], userId) == false) {
293         LOGE("Parameter input error, please retry");
294         return -EINVAL;
295     }
296     std::string keyId = args[INDEX_4];
297     return OHOS::StorageDaemon::StorageDaemonClient::DeleteAppkey(userId, keyId);
298 }
299 
CreateRecoverKey(const std::vector<std::string> & args)300 static int32_t CreateRecoverKey(const std::vector<std::string> &args)
301 {
302     if (args.size() < ARG_CNT_4) {
303         LOGE("Parameter nums is less than 4, please retry");
304         return -EINVAL;
305     }
306     uint32_t userId;
307     uint32_t userType;
308 
309     if (!OHOS::StorageDaemon::StringToUint32(args[INDEX_3], userId)) {
310         LOGE("Parameter input error, please retry");
311         return -EINVAL;
312     }
313     if (!OHOS::StorageDaemon::StringToUint32(args[INDEX_4], userType)) {
314         LOGE("Parameter input error, please retry");
315         return -EINVAL;
316     }
317     return OHOS::StorageDaemon::StorageDaemonClient::CreateRecoverKey(userId, userType, {}, {});
318 }
319 
SetRecoverKey(const std::vector<std::string> & args)320 static int32_t SetRecoverKey(const std::vector<std::string> &args)
321 {
322     if (args.size() < ARG_CNT_4) {
323         LOGE("SetRecoverKey Parameter nums is less than 4, please retry");
324         return -EINVAL;
325     }
326 
327     std::vector<uint8_t> key(args[INDEX_3].begin(), args[INDEX_3].end());
328     return OHOS::StorageDaemon::StorageDaemonClient::SetRecoverKey(key);
329 }
330 
StopUser(const std::vector<std::string> & args)331 static int32_t StopUser(const std::vector<std::string> &args)
332 {
333     if (args.size() < ARG_CNT_4) {
334         LOGE("Parameter nums is less than 4, please retry");
335         return -EINVAL;
336     }
337     uint32_t userId;
338     if (OHOS::StorageDaemon::StringToUint32(args[INDEX_3], userId) == false) {
339         LOGE("Parameter input error, please retry");
340         return -EINVAL;
341     }
342     return OHOS::StorageDaemon::StorageDaemonClient::StopUser(userId);
343 }
344 
StartUser(const std::vector<std::string> & args)345 static int32_t StartUser(const std::vector<std::string> &args)
346 {
347     if (args.size() < ARG_CNT_4) {
348         LOGE("Parameter nums is less than 4, please retry");
349         return -EINVAL;
350     }
351     uint32_t userId;
352     if (OHOS::StorageDaemon::StringToUint32(args[INDEX_3], userId) == false) {
353         LOGE("Parameter input error, please retry");
354         return -EINVAL;
355     }
356     return OHOS::StorageDaemon::StorageDaemonClient::StartUser(userId);
357 }
358 #endif
359 
360 static const auto g_fscryptCmdHandler = std::map<std::string,
361     std::function<int32_t(const std::vector<std::string> &)>> {
362     {"init_global_key", InitGlobalKey},
363     {"init_main_user", InitMainUser},
364 #ifdef SDC_TEST_ENABLE
365     {"generate_user_keys", GenerateUserKeys},
366     {"prepare_user_space", PrepareUserSpace},
367     {"delete_user_keys", DeleteUserKeys},
368     {"destroy_user_space", DestroyUserSpace},
369     {"update_user_auth", UpdateUserAuth},
370     {"active_user_key", ActiveUserKey},
371     {"inactive_user_key", InactiveUserKey},
372     {"enable", EnableFscrypt},
373     {"update_key_context", UpdateKeyContext},
374     {"lock_user_screen", LockUserScreen},
375     {"unlock_user_screen", UnlockUserScreen},
376     {"generate_app_key", GenerateAppkey},
377     {"delete_app_key", DeleteAppkey},
378     {"Get_unlock_status", GetFileEncryptStatus},
379     {"create_recover_key", CreateRecoverKey},
380     {"set_recover_key", SetRecoverKey},
381     {"stop_user", StopUser},
382     {"start_user", StartUser},
383 #endif
384 };
385 
HandleFileCrypt(const std::string & cmd,const std::vector<std::string> & args)386 static int HandleFileCrypt(const std::string &cmd, const std::vector<std::string> &args)
387 {
388     LOGI("fscrypt cmd: %{public}s", cmd.c_str());
389 
390     auto handler = g_fscryptCmdHandler.find(cmd);
391     if (handler == g_fscryptCmdHandler.end()) {
392         LOGE("Unknown fscrypt cmd: %{public}s", cmd.c_str());
393         return -EINVAL;
394     }
395     auto ret = handler->second(args);
396     if (ret != 0) {
397         LOGE("fscrypt cmd: %{public}s failed, ret: %{public}d", cmd.c_str(), ret);
398     } else {
399         LOGI("fscrypt cmd: %{public}s success", cmd.c_str());
400     }
401     return ret;
402 }
403 
main(int argc,char ** argv)404 int main(int argc, char **argv)
405 {
406     LOGI("sdc start");
407     std::vector<std::string> args(argv, argv + argc);
408 
409     if (argc < ARG_CNT_2) {
410         LOGE("usage: sdc <subsystem> [cmd]");
411         return 0;
412     }
413 
414     int ret = 0;
415     // no.1 param is the cmd
416     if (args[INDEX_1] == "filecrypt") {
417         ret = HandleFileCrypt(args[INDEX_2], args); // no.2 param is the cmd
418     } else {
419         LOGE("Unknown subsystem: %{public}s", args[INDEX_1].c_str()); // no.1 param is the cmd
420         ret = -EINVAL;
421     }
422 
423     LOGI("sdc end");
424     std::cout << "ret: " << ret << std::endl;
425     return ret;
426 }
427