blob: abc8f8245b1b21d84cc929586cd25ab5552ab111 [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
21#include <dirent.h>
22#include <errno.h>
23#include <sys/types.h>
24#include <unistd.h>
25
26#include <fstream>
27#include <iostream>
28#include <iterator>
29#include <sstream>
30#include <string>
31#include <vector>
32
33#include <android-base/file.h>
34#include <android-base/logging.h>
35#include <android-base/properties.h>
36#include <android-base/strings.h>
37#include <keyutils.h>
38
39static constexpr int kMaxCertSize = 4096;
40
41// Add all the certs from directory path to keyring with keyring_id. Returns the number of keys
42// added.
43int AddKeys(const std::string& path, const key_serial_t keyring_id, const std::string& keyring_desc,
44 int start_index) {
45 std::unique_ptr<DIR, int (*)(DIR*)> dir(opendir(path.c_str()), closedir);
46 if (!dir) {
47 PLOG(WARNING) << "Failed to open directory " << path;
48 return 0;
49 }
50 int keys_added = 0;
51 struct dirent* dp;
52 while ((dp = readdir(dir.get())) != NULL) {
53 if (dp->d_type != DT_REG) {
54 continue;
55 }
56 std::string cert_path = path + "/" + dp->d_name;
57 std::string cert_buf;
58 if (!android::base::ReadFileToString(cert_path, &cert_buf, false /* follow_symlinks */)) {
59 LOG(ERROR) << "Failed to read " << cert_path;
60 continue;
61 }
62
63 if (cert_buf.size() > kMaxCertSize) {
64 LOG(ERROR) << "Certficate size too large: " << cert_path;
65 continue;
66 }
67
68 // Add key to keyring.
69 int key_desc_index = keys_added + start_index;
70 std::string key_desc = keyring_desc + "-key" + std::to_string(key_desc_index);
71 key_serial_t key =
72 add_key("asymmetric", key_desc.c_str(), &cert_buf[0], cert_buf.size(), keyring_id);
73 if (key < 0) {
74 PLOG(ERROR) << "Failed to add key to keyring: " << cert_path;
75 continue;
76 }
77 keys_added++;
78 }
79 return keys_added;
80}
81
82std::vector<std::string> SplitBySpace(const std::string& s) {
83 std::istringstream iss(s);
84 return std::vector<std::string>{std::istream_iterator<std::string>{iss},
85 std::istream_iterator<std::string>{}};
86}
87
88// Find the keyring id. Because request_key(2) syscall is not available or the key is
89// kernel keyring, the id is looked up from /proc/keys. The keyring description may contain other
90// information in the descritption section depending on the key type, only the first word in the
91// keyring description is used for searching.
92bool GetKeyringId(const std::string& keyring_desc, key_serial_t* keyring_id) {
93 if (!keyring_id) {
94 LOG(ERROR) << "keyring_id is null";
95 return false;
96 }
97
98 // Only keys allowed by SELinux rules will be shown here.
99 std::ifstream proc_keys_file("/proc/keys");
100 if (!proc_keys_file.is_open()) {
101 PLOG(ERROR) << "Failed to open /proc/keys";
102 return false;
103 }
104
105 std::string line;
106 while (getline(proc_keys_file, line)) {
107 std::vector<std::string> tokens = SplitBySpace(line);
108 if (tokens.size() < 9) {
109 continue;
110 }
111 std::string key_id = tokens[0];
112 std::string key_type = tokens[7];
113 // The key description may contain space.
114 std::string key_desc_prefix = tokens[8];
115 // The prefix has a ":" at the end
116 std::string key_desc_pattern = keyring_desc + ":";
117 if (key_type != "keyring" || key_desc_prefix != key_desc_pattern) {
118 continue;
119 }
120 *keyring_id = std::stoi(key_id, nullptr, 16);
121 return true;
122 }
123 return false;
124}
125
126static void Usage(int exit_code) {
127 fprintf(stderr, "usage: mini-keyctl -c PATHS -s DESCRIPTION\n");
128 fprintf(stderr, "\n");
129 fprintf(stderr, "-c, --cert_dirs the certificate locations, separated by comma\n");
130 fprintf(stderr, "-k, --keyring the keyring description\n");
131 _exit(exit_code);
132}
133
134int main(int argc, char** argv) {
135 if (argc < 5) Usage(1);
136
137 std::string arg_cert_dirs;
138 std::string arg_keyring_desc;
139
140 for (int i = 1; i < argc; i++) {
141 std::string option = argv[i];
142 if (option == "-c" || option == "--cert_dirs") {
143 if (i + 1 < argc) arg_cert_dirs = argv[++i];
144 } else if (option == "-k" || option == "--keyring") {
145 if (i + 1 < argc) arg_keyring_desc = argv[++i];
146 }
147 }
148
149 if (arg_cert_dirs.empty() || arg_keyring_desc.empty()) {
150 LOG(ERROR) << "Missing cert_dirs or keyring desc";
151 Usage(1);
152 }
153
154 // Get the keyring id
155 key_serial_t key_ring_id;
156 if (!GetKeyringId(arg_keyring_desc, &key_ring_id)) {
157 PLOG(ERROR) << "Can't find keyring with " << arg_keyring_desc;
158 return 1;
159 }
160
161 std::vector<std::string> cert_dirs = android::base::Split(arg_cert_dirs, ",");
162 int start_index = 0;
163 for (const auto& cert_dir : cert_dirs) {
164 int keys_added = AddKeys(cert_dir, key_ring_id, arg_keyring_desc, start_index);
165 start_index += keys_added;
166 }
167
168 // Prevent new keys to be added.
169 if (!android::base::GetBoolProperty("ro.debuggable", false) &&
170 keyctl_restrict_keyring(key_ring_id, nullptr, nullptr) < 0) {
171 PLOG(ERROR) << "Failed to restrict key ring " << arg_keyring_desc;
172 return 1;
173 }
174
175 return 0;
176}