blob: e32d59860474a4c837cb18560a6539a298c0738d [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 Willdenf4527742017-11-09 15:59:39 -070020
21#include "authorization_set.h"
22#include "keymaster_tags.h"
23#include "keystore_hidl_support.h"
Janis Danisevskis8e537b82016-10-26 14:27:10 +010024
25using namespace ::keystore;
Shawn Willden12e72ad2017-03-01 06:44:15 -070026using android::hardware::hidl_string;
Paul Crowley1ef25582016-01-21 20:26:12 +000027
28namespace android {
29namespace vold {
30
Paul Crowley0323afd2016-03-15 17:04:39 -070031KeymasterOperation::~KeymasterOperation() {
Janis Danisevskis8e537b82016-10-26 14:27:10 +010032 if (mDevice.get()) mDevice->abort(mOpHandle);
Paul Crowley0323afd2016-03-15 17:04:39 -070033}
34
Pavel Grafove2e2d302017-08-01 17:15:53 +010035bool KeymasterOperation::updateCompletely(const char* input, size_t inputLen,
36 const std::function<void(const char*, size_t)> consumer) {
37 uint32_t inputConsumed = 0;
Janis Danisevskis8e537b82016-10-26 14:27:10 +010038
39 ErrorCode km_error;
Pavel Grafove2e2d302017-08-01 17:15:53 +010040 auto hidlCB = [&] (ErrorCode ret, uint32_t inputConsumedDelta,
Janis Danisevskis8e537b82016-10-26 14:27:10 +010041 const hidl_vec<KeyParameter>& /*ignored*/, const hidl_vec<uint8_t>& _output) {
42 km_error = ret;
43 if (km_error != 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);
50 auto inputBlob =
51 blob2hidlVec(reinterpret_cast<const uint8_t*>(&input[inputConsumed]), toRead);
Janis Danisevskis8e537b82016-10-26 14:27:10 +010052 auto error = mDevice->update(mOpHandle, hidl_vec<KeyParameter>(), inputBlob, hidlCB);
53 if (!error.isOk()) {
54 LOG(ERROR) << "update failed: " << error.description();
Paul Crowley13ffd8e2016-01-27 14:30:22 +000055 mDevice = nullptr;
Paul Crowley1ef25582016-01-21 20:26:12 +000056 return false;
57 }
Janis Danisevskis8e537b82016-10-26 14:27:10 +010058 if (km_error != ErrorCode::OK) {
Janis Danisevskis015ec302017-01-31 11:31:08 +000059 LOG(ERROR) << "update failed, code " << int32_t(km_error);
Janis Danisevskis8e537b82016-10-26 14:27:10 +010060 mDevice = nullptr;
61 return false;
62 }
Pavel Grafove2e2d302017-08-01 17:15:53 +010063 if (inputConsumed > inputLen) {
Paul Crowley1ef25582016-01-21 20:26:12 +000064 LOG(ERROR) << "update reported too much input consumed";
Paul Crowley13ffd8e2016-01-27 14:30:22 +000065 mDevice = nullptr;
Paul Crowley1ef25582016-01-21 20:26:12 +000066 return false;
67 }
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) {
Janis Danisevskis015ec302017-01-31 11:31:08 +000089 LOG(ERROR) << "finish failed, code " << int32_t(km_error);
Janis Danisevskis8e537b82016-10-26 14:27:10 +010090 return false;
Paul Crowleydff8c722016-05-16 08:14:56 -070091 }
Paul Crowley1ef25582016-01-21 20:26:12 +000092 return true;
93}
94
95Keymaster::Keymaster() {
Chris Phoenix9e8a5772017-01-25 15:15:46 -080096 mDevice = ::android::hardware::keymaster::V3_0::IKeymasterDevice::getService();
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) {
Janis Danisevskis015ec302017-01-31 11:31:08 +0000180 LOG(ERROR) << "begin failed, code " << int32_t(km_error);
Janis Danisevskis8e537b82016-10-26 14:27:10 +0100181 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}
Janis Danisevskis015ec302017-01-31 11:31:08 +0000185bool Keymaster::isSecure() {
186 bool _isSecure = false;
187 auto rc = mDevice->getHardwareFeatures(
Shawn Willden12e72ad2017-03-01 06:44:15 -0700188 [&] (bool isSecure, bool, bool, bool, bool, const hidl_string&, const hidl_string&) {
189 _isSecure = isSecure; });
Janis Danisevskis015ec302017-01-31 11:31:08 +0000190 return rc.isOk() && _isSecure;
191}
Paul Crowley1ef25582016-01-21 20:26:12 +0000192
193} // namespace vold
194} // namespace android
Janis Danisevskis015ec302017-01-31 11:31:08 +0000195
196using namespace ::android::vold;
197
198int keymaster_compatibility_cryptfs_scrypt() {
Janis Danisevskise7152c32017-03-08 11:02:30 -0800199 Keymaster dev;
200 if (!dev) {
201 LOG(ERROR) << "Failed to initiate keymaster session";
202 return -1;
203 }
204 return dev.isSecure();
Janis Danisevskis015ec302017-01-31 11:31:08 +0000205}
206
Paul Crowley73473332017-11-21 15:43:51 -0800207static bool write_string_to_buf(const std::string& towrite, uint8_t* buffer, uint32_t buffer_size,
208 uint32_t* out_size) {
209 if (!buffer || !out_size) {
210 LOG(ERROR) << "Missing target pointers";
211 return false;
Janis Danisevskis015ec302017-01-31 11:31:08 +0000212 }
Paul Crowley73473332017-11-21 15:43:51 -0800213 *out_size = towrite.size();
214 if (buffer_size < towrite.size()) {
215 LOG(ERROR) << "Buffer too small " << buffer_size << " < " << towrite.size();
216 return false;
Janis Danisevskis015ec302017-01-31 11:31:08 +0000217 }
Paul Crowley73473332017-11-21 15:43:51 -0800218 memset(buffer, '\0', buffer_size);
219 std::copy(towrite.begin(), towrite.end(), buffer);
220 return true;
221}
222
223static AuthorizationSet keyParams(uint32_t rsa_key_size, uint64_t rsa_exponent, uint32_t ratelimit) {
224 return AuthorizationSetBuilder()
225 .Authorization(TAG_ALGORITHM, Algorithm::RSA)
226 .Authorization(TAG_KEY_SIZE, rsa_key_size)
227 .Authorization(TAG_RSA_PUBLIC_EXPONENT, rsa_exponent)
228 .Authorization(TAG_PURPOSE, KeyPurpose::SIGN)
229 .Authorization(TAG_PADDING, PaddingMode::NONE)
230 .Authorization(TAG_DIGEST, Digest::NONE)
231 .Authorization(TAG_BLOB_USAGE_REQUIREMENTS, KeyBlobUsageRequirements::STANDALONE)
232 .Authorization(TAG_NO_AUTH_REQUIRED)
233 .Authorization(TAG_MIN_SECONDS_BETWEEN_OPS, ratelimit);
234}
235
236int keymaster_create_key_for_cryptfs_scrypt(uint32_t rsa_key_size, uint64_t rsa_exponent,
237 uint32_t ratelimit, uint8_t* key_buffer,
238 uint32_t key_buffer_size, uint32_t* key_out_size) {
Janis Danisevskis015ec302017-01-31 11:31:08 +0000239 if (key_out_size) {
240 *key_out_size = 0;
241 }
Janis Danisevskis015ec302017-01-31 11:31:08 +0000242 Keymaster dev;
243 if (!dev) {
244 LOG(ERROR) << "Failed to initiate keymaster session";
245 return -1;
246 }
Paul Crowley73473332017-11-21 15:43:51 -0800247 std::string key;
248 if (!dev.generateKey(keyParams(rsa_key_size, rsa_exponent, ratelimit), &key)) return -1;
249 if (!write_string_to_buf(key, key_buffer, key_buffer_size, key_out_size)) return -1;
250 return 0;
251}
252
253int keymaster_upgrade_key_for_cryptfs_scrypt(uint32_t rsa_key_size, uint64_t rsa_exponent,
254 uint32_t ratelimit, const uint8_t* key_blob,
255 size_t key_blob_size, uint8_t* key_buffer,
256 uint32_t key_buffer_size, uint32_t* key_out_size) {
257 if (key_out_size) {
258 *key_out_size = 0;
259 }
260 Keymaster dev;
261 if (!dev) {
262 LOG(ERROR) << "Failed to initiate keymaster session";
263 return -1;
264 }
265 std::string old_key(reinterpret_cast<const char*>(key_blob), key_blob_size);
266 std::string new_key;
267 if (!dev.upgradeKey(old_key, keyParams(rsa_key_size, rsa_exponent, ratelimit), &new_key))
268 return -1;
269 if (!write_string_to_buf(new_key, key_buffer, key_buffer_size, key_out_size)) return -1;
270 return 0;
271}
272
273KeymasterSignResult keymaster_sign_object_for_cryptfs_scrypt(
274 const uint8_t* key_blob, size_t key_blob_size, uint32_t ratelimit, const uint8_t* object,
275 const size_t object_size, uint8_t** signature_buffer, size_t* signature_buffer_size) {
276 Keymaster dev;
277 if (!dev) {
278 LOG(ERROR) << "Failed to initiate keymaster session";
279 return KeymasterSignResult::error;
280 }
Janis Danisevskis015ec302017-01-31 11:31:08 +0000281 if (!key_blob || !object || !signature_buffer || !signature_buffer_size) {
282 LOG(ERROR) << __FILE__ << ":" << __LINE__ << ":Invalid argument";
Paul Crowley73473332017-11-21 15:43:51 -0800283 return KeymasterSignResult::error;
Janis Danisevskis015ec302017-01-31 11:31:08 +0000284 }
285
286 AuthorizationSet outParams;
287 std::string key(reinterpret_cast<const char*>(key_blob), key_blob_size);
288 std::string input(reinterpret_cast<const char*>(object), object_size);
289 std::string output;
290 KeymasterOperation op;
291
292 auto paramBuilder = AuthorizationSetBuilder()
293 .Authorization(TAG_PADDING, PaddingMode::NONE)
294 .Authorization(TAG_DIGEST, Digest::NONE);
295
296 while (true) {
297 op = dev.begin(KeyPurpose::SIGN, key, paramBuilder, &outParams);
Wei Wang4375f1b2017-02-24 17:43:01 -0800298 if (op.errorCode() == ErrorCode::KEY_RATE_LIMIT_EXCEEDED) {
Janis Danisevskis015ec302017-01-31 11:31:08 +0000299 sleep(ratelimit);
300 continue;
301 } else break;
302 }
303
Paul Crowley73473332017-11-21 15:43:51 -0800304 if (op.errorCode() == ErrorCode::KEY_REQUIRES_UPGRADE) {
305 LOG(ERROR) << "Keymaster key requires upgrade";
306 return KeymasterSignResult::upgrade;
307 }
308
Wei Wang4375f1b2017-02-24 17:43:01 -0800309 if (op.errorCode() != ErrorCode::OK) {
310 LOG(ERROR) << "Error starting keymaster signature transaction: " << int32_t(op.errorCode());
Paul Crowley73473332017-11-21 15:43:51 -0800311 return KeymasterSignResult::error;
Janis Danisevskis015ec302017-01-31 11:31:08 +0000312 }
313
314 if (!op.updateCompletely(input, &output)) {
315 LOG(ERROR) << "Error sending data to keymaster signature transaction: "
Wei Wang4375f1b2017-02-24 17:43:01 -0800316 << uint32_t(op.errorCode());
Paul Crowley73473332017-11-21 15:43:51 -0800317 return KeymasterSignResult::error;
Janis Danisevskis015ec302017-01-31 11:31:08 +0000318 }
319
320 if (!op.finish(&output)) {
Wei Wang4375f1b2017-02-24 17:43:01 -0800321 LOG(ERROR) << "Error finalizing keymaster signature transaction: " << int32_t(op.errorCode());
Paul Crowley73473332017-11-21 15:43:51 -0800322 return KeymasterSignResult::error;
Janis Danisevskis015ec302017-01-31 11:31:08 +0000323 }
324
325 *signature_buffer = reinterpret_cast<uint8_t*>(malloc(output.size()));
326 if (*signature_buffer == nullptr) {
327 LOG(ERROR) << "Error allocation buffer for keymaster signature";
Paul Crowley73473332017-11-21 15:43:51 -0800328 return KeymasterSignResult::error;
Janis Danisevskis015ec302017-01-31 11:31:08 +0000329 }
330 *signature_buffer_size = output.size();
331 std::copy(output.data(), output.data() + output.size(), *signature_buffer);
Paul Crowley73473332017-11-21 15:43:51 -0800332 return KeymasterSignResult::ok;
Janis Danisevskis015ec302017-01-31 11:31:08 +0000333}