blob: ffa3a7a7a526e2a4b5fd65d28d9aaafa3ad6ddc0 [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;
Shawn Willden12e72ad2017-03-01 06:44:15 -070025using android::hardware::hidl_string;
Paul Crowley1ef25582016-01-21 20:26:12 +000026
27namespace android {
28namespace vold {
29
Paul Crowley0323afd2016-03-15 17:04:39 -070030KeymasterOperation::~KeymasterOperation() {
Janis Danisevskis8e537b82016-10-26 14:27:10 +010031 if (mDevice.get()) mDevice->abort(mOpHandle);
Paul Crowley0323afd2016-03-15 17:04:39 -070032}
33
Paul Crowleydf528a72016-03-09 09:31:37 -080034bool KeymasterOperation::updateCompletely(const std::string& input, std::string* output) {
Janis Danisevskis015ec302017-01-31 11:31:08 +000035 if (output)
36 output->clear();
Paul Crowley1ef25582016-01-21 20:26:12 +000037 auto it = input.begin();
Janis Danisevskis8e537b82016-10-26 14:27:10 +010038 uint32_t inputConsumed;
39
40 ErrorCode km_error;
41 auto hidlCB = [&] (ErrorCode ret, uint32_t _inputConsumed,
42 const hidl_vec<KeyParameter>& /*ignored*/, const hidl_vec<uint8_t>& _output) {
43 km_error = ret;
44 if (km_error != ErrorCode::OK) return;
45 inputConsumed = _inputConsumed;
46 if (output)
47 output->append(reinterpret_cast<const char*>(&_output[0]), _output.size());
48 };
49
Paul Crowley1ef25582016-01-21 20:26:12 +000050 while (it != input.end()) {
Paul Crowley13ffd8e2016-01-27 14:30:22 +000051 size_t toRead = static_cast<size_t>(input.end() - it);
Janis Danisevskis8e537b82016-10-26 14:27:10 +010052 auto inputBlob = blob2hidlVec(reinterpret_cast<const uint8_t*>(&*it), toRead);
53 auto error = mDevice->update(mOpHandle, hidl_vec<KeyParameter>(), inputBlob, hidlCB);
54 if (!error.isOk()) {
55 LOG(ERROR) << "update failed: " << error.description();
Paul Crowley13ffd8e2016-01-27 14:30:22 +000056 mDevice = nullptr;
Paul Crowley1ef25582016-01-21 20:26:12 +000057 return false;
58 }
Janis Danisevskis8e537b82016-10-26 14:27:10 +010059 if (km_error != ErrorCode::OK) {
Janis Danisevskis015ec302017-01-31 11:31:08 +000060 LOG(ERROR) << "update failed, code " << int32_t(km_error);
Janis Danisevskis8e537b82016-10-26 14:27:10 +010061 mDevice = nullptr;
62 return false;
63 }
Paul Crowley13ffd8e2016-01-27 14:30:22 +000064 if (inputConsumed > toRead) {
Paul Crowley1ef25582016-01-21 20:26:12 +000065 LOG(ERROR) << "update reported too much input consumed";
Paul Crowley13ffd8e2016-01-27 14:30:22 +000066 mDevice = nullptr;
Paul Crowley1ef25582016-01-21 20:26:12 +000067 return false;
68 }
Paul Crowley13ffd8e2016-01-27 14:30:22 +000069 it += inputConsumed;
Paul Crowley1ef25582016-01-21 20:26:12 +000070 }
71 return true;
72}
73
Paul Crowleydff8c722016-05-16 08:14:56 -070074bool KeymasterOperation::finish(std::string* output) {
Janis Danisevskis8e537b82016-10-26 14:27:10 +010075 ErrorCode km_error;
76 auto hidlCb = [&] (ErrorCode ret, const hidl_vec<KeyParameter>& /*ignored*/,
77 const hidl_vec<uint8_t>& _output) {
78 km_error = ret;
79 if (km_error != ErrorCode::OK) return;
80 if (output)
81 output->assign(reinterpret_cast<const char*>(&_output[0]), _output.size());
82 };
83 auto error = mDevice->finish(mOpHandle, hidl_vec<KeyParameter>(), hidl_vec<uint8_t>(),
84 hidl_vec<uint8_t>(), hidlCb);
Paul Crowley13ffd8e2016-01-27 14:30:22 +000085 mDevice = nullptr;
Janis Danisevskis8e537b82016-10-26 14:27:10 +010086 if (!error.isOk()) {
87 LOG(ERROR) << "finish failed: " << error.description();
Paul Crowley1ef25582016-01-21 20:26:12 +000088 return false;
89 }
Janis Danisevskis8e537b82016-10-26 14:27:10 +010090 if (km_error != ErrorCode::OK) {
Janis Danisevskis015ec302017-01-31 11:31:08 +000091 LOG(ERROR) << "finish failed, code " << int32_t(km_error);
Janis Danisevskis8e537b82016-10-26 14:27:10 +010092 return false;
Paul Crowleydff8c722016-05-16 08:14:56 -070093 }
Paul Crowley1ef25582016-01-21 20:26:12 +000094 return true;
95}
96
97Keymaster::Keymaster() {
Chris Phoenix9e8a5772017-01-25 15:15:46 -080098 mDevice = ::android::hardware::keymaster::V3_0::IKeymasterDevice::getService();
Paul Crowley1ef25582016-01-21 20:26:12 +000099}
100
Janis Danisevskis8e537b82016-10-26 14:27:10 +0100101bool Keymaster::generateKey(const AuthorizationSet& inParams, std::string* key) {
102 ErrorCode km_error;
103 auto hidlCb = [&] (ErrorCode ret, const hidl_vec<uint8_t>& keyBlob,
104 const KeyCharacteristics& /*ignored*/) {
105 km_error = ret;
106 if (km_error != ErrorCode::OK) return;
107 if (key)
108 key->assign(reinterpret_cast<const char*>(&keyBlob[0]), keyBlob.size());
109 };
110
111 auto error = mDevice->generateKey(inParams.hidl_data(), hidlCb);
112 if (!error.isOk()) {
113 LOG(ERROR) << "generate_key failed: " << error.description();
Paul Crowley1ef25582016-01-21 20:26:12 +0000114 return false;
115 }
Janis Danisevskis8e537b82016-10-26 14:27:10 +0100116 if (km_error != ErrorCode::OK) {
117 LOG(ERROR) << "generate_key failed, code " << int32_t(km_error);
118 return false;
119 }
Paul Crowley1ef25582016-01-21 20:26:12 +0000120 return true;
121}
122
Paul Crowleydf528a72016-03-09 09:31:37 -0800123bool Keymaster::deleteKey(const std::string& key) {
Janis Danisevskis8e537b82016-10-26 14:27:10 +0100124 auto keyBlob = blob2hidlVec(key);
125 auto error = mDevice->deleteKey(keyBlob);
126 if (!error.isOk()) {
127 LOG(ERROR) << "delete_key failed: " << error.description();
128 return false;
129 }
130 if (ErrorCode(error) != ErrorCode::OK) {
131 LOG(ERROR) << "delete_key failed, code " << uint32_t(ErrorCode(error));
Paul Crowley1ef25582016-01-21 20:26:12 +0000132 return false;
133 }
134 return true;
135}
136
Paul Crowleydff8c722016-05-16 08:14:56 -0700137bool Keymaster::upgradeKey(const std::string& oldKey, const AuthorizationSet& inParams,
138 std::string* newKey) {
Janis Danisevskis8e537b82016-10-26 14:27:10 +0100139 auto oldKeyBlob = blob2hidlVec(oldKey);
140 ErrorCode km_error;
141 auto hidlCb = [&] (ErrorCode ret, const hidl_vec<uint8_t>& upgradedKeyBlob) {
142 km_error = ret;
143 if (km_error != ErrorCode::OK) return;
144 if (newKey)
145 newKey->assign(reinterpret_cast<const char*>(&upgradedKeyBlob[0]),
146 upgradedKeyBlob.size());
147 };
148 auto error = mDevice->upgradeKey(oldKeyBlob, inParams.hidl_data(), hidlCb);
149 if (!error.isOk()) {
150 LOG(ERROR) << "upgrade_key failed: " << error.description();
Paul Crowleydff8c722016-05-16 08:14:56 -0700151 return false;
152 }
Janis Danisevskis8e537b82016-10-26 14:27:10 +0100153 if (km_error != ErrorCode::OK) {
154 LOG(ERROR) << "upgrade_key failed, code " << int32_t(km_error);
155 return false;
156 }
Paul Crowleydff8c722016-05-16 08:14:56 -0700157 return true;
158}
159
Janis Danisevskis8e537b82016-10-26 14:27:10 +0100160KeymasterOperation Keymaster::begin(KeyPurpose purpose, const std::string& key,
161 const AuthorizationSet& inParams,
162 AuthorizationSet* outParams) {
163 auto keyBlob = blob2hidlVec(key);
164 uint64_t mOpHandle;
165 ErrorCode km_error;
166
167 auto hidlCb = [&] (ErrorCode ret, const hidl_vec<KeyParameter>& _outParams,
168 uint64_t operationHandle) {
169 km_error = ret;
170 if (km_error != ErrorCode::OK) return;
171 if (outParams)
172 *outParams = _outParams;
173 mOpHandle = operationHandle;
174 };
175
176 auto error = mDevice->begin(purpose, keyBlob, inParams.hidl_data(), hidlCb);
177 if (!error.isOk()) {
178 LOG(ERROR) << "begin failed: " << error.description();
179 return KeymasterOperation(ErrorCode::UNKNOWN_ERROR);
Paul Crowley1ef25582016-01-21 20:26:12 +0000180 }
Janis Danisevskis8e537b82016-10-26 14:27:10 +0100181 if (km_error != ErrorCode::OK) {
Janis Danisevskis015ec302017-01-31 11:31:08 +0000182 LOG(ERROR) << "begin failed, code " << int32_t(km_error);
Janis Danisevskis8e537b82016-10-26 14:27:10 +0100183 return KeymasterOperation(km_error);
Paul Crowley1ef25582016-01-21 20:26:12 +0000184 }
Paul Crowley13ffd8e2016-01-27 14:30:22 +0000185 return KeymasterOperation(mDevice, mOpHandle);
Paul Crowley1ef25582016-01-21 20:26:12 +0000186}
Janis Danisevskis015ec302017-01-31 11:31:08 +0000187bool Keymaster::isSecure() {
188 bool _isSecure = false;
189 auto rc = mDevice->getHardwareFeatures(
Shawn Willden12e72ad2017-03-01 06:44:15 -0700190 [&] (bool isSecure, bool, bool, bool, bool, const hidl_string&, const hidl_string&) {
191 _isSecure = isSecure; });
Janis Danisevskis015ec302017-01-31 11:31:08 +0000192 return rc.isOk() && _isSecure;
193}
Paul Crowley1ef25582016-01-21 20:26:12 +0000194
195} // namespace vold
196} // namespace android
Janis Danisevskis015ec302017-01-31 11:31:08 +0000197
198using namespace ::android::vold;
199
200int keymaster_compatibility_cryptfs_scrypt() {
Janis Danisevskise7152c32017-03-08 11:02:30 -0800201 Keymaster dev;
202 if (!dev) {
203 LOG(ERROR) << "Failed to initiate keymaster session";
204 return -1;
205 }
206 return dev.isSecure();
Janis Danisevskis015ec302017-01-31 11:31:08 +0000207}
208
209int keymaster_create_key_for_cryptfs_scrypt(uint32_t rsa_key_size,
210 uint64_t rsa_exponent,
211 uint32_t ratelimit,
212 uint8_t* key_buffer,
213 uint32_t key_buffer_size,
214 uint32_t* key_out_size)
215{
216 Keymaster dev;
217 std::string key;
218 if (!dev) {
219 LOG(ERROR) << "Failed to initiate keymaster session";
220 return -1;
221 }
222 if (!key_buffer || !key_out_size) {
223 LOG(ERROR) << __FILE__ << ":" << __LINE__ << ":Invalid argument";
224 return -1;
225 }
226 if (key_out_size) {
227 *key_out_size = 0;
228 }
229
230 auto paramBuilder = AuthorizationSetBuilder()
231 .Authorization(TAG_ALGORITHM, Algorithm::RSA)
232 .Authorization(TAG_KEY_SIZE, rsa_key_size)
233 .Authorization(TAG_RSA_PUBLIC_EXPONENT, rsa_exponent)
234 .Authorization(TAG_PURPOSE, KeyPurpose::SIGN)
235 .Authorization(TAG_PADDING, PaddingMode::NONE)
236 .Authorization(TAG_DIGEST, Digest::NONE)
237 .Authorization(TAG_BLOB_USAGE_REQUIREMENTS,
238 KeyBlobUsageRequirements::STANDALONE)
239 .Authorization(TAG_NO_AUTH_REQUIRED)
240 .Authorization(TAG_MIN_SECONDS_BETWEEN_OPS, ratelimit);
241
242 if (!dev.generateKey(paramBuilder, &key)) {
243 return -1;
244 }
245
246 if (key_out_size) {
247 *key_out_size = key.size();
248 }
249
250 if (key_buffer_size < key.size()) {
251 return -1;
252 }
253
254 std::copy(key.data(), key.data() + key.size(), key_buffer);
255 return 0;
256}
257
258int keymaster_sign_object_for_cryptfs_scrypt(const uint8_t* key_blob,
259 size_t key_blob_size,
260 uint32_t ratelimit,
261 const uint8_t* object,
262 const size_t object_size,
263 uint8_t** signature_buffer,
264 size_t* signature_buffer_size)
265{
266 Keymaster dev;
267 if (!dev) {
268 LOG(ERROR) << "Failed to initiate keymaster session";
269 return -1;
270 }
271 if (!key_blob || !object || !signature_buffer || !signature_buffer_size) {
272 LOG(ERROR) << __FILE__ << ":" << __LINE__ << ":Invalid argument";
273 return -1;
274 }
275
276 AuthorizationSet outParams;
277 std::string key(reinterpret_cast<const char*>(key_blob), key_blob_size);
278 std::string input(reinterpret_cast<const char*>(object), object_size);
279 std::string output;
280 KeymasterOperation op;
281
282 auto paramBuilder = AuthorizationSetBuilder()
283 .Authorization(TAG_PADDING, PaddingMode::NONE)
284 .Authorization(TAG_DIGEST, Digest::NONE);
285
286 while (true) {
287 op = dev.begin(KeyPurpose::SIGN, key, paramBuilder, &outParams);
Wei Wang4375f1b2017-02-24 17:43:01 -0800288 if (op.errorCode() == ErrorCode::KEY_RATE_LIMIT_EXCEEDED) {
Janis Danisevskis015ec302017-01-31 11:31:08 +0000289 sleep(ratelimit);
290 continue;
291 } else break;
292 }
293
Wei Wang4375f1b2017-02-24 17:43:01 -0800294 if (op.errorCode() != ErrorCode::OK) {
295 LOG(ERROR) << "Error starting keymaster signature transaction: " << int32_t(op.errorCode());
Janis Danisevskis015ec302017-01-31 11:31:08 +0000296 return -1;
297 }
298
299 if (!op.updateCompletely(input, &output)) {
300 LOG(ERROR) << "Error sending data to keymaster signature transaction: "
Wei Wang4375f1b2017-02-24 17:43:01 -0800301 << uint32_t(op.errorCode());
Janis Danisevskis015ec302017-01-31 11:31:08 +0000302 return -1;
303 }
304
305 if (!op.finish(&output)) {
Wei Wang4375f1b2017-02-24 17:43:01 -0800306 LOG(ERROR) << "Error finalizing keymaster signature transaction: " << int32_t(op.errorCode());
Janis Danisevskis015ec302017-01-31 11:31:08 +0000307 return -1;
308 }
309
310 *signature_buffer = reinterpret_cast<uint8_t*>(malloc(output.size()));
311 if (*signature_buffer == nullptr) {
312 LOG(ERROR) << "Error allocation buffer for keymaster signature";
313 return -1;
314 }
315 *signature_buffer_size = output.size();
316 std::copy(output.data(), output.data() + output.size(), *signature_buffer);
317 return 0;
318}