• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  **
3  ** Copyright 2017, The Android Open Source Project
4  **
5  ** Licensed under the Apache License, Version 2.0 (the "License");
6  ** you may not use this file except in compliance with the License.
7  ** You may obtain a copy of the License at
8  **
9  **     http://www.apache.org/licenses/LICENSE-2.0
10  **
11  ** Unless required by applicable law or agreed to in writing, software
12  ** distributed under the License is distributed on an "AS IS" BASIS,
13  ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  ** See the License for the specific language governing permissions and
15  ** limitations under the License.
16  */
17 
18 #define LOG_TAG "android.hardware.keymaster@4.0-impl"
19 #include <log/log.h>
20 
21 #include "include/AndroidKeymaster4Device.h"
22 
23 #include <keymasterV4_0/authorization_set.h>
24 #include <keymasterV4_0/keymaster_utils.h>
25 
26 #include <keymaster/android_keymaster.h>
27 #include <keymaster/android_keymaster_messages.h>
28 #include <keymaster/contexts/pure_soft_keymaster_context.h>
29 #include <keymaster/contexts/soft_keymaster_context.h>
30 #include <keymaster/keymaster_configuration.h>
31 #include <keymaster/keymaster_enforcement.h>
32 #include <keymaster/km_openssl/soft_keymaster_enforcement.h>
33 
34 using android::hardware::keymaster::V4_0::support::authToken2HidlVec;
35 
36 namespace keymaster {
37 namespace V4_0 {
38 namespace ng {
39 
40 namespace {
41 
42 constexpr size_t kOperationTableSize = 16;
43 
legacy_enum_conversion(const Tag value)44 inline keymaster_tag_t legacy_enum_conversion(const Tag value) {
45     return keymaster_tag_t(value);
46 }
47 
legacy_enum_conversion(const keymaster_tag_t value)48 inline Tag legacy_enum_conversion(const keymaster_tag_t value) {
49     return Tag(value);
50 }
51 
legacy_enum_conversion(const KeyPurpose value)52 inline keymaster_purpose_t legacy_enum_conversion(const KeyPurpose value) {
53     return static_cast<keymaster_purpose_t>(value);
54 }
55 
legacy_enum_conversion(const KeyFormat value)56 inline keymaster_key_format_t legacy_enum_conversion(const KeyFormat value) {
57     return static_cast<keymaster_key_format_t>(value);
58 }
59 
legacy_enum_conversion(const keymaster_security_level_t value)60 inline SecurityLevel legacy_enum_conversion(const keymaster_security_level_t value) {
61     return static_cast<SecurityLevel>(value);
62 }
63 
legacy_enum_conversion(const HardwareAuthenticatorType value)64 inline hw_authenticator_type_t legacy_enum_conversion(const HardwareAuthenticatorType value) {
65     return static_cast<hw_authenticator_type_t>(value);
66 }
67 
legacy_enum_conversion(const keymaster_error_t value)68 inline ErrorCode legacy_enum_conversion(const keymaster_error_t value) {
69     return static_cast<ErrorCode>(value);
70 }
71 
typeFromTag(const keymaster_tag_t tag)72 inline keymaster_tag_type_t typeFromTag(const keymaster_tag_t tag) {
73     return keymaster_tag_get_type(tag);
74 }
75 
76 class KmParamSet : public keymaster_key_param_set_t {
77   public:
KmParamSet(const hidl_vec<KeyParameter> & keyParams)78     explicit KmParamSet(const hidl_vec<KeyParameter>& keyParams)
79         : keymaster_key_param_set_t(hidlKeyParams2Km(keyParams)) {}
KmParamSet(KmParamSet && other)80     KmParamSet(KmParamSet&& other) : keymaster_key_param_set_t{other.params, other.length} {
81         other.length = 0;
82         other.params = nullptr;
83     }
84     KmParamSet(const KmParamSet&) = delete;
~KmParamSet()85     ~KmParamSet() { delete[] params; }
86 };
87 
kmBlob2hidlVec(const keymaster_key_blob_t & blob)88 inline hidl_vec<uint8_t> kmBlob2hidlVec(const keymaster_key_blob_t& blob) {
89     hidl_vec<uint8_t> result;
90     result.setToExternal(const_cast<unsigned char*>(blob.key_material), blob.key_material_size);
91     return result;
92 }
93 
kmBlob2hidlVec(const keymaster_blob_t & blob)94 inline hidl_vec<uint8_t> kmBlob2hidlVec(const keymaster_blob_t& blob) {
95     hidl_vec<uint8_t> result;
96     result.setToExternal(const_cast<unsigned char*>(blob.data), blob.data_length);
97     return result;
98 }
99 
kmBuffer2hidlVec(const::keymaster::Buffer & buf)100 inline hidl_vec<uint8_t> kmBuffer2hidlVec(const ::keymaster::Buffer& buf) {
101     hidl_vec<uint8_t> result;
102     result.setToExternal(const_cast<unsigned char*>(buf.peek_read()), buf.available_read());
103     return result;
104 }
105 
106 inline static hidl_vec<hidl_vec<uint8_t>>
kmCertChain2Hidl(const keymaster_cert_chain_t & cert_chain)107 kmCertChain2Hidl(const keymaster_cert_chain_t& cert_chain) {
108     hidl_vec<hidl_vec<uint8_t>> result;
109     if (!cert_chain.entry_count || !cert_chain.entries) return result;
110 
111     result.resize(cert_chain.entry_count);
112     for (size_t i = 0; i < cert_chain.entry_count; ++i) {
113         result[i] = kmBlob2hidlVec(cert_chain.entries[i]);
114     }
115 
116     return result;
117 }
118 
kmParamSet2Hidl(const keymaster_key_param_set_t & set)119 static inline hidl_vec<KeyParameter> kmParamSet2Hidl(const keymaster_key_param_set_t& set) {
120     hidl_vec<KeyParameter> result;
121     if (set.length == 0 || set.params == nullptr) return result;
122 
123     result.resize(set.length);
124     keymaster_key_param_t* params = set.params;
125     for (size_t i = 0; i < set.length; ++i) {
126         auto tag = params[i].tag;
127         result[i].tag = legacy_enum_conversion(tag);
128         switch (typeFromTag(tag)) {
129         case KM_ENUM:
130         case KM_ENUM_REP:
131             result[i].f.integer = params[i].enumerated;
132             break;
133         case KM_UINT:
134         case KM_UINT_REP:
135             result[i].f.integer = params[i].integer;
136             break;
137         case KM_ULONG:
138         case KM_ULONG_REP:
139             result[i].f.longInteger = params[i].long_integer;
140             break;
141         case KM_DATE:
142             result[i].f.dateTime = params[i].date_time;
143             break;
144         case KM_BOOL:
145             result[i].f.boolValue = params[i].boolean;
146             break;
147         case KM_BIGNUM:
148         case KM_BYTES:
149             result[i].blob.setToExternal(const_cast<unsigned char*>(params[i].blob.data),
150                                          params[i].blob.data_length);
151             break;
152         case KM_INVALID:
153         default:
154             params[i].tag = KM_TAG_INVALID;
155             /* just skip */
156             break;
157         }
158     }
159     return result;
160 }
161 
addClientAndAppData(const hidl_vec<uint8_t> & clientId,const hidl_vec<uint8_t> & appData,::keymaster::AuthorizationSet * params)162 void addClientAndAppData(const hidl_vec<uint8_t>& clientId, const hidl_vec<uint8_t>& appData,
163                          ::keymaster::AuthorizationSet* params) {
164     params->Clear();
165     if (clientId.size()) {
166         params->push_back(::keymaster::TAG_APPLICATION_ID, clientId.data(), clientId.size());
167     }
168     if (appData.size()) {
169         params->push_back(::keymaster::TAG_APPLICATION_DATA, appData.data(), appData.size());
170     }
171 }
172 
173 }  // anonymous namespace
174 
hidlKeyParams2Km(const hidl_vec<KeyParameter> & keyParams)175 keymaster_key_param_set_t hidlKeyParams2Km(const hidl_vec<KeyParameter>& keyParams) {
176     keymaster_key_param_set_t set;
177 
178     set.params = new keymaster_key_param_t[keyParams.size()];
179     set.length = keyParams.size();
180 
181     for (size_t i = 0; i < keyParams.size(); ++i) {
182         auto tag = legacy_enum_conversion(keyParams[i].tag);
183         switch (typeFromTag(tag)) {
184         case KM_ENUM:
185         case KM_ENUM_REP:
186             set.params[i] = keymaster_param_enum(tag, keyParams[i].f.integer);
187             break;
188         case KM_UINT:
189         case KM_UINT_REP:
190             set.params[i] = keymaster_param_int(tag, keyParams[i].f.integer);
191             break;
192         case KM_ULONG:
193         case KM_ULONG_REP:
194             set.params[i] = keymaster_param_long(tag, keyParams[i].f.longInteger);
195             break;
196         case KM_DATE:
197             set.params[i] = keymaster_param_date(tag, keyParams[i].f.dateTime);
198             break;
199         case KM_BOOL:
200             if (keyParams[i].f.boolValue)
201                 set.params[i] = keymaster_param_bool(tag);
202             else
203                 set.params[i].tag = KM_TAG_INVALID;
204             break;
205         case KM_BIGNUM:
206         case KM_BYTES:
207             set.params[i] =
208                 keymaster_param_blob(tag, &keyParams[i].blob[0], keyParams[i].blob.size());
209             break;
210         case KM_INVALID:
211         default:
212             set.params[i].tag = KM_TAG_INVALID;
213             /* just skip */
214             break;
215         }
216     }
217 
218     return set;
219 }
220 
AndroidKeymaster4Device(KmVersion version,SecurityLevel securityLevel)221 AndroidKeymaster4Device::AndroidKeymaster4Device(KmVersion version, SecurityLevel securityLevel)
222     : impl_(new ::keymaster::AndroidKeymaster(
223           [&]() -> auto {
224               auto context = new PureSoftKeymasterContext(
225                   version, static_cast<keymaster_security_level_t>(securityLevel));
226               context->SetSystemVersion(GetOsVersion(), GetOsPatchlevel());
227               return context;
228           }(),
229           kOperationTableSize)),
230       securityLevel_(securityLevel) {}
231 
~AndroidKeymaster4Device()232 AndroidKeymaster4Device::~AndroidKeymaster4Device() {}
233 
getHardwareInfo(getHardwareInfo_cb _hidl_cb)234 Return<void> AndroidKeymaster4Device::getHardwareInfo(getHardwareInfo_cb _hidl_cb) {
235     _hidl_cb(securityLevel_, "SoftwareKeymasterDevice", "Google");
236     return Void();
237 }
238 
239 Return<void>
getHmacSharingParameters(getHmacSharingParameters_cb _hidl_cb)240 AndroidKeymaster4Device::getHmacSharingParameters(getHmacSharingParameters_cb _hidl_cb) {
241     auto response = impl_->GetHmacSharingParameters();
242 
243     ::android::hardware::keymaster::V4_0::HmacSharingParameters params;
244     params.seed.setToExternal(const_cast<uint8_t*>(response.params.seed.data),
245                               response.params.seed.data_length);
246     static_assert(sizeof(response.params.nonce) == params.nonce.size(), "Nonce sizes don't match");
247     memcpy(params.nonce.data(), response.params.nonce, params.nonce.size());
248     _hidl_cb(legacy_enum_conversion(response.error), params);
249     return Void();
250 }
251 
computeSharedHmac(const hidl_vec<::android::hardware::keymaster::V4_0::HmacSharingParameters> & params,computeSharedHmac_cb _hidl_cb)252 Return<void> AndroidKeymaster4Device::computeSharedHmac(
253     const hidl_vec<::android::hardware::keymaster::V4_0::HmacSharingParameters>& params,
254     computeSharedHmac_cb _hidl_cb) {
255     ComputeSharedHmacRequest request(impl_->message_version());
256     request.params_array.params_array = new keymaster::HmacSharingParameters[params.size()];
257     request.params_array.num_params = params.size();
258     for (size_t i = 0; i < params.size(); ++i) {
259         request.params_array.params_array[i].seed = {params[i].seed.data(), params[i].seed.size()};
260         static_assert(sizeof(request.params_array.params_array[i].nonce) ==
261                           decltype(params[i].nonce)::size(),
262                       "Nonce sizes don't match");
263         memcpy(request.params_array.params_array[i].nonce, params[i].nonce.data(),
264                params[i].nonce.size());
265     }
266 
267     auto response = impl_->ComputeSharedHmac(request);
268     hidl_vec<uint8_t> sharing_check;
269     if (response.error == KM_ERROR_OK) sharing_check = kmBlob2hidlVec(response.sharing_check);
270 
271     _hidl_cb(legacy_enum_conversion(response.error), sharing_check);
272     return Void();
273 }
274 
verifyAuthorization(uint64_t challenge,const hidl_vec<KeyParameter> & parametersToVerify,const::android::hardware::keymaster::V4_0::HardwareAuthToken & authToken,verifyAuthorization_cb _hidl_cb)275 Return<void> AndroidKeymaster4Device::verifyAuthorization(
276     uint64_t challenge, const hidl_vec<KeyParameter>& parametersToVerify,
277     const ::android::hardware::keymaster::V4_0::HardwareAuthToken& authToken,
278     verifyAuthorization_cb _hidl_cb) {
279 
280     VerifyAuthorizationRequest request(impl_->message_version());
281     request.challenge = challenge;
282     request.parameters_to_verify.Reinitialize(KmParamSet(parametersToVerify));
283     request.auth_token.challenge = authToken.challenge;
284     request.auth_token.user_id = authToken.userId;
285     request.auth_token.authenticator_id = authToken.authenticatorId;
286     request.auth_token.authenticator_type = legacy_enum_conversion(authToken.authenticatorType);
287     request.auth_token.timestamp = authToken.timestamp;
288     KeymasterBlob mac(authToken.mac.data(), authToken.mac.size());
289     request.auth_token.mac = mac;
290 
291     auto response = impl_->VerifyAuthorization(request);
292 
293     ::android::hardware::keymaster::V4_0::VerificationToken token;
294     token.challenge = response.token.challenge;
295     token.timestamp = response.token.timestamp;
296     token.parametersVerified = kmParamSet2Hidl(response.token.parameters_verified);
297     token.securityLevel = legacy_enum_conversion(response.token.security_level);
298     token.mac = kmBlob2hidlVec(response.token.mac);
299 
300     _hidl_cb(legacy_enum_conversion(response.error), token);
301 
302     return Void();
303 }
304 
addRngEntropy(const hidl_vec<uint8_t> & data)305 Return<ErrorCode> AndroidKeymaster4Device::addRngEntropy(const hidl_vec<uint8_t>& data) {
306     if (data.size() == 0) return ErrorCode::OK;
307     AddEntropyRequest request(impl_->message_version());
308     request.random_data.Reinitialize(data.data(), data.size());
309 
310     AddEntropyResponse response(impl_->message_version());
311     impl_->AddRngEntropy(request, &response);
312 
313     return legacy_enum_conversion(response.error);
314 }
315 
generateKey(const hidl_vec<KeyParameter> & keyParams,generateKey_cb _hidl_cb)316 Return<void> AndroidKeymaster4Device::generateKey(const hidl_vec<KeyParameter>& keyParams,
317                                                   generateKey_cb _hidl_cb) {
318     GenerateKeyRequest request(impl_->message_version());
319     request.key_description.Reinitialize(KmParamSet(keyParams));
320 
321     GenerateKeyResponse response(impl_->message_version());
322     impl_->GenerateKey(request, &response);
323 
324     KeyCharacteristics resultCharacteristics;
325     hidl_vec<uint8_t> resultKeyBlob;
326     if (response.error == KM_ERROR_OK) {
327         resultKeyBlob = kmBlob2hidlVec(response.key_blob);
328         resultCharacteristics.hardwareEnforced = kmParamSet2Hidl(response.enforced);
329         resultCharacteristics.softwareEnforced = kmParamSet2Hidl(response.unenforced);
330     }
331     _hidl_cb(legacy_enum_conversion(response.error), resultKeyBlob, resultCharacteristics);
332     return Void();
333 }
334 
getKeyCharacteristics(const hidl_vec<uint8_t> & keyBlob,const hidl_vec<uint8_t> & clientId,const hidl_vec<uint8_t> & appData,getKeyCharacteristics_cb _hidl_cb)335 Return<void> AndroidKeymaster4Device::getKeyCharacteristics(const hidl_vec<uint8_t>& keyBlob,
336                                                             const hidl_vec<uint8_t>& clientId,
337                                                             const hidl_vec<uint8_t>& appData,
338                                                             getKeyCharacteristics_cb _hidl_cb) {
339     GetKeyCharacteristicsRequest request(impl_->message_version());
340     request.SetKeyMaterial(keyBlob.data(), keyBlob.size());
341     addClientAndAppData(clientId, appData, &request.additional_params);
342 
343     GetKeyCharacteristicsResponse response(impl_->message_version());
344     impl_->GetKeyCharacteristics(request, &response);
345 
346     KeyCharacteristics resultCharacteristics;
347     if (response.error == KM_ERROR_OK) {
348         resultCharacteristics.hardwareEnforced = kmParamSet2Hidl(response.enforced);
349         resultCharacteristics.softwareEnforced = kmParamSet2Hidl(response.unenforced);
350     }
351     _hidl_cb(legacy_enum_conversion(response.error), resultCharacteristics);
352     return Void();
353 }
354 
importKey(const hidl_vec<KeyParameter> & params,KeyFormat keyFormat,const hidl_vec<uint8_t> & keyData,importKey_cb _hidl_cb)355 Return<void> AndroidKeymaster4Device::importKey(const hidl_vec<KeyParameter>& params,
356                                                 KeyFormat keyFormat,
357                                                 const hidl_vec<uint8_t>& keyData,
358                                                 importKey_cb _hidl_cb) {
359     ImportKeyRequest request(impl_->message_version());
360     request.key_description.Reinitialize(KmParamSet(params));
361     request.key_format = legacy_enum_conversion(keyFormat);
362     request.key_data = KeymasterKeyBlob(keyData.data(), keyData.size());
363 
364     ImportKeyResponse response(impl_->message_version());
365     impl_->ImportKey(request, &response);
366 
367     KeyCharacteristics resultCharacteristics;
368     hidl_vec<uint8_t> resultKeyBlob;
369     if (response.error == KM_ERROR_OK) {
370         resultKeyBlob = kmBlob2hidlVec(response.key_blob);
371         resultCharacteristics.hardwareEnforced = kmParamSet2Hidl(response.enforced);
372         resultCharacteristics.softwareEnforced = kmParamSet2Hidl(response.unenforced);
373     }
374     _hidl_cb(legacy_enum_conversion(response.error), resultKeyBlob, resultCharacteristics);
375     return Void();
376 }
377 
importWrappedKey(const hidl_vec<uint8_t> & wrappedKeyData,const hidl_vec<uint8_t> & wrappingKeyBlob,const hidl_vec<uint8_t> & maskingKey,const hidl_vec<KeyParameter> & unwrappingParams,uint64_t passwordSid,uint64_t biometricSid,importWrappedKey_cb _hidl_cb)378 Return<void> AndroidKeymaster4Device::importWrappedKey(
379     const hidl_vec<uint8_t>& wrappedKeyData, const hidl_vec<uint8_t>& wrappingKeyBlob,
380     const hidl_vec<uint8_t>& maskingKey, const hidl_vec<KeyParameter>& unwrappingParams,
381     uint64_t passwordSid, uint64_t biometricSid, importWrappedKey_cb _hidl_cb) {
382 
383     ImportWrappedKeyRequest request(impl_->message_version());
384     request.SetWrappedMaterial(wrappedKeyData.data(), wrappedKeyData.size());
385     request.SetWrappingMaterial(wrappingKeyBlob.data(), wrappingKeyBlob.size());
386     request.SetMaskingKeyMaterial(maskingKey.data(), maskingKey.size());
387     request.additional_params.Reinitialize(KmParamSet(unwrappingParams));
388     request.password_sid = passwordSid;
389     request.biometric_sid = biometricSid;
390 
391     ImportWrappedKeyResponse response(impl_->message_version());
392     impl_->ImportWrappedKey(request, &response);
393 
394     KeyCharacteristics resultCharacteristics;
395     hidl_vec<uint8_t> resultKeyBlob;
396     if (response.error == KM_ERROR_OK) {
397         resultKeyBlob = kmBlob2hidlVec(response.key_blob);
398         resultCharacteristics.hardwareEnforced = kmParamSet2Hidl(response.enforced);
399         resultCharacteristics.softwareEnforced = kmParamSet2Hidl(response.unenforced);
400     }
401     _hidl_cb(legacy_enum_conversion(response.error), resultKeyBlob, resultCharacteristics);
402     return Void();
403 }
404 
exportKey(KeyFormat exportFormat,const hidl_vec<uint8_t> & keyBlob,const hidl_vec<uint8_t> & clientId,const hidl_vec<uint8_t> & appData,exportKey_cb _hidl_cb)405 Return<void> AndroidKeymaster4Device::exportKey(KeyFormat exportFormat,
406                                                 const hidl_vec<uint8_t>& keyBlob,
407                                                 const hidl_vec<uint8_t>& clientId,
408                                                 const hidl_vec<uint8_t>& appData,
409                                                 exportKey_cb _hidl_cb) {
410     ExportKeyRequest request(impl_->message_version());
411     request.key_format = legacy_enum_conversion(exportFormat);
412     request.SetKeyMaterial(keyBlob.data(), keyBlob.size());
413     addClientAndAppData(clientId, appData, &request.additional_params);
414 
415     ExportKeyResponse response(impl_->message_version());
416     impl_->ExportKey(request, &response);
417 
418     hidl_vec<uint8_t> resultKeyBlob;
419     if (response.error == KM_ERROR_OK) {
420         resultKeyBlob.setToExternal(response.key_data, response.key_data_length);
421     }
422     _hidl_cb(legacy_enum_conversion(response.error), resultKeyBlob);
423     return Void();
424 }
425 
attestKey(const hidl_vec<uint8_t> & keyToAttest,const hidl_vec<KeyParameter> & attestParams,attestKey_cb _hidl_cb)426 Return<void> AndroidKeymaster4Device::attestKey(const hidl_vec<uint8_t>& keyToAttest,
427                                                 const hidl_vec<KeyParameter>& attestParams,
428                                                 attestKey_cb _hidl_cb) {
429     AttestKeyRequest request(impl_->message_version());
430     request.SetKeyMaterial(keyToAttest.data(), keyToAttest.size());
431     request.attest_params.Reinitialize(KmParamSet(attestParams));
432 
433     AttestKeyResponse response(impl_->message_version());
434     impl_->AttestKey(request, &response);
435 
436     hidl_vec<hidl_vec<uint8_t>> resultCertChain;
437     if (response.error == KM_ERROR_OK) {
438         resultCertChain = kmCertChain2Hidl(response.certificate_chain);
439     }
440     _hidl_cb(legacy_enum_conversion(response.error), resultCertChain);
441     return Void();
442 }
443 
upgradeKey(const hidl_vec<uint8_t> & keyBlobToUpgrade,const hidl_vec<KeyParameter> & upgradeParams,upgradeKey_cb _hidl_cb)444 Return<void> AndroidKeymaster4Device::upgradeKey(const hidl_vec<uint8_t>& keyBlobToUpgrade,
445                                                  const hidl_vec<KeyParameter>& upgradeParams,
446                                                  upgradeKey_cb _hidl_cb) {
447     // There's nothing to be done to upgrade software key blobs.  Further, the software
448     // implementation never returns ErrorCode::KEY_REQUIRES_UPGRADE, so this should never be called.
449     UpgradeKeyRequest request(impl_->message_version());
450     request.SetKeyMaterial(keyBlobToUpgrade.data(), keyBlobToUpgrade.size());
451     request.upgrade_params.Reinitialize(KmParamSet(upgradeParams));
452 
453     UpgradeKeyResponse response(impl_->message_version());
454     impl_->UpgradeKey(request, &response);
455 
456     if (response.error == KM_ERROR_OK) {
457         _hidl_cb(ErrorCode::OK, kmBlob2hidlVec(response.upgraded_key));
458     } else {
459         _hidl_cb(legacy_enum_conversion(response.error), hidl_vec<uint8_t>());
460     }
461     return Void();
462 }
463 
deleteKey(const hidl_vec<uint8_t> & keyBlob)464 Return<ErrorCode> AndroidKeymaster4Device::deleteKey(const hidl_vec<uint8_t>& keyBlob) {
465     // There's nothing to be done to delete software key blobs.
466     DeleteKeyRequest request(impl_->message_version());
467     request.SetKeyMaterial(keyBlob.data(), keyBlob.size());
468 
469     DeleteKeyResponse response(impl_->message_version());
470     impl_->DeleteKey(request, &response);
471 
472     return legacy_enum_conversion(response.error);
473 }
474 
deleteAllKeys()475 Return<ErrorCode> AndroidKeymaster4Device::deleteAllKeys() {
476     // There's nothing to be done to delete software key blobs.
477     DeleteAllKeysRequest request(impl_->message_version());
478     DeleteAllKeysResponse response(impl_->message_version());
479     impl_->DeleteAllKeys(request, &response);
480 
481     return legacy_enum_conversion(response.error);
482 }
483 
destroyAttestationIds()484 Return<ErrorCode> AndroidKeymaster4Device::destroyAttestationIds() {
485     return ErrorCode::UNIMPLEMENTED;
486 }
487 
begin(KeyPurpose purpose,const hidl_vec<uint8_t> & key,const hidl_vec<KeyParameter> & inParams,const HardwareAuthToken & authToken,begin_cb _hidl_cb)488 Return<void> AndroidKeymaster4Device::begin(KeyPurpose purpose, const hidl_vec<uint8_t>& key,
489                                             const hidl_vec<KeyParameter>& inParams,
490                                             const HardwareAuthToken& authToken, begin_cb _hidl_cb) {
491 
492     BeginOperationRequest request(impl_->message_version());
493     request.purpose = legacy_enum_conversion(purpose);
494     request.SetKeyMaterial(key.data(), key.size());
495     request.additional_params.Reinitialize(KmParamSet(inParams));
496 
497     hidl_vec<uint8_t> hidl_vec_token = authToken2HidlVec(authToken);
498     request.additional_params.push_back(
499         TAG_AUTH_TOKEN, reinterpret_cast<uint8_t*>(hidl_vec_token.data()), hidl_vec_token.size());
500 
501     BeginOperationResponse response(impl_->message_version());
502     impl_->BeginOperation(request, &response);
503 
504     hidl_vec<KeyParameter> resultParams;
505     if (response.error == KM_ERROR_OK) resultParams = kmParamSet2Hidl(response.output_params);
506 
507     _hidl_cb(legacy_enum_conversion(response.error), resultParams, response.op_handle);
508     return Void();
509 }
510 
update(uint64_t operationHandle,const hidl_vec<KeyParameter> & inParams,const hidl_vec<uint8_t> & input,const HardwareAuthToken & authToken,const VerificationToken &,update_cb _hidl_cb)511 Return<void> AndroidKeymaster4Device::update(uint64_t operationHandle,
512                                              const hidl_vec<KeyParameter>& inParams,
513                                              const hidl_vec<uint8_t>& input,
514                                              const HardwareAuthToken&  authToken ,
515                                              const VerificationToken& /* verificationToken */,
516                                              update_cb _hidl_cb) {
517     UpdateOperationRequest request(impl_->message_version());
518     request.op_handle = operationHandle;
519     request.input.Reinitialize(input.data(), input.size());
520     request.additional_params.Reinitialize(KmParamSet(inParams));
521 
522     hidl_vec<uint8_t> hidl_vec_token = authToken2HidlVec(authToken);
523     request.additional_params.push_back(
524         TAG_AUTH_TOKEN, reinterpret_cast<uint8_t*>(hidl_vec_token.data()), hidl_vec_token.size());
525 
526     UpdateOperationResponse response(impl_->message_version());
527     impl_->UpdateOperation(request, &response);
528 
529     uint32_t resultConsumed = 0;
530     hidl_vec<KeyParameter> resultParams;
531     hidl_vec<uint8_t> resultBlob;
532     if (response.error == KM_ERROR_OK) {
533         resultConsumed = response.input_consumed;
534         resultParams = kmParamSet2Hidl(response.output_params);
535         resultBlob = kmBuffer2hidlVec(response.output);
536     }
537     _hidl_cb(legacy_enum_conversion(response.error), resultConsumed, resultParams, resultBlob);
538     return Void();
539 }
540 
finish(uint64_t operationHandle,const hidl_vec<KeyParameter> & inParams,const hidl_vec<uint8_t> & input,const hidl_vec<uint8_t> & signature,const HardwareAuthToken & authToken,const VerificationToken &,finish_cb _hidl_cb)541 Return<void> AndroidKeymaster4Device::finish(uint64_t operationHandle,
542                                              const hidl_vec<KeyParameter>& inParams,
543                                              const hidl_vec<uint8_t>& input,
544                                              const hidl_vec<uint8_t>& signature,
545                                              const HardwareAuthToken&  authToken ,
546                                              const VerificationToken& /* verificationToken */,
547                                              finish_cb _hidl_cb) {
548     FinishOperationRequest request(impl_->message_version());
549     request.op_handle = operationHandle;
550     request.input.Reinitialize(input.data(), input.size());
551     request.signature.Reinitialize(signature.data(), signature.size());
552     request.additional_params.Reinitialize(KmParamSet(inParams));
553 
554     hidl_vec<uint8_t> hidl_vec_token = authToken2HidlVec(authToken);
555     request.additional_params.push_back(
556         TAG_AUTH_TOKEN, reinterpret_cast<uint8_t*>(hidl_vec_token.data()), hidl_vec_token.size());
557 
558 
559     FinishOperationResponse response(impl_->message_version());
560     impl_->FinishOperation(request, &response);
561 
562     hidl_vec<KeyParameter> resultParams;
563     hidl_vec<uint8_t> resultBlob;
564     if (response.error == KM_ERROR_OK) {
565         resultParams = kmParamSet2Hidl(response.output_params);
566         resultBlob = kmBuffer2hidlVec(response.output);
567     }
568     _hidl_cb(legacy_enum_conversion(response.error), resultParams, resultBlob);
569     return Void();
570 }
571 
abort(uint64_t operationHandle)572 Return<ErrorCode> AndroidKeymaster4Device::abort(uint64_t operationHandle) {
573     AbortOperationRequest request(impl_->message_version());
574     request.op_handle = operationHandle;
575 
576     AbortOperationResponse response(impl_->message_version());
577     impl_->AbortOperation(request, &response);
578 
579     return legacy_enum_conversion(response.error);
580 }
581 
CreateKeymasterDevice(SecurityLevel securityLevel)582 IKeymasterDevice* CreateKeymasterDevice(SecurityLevel securityLevel) {
583     return new AndroidKeymaster4Device(securityLevel);
584 }
585 
586 }  // namespace ng
587 }  // namespace V4_0
588 }  // namespace keymaster
589