blob: 844f873bc55d1bab32990157aa86c928c89a45e5 [file] [log] [blame]
Xiaoyong Zhou4a5c3522019-01-29 15:53:21 -08001/*
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 Zhoub29b27e2019-03-08 09:59:42 -080021#include "mini_keyctl_utils.h"
22
Victor Hsieh327037f2019-03-15 11:35:45 -070023#include <stdio.h>
Xiaoyong Zhou4a5c3522019-01-29 15:53:21 -080024#include <unistd.h>
25
Victor Hsieh327037f2019-03-15 11:35:45 -070026#include <android-base/parseint.h>
27
Xiaoyong Zhou4a5c3522019-01-29 15:53:21 -080028static void Usage(int exit_code) {
Xiaoyong Zhoub29b27e2019-03-08 09:59:42 -080029 fprintf(stderr, "usage: mini-keyctl <action> [args,]\n");
30 fprintf(stderr, " mini-keyctl add <type> <desc> <data> <keyring>\n");
31 fprintf(stderr, " mini-keyctl padd <type> <desc> <keyring>\n");
32 fprintf(stderr, " mini-keyctl dadd <type> <desc_prefix> <cert_dir> <keyring>\n");
33 fprintf(stderr, " mini-keyctl unlink <key> <keyring>\n");
34 fprintf(stderr, " mini-keyctl restrict_keyring <keyring>\n");
Victor Hsieh327037f2019-03-15 11:35:45 -070035 fprintf(stderr, " mini-keyctl security <key>\n");
Xiaoyong Zhou4a5c3522019-01-29 15:53:21 -080036 _exit(exit_code);
37}
38
Xiaoyong Zhoub29b27e2019-03-08 09:59:42 -080039int main(int argc, const char** argv) {
40 if (argc < 2) Usage(1);
41 const std::string action = argv[1];
Xiaoyong Zhou4a5c3522019-01-29 15:53:21 -080042
Xiaoyong Zhoub29b27e2019-03-08 09:59:42 -080043 if (action == "add") {
44 if (argc != 6) Usage(1);
45 std::string type = argv[2];
46 std::string desc = argv[3];
47 std::string data = argv[4];
48 std::string keyring = argv[5];
49 return Add(type, desc, data, keyring);
50 } else if (action == "dadd") {
51 if (argc != 6) Usage(1);
52 std::string type = argv[2];
53 // The key description contains desc_prefix and an index.
54 std::string desc_prefix = argv[3];
55 std::string cert_dir = argv[4];
56 std::string keyring = argv[5];
57 return AddCertsFromDir(type, desc_prefix, cert_dir, keyring);
58 } else if (action == "padd") {
59 if (argc != 5) Usage(1);
60 std::string type = argv[2];
61 std::string desc = argv[3];
62 std::string keyring = argv[4];
63 return Padd(type, desc, keyring);
64 } else if (action == "restrict_keyring") {
65 if (argc != 3) Usage(1);
66 std::string keyring = argv[2];
67 return RestrictKeyring(keyring);
68 } else if (action == "unlink") {
69 if (argc != 4) Usage(1);
70 key_serial_t key = std::stoi(argv[2], nullptr, 16);
71 const std::string keyring = argv[3];
72 return Unlink(key, keyring);
Victor Hsieh327037f2019-03-15 11:35:45 -070073 } else if (action == "security") {
74 if (argc != 3) Usage(1);
75 const char* key_str = argv[2];
76 key_serial_t key;
77 if (!android::base::ParseInt(key_str, &key)) {
78 fprintf(stderr, "Unparsable key: '%s'\n", key_str);
79 return 1;
80 }
81 std::string context = RetrieveSecurityContext(key);
82 if (context.empty()) {
83 perror(key_str);
84 return 1;
85 }
86 fprintf(stderr, "%s\n", context.c_str());
87 return 0;
Xiaoyong Zhoub29b27e2019-03-08 09:59:42 -080088 } else {
Victor Hsieh327037f2019-03-15 11:35:45 -070089 fprintf(stderr, "Unrecognized action: %s\n", action.c_str());
Xiaoyong Zhou4a5c3522019-01-29 15:53:21 -080090 Usage(1);
91 }
92
Xiaoyong Zhou4a5c3522019-01-29 15:53:21 -080093 return 0;
94}