1 /*
2 * Copyright (C) 2021 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include <string>
18
19 #include <android-base/file.h>
20 #include <android-base/logging.h>
21 #include <android-base/properties.h>
22 #include <fsverity_init.h>
23 #include <log/log.h>
24 #include <mini_keyctl_utils.h>
25
main(int argc,const char ** argv)26 int main(int argc, const char** argv) {
27 if (argc < 2) {
28 LOG(ERROR) << "Not enough arguments";
29 return -1;
30 }
31
32 key_serial_t keyring_id = android::GetKeyringId(".fs-verity");
33 if (keyring_id < 0) {
34 LOG(ERROR) << "Failed to find .fs-verity keyring id";
35 return -1;
36 }
37
38 const std::string_view command = argv[1];
39
40 if (command == "--load-verified-keys") {
41 LoadKeyFromVerifiedPartitions(keyring_id);
42 } else if (command == "--load-extra-key") {
43 if (argc != 3) {
44 LOG(ERROR) << "--load-extra-key requires <key_name> argument.";
45 return -1;
46 }
47 if (!LoadKeyFromStdin(keyring_id, argv[2])) {
48 return -1;
49 }
50 } else if (command == "--lock") {
51 if (!android::base::GetBoolProperty("ro.debuggable", false)) {
52 if (keyctl_restrict_keyring(keyring_id, nullptr, nullptr) < 0) {
53 PLOG(ERROR) << "Cannot restrict .fs-verity keyring";
54 }
55 }
56 } else {
57 LOG(ERROR) << "Unknown argument(s).";
58 return -1;
59 }
60
61 return 0;
62 }
63