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-10-26 15:27:10 +02:00
|
|
|
#include <keystore/keymaster_tags.h>
|
|
|
|
#include <keystore/authorization_set.h>
|
|
|
|
#include <keystore/keystore_hidl_support.h>
|
|
|
|
|
|
|
|
using namespace ::keystore;
|
2016-01-21 21:26:12 +01:00
|
|
|
|
|
|
|
namespace android {
|
|
|
|
namespace vold {
|
|
|
|
|
2016-03-16 01:04:39 +01:00
|
|
|
KeymasterOperation::~KeymasterOperation() {
|
2016-10-26 15:27:10 +02:00
|
|
|
if (mDevice.get()) mDevice->abort(mOpHandle);
|
2016-03-16 01:04:39 +01:00
|
|
|
}
|
|
|
|
|
2016-03-09 18:31:37 +01:00
|
|
|
bool KeymasterOperation::updateCompletely(const std::string& input, std::string* output) {
|
2017-02-06 19:11:22 +01:00
|
|
|
output->clear();
|
2016-01-21 21:26:12 +01:00
|
|
|
auto it = input.begin();
|
2016-10-26 15:27:10 +02:00
|
|
|
uint32_t inputConsumed;
|
|
|
|
|
|
|
|
ErrorCode km_error;
|
|
|
|
auto hidlCB = [&] (ErrorCode ret, uint32_t _inputConsumed,
|
|
|
|
const hidl_vec<KeyParameter>& /*ignored*/, const hidl_vec<uint8_t>& _output) {
|
|
|
|
km_error = ret;
|
|
|
|
if (km_error != ErrorCode::OK) return;
|
|
|
|
inputConsumed = _inputConsumed;
|
|
|
|
if (output)
|
|
|
|
output->append(reinterpret_cast<const char*>(&_output[0]), _output.size());
|
|
|
|
};
|
|
|
|
|
2016-01-21 21:26:12 +01:00
|
|
|
while (it != input.end()) {
|
2016-01-27 15:30:22 +01:00
|
|
|
size_t toRead = static_cast<size_t>(input.end() - it);
|
2016-10-26 15:27:10 +02:00
|
|
|
auto inputBlob = blob2hidlVec(reinterpret_cast<const uint8_t*>(&*it), toRead);
|
|
|
|
auto error = mDevice->update(mOpHandle, hidl_vec<KeyParameter>(), inputBlob, hidlCB);
|
|
|
|
if (!error.isOk()) {
|
|
|
|
LOG(ERROR) << "update failed: " << error.description();
|
|
|
|
mDevice = nullptr;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (km_error != ErrorCode::OK) {
|
|
|
|
LOG(ERROR) << "update failed, code " << uint32_t(km_error);
|
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
|
|
|
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-10-26 15:27:10 +02:00
|
|
|
ErrorCode km_error;
|
|
|
|
auto hidlCb = [&] (ErrorCode ret, const hidl_vec<KeyParameter>& /*ignored*/,
|
|
|
|
const hidl_vec<uint8_t>& _output) {
|
|
|
|
km_error = ret;
|
|
|
|
if (km_error != ErrorCode::OK) return;
|
|
|
|
if (output)
|
|
|
|
output->assign(reinterpret_cast<const char*>(&_output[0]), _output.size());
|
|
|
|
};
|
|
|
|
auto error = mDevice->finish(mOpHandle, hidl_vec<KeyParameter>(), hidl_vec<uint8_t>(),
|
|
|
|
hidl_vec<uint8_t>(), hidlCb);
|
2016-01-27 15:30:22 +01:00
|
|
|
mDevice = nullptr;
|
2016-10-26 15:27:10 +02:00
|
|
|
if (!error.isOk()) {
|
|
|
|
LOG(ERROR) << "finish failed: " << error.description();
|
2016-01-21 21:26:12 +01:00
|
|
|
return false;
|
|
|
|
}
|
2016-10-26 15:27:10 +02:00
|
|
|
if (km_error != ErrorCode::OK) {
|
|
|
|
LOG(ERROR) << "finish failed, code " << uint32_t(km_error);
|
|
|
|
return false;
|
2016-05-16 17:14:56 +02:00
|
|
|
}
|
2016-01-21 21:26:12 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
Keymaster::Keymaster() {
|
2016-10-26 15:27:10 +02:00
|
|
|
mDevice = ::android::hardware::keymaster::V3_0::IKeymasterDevice::getService("keymaster");
|
2016-01-21 21:26:12 +01:00
|
|
|
}
|
|
|
|
|
2016-10-26 15:27:10 +02:00
|
|
|
bool Keymaster::generateKey(const AuthorizationSet& inParams, std::string* key) {
|
|
|
|
ErrorCode km_error;
|
|
|
|
auto hidlCb = [&] (ErrorCode ret, const hidl_vec<uint8_t>& keyBlob,
|
|
|
|
const KeyCharacteristics& /*ignored*/) {
|
|
|
|
km_error = ret;
|
|
|
|
if (km_error != ErrorCode::OK) return;
|
|
|
|
if (key)
|
|
|
|
key->assign(reinterpret_cast<const char*>(&keyBlob[0]), keyBlob.size());
|
|
|
|
};
|
|
|
|
|
|
|
|
auto error = mDevice->generateKey(inParams.hidl_data(), hidlCb);
|
|
|
|
if (!error.isOk()) {
|
|
|
|
LOG(ERROR) << "generate_key failed: " << error.description();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (km_error != ErrorCode::OK) {
|
|
|
|
LOG(ERROR) << "generate_key failed, code " << int32_t(km_error);
|
2016-01-21 21:26:12 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-03-09 18:31:37 +01:00
|
|
|
bool Keymaster::deleteKey(const std::string& key) {
|
2016-10-26 15:27:10 +02:00
|
|
|
auto keyBlob = blob2hidlVec(key);
|
|
|
|
auto error = mDevice->deleteKey(keyBlob);
|
|
|
|
if (!error.isOk()) {
|
|
|
|
LOG(ERROR) << "delete_key failed: " << error.description();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (ErrorCode(error) != ErrorCode::OK) {
|
|
|
|
LOG(ERROR) << "delete_key failed, code " << uint32_t(ErrorCode(error));
|
2016-01-21 21:26:12 +01:00
|
|
|
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) {
|
2016-10-26 15:27:10 +02:00
|
|
|
auto oldKeyBlob = blob2hidlVec(oldKey);
|
|
|
|
ErrorCode km_error;
|
|
|
|
auto hidlCb = [&] (ErrorCode ret, const hidl_vec<uint8_t>& upgradedKeyBlob) {
|
|
|
|
km_error = ret;
|
|
|
|
if (km_error != ErrorCode::OK) return;
|
|
|
|
if (newKey)
|
|
|
|
newKey->assign(reinterpret_cast<const char*>(&upgradedKeyBlob[0]),
|
|
|
|
upgradedKeyBlob.size());
|
|
|
|
};
|
|
|
|
auto error = mDevice->upgradeKey(oldKeyBlob, inParams.hidl_data(), hidlCb);
|
|
|
|
if (!error.isOk()) {
|
|
|
|
LOG(ERROR) << "upgrade_key failed: " << error.description();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (km_error != ErrorCode::OK) {
|
|
|
|
LOG(ERROR) << "upgrade_key failed, code " << int32_t(km_error);
|
2016-05-16 17:14:56 +02:00
|
|
|
return false;
|
2016-01-21 21:26:12 +01:00
|
|
|
}
|
2016-05-16 17:14:56 +02:00
|
|
|
return true;
|
2016-01-21 21:26:12 +01:00
|
|
|
}
|
|
|
|
|
2016-10-26 15:27:10 +02:00
|
|
|
KeymasterOperation Keymaster::begin(KeyPurpose purpose, const std::string& key,
|
|
|
|
const AuthorizationSet& inParams,
|
|
|
|
AuthorizationSet* outParams) {
|
|
|
|
auto keyBlob = blob2hidlVec(key);
|
|
|
|
uint64_t mOpHandle;
|
|
|
|
ErrorCode km_error;
|
|
|
|
|
|
|
|
auto hidlCb = [&] (ErrorCode ret, const hidl_vec<KeyParameter>& _outParams,
|
|
|
|
uint64_t operationHandle) {
|
|
|
|
km_error = ret;
|
|
|
|
if (km_error != ErrorCode::OK) return;
|
|
|
|
if (outParams)
|
|
|
|
*outParams = _outParams;
|
|
|
|
mOpHandle = operationHandle;
|
|
|
|
};
|
|
|
|
|
|
|
|
auto error = mDevice->begin(purpose, keyBlob, inParams.hidl_data(), hidlCb);
|
|
|
|
if (!error.isOk()) {
|
|
|
|
LOG(ERROR) << "begin failed: " << error.description();
|
|
|
|
return KeymasterOperation(ErrorCode::UNKNOWN_ERROR);
|
|
|
|
}
|
|
|
|
if (km_error != ErrorCode::OK) {
|
|
|
|
LOG(ERROR) << "begin failed, code " << uint32_t(km_error);
|
|
|
|
return KeymasterOperation(km_error);
|
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
|