2016-06-02 20:01:19 +02:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2016 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 "KeyUtil.h"
|
|
|
|
|
2018-10-23 22:07:43 +02:00
|
|
|
#include <linux/fs.h>
|
2016-06-02 20:01:19 +02:00
|
|
|
#include <iomanip>
|
|
|
|
#include <sstream>
|
|
|
|
#include <string>
|
|
|
|
|
|
|
|
#include <openssl/sha.h>
|
|
|
|
|
|
|
|
#include <android-base/file.h>
|
|
|
|
#include <android-base/logging.h>
|
2017-05-10 02:01:04 +02:00
|
|
|
#include <keyutils.h>
|
2016-06-02 20:01:19 +02:00
|
|
|
|
|
|
|
#include "KeyStorage.h"
|
|
|
|
#include "Utils.h"
|
|
|
|
|
|
|
|
namespace android {
|
|
|
|
namespace vold {
|
|
|
|
|
2018-10-23 22:06:55 +02:00
|
|
|
constexpr int FS_AES_256_XTS_KEY_SIZE = 64;
|
2017-08-01 18:15:53 +02:00
|
|
|
|
|
|
|
bool randomKey(KeyBuffer* key) {
|
2018-10-23 22:06:55 +02:00
|
|
|
*key = KeyBuffer(FS_AES_256_XTS_KEY_SIZE);
|
2017-08-01 18:15:53 +02:00
|
|
|
if (ReadRandomBytes(key->size(), key->data()) != 0) {
|
2016-06-02 20:01:19 +02:00
|
|
|
// TODO status_t plays badly with PLOG, fix it.
|
|
|
|
LOG(ERROR) << "Random read failed";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get raw keyref - used to make keyname and to pass to ioctl
|
2018-10-23 22:07:43 +02:00
|
|
|
static std::string generateKeyRef(const uint8_t* key, int length) {
|
2016-06-02 20:01:19 +02:00
|
|
|
SHA512_CTX c;
|
|
|
|
|
|
|
|
SHA512_Init(&c);
|
|
|
|
SHA512_Update(&c, key, length);
|
|
|
|
unsigned char key_ref1[SHA512_DIGEST_LENGTH];
|
|
|
|
SHA512_Final(key_ref1, &c);
|
|
|
|
|
|
|
|
SHA512_Init(&c);
|
|
|
|
SHA512_Update(&c, key_ref1, SHA512_DIGEST_LENGTH);
|
|
|
|
unsigned char key_ref2[SHA512_DIGEST_LENGTH];
|
|
|
|
SHA512_Final(key_ref2, &c);
|
|
|
|
|
2018-10-23 22:06:55 +02:00
|
|
|
static_assert(FS_KEY_DESCRIPTOR_SIZE <= SHA512_DIGEST_LENGTH, "Hash too short for descriptor");
|
|
|
|
return std::string((char*)key_ref2, FS_KEY_DESCRIPTOR_SIZE);
|
2016-06-02 20:01:19 +02:00
|
|
|
}
|
|
|
|
|
2018-10-23 22:06:55 +02:00
|
|
|
static bool fillKey(const KeyBuffer& key, fscrypt_key* fs_key) {
|
|
|
|
if (key.size() != FS_AES_256_XTS_KEY_SIZE) {
|
2016-06-02 20:01:19 +02:00
|
|
|
LOG(ERROR) << "Wrong size key " << key.size();
|
|
|
|
return false;
|
|
|
|
}
|
2018-10-23 22:06:55 +02:00
|
|
|
static_assert(FS_AES_256_XTS_KEY_SIZE <= sizeof(fs_key->raw), "Key too long!");
|
|
|
|
fs_key->mode = FS_ENCRYPTION_MODE_AES_256_XTS;
|
|
|
|
fs_key->size = key.size();
|
|
|
|
memset(fs_key->raw, 0, sizeof(fs_key->raw));
|
|
|
|
memcpy(fs_key->raw, key.data(), key.size());
|
2016-06-02 20:01:19 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-09-18 22:30:21 +02:00
|
|
|
static char const* const NAME_PREFIXES[] = {"ext4", "f2fs", "fscrypt", nullptr};
|
2017-06-20 01:05:55 +02:00
|
|
|
|
|
|
|
static std::string keyname(const std::string& prefix, const std::string& raw_ref) {
|
2016-06-02 20:01:19 +02:00
|
|
|
std::ostringstream o;
|
2017-06-20 01:05:55 +02:00
|
|
|
o << prefix << ":";
|
2017-08-18 08:49:45 +02:00
|
|
|
for (unsigned char i : raw_ref) {
|
2016-06-02 20:01:19 +02:00
|
|
|
o << std::hex << std::setw(2) << std::setfill('0') << (int)i;
|
|
|
|
}
|
|
|
|
return o.str();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the keyring we store all keys in
|
2018-10-23 22:06:55 +02:00
|
|
|
static bool fscryptKeyring(key_serial_t* device_keyring) {
|
|
|
|
*device_keyring = keyctl_search(KEY_SPEC_SESSION_KEYRING, "keyring", "fscrypt", 0);
|
2016-06-02 20:01:19 +02:00
|
|
|
if (*device_keyring == -1) {
|
|
|
|
PLOG(ERROR) << "Unable to find device keyring";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Install password into global keyring
|
|
|
|
// Return raw key reference for use in policy
|
2017-08-01 18:15:53 +02:00
|
|
|
bool installKey(const KeyBuffer& key, std::string* raw_ref) {
|
2018-10-23 22:06:55 +02:00
|
|
|
// Place fscrypt_key into automatically zeroing buffer.
|
|
|
|
KeyBuffer fsKeyBuffer(sizeof(fscrypt_key));
|
|
|
|
fscrypt_key& fs_key = *reinterpret_cast<fscrypt_key*>(fsKeyBuffer.data());
|
2017-08-01 18:15:53 +02:00
|
|
|
|
2018-10-23 22:06:55 +02:00
|
|
|
if (!fillKey(key, &fs_key)) return false;
|
|
|
|
*raw_ref = generateKeyRef(fs_key.raw, fs_key.size);
|
2016-06-02 20:01:19 +02:00
|
|
|
key_serial_t device_keyring;
|
2018-10-23 22:06:55 +02:00
|
|
|
if (!fscryptKeyring(&device_keyring)) return false;
|
2017-06-20 01:05:55 +02:00
|
|
|
for (char const* const* name_prefix = NAME_PREFIXES; *name_prefix != nullptr; name_prefix++) {
|
|
|
|
auto ref = keyname(*name_prefix, *raw_ref);
|
|
|
|
key_serial_t key_id =
|
2018-10-23 22:06:55 +02:00
|
|
|
add_key("logon", ref.c_str(), (void*)&fs_key, sizeof(fs_key), device_keyring);
|
2017-06-20 01:05:55 +02:00
|
|
|
if (key_id == -1) {
|
|
|
|
PLOG(ERROR) << "Failed to insert key into keyring " << device_keyring;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
LOG(DEBUG) << "Added key " << key_id << " (" << ref << ") to keyring " << device_keyring
|
|
|
|
<< " in process " << getpid();
|
2016-06-02 20:01:19 +02:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool evictKey(const std::string& raw_ref) {
|
|
|
|
key_serial_t device_keyring;
|
2018-10-23 22:06:55 +02:00
|
|
|
if (!fscryptKeyring(&device_keyring)) return false;
|
2017-06-20 01:05:55 +02:00
|
|
|
bool success = true;
|
|
|
|
for (char const* const* name_prefix = NAME_PREFIXES; *name_prefix != nullptr; name_prefix++) {
|
|
|
|
auto ref = keyname(*name_prefix, raw_ref);
|
|
|
|
auto key_serial = keyctl_search(device_keyring, "logon", ref.c_str(), 0);
|
|
|
|
|
|
|
|
// Unlink the key from the keyring. Prefer unlinking to revoking or
|
|
|
|
// invalidating, since unlinking is actually no less secure currently, and
|
|
|
|
// it avoids bugs in certain kernel versions where the keyring key is
|
|
|
|
// referenced from places it shouldn't be.
|
|
|
|
if (keyctl_unlink(key_serial, device_keyring) != 0) {
|
|
|
|
PLOG(ERROR) << "Failed to unlink key with serial " << key_serial << " ref " << ref;
|
|
|
|
success = false;
|
|
|
|
} else {
|
|
|
|
LOG(DEBUG) << "Unlinked key with serial " << key_serial << " ref " << ref;
|
|
|
|
}
|
2016-06-02 20:01:19 +02:00
|
|
|
}
|
2017-06-20 01:05:55 +02:00
|
|
|
return success;
|
2016-06-02 20:01:19 +02:00
|
|
|
}
|
|
|
|
|
2017-10-26 20:16:39 +02:00
|
|
|
bool retrieveAndInstallKey(bool create_if_absent, const KeyAuthentication& key_authentication,
|
|
|
|
const std::string& key_path, const std::string& tmp_path,
|
|
|
|
std::string* key_ref) {
|
2017-08-01 18:15:53 +02:00
|
|
|
KeyBuffer key;
|
2016-06-02 20:01:19 +02:00
|
|
|
if (pathExists(key_path)) {
|
|
|
|
LOG(DEBUG) << "Key exists, using: " << key_path;
|
2017-10-26 20:16:39 +02:00
|
|
|
if (!retrieveKey(key_path, key_authentication, &key)) return false;
|
2016-06-02 20:01:19 +02:00
|
|
|
} else {
|
|
|
|
if (!create_if_absent) {
|
2018-09-18 22:30:21 +02:00
|
|
|
LOG(ERROR) << "No key found in " << key_path;
|
|
|
|
return false;
|
2016-06-02 20:01:19 +02:00
|
|
|
}
|
|
|
|
LOG(INFO) << "Creating new key in " << key_path;
|
|
|
|
if (!randomKey(&key)) return false;
|
2017-10-26 20:16:39 +02:00
|
|
|
if (!storeKeyAtomically(key_path, tmp_path, key_authentication, key)) return false;
|
2016-06-02 20:01:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!installKey(key, key_ref)) {
|
|
|
|
LOG(ERROR) << "Failed to install key in " << key_path;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-09-18 22:30:21 +02:00
|
|
|
bool retrieveKey(bool create_if_absent, const std::string& key_path, const std::string& tmp_path,
|
|
|
|
KeyBuffer* key) {
|
2016-06-02 20:04:27 +02:00
|
|
|
if (pathExists(key_path)) {
|
|
|
|
LOG(DEBUG) << "Key exists, using: " << key_path;
|
|
|
|
if (!retrieveKey(key_path, kEmptyAuthentication, key)) return false;
|
|
|
|
} else {
|
|
|
|
if (!create_if_absent) {
|
2018-09-18 22:30:21 +02:00
|
|
|
LOG(ERROR) << "No key found in " << key_path;
|
|
|
|
return false;
|
2016-06-02 20:04:27 +02:00
|
|
|
}
|
|
|
|
LOG(INFO) << "Creating new key in " << key_path;
|
|
|
|
if (!randomKey(key)) return false;
|
2018-09-18 22:30:21 +02:00
|
|
|
if (!storeKeyAtomically(key_path, tmp_path, kEmptyAuthentication, *key)) return false;
|
2016-06-02 20:04:27 +02:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-06-02 20:01:19 +02:00
|
|
|
} // namespace vold
|
|
|
|
} // namespace android
|