blob: fe89e62abc970ecda6888a49fb73084bc37f4a4a [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 Hsieh0fb290b2019-03-18 13:49:02 -070023#include <error.h>
Victor Hsieh327037f2019-03-15 11:35:45 -070024#include <stdio.h>
Xiaoyong Zhou4a5c3522019-01-29 15:53:21 -080025#include <unistd.h>
26
Victor Hsieh327037f2019-03-15 11:35:45 -070027#include <android-base/parseint.h>
28
Xiaoyong Zhou4a5c3522019-01-29 15:53:21 -080029static void Usage(int exit_code) {
Xiaoyong Zhoub29b27e2019-03-08 09:59:42 -080030 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");
Xiaoyong Zhoub29b27e2019-03-08 09:59:42 -080033 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
Victor Hsieh0fb290b2019-03-18 13:49:02 -070039static key_serial_t parseKeyOrDie(const char* str) {
40 key_serial_t key;
41 if (!android::base::ParseInt(str, &key)) {
42 error(1 /* exit code */, 0 /* errno */, "Unparsable key: '%s'\n", str);
43 }
44 return key;
45}
46
Xiaoyong Zhoub29b27e2019-03-08 09:59:42 -080047int main(int argc, const char** argv) {
48 if (argc < 2) Usage(1);
49 const std::string action = argv[1];
Xiaoyong Zhou4a5c3522019-01-29 15:53:21 -080050
Xiaoyong Zhoub29b27e2019-03-08 09:59:42 -080051 if (action == "add") {
52 if (argc != 6) Usage(1);
53 std::string type = argv[2];
54 std::string desc = argv[3];
55 std::string data = argv[4];
56 std::string keyring = argv[5];
57 return Add(type, desc, data, keyring);
Xiaoyong Zhoub29b27e2019-03-08 09:59:42 -080058 } 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);
Victor Hsieh0fb290b2019-03-18 13:49:02 -070070 key_serial_t key = parseKeyOrDie(argv[2]);
Xiaoyong Zhoub29b27e2019-03-08 09:59:42 -080071 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];
Victor Hsieh0fb290b2019-03-18 13:49:02 -070076 key_serial_t key = parseKeyOrDie(key_str);
Victor Hsieh327037f2019-03-15 11:35:45 -070077 std::string context = RetrieveSecurityContext(key);
78 if (context.empty()) {
79 perror(key_str);
80 return 1;
81 }
82 fprintf(stderr, "%s\n", context.c_str());
83 return 0;
Xiaoyong Zhoub29b27e2019-03-08 09:59:42 -080084 } else {
Victor Hsieh327037f2019-03-15 11:35:45 -070085 fprintf(stderr, "Unrecognized action: %s\n", action.c_str());
Xiaoyong Zhou4a5c3522019-01-29 15:53:21 -080086 Usage(1);
87 }
88
Xiaoyong Zhou4a5c3522019-01-29 15:53:21 -080089 return 0;
90}