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 | |
Victor Hsieh | 0fb290b | 2019-03-18 13:49:02 -0700 | [diff] [blame^] | 23 | #include <error.h> |
Victor Hsieh | 327037f | 2019-03-15 11:35:45 -0700 | [diff] [blame] | 24 | #include <stdio.h> |
Xiaoyong Zhou | 4a5c352 | 2019-01-29 15:53:21 -0800 | [diff] [blame] | 25 | #include <unistd.h> |
| 26 | |
Victor Hsieh | 327037f | 2019-03-15 11:35:45 -0700 | [diff] [blame] | 27 | #include <android-base/parseint.h> |
| 28 | |
Xiaoyong Zhou | 4a5c352 | 2019-01-29 15:53:21 -0800 | [diff] [blame] | 29 | static void Usage(int exit_code) { |
Xiaoyong Zhou | b29b27e | 2019-03-08 09:59:42 -0800 | [diff] [blame] | 30 | fprintf(stderr, "usage: mini-keyctl <action> [args,]\n"); |
| 31 | fprintf(stderr, " mini-keyctl add <type> <desc> <data> <keyring>\n"); |
| 32 | fprintf(stderr, " mini-keyctl padd <type> <desc> <keyring>\n"); |
| 33 | fprintf(stderr, " mini-keyctl dadd <type> <desc_prefix> <cert_dir> <keyring>\n"); |
| 34 | fprintf(stderr, " mini-keyctl unlink <key> <keyring>\n"); |
| 35 | fprintf(stderr, " mini-keyctl restrict_keyring <keyring>\n"); |
Victor Hsieh | 327037f | 2019-03-15 11:35:45 -0700 | [diff] [blame] | 36 | fprintf(stderr, " mini-keyctl security <key>\n"); |
Xiaoyong Zhou | 4a5c352 | 2019-01-29 15:53:21 -0800 | [diff] [blame] | 37 | _exit(exit_code); |
| 38 | } |
| 39 | |
Victor Hsieh | 0fb290b | 2019-03-18 13:49:02 -0700 | [diff] [blame^] | 40 | static key_serial_t parseKeyOrDie(const char* str) { |
| 41 | key_serial_t key; |
| 42 | if (!android::base::ParseInt(str, &key)) { |
| 43 | error(1 /* exit code */, 0 /* errno */, "Unparsable key: '%s'\n", str); |
| 44 | } |
| 45 | return key; |
| 46 | } |
| 47 | |
Xiaoyong Zhou | b29b27e | 2019-03-08 09:59:42 -0800 | [diff] [blame] | 48 | int main(int argc, const char** argv) { |
| 49 | if (argc < 2) Usage(1); |
| 50 | const std::string action = argv[1]; |
Xiaoyong Zhou | 4a5c352 | 2019-01-29 15:53:21 -0800 | [diff] [blame] | 51 | |
Xiaoyong Zhou | b29b27e | 2019-03-08 09:59:42 -0800 | [diff] [blame] | 52 | if (action == "add") { |
| 53 | if (argc != 6) Usage(1); |
| 54 | std::string type = argv[2]; |
| 55 | std::string desc = argv[3]; |
| 56 | std::string data = argv[4]; |
| 57 | std::string keyring = argv[5]; |
| 58 | return Add(type, desc, data, keyring); |
| 59 | } else if (action == "dadd") { |
| 60 | if (argc != 6) Usage(1); |
| 61 | std::string type = argv[2]; |
| 62 | // The key description contains desc_prefix and an index. |
| 63 | std::string desc_prefix = argv[3]; |
| 64 | std::string cert_dir = argv[4]; |
| 65 | std::string keyring = argv[5]; |
| 66 | return AddCertsFromDir(type, desc_prefix, cert_dir, keyring); |
| 67 | } else if (action == "padd") { |
| 68 | if (argc != 5) Usage(1); |
| 69 | std::string type = argv[2]; |
| 70 | std::string desc = argv[3]; |
| 71 | std::string keyring = argv[4]; |
| 72 | return Padd(type, desc, keyring); |
| 73 | } else if (action == "restrict_keyring") { |
| 74 | if (argc != 3) Usage(1); |
| 75 | std::string keyring = argv[2]; |
| 76 | return RestrictKeyring(keyring); |
| 77 | } else if (action == "unlink") { |
| 78 | if (argc != 4) Usage(1); |
Victor Hsieh | 0fb290b | 2019-03-18 13:49:02 -0700 | [diff] [blame^] | 79 | key_serial_t key = parseKeyOrDie(argv[2]); |
Xiaoyong Zhou | b29b27e | 2019-03-08 09:59:42 -0800 | [diff] [blame] | 80 | const std::string keyring = argv[3]; |
| 81 | return Unlink(key, keyring); |
Victor Hsieh | 327037f | 2019-03-15 11:35:45 -0700 | [diff] [blame] | 82 | } else if (action == "security") { |
| 83 | if (argc != 3) Usage(1); |
| 84 | const char* key_str = argv[2]; |
Victor Hsieh | 0fb290b | 2019-03-18 13:49:02 -0700 | [diff] [blame^] | 85 | key_serial_t key = parseKeyOrDie(key_str); |
Victor Hsieh | 327037f | 2019-03-15 11:35:45 -0700 | [diff] [blame] | 86 | std::string context = RetrieveSecurityContext(key); |
| 87 | if (context.empty()) { |
| 88 | perror(key_str); |
| 89 | return 1; |
| 90 | } |
| 91 | fprintf(stderr, "%s\n", context.c_str()); |
| 92 | return 0; |
Xiaoyong Zhou | b29b27e | 2019-03-08 09:59:42 -0800 | [diff] [blame] | 93 | } else { |
Victor Hsieh | 327037f | 2019-03-15 11:35:45 -0700 | [diff] [blame] | 94 | fprintf(stderr, "Unrecognized action: %s\n", action.c_str()); |
Xiaoyong Zhou | 4a5c352 | 2019-01-29 15:53:21 -0800 | [diff] [blame] | 95 | Usage(1); |
| 96 | } |
| 97 | |
Xiaoyong Zhou | 4a5c352 | 2019-01-29 15:53:21 -0800 | [diff] [blame] | 98 | return 0; |
| 99 | } |