blob: 88adf2aeec8465985436cacb7ad4a63a027bed8b [file] [log] [blame]
Andrew Sculle4e2ffc2017-08-10 10:03:20 +01001/*
2 * Copyright 2017, 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 "KeymasterDevice.h"
nagendra modadugu284b9392017-09-27 13:51:23 -070018#include "proto_utils.h"
19
nagendra modadugu9e8c8562017-09-18 18:58:54 -070020#include <Keymaster.client.h>
nagendra modadugu284b9392017-09-27 13:51:23 -070021#include <nos/NuggetClient.h>
Andrew Sculle4e2ffc2017-08-10 10:03:20 +010022
23#include <android-base/logging.h>
24
25namespace android {
26namespace hardware {
27namespace keymaster {
28
nagendra modadugu8a883372017-09-18 22:29:00 -070029// std
30using std::string;
31
Andrew Sculle4e2ffc2017-08-10 10:03:20 +010032// libhidl
33using ::android::hardware::Void;
34
35// HAL
nagendra modadugu284b9392017-09-27 13:51:23 -070036using ::android::hardware::keymaster::V3_0::Algorithm;
Andrew Sculle4e2ffc2017-08-10 10:03:20 +010037using ::android::hardware::keymaster::V3_0::KeyCharacteristics;
nagendra modadugu284b9392017-09-27 13:51:23 -070038using ::android::hardware::keymaster::V3_0::KeyFormat;
39using ::android::hardware::keymaster::V3_0::Tag;
40
41// nos
42using nos::NuggetClient;
43
44// Keymaster app
45using ::nugget::app::keymaster::AddRngEntropyRequest;
46using ::nugget::app::keymaster::AddRngEntropyResponse;
47using ::nugget::app::keymaster::GenerateKeyRequest;
48using ::nugget::app::keymaster::GenerateKeyResponse;
49using ::nugget::app::keymaster::GetKeyCharacteristicsRequest;
50using ::nugget::app::keymaster::GetKeyCharacteristicsResponse;
51using ::nugget::app::keymaster::ImportKeyRequest;
52using ::nugget::app::keymaster::ImportKeyResponse;
53using ::nugget::app::keymaster::ExportKeyRequest;
54using ::nugget::app::keymaster::ExportKeyResponse;
55using ::nugget::app::keymaster::AttestKeyRequest;
56using ::nugget::app::keymaster::AttestKeyResponse;
57using ::nugget::app::keymaster::UpgradeKeyRequest;
58using ::nugget::app::keymaster::UpgradeKeyResponse;
59using ::nugget::app::keymaster::DeleteKeyRequest;
60using ::nugget::app::keymaster::DeleteKeyResponse;
61using ::nugget::app::keymaster::DeleteAllKeysRequest;
62using ::nugget::app::keymaster::DeleteAllKeysResponse;
63using ::nugget::app::keymaster::DestroyAttestationIdsRequest;
64using ::nugget::app::keymaster::DestroyAttestationIdsResponse;
65using ::nugget::app::keymaster::BeginOperationRequest;
66using ::nugget::app::keymaster::BeginOperationResponse;
67using ::nugget::app::keymaster::UpdateOperationRequest;
68using ::nugget::app::keymaster::UpdateOperationResponse;
69using ::nugget::app::keymaster::FinishOperationRequest;
70using ::nugget::app::keymaster::FinishOperationResponse;
71using ::nugget::app::keymaster::AbortOperationRequest;
72using ::nugget::app::keymaster::AbortOperationResponse;
73
74static ErrorCode status_to_error_code(uint32_t status)
75{
76 switch (status) {
77 case APP_SUCCESS:
78 return ErrorCode::OK;
79 break;
80 case APP_ERROR_BOGUS_ARGS:
81 return ErrorCode::INVALID_ARGUMENT;
82 break;
83 case APP_ERROR_INTERNAL:
84 return ErrorCode::UNKNOWN_ERROR;
85 break;
86 case APP_ERROR_TOO_MUCH:
87 return ErrorCode::INSUFFICIENT_BUFFER_SPACE;
88 break;
89 case APP_ERROR_RPC:
90 return ErrorCode::SECURE_HW_COMMUNICATION_FAILED;
91 break;
92 // TODO: app specific error codes go here.
93 default:
94 return ErrorCode::UNKNOWN_ERROR;
95 break;
96 }
97}
98
99#define KM_CALL(meth) { \
100 const uint32_t status = _keymaster. meth (request, response); \
101 if (status != APP_SUCCESS) { \
102 LOG(ERROR) << #meth << " : request failed with status: " \
103 << NuggetClient::StatusCodeString(status); \
104 return status_to_error_code(status); \
105 } \
106 if ((ErrorCode)response.error_code() != ErrorCode::OK) { \
107 LOG(ERROR) << #meth << " : device response error code: " \
108 << response.error_code(); \
109 return (ErrorCode)response.error_code(); \
110 } \
111}
112
113#define KM_CALLV(meth, ...) { \
114 const uint32_t status = _keymaster. meth (request, response); \
115 if (status != APP_SUCCESS) { \
116 LOG(ERROR) << #meth << " : request failed with status: " \
117 << NuggetClient::StatusCodeString(status); \
118 _hidl_cb(status_to_error_code(status), __VA_ARGS__); \
119 return Void(); \
120 } \
121 if ((ErrorCode)response.error_code() != ErrorCode::OK) { \
122 LOG(ERROR) << #meth << " : device response error code: " \
123 << response.error_code(); \
124 _hidl_cb((ErrorCode)response.error_code(), __VA_ARGS__); \
125 return Void(); \
126 } \
127}
Andrew Sculle4e2ffc2017-08-10 10:03:20 +0100128
129// Methods from ::android::hardware::keymaster::V3_0::IKeymasterDevice follow.
nagendra modadugu284b9392017-09-27 13:51:23 -0700130
nagendra modadugu8a883372017-09-18 22:29:00 -0700131Return<void> KeymasterDevice::getHardwareFeatures(
nagendra modadugu284b9392017-09-27 13:51:23 -0700132 getHardwareFeatures_cb _hidl_cb)
133{
Andrew Sculle4e2ffc2017-08-10 10:03:20 +0100134 LOG(VERBOSE) << "Running KeymasterDevice::getHardwareFeatures";
135
nagendra modadugu8a883372017-09-18 22:29:00 -0700136 (void)_keymaster;
137 _hidl_cb(true, true, true, true,
138 true, string("CitadelKeymaster"), string("Google"));
Andrew Sculle4e2ffc2017-08-10 10:03:20 +0100139 return Void();
140}
141
nagendra modadugu284b9392017-09-27 13:51:23 -0700142Return<ErrorCode> KeymasterDevice::addRngEntropy(const hidl_vec<uint8_t>& data)
143{
Andrew Sculle4e2ffc2017-08-10 10:03:20 +0100144 LOG(VERBOSE) << "Running KeymasterDevice::addRngEntropy";
145
nagendra modadugu284b9392017-09-27 13:51:23 -0700146 if (!data.size()) return ErrorCode::OK;
Andrew Sculle4e2ffc2017-08-10 10:03:20 +0100147
nagendra modadugu284b9392017-09-27 13:51:23 -0700148 AddRngEntropyRequest request;
149 AddRngEntropyResponse response;
150 request.set_data(&data[0], data.size());
151
152 // Call device.
153 KM_CALL(AddRngEntropy);
154
155 return (ErrorCode)response.error_code();
Andrew Sculle4e2ffc2017-08-10 10:03:20 +0100156}
157
nagendra modadugu284b9392017-09-27 13:51:23 -0700158Return<void> KeymasterDevice::generateKey(
159 const hidl_vec<KeyParameter>& keyParams,
160 generateKey_cb _hidl_cb)
161{
Andrew Sculle4e2ffc2017-08-10 10:03:20 +0100162 LOG(VERBOSE) << "Running KeymasterDevice::generateKey";
163
nagendra modadugu284b9392017-09-27 13:51:23 -0700164 GenerateKeyRequest request;
165 GenerateKeyResponse response;
Andrew Sculle4e2ffc2017-08-10 10:03:20 +0100166
nagendra modadugu284b9392017-09-27 13:51:23 -0700167 hidl_params_to_pb(keyParams, request.mutable_params());
168
169 // Call device.
170 KM_CALLV(GenerateKey, hidl_vec<uint8_t>{}, KeyCharacteristics());
171
172 hidl_vec<uint8_t> blob;
173 KeyCharacteristics characteristics;
174 blob.setToExternal(
175 reinterpret_cast<uint8_t*>(
176 const_cast<char*>(response.blob().blob().data())),
177 response.blob().blob().size(), false);
178 pb_to_hidl_params(response.characteristics().tee_enforced(),
179 &characteristics.teeEnforced);
180
181 _hidl_cb((ErrorCode)response.error_code(), blob, characteristics);
Andrew Sculle4e2ffc2017-08-10 10:03:20 +0100182 return Void();
183}
184
nagendra modadugu284b9392017-09-27 13:51:23 -0700185Return<void> KeymasterDevice::getKeyCharacteristics(
186 const hidl_vec<uint8_t>& keyBlob,
187 const hidl_vec<uint8_t>& clientId,
188 const hidl_vec<uint8_t>& appData,
189 getKeyCharacteristics_cb _hidl_cb)
190{
Andrew Sculle4e2ffc2017-08-10 10:03:20 +0100191 LOG(VERBOSE) << "Running KeymasterDevice::getKeyCharacteristics";
192
nagendra modadugu284b9392017-09-27 13:51:23 -0700193 GetKeyCharacteristicsRequest request;
194 GetKeyCharacteristicsResponse response;
Andrew Sculle4e2ffc2017-08-10 10:03:20 +0100195
nagendra modadugu284b9392017-09-27 13:51:23 -0700196 request.mutable_blob()->set_blob(&keyBlob[0], keyBlob.size());
197 request.set_client_id(&clientId[0], clientId.size());
198 request.set_app_data(&appData[0], appData.size());
199
200 // Call device.
201 KM_CALLV(GetKeyCharacteristics, KeyCharacteristics());
202
203 KeyCharacteristics characteristics;
204 pb_to_hidl_params(response.characteristics().tee_enforced(),
205 &characteristics.teeEnforced);
206
207 _hidl_cb((ErrorCode)response.error_code(), characteristics);
Andrew Sculle4e2ffc2017-08-10 10:03:20 +0100208 return Void();
209}
210
nagendra modadugu284b9392017-09-27 13:51:23 -0700211Return<void> KeymasterDevice::importKey(
212 const hidl_vec<KeyParameter>& params, KeyFormat keyFormat,
213 const hidl_vec<uint8_t>& keyData, importKey_cb _hidl_cb)
214{
Andrew Sculle4e2ffc2017-08-10 10:03:20 +0100215 LOG(VERBOSE) << "Running KeymasterDevice::importKey";
216
nagendra modadugu284b9392017-09-27 13:51:23 -0700217 ImportKeyRequest request;
218 ImportKeyResponse response;
Andrew Sculle4e2ffc2017-08-10 10:03:20 +0100219
nagendra modadugu284b9392017-09-27 13:51:23 -0700220 hidl_params_to_pb(params, request.mutable_params());
221 // TODO: serialize into ImportKeyRequest proto.
222 (void)keyFormat;
223 (void)keyData;
224
225 KM_CALLV(ImportKey, hidl_vec<uint8_t>{}, KeyCharacteristics{});
226
227 hidl_vec<uint8_t> blob;
228 blob.setToExternal(
229 reinterpret_cast<uint8_t*>(
230 const_cast<char*>(response.blob().blob().data())),
231 response.blob().blob().size(), false);
232
233 KeyCharacteristics characteristics;
234 pb_to_hidl_params(response.characteristics().tee_enforced(),
235 &characteristics.teeEnforced);
236
237 _hidl_cb(ErrorCode::OK, blob, characteristics);
Andrew Sculle4e2ffc2017-08-10 10:03:20 +0100238 return Void();
239}
240
nagendra modadugu284b9392017-09-27 13:51:23 -0700241Return<void> KeymasterDevice::exportKey(
242 KeyFormat exportFormat, const hidl_vec<uint8_t>& keyBlob,
243 const hidl_vec<uint8_t>& clientId,
244 const hidl_vec<uint8_t>& appData, exportKey_cb _hidl_cb)
245{
Andrew Sculle4e2ffc2017-08-10 10:03:20 +0100246 LOG(VERBOSE) << "Running KeymasterDevice::exportKey";
247
nagendra modadugu284b9392017-09-27 13:51:23 -0700248 ExportKeyRequest request;
249 ExportKeyResponse response;
Andrew Sculle4e2ffc2017-08-10 10:03:20 +0100250
nagendra modadugu284b9392017-09-27 13:51:23 -0700251 request.set_format((::nugget::app::keymaster::KeyFormat)exportFormat);
252 request.mutable_blob()->set_blob(&keyBlob[0], keyBlob.size());
253 request.set_client_id(&clientId[0], clientId.size());
254 request.set_app_data(&appData[0], appData.size());
255
256 KM_CALLV(ExportKey, hidl_vec<uint8_t>{});
257
258 hidl_vec<uint8_t> blob;
259 blob.setToExternal(
260 reinterpret_cast<uint8_t*>(
261 const_cast<char*>(response.key_material().data())),
262 response.key_material().size(), false);
263
264 _hidl_cb((ErrorCode)response.error_code(), blob);
Andrew Sculle4e2ffc2017-08-10 10:03:20 +0100265 return Void();
266}
267
nagendra modadugu284b9392017-09-27 13:51:23 -0700268Return<void> KeymasterDevice::attestKey(
269 const hidl_vec<uint8_t>& keyToAttest,
270 const hidl_vec<KeyParameter>& attestParams,
271 attestKey_cb _hidl_cb)
272{
Andrew Sculle4e2ffc2017-08-10 10:03:20 +0100273 LOG(VERBOSE) << "Running KeymasterDevice::attestKey";
274
nagendra modadugu284b9392017-09-27 13:51:23 -0700275 AttestKeyRequest request;
276 AttestKeyResponse response;
Andrew Sculle4e2ffc2017-08-10 10:03:20 +0100277
nagendra modadugu284b9392017-09-27 13:51:23 -0700278 request.mutable_blob()->set_blob(&keyToAttest[0], keyToAttest.size());
279 hidl_params_to_pb(attestParams, request.mutable_params());
280
281 KM_CALLV(AttestKey, hidl_vec<hidl_vec<uint8_t> >{});
282
283 vector<hidl_vec<uint8_t> > chain;
284 for (int i = 0; i < response.chain().certificates_size(); i++) {
285 hidl_vec<uint8_t> blob;
286 blob.setToExternal(
287 reinterpret_cast<uint8_t*>(
288 const_cast<char*>(
289 response.chain().certificates(i).data().data())),
290 response.chain().certificates(i).data().size(), false);
291 chain.push_back(blob);
292 }
293
294 _hidl_cb((ErrorCode)response.error_code(),
295 hidl_vec<hidl_vec<uint8_t> >(chain));
Andrew Sculle4e2ffc2017-08-10 10:03:20 +0100296 return Void();
297}
298
nagendra modadugu284b9392017-09-27 13:51:23 -0700299Return<void> KeymasterDevice::upgradeKey(
300 const hidl_vec<uint8_t>& keyBlobToUpgrade,
301 const hidl_vec<KeyParameter>& upgradeParams,
302 upgradeKey_cb _hidl_cb)
303{
Andrew Sculle4e2ffc2017-08-10 10:03:20 +0100304 LOG(VERBOSE) << "Running KeymasterDevice::upgradeKey";
305
nagendra modadugu284b9392017-09-27 13:51:23 -0700306 UpgradeKeyRequest request;
307 UpgradeKeyResponse response;
Andrew Sculle4e2ffc2017-08-10 10:03:20 +0100308
nagendra modadugu284b9392017-09-27 13:51:23 -0700309 request.mutable_blob()->set_blob(&keyBlobToUpgrade[0],
310 keyBlobToUpgrade.size());
311 hidl_params_to_pb(upgradeParams, request.mutable_params());
312
313 KM_CALLV(UpgradeKey, hidl_vec<uint8_t>{});
314
315 hidl_vec<uint8_t> blob;
316 blob.setToExternal(
317 reinterpret_cast<uint8_t*>(
318 const_cast<char*>(response.blob().blob().data())),
319 response.blob().blob().size(), false);
320
321 _hidl_cb((ErrorCode)response.error_code(), blob);
Andrew Sculle4e2ffc2017-08-10 10:03:20 +0100322 return Void();
323}
324
nagendra modadugu284b9392017-09-27 13:51:23 -0700325Return<ErrorCode> KeymasterDevice::deleteKey(const hidl_vec<uint8_t>& keyBlob)
326{
Andrew Sculle4e2ffc2017-08-10 10:03:20 +0100327 LOG(VERBOSE) << "Running KeymasterDevice::deleteKey";
328
nagendra modadugu284b9392017-09-27 13:51:23 -0700329 DeleteKeyRequest request;
330 DeleteKeyResponse response;
Andrew Sculle4e2ffc2017-08-10 10:03:20 +0100331
nagendra modadugu284b9392017-09-27 13:51:23 -0700332 request.mutable_blob()->set_blob(&keyBlob[0], keyBlob.size());
333
334 KM_CALL(DeleteKey);
335
336 return (ErrorCode)response.error_code();
Andrew Sculle4e2ffc2017-08-10 10:03:20 +0100337}
338
nagendra modadugu284b9392017-09-27 13:51:23 -0700339Return<ErrorCode> KeymasterDevice::deleteAllKeys()
340{
Andrew Sculle4e2ffc2017-08-10 10:03:20 +0100341 LOG(VERBOSE) << "Running KeymasterDevice::deleteAllKeys";
342
nagendra modadugu284b9392017-09-27 13:51:23 -0700343 DeleteAllKeysRequest request;
344 DeleteAllKeysResponse response;
Andrew Sculle4e2ffc2017-08-10 10:03:20 +0100345
nagendra modadugu284b9392017-09-27 13:51:23 -0700346 KM_CALL(DeleteAllKeys);
347
348 return (ErrorCode)response.error_code();
Andrew Sculle4e2ffc2017-08-10 10:03:20 +0100349}
350
nagendra modadugu284b9392017-09-27 13:51:23 -0700351Return<ErrorCode> KeymasterDevice::destroyAttestationIds()
352{
Andrew Sculle4e2ffc2017-08-10 10:03:20 +0100353 LOG(VERBOSE) << "Running KeymasterDevice::destroyAttestationIds";
354
nagendra modadugu284b9392017-09-27 13:51:23 -0700355 DestroyAttestationIdsRequest request;
356 DestroyAttestationIdsResponse response;
Andrew Sculle4e2ffc2017-08-10 10:03:20 +0100357
nagendra modadugu284b9392017-09-27 13:51:23 -0700358 KM_CALL(DestroyAttestationIds);
359
360 return (ErrorCode)response.error_code();
Andrew Sculle4e2ffc2017-08-10 10:03:20 +0100361}
362
nagendra modadugu284b9392017-09-27 13:51:23 -0700363Return<void> KeymasterDevice::begin(
364 KeyPurpose purpose, const hidl_vec<uint8_t>& key,
365 const hidl_vec<KeyParameter>& inParams, begin_cb _hidl_cb)
366{
Andrew Sculle4e2ffc2017-08-10 10:03:20 +0100367 LOG(VERBOSE) << "Running KeymasterDevice::begin";
368
nagendra modadugu284b9392017-09-27 13:51:23 -0700369 BeginOperationRequest request;
370 BeginOperationResponse response;
Andrew Sculle4e2ffc2017-08-10 10:03:20 +0100371
nagendra modadugu284b9392017-09-27 13:51:23 -0700372 request.set_purpose((::nugget::app::keymaster::KeyPurpose)purpose);
373 request.mutable_blob()->set_blob(&key[0], key.size());
374 hidl_params_to_pb(inParams, request.mutable_params());
375
376 KM_CALLV(BeginOperation, hidl_vec<KeyParameter>{}, 0);
377
378 hidl_vec<KeyParameter> params;
379 pb_to_hidl_params(response.params(), &params);
380
381 _hidl_cb((ErrorCode)response.error_code(), params,
382 response.handle().handle());
Andrew Sculle4e2ffc2017-08-10 10:03:20 +0100383 return Void();
384}
385
nagendra modadugu284b9392017-09-27 13:51:23 -0700386Return<void> KeymasterDevice::update(
387 uint64_t operationHandle,
388 const hidl_vec<KeyParameter>& inParams,
389 const hidl_vec<uint8_t>& input, update_cb _hidl_cb)
390{
Andrew Sculle4e2ffc2017-08-10 10:03:20 +0100391 LOG(VERBOSE) << "Running KeymasterDevice::update";
392
nagendra modadugu284b9392017-09-27 13:51:23 -0700393 UpdateOperationRequest request;
394 UpdateOperationResponse response;
Andrew Sculle4e2ffc2017-08-10 10:03:20 +0100395
nagendra modadugu284b9392017-09-27 13:51:23 -0700396 request.mutable_handle()->set_handle(operationHandle);
397 hidl_params_to_pb(inParams, request.mutable_params());
398 request.set_input(&input[0], input.size());
399
400 KM_CALLV(UpdateOperation, 0, hidl_vec<KeyParameter>{}, hidl_vec<uint8_t>{});
401
402 hidl_vec<KeyParameter> params;
403 hidl_vec<uint8_t> output;
404 pb_to_hidl_params(response.params(), &params);
405 output.setToExternal(
406 reinterpret_cast<uint8_t*>(const_cast<char*>(response.output().data())),
407 response.output().size(), false);
408
409 _hidl_cb(ErrorCode::OK, response.consumed(), params, output);
Andrew Sculle4e2ffc2017-08-10 10:03:20 +0100410 return Void();
411}
412
nagendra modadugu284b9392017-09-27 13:51:23 -0700413Return<void> KeymasterDevice::finish(
414 uint64_t operationHandle,
415 const hidl_vec<KeyParameter>& inParams,
416 const hidl_vec<uint8_t>& input,
417 const hidl_vec<uint8_t>& signature, finish_cb _hidl_cb)
418{
Andrew Sculle4e2ffc2017-08-10 10:03:20 +0100419 LOG(VERBOSE) << "Running KeymasterDevice::finish";
420
nagendra modadugu284b9392017-09-27 13:51:23 -0700421 FinishOperationRequest request;
422 FinishOperationResponse response;
Andrew Sculle4e2ffc2017-08-10 10:03:20 +0100423
nagendra modadugu284b9392017-09-27 13:51:23 -0700424 request.mutable_handle()->set_handle(operationHandle);
425 hidl_params_to_pb(inParams, request.mutable_params());
426 request.set_input(&input[0], input.size());
427 request.set_signature(&signature[0], signature.size());
428
429 KM_CALLV(FinishOperation, hidl_vec<KeyParameter>{}, hidl_vec<uint8_t>{});
430
431 hidl_vec<KeyParameter> params;
432 pb_to_hidl_params(response.params(), &params);
433 hidl_vec<uint8_t> output;
434 output.setToExternal(
435 reinterpret_cast<uint8_t*>(const_cast<char*>(response.output().data())),
436 response.output().size(), false);
437
438 _hidl_cb(ErrorCode::OK, params, output);
Andrew Sculle4e2ffc2017-08-10 10:03:20 +0100439 return Void();
440}
441
nagendra modadugu284b9392017-09-27 13:51:23 -0700442Return<ErrorCode> KeymasterDevice::abort(uint64_t operationHandle)
443{
Andrew Sculle4e2ffc2017-08-10 10:03:20 +0100444 LOG(VERBOSE) << "Running KeymasterDevice::abort";
445
nagendra modadugu284b9392017-09-27 13:51:23 -0700446 AbortOperationRequest request;
447 AbortOperationResponse response;
Andrew Sculle4e2ffc2017-08-10 10:03:20 +0100448
nagendra modadugu284b9392017-09-27 13:51:23 -0700449 request.mutable_handle()->set_handle(operationHandle);
450
451 KM_CALL(AbortOperation);
452
453 return ErrorCode::OK;
Andrew Sculle4e2ffc2017-08-10 10:03:20 +0100454}
455
456} // namespace keymaster
457} // namespace hardware
458} // namespace android