2016-01-21 21:26:12 +01: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 "Keymaster.h"
|
|
|
|
|
|
|
|
#include <android-base/logging.h>
|
2016-03-16 01:04:39 +01:00
|
|
|
#include <hardware/hardware.h>
|
|
|
|
#include <hardware/keymaster1.h>
|
|
|
|
#include <hardware/keymaster2.h>
|
2016-05-16 17:14:56 +02:00
|
|
|
#include <keymaster/keymaster_configuration.h>
|
2016-01-21 21:26:12 +01:00
|
|
|
|
|
|
|
namespace android {
|
|
|
|
namespace vold {
|
|
|
|
|
2016-03-16 01:04:39 +01:00
|
|
|
class IKeymasterDevice {
|
|
|
|
public:
|
|
|
|
IKeymasterDevice() {}
|
|
|
|
virtual ~IKeymasterDevice() {}
|
|
|
|
virtual keymaster_error_t generate_key(const keymaster_key_param_set_t* params,
|
|
|
|
keymaster_key_blob_t* key_blob) const = 0;
|
|
|
|
virtual keymaster_error_t delete_key(const keymaster_key_blob_t* key) const = 0;
|
2016-05-16 17:14:56 +02:00
|
|
|
virtual keymaster_error_t upgrade_key(const keymaster_key_blob_t* key_to_upgrade,
|
|
|
|
const keymaster_key_param_set_t* upgrade_params,
|
|
|
|
keymaster_key_blob_t* upgraded_key) const = 0;
|
2016-03-16 01:04:39 +01:00
|
|
|
virtual keymaster_error_t begin(keymaster_purpose_t purpose, const keymaster_key_blob_t* key,
|
|
|
|
const keymaster_key_param_set_t* in_params,
|
|
|
|
keymaster_key_param_set_t* out_params,
|
|
|
|
keymaster_operation_handle_t* operation_handle) const = 0;
|
|
|
|
virtual keymaster_error_t update(keymaster_operation_handle_t operation_handle,
|
|
|
|
const keymaster_key_param_set_t* in_params,
|
|
|
|
const keymaster_blob_t* input, size_t* input_consumed,
|
|
|
|
keymaster_key_param_set_t* out_params,
|
|
|
|
keymaster_blob_t* output) const = 0;
|
|
|
|
virtual keymaster_error_t finish(keymaster_operation_handle_t operation_handle,
|
|
|
|
const keymaster_key_param_set_t* in_params,
|
|
|
|
const keymaster_blob_t* signature,
|
|
|
|
keymaster_key_param_set_t* out_params,
|
|
|
|
keymaster_blob_t* output) const = 0;
|
|
|
|
virtual keymaster_error_t abort(keymaster_operation_handle_t operation_handle) const = 0;
|
|
|
|
|
|
|
|
protected:
|
|
|
|
DISALLOW_COPY_AND_ASSIGN(IKeymasterDevice);
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename T> class KeymasterDevice : public IKeymasterDevice {
|
|
|
|
public:
|
2016-04-29 23:42:01 +02:00
|
|
|
explicit KeymasterDevice(T* d) : mDevice{d} {}
|
2016-03-16 01:04:39 +01:00
|
|
|
keymaster_error_t generate_key(const keymaster_key_param_set_t* params,
|
|
|
|
keymaster_key_blob_t* key_blob) const override final {
|
|
|
|
return mDevice->generate_key(mDevice, params, key_blob, nullptr);
|
|
|
|
}
|
|
|
|
keymaster_error_t delete_key(const keymaster_key_blob_t* key) const override final {
|
|
|
|
if (mDevice->delete_key == nullptr) return KM_ERROR_OK;
|
|
|
|
return mDevice->delete_key(mDevice, key);
|
|
|
|
}
|
|
|
|
keymaster_error_t begin(keymaster_purpose_t purpose, const keymaster_key_blob_t* key,
|
|
|
|
const keymaster_key_param_set_t* in_params,
|
|
|
|
keymaster_key_param_set_t* out_params,
|
|
|
|
keymaster_operation_handle_t* operation_handle) const override final {
|
|
|
|
return mDevice->begin(mDevice, purpose, key, in_params, out_params, operation_handle);
|
|
|
|
}
|
|
|
|
keymaster_error_t update(keymaster_operation_handle_t operation_handle,
|
|
|
|
const keymaster_key_param_set_t* in_params,
|
|
|
|
const keymaster_blob_t* input, size_t* input_consumed,
|
|
|
|
keymaster_key_param_set_t* out_params,
|
|
|
|
keymaster_blob_t* output) const override final {
|
|
|
|
return mDevice->update(mDevice, operation_handle, in_params, input, input_consumed,
|
|
|
|
out_params, output);
|
|
|
|
}
|
|
|
|
keymaster_error_t abort(keymaster_operation_handle_t operation_handle) const override final {
|
|
|
|
return mDevice->abort(mDevice, operation_handle);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
T* const mDevice;
|
|
|
|
};
|
|
|
|
|
|
|
|
class Keymaster1Device : public KeymasterDevice<keymaster1_device_t> {
|
|
|
|
public:
|
2016-04-29 23:42:01 +02:00
|
|
|
explicit Keymaster1Device(keymaster1_device_t* d) : KeymasterDevice<keymaster1_device_t>{d} {}
|
2016-03-16 01:04:39 +01:00
|
|
|
~Keymaster1Device() override final { keymaster1_close(mDevice); }
|
2016-05-16 17:14:56 +02:00
|
|
|
keymaster_error_t upgrade_key(const keymaster_key_blob_t* key_to_upgrade,
|
|
|
|
const keymaster_key_param_set_t* upgrade_params,
|
|
|
|
keymaster_key_blob_t* upgraded_key) const override final {
|
|
|
|
return KM_ERROR_UNIMPLEMENTED;
|
|
|
|
}
|
2016-03-16 01:04:39 +01:00
|
|
|
keymaster_error_t finish(keymaster_operation_handle_t operation_handle,
|
|
|
|
const keymaster_key_param_set_t* in_params,
|
|
|
|
const keymaster_blob_t* signature,
|
|
|
|
keymaster_key_param_set_t* out_params,
|
|
|
|
keymaster_blob_t* output) const override final {
|
|
|
|
return mDevice->finish(mDevice, operation_handle, in_params, signature, out_params, output);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class Keymaster2Device : public KeymasterDevice<keymaster2_device_t> {
|
|
|
|
public:
|
2016-04-29 23:42:01 +02:00
|
|
|
explicit Keymaster2Device(keymaster2_device_t* d) : KeymasterDevice<keymaster2_device_t>{d} {}
|
2016-03-16 01:04:39 +01:00
|
|
|
~Keymaster2Device() override final { keymaster2_close(mDevice); }
|
2016-05-16 17:14:56 +02:00
|
|
|
keymaster_error_t upgrade_key(const keymaster_key_blob_t* key_to_upgrade,
|
|
|
|
const keymaster_key_param_set_t* upgrade_params,
|
|
|
|
keymaster_key_blob_t* upgraded_key) const override final {
|
|
|
|
return mDevice->upgrade_key(mDevice, key_to_upgrade, upgrade_params, upgraded_key);
|
|
|
|
}
|
2016-03-16 01:04:39 +01:00
|
|
|
keymaster_error_t finish(keymaster_operation_handle_t operation_handle,
|
|
|
|
const keymaster_key_param_set_t* in_params,
|
|
|
|
const keymaster_blob_t* signature,
|
|
|
|
keymaster_key_param_set_t* out_params,
|
|
|
|
keymaster_blob_t* output) const override final {
|
|
|
|
return mDevice->finish(mDevice, operation_handle, in_params, nullptr, signature, out_params,
|
|
|
|
output);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
KeymasterOperation::~KeymasterOperation() {
|
|
|
|
if (mDevice) mDevice->abort(mOpHandle);
|
|
|
|
}
|
|
|
|
|
2016-03-09 18:31:37 +01:00
|
|
|
bool KeymasterOperation::updateCompletely(const std::string& input, std::string* output) {
|
2016-03-09 01:08:32 +01:00
|
|
|
output->clear();
|
2016-01-21 21:26:12 +01:00
|
|
|
auto it = input.begin();
|
|
|
|
while (it != input.end()) {
|
2016-01-27 15:30:22 +01:00
|
|
|
size_t toRead = static_cast<size_t>(input.end() - it);
|
2016-03-09 18:31:37 +01:00
|
|
|
keymaster_blob_t inputBlob{reinterpret_cast<const uint8_t*>(&*it), toRead};
|
2016-01-27 15:30:22 +01:00
|
|
|
keymaster_blob_t outputBlob;
|
|
|
|
size_t inputConsumed;
|
2016-03-16 01:04:39 +01:00
|
|
|
auto error =
|
|
|
|
mDevice->update(mOpHandle, nullptr, &inputBlob, &inputConsumed, nullptr, &outputBlob);
|
2016-01-21 21:26:12 +01:00
|
|
|
if (error != KM_ERROR_OK) {
|
|
|
|
LOG(ERROR) << "update failed, code " << error;
|
2016-01-27 15:30:22 +01:00
|
|
|
mDevice = nullptr;
|
2016-01-21 21:26:12 +01:00
|
|
|
return false;
|
|
|
|
}
|
2016-03-09 18:31:37 +01:00
|
|
|
output->append(reinterpret_cast<const char*>(outputBlob.data), outputBlob.data_length);
|
|
|
|
free(const_cast<uint8_t*>(outputBlob.data));
|
2016-01-27 15:30:22 +01:00
|
|
|
if (inputConsumed > toRead) {
|
2016-01-21 21:26:12 +01:00
|
|
|
LOG(ERROR) << "update reported too much input consumed";
|
2016-01-27 15:30:22 +01:00
|
|
|
mDevice = nullptr;
|
2016-01-21 21:26:12 +01:00
|
|
|
return false;
|
|
|
|
}
|
2016-01-27 15:30:22 +01:00
|
|
|
it += inputConsumed;
|
2016-01-21 21:26:12 +01:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-05-16 17:14:56 +02:00
|
|
|
bool KeymasterOperation::finish(std::string* output) {
|
2016-01-27 15:30:22 +01:00
|
|
|
keymaster_blob_t outputBlob;
|
2016-05-16 17:14:56 +02:00
|
|
|
auto error = mDevice->finish(mOpHandle, nullptr, nullptr, nullptr,
|
|
|
|
output ? &outputBlob : nullptr);
|
2016-01-27 15:30:22 +01:00
|
|
|
mDevice = nullptr;
|
2016-01-21 21:26:12 +01:00
|
|
|
if (error != KM_ERROR_OK) {
|
|
|
|
LOG(ERROR) << "finish failed, code " << error;
|
|
|
|
return false;
|
|
|
|
}
|
2016-05-16 17:14:56 +02:00
|
|
|
if (output) {
|
|
|
|
output->assign(reinterpret_cast<const char*>(outputBlob.data), outputBlob.data_length);
|
|
|
|
free(const_cast<uint8_t*>(outputBlob.data));
|
|
|
|
}
|
2016-01-21 21:26:12 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
Keymaster::Keymaster() {
|
2016-01-27 15:30:22 +01:00
|
|
|
mDevice = nullptr;
|
2016-03-09 18:31:37 +01:00
|
|
|
const hw_module_t* module;
|
2016-01-21 21:26:12 +01:00
|
|
|
int ret = hw_get_module_by_class(KEYSTORE_HARDWARE_MODULE_ID, NULL, &module);
|
|
|
|
if (ret != 0) {
|
|
|
|
LOG(ERROR) << "hw_get_module_by_class returned " << ret;
|
|
|
|
return;
|
|
|
|
}
|
2016-05-16 17:14:56 +02:00
|
|
|
LOG(DEBUG) << "module_api_version is " << module->module_api_version;
|
2016-03-16 01:04:39 +01:00
|
|
|
if (module->module_api_version == KEYMASTER_MODULE_API_VERSION_1_0) {
|
|
|
|
keymaster1_device_t* device;
|
|
|
|
ret = keymaster1_open(module, &device);
|
|
|
|
if (ret != 0) {
|
|
|
|
LOG(ERROR) << "keymaster1_open returned " << ret;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
mDevice = std::make_shared<Keymaster1Device>(device);
|
|
|
|
} else if (module->module_api_version == KEYMASTER_MODULE_API_VERSION_2_0) {
|
|
|
|
keymaster2_device_t* device;
|
|
|
|
ret = keymaster2_open(module, &device);
|
|
|
|
if (ret != 0) {
|
|
|
|
LOG(ERROR) << "keymaster2_open returned " << ret;
|
|
|
|
return;
|
|
|
|
}
|
2016-05-16 17:14:56 +02:00
|
|
|
auto error = ConfigureDevice(device);
|
|
|
|
if (error != KM_ERROR_OK) {
|
|
|
|
LOG(ERROR) << "ConfigureDevice returned " << error;
|
|
|
|
return;
|
|
|
|
}
|
2016-03-16 01:04:39 +01:00
|
|
|
mDevice = std::make_shared<Keymaster2Device>(device);
|
|
|
|
} else {
|
2016-01-21 21:26:12 +01:00
|
|
|
LOG(ERROR) << "module_api_version is " << module->module_api_version;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-09 18:31:37 +01:00
|
|
|
bool Keymaster::generateKey(const keymaster::AuthorizationSet& inParams, std::string* key) {
|
2016-01-27 15:30:22 +01:00
|
|
|
keymaster_key_blob_t keyBlob;
|
2016-03-16 01:04:39 +01:00
|
|
|
auto error = mDevice->generate_key(&inParams, &keyBlob);
|
2016-01-21 21:26:12 +01:00
|
|
|
if (error != KM_ERROR_OK) {
|
|
|
|
LOG(ERROR) << "generate_key failed, code " << error;
|
|
|
|
return false;
|
|
|
|
}
|
2016-03-09 18:31:37 +01:00
|
|
|
key->assign(reinterpret_cast<const char*>(keyBlob.key_material), keyBlob.key_material_size);
|
|
|
|
free(const_cast<uint8_t*>(keyBlob.key_material));
|
2016-01-21 21:26:12 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-03-09 18:31:37 +01:00
|
|
|
bool Keymaster::deleteKey(const std::string& key) {
|
|
|
|
keymaster_key_blob_t keyBlob{reinterpret_cast<const uint8_t*>(key.data()), key.size()};
|
2016-03-16 01:04:39 +01:00
|
|
|
auto error = mDevice->delete_key(&keyBlob);
|
2016-01-21 21:26:12 +01:00
|
|
|
if (error != KM_ERROR_OK) {
|
|
|
|
LOG(ERROR) << "delete_key failed, code " << error;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-05-16 17:14:56 +02:00
|
|
|
bool Keymaster::upgradeKey(const std::string& oldKey, const AuthorizationSet& inParams,
|
|
|
|
std::string* newKey) {
|
|
|
|
keymaster_key_blob_t oldKeyBlob{reinterpret_cast<const uint8_t*>(oldKey.data()), oldKey.size()};
|
|
|
|
keymaster_key_blob_t newKeyBlob;
|
|
|
|
auto error = mDevice->upgrade_key(&oldKeyBlob, &inParams, &newKeyBlob);
|
2016-01-21 21:26:12 +01:00
|
|
|
if (error != KM_ERROR_OK) {
|
2016-05-16 17:14:56 +02:00
|
|
|
LOG(ERROR) << "upgrade_key failed, code " << error;
|
|
|
|
return false;
|
2016-01-21 21:26:12 +01:00
|
|
|
}
|
2016-05-16 17:14:56 +02:00
|
|
|
newKey->assign(reinterpret_cast<const char*>(newKeyBlob.key_material),
|
|
|
|
newKeyBlob.key_material_size);
|
|
|
|
free(const_cast<uint8_t*>(newKeyBlob.key_material));
|
|
|
|
return true;
|
2016-01-21 21:26:12 +01:00
|
|
|
}
|
|
|
|
|
2016-03-09 18:31:37 +01:00
|
|
|
KeymasterOperation Keymaster::begin(keymaster_purpose_t purpose, const std::string& key,
|
2016-05-16 17:14:56 +02:00
|
|
|
const keymaster::AuthorizationSet& inParams,
|
|
|
|
keymaster::AuthorizationSet* outParams) {
|
2016-03-09 18:31:37 +01:00
|
|
|
keymaster_key_blob_t keyBlob{reinterpret_cast<const uint8_t*>(key.data()), key.size()};
|
2016-01-27 15:30:22 +01:00
|
|
|
keymaster_operation_handle_t mOpHandle;
|
2016-05-16 17:14:56 +02:00
|
|
|
keymaster_key_param_set_t outParams_set;
|
|
|
|
auto error = mDevice->begin(purpose, &keyBlob, &inParams,
|
|
|
|
outParams ? &outParams_set : nullptr, &mOpHandle);
|
2016-01-21 21:26:12 +01:00
|
|
|
if (error != KM_ERROR_OK) {
|
|
|
|
LOG(ERROR) << "begin failed, code " << error;
|
2016-05-16 17:14:56 +02:00
|
|
|
return KeymasterOperation(error);
|
|
|
|
}
|
|
|
|
if (outParams) {
|
|
|
|
outParams->Clear();
|
|
|
|
outParams->push_back(outParams_set);
|
|
|
|
keymaster_free_param_set(&outParams_set);
|
2016-01-21 21:26:12 +01:00
|
|
|
}
|
2016-01-27 15:30:22 +01:00
|
|
|
return KeymasterOperation(mDevice, mOpHandle);
|
2016-01-21 21:26:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace vold
|
|
|
|
} // namespace android
|