blob: 4d055c257142a125234ea412bea9fdb771b3484f [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
Pavel Grafove2e2d302017-08-01 17:15:53 +010034bool KeymasterOperation::updateCompletely(const char* input, size_t inputLen,
35 const std::function<void(const char*, size_t)> consumer) {
36 uint32_t inputConsumed = 0;
Janis Danisevskis8e537b82016-10-26 14:27:10 +010037
38 ErrorCode km_error;
Pavel Grafove2e2d302017-08-01 17:15:53 +010039 auto hidlCB = [&] (ErrorCode ret, uint32_t inputConsumedDelta,
Janis Danisevskis8e537b82016-10-26 14:27:10 +010040 const hidl_vec<KeyParameter>& /*ignored*/, const hidl_vec<uint8_t>& _output) {
41 km_error = ret;
42 if (km_error != ErrorCode::OK) return;
Pavel Grafove2e2d302017-08-01 17:15:53 +010043 inputConsumed += inputConsumedDelta;
44 consumer(reinterpret_cast<const char*>(&_output[0]), _output.size());
Janis Danisevskis8e537b82016-10-26 14:27:10 +010045 };
46
Pavel Grafove2e2d302017-08-01 17:15:53 +010047 while (inputConsumed != inputLen) {
48 size_t toRead = static_cast<size_t>(inputLen - inputConsumed);
49 auto inputBlob =
50 blob2hidlVec(reinterpret_cast<const uint8_t*>(&input[inputConsumed]), toRead);
Janis Danisevskis8e537b82016-10-26 14:27:10 +010051 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) {
Janis Danisevskis015ec302017-01-31 11:31:08 +000058 LOG(ERROR) << "update failed, code " << int32_t(km_error);
Janis Danisevskis8e537b82016-10-26 14:27:10 +010059 mDevice = nullptr;
60 return false;
61 }
Pavel Grafove2e2d302017-08-01 17:15:53 +010062 if (inputConsumed > inputLen) {
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 Crowley1ef25582016-01-21 20:26:12 +000067 }
68 return true;
69}
70
Paul Crowleydff8c722016-05-16 08:14:56 -070071bool KeymasterOperation::finish(std::string* output) {
Janis Danisevskis8e537b82016-10-26 14:27:10 +010072 ErrorCode km_error;
73 auto hidlCb = [&] (ErrorCode ret, const hidl_vec<KeyParameter>& /*ignored*/,
74 const hidl_vec<uint8_t>& _output) {
75 km_error = ret;
76 if (km_error != ErrorCode::OK) return;
77 if (output)
78 output->assign(reinterpret_cast<const char*>(&_output[0]), _output.size());
79 };
80 auto error = mDevice->finish(mOpHandle, hidl_vec<KeyParameter>(), hidl_vec<uint8_t>(),
81 hidl_vec<uint8_t>(), hidlCb);
Paul Crowley13ffd8e2016-01-27 14:30:22 +000082 mDevice = nullptr;
Janis Danisevskis8e537b82016-10-26 14:27:10 +010083 if (!error.isOk()) {
84 LOG(ERROR) << "finish failed: " << error.description();
Paul Crowley1ef25582016-01-21 20:26:12 +000085 return false;
86 }
Janis Danisevskis8e537b82016-10-26 14:27:10 +010087 if (km_error != ErrorCode::OK) {
Janis Danisevskis015ec302017-01-31 11:31:08 +000088 LOG(ERROR) << "finish failed, code " << int32_t(km_error);
Janis Danisevskis8e537b82016-10-26 14:27:10 +010089 return false;
Paul Crowleydff8c722016-05-16 08:14:56 -070090 }
Paul Crowley1ef25582016-01-21 20:26:12 +000091 return true;
92}
93
94Keymaster::Keymaster() {
Chris Phoenix9e8a5772017-01-25 15:15:46 -080095 mDevice = ::android::hardware::keymaster::V3_0::IKeymasterDevice::getService();
Paul Crowley1ef25582016-01-21 20:26:12 +000096}
97
Janis Danisevskis8e537b82016-10-26 14:27:10 +010098bool Keymaster::generateKey(const AuthorizationSet& inParams, std::string* key) {
99 ErrorCode km_error;
100 auto hidlCb = [&] (ErrorCode ret, const hidl_vec<uint8_t>& keyBlob,
101 const KeyCharacteristics& /*ignored*/) {
102 km_error = ret;
103 if (km_error != ErrorCode::OK) return;
104 if (key)
105 key->assign(reinterpret_cast<const char*>(&keyBlob[0]), keyBlob.size());
106 };
107
108 auto error = mDevice->generateKey(inParams.hidl_data(), hidlCb);
109 if (!error.isOk()) {
110 LOG(ERROR) << "generate_key failed: " << error.description();
Paul Crowley1ef25582016-01-21 20:26:12 +0000111 return false;
112 }
Janis Danisevskis8e537b82016-10-26 14:27:10 +0100113 if (km_error != ErrorCode::OK) {
114 LOG(ERROR) << "generate_key failed, code " << int32_t(km_error);
115 return false;
116 }
Paul Crowley1ef25582016-01-21 20:26:12 +0000117 return true;
118}
119
Paul Crowleydf528a72016-03-09 09:31:37 -0800120bool Keymaster::deleteKey(const std::string& key) {
Janis Danisevskis8e537b82016-10-26 14:27:10 +0100121 auto keyBlob = blob2hidlVec(key);
122 auto error = mDevice->deleteKey(keyBlob);
123 if (!error.isOk()) {
124 LOG(ERROR) << "delete_key failed: " << error.description();
125 return false;
126 }
127 if (ErrorCode(error) != ErrorCode::OK) {
128 LOG(ERROR) << "delete_key failed, code " << uint32_t(ErrorCode(error));
Paul Crowley1ef25582016-01-21 20:26:12 +0000129 return false;
130 }
131 return true;
132}
133
Paul Crowleydff8c722016-05-16 08:14:56 -0700134bool Keymaster::upgradeKey(const std::string& oldKey, const AuthorizationSet& inParams,
135 std::string* newKey) {
Janis Danisevskis8e537b82016-10-26 14:27:10 +0100136 auto oldKeyBlob = blob2hidlVec(oldKey);
137 ErrorCode km_error;
138 auto hidlCb = [&] (ErrorCode ret, const hidl_vec<uint8_t>& upgradedKeyBlob) {
139 km_error = ret;
140 if (km_error != ErrorCode::OK) return;
141 if (newKey)
142 newKey->assign(reinterpret_cast<const char*>(&upgradedKeyBlob[0]),
143 upgradedKeyBlob.size());
144 };
145 auto error = mDevice->upgradeKey(oldKeyBlob, inParams.hidl_data(), hidlCb);
146 if (!error.isOk()) {
147 LOG(ERROR) << "upgrade_key failed: " << error.description();
Paul Crowleydff8c722016-05-16 08:14:56 -0700148 return false;
149 }
Janis Danisevskis8e537b82016-10-26 14:27:10 +0100150 if (km_error != ErrorCode::OK) {
151 LOG(ERROR) << "upgrade_key failed, code " << int32_t(km_error);
152 return false;
153 }
Paul Crowleydff8c722016-05-16 08:14:56 -0700154 return true;
155}
156
Janis Danisevskis8e537b82016-10-26 14:27:10 +0100157KeymasterOperation Keymaster::begin(KeyPurpose purpose, const std::string& key,
158 const AuthorizationSet& inParams,
159 AuthorizationSet* outParams) {
160 auto keyBlob = blob2hidlVec(key);
161 uint64_t mOpHandle;
162 ErrorCode km_error;
163
164 auto hidlCb = [&] (ErrorCode ret, const hidl_vec<KeyParameter>& _outParams,
165 uint64_t operationHandle) {
166 km_error = ret;
167 if (km_error != ErrorCode::OK) return;
168 if (outParams)
169 *outParams = _outParams;
170 mOpHandle = operationHandle;
171 };
172
173 auto error = mDevice->begin(purpose, keyBlob, inParams.hidl_data(), hidlCb);
174 if (!error.isOk()) {
175 LOG(ERROR) << "begin failed: " << error.description();
176 return KeymasterOperation(ErrorCode::UNKNOWN_ERROR);
Paul Crowley1ef25582016-01-21 20:26:12 +0000177 }
Janis Danisevskis8e537b82016-10-26 14:27:10 +0100178 if (km_error != ErrorCode::OK) {
Janis Danisevskis015ec302017-01-31 11:31:08 +0000179 LOG(ERROR) << "begin failed, code " << int32_t(km_error);
Janis Danisevskis8e537b82016-10-26 14:27:10 +0100180 return KeymasterOperation(km_error);
Paul Crowley1ef25582016-01-21 20:26:12 +0000181 }
Paul Crowley13ffd8e2016-01-27 14:30:22 +0000182 return KeymasterOperation(mDevice, mOpHandle);
Paul Crowley1ef25582016-01-21 20:26:12 +0000183}
Janis Danisevskis015ec302017-01-31 11:31:08 +0000184bool Keymaster::isSecure() {
185 bool _isSecure = false;
186 auto rc = mDevice->getHardwareFeatures(
Shawn Willden12e72ad2017-03-01 06:44:15 -0700187 [&] (bool isSecure, bool, bool, bool, bool, const hidl_string&, const hidl_string&) {
188 _isSecure = isSecure; });
Janis Danisevskis015ec302017-01-31 11:31:08 +0000189 return rc.isOk() && _isSecure;
190}
Paul Crowley1ef25582016-01-21 20:26:12 +0000191
192} // namespace vold
193} // namespace android
Janis Danisevskis015ec302017-01-31 11:31:08 +0000194
195using namespace ::android::vold;
196
197int keymaster_compatibility_cryptfs_scrypt() {
Janis Danisevskise7152c32017-03-08 11:02:30 -0800198 Keymaster dev;
199 if (!dev) {
200 LOG(ERROR) << "Failed to initiate keymaster session";
201 return -1;
202 }
203 return dev.isSecure();
Janis Danisevskis015ec302017-01-31 11:31:08 +0000204}
205
Paul Crowley73473332017-11-21 15:43:51 -0800206static bool write_string_to_buf(const std::string& towrite, uint8_t* buffer, uint32_t buffer_size,
207 uint32_t* out_size) {
208 if (!buffer || !out_size) {
209 LOG(ERROR) << "Missing target pointers";
210 return false;
Janis Danisevskis015ec302017-01-31 11:31:08 +0000211 }
Paul Crowley73473332017-11-21 15:43:51 -0800212 *out_size = towrite.size();
213 if (buffer_size < towrite.size()) {
214 LOG(ERROR) << "Buffer too small " << buffer_size << " < " << towrite.size();
215 return false;
Janis Danisevskis015ec302017-01-31 11:31:08 +0000216 }
Paul Crowley73473332017-11-21 15:43:51 -0800217 memset(buffer, '\0', buffer_size);
218 std::copy(towrite.begin(), towrite.end(), buffer);
219 return true;
220}
221
222static AuthorizationSet keyParams(uint32_t rsa_key_size, uint64_t rsa_exponent, uint32_t ratelimit) {
223 return AuthorizationSetBuilder()
224 .Authorization(TAG_ALGORITHM, Algorithm::RSA)
225 .Authorization(TAG_KEY_SIZE, rsa_key_size)
226 .Authorization(TAG_RSA_PUBLIC_EXPONENT, rsa_exponent)
227 .Authorization(TAG_PURPOSE, KeyPurpose::SIGN)
228 .Authorization(TAG_PADDING, PaddingMode::NONE)
229 .Authorization(TAG_DIGEST, Digest::NONE)
230 .Authorization(TAG_BLOB_USAGE_REQUIREMENTS, KeyBlobUsageRequirements::STANDALONE)
231 .Authorization(TAG_NO_AUTH_REQUIRED)
232 .Authorization(TAG_MIN_SECONDS_BETWEEN_OPS, ratelimit);
233}
234
235int keymaster_create_key_for_cryptfs_scrypt(uint32_t rsa_key_size, uint64_t rsa_exponent,
236 uint32_t ratelimit, uint8_t* key_buffer,
237 uint32_t key_buffer_size, uint32_t* key_out_size) {
Janis Danisevskis015ec302017-01-31 11:31:08 +0000238 if (key_out_size) {
239 *key_out_size = 0;
240 }
Janis Danisevskis015ec302017-01-31 11:31:08 +0000241 Keymaster dev;
242 if (!dev) {
243 LOG(ERROR) << "Failed to initiate keymaster session";
244 return -1;
245 }
Paul Crowley73473332017-11-21 15:43:51 -0800246 std::string key;
247 if (!dev.generateKey(keyParams(rsa_key_size, rsa_exponent, ratelimit), &key)) return -1;
248 if (!write_string_to_buf(key, key_buffer, key_buffer_size, key_out_size)) return -1;
249 return 0;
250}
251
252int keymaster_upgrade_key_for_cryptfs_scrypt(uint32_t rsa_key_size, uint64_t rsa_exponent,
253 uint32_t ratelimit, const uint8_t* key_blob,
254 size_t key_blob_size, uint8_t* key_buffer,
255 uint32_t key_buffer_size, uint32_t* key_out_size) {
256 if (key_out_size) {
257 *key_out_size = 0;
258 }
259 Keymaster dev;
260 if (!dev) {
261 LOG(ERROR) << "Failed to initiate keymaster session";
262 return -1;
263 }
264 std::string old_key(reinterpret_cast<const char*>(key_blob), key_blob_size);
265 std::string new_key;
266 if (!dev.upgradeKey(old_key, keyParams(rsa_key_size, rsa_exponent, ratelimit), &new_key))
267 return -1;
268 if (!write_string_to_buf(new_key, key_buffer, key_buffer_size, key_out_size)) return -1;
269 return 0;
270}
271
272KeymasterSignResult keymaster_sign_object_for_cryptfs_scrypt(
273 const uint8_t* key_blob, size_t key_blob_size, uint32_t ratelimit, const uint8_t* object,
274 const size_t object_size, uint8_t** signature_buffer, size_t* signature_buffer_size) {
275 Keymaster dev;
276 if (!dev) {
277 LOG(ERROR) << "Failed to initiate keymaster session";
278 return KeymasterSignResult::error;
279 }
Janis Danisevskis015ec302017-01-31 11:31:08 +0000280 if (!key_blob || !object || !signature_buffer || !signature_buffer_size) {
281 LOG(ERROR) << __FILE__ << ":" << __LINE__ << ":Invalid argument";
Paul Crowley73473332017-11-21 15:43:51 -0800282 return KeymasterSignResult::error;
Janis Danisevskis015ec302017-01-31 11:31:08 +0000283 }
284
285 AuthorizationSet outParams;
286 std::string key(reinterpret_cast<const char*>(key_blob), key_blob_size);
287 std::string input(reinterpret_cast<const char*>(object), object_size);
288 std::string output;
289 KeymasterOperation op;
290
291 auto paramBuilder = AuthorizationSetBuilder()
292 .Authorization(TAG_PADDING, PaddingMode::NONE)
293 .Authorization(TAG_DIGEST, Digest::NONE);
294
295 while (true) {
296 op = dev.begin(KeyPurpose::SIGN, key, paramBuilder, &outParams);
Wei Wang4375f1b2017-02-24 17:43:01 -0800297 if (op.errorCode() == ErrorCode::KEY_RATE_LIMIT_EXCEEDED) {
Janis Danisevskis015ec302017-01-31 11:31:08 +0000298 sleep(ratelimit);
299 continue;
300 } else break;
301 }
302
Paul Crowley73473332017-11-21 15:43:51 -0800303 if (op.errorCode() == ErrorCode::KEY_REQUIRES_UPGRADE) {
304 LOG(ERROR) << "Keymaster key requires upgrade";
305 return KeymasterSignResult::upgrade;
306 }
307
Wei Wang4375f1b2017-02-24 17:43:01 -0800308 if (op.errorCode() != ErrorCode::OK) {
309 LOG(ERROR) << "Error starting keymaster signature transaction: " << int32_t(op.errorCode());
Paul Crowley73473332017-11-21 15:43:51 -0800310 return KeymasterSignResult::error;
Janis Danisevskis015ec302017-01-31 11:31:08 +0000311 }
312
313 if (!op.updateCompletely(input, &output)) {
314 LOG(ERROR) << "Error sending data to keymaster signature transaction: "
Wei Wang4375f1b2017-02-24 17:43:01 -0800315 << uint32_t(op.errorCode());
Paul Crowley73473332017-11-21 15:43:51 -0800316 return KeymasterSignResult::error;
Janis Danisevskis015ec302017-01-31 11:31:08 +0000317 }
318
319 if (!op.finish(&output)) {
Wei Wang4375f1b2017-02-24 17:43:01 -0800320 LOG(ERROR) << "Error finalizing keymaster signature transaction: " << int32_t(op.errorCode());
Paul Crowley73473332017-11-21 15:43:51 -0800321 return KeymasterSignResult::error;
Janis Danisevskis015ec302017-01-31 11:31:08 +0000322 }
323
324 *signature_buffer = reinterpret_cast<uint8_t*>(malloc(output.size()));
325 if (*signature_buffer == nullptr) {
326 LOG(ERROR) << "Error allocation buffer for keymaster signature";
Paul Crowley73473332017-11-21 15:43:51 -0800327 return KeymasterSignResult::error;
Janis Danisevskis015ec302017-01-31 11:31:08 +0000328 }
329 *signature_buffer_size = output.size();
330 std::copy(output.data(), output.data() + output.size(), *signature_buffer);
Paul Crowley73473332017-11-21 15:43:51 -0800331 return KeymasterSignResult::ok;
Janis Danisevskis015ec302017-01-31 11:31:08 +0000332}