blob: bd79a8cd289ceea842beeb5ee83af050527aaa92 [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 modadugu97471402017-09-27 14:01:00 -070018#include "import_key.h"
nagendra modaduguec667302017-12-04 21:37:22 -080019#include "import_wrapped_key.h"
nagendra modadugu284b9392017-09-27 13:51:23 -070020#include "proto_utils.h"
21
nagendra modadugu9e8c8562017-09-18 18:58:54 -070022#include <Keymaster.client.h>
Andrew Scull8e96ff02017-10-09 11:09:09 +010023#include <nos/debug.h>
nagendra modadugu284b9392017-09-27 13:51:23 -070024#include <nos/NuggetClient.h>
Andrew Sculle4e2ffc2017-08-10 10:03:20 +010025
nagendra modadugue2914e42018-01-02 17:42:46 -080026#include <keymasterV4_0/key_param_output.h>
27
Andrew Sculle4e2ffc2017-08-10 10:03:20 +010028#include <android-base/logging.h>
29
30namespace android {
31namespace hardware {
32namespace keymaster {
33
nagendra modadugu8a883372017-09-18 22:29:00 -070034// std
35using std::string;
36
Andrew Sculle4e2ffc2017-08-10 10:03:20 +010037// libhidl
38using ::android::hardware::Void;
39
40// HAL
nagendra modaduguf1101762018-01-04 17:59:51 -080041using ::android::hardware::keymaster::V4_0::Algorithm;
nagendra modaduguec667302017-12-04 21:37:22 -080042using ::android::hardware::keymaster::V4_0::KeyCharacteristics;
nagendra modaduguf1101762018-01-04 17:59:51 -080043using ::android::hardware::keymaster::V4_0::KeyFormat;
nagendra modaduguec667302017-12-04 21:37:22 -080044using ::android::hardware::keymaster::V4_0::SecurityLevel;
45using ::android::hardware::keymaster::V4_0::Tag;
nagendra modadugu284b9392017-09-27 13:51:23 -070046
47// nos
48using nos::NuggetClient;
49
50// Keymaster app
nagendra modaduguec667302017-12-04 21:37:22 -080051// KM 3.0 types
nagendra modadugu284b9392017-09-27 13:51:23 -070052using ::nugget::app::keymaster::AddRngEntropyRequest;
53using ::nugget::app::keymaster::AddRngEntropyResponse;
54using ::nugget::app::keymaster::GenerateKeyRequest;
55using ::nugget::app::keymaster::GenerateKeyResponse;
56using ::nugget::app::keymaster::GetKeyCharacteristicsRequest;
57using ::nugget::app::keymaster::GetKeyCharacteristicsResponse;
58using ::nugget::app::keymaster::ImportKeyRequest;
59using ::nugget::app::keymaster::ImportKeyResponse;
60using ::nugget::app::keymaster::ExportKeyRequest;
61using ::nugget::app::keymaster::ExportKeyResponse;
62using ::nugget::app::keymaster::AttestKeyRequest;
63using ::nugget::app::keymaster::AttestKeyResponse;
64using ::nugget::app::keymaster::UpgradeKeyRequest;
65using ::nugget::app::keymaster::UpgradeKeyResponse;
66using ::nugget::app::keymaster::DeleteKeyRequest;
67using ::nugget::app::keymaster::DeleteKeyResponse;
68using ::nugget::app::keymaster::DeleteAllKeysRequest;
69using ::nugget::app::keymaster::DeleteAllKeysResponse;
70using ::nugget::app::keymaster::DestroyAttestationIdsRequest;
71using ::nugget::app::keymaster::DestroyAttestationIdsResponse;
72using ::nugget::app::keymaster::BeginOperationRequest;
73using ::nugget::app::keymaster::BeginOperationResponse;
74using ::nugget::app::keymaster::UpdateOperationRequest;
75using ::nugget::app::keymaster::UpdateOperationResponse;
76using ::nugget::app::keymaster::FinishOperationRequest;
77using ::nugget::app::keymaster::FinishOperationResponse;
78using ::nugget::app::keymaster::AbortOperationRequest;
79using ::nugget::app::keymaster::AbortOperationResponse;
80
nagendra modaduguec667302017-12-04 21:37:22 -080081// KM 4.0 types
82using ::nugget::app::keymaster::ImportWrappedKeyRequest;
83
nagendra modadugu284b9392017-09-27 13:51:23 -070084static ErrorCode status_to_error_code(uint32_t status)
85{
86 switch (status) {
87 case APP_SUCCESS:
88 return ErrorCode::OK;
89 break;
90 case APP_ERROR_BOGUS_ARGS:
91 return ErrorCode::INVALID_ARGUMENT;
92 break;
93 case APP_ERROR_INTERNAL:
94 return ErrorCode::UNKNOWN_ERROR;
95 break;
96 case APP_ERROR_TOO_MUCH:
97 return ErrorCode::INSUFFICIENT_BUFFER_SPACE;
98 break;
99 case APP_ERROR_RPC:
100 return ErrorCode::SECURE_HW_COMMUNICATION_FAILED;
101 break;
102 // TODO: app specific error codes go here.
103 default:
104 return ErrorCode::UNKNOWN_ERROR;
105 break;
106 }
107}
108
nagendra modadugu07be7f12017-12-28 11:20:45 -0800109#define KM_CALL(meth) { \
110 const uint32_t status = _keymaster. meth (request, &response); \
111 const ErrorCode error_code = translate_error_code(response.error_code()); \
112 if (status != APP_SUCCESS) { \
113 LOG(ERROR) << #meth << " : request failed with status: " \
114 << nos::StatusCodeString(status); \
115 return status_to_error_code(status); \
116 } \
117 if (error_code != ErrorCode::OK) { \
118 LOG(ERROR) << #meth << " : device response error code: " \
nagendra modadugue2914e42018-01-02 17:42:46 -0800119 << error_code; \
nagendra modadugu07be7f12017-12-28 11:20:45 -0800120 return error_code; \
121 } \
nagendra modadugu284b9392017-09-27 13:51:23 -0700122}
123
nagendra modadugu07be7f12017-12-28 11:20:45 -0800124#define KM_CALLV(meth, ...) { \
125 const uint32_t status = _keymaster. meth (request, &response); \
126 const ErrorCode error_code = translate_error_code(response.error_code()); \
127 if (status != APP_SUCCESS) { \
128 LOG(ERROR) << #meth << " : request failed with status: " \
129 << nos::StatusCodeString(status); \
130 _hidl_cb(status_to_error_code(status), __VA_ARGS__); \
131 return Void(); \
132 } \
133 if (error_code != ErrorCode::OK) { \
134 LOG(ERROR) << #meth << " : device response error code: " \
nagendra modadugue2914e42018-01-02 17:42:46 -0800135 << error_code; \
nagendra modadugu07be7f12017-12-28 11:20:45 -0800136 /* TODO: translate error codes. */ \
137 _hidl_cb(error_code, __VA_ARGS__); \
138 return Void(); \
139 } \
nagendra modadugu284b9392017-09-27 13:51:23 -0700140}
Andrew Sculle4e2ffc2017-08-10 10:03:20 +0100141
142// Methods from ::android::hardware::keymaster::V3_0::IKeymasterDevice follow.
nagendra modadugu284b9392017-09-27 13:51:23 -0700143
nagendra modaduguec667302017-12-04 21:37:22 -0800144Return<void> KeymasterDevice::getHardwareInfo(
145 getHardwareInfo_cb _hidl_cb)
nagendra modadugu284b9392017-09-27 13:51:23 -0700146{
nagendra modaduguec667302017-12-04 21:37:22 -0800147 LOG(VERBOSE) << "Running KeymasterDevice::getHardwareInfo";
Andrew Sculle4e2ffc2017-08-10 10:03:20 +0100148
nagendra modadugu8a883372017-09-18 22:29:00 -0700149 (void)_keymaster;
nagendra modaduguec667302017-12-04 21:37:22 -0800150 _hidl_cb(SecurityLevel::STRONGBOX,
151 string("CitadelKeymaster"), string("Google"));
152
153 return Void();
154}
155
156Return<void> KeymasterDevice::getHmacSharingParameters(
157 getHmacSharingParameters_cb _hidl_cb)
158{
159 LOG(VERBOSE) << "Running KeymasterDevice::getHmacSharingParameters";
160
161 (void)_keymaster;
162 _hidl_cb(ErrorCode::UNIMPLEMENTED, HmacSharingParameters());
163
164 return Void();
165}
166
167Return<void> KeymasterDevice::computeSharedHmac(
168 const hidl_vec<HmacSharingParameters>& params,
169 computeSharedHmac_cb _hidl_cb)
170{
171 LOG(VERBOSE) << "Running KeymasterDevice::computeSharedHmac";
172
173 (void)params;
174
175 (void)_keymaster;
176 _hidl_cb(ErrorCode::UNIMPLEMENTED, hidl_vec<uint8_t>{});
177
178 return Void();
179}
180
181Return<void> KeymasterDevice::verifyAuthorization(
182 uint64_t challenge, const hidl_vec<KeyParameter>& parametersToVerify,
183 const HardwareAuthToken& authToken, verifyAuthorization_cb _hidl_cb)
184{
185 LOG(VERBOSE) << "Running KeymasterDevice::verifyAuthorization";
186
187 (void)challenge;
188 (void)parametersToVerify;
189 (void)authToken;
190
191 (void)_keymaster;
192 _hidl_cb(ErrorCode::UNIMPLEMENTED, VerificationToken());
193
Andrew Sculle4e2ffc2017-08-10 10:03:20 +0100194 return Void();
195}
196
nagendra modadugu284b9392017-09-27 13:51:23 -0700197Return<ErrorCode> KeymasterDevice::addRngEntropy(const hidl_vec<uint8_t>& data)
198{
Andrew Sculle4e2ffc2017-08-10 10:03:20 +0100199 LOG(VERBOSE) << "Running KeymasterDevice::addRngEntropy";
200
nagendra modadugu284b9392017-09-27 13:51:23 -0700201 if (!data.size()) return ErrorCode::OK;
Andrew Sculle4e2ffc2017-08-10 10:03:20 +0100202
nagendra modadugue2914e42018-01-02 17:42:46 -0800203 const size_t chunk_size = 1024;
204 for (size_t i = 0; i < data.size(); i += chunk_size) {
205 AddRngEntropyRequest request;
206 AddRngEntropyResponse response;
nagendra modadugu284b9392017-09-27 13:51:23 -0700207
nagendra modadugue2914e42018-01-02 17:42:46 -0800208 request.set_data(&data[i], std::min(chunk_size, data.size() - i));
nagendra modadugu284b9392017-09-27 13:51:23 -0700209
nagendra modadugue2914e42018-01-02 17:42:46 -0800210 // Call device.
211 KM_CALL(AddRngEntropy);
212 }
213
214 return ErrorCode::OK;
Andrew Sculle4e2ffc2017-08-10 10:03:20 +0100215}
216
nagendra modadugu284b9392017-09-27 13:51:23 -0700217Return<void> KeymasterDevice::generateKey(
218 const hidl_vec<KeyParameter>& keyParams,
219 generateKey_cb _hidl_cb)
220{
Andrew Sculle4e2ffc2017-08-10 10:03:20 +0100221 LOG(VERBOSE) << "Running KeymasterDevice::generateKey";
222
nagendra modadugu284b9392017-09-27 13:51:23 -0700223 GenerateKeyRequest request;
224 GenerateKeyResponse response;
Andrew Sculle4e2ffc2017-08-10 10:03:20 +0100225
nagendra modadugue3ef4692017-10-25 23:58:07 -0700226 hidl_vec<uint8_t> blob;
227 KeyCharacteristics characteristics;
228 if (hidl_params_to_pb(
229 keyParams, request.mutable_params()) != ErrorCode::OK) {
230 _hidl_cb(ErrorCode::INVALID_ARGUMENT, blob, characteristics);
231 return Void();
232 }
nagendra modadugu284b9392017-09-27 13:51:23 -0700233
234 // Call device.
235 KM_CALLV(GenerateKey, hidl_vec<uint8_t>{}, KeyCharacteristics());
236
nagendra modadugu284b9392017-09-27 13:51:23 -0700237 blob.setToExternal(
238 reinterpret_cast<uint8_t*>(
239 const_cast<char*>(response.blob().blob().data())),
240 response.blob().blob().size(), false);
nagendra modadugue3ef4692017-10-25 23:58:07 -0700241 pb_to_hidl_params(response.characteristics().software_enforced(),
242 &characteristics.softwareEnforced);
nagendra modadugu284b9392017-09-27 13:51:23 -0700243 pb_to_hidl_params(response.characteristics().tee_enforced(),
nagendra modaduguec667302017-12-04 21:37:22 -0800244 &characteristics.hardwareEnforced);
nagendra modadugu284b9392017-09-27 13:51:23 -0700245
nagendra modadugue2914e42018-01-02 17:42:46 -0800246 _hidl_cb(translate_error_code(response.error_code()),
247 blob, characteristics);
Andrew Sculle4e2ffc2017-08-10 10:03:20 +0100248 return Void();
249}
250
nagendra modadugu284b9392017-09-27 13:51:23 -0700251Return<void> KeymasterDevice::getKeyCharacteristics(
252 const hidl_vec<uint8_t>& keyBlob,
253 const hidl_vec<uint8_t>& clientId,
254 const hidl_vec<uint8_t>& appData,
255 getKeyCharacteristics_cb _hidl_cb)
256{
Andrew Sculle4e2ffc2017-08-10 10:03:20 +0100257 LOG(VERBOSE) << "Running KeymasterDevice::getKeyCharacteristics";
258
nagendra modadugu284b9392017-09-27 13:51:23 -0700259 GetKeyCharacteristicsRequest request;
260 GetKeyCharacteristicsResponse response;
Andrew Sculle4e2ffc2017-08-10 10:03:20 +0100261
nagendra modadugu284b9392017-09-27 13:51:23 -0700262 request.mutable_blob()->set_blob(&keyBlob[0], keyBlob.size());
263 request.set_client_id(&clientId[0], clientId.size());
264 request.set_app_data(&appData[0], appData.size());
265
266 // Call device.
267 KM_CALLV(GetKeyCharacteristics, KeyCharacteristics());
268
269 KeyCharacteristics characteristics;
nagendra modadugue3ef4692017-10-25 23:58:07 -0700270 pb_to_hidl_params(response.characteristics().software_enforced(),
271 &characteristics.softwareEnforced);
nagendra modadugu284b9392017-09-27 13:51:23 -0700272 pb_to_hidl_params(response.characteristics().tee_enforced(),
nagendra modaduguec667302017-12-04 21:37:22 -0800273 &characteristics.hardwareEnforced);
nagendra modadugu284b9392017-09-27 13:51:23 -0700274
nagendra modadugue2914e42018-01-02 17:42:46 -0800275 _hidl_cb(translate_error_code(response.error_code()), characteristics);
Andrew Sculle4e2ffc2017-08-10 10:03:20 +0100276 return Void();
277}
278
nagendra modadugu284b9392017-09-27 13:51:23 -0700279Return<void> KeymasterDevice::importKey(
280 const hidl_vec<KeyParameter>& params, KeyFormat keyFormat,
281 const hidl_vec<uint8_t>& keyData, importKey_cb _hidl_cb)
282{
Andrew Sculle4e2ffc2017-08-10 10:03:20 +0100283 LOG(VERBOSE) << "Running KeymasterDevice::importKey";
284
nagendra modadugu97471402017-09-27 14:01:00 -0700285 ErrorCode error;
nagendra modadugu284b9392017-09-27 13:51:23 -0700286 ImportKeyRequest request;
287 ImportKeyResponse response;
Andrew Sculle4e2ffc2017-08-10 10:03:20 +0100288
nagendra modadugu97471402017-09-27 14:01:00 -0700289 error = import_key_request(params, keyFormat, keyData, &request);
290 if (error != ErrorCode::OK) {
291 LOG(ERROR) << "ImportKey request parsing failed with error "
nagendra modadugue2914e42018-01-02 17:42:46 -0800292 << error;
nagendra modadugu97471402017-09-27 14:01:00 -0700293 _hidl_cb(error, hidl_vec<uint8_t>{}, KeyCharacteristics{});
294 return Void();
295 }
nagendra modadugu284b9392017-09-27 13:51:23 -0700296
297 KM_CALLV(ImportKey, hidl_vec<uint8_t>{}, KeyCharacteristics{});
298
299 hidl_vec<uint8_t> blob;
300 blob.setToExternal(
301 reinterpret_cast<uint8_t*>(
302 const_cast<char*>(response.blob().blob().data())),
303 response.blob().blob().size(), false);
304
305 KeyCharacteristics characteristics;
nagendra modadugue3ef4692017-10-25 23:58:07 -0700306 pb_to_hidl_params(response.characteristics().software_enforced(),
307 &characteristics.softwareEnforced);
nagendra modadugu97471402017-09-27 14:01:00 -0700308 error = pb_to_hidl_params(response.characteristics().tee_enforced(),
nagendra modaduguec667302017-12-04 21:37:22 -0800309 &characteristics.hardwareEnforced);
nagendra modadugu97471402017-09-27 14:01:00 -0700310 if (error != ErrorCode::OK) {
311 LOG(ERROR) << "KeymasterDevice::importKey: response tee_enforced :"
nagendra modadugue2914e42018-01-02 17:42:46 -0800312 << error;
nagendra modaduguec667302017-12-04 21:37:22 -0800313 _hidl_cb(error, hidl_vec<uint8_t>{}, KeyCharacteristics{});
314 return Void();
nagendra modadugu97471402017-09-27 14:01:00 -0700315 }
nagendra modadugu284b9392017-09-27 13:51:23 -0700316
317 _hidl_cb(ErrorCode::OK, blob, characteristics);
Andrew Sculle4e2ffc2017-08-10 10:03:20 +0100318 return Void();
319}
320
nagendra modadugu284b9392017-09-27 13:51:23 -0700321Return<void> KeymasterDevice::exportKey(
322 KeyFormat exportFormat, const hidl_vec<uint8_t>& keyBlob,
323 const hidl_vec<uint8_t>& clientId,
324 const hidl_vec<uint8_t>& appData, exportKey_cb _hidl_cb)
325{
Andrew Sculle4e2ffc2017-08-10 10:03:20 +0100326 LOG(VERBOSE) << "Running KeymasterDevice::exportKey";
327
nagendra modadugu284b9392017-09-27 13:51:23 -0700328 ExportKeyRequest request;
329 ExportKeyResponse response;
Andrew Sculle4e2ffc2017-08-10 10:03:20 +0100330
nagendra modadugu284b9392017-09-27 13:51:23 -0700331 request.set_format((::nugget::app::keymaster::KeyFormat)exportFormat);
332 request.mutable_blob()->set_blob(&keyBlob[0], keyBlob.size());
333 request.set_client_id(&clientId[0], clientId.size());
334 request.set_app_data(&appData[0], appData.size());
335
336 KM_CALLV(ExportKey, hidl_vec<uint8_t>{});
337
338 hidl_vec<uint8_t> blob;
339 blob.setToExternal(
340 reinterpret_cast<uint8_t*>(
341 const_cast<char*>(response.key_material().data())),
342 response.key_material().size(), false);
343
nagendra modadugue2914e42018-01-02 17:42:46 -0800344 _hidl_cb(translate_error_code(response.error_code()), blob);
Andrew Sculle4e2ffc2017-08-10 10:03:20 +0100345 return Void();
346}
347
nagendra modadugu284b9392017-09-27 13:51:23 -0700348Return<void> KeymasterDevice::attestKey(
349 const hidl_vec<uint8_t>& keyToAttest,
350 const hidl_vec<KeyParameter>& attestParams,
351 attestKey_cb _hidl_cb)
352{
Andrew Sculle4e2ffc2017-08-10 10:03:20 +0100353 LOG(VERBOSE) << "Running KeymasterDevice::attestKey";
354
nagendra modadugu284b9392017-09-27 13:51:23 -0700355 AttestKeyRequest request;
356 AttestKeyResponse response;
Andrew Sculle4e2ffc2017-08-10 10:03:20 +0100357
nagendra modadugu284b9392017-09-27 13:51:23 -0700358 request.mutable_blob()->set_blob(&keyToAttest[0], keyToAttest.size());
nagendra modadugue3ef4692017-10-25 23:58:07 -0700359
360 vector<hidl_vec<uint8_t> > chain;
361 if (hidl_params_to_pb(
362 attestParams, request.mutable_params()) != ErrorCode::OK) {
363 _hidl_cb(ErrorCode::INVALID_ARGUMENT, chain);
364 return Void();
365 }
nagendra modadugu284b9392017-09-27 13:51:23 -0700366
367 KM_CALLV(AttestKey, hidl_vec<hidl_vec<uint8_t> >{});
368
nagendra modadugu284b9392017-09-27 13:51:23 -0700369 for (int i = 0; i < response.chain().certificates_size(); i++) {
370 hidl_vec<uint8_t> blob;
371 blob.setToExternal(
372 reinterpret_cast<uint8_t*>(
373 const_cast<char*>(
374 response.chain().certificates(i).data().data())),
375 response.chain().certificates(i).data().size(), false);
376 chain.push_back(blob);
377 }
378
nagendra modadugue2914e42018-01-02 17:42:46 -0800379 _hidl_cb(translate_error_code(response.error_code()),
nagendra modadugu284b9392017-09-27 13:51:23 -0700380 hidl_vec<hidl_vec<uint8_t> >(chain));
Andrew Sculle4e2ffc2017-08-10 10:03:20 +0100381 return Void();
382}
383
nagendra modadugu284b9392017-09-27 13:51:23 -0700384Return<void> KeymasterDevice::upgradeKey(
385 const hidl_vec<uint8_t>& keyBlobToUpgrade,
386 const hidl_vec<KeyParameter>& upgradeParams,
387 upgradeKey_cb _hidl_cb)
388{
Andrew Sculle4e2ffc2017-08-10 10:03:20 +0100389 LOG(VERBOSE) << "Running KeymasterDevice::upgradeKey";
390
nagendra modadugu284b9392017-09-27 13:51:23 -0700391 UpgradeKeyRequest request;
392 UpgradeKeyResponse response;
Andrew Sculle4e2ffc2017-08-10 10:03:20 +0100393
nagendra modadugu284b9392017-09-27 13:51:23 -0700394 request.mutable_blob()->set_blob(&keyBlobToUpgrade[0],
395 keyBlobToUpgrade.size());
nagendra modadugue3ef4692017-10-25 23:58:07 -0700396
397 hidl_vec<uint8_t> blob;
398 if (hidl_params_to_pb(
399 upgradeParams, request.mutable_params()) != ErrorCode::OK) {
400 _hidl_cb(ErrorCode::INVALID_ARGUMENT, blob);
401 return Void();
402 }
nagendra modadugu284b9392017-09-27 13:51:23 -0700403
404 KM_CALLV(UpgradeKey, hidl_vec<uint8_t>{});
405
nagendra modadugu284b9392017-09-27 13:51:23 -0700406 blob.setToExternal(
407 reinterpret_cast<uint8_t*>(
408 const_cast<char*>(response.blob().blob().data())),
409 response.blob().blob().size(), false);
410
nagendra modadugue2914e42018-01-02 17:42:46 -0800411 _hidl_cb(translate_error_code(response.error_code()), blob);
Andrew Sculle4e2ffc2017-08-10 10:03:20 +0100412 return Void();
413}
414
nagendra modadugu284b9392017-09-27 13:51:23 -0700415Return<ErrorCode> KeymasterDevice::deleteKey(const hidl_vec<uint8_t>& keyBlob)
416{
Andrew Sculle4e2ffc2017-08-10 10:03:20 +0100417 LOG(VERBOSE) << "Running KeymasterDevice::deleteKey";
418
nagendra modadugu284b9392017-09-27 13:51:23 -0700419 DeleteKeyRequest request;
420 DeleteKeyResponse response;
Andrew Sculle4e2ffc2017-08-10 10:03:20 +0100421
nagendra modadugu284b9392017-09-27 13:51:23 -0700422 request.mutable_blob()->set_blob(&keyBlob[0], keyBlob.size());
423
424 KM_CALL(DeleteKey);
425
nagendra modadugue2914e42018-01-02 17:42:46 -0800426 return translate_error_code(response.error_code());
Andrew Sculle4e2ffc2017-08-10 10:03:20 +0100427}
428
nagendra modadugu284b9392017-09-27 13:51:23 -0700429Return<ErrorCode> KeymasterDevice::deleteAllKeys()
430{
Andrew Sculle4e2ffc2017-08-10 10:03:20 +0100431 LOG(VERBOSE) << "Running KeymasterDevice::deleteAllKeys";
432
nagendra modadugu284b9392017-09-27 13:51:23 -0700433 DeleteAllKeysRequest request;
434 DeleteAllKeysResponse response;
Andrew Sculle4e2ffc2017-08-10 10:03:20 +0100435
nagendra modadugu284b9392017-09-27 13:51:23 -0700436 KM_CALL(DeleteAllKeys);
437
nagendra modadugue2914e42018-01-02 17:42:46 -0800438 return translate_error_code(response.error_code());
Andrew Sculle4e2ffc2017-08-10 10:03:20 +0100439}
440
nagendra modadugu284b9392017-09-27 13:51:23 -0700441Return<ErrorCode> KeymasterDevice::destroyAttestationIds()
442{
Andrew Sculle4e2ffc2017-08-10 10:03:20 +0100443 LOG(VERBOSE) << "Running KeymasterDevice::destroyAttestationIds";
444
nagendra modadugu284b9392017-09-27 13:51:23 -0700445 DestroyAttestationIdsRequest request;
446 DestroyAttestationIdsResponse response;
Andrew Sculle4e2ffc2017-08-10 10:03:20 +0100447
nagendra modadugu284b9392017-09-27 13:51:23 -0700448 KM_CALL(DestroyAttestationIds);
449
nagendra modadugue2914e42018-01-02 17:42:46 -0800450 return translate_error_code(response.error_code());
Andrew Sculle4e2ffc2017-08-10 10:03:20 +0100451}
452
nagendra modadugu284b9392017-09-27 13:51:23 -0700453Return<void> KeymasterDevice::begin(
454 KeyPurpose purpose, const hidl_vec<uint8_t>& key,
nagendra modaduguec667302017-12-04 21:37:22 -0800455 const hidl_vec<KeyParameter>& inParams,
456 const HardwareAuthToken& authToken,
457 begin_cb _hidl_cb)
nagendra modadugu284b9392017-09-27 13:51:23 -0700458{
Andrew Sculle4e2ffc2017-08-10 10:03:20 +0100459 LOG(VERBOSE) << "Running KeymasterDevice::begin";
460
nagendra modadugu284b9392017-09-27 13:51:23 -0700461 BeginOperationRequest request;
462 BeginOperationResponse response;
Andrew Sculle4e2ffc2017-08-10 10:03:20 +0100463
nagendra modadugu284b9392017-09-27 13:51:23 -0700464 request.set_purpose((::nugget::app::keymaster::KeyPurpose)purpose);
465 request.mutable_blob()->set_blob(&key[0], key.size());
nagendra modaduguec667302017-12-04 21:37:22 -0800466 // TODO: set request.auth_token().
467 (void)authToken;
nagendra modadugue3ef4692017-10-25 23:58:07 -0700468
469 hidl_vec<KeyParameter> params;
470 if (hidl_params_to_pb(
471 inParams, request.mutable_params()) != ErrorCode::OK) {
472 _hidl_cb(ErrorCode::INVALID_ARGUMENT, params,
473 response.handle().handle());
474 return Void();
475 }
nagendra modadugu284b9392017-09-27 13:51:23 -0700476
477 KM_CALLV(BeginOperation, hidl_vec<KeyParameter>{}, 0);
478
nagendra modadugu284b9392017-09-27 13:51:23 -0700479 pb_to_hidl_params(response.params(), &params);
480
nagendra modadugue2914e42018-01-02 17:42:46 -0800481 _hidl_cb(translate_error_code(response.error_code()), params,
nagendra modadugu284b9392017-09-27 13:51:23 -0700482 response.handle().handle());
Andrew Sculle4e2ffc2017-08-10 10:03:20 +0100483 return Void();
484}
485
nagendra modadugu284b9392017-09-27 13:51:23 -0700486Return<void> KeymasterDevice::update(
487 uint64_t operationHandle,
488 const hidl_vec<KeyParameter>& inParams,
nagendra modaduguec667302017-12-04 21:37:22 -0800489 const hidl_vec<uint8_t>& input,
490 const HardwareAuthToken& authToken,
491 const VerificationToken& verificationToken,
492 update_cb _hidl_cb)
nagendra modadugu284b9392017-09-27 13:51:23 -0700493{
Andrew Sculle4e2ffc2017-08-10 10:03:20 +0100494 LOG(VERBOSE) << "Running KeymasterDevice::update";
495
nagendra modadugu284b9392017-09-27 13:51:23 -0700496 UpdateOperationRequest request;
497 UpdateOperationResponse response;
Andrew Sculle4e2ffc2017-08-10 10:03:20 +0100498
nagendra modadugu19439672018-01-17 16:43:26 -0800499 if (input.size() > KM_MAX_PROTO_FIELD_SIZE) {
500 LOG(ERROR) << "Excess input length: " << input.size()
501 << "max allowed: " << KM_MAX_PROTO_FIELD_SIZE;
502 if (this->abort(operationHandle) != ErrorCode::OK) {
503 LOG(ERROR) << "abort( " << operationHandle
504 << ") failed";
505 }
506 _hidl_cb(ErrorCode::INVALID_INPUT_LENGTH, 0,
507 hidl_vec<KeyParameter>{}, hidl_vec<uint8_t>{});
508 return Void();
509 }
510
nagendra modadugu284b9392017-09-27 13:51:23 -0700511 request.mutable_handle()->set_handle(operationHandle);
nagendra modadugue3ef4692017-10-25 23:58:07 -0700512
513 hidl_vec<KeyParameter> params;
514 hidl_vec<uint8_t> output;
515 if (hidl_params_to_pb(
516 inParams, request.mutable_params()) != ErrorCode::OK) {
517 _hidl_cb(ErrorCode::INVALID_ARGUMENT, 0, params, output);
518 return Void();
519 }
520
nagendra modadugu284b9392017-09-27 13:51:23 -0700521 request.set_input(&input[0], input.size());
nagendra modaduguec667302017-12-04 21:37:22 -0800522 // TODO: add authToken and verificationToken.
523 (void)authToken;
524 (void)verificationToken;
nagendra modadugu284b9392017-09-27 13:51:23 -0700525
526 KM_CALLV(UpdateOperation, 0, hidl_vec<KeyParameter>{}, hidl_vec<uint8_t>{});
527
nagendra modadugu284b9392017-09-27 13:51:23 -0700528 pb_to_hidl_params(response.params(), &params);
529 output.setToExternal(
530 reinterpret_cast<uint8_t*>(const_cast<char*>(response.output().data())),
531 response.output().size(), false);
532
533 _hidl_cb(ErrorCode::OK, response.consumed(), params, output);
Andrew Sculle4e2ffc2017-08-10 10:03:20 +0100534 return Void();
535}
536
nagendra modadugu284b9392017-09-27 13:51:23 -0700537Return<void> KeymasterDevice::finish(
538 uint64_t operationHandle,
539 const hidl_vec<KeyParameter>& inParams,
540 const hidl_vec<uint8_t>& input,
nagendra modaduguec667302017-12-04 21:37:22 -0800541 const hidl_vec<uint8_t>& signature,
542 const HardwareAuthToken& authToken,
543 const VerificationToken& verificationToken,
544 finish_cb _hidl_cb)
nagendra modadugu284b9392017-09-27 13:51:23 -0700545{
Andrew Sculle4e2ffc2017-08-10 10:03:20 +0100546 LOG(VERBOSE) << "Running KeymasterDevice::finish";
547
nagendra modadugu284b9392017-09-27 13:51:23 -0700548 FinishOperationRequest request;
549 FinishOperationResponse response;
Andrew Sculle4e2ffc2017-08-10 10:03:20 +0100550
nagendra modadugu19439672018-01-17 16:43:26 -0800551 if (input.size() > KM_MAX_PROTO_FIELD_SIZE) {
552 LOG(ERROR) << "Excess input length: " << input.size()
553 << "max allowed: " << KM_MAX_PROTO_FIELD_SIZE;
554 if (this->abort(operationHandle) != ErrorCode::OK) {
555 LOG(ERROR) << "abort( " << operationHandle
556 << ") failed";
557 }
558 _hidl_cb(ErrorCode::INVALID_INPUT_LENGTH,
559 hidl_vec<KeyParameter>{}, hidl_vec<uint8_t>{});
560 return Void();
561 }
562
nagendra modadugu284b9392017-09-27 13:51:23 -0700563 request.mutable_handle()->set_handle(operationHandle);
nagendra modadugue3ef4692017-10-25 23:58:07 -0700564
565 hidl_vec<KeyParameter> params;
566 hidl_vec<uint8_t> output;
567 if (hidl_params_to_pb(
568 inParams, request.mutable_params()) != ErrorCode::OK) {
569 _hidl_cb(ErrorCode::INVALID_ARGUMENT, params, output);
570 return Void();
571 }
572
nagendra modadugu284b9392017-09-27 13:51:23 -0700573 request.set_input(&input[0], input.size());
574 request.set_signature(&signature[0], signature.size());
575
nagendra modaduguec667302017-12-04 21:37:22 -0800576 // TODO: add authToken and verificationToken.
577 (void)authToken;
578 (void)verificationToken;
579
nagendra modadugu284b9392017-09-27 13:51:23 -0700580 KM_CALLV(FinishOperation, hidl_vec<KeyParameter>{}, hidl_vec<uint8_t>{});
581
nagendra modadugu284b9392017-09-27 13:51:23 -0700582 pb_to_hidl_params(response.params(), &params);
nagendra modadugu284b9392017-09-27 13:51:23 -0700583 output.setToExternal(
584 reinterpret_cast<uint8_t*>(const_cast<char*>(response.output().data())),
585 response.output().size(), false);
586
587 _hidl_cb(ErrorCode::OK, params, output);
Andrew Sculle4e2ffc2017-08-10 10:03:20 +0100588 return Void();
589}
590
nagendra modadugu284b9392017-09-27 13:51:23 -0700591Return<ErrorCode> KeymasterDevice::abort(uint64_t operationHandle)
592{
Andrew Sculle4e2ffc2017-08-10 10:03:20 +0100593 LOG(VERBOSE) << "Running KeymasterDevice::abort";
594
nagendra modadugu284b9392017-09-27 13:51:23 -0700595 AbortOperationRequest request;
596 AbortOperationResponse response;
Andrew Sculle4e2ffc2017-08-10 10:03:20 +0100597
nagendra modadugu284b9392017-09-27 13:51:23 -0700598 request.mutable_handle()->set_handle(operationHandle);
599
600 KM_CALL(AbortOperation);
601
602 return ErrorCode::OK;
Andrew Sculle4e2ffc2017-08-10 10:03:20 +0100603}
604
nagendra modaduguec667302017-12-04 21:37:22 -0800605// Methods from ::android::hardware::keymaster::V4_0::IKeymasterDevice follow.
606Return<void> KeymasterDevice::importWrappedKey(
607 const hidl_vec<uint8_t>& wrappedKeyData,
608 const hidl_vec<uint8_t>& wrappingKeyBlob,
609 const hidl_vec<uint8_t>& maskingKey,
nagendra modadugu9c231ef2018-01-19 15:51:59 -0800610 const hidl_vec<KeyParameter>& /* unwrappingParams */,
611 uint64_t /* passwordSid */, uint64_t /* biometricSid */,
nagendra modaduguec667302017-12-04 21:37:22 -0800612 importWrappedKey_cb _hidl_cb)
613{
614 LOG(VERBOSE) << "Running KeymasterDevice::importWrappedKey";
615
616 ErrorCode error;
617 ImportWrappedKeyRequest request;
618 ImportKeyResponse response;
619
620 if (maskingKey.size() != KM_WRAPPER_MASKING_KEY_SIZE) {
621 _hidl_cb(ErrorCode::INVALID_ARGUMENT, hidl_vec<uint8_t>{},
622 KeyCharacteristics{});
623 return Void();
624 }
625
626 error = import_wrapped_key_request(wrappedKeyData, wrappingKeyBlob,
627 maskingKey, &request);
628 if (error != ErrorCode::OK) {
629 LOG(ERROR) << "ImportWrappedKey request parsing failed with error "
nagendra modadugue2914e42018-01-02 17:42:46 -0800630 << error;
nagendra modaduguec667302017-12-04 21:37:22 -0800631 _hidl_cb(error, hidl_vec<uint8_t>{}, KeyCharacteristics{});
632 return Void();
633 }
634
635 KM_CALLV(ImportWrappedKey, hidl_vec<uint8_t>{}, KeyCharacteristics{});
636
637 hidl_vec<uint8_t> blob;
638 blob.setToExternal(
639 reinterpret_cast<uint8_t*>(
640 const_cast<char*>(response.blob().blob().data())),
641 response.blob().blob().size(), false);
642
643 KeyCharacteristics characteristics;
644 // TODO: anything to do here with softwareEnforced?
645 pb_to_hidl_params(response.characteristics().software_enforced(),
646 &characteristics.softwareEnforced);
647 error = pb_to_hidl_params(response.characteristics().tee_enforced(),
648 &characteristics.hardwareEnforced);
649 if (error != ErrorCode::OK) {
650 LOG(ERROR) <<
651 "KeymasterDevice::importWrappedKey: response tee_enforced :"
nagendra modadugue2914e42018-01-02 17:42:46 -0800652 << error;
nagendra modaduguec667302017-12-04 21:37:22 -0800653 _hidl_cb(error, hidl_vec<uint8_t>{}, KeyCharacteristics{});
654 return Void();
655 }
656
657 _hidl_cb(ErrorCode::OK, blob, characteristics);
658 return Void();
659}
660
Andrew Sculle4e2ffc2017-08-10 10:03:20 +0100661} // namespace keymaster
662} // namespace hardware
663} // namespace android