blob: 4921448fe7e7430159d47dd2001854b6733b938a [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>
Shawn Willden35351812018-01-22 09:08:32 -070020#include <keymasterV4_0/authorization_set.h>
21#include <keymasterV4_0/keymaster_utils.h>
Paul Crowley1ef25582016-01-21 20:26:12 +000022
23namespace android {
24namespace vold {
25
Shawn Willden35351812018-01-22 09:08:32 -070026using ::android::hardware::hidl_string;
27using ::android::hardware::hidl_vec;
Shawn Willdenc1903ad2018-03-30 17:06:14 -060028using ::android::hardware::keymaster::V4_0::SecurityLevel;
Shawn Willden35351812018-01-22 09:08:32 -070029
Paul Crowley0323afd2016-03-15 17:04:39 -070030KeymasterOperation::~KeymasterOperation() {
Shawn Willden35351812018-01-22 09:08:32 -070031 if (mDevice) 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,
Shawn Willden785365b2018-01-20 09:37:36 -070035 const std::function<void(const char*, size_t)> consumer) {
Pavel Grafove2e2d302017-08-01 17:15:53 +010036 uint32_t inputConsumed = 0;
Janis Danisevskis8e537b82016-10-26 14:27:10 +010037
Shawn Willden35351812018-01-22 09:08:32 -070038 km::ErrorCode km_error;
39 auto hidlCB = [&](km::ErrorCode ret, uint32_t inputConsumedDelta,
40 const hidl_vec<km::KeyParameter>& /*ignored*/,
41 const hidl_vec<uint8_t>& _output) {
Janis Danisevskis8e537b82016-10-26 14:27:10 +010042 km_error = ret;
Shawn Willden35351812018-01-22 09:08:32 -070043 if (km_error != km::ErrorCode::OK) return;
Pavel Grafove2e2d302017-08-01 17:15:53 +010044 inputConsumed += inputConsumedDelta;
45 consumer(reinterpret_cast<const char*>(&_output[0]), _output.size());
Janis Danisevskis8e537b82016-10-26 14:27:10 +010046 };
47
Pavel Grafove2e2d302017-08-01 17:15:53 +010048 while (inputConsumed != inputLen) {
49 size_t toRead = static_cast<size_t>(inputLen - inputConsumed);
Shawn Willden35351812018-01-22 09:08:32 -070050 auto inputBlob = km::support::blob2hidlVec(
51 reinterpret_cast<const uint8_t*>(&input[inputConsumed]), toRead);
52 // TODO(swillden): Need to handle getting a VerificationToken from the TEE if mDevice is
53 // StrongBox, so we can provide it here. The VerificationToken will need to be
54 // requested/retrieved during Keymaster::begin().
55 auto error = mDevice->update(mOpHandle, hidl_vec<km::KeyParameter>(), inputBlob,
56 km::HardwareAuthToken(), km::VerificationToken(), hidlCB);
Janis Danisevskis8e537b82016-10-26 14:27:10 +010057 if (!error.isOk()) {
58 LOG(ERROR) << "update failed: " << error.description();
Paul Crowley13ffd8e2016-01-27 14:30:22 +000059 mDevice = nullptr;
Paul Crowley1ef25582016-01-21 20:26:12 +000060 return false;
61 }
Shawn Willden35351812018-01-22 09:08:32 -070062 if (km_error != km::ErrorCode::OK) {
Janis Danisevskis015ec302017-01-31 11:31:08 +000063 LOG(ERROR) << "update failed, code " << int32_t(km_error);
Janis Danisevskis8e537b82016-10-26 14:27:10 +010064 mDevice = nullptr;
65 return false;
66 }
Pavel Grafove2e2d302017-08-01 17:15:53 +010067 if (inputConsumed > inputLen) {
Paul Crowley1ef25582016-01-21 20:26:12 +000068 LOG(ERROR) << "update reported too much input consumed";
Paul Crowley13ffd8e2016-01-27 14:30:22 +000069 mDevice = nullptr;
Paul Crowley1ef25582016-01-21 20:26:12 +000070 return false;
71 }
Paul Crowley1ef25582016-01-21 20:26:12 +000072 }
73 return true;
74}
75
Paul Crowleydff8c722016-05-16 08:14:56 -070076bool KeymasterOperation::finish(std::string* output) {
Shawn Willden35351812018-01-22 09:08:32 -070077 km::ErrorCode km_error;
78 auto hidlCb = [&](km::ErrorCode ret, const hidl_vec<km::KeyParameter>& /*ignored*/,
Shawn Willden785365b2018-01-20 09:37:36 -070079 const hidl_vec<uint8_t>& _output) {
Janis Danisevskis8e537b82016-10-26 14:27:10 +010080 km_error = ret;
Shawn Willden35351812018-01-22 09:08:32 -070081 if (km_error != km::ErrorCode::OK) return;
Shawn Willden785365b2018-01-20 09:37:36 -070082 if (output) output->assign(reinterpret_cast<const char*>(&_output[0]), _output.size());
Janis Danisevskis8e537b82016-10-26 14:27:10 +010083 };
Shawn Willden35351812018-01-22 09:08:32 -070084 auto error = mDevice->finish(mOpHandle, hidl_vec<km::KeyParameter>(), hidl_vec<uint8_t>(),
85 hidl_vec<uint8_t>(), km::HardwareAuthToken(),
86 km::VerificationToken(), hidlCb);
Paul Crowley13ffd8e2016-01-27 14:30:22 +000087 mDevice = nullptr;
Janis Danisevskis8e537b82016-10-26 14:27:10 +010088 if (!error.isOk()) {
89 LOG(ERROR) << "finish failed: " << error.description();
Paul Crowley1ef25582016-01-21 20:26:12 +000090 return false;
91 }
Shawn Willden35351812018-01-22 09:08:32 -070092 if (km_error != km::ErrorCode::OK) {
Janis Danisevskis015ec302017-01-31 11:31:08 +000093 LOG(ERROR) << "finish failed, code " << int32_t(km_error);
Janis Danisevskis8e537b82016-10-26 14:27:10 +010094 return false;
Paul Crowleydff8c722016-05-16 08:14:56 -070095 }
Paul Crowley1ef25582016-01-21 20:26:12 +000096 return true;
97}
98
Shawn Willden28075362018-05-09 08:12:10 -060099/* static */ bool Keymaster::hmacKeyGenerated = false;
100
Paul Crowley1ef25582016-01-21 20:26:12 +0000101Keymaster::Keymaster() {
Shawn Willden35351812018-01-22 09:08:32 -0700102 auto devices = KmDevice::enumerateAvailableDevices();
Shawn Willden28075362018-05-09 08:12:10 -0600103 if (!hmacKeyGenerated) {
104 KmDevice::performHmacKeyAgreement(devices);
105 hmacKeyGenerated = true;
106 }
Shawn Willdenc1903ad2018-03-30 17:06:14 -0600107 for (auto& dev : devices) {
108 // Explicitly avoid using STRONGBOX for now.
109 // TODO: Re-enable STRONGBOX, since it's what we really want. b/77338527
110 if (dev->halVersion().securityLevel != SecurityLevel::STRONGBOX) {
111 mDevice = std::move(dev);
112 break;
113 }
114 }
115 if (!mDevice) return;
Shawn Willden35351812018-01-22 09:08:32 -0700116 auto& version = mDevice->halVersion();
117 LOG(INFO) << "Using " << version.keymasterName << " from " << version.authorName
118 << " for encryption. Security level: " << toString(version.securityLevel)
119 << ", HAL: " << mDevice->descriptor() << "/" << mDevice->instanceName();
Paul Crowley1ef25582016-01-21 20:26:12 +0000120}
121
Shawn Willden35351812018-01-22 09:08:32 -0700122bool Keymaster::generateKey(const km::AuthorizationSet& inParams, std::string* key) {
123 km::ErrorCode km_error;
124 auto hidlCb = [&](km::ErrorCode ret, const hidl_vec<uint8_t>& keyBlob,
125 const km::KeyCharacteristics& /*ignored*/) {
Janis Danisevskis8e537b82016-10-26 14:27:10 +0100126 km_error = ret;
Shawn Willden35351812018-01-22 09:08:32 -0700127 if (km_error != km::ErrorCode::OK) return;
Shawn Willden785365b2018-01-20 09:37:36 -0700128 if (key) key->assign(reinterpret_cast<const char*>(&keyBlob[0]), keyBlob.size());
Janis Danisevskis8e537b82016-10-26 14:27:10 +0100129 };
130
131 auto error = mDevice->generateKey(inParams.hidl_data(), hidlCb);
132 if (!error.isOk()) {
133 LOG(ERROR) << "generate_key failed: " << error.description();
Paul Crowley1ef25582016-01-21 20:26:12 +0000134 return false;
135 }
Shawn Willden35351812018-01-22 09:08:32 -0700136 if (km_error != km::ErrorCode::OK) {
Janis Danisevskis8e537b82016-10-26 14:27:10 +0100137 LOG(ERROR) << "generate_key failed, code " << int32_t(km_error);
138 return false;
139 }
Paul Crowley1ef25582016-01-21 20:26:12 +0000140 return true;
141}
142
Paul Crowleydf528a72016-03-09 09:31:37 -0800143bool Keymaster::deleteKey(const std::string& key) {
Shawn Willden35351812018-01-22 09:08:32 -0700144 auto keyBlob = km::support::blob2hidlVec(key);
Janis Danisevskis8e537b82016-10-26 14:27:10 +0100145 auto error = mDevice->deleteKey(keyBlob);
146 if (!error.isOk()) {
147 LOG(ERROR) << "delete_key failed: " << error.description();
148 return false;
149 }
Shawn Willden35351812018-01-22 09:08:32 -0700150 if (error != km::ErrorCode::OK) {
151 LOG(ERROR) << "delete_key failed, code " << int32_t(km::ErrorCode(error));
Paul Crowley1ef25582016-01-21 20:26:12 +0000152 return false;
153 }
154 return true;
155}
156
Shawn Willden35351812018-01-22 09:08:32 -0700157bool Keymaster::upgradeKey(const std::string& oldKey, const km::AuthorizationSet& inParams,
Paul Crowleydff8c722016-05-16 08:14:56 -0700158 std::string* newKey) {
Shawn Willden35351812018-01-22 09:08:32 -0700159 auto oldKeyBlob = km::support::blob2hidlVec(oldKey);
160 km::ErrorCode km_error;
161 auto hidlCb = [&](km::ErrorCode ret, const hidl_vec<uint8_t>& upgradedKeyBlob) {
Janis Danisevskis8e537b82016-10-26 14:27:10 +0100162 km_error = ret;
Shawn Willden35351812018-01-22 09:08:32 -0700163 if (km_error != km::ErrorCode::OK) return;
Janis Danisevskis8e537b82016-10-26 14:27:10 +0100164 if (newKey)
165 newKey->assign(reinterpret_cast<const char*>(&upgradedKeyBlob[0]),
Shawn Willden785365b2018-01-20 09:37:36 -0700166 upgradedKeyBlob.size());
Janis Danisevskis8e537b82016-10-26 14:27:10 +0100167 };
168 auto error = mDevice->upgradeKey(oldKeyBlob, inParams.hidl_data(), hidlCb);
169 if (!error.isOk()) {
170 LOG(ERROR) << "upgrade_key failed: " << error.description();
Paul Crowleydff8c722016-05-16 08:14:56 -0700171 return false;
172 }
Shawn Willden35351812018-01-22 09:08:32 -0700173 if (km_error != km::ErrorCode::OK) {
Janis Danisevskis8e537b82016-10-26 14:27:10 +0100174 LOG(ERROR) << "upgrade_key failed, code " << int32_t(km_error);
175 return false;
176 }
Paul Crowleydff8c722016-05-16 08:14:56 -0700177 return true;
178}
179
Shawn Willden35351812018-01-22 09:08:32 -0700180KeymasterOperation Keymaster::begin(km::KeyPurpose purpose, const std::string& key,
181 const km::AuthorizationSet& inParams,
182 const km::HardwareAuthToken& authToken,
183 km::AuthorizationSet* outParams) {
184 auto keyBlob = km::support::blob2hidlVec(key);
Janis Danisevskis8e537b82016-10-26 14:27:10 +0100185 uint64_t mOpHandle;
Shawn Willden35351812018-01-22 09:08:32 -0700186 km::ErrorCode km_error;
Janis Danisevskis8e537b82016-10-26 14:27:10 +0100187
Shawn Willden35351812018-01-22 09:08:32 -0700188 auto hidlCb = [&](km::ErrorCode ret, const hidl_vec<km::KeyParameter>& _outParams,
Shawn Willden785365b2018-01-20 09:37:36 -0700189 uint64_t operationHandle) {
Janis Danisevskis8e537b82016-10-26 14:27:10 +0100190 km_error = ret;
Shawn Willden35351812018-01-22 09:08:32 -0700191 if (km_error != km::ErrorCode::OK) return;
Shawn Willden785365b2018-01-20 09:37:36 -0700192 if (outParams) *outParams = _outParams;
Janis Danisevskis8e537b82016-10-26 14:27:10 +0100193 mOpHandle = operationHandle;
194 };
195
Shawn Willden35351812018-01-22 09:08:32 -0700196 auto error = mDevice->begin(purpose, keyBlob, inParams.hidl_data(), authToken, hidlCb);
Janis Danisevskis8e537b82016-10-26 14:27:10 +0100197 if (!error.isOk()) {
198 LOG(ERROR) << "begin failed: " << error.description();
Shawn Willden35351812018-01-22 09:08:32 -0700199 return KeymasterOperation(km::ErrorCode::UNKNOWN_ERROR);
Paul Crowley1ef25582016-01-21 20:26:12 +0000200 }
Shawn Willden35351812018-01-22 09:08:32 -0700201 if (km_error != km::ErrorCode::OK) {
Janis Danisevskis015ec302017-01-31 11:31:08 +0000202 LOG(ERROR) << "begin failed, code " << int32_t(km_error);
Janis Danisevskis8e537b82016-10-26 14:27:10 +0100203 return KeymasterOperation(km_error);
Paul Crowley1ef25582016-01-21 20:26:12 +0000204 }
Shawn Willden35351812018-01-22 09:08:32 -0700205 return KeymasterOperation(mDevice.get(), mOpHandle);
Paul Crowley1ef25582016-01-21 20:26:12 +0000206}
Shawn Willden35351812018-01-22 09:08:32 -0700207
Janis Danisevskis015ec302017-01-31 11:31:08 +0000208bool Keymaster::isSecure() {
Shawn Willden35351812018-01-22 09:08:32 -0700209 return mDevice->halVersion().securityLevel != km::SecurityLevel::SOFTWARE;
Janis Danisevskis015ec302017-01-31 11:31:08 +0000210}
Paul Crowley1ef25582016-01-21 20:26:12 +0000211
212} // namespace vold
213} // namespace android
Janis Danisevskis015ec302017-01-31 11:31:08 +0000214
215using namespace ::android::vold;
216
217int keymaster_compatibility_cryptfs_scrypt() {
Janis Danisevskise7152c32017-03-08 11:02:30 -0800218 Keymaster dev;
219 if (!dev) {
220 LOG(ERROR) << "Failed to initiate keymaster session";
221 return -1;
222 }
223 return dev.isSecure();
Janis Danisevskis015ec302017-01-31 11:31:08 +0000224}
225
Paul Crowley73473332017-11-21 15:43:51 -0800226static bool write_string_to_buf(const std::string& towrite, uint8_t* buffer, uint32_t buffer_size,
227 uint32_t* out_size) {
228 if (!buffer || !out_size) {
229 LOG(ERROR) << "Missing target pointers";
230 return false;
Janis Danisevskis015ec302017-01-31 11:31:08 +0000231 }
Paul Crowley73473332017-11-21 15:43:51 -0800232 *out_size = towrite.size();
233 if (buffer_size < towrite.size()) {
234 LOG(ERROR) << "Buffer too small " << buffer_size << " < " << towrite.size();
235 return false;
Janis Danisevskis015ec302017-01-31 11:31:08 +0000236 }
Paul Crowley73473332017-11-21 15:43:51 -0800237 memset(buffer, '\0', buffer_size);
238 std::copy(towrite.begin(), towrite.end(), buffer);
239 return true;
240}
241
Shawn Willden35351812018-01-22 09:08:32 -0700242static km::AuthorizationSet keyParams(uint32_t rsa_key_size, uint64_t rsa_exponent,
243 uint32_t ratelimit) {
244 return km::AuthorizationSetBuilder()
245 .RsaSigningKey(rsa_key_size, rsa_exponent)
246 .NoDigestOrPadding()
247 .Authorization(km::TAG_BLOB_USAGE_REQUIREMENTS, km::KeyBlobUsageRequirements::STANDALONE)
248 .Authorization(km::TAG_NO_AUTH_REQUIRED)
249 .Authorization(km::TAG_MIN_SECONDS_BETWEEN_OPS, ratelimit);
Paul Crowley73473332017-11-21 15:43:51 -0800250}
251
252int keymaster_create_key_for_cryptfs_scrypt(uint32_t rsa_key_size, uint64_t rsa_exponent,
253 uint32_t ratelimit, uint8_t* key_buffer,
254 uint32_t key_buffer_size, uint32_t* key_out_size) {
Janis Danisevskis015ec302017-01-31 11:31:08 +0000255 if (key_out_size) {
256 *key_out_size = 0;
257 }
Janis Danisevskis015ec302017-01-31 11:31:08 +0000258 Keymaster dev;
259 if (!dev) {
260 LOG(ERROR) << "Failed to initiate keymaster session";
261 return -1;
262 }
Paul Crowley73473332017-11-21 15:43:51 -0800263 std::string key;
264 if (!dev.generateKey(keyParams(rsa_key_size, rsa_exponent, ratelimit), &key)) return -1;
265 if (!write_string_to_buf(key, key_buffer, key_buffer_size, key_out_size)) return -1;
266 return 0;
267}
268
269int keymaster_upgrade_key_for_cryptfs_scrypt(uint32_t rsa_key_size, uint64_t rsa_exponent,
270 uint32_t ratelimit, const uint8_t* key_blob,
271 size_t key_blob_size, uint8_t* key_buffer,
272 uint32_t key_buffer_size, uint32_t* key_out_size) {
273 if (key_out_size) {
274 *key_out_size = 0;
275 }
276 Keymaster dev;
277 if (!dev) {
278 LOG(ERROR) << "Failed to initiate keymaster session";
279 return -1;
280 }
281 std::string old_key(reinterpret_cast<const char*>(key_blob), key_blob_size);
282 std::string new_key;
283 if (!dev.upgradeKey(old_key, keyParams(rsa_key_size, rsa_exponent, ratelimit), &new_key))
284 return -1;
285 if (!write_string_to_buf(new_key, key_buffer, key_buffer_size, key_out_size)) return -1;
286 return 0;
287}
288
289KeymasterSignResult keymaster_sign_object_for_cryptfs_scrypt(
290 const uint8_t* key_blob, size_t key_blob_size, uint32_t ratelimit, const uint8_t* object,
291 const size_t object_size, uint8_t** signature_buffer, size_t* signature_buffer_size) {
292 Keymaster dev;
293 if (!dev) {
294 LOG(ERROR) << "Failed to initiate keymaster session";
295 return KeymasterSignResult::error;
296 }
Janis Danisevskis015ec302017-01-31 11:31:08 +0000297 if (!key_blob || !object || !signature_buffer || !signature_buffer_size) {
298 LOG(ERROR) << __FILE__ << ":" << __LINE__ << ":Invalid argument";
Paul Crowley73473332017-11-21 15:43:51 -0800299 return KeymasterSignResult::error;
Janis Danisevskis015ec302017-01-31 11:31:08 +0000300 }
301
Shawn Willden35351812018-01-22 09:08:32 -0700302 km::AuthorizationSet outParams;
Janis Danisevskis015ec302017-01-31 11:31:08 +0000303 std::string key(reinterpret_cast<const char*>(key_blob), key_blob_size);
304 std::string input(reinterpret_cast<const char*>(object), object_size);
305 std::string output;
306 KeymasterOperation op;
307
Shawn Willden35351812018-01-22 09:08:32 -0700308 auto paramBuilder = km::AuthorizationSetBuilder().NoDigestOrPadding();
Janis Danisevskis015ec302017-01-31 11:31:08 +0000309 while (true) {
Shawn Willden35351812018-01-22 09:08:32 -0700310 op = dev.begin(km::KeyPurpose::SIGN, key, paramBuilder, km::HardwareAuthToken(), &outParams);
311 if (op.errorCode() == km::ErrorCode::KEY_RATE_LIMIT_EXCEEDED) {
Janis Danisevskis015ec302017-01-31 11:31:08 +0000312 sleep(ratelimit);
313 continue;
Shawn Willden785365b2018-01-20 09:37:36 -0700314 } else
315 break;
Janis Danisevskis015ec302017-01-31 11:31:08 +0000316 }
317
Shawn Willden35351812018-01-22 09:08:32 -0700318 if (op.errorCode() == km::ErrorCode::KEY_REQUIRES_UPGRADE) {
Paul Crowley73473332017-11-21 15:43:51 -0800319 LOG(ERROR) << "Keymaster key requires upgrade";
320 return KeymasterSignResult::upgrade;
321 }
322
Shawn Willden35351812018-01-22 09:08:32 -0700323 if (op.errorCode() != km::ErrorCode::OK) {
Wei Wang4375f1b2017-02-24 17:43:01 -0800324 LOG(ERROR) << "Error starting keymaster signature transaction: " << int32_t(op.errorCode());
Paul Crowley73473332017-11-21 15:43:51 -0800325 return KeymasterSignResult::error;
Janis Danisevskis015ec302017-01-31 11:31:08 +0000326 }
327
328 if (!op.updateCompletely(input, &output)) {
329 LOG(ERROR) << "Error sending data to keymaster signature transaction: "
Wei Wang4375f1b2017-02-24 17:43:01 -0800330 << uint32_t(op.errorCode());
Paul Crowley73473332017-11-21 15:43:51 -0800331 return KeymasterSignResult::error;
Janis Danisevskis015ec302017-01-31 11:31:08 +0000332 }
333
334 if (!op.finish(&output)) {
Shawn Willden785365b2018-01-20 09:37:36 -0700335 LOG(ERROR) << "Error finalizing keymaster signature transaction: "
336 << int32_t(op.errorCode());
Paul Crowley73473332017-11-21 15:43:51 -0800337 return KeymasterSignResult::error;
Janis Danisevskis015ec302017-01-31 11:31:08 +0000338 }
339
340 *signature_buffer = reinterpret_cast<uint8_t*>(malloc(output.size()));
341 if (*signature_buffer == nullptr) {
342 LOG(ERROR) << "Error allocation buffer for keymaster signature";
Paul Crowley73473332017-11-21 15:43:51 -0800343 return KeymasterSignResult::error;
Janis Danisevskis015ec302017-01-31 11:31:08 +0000344 }
345 *signature_buffer_size = output.size();
346 std::copy(output.data(), output.data() + output.size(), *signature_buffer);
Paul Crowley73473332017-11-21 15:43:51 -0800347 return KeymasterSignResult::ok;
Janis Danisevskis015ec302017-01-31 11:31:08 +0000348}