blob: bb99cde1c5c35879f55cd8aee711d60f5608d6c5 [file] [log] [blame]
Paul Crowley1ef25582016-01-21 20:26:12 +00001/*
2 * Copyright (C) 2016 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#include "Keymaster.h"
18
19#include <android-base/logging.h>
Janis Danisevskis8e537b82016-10-26 14:27:10 +010020#include <keystore/keymaster_tags.h>
21#include <keystore/authorization_set.h>
22#include <keystore/keystore_hidl_support.h>
23
24using namespace ::keystore;
Paul Crowley1ef25582016-01-21 20:26:12 +000025
26namespace android {
27namespace vold {
28
Paul Crowley0323afd2016-03-15 17:04:39 -070029KeymasterOperation::~KeymasterOperation() {
Janis Danisevskis8e537b82016-10-26 14:27:10 +010030 if (mDevice.get()) mDevice->abort(mOpHandle);
Paul Crowley0323afd2016-03-15 17:04:39 -070031}
32
Paul Crowleydf528a72016-03-09 09:31:37 -080033bool KeymasterOperation::updateCompletely(const std::string& input, std::string* output) {
Paul Crowleya051eb72016-03-08 16:08:32 -080034 output->clear();
Paul Crowley1ef25582016-01-21 20:26:12 +000035 auto it = input.begin();
Janis Danisevskis8e537b82016-10-26 14:27:10 +010036 uint32_t inputConsumed;
37
38 ErrorCode km_error;
39 auto hidlCB = [&] (ErrorCode ret, uint32_t _inputConsumed,
40 const hidl_vec<KeyParameter>& /*ignored*/, const hidl_vec<uint8_t>& _output) {
41 km_error = ret;
42 if (km_error != ErrorCode::OK) return;
43 inputConsumed = _inputConsumed;
44 if (output)
45 output->append(reinterpret_cast<const char*>(&_output[0]), _output.size());
46 };
47
Paul Crowley1ef25582016-01-21 20:26:12 +000048 while (it != input.end()) {
Paul Crowley13ffd8e2016-01-27 14:30:22 +000049 size_t toRead = static_cast<size_t>(input.end() - it);
Janis Danisevskis8e537b82016-10-26 14:27:10 +010050 auto inputBlob = blob2hidlVec(reinterpret_cast<const uint8_t*>(&*it), toRead);
51 auto error = mDevice->update(mOpHandle, hidl_vec<KeyParameter>(), inputBlob, hidlCB);
52 if (!error.isOk()) {
53 LOG(ERROR) << "update failed: " << error.description();
Paul Crowley13ffd8e2016-01-27 14:30:22 +000054 mDevice = nullptr;
Paul Crowley1ef25582016-01-21 20:26:12 +000055 return false;
56 }
Janis Danisevskis8e537b82016-10-26 14:27:10 +010057 if (km_error != ErrorCode::OK) {
58 LOG(ERROR) << "update failed, code " << uint32_t(km_error);
59 mDevice = nullptr;
60 return false;
61 }
Paul Crowley13ffd8e2016-01-27 14:30:22 +000062 if (inputConsumed > toRead) {
Paul Crowley1ef25582016-01-21 20:26:12 +000063 LOG(ERROR) << "update reported too much input consumed";
Paul Crowley13ffd8e2016-01-27 14:30:22 +000064 mDevice = nullptr;
Paul Crowley1ef25582016-01-21 20:26:12 +000065 return false;
66 }
Paul Crowley13ffd8e2016-01-27 14:30:22 +000067 it += inputConsumed;
Paul Crowley1ef25582016-01-21 20:26:12 +000068 }
69 return true;
70}
71
Paul Crowleydff8c722016-05-16 08:14:56 -070072bool KeymasterOperation::finish(std::string* output) {
Janis Danisevskis8e537b82016-10-26 14:27:10 +010073 ErrorCode km_error;
74 auto hidlCb = [&] (ErrorCode ret, const hidl_vec<KeyParameter>& /*ignored*/,
75 const hidl_vec<uint8_t>& _output) {
76 km_error = ret;
77 if (km_error != ErrorCode::OK) return;
78 if (output)
79 output->assign(reinterpret_cast<const char*>(&_output[0]), _output.size());
80 };
81 auto error = mDevice->finish(mOpHandle, hidl_vec<KeyParameter>(), hidl_vec<uint8_t>(),
82 hidl_vec<uint8_t>(), hidlCb);
Paul Crowley13ffd8e2016-01-27 14:30:22 +000083 mDevice = nullptr;
Janis Danisevskis8e537b82016-10-26 14:27:10 +010084 if (!error.isOk()) {
85 LOG(ERROR) << "finish failed: " << error.description();
Paul Crowley1ef25582016-01-21 20:26:12 +000086 return false;
87 }
Janis Danisevskis8e537b82016-10-26 14:27:10 +010088 if (km_error != ErrorCode::OK) {
89 LOG(ERROR) << "finish failed, code " << uint32_t(km_error);
90 return false;
Paul Crowleydff8c722016-05-16 08:14:56 -070091 }
Paul Crowley1ef25582016-01-21 20:26:12 +000092 return true;
93}
94
95Keymaster::Keymaster() {
Janis Danisevskis8e537b82016-10-26 14:27:10 +010096 mDevice = ::android::hardware::keymaster::V3_0::IKeymasterDevice::getService("keymaster");
Paul Crowley1ef25582016-01-21 20:26:12 +000097}
98
Janis Danisevskis8e537b82016-10-26 14:27:10 +010099bool Keymaster::generateKey(const AuthorizationSet& inParams, std::string* key) {
100 ErrorCode km_error;
101 auto hidlCb = [&] (ErrorCode ret, const hidl_vec<uint8_t>& keyBlob,
102 const KeyCharacteristics& /*ignored*/) {
103 km_error = ret;
104 if (km_error != ErrorCode::OK) return;
105 if (key)
106 key->assign(reinterpret_cast<const char*>(&keyBlob[0]), keyBlob.size());
107 };
108
109 auto error = mDevice->generateKey(inParams.hidl_data(), hidlCb);
110 if (!error.isOk()) {
111 LOG(ERROR) << "generate_key failed: " << error.description();
Paul Crowley1ef25582016-01-21 20:26:12 +0000112 return false;
113 }
Janis Danisevskis8e537b82016-10-26 14:27:10 +0100114 if (km_error != ErrorCode::OK) {
115 LOG(ERROR) << "generate_key failed, code " << int32_t(km_error);
116 return false;
117 }
Paul Crowley1ef25582016-01-21 20:26:12 +0000118 return true;
119}
120
Paul Crowleydf528a72016-03-09 09:31:37 -0800121bool Keymaster::deleteKey(const std::string& key) {
Janis Danisevskis8e537b82016-10-26 14:27:10 +0100122 auto keyBlob = blob2hidlVec(key);
123 auto error = mDevice->deleteKey(keyBlob);
124 if (!error.isOk()) {
125 LOG(ERROR) << "delete_key failed: " << error.description();
126 return false;
127 }
128 if (ErrorCode(error) != ErrorCode::OK) {
129 LOG(ERROR) << "delete_key failed, code " << uint32_t(ErrorCode(error));
Paul Crowley1ef25582016-01-21 20:26:12 +0000130 return false;
131 }
132 return true;
133}
134
Paul Crowleydff8c722016-05-16 08:14:56 -0700135bool Keymaster::upgradeKey(const std::string& oldKey, const AuthorizationSet& inParams,
136 std::string* newKey) {
Janis Danisevskis8e537b82016-10-26 14:27:10 +0100137 auto oldKeyBlob = blob2hidlVec(oldKey);
138 ErrorCode km_error;
139 auto hidlCb = [&] (ErrorCode ret, const hidl_vec<uint8_t>& upgradedKeyBlob) {
140 km_error = ret;
141 if (km_error != ErrorCode::OK) return;
142 if (newKey)
143 newKey->assign(reinterpret_cast<const char*>(&upgradedKeyBlob[0]),
144 upgradedKeyBlob.size());
145 };
146 auto error = mDevice->upgradeKey(oldKeyBlob, inParams.hidl_data(), hidlCb);
147 if (!error.isOk()) {
148 LOG(ERROR) << "upgrade_key failed: " << error.description();
Paul Crowleydff8c722016-05-16 08:14:56 -0700149 return false;
150 }
Janis Danisevskis8e537b82016-10-26 14:27:10 +0100151 if (km_error != ErrorCode::OK) {
152 LOG(ERROR) << "upgrade_key failed, code " << int32_t(km_error);
153 return false;
154 }
Paul Crowleydff8c722016-05-16 08:14:56 -0700155 return true;
156}
157
Janis Danisevskis8e537b82016-10-26 14:27:10 +0100158KeymasterOperation Keymaster::begin(KeyPurpose purpose, const std::string& key,
159 const AuthorizationSet& inParams,
160 AuthorizationSet* outParams) {
161 auto keyBlob = blob2hidlVec(key);
162 uint64_t mOpHandle;
163 ErrorCode km_error;
164
165 auto hidlCb = [&] (ErrorCode ret, const hidl_vec<KeyParameter>& _outParams,
166 uint64_t operationHandle) {
167 km_error = ret;
168 if (km_error != ErrorCode::OK) return;
169 if (outParams)
170 *outParams = _outParams;
171 mOpHandle = operationHandle;
172 };
173
174 auto error = mDevice->begin(purpose, keyBlob, inParams.hidl_data(), hidlCb);
175 if (!error.isOk()) {
176 LOG(ERROR) << "begin failed: " << error.description();
177 return KeymasterOperation(ErrorCode::UNKNOWN_ERROR);
Paul Crowley1ef25582016-01-21 20:26:12 +0000178 }
Janis Danisevskis8e537b82016-10-26 14:27:10 +0100179 if (km_error != ErrorCode::OK) {
180 LOG(ERROR) << "begin failed, code " << uint32_t(km_error);
181 return KeymasterOperation(km_error);
Paul Crowley1ef25582016-01-21 20:26:12 +0000182 }
Paul Crowley13ffd8e2016-01-27 14:30:22 +0000183 return KeymasterOperation(mDevice, mOpHandle);
Paul Crowley1ef25582016-01-21 20:26:12 +0000184}
185
186} // namespace vold
187} // namespace android