blob: fb7b632e555f66ab531812c04551a0ed881e43c6 [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 Drysdale9d480da2021-11-12 08:00:37 +000056 if (include_keystore_enforced && !sw_enforced.empty()) {
David Drysdalea49f3fd2021-05-20 12:04:06 +010057 // 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:
Eran Messeri466527c2022-11-08 12:35:05 +0000111 case KM_TAG_ATTESTATION_ID_SECOND_IMEI:
Shawn Willden763166c2021-01-10 19:45:01 -0700112 case KM_TAG_ATTESTATION_ID_MANUFACTURER:
113 case KM_TAG_ATTESTATION_ID_MEID:
114 case KM_TAG_ATTESTATION_ID_MODEL:
115 case KM_TAG_ATTESTATION_ID_PRODUCT:
116 case KM_TAG_ATTESTATION_ID_SERIAL:
117 case KM_TAG_AUTH_TOKEN:
118 case KM_TAG_CERTIFICATE_SERIAL:
119 case KM_TAG_CERTIFICATE_SUBJECT:
Janis Danisevskisa5780e22021-01-31 22:05:22 -0800120 case KM_TAG_CERTIFICATE_NOT_AFTER:
121 case KM_TAG_CERTIFICATE_NOT_BEFORE:
Shawn Willden763166c2021-01-10 19:45:01 -0700122 case KM_TAG_CONFIRMATION_TOKEN:
123 case KM_TAG_DEVICE_UNIQUE_ATTESTATION:
124 case KM_TAG_IDENTITY_CREDENTIAL_KEY:
David Drysdalee104f382021-10-13 10:19:38 +0100125 case KM_TAG_INCLUDE_UNIQUE_ID:
Shawn Willden763166c2021-01-10 19:45:01 -0700126 case KM_TAG_MAC_LENGTH:
127 case KM_TAG_NONCE:
128 case KM_TAG_RESET_SINCE_ID_ROTATION:
129 case KM_TAG_ROOT_OF_TRUST:
130 case KM_TAG_UNIQUE_ID:
131 break;
132
Shawn Willden09f47102021-01-15 15:21:56 -0700133 /* KeyMint-enforced */
Shawn Willden763166c2021-01-10 19:45:01 -0700134 case KM_TAG_ALGORITHM:
Shawn Willden09f47102021-01-15 15:21:56 -0700135 case KM_TAG_APPLICATION_ID:
Shawn Willden763166c2021-01-10 19:45:01 -0700136 case KM_TAG_AUTH_TIMEOUT:
137 case KM_TAG_BLOB_USAGE_REQUIREMENTS:
138 case KM_TAG_BLOCK_MODE:
139 case KM_TAG_BOOT_PATCHLEVEL:
140 case KM_TAG_CALLER_NONCE:
141 case KM_TAG_DIGEST:
David Drysdale82fe2592021-05-27 11:57:03 +0100142 case KM_TAG_EARLY_BOOT_ONLY:
Shawn Willden763166c2021-01-10 19:45:01 -0700143 case KM_TAG_EC_CURVE:
144 case KM_TAG_EXPORTABLE:
Shawn Willden763166c2021-01-10 19:45:01 -0700145 case KM_TAG_KEY_SIZE:
146 case KM_TAG_MAX_USES_PER_BOOT:
147 case KM_TAG_MIN_MAC_LENGTH:
148 case KM_TAG_MIN_SECONDS_BETWEEN_OPS:
149 case KM_TAG_NO_AUTH_REQUIRED:
150 case KM_TAG_ORIGIN:
151 case KM_TAG_OS_PATCHLEVEL:
152 case KM_TAG_OS_VERSION:
153 case KM_TAG_PADDING:
154 case KM_TAG_PURPOSE:
155 case KM_TAG_RSA_OAEP_MGF_DIGEST:
156 case KM_TAG_RSA_PUBLIC_EXPONENT:
David Drysdale82fe2592021-05-27 11:57:03 +0100157 case KM_TAG_TRUSTED_CONFIRMATION_REQUIRED:
158 case KM_TAG_TRUSTED_USER_PRESENCE_REQUIRED:
Shawn Willden763166c2021-01-10 19:45:01 -0700159 case KM_TAG_UNLOCKED_DEVICE_REQUIRED:
160 case KM_TAG_USER_AUTH_TYPE:
161 case KM_TAG_USER_SECURE_ID:
162 case KM_TAG_VENDOR_PATCHLEVEL:
Shawn Willden09f47102021-01-15 15:21:56 -0700163 keyMintEnforced.authorizations.push_back(kmParam2Aidl(entry));
164 break;
165
166 /* Keystore-enforced */
167 case KM_TAG_ACTIVE_DATETIME:
168 case KM_TAG_ALL_APPLICATIONS:
169 case KM_TAG_ALL_USERS:
Paul Crowley83846532021-02-10 11:24:50 -0800170 case KM_TAG_MAX_BOOT_LEVEL:
Shawn Willden09f47102021-01-15 15:21:56 -0700171 case KM_TAG_ORIGINATION_EXPIRE_DATETIME:
172 case KM_TAG_USAGE_EXPIRE_DATETIME:
173 case KM_TAG_USER_ID:
174 case KM_TAG_USAGE_COUNT_LIMIT:
175 keystoreEnforced.authorizations.push_back(kmParam2Aidl(entry));
176 break;
Shawn Willden763166c2021-01-10 19:45:01 -0700177 }
178 }
179
Shawn Willden09f47102021-01-15 15:21:56 -0700180 vector<KeyCharacteristics> retval;
181 retval.reserve(2);
182 if (!keyMintEnforced.authorizations.empty()) retval.push_back(std::move(keyMintEnforced));
David Drysdalea49f3fd2021-05-20 12:04:06 +0100183 if (include_keystore_enforced && !keystoreEnforced.authorizations.empty()) {
184 retval.push_back(std::move(keystoreEnforced));
185 }
Shawn Willden09f47102021-01-15 15:21:56 -0700186
187 return retval;
Shawn Willden763166c2021-01-10 19:45:01 -0700188}
189
Shawn Willdend7d4f312020-12-21 08:31:01 -0700190Certificate convertCertificate(const keymaster_blob_t& cert) {
191 return {std::vector<uint8_t>(cert.data, cert.data + cert.data_length)};
192}
193
194vector<Certificate> convertCertificateChain(const CertificateChain& chain) {
195 vector<Certificate> retval;
196 retval.reserve(chain.entry_count);
197 std::transform(chain.begin(), chain.end(), std::back_inserter(retval), convertCertificate);
198 return retval;
199}
200
David Drysdalea49f3fd2021-05-20 12:04:06 +0100201void addClientAndAppData(const std::vector<uint8_t>& appId, const std::vector<uint8_t>& appData,
202 ::keymaster::AuthorizationSet* params) {
203 params->Clear();
204 if (appId.size()) {
205 params->push_back(::keymaster::TAG_APPLICATION_ID, appId.data(), appId.size());
206 }
207 if (appData.size()) {
208 params->push_back(::keymaster::TAG_APPLICATION_DATA, appData.data(), appData.size());
209 }
210}
211
Shawn Willden763166c2021-01-10 19:45:01 -0700212} // namespace
213
Shawn Willden815e8962020-12-11 13:05:27 +0000214constexpr size_t kOperationTableSize = 16;
215
216AndroidKeyMintDevice::AndroidKeyMintDevice(SecurityLevel securityLevel)
Eran Messeri466527c2022-11-08 12:35:05 +0000217 : impl_(new(std::nothrow)::keymaster::AndroidKeymaster(
David Drysdaleb80ef352022-01-24 08:07:34 +0000218 [&]() -> auto{
219 auto context = new (std::nothrow) PureSoftKeymasterContext(
Eran Messeri466527c2022-11-08 12:35:05 +0000220 KmVersion::KEYMINT_3, static_cast<keymaster_security_level_t>(securityLevel));
Shawn Willden815e8962020-12-11 13:05:27 +0000221 context->SetSystemVersion(::keymaster::GetOsVersion(),
222 ::keymaster::GetOsPatchlevel());
Janis Danisevskisc427c562021-10-29 09:34:54 -0700223 context->SetVendorPatchlevel(::keymaster::GetVendorPatchlevel());
224 // Software devices cannot be configured by the boot loader but they have
225 // to return a boot patch level. So lets just return the OS patch level.
226 // The OS patch level only has a year and a month so we just add the 1st
227 // of the month as day field.
228 context->SetBootPatchlevel(GetOsPatchlevel() * 100 + 1);
Seth Moore27accf52022-02-14 10:30:04 -0800229 auto digest = ::keymaster::GetVbmetaDigest();
230 if (digest) {
231 std::string bootState = ::keymaster::GetVerifiedBootState();
232 std::string bootloaderState = ::keymaster::GetBootloaderState();
233 context->SetVerifiedBootInfo(bootState, bootloaderState, *digest);
234 } else {
235 LOG(ERROR) << "Unable to read vb_meta digest";
236 }
Shawn Willden815e8962020-12-11 13:05:27 +0000237 return context;
238 }(),
239 kOperationTableSize)),
240 securityLevel_(securityLevel) {}
241
242AndroidKeyMintDevice::~AndroidKeyMintDevice() {}
243
244ScopedAStatus AndroidKeyMintDevice::getHardwareInfo(KeyMintHardwareInfo* info) {
Eran Messeri466527c2022-11-08 12:35:05 +0000245 info->versionNumber = 3;
Shawn Willden815e8962020-12-11 13:05:27 +0000246 info->securityLevel = securityLevel_;
247 info->keyMintName = "FakeKeyMintDevice";
248 info->keyMintAuthorName = "Google";
Chirag Pathakb292e9a2021-02-02 07:28:09 +0000249 info->timestampTokenRequired = false;
Shawn Willden815e8962020-12-11 13:05:27 +0000250 return ScopedAStatus::ok();
251}
252
Shawn Willden815e8962020-12-11 13:05:27 +0000253ScopedAStatus AndroidKeyMintDevice::addRngEntropy(const vector<uint8_t>& data) {
254 if (data.size() == 0) {
255 return ScopedAStatus::ok();
256 }
257
Shawn Willden950eb0b2021-01-06 19:15:29 +0000258 AddEntropyRequest request(impl_->message_version());
Shawn Willden815e8962020-12-11 13:05:27 +0000259 request.random_data.Reinitialize(data.data(), data.size());
260
Shawn Willden950eb0b2021-01-06 19:15:29 +0000261 AddEntropyResponse response(impl_->message_version());
Shawn Willden815e8962020-12-11 13:05:27 +0000262 impl_->AddRngEntropy(request, &response);
263
264 return kmError2ScopedAStatus(response.error);
265}
266
267ScopedAStatus AndroidKeyMintDevice::generateKey(const vector<KeyParameter>& keyParams,
Shawn Willden44c38882020-12-21 18:35:13 -0700268 const optional<AttestationKey>& attestationKey,
Shawn Willden763166c2021-01-10 19:45:01 -0700269 KeyCreationResult* creationResult) {
Shawn Willden815e8962020-12-11 13:05:27 +0000270
Shawn Willden950eb0b2021-01-06 19:15:29 +0000271 GenerateKeyRequest request(impl_->message_version());
Shawn Willden815e8962020-12-11 13:05:27 +0000272 request.key_description.Reinitialize(KmParamSet(keyParams));
Shawn Willden44c38882020-12-21 18:35:13 -0700273 if (attestationKey) {
274 request.attestation_signing_key_blob =
275 KeymasterKeyBlob(attestationKey->keyBlob.data(), attestationKey->keyBlob.size());
276 request.attest_key_params.Reinitialize(KmParamSet(attestationKey->attestKeyParams));
277 request.issuer_subject = KeymasterBlob(attestationKey->issuerSubjectName.data(),
278 attestationKey->issuerSubjectName.size());
279 }
Shawn Willden815e8962020-12-11 13:05:27 +0000280
Shawn Willden950eb0b2021-01-06 19:15:29 +0000281 GenerateKeyResponse response(impl_->message_version());
Shawn Willden815e8962020-12-11 13:05:27 +0000282 impl_->GenerateKey(request, &response);
283
284 if (response.error != KM_ERROR_OK) {
285 // Note a key difference between this current aidl and previous hal, is
286 // that hal returns void where as aidl returns the error status. If
287 // aidl returns error, then aidl will not return any change you may make
288 // to the out parameters. This is quite different from hal where all
289 // output variable can be modified due to hal returning void.
290 //
291 // So the caller need to be aware not to expect aidl functions to clear
292 // the output variables for you in case of error. If you left some
293 // wrong data set in the out parameters, they will stay there.
294 return kmError2ScopedAStatus(response.error);
295 }
296
Shawn Willden763166c2021-01-10 19:45:01 -0700297 creationResult->keyBlob = kmBlob2vector(response.key_blob);
David Drysdaleb90937d2021-03-08 13:52:17 +0000298 creationResult->keyCharacteristics = convertKeyCharacteristics(
299 securityLevel_, request.key_description, response.unenforced, response.enforced);
Shawn Willdend7d4f312020-12-21 08:31:01 -0700300 creationResult->certificateChain = convertCertificateChain(response.certificate_chain);
Shawn Willden815e8962020-12-11 13:05:27 +0000301 return ScopedAStatus::ok();
302}
303
304ScopedAStatus AndroidKeyMintDevice::importKey(const vector<KeyParameter>& keyParams,
305 KeyFormat keyFormat, const vector<uint8_t>& keyData,
Shawn Willden44c38882020-12-21 18:35:13 -0700306 const optional<AttestationKey>& attestationKey,
Shawn Willden763166c2021-01-10 19:45:01 -0700307 KeyCreationResult* creationResult) {
Shawn Willden815e8962020-12-11 13:05:27 +0000308
Shawn Willden950eb0b2021-01-06 19:15:29 +0000309 ImportKeyRequest request(impl_->message_version());
Shawn Willden815e8962020-12-11 13:05:27 +0000310 request.key_description.Reinitialize(KmParamSet(keyParams));
311 request.key_format = legacy_enum_conversion(keyFormat);
Shawn Willden6e20ea42020-12-21 18:52:34 -0700312 request.key_data = KeymasterKeyBlob(keyData.data(), keyData.size());
Shawn Willden44c38882020-12-21 18:35:13 -0700313 if (attestationKey) {
314 request.attestation_signing_key_blob =
315 KeymasterKeyBlob(attestationKey->keyBlob.data(), attestationKey->keyBlob.size());
316 request.attest_key_params.Reinitialize(KmParamSet(attestationKey->attestKeyParams));
317 request.issuer_subject = KeymasterBlob(attestationKey->issuerSubjectName.data(),
318 attestationKey->issuerSubjectName.size());
319 }
Shawn Willden815e8962020-12-11 13:05:27 +0000320
Shawn Willden950eb0b2021-01-06 19:15:29 +0000321 ImportKeyResponse response(impl_->message_version());
Shawn Willden815e8962020-12-11 13:05:27 +0000322 impl_->ImportKey(request, &response);
323
324 if (response.error != KM_ERROR_OK) {
325 return kmError2ScopedAStatus(response.error);
326 }
327
Shawn Willden763166c2021-01-10 19:45:01 -0700328 creationResult->keyBlob = kmBlob2vector(response.key_blob);
David Drysdaleb90937d2021-03-08 13:52:17 +0000329 creationResult->keyCharacteristics = convertKeyCharacteristics(
330 securityLevel_, request.key_description, response.unenforced, response.enforced);
Shawn Willdend7d4f312020-12-21 08:31:01 -0700331 creationResult->certificateChain = convertCertificateChain(response.certificate_chain);
Shawn Willden815e8962020-12-11 13:05:27 +0000332
333 return ScopedAStatus::ok();
334}
335
Shawn Willden44c38882020-12-21 18:35:13 -0700336ScopedAStatus
337AndroidKeyMintDevice::importWrappedKey(const vector<uint8_t>& wrappedKeyData, //
338 const vector<uint8_t>& wrappingKeyBlob, //
339 const vector<uint8_t>& maskingKey, //
340 const vector<KeyParameter>& unwrappingParams, //
341 int64_t passwordSid, int64_t biometricSid, //
342 KeyCreationResult* creationResult) {
Shawn Willden815e8962020-12-11 13:05:27 +0000343
Shawn Willden950eb0b2021-01-06 19:15:29 +0000344 ImportWrappedKeyRequest request(impl_->message_version());
Shawn Willden815e8962020-12-11 13:05:27 +0000345 request.SetWrappedMaterial(wrappedKeyData.data(), wrappedKeyData.size());
346 request.SetWrappingMaterial(wrappingKeyBlob.data(), wrappingKeyBlob.size());
347 request.SetMaskingKeyMaterial(maskingKey.data(), maskingKey.size());
348 request.additional_params.Reinitialize(KmParamSet(unwrappingParams));
349 request.password_sid = static_cast<uint64_t>(passwordSid);
350 request.biometric_sid = static_cast<uint64_t>(biometricSid);
351
Shawn Willden950eb0b2021-01-06 19:15:29 +0000352 ImportWrappedKeyResponse response(impl_->message_version());
Shawn Willden815e8962020-12-11 13:05:27 +0000353 impl_->ImportWrappedKey(request, &response);
354
355 if (response.error != KM_ERROR_OK) {
356 return kmError2ScopedAStatus(response.error);
357 }
358
Shawn Willden763166c2021-01-10 19:45:01 -0700359 creationResult->keyBlob = kmBlob2vector(response.key_blob);
David Drysdaleb90937d2021-03-08 13:52:17 +0000360 creationResult->keyCharacteristics = convertKeyCharacteristics(
361 securityLevel_, request.additional_params, response.unenforced, response.enforced);
Shawn Willdend7d4f312020-12-21 08:31:01 -0700362 creationResult->certificateChain = convertCertificateChain(response.certificate_chain);
Shawn Willden815e8962020-12-11 13:05:27 +0000363
364 return ScopedAStatus::ok();
365}
366
367ScopedAStatus AndroidKeyMintDevice::upgradeKey(const vector<uint8_t>& keyBlobToUpgrade,
368 const vector<KeyParameter>& upgradeParams,
369 vector<uint8_t>* keyBlob) {
370
Shawn Willden950eb0b2021-01-06 19:15:29 +0000371 UpgradeKeyRequest request(impl_->message_version());
Shawn Willden815e8962020-12-11 13:05:27 +0000372 request.SetKeyMaterial(keyBlobToUpgrade.data(), keyBlobToUpgrade.size());
373 request.upgrade_params.Reinitialize(KmParamSet(upgradeParams));
374
Shawn Willden950eb0b2021-01-06 19:15:29 +0000375 UpgradeKeyResponse response(impl_->message_version());
Shawn Willden815e8962020-12-11 13:05:27 +0000376 impl_->UpgradeKey(request, &response);
377
378 if (response.error != KM_ERROR_OK) {
379 return kmError2ScopedAStatus(response.error);
380 }
381
382 *keyBlob = kmBlob2vector(response.upgraded_key);
383 return ScopedAStatus::ok();
384}
385
386ScopedAStatus AndroidKeyMintDevice::deleteKey(const vector<uint8_t>& keyBlob) {
Shawn Willden950eb0b2021-01-06 19:15:29 +0000387 DeleteKeyRequest request(impl_->message_version());
Shawn Willden815e8962020-12-11 13:05:27 +0000388 request.SetKeyMaterial(keyBlob.data(), keyBlob.size());
389
Shawn Willden950eb0b2021-01-06 19:15:29 +0000390 DeleteKeyResponse response(impl_->message_version());
Shawn Willden815e8962020-12-11 13:05:27 +0000391 impl_->DeleteKey(request, &response);
392
393 return kmError2ScopedAStatus(response.error);
394}
395
396ScopedAStatus AndroidKeyMintDevice::deleteAllKeys() {
397 // There's nothing to be done to delete software key blobs.
Shawn Willden950eb0b2021-01-06 19:15:29 +0000398 DeleteAllKeysRequest request(impl_->message_version());
399 DeleteAllKeysResponse response(impl_->message_version());
Shawn Willden815e8962020-12-11 13:05:27 +0000400 impl_->DeleteAllKeys(request, &response);
401
402 return kmError2ScopedAStatus(response.error);
403}
404
405ScopedAStatus AndroidKeyMintDevice::destroyAttestationIds() {
406 return kmError2ScopedAStatus(KM_ERROR_UNIMPLEMENTED);
407}
408
409ScopedAStatus AndroidKeyMintDevice::begin(KeyPurpose purpose, const vector<uint8_t>& keyBlob,
410 const vector<KeyParameter>& params,
David Drysdale81815912021-04-19 19:11:41 +0100411 const optional<HardwareAuthToken>& authToken,
412 BeginResult* result) {
Shawn Willden815e8962020-12-11 13:05:27 +0000413
Shawn Willden950eb0b2021-01-06 19:15:29 +0000414 BeginOperationRequest request(impl_->message_version());
Shawn Willden815e8962020-12-11 13:05:27 +0000415 request.purpose = legacy_enum_conversion(purpose);
416 request.SetKeyMaterial(keyBlob.data(), keyBlob.size());
417 request.additional_params.Reinitialize(KmParamSet(params));
418
419 vector<uint8_t> vector_token = authToken2AidlVec(authToken);
420 request.additional_params.push_back(
421 TAG_AUTH_TOKEN, reinterpret_cast<uint8_t*>(vector_token.data()), vector_token.size());
422
Shawn Willden950eb0b2021-01-06 19:15:29 +0000423 BeginOperationResponse response(impl_->message_version());
Shawn Willden815e8962020-12-11 13:05:27 +0000424 impl_->BeginOperation(request, &response);
425
426 if (response.error != KM_ERROR_OK) {
427 return kmError2ScopedAStatus(response.error);
428 }
429
430 result->params = kmParamSet2Aidl(response.output_params);
431 result->challenge = response.op_handle;
432 result->operation =
433 ndk::SharedRefBase::make<AndroidKeyMintOperation>(impl_, response.op_handle);
434 return ScopedAStatus::ok();
435}
436
Chirag Pathakb292e9a2021-02-02 07:28:09 +0000437ScopedAStatus AndroidKeyMintDevice::deviceLocked(
Shawn Willden55929692021-02-19 14:53:02 -0700438 bool passwordOnly, const std::optional<secureclock::TimeStampToken>& timestampToken) {
Chirag Pathakb292e9a2021-02-02 07:28:09 +0000439 DeviceLockedRequest request(impl_->message_version());
Shawn Willden55929692021-02-19 14:53:02 -0700440 request.passwordOnly = passwordOnly;
441 if (timestampToken.has_value()) {
442 request.token.challenge = timestampToken->challenge;
443 request.token.mac = {timestampToken->mac.data(), timestampToken->mac.size()};
444 request.token.timestamp = timestampToken->timestamp.milliSeconds;
Chirag Pathakb292e9a2021-02-02 07:28:09 +0000445 }
446 DeviceLockedResponse response = impl_->DeviceLocked(request);
447 return kmError2ScopedAStatus(response.error);
448}
449
450ScopedAStatus AndroidKeyMintDevice::earlyBootEnded() {
451 EarlyBootEndedResponse response = impl_->EarlyBootEnded();
452 return kmError2ScopedAStatus(response.error);
453}
454
Satya Tangirala86d25402021-03-05 16:33:50 -0800455ScopedAStatus
456AndroidKeyMintDevice::convertStorageKeyToEphemeral(const std::vector<uint8_t>& /* storageKeyBlob */,
457 std::vector<uint8_t>* /* ephemeralKeyBlob */) {
458 return kmError2ScopedAStatus(KM_ERROR_UNIMPLEMENTED);
459}
460
Paul Crowley08641bf2021-04-29 12:46:49 -0700461ScopedAStatus AndroidKeyMintDevice::getKeyCharacteristics(
David Drysdalea49f3fd2021-05-20 12:04:06 +0100462 const std::vector<uint8_t>& keyBlob, const std::vector<uint8_t>& appId,
463 const std::vector<uint8_t>& appData, std::vector<KeyCharacteristics>* keyCharacteristics) {
464 GetKeyCharacteristicsRequest request(impl_->message_version());
465 request.SetKeyMaterial(keyBlob.data(), keyBlob.size());
466 addClientAndAppData(appId, appData, &request.additional_params);
467
468 GetKeyCharacteristicsResponse response(impl_->message_version());
469 impl_->GetKeyCharacteristics(request, &response);
470
471 if (response.error != KM_ERROR_OK) {
472 return kmError2ScopedAStatus(response.error);
473 }
474
475 AuthorizationSet emptySet;
476 *keyCharacteristics =
477 convertKeyCharacteristics(securityLevel_, emptySet, response.unenforced, response.enforced,
478 /* include_keystore_enforced = */ false);
479
480 return ScopedAStatus::ok();
Paul Crowley08641bf2021-04-29 12:46:49 -0700481}
482
Shawn Willden5c4f5702022-02-17 15:48:35 -0700483ScopedAStatus AndroidKeyMintDevice::getRootOfTrustChallenge(array<uint8_t, 16>* /* challenge */) {
484 return kmError2ScopedAStatus(KM_ERROR_UNIMPLEMENTED);
485}
486
487ScopedAStatus AndroidKeyMintDevice::getRootOfTrust(const array<uint8_t, 16>& /* challenge */,
488 vector<uint8_t>* /* rootOfTrust */) {
489 return kmError2ScopedAStatus(KM_ERROR_UNIMPLEMENTED);
490}
491
492ScopedAStatus AndroidKeyMintDevice::sendRootOfTrust(const vector<uint8_t>& /* rootOfTrust */) {
493 return kmError2ScopedAStatus(KM_ERROR_UNIMPLEMENTED);
494}
495
Janis Danisevskis99e4a382022-02-15 13:27:44 -0800496std::shared_ptr<IKeyMintDevice> CreateKeyMintDevice(SecurityLevel securityLevel) {
497 return ndk::SharedRefBase::make<AndroidKeyMintDevice>(securityLevel);
Shawn Willden815e8962020-12-11 13:05:27 +0000498}
499
500} // namespace aidl::android::hardware::security::keymint