blob: f511c3f46483278b1d3a45c575048eaedc832d03 [file] [log] [blame]
Shawn Willden815e8962020-12-11 13:05:27 +00001/*
2 * Copyright 2020, 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#define LOG_TAG "android.hardware.security.keymint-impl"
Shawn Willden763166c2021-01-10 19:45:01 -070018#include <android-base/logging.h>
Shawn Willden815e8962020-12-11 13:05:27 +000019
20#include "AndroidKeyMintDevice.h"
21
22#include <aidl/android/hardware/security/keymint/ErrorCode.h>
23
24#include <keymaster/android_keymaster.h>
25#include <keymaster/contexts/pure_soft_keymaster_context.h>
26#include <keymaster/keymaster_configuration.h>
27
28#include "AndroidKeyMintOperation.h"
29#include "KeyMintUtils.h"
30
31namespace aidl::android::hardware::security::keymint {
32
Shawn Willden9a3792e2021-04-08 09:38:14 -060033using namespace keymaster; // NOLINT(google-build-using-namespace)
34
35using km_utils::authToken2AidlVec;
36using km_utils::kmBlob2vector;
37using km_utils::kmError2ScopedAStatus;
38using km_utils::kmParam2Aidl;
39using km_utils::KmParamSet;
40using km_utils::kmParamSet2Aidl;
41using km_utils::legacy_enum_conversion;
Chirag Pathakb292e9a2021-02-02 07:28:09 +000042using secureclock::TimeStampToken;
Shawn Willden815e8962020-12-11 13:05:27 +000043
Shawn Willden763166c2021-01-10 19:45:01 -070044namespace {
45
46vector<KeyCharacteristics> convertKeyCharacteristics(SecurityLevel keyMintSecurityLevel,
David Drysdaleb90937d2021-03-08 13:52:17 +000047 const AuthorizationSet& requestParams,
Shawn Willden763166c2021-01-10 19:45:01 -070048 const AuthorizationSet& sw_enforced,
David Drysdalea49f3fd2021-05-20 12:04:06 +010049 const AuthorizationSet& hw_enforced,
50 bool include_keystore_enforced = true) {
Shawn Willden09f47102021-01-15 15:21:56 -070051 KeyCharacteristics keyMintEnforced{keyMintSecurityLevel, {}};
Shawn Willden763166c2021-01-10 19:45:01 -070052
Shawn Willden763166c2021-01-10 19:45:01 -070053 if (keyMintSecurityLevel != SecurityLevel::SOFTWARE) {
Qi Wuf7c8e9d2021-02-11 03:14:52 +080054 // We're pretending to be TRUSTED_ENVIRONMENT or STRONGBOX.
Shawn Willden09f47102021-01-15 15:21:56 -070055 keyMintEnforced.authorizations = kmParamSet2Aidl(hw_enforced);
David Drysdalea49f3fd2021-05-20 12:04:06 +010056 if (include_keystore_enforced) {
57 // Put all the software authorizations in the keystore list.
58 KeyCharacteristics keystoreEnforced{SecurityLevel::KEYSTORE,
59 kmParamSet2Aidl(sw_enforced)};
60 return {std::move(keyMintEnforced), std::move(keystoreEnforced)};
61 } else {
62 return {std::move(keyMintEnforced)};
63 }
Shawn Willden763166c2021-01-10 19:45:01 -070064 }
65
Shawn Willden09f47102021-01-15 15:21:56 -070066 KeyCharacteristics keystoreEnforced{SecurityLevel::KEYSTORE, {}};
Shawn Willden763166c2021-01-10 19:45:01 -070067 CHECK(hw_enforced.empty()) << "Hardware-enforced list is non-empty for pure SW KeyMint";
68
Shawn Willden09f47102021-01-15 15:21:56 -070069 // This is a pure software implementation, so all tags are in sw_enforced.
70 // We need to walk through the SW-enforced list and figure out which tags to
71 // return in the software list and which in the keystore list.
Shawn Willden763166c2021-01-10 19:45:01 -070072
73 for (auto& entry : sw_enforced) {
74 switch (entry.tag) {
75 /* Invalid and unused */
76 case KM_TAG_ECIES_SINGLE_HASH_MODE:
77 case KM_TAG_INVALID:
78 case KM_TAG_KDF:
Shawn Willden09f47102021-01-15 15:21:56 -070079 case KM_TAG_ROLLBACK_RESISTANCE:
Shawn Willden763166c2021-01-10 19:45:01 -070080 CHECK(false) << "We shouldn't see tag " << entry.tag;
81 break;
82
83 /* Unimplemented */
84 case KM_TAG_ALLOW_WHILE_ON_BODY:
Shawn Willden763166c2021-01-10 19:45:01 -070085 case KM_TAG_BOOTLOADER_ONLY:
Shawn Willden763166c2021-01-10 19:45:01 -070086 case KM_TAG_ROLLBACK_RESISTANT:
87 case KM_TAG_STORAGE_KEY:
88 break;
89
David Drysdaleb90937d2021-03-08 13:52:17 +000090 /* Keystore-enforced if not locally generated. */
Shawn Willden763166c2021-01-10 19:45:01 -070091 case KM_TAG_CREATION_DATETIME:
David Drysdaleb90937d2021-03-08 13:52:17 +000092 // A KeyMaster implementation is required to add this tag to generated/imported keys.
93 // A KeyMint implementation is not required to create this tag, only to echo it back if
94 // it was included in the key generation/import request.
95 if (requestParams.Contains(KM_TAG_CREATION_DATETIME)) {
96 keystoreEnforced.authorizations.push_back(kmParam2Aidl(entry));
97 }
Shawn Willden763166c2021-01-10 19:45:01 -070098 break;
99
100 /* Disallowed in KeyCharacteristics */
101 case KM_TAG_APPLICATION_DATA:
Shawn Willden09f47102021-01-15 15:21:56 -0700102 case KM_TAG_ATTESTATION_APPLICATION_ID:
Shawn Willden763166c2021-01-10 19:45:01 -0700103 break;
104
105 /* Not key characteristics */
106 case KM_TAG_ASSOCIATED_DATA:
Shawn Willden763166c2021-01-10 19:45:01 -0700107 case KM_TAG_ATTESTATION_CHALLENGE:
108 case KM_TAG_ATTESTATION_ID_BRAND:
109 case KM_TAG_ATTESTATION_ID_DEVICE:
110 case KM_TAG_ATTESTATION_ID_IMEI:
111 case KM_TAG_ATTESTATION_ID_MANUFACTURER:
112 case KM_TAG_ATTESTATION_ID_MEID:
113 case KM_TAG_ATTESTATION_ID_MODEL:
114 case KM_TAG_ATTESTATION_ID_PRODUCT:
115 case KM_TAG_ATTESTATION_ID_SERIAL:
116 case KM_TAG_AUTH_TOKEN:
117 case KM_TAG_CERTIFICATE_SERIAL:
118 case KM_TAG_CERTIFICATE_SUBJECT:
Janis Danisevskisa5780e22021-01-31 22:05:22 -0800119 case KM_TAG_CERTIFICATE_NOT_AFTER:
120 case KM_TAG_CERTIFICATE_NOT_BEFORE:
Shawn Willden763166c2021-01-10 19:45:01 -0700121 case KM_TAG_CONFIRMATION_TOKEN:
122 case KM_TAG_DEVICE_UNIQUE_ATTESTATION:
123 case KM_TAG_IDENTITY_CREDENTIAL_KEY:
124 case KM_TAG_MAC_LENGTH:
125 case KM_TAG_NONCE:
126 case KM_TAG_RESET_SINCE_ID_ROTATION:
127 case KM_TAG_ROOT_OF_TRUST:
128 case KM_TAG_UNIQUE_ID:
129 break;
130
Shawn Willden09f47102021-01-15 15:21:56 -0700131 /* KeyMint-enforced */
Shawn Willden763166c2021-01-10 19:45:01 -0700132 case KM_TAG_ALGORITHM:
Shawn Willden09f47102021-01-15 15:21:56 -0700133 case KM_TAG_APPLICATION_ID:
Shawn Willden763166c2021-01-10 19:45:01 -0700134 case KM_TAG_AUTH_TIMEOUT:
135 case KM_TAG_BLOB_USAGE_REQUIREMENTS:
136 case KM_TAG_BLOCK_MODE:
137 case KM_TAG_BOOT_PATCHLEVEL:
138 case KM_TAG_CALLER_NONCE:
139 case KM_TAG_DIGEST:
David Drysdale82fe2592021-05-27 11:57:03 +0100140 case KM_TAG_EARLY_BOOT_ONLY:
Shawn Willden763166c2021-01-10 19:45:01 -0700141 case KM_TAG_EC_CURVE:
142 case KM_TAG_EXPORTABLE:
143 case KM_TAG_INCLUDE_UNIQUE_ID:
144 case KM_TAG_KEY_SIZE:
145 case KM_TAG_MAX_USES_PER_BOOT:
146 case KM_TAG_MIN_MAC_LENGTH:
147 case KM_TAG_MIN_SECONDS_BETWEEN_OPS:
148 case KM_TAG_NO_AUTH_REQUIRED:
149 case KM_TAG_ORIGIN:
150 case KM_TAG_OS_PATCHLEVEL:
151 case KM_TAG_OS_VERSION:
152 case KM_TAG_PADDING:
153 case KM_TAG_PURPOSE:
154 case KM_TAG_RSA_OAEP_MGF_DIGEST:
155 case KM_TAG_RSA_PUBLIC_EXPONENT:
David Drysdale82fe2592021-05-27 11:57:03 +0100156 case KM_TAG_TRUSTED_CONFIRMATION_REQUIRED:
157 case KM_TAG_TRUSTED_USER_PRESENCE_REQUIRED:
Shawn Willden763166c2021-01-10 19:45:01 -0700158 case KM_TAG_UNLOCKED_DEVICE_REQUIRED:
159 case KM_TAG_USER_AUTH_TYPE:
160 case KM_TAG_USER_SECURE_ID:
161 case KM_TAG_VENDOR_PATCHLEVEL:
Shawn Willden09f47102021-01-15 15:21:56 -0700162 keyMintEnforced.authorizations.push_back(kmParam2Aidl(entry));
163 break;
164
165 /* Keystore-enforced */
166 case KM_TAG_ACTIVE_DATETIME:
167 case KM_TAG_ALL_APPLICATIONS:
168 case KM_TAG_ALL_USERS:
Paul Crowley83846532021-02-10 11:24:50 -0800169 case KM_TAG_MAX_BOOT_LEVEL:
Shawn Willden09f47102021-01-15 15:21:56 -0700170 case KM_TAG_ORIGINATION_EXPIRE_DATETIME:
171 case KM_TAG_USAGE_EXPIRE_DATETIME:
172 case KM_TAG_USER_ID:
173 case KM_TAG_USAGE_COUNT_LIMIT:
174 keystoreEnforced.authorizations.push_back(kmParam2Aidl(entry));
175 break;
Shawn Willden763166c2021-01-10 19:45:01 -0700176 }
177 }
178
Shawn Willden09f47102021-01-15 15:21:56 -0700179 vector<KeyCharacteristics> retval;
180 retval.reserve(2);
181 if (!keyMintEnforced.authorizations.empty()) retval.push_back(std::move(keyMintEnforced));
David Drysdalea49f3fd2021-05-20 12:04:06 +0100182 if (include_keystore_enforced && !keystoreEnforced.authorizations.empty()) {
183 retval.push_back(std::move(keystoreEnforced));
184 }
Shawn Willden09f47102021-01-15 15:21:56 -0700185
186 return retval;
Shawn Willden763166c2021-01-10 19:45:01 -0700187}
188
Shawn Willdend7d4f312020-12-21 08:31:01 -0700189Certificate convertCertificate(const keymaster_blob_t& cert) {
190 return {std::vector<uint8_t>(cert.data, cert.data + cert.data_length)};
191}
192
193vector<Certificate> convertCertificateChain(const CertificateChain& chain) {
194 vector<Certificate> retval;
195 retval.reserve(chain.entry_count);
196 std::transform(chain.begin(), chain.end(), std::back_inserter(retval), convertCertificate);
197 return retval;
198}
199
David Drysdalea49f3fd2021-05-20 12:04:06 +0100200void addClientAndAppData(const std::vector<uint8_t>& appId, const std::vector<uint8_t>& appData,
201 ::keymaster::AuthorizationSet* params) {
202 params->Clear();
203 if (appId.size()) {
204 params->push_back(::keymaster::TAG_APPLICATION_ID, appId.data(), appId.size());
205 }
206 if (appData.size()) {
207 params->push_back(::keymaster::TAG_APPLICATION_DATA, appData.data(), appData.size());
208 }
209}
210
Shawn Willden763166c2021-01-10 19:45:01 -0700211} // namespace
212
Shawn Willden815e8962020-12-11 13:05:27 +0000213constexpr size_t kOperationTableSize = 16;
214
215AndroidKeyMintDevice::AndroidKeyMintDevice(SecurityLevel securityLevel)
216 : impl_(new ::keymaster::AndroidKeymaster(
217 [&]() -> auto {
218 auto context = new PureSoftKeymasterContext(
219 KmVersion::KEYMINT_1, static_cast<keymaster_security_level_t>(securityLevel));
220 context->SetSystemVersion(::keymaster::GetOsVersion(),
221 ::keymaster::GetOsPatchlevel());
222 return context;
223 }(),
224 kOperationTableSize)),
225 securityLevel_(securityLevel) {}
226
227AndroidKeyMintDevice::~AndroidKeyMintDevice() {}
228
229ScopedAStatus AndroidKeyMintDevice::getHardwareInfo(KeyMintHardwareInfo* info) {
230 info->versionNumber = 1;
231 info->securityLevel = securityLevel_;
232 info->keyMintName = "FakeKeyMintDevice";
233 info->keyMintAuthorName = "Google";
Chirag Pathakb292e9a2021-02-02 07:28:09 +0000234 info->timestampTokenRequired = false;
Shawn Willden815e8962020-12-11 13:05:27 +0000235 return ScopedAStatus::ok();
236}
237
Shawn Willden815e8962020-12-11 13:05:27 +0000238ScopedAStatus AndroidKeyMintDevice::addRngEntropy(const vector<uint8_t>& data) {
239 if (data.size() == 0) {
240 return ScopedAStatus::ok();
241 }
242
Shawn Willden950eb0b2021-01-06 19:15:29 +0000243 AddEntropyRequest request(impl_->message_version());
Shawn Willden815e8962020-12-11 13:05:27 +0000244 request.random_data.Reinitialize(data.data(), data.size());
245
Shawn Willden950eb0b2021-01-06 19:15:29 +0000246 AddEntropyResponse response(impl_->message_version());
Shawn Willden815e8962020-12-11 13:05:27 +0000247 impl_->AddRngEntropy(request, &response);
248
249 return kmError2ScopedAStatus(response.error);
250}
251
252ScopedAStatus AndroidKeyMintDevice::generateKey(const vector<KeyParameter>& keyParams,
Shawn Willden44c38882020-12-21 18:35:13 -0700253 const optional<AttestationKey>& attestationKey,
Shawn Willden763166c2021-01-10 19:45:01 -0700254 KeyCreationResult* creationResult) {
Shawn Willden815e8962020-12-11 13:05:27 +0000255
Shawn Willden950eb0b2021-01-06 19:15:29 +0000256 GenerateKeyRequest request(impl_->message_version());
Shawn Willden815e8962020-12-11 13:05:27 +0000257 request.key_description.Reinitialize(KmParamSet(keyParams));
Shawn Willden44c38882020-12-21 18:35:13 -0700258 if (attestationKey) {
259 request.attestation_signing_key_blob =
260 KeymasterKeyBlob(attestationKey->keyBlob.data(), attestationKey->keyBlob.size());
261 request.attest_key_params.Reinitialize(KmParamSet(attestationKey->attestKeyParams));
262 request.issuer_subject = KeymasterBlob(attestationKey->issuerSubjectName.data(),
263 attestationKey->issuerSubjectName.size());
264 }
Shawn Willden815e8962020-12-11 13:05:27 +0000265
Shawn Willden950eb0b2021-01-06 19:15:29 +0000266 GenerateKeyResponse response(impl_->message_version());
Shawn Willden815e8962020-12-11 13:05:27 +0000267 impl_->GenerateKey(request, &response);
268
269 if (response.error != KM_ERROR_OK) {
270 // Note a key difference between this current aidl and previous hal, is
271 // that hal returns void where as aidl returns the error status. If
272 // aidl returns error, then aidl will not return any change you may make
273 // to the out parameters. This is quite different from hal where all
274 // output variable can be modified due to hal returning void.
275 //
276 // So the caller need to be aware not to expect aidl functions to clear
277 // the output variables for you in case of error. If you left some
278 // wrong data set in the out parameters, they will stay there.
279 return kmError2ScopedAStatus(response.error);
280 }
281
Shawn Willden763166c2021-01-10 19:45:01 -0700282 creationResult->keyBlob = kmBlob2vector(response.key_blob);
David Drysdaleb90937d2021-03-08 13:52:17 +0000283 creationResult->keyCharacteristics = convertKeyCharacteristics(
284 securityLevel_, request.key_description, response.unenforced, response.enforced);
Shawn Willdend7d4f312020-12-21 08:31:01 -0700285 creationResult->certificateChain = convertCertificateChain(response.certificate_chain);
Shawn Willden815e8962020-12-11 13:05:27 +0000286 return ScopedAStatus::ok();
287}
288
289ScopedAStatus AndroidKeyMintDevice::importKey(const vector<KeyParameter>& keyParams,
290 KeyFormat keyFormat, const vector<uint8_t>& keyData,
Shawn Willden44c38882020-12-21 18:35:13 -0700291 const optional<AttestationKey>& attestationKey,
Shawn Willden763166c2021-01-10 19:45:01 -0700292 KeyCreationResult* creationResult) {
Shawn Willden815e8962020-12-11 13:05:27 +0000293
Shawn Willden950eb0b2021-01-06 19:15:29 +0000294 ImportKeyRequest request(impl_->message_version());
Shawn Willden815e8962020-12-11 13:05:27 +0000295 request.key_description.Reinitialize(KmParamSet(keyParams));
296 request.key_format = legacy_enum_conversion(keyFormat);
Shawn Willden6e20ea42020-12-21 18:52:34 -0700297 request.key_data = KeymasterKeyBlob(keyData.data(), keyData.size());
Shawn Willden44c38882020-12-21 18:35:13 -0700298 if (attestationKey) {
299 request.attestation_signing_key_blob =
300 KeymasterKeyBlob(attestationKey->keyBlob.data(), attestationKey->keyBlob.size());
301 request.attest_key_params.Reinitialize(KmParamSet(attestationKey->attestKeyParams));
302 request.issuer_subject = KeymasterBlob(attestationKey->issuerSubjectName.data(),
303 attestationKey->issuerSubjectName.size());
304 }
Shawn Willden815e8962020-12-11 13:05:27 +0000305
Shawn Willden950eb0b2021-01-06 19:15:29 +0000306 ImportKeyResponse response(impl_->message_version());
Shawn Willden815e8962020-12-11 13:05:27 +0000307 impl_->ImportKey(request, &response);
308
309 if (response.error != KM_ERROR_OK) {
310 return kmError2ScopedAStatus(response.error);
311 }
312
Shawn Willden763166c2021-01-10 19:45:01 -0700313 creationResult->keyBlob = kmBlob2vector(response.key_blob);
David Drysdaleb90937d2021-03-08 13:52:17 +0000314 creationResult->keyCharacteristics = convertKeyCharacteristics(
315 securityLevel_, request.key_description, response.unenforced, response.enforced);
Shawn Willdend7d4f312020-12-21 08:31:01 -0700316 creationResult->certificateChain = convertCertificateChain(response.certificate_chain);
Shawn Willden815e8962020-12-11 13:05:27 +0000317
318 return ScopedAStatus::ok();
319}
320
Shawn Willden44c38882020-12-21 18:35:13 -0700321ScopedAStatus
322AndroidKeyMintDevice::importWrappedKey(const vector<uint8_t>& wrappedKeyData, //
323 const vector<uint8_t>& wrappingKeyBlob, //
324 const vector<uint8_t>& maskingKey, //
325 const vector<KeyParameter>& unwrappingParams, //
326 int64_t passwordSid, int64_t biometricSid, //
327 KeyCreationResult* creationResult) {
Shawn Willden815e8962020-12-11 13:05:27 +0000328
Shawn Willden950eb0b2021-01-06 19:15:29 +0000329 ImportWrappedKeyRequest request(impl_->message_version());
Shawn Willden815e8962020-12-11 13:05:27 +0000330 request.SetWrappedMaterial(wrappedKeyData.data(), wrappedKeyData.size());
331 request.SetWrappingMaterial(wrappingKeyBlob.data(), wrappingKeyBlob.size());
332 request.SetMaskingKeyMaterial(maskingKey.data(), maskingKey.size());
333 request.additional_params.Reinitialize(KmParamSet(unwrappingParams));
334 request.password_sid = static_cast<uint64_t>(passwordSid);
335 request.biometric_sid = static_cast<uint64_t>(biometricSid);
336
Shawn Willden950eb0b2021-01-06 19:15:29 +0000337 ImportWrappedKeyResponse response(impl_->message_version());
Shawn Willden815e8962020-12-11 13:05:27 +0000338 impl_->ImportWrappedKey(request, &response);
339
340 if (response.error != KM_ERROR_OK) {
341 return kmError2ScopedAStatus(response.error);
342 }
343
Shawn Willden763166c2021-01-10 19:45:01 -0700344 creationResult->keyBlob = kmBlob2vector(response.key_blob);
David Drysdaleb90937d2021-03-08 13:52:17 +0000345 creationResult->keyCharacteristics = convertKeyCharacteristics(
346 securityLevel_, request.additional_params, response.unenforced, response.enforced);
Shawn Willdend7d4f312020-12-21 08:31:01 -0700347 creationResult->certificateChain = convertCertificateChain(response.certificate_chain);
Shawn Willden815e8962020-12-11 13:05:27 +0000348
349 return ScopedAStatus::ok();
350}
351
352ScopedAStatus AndroidKeyMintDevice::upgradeKey(const vector<uint8_t>& keyBlobToUpgrade,
353 const vector<KeyParameter>& upgradeParams,
354 vector<uint8_t>* keyBlob) {
355
Shawn Willden950eb0b2021-01-06 19:15:29 +0000356 UpgradeKeyRequest request(impl_->message_version());
Shawn Willden815e8962020-12-11 13:05:27 +0000357 request.SetKeyMaterial(keyBlobToUpgrade.data(), keyBlobToUpgrade.size());
358 request.upgrade_params.Reinitialize(KmParamSet(upgradeParams));
359
Shawn Willden950eb0b2021-01-06 19:15:29 +0000360 UpgradeKeyResponse response(impl_->message_version());
Shawn Willden815e8962020-12-11 13:05:27 +0000361 impl_->UpgradeKey(request, &response);
362
363 if (response.error != KM_ERROR_OK) {
364 return kmError2ScopedAStatus(response.error);
365 }
366
367 *keyBlob = kmBlob2vector(response.upgraded_key);
368 return ScopedAStatus::ok();
369}
370
371ScopedAStatus AndroidKeyMintDevice::deleteKey(const vector<uint8_t>& keyBlob) {
Shawn Willden950eb0b2021-01-06 19:15:29 +0000372 DeleteKeyRequest request(impl_->message_version());
Shawn Willden815e8962020-12-11 13:05:27 +0000373 request.SetKeyMaterial(keyBlob.data(), keyBlob.size());
374
Shawn Willden950eb0b2021-01-06 19:15:29 +0000375 DeleteKeyResponse response(impl_->message_version());
Shawn Willden815e8962020-12-11 13:05:27 +0000376 impl_->DeleteKey(request, &response);
377
378 return kmError2ScopedAStatus(response.error);
379}
380
381ScopedAStatus AndroidKeyMintDevice::deleteAllKeys() {
382 // There's nothing to be done to delete software key blobs.
Shawn Willden950eb0b2021-01-06 19:15:29 +0000383 DeleteAllKeysRequest request(impl_->message_version());
384 DeleteAllKeysResponse response(impl_->message_version());
Shawn Willden815e8962020-12-11 13:05:27 +0000385 impl_->DeleteAllKeys(request, &response);
386
387 return kmError2ScopedAStatus(response.error);
388}
389
390ScopedAStatus AndroidKeyMintDevice::destroyAttestationIds() {
391 return kmError2ScopedAStatus(KM_ERROR_UNIMPLEMENTED);
392}
393
394ScopedAStatus AndroidKeyMintDevice::begin(KeyPurpose purpose, const vector<uint8_t>& keyBlob,
395 const vector<KeyParameter>& params,
David Drysdale81815912021-04-19 19:11:41 +0100396 const optional<HardwareAuthToken>& authToken,
397 BeginResult* result) {
Shawn Willden815e8962020-12-11 13:05:27 +0000398
Shawn Willden950eb0b2021-01-06 19:15:29 +0000399 BeginOperationRequest request(impl_->message_version());
Shawn Willden815e8962020-12-11 13:05:27 +0000400 request.purpose = legacy_enum_conversion(purpose);
401 request.SetKeyMaterial(keyBlob.data(), keyBlob.size());
402 request.additional_params.Reinitialize(KmParamSet(params));
403
404 vector<uint8_t> vector_token = authToken2AidlVec(authToken);
405 request.additional_params.push_back(
406 TAG_AUTH_TOKEN, reinterpret_cast<uint8_t*>(vector_token.data()), vector_token.size());
407
Shawn Willden950eb0b2021-01-06 19:15:29 +0000408 BeginOperationResponse response(impl_->message_version());
Shawn Willden815e8962020-12-11 13:05:27 +0000409 impl_->BeginOperation(request, &response);
410
411 if (response.error != KM_ERROR_OK) {
412 return kmError2ScopedAStatus(response.error);
413 }
414
415 result->params = kmParamSet2Aidl(response.output_params);
416 result->challenge = response.op_handle;
417 result->operation =
418 ndk::SharedRefBase::make<AndroidKeyMintOperation>(impl_, response.op_handle);
419 return ScopedAStatus::ok();
420}
421
Chirag Pathakb292e9a2021-02-02 07:28:09 +0000422ScopedAStatus AndroidKeyMintDevice::deviceLocked(
Shawn Willden55929692021-02-19 14:53:02 -0700423 bool passwordOnly, const std::optional<secureclock::TimeStampToken>& timestampToken) {
Chirag Pathakb292e9a2021-02-02 07:28:09 +0000424 DeviceLockedRequest request(impl_->message_version());
Shawn Willden55929692021-02-19 14:53:02 -0700425 request.passwordOnly = passwordOnly;
426 if (timestampToken.has_value()) {
427 request.token.challenge = timestampToken->challenge;
428 request.token.mac = {timestampToken->mac.data(), timestampToken->mac.size()};
429 request.token.timestamp = timestampToken->timestamp.milliSeconds;
Chirag Pathakb292e9a2021-02-02 07:28:09 +0000430 }
431 DeviceLockedResponse response = impl_->DeviceLocked(request);
432 return kmError2ScopedAStatus(response.error);
433}
434
435ScopedAStatus AndroidKeyMintDevice::earlyBootEnded() {
436 EarlyBootEndedResponse response = impl_->EarlyBootEnded();
437 return kmError2ScopedAStatus(response.error);
438}
439
Satya Tangirala86d25402021-03-05 16:33:50 -0800440ScopedAStatus
441AndroidKeyMintDevice::convertStorageKeyToEphemeral(const std::vector<uint8_t>& /* storageKeyBlob */,
442 std::vector<uint8_t>* /* ephemeralKeyBlob */) {
443 return kmError2ScopedAStatus(KM_ERROR_UNIMPLEMENTED);
444}
445
Paul Crowley08641bf2021-04-29 12:46:49 -0700446ScopedAStatus AndroidKeyMintDevice::getKeyCharacteristics(
David Drysdalea49f3fd2021-05-20 12:04:06 +0100447 const std::vector<uint8_t>& keyBlob, const std::vector<uint8_t>& appId,
448 const std::vector<uint8_t>& appData, std::vector<KeyCharacteristics>* keyCharacteristics) {
449 GetKeyCharacteristicsRequest request(impl_->message_version());
450 request.SetKeyMaterial(keyBlob.data(), keyBlob.size());
451 addClientAndAppData(appId, appData, &request.additional_params);
452
453 GetKeyCharacteristicsResponse response(impl_->message_version());
454 impl_->GetKeyCharacteristics(request, &response);
455
456 if (response.error != KM_ERROR_OK) {
457 return kmError2ScopedAStatus(response.error);
458 }
459
460 AuthorizationSet emptySet;
461 *keyCharacteristics =
462 convertKeyCharacteristics(securityLevel_, emptySet, response.unenforced, response.enforced,
463 /* include_keystore_enforced = */ false);
464
465 return ScopedAStatus::ok();
Paul Crowley08641bf2021-04-29 12:46:49 -0700466}
467
Shawn Willden815e8962020-12-11 13:05:27 +0000468IKeyMintDevice* CreateKeyMintDevice(SecurityLevel securityLevel) {
Shawn Willden815e8962020-12-11 13:05:27 +0000469 return ::new AndroidKeyMintDevice(securityLevel);
470}
471
472} // namespace aidl::android::hardware::security::keymint