• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  **
3  ** Copyright 2016, 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@3.0-impl"
19 
20 #include "include/AndroidKeymaster3Device.h"
21 
22 #include <android/log.h>
23 
24 #include "keymaster_tags.h"
25 
26 #include <keymaster/android_keymaster.h>
27 #include <keymaster/android_keymaster_messages.h>
28 #include <keymaster/contexts/keymaster1_passthrough_context.h>
29 #include <keymaster/contexts/keymaster2_passthrough_context.h>
30 #include <keymaster/contexts/pure_soft_keymaster_context.h>
31 #include <keymaster/contexts/soft_keymaster_context.h>
32 #include <keymaster/keymaster_configuration.h>
33 #include <keymaster/keymaster_enforcement.h>
34 #include <keymaster/km_openssl/soft_keymaster_enforcement.h>
35 
36 using ::keymaster::AbortOperationRequest;
37 using ::keymaster::AbortOperationResponse;
38 using ::keymaster::AddEntropyRequest;
39 using ::keymaster::AddEntropyResponse;
40 using ::keymaster::AttestKeyRequest;
41 using ::keymaster::AttestKeyResponse;
42 using ::keymaster::AuthorizationSet;
43 using ::keymaster::BeginOperationRequest;
44 using ::keymaster::BeginOperationResponse;
45 using ::keymaster::ExportKeyRequest;
46 using ::keymaster::ExportKeyResponse;
47 using ::keymaster::FinishOperationRequest;
48 using ::keymaster::FinishOperationResponse;
49 using ::keymaster::GenerateKeyRequest;
50 using ::keymaster::GenerateKeyResponse;
51 using ::keymaster::GetKeyCharacteristicsRequest;
52 using ::keymaster::GetKeyCharacteristicsResponse;
53 using ::keymaster::ImportKeyRequest;
54 using ::keymaster::ImportKeyResponse;
55 using ::keymaster::UpdateOperationRequest;
56 using ::keymaster::UpdateOperationResponse;
57 
58 namespace keymaster {
59 namespace ng {
60 
61 namespace {
62 
63 constexpr size_t kOperationTableSize = 16;
64 
legacy_enum_conversion(const Tag value)65 inline keymaster_tag_t legacy_enum_conversion(const Tag value) {
66     return keymaster_tag_t(value);
67 }
legacy_enum_conversion(const keymaster_tag_t value)68 inline Tag legacy_enum_conversion(const keymaster_tag_t value) {
69     return Tag(value);
70 }
legacy_enum_conversion(const KeyPurpose value)71 inline keymaster_purpose_t legacy_enum_conversion(const KeyPurpose value) {
72     return keymaster_purpose_t(value);
73 }
legacy_enum_conversion(const KeyFormat value)74 inline keymaster_key_format_t legacy_enum_conversion(const KeyFormat value) {
75     return keymaster_key_format_t(value);
76 }
legacy_enum_conversion(const keymaster_error_t value)77 inline ErrorCode legacy_enum_conversion(const keymaster_error_t value) {
78     return ErrorCode(value);
79 }
80 
typeFromTag(const keymaster_tag_t tag)81 inline keymaster_tag_type_t typeFromTag(const keymaster_tag_t tag) {
82     return keymaster_tag_get_type(tag);
83 }
84 
85 class KmParamSet : public keymaster_key_param_set_t {
86   public:
KmParamSet(const hidl_vec<KeyParameter> & keyParams)87     explicit KmParamSet(const hidl_vec<KeyParameter>& keyParams) {
88         params = new (std::nothrow) keymaster_key_param_t[keyParams.size()];
89         length = keyParams.size();
90         for (size_t i = 0; i < keyParams.size(); ++i) {
91             auto tag = legacy_enum_conversion(keyParams[i].tag);
92             switch (typeFromTag(tag)) {
93             case KM_ENUM:
94             case KM_ENUM_REP:
95                 params[i] = keymaster_param_enum(tag, keyParams[i].f.integer);
96                 break;
97             case KM_UINT:
98             case KM_UINT_REP:
99                 params[i] = keymaster_param_int(tag, keyParams[i].f.integer);
100                 break;
101             case KM_ULONG:
102             case KM_ULONG_REP:
103                 params[i] = keymaster_param_long(tag, keyParams[i].f.longInteger);
104                 break;
105             case KM_DATE:
106                 params[i] = keymaster_param_date(tag, keyParams[i].f.dateTime);
107                 break;
108             case KM_BOOL:
109                 if (keyParams[i].f.boolValue)
110                     params[i] = keymaster_param_bool(tag);
111                 else
112                     params[i].tag = KM_TAG_INVALID;
113                 break;
114             case KM_BIGNUM:
115             case KM_BYTES:
116                 params[i] =
117                     keymaster_param_blob(tag, &keyParams[i].blob[0], keyParams[i].blob.size());
118                 break;
119             case KM_INVALID:
120             default:
121                 params[i].tag = KM_TAG_INVALID;
122                 /* just skip */
123                 break;
124             }
125         }
126     }
KmParamSet(KmParamSet && other)127     KmParamSet(KmParamSet&& other) : keymaster_key_param_set_t{other.params, other.length} {
128         other.length = 0;
129         other.params = nullptr;
130     }
131     KmParamSet(const KmParamSet&) = delete;
~KmParamSet()132     ~KmParamSet() { delete[] params; }
133 };
134 
kmBlob2hidlVec(const keymaster_key_blob_t & blob)135 inline hidl_vec<uint8_t> kmBlob2hidlVec(const keymaster_key_blob_t& blob) {
136     hidl_vec<uint8_t> result;
137     result.setToExternal(const_cast<unsigned char*>(blob.key_material), blob.key_material_size);
138     return result;
139 }
140 
kmBlob2hidlVec(const keymaster_blob_t & blob)141 inline hidl_vec<uint8_t> kmBlob2hidlVec(const keymaster_blob_t& blob) {
142     hidl_vec<uint8_t> result;
143     result.setToExternal(const_cast<unsigned char*>(blob.data), blob.data_length);
144     return result;
145 }
146 
kmBuffer2hidlVec(const::keymaster::Buffer & buf)147 inline hidl_vec<uint8_t> kmBuffer2hidlVec(const ::keymaster::Buffer& buf) {
148     hidl_vec<uint8_t> result;
149     result.setToExternal(const_cast<unsigned char*>(buf.peek_read()), buf.available_read());
150     return result;
151 }
152 
153 inline static hidl_vec<hidl_vec<uint8_t>>
kmCertChain2Hidl(const keymaster_cert_chain_t & cert_chain)154 kmCertChain2Hidl(const keymaster_cert_chain_t& cert_chain) {
155     hidl_vec<hidl_vec<uint8_t>> result;
156     if (!cert_chain.entry_count || !cert_chain.entries) return result;
157 
158     result.resize(cert_chain.entry_count);
159     for (size_t i = 0; i < cert_chain.entry_count; ++i) {
160         result[i] = kmBlob2hidlVec(cert_chain.entries[i]);
161     }
162 
163     return result;
164 }
165 
kmParamSet2Hidl(const keymaster_key_param_set_t & set)166 static inline hidl_vec<KeyParameter> kmParamSet2Hidl(const keymaster_key_param_set_t& set) {
167     hidl_vec<KeyParameter> result;
168     if (set.length == 0 || set.params == nullptr) return result;
169 
170     result.resize(set.length);
171     keymaster_key_param_t* params = set.params;
172     for (size_t i = 0; i < set.length; ++i) {
173         auto tag = params[i].tag;
174         result[i].tag = legacy_enum_conversion(tag);
175         switch (typeFromTag(tag)) {
176         case KM_ENUM:
177         case KM_ENUM_REP:
178             result[i].f.integer = params[i].enumerated;
179             break;
180         case KM_UINT:
181         case KM_UINT_REP:
182             result[i].f.integer = params[i].integer;
183             break;
184         case KM_ULONG:
185         case KM_ULONG_REP:
186             result[i].f.longInteger = params[i].long_integer;
187             break;
188         case KM_DATE:
189             result[i].f.dateTime = params[i].date_time;
190             break;
191         case KM_BOOL:
192             result[i].f.boolValue = params[i].boolean;
193             break;
194         case KM_BIGNUM:
195         case KM_BYTES:
196             result[i].blob.setToExternal(const_cast<unsigned char*>(params[i].blob.data),
197                                          params[i].blob.data_length);
198             break;
199         case KM_INVALID:
200         default:
201             params[i].tag = KM_TAG_INVALID;
202             /* just skip */
203             break;
204         }
205     }
206     return result;
207 }
208 
addClientAndAppData(const hidl_vec<uint8_t> & clientId,const hidl_vec<uint8_t> & appData,::keymaster::AuthorizationSet * params)209 void addClientAndAppData(const hidl_vec<uint8_t>& clientId, const hidl_vec<uint8_t>& appData,
210                          ::keymaster::AuthorizationSet* params) {
211     params->Clear();
212     if (clientId.size()) {
213         params->push_back(::keymaster::TAG_APPLICATION_ID, clientId.data(), clientId.size());
214     }
215     if (appData.size()) {
216         params->push_back(::keymaster::TAG_APPLICATION_DATA, appData.data(), appData.size());
217     }
218 }
219 
220 }  // anonymous namespace
221 
AndroidKeymaster3Device()222 AndroidKeymaster3Device::AndroidKeymaster3Device()
223     : impl_(new (std::nothrow)::keymaster::AndroidKeymaster(
224           []() -> auto{
225               auto context = new (std::nothrow) PureSoftKeymasterContext(KmVersion::KEYMASTER_3);
226               context->SetSystemVersion(GetOsVersion(), GetOsPatchlevel());
227               context->SetVendorPatchlevel(GetVendorPatchlevel());
228               // Software devices cannot be configured by the boot loader but they have
229               // to return a boot patch level. So lets just return the OS patch level.
230               // The OS patch level only has a year and a month so we just add the 1st
231               // of the month as day field.
232               context->SetBootPatchlevel(GetOsPatchlevel() * 100 + 1);
233               return context;
234           }(),
235           kOperationTableSize)),
236       profile_(KeymasterHardwareProfile::SW) {}
237 
AndroidKeymaster3Device(KeymasterContext * context,KeymasterHardwareProfile profile)238 AndroidKeymaster3Device::AndroidKeymaster3Device(KeymasterContext* context,
239                                                  KeymasterHardwareProfile profile)
240     : impl_(new (std::nothrow)::keymaster::AndroidKeymaster(context, kOperationTableSize)),
241       profile_(profile) {}
242 
~AndroidKeymaster3Device()243 AndroidKeymaster3Device::~AndroidKeymaster3Device() {}
244 
245 // Methods from ::android::hardware::keymaster::V3_0::IKeymasterDevice follow.
getHardwareFeatures(getHardwareFeatures_cb _hidl_cb)246 Return<void> AndroidKeymaster3Device::getHardwareFeatures(getHardwareFeatures_cb _hidl_cb) {
247     switch (profile_) {
248     case KeymasterHardwareProfile::KM0:
249         _hidl_cb(true /* is_secure */, false /* supports_ec */,
250                  false /* supports_symmetric_cryptography */, false /* supports_attestation */,
251                  false /* supportsAllDigests */, "SoftwareWrappedKeymaster0Device", "Google");
252         break;
253     case KeymasterHardwareProfile::KM1:
254         _hidl_cb(true /* is_secure */, true /* supports_ec */,
255                  true /* supports_symmetric_cryptography */, false /* supports_attestation */,
256                  false /* supportsAllDigests */, "SoftwareWrappedKeymaster1Device", "Google");
257         break;
258     case KeymasterHardwareProfile::KM2:
259         _hidl_cb(true /* is_secure */, true /* supports_ec */,
260                  true /* supports_symmetric_cryptography */, true /* supports_attestation */,
261                  true /* supportsAllDigests */, "SoftwareWrappedKeymaster2Device", "Google");
262         break;
263     case KeymasterHardwareProfile::SW:
264     default:
265         _hidl_cb(false /* is_secure */, false /* supports_ec */,
266                  false /* supports_symmetric_cryptography */, false /* supports_attestation */,
267                  false /* supportsAllDigests */, "SoftwareKeymasterDevice", "Google");
268         break;
269     }
270     return Void();
271 }
272 
addRngEntropy(const hidl_vec<uint8_t> & data)273 Return<ErrorCode> AndroidKeymaster3Device::addRngEntropy(const hidl_vec<uint8_t>& data) {
274     if (data.size() == 0) return ErrorCode::OK;
275     AddEntropyRequest request(impl_->message_version());
276     request.random_data.Reinitialize(data.data(), data.size());
277 
278     AddEntropyResponse response(impl_->message_version());
279     impl_->AddRngEntropy(request, &response);
280 
281     return legacy_enum_conversion(response.error);
282 }
283 
generateKey(const hidl_vec<KeyParameter> & keyParams,generateKey_cb _hidl_cb)284 Return<void> AndroidKeymaster3Device::generateKey(const hidl_vec<KeyParameter>& keyParams,
285                                                   generateKey_cb _hidl_cb) {
286     GenerateKeyRequest request(impl_->message_version());
287     request.key_description.Reinitialize(KmParamSet(keyParams));
288 
289     GenerateKeyResponse response(impl_->message_version());
290     impl_->GenerateKey(request, &response);
291 
292     KeyCharacteristics resultCharacteristics;
293     hidl_vec<uint8_t> resultKeyBlob;
294     if (response.error == KM_ERROR_OK) {
295         resultKeyBlob = kmBlob2hidlVec(response.key_blob);
296         resultCharacteristics.teeEnforced = kmParamSet2Hidl(response.enforced);
297         resultCharacteristics.softwareEnforced = kmParamSet2Hidl(response.unenforced);
298     }
299     _hidl_cb(legacy_enum_conversion(response.error), resultKeyBlob, resultCharacteristics);
300     return Void();
301 }
302 
getKeyCharacteristics(const hidl_vec<uint8_t> & keyBlob,const hidl_vec<uint8_t> & clientId,const hidl_vec<uint8_t> & appData,getKeyCharacteristics_cb _hidl_cb)303 Return<void> AndroidKeymaster3Device::getKeyCharacteristics(const hidl_vec<uint8_t>& keyBlob,
304                                                             const hidl_vec<uint8_t>& clientId,
305                                                             const hidl_vec<uint8_t>& appData,
306                                                             getKeyCharacteristics_cb _hidl_cb) {
307     GetKeyCharacteristicsRequest request(impl_->message_version());
308     request.SetKeyMaterial(keyBlob.data(), keyBlob.size());
309     addClientAndAppData(clientId, appData, &request.additional_params);
310 
311     GetKeyCharacteristicsResponse response(impl_->message_version());
312     impl_->GetKeyCharacteristics(request, &response);
313 
314     KeyCharacteristics resultCharacteristics;
315     if (response.error == KM_ERROR_OK) {
316         resultCharacteristics.teeEnforced = kmParamSet2Hidl(response.enforced);
317         resultCharacteristics.softwareEnforced = kmParamSet2Hidl(response.unenforced);
318     }
319     _hidl_cb(legacy_enum_conversion(response.error), resultCharacteristics);
320     return Void();
321 }
322 
importKey(const hidl_vec<KeyParameter> & params,KeyFormat keyFormat,const hidl_vec<uint8_t> & keyData,importKey_cb _hidl_cb)323 Return<void> AndroidKeymaster3Device::importKey(const hidl_vec<KeyParameter>& params,
324                                                 KeyFormat keyFormat,
325                                                 const hidl_vec<uint8_t>& keyData,
326                                                 importKey_cb _hidl_cb) {
327     ImportKeyRequest request(impl_->message_version());
328     request.key_description.Reinitialize(KmParamSet(params));
329     request.key_format = legacy_enum_conversion(keyFormat);
330     request.key_data = KeymasterKeyBlob(keyData.data(), keyData.size());
331 
332     ImportKeyResponse response(impl_->message_version());
333     impl_->ImportKey(request, &response);
334 
335     KeyCharacteristics resultCharacteristics;
336     hidl_vec<uint8_t> resultKeyBlob;
337     if (response.error == KM_ERROR_OK) {
338         resultKeyBlob = kmBlob2hidlVec(response.key_blob);
339         resultCharacteristics.teeEnforced = kmParamSet2Hidl(response.enforced);
340         resultCharacteristics.softwareEnforced = kmParamSet2Hidl(response.unenforced);
341     }
342     _hidl_cb(legacy_enum_conversion(response.error), resultKeyBlob, resultCharacteristics);
343     return Void();
344 }
345 
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)346 Return<void> AndroidKeymaster3Device::exportKey(KeyFormat exportFormat,
347                                                 const hidl_vec<uint8_t>& keyBlob,
348                                                 const hidl_vec<uint8_t>& clientId,
349                                                 const hidl_vec<uint8_t>& appData,
350                                                 exportKey_cb _hidl_cb) {
351     ExportKeyRequest request(impl_->message_version());
352     request.key_format = legacy_enum_conversion(exportFormat);
353     request.SetKeyMaterial(keyBlob.data(), keyBlob.size());
354     addClientAndAppData(clientId, appData, &request.additional_params);
355 
356     ExportKeyResponse response(impl_->message_version());
357     impl_->ExportKey(request, &response);
358 
359     hidl_vec<uint8_t> resultKeyBlob;
360     if (response.error == KM_ERROR_OK) {
361         resultKeyBlob.setToExternal(response.key_data, response.key_data_length);
362     }
363     _hidl_cb(legacy_enum_conversion(response.error), resultKeyBlob);
364     return Void();
365 }
366 
attestKey(const hidl_vec<uint8_t> & keyToAttest,const hidl_vec<KeyParameter> & attestParams,attestKey_cb _hidl_cb)367 Return<void> AndroidKeymaster3Device::attestKey(const hidl_vec<uint8_t>& keyToAttest,
368                                                 const hidl_vec<KeyParameter>& attestParams,
369                                                 attestKey_cb _hidl_cb) {
370     AttestKeyRequest request(impl_->message_version());
371     request.SetKeyMaterial(keyToAttest.data(), keyToAttest.size());
372     request.attest_params.Reinitialize(KmParamSet(attestParams));
373 
374     AttestKeyResponse response(impl_->message_version());
375     impl_->AttestKey(request, &response);
376 
377     hidl_vec<hidl_vec<uint8_t>> resultCertChain;
378     if (response.error == KM_ERROR_OK) {
379         resultCertChain = kmCertChain2Hidl(response.certificate_chain);
380     }
381     _hidl_cb(legacy_enum_conversion(response.error), resultCertChain);
382     return Void();
383 }
384 
upgradeKey(const hidl_vec<uint8_t> & keyBlobToUpgrade,const hidl_vec<KeyParameter> & upgradeParams,upgradeKey_cb _hidl_cb)385 Return<void> AndroidKeymaster3Device::upgradeKey(const hidl_vec<uint8_t>& keyBlobToUpgrade,
386                                                  const hidl_vec<KeyParameter>& upgradeParams,
387                                                  upgradeKey_cb _hidl_cb) {
388     // There's nothing to be done to upgrade software key blobs.  Further, the software
389     // implementation never returns ErrorCode::KEY_REQUIRES_UPGRADE, so this should never be called.
390     UpgradeKeyRequest request(impl_->message_version());
391     request.SetKeyMaterial(keyBlobToUpgrade.data(), keyBlobToUpgrade.size());
392     request.upgrade_params.Reinitialize(KmParamSet(upgradeParams));
393 
394     UpgradeKeyResponse response(impl_->message_version());
395     impl_->UpgradeKey(request, &response);
396 
397     if (response.error == KM_ERROR_OK) {
398         _hidl_cb(ErrorCode::OK, kmBlob2hidlVec(response.upgraded_key));
399     } else {
400         _hidl_cb(legacy_enum_conversion(response.error), hidl_vec<uint8_t>());
401     }
402     return Void();
403 }
404 
deleteKey(const hidl_vec<uint8_t> & keyBlob)405 Return<ErrorCode> AndroidKeymaster3Device::deleteKey(const hidl_vec<uint8_t>& keyBlob) {
406     // There's nothing to be done to delete software key blobs.
407     DeleteKeyRequest request(impl_->message_version());
408     request.SetKeyMaterial(keyBlob.data(), keyBlob.size());
409 
410     DeleteKeyResponse response(impl_->message_version());
411     impl_->DeleteKey(request, &response);
412 
413     return legacy_enum_conversion(response.error);
414 }
415 
deleteAllKeys()416 Return<ErrorCode> AndroidKeymaster3Device::deleteAllKeys() {
417     // There's nothing to be done to delete software key blobs.
418     DeleteAllKeysRequest request(impl_->message_version());
419     DeleteAllKeysResponse response(impl_->message_version());
420     impl_->DeleteAllKeys(request, &response);
421 
422     return legacy_enum_conversion(response.error);
423 }
424 
destroyAttestationIds()425 Return<ErrorCode> AndroidKeymaster3Device::destroyAttestationIds() {
426     return ErrorCode::UNIMPLEMENTED;
427 }
428 
begin(KeyPurpose purpose,const hidl_vec<uint8_t> & key,const hidl_vec<KeyParameter> & inParams,begin_cb _hidl_cb)429 Return<void> AndroidKeymaster3Device::begin(KeyPurpose purpose, const hidl_vec<uint8_t>& key,
430                                             const hidl_vec<KeyParameter>& inParams,
431                                             begin_cb _hidl_cb) {
432 
433     BeginOperationRequest request(impl_->message_version());
434     request.purpose = legacy_enum_conversion(purpose);
435     request.SetKeyMaterial(key.data(), key.size());
436     request.additional_params.Reinitialize(KmParamSet(inParams));
437 
438     BeginOperationResponse response(impl_->message_version());
439     impl_->BeginOperation(request, &response);
440 
441     hidl_vec<KeyParameter> resultParams;
442     if (response.error == KM_ERROR_OK) {
443         resultParams = kmParamSet2Hidl(response.output_params);
444     }
445 
446     _hidl_cb(legacy_enum_conversion(response.error), resultParams, response.op_handle);
447     return Void();
448 }
449 
update(uint64_t operationHandle,const hidl_vec<KeyParameter> & inParams,const hidl_vec<uint8_t> & input,update_cb _hidl_cb)450 Return<void> AndroidKeymaster3Device::update(uint64_t operationHandle,
451                                              const hidl_vec<KeyParameter>& inParams,
452                                              const hidl_vec<uint8_t>& input, update_cb _hidl_cb) {
453     UpdateOperationRequest request(impl_->message_version());
454     request.op_handle = operationHandle;
455     request.input.Reinitialize(input.data(), input.size());
456     request.additional_params.Reinitialize(KmParamSet(inParams));
457 
458     UpdateOperationResponse response(impl_->message_version());
459     impl_->UpdateOperation(request, &response);
460 
461     uint32_t resultConsumed = 0;
462     hidl_vec<KeyParameter> resultParams;
463     hidl_vec<uint8_t> resultBlob;
464     if (response.error == KM_ERROR_OK) {
465         resultConsumed = response.input_consumed;
466         resultParams = kmParamSet2Hidl(response.output_params);
467         resultBlob = kmBuffer2hidlVec(response.output);
468     }
469     _hidl_cb(legacy_enum_conversion(response.error), resultConsumed, resultParams, resultBlob);
470     return Void();
471 }
472 
finish(uint64_t operationHandle,const hidl_vec<KeyParameter> & inParams,const hidl_vec<uint8_t> & input,const hidl_vec<uint8_t> & signature,finish_cb _hidl_cb)473 Return<void> AndroidKeymaster3Device::finish(uint64_t operationHandle,
474                                              const hidl_vec<KeyParameter>& inParams,
475                                              const hidl_vec<uint8_t>& input,
476                                              const hidl_vec<uint8_t>& signature,
477                                              finish_cb _hidl_cb) {
478     FinishOperationRequest request(impl_->message_version());
479     request.op_handle = operationHandle;
480     request.input.Reinitialize(input.data(), input.size());
481     request.signature.Reinitialize(signature.data(), signature.size());
482     request.additional_params.Reinitialize(KmParamSet(inParams));
483 
484     FinishOperationResponse response(impl_->message_version());
485     impl_->FinishOperation(request, &response);
486 
487     hidl_vec<KeyParameter> resultParams;
488     hidl_vec<uint8_t> resultBlob;
489     if (response.error == KM_ERROR_OK) {
490         resultParams = kmParamSet2Hidl(response.output_params);
491         resultBlob = kmBuffer2hidlVec(response.output);
492     }
493     _hidl_cb(legacy_enum_conversion(response.error), resultParams, resultBlob);
494     return Void();
495 }
496 
abort(uint64_t operationHandle)497 Return<ErrorCode> AndroidKeymaster3Device::abort(uint64_t operationHandle) {
498     AbortOperationRequest request(impl_->message_version());
499     request.op_handle = operationHandle;
500 
501     AbortOperationResponse response(impl_->message_version());
502     impl_->AbortOperation(request, &response);
503 
504     return legacy_enum_conversion(response.error);
505 }
506 
CreateKeymasterDevice()507 IKeymasterDevice* CreateKeymasterDevice() {
508     return new (std::nothrow) AndroidKeymaster3Device();
509 }
510 
CreateKeymasterDevice(keymaster2_device_t * km2_device)511 IKeymasterDevice* CreateKeymasterDevice(keymaster2_device_t* km2_device) {
512     if (ConfigureDevice(km2_device) != KM_ERROR_OK) return nullptr;
513     auto context =
514         new (std::nothrow) Keymaster2PassthroughContext(KmVersion::KEYMASTER_3, km2_device);
515     context->SetSystemVersion(GetOsVersion(), GetOsPatchlevel());
516     return new (std::nothrow) AndroidKeymaster3Device(context, KeymasterHardwareProfile::KM2);
517 }
518 
CreateKeymasterDevice(keymaster1_device_t * km1_device)519 IKeymasterDevice* CreateKeymasterDevice(keymaster1_device_t* km1_device) {
520     auto context =
521         new (std::nothrow) Keymaster1PassthroughContext(KmVersion::KEYMASTER_3, km1_device);
522     context->SetSystemVersion(GetOsVersion(), GetOsPatchlevel());
523     return new (std::nothrow) AndroidKeymaster3Device(context, KeymasterHardwareProfile::KM1);
524 }
525 
526 }  // namespace ng
527 }  // namespace keymaster
528