2019-03-08 17:59:42 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2019 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <mini_keyctl_utils.h>
|
|
|
|
|
|
|
|
#include <dirent.h>
|
|
|
|
#include <errno.h>
|
2019-04-08 17:08:44 +00:00
|
|
|
#include <error.h>
|
2019-03-08 17:59:42 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include <fstream>
|
|
|
|
#include <iostream>
|
|
|
|
#include <iterator>
|
|
|
|
#include <sstream>
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
#include <android-base/file.h>
|
2019-03-18 20:49:02 +00:00
|
|
|
#include <android-base/parseint.h>
|
2019-03-08 17:59:42 +00:00
|
|
|
#include <android-base/properties.h>
|
|
|
|
#include <android-base/strings.h>
|
|
|
|
#include <keyutils.h>
|
|
|
|
|
|
|
|
static constexpr int kMaxCertSize = 4096;
|
|
|
|
|
2019-03-15 18:36:39 +00:00
|
|
|
static std::vector<std::string> SplitBySpace(const std::string& s) {
|
2019-03-08 17:59:42 +00:00
|
|
|
std::istringstream iss(s);
|
|
|
|
return std::vector<std::string>{std::istream_iterator<std::string>{iss},
|
|
|
|
std::istream_iterator<std::string>{}};
|
|
|
|
}
|
|
|
|
|
2019-03-15 18:36:39 +00:00
|
|
|
// Find the keyring id. Because request_key(2) syscall is not available or the key is
|
|
|
|
// kernel keyring, the id is looked up from /proc/keys. The keyring description may contain other
|
|
|
|
// information in the descritption section depending on the key type, only the first word in the
|
|
|
|
// keyring description is used for searching.
|
2019-04-08 17:08:44 +00:00
|
|
|
static key_serial_t GetKeyringIdOrDie(const std::string& keyring_desc) {
|
2019-03-15 18:36:39 +00:00
|
|
|
// If the keyring id is already a hex number, directly convert it to keyring id
|
2019-04-08 17:08:44 +00:00
|
|
|
key_serial_t keyring_id;
|
|
|
|
if (android::base::ParseInt(keyring_desc.c_str(), &keyring_id)) {
|
|
|
|
return keyring_id;
|
2019-03-15 18:36:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Only keys allowed by SELinux rules will be shown here.
|
|
|
|
std::ifstream proc_keys_file("/proc/keys");
|
|
|
|
if (!proc_keys_file.is_open()) {
|
2019-04-08 17:08:44 +00:00
|
|
|
error(1, errno, "Failed to open /proc/keys");
|
|
|
|
return -1;
|
2019-03-15 18:36:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string line;
|
|
|
|
while (getline(proc_keys_file, line)) {
|
|
|
|
std::vector<std::string> tokens = SplitBySpace(line);
|
|
|
|
if (tokens.size() < 9) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
std::string key_id = tokens[0];
|
|
|
|
std::string key_type = tokens[7];
|
|
|
|
// The key description may contain space.
|
|
|
|
std::string key_desc_prefix = tokens[8];
|
|
|
|
// The prefix has a ":" at the end
|
|
|
|
std::string key_desc_pattern = keyring_desc + ":";
|
|
|
|
if (key_type != "keyring" || key_desc_prefix != key_desc_pattern) {
|
|
|
|
continue;
|
|
|
|
}
|
2019-04-08 17:08:44 +00:00
|
|
|
if (!android::base::ParseInt(key_id.c_str(), &keyring_id)) {
|
|
|
|
error(1, 0, "Unexpected key format in /proc/keys");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return keyring_id;
|
2019-03-15 18:36:39 +00:00
|
|
|
}
|
2019-04-08 17:08:44 +00:00
|
|
|
return -1;
|
2019-03-15 18:36:39 +00:00
|
|
|
}
|
|
|
|
|
2019-03-08 17:59:42 +00:00
|
|
|
int Unlink(key_serial_t key, const std::string& keyring) {
|
2019-04-08 17:08:44 +00:00
|
|
|
key_serial_t keyring_id = GetKeyringIdOrDie(keyring);
|
2019-03-08 17:59:42 +00:00
|
|
|
if (keyctl_unlink(key, keyring_id) < 0) {
|
2019-04-08 17:08:44 +00:00
|
|
|
error(1, errno, "Failed to unlink key %x from keyring %s", key, keyring.c_str());
|
2019-03-08 17:59:42 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int Add(const std::string& type, const std::string& desc, const std::string& data,
|
|
|
|
const std::string& keyring) {
|
|
|
|
if (data.size() > kMaxCertSize) {
|
2019-04-08 17:08:44 +00:00
|
|
|
error(1, 0, "Certificate too large");
|
2019-03-08 17:59:42 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2019-04-08 17:08:44 +00:00
|
|
|
key_serial_t keyring_id = GetKeyringIdOrDie(keyring);
|
2019-03-08 17:59:42 +00:00
|
|
|
key_serial_t key = add_key(type.c_str(), desc.c_str(), data.c_str(), data.size(), keyring_id);
|
|
|
|
|
|
|
|
if (key < 0) {
|
2019-04-08 17:08:44 +00:00
|
|
|
error(1, errno, "Failed to add key");
|
2019-03-08 17:59:42 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2019-04-08 17:08:44 +00:00
|
|
|
std::cout << key << std::endl;
|
2019-03-08 17:59:42 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int Padd(const std::string& type, const std::string& desc, const std::string& keyring) {
|
2019-04-08 17:08:44 +00:00
|
|
|
key_serial_t keyring_id = GetKeyringIdOrDie(keyring);
|
2019-03-08 17:59:42 +00:00
|
|
|
|
|
|
|
// read from stdin to get the certificates
|
|
|
|
std::istreambuf_iterator<char> begin(std::cin), end;
|
|
|
|
std::string data(begin, end);
|
|
|
|
|
|
|
|
if (data.size() > kMaxCertSize) {
|
2019-04-08 17:08:44 +00:00
|
|
|
error(1, 0, "Certificate too large");
|
2019-03-08 17:59:42 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
key_serial_t key = add_key(type.c_str(), desc.c_str(), data.c_str(), data.size(), keyring_id);
|
|
|
|
|
|
|
|
if (key < 0) {
|
2019-04-08 17:08:44 +00:00
|
|
|
error(1, errno, "Failed to add key");
|
2019-03-08 17:59:42 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2019-04-08 17:08:44 +00:00
|
|
|
std::cout << key << std::endl;
|
2019-03-08 17:59:42 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int RestrictKeyring(const std::string& keyring) {
|
2019-04-08 17:08:44 +00:00
|
|
|
key_serial_t keyring_id = GetKeyringIdOrDie(keyring);
|
2019-03-08 17:59:42 +00:00
|
|
|
if (keyctl_restrict_keyring(keyring_id, nullptr, nullptr) < 0) {
|
2019-04-08 17:08:44 +00:00
|
|
|
error(1, errno, "Cannot restrict keyring '%s'", keyring.c_str());
|
2019-03-08 17:59:42 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2019-03-15 18:35:45 +00:00
|
|
|
|
|
|
|
std::string RetrieveSecurityContext(key_serial_t key) {
|
|
|
|
// Simply assume this size is enough in practice.
|
|
|
|
const int kMaxSupportedSize = 256;
|
|
|
|
std::string context;
|
|
|
|
context.resize(kMaxSupportedSize);
|
|
|
|
long retval = keyctl_get_security(key, context.data(), kMaxSupportedSize);
|
|
|
|
if (retval < 0) {
|
2019-04-08 17:08:44 +00:00
|
|
|
error(1, errno, "Cannot get security context of key %x", key);
|
2019-03-15 18:35:45 +00:00
|
|
|
return std::string();
|
|
|
|
}
|
|
|
|
if (retval > kMaxSupportedSize) {
|
2019-04-08 17:08:44 +00:00
|
|
|
error(1, 0, "The key has unexpectedly long security context than %d", kMaxSupportedSize);
|
2019-03-15 18:35:45 +00:00
|
|
|
return std::string();
|
|
|
|
}
|
|
|
|
context.resize(retval);
|
|
|
|
return context;
|
|
|
|
}
|