Xiaoyong Zhou | 4a5c352 | 2019-01-29 15:53:21 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2019 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 | /* |
| 18 | * A tool loads keys to keyring. |
| 19 | */ |
| 20 | |
Xiaoyong Zhou | b29b27e | 2019-03-08 09:59:42 -0800 | [diff] [blame] | 21 | #include "mini_keyctl_utils.h" |
| 22 | |
Xiaoyong Zhou | 4a5c352 | 2019-01-29 15:53:21 -0800 | [diff] [blame] | 23 | #include <unistd.h> |
| 24 | |
Xiaoyong Zhou | 4a5c352 | 2019-01-29 15:53:21 -0800 | [diff] [blame] | 25 | static void Usage(int exit_code) { |
Xiaoyong Zhou | b29b27e | 2019-03-08 09:59:42 -0800 | [diff] [blame] | 26 | fprintf(stderr, "usage: mini-keyctl <action> [args,]\n"); |
| 27 | fprintf(stderr, " mini-keyctl add <type> <desc> <data> <keyring>\n"); |
| 28 | fprintf(stderr, " mini-keyctl padd <type> <desc> <keyring>\n"); |
| 29 | fprintf(stderr, " mini-keyctl dadd <type> <desc_prefix> <cert_dir> <keyring>\n"); |
| 30 | fprintf(stderr, " mini-keyctl unlink <key> <keyring>\n"); |
| 31 | fprintf(stderr, " mini-keyctl restrict_keyring <keyring>\n"); |
Xiaoyong Zhou | 4a5c352 | 2019-01-29 15:53:21 -0800 | [diff] [blame] | 32 | _exit(exit_code); |
| 33 | } |
| 34 | |
Xiaoyong Zhou | b29b27e | 2019-03-08 09:59:42 -0800 | [diff] [blame] | 35 | int main(int argc, const char** argv) { |
| 36 | if (argc < 2) Usage(1); |
| 37 | const std::string action = argv[1]; |
Xiaoyong Zhou | 4a5c352 | 2019-01-29 15:53:21 -0800 | [diff] [blame] | 38 | |
Xiaoyong Zhou | b29b27e | 2019-03-08 09:59:42 -0800 | [diff] [blame] | 39 | if (action == "add") { |
| 40 | if (argc != 6) Usage(1); |
| 41 | std::string type = argv[2]; |
| 42 | std::string desc = argv[3]; |
| 43 | std::string data = argv[4]; |
| 44 | std::string keyring = argv[5]; |
| 45 | return Add(type, desc, data, keyring); |
| 46 | } else if (action == "dadd") { |
| 47 | if (argc != 6) Usage(1); |
| 48 | std::string type = argv[2]; |
| 49 | // The key description contains desc_prefix and an index. |
| 50 | std::string desc_prefix = argv[3]; |
| 51 | std::string cert_dir = argv[4]; |
| 52 | std::string keyring = argv[5]; |
| 53 | return AddCertsFromDir(type, desc_prefix, cert_dir, keyring); |
| 54 | } else if (action == "padd") { |
| 55 | if (argc != 5) Usage(1); |
| 56 | std::string type = argv[2]; |
| 57 | std::string desc = argv[3]; |
| 58 | std::string keyring = argv[4]; |
| 59 | return Padd(type, desc, keyring); |
| 60 | } else if (action == "restrict_keyring") { |
| 61 | if (argc != 3) Usage(1); |
| 62 | std::string keyring = argv[2]; |
| 63 | return RestrictKeyring(keyring); |
| 64 | } else if (action == "unlink") { |
| 65 | if (argc != 4) Usage(1); |
| 66 | key_serial_t key = std::stoi(argv[2], nullptr, 16); |
| 67 | const std::string keyring = argv[3]; |
| 68 | return Unlink(key, keyring); |
| 69 | } else { |
Xiaoyong Zhou | 4a5c352 | 2019-01-29 15:53:21 -0800 | [diff] [blame] | 70 | Usage(1); |
| 71 | } |
| 72 | |
Xiaoyong Zhou | 4a5c352 | 2019-01-29 15:53:21 -0800 | [diff] [blame] | 73 | return 0; |
| 74 | } |