• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
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 
InitGlobalKey(const std::vector<std::string> & args)27 static int32_t InitGlobalKey(const std::vector<std::string> &args)
28 {
29     (void)args;
30     return OHOS::StorageDaemon::StorageDaemonClient::InitGlobalKey();
31 }
32 
InitMainUser(const std::vector<std::string> & args)33 static int32_t InitMainUser(const std::vector<std::string> &args)
34 {
35     (void)args;
36     return OHOS::StorageDaemon::StorageDaemonClient::InitGlobalUserKeys();
37 }
38 
GenerateUserKeys(const std::vector<std::string> & args)39 static int32_t GenerateUserKeys(const std::vector<std::string> &args)
40 {
41     if (args.size() < 5) {
42         LOGE("Parameter nums is less than 5, please retry");
43         return -EINVAL;
44     }
45     uint32_t userId;
46     uint32_t flags;
47     if ((OHOS::StorageDaemon::StringToUint32(args[3], userId) == false) ||
48         (OHOS::StorageDaemon::StringToUint32(args[4], flags) == false)) {
49         LOGE("Parameter input error, please retry");
50         return -EINVAL;
51     }
52     return OHOS::StorageDaemon::StorageDaemonClient::GenerateUserKeys(userId, flags);
53 }
54 
PrepareUserSpace(const std::vector<std::string> & args)55 static int32_t PrepareUserSpace(const std::vector<std::string> &args)
56 {
57     if (args.size() < 5) {
58         LOGE("Parameter nums is less than 5, please retry");
59         return -EINVAL;
60     }
61     uint32_t userId;
62     uint32_t flags;
63     if ((OHOS::StorageDaemon::StringToUint32(args[3], userId) == false) ||
64         (OHOS::StorageDaemon::StringToUint32(args[4], flags) == false)) {
65         LOGE("Parameter input error, please retry");
66         return -EINVAL;
67     }
68     return OHOS::StorageDaemon::StorageDaemonClient::PrepareUserDirs(userId, flags);
69 }
70 
DeleteUserKeys(const std::vector<std::string> & args)71 static int32_t DeleteUserKeys(const std::vector<std::string> &args)
72 {
73     if (args.size() < 4) {
74         LOGE("Parameter nums is less than 4, please retry");
75         return -EINVAL;
76     }
77     uint32_t userId;
78     if (OHOS::StorageDaemon::StringToUint32(args[3], userId) == false) {
79         LOGE("Parameter input error, please retry");
80         return -EINVAL;
81     }
82     return OHOS::StorageDaemon::StorageDaemonClient::DeleteUserKeys(userId);
83 }
84 
DestroyUserSpace(const std::vector<std::string> & args)85 static int32_t DestroyUserSpace(const std::vector<std::string> &args)
86 {
87     if (args.size() < 5) {
88         LOGE("Parameter nums is less than 5, please retry");
89         return -EINVAL;
90     }
91     uint32_t userId;
92     uint32_t flags;
93     if (OHOS::StorageDaemon::StringToUint32(args[3], userId) == false ||
94         OHOS::StorageDaemon::StringToUint32(args[4], flags) == false) {
95         LOGE("Parameter input error, please retry");
96         return -EINVAL;
97     }
98     return OHOS::StorageDaemon::StorageDaemonClient::DestroyUserDirs(userId, flags);
99 }
100 
UpdateUserAuth(const std::vector<std::string> & args)101 static int32_t UpdateUserAuth(const std::vector<std::string> &args)
102 {
103     if (args.size() < 5) {
104         LOGE("Parameter nums is less than 5, please retry");
105         return -EINVAL;
106     }
107     uint32_t userId;
108     if (OHOS::StorageDaemon::StringToUint32(args[3], userId) == false) {
109         LOGE("Parameter input error, please retry");
110         return -EINVAL;
111     }
112 
113     if (args.size() == 6) {
114         std::vector<uint8_t> oldSecret(args[4].begin(), args[4].end());
115         std::vector<uint8_t> newSecret(args[5].begin(), args[5].end());
116         return OHOS::StorageDaemon::StorageDaemonClient::UpdateUserAuth(userId, {}, oldSecret, newSecret);
117     }
118     std::vector<uint8_t> newSecret(args[4].begin(), args[4].end());
119     return OHOS::StorageDaemon::StorageDaemonClient::UpdateUserAuth(userId, {}, {}, newSecret);
120 }
121 
ActiveUserKey(const std::vector<std::string> & args)122 static int32_t ActiveUserKey(const std::vector<std::string> &args)
123 {
124     if (args.size() < 4) {
125         LOGE("Parameter nums is less than 4, please retry");
126         return -EINVAL;
127     }
128     uint32_t userId;
129     if (OHOS::StorageDaemon::StringToUint32(args[3], userId) == false) {
130         LOGE("Parameter input error, please retry");
131         return -EINVAL;
132     }
133 
134     if (args.size() == 5) {
135         std::vector<uint8_t> secret(args[4].begin(), args[4].end());
136         return OHOS::StorageDaemon::StorageDaemonClient::ActiveUserKey(userId, {}, secret);
137     }
138     return OHOS::StorageDaemon::StorageDaemonClient::ActiveUserKey(userId, {}, {});
139 }
140 
InactiveUserKey(const std::vector<std::string> & args)141 static int32_t InactiveUserKey(const std::vector<std::string> &args)
142 {
143     if (args.size() < 4) {
144         LOGE("Parameter nums is less than 4, please retry");
145         return -EINVAL;
146     }
147     uint32_t userId;
148     if (OHOS::StorageDaemon::StringToUint32(args[3], userId) == false) {
149         LOGE("Parameter input error, please retry");
150         return -EINVAL;
151     }
152     return OHOS::StorageDaemon::StorageDaemonClient::InactiveUserKey(userId);
153 }
154 
EnableFscrypt(const std::vector<std::string> & args)155 static int32_t EnableFscrypt(const std::vector<std::string> &args)
156 {
157     if (args.size() < 4) {
158         LOGE("Parameter nums is less than 4, please retry");
159         return -EINVAL;
160     }
161     auto option = args[3]; // cmd no.3 param is the option
162     return OHOS::StorageDaemon::StorageDaemonClient::FscryptEnable(option);
163 }
164 
UpdateKeyContext(const std::vector<std::string> & args)165 static int32_t UpdateKeyContext(const std::vector<std::string> &args)
166 {
167     if (args.size() < 4) {
168         LOGE("Parameter nums is less than 4, please retry");
169         return -EINVAL;
170     }
171     uint32_t userId;
172     if (OHOS::StorageDaemon::StringToUint32(args[3], userId) == false) {
173         LOGE("Parameter input error, please retry");
174         return -EINVAL;
175     }
176     return OHOS::StorageDaemon::StorageDaemonClient::UpdateKeyContext(userId);
177 }
178 
179 static const auto g_fscryptCmdHandler = std::map<std::string,
180     std::function<int32_t(const std::vector<std::string> &)>> {
181     {"init_global_key", InitGlobalKey},
182     {"init_main_user", InitMainUser},
183     {"generate_user_keys", GenerateUserKeys},
184     {"prepare_user_space", PrepareUserSpace},
185     {"delete_user_keys", DeleteUserKeys},
186     {"destroy_user_space", DestroyUserSpace},
187     {"update_user_auth", UpdateUserAuth},
188     {"active_user_key", ActiveUserKey},
189     {"inactive_user_key", InactiveUserKey},
190     {"enable", EnableFscrypt},
191     {"update_key_context", UpdateKeyContext},
192 };
193 
HandleFileCrypt(const std::string & cmd,const std::vector<std::string> & args)194 static int HandleFileCrypt(const std::string &cmd, const std::vector<std::string> &args)
195 {
196     LOGI("fscrypt cmd: %{public}s", cmd.c_str());
197 
198     auto handler = g_fscryptCmdHandler.find(cmd);
199     if (handler == g_fscryptCmdHandler.end()) {
200         LOGE("Unknown fscrypt cmd: %{public}s", cmd.c_str());
201         return -EINVAL;
202     }
203     auto ret = handler->second(args);
204     if (ret != 0) {
205         LOGE("fscrypt cmd: %{public}s failed, ret: %{public}d", cmd.c_str(), ret);
206     } else {
207         LOGI("fscrypt cmd: %{public}s success", cmd.c_str());
208     }
209     return ret;
210 }
211 
main(int argc,char ** argv)212 int main(int argc, char **argv)
213 {
214     LOGI("sdc start");
215     std::vector<std::string> args(argv, argv + argc);
216 
217     if (argc < 2) {
218         LOGE("usage: sdc <subsystem> [cmd]");
219         return 0;
220     }
221 
222     int ret = 0;
223     if (args[1] == "filecrypt") {
224         ret = HandleFileCrypt(args[2], args); // no.2 param is the cmd
225     } else {
226         LOGE("Unknown subsystem: %{public}s", args[1].c_str());
227         ret = -EINVAL;
228     }
229 
230     LOGI("sdc end");
231     std::cout << "ret: " << ret << std::endl;
232     return ret;
233 }
234