blob: e2e21e886f05bc31415dedd45af9b33f21fd3b22 [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
207int keymaster_create_key_for_cryptfs_scrypt(uint32_t rsa_key_size,
208 uint64_t rsa_exponent,
209 uint32_t ratelimit,
210 uint8_t* key_buffer,
211 uint32_t key_buffer_size,
212 uint32_t* key_out_size)
213{
214 Keymaster dev;
215 std::string key;
216 if (!dev) {
217 LOG(ERROR) << "Failed to initiate keymaster session";
218 return -1;
219 }
220 if (!key_buffer || !key_out_size) {
221 LOG(ERROR) << __FILE__ << ":" << __LINE__ << ":Invalid argument";
222 return -1;
223 }
224 if (key_out_size) {
225 *key_out_size = 0;
226 }
227
228 auto paramBuilder = AuthorizationSetBuilder()
229 .Authorization(TAG_ALGORITHM, Algorithm::RSA)
230 .Authorization(TAG_KEY_SIZE, rsa_key_size)
231 .Authorization(TAG_RSA_PUBLIC_EXPONENT, rsa_exponent)
232 .Authorization(TAG_PURPOSE, KeyPurpose::SIGN)
233 .Authorization(TAG_PADDING, PaddingMode::NONE)
234 .Authorization(TAG_DIGEST, Digest::NONE)
235 .Authorization(TAG_BLOB_USAGE_REQUIREMENTS,
236 KeyBlobUsageRequirements::STANDALONE)
237 .Authorization(TAG_NO_AUTH_REQUIRED)
238 .Authorization(TAG_MIN_SECONDS_BETWEEN_OPS, ratelimit);
239
240 if (!dev.generateKey(paramBuilder, &key)) {
241 return -1;
242 }
243
244 if (key_out_size) {
245 *key_out_size = key.size();
246 }
247
248 if (key_buffer_size < key.size()) {
249 return -1;
250 }
251
252 std::copy(key.data(), key.data() + key.size(), key_buffer);
253 return 0;
254}
255
256int keymaster_sign_object_for_cryptfs_scrypt(const uint8_t* key_blob,
257 size_t key_blob_size,
258 uint32_t ratelimit,
259 const uint8_t* object,
260 const size_t object_size,
261 uint8_t** signature_buffer,
262 size_t* signature_buffer_size)
263{
264 Keymaster dev;
265 if (!dev) {
266 LOG(ERROR) << "Failed to initiate keymaster session";
267 return -1;
268 }
269 if (!key_blob || !object || !signature_buffer || !signature_buffer_size) {
270 LOG(ERROR) << __FILE__ << ":" << __LINE__ << ":Invalid argument";
271 return -1;
272 }
273
274 AuthorizationSet outParams;
275 std::string key(reinterpret_cast<const char*>(key_blob), key_blob_size);
276 std::string input(reinterpret_cast<const char*>(object), object_size);
277 std::string output;
278 KeymasterOperation op;
279
280 auto paramBuilder = AuthorizationSetBuilder()
281 .Authorization(TAG_PADDING, PaddingMode::NONE)
282 .Authorization(TAG_DIGEST, Digest::NONE);
283
284 while (true) {
285 op = dev.begin(KeyPurpose::SIGN, key, paramBuilder, &outParams);
Wei Wang4375f1b2017-02-24 17:43:01 -0800286 if (op.errorCode() == ErrorCode::KEY_RATE_LIMIT_EXCEEDED) {
Janis Danisevskis015ec302017-01-31 11:31:08 +0000287 sleep(ratelimit);
288 continue;
289 } else break;
290 }
291
Wei Wang4375f1b2017-02-24 17:43:01 -0800292 if (op.errorCode() != ErrorCode::OK) {
293 LOG(ERROR) << "Error starting keymaster signature transaction: " << int32_t(op.errorCode());
Janis Danisevskis015ec302017-01-31 11:31:08 +0000294 return -1;
295 }
296
297 if (!op.updateCompletely(input, &output)) {
298 LOG(ERROR) << "Error sending data to keymaster signature transaction: "
Wei Wang4375f1b2017-02-24 17:43:01 -0800299 << uint32_t(op.errorCode());
Janis Danisevskis015ec302017-01-31 11:31:08 +0000300 return -1;
301 }
302
303 if (!op.finish(&output)) {
Wei Wang4375f1b2017-02-24 17:43:01 -0800304 LOG(ERROR) << "Error finalizing keymaster signature transaction: " << int32_t(op.errorCode());
Janis Danisevskis015ec302017-01-31 11:31:08 +0000305 return -1;
306 }
307
308 *signature_buffer = reinterpret_cast<uint8_t*>(malloc(output.size()));
309 if (*signature_buffer == nullptr) {
310 LOG(ERROR) << "Error allocation buffer for keymaster signature";
311 return -1;
312 }
313 *signature_buffer_size = output.size();
314 std::copy(output.data(), output.data() + output.size(), *signature_buffer);
315 return 0;
316}