Shawn Willden | 815e896 | 2020-12-11 13:05:27 +0000 | [diff] [blame] | 1 | /* |
| 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 Willden | 763166c | 2021-01-10 19:45:01 -0700 | [diff] [blame] | 18 | #include <android-base/logging.h> |
Shawn Willden | 815e896 | 2020-12-11 13:05:27 +0000 | [diff] [blame] | 19 | |
| 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 | |
| 31 | namespace aidl::android::hardware::security::keymint { |
| 32 | |
Shawn Willden | 9a3792e | 2021-04-08 09:38:14 -0600 | [diff] [blame] | 33 | using namespace keymaster; // NOLINT(google-build-using-namespace) |
| 34 | |
| 35 | using km_utils::authToken2AidlVec; |
| 36 | using km_utils::kmBlob2vector; |
| 37 | using km_utils::kmError2ScopedAStatus; |
| 38 | using km_utils::kmParam2Aidl; |
| 39 | using km_utils::KmParamSet; |
| 40 | using km_utils::kmParamSet2Aidl; |
| 41 | using km_utils::legacy_enum_conversion; |
Chirag Pathak | b292e9a | 2021-02-02 07:28:09 +0000 | [diff] [blame] | 42 | using secureclock::TimeStampToken; |
Shawn Willden | 815e896 | 2020-12-11 13:05:27 +0000 | [diff] [blame] | 43 | |
Shawn Willden | 763166c | 2021-01-10 19:45:01 -0700 | [diff] [blame] | 44 | namespace { |
| 45 | |
| 46 | vector<KeyCharacteristics> convertKeyCharacteristics(SecurityLevel keyMintSecurityLevel, |
David Drysdale | b90937d | 2021-03-08 13:52:17 +0000 | [diff] [blame] | 47 | const AuthorizationSet& requestParams, |
Shawn Willden | 763166c | 2021-01-10 19:45:01 -0700 | [diff] [blame] | 48 | const AuthorizationSet& sw_enforced, |
David Drysdale | a49f3fd | 2021-05-20 12:04:06 +0100 | [diff] [blame] | 49 | const AuthorizationSet& hw_enforced, |
| 50 | bool include_keystore_enforced = true) { |
Shawn Willden | 09f4710 | 2021-01-15 15:21:56 -0700 | [diff] [blame] | 51 | KeyCharacteristics keyMintEnforced{keyMintSecurityLevel, {}}; |
Shawn Willden | 763166c | 2021-01-10 19:45:01 -0700 | [diff] [blame] | 52 | |
Shawn Willden | 763166c | 2021-01-10 19:45:01 -0700 | [diff] [blame] | 53 | if (keyMintSecurityLevel != SecurityLevel::SOFTWARE) { |
Qi Wu | f7c8e9d | 2021-02-11 03:14:52 +0800 | [diff] [blame] | 54 | // We're pretending to be TRUSTED_ENVIRONMENT or STRONGBOX. |
Shawn Willden | 09f4710 | 2021-01-15 15:21:56 -0700 | [diff] [blame] | 55 | keyMintEnforced.authorizations = kmParamSet2Aidl(hw_enforced); |
David Drysdale | 9d480da | 2021-11-12 08:00:37 +0000 | [diff] [blame] | 56 | if (include_keystore_enforced && !sw_enforced.empty()) { |
David Drysdale | a49f3fd | 2021-05-20 12:04:06 +0100 | [diff] [blame] | 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 Willden | 763166c | 2021-01-10 19:45:01 -0700 | [diff] [blame] | 64 | } |
| 65 | |
Shawn Willden | 09f4710 | 2021-01-15 15:21:56 -0700 | [diff] [blame] | 66 | KeyCharacteristics keystoreEnforced{SecurityLevel::KEYSTORE, {}}; |
Shawn Willden | 763166c | 2021-01-10 19:45:01 -0700 | [diff] [blame] | 67 | CHECK(hw_enforced.empty()) << "Hardware-enforced list is non-empty for pure SW KeyMint"; |
| 68 | |
Shawn Willden | 09f4710 | 2021-01-15 15:21:56 -0700 | [diff] [blame] | 69 | // 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 Willden | 763166c | 2021-01-10 19:45:01 -0700 | [diff] [blame] | 72 | |
| 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 Willden | 09f4710 | 2021-01-15 15:21:56 -0700 | [diff] [blame] | 79 | case KM_TAG_ROLLBACK_RESISTANCE: |
Shawn Willden | 763166c | 2021-01-10 19:45:01 -0700 | [diff] [blame] | 80 | CHECK(false) << "We shouldn't see tag " << entry.tag; |
| 81 | break; |
| 82 | |
| 83 | /* Unimplemented */ |
| 84 | case KM_TAG_ALLOW_WHILE_ON_BODY: |
Shawn Willden | 763166c | 2021-01-10 19:45:01 -0700 | [diff] [blame] | 85 | case KM_TAG_BOOTLOADER_ONLY: |
Shawn Willden | 763166c | 2021-01-10 19:45:01 -0700 | [diff] [blame] | 86 | case KM_TAG_ROLLBACK_RESISTANT: |
| 87 | case KM_TAG_STORAGE_KEY: |
| 88 | break; |
| 89 | |
David Drysdale | b90937d | 2021-03-08 13:52:17 +0000 | [diff] [blame] | 90 | /* Keystore-enforced if not locally generated. */ |
Shawn Willden | 763166c | 2021-01-10 19:45:01 -0700 | [diff] [blame] | 91 | case KM_TAG_CREATION_DATETIME: |
David Drysdale | b90937d | 2021-03-08 13:52:17 +0000 | [diff] [blame] | 92 | // 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 Willden | 763166c | 2021-01-10 19:45:01 -0700 | [diff] [blame] | 98 | break; |
| 99 | |
| 100 | /* Disallowed in KeyCharacteristics */ |
| 101 | case KM_TAG_APPLICATION_DATA: |
Shawn Willden | 09f4710 | 2021-01-15 15:21:56 -0700 | [diff] [blame] | 102 | case KM_TAG_ATTESTATION_APPLICATION_ID: |
Shawn Willden | 763166c | 2021-01-10 19:45:01 -0700 | [diff] [blame] | 103 | break; |
| 104 | |
| 105 | /* Not key characteristics */ |
| 106 | case KM_TAG_ASSOCIATED_DATA: |
Shawn Willden | 763166c | 2021-01-10 19:45:01 -0700 | [diff] [blame] | 107 | 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 Danisevskis | a5780e2 | 2021-01-31 22:05:22 -0800 | [diff] [blame] | 119 | case KM_TAG_CERTIFICATE_NOT_AFTER: |
| 120 | case KM_TAG_CERTIFICATE_NOT_BEFORE: |
Shawn Willden | 763166c | 2021-01-10 19:45:01 -0700 | [diff] [blame] | 121 | case KM_TAG_CONFIRMATION_TOKEN: |
| 122 | case KM_TAG_DEVICE_UNIQUE_ATTESTATION: |
| 123 | case KM_TAG_IDENTITY_CREDENTIAL_KEY: |
David Drysdale | e104f38 | 2021-10-13 10:19:38 +0100 | [diff] [blame] | 124 | case KM_TAG_INCLUDE_UNIQUE_ID: |
Shawn Willden | 763166c | 2021-01-10 19:45:01 -0700 | [diff] [blame] | 125 | case KM_TAG_MAC_LENGTH: |
| 126 | case KM_TAG_NONCE: |
| 127 | case KM_TAG_RESET_SINCE_ID_ROTATION: |
| 128 | case KM_TAG_ROOT_OF_TRUST: |
| 129 | case KM_TAG_UNIQUE_ID: |
| 130 | break; |
| 131 | |
Shawn Willden | 09f4710 | 2021-01-15 15:21:56 -0700 | [diff] [blame] | 132 | /* KeyMint-enforced */ |
Shawn Willden | 763166c | 2021-01-10 19:45:01 -0700 | [diff] [blame] | 133 | case KM_TAG_ALGORITHM: |
Shawn Willden | 09f4710 | 2021-01-15 15:21:56 -0700 | [diff] [blame] | 134 | case KM_TAG_APPLICATION_ID: |
Shawn Willden | 763166c | 2021-01-10 19:45:01 -0700 | [diff] [blame] | 135 | case KM_TAG_AUTH_TIMEOUT: |
| 136 | case KM_TAG_BLOB_USAGE_REQUIREMENTS: |
| 137 | case KM_TAG_BLOCK_MODE: |
| 138 | case KM_TAG_BOOT_PATCHLEVEL: |
| 139 | case KM_TAG_CALLER_NONCE: |
| 140 | case KM_TAG_DIGEST: |
David Drysdale | 82fe259 | 2021-05-27 11:57:03 +0100 | [diff] [blame] | 141 | case KM_TAG_EARLY_BOOT_ONLY: |
Shawn Willden | 763166c | 2021-01-10 19:45:01 -0700 | [diff] [blame] | 142 | case KM_TAG_EC_CURVE: |
| 143 | case KM_TAG_EXPORTABLE: |
Shawn Willden | 763166c | 2021-01-10 19:45:01 -0700 | [diff] [blame] | 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 Drysdale | 82fe259 | 2021-05-27 11:57:03 +0100 | [diff] [blame] | 156 | case KM_TAG_TRUSTED_CONFIRMATION_REQUIRED: |
| 157 | case KM_TAG_TRUSTED_USER_PRESENCE_REQUIRED: |
Shawn Willden | 763166c | 2021-01-10 19:45:01 -0700 | [diff] [blame] | 158 | 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 Willden | 09f4710 | 2021-01-15 15:21:56 -0700 | [diff] [blame] | 162 | 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 Crowley | 8384653 | 2021-02-10 11:24:50 -0800 | [diff] [blame] | 169 | case KM_TAG_MAX_BOOT_LEVEL: |
Shawn Willden | 09f4710 | 2021-01-15 15:21:56 -0700 | [diff] [blame] | 170 | 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 Willden | 763166c | 2021-01-10 19:45:01 -0700 | [diff] [blame] | 176 | } |
| 177 | } |
| 178 | |
Shawn Willden | 09f4710 | 2021-01-15 15:21:56 -0700 | [diff] [blame] | 179 | vector<KeyCharacteristics> retval; |
| 180 | retval.reserve(2); |
| 181 | if (!keyMintEnforced.authorizations.empty()) retval.push_back(std::move(keyMintEnforced)); |
David Drysdale | a49f3fd | 2021-05-20 12:04:06 +0100 | [diff] [blame] | 182 | if (include_keystore_enforced && !keystoreEnforced.authorizations.empty()) { |
| 183 | retval.push_back(std::move(keystoreEnforced)); |
| 184 | } |
Shawn Willden | 09f4710 | 2021-01-15 15:21:56 -0700 | [diff] [blame] | 185 | |
| 186 | return retval; |
Shawn Willden | 763166c | 2021-01-10 19:45:01 -0700 | [diff] [blame] | 187 | } |
| 188 | |
Shawn Willden | d7d4f31 | 2020-12-21 08:31:01 -0700 | [diff] [blame] | 189 | Certificate convertCertificate(const keymaster_blob_t& cert) { |
| 190 | return {std::vector<uint8_t>(cert.data, cert.data + cert.data_length)}; |
| 191 | } |
| 192 | |
| 193 | vector<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 Drysdale | a49f3fd | 2021-05-20 12:04:06 +0100 | [diff] [blame] | 200 | void 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 Willden | 763166c | 2021-01-10 19:45:01 -0700 | [diff] [blame] | 211 | } // namespace |
| 212 | |
Shawn Willden | 815e896 | 2020-12-11 13:05:27 +0000 | [diff] [blame] | 213 | constexpr size_t kOperationTableSize = 16; |
| 214 | |
| 215 | AndroidKeyMintDevice::AndroidKeyMintDevice(SecurityLevel securityLevel) |
| 216 | : impl_(new ::keymaster::AndroidKeymaster( |
| 217 | [&]() -> auto { |
| 218 | auto context = new PureSoftKeymasterContext( |
David Drysdale | 3475416 | 2021-12-06 10:03:56 +0000 | [diff] [blame] | 219 | KmVersion::KEYMINT_2, static_cast<keymaster_security_level_t>(securityLevel)); |
Shawn Willden | 815e896 | 2020-12-11 13:05:27 +0000 | [diff] [blame] | 220 | context->SetSystemVersion(::keymaster::GetOsVersion(), |
| 221 | ::keymaster::GetOsPatchlevel()); |
Janis Danisevskis | c427c56 | 2021-10-29 09:34:54 -0700 | [diff] [blame] | 222 | context->SetVendorPatchlevel(::keymaster::GetVendorPatchlevel()); |
| 223 | // Software devices cannot be configured by the boot loader but they have |
| 224 | // to return a boot patch level. So lets just return the OS patch level. |
| 225 | // The OS patch level only has a year and a month so we just add the 1st |
| 226 | // of the month as day field. |
| 227 | context->SetBootPatchlevel(GetOsPatchlevel() * 100 + 1); |
Seth Moore | 27accf5 | 2022-02-14 10:30:04 -0800 | [diff] [blame] | 228 | auto digest = ::keymaster::GetVbmetaDigest(); |
| 229 | if (digest) { |
| 230 | std::string bootState = ::keymaster::GetVerifiedBootState(); |
| 231 | std::string bootloaderState = ::keymaster::GetBootloaderState(); |
| 232 | context->SetVerifiedBootInfo(bootState, bootloaderState, *digest); |
| 233 | } else { |
| 234 | LOG(ERROR) << "Unable to read vb_meta digest"; |
| 235 | } |
Shawn Willden | 815e896 | 2020-12-11 13:05:27 +0000 | [diff] [blame] | 236 | return context; |
| 237 | }(), |
| 238 | kOperationTableSize)), |
| 239 | securityLevel_(securityLevel) {} |
| 240 | |
| 241 | AndroidKeyMintDevice::~AndroidKeyMintDevice() {} |
| 242 | |
| 243 | ScopedAStatus AndroidKeyMintDevice::getHardwareInfo(KeyMintHardwareInfo* info) { |
David Drysdale | 3475416 | 2021-12-06 10:03:56 +0000 | [diff] [blame] | 244 | info->versionNumber = 2; |
Shawn Willden | 815e896 | 2020-12-11 13:05:27 +0000 | [diff] [blame] | 245 | info->securityLevel = securityLevel_; |
| 246 | info->keyMintName = "FakeKeyMintDevice"; |
| 247 | info->keyMintAuthorName = "Google"; |
Chirag Pathak | b292e9a | 2021-02-02 07:28:09 +0000 | [diff] [blame] | 248 | info->timestampTokenRequired = false; |
Shawn Willden | 815e896 | 2020-12-11 13:05:27 +0000 | [diff] [blame] | 249 | return ScopedAStatus::ok(); |
| 250 | } |
| 251 | |
Shawn Willden | 815e896 | 2020-12-11 13:05:27 +0000 | [diff] [blame] | 252 | ScopedAStatus AndroidKeyMintDevice::addRngEntropy(const vector<uint8_t>& data) { |
| 253 | if (data.size() == 0) { |
| 254 | return ScopedAStatus::ok(); |
| 255 | } |
| 256 | |
Shawn Willden | 950eb0b | 2021-01-06 19:15:29 +0000 | [diff] [blame] | 257 | AddEntropyRequest request(impl_->message_version()); |
Shawn Willden | 815e896 | 2020-12-11 13:05:27 +0000 | [diff] [blame] | 258 | request.random_data.Reinitialize(data.data(), data.size()); |
| 259 | |
Shawn Willden | 950eb0b | 2021-01-06 19:15:29 +0000 | [diff] [blame] | 260 | AddEntropyResponse response(impl_->message_version()); |
Shawn Willden | 815e896 | 2020-12-11 13:05:27 +0000 | [diff] [blame] | 261 | impl_->AddRngEntropy(request, &response); |
| 262 | |
| 263 | return kmError2ScopedAStatus(response.error); |
| 264 | } |
| 265 | |
| 266 | ScopedAStatus AndroidKeyMintDevice::generateKey(const vector<KeyParameter>& keyParams, |
Shawn Willden | 44c3888 | 2020-12-21 18:35:13 -0700 | [diff] [blame] | 267 | const optional<AttestationKey>& attestationKey, |
Shawn Willden | 763166c | 2021-01-10 19:45:01 -0700 | [diff] [blame] | 268 | KeyCreationResult* creationResult) { |
Shawn Willden | 815e896 | 2020-12-11 13:05:27 +0000 | [diff] [blame] | 269 | |
Shawn Willden | 950eb0b | 2021-01-06 19:15:29 +0000 | [diff] [blame] | 270 | GenerateKeyRequest request(impl_->message_version()); |
Shawn Willden | 815e896 | 2020-12-11 13:05:27 +0000 | [diff] [blame] | 271 | request.key_description.Reinitialize(KmParamSet(keyParams)); |
Shawn Willden | 44c3888 | 2020-12-21 18:35:13 -0700 | [diff] [blame] | 272 | if (attestationKey) { |
| 273 | request.attestation_signing_key_blob = |
| 274 | KeymasterKeyBlob(attestationKey->keyBlob.data(), attestationKey->keyBlob.size()); |
| 275 | request.attest_key_params.Reinitialize(KmParamSet(attestationKey->attestKeyParams)); |
| 276 | request.issuer_subject = KeymasterBlob(attestationKey->issuerSubjectName.data(), |
| 277 | attestationKey->issuerSubjectName.size()); |
| 278 | } |
Shawn Willden | 815e896 | 2020-12-11 13:05:27 +0000 | [diff] [blame] | 279 | |
Shawn Willden | 950eb0b | 2021-01-06 19:15:29 +0000 | [diff] [blame] | 280 | GenerateKeyResponse response(impl_->message_version()); |
Shawn Willden | 815e896 | 2020-12-11 13:05:27 +0000 | [diff] [blame] | 281 | impl_->GenerateKey(request, &response); |
| 282 | |
| 283 | if (response.error != KM_ERROR_OK) { |
| 284 | // Note a key difference between this current aidl and previous hal, is |
| 285 | // that hal returns void where as aidl returns the error status. If |
| 286 | // aidl returns error, then aidl will not return any change you may make |
| 287 | // to the out parameters. This is quite different from hal where all |
| 288 | // output variable can be modified due to hal returning void. |
| 289 | // |
| 290 | // So the caller need to be aware not to expect aidl functions to clear |
| 291 | // the output variables for you in case of error. If you left some |
| 292 | // wrong data set in the out parameters, they will stay there. |
| 293 | return kmError2ScopedAStatus(response.error); |
| 294 | } |
| 295 | |
Shawn Willden | 763166c | 2021-01-10 19:45:01 -0700 | [diff] [blame] | 296 | creationResult->keyBlob = kmBlob2vector(response.key_blob); |
David Drysdale | b90937d | 2021-03-08 13:52:17 +0000 | [diff] [blame] | 297 | creationResult->keyCharacteristics = convertKeyCharacteristics( |
| 298 | securityLevel_, request.key_description, response.unenforced, response.enforced); |
Shawn Willden | d7d4f31 | 2020-12-21 08:31:01 -0700 | [diff] [blame] | 299 | creationResult->certificateChain = convertCertificateChain(response.certificate_chain); |
Shawn Willden | 815e896 | 2020-12-11 13:05:27 +0000 | [diff] [blame] | 300 | return ScopedAStatus::ok(); |
| 301 | } |
| 302 | |
| 303 | ScopedAStatus AndroidKeyMintDevice::importKey(const vector<KeyParameter>& keyParams, |
| 304 | KeyFormat keyFormat, const vector<uint8_t>& keyData, |
Shawn Willden | 44c3888 | 2020-12-21 18:35:13 -0700 | [diff] [blame] | 305 | const optional<AttestationKey>& attestationKey, |
Shawn Willden | 763166c | 2021-01-10 19:45:01 -0700 | [diff] [blame] | 306 | KeyCreationResult* creationResult) { |
Shawn Willden | 815e896 | 2020-12-11 13:05:27 +0000 | [diff] [blame] | 307 | |
Shawn Willden | 950eb0b | 2021-01-06 19:15:29 +0000 | [diff] [blame] | 308 | ImportKeyRequest request(impl_->message_version()); |
Shawn Willden | 815e896 | 2020-12-11 13:05:27 +0000 | [diff] [blame] | 309 | request.key_description.Reinitialize(KmParamSet(keyParams)); |
| 310 | request.key_format = legacy_enum_conversion(keyFormat); |
Shawn Willden | 6e20ea4 | 2020-12-21 18:52:34 -0700 | [diff] [blame] | 311 | request.key_data = KeymasterKeyBlob(keyData.data(), keyData.size()); |
Shawn Willden | 44c3888 | 2020-12-21 18:35:13 -0700 | [diff] [blame] | 312 | if (attestationKey) { |
| 313 | request.attestation_signing_key_blob = |
| 314 | KeymasterKeyBlob(attestationKey->keyBlob.data(), attestationKey->keyBlob.size()); |
| 315 | request.attest_key_params.Reinitialize(KmParamSet(attestationKey->attestKeyParams)); |
| 316 | request.issuer_subject = KeymasterBlob(attestationKey->issuerSubjectName.data(), |
| 317 | attestationKey->issuerSubjectName.size()); |
| 318 | } |
Shawn Willden | 815e896 | 2020-12-11 13:05:27 +0000 | [diff] [blame] | 319 | |
Shawn Willden | 950eb0b | 2021-01-06 19:15:29 +0000 | [diff] [blame] | 320 | ImportKeyResponse response(impl_->message_version()); |
Shawn Willden | 815e896 | 2020-12-11 13:05:27 +0000 | [diff] [blame] | 321 | impl_->ImportKey(request, &response); |
| 322 | |
| 323 | if (response.error != KM_ERROR_OK) { |
| 324 | return kmError2ScopedAStatus(response.error); |
| 325 | } |
| 326 | |
Shawn Willden | 763166c | 2021-01-10 19:45:01 -0700 | [diff] [blame] | 327 | creationResult->keyBlob = kmBlob2vector(response.key_blob); |
David Drysdale | b90937d | 2021-03-08 13:52:17 +0000 | [diff] [blame] | 328 | creationResult->keyCharacteristics = convertKeyCharacteristics( |
| 329 | securityLevel_, request.key_description, response.unenforced, response.enforced); |
Shawn Willden | d7d4f31 | 2020-12-21 08:31:01 -0700 | [diff] [blame] | 330 | creationResult->certificateChain = convertCertificateChain(response.certificate_chain); |
Shawn Willden | 815e896 | 2020-12-11 13:05:27 +0000 | [diff] [blame] | 331 | |
| 332 | return ScopedAStatus::ok(); |
| 333 | } |
| 334 | |
Shawn Willden | 44c3888 | 2020-12-21 18:35:13 -0700 | [diff] [blame] | 335 | ScopedAStatus |
| 336 | AndroidKeyMintDevice::importWrappedKey(const vector<uint8_t>& wrappedKeyData, // |
| 337 | const vector<uint8_t>& wrappingKeyBlob, // |
| 338 | const vector<uint8_t>& maskingKey, // |
| 339 | const vector<KeyParameter>& unwrappingParams, // |
| 340 | int64_t passwordSid, int64_t biometricSid, // |
| 341 | KeyCreationResult* creationResult) { |
Shawn Willden | 815e896 | 2020-12-11 13:05:27 +0000 | [diff] [blame] | 342 | |
Shawn Willden | 950eb0b | 2021-01-06 19:15:29 +0000 | [diff] [blame] | 343 | ImportWrappedKeyRequest request(impl_->message_version()); |
Shawn Willden | 815e896 | 2020-12-11 13:05:27 +0000 | [diff] [blame] | 344 | request.SetWrappedMaterial(wrappedKeyData.data(), wrappedKeyData.size()); |
| 345 | request.SetWrappingMaterial(wrappingKeyBlob.data(), wrappingKeyBlob.size()); |
| 346 | request.SetMaskingKeyMaterial(maskingKey.data(), maskingKey.size()); |
| 347 | request.additional_params.Reinitialize(KmParamSet(unwrappingParams)); |
| 348 | request.password_sid = static_cast<uint64_t>(passwordSid); |
| 349 | request.biometric_sid = static_cast<uint64_t>(biometricSid); |
| 350 | |
Shawn Willden | 950eb0b | 2021-01-06 19:15:29 +0000 | [diff] [blame] | 351 | ImportWrappedKeyResponse response(impl_->message_version()); |
Shawn Willden | 815e896 | 2020-12-11 13:05:27 +0000 | [diff] [blame] | 352 | impl_->ImportWrappedKey(request, &response); |
| 353 | |
| 354 | if (response.error != KM_ERROR_OK) { |
| 355 | return kmError2ScopedAStatus(response.error); |
| 356 | } |
| 357 | |
Shawn Willden | 763166c | 2021-01-10 19:45:01 -0700 | [diff] [blame] | 358 | creationResult->keyBlob = kmBlob2vector(response.key_blob); |
David Drysdale | b90937d | 2021-03-08 13:52:17 +0000 | [diff] [blame] | 359 | creationResult->keyCharacteristics = convertKeyCharacteristics( |
| 360 | securityLevel_, request.additional_params, response.unenforced, response.enforced); |
Shawn Willden | d7d4f31 | 2020-12-21 08:31:01 -0700 | [diff] [blame] | 361 | creationResult->certificateChain = convertCertificateChain(response.certificate_chain); |
Shawn Willden | 815e896 | 2020-12-11 13:05:27 +0000 | [diff] [blame] | 362 | |
| 363 | return ScopedAStatus::ok(); |
| 364 | } |
| 365 | |
| 366 | ScopedAStatus AndroidKeyMintDevice::upgradeKey(const vector<uint8_t>& keyBlobToUpgrade, |
| 367 | const vector<KeyParameter>& upgradeParams, |
| 368 | vector<uint8_t>* keyBlob) { |
| 369 | |
Shawn Willden | 950eb0b | 2021-01-06 19:15:29 +0000 | [diff] [blame] | 370 | UpgradeKeyRequest request(impl_->message_version()); |
Shawn Willden | 815e896 | 2020-12-11 13:05:27 +0000 | [diff] [blame] | 371 | request.SetKeyMaterial(keyBlobToUpgrade.data(), keyBlobToUpgrade.size()); |
| 372 | request.upgrade_params.Reinitialize(KmParamSet(upgradeParams)); |
| 373 | |
Shawn Willden | 950eb0b | 2021-01-06 19:15:29 +0000 | [diff] [blame] | 374 | UpgradeKeyResponse response(impl_->message_version()); |
Shawn Willden | 815e896 | 2020-12-11 13:05:27 +0000 | [diff] [blame] | 375 | impl_->UpgradeKey(request, &response); |
| 376 | |
| 377 | if (response.error != KM_ERROR_OK) { |
| 378 | return kmError2ScopedAStatus(response.error); |
| 379 | } |
| 380 | |
| 381 | *keyBlob = kmBlob2vector(response.upgraded_key); |
| 382 | return ScopedAStatus::ok(); |
| 383 | } |
| 384 | |
| 385 | ScopedAStatus AndroidKeyMintDevice::deleteKey(const vector<uint8_t>& keyBlob) { |
Shawn Willden | 950eb0b | 2021-01-06 19:15:29 +0000 | [diff] [blame] | 386 | DeleteKeyRequest request(impl_->message_version()); |
Shawn Willden | 815e896 | 2020-12-11 13:05:27 +0000 | [diff] [blame] | 387 | request.SetKeyMaterial(keyBlob.data(), keyBlob.size()); |
| 388 | |
Shawn Willden | 950eb0b | 2021-01-06 19:15:29 +0000 | [diff] [blame] | 389 | DeleteKeyResponse response(impl_->message_version()); |
Shawn Willden | 815e896 | 2020-12-11 13:05:27 +0000 | [diff] [blame] | 390 | impl_->DeleteKey(request, &response); |
| 391 | |
| 392 | return kmError2ScopedAStatus(response.error); |
| 393 | } |
| 394 | |
| 395 | ScopedAStatus AndroidKeyMintDevice::deleteAllKeys() { |
| 396 | // There's nothing to be done to delete software key blobs. |
Shawn Willden | 950eb0b | 2021-01-06 19:15:29 +0000 | [diff] [blame] | 397 | DeleteAllKeysRequest request(impl_->message_version()); |
| 398 | DeleteAllKeysResponse response(impl_->message_version()); |
Shawn Willden | 815e896 | 2020-12-11 13:05:27 +0000 | [diff] [blame] | 399 | impl_->DeleteAllKeys(request, &response); |
| 400 | |
| 401 | return kmError2ScopedAStatus(response.error); |
| 402 | } |
| 403 | |
| 404 | ScopedAStatus AndroidKeyMintDevice::destroyAttestationIds() { |
| 405 | return kmError2ScopedAStatus(KM_ERROR_UNIMPLEMENTED); |
| 406 | } |
| 407 | |
| 408 | ScopedAStatus AndroidKeyMintDevice::begin(KeyPurpose purpose, const vector<uint8_t>& keyBlob, |
| 409 | const vector<KeyParameter>& params, |
David Drysdale | 8181591 | 2021-04-19 19:11:41 +0100 | [diff] [blame] | 410 | const optional<HardwareAuthToken>& authToken, |
| 411 | BeginResult* result) { |
Shawn Willden | 815e896 | 2020-12-11 13:05:27 +0000 | [diff] [blame] | 412 | |
Shawn Willden | 950eb0b | 2021-01-06 19:15:29 +0000 | [diff] [blame] | 413 | BeginOperationRequest request(impl_->message_version()); |
Shawn Willden | 815e896 | 2020-12-11 13:05:27 +0000 | [diff] [blame] | 414 | request.purpose = legacy_enum_conversion(purpose); |
| 415 | request.SetKeyMaterial(keyBlob.data(), keyBlob.size()); |
| 416 | request.additional_params.Reinitialize(KmParamSet(params)); |
| 417 | |
| 418 | vector<uint8_t> vector_token = authToken2AidlVec(authToken); |
| 419 | request.additional_params.push_back( |
| 420 | TAG_AUTH_TOKEN, reinterpret_cast<uint8_t*>(vector_token.data()), vector_token.size()); |
| 421 | |
Shawn Willden | 950eb0b | 2021-01-06 19:15:29 +0000 | [diff] [blame] | 422 | BeginOperationResponse response(impl_->message_version()); |
Shawn Willden | 815e896 | 2020-12-11 13:05:27 +0000 | [diff] [blame] | 423 | impl_->BeginOperation(request, &response); |
| 424 | |
| 425 | if (response.error != KM_ERROR_OK) { |
| 426 | return kmError2ScopedAStatus(response.error); |
| 427 | } |
| 428 | |
| 429 | result->params = kmParamSet2Aidl(response.output_params); |
| 430 | result->challenge = response.op_handle; |
| 431 | result->operation = |
| 432 | ndk::SharedRefBase::make<AndroidKeyMintOperation>(impl_, response.op_handle); |
| 433 | return ScopedAStatus::ok(); |
| 434 | } |
| 435 | |
Chirag Pathak | b292e9a | 2021-02-02 07:28:09 +0000 | [diff] [blame] | 436 | ScopedAStatus AndroidKeyMintDevice::deviceLocked( |
Shawn Willden | 5592969 | 2021-02-19 14:53:02 -0700 | [diff] [blame] | 437 | bool passwordOnly, const std::optional<secureclock::TimeStampToken>& timestampToken) { |
Chirag Pathak | b292e9a | 2021-02-02 07:28:09 +0000 | [diff] [blame] | 438 | DeviceLockedRequest request(impl_->message_version()); |
Shawn Willden | 5592969 | 2021-02-19 14:53:02 -0700 | [diff] [blame] | 439 | request.passwordOnly = passwordOnly; |
| 440 | if (timestampToken.has_value()) { |
| 441 | request.token.challenge = timestampToken->challenge; |
| 442 | request.token.mac = {timestampToken->mac.data(), timestampToken->mac.size()}; |
| 443 | request.token.timestamp = timestampToken->timestamp.milliSeconds; |
Chirag Pathak | b292e9a | 2021-02-02 07:28:09 +0000 | [diff] [blame] | 444 | } |
| 445 | DeviceLockedResponse response = impl_->DeviceLocked(request); |
| 446 | return kmError2ScopedAStatus(response.error); |
| 447 | } |
| 448 | |
| 449 | ScopedAStatus AndroidKeyMintDevice::earlyBootEnded() { |
| 450 | EarlyBootEndedResponse response = impl_->EarlyBootEnded(); |
| 451 | return kmError2ScopedAStatus(response.error); |
| 452 | } |
| 453 | |
Satya Tangirala | 86d2540 | 2021-03-05 16:33:50 -0800 | [diff] [blame] | 454 | ScopedAStatus |
| 455 | AndroidKeyMintDevice::convertStorageKeyToEphemeral(const std::vector<uint8_t>& /* storageKeyBlob */, |
| 456 | std::vector<uint8_t>* /* ephemeralKeyBlob */) { |
| 457 | return kmError2ScopedAStatus(KM_ERROR_UNIMPLEMENTED); |
| 458 | } |
| 459 | |
Paul Crowley | 08641bf | 2021-04-29 12:46:49 -0700 | [diff] [blame] | 460 | ScopedAStatus AndroidKeyMintDevice::getKeyCharacteristics( |
David Drysdale | a49f3fd | 2021-05-20 12:04:06 +0100 | [diff] [blame] | 461 | const std::vector<uint8_t>& keyBlob, const std::vector<uint8_t>& appId, |
| 462 | const std::vector<uint8_t>& appData, std::vector<KeyCharacteristics>* keyCharacteristics) { |
| 463 | GetKeyCharacteristicsRequest request(impl_->message_version()); |
| 464 | request.SetKeyMaterial(keyBlob.data(), keyBlob.size()); |
| 465 | addClientAndAppData(appId, appData, &request.additional_params); |
| 466 | |
| 467 | GetKeyCharacteristicsResponse response(impl_->message_version()); |
| 468 | impl_->GetKeyCharacteristics(request, &response); |
| 469 | |
| 470 | if (response.error != KM_ERROR_OK) { |
| 471 | return kmError2ScopedAStatus(response.error); |
| 472 | } |
| 473 | |
| 474 | AuthorizationSet emptySet; |
| 475 | *keyCharacteristics = |
| 476 | convertKeyCharacteristics(securityLevel_, emptySet, response.unenforced, response.enforced, |
| 477 | /* include_keystore_enforced = */ false); |
| 478 | |
| 479 | return ScopedAStatus::ok(); |
Paul Crowley | 08641bf | 2021-04-29 12:46:49 -0700 | [diff] [blame] | 480 | } |
| 481 | |
Shawn Willden | 5c4f570 | 2022-02-17 15:48:35 -0700 | [diff] [blame] | 482 | ScopedAStatus AndroidKeyMintDevice::getRootOfTrustChallenge(array<uint8_t, 16>* /* challenge */) { |
| 483 | return kmError2ScopedAStatus(KM_ERROR_UNIMPLEMENTED); |
| 484 | } |
| 485 | |
| 486 | ScopedAStatus AndroidKeyMintDevice::getRootOfTrust(const array<uint8_t, 16>& /* challenge */, |
| 487 | vector<uint8_t>* /* rootOfTrust */) { |
| 488 | return kmError2ScopedAStatus(KM_ERROR_UNIMPLEMENTED); |
| 489 | } |
| 490 | |
| 491 | ScopedAStatus AndroidKeyMintDevice::sendRootOfTrust(const vector<uint8_t>& /* rootOfTrust */) { |
| 492 | return kmError2ScopedAStatus(KM_ERROR_UNIMPLEMENTED); |
| 493 | } |
| 494 | |
Shawn Willden | 815e896 | 2020-12-11 13:05:27 +0000 | [diff] [blame] | 495 | IKeyMintDevice* CreateKeyMintDevice(SecurityLevel securityLevel) { |
Shawn Willden | 815e896 | 2020-12-11 13:05:27 +0000 | [diff] [blame] | 496 | return ::new AndroidKeyMintDevice(securityLevel); |
| 497 | } |
| 498 | |
| 499 | } // namespace aidl::android::hardware::security::keymint |